-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathget-blackduck-report.sh
executable file
·283 lines (244 loc) · 7.35 KB
/
get-blackduck-report.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
#!/usr/bin/env bash
set -E
#### Functions
function show_banner {
echo "=============================================================="
echo " Create Black Duck Report "
echo "=============================================================="
echo
}
function show_usage {
echo
echo "Usage: ./get-blackduck-report.sh [blackduck-url] [blackduck-api-token] [project-name] [version-name] <report-type> <report-format>"
}
function get_bearer {
result=$(curl --silent --location --request POST "${blackduck_url}/api/tokens/authenticate" \
--header "Authorization: token $blackduck_token")
if [ "$(echo "$result" | jq -r .errorCode)" != null ]
then
>&2 echo "ERROR: No bearer token found"
exit 1
else
echo "$result" | jq -r .bearerToken
fi
}
function get_project_id {
result=$(curl --silent -G "${blackduck_url}/api/projects" --data-urlencode "q=name:${project}" \
--header "Authorization: Bearer ${bearer_token}" )
if [ "$(echo "$result" | jq -r .totalCount)" -eq 0 ]
then
>&2 echo "ERROR: No project found with name: $project"
exit 1
else
echo "$result" | jq -r .items[0]._meta.href
fi
}
function get_version_id {
result=$(curl --silent -G "${project_api_url}/versions" --data-urlencode "q=versionName:${version}" \
--header "Authorization: Bearer $bearer_token")
if [ "$(echo "$result" | jq -r .totalCount)" -eq 0 ]
then
>&2 echo "ERROR: No version found with name: $version"
exit 1
else
echo "$result" | jq -r .items[0]._meta.href
fi
}
function get_scan_status {
scan_status="IN_PROGRESS"
max_retries=50
retries=0
while [ "$scan_status" = "IN_PROGRESS" ]
do
((retries++))
if [ "$retries" -gt "$max_retries" ];
then
>&2 echo "ERROR: max retries reached"
exit 1
fi
echo "| attempt $retries of $max_retries to get scan status"
sleep 15
result=$(curl --silent --location --get "$version_api_url/codelocations" \
--header "Authorization: Bearer $bearer_token" \
--header 'Content-Type: application/json')
scan_status=$(echo "$result" | jq -r '.items[] | select(.status[] | .operationNameCode == "ServerScanning" and .status != "COMPLETED")')
if [ -z "$scan_status" ];
then
scan_status="COMPLETED"
else
scan_status="IN_PROGRESS"
fi
echo "| - scan_status: $scan_status"
done
if [ "$scan_status" != "COMPLETED" ];
then
>&2 echo " ERROR: scan_status is not COMPLETED, it is $scan_status."
exit 1
fi
}
function create_sbom_report {
dataraw="{\"reportFormat\": \"$report_format\", \"reportType\" : \"SBOM\", \"sbomType\" : \"$sbom_type\"}"
result=$(curl --silent --location --request POST "$version_api_url/sbom-reports" \
--header "Authorization: Bearer $bearer_token" \
--header 'Content-Type: application/json' \
--data-raw "$dataraw" )
if [ "$result" != "" ]
then
>&2 echo "ERROR: error in creating sbom report"
>&2 echo "$result"
exit 1
fi
}
function get_license_report_endpoint {
result=$(curl --silent -G "${project_api_url}/versions" --data-urlencode "q=versionName:${version}" \
--header "Authorization: Bearer $bearer_token")
if [ "$(echo "$result" | jq -r .totalCount)" -eq 0 ]
then
>&2 echo "ERROR: Cannot obtain license report endpoint for version: $version"
exit 1
else
echo "$result" | jq -r '.items[0]._meta.links[] | select(.rel=="licenseReports") | .href'
fi
}
function create_version_license_report {
version_report_api=$(get_license_report_endpoint)
dataraw="{\"reportFormat\": \"TEXT\", \"reportType\" : \"VERSION_LICENSE\"}"
result=$(curl --silent --location --request POST "$version_report_api" \
--header "Authorization: Bearer $bearer_token" \
--header 'Content-Type: application/json' \
--data-raw "$dataraw" )
if [ "$result" != "" ]
then
>&2 echo "ERROR: error in creating license report"
>&2 echo "$result"
exit 1
fi
}
function get_report_id {
report_status="IN_PROGRESS"
max_retries=50
retries=0
while [ "$report_status" = "IN_PROGRESS" ]
do
((retries++))
if [ "$retries" -gt "$max_retries" ];
then
>&2 echo "ERROR: max retries reached"
exit 1
fi
echo "| attempt $retries of $max_retries to get SDPX report"
sleep 15
result=$(curl --silent --location --get "$version_api_url/reports" \
--header "Authorization: Bearer $bearer_token" \
--header 'Content-Type: application/json')
report_api_url=$(echo "$result" | jq -r '.items[0]._meta.href')
report_status=$(echo "$result" | jq -r '.items[0].status')
echo "| - report_status: $report_status"
done
if [ "$report_status" != "COMPLETED" ];
then
>&2 echo " ERROR: report_status is not COMPLETED, it is $report_status."
exit 1
fi
}
function download_sbom_report {
curl --silent --location --get "$report_api_url/download.zip" \
-o report.zip \
--header "Authorization: Bearer $bearer_token" \
--header 'Content-Type: application/zip'
}
function get_report_contents {
curl --silent --location --get "$report_api_url/contents" \
--header "Authorization: Bearer $bearer_token" \
--header 'Content-Type: application/json' | jq -rc .reportContent[0].fileContent
}
#### Main program
show_banner
error=false
if [ -z "$1" ]
then
echo "ERROR: No blackduck-url supplied"
error=true
fi
if [ -z "$2" ]
then
echo "ERROR: No blackduck-api-token supplied"
error=true
fi
if [ -z "$3" ]
then
echo "ERROR: No project-name supplied"
error=true
fi
if [ -z "$4" ]
then
echo "ERROR: No version-name supplied"
error=true
fi
if [ -z "$5" ]
then
echo "INFO: No report-format supplied. Defaulting to JSON report-format"
fi
if [ $error == "true" ]
then
show_usage
exit 1
fi
blackduck_url=$1
blackduck_token=$2
project=$3
version=$4
sbom_type=${5:-"SPDX_22"}
if [ "$sbom_type" == "CYCLONEDX_13" ] || [ "$sbom_type" == "CYCLONEDX_14" ]
then
echo "INFO: sbomType \"CYCLONEDX_13\" or \"CYCLONEDX_14\" allows reportFormat values of \"JSON\"."
report_format="JSON"
elif [ "$sbom_type" == "VERSION_LICENSE" ]
then
report_format="TEXT"
else
report_format=${6:-"JSON"}
fi
echo "+ getting bearer"
bearer_token=$(get_bearer)
echo "| got bearer"
echo
echo "+ getting project api base url for project: ${project}"
project_api_url=$(get_project_id)
echo "| got project api base url: ${project_api_url}"
echo
echo "+ getting version api base url"
version_api_url=$(get_version_id)
echo "| got version api base url: ${version_api_url}"
echo
echo "+ getting scan status"
get_scan_status
echo "+ got scan status"
echo "+ creating SBOM report"
if [ "${NO_CREATE}" == true ]
then
echo "| We're not creating a new report for the because of the secret environment variable NO_CREATE"
elif [ "$sbom_type" == "VERSION_LICENSE" ]
then
create_version_license_report
echo "| triggered creating VERSION LICENSE report"
else
create_sbom_report
echo "| triggered creating SBOM report"
fi
echo
echo "+ getting SBOM report api base url"
get_report_id
echo "| got SBOM report status: ${report_status}"
echo "| got SBOM report api base url: ${report_api_url}"
echo
echo "+ getting SBOM report"
download_sbom_report
echo "| got SBOM report"
echo
echo "+ getting content information"
report_contents=$(get_report_contents)
echo "| got content information"
echo
echo "sbom-file=report.zip" >> "$GITHUB_OUTPUT"
echo "sbom-contents=${report_contents}" >> "$GITHUB_OUTPUT"