From 54d8f9aedbe54f3997cf8638982d8513782a4003 Mon Sep 17 00:00:00 2001 From: Paul Hewlett Date: Tue, 9 Nov 2021 14:02:08 +0000 Subject: [PATCH] SBOM scraper script Problem: Extract SBOM from docker image and upload to archivist Solution: sbom_scraper.sh using app registrations flow. Signed-off-by: Paul Hewlett --- scripts/sbom_scraper.sh | 177 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 177 insertions(+) create mode 100755 scripts/sbom_scraper.sh diff --git a/scripts/sbom_scraper.sh b/scripts/sbom_scraper.sh new file mode 100755 index 0000000..83bec7f --- /dev/null +++ b/scripts/sbom_scraper.sh @@ -0,0 +1,177 @@ +#!/usr/bin/env bash +# +# Scrape a docker image and upload as SBOM file +# +# Preparation: +# +# Install syft - https://github.com/anchore/syft +# +# Read the docs on syft usage. +# +# Create App registration called "SBOM scraper" following the flow described in +# https://docs.rkvst.com/docs/setup-and-administration/getting-access-tokens-using-app-registrations/#using-the-rkvst-ui-(required-for-first-time-setup) +# and note down the CLIENT_ID and SECRET. +# +# Copy the SECRET generated to the file specified by ${CLIENTSECRET_FILE} below. This +# file should reside in a subdirectory with 0600 permissions. +# +# Use the CLIENT_ID as the first fixed argument to this script. +# + +SCRIPTNAME=$(basename "$0") + +SYFT=$(which syft) +if [ -z "${SYFT}" ] +then + echo "syft command not found" + exit 10 +fi + +set -e +set -u + +LOGTAG=$$ +log() { + echo "${LOGTAG}:$(date --rfc-3339=seconds):$* ..." +} + +# defaults +FORMAT=cyclonedx + +# credentials directory has 0600 permissions +CLIENTSECRET_FILE=credentials/client_secret +SBOM=false + +URL=https://app.rkvst.io + +usage() { + cat >&2 < "${OUTPUT}" +else + OUTPUT="${DOCKER_IMAGE}" +fi + +# ---------------------------------------------------------------------------- +# Handle client id and secrets for SBOM scraper via App registrations +# ---------------------------------------------------------------------------- +HTTP_STATUS="" +# get token +log "Get token" +HTTP_STATUS=$(curl -sS -w "%{http_code}" \ + -o "${TEMPDIR}/access_token" \ + --data-urlencode "grant_type=client_credentials" \ + --data-urlencode "client_id=${CLIENT_ID}" \ + --data-urlencode "client_secret=${SECRET}" \ + "${URL}/archivist/iam/v1/appidp/token") +if [ "${HTTP_STATUS}" != "200" ] +then + log "Get token failure ${HTTP_STATUS}" + exit 2 +fi + +TOKEN=$(jq -r .access_token "${TEMPDIR}"/access_token ) + +# create token file +BEARER_TOKEN_FILE=${TEMPDIR}/token +cat > "${BEARER_TOKEN_FILE}" <