diff --git a/README.md b/README.md index 70b05ab..d97fdf2 100644 --- a/README.md +++ b/README.md @@ -70,3 +70,31 @@ 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@v1.2 + 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@v1.2 + with: + mode: block # or warn +``` diff --git a/gitleaks/action.yml b/gitleaks/action.yml new file mode 100644 index 0000000..41f15f0 --- /dev/null +++ b/gitleaks/action.yml @@ -0,0 +1,126 @@ +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 + [ -n "$opts" ] || opts="--diff-merges=off -1 ${SHA:-HEAD}" + 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 diff --git a/trivy-gate/action.yml b/trivy-gate/action.yml index c0fc923..0a9d0a7 100644 --- a/trivy-gate/action.yml +++ b/trivy-gate/action.yml @@ -15,6 +15,11 @@ inputs: 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 @@ -71,6 +76,7 @@ runs: 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 @@ -108,7 +114,7 @@ runs: || { echo "::error::jq is required"; exit 1; } fi - common="--skip-java-db-update --scanners vuln,secret --list-all-pkgs --format json --quiet" + common="--skip-java-db-update --scanners $SCANNERS --list-all-pkgs --format json --quiet" ignore_args="" if [ -n "$IGNOREFILE" ] && [ -f "$IGNOREFILE" ]; then echo "ignore file: $IGNOREFILE ($(grep -c -- '- id:' "$IGNOREFILE" || true) entries)" @@ -196,6 +202,16 @@ 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"