Skip to content

Commit 4d54c39

Browse files
jshartleyj-hartley
authored andcommitted
Work without git context
Problem Users might want to use the script outside of a git repo context Solution Prepare sensible default options in the absence of git repo context Signed-off-by: John Hartley
1 parent 6dd4a86 commit 4d54c39

File tree

1 file changed

+31
-14
lines changed

1 file changed

+31
-14
lines changed

scripts/sbom_scraper.sh

Lines changed: 31 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020

2121
SCRIPTNAME=$(basename "$0")
2222

23-
for TOOL in syft jq xq xmllint python3
23+
for TOOL in syft jq xq xmllint python3 openssl curl shasum
2424
do
2525
if ! type $TOOL > /dev/null
2626
then
@@ -34,25 +34,38 @@ set -u
3434

3535
LOGTAG=$$
3636
log() {
37-
echo "${LOGTAG}:$(date ):$*"
37+
echo "${LOGTAG}:$(date):$*"
3838
}
3939

40-
GIT_STATUS=$(git status --porcelain)
40+
# ----------------------------------------------------------------------------
41+
# Option parsing
42+
# ----------------------------------------------------------------------------
43+
44+
# Prepare defaults
45+
if type git > /dev/null 2>&1 && git rev-parse --git-dir > /dev/null 2>&1
46+
then
47+
# we are in a git repo so set defaults using git
48+
GIT_STATUS=$(git status --porcelain)
49+
50+
AUTHOR_NAME="$(git config user.name || echo "$USER")"
51+
AUTHOR_EMAIL="$(git config user.email || true)"
52+
TOOL_NAME="$(git config --get remote.origin.url) $(git ls-files --full-name "$SCRIPTNAME")"
53+
TOOL_VERSION=$(git describe --tags)${GIT_STATUS:++}
54+
else
55+
AUTHOR_NAME="$USER"
56+
AUTHOR_EMAIL=""
57+
TOOL_NAME="$SCRIPTNAME"
58+
TOOL_VERSION="unknown"
59+
fi
4160

42-
# defaults
4361
FORMAT=cyclonedx
44-
AUTHOR_NAME="$(git config user.name)"
45-
AUTHOR_EMAIL="$(git config user.email)"
4662
COMPONENT_AUTHOR_NAME="$AUTHOR_NAME"
4763
SUPPLIER_NAME=dockerhub
4864
SUPPLIER_URL=https://hub.docker.com
49-
TOOL_NAME="$(git config --get remote.origin.url) $(git ls-files --full-name "$SCRIPTNAME")"
50-
TOOL_VERSION=$(git describe --tags)${GIT_STATUS:++}
5165
TOOL_VENDOR="Jitsuin Inc"
5266
TOOL_HASH_ALG=SHA-256
5367
# shellcheck disable=SC2002
54-
TOOL_HASH_CONTENT=$(cat "$0" | openssl dgst -sha256)
55-
68+
TOOL_HASH_CONTENT=$(shasum -a 256 "$0" | cut -d' ' -f1)
5669
# credentials directory should have 0700 permissions
5770
CLIENTSECRET_FILE=credentials/client_secret
5871
SBOM=false
@@ -68,7 +81,7 @@ Create a Cyclone DX 1.2 XML SBOM from a docker image and upload to RKVST SBOM Hu
6881
Usage: $SCRIPTNAME [-a AUTHOR_NAME] [-A AUTHOR_NAME] [-c CLIENT_SECRET_FILE] [-e AUTHOR_EMAIL] [-s] [-p] [-u URL] CLIENT_ID [docker-image:tag|sbom file]
6982
7083
-a AUTHOR name of the author of the SBOM. Default ($AUTHOR_NAME)
71-
-A COMPONENT_AUTHOR name of the author of the docker image. Default ($COMPONENT_AUTHOR_NAME)
84+
-A COMPONENT_AUTHOR name of the author and publisher of the docker image. Default ($COMPONENT_AUTHOR_NAME)
7285
-c CLIENT_SECRET_FILE containing client secret (default ${CLIENTSECRET_FILE})
7386
-e AUTHOR_EMAIL email address of the author of the SBOM. Default ($AUTHOR_EMAIL)
7487
-s if specified the second argument is an sbom file.
@@ -189,6 +202,7 @@ echo " supplier:"
189202
echo " name: $SUPPLIER_NAME"
190203
echo " url: $SUPPLIER_URL"
191204
echo " author: $COMPONENT_AUTHOR_NAME"
205+
echo " publisher: $COMPONENT_AUTHOR_NAME"
192206
echo " name: $ORIG_COMPONENT_NAME -> $COMPONENT_NAME"
193207
echo " version: $ORIG_COMPONENT_VERSION -> $COMPONENT_VERSION"
194208
echo " hashes:"
@@ -198,11 +212,9 @@ echo " content: $COMPONENT_HASH_CONTENT"
198212

199213
[ -z "$TOOL_VENDOR" ] && echo >&2 "Unable to determine SBOM tool vendor" && exit 1
200214
[ -z "$TOOL_NAME" ] && echo >&2 "Unable to determine SBOM tool name" && exit 1
201-
[ -z "$TOOL_VERSION" ] && echo >&2 "Unable to determine SBOM tool version" && exit 1
202215
[ -z "$TOOL_HASH_ALG" ] && echo >&2 "Unable to determine SBOM tool hash algorithm" && exit 1
203216
[ -z "$TOOL_HASH_CONTENT" ] && echo >&2 "Unable to determine SBOM tool hash content" && exit 1
204217
[ -z "$AUTHOR_NAME" ] && echo >&2 "Unable to determine SBOM author name" && exit 1
205-
[ -z "$AUTHOR_EMAIL" ] && echo >&2 "Unable to determine SBOM author email" && exit 1
206218
[ -z "$SUPPLIER_NAME" ] && echo >&2 "Unable to determine component supplier name" && exit 1
207219
[ -z "$SUPPLIER_URL" ] && echo >&2 "Unable to determine component supplier url" && exit 1
208220
[ -z "$COMPONENT_AUTHOR_NAME" ] && echo >&2 "Unable to determine component author name" && exit 1
@@ -264,7 +276,12 @@ ET.SubElement(author, 'email').text = '$AUTHOR_EMAIL'
264276
265277
component = metadata.find('component', ns)
266278
267-
# Update component author
279+
# Update component publisher and author
280+
publisher = component.find('publisher', ns)
281+
if not publisher:
282+
publisher = ET.Element('publisher')
283+
component.insert(0, publisher)
284+
publisher.text = '$COMPONENT_AUTHOR_NAME'
268285
author = component.find('author', ns)
269286
if not author:
270287
author = ET.Element('author')

0 commit comments

Comments
 (0)