Skip to content

Commit 2766f45

Browse files
James Lowmaneccles
authored andcommitted
Add timeout to SBOM upload
Problem: Curl upload occasionally hangs when running in Azure container instance Solution: Use "timeout" command to limit upload time to 10 seconds (all example SBOM uploads to date have been under 2 seconds) Signed-off-by: James Lowman
1 parent 9c8813a commit 2766f45

File tree

1 file changed

+19
-3
lines changed

1 file changed

+19
-3
lines changed

scripts/sbom_scraper.sh

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ SUPPLIER_NAME=dockerhub
6767
SUPPLIER_URL=https://hub.docker.com
6868
TOOL_VENDOR="Jitsuin Inc"
6969
TOOL_HASH_ALG=SHA-256
70+
SBOM_UPLOAD_TIMEOUT=10
7071
# shellcheck disable=SC2002
7172
TOOL_HASH_CONTENT=$(shasum -a 256 "$0" | cut -d' ' -f1)
7273
# credentials directory should have 0700 permissions
@@ -366,18 +367,33 @@ EOF
366367
# ----------------------------------------------------------------------------
367368
log "Upload ${PRIVACY} ${OUTPUT} ..."
368369

369-
HTTP_STATUS=$(curl -s -w "%{http_code}" -X POST \
370+
HTTP_STATUS=$(timeout ${SBOM_UPLOAD_TIMEOUT} \
371+
curl -s -w "%{http_code}" -X POST \
370372
-o "${TEMPDIR}/upload" \
371373
-H "@${BEARER_TOKEN_FILE}" \
372374
-H "content_type=text/xml" \
373375
-F "sbom=@${PATCHED_OUTPUT}" \
374376
"${URL}/archivist/v1/sboms?privacy=${PRIVACY}")
375377

376-
if [ "${HTTP_STATUS}" != "200" ]
378+
RETURN_CODE=$?
379+
380+
# timeout returns 124 if the command exceeded the time limit
381+
if [ ${RETURN_CODE} -eq 124 ]
377382
then
378-
log "Upload failure ${HTTP_STATUS}"
383+
log "Upload failure: Timeout"
384+
exit 3
385+
# all other non-zero return codes
386+
elif [ ${RETURN_CODE} -gt 0 ]
387+
then
388+
log "Upload failure: Error code ${RETURN_CODE}"
379389
exit 4
380390
fi
391+
392+
if [ "${HTTP_STATUS}" != "200" ]
393+
then
394+
log "Upload failure: HTTP ${HTTP_STATUS}"
395+
exit 5
396+
fi
381397
log "Upload success: "
382398
jq . "${TEMPDIR}/upload"
383399
exit 0

0 commit comments

Comments
 (0)