forked from Azure/review-checklists
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchecklist_graph.sh
368 lines (351 loc) · 15.6 KB
/
checklist_graph.sh
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
#!/usr/bin/zsh
##################################################################################################
# This script downloads the latest JSON checklist in https://github.com/Azure/review-checklists
# and performs the Azure Resource Graph queries defined in the tests.
# It will output a the test results (successful/failed) for each check in the list, that can be
# then sent to a file or copy/pasted in a different tool (like the sample spreadsheet in this
# repository).
# The script can take some arguments:
# -b/--base-url: URL where to download the JSON file from. Defaults to https://raw.githubusercontent.com/Azure/review-checklists/main/samples/
# -t/--technology: technology to verify. Accepted values lz, aks and avd. Defaults to aks
# -c/--category: category to filter the tests. Accepted values: 0 to (number_of_categories-1)
# -l/--list-categories: instead of running Azure Graph queries, it only displays the categories in the file and its corresponding indexes
# -s/--show-text: shows the check text (good for troubleshooting, not so good to copy/paste to Excel)
# -n/--no-empty: does not show checks without an associated query
# -o/--output: can be either console or json
# -f/--format: can be either short (syntax "<resourceGroup>/<name>") or long (syntax "<id>")
# --file: custom JSON file (otherwise the latest checklist is downloaded from Github)
# -mg/--management-group: per default the Azure Resource Graph queries are scoped to the current subscription, you can enlarge the scope to a given management group
# -d/--debug: increase verbosity
#
# Example:
# ./checklist_graph.sh -l -t=aks
# ./checklist_graph.sh -c=0 -t=aks
#
# Jose Moreno, October 2021
###################################################################################################
# Defaults
base_url="https://raw.githubusercontent.com/Azure/review-checklists/main/checklists/"
technology="aks"
category_id=""
debug="no"
help="no"
error_file=/tmp/error.json
list_categories=no
check_text=no
no_empty=no
output=console
format=short
mg=""
filename=""
# Color format variables
normal="\e[0m"
underline="\e[4m"
red="\e[31m"
green="\e[32m"
yellow="\e[33m"
blue="\e[34m"
bold="\e[1m"
# Parse arguments
for i in "$@"
do
case $i in
-b=*|--base-url=*)
base_url="${i#*=}"
shift # past argument=value
;;
-t=*|--technology=*)
technology="${i#*=}"
shift # past argument=value
;;
-l*|--list-categories*)
list_categories="yes"
shift # past argument with no value
;;
-c=*|--category=*)
category_id="${i#*=}"
shift # past argument=value
;;
-f=*|--format=*)
format="${i#*=}"
shift # past argument=value
;;
-s*|--show-text*)
check_text="yes"
shift # past argument with no value
;;
-n*|--no-empty*)
no_empty="yes"
shift # past argument with no value
;;
-o=*|--output=*)
output="${i#*=}"
shift # past argument=value
;;
-mg=*|--management-group=*)
mg="${i#*=}"
shift # past argument=value
;;
--file=*)
filename="${i#*=}"
shift # past argument=value
;;
-d*|--debug*)
debug="yes"
shift # past argument with no value
;;
--help|-h)
help=yes
shift # past argument with no value
;;
*)
# unknown option
;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional parameters
# Print help message
if [[ "$help" == "yes" ]]
then
script_name="$0"
echo "Please run this script as:
$script_name [--technology=lz|aks|avd] [--category=<category_id>] [--management-group=<mgmt_group>] [--base-url=<base_url>] [--show-text] [--no-empty] [--debug]
$script_name [--file=<path_to_json_file>] [--category=<category_id>] [--management-group=<mgmt_group>] [--base-url=<base_url>] [--show-text] [--no-empty] [--debug]
$script_name [--list-categories] [--base-url=<base_url>] [--technology=lz|aks|avd] [--debug]"
exit
fi
# If outputing to JSON, we dont want output that breaks the JSON syntax
if [[ "$output" == "json" ]]; then
check_text=no
no_empty=yes
if [[ "$debug" == "yes" ]]; then echo "DEBUG: Output is $output, setting --check-text=${check_text} and --no-empty=${no_empty}..."; fi
fi
# If a Management Group was specified, use it in the az graph command
if [[ -z "$mg" ]]; then
mg_option=""
else
mg_option="-m $mg"
if [[ "$debug" == "yes" ]]; then echo "DEBUG: Setting management group $mg as scope for the az graph query commands..."; fi
fi
# Download checklist from Github or upload from file
if [[ -n "$filename" ]]
then
if [[ -e "$filename" ]]
then
if [[ "$debug" == "yes" ]]; then echo "DEBUG: Getting JSON content from filename $filename..."; fi
checklist_json=$(cat "$filename")
else
echo "ERROR: File $filename could not be found"
exit
fi
else
# Set URL and download checklist from base URL
checklist_url="${base_url}${technology}_checklist.en.json"
if [[ "$debug" == "yes" ]]; then echo "DEBUG: Getting checklist from $checklist_url..."; fi
checklist_json=$(curl -s "$checklist_url")
fi
# If in list_categories mode, just get the categories part:
if [[ "$list_categories" == "yes" ]]
then
i=0
category_list=$(echo $checklist_json | jq -r '.categories[].name')
while IFS= read -r category; do
echo "${i}: - $category"
i=$(($i+1))
done <<< "$category_list"
exit 0
fi
# If there is a category specified, get the name
if [[ -n "$category_id" ]]
then
category_name=$(echo $checklist_json | jq -r '.categories['$category_id'].name')
if [[ "$debug" == "yes" ]]; then echo "DEBUG: Performing tests for category $category_name..."; fi
else
if [[ "$debug" == "yes" ]]; then echo "DEBUG: Performing tests for all categories..."; fi
fi
# Get a list of the items
if [[ "$debug" == "yes" ]]; then echo "DEBUG: Extracting information from JSON..."; fi
if [[ -n "$category_name" ]]
then
graph_success_list=$(print -r "$checklist_json" | jq -r '.items[] | select(.category=="'$category_name'") | .graph_success')
graph_failure_list=$(print -r "$checklist_json" | jq -r '.items[] | select(.category=="'$category_name'") | .graph_failure')
category_list=$(print -r "$checklist_json" | jq -r '.items[] | select(.category=="'$category_name'") | .category')
text_list=$(print -r "$checklist_json" | jq -r '.items[] | select(.category=="'$category_name'") | .text')
guid_list=$(print -r "$checklist_json" | jq -r '.items[] | select(.category=="'$category_name'") | .guid')
if [[ -z "$text_list" ]]; then
echo "ERROR: error processing JSON file, please verify the syntax"
exit
fi
if [[ "$debug" == "yes" ]]; then echo "DEBUG: $(echo $text_list | wc -l) tests found in the checklist for category ${category_name}."; fi
else
graph_success_list=$(print -r "$checklist_json" | jq -r '.items[] | .graph_success')
graph_failure_list=$(print -r "$checklist_json" | jq -r '.items[] | .graph_failure')
category_list=$(print -r "$checklist_json" | jq -r '.items[] | .category')
text_list=$(print -r "$checklist_json" | jq -r '.items[] | .text')
guid_list=$(print -r "$checklist_json" | jq -r '.items[] | .guid')
if [[ -z "$text_list" ]]; then
echo "ERROR: error processing JSON file, please verify the syntax"
exit
fi
if [[ "$debug" == "yes" ]]; then echo "DEBUG: $(echo $text_list | wc -l) tests found in the checklist."; fi
fi
# Debug
if [[ "$debug" == "yes" ]]; then
echo "DEBUG: $(echo $graph_success_list | wc -l) graph queries for success tests found in the checklist."
# echo "$graph_success_list"
fi
if [[ "$debug" == "yes" ]]; then
echo "DEBUG: $(echo $graph_failure_list | wc -l) graph queries for failure tests found in the checklist."
# echo "$graph_failure_list"
fi
if [[ "$debug" == "yes" ]]; then
echo "DEBUG: $(echo $guid_list | wc -l) GUIDs with a defined Graph query found in the checklist"
# echo "$guid_list"
fi
# Make sure the Azure CLI extension for Azure Resource Graph is installed and updated
extension_name=resource-graph
extension_version=$(az extension show -n $extension_name --query version -o tsv 2>/dev/null)
if [[ -z "$extension_version" ]]
then
if [[ "$debug" == "yes" ]]; then echo "DEBUG: Azure CLI extension $extension_name not found, installing now..."; fi
az extension add -n $extension_name -o none 2>/dev/null
else
if [[ "$debug" == "yes" ]]; then echo "DEBUG: Azure CLI extension $extension_name found with version $extension_version, trying to upgrade..."; fi
az extension update -n $extension_name -o none 2>/dev/null
fi
extension_version=$(az extension show -n $extension_name --query version -o tsv 2>/dev/null)
if [[ "$debug" == "yes" ]]; then echo "Azure CLI extension $extension_name running with version $extension_version"; fi
# Run queries
i=0
this_category_name=""
json_output="{ \"metadata\": {\"format\": \"${format}\", \"timestamp\": \"$(date)\"}, \"checks\": ["
json_output_empty="yes"
while IFS= read -r graph_success_query; do
i=$(($i+1))
this_guid=$(echo $guid_list | head -$i | tail -1)
if [[ "$debug" == "yes" ]]; then echo "Processing check item $i, GUID '$this_guid'..."; fi
if [[ "$this_guid" == "null" ]] && [[ "$output" == "json" ]]; then
if [[ "$debug" == "yes" ]]; then echo "ERROR: GUID not defined for check number $i"; fi
fi
# Print header category if required
if [[ "$this_category_name" != "$(echo $category_list | head -$i | tail -1)" ]]
then
this_category_name=$(echo $category_list | head -$i | tail -1)
if [[ "$output" == "console" ]]; then
echo "${bold}${blue}CHECKLIST CATEGORY: ${this_category_name}${normal}"
fi
fi
# Check if there is any query
if [[ "$graph_success_query" == "null" ]]; then
if [[ "$no_empty" != "yes" ]]; then
# Print title if required
if [[ "$check_text" == "yes" ]]; then
this_text=$(echo $text_list | head -$i | tail -1)
echo "CHECKLIST ITEM: ${this_text}:"
fi
# Print output
echo "N/A"
fi
else
# Print title if required
if [[ "$check_text" == "yes" ]] && [[ "$output" == "console" ]]; then
this_text=$(echo $text_list | head -$i | tail -1)
echo "CHECKLIST ITEM: ${this_text}:"
fi
rm $error_file 2>/dev/null; touch $error_file
if [[ "$debug" == "yes" ]]; then echo "DEBUG: Running success query '$graph_success_query'..."; fi
# If format is short, the graph query command returns a single line, if format is long, it is one line per resource
if [[ "$format" == "short" ]]; then
success_result=$(az graph query -q "$graph_success_query" ${(z)mg_option} -o json 2>$error_file | jq -r '.data[] | "\(.resourceGroup)/\(.name)"' 2>>$error_file | tr '\n' ',')
if [[ -s $error_file ]]; then
success_result="Error";
if [[ "$debug" == "yes" ]]; then cat $error_file; fi
else
# Remove last comma
success_result=${success_result%?}
fi
# If no object was returned
if [[ -z "$success_result" ]]; then success_result='None'; fi
else
success_result=$(az graph query -q "$graph_success_query" ${(z)mg_option} -o tsv 2>$error_file --query 'data[].id' | sort -u)
fi
rm $error_file 2>/dev/null; touch $error_file
graph_failure_query=$(print -r "$graph_failure_list" | head -$i | tail -1)
if [[ "$debug" == "yes" ]]; then echo "DEBUG: Running failure query '$graph_failure_query'..."; fi
# If format is short, the graph query command returns a single line, if format is long, it is one line per resource
if [[ "$format" == "short" ]]; then
# NOTE: this line will give some unexpected results when running Graph queries that return subscription IDs, since those do not have a RG
failure_result=$(az graph query -q "$graph_failure_query" ${(z)mg_option} -o json 2>$error_file | jq -r '.data[] | "\(.resourceGroup)/\(.name)"' 2>>$error_file | tr '\n' ',')
if [[ -s $error_file ]]; then
failure_result="Error"
if [[ "$debug" == "yes" ]]; then cat $error_file; fi
else
# Remove last comma
failure_result=${failure_result%?}
fi
# If no object was returned
if [[ -z "$failure_result" ]]; then failure_result='None'; fi
else
# If format is long, the result should be a list of IDs
failure_result=$(az graph query -q "$graph_failure_query" ${(z)mg_option} -o tsv 2>$error_file --query 'data[].id' | sort -u)
fi
# Print output in color format
if [[ "$output" == "console" ]] && [[ "$format" == "short" ]]; then
if [[ "$success_result" == "None" ]]; then
success_color=$yellow
else
success_color=$green;
fi
if [[ "$failure_result" == "None" ]]; then
failure_color=$green
else
failure_color=$red;
fi
echo "Success: ${success_color}${success_result}${normal}. Fail: ${failure_color}${failure_result}${normal}"
fi
# Append JSON element, depending on the chosen format short/long
if [[ "$format" == "short" ]]; then
# First, add a comma if this wasnt the first element
if [[ "$json_output_empty" == "yes" ]]; then
json_output_empty="no"
else
json_output+=", "
fi
json_output+="{\"guid\": \"$this_guid\", \"success\": \"$success_result\", \"failure\": \"$failure_result\"}"
else
# If long format, we append a line for each found compliant/non-compliant resource
while IFS= read -r resource_id
do
if [[ -n "$resource_id" ]]; then
# First, add a comma if this wasnt the first element
if [[ "$json_output_empty" == "yes" ]]; then
json_output_empty="no"
else
json_output+=", "
fi
# Add an item per compliant resource
json_output+="{\"guid\": \"$this_guid\", \"result\": \"success\", \"id\": \"$resource_id\"}"
fi
done < <(printf '%s\n' "$success_result")
while IFS= read -r resource_id
do
if [[ -n "$resource_id" ]]; then
# First, add a comma if this wasnt the first element
if [[ "$json_output_empty" == "yes" ]]; then
json_output_empty="no"
else
json_output+=", "
fi
# Add an item per non-compliant resource
json_output+="{\"guid\": \"$this_guid\", \"result\": \"fail\", \"id\": \"$resource_id\"}"
fi
done < <(printf '%s\n' "$failure_result")
fi
fi
done <<< "$graph_success_list"
# Close JSON format
json_output+="]}"
# If output is JSON, print check_results variable
if [[ "$output" == "json" ]]; then
echo "$json_output"
fi