-
Notifications
You must be signed in to change notification settings - Fork 19
/
benchmark.sh
executable file
·172 lines (147 loc) · 4.2 KB
/
benchmark.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
#!/bin/bash
# Universal cloud provider provisioning calculator
# Based on the cloud provider, downloads the necessary scripts
# to perform a sizing calculation.
base_url=https://raw.githubusercontent.com/CrowdStrike/Cloud-Benchmark/main
# Usage message
usage() {
echo """
Usage: $0 [aws|azure|gcp]...
More than one cloud provider can be specified.
If no cloud provider is specified, the script will attempt to detect the provider.
----------------------------------------------------------------------------------
The script recognizes the following environment variables:
- AWS_ASSUME_ROLE_NAME: The name of the AWS role to assume (optional)"""
}
# Check if the system has Python3 and pip installed
check_python3() {
if ! type python3 >/dev/null 2>&1; then
echo "Python3 not found. Please install Python3 and try again."
exit 1
fi
if ! type pip3 >/dev/null 2>&1; then
echo "Pip not found. Please install pip and try again."
exit 1
fi
}
# Ensures the provided cloud provider arg is valid
is_valid_cloud() {
local cloud="$1"
local lower_cloud
lower_cloud=$(echo "$cloud" | tr '[:upper:]' '[:lower:]')
case "$lower_cloud" in
aws)
echo "AWS"
return 0
;;
azure)
echo "Azure"
return 0
;;
gcp)
echo "GCP"
return 0
;;
*)
return 1
;;
esac
}
# Calls the python script for the specified cloud provider with the
# appropriate arguments
call_benchmark_script() {
local cloud="$1"
local file="$2"
local args=()
case "$cloud" in
AWS)
[[ -n $AWS_ASSUME_ROLE_NAME ]] && args+=("-r" "$AWS_ASSUME_ROLE_NAME")
# Below is how we would pass in additional arguments if needed
# [[ -n $AWS_EXAMPLE ]] && args+=("-t" "$AWS_EXAMPLE")
;;
Azure)
;;
GCP)
;;
*)
echo "Invalid cloud provider specified: $cloud"
usage
exit 1
;;
esac
python3 "${file}" "${args[@]}"
}
audit() {
CLOUD="$1"
echo "Working in cloud: ${CLOUD}"
cloud=$(echo "$CLOUD" | tr '[:upper:]' '[:lower:]')
curl -s -o requirements.txt "${base_url}/${CLOUD}/requirements.txt"
echo "Installing python dependencies for communicating with ${CLOUD} into (~/cloud-benchmark)"
python3 -m pip install --disable-pip-version-check -qq -r requirements.txt
file="${cloud}_cspm_benchmark.py"
curl -s -o "${file}" "${base_url}/${CLOUD}/${file}"
call_benchmark_script "$CLOUD" "${file}"
}
check_python3
python3 -m venv ./cloud-benchmark
pushd ./cloud-benchmark >/dev/null || exit
# shellcheck source=/dev/null
source ./bin/activate
# MAIN ROUTINE
found_provider=false
# If arguments are provided, audit the specified providers
for arg in "$@"; do
result=$(is_valid_cloud "$arg")
# shellcheck disable=SC2181
if [ $? -eq 0 ]; then
audit "$result"
found_provider=true
else
echo "Invalid cloud provider specified: $arg"
# Exit only if found_provider is false. This means that if the user
# specifies a valid cloud provider, but also an invalid one, we will
# still run the audit for the valid provider.
if [ "$found_provider" = false ]; then
usage
popd >/dev/null && exit 1
fi
fi
done
# If no arguments provided, auto-detect the available cloud providers
if [ $# -eq 0 ]; then
echo "Determining cloud provider..."
if type aws >/dev/null 2>&1; then
audit "AWS"
found_provider=true
fi
if type az >/dev/null 2>&1; then
audit "Azure"
found_provider=true
fi
if type gcloud >/dev/null 2>&1; then
audit "GCP"
found_provider=true
fi
fi
if [ "$found_provider" = false ]; then
echo "No supported cloud provider found."
usage
popd >/dev/null && exit 1
fi
popd >/dev/null || exit
deactivate
echo "Type following command to export cloud counts:"
echo "cat ./cloud-benchmark/*benchmark.csv"
# END
#
# -''--.
# _`> `\.-'<
# _.' _ '._
# .' _.=' '=._ '.
# >_ / /_\ /_\ \ _< - jgs
# / ( \o/\\o/ ) \
# >._\ .-,_)-. /_.<
# /__/ \__\
# '---' E=mc^2
#
#