-
Notifications
You must be signed in to change notification settings - Fork 5
/
FortifyToolsInstaller.sh
executable file
·770 lines (691 loc) · 29.6 KB
/
FortifyToolsInstaller.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
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
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
#!/bin/bash
######################################################################################################
###
### Define the tools that can be installed using this script
###
######################################################################################################
defineTools() {
# Tool names and their properties can be defines using the following functions:
#
# - addToolAliases <toolName> [toolAlias1] [toolAlias2] [...]
# Define zero or more aliases for the given <toolName>, allowing users to either
# use a short name for brevity or long name for clarity
#
# - setToolDefaultVersion <toolName> <defaultVersion>
# Define default version to be used for <toolName> if no version specified
#
# - addToolDownloadUrl <toolName> <toolVersion|'default'> <downloadUrl>
# Define download URL's for given <toolName>. Different URL's can be specified
# for different tool versions, for example if the download URL for the 'latest'
# version is different from download URL's for specific versions. One 'default'
# URL can be specified for each tool, which will be used if no version-specific
# URL has been configured. The <downloadUrl> may contain properly escaped/quoted
# ${toolVersion) variable.
#
# For each tool name, the following functions may be invoked:
# - _preInstall_<toolName> <toolAlias> <toolVersion> <toolInstallDir>
# Optional: Commonly used to define script variables used by both the
# _install_<toolName> and _postInstall_<toolName> functions.
# If available, this function is always invoked even if the
# tool has already been installed.
# - _install_<toolName> <toolAlias> <toolVersion> <toolInstallDir>
# Required: Download and install the tool to <toolInstallDir> if it hasn't
# been installed yet or if it is being force-installed.
# - _postInstall_<toolName> <toolAlias> <toolVersion> <toolInstallDir>
# Optional: Commonly used to perform any post-installation tasks like
# updating configuration files and defining output variables.
# If available, this function is always invoked even if the
# tool has already been installed.
addToolAliases FoDUploader FoDUpload fu
setToolDefaultVersion FoDUploader latest
addToolDownloadUrl FoDUploader default 'https://github.com/fod-dev/fod-uploader-java/releases/download/${toolVersion}/FodUpload.jar'
addToolDownloadUrl FoDUploader latest 'https://github.com/fod-dev/fod-uploader-java/releases/latest/download/FodUpload.jar'
addToolSHA256 FoDUploader latest '7d3fc9ada2df3cd5ed6159685d36656d04cef6ddda69bff2110702c052e64fce'
addToolSHA256 FoDUploader v5.4.0 '7d3fc9ada2df3cd5ed6159685d36656d04cef6ddda69bff2110702c052e64fce'
addToolSHA256 FoDUploader v5.3.1 '9ac7d8c093511796b9d69c24e7ef17b763614c24b27787eff27ed7960d9105f4'
addToolSHA256 FoDUploader v5.3.0 '302e394dd0a84321a388ae0a802c5847dcfe52284e2fe9d7d524952aea8214f0'
addToolSHA256 FoDUploader v5.2.1 'f52e070309cc5539ed1937cd16c370ccf5c09d5cf4e80708766ab43959d4fa20'
addToolSHA256 FoDUploader v5.2.0 '1e08c0fc1269e39fe502fe3ab301182e2ef2a8884328ac2beec092873d308100'
addToolSHA256 FoDUploader v5.0.1 'fc31198af03c074bc9190b85d6b9acf42495163f2d7db763c8bb4ed1df6b7d92'
addToolSHA256 FoDUploader v5.0.0 '7d06869581879ffcb055ab0c2f771d69e21f90caa4a22440d918a2f18b0a3125'
addToolSHA256 FoDUploader v4.0.4 'adf474e314e2ba2b2e2d8a63ef8e5bc513f49648c1979af2ce04a3191edd6130'
addToolSHA256 FoDUploader v4.0.3 '4c69b360752a74d5cd728fd8f5b519bbed4af5d6fff47dd3e849a924ba31d0c1'
addToolSHA256 FoDUploader v4.0.2 '47b3056c7dcee7b790670458f65a21757c422e67b178d6ac907ed32aead004be'
addToolSHA256 FoDUploader v4.0.1 '7c5abde97d07288ae146160c4eee14091606b26590536b00d5e7b9bba31f0507'
addToolSHA256 FoDUploader v4.0.0 '6921614493c070d1788c9326a43ce7d65cad5427d7de47dd2548be74c1defa7c'
addToolAliases ScanCentralClient ScanCentral sc
setToolDefaultVersion ScanCentralClient latest
addToolDownloadUrl ScanCentralClient default 'https://tools.fortify.com/scancentral/Fortify_ScanCentral_Client_${toolVersion}_x64.zip'
addToolDownloadUrl ScanCentralClient latest 'https://tools.fortify.com/scancentral/Fortify_ScanCentral_Client_Latest_x64.zip'
addToolSHA256 ScanCentralClient latest '534034b322811b6eb15289c5ae538837c615f2d94ef17f24a1d5c26f492b98e0'
addToolSHA256 ScanCentralClient 22.1.2 '534034b322811b6eb15289c5ae538837c615f2d94ef17f24a1d5c26f492b98e0'
addToolSHA256 ScanCentralClient 22.1.0 'dc57f68e1b89578ee7b9230c6ff5ea268480e8ca6d847f3ddbdb0bb95065e107'
addToolSHA256 ScanCentralClient 21.2.3 '96b6678e0a133f2589ac1df7b7ff34141630c2c697ea17be3a610bccb3b0d97e'
addToolSHA256 ScanCentralClient 21.2.2 'e38dcd114153ff9e3f570f2dae2a9e5ebf30b10c9900ae8b96a03e22bc35a487'
addToolSHA256 ScanCentralClient 21.2.0 '055e26cb8ee81f148536a9e94b64a07d388a243291b48293df17d7ee2b4f2d58'
addToolSHA256 ScanCentralClient 21.1.4 'c12b4725fa1bcaf718d05a7dcd5fb44b90139b21fa4009fa3b1c170b886df80d'
addToolSHA256 ScanCentralClient 21.1.3 '176e6ec213cb958b6828ca8601dc482d5a4ba5126f79e3249bf4e58d6982aea0'
addToolSHA256 ScanCentralClient 21.1.2 '313d37acc652edba9657fbc8fed1d811ad0df014636f02314a97865c48244dd6'
addToolSHA256 ScanCentralClient 20.2.4 'a0a6acba48b7b3f989db191053d336410b124cb493f006abcba09cc70311b095'
addToolSHA256 ScanCentralClient 20.2.0 'c559e1e08c0d90af71e77bdbb806731f818f59d4b3da7e41c02a307495c38d06'
addToolSHA256 ScanCentralClient 20.1.0 '4a315c9ab9c61978b47772945e29063545478ae2f0e4574c0111bce891c04eb5'
addToolAliases FortifyVulnerabilityExporter fve
setToolDefaultVersion FortifyVulnerabilityExporter latest
addToolDownloadUrl FortifyVulnerabilityExporter default 'https://github.com/fortify/FortifyVulnerabilityExporter/releases/download/${toolVersion}/FortifyVulnerabilityExporter.zip'
addToolDownloadUrl FortifyVulnerabilityExporter latest 'https://github.com/fortify/FortifyVulnerabilityExporter/releases/latest/download/FortifyVulnerabilityExporter.zip'
addToolSHA256 FortifyVulnerabilityExporter latest '1f3a710003c81a9e767f2c90bbfca3f2250b01ad3a50b181a04eb7d025c05da6'
addToolSHA256 FortifyVulnerabilityExporter v1.8.0 '1f3a710003c81a9e767f2c90bbfca3f2250b01ad3a50b181a04eb7d025c05da6'
addToolSHA256 FortifyVulnerabilityExporter v1.7.0 'd04e71cd9049475911094f43bcf96184763cc0e8989fb329cc18117f8a96d98d'
addToolSHA256 FortifyVulnerabilityExporter v1.6.0 '48dffe599c8712393c7491f5c824de1a17afea0d0a5b4c8f0c657674ff2c6bb9'
addToolSHA256 FortifyVulnerabilityExporter v1.5.5 '6a6fe679768a8f079f00befe1daf6a606b091bf199829d66fe4f8b28de40e67c'
addToolSHA256 FortifyVulnerabilityExporter v1.5.4 '98a1deb443859018de25c853bb90001088819be556495b309a7e34c798c8da33'
addToolSHA256 FortifyVulnerabilityExporter v1.5.3 '86ce98782823354a3bfc0fe770ec8344df616b69316b73d2319e875a9d2bb8d2'
addToolSHA256 FortifyVulnerabilityExporter v1.5.2 '86eaa462085f8a0b178a4a697e13332ce1954a9e3d886a7d8c710ce5b53a3a07'
addToolSHA256 FortifyVulnerabilityExporter v1.5.1 '8db8d6db6c7d3e6ddeee0722179cb0f413a0d3dcefa19a21db5461a0b0c410d4'
addToolSHA256 FortifyVulnerabilityExporter v1.5.0 'cc86851b0b5adada0b04f43b93693a637ab3735e85d25246f859535aeb7835cf'
addToolSHA256 FortifyVulnerabilityExporter v1.4.1 '6e9fa005364513593ab820c79d2c1f05283ff9769fb313c669f0159c395e3d4c'
addToolSHA256 FortifyVulnerabilityExporter v1.4.0 '63b6e90c5a06f3db6d913121a5fb8a7578d2a1c65a4bdc8cfd6fd0aef286d296'
addToolSHA256 FortifyVulnerabilityExporter v1.3.1 '8b3f1d8696ed183a9ae0e005bc4165571b0be3e80e59038eb16a446e3ef5e91b'
addToolSHA256 FortifyVulnerabilityExporter v1.3.0 '28515fd51112b803a1d154884efd3743013895bb3f0037e4eedb2a8b18659bb7'
addToolSHA256 FortifyVulnerabilityExporter v1.2.1 '1badaf7f91be4482d7666d398893e1b68e24d446811843bee74aea5144f0fe1d'
addToolSHA256 FortifyVulnerabilityExporter v1.2.0 '05acd1451bcd5b7639e5abb43d42544a6ff5b53d3e9f6c49891094a4d9d6fc6a'
addToolSHA256 FortifyVulnerabilityExporter v1.1.3 '07b885a3690d111a0bc3b4e40581cd148bd36435255f1a7638c574ebeb8975e1'
addToolSHA256 FortifyVulnerabilityExporter v1.1.2 '5c27aa10b9fa8cd6b49317543f9fb0ae8e53d6d099f62daadd3b8086f3e56500'
addToolSHA256 FortifyVulnerabilityExporter v1.1.1 'd27c234a0a85ac79ab0f317777a151b70c8899f3c8bdf909779823555227d98f'
addToolSHA256 FortifyVulnerabilityExporter v1.1.0 '05a6fed8ded797ab4afb0bdede2a53fa830c218f9944cc076fcaab316505d20d'
addToolSHA256 FortifyVulnerabilityExporter v1.0.1 '23f499053ed2663084ca509d292d4190c1a609941371d7094a7b3cf474363d4f'
addToolSHA256 FortifyVulnerabilityExporter v1.0.0 '2edf4bc065e48cb8a2571752dfbe5a944495824e8a3c899e06206a24bf15562c'
}
######################################################################################################
###
### Generic utility functions
###
######################################################################################################
# Exit script, providing the following advantages over a regular 'exit':
# - Doesn't exit shell if script is being sourced
# - Script exits immediately even if _exit is called from a
# function running in a subshell, i.e. no need to do
# $(getXXX a b c) || exit $?
_exit() {
kill -INT $$
}
# Print a message to stderr
msg() {
cat <<< "$@" >&2;
}
# Exit with an error code after printing an error message
# Usage: exitWithError <msg>
exitWithError() {
logError "$@"
_exit 1
}
# Log info message
# Usage: logInfo <msg>
logInfo() {
msg "INFO: $@"
}
# Log warn message
# Usage: logWarn <msg>
logWarn() {
msg "WARN: $@"
}
logError() {
msg "ERROR: $@"
}
# Check if single argument is an existing command
# Usage: if isCommand someCommand; then ...
isCommand() {
command -v "$1" >/dev/null
}
# Check if single argument is an existing function
# Usage: if isFunction someFunction; then ...
isFunction() {
[[ $(type -t $1) == function ]] >/dev/null
}
# Print the contents of a given URL, using either wget or curl
# Exit with an error if neither wget or curl are available
# Usage: printUrlContents <url>
printUrlContents() {
local url=$1
if isCommand wget; then
wget -qO- $url || exitWithError "Error accessing ${url}"
elif isCommand curl; then
curl -sL $url || exitWithError "Error accessing ${url}"
else
exitWithError "Either wget or curl must be installed to download files"
fi
}
# Get a temporary file name
# Usage: myTempFile=$(_mktemp)
_mktemp() {
if isCommand mktemp; then
mktemp
else
mkdir -p "$FORTIFY_HOME/tmp"
echo "$FORTIFY_HOME/tmp/fti_tmp_$$"
fi
}
# Execute chmod with the given arguments if installed,
# otherwise log a warning message.
# Usage: _chmod <chmod-args>
_chmod() {
if isCommand chmod; then
chmod "$@"
else
logWarn "Command chmod not found, not executing 'chmod $@'"
fi
}
# Evaluate the given string, expanding any variables contained in the string
# Usage: expandedString=$(evalStringWithVars ${stringWithVars})
evalStringWithVars() {
stringWithVars=$1
echo $(source <(echo "echo \"${stringWithVars}\""))
}
# Get input value from either VARS_OUT or regular (environment) variables,
# using both uppercase and lowercase lookup
# Usage: myVar=$(getVar <someVariableName>)
getVar() {
local name=$1
local defaultValue=$2
local nameUpper=${name^^}
local nameLower=${name,,}
echoFirstNotBlank \
"${SCRIPT_VARS[$name]}" \
"${SCRIPT_VARS[$nameUpper]}" \
"${SCRIPT_VARS[$nameLower]}" \
"${VARS_OUT[$name]}" \
"${VARS_OUT[$nameUpper]}" \
"${VARS_OUT[$nameLower]}" \
"${!name}" \
"${!nameUpper}" \
"${!nameLower}" \
"$defaultValue"
}
# Echo first non-blank parameter value
# Usage: echoFirstNotBlank "${someVar}" "${someOtherVar}" "${someThirdVar}"
echoFirstNotBlank() {
for value in "$@"
do
[ -n "${value}" ] && echo ${value} && break
done
}
# Check whether the given value equals either 'true' (case-insensitive) or '1'
# Usage: isTrue <value>
isTrue() {
value=$1
[[ "${value,,}" == "true" || "${value}" == "1" ]]
}
# Check whether the value for the given variable name equals either 'true' (case-insensitive) or '1'
# Usage: isVarTrue <variableName>
isVarTrue() {
var=$1
isTrue $(getVar $var)
}
# Check whether the given function exists
# Usage: checkFunctionExists <functionName>
checkFunctionExists() {
local fn=$1
if ! isFunction $fn; then
exitWithError "Unknown function: $1"
fi
}
######################################################################################################
###
### Script logic
###
######################################################################################################
# Print an error message followed by usage instructions, then exit the script
# Usage: exitWithUsage "<error message>"
exitWithUsage() {
msg "ERROR: $@"
msg ""
usage
_exit 1
}
# Print usage instructions
usage() {
msg "This utility will install and optionally run various Fortify tools that are commonly used in CI/CD pipelines"
msg ""
msg "Usage:"
msg " ./FortifyToolsInstaller.sh <-h|--help>"
msg " [options] source ./FortifyToolsInstaller.sh"
msg " [options] source <(curl -sL https://raw.githubusercontent.com/fortify/FortifyToolsInstaller/latest/FortifyToolsInstaller.sh)"
msg " [options] source <(curl -sL https://raw.githubusercontent.com/fortify/FortifyToolsInstaller/<version|branch>/FortifyToolsInstaller.sh)"
msg ""
msg "Note that as a best practice, you may want to verify the integrity of the script before executing it"
msg "from a remote location, for example by running sha256sum on the downloaded script and comparing this"
msg "with a known SHA256 hash."
msg ""
msg "[options] is a set of variable definitions. Variables must be specified in either lower case or uppercase"
msg "case; mixed case variables will not be recognized. Tool aliases are case-insensitive. The following options"
msg "are recognized by this tool:"
msg ""
msg " FTI_TOOLS=<toolAlias1[:toolVersion]>[,<toolAlias2>[:version][,...]"
msg " Required: Define one or more tool aliases and optional tool versions to be installed"
msg " Example: FTI_TOOLS=sc,fu:latest,fve:v1.4.1"
msg " Supported tools, their aliases and default versions:"
printSupportedTools
msg ""
msg " FTI_FORCE_INSTALL=true|1"
msg " Force tools to be re-downloaded and installed even if they are already installed"
msg ""
msg " FTI_DISABLE_INTEGRITY_CHECK=true|1"
msg " Disable integrity check for downloaded tools. It is recommended to leave integrity checks"
msg " enabled; also see <toolAlias|toolName>_SHA256 below. Note that hashes for 'latest' versions"
msg " may change whenever a new tool version is released, which will cause the integrity check to"
msg " fail until this script has been updated with the correct hash."
msg ""
msg " FTI_VARS_OUT=export|verify"
msg " If set to 'export' (default), output variables will be exported to the shell environment."
msg " The 'verify' option is useful when building docker images, to verify that the Dockerfile"
msg " contains ENV instructions that match the output variables of FortifyToolsInstaller."
msg ""
msg " FORTIFY_HOME=</path/to/fortify.home>"
msg " Override Fortify home directory, defaults to ~/.fortify"
msg ""
msg " FORTIFY_TOOLS_HOME=</path/to/fortify/tools/dir>"
msg " Override Fortify tools directory, defaults to <FORTIFY_HOME>/tools"
msg " Tools will be installed to <FORTIFY_TOOLS_HOME>/<toolName>/<toolVersion>"
msg ""
msg " <toolAlias|toolName>_HOME=</path/to/tool/installation/directory>"
msg " Override installation directory for the given tool name or alias, defaults to"
msg " <FORTIFY_TOOLS_HOME>/<toolName>/<toolVersion>"
msg ""
msg " <toolAlias|toolName>_SHA256=<SHA256 for given tool name being installed>"
msg " Override SHA256 hash for the given tool alias or name, for the version specified in the"
msg " FTI_TOOLS variable. This may be useful in case this script hasn't been updated yet with"
msg " hashes for new tool versions."
msg ""
msg " SCANCENTRAL_CLIENT_AUTH_TOKEN=<token>"
msg " SC_CLIENT_AUTH_TOKEN=<token>"
msg " Optional ScanCentral client authentication token to be stored in client.properties"
msg ""
}
# Print list of tools that can be installed using this script, together with tool properties
# like default version and aliases.
# Usage: printSupportedTools
printSupportedTools() {
for toolName in "${!toolFriendlyAliasesByName[@]}"
do
local toolDefaultVersion=${toolDefaultVersionsByName[${toolName}]}
local toolAliases=${toolFriendlyAliasesByName[${toolName}]}
msg " ${toolName}"
msg " Aliases: ${toolAliases}"
msg " Default version: ${toolDefaultVersion}"
done
}
# Main function for running this script
# Usage: run
run() {
local args="$@"
declare -A toolNamesByAlias toolFriendlyAliasesByName toolDefaultVersionsByName toolDownloadUrlsByNameAndVersion toolSHA256ByNameAndVersion
declare -a toolFriendlyAliases;
defineTools
if [[ "$1" == "--help" || "$1" == "-h" ]]; then
usage
else
# This associative array is used to store variables shared between various functions
declare -A SCRIPT_VARS
# This associative array is used to store environment variables related to the various tools being installed
declare -A VARS_OUT
# This array is used to store path entries to be added to the PATH variable
declare -a PATH_OUT
defineGenericOutputVars
installTools
processVarsOut
fi
}
# Add tool aliases
# Usage: addToolAliases <toolName> [alias1] [alias2] [...]
addToolAliases() {
local toolName=$1; shift;
addToolAlias ${toolName} ${toolName} # Add toolName as alias for itself
for toolAlias in "$@"
do
addToolAlias ${toolName} ${toolAlias}
done
}
# Add tool alias
# Usage: addToolAlias <toolName> <toolAlias>
addToolAlias() {
local toolName=$1; local toolFriendlyAlias=$2
toolNamesByAlias[${toolFriendlyAlias,,}]=${toolName}
toolFriendlyAliases+=( ${toolFriendlyAlias} )
if [ -z "${toolFriendlyAliasesByName[${toolName}]+_}" ]; then
toolFriendlyAliasesByName[${toolName}]="${toolFriendlyAlias}"
else
toolFriendlyAliasesByName[${toolName}]+=", ${toolFriendlyAlias}"
fi
}
# Set tool default version
# Usage: setToolDefaultVersion <toolName> <toolDefaultVersion>
setToolDefaultVersion() {
local toolName=$1;
local toolDefaultVersion=$2;
toolDefaultVersionsByName[${toolName}]=${toolDefaultVersion}
}
# Add tool download URL
# Usage: addToolDownloadUrl <toolName> <toolVersion|'default'> <downloadURL>
addToolDownloadUrl() {
local toolName=$1;
local toolVersion=$2;
local toolDownloadUrl=$3;
toolDownloadUrlsByNameAndVersion[${toolName}_${toolVersion}]="${toolDownloadUrl}"
}
# Add tool SHA256
# Usage: addToolSHA256 <toolName> <toolVersion|'default'> <SHA256>
addToolSHA256() {
local toolName=$1;
local toolVersion=$2;
local toolSHA256=$3;
toolSHA256ByNameAndVersion[${toolName}_${toolVersion}]="${toolSHA256}"
}
# Define generic output variables
# Usage: defineGenericOutputVars
defineGenericOutputVars() {
SCRIPT_VARS[FORTIFY_HOME]=$(getVar FORTIFY_HOME "${HOME}/.fortify")
SCRIPT_VARS[FORTIFY_TOOLS_HOME]=$(getVar FORTIFY_TOOLS_HOME "$(getVar FORTIFY_HOME)/tools")
}
# Install the tools as configured in the FTI_TOOLS variable
# Usage: installTools
installTools() {
local fciTools=$(getVar FTI_TOOLS)
[ -z "$fciTools" ] && exitWithUsage "FTI_TOOLS option must be defined"
for toolAndVersion in ${fciTools//,/ }; do
IFS=':' read -r toolAlias toolVersion <<< "$toolAndVersion"
toolVersion=${toolVersion:-$(getToolDefaultVersion ${toolAlias})}
installTool "${toolAlias}" "${toolVersion}"
done
}
# Install the given tool
# Usage: installTool <toolAlias> <toolVersion>
installTool() {
local toolAlias=$1
local toolVersion=$2
local toolName=$(getToolName ${toolAlias})
local toolInstallDir=$(getToolInstallDir ${toolAlias} ${toolVersion})
# Get the tool-specific functions. We always run pre-install and post-install
# functions if available, even if tool is already installed.
local fnPreInstall=$(getToolFunction "_preInstall" $toolAlias)
local fnInstall=$(getToolFunction "_install" $toolAlias)
local fnPostInstall=$(getToolFunction "_postInstall" $toolAlias)
# Install function is required, other functions are optional
checkFunctionExists $fnInstall
isFunction $fnPreInstall && $fnPreInstall ${toolAlias} ${toolVersion} ${toolInstallDir}
if doToolInstall ${toolAlias} ${toolVersion} ${toolInstallDir}; then
rm -rf "$toolInstallDir" 2> /dev/null
mkdir -p "$toolInstallDir"
logInfo "Installing ${toolName}:${toolVersion} to ${toolInstallDir}"
$fnInstall ${toolAlias} ${toolVersion} ${toolInstallDir}
else
logInfo "Found existing ${toolName}:${toolVersion} in ${toolInstallDir}"
fi
isFunction $fnPostInstall && $fnPostInstall ${toolAlias} ${toolVersion} ${toolInstallDir}
addOptionalBinDirToPathOut "${toolInstallDir}/bin"
}
# Determine whether the given tool needs to be installed
# Usage: if doToolInstall <toolAlias> <toolVersion> <toolInstallDir>; then ...
doToolInstall() {
local toolAlias=$1
local toolVersion=$2
local toolInstallDir=$3
isVarTrue FTI_FORCE_INSTALL || [[ ! -d "$toolInstallDir" ]] || [[ -z `ls -A "$toolInstallDir"` ]]
}
# Get the tool-specific function for the given tool with the given prefix
# Usage: fn=$(getToolFunction <functionPrefix> <toolAlias>); fn <args>
getToolFunction() {
local fnPrefix=$1
local toolAlias=$2
local toolName=$(getToolName ${toolAlias})
local fn=${fnPrefix}_${toolName}
echo $fn
}
# Get the tool name for the given tool alias
# Usage: toolName=$(getToolName <toolAlias>)
getToolName() {
local toolAlias=$1
local toolAliasLowerCase=${toolAlias,,}
if [ ${toolNamesByAlias[${toolAliasLowerCase}]+_} ]; then
echo ${toolNamesByAlias[${toolAliasLowerCase}]}
else
exitWithError "Unknown tool alias: $toolAlias"
fi
}
# Get the default tool version for the given tool alias
# Usage: toolDefaultVersion=$(getToolDefaultVersion <toolAlias>)
getToolDefaultVersion() {
local toolAlias=$1
local toolName=$(getToolName ${toolAlias})
if [ ${toolDefaultVersionsByName[${toolName}]+_} ]; then
echo ${toolDefaultVersionsByName[${toolName}]}
else
exitWithError "No default version defined for tool: $toolAlias"
fi
}
# Get the installation directory for the given tool alias and version
# Usage: toolInstallDir=$(getToolInstallDir <toolAlias> <toolVersion>)
getToolInstallDir() {
local toolAlias=$1
local toolVersion=$2
local toolName=$(getToolName ${toolAlias})
local defaultToolInstallDir;
if [ "${toolVersion}" == "latest" ]; then
defaultToolInstallDir="$(getVar FORTIFY_TOOLS_HOME)/${toolName}/latest-$(date +'%Y%m%d')"
else
defaultToolInstallDir="$(getVar FORTIFY_TOOLS_HOME)/${toolName}/${toolVersion}"
fi
echo $(getVar "${toolAlias}_HOME" $(getVar "${toolName}_HOME" ${defaultToolInstallDir}))
}
# Get the download URL for the given tool alias and version
# Usage: toolDownloadUrl=$(getToolDownloadUrl <toolAlias> <toolVersion>)
getToolDownloadUrl() {
local toolAlias=$1
local toolVersion=$2
local toolName=$(getToolName ${toolAlias})
local downloadUrlWithVars;
if [ ${toolDownloadUrlsByNameAndVersion[${toolName}_$toolVersion]+_} ]; then
downloadUrlWithVars="${toolDownloadUrlsByNameAndVersion[${toolName}_$toolVersion]}"
elif [ ${toolDownloadUrlsByNameAndVersion[${toolName}_default]+_} ]; then
downloadUrlWithVars="${toolDownloadUrlsByNameAndVersion[${toolName}_default]}"
else
exitWithError "No download URL defined for tool: $toolAlias"
fi
evalStringWithVars "${downloadUrlWithVars}"
}
# Get the SHA256 for the given tool alias and version
# Usage: toolSHA256=$(getToolSHA256 <toolAlias> <toolVersion>)
getToolSHA256() {
local toolAlias=$1
local toolVersion=$2
local toolName=$(getToolName ${toolAlias})
local sha256Override=$(getVar "${toolAlias}_SHA256" $(getVar "${toolName}_SHA256" ""))
if [[ "${sha256Override}" != "" ]]; then
echo "${sha256Override}"
elif [ ${toolSHA256ByNameAndVersion[${toolName}_$toolVersion]+_} ]; then
echo "${toolSHA256ByNameAndVersion[${toolName}_$toolVersion]}"
else
exitWithError "No SHA256 hash defined for $toolAlias:$toolVersion"
fi
}
# Check the SHA256 for the given tool alias and version
# Usage: checkToolSHA256 <toolAlias> <toolVersion> <downloadedFile>
checkToolSHA256() {
local toolAlias=$1
local toolVersion=$2
local toolDownload=$3
if ! isVarTrue FTI_DISABLE_INTEGRITY_CHECK; then
local hash=$(sha256sum "$toolDownload" | head -c64)
local expectedHash=$(getToolSHA256 $toolAlias $toolVersion)
if [[ "${hash}" != "${expectedHash}" ]]; then
exitWithError "Got SHA256 hash ${hash} for $toolAlias:$toolVersion, expected ${expectedHash} instead"
fi
fi
}
# Download and save the contents for a given tool
# Usage: downloadAndSaveToolContents <toolAlias> <toolVersion> <outputFile>
downloadAndSaveToolContents() {
local toolAlias=$1
local toolVersion=$2
local saveLocation=$3
local url=$(getToolDownloadUrl $toolAlias $toolVersion)
printUrlContents "$url" > "$saveLocation"
checkToolSHA256 $toolAlias $toolVersion "$saveLocation"
}
# Download and unzip the contents for a given tool
# Usage: downloadAndUnzipToolContents <toolAlias> <toolVersion> <unzipDir>
downloadAndUnzipToolContents() {
local toolAlias=$1
local toolVersion=$2
local unzipDir=$3
local url=$(getToolDownloadUrl $toolAlias $toolVersion)
local tmpFile=$(_mktemp)
if ! isCommand unzip; then
exitWithError "Unzip command must be installed to extract contents from $url"
fi
printUrlContents "$url" > "$tmpFile"
checkToolSHA256 $toolAlias $toolVersion "$tmpFile"
unzip -qod "$unzipDir" "$tmpFile"
rm -f "$tmpFile"
}
addOptionalBinDirToPathOut() {
local binDir=$1
if [[ -d "${binDir}" ]]; then
if [[ ! -x "${binDir}" ]]; then
logWarn "Bin-directory found but not accessible: ${binDir}"
else
PATH_OUT+=("${binDir}")
fi
fi
}
# Process the output variables by exporting them
processVarsOut() {
if [[ "$FTI_VARS_OUT" == "verify" ]]; then
verifyVarsOut
verifyPathOut
else
exportVarsOut
exportPathOut
fi
}
exportVarsOut() {
for key in "${!VARS_OUT[@]}"
do
export $key="${VARS_OUT[$key]}"
done
}
exportPathOut() {
for entry in "${PATH_OUT[@]}"
do
# We add each entry to the front of the path, independent of whether it already exists, to make sure latest installation is used
export PATH="${entry}:${PATH}"
done
}
verifyVarsOut() {
local hasError=0
for key in "${!VARS_OUT[@]}"
do
if [[ -z "${!key}" ]]; then
logError "Environment variable '${key}' is not defined"
hasError=1
elif [[ "${!key}" != "${VARS_OUT[$key]}" ]]; then
logError "Environment variable '${key}' has unexpected value ${!key}"
logError=1
fi
done
if [[ "${hasError}" == 1 ]]; then
logInfo "Expected variable values:"
for key in "${!VARS_OUT[@]}"
do
msg "$key=${VARS_OUT[$key]}"
done
_exit 1
fi
}
verifyPathOut() {
local hasError=0
for entry in "${PATH_OUT[@]}"
do
if [[ "$PATH" != *"$entry"* ]]; then
logError "PATH is missing entry '$entry'"
hasError=1
fi
done
if [[ "${hasError}" == 1 ]]; then
logInfo "Expected PATH entries:"
echo $(IFS=: ; echo "${PATH_OUT[*]}")
_exit 1
fi
}
######################################################################################################
###
### Functions for installing and configuring FoDUploader
###
######################################################################################################
_install_FoDUploader() {
local toolAlias=$1
local toolVersion=$2
local toolInstallDir=$3
local jarFile=${toolInstallDir}/FoDUpload.jar
downloadAndSaveToolContents "$toolAlias" "$toolVersion" "${jarFile}"
_addBinScript_FoDUploader "${toolInstallDir}" "${jarFile}"
}
_addBinScript_FoDUploader() {
local toolInstallDir=$1
local jarFile=$2
local binDir=${toolInstallDir}/bin
local binScript=${binDir}/FoDUpload
mkdir -p "${binDir}"
cat <<-EOF > "${binScript}"
#!/bin/bash
java -jar "${jarFile}" "\$@"
EOF
_chmod 755 "${binScript}"
}
######################################################################################################
###
### Functions for installing and configuring ScanCentral Client
###
######################################################################################################
_install_ScanCentralClient() {
local toolAlias=$1
local toolVersion=$2
local toolInstallDir=$3
downloadAndUnzipToolContents "$toolAlias" "$toolVersion" "${toolInstallDir}"
_chmod 755 "${toolInstallDir}/bin/packagescanner"
_chmod 755 "${toolInstallDir}/bin/pwtool"
_chmod 755 "${toolInstallDir}/bin/scancentral"
}
_postInstall_ScanCentralClient() {
local toolAlias=$1
local toolVersion=$2
local toolInstallDir=$3
# Generate or update ScanCentral client.properties file
local clientAuthToken="$(getVar SC_CLIENT_AUTH_TOKEN $(getVar SCANCENTRAL_CLIENT_AUTH_TOKEN))"
local clientPropertiesFile=${toolInstallDir}/Core/config/client.properties
[ -z "${clientAuthToken}" ] || echo "client_auth_token=${clientAuthToken}" > ${clientPropertiesFile}
}
######################################################################################################
###
### Functions for installing and configuring FortifyVulnerabilityExporter
###
######################################################################################################
_install_FortifyVulnerabilityExporter() {
local toolAlias=$1
local toolVersion=$2
local toolInstallDir=$3
downloadAndUnzipToolContents "$toolAlias" "$toolVersion" "${toolInstallDir}"
_addBinScript_FortifyVulnerabilityExporter "${toolInstallDir}" "${toolInstallDir}/FortifyVulnerabilityExporter.jar"
}
_addBinScript_FortifyVulnerabilityExporter() {
local toolInstallDir=$1
local jarFile=$2
local binDir=${toolInstallDir}/bin
local binScript=${binDir}/FortifyVulnerabilityExporter
mkdir -p "${binDir}"
cat <<-EOF > "${binScript}"
#!/bin/bash
java -DpluginDir="${toolInstallDir}/plugins" -jar "${jarFile}" "\$@"
EOF
_chmod 755 "${binScript}"
}
run "$@"