Skip to content

Commit 03a86aa

Browse files
Jeffrey Zhangcjinaws
authored andcommitted
Added flag to change go version used for scan
cr: https://code.amazon.com/reviews/CR-97104150
1 parent 3377043 commit 03a86aa

File tree

2 files changed

+49
-41
lines changed

2 files changed

+49
-41
lines changed

Tools/src/static_analysis.sh

Lines changed: 48 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@
1010
# 5. This script should be ran through the makefile using `make analyze` to use the recommended flags.
1111
# 6. Using -o=dir or -o=file will create formated markdown files for easier debugging
1212
# 7. This script is designed to scan packages not files. To scan ./agent/agent/agent.go, pass in ./agent/agent/
13-
#
13+
# 8. The order of flags matters! For example, using ./static_analysis.sh -I -s=gosec will install all
14+
# dependencies but then only scan with gosec. While ./static_analysis.sh -s=gosec -I will tell the intaller to only install gosec
15+
#
1416
# Example commands:
1517
# 1) ./static_analysis.sh -d -o=file -n="results.md" -s="gosec govulncheck"
1618
# -> [-d] Uses default package locations
@@ -72,37 +74,38 @@ PATH=${PATH}:$(go env GOPATH)/bin
7274

7375
# Color and text constants for shell. Checks that we are not inside a dumb terminal which has no color features
7476
if [[ ${TERM} != "dumb" ]]; then
75-
RED=$(tput setaf 1)
76-
GREEN=$(tput setaf 2)
77-
YELLOW=$(tput setaf 3)
77+
RED=$(tput setaf 1)
78+
GREEN=$(tput setaf 2)
79+
YELLOW=$(tput setaf 3)
7880
BLUE=$(tput setaf 4)
7981
MAGENTA=$(tput setaf 5)
8082
CYAN=$(tput setaf 6)
81-
RESET_COLOR=$(tput init)
83+
RESET_COLOR=$(tput init)
8284
BOLD=$(tput bold)
8385
UNDERLINE=$(tput smul)
8486
NORMAL=$(tput sgr0)
8587
fi
8688

8789
# Help Message
8890
help="
89-
${YELLOW}This script uses multiple static security analysis libraries to scan and find known CVEs and vulnerabilities within golang packages${RESET_COLOR}\n
91+
${YELLOW}This script uses multiple static security analysis libraries to scan and find known CVEs and vulnerabilities within Golang packages${RESET_COLOR}\n
9092
${BOLD}${UNDERLINE}Usage${NORMAL}:
9193
\t$(basename $0) [options] [files...]
9294
${BOLD}${UNDERLINE}Options${NORMAL}:
93-
\t-s --scanner=\"arg...\" List of security scanners that will be used currently there ${SCANNERS} are avalible
95+
\t-s --scanner=\"arg...\" List of security scanners that will be used currently there ${SCANNERS} are available
9496
\t --[scanner]=\"flags\" Pass in additional command flags to scanner
9597
\t-d --default Runs scans on specified default locations
9698
\t-f --fail Prevents script from exiting after first failure
9799
\t-r --rel=<path> Set the path all commands are run relative to
98-
\t-i --install Installs latest scanners automatically if missing
99-
\t-I Installs scanners depenedencies and exits
100+
\t-i --install Installs latest scanners automatically if missing just before a scan start
101+
\t-I Installs scanners dependencies before all scanners
100102
\t-q --quiet Disables additional prints
101103
\t-c --color Disables color output
102104
\t-t --tests Enable scanning on test code
103105
\t-o --out=<file|dir> Set location for debugging output. Defaults to console
104106
\t-n --name=\"name\" Names of the output file/directory
105107
\t${GREEN}-h --help Help information${RESET_COLOR}
108+
\t-g --go=\"version\" Go version override
106109
${BOLD}${UNDERLINE}Arguments${NORMAL}:
107110
\t[files...] Defaults to ./... which recursively scans all subpackages of the project
108111
"
@@ -124,6 +127,28 @@ indexOf() {
124127
return ${#NAMES[@]}
125128
}
126129

130+
# Checks installation of scanners and install based on provided options:
131+
# $1: command we are running
132+
# $2: installation command
133+
checkInstallation() {
134+
if [[ -x $(command -v $1) ]]; then
135+
print "${GREEN}Found ${CYAN}$1${RESET_COLOR} executable"
136+
return 0
137+
elif [[ $3 == true ]]; then
138+
print "${YELLOW}Installing ${CYAN}$1${RESET_COLOR} using \"$2\""
139+
eval $2
140+
if [[ $(command -v $1) ]]; then
141+
print "${GREEN}Installation Successfull!${RESET_COLOR} continuing"
142+
return 0
143+
fi
144+
print "${BOLD}${RED}Installation Failed!${NORMAL}${RESET_COLOR} There may be something wrong with installation link"
145+
exit 1
146+
else
147+
print "${BOLD}${RED}Error!${NORMAL}${RESET_COLOR} ${CYAN}$1${RESET_COLOR} executable not found. Please install or use ${YELLOW}${name} -i${RESET_COLOR} flag"
148+
exit 1
149+
fi
150+
}
151+
127152
# Default option values
128153
option_s=${NAMES[@]} # List of scanners that will be used this run (defaults to all of them)
129154
option_d=false # Use default scan locations
@@ -169,6 +194,11 @@ special_args() {
169194
n|name)
170195
filename="$2"
171196
;;
197+
g|go)
198+
go install golang.org/dl/$2@latest
199+
$2 download
200+
export PATH=$($2 env GOROOT)/bin:$PATH
201+
;;
172202
*)
173203
indexOf ${1}
174204
index=$?
@@ -195,12 +225,12 @@ while (( $# )); do
195225
;;
196226
-I)
197227
print "${GREEN}Installing dependencies"
198-
for val in "${SCANNER_INSTALLATION_URL[@]}"; do
199-
print "${BLUE}Installing:${RESET_COLOR} ${val}"
200-
eval ${val}
228+
for scanner in ${option_s[@]}; do
229+
indexOf ${scanner}
230+
index=$?
231+
checkInstallation "${scanner}" "${SCANNER_INSTALLATION_URL[${index}]}" "true"
201232
done
202233
print "${GREEN}Installation complete!${RESET_COLOR} Exiting"
203-
exit 0
204234
;;
205235
-q|--quiet)
206236
option_q=true
@@ -248,32 +278,10 @@ done
248278
# Append test flags if -t (--test) is set
249279
if [[ ${option_t} == true ]]; then
250280
for index in ${!FLAGS[@]}; do
251-
FLAGS[${index}]="${TEST_FLAGS[${index}]} ${FLAGS[${index}]}"
281+
FLAGS[${index}]="${FLAGS[${index}]} ${TEST_FLAGS[${index}]}"
252282
done
253283
fi
254284

