forked from webpwnized/gcp-audit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility-run-all-scripts.sh
executable file
·66 lines (56 loc) · 1.78 KB
/
utility-run-all-scripts.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
#!/bin/bash
PROJECT_IDS="";
print_help(){
echo "Usage: $0 [OPTION]..."
echo "Run all CIS audit scripts for each project."
echo "Example: $0 -p project-12345 project-54321"
echo
echo "Options:"
echo " -p, --project projects to be audited; multiple projects are allowed; if no project provided, all projects will be audited"
echo " -h, --help display this help and exit"
}
for arg in "$@"; do
shift
case "$arg" in
"--help") set -- "$@" "-h" ;;
"--project") set -- "$@" "-p" ;;
*) set -- "$@" "$arg"
esac
done
while getopts "hdp:" option
do
case "${option}"
in
p)
PROJECT_IDS=${OPTARG};;
h)
print_help
exit 0;;
esac;
done;
declare -a commands=("jq" "gcloud")
for cmd in "${commands[@]}"; do
if ! command -v $cmd &> /dev/null
then
echo "$cmd could not be found on this host and is required to run the script. Please install the missing tool and try again."
exit 1
fi
done;
if [[ $PROJECT_IDS == "" ]]; then
declare PROJECT_IDS=$(gcloud projects list --format="flattened(PROJECT_ID)" | grep project_id | cut -d " " -f 2);
fi;
FILENAME_PATTERN="(cis)-([0-9]{1,2}.[0-9]{1,2}.[0-9]{1,2})-([a-zA-Z/-]*)"
AUDIT_LOG_PREFIX=audit
AUDIT_SCRIPTS=$(ls | grep -E $FILENAME_PATTERN)
for PROJECT_ID in $PROJECT_IDS; do
echo "---- Starting Audit for $PROJECT_ID ----"
gcloud config set project $PROJECT_ID 2>/dev/null
AUDIT_LOG="$AUDIT_LOG_PREFIX-$PROJECT_ID.log"
for file in $AUDIT_SCRIPTS;
do
echo "Running $file"
echo $file | sed -E "s/$FILENAME_PATTERN\.(sh)/------CIS \2,\3------/" >> $AUDIT_LOG
./$file -p $PROJECT_ID >> $AUDIT_LOG
echo "-----------------------------------------" >> $AUDIT_LOG
done;
done;