-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidatecicsbundle
executable file
·247 lines (218 loc) · 7.06 KB
/
validatecicsbundle
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#!/bin/bash
#############################################################################################
# Script to validate contents of a CICS bundle against a set of rules
#
# Licensed Materials - Property of IBM.
# (c) Copyright IBM Corporation 2016. All Rights Reserved.
# US Government Users Restricted Rights
# Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corporation
#############################################################################################
show_usage () {
echo -e "Usage:\t"$(basename "$0") "[-hv] [-f FILEPATTERN -x XPATH -e REGEX] [-r FILE] DIRECTORY"
}
show_help () {
show_usage
echo
echo "Validate the CICS bundle specified by DIRECTORY against a rule specified by the -f -x -e options"
echo "and/or the rules specified in FILE. The script return code is set to 0 if all rules validate to"
echo "true, otherwise it is set to > 0."
echo
echo "Options:"
echo -e "\t-h, --help\t\t\tHelp"
echo -e "\t-v, --verbose\t\t\tVerbose messages"
echo -e "\t-f, --filepattern FILEPATTERN\tRule file pattern"
echo -e "\t-x, --xpath XPATH\t\tRule XPath to evaluate"
echo -e "\t-e, --regex REGEX\t\tRule regular expression that the XPath is required to match"
echo -e "\t-r, --rules FILE\t\tFile containing a set of rules"
echo -e "\tDIRECTORY is the CICS bundle directory to be validated."
echo
echo -e "\tFILE is a file containing a logical set of rules, one on each line. Escape characters can be used."
echo -e "\tComments starting with #, blank lines, and tab characters are ignored."
echo -e "\tEach rule in FILE should follow the format: FILEPATTERN XPATH REGEX"
echo
echo "Examples:"
echo -e "\tFor CICS bundle catalog.example.service validate that all *.urimap files contain an attribute"
echo -e "\t'name' with a value that starts with the characters EX:"
echo -e "\t"$(basename "$0") '-f "*.urimap" -x "string(//@name)" -e "^EX" -v catalog.example.service'
echo
echo -e "\tFor CICS bundle catalog.example.service validate the rules specified in file rules.txt:"
echo -e "\t"$(basename "$0") "-r rules.txt -v catalog.example.service"
echo
echo "Example rules file:"
echo -e "\t# The resource name for all URIMAPs must start with EX"
echo -e "\t*.urimap string(//@name) ^EX"
echo
echo -e "\t# For URIMAPs the scheme attribute must be HTTP"
echo -e "\t*.urimap string(//@scheme) HTTP"
echo
echo -e "\t# For URIMAPs the authenticate attribute must be NO"
echo -e "\t*.urimap string(//@authenticate) NO"
echo
echo -e "\t# For WEBSERVICEs the pipeline attribute must be PIPE01"
echo -e "\t*.webservice string(//@pipeline) PIPE01"
echo
echo -e "\t# For WEBSERVICEs the wsdlfile attribute must be present"
echo -e "\t*.webservice boolean(//*[@validation]) true"
echo
echo -e "\t# The JVMSERVER resource must not be present"
echo -e "\tcics.xml boolean(//*[local-name()='define'][@type=\"http://www.ibm.com/xmlns/prod/cics/bundle/JVMSERVER\"]) false"
}
# Function to validate the XPATH value against a regular expression
validate_xpath () {
local XPATH=$1
local REGEX=$2
local FILE=$3
local RULE=$4
local VALUE=$(xmllint --xpath "${XPATH}" "${FILE}")
local RC=$?
if [[ ${RC} != 0 ]] ; then
echo "xmllint gave return code ${RC}"
exit 1
fi
if [[ -z ${VALUE} ]]; then
RC=1
else
# Compare result of XPATH with expected regex
echo ${VALUE} | $(sed -n '/'${REGEX}'/!{q2}')
RC=$?
fi
if [[ ${RC} == 1 ]] ; then
echo "File:${FILE} Rule ${RULE}: XPath:${XPATH} failed"
elif [[ ${RC} == 2 ]] ; then
echo "File:${FILE} Rule ${RULE}: XPath:${XPATH}=${VALUE} did not match regex ${REGEX}"
elif [[ $VERBOSE == 1 ]] ; then
echo "File:${FILE} Rule ${RULE}: XPath:${XPATH}=${VALUE} matched regex ${REGEX}"
fi
exit ${RC}
}
# Function to validate a specified resource file
validate_rules_for_file () {
local TOTALRC=0
local RC=0
local FILE=${1}
local FILEBASENAME=$(basename "$FILE")
for (( i = 0 ; (i * 3) < ${#VALIDATIONS[@]} ; i += 1 )) ; do
case "$FILEBASENAME" in (${VALIDATIONS[${i}*3]})
MESSAGE=$(validate_xpath "${VALIDATIONS[${i}*3+1]}" "${VALIDATIONS[${i}*3+2]}" "${FILE}" "$i")
RC=$?
if [[ ! -z ${MESSAGE} ]] ; then
echo -n ${MESSAGE}"\n"
fi
TOTALRC=$((TOTALRC + RC)) ;;
esac
done
exit ${TOTALRC}
}
# Function to validate a CICS bundle
validate_cics_bundle() {
# For each file in the bundle that is not hidden, validate it against the rules
if [[ $VERBOSE == 1 ]] ; then
echo "Validating files in directory ${DIRECTORY} against rules:"
fi
cd ${DIRECTORY}
for FILE in $(find . -type f -not -path '*/\.*' -name "*") ; do
MESSAGE=$(validate_rules_for_file "${FILE}")
RC=$?
if [[ ! -z ${MESSAGE} ]] ; then
echo -n -e "${MESSAGE}"
fi
TOTALRC=$((TOTALRC + RC))
done
}
# Process script long options by transforming them to the short option equivalent
for arg in "$@" ; do
shift
case "$arg" in
"--filepattern")
set -- "$@" "-f" ;;
"--xpath")
set -- "$@" "-x" ;;
"--regex")
set -- "$@" "-e" ;;
"--rules")
set -- "$@" "-r" ;;
"--verbose")
set -- "$@" "-v" ;;
"--help")
set -- "$@" "-h" ;;
*)
set -- "$@" "$arg"
esac
done
# Process script short options
VERBOSE=0
while getopts ":f:x:e:r:vh" opt ; do
case "$opt" in
f)
FILEPATTERN=${OPTARG} ;;
x)
XPATH=${OPTARG} ;;
e)
REGEX=${OPTARG} ;;
r)
RULES=${OPTARG} ;;
v)
VERBOSE=1 ;;
h)
show_help
exit 0 ;;
esac
done
shift $((OPTIND-1))
# The remainder of the command line is the CICS bundle directory
DIRECTORY=$@
TOTALRC=0
# Validate directory is a CICS bundle
if [[ -z "${DIRECTORY}" ]] ; then
echo "DIRECTORY was not specified"
show_usage
exit 1
elif [[ ! -d "${DIRECTORY}" ]] ; then
echo "${DIRECTORY} is not a directory"
show_usage
exit 1
elif [[ ! -f "${DIRECTORY}/META-INF/cics.xml" ]] ; then
echo "${DIRECTORY} directory is not a CICS bundle"
show_usage
exit 1
fi
# Validate rules file
if [[ ! -z ${RULES} ]] ; then
if [[ ! -f ${RULES} ]] ; then
echo "${RULES} is not a file, or does not exist"
show_usage
exit 1
fi
# Load the rules file, strip blank lines, and strip character "#" and trailing characters
RULES_CONTENTS=$(sed -e 's/[[:space:]]*#.*// ; /^[[:space:]]*$/d ; s/\t/ /g' "${RULES}")
fi
# Pre-pend the rule specified on the comment line
if [[ ! -z ${FILEPATTERN} ]] && [[ ! -z ${XPATH} ]] && [[ ! -z ${REGEX} ]] ; then
NEWLINE=$'\n'
RULES_CONTENTS="${FILEPATTERN} ${XPATH} ${REGEX}${NEWLINE}${RULES_CONTENTS}"
fi
# For each rule, extract 1st word as file pattern, 2nd word as xpath, and remaining words as regex
INDEX=0
while read FILEPATTERN XPATH REGEX ; do
if [[ -z "${FILEPATTERN}" ]] || [[ -z "${XPATH}" ]] || [[ -z "${REGEX}" ]] ; then
continue
fi
VALIDATIONS[(${INDEX}*3)]="$FILEPATTERN"
VALIDATIONS[(${INDEX}*3)+1]="$XPATH"
VALIDATIONS[(${INDEX}*3)+2]="$REGEX"
if [[ $VERBOSE == 1 ]] ; then
echo "Rule ${INDEX}: File pattern:${FILEPATTERN} XPath:${XPATH} regex:${REGEX}"
fi
INDEX=$((INDEX + 1))
done <<< "$RULES_CONTENTS"
if [[ ${INDEX} = 0 ]] ; then
if [[ $VERBOSE == 1 ]] ; then
echo "No rules found"
fi
else
validate_cics_bundle
fi
if [[ $VERBOSE == 1 ]] ; then
echo "Exiting with RC=${TOTALRC}"
fi
exit ${TOTALRC}