255-
# Checks installation of scanners and install based on provided options:
256-
# $1: command we are running
257-
# $2: installation command
258-
checkInstallation() {
259-
if [[ -x $(command -v $1) ]]; then
260-
print "${GREEN}Found ${CYAN}$1${RESET_COLOR} executable"
261-
return 0
262-
elif [[ ${option_i} == true ]]; then
263-
print "${YELLOW}Installing ${CYAN}$1${RESET_COLOR} using \"$2\""
264-
eval $2
265-
if [[ $(command -v $1) ]]; then
266-
print "${GREEN}Installation Successfull!${RESET_COLOR} continuing"
267-
return 0
268-
fi
269-
print "${BOLD}${RED}Installation Failed!${NORMAL}${RESET_COLOR} There may be something wrong with installation link"
270-
return 1
271-
else
272-
print "${BOLD}${RED}Error!${NORMAL}${RESET_COLOR} ${CYAN}$1${RESET_COLOR} executable not found. Please install or use ${YELLOW}${name} -i${RESET_COLOR} flag"
273-
return 1
274-
fi
275-
}
276-
277285
# Run the scans depending on options provide
278286
runScan() {
279287
local out=0
@@ -343,9 +351,8 @@ elif [[ $# == 0 ]]; then
343351
print "${GREEN}No package changes found since last commit${RESET_COLOR}. Specify package paths manually or use ${YELLOW}${name} -d${RESET_COLOR} for defaults locations"
344352
exit 0
345353
fi
346-
else
347-
scan_packages=( $@ )
348-
fi
354+
fi
355+
scan_packages=" $@ ${scan_packages[@]} "
349356

350357
# Creating logfiles if necessary
351358
if [[ ${option_o} == "file" ]]; then
@@ -360,12 +367,13 @@ elif [[ ${option_o} == "dir" ]]; then
360367
fi
361368

362369
# Start scanner code
370+
print "${BOLD}${GREEN}Running analysis ${NORMAL}${RESET_COLOR}script using $(go version)"
363371
exitcode=0
364372
for scanner in ${option_s[@]}; do
365373
indexOf ${scanner}
366374
index=$?
367375
print "${BOLD}${GREEN}Starting ${NORMAL}${CYAN}${scanner}${RESET_COLOR} scanner"
368-
checkInstallation "${scanner}" "${SCANNER_INSTALLATION_URL[${index}]}"
376+
checkInstallation "${scanner}" "${SCANNER_INSTALLATION_URL[${index}]}" ${option_i}
369377
if [[ $? == 0 ]]; then
370378
runScan "${scanner}" "${FLAGS[${index}]}"
371379
result=$?

makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ checkstyle::
2121
$(GO_SPACE)/Tools/src/checkstyle.sh
2222

2323
analyze-install::
24-
$(GO_SPACE)/Tools/src/static_analysis.sh -I
24+
$(GO_SPACE)/Tools/src/static_analysis.sh $(shell echo ${flags} | tr ",\[\]" " \"") -I
2525

2626
analyze::
2727
# Runs analysis script located inside Tools/src

0 commit comments

Comments
 (0)