Skip to content

Commit a0af51a

Browse files
GODRIVER-3599 Add task script to generate CycloneDX SBOM
The GODRIVER SBOM (sbom.json) does not contain the direct and transitive dependencies defined in go.mod. Added code to generate a CycloneDX SBOM in order to better meet NITA Minimum Elements for Software Bill of Materials, OWASP Software Component Verification Standard (SCVS) Level 1, as well as include the necessary component identifiers for vulnerability discovery and VEX responses. Added a task, etc/script, and pre-commit hook for generating a CycloneDX SBOM using a pinned version of the cyclonedx-gomod tool. The SBOM includes the aggregate of modules required by packages in the mongo-go-driver library, excluding examples, tests and test packages. The task (generate-sbom) is added to the default tasks and will run only when go.mod is newer than sbom.cdx.json. The pre-commit hook (sbom-currency) ensures that if go.mod is staged for commit, that an updated sbom.json is also staged. Future TODO: Add libmongocrypt as an optional component once the libmongocrypt SBOM is updated with newer automation
1 parent af4285f commit a0af51a

File tree

5 files changed

+436
-11
lines changed

5 files changed

+436
-11
lines changed

.evergreen/config.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -643,6 +643,14 @@ tasks:
643643
binary: bash
644644
args: [*task-runner, govulncheck]
645645

646+
- name: generate-sbom
647+
tags: ["ssdlc"]
648+
commands:
649+
- command: subprocess.exec
650+
params:
651+
binary: bash
652+
args: [*task-runner, generate-sbom]
653+
646654
- name: pull-request-helpers
647655
allowed_requesters: ["patch", "github_pr"]
648656
commands:

.pre-commit-config.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,10 @@ repos:
7171
language: system
7272
types: [go]
7373
entry: etc/check_license.sh
74+
75+
- id: sbom-currency
76+
name: sbom-currency
77+
language: system
78+
types: [json]
79+
require_serial: true
80+
entry: etc/generate-sbom.sh -c

Taskfile.yml

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ tasks:
1111

1212
### Utility tasks. ###
1313
default:
14-
deps: [build, check-license, check-fmt, check-modules, lint, test-short]
14+
deps: [build, check-license, check-fmt, check-modules, lint, test-short, generate-sbom]
1515

1616
add-license: bash etc/check_license.sh -a
1717

@@ -87,6 +87,17 @@ tasks:
8787

8888
govulncheck: bash etc/govulncheck.sh
8989

90+
generate-sbom:
91+
desc: Generate a CycloneDX SBOM
92+
summary: |
93+
Generate a CycloneDX SBOM with the cyclonedx-gomod 'mod' subcommand
94+
The SBOM includes the aggregate of modules required by packages in the mongo-go-driver library, excluding examples, tests and test packages.
95+
Task will run only when go.mod is newer than sbom.cdx.json.
96+
method: timestamp
97+
sources: [go.mod]
98+
generates: [sbom.json]
99+
cmd: bash etc/generate-sbom.sh
100+
90101
update-notices: bash etc/generate_notices.pl > THIRD-PARTY-NOTICES
91102

92103
### Local testing tasks. ###

etc/generate-sbom.sh

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
CHECK_CURRENCY="false"
5+
6+
# Options are:
7+
# -c : check currency of staged sbom.json versus go.mod.
8+
while getopts "c" opt; do
9+
case $opt in
10+
c)
11+
CHECK_CURRENCY="true"
12+
;;
13+
*)
14+
echo "usage: $0 [-c]" >&2
15+
echo " -c : (optional) check currency of staged sbom.json versus go.mod." >&2
16+
exit 1
17+
;;
18+
esac
19+
done
20+
#shift $((OPTIND - 1))
21+
22+
if ! $CHECK_CURRENCY; then
23+
# The cyclonedx-gomod 'mod' subcommand is used to generate a CycloneDX SBOM with GOWORK=off to exclude example/test code.
24+
# TODO: Add libmongocrypt as an optional component via a merge once the libmongocrypt SBOM is updated with newer automation
25+
26+
## The pipe to jq is a temporary workaround until this issue is resolved: https://github.com/CycloneDX/cyclonedx-gomod/issues/662.
27+
## When resolved, bump version and replace with commented line below.
28+
# GOWORK=off go run github.com/CycloneDX/cyclonedx-gomod/cmd/cyclonedx-gomod@[UPDATED VERSION] mod -type library -licenses -assert-licenses -output-version 1.5 -json -output sbom.json .
29+
GOWORK=off go run github.com/CycloneDX/cyclonedx-gomod/cmd/[email protected] mod -type library -licenses -assert-licenses -output-version 1.5 -json . | jq '.metadata.component.purl |= split("?")[0]' | jq '.components[].purl |= split("?")[0]' > sbom.json
30+
elif [[ $(git diff --name-only --cached go.mod) && ! $(git diff --name-only --cached sbom.json) ]]; then
31+
echo "'go.mod' has changed. 'sbom.json' must be re-generated (run 'task generate-sbom' or 'etc/generate-sbom.sh') and staged." && exit 1
32+
fi

0 commit comments

Comments
 (0)