Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
143 changes: 138 additions & 5 deletions .github/workflows/security.yml
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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() }}
Expand Down
81 changes: 77 additions & 4 deletions .github/workflows/selftest.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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:
Expand Down
Loading
Loading