-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathveracode-fix-wrapper.sh
More file actions
628 lines (546 loc) · 21.9 KB
/
veracode-fix-wrapper.sh
File metadata and controls
628 lines (546 loc) · 21.9 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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
#!/bin/bash
DEBUG=false
auto_package=false
package=false
trust=false
package_only=false
artifact_path=""
results_path="results.json"
all_results=()
all_paths=()
out_dir="./"
source=""
type="directory"
VERB=false # DO NOT SET TO TRUE
#veracode_cli_version=""
script_version="v2.1.3"
install_cli=false
files_not_found_warning=false
#global_path=true
#exec_path="/usr/local/bin"
#TODO: create function to run a warning when user is running script as root
# Precondition:
# Postcondition:
program_exists() {
command -v "$1" >/dev/null 2>&1
}
move_to_path(){
# Check if the correct number of arguments is provided
if [ "$#" -ne 1 ]; then
return 1
fi
local file_path="$1"
# Check if the file exists
if [ ! -e "$file_path" ]; then
echo "Error: File does not exist."
return 1
fi
# Iterate through each directory in the PATH
IFS=':' read -ra path_directories <<< "$PATH"
for dir in "${path_directories[@]}"; do
# Check if the script has permission to move the file to the current path location
if [ -w "$dir" ]; then
mv "$file_path" "$dir"
echo "File moved successfully to '$dir'."
return 0 # Exit the function with success status
fi
done
# If the loop completes without successfully moving the file
echo "Error: Permission denied. Cannot move file to any directory in the PATH."
return 1
}
force_update(){
local update_path=""
local file_path=""
if which veracode ; then
echo "Update path"
update_path="$( which veracode )"
curl -fsS https://tools.veracode.com/veracode-cli/install | sh
file_path="./veracode"
# Check if the file exists
if [ ! -e "$file_path" ]; then
echo "Error: File does not exist."
return 1
fi
mv -u "$file_path" "$update_path"
veracode configure
else
echo "Veracode CLI has not been installed yet, run the --install-cli parameter to install it"
fi
}
# Precondition:
# Postcondition:
install_veracode_cli(){
if program_exists "veracode"; then
if [ "$DEBUG" == "true" ]; then
echo "[DEBUG=$DEBUG]:: Veracode cli already exists in the execution path."
fi
echo "Veracode-Cli: $( which veracode )"
else
echo "[INFO]:: Veracode Cli wasn't found in the execution path, installing..."
curl -fsS https://tools.veracode.com/veracode-cli/install | sh
move_to_path ./veracode
fi
echo "[INFO]:: Running Veracode Configure, Configure the CLI"
veracode configure
}
# Precondition:
# Postcondition:
remove_trailing_period(){
filename="$1"
# Remove trailing period without affecting file extension
new_filename=$(basename "$filename" .)
new_filename_with_extension="${new_filename}${filename##*.}"
if [ "$VERB" == "true" ]; then
echo "Original filename: $filename"
echo "New filename: $new_filename_with_extension"
fi
echo "$new_filename_with_extension"
}
# Precondition:
# Postcondition:
help(){
echo "Veracode Fix Demo Tool"
echo "------------------------------------------------------------------------------------------------------------------------------------------"
echo "- Run without any parameters to use the default results.json file"
echo "- It will parse the results.json and look for found relative paths"
echo "- You will be prompted which file you want to run fix on in a menu"
echo "- You can select the file to attempt to apply a fix to, and then select the fix"
echo "- When you are done, you can tell the program you don't want to continue and it will close"
echo "- Each itteration, the program will ask if you want to attempt to apply a fix, if you say Y, or Yes, then it will take you to apply fixes"
echo "- If you don't want to continue to apply fixes, respond to the prompt N or No when asked if you want to apply fixes"
echo "- If you want to override the results.json and generate a new one, pass an artifact path for the static pipeline scanner to analyze"
echo "- If the results.json is named differently or in a different location specify that after the --results parameter"
echo "------ Usage ------------------------------------------------------------------------------------------------------------------------------"
echo "- Get Help:"
echo " $0 --help"
echo "- Run Veracode Fix against a results.json:"
echo " $0"
echo "- Run A Pipeline Scan First, then Veracode Fix against results.json:"
echo " $0 --artifact app/target/verademo.war"
echo "- Run Veracode Fix against results in specified file:"
echo " $0 --results app/results.json"
echo "- Run a Pipeline Scan First and output to specified file, then run Veracode Fix against the results file specified"
echo " $0 --artifact app/target/verdemo.war --results app/results.json"
echo "[Experimental] - Package the artifact before scanning it with the pipeline scan"
echo " $0 --outdir ./ --source verademo/ --type directory --trust --package"
echo "[Experimental] - Package the artifact, run a pipeline scan against the artifact, and generate fix recomendations from the results"
echo " $0 --outdir ./ --source verademo/ --type directory --trust --results results.json --package"
echo "[Experimental] - Install the Veracode Cli"
echo " $0 --install-cli <additional args>"
echo "- Turn on debug"
echo " $0 --debug <additional args>"
echo "[Experimental] - Install Veracode CLI"
echo " $0 --install-cli <additional args>"
echo "[Experimental] - Force Install Veracode CLI"
echo " $0 --force-install <additional args>"
echo "[Experimental] - Force Install Veracode CLI Locally"
echo " $0 --force-install-local "
echo "[Experimental] - Force Update Veracode CLI "
echo " $0 --force-update <additional args>"
}
# Precondition:
# Postcondition:
package_archive() {
# echo "[Experimental] - Package the artifact before scanning it with the pipeline scan"
# echo " ./veracode-fix-demo.sh --outdir ./ --source verademo/ --type directory --trust --package"
#check to see if type is set correctly
if [ "$type" == "repo" ] || [ "$type" == "directory" ]; then
if [[ "$DEBUG" == "true" ]]; then
echo "[DEBUG=$DEBUG]:: Type is set to $type "
fi
else
if [[ "$DEBUG" == "true" ]]; then
echo "[DEBUG=$DEBUG]:: Type is set to $type "
fi
echo "[ERROR]:(Type_Error): Type not set properly. Valid options are 'repo' and 'directory' "
exit -1
fi
#check to see if package is set
if [ "$package" == "true" ]; then
if [[ "$DEBUG" == "true" ]]; then
echo "[DEBUG=$DEBUG]:: Package is set to $package "
fi
else
if [[ "$DEBUG" == "true" ]]; then
echo "[DEBUG=$DEBUG]:: Package is set to $package "
fi
return
fi
#check to see if source is set
if [ -n "$outdir" ] && [ -n "$source" ]; then
if [[ "$DEBUG" == "true" ]]; then
echo "[DEBUG=$DEBUG]:: Outdir is set to $outdir "
echo "[DEBUG=$DEBUG]:: Source is set to $source "
fi
else
if [[ "$DEBUG" == "true" ]]; then
echo "[DEBUG=$DEBUG]:: Outdir is set to $outdir "
echo "[DEBUG=$DEBUG]:: Source is set to $source "
fi
echo "[ERROR]:(Directory_Error): Confirm that the output directory and source are valid directories"
exit -1
fi
#check to see if trust is set
if [ -n $trust ] && [ "$trust" == "true" ]; then
if [[ "$DEBUG" == "true" ]]; then
echo "[DEBUG=$DEBUG]:: Trust is set to $trust "
if [ "$outdir" == "./" ]; then
artifact_path="$( remove_trailing_period $( veracode package --output "$outdir" --source "$source" --type "$type" --trust | grep "Copied artifact:" | cut -d ':' -f3 ) )"
echo "$artifact_path"
else
artifact_path="${outdir}$( remove_trailing_period $( veracode package --output "$outdir" --source "$source" --type "$type" --trust | grep "Copied artifact:" | cut -d ':' -f3 ) )"
echo "$artifact_path"
fi
else
if [ "$outdir" == "./" ]; then
artifact_path="$( remove_trailing_period $( veracode package --output "$outdir" --source "$source" --type "$type" --trust | grep "Copied artifact:" | cut -d ':' -f3 ) )"
echo "$artifact_path"
else
artifact_path="${outdir}$( remove_trailing_period $( veracode package --output "$outdir" --source "$source" --type "$type" --trust | grep "Copied artifact:" | cut -d ':' -f3 ) )"
echo "$artifact_path"
fi
fi
else
if [[ "$DEBUG" == "true" ]]; then
echo "[DEBUG=$DEBUG]:: Trust is set to $trust "
if [ "$outdir" == "./" ]; then
artifact_path="$( remove_trailing_period $( veracode package --output "$outdir" --source "$source" --type "$type" | grep "Copied artifact:" | cut -d ':' -f3 ) )"
echo "$artifact_path"
else
artifact_path="${outdir}$( remove_trailing_period $( veracode package --output "$outdir" --source "$source" --type "$type" | grep "Copied artifact:" | cut -d ':' -f3 ) )"
echo "$artifact_path"
fi
else
if [ "$outdir" == "./" ]; then
artifact_path="$( remove_trailing_period $( veracode package --output "$outdir" --source "$source" --type "$type" | grep "Copied artifact:" | cut -d ':' -f3 ) )"
echo "$artifact_path"
else
artifact_path="${outdir}$( remove_trailing_period $( veracode package --output "$outdir" --source "$source" --type "$type" | grep "Copied artifact:" | cut -d ':' -f3 ) )"
echo "$artifact_path"
fi
fi
fi
}
# Precondition:
# Postcondition:
# Function to extract file name from FilePath
extract_file_name() {
# Extract file name from the given FilePath
filepath="$1"
echo "$(basename "$filepath")"
#return "$(basename "$filepath")"
}
# Precondition:
# Postcondition:
is_filepath_in_array() {
local filepath_to_check="$1"
for existing_filepath in "${all_results[@]}"; do
if [ "$existing_filepath" == "$filepath_to_check" ]; then
return 1 # Filepath is already in the array
fi
done
return 0 # Filepath is not in the array
}
# Precondition:
# Postcondition:
search_file() {
# File name to search for
filename="$1"
# Get a list of directories using ls command
directories=($(ls -d */ 2>/dev/null ))
# Array to store all identified paths and filenames
#all_results=()
# Loop through each directory
for dir in "${directories[@]}"; do
# Check if the directory exists
if [ -d "$dir" ]; then
# Use find to search for the file recursively up to 12 levels deep
result=$(find "$dir" -maxdepth 12 -type f -name "$filename" 2>/dev/null)
# Check if the file was found
if [ -n "$result" ]; then
# Extract the relative path and file name
relative_path="${result%/*}"
file_name="${result##*/}"
if is_filepath_in_array "$relative_path/$file_name"; then
all_results+=("$relative_path/$file_name")
fi
# Add the result to the array
#all_results+=("${relative_path}")
else
if [ "$DEBUG" == "true" ]; then
echo "[DEBUG=$DEBUG] File not found in $dir or its subdirectories."
fi
files_not_found_warning=true
fi
else
if [ "$DEBUG" == "true" ]; then
echo "[DEBUG=$DEBUG]:: Directory $dir does not exist."
fi
continue
fi
done
# Check if any results were found
if [ ${#all_results[@]} -eq 0 ]; then
if [ "$DEBUG" == "true" ]; then
echo "[DEBUG=$DEBUG]:: No matching files found."
fi
files_not_found_warning=true
else
# Iterate through the array and print each unique filepath
if [ "$DEBUG" == "true" ]; then
echo "[DEBUG=$DEBUG]:: Unique File Paths:"
fi
for path in $(echo "${all_results[@]}" | tr ' ' '\n' | sort -u); do
# echo "$path"
all_paths+=("$path")
done
fi
}
# Precondition:
# Postcondition:
file_exists(){
filename="$1"
if [ "$DEBUG" == "true" ]; then
echo "[Debug=$DEBUG]:: checking filename: $filename"
fi
if [ -e $filename ]; then
echo true
else
echo false
fi
}
# Precondition:
# Postcondition:
select_option() {
# little helpers for terminal print control and key input
ESC=$( printf "\033")
cursor_blink_on() { printf "$ESC[?25h"; }
cursor_blink_off() { printf "$ESC[?25l"; }
cursor_to() { printf "$ESC[$1;${2:-1}H"; }
print_option() { printf " $1 "; }
print_selected() { printf " $ESC[7m $1 $ESC[27m"; }
get_cursor_row() { IFS=';' read -sdR -p $'\E[6n' ROW COL; echo ${ROW#*[}; }
key_input() { read -s -n3 key 2>/dev/null >&2
if [[ $key = $ESC[A ]]; then echo up; fi
if [[ $key = $ESC[B ]]; then echo down; fi
if [[ $key = "" ]]; then echo enter; fi; }
# initially print empty new lines (scroll down if at bottom of screen)
for opt; do printf "\n"; done
# determine current screen position for overwriting the options
local lastrow=`get_cursor_row`
local startrow=$(($lastrow - $#))
# ensure cursor and input echoing back on upon a ctrl+c during read -s
trap "cursor_blink_on; stty echo; printf '\n'; exit" 2
cursor_blink_off
local selected=0
while true; do
# print options by overwriting the last lines
local idx=0
for opt; do
cursor_to $(($startrow + $idx))
if [ $idx -eq $selected ]; then
print_selected "$opt"
else
print_option "$opt"
fi
((idx++))
done
# user key control
case `key_input` in
enter) break;;
up) ((selected--));
if [ $selected -lt 0 ]; then selected=$(($# - 1)); fi;;
down) ((selected++));
if [ $selected -ge $# ]; then selected=0; fi;;
esac
done
# cursor position back to normal
cursor_to $lastrow
printf "\n"
cursor_blink_on
return $selected
}
# Precondition:
# Postcondition:
main() {
if [ "$install_cli" == "true" ]; then
install_veracode_cli
fi
if [ "$package" == "true" ]; then
echo "Running Packager"
package_archive
fi
if [ -z "$artifact_path" ]; then
echo "No artifact was passed"
if [ -f $results_path ]; then
echo "Using Results file that found in default/specified location"
else
read -p "Please enter the artifact path and name: " artifact_path
veracode static scan "$artifact_path" --results-file $results_path
fi
else
echo "artifact was passed"
#artifact_path="$1"
if [ -e $artifact_path ] && [ -f $artifact_path ]; then
if [ "$DEBUG" == "true" ]; then
echo "[Debug=$DEBUG]:: File exists"
fi
veracode static scan $artifact_path --results-file $results_path
else
echo "[ERROR]:: File doesn't exist"
exit
fi
fi
# Example usage: jq query with file name extraction and search
# Guardrails to only show fixes for fixable findings (need to add other languages)
#jq_result=($(jq -r '.findings[] as $finding | select(( $finding.cwe_id ) as $id | $id == "73" or $id == "80" or $id == "89" or $id == "113" or $id == "117" or $id == "327" or $id == "331" or $id == "382" or $id == "470" or $id == "597" or $id == "601") | $finding.files.source_file.file' results.json ))
jq_result=($(jq -r '.findings[] as $finding | $finding.files.source_file.file' results.json ))
filePaths=()
fileNames=()
options=()
for item in $(echo "${jq_result[@]}" | tr ' ' '\n' | sort -u) ; do
fileNames+=("$(basename $item)")
filePaths+=("$item")
search_file "$(basename $item)"
done
for path in $( echo "${all_paths[@]}"| tr ' ' '\n' | sort -u); do
options+=("$path")
done
if [ "$files_not_found_warning" == "true" ]; then
echo "[WARNING]:: Please check to make sure the source files are present, some files have not been found"
fi
while true; do
# Prompt the user for input
#read -p "Do you want to attempt to apply a(nother) fix? [(Y)es | (R)epackage_and_Scan | (N)o | (E)xit]: " response
read -p "Do you want to attempt to apply a(nother) fix from the flaws found in the $results_path? [(Y)es | (N)o ]: " response
# Convert the response to lowercase for case-insensitive comparison
response_lower=$(echo "$response" | tr '[:upper:]' '[:lower:]')
# Check if the response is "yes" or "y"
if [ "$response_lower" == "yes" ] || [ "$response_lower" == "y" ]; then
#echo "Continuing..."
# Add your logic here for what you want to do inside the loop
echo "Select one option using up/down keys and enter to confirm:"
echo ""
select_option "${options[@]}"
choice=$?
echo "Choosen index = $choice"
echo " value = ${options[$choice]}"
veracode fix "${options[$choice]}" --results $results_path
# elif [ "$response_lower" == "no" ] || [ "$response_lower" == "n" ]; then
# read -p "Would you like to rescan and repackage ? [(Y)es | (N)o ]:" response
# response_lower_=$(echo "$response" | tr '[:upper:]' '[:lower:]')
# if [ "$response_lower_" == "yes" ] || [ "$response_lower_" == "y" ]; then
# echo "Running Packager"
# package_archive
# if [ -z "$artifact_path" ]; then
# read -p "Please enter the artifact path location: " artifact_path
# read -p "Please enter the type, directory or repo: " type
# echo "Running Packager"
# package_archive
# veracode static scan "$artifact_path" --results-file $results_path
# else
# echo "artifact was passed"
# #artifact_path="$1"
# if [ -e $artifact_path ] && [ -f $artifact_path ]; then
# if [ "$DEBUG" == "true" ]; then
# echo "[Debug=$DEBUG]:: File exists"
# fi
# veracode static scan $artifact_path --results-file $results_path
# else
# echo "[ERROR]:: File passed doesn't exist"
# exit
# fi
# fi
# else
# break
# fi
else
echo "Exiting loop."
break # Exit the loop if the response is not "yes" or "y"
fi
done
}
# Proccess the input
while [[ $# -gt 0 ]]; do
case "$1" in
# Specify the output directory
--help)
help
shift 1
exit
;;
# Display the Help Menu
--artifact)
artifact_path="$2"
shift 2
;;
#
--results)
results_path="$2"
shift 2
;;
--outdir)
outdir="$2"
shift 2
;;
--source)
source="$2"
shift 2
;;
--type)
type="$2"
shift 2
;;
--trust)
trust=true
shift 1
;;
--package)
package=true
shift 1
;;
--package-only)
package_only=true
shift 1
;;
--version)
echo "Veracode Fix Script Wrapper Version: $script_version"
echo "Veracode CLI Version: $( veracode version )"
shift 1
;;
--install-cli)
install_cli=true
shift 1
;;
--force-install-local)
curl -fsS https://tools.veracode.com/veracode-cli/install | sh
echo "Please place the veracode cli in the running user's path"
exit 0
;;
--force-install)
curl -fsS https://tools.veracode.com/veracode-cli/install | sh
move_to_path ./veracode
shift 1
;;
--force-update)
force_update
shift 1
;;
# --auto-package)
# auto_package=true
# shift 1
# ;;
--debug)
DEBUG=true
shift 1
;;
*)
echo "Unknown argument: $2"
help
exit 1
;;
esac
done
#TODO: add type check error
main