-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathbenchmark.sh
More file actions
executable file
·239 lines (211 loc) · 7.87 KB
/
benchmark.sh
File metadata and controls
executable file
·239 lines (211 loc) · 7.87 KB
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
#!/bin/bash
# Universal cloud provider provisioning calculator
# Based on the cloud provider, downloads the necessary scripts
# to perform a sizing calculation.
RELEASE_VERSION="v1.0.0"
base_url="https://raw.githubusercontent.com/CrowdStrike/cloud-resource-estimator/${RELEASE_VERSION}"
# Usage message
usage() {
echo """
Usage: $0 [OPTIONS] [aws|azure|gcp]...
The script recognizes the following environment variables:
AWS:
- AWS_ASSUME_ROLE_NAME: The name of the AWS role to assume (optional)
- AWS_REGIONS: The name of the AWS Region to target or a comma-delimited list of AWS Regions to target (optional)
- AWS_THREADS: Number of worker threads for parallel processing (default: 5)
- AWS_BATCH_SIZE: Number of accounts to process per batch (default: 20)
- AWS_BATCH_DELAY: Delay in seconds between batches (default: 30)
- AWS_API_DELAY: Delay in seconds between API calls (default: 0.1)
- AWS_MAX_RETRIES: Maximum retry attempts for failed operations (default: 5)
- AWS_OPERATION_TIMEOUT: Timeout in seconds for individual operations (default: 300)
- AWS_RESUME_FILE: File to store/resume progress (default: aws_benchmark_progress.json)
- AWS_SKIP_ACCOUNTS: Comma-separated list of account IDs to skip
- AWS_DRY_RUN: Set to 'true' to show what would be processed without making API calls
Example for large organizations (200+ accounts):
export AWS_THREADS=3
export AWS_BATCH_SIZE=12
export AWS_BATCH_DELAY=45
export AWS_API_DELAY=0.15
Azure:
- AZURE_SKIP_SUBSCRIPTIONS: Comma-separated list of subscription IDs to exclude from scanning
- AZURE_INCLUDE_SUBSCRIPTIONS: Comma-separated list of subscription IDs to scan (exclusive filter)
Note: AZURE_INCLUDE_SUBSCRIPTIONS takes full precedence. If set, AZURE_SKIP_SUBSCRIPTIONS is ignored.
Example (use one or the other, not both):
export AZURE_SKIP_SUBSCRIPTIONS="sub-id-1,sub-id-2"
OR
export AZURE_INCLUDE_SUBSCRIPTIONS="sub-id-3,sub-id-4"
"""
}
# 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")
[[ -n $AWS_REGIONS ]] && args+=("-R" "$AWS_REGIONS")
[[ -n $AWS_THREADS ]] && args+=("--threads" "$AWS_THREADS")
[[ -n $AWS_BATCH_SIZE ]] && args+=("--batch-size" "$AWS_BATCH_SIZE")
[[ -n $AWS_BATCH_DELAY ]] && args+=("--batch-delay" "$AWS_BATCH_DELAY")
[[ -n $AWS_API_DELAY ]] && args+=("--api-delay" "$AWS_API_DELAY")
[[ -n $AWS_MAX_RETRIES ]] && args+=("--max-retries" "$AWS_MAX_RETRIES")
[[ -n $AWS_OPERATION_TIMEOUT ]] && args+=("--operation-timeout" "$AWS_OPERATION_TIMEOUT")
[[ -n $AWS_RESUME_FILE ]] && args+=("--resume-file" "$AWS_RESUME_FILE")
[[ -n $AWS_SKIP_ACCOUNTS ]] && args+=("--skip-accounts" "$AWS_SKIP_ACCOUNTS")
[[ -n $AWS_DRY_RUN ]] && [[ $AWS_DRY_RUN == "true" ]] && args+=("--dry-run")
;;
Azure)
[[ -n $AZURE_SKIP_SUBSCRIPTIONS ]] && args+=("--skip-subscriptions" "$AZURE_SKIP_SUBSCRIPTIONS")
[[ -n $AZURE_INCLUDE_SUBSCRIPTIONS ]] && args+=("--include-subscriptions" "$AZURE_INCLUDE_SUBSCRIPTIONS")
;;
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:]')
case "$CLOUD" in
AWS)
# Use local AWS script if available
if [ -f "../AWS/aws_cspm_benchmark.py" ]; then
echo "Using local AWS CSPM benchmark script..."
file="../AWS/aws_cspm_benchmark.py"
# Install requirements from local AWS directory
if [ -f "../AWS/requirements.txt" ]; then
python3 -m pip install --disable-pip-version-check -qq -r "../AWS/requirements.txt"
else
echo "AWS requirements.txt not found locally, downloading from remote"
curl -s -o requirements.txt "${base_url}/${CLOUD}/requirements.txt"
python3 -m pip install --disable-pip-version-check -qq -r requirements.txt
fi
else
echo "Local AWS script not found, downloading from remote"
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}"
fi
;;
Azure|GCP)
# Use remote scripts for Azure and GCP (unchanged behavior)
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}"
;;
*)
echo "Unsupported cloud provider: $CLOUD"
exit 1
;;
esac
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
#
#