diff --git a/.github/workflows/security.yml b/.github/workflows/security.yml index cdc73ee..457400d 100644 --- a/.github/workflows/security.yml +++ b/.github/workflows/security.yml @@ -1,9 +1,11 @@ # Reusable security scan: secrets, dependencies, SAST, configuration, -# licences, SBOM and (optionally) the container image. +# licences, SBOM and (optionally) the container image and built root +# filesystems. # -# A scheduled run (the callers' weekly cron) scans the dependencies and the -# image only: new advisories against unchanged code show up there. The other -# scans repeat what the last pull request found (`scheduled-full` runs them). +# A scheduled run (the callers' weekly cron) scans the dependencies, the +# image and the root filesystems only: new advisories against unchanged code +# show up there. The other scans repeat what the last pull request found +# (`scheduled-full` runs them). # # Every scanner job only reports. The `report` job reads all findings, # applies the repository's exceptions (security/vex.openvex.json, @@ -118,6 +120,25 @@ on: description: Events that build and scan the image. Public repositories and release pull requests scan on pull_request too. type: string default: push,schedule,workflow_dispatch + rootfs-recipe: + description: >- + Recipe that builds a root filesystem (the tree of an OS image), run + in working-directory once per rootfs-matrix entry with that entry's + args. It runs with ROOTFS_WORK set, a scratch directory on the + runner's larger disk, and leaves the tree there. Every event scans + it, schedule included. + type: string + default: "" + rootfs-matrix: + description: >- + JSON list of the trees to build and scan, each {name, args, path, + runner}: name ends the artifact names and the code-scanning + category (letters, digits, . _ -); args are the recipe's arguments + (space-separated); path is the tree the recipe leaves, relative to + ROOTFS_WORK or absolute (read as root); runner defaults to + ubuntu-latest. + type: string + default: "[]" vex-file: type: string default: security/vex.openvex.json @@ -612,8 +633,120 @@ jobs: sarif_file: ${{ runner.temp }}/code-scanning category: image + # One job per rootfs-matrix entry: the recipe builds the tree, trivy scans + # its packages. A tree the recipe did not leave, or one without an operating + # system and its packages, fails the job instead of looking clean; `report` + # then reports the scan as incomplete. A matrix without a recipe fails too. + rootfs: + if: inputs.rootfs-recipe != '' || (inputs.rootfs-matrix != '' && inputs.rootfs-matrix != '[]') + name: rootfs (${{ matrix.name }}) + runs-on: ${{ matrix.runner || 'ubuntu-latest' }} + timeout-minutes: 40 + permissions: + contents: read + security-events: write + strategy: + fail-fast: false + matrix: + include: ${{ fromJSON(inputs.rootfs-matrix) }} + env: + # On the runner's second disk where it has one (x64): a tree takes + # gigabytes. + ROOTFS_WORK: /mnt/rootfs-work + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + repository: IronFlock/workflows + ref: ${{ inputs.harness-ref }} + path: .harness + persist-credentials: false + - name: move harness out of the workspace + run: | + mv .harness "$RUNNER_TEMP/harness" + echo "HARNESS=$RUNNER_TEMP/harness" >> "$GITHUB_ENV" + mkdir -p "$RUNNER_TEMP/findings" "$RUNNER_TEMP/sbom" + - name: check the entry, scratch space for the build + env: + RECIPE: ${{ inputs.rootfs-recipe }} + NAME: ${{ matrix.name }} + TREE: ${{ matrix.path }} + run: | + if [ -z "$RECIPE" ]; then + echo "::error::rootfs-matrix is set, but rootfs-recipe is empty: nothing builds the trees" + exit 1 + fi + if ! [[ $NAME =~ ^[A-Za-z0-9][A-Za-z0-9._-]*$ ]]; then + echo "::error::rootfs-matrix: every entry needs a name of letters, digits, '.', '_' and '-' (got '$NAME')" + exit 1 + fi + if [ -z "$TREE" ]; then + echo "::error::rootfs-matrix: entry $NAME has no path" + exit 1 + fi + sudo mkdir -p "$ROOTFS_WORK" + sudo chown "$(id -u):$(id -g)" "$ROOTFS_WORK" + - name: install just and trivy (tools.lock) + run: '"$HARNESS/scripts/install-tool.sh" just trivy' + - name: just ${{ inputs.rootfs-recipe }} ${{ matrix.args }} + working-directory: ${{ inputs.working-directory }} + env: + RECIPE: ${{ inputs.rootfs-recipe }} + ARGS: ${{ matrix.args }} + run: | + read -ra args <<<"$ARGS" + just "$RECIPE" "${args[@]}" + - name: trivy rootfs (vulnerabilities with a fix, SBOM) + env: + NAME: ${{ matrix.name }} + TREE: ${{ matrix.path }} + VEX: ${{ inputs.vex-file }} + TRIVYIGNORE: ${{ inputs.trivyignore }} + run: | + case $TREE in + /*) tree=$TREE ;; + *) tree=$ROOTFS_WORK/$TREE ;; + esac + # As root: a build usually leaves parts of the tree readable by root only. + sudo env "PATH=$PATH" "VEX=$VEX" "TRIVYIGNORE=$TRIVYIGNORE" "$HARNESS/scripts/trivy-rootfs.sh" "$tree" \ + "$RUNNER_TEMP/findings/trivy-rootfs-$NAME.sarif" "$RUNNER_TEMP/sbom/$NAME.cdx.json" | tee "$RUNNER_TEMP/table.txt" + sudo chown -R "$(id -u):$(id -g)" "$RUNNER_TEMP/findings" "$RUNNER_TEMP/sbom" + { + echo "### rootfs $NAME: packages with a fixed HIGH or CRITICAL vulnerability" + echo '```' + tail -n 60 "$RUNNER_TEMP/table.txt" + echo '```' + } >> "$GITHUB_STEP_SUMMARY" + - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: ${{ inputs.artifact-prefix }}-findings-rootfs-${{ matrix.name }} + path: ${{ runner.temp }}/findings + if-no-files-found: error + retention-days: 90 + - uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: ${{ inputs.artifact-prefix }}-rootfs-sbom-${{ matrix.name }} + path: ${{ runner.temp }}/sbom + if-no-files-found: error + retention-days: 90 + - id: cs + if: always() && inputs.code-scanning && !github.event.repository.private + env: + VEX: ${{ inputs.vex-file }} + run: | + python3 -I "$HARNESS/scripts/sarif-summary.py" upload-copy --dir "$RUNNER_TEMP/findings" --out "$RUNNER_TEMP/code-scanning" --vex "$VEX" + echo "files=$(find "$RUNNER_TEMP/code-scanning" -name '*.sarif' | wc -l)" >> "$GITHUB_OUTPUT" + - uses: github/codeql-action/upload-sarif@6f2ac81f5cd81fe351a197ba0b00d91d03cd2aee # v4.39.0 + if: always() && steps.cs.outputs.files > 0 + continue-on-error: true + with: + sarif_file: ${{ runner.temp }}/code-scanning + category: rootfs-${{ matrix.name }} + report: - needs: [gitleaks, deps, semgrep, sbom, config, licenses, image] + needs: [gitleaks, deps, semgrep, sbom, config, licenses, image, rootfs] # Runs even when a scanner job failed or timed out (it then reports the # scan as incomplete), but not when the whole run was cancelled. if: ${{ !cancelled() }} diff --git a/.github/workflows/selftest.yml b/.github/workflows/selftest.yml index 9467796..5f5c3dc 100644 --- a/.github/workflows/selftest.yml +++ b/.github/workflows/selftest.yml @@ -9,8 +9,10 @@ # secret logged to the console, root container, unpinned # action, failing Go, Node, Python and bats tests, an # unquoted shell expansion, a second or missing -# lockfile, a privileged container in a Helm chart) are each -# reported; a Helm chart trivy skips fails the scan +# lockfile, a privileged container in a Helm chart, an +# outdated package in a built root filesystem) are each +# reported; a Helm chart trivy skips and a root tree +# without an operating system fail the scan # Negative controls run report-only and are asserted from the outputs, so the # self-test is green exactly when every planted finding is detected. # `v1` is advanced only to a commit whose self-test is green. @@ -458,6 +460,55 @@ jobs: scheduled-full: true harness-ref: ${{ github.sha }} + # The root filesystem scan: the fixture recipe leaves the tree of an + # outdated base image, readable by root only, natively on an x64 and an + # arm64 runner. Its fixed vulnerability and both SBOMs are checked in + # assert-planted. + security-rootfs: + uses: ./.github/workflows/security.yml + permissions: + contents: read + security-events: write + with: + working-directory: tests/fixtures/rootfs + artifact-prefix: rootfs + gitleaks: false + semgrep: false + config-scan: false + licenses: false + rootfs-recipe: rootfs + rootfs-matrix: >- + [{"name": "alpine-amd64", "args": "alpine-amd64 alpine:3.12", "path": "alpine-amd64"}, + {"name": "alpine-arm64", "args": "alpine-arm64 alpine:3.12", "path": "alpine-arm64", "runner": "ubuntu-24.04-arm"}] + code-scanning: false + harness-ref: ${{ github.sha }} + + # A tree without an operating system (a wrong path, a build that stopped + # early): trivy scans it without an error, so the rootfs scan must fail + # instead of reporting it clean. + rootfs-scan-empty-tree: + runs-on: ubuntu-latest + timeout-minutes: 10 + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + - run: scripts/install-tool.sh trivy + # Output captured: the expected ::error:: line is not an annotation here. + - name: the empty tree must fail trivy-rootfs.sh + run: | + mkdir -p "$RUNNER_TEMP/empty" + if out=$(scripts/trivy-rootfs.sh "$RUNNER_TEMP/empty" "$RUNNER_TEMP/empty.sarif" "$RUNNER_TEMP/empty.cdx.json" 2>&1); then + echo "::error::trivy-rootfs.sh passed although the tree has no operating system" + exit 1 + fi + if ! grep -q 'no operating system with packages' <<<"$out"; then + echo "$out" + echo "::error::trivy-rootfs.sh failed, but not on the missing operating system" + exit 1 + fi + echo "trivy-rootfs.sh failed on the empty tree as expected" + # Dry run of the release workflow (not the merge of a release pull request: # builds both platforms natively, pushes nothing, needs no credentials). # The decisions of a real release (merge title, existing tag, re-run) are @@ -609,7 +660,7 @@ jobs: [ "$environment" = image-provenance ] || { echo "::error::no environment claim image-provenance with deployment: false; release-image.yml could not sign"; exit 1; } assert-planted: - needs: [security-planted, security-helm, lint-planted] + needs: [security-planted, security-helm, security-rootfs, lint-planted] # Also when a scanner job broke: the counts then show what is missing. if: ${{ !cancelled() }} runs-on: ubuntu-latest @@ -619,10 +670,12 @@ jobs: env: FINDINGS: ${{ needs.security-planted.outputs.findings }} HELM: ${{ needs.security-helm.outputs.findings }} + ROOTFS: ${{ needs.security-rootfs.outputs.findings }} LINT: ${{ needs.lint-planted.outputs.findings }} run: | echo "security: $FINDINGS" echo "security (helm): $HELM" + echo "security (rootfs): $ROOTFS" echo "workflow lint: $LINT" fail=0 for cat in secrets vuln-high misconfig-high semgrep-error; do @@ -632,18 +685,38 @@ jobs: if [ "$(jq -r '.["misconfig-high"] // 0' <<<"$HELM")" -lt 1 ]; then echo "::error::no misconfig-high finding in the planted Helm chart"; fail=1 fi + if [ "$(jq -r '(.["vuln-critical"] // 0) + (.["vuln-high"] // 0)' <<<"$ROOTFS")" -lt 1 ]; then + echo "::error::no vuln-critical or vuln-high finding in the planted root trees"; fail=1 + fi for tool in zizmor pinact; do if [ "$(jq -r --arg t "$tool" '.[$t] // 0' <<<"$LINT")" != 1 ]; then echo "::error::$tool did not flag the planted workflow"; fail=1 fi done exit "$fail" + - uses: actions/download-artifact@9000827ccba6bdab643e8b6fd33ac0654aef8333 # v8.0.2 + with: + pattern: rootfs-rootfs-sbom-* + path: sbom + - name: an SBOM per root tree, each of its runner's architecture + run: | + fail=0 + for entry in alpine-amd64:x86_64 alpine-arm64:aarch64; do + name=${entry%%:*} arch=${entry#*:} + sbom=sbom/rootfs-rootfs-sbom-$name/$name.cdx.json + if [ ! -s "$sbom" ]; then + echo "::error::no SBOM of the root tree $name"; fail=1 + elif ! grep -q "arch=$arch" "$sbom"; then + echo "::error::the SBOM of $name lists no $arch package"; fail=1 + fi + done + exit "$fail" # The required check of the main-branch ruleset: green only when every job # above succeeded (skipped or cancelled counts as failed). selftest-ok: if: ${{ always() }} - needs: [lint-self, lint-templates, lint-planted, scripts, gate-pass, gate-fail, node-gate-npm, node-gate-npm-tools, node-gate-bun, node-gate-fail, python-gate-pass, python-gate-fail, shell-gate-pass, shell-gate-artifact, shell-gate-fail, shell-gate-arm64, tools-arm64, security-clean, security-clean-node, security-planted, config-scan-skipped-chart, security-helm, release-dry-run, release-dry-run-package-json, build-context-artifact, release-dry-run-build-context, release-dry-run-image-tag, assert-image-tag, provenance-sign, provenance-environment-claim, assert-planted] + needs: [lint-self, lint-templates, lint-planted, scripts, gate-pass, gate-fail, node-gate-npm, node-gate-npm-tools, node-gate-bun, node-gate-fail, python-gate-pass, python-gate-fail, shell-gate-pass, shell-gate-artifact, shell-gate-fail, shell-gate-arm64, tools-arm64, security-clean, security-clean-node, security-planted, config-scan-skipped-chart, security-helm, security-rootfs, rootfs-scan-empty-tree, release-dry-run, release-dry-run-package-json, build-context-artifact, release-dry-run-build-context, release-dry-run-image-tag, assert-image-tag, provenance-sign, provenance-environment-claim, assert-planted] runs-on: ubuntu-latest timeout-minutes: 5 steps: diff --git a/README.md b/README.md index 2616809..d8db291 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,8 @@ Reusable GitHub Actions workflows that give every IronFlock repository the same gate (lint, unit tests, build) and the same security screening (secrets, -dependencies, SAST, configuration, licences, SBOM, container image). +dependencies, SAST, configuration, licences, SBOM, container image, built root +filesystems). Every logic step calls a `just` recipe of the calling repository, so a developer runs the same checks locally that CI runs. @@ -44,7 +45,7 @@ The inputs of each workflow are documented in its `on.workflow_call` block. | [node-gate.yml](.github/workflows/node-gate.yml) | `lint`, `unit`, `build` | Node/Bun gate: `package-manager` bun or npm, installs from the lockfile only (`bun install --frozen-lockfile` / `npm ci`), fails on a lockfile of the other package manager; `skip` drops jobs a repository has no recipe for | | [python-gate.yml](.github/workflows/python-gate.yml) | `lint`, `unit`, `build` | Python gate: uv from `tools.lock`; with a `uv.lock`, `uv sync --frozen` (all groups) before the recipes, otherwise the recipes bring their own tools (pinned `uvx`) | | [shell-gate.yml](.github/workflows/shell-gate.yml) | `lint`, `unit`, `build` | Shell/just gate (image builders, installers, script collections): just, shellcheck, shfmt from `tools.lock`, yamllint and bats from the Ubuntu archive; `build-apt-packages` for build tools, `build-args` for the build recipe, `build-runs-on` for a native arm64 build, `build-artifact-path` hands the build output to a later job | -| [security.yml](.github/workflows/security.yml) | `gitleaks`, `deps`, `semgrep`, `sbom`, `config`, `licenses`, `image`, `report` | security screening; `report` applies `fail-on` | +| [security.yml](.github/workflows/security.yml) | `gitleaks`, `deps`, `semgrep`, `sbom`, `config`, `licenses`, `image`, `rootfs`, `report` | security screening; `report` applies `fail-on` | | [workflows-lint.yml](.github/workflows/workflows-lint.yml) | `lint` | actionlint, zizmor, pinact on the caller's workflows; always blocking | | [release-intent.yml](.github/workflows/release-intent.yml) | `intent` | the version file decides: a rise makes a release PR (title `release ` required), an unchanged version a no-release PR; output `kind` gates the release dry run; listed in the `needs` of `ci-ok` | | [release-image.yml](.github/workflows/release-image.yml) | `verify`, `build`, `publish` | release of a container image on the merge of a release PR into the protected `main`; native multi-arch build, SBOM attached, SLSA provenance signed with a Cloud KMS key and attached (see [Signed image provenance](#signed-image-provenance)), keyless push; on a release PR a dry run (every platform build, no push); re-runs are safe; read rights only; `build-contexts-artifact` hands data an earlier job prepared (with secrets the build must not see) to the build as named contexts | @@ -92,9 +93,26 @@ repository-local `integration` job next to the gate. repositories skip the build on pull requests to save Actions minutes, except on release pull requests (title `release v`): the image that the merge publishes is scanned before the merge. +- Root filesystems (the tree of an OS image) are built and scanned in the + `rootfs` job, one job per entry of `rootfs-matrix`, on every event. The + repository's `rootfs-recipe` runs in `working-directory` with the entry's + `args` and `ROOTFS_WORK` set, a scratch directory on the runner's larger + disk, and leaves the tree there; `path` names it (relative to + `ROOTFS_WORK`, or absolute). trivy reads it as root, so the build may leave + it readable by root only. The vulnerabilities with a fix count in the + `vuln-*` categories; a tree without an operating system and its packages + (a wrong `path`, a build that stopped early) fails the job, and so the + report, instead of looking clean. + + ```yaml + rootfs-recipe: rootfs + rootfs-matrix: >- + [{"name": "amd64-base", "args": "amd64 base", "path": "amd64-base/rootfs"}, + {"name": "arm64-base", "args": "arm64 base", "path": "arm64-base/rootfs", "runner": "ubuntu-24.04-arm"}] + ``` Every run writes a summary table to the job summary and keeps the raw reports -as artifacts (`security-findings-*`, `security-sbom`) for 90 days. A workflow that +as artifacts (`security-findings-*`, `security-sbom`, `security-rootfs-sbom-`) for 90 days. A workflow that calls `security.yml` more than once per run gives each call its own `artifact-prefix`. Public repositories also get the results in their Security tab, without the vulnerabilities the VEX file accepts, so the tab and the report agree. @@ -213,8 +231,8 @@ succeeded (see [templates/workflows/ci-go.yml](templates/workflows/ci-go.yml)). The check workflows (`ci.yml`, `security.yml`) run on pull requests into `main`, plus the weekly security scan; a run after the merge would test the -same tree again. The weekly scan covers the dependencies and the image only, -where new advisories appear without a code change (`scheduled-full: true` +same tree again. The weekly scan covers the dependencies, the image and the +root filesystems only, where new advisories appear without a code change (`scheduled-full: true` runs every scanner). The version file decides what a pull request is (`release-intent.yml`): @@ -312,7 +330,7 @@ request that adds it. Templates are in [templates/repo](templates/repo). | File | For | Expiry | |---|---|---| | `security/vex.openvex.json` | vulnerabilities that do not affect the code (OpenVEX `not_affected` with a `justification`, or `fixed`) | statement `timestamp` + 12 months | -| `.trivyignore.yaml` | image, configuration and licence findings | `expired_at` (required) | +| `.trivyignore.yaml` | image, root filesystem, configuration and licence findings | `expired_at` (required) | | `.gitleaks.toml` | reviewed secret false positives | review with each change | | `.gitleaks-baseline.json` | historical findings that cannot be removed from history | — | | `// nosemgrep: ` | a reviewed semgrep false positive, with the reason | — | @@ -335,9 +353,11 @@ The summary lists exceptions that expire within 30 days. a reachable call into a vulnerable `golang.org/x/text`, a secret written to the console, a root container on an outdated base image, an unpinned action, a failing test, a privileged container in a Helm chart whose dependency is - fetched first); the self-test is green only when each one is reported, the - same chart without its dependency fails the scan, and the clean fixture - passes with every `fail-on` category set. Alerts that + fetched first, an outdated package in a root tree built on an x64 and an + arm64 runner); the self-test is green only when each one is reported, the + same chart without its dependency fails the scan, so does a root tree + without an operating system, and the clean fixture passes with every + `fail-on` category set. Alerts that GitHub raises for the fixtures are expected. - **Releases.** Callers use the moving tag `v1`, so a change reaches every repository when `v1` moves. The harness has the same recipes as every diff --git a/scripts/trivy-rootfs.sh b/scripts/trivy-rootfs.sh new file mode 100755 index 0000000..be1bc96 --- /dev/null +++ b/scripts/trivy-rootfs.sh @@ -0,0 +1,47 @@ +#!/usr/bin/env bash +# Scan a built root filesystem (the packages of an OS image) with trivy: +# trivy-rootfs.sh +# Writes the CycloneDX SBOM of all packages and SARIF of the vulnerabilities +# that have a fix (the report job counts them), and prints the fixed HIGH and +# CRITICAL ones as a table. Never fails on findings; the report job decides. +# Fails when the tree holds no operating system with packages: trivy scans an +# empty or wrong directory without an error, and the report would look clean. +# Run as root when the build left the tree readable by root only. +# Optional env: VEX, TRIVYIGNORE (each used when the file exists). +set -euo pipefail + +tree=$1 +sarif=$2 +sbom=$3 + +if [ ! -d "$tree" ]; then + echo "::error::no root tree at $tree: the recipe did not leave one there (check the entry's path)" + exit 1 +fi + +args=(--quiet --scanners vuln) +if [ -n "${VEX:-}" ] && [ -f "$VEX" ]; then args+=(--vex "$VEX"); fi +if [ -n "${TRIVYIGNORE:-}" ] && [ -f "$TRIVYIGNORE" ]; then args+=(--ignorefile "$TRIVYIGNORE"); fi + +trivy rootfs "${args[@]}" --format cyclonedx --output "$sbom" "$tree" + +# The SBOM names the operating system trivy detected (type debian, alpine, +# ...) and lists the packages it read from the package database (dpkg, apk, +# rpm) with that type. +packages=$(python3 -I -c ' +import json, sys +doc = json.load(open(sys.argv[1], encoding="utf-8")) +def props(c): + return {p.get("name"): p.get("value") for p in c.get("properties") or []} +comps = doc.get("components") or [] +systems = {props(c).get("aquasecurity:trivy:Type") for c in comps if c.get("type") == "operating-system"} +print(sum(1 for c in comps if c.get("type") == "library" and props(c).get("aquasecurity:trivy:PkgType") in systems - {None})) +' "$sbom") +if [ "$packages" -eq 0 ]; then + echo "::error::trivy found no operating system with packages in $tree: the entry's path points elsewhere, or the build stopped before the packages were installed. The scan would report the image clean." + exit 1 +fi +echo "$packages OS packages in $tree" >&2 + +trivy rootfs "${args[@]}" --ignore-unfixed --format sarif --output "$sarif" "$tree" +trivy rootfs "${args[@]}" --ignore-unfixed --severity HIGH,CRITICAL --format table "$tree" diff --git a/tests/fixtures/rootfs/Justfile b/tests/fixtures/rootfs/Justfile new file mode 100644 index 0000000..a589b0b --- /dev/null +++ b/tests/fixtures/rootfs/Justfile @@ -0,0 +1,14 @@ +# Planted for the harness self-test: the root tree of an outdated base image +# (a fixed critical vulnerability in zlib), standing in for the tree an OS +# image build leaves. Never use the tree for anything else. + +# Export IMAGE's filesystem to $ROOTFS_WORK/NAME, readable by root only +rootfs NAME IMAGE: + #!/usr/bin/env bash + set -euo pipefail + dest="$ROOTFS_WORK/{{ NAME }}" + id=$(docker create "{{ IMAGE }}") + trap 'docker rm "$id" >/dev/null' EXIT + sudo mkdir -p "$dest" + docker export "$id" | sudo tar -x -C "$dest" + sudo chmod 700 "$dest"