-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocker-sast.sh
executable file
·296 lines (269 loc) · 8.79 KB
/
docker-sast.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
#! /usr/bin/env bash
# TODO: fix these
# shellcheck disable=SC2164
# shellcheck disable=SC2006
# keep exit values after pipe: this makes it so the build step will correctly exit with error if one of the tests fails
set -o pipefail
shopt -s globstar
exit_code=0
target="/sast-files"
# Parse arguments
while :
do
case "$1" in
--help)
echo "Usage:"
echo "positional arguments:"
echo
echo "--target TARGET: the target to run on. SAST-scan will automatically run recursively on folders. Will default to /sast-files if no target is set."
echo
echo "optional arguments:"
echo
echo "--help: print usage and exit"
echo "--context CONTEXT where this sast scan will be executed (options: commit-hook, cloudbuild)"
exit 0
;;
--target)
target="$2"
shift 2
;;
--context)
context="$2"
shift 2
;;
-*)
echo "Error: Unknown argument: $1" >&2
echo "Use --help for possible arguments"
exit 1
;;
*)
break
;;
esac
done
# Check if target is set
[ -z "$target" ] && echo "target not set" && exit 1
# Execute recursively on folders
if [[ -d "$target" ]]; then
target_type="directory"
directory_copy="/directory"
rm -rf "/directory"
cp -r "$target" "$directory_copy"
cd "$directory_copy" && target=.
elif [[ -f "$target" ]]; then
target_type="file"
else
echo "target $target does not exist" && exit 1
fi
# Copy sast-config folder
if [[ $target_type == "directory" && -d "$target/sast-config" ]]; then
shopt -s dotglob
mv "$target"/sast-config/* .
shopt -u dotglob
fi
# Read sast-config file (.sast by default)
if [[ -f ".sast" ]]; then
config_file=".sast"
elif [[ -f ".sast-config" ]]; then
config_file=".sast-config"
fi
if [[ -n "$config_file" ]]; then
# Add newline char to end of file to make sure it has at least one
echo "" >> "$config_file"
while IFS= read -r line; do
# Loop over words
for word in $line; do
# Append every word as an argument
[[ "$word" == "--no-shellcheck" ]] && no_shellcheck=true
[[ "$word" == "--no-jsonlint" ]] && no_jsonlint=true
[[ "$word" == "--no-yamllint" ]] && no_yamllint=true
[[ "$word" == "--no-trufflehog" ]] && no_trufflehog=true
[[ "$word" == "--no-bandit" ]] && no_bandit=true
[[ "$word" == "--no-flake8" ]] && no_flake8=true
[[ "$word" == "--no-eslint" ]] && no_eslint=true
done
done < "$config_file"
fi
if [[ -n ${context+x} ]]; then
if [[ "$context" == "pre-commit" ]]; then
no_trufflehog=true
elif [[ "$context" == "post-commit" ]]; then
no_shellcheck=true
no_jsonlint=true
no_yamllint=true
no_bandit=true
no_flake8=true
no_eslint=true
elif [[ "$context" == "cloudbuild" ]]; then
:
fi
fi
# Hide node_modules
if [[ -d "$target/node_modules" ]]; then
printf "Hide node_modules temporarily\n"
mv "$target"/node_modules "$target"/.node_modules
else
echo "No node_modules found"
fi
########################## ShellCheck ######################################
# SAST will look for .shellcheck files
if [[ -z "$no_shellcheck" ]]; then
# If config file is found, parse arguments
if [[ -f ".shellcheck" ]]; then
# Add newline char to end of file to make sure it has at least one
echo "" >> ".shellcheck"
# Loop over lines
while IFS= read -r line; do
# Loop over words
for word in $line; do
# Append every word as an argument
shellcheck_args=( "${shellcheck_args[@]}" "$word" )
done
done < ".shellcheck"
fi
printf ">> shellcheck...\n";
if [[ $target_type == "directory" ]]; then
for f in "$target"/**/*.sh; do
# if glob does not match, stop execution
[[ -e "$f" ]] || continue
eval shellcheck "${f}" --shell=bash "${shellcheck_args[@]/#}" || exit_code=1
done
elif [[ "${target: -3}" == ".sh" ]]; then
shellcheck "$target" || exit_code=1
fi
else
echo "Skipping shellcheck..."
fi
########################## Yaml lint ######################################
# Yamllint looks for .yamllint, .yamllint.yaml and .yamllint.yml config files by default
if [[ -f ".yamllint" ]]; then
yamllint_config=".yamllint"
elif [[ -f ".yamllint.yaml" ]]; then
yamllint_config=".yamllint.yaml"
elif [[ -f ".yamllint.yml" ]]; then
yamllint_config=".yamllint.yml"
fi
if [[ -n $yamllint_config ]]; then
yamllint_config_arg="-c${yamllint_config}"
else
yamllint_config_arg="-d {extends: default, ignore: .node_modules, rules: {line-length: {max: 120}}}"
fi
if [[ -z "$no_yamllint" ]]; then
printf ">> yamllint...\n"
if [[ $target_type == "directory" || "${target: -5}" == ".yaml" ]]; then
yamllint "$target" "${yamllint_config_arg}" || exit_code=1
fi
else
echo "Skipping yamllint..."
fi
########################## JSONLint ######################################
# Custom jsonlint only checks if json is valid so no configuration is possible
if [[ -z "$no_jsonlint" ]]; then
printf ">> jsonlint...\n"
if [[ $target_type == "directory" ]]; then
for f in "$target"/**/*.json; do
# if glob does not match, stop execution
[[ -e "$f" ]] || continue
python /usr/local/bin/jsonlint.py "$f" > /dev/null 2>&1 || echo -e "\e[4m$f\e[0m"
python /usr/local/bin/jsonlint.py "$f" || exit_code=1
done
elif [[ "${target: -5}" == ".json" ]]; then
python /usr/local/bin/jsonlint.py "$target" > /dev/null 2>&1 || echo -e "\e[4m$target\e[0m"
python /usr/local/bin/jsonlint.py "$target" || exit_code=1
fi
else
echo "Skipping jsonlint..."
fi
########################## Trufflehog ####################################
# SAST will look for a .trufflehog file
if [[ -z "$no_trufflehog" && $target_type == "directory" && -a "$target/.git" ]]; then
echo ">> Recursively checking trufflehog:"
while IFS=$'\n' read -r d
do
cd "$d"
# If config file is found, parse arguments
if [[ ! -f ".trufflehog" ]]; then
cp /usr/local/etc/thexclude.txt thexclude.txt
cp /usr/local/etc/.trufflehog .trufflehog
fi
# Add newline char to end of file to make sure it has at least one
echo "" >> ".trufflehog"
# Loop over lines
while IFS= read -r line
do
# Loop over words
for word in $line; do
# Append every word as an argument
trufflehog_args=( "${trufflehog_args[@]}" "$word" )
done
done < ".trufflehog"
git_url=$(git config --get remote.origin.url)
if [ -z "$git_url" ]
then
git_url="local repository"
fi
printf ">> running trufflehog on %s in %s ...\n" "$git_url" "$d"
thrules=thrules.json
if [[ ! -f "$thrules" ]]; then
cp /usr/local/etc/thrules.json thrules.json
fi
eval python3 /usr/local/bin/truffleHog.py --regex --cleanup --max_depth=1 "${trufflehog_args[@]/#}" "${target}" --rules thrules.json || exit_code=1
cd "$target"
done< <(find . -name .git -type d -exec dirname {} \;)
else
echo "Skipping trufflehog..."
fi
############################# Bandit #####################################
# Bandit looks for .bandit config files by default
# installing bandit through pip3 instead of pip causes -q (quiet) to fail
if [[ -z "$no_bandit" ]]; then
printf ">> bandit...\n"
if [[ $target_type == "directory" ]]; then
if [[ ! -f ".bandit" ]]; then
bandit -r -q -l -x "$target"/.node_modules -s B105 "$target"|| exit_code=1
else
bandit -r -q -l "$target" || exit_code=1
fi
elif [[ "${target: -3}" == ".py" ]]; then
bandit -q -l "$target" || exit_code=1
fi
else
echo "Skipping bandit..."
fi
############################# Flake8 #####################################
# Flake8 looks for setup.cfg, tox.ini and .flake8 files by default
if [[ -z "$no_flake8" ]]; then
printf ">> flake8...\n"
if [[ $target_type == "directory" ]]; then
if [[ ! -f ".flake8" && ! -f "tox.ini" && ! -f "setup.cfg" ]]; then
flake8 --max-complexity=10 --ignore=E203,W503 --max-line-length=139 "$target" --exclude .node_modules || exit_code=1
else
flake8 "$target" || exit_code=1
fi
elif [[ "${target: -3}" == ".py" ]];then
flake8 --max-complexity=10 --ignore=E203,W503 --max-line-length=139 "$target" || exit_code=1
fi
else
echo "Skipping flake8..."
fi
if [[ -d "$target/.node_modules" ]]; then
printf "Unhide node_modules\n"
mv "$target"/.node_modules "$target"/node_modules
fi
############################# ESLint #####################################
if [[ -z "$no_eslint" ]]; then
printf ">> eslint...\n"
if [[ "$target_type" == "directory" ]]; then
if [[ `find "$target" -type f -name "*.ts" -not -path "$target/node_modules/*"` && -d "$target"/node_modules ]]; then
esconf=eslintrc.json
if [[ ! -f "$esconf" ]]; then
cp /usr/local/etc/eslintrc.json eslintrc.json
fi
eslint . -c eslintrc.json --ext .ts || exit_code=1
fi
fi
else
echo "Skipping eslint..."
fi
[[ $target_type == "directory" ]] && cd /
exit $exit_code