Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a3c4ae5edf | ||
|
|
67a711fe3c | ||
|
|
9591a27e59 | ||
|
|
95abc7822e | ||
|
|
7fae1fa2f6 |
No files matched your search
@@ -27,6 +27,20 @@ Intended order in a pipeline:
|
||||
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@v2
|
||||
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
|
||||
statement and an expiry, so they come back on their own:
|
||||
|
||||
@@ -56,3 +70,65 @@ Dependency-Track. Never fails the job.
|
||||
project: my-image
|
||||
version: latest
|
||||
```
|
||||
|
||||
## trivy-gate: misconfigurations
|
||||
|
||||
`scanners: misconfig` with `scan-type: fs` checks Dockerfiles, compose and
|
||||
other IaC in the tree. These findings are reported (HIGH/CRITICAL by
|
||||
default) and never block.
|
||||
|
||||
```yaml
|
||||
- uses: https://git.rfa.cz/tth-pub/ci-actions/trivy-gate@v2
|
||||
with:
|
||||
scan-type: fs
|
||||
scanners: misconfig
|
||||
mode: warn
|
||||
```
|
||||
|
||||
## gitleaks
|
||||
|
||||
Scans the commits of the push (`before..sha`, first-parent line, merge diffs
|
||||
off, so merging upstream into a fork does not report upstream history) for
|
||||
secrets, with redacted output. Without a `.gitleaks.toml` in the repo the
|
||||
default rules apply with test files and fixtures allowlisted. The release
|
||||
binary is verified by SHA-256.
|
||||
|
||||
```yaml
|
||||
- uses: https://git.rfa.cz/tth-pub/ci-actions/gitleaks@v2
|
||||
with:
|
||||
mode: block # or warn
|
||||
```
|
||||
|
||||
## Versions
|
||||
|
||||
`v1` image gate + SBOM; `v2` fs scans, binary runtime, misconfig, gitleaks;
|
||||
`v3` gitleaks fetches the parent of a single commit in a depth-1 checkout
|
||||
(dispatch/cron runs scanned the whole tree as a root commit before).
|
||||
|
||||
## portainer-deploy
|
||||
|
||||
Updates a Portainer Swarm stack from a compose file in the repository - the
|
||||
last step of the build → `trivy-gate` → move `latest` → deploy pipeline. The
|
||||
compose file in git is the single source of truth: Portainer's stored
|
||||
compose is replaced on every deploy. The stack's env vars (where the secrets
|
||||
live) are kept and never logged; only `vars` are overwritten. Images are
|
||||
re-pulled (`pullImage`).
|
||||
|
||||
```yaml
|
||||
- uses: https://git.rfa.cz/tth-pub/ci-actions/portainer-deploy@v4
|
||||
with:
|
||||
url: ${{ secrets.PORTAINER_URL }}
|
||||
token: ${{ secrets.PORTAINER_TOKEN }}
|
||||
stack-id: ${{ secrets.PORTAINER_STACK_ID }} # empty = skip with a notice
|
||||
compose-file: docker-compose.swarm.yml
|
||||
vars: |
|
||||
IMAGE_TAG=sha-${{ steps.build.outputs.sha }}
|
||||
smoke-url: https://app.example.org/health # optional
|
||||
smoke-status: "200"
|
||||
smoke-timeout: "180"
|
||||
```
|
||||
|
||||
Pin images in the compose file to the variable (`image: …:${IMAGE_TAG}`), so
|
||||
the deployed version is the commit that passed the gate, not whatever
|
||||
`latest` points at when Swarm pulls.
|
||||
|
||||
+14
-4
@@ -54,12 +54,22 @@ runs:
|
||||
[ -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"
|
||||
# Same runtime as trivy-gate: its container when a Docker daemon is
|
||||
# reachable, else the checksum-verified binary trivy-gate fetched.
|
||||
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
|
||||
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
|
||||
n=$(jq '.components | length' "$work/bom.json") || warn "unreadable SBOM"
|
||||
[ "$n" -ge "$MIN" ] || warn "only $n components, not uploading"
|
||||
|
||||
@@ -0,0 +1,133 @@
|
||||
name: gitleaks
|
||||
description: >
|
||||
Scan the commits a push brings for secrets with gitleaks. Only the
|
||||
first-parent line is scanned, so a merge of upstream into a fork reports
|
||||
our commits, not the upstream history. Findings are always redacted.
|
||||
|
||||
inputs:
|
||||
mode:
|
||||
description: "block = fail the step on findings, warn = report only."
|
||||
default: block
|
||||
log-opts:
|
||||
description: >
|
||||
git log range/options passed to gitleaks. Empty = the commits of this
|
||||
push (before..sha) on the first-parent line, else the last commit.
|
||||
default: ""
|
||||
config:
|
||||
description: >
|
||||
gitleaks config in the repo (allowlists); used when it exists. Without
|
||||
one, the default rules apply with test files allowlisted (fixtures
|
||||
carry fake keys by design and the noise would bury a real one).
|
||||
default: .gitleaks.toml
|
||||
skip:
|
||||
description: Emergency bypass (workflow_dispatch input); logged loudly.
|
||||
default: "false"
|
||||
|
||||
outputs:
|
||||
findings:
|
||||
description: Number of findings.
|
||||
value: ${{ steps.scan.outputs.findings }}
|
||||
|
||||
runs:
|
||||
using: composite
|
||||
steps:
|
||||
- id: scan
|
||||
shell: bash
|
||||
env:
|
||||
MODE: ${{ inputs.mode }}
|
||||
LOG_OPTS: ${{ inputs.log-opts }}
|
||||
CONFIG: ${{ inputs.config }}
|
||||
SKIP: ${{ inputs.skip }}
|
||||
BEFORE: ${{ github.event.before }}
|
||||
SHA: ${{ github.sha }}
|
||||
GITLEAKS_VERSION: "8.30.1"
|
||||
GITLEAKS_SHA256_X64: 551f6fc83ea457d62a0d98237cbad105af8d557003051f41f3e7ca7b3f2470eb
|
||||
GITLEAKS_SHA256_ARM64: e4a487ee7ccd7d3a7f7ec08657610aa3606637dab924210b3aee62570fb4b080
|
||||
run: |
|
||||
set -euo pipefail
|
||||
if [ "$SKIP" = "true" ]; then
|
||||
echo "::warning::GITLEAKS SKIPPED (manual bypass)"; echo "findings=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}/gitleaks"; mkdir -p "$work"
|
||||
if ! command -v jq >/dev/null; then
|
||||
(apt-get update -qq && apt-get install -y -qq jq) >/dev/null 2>&1 || { echo "::error::jq is required"; exit 1; }
|
||||
fi
|
||||
git config --global --add safe.directory "$PWD" 2>/dev/null || true
|
||||
|
||||
# Release binary, verified by SHA-256 (no Docker needed: the repo is
|
||||
# local to the job, wherever the job runs).
|
||||
bin="$work/gitleaks"
|
||||
if [ ! -x "$bin" ]; then
|
||||
case "$(uname -m)" in
|
||||
x86_64) arch=x64; sum="$GITLEAKS_SHA256_X64" ;;
|
||||
aarch64|arm64) arch=arm64; sum="$GITLEAKS_SHA256_ARM64" ;;
|
||||
*) echo "::error::no pinned gitleaks binary for $(uname -m)"; exit 1 ;;
|
||||
esac
|
||||
curl -fsSL -o "$work/gl.tgz" \
|
||||
"https://github.com/gitleaks/gitleaks/releases/download/v${GITLEAKS_VERSION}/gitleaks_${GITLEAKS_VERSION}_linux_${arch}.tar.gz"
|
||||
echo "$sum $work/gl.tgz" | sha256sum -c --quiet
|
||||
tar -xzf "$work/gl.tgz" -C "$work" gitleaks
|
||||
fi
|
||||
|
||||
opts="$LOG_OPTS"
|
||||
if [ -z "$opts" ]; then
|
||||
zero=0000000000000000000000000000000000000000
|
||||
if [ -n "$BEFORE" ] && [ "$BEFORE" != "$zero" ]; then
|
||||
# Checkouts are often shallow: deepen until the old head is there.
|
||||
for _ in 1 2 3 4 5 6; do
|
||||
git cat-file -e "$BEFORE^{commit}" 2>/dev/null && break
|
||||
git fetch -q --deepen=100 origin 2>/dev/null || break
|
||||
done
|
||||
if git cat-file -e "$BEFORE^{commit}" 2>/dev/null; then
|
||||
opts="--first-parent --diff-merges=off $BEFORE..$SHA"
|
||||
fi
|
||||
fi
|
||||
if [ -z "$opts" ]; then
|
||||
# One commit (dispatch/cron). In a depth-1 checkout it has no
|
||||
# parent and would be diffed as a root commit, i.e. the whole
|
||||
# tree (upstream files of a fork included): fetch its parent.
|
||||
head="${SHA:-HEAD}"
|
||||
git rev-parse -q --verify "$head^" >/dev/null 2>&1 || git fetch -q --deepen=1 origin 2>/dev/null || true
|
||||
opts="--diff-merges=off -1 $head"
|
||||
fi
|
||||
fi
|
||||
if [ -n "$CONFIG" ] && [ -f "$CONFIG" ]; then
|
||||
cfg=(--config "$CONFIG")
|
||||
else
|
||||
cat >"$work/default.toml" <<'TOML'
|
||||
[extend]
|
||||
useDefault = true
|
||||
|
||||
[[allowlists]]
|
||||
description = "test files and fixtures"
|
||||
paths = [
|
||||
'(^|/)(tests?|__tests__|testdata|fixtures?|e2e)/',
|
||||
'_test\.(go|dart|py)$',
|
||||
'(^|/)test_[^/]*\.py$',
|
||||
'\.(test|spec)\.(ts|js|mjs|tsx)$',
|
||||
'(^|/)test-[^/]*\.js$',
|
||||
]
|
||||
TOML
|
||||
cfg=(--config "$work/default.toml")
|
||||
fi
|
||||
echo "gitleaks over: git log $opts"
|
||||
|
||||
rc=0
|
||||
"$bin" git --no-banner --redact --log-level warn "${cfg[@]}" --log-opts "$opts" \
|
||||
--report-format json --report-path "$work/report.json" . || rc=$?
|
||||
[ "$rc" -le 1 ] || { echo "::error::gitleaks failed (exit $rc)"; exit 1; }
|
||||
n=$(jq length "$work/report.json" 2>/dev/null || echo 0)
|
||||
echo "findings=$n" >>"$GITHUB_OUTPUT"
|
||||
if [ "$n" -gt 0 ]; then
|
||||
jq -r '.[] | [.RuleID, .File, (.StartLine|tostring), .Commit[0:10]] | @tsv' "$work/report.json" \
|
||||
| { printf 'RULE\tFILE\tLINE\tCOMMIT\n'; cat; } \
|
||||
| { if command -v column >/dev/null; then column -t -s $'\t'; else cat; fi; }
|
||||
if [ "$MODE" = block ]; then
|
||||
echo "::error::gitleaks: $n secrets in the pushed commits - rotate them, then allowlist or rewrite"
|
||||
exit 1
|
||||
fi
|
||||
echo "::warning::gitleaks (warn mode): $n secrets in the pushed commits would block"
|
||||
else
|
||||
echo "gitleaks: no secrets in the pushed commits"
|
||||
fi
|
||||
@@ -0,0 +1,135 @@
|
||||
name: portainer-deploy
|
||||
description: >
|
||||
Update a Portainer Swarm stack from a compose file in the repository. The
|
||||
compose file is the single source of truth: Portainer's stored compose is
|
||||
replaced on every deploy, the stack's env vars (where the secrets live) are
|
||||
kept and only the given variables are overwritten. Images are re-pulled.
|
||||
Optionally waits until a URL answers with the expected status.
|
||||
|
||||
inputs:
|
||||
url:
|
||||
description: Portainer base URL.
|
||||
required: true
|
||||
token:
|
||||
description: Portainer API key (X-API-Key).
|
||||
required: true
|
||||
stack-id:
|
||||
description: >
|
||||
Portainer stack id. Empty = skip the deploy with a notice (a repo that
|
||||
has no stack yet).
|
||||
default: ""
|
||||
compose-file:
|
||||
description: Compose file in the checkout to deploy.
|
||||
required: true
|
||||
endpoint-id:
|
||||
description: Portainer environment (endpoint) id.
|
||||
default: "1"
|
||||
vars:
|
||||
description: >
|
||||
Stack variables to set, one NAME=value per line (e.g. IMAGE_TAG=sha-1234abcd).
|
||||
Every other stack variable is kept as it is.
|
||||
default: ""
|
||||
prune:
|
||||
description: Remove services that are no longer in the compose file.
|
||||
default: "false"
|
||||
smoke-url:
|
||||
description: URL polled after the deploy until it answers smoke-status. Empty = no check.
|
||||
default: ""
|
||||
smoke-status:
|
||||
description: Expected HTTP status of smoke-url.
|
||||
default: "200"
|
||||
smoke-timeout:
|
||||
description: Seconds to wait for smoke-url.
|
||||
default: "180"
|
||||
|
||||
runs:
|
||||
using: composite
|
||||
steps:
|
||||
- shell: bash
|
||||
env:
|
||||
PORTAINER_URL: ${{ inputs.url }}
|
||||
PORTAINER_TOKEN: ${{ inputs.token }}
|
||||
STACK_ID: ${{ inputs.stack-id }}
|
||||
COMPOSE_FILE: ${{ inputs.compose-file }}
|
||||
ENDPOINT_ID: ${{ inputs.endpoint-id }}
|
||||
SET_VARS: ${{ inputs.vars }}
|
||||
PRUNE: ${{ inputs.prune }}
|
||||
SMOKE_URL: ${{ inputs.smoke-url }}
|
||||
SMOKE_STATUS: ${{ inputs.smoke-status }}
|
||||
SMOKE_TIMEOUT: ${{ inputs.smoke-timeout }}
|
||||
run: |
|
||||
set -euo pipefail
|
||||
if [ -z "$STACK_ID" ]; then
|
||||
echo "::notice::portainer-deploy: no stack id, deploy skipped"
|
||||
exit 0
|
||||
fi
|
||||
: "${PORTAINER_URL:?url input is empty}" "${PORTAINER_TOKEN:?token input is empty}"
|
||||
[ -f "$COMPOSE_FILE" ] || { echo "::error::compose file $COMPOSE_FILE not found"; exit 1; }
|
||||
work="${RUNNER_TEMP:-/tmp}/portainer-deploy"; mkdir -p "$work"; chmod 700 "$work"
|
||||
trap 'rm -rf "$work"' EXIT
|
||||
base="${PORTAINER_URL%/}"
|
||||
|
||||
# The stack body carries the env (secrets): it goes from this GET into
|
||||
# the PUT body through a private temp file and never to the log.
|
||||
code=$(curl -sS -o "$work/stack.json" -w '%{http_code}' \
|
||||
-H "X-API-Key: $PORTAINER_TOKEN" "$base/api/stacks/$STACK_ID") || code=000
|
||||
if [ "$code" != 200 ]; then
|
||||
echo "::error::Portainer GET /api/stacks/$STACK_ID returned HTTP $code"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
python3 - "$work/stack.json" "$COMPOSE_FILE" "$work/body.json" <<'PY'
|
||||
import json, os, sys
|
||||
stack_path, compose_path, out_path = sys.argv[1:]
|
||||
with open(stack_path) as f:
|
||||
stack = json.load(f)
|
||||
sets = {}
|
||||
for line in os.environ.get("SET_VARS", "").splitlines():
|
||||
line = line.strip()
|
||||
if not line:
|
||||
continue
|
||||
name, sep, value = line.partition("=")
|
||||
if not sep or not name:
|
||||
sys.exit(f"portainer-deploy: bad vars line (want NAME=value): {name!r}")
|
||||
sets[name.strip()] = value
|
||||
env = [e for e in (stack.get("Env") or []) if e.get("name") not in sets]
|
||||
env += [{"name": k, "value": v} for k, v in sets.items()]
|
||||
with open(compose_path) as f:
|
||||
compose = f.read()
|
||||
body = {
|
||||
"stackFileContent": compose,
|
||||
"env": env,
|
||||
"prune": os.environ.get("PRUNE", "false").lower() == "true",
|
||||
"pullImage": True,
|
||||
}
|
||||
fd = os.open(out_path, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600)
|
||||
with os.fdopen(fd, "w") as f:
|
||||
json.dump(body, f)
|
||||
kept = len(env) - len(sets)
|
||||
print(f"stack {stack.get('Name', '?')}: setting {', '.join(f'{k}={v}' for k, v in sets.items()) or 'nothing'}; {kept} other variable(s) kept")
|
||||
PY
|
||||
|
||||
code=$(curl -sS -o "$work/resp.json" -w '%{http_code}' -X PUT \
|
||||
"$base/api/stacks/$STACK_ID?endpointId=$ENDPOINT_ID" \
|
||||
-H "X-API-Key: $PORTAINER_TOKEN" -H "Content-Type: application/json" \
|
||||
--data-binary "@$work/body.json") || code=000
|
||||
if [ "$code" != 200 ]; then
|
||||
echo "::error::Portainer stack PUT returned HTTP $code: $(head -c 300 "$work/resp.json" 2>/dev/null)"
|
||||
exit 1
|
||||
fi
|
||||
echo "stack $STACK_ID updated from $COMPOSE_FILE"
|
||||
|
||||
[ -n "$SMOKE_URL" ] || exit 0
|
||||
deadline=$(( $(date +%s) + SMOKE_TIMEOUT ))
|
||||
while :; do
|
||||
got=$(curl -sk -o /dev/null -w '%{http_code}' --max-time 10 "$SMOKE_URL" 2>/dev/null) || got=000
|
||||
if [ "$got" = "$SMOKE_STATUS" ]; then
|
||||
echo "smoke: $SMOKE_URL answered $got"
|
||||
break
|
||||
fi
|
||||
if [ "$(date +%s)" -ge "$deadline" ]; then
|
||||
echo "::error::smoke: $SMOKE_URL answered $got, expected $SMOKE_STATUS (waited ${SMOKE_TIMEOUT}s)"
|
||||
exit 1
|
||||
fi
|
||||
sleep 10
|
||||
done
|
||||
+115
-24
@@ -1,13 +1,30 @@
|
||||
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.
|
||||
Scan a pushed image (or a source tree's lockfiles) 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 or release.
|
||||
|
||||
inputs:
|
||||
image:
|
||||
description: Full image reference, ideally the immutable sha tag.
|
||||
required: true
|
||||
description: Full image reference, ideally the immutable sha tag (scan-type image).
|
||||
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: .
|
||||
scanners:
|
||||
description: >
|
||||
Trivy scanners. vuln and secret findings block (per mode); misconfig
|
||||
(Dockerfile, compose, k8s, ...) is only reported, never blocks.
|
||||
default: vuln,secret
|
||||
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:
|
||||
description: Registry user (private images).
|
||||
default: ""
|
||||
@@ -56,6 +73,13 @@ runs:
|
||||
shell: bash
|
||||
env:
|
||||
IMAGE: ${{ inputs.image }}
|
||||
SCAN_TYPE: ${{ inputs.scan-type }}
|
||||
SCAN_REF: ${{ inputs.scan-ref }}
|
||||
RUNTIME: ${{ inputs.runtime }}
|
||||
SCANNERS: ${{ inputs.scanners }}
|
||||
TRIVY_VERSION: "0.75.0"
|
||||
TRIVY_SHA256_AMD64: c6e65abddb348e25f10549df887045629cf28cc72453cd1c63acb717316b3f3f
|
||||
TRIVY_SHA256_ARM64: a1ee9f6ffb7d112b64ff726a2a0717c21175c1114361391f4a132956751a13b3
|
||||
TRIVY_USERNAME: ${{ inputs.username }}
|
||||
TRIVY_PASSWORD: ${{ inputs.password }}
|
||||
PLATFORM: ${{ inputs.platform }}
|
||||
@@ -68,37 +92,94 @@ runs:
|
||||
run: |
|
||||
set -euo pipefail
|
||||
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"
|
||||
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"
|
||||
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
|
||||
# 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 [ "$RUNTIME" = auto ]; then
|
||||
if command -v docker >/dev/null && docker info >/dev/null 2>&1; then RUNTIME=docker; else RUNTIME=binary; fi
|
||||
fi
|
||||
if ! command -v jq >/dev/null; then
|
||||
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 $SCANNERS --list-all-pkgs --format json --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"
|
||||
# fs scans see the git-tracked files plus lockfiles generated in the
|
||||
# job even when .gitignore lists them (Flutter ignores pubspec.lock).
|
||||
fs_list() {
|
||||
(cd "$SCAN_REF" && { git ls-files 2>/dev/null || find . -type f -not -path './.git/*'; \
|
||||
git ls-files --others 2>/dev/null \
|
||||
| grep -E '(^|/)([^/]*\.lock|[^/]*lock\.json|[^/]*\.lockfile)$' \
|
||||
| grep -vE '(^|/)(node_modules|build|\.dart_tool|\.pub-cache|\.gradle)/' || true; } \
|
||||
| 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; }
|
||||
sev_json=$(jq -cn --arg s "$SEVERITY" '$s | split(",") | map(ascii_upcase | gsub(" "; ""))')
|
||||
@@ -113,7 +194,7 @@ runs:
|
||||
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)"
|
||||
echo "Trivy $target: $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
|
||||
@@ -121,13 +202,23 @@ runs:
|
||||
{ printf 'SECRET\tRULE\tTITLE\tFILE\tLINE\n'; cat "$work/secrets.tsv"; } | table
|
||||
fi
|
||||
|
||||
# Misconfigurations: reported, never blocking.
|
||||
jq -r --argjson sev "$sev_json" '[.Results[]? | .Target as $t | .Misconfigurations[]?
|
||||
| select(.Status == "FAIL" and (.Severity as $x | $sev | index($x)))
|
||||
| [.Severity, .ID, .Title, $t]] | unique | .[] | @tsv' "$report" >"$work/misconf.tsv"
|
||||
nm=$(wc -l <"$work/misconf.tsv")
|
||||
if [ "$nm" -gt 0 ]; then
|
||||
echo "::warning::Trivy config: $nm $SEVERITY misconfigurations in $target (not blocking)"
|
||||
{ printf 'SEVERITY\tID\tTITLE\tFILE\n'; cat "$work/misconf.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"
|
||||
echo "::error::Trivy gate: $blocking blocking findings in $target"
|
||||
exit 1
|
||||
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
|
||||
Reference in new issue
Block a user