1 Commits
Author SHA1 Message Date
tthandClaude Opus 5.5 7fae1fa2f6 trivy-gate: fs scans of lockfiles, checksum-verified binary when no Docker
dtrack-sbom converts with the same runtime.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-25 18:54:03 +01:00
3 changed files with 127 additions and 28 deletions

No files matched your search

+14
View File
@@ -27,6 +27,20 @@ Intended order in a pipeline:
skip: ${{ inputs.skip_trivy || 'false' }} skip: ${{ inputs.skip_trivy || 'false' }}
``` ```
Source trees (apps that ship no image, e.g. an APK): `scan-type: fs` scans
the git-tracked files plus lockfiles generated in the job (also gitignored
ones like Flutter's `pubspec.lock`), so run it after the dependency fetch:
```yaml
- uses: https://git.rfa.cz/tth-pub/ci-actions/trivy-gate@v1.1
with:
scan-type: fs
```
Jobs without a Docker daemon (a `container:` image such as Flutter) fall
back to the pinned Trivy release binary, verified by SHA-256 (`runtime:
auto`); `jq` is installed with apt when missing.
Accepted risks go to `.trivyignore.yaml` in the calling repo, each with a Accepted risks go to `.trivyignore.yaml` in the calling repo, each with a
statement and an expiry, so they come back on their own: statement and an expiry, so they come back on their own:
+14 -4
View File
@@ -54,12 +54,22 @@ runs:
[ -n "$REPORT" ] && [ -f "$REPORT" ] || warn "no report (gate skipped?)" [ -n "$REPORT" ] && [ -f "$REPORT" ] || warn "no report (gate skipped?)"
[ -n "$DT_KEY" ] || warn "no API key" [ -n "$DT_KEY" ] || warn "no API key"
work="${RUNNER_TEMP:-/tmp}/dtrack-sbom"; mkdir -p "$work" work="${RUNNER_TEMP:-/tmp}/dtrack-sbom"; mkdir -p "$work"
docker run -i --rm --entrypoint sh "$TRIVY" -c \ # Same runtime as trivy-gate: its container when a Docker daemon is
'cat >/tmp/r.json && trivy convert --quiet --format cyclonedx /tmp/r.json' \ # reachable, else the checksum-verified binary trivy-gate fetched.
<"$REPORT" >"$work/bom.json" || warn "trivy convert failed" gate_bin="${RUNNER_TEMP:-/tmp}/trivy-gate/bin/trivy"
if command -v docker >/dev/null && docker info >/dev/null 2>&1; then
docker run -i --rm --entrypoint sh "$TRIVY" -c \
'cat >/tmp/r.json && trivy convert --quiet --format cyclonedx /tmp/r.json' \
<"$REPORT" >"$work/bom.json" || warn "trivy convert failed"
elif [ -x "$gate_bin" ]; then
"$gate_bin" convert --quiet --format cyclonedx "$REPORT" >"$work/bom.json" || warn "trivy convert failed"
else
warn "neither Docker nor the trivy-gate binary available"
fi
if [ -n "$EXTRA" ]; then if [ -n "$EXTRA" ]; then
jq --slurpfile extra "$EXTRA" '.components += $extra[0]' "$work/bom.json" >"$work/bom2.json" \ jq --slurpfile extra "$EXTRA" '.components += $extra[0]' "$work/bom.json" >"$work/bom2.json" \
&& mv "$work/bom2.json" "$work/bom.json" || warn "cannot merge $EXTRA" || warn "cannot merge $EXTRA"
mv "$work/bom2.json" "$work/bom.json"
fi fi
n=$(jq '.components | length' "$work/bom.json") || warn "unreadable SBOM" n=$(jq '.components | length' "$work/bom.json") || warn "unreadable SBOM"
[ "$n" -ge "$MIN" ] || warn "only $n components, not uploading" [ "$n" -ge "$MIN" ] || warn "only $n components, not uploading"
+99 -24
View File
@@ -1,13 +1,25 @@
name: trivy-gate name: trivy-gate
description: > description: >
Scan a pushed image with Trivy and fail when it has fixable vulnerabilities Scan a pushed image (or a source tree's lockfiles) with Trivy and fail when
of the given severities or embedded secrets. Run it on the immutable tag, it has fixable vulnerabilities of the given severities or embedded secrets.
before the floating tag (latest / stable-*) moves and before any deploy. Run it on the immutable tag, before the floating tag (latest / stable-*)
moves and before any deploy or release.
inputs: inputs:
image: image:
description: Full image reference, ideally the immutable sha tag. description: Full image reference, ideally the immutable sha tag (scan-type image).
required: true default: ""
scan-type:
description: "image = pushed container image, fs = files in the workspace (lockfiles)."
default: image
scan-ref:
description: Directory for scan-type fs (git-tracked files plus untracked lockfiles).
default: .
runtime:
description: >
auto = Trivy container when a Docker daemon is reachable, else the
pinned Trivy binary (checksum-verified); docker / binary force one.
default: auto
username: username:
description: Registry user (private images). description: Registry user (private images).
default: "" default: ""
@@ -56,6 +68,12 @@ runs:
shell: bash shell: bash
env: env:
IMAGE: ${{ inputs.image }} IMAGE: ${{ inputs.image }}
SCAN_TYPE: ${{ inputs.scan-type }}
SCAN_REF: ${{ inputs.scan-ref }}
RUNTIME: ${{ inputs.runtime }}
TRIVY_VERSION: "0.75.0"
TRIVY_SHA256_AMD64: c6e65abddb348e25f10549df887045629cf28cc72453cd1c63acb717316b3f3f
TRIVY_SHA256_ARM64: a1ee9f6ffb7d112b64ff726a2a0717c21175c1114361391f4a132956751a13b3
TRIVY_USERNAME: ${{ inputs.username }} TRIVY_USERNAME: ${{ inputs.username }}
TRIVY_PASSWORD: ${{ inputs.password }} TRIVY_PASSWORD: ${{ inputs.password }}
PLATFORM: ${{ inputs.platform }} PLATFORM: ${{ inputs.platform }}
@@ -68,37 +86,94 @@ runs:
run: | run: |
set -euo pipefail set -euo pipefail
if [ "$SKIP" = "true" ]; then if [ "$SKIP" = "true" ]; then
echo "::warning::TRIVY GATE SKIPPED for $IMAGE (manual bypass)" echo "::warning::TRIVY GATE SKIPPED for ${IMAGE:-$SCAN_REF} (manual bypass)"
echo "report=" >>"$GITHUB_OUTPUT"; echo "blocking=0" >>"$GITHUB_OUTPUT" echo "report=" >>"$GITHUB_OUTPUT"; echo "blocking=0" >>"$GITHUB_OUTPUT"
exit 0 exit 0
fi fi
case "$MODE" in block|warn) ;; *) echo "::error::mode must be block or warn"; exit 1 ;; esac case "$MODE" in block|warn) ;; *) echo "::error::mode must be block or warn"; exit 1 ;; esac
work="${RUNNER_TEMP:-/tmp}/trivy-gate"; mkdir -p "$work" work="${RUNNER_TEMP:-/tmp}/trivy-gate"; mkdir -p "$work"
report="$work/$(printf '%s' "$IMAGE" | tr -c 'A-Za-z0-9._-' '_').json" case "$SCAN_TYPE" in
image) [ -n "$IMAGE" ] || { echo "::error::image is required"; exit 1; }; target="$IMAGE" ;;
fs) target="fs:$(cd "$SCAN_REF" && pwd)" ;;
*) echo "::error::scan-type must be image or fs"; exit 1 ;;
esac
report="$work/$(printf '%s' "$target" | tr -c 'A-Za-z0-9._-' '_').json"
# DB refresh under an exclusive lock (parallel matrix jobs share the if [ "$RUNTIME" = auto ]; then
# volume); Trivy only downloads when the DB is older than its if command -v docker >/dev/null && docker info >/dev/null 2>&1; then RUNTIME=docker; else RUNTIME=binary; fi
# NextUpdate. Scans then read the volume read-only under a shared lock. fi
docker run --rm -v "$VOLUME:/cache" --entrypoint sh "$TRIVY" -c \ if ! command -v jq >/dev/null; then
'flock /cache/.lock trivy image --download-db-only --cache-dir /cache --quiet' echo "jq missing, installing"; (apt-get update -qq && apt-get install -y -qq jq) >/dev/null 2>&1 \
|| { echo "::error::jq is required"; exit 1; }
fi
common="--skip-java-db-update --scanners vuln,secret --list-all-pkgs --format json --quiet"
ignore_args="" ignore_args=""
if [ -n "$IGNOREFILE" ] && [ -f "$IGNOREFILE" ]; then if [ -n "$IGNOREFILE" ] && [ -f "$IGNOREFILE" ]; then
ignore_args="--ignorefile /tmp/ignore.yaml"
echo "ignore file: $IGNOREFILE ($(grep -c -- '- id:' "$IGNOREFILE" || true) entries)" echo "ignore file: $IGNOREFILE ($(grep -c -- '- id:' "$IGNOREFILE" || true) entries)"
fi fi
platform_args="" platform_args=""
[ -z "$PLATFORM" ] || platform_args="--platform $PLATFORM" [ -z "$PLATFORM" ] || platform_args="--platform $PLATFORM"
# Image is pulled by Trivy from the registry (no daemon socket). The # fs scans see the git-tracked files plus lockfiles generated in the
# ignore file goes in on stdin, outputs come back on stdout. # job even when .gitignore lists them (Flutter ignores pubspec.lock).
docker run -i --rm -e TRIVY_USERNAME -e TRIVY_PASSWORD -v "$VOLUME:/cache:ro" \ fs_list() {
--entrypoint sh "$TRIVY" -c "cat >/tmp/ignore.yaml; flock -s /cache/.lock \ (cd "$SCAN_REF" && { git ls-files 2>/dev/null || find . -type f -not -path './.git/*'; \
trivy image --cache-dir /cache --skip-db-update --skip-java-db-update \ git ls-files --others 2>/dev/null \
--cache-backend memory --image-src remote --scanners vuln,secret --list-all-pkgs \ | grep -E '(^|/)([^/]*\.lock|[^/]*lock\.json|[^/]*\.lockfile)$' \
--format json --quiet $ignore_args $platform_args \"\$0\"" "$IMAGE" \ | grep -vE '(^|/)(node_modules|build|\.dart_tool|\.pub-cache|\.gradle)/' || true; } \
< <( [ -n "$ignore_args" ] && cat "$IGNOREFILE" || true ) >"$report" | sort -u)
}
if [ "$RUNTIME" = docker ]; then
# DB refresh under an exclusive lock (parallel matrix jobs share the
# volume); Trivy only downloads when the DB is older than its
# NextUpdate. Scans then read the volume read-only under a shared lock.
docker run --rm -v "$VOLUME:/cache" --entrypoint sh "$TRIVY" -c \
'flock /cache/.lock trivy image --download-db-only --cache-dir /cache --quiet'
if [ "$SCAN_TYPE" = image ]; then
[ -z "$IGNOREFILE" ] || [ ! -f "$IGNOREFILE" ] || ignore_args="--ignorefile /tmp/ignore.yaml"
# Image is pulled by Trivy from the registry (no daemon socket). The
# ignore file goes in on stdin, outputs come back on stdout.
docker run -i --rm -e TRIVY_USERNAME -e TRIVY_PASSWORD -v "$VOLUME:/cache:ro" \
--entrypoint sh "$TRIVY" -c "cat >/tmp/ignore.yaml; flock -s /cache/.lock \
trivy image --cache-dir /cache --skip-db-update --cache-backend memory \
--image-src remote $common $ignore_args $platform_args \"\$0\"" "$IMAGE" \
< <( [ -n "$ignore_args" ] && cat "$IGNOREFILE" || true ) >"$report"
else
# The job's paths do not exist for the host daemon: the tree goes
# in as a tar stream on stdin.
[ -z "$IGNOREFILE" ] || [ ! -f "$IGNOREFILE" ] || ignore_args="--ignorefile /src/$IGNOREFILE"
fs_list | (cd "$SCAN_REF" && tar -c -T -) | docker run -i --rm -v "$VOLUME:/cache:ro" \
--entrypoint sh "$TRIVY" -c "mkdir /src && tar -x -C /src && flock -s /cache/.lock \
trivy fs --cache-dir /cache --skip-db-update --cache-backend memory \
$common $ignore_args /src" >"$report"
fi
else
bin="$work/bin/trivy"
if [ ! -x "$bin" ]; then
case "$(uname -m)" in
x86_64) arch=64bit; sum="$TRIVY_SHA256_AMD64" ;;
aarch64|arm64) arch=ARM64; sum="$TRIVY_SHA256_ARM64" ;;
*) echo "::error::no pinned Trivy binary for $(uname -m)"; exit 1 ;;
esac
mkdir -p "$work/bin"
curl -fsSL -o "$work/trivy.tgz" \
"https://github.com/aquasecurity/trivy/releases/download/v${TRIVY_VERSION}/trivy_${TRIVY_VERSION}_Linux-${arch}.tar.gz"
echo "$sum $work/trivy.tgz" | sha256sum -c --quiet
tar -xzf "$work/trivy.tgz" -C "$work/bin" trivy
fi
[ -z "$IGNOREFILE" ] || [ ! -f "$IGNOREFILE" ] || ignore_args="--ignorefile $IGNOREFILE"
if [ "$SCAN_TYPE" = image ]; then
"$bin" image --cache-dir "$work/cache" --cache-backend memory --image-src remote \
$common $ignore_args $platform_args "$IMAGE" >"$report"
else
mkdir -p "$work/src"; rm -rf "${work:?}/src/"*
fs_list | (cd "$SCAN_REF" && tar -c -T -) | tar -x -C "$work/src"
"$bin" fs --cache-dir "$work/cache" --cache-backend memory $common $ignore_args "$work/src" >"$report"
fi
fi
table() { if command -v column >/dev/null; then column -t -s $'\t'; else cat; fi; } table() { if command -v column >/dev/null; then column -t -s $'\t'; else cat; fi; }
sev_json=$(jq -cn --arg s "$SEVERITY" '$s | split(",") | map(ascii_upcase | gsub(" "; ""))') sev_json=$(jq -cn --arg s "$SEVERITY" '$s | split(",") | map(ascii_upcase | gsub(" "; ""))')
@@ -113,7 +188,7 @@ runs:
unfixed=$(jq --argjson sev "$sev_json" '[.Results[]?.Vulnerabilities[]? unfixed=$(jq --argjson sev "$sev_json" '[.Results[]?.Vulnerabilities[]?
| select((.Severity as $x | $sev | index($x)) and ((.FixedVersion // "") == ""))] | length' "$report") | select((.Severity as $x | $sev | index($x)) and ((.FixedVersion // "") == ""))] | length' "$report")
echo "Trivy $IMAGE: $nv fixable $SEVERITY, $ns secrets ($unfixed $SEVERITY without a fix, not blocking)" echo "Trivy $target: $nv fixable $SEVERITY, $ns secrets ($unfixed $SEVERITY without a fix, not blocking)"
if [ "$nv" -gt 0 ]; then if [ "$nv" -gt 0 ]; then
{ printf 'SEVERITY\tID\tPACKAGE\tINSTALLED\tFIXED\tTARGET\n'; cat "$work/vulns.tsv"; } | table { printf 'SEVERITY\tID\tPACKAGE\tINSTALLED\tFIXED\tTARGET\n'; cat "$work/vulns.tsv"; } | table
fi fi
@@ -126,8 +201,8 @@ runs:
echo "blocking=$blocking" >>"$GITHUB_OUTPUT" echo "blocking=$blocking" >>"$GITHUB_OUTPUT"
if [ "$blocking" -gt 0 ]; then if [ "$blocking" -gt 0 ]; then
if [ "$MODE" = block ]; then if [ "$MODE" = block ]; then
echo "::error::Trivy gate: $blocking blocking findings in $IMAGE" echo "::error::Trivy gate: $blocking blocking findings in $target"
exit 1 exit 1
fi fi
echo "::warning::Trivy gate (warn mode): $blocking findings in $IMAGE would block" echo "::warning::Trivy gate (warn mode): $blocking findings in $target would block"
fi fi