eBPF-based security platform
backend: add a job that stores an image's SBOM
John Fastabend0cc0fcf3e8e7
diff --git a/Documentation/contribute/testing/test-coverage.rst b/Documentation/contribute/testing/test-coverage.rstindex fd99e680..b9e6b704 100644--- a/Documentation/contribute/testing/test-coverage.rst+++ b/Documentation/contribute/testing/test-coverage.rst@@ -977,12 +977,14 @@ trust from ``untrusted`` to ``enrolled``, each status check and thehistory it reads, ``node_vulns`` and its inputs, theretention script and the views that keep boot baselines, the treeingest job on a single publication and on a walk over sealed segments,-the SBOM scan job on a store that holds an image in every state, and-the queries of :src:`tools/xtop/sql/nodes.sql` and-:src:`tools/xtop/sql/shields.sql`, with the names and types of their-columns, over nodes healthy, unhealthy, stale and without a tree or an-image, and a tree with one upatch deployed and one still rolling out-(``test/xtop-nodes``, ``test/xtop-shields``).+the SBOM scan job on a store that holds an image in every state, the+SBOM put job into a ``file://`` and an ``s3://`` store, again and with+another SBOM, and what it refuses (``test/sbom-put``), and the queries+of :src:`tools/xtop/sql/nodes.sql` and :src:`tools/xtop/sql/shields.sql`,+with the names and types of their columns, over nodes healthy,+unhealthy, stale and without a tree or an image, and a tree with one+upatch deployed and one still rolling out (``test/xtop-nodes``,+``test/xtop-shields``).The unit tests of :src:`tools/xtop/test/` cover how xtop turns the rowsof those queries into nodes and shields, the health percent, thediff --git a/Documentation/contribute/testing/testing.rst b/Documentation/contribute/testing/testing.rstindex a80e06f7..ed1e4b3f 100644--- a/Documentation/contribute/testing/testing.rst+++ b/Documentation/contribute/testing/testing.rst@@ -1203,7 +1203,10 @@ The tests of ``jobs/sbom-scan`` run the job the same way, on the storeof place, with ``test/stub-trivy.sh`` in place of Trivy and``test/stub-aws.sh`` in place of ``aws``. The stub matches nothing: itprints the report in ``test/trivy/`` named by the SHA-256 of the SBOM it-is given. A script that exits with status 77 skips its test and says+is given. ``test/sbom-put`` stores two of those SBOMs with+``jobs/sbom-put`` into a ``file://`` store and through the same AWS stub+into an ``s3://`` one, runs ``--scan`` on the first, and checks what it+refuses. A script that exits with status 77 skips its test and sayswhy. ``test/sbom-scan-trivy`` is skipped unless ``trivy`` is on the hostand ``SBOM_TRIVY_CACHE`` names a Trivy cache directory with a database,since its findings would change with every database CI could fetch.diff --git a/Documentation/operate/backend/exposure.rst b/Documentation/operate/backend/exposure.rstindex a7821dcb..f9de61a6 100644--- a/Documentation/operate/backend/exposure.rst+++ b/Documentation/operate/backend/exposure.rst@@ -47,7 +47,8 @@ Inputs------:src:`tools/backend/jobs/sbom-scan` writes ``sbom_images`` and-``sbom_vulns``, see :doc:`sbom-scan`. Nothing in the backend fills+``sbom_vulns`` from the SBOM store, see :doc:`sbom-scan`, which an image+pipeline fills itself or with :doc:`sbom-put`. Nothing in the backend fills``kernel_vulns``: the operator writes it.``shirudo.sbom_images``:diff --git a/Documentation/operate/backend/index.rst b/Documentation/operate/backend/index.rstindex bfb6b191..392d86bb 100644--- a/Documentation/operate/backend/index.rst+++ b/Documentation/operate/backend/index.rst@@ -78,6 +78,9 @@ Fileskeeps a record and ``shirudo.boot_first`` a boot.* - :src:`tools/backend/jobs/tree-ingest`- Loads every segment of a upatch tree into the reference tables.+ * - :src:`tools/backend/jobs/sbom-put`+ - Stores the CycloneDX SBOM of an image in the SBOM store, for an+ image pipeline to call (:doc:`sbom-put`).* - :src:`tools/backend/jobs/sbom-scan`- Scans the SBOM of every image in the SBOM store and loads theoutcome into ``sbom_images`` and ``sbom_vulns``.@@ -98,6 +101,7 @@ Filesexposuretree-ingestsbom-scan+ sbom-putmaintainboot-baselinesxtopdiff --git a/Documentation/operate/backend/sbom-put.rst b/Documentation/operate/backend/sbom-put.rstnew file mode 100644index 00000000..e9b9c4e6--- /dev/null+++ b/Documentation/operate/backend/sbom-put.rst@@ -0,0 +1,96 @@+..+ SPDX-License-Identifier: BSD-2-Clause+ Copyright (c) 2026 Authors of shirudo++:page-type: How-to+:audience: Operators++Store an image's SBOM+=====================++:doc:`sbom-scan` reads the SBOMs from a store, one directory per image.+An image pipeline that does not write that layout itself hands each+image's SBOM to :src:`tools/backend/jobs/sbom-put`, which checks it and+writes the image's directory. It is POSIX shell, with+:src:`tools/backend/jobs/sbom-rows.py` to read the SBOM, so the host that+runs it needs ``python3`` and, for a store in S3, the AWS command line+``aws``; with ``--scan``, what :doc:`sbom-scan` needs too.++1. Build the image and its CycloneDX JSON SBOM.+2. Store it under the key the image's nodes report in their keepalive,+ its ``image_id``, ``image_version`` and, for an image with verity,+ ``image_root``:++ .. code-block:: console++ $ jobs/sbom-put --store s3://fleet-sboms --image-id fleet-os \+ --image-version 2026.11.1 \+ --image-root a3c3f1b0e4c2d7f09e3b6b2a8b1d5e7c4f6a9d0b2c8e1f3a5b7d9c0e2f4a6b8c \+ --built-at 2026-11-02T08:00:00Z sbom.cdx.json+ sbom-put: fleet-os/2026.11.1/a3c3f1b0e4c2d7f09e3b6b2a8b1d5e7c4f6a9d0b2c8e1f3a5b7d9c0e2f4a6b8c: stored, sha256 fd2407ebf451915ddb23cd4cabd3cee23db62e7f1d7a81c8c3cfb3b1c48596e4, components: 4++3. Scan it now, rather than at the next scheduled run of+ :doc:`sbom-scan`, by adding ``--scan``, which runs that job over the+ whole store once the image is in it.++Options+-------++.. list-table::+ :header-rows: 1++ * - Option+ - Value+ * - ``--store <url>``+ - ``file://<directory>`` or ``s3://<bucket>[/<prefix>]``, the store+ :doc:`sbom-scan` reads; ``SBOM_STORE`` in its place.+ * - ``--image-id <id>``, ``--image-version <version>``+ - The image's id and version: 1 to 255 characters of+ ``[A-Za-z0-9._~+-]`` that do not start with a dot.+ * - ``--image-root <root>``+ - The root hash of the image's verity tree, 64 lowercase hex+ digits. Left out or empty for an image built without verity,+ stored under ``-``.+ * - ``--built-at <time>``+ - When the image was built, UTC as ``YYYY-MM-DDThh:mm:ssZ``. Default:+ now.+ * - ``--scan``+ - Run :src:`tools/backend/jobs/sbom-scan` over the store afterwards,+ which reads its own variables, and exit with its status.++What it writes+--------------++The image's directory, as :doc:`sbom-scan` reads it: the SBOM as+``sbom.cdx.json``, byte for byte, then an ``image.json`` naming the key,+``cyclonedx-json``, the SBOM's SHA-256 and the build time. ``image.json``+goes last and, in a ``file://`` store, each file is renamed into place,+so a scan running meanwhile sees the image whole or not at all.++The SBOM is checked as the scan would check it before anything is+written: it must be JSON with ``"bomFormat": "CycloneDX"`` and components+that have names. A key out of form, a time out of form, or an SBOM the+scan would refuse leaves the store as it was, with status 1.++The store is written once. An image already in it with the same SBOM, by+SHA-256, is left as it is, and the job says so and exits with status 0,+so a pipeline can run it again. One with another SBOM is refused with+status 1: a rebuild that changes the SBOM is a new version, or a new+root.++Exit status+-----------++0 when the image is in the store, and was scanned with ``--scan``; 1+when the image was refused, could not be read or written, or the scan+failed; 2 for wrong usage.++Connection+----------++For an ``s3://`` store it runs ``aws s3 cp``, once with ``--recursive``+to read the image's ``image.json`` if there is one and once per file it+writes, with the credentials and region of AWS's own environment and+configuration. ``S3_CLIENT`` names another command with its options,+split at spaces, as for :doc:`sbom-scan`. Its user needs to read and+write the store's objects.diff --git a/Documentation/operate/backend/sbom-scan.rst b/Documentation/operate/backend/sbom-scan.rstindex dd3a276a..b2debcb7 100644--- a/Documentation/operate/backend/sbom-scan.rst+++ b/Documentation/operate/backend/sbom-scan.rst@@ -27,7 +27,7 @@ What it reads-------------One directory per image, which the image pipeline writes once and never-changes; neither shirudo nor this job writes it:+changes, itself or through :doc:`sbom-put`; this job never writes it:.. code-block:: none@@ -164,7 +164,8 @@ When to run itDaily, after updating Trivy's database, and more often than``sbom_stale_seconds`` allows; and, if you like, when the image pipeline-has stored a new image, which is otherwise scanned at the next run. With+has stored a new image, which is otherwise scanned at the next run:+``sbom-put --scan`` does that. With``SBOM_STORE`` and ``TRIVY_CACHE_DIR``, which Trivy reads too, in``/etc/shirudo-backend/sbom-scan.env``:diff --git a/tools/backend/jobs/sbom-put b/tools/backend/jobs/sbom-putnew file mode 100755index 00000000..fc58140e--- /dev/null+++ b/tools/backend/jobs/sbom-put@@ -0,0 +1,154 @@+#!/bin/sh+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo+#+# jobs/sbom-put -- store the CycloneDX SBOM of an image in the SBOM store+# that jobs/sbom-scan reads, for an image pipeline to call once it has+# built an image and its SBOM.+#+# jobs/sbom-put [--store <url>] --image-id <id> --image-version <version>+# [--image-root <root>] [--built-at <YYYY-MM-DDThh:mm:ssZ>]+# [--scan] <sbom.cdx.json>+#+# It checks the image's key and the SBOM as jobs/sbom-scan would and+# writes <image_id>/<image_version>/<image_root or ->/ in the store: the+# SBOM, then image.json, which names its SHA-256 and is what makes a scan+# read the image. The store is written once: an image already there with+# the same SBOM is left as it is, one with another SBOM is refused. With+# --scan, jobs/sbom-scan runs next, over the whole store, so the image's+# findings reach the backend now rather than at the next scheduled scan.+#+# SBOM_STORE the store, in place of --store.+# S3_CLIENT the AWS command line and its options, split at+# spaces, for an s3:// store. Default: aws.+#+# jobs/sbom-scan reads its own variables for --scan.+#+# Exit status: 0 when the image is in the store, and was scanned with+# --scan; 1 when the image was refused, could not be stored, or the scan+# failed; 2 for wrong usage.++set -eu++here=$(cd "$(dirname "$0")" && pwd)+store=${SBOM_STORE:-}+image_id=+image_version=+image_root=-+built_at=+scan=0+work=++die()+{+ printf 'sbom-put: %s\n' "$1" >&2+ exit 1+}++usage()+{+ printf '%s\n' \+ 'usage: sbom-put [--store <url>] --image-id <id> --image-version <version>' \+ ' [--image-root <root>] [--built-at <YYYY-MM-DDThh:mm:ssZ>]' \+ ' [--scan] <sbom.cdx.json>' >&2+ exit 2+}++cleanup()+{+ [ -z "$work" ] || rm -rf "$work"+}++s3()+{+ # shellcheck disable=SC2086 # a command with its options, to be split+ ${S3_CLIENT:-aws} "$@"+}++while [ $# -gt 0 ]; do+ case "$1" in+ --store|--image-id|--image-version|--image-root|--built-at)+ [ $# -ge 2 ] || usage+ case "$1" in+ --store) store=$2 ;;+ --image-id) image_id=$2 ;;+ --image-version) image_version=$2 ;;+ --image-root) image_root=$2 ;;+ --built-at) built_at=$2 ;;+ esac+ shift 2 ;;+ --scan)+ scan=1+ shift ;;+ -*)+ usage ;;+ *)+ break ;;+ esac+done+[ $# -eq 1 ] && [ -n "$store" ] && [ -n "$image_id" ] && [ -n "$image_version" ] ||+ usage+sbom=$1+# An empty root is an image built without verity, as in image.json.+[ -n "$image_root" ] || image_root=-+[ -n "$built_at" ] || built_at=$(date -u +%Y-%m-%dT%H:%M:%SZ)++trap cleanup EXIT+trap 'exit 1' HUP INT TERM+work=$(mktemp -d)+mkdir "$work/new" "$work/old"++python3 "$here/sbom-rows.py" put "$work/new" "$image_id" "$image_version" \+ "$image_root" "$built_at" "$sbom" >"$work/put" || exit 1+IFS=' ' read -r path sha components <"$work/put"++# stored <image.json>: whether the store already holds this image with+# this SBOM; an image.json naming another refuses the image.+stored()+{+ [ -f "$1" ] || return 1+ python3 "$here/sbom-rows.py" stored "$1" "$sha" ||+ die "$path is already in the store with another SBOM; the store is written once"+}++case "$store" in+file://?*)+ directory=${store#file://}+ [ -d "$directory" ] || die "no store directory $directory"+ target=$directory/$path+ if stored "$target/image.json"; then+ printf 'sbom-put: %s: already stored with this SBOM\n' "$path"+ else+ mkdir -p "$target" || die "cannot make $target"+ # Each file is renamed into place, the SBOM first: a scan in+ # between sees no image.json, or a whole one naming a whole SBOM.+ for name in sbom.cdx.json image.json; do+ cp "$work/new/$name" "$target/.$name.tmp" &&+ mv -f "$target/.$name.tmp" "$target/$name" ||+ die "cannot write $target/$name"+ done+ printf 'sbom-put: %s: stored, sha256 %s, components: %s\n' \+ "$path" "$sha" "$components"+ fi ;;+s3://?*)+ target=${store%/}/$path+ # A prefix with nothing under it copies nothing and succeeds.+ s3 s3 cp --recursive --only-show-errors "$target/" "$work/old" \+ --exclude '*' --include image.json </dev/null ||+ die "cannot read $target"+ if stored "$work/old/image.json"; then+ printf 'sbom-put: %s: already stored with this SBOM\n' "$path"+ else+ for name in sbom.cdx.json image.json; do+ s3 s3 cp --only-show-errors "$work/new/$name" "$target/$name" </dev/null ||+ die "cannot write $target/$name"+ done+ printf 'sbom-put: %s: stored, sha256 %s, components: %s\n' \+ "$path" "$sha" "$components"+ fi ;;+*)+ die "the store $store is neither file://<directory> nor s3://<bucket>" ;;+esac++[ "$scan" -eq 1 ] || exit 0+exec "$here/sbom-scan" --store "$store"diff --git a/tools/backend/jobs/sbom-rows.py b/tools/backend/jobs/sbom-rows.pyindex 5c262a4c..0786eb6d 100755--- a/tools/backend/jobs/sbom-rows.py+++ b/tools/backend/jobs/sbom-rows.py@@ -2,12 +2,16 @@# SPDX-License-Identifier: BSD-2-Clause# Copyright (c) 2026 Authors of shirudo#-# jobs/sbom-rows.py -- the parts of jobs/sbom-scan that shell does not-# reach: reading image.json and the SBOMs, and the matcher's JSON.+# jobs/sbom-rows.py -- the parts of jobs/sbom-scan and jobs/sbom-put that+# shell does not reach: reading image.json and the SBOMs, and the+# matcher's JSON.## sbom-rows.py files# sbom-rows.py images <store directory> <work directory># sbom-rows.py rows <work directory> <scanned_at>+# sbom-rows.py put <directory> <image_id> <image_version> <image_root or ->+# <built_at> <sbom>+# sbom-rows.py stored <image.json> <sbom_sha256>## files prints the names of the objects an image directory may hold that# the job reads, one per line.@@ -25,9 +29,19 @@# seconds since the epoch, on every row, and summary, one line per image# loaded.#+# put checks an image's key and build time and its CycloneDX SBOM as+# images would, and writes into the directory, which must be empty, the+# image.json and sbom.cdx.json that make the image's directory in the+# store. It prints the image's path in the store, the SHA-256 of the+# SBOM and the number of its components, separated by tabs.+#+# stored says whether the image.json of an image already in the store+# names that SHA-256 and format: status 0 when it does, 1 when it names+# another SBOM or cannot be read.+## Exit status: 0; 3 when an object or a matcher's output was left out and-# named on standard error, the rest written; 1 when nothing could be done;-# 2 for wrong usage.+# named on standard error, the rest written; 1 when nothing could be done+# or put refused the image; 2 for wrong usage.import calendarimport hashlib@@ -402,7 +416,68 @@ def rows_main(work, scanned_at):return LEFT_OUT if left_out else 0+def put_main(directory, image_id, image_version, root, built_at, sbom):+ """Stage the image.json and SBOM of one image for the store."""+ try:+ if not SEGMENT.fullmatch(image_id):+ raise Refused("the image id is not 1 to 255 of [A-Za-z0-9._~+-]"+ " that do not start with a dot")+ if not SEGMENT.fullmatch(image_version):+ raise Refused("the image version is not 1 to 255 of"+ " [A-Za-z0-9._~+-] that do not start with a dot")+ if root != "-" and not HEX64.fullmatch(root):+ raise Refused("the image root is not 64 lowercase hex digits")+ try:+ epoch(built_at)+ except Refused:+ raise Refused('the build time is not a UTC "YYYY-MM-DDThh:mm:ssZ"')+ name, reader = FORMATS[DEFAULT_FORMAT]+ with open(sbom, "rb") as f:+ data = f.read()+ components = reader(data)+ except Refused as reason:+ print("%s: %s" % (PROG, reason), file=sys.stderr)+ return 1+ except OSError as error:+ print("%s: cannot read %s" % (PROG, printable(error.filename or sbom)),+ file=sys.stderr)+ return 1+ sbom_sha256 = hashlib.sha256(data).hexdigest()+ meta = {+ "image_id": image_id,+ "image_version": image_version,+ "image_root": "" if root == "-" else root,+ "sbom_format": DEFAULT_FORMAT,+ "sbom_sha256": sbom_sha256,+ "built_at": built_at,+ }+ with open(os.path.join(directory, name), "wb") as f:+ f.write(data)+ with open(os.path.join(directory, "image.json"), "w",+ encoding="ascii") as f:+ json.dump(meta, f, separators=(",", ":"))+ f.write("\n")+ print("%s/%s/%s\t%s\t%d" % (image_id, image_version, root, sbom_sha256,+ len(components)))+ return 0+++def stored_main(path, sbom_sha256):+ """Whether the stored image.json names this SBOM."""+ try:+ with open(path, "rb") as f:+ meta = parse_json(f.read(), "image.json")+ except (OSError, Refused):+ return 1+ if type(meta) is not dict:+ return 1+ same = (meta.get("sbom_sha256") == sbom_sha256+ and meta.get("sbom_format", DEFAULT_FORMAT) == DEFAULT_FORMAT)+ return 0 if same else 1++def main():+ global PROGargs = sys.argv[1:]if args == ["files"]:print("image.json")@@ -413,8 +488,15 @@ def main():return images_main(args[1], args[2])if len(args) == 3 and args[0] == "rows":return rows_main(args[1], args[2])+ if len(args) == 7 and args[0] == "put":+ PROG = "sbom-put"+ return put_main(*args[1:])+ if len(args) == 3 and args[0] == "stored":+ return stored_main(args[1], args[2])print("usage: sbom-rows.py files | images <store> <work> | "- "rows <work> <scanned_at>", file=sys.stderr)+ "rows <work> <scanned_at> | put <directory> <image_id> "+ "<image_version> <image_root or -> <built_at> <sbom> | "+ "stored <image.json> <sbom_sha256>", file=sys.stderr)return 2diff --git a/tools/backend/test/sbom-put.expected b/tools/backend/test/sbom-put.expectednew file mode 100644index 00000000..cb453553--- /dev/null+++ b/tools/backend/test/sbom-put.expected@@ -0,0 +1,62 @@+sbom-put: fleet-os/2026.11.1/a3c3f1b0e4c2d7f09e3b6b2a8b1d5e7c4f6a9d0b2c8e1f3a5b7d9c0e2f4a6b8c: stored, sha256 fd2407ebf451915ddb23cd4cabd3cee23db62e7f1d7a81c8c3cfb3b1c48596e4, components: 4+sbom-scan: fleet-os/2026.11.1/a3c3f1b0e4c2d7f09e3b6b2a8b1d5e7c4f6a9d0b2c8e1f3a5b7d9c0e2f4a6b8c: ok, components: 4, findings: 2, of kernel packages left out: 3+sbom-scan: loaded images: 1, findings: 2+put and scan: status 0+./fleet-os/2026.11.1/a3c3f1b0e4c2d7f09e3b6b2a8b1d5e7c4f6a9d0b2c8e1f3a5b7d9c0e2f4a6b8c/image.json+./fleet-os/2026.11.1/a3c3f1b0e4c2d7f09e3b6b2a8b1d5e7c4f6a9d0b2c8e1f3a5b7d9c0e2f4a6b8c/sbom.cdx.json+{"image_id":"fleet-os","image_version":"2026.11.1","image_root":"a3c3f1b0e4c2d7f09e3b6b2a8b1d5e7c4f6a9d0b2c8e1f3a5b7d9c0e2f4a6b8c","sbom_format":"cyclonedx-json","sbom_sha256":"fd2407ebf451915ddb23cd4cabd3cee23db62e7f1d7a81c8c3cfb3b1c48596e4","built_at":"2026-11-02T08:00:00Z"}+the SBOM is the bytes given+sbom-put: fleet-os/2026.11.1/a3c3f1b0e4c2d7f09e3b6b2a8b1d5e7c4f6a9d0b2c8e1f3a5b7d9c0e2f4a6b8c: already stored with this SBOM+same again: status 0+sbom-put: fleet-os/2026.11.1/a3c3f1b0e4c2d7f09e3b6b2a8b1d5e7c4f6a9d0b2c8e1f3a5b7d9c0e2f4a6b8c is already in the store with another SBOM; the store is written once+another sbom: status 1+sbom-put: fleet-os/2026.11.6/-: stored, sha256 d89d4d8966a7264023fb306de62ee1b5a40e6376dfffec4b59d0774fb988f333, components: 2+no root, default time: status 0+{"image_id":"fleet-os","image_version":"2026.11.6","image_root":"","sbom_format":"cyclonedx-json","sbom_sha256":"d89d4d8966a7264023fb306de62ee1b5a40e6376dfffec4b59d0774fb988f333","built_at":"<now>"}+stub-aws: s3 cp --recursive --only-show-errors s3://fleet-images/sboms/fleet-os/2026.11.1/a3c3f1b0e4c2d7f09e3b6b2a8b1d5e7c4f6a9d0b2c8e1f3a5b7d9c0e2f4a6b8c/ <directory> --exclude * --include image.json+stub-aws: s3 cp --only-show-errors <file> s3://fleet-images/sboms/fleet-os/2026.11.1/a3c3f1b0e4c2d7f09e3b6b2a8b1d5e7c4f6a9d0b2c8e1f3a5b7d9c0e2f4a6b8c/sbom.cdx.json+stub-aws: s3 cp --only-show-errors <file> s3://fleet-images/sboms/fleet-os/2026.11.1/a3c3f1b0e4c2d7f09e3b6b2a8b1d5e7c4f6a9d0b2c8e1f3a5b7d9c0e2f4a6b8c/image.json+sbom-put: fleet-os/2026.11.1/a3c3f1b0e4c2d7f09e3b6b2a8b1d5e7c4f6a9d0b2c8e1f3a5b7d9c0e2f4a6b8c: stored, sha256 fd2407ebf451915ddb23cd4cabd3cee23db62e7f1d7a81c8c3cfb3b1c48596e4, components: 4+s3 put: status 0+./fleet-images/sboms/fleet-os/2026.11.1/a3c3f1b0e4c2d7f09e3b6b2a8b1d5e7c4f6a9d0b2c8e1f3a5b7d9c0e2f4a6b8c/image.json+./fleet-images/sboms/fleet-os/2026.11.1/a3c3f1b0e4c2d7f09e3b6b2a8b1d5e7c4f6a9d0b2c8e1f3a5b7d9c0e2f4a6b8c/sbom.cdx.json+stub-aws: s3 cp --recursive --only-show-errors s3://fleet-images/sboms/fleet-os/2026.11.1/a3c3f1b0e4c2d7f09e3b6b2a8b1d5e7c4f6a9d0b2c8e1f3a5b7d9c0e2f4a6b8c/ <directory> --exclude * --include image.json+sbom-put: fleet-os/2026.11.1/a3c3f1b0e4c2d7f09e3b6b2a8b1d5e7c4f6a9d0b2c8e1f3a5b7d9c0e2f4a6b8c: already stored with this SBOM+s3 same again: status 0+stub-aws: s3 cp --recursive --only-show-errors s3://fleet-images/sboms/fleet-os/2026.11.1/a3c3f1b0e4c2d7f09e3b6b2a8b1d5e7c4f6a9d0b2c8e1f3a5b7d9c0e2f4a6b8c/ <directory> --exclude * --include image.json+sbom-put: fleet-os/2026.11.1/a3c3f1b0e4c2d7f09e3b6b2a8b1d5e7c4f6a9d0b2c8e1f3a5b7d9c0e2f4a6b8c is already in the store with another SBOM; the store is written once+s3 another sbom: status 1+stub-aws: s3 cp --recursive --only-show-errors s3://fleet-images/sboms/fleet-os/2026.11.2/-/ <directory> --exclude * --include image.json+sbom-put: cannot read s3://fleet-images/sboms/fleet-os/2026.11.2/-+s3 fails: status 1+sbom-put: the image id is not 1 to 255 of [A-Za-z0-9._~+-] that do not start with a dot+dot id: status 1+sbom-put: the image version is not 1 to 255 of [A-Za-z0-9._~+-] that do not start with a dot+slash version: status 1+sbom-put: the image root is not 64 lowercase hex digits+short root: status 1+sbom-put: the build time is not a UTC "YYYY-MM-DDThh:mm:ssZ"+bad time: status 1+sbom-put: bomFormat of sbom.cdx.json is not "CycloneDX"+not cyclonedx: status 1+sbom-put: cannot read none.json+no sbom: status 1+sbom-put: the store https://example.com/sboms is neither file://<directory> nor s3://<bucket>+https store: status 1+sbom-put: no store directory no-such-store+no store directory: status 1+usage: sbom-put [--store <url>] --image-id <id> --image-version <version>+ [--image-root <root>] [--built-at <YYYY-MM-DDThh:mm:ssZ>]+ [--scan] <sbom.cdx.json>+no image id: status 2+usage: sbom-put [--store <url>] --image-id <id> --image-version <version>+ [--image-root <root>] [--built-at <YYYY-MM-DDThh:mm:ssZ>]+ [--scan] <sbom.cdx.json>+two sboms: status 2+./fleet-os/2026.11.1/a3c3f1b0e4c2d7f09e3b6b2a8b1d5e7c4f6a9d0b2c8e1f3a5b7d9c0e2f4a6b8c/image.json+./fleet-os/2026.11.1/a3c3f1b0e4c2d7f09e3b6b2a8b1d5e7c4f6a9d0b2c8e1f3a5b7d9c0e2f4a6b8c/sbom.cdx.json+./fleet-os/2026.11.6/-/image.json+./fleet-os/2026.11.6/-/sbom.cdx.json+fleet-os 2026.11.1 a3c3f1b0e4c2d7f09e3b6b2a8b1d5e7c4f6a9d0b2c8e1f3a5b7d9c0e2f4a6b8c cyclonedx-json fd2407ebf451915ddb23cd4cabd3cee23db62e7f1d7a81c8c3cfb3b1c48596e4 ok 2026-11-02 08:00:00 2026-11-11T06:00:00Z+fd2407ebf451915ddb23cd4cabd3cee23db62e7f1d7a81c8c3cfb3b1c48596e4 EXAMPLE-0202 openssl+fd2407ebf451915ddb23cd4cabd3cee23db62e7f1d7a81c8c3cfb3b1c48596e4 EXAMPLE-0505 zlibdiff --git a/tools/backend/test/sbom-put.sh b/tools/backend/test/sbom-put.shnew file mode 100644index 00000000..a01a508b--- /dev/null+++ b/tools/backend/test/sbom-put.sh@@ -0,0 +1,85 @@+#!/bin/sh+# SPDX-License-Identifier: BSD-2-Clause+# Copyright (c) 2026 Authors of shirudo+#+# jobs/sbom-put into a file:// store and an s3:// one, with+# test/stub-aws.sh in place of the AWS command line and+# test/stub-trivy.sh as the matcher of --scan. An image is stored, its+# files are those sbom-scan reads, and --scan loads it; storing it again+# with the same SBOM changes nothing, with another is refused. Then what+# it refuses before writing anything: a key out of the layout, a root+# that is not a root, a time out of form, an SBOM that is not CycloneDX+# and one that is not there, a store that is not one and wrong usage.++set -eu++test=$(cd "$(dirname "$0")" && pwd)+job=$test/../jobs/sbom-put+root=a3c3f1b0e4c2d7f09e3b6b2a8b1d5e7c4f6a9d0b2c8e1f3a5b7d9c0e2f4a6b8c+sbom=$test/sbom-store/fleet-os/2026.11.1/$root/sbom.cdx.json+other=$test/sbom-store/fleet-os/2026.11.6/-/sbom.cdx.json+unset SBOM_STORE TRIVY_CACHE_DIR S3_CLIENT+export TRIVY="$test/stub-trivy.sh"++work=$(mktemp -d)+trap 'rm -rf "$work"' EXIT+mkdir "$work/store"++# run <what> <command>...: run a command, then name its status.+run()+{+ what=$1+ shift+ status=0+ "$@" 2>&1 || status=$?+ printf '%s: status %d\n' "$what" "$status"+}++run 'put and scan' "$job" --store "file://$work/store" --image-id fleet-os \+ --image-version 2026.11.1 --image-root "$root" \+ --built-at 2026-11-02T08:00:00Z --scan "$sbom"+(cd "$work/store" && find . -type f | sort)+cat "$work/store/fleet-os/2026.11.1/$root/image.json"+cmp "$sbom" "$work/store/fleet-os/2026.11.1/$root/sbom.cdx.json" && echo 'the SBOM is the bytes given'++run 'same again' "$job" --store "file://$work/store" --image-id fleet-os \+ --image-version 2026.11.1 --image-root "$root" "$sbom"+run 'another sbom' "$job" --store "file://$work/store" --image-id fleet-os \+ --image-version 2026.11.1 --image-root "$root" "$other"++SBOM_STORE=file://$work/store run 'no root, default time' "$job" \+ --image-id fleet-os --image-version 2026.11.6 "$other"+sed 's/"built_at":"[0-9]\{4\}-[0-9][0-9]-[0-9][0-9]T[0-9][0-9]:[0-9][0-9]:[0-9][0-9]Z"/"built_at":"<now>"/' \+ "$work/store/fleet-os/2026.11.6/-/image.json"++mkdir -p "$work/s3/fleet-images"+export STUB_S3_ROOT="$work/s3"+S3_CLIENT=$test/stub-aws.sh run 's3 put' "$job" --store s3://fleet-images/sboms \+ --image-id fleet-os --image-version 2026.11.1 --image-root "$root" \+ --built-at 2026-11-02T08:00:00Z "$sbom"+(cd "$work/s3" && find . -type f | sort)+S3_CLIENT=$test/stub-aws.sh run 's3 same again' "$job" --store s3://fleet-images/sboms \+ --image-id fleet-os --image-version 2026.11.1 --image-root "$root" "$sbom"+S3_CLIENT=$test/stub-aws.sh run 's3 another sbom' "$job" --store s3://fleet-images/sboms \+ --image-id fleet-os --image-version 2026.11.1 --image-root "$root" "$other"+S3_CLIENT=$test/stub-aws.sh STUB_S3_STATUS=1 run 's3 fails' "$job" \+ --store s3://fleet-images/sboms --image-id fleet-os --image-version 2026.11.2 "$sbom"++put()+{+ "$job" --store "file://$work/store" "$@"+}+printf '{"bomFormat":"SPDX"}\n' >"$work/spdx.json"+run 'dot id' put --image-id .fleet --image-version 1 "$sbom"+run 'slash version' put --image-id fleet-os --image-version a/b "$sbom"+run 'short root' put --image-id fleet-os --image-version 1 --image-root abc "$sbom"+run 'bad time' put --image-id fleet-os --image-version 1 --built-at 2026-11-02 "$sbom"+run 'not cyclonedx' put --image-id fleet-os --image-version 1 "$work/spdx.json"+(cd "$work" && run 'no sbom' put --image-id fleet-os --image-version 1 none.json)+run 'https store' "$job" --store https://example.com/sboms --image-id fleet-os \+ --image-version 1 "$sbom"+run 'no store directory' "$job" --store file://no-such-store --image-id fleet-os \+ --image-version 1 "$sbom"+run 'no image id' put --image-version 1 "$sbom"+run 'two sboms' put --image-id fleet-os --image-version 1 "$sbom" "$sbom"+(cd "$work/store" && find . -type f | sort)diff --git a/tools/backend/test/sbom-put.sql b/tools/backend/test/sbom-put.sqlnew file mode 100644index 00000000..03ae34fc--- /dev/null+++ b/tools/backend/test/sbom-put.sql@@ -0,0 +1,10 @@+-- SPDX-License-Identifier: BSD-2-Clause+-- Copyright (c) 2026 Authors of shirudo+--+-- What the --scan of test/sbom-put.sh loaded: the image it stored, with+-- the findings of its SBOM.++SELECT image_id, image_version, image_root, sbom_format, sbom_sha256, state, built_at, db_version+FROM shirudo.sbom_images FINAL+ORDER BY image_id, image_version, image_root;+SELECT sbom_sha256, vuln_id, package FROM shirudo.sbom_vulns ORDER BY vuln_id, package;diff --git a/tools/backend/test/stub-aws.sh b/tools/backend/test/stub-aws.shindex 6c5fb1ce..c2c2949f 100755--- a/tools/backend/test/stub-aws.sh+++ b/tools/backend/test/stub-aws.sh@@ -2,28 +2,38 @@# SPDX-License-Identifier: BSD-2-Clause# Copyright (c) 2026 Authors of shirudo#-# test/stub-aws.sh -- stand in for `aws s3 cp --recursive` in the tests-# of jobs/sbom-scan. The object s3://<bucket>/<key> is the file-# STUB_S3_ROOT/<bucket>/<key>. It names its arguments on standard error,-# the destination as <directory> since the job makes it, copies the-# files whose names the --include patterns end in and exits with-# STUB_S3_STATUS, 0 by default.+# test/stub-aws.sh -- stand in for `aws s3 cp` in the tests of+# jobs/sbom-scan and jobs/sbom-put. The object s3://<bucket>/<key> is the+# file STUB_S3_ROOT/<bucket>/<key>. It names its arguments on standard+# error, a local path as <directory> or <file> since the job makes it,+# and exits with STUB_S3_STATUS, 0 by default. With --recursive it copies+# the files under the source whose names the --include patterns end in,+# and nothing when there is no such prefix, as AWS does; without, it+# copies one local file to an object.set -eu-[ "${1:-} ${2:-} ${3:-}" = 's3 cp --recursive' ] || {- echo "stub-aws: only s3 cp --recursive, not $*" >&2+[ "${1:-} ${2:-}" = 's3 cp' ] || {+ echo "stub-aws: only s3 cp, not $*" >&2exit 2}-said='s3 cp --recursive'-shift 3+said='s3 cp'+shift 2+recursive=0source=destination=names=while [ $# -gt 0 ]; docase "$1" ins3://*)- source=$STUB_S3_ROOT/${1#s3://}+ if [ -z "$source" ] && [ "$recursive" -eq 1 ]; then+ source=$STUB_S3_ROOT/${1#s3://}+ else+ destination=$STUB_S3_ROOT/${1#s3://}+ fi+ said="$said $1" ;;+ --recursive)+ recursive=1said="$said $1" ;;--include)names="$names ${2##*/}"@@ -35,14 +45,25 @@ while [ $# -gt 0 ]; do-*)said="$said $1" ;;*)- destination=$1- said="$said <directory>" ;;+ if [ "$recursive" -eq 1 ]; then+ destination=$1+ said="$said <directory>"+ else+ source=$1+ said="$said <file>"+ fi ;;esacshiftdoneprintf 'stub-aws: %s\n' "$said" >&2[ "${STUB_S3_STATUS:-0}" -eq 0 ] || exit "$STUB_S3_STATUS"+if [ "$recursive" -eq 0 ]; then+ mkdir -p "${destination%/*}"+ cp "$source" "$destination"+ exit 0+fi+[ -d "$source" ] || exit 0cd "$source"for name in $names; dofind . -type f -name "$name" | while read -r file; do