-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-sbom.sh
executable file
·254 lines (188 loc) · 7.26 KB
/
generate-sbom.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
#!/bin/bash
# set -x
set -euo pipefail
# CONFIGS
OUTPUT_FORMAT=${1:-'cyclonedx-json'}
OUTPUT_DIR=${2:-'structured_sbom_outputs'}
EXECUTE_PATH=${3:-'/'}
# SBOM
# <<-- GO SBOM
bom_go() {
echo "Starting GO sbom generation..........."
local dir=${1:4}
echo "Output SBOM format: $OUTPUT_FORMAT"
echo "Using SBOM tool: cyclonedx-gomod"
echo "Output file path relative to output directory: $dir"
mkdir -p $OUTPUT_DIR$dir
$(cyclonedx-gomod mod -$OUTPUT_FORMAT_DX -licenses -output $OUTPUT_DIR$dir/golang_bom.$OUTPUT_FORMAT_DX $1)
if [ "$OUTPUT_FORMAT" == "spdx-json" ]; then
cyclonedx-cli convert --input-file $OUTPUT_DIR$dir/golang_bom.$OUTPUT_FORMAT_DX --output-format spdxjson --output-file $OUTPUT_DIR$dir/golang_bom_spdx.$OUTPUT_FORMAT_DX
rm -f $OUTPUT_DIR$dir/golang_bom.$OUTPUT_FORMAT_DX
fi
echo "Finished GO sbom generation"
}
# -->>
# <<-- NODE SBOM
bom_node() {
echo "Starting Node sbom generation..........."
local dir=${2:4}
local clean_up=0;
echo "Output SBOM format: $OUTPUT_FORMAT"
echo "Using SBOM tool: cyclonedx-npm"
echo "Output file path relative to output directory: $dir"
mkdir -p $OUTPUT_DIR$dir
if [[ ! -d "${2}/node_modules" ]]; then
pushd ${2}
npm install
clean_up=1;
popd
fi
cyclonedx-npm --output-file $OUTPUT_DIR$dir/node_bom.$OUTPUT_FORMAT_DX --output-format $OUTPUT_FORMAT_DX ${1}
if [[ $clean_up == 1 ]]; then
rm -rf ${2}/node_modules
fi
if [ "$OUTPUT_FORMAT" == "spdx-json" ]; then
cyclonedx-cli convert --input-file $OUTPUT_DIR$dir/node_bom.$OUTPUT_FORMAT_DX --output-format spdxjson --output-file $OUTPUT_DIR$dir/node_bom_spdx.$OUTPUT_FORMAT_DX
rm -f $OUTPUT_DIR$dir/node_bom.$OUTPUT_FORMAT_DX
fi
echo "Finished Node sbom generation"
}
# -->>
# <<-- RUBY SBOM
bom_ruby() {
echo "Starting Ruby sbom generation..........."
local dir=${1:4}
echo "Output SBOM format: $OUTPUT_FORMAT"
echo "Using SBOM tool: cyclonedx-ruby"
echo "Output file path relative to output directory: $dir"
mkdir -p $OUTPUT_DIR$dir
cyclonedx-ruby -p $1 -f $OUTPUT_FORMAT_DX -o $OUTPUT_DIR$dir/ruby_bom.$OUTPUT_FORMAT_DX
if [ "$OUTPUT_FORMAT" == "spdx-json" ]; then
cyclonedx-cli convert --input-file $OUTPUT_DIR$dir/ruby_bom.$OUTPUT_FORMAT_DX --output-format spdxjson --output-file $OUTPUT_DIR$dir/ruby_bom_spdx.$OUTPUT_FORMAT_DX
rm -f $OUTPUT_DIR$dir/ruby_bom.$OUTPUT_FORMAT_DX
fi
echo "Finished Ruby sbom generation"
}
# -->>
# <<-- PYTHON SBOM
bom_python() {
echo "Starting Python sbom generation..........."
local dir=${2:4}
echo "Output SBOM format: $OUTPUT_FORMAT"
echo "Using SBOM tool: cyclonedx-py"
echo "Output file path relative to output directory: $dir"
mkdir -p $OUTPUT_DIR$dir
cyclonedx-py $3 -i $1 --format $OUTPUT_FORMAT_DX -o $OUTPUT_DIR$dir/python_bom.$OUTPUT_FORMAT_DX
if [ "$OUTPUT_FORMAT" == "spdx-json" ]; then
cyclonedx-cli convert --input-file $OUTPUT_DIR$dir/python_bom.$OUTPUT_FORMAT_DX --output-format spdxjson --output-file $OUTPUT_DIR$dir/python_bom_spdx.$OUTPUT_FORMAT_DX
rm -f $OUTPUT_DIR$dir/python_bom.$OUTPUT_FORMAT_DX
fi
echo "Write output to: $OUTPUT_DIR$dir/python_bom.$OUTPUT_FORMAT_DX"
echo "Finished Python sbom generation"
}
# -->>
# <<-- PHP SBOM
bom_php() {
# echo "Exection location is ${2}"
echo "Starting PHP sbom generation..........."
local dir=${2:4}
echo "Output SBOM format: $OUTPUT_FORMAT"
echo "Output file path relative to output directory: $dir"
mkdir -p $OUTPUT_DIR$dir
composer make-bom --working-dir=$2 --output-format=$OUTPUT_FORMAT_DX --output-file=$OUTPUT_DIR$dir/php_composer_bom.$OUTPUT_FORMAT_DX $1
if [ "$OUTPUT_FORMAT" == "spdx-json" ]; then
cyclonedx-cli convert --input-file $OUTPUT_DIR$dir/php_composer_bom.$OUTPUT_FORMAT_DX --output-format spdxjson --output-file $OUTPUT_DIR$dir/php_composer_bom_spdx.$OUTPUT_FORMAT_DX
rm -f $OUTPUT_DIR$dir/php_composer_bom.$OUTPUT_FORMAT_DX
fi
echo "Finished PHP sbom generation"
}
# -->>
# <<-- CPP SBOM
bom_cpp() {
echo "Starting Conan sbom generation..........."
local dir=${2:4}
echo "Output SBOM format: $OUTPUT_FORMAT"
echo "Using SBOM tool: cyclonedx-conan"
echo "Output file path relative to output directory: $dir"
mkdir -p $OUTPUT_DIR$dir
cyclonedx-conan -s compiler.version=11 $1 > /dev/null
cyclonedx-conan -s compiler.version=11 $1 1> $OUTPUT_DIR$dir/cpp_conan_bom.$OUTPUT_FORMAT_DX
if [ "$OUTPUT_FORMAT" == "spdx-json" ]; then
cyclonedx-cli convert --input-file $OUTPUT_DIR$dir/cpp_conan_bom.$OUTPUT_FORMAT_DX --output-format spdxjson --output-file $OUTPUT_DIR$dir/cpp_conan_bom_spdx.$OUTPUT_FORMAT_DX
rm -f $OUTPUT_DIR$dir/cpp_conan_bom.$OUTPUT_FORMAT_DX
fi
echo "Finished Conan sbom generation"
}
# -->>
# <<-- JAVA SBOM
bom_java() {
echo "Starting Java sbom generation..........."
local dir=${1:4}
echo "Output SBOM format: $OUTPUT_FORMAT"
echo "Using SBOM tool: syft"
echo "Output file path relative to output directory: $dir"
mkdir -p $OUTPUT_DIR$dir
syft $1 --catalogers java -o $OUTPUT_FORMAT=$OUTPUT_DIR$dir/java_bom.$OUTPUT_FORMAT_DX
echo "Finished Java sbom generation"
}
# -->>
heartbeat() {
while true
do
echo "[$(date)] SBOM in progress"
sleep 600
done
}
## Main Exection
execute() {
pwd
if [[ -z $EXECUTE_PATH ]]; then
echo "no path provided"
exit 0
fi
case ${OUTPUT_FORMAT} in
"cyclonedx-json") readonly OUTPUT_FORMAT_DX="json"
;;
"cyclonedx-xml") readonly OUTPUT_FORMAT_DX="xml"
;;
"spdx-json") readonly OUTPUT_FORMAT_DX="json"
;;
esac
echo "Scanning source code..."
readarray -td, a < <(printf '%s' "$EXECUTE_PATH"); declare -p a >/dev/null;
mkdir -p raw_tool_outputs
heartbeat &
for i in "${a[@]}"; do
echo "Scanning input directory $i"
for file in `find /app${i} -maxdepth 1 -type f \( -iname "package-lock.json" -o -iname "go.mod" -o -iname "Gemfile.lock" -o -iname "Pipfile.lock" -o -iname "poetry.lock" -o -iname "requirements.txt" -o -iname "composer.json" -o -iname "conanfile.txt" -o -iname "pom.xml" -o -iname "build.gradle" \)`; do
local dir=$(dirname "$file")
case ${file} in
*"/Gemfile.lock") echo "Searching gemfile..." && bom_ruby $dir
;;
*"/go.mod") echo "Searching go files..." && bom_go $dir
;;
*"/package-lock.json") echo "Searching node package files..." && bom_node $file $dir
;;
*"/Pipfile.lock") echo "Searching python package files..." && bom_python $file $dir "-pip"
;;
*"/poetry.lock") echo "Searching python package files..." && bom_python $file $dir "-p"
;;
*"/requirements.txt") echo "Searching python package files..." && bom_python $file $dir "-r"
;;
*"/composer.json") echo "Searching composer package files..." && bom_php $file $dir
;;
*"/conanfile.txt") echo "Searching conan package files..." && bom_cpp $file $dir
;;
*"/pom.xml" | *"/build.gradle") echo "Searching java package files..." && bom_java $dir
;;
esac
done;
done;
echo "Successfully generated sbom files"
kill `jobs -p`
#fle integrity stamping
echo "Creating SHA256 Checksum.........."
`find $OUTPUT_DIR/ ! -name '*sha256sums.txt' -type f -exec sha256sum {} > $OUTPUT_DIR/sha256sums.txt \;`
echo "Successfully Created SHA256 Checksum........."
}
execute