commit 86409a2413173a677e308d2d7ea0938f85c5e084 Author: TeTeHacko Date: Sun Oct 25 18:14:04 2026 +0100 trivy-gate and dtrack-sbom composite actions Co-Authored-By: Claude Opus 5.5 diff --git a/README.md b/README.md new file mode 100644 index 0000000..f559b6a --- /dev/null +++ b/README.md @@ -0,0 +1,58 @@ +# ci-actions + +Shared composite actions for Gitea Actions. + +## trivy-gate + +Scans a pushed image with Trivy (vulnerabilities + secrets) and fails when it +finds a **fixable** vulnerability of the given severities (default +`HIGH,CRITICAL`) or an embedded secret. Vulnerabilities without a fix are +reported but never block - otherwise a deploy could be stuck forever. + +Intended order in a pipeline: + +1. build and push only the immutable tag (`sha-…`) +2. `trivy-gate` on that tag +3. move the floating tag (`latest`, `stable-`) and deploy + +```yaml +- id: trivy + uses: https://git.rfa.cz/tth-pub/ci-actions/trivy-gate@v1 + with: + image: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:sha-${{ steps.build.outputs.sha }} + username: ${{ gitea.actor }} + password: ${{ secrets.REGISTRY_TOKEN }} + platform: linux/arm64 # single-platform non-amd64 images only + mode: block # or warn + skip: ${{ inputs.skip_trivy || 'false' }} +``` + +Accepted risks go to `.trivyignore.yaml` in the calling repo, each with a +statement and an expiry, so they come back on their own: + +```yaml +vulnerabilities: + - id: CVE-2026-12345 + statement: not reachable, the affected feature is disabled + expired_at: 2026-12-31 +``` + +The vulnerability DB lives in the Docker named volume `trivy-cache` on the +runner host (refreshed under an exclusive lock, scans read it read-only under +a shared lock, so parallel matrix jobs are fine). + +## dtrack-sbom + +Converts the `trivy-gate` report to CycloneDX and uploads it to +Dependency-Track. Never fails the job. + +```yaml +- uses: https://git.rfa.cz/tth-pub/ci-actions/dtrack-sbom@v1 + with: + report: ${{ steps.trivy.outputs.report }} + url: ${{ vars.DTRACK_URL }} + api-key: ${{ secrets.DTRACK_API_KEY }} + ca-pem: ${{ vars.RFA_CA_PEM }} + project: my-image + version: latest +``` diff --git a/dtrack-sbom/action.yml b/dtrack-sbom/action.yml new file mode 100644 index 0000000..ff1ec12 --- /dev/null +++ b/dtrack-sbom/action.yml @@ -0,0 +1,73 @@ +name: dtrack-sbom +description: > + Convert a trivy-gate report to CycloneDX and upload it to Dependency-Track. + Never fails the job: an SBOM outage must not stop a deploy, it only warns. + +inputs: + report: + description: Trivy JSON report from trivy-gate (outputs.report). + required: true + url: + description: Dependency-Track base URL. + required: true + api-key: + description: API key with BOM_UPLOAD (+ PROJECT_CREATION_UPLOAD). + required: true + ca-pem: + description: PEM of the CA that signed the Dependency-Track certificate. + default: "" + project: + description: Project name in Dependency-Track. + required: true + version: + description: Project version (use the deployed tag, e.g. latest). + required: true + extra-components: + description: > + JSON file with a CycloneDX components array appended to the SBOM + (vendored libraries without package metadata). + default: "" + min-components: + description: Refuse to upload a suspiciously small SBOM. + default: "5" + trivy-image: + description: Trivy container image, pinned by digest. + default: aquasec/trivy:0.75.0@sha256:af6acf9a6b85dfe389a1941505c0ce9efef52a4719635e1a962f022a3d855daa + +runs: + using: composite + steps: + - shell: bash + env: + REPORT: ${{ inputs.report }} + DT_URL: ${{ inputs.url }} + DT_KEY: ${{ inputs.api-key }} + CA_PEM: ${{ inputs.ca-pem }} + PROJECT: ${{ inputs.project }} + VERSION: ${{ inputs.version }} + EXTRA: ${{ inputs.extra-components }} + MIN: ${{ inputs.min-components }} + TRIVY: ${{ inputs.trivy-image }} + run: | + set -uo pipefail + warn() { echo "::warning::SBOM $PROJECT/$VERSION: $*"; exit 0; } + [ -n "$REPORT" ] && [ -f "$REPORT" ] || warn "no report (gate skipped?)" + [ -n "$DT_KEY" ] || warn "no API key" + work="${RUNNER_TEMP:-/tmp}/dtrack-sbom"; mkdir -p "$work" + 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" + if [ -n "$EXTRA" ]; then + jq --slurpfile extra "$EXTRA" '.components += $extra[0]' "$work/bom.json" >"$work/bom2.json" \ + && mv "$work/bom2.json" "$work/bom.json" || warn "cannot merge $EXTRA" + fi + n=$(jq '.components | length' "$work/bom.json") || warn "unreadable SBOM" + [ "$n" -ge "$MIN" ] || warn "only $n components, not uploading" + ca=() + if [ -n "$CA_PEM" ]; then printf '%s\n' "$CA_PEM" >"$work/ca.pem"; ca=(--cacert "$work/ca.pem"); fi + code=$(curl -sS "${ca[@]}" -o "$work/resp.json" -w '%{http_code}' \ + -X POST "$DT_URL/api/v1/bom" -H "X-Api-Key: $DT_KEY" \ + -F autoCreate=true -F "projectName=$PROJECT" -F "projectVersion=$VERSION" \ + -F "bom=@$work/bom.json") || code=000 + [ "$code" = 200 ] || { cat "$work/resp.json" 2>/dev/null; echo; warn "HTTP $code"; } + echo "SBOM $PROJECT/$VERSION: $n components uploaded (token $(jq -r .token "$work/resp.json"))" diff --git a/trivy-gate/action.yml b/trivy-gate/action.yml new file mode 100644 index 0000000..2d65651 --- /dev/null +++ b/trivy-gate/action.yml @@ -0,0 +1,133 @@ +name: trivy-gate +description: > + Scan a pushed image with Trivy and fail when it has fixable vulnerabilities + of the given severities or embedded secrets. Run it on the immutable tag, + before the floating tag (latest / stable-*) moves and before any deploy. + +inputs: + image: + description: Full image reference, ideally the immutable sha tag. + required: true + username: + description: Registry user (private images). + default: "" + password: + description: Registry token (private images). + default: "" + platform: + description: Platform of a multi-platform image, e.g. linux/arm64. + default: "" + severity: + description: Severities that block, comma separated. + default: HIGH,CRITICAL + mode: + description: "block = fail the step on findings, warn = report only." + default: block + ignorefile: + description: > + Trivy ignore file in the repo (YAML, each entry with a reason and + expired_at). Used when it exists. + default: .trivyignore.yaml + skip: + description: Emergency bypass (workflow_dispatch input); logged loudly. + default: "false" + trivy-image: + description: Trivy container image, pinned by digest. + default: aquasec/trivy:0.75.0@sha256:af6acf9a6b85dfe389a1941505c0ce9efef52a4719635e1a962f022a3d855daa + cache-volume: + description: > + Docker named volume for the vulnerability DB. A named volume, not a + path: the job talks to the host's Docker daemon, which cannot see + paths inside the job container. + default: trivy-cache + +outputs: + report: + description: Path of the Trivy JSON report (with all packages, for SBOM). + value: ${{ steps.scan.outputs.report }} + blocking: + description: Number of blocking findings (vulnerabilities + secrets). + value: ${{ steps.scan.outputs.blocking }} + +runs: + using: composite + steps: + - id: scan + shell: bash + env: + IMAGE: ${{ inputs.image }} + TRIVY_USERNAME: ${{ inputs.username }} + TRIVY_PASSWORD: ${{ inputs.password }} + PLATFORM: ${{ inputs.platform }} + SEVERITY: ${{ inputs.severity }} + MODE: ${{ inputs.mode }} + IGNOREFILE: ${{ inputs.ignorefile }} + SKIP: ${{ inputs.skip }} + TRIVY: ${{ inputs.trivy-image }} + VOLUME: ${{ inputs.cache-volume }} + run: | + set -euo pipefail + if [ "$SKIP" = "true" ]; then + echo "::warning::TRIVY GATE SKIPPED for $IMAGE (manual bypass)" + echo "report=" >>"$GITHUB_OUTPUT"; echo "blocking=0" >>"$GITHUB_OUTPUT" + exit 0 + fi + 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" + report="$work/$(printf '%s' "$IMAGE" | tr -c 'A-Za-z0-9._-' '_').json" + + # 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' + + ignore_args="" + if [ -n "$IGNOREFILE" ] && [ -f "$IGNOREFILE" ]; then + ignore_args="--ignorefile /tmp/ignore.yaml" + echo "ignore file: $IGNOREFILE ($(grep -c -- '- id:' "$IGNOREFILE" || true) entries)" + fi + platform_args="" + [ -z "$PLATFORM" ] || platform_args="--platform $PLATFORM" + + # 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 --skip-java-db-update \ + --cache-backend memory --image-src remote --scanners vuln,secret --list-all-pkgs \ + --format json --quiet $ignore_args $platform_args \"\$0\"" "$IMAGE" \ + < <( [ -n "$ignore_args" ] && cat "$IGNOREFILE" || true ) >"$report" + + 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(" "; ""))') + jq -r --argjson sev "$sev_json" ' + [.Results[]? | .Target as $t | .Vulnerabilities[]? + | select((.Severity as $x | $sev | index($x)) and ((.FixedVersion // "") != "")) + | [.Severity, .VulnerabilityID, .PkgName, .InstalledVersion, (.FixedVersion | split(", ")[0]), $t]] + | unique | .[] | @tsv' "$report" >"$work/vulns.tsv" + jq -r '[.Results[]? | .Target as $t | .Secrets[]? | ["SECRET", .RuleID, .Title, $t, (.StartLine | tostring)]] | .[] | @tsv' \ + "$report" >"$work/secrets.tsv" + nv=$(wc -l <"$work/vulns.tsv"); ns=$(wc -l <"$work/secrets.tsv") + unfixed=$(jq --argjson sev "$sev_json" '[.Results[]?.Vulnerabilities[]? + | 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)" + if [ "$nv" -gt 0 ]; then + { printf 'SEVERITY\tID\tPACKAGE\tINSTALLED\tFIXED\tTARGET\n'; cat "$work/vulns.tsv"; } | table + fi + if [ "$ns" -gt 0 ]; then + { printf 'SECRET\tRULE\tTITLE\tFILE\tLINE\n'; cat "$work/secrets.tsv"; } | table + fi + + blocking=$((nv + ns)) + echo "report=$report" >>"$GITHUB_OUTPUT" + echo "blocking=$blocking" >>"$GITHUB_OUTPUT" + if [ "$blocking" -gt 0 ]; then + if [ "$MODE" = block ]; then + echo "::error::Trivy gate: $blocking blocking findings in $IMAGE" + exit 1 + fi + echo "::warning::Trivy gate (warn mode): $blocking findings in $IMAGE would block" + fi