Files
software-infrastructure/.gitea/workflows/reusable-secret-scan.yaml
claudeandClaude Opus 5.5 ef693ffd97 G9: reusable gitleaks secret scan, config and docs
Add a reusable workflow that downloads a pinned, checksum-verified gitleaks
and scans the pushed commits, the PR's commits, or the current tree. Logs
show only a count, never the rule, file, line or value, because the logs
are public. This repo's caller is manual-only until S8 passes.

.gitleaks.toml extends the default rules and allowlists Helm checksum/*
annotations. docs/ci-cd/secret-scanning.md covers usage, what to do when it
fails, and the policy for history scans (done privately, not in CI).

Tested locally by running the step verbatim in a container against
throwaway repos: a planted fake token fails on push, new-branch push and PR;
clean ranges, checksum annotations and this repo's tree pass; a bad
checksum fails; the value never appears in the output. actionlint and
yamllint pass.

Refs DevOps/software-infrastructure#57

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-14 18:44:40 -05:00

113 lines
4.6 KiB
YAML

# Reusable secret scan (gitleaks). DevOps/software-infrastructure#57 (G9).
# Usage and policy: docs/ci-cd/secret-scanning.md.
#
# What it scans:
# push the pushed commits (before..sha); the current tree if the
# push created the branch or "before" can't be found
# pull_request the PR's commits (base..head)
# anything else the current tree only (not history)
#
# Repos are public and so are their Actions logs, so by default the log shows
# only how many possible secrets were found, never which file, line or rule.
# Run gitleaks locally to see details (the command is printed on failure).
# Full-history scans are done privately, not in CI.
name: reusable-secret-scan
on:
workflow_call:
inputs:
gitleaks-version:
description: gitleaks release to download. Change gitleaks-sha256 with it.
type: string
default: "8.30.1"
gitleaks-sha256:
description: SHA-256 of gitleaks_<version>_linux_x64.tar.gz from the release's checksums file.
type: string
default: "551f6fc83ea457d62a0d98237cbad105af8d557003051f41f3e7ca7b3f2470eb"
show-details:
description: Print rule, file and line (values stay redacted). Only for private repos.
type: boolean
default: false
permissions:
contents: read
jobs:
gitleaks:
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
- uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0
with:
fetch-depth: 0
- name: Scan for secrets
env:
GITLEAKS_VERSION: ${{ inputs.gitleaks-version }}
GITLEAKS_SHA256: ${{ inputs.gitleaks-sha256 }}
SHOW_DETAILS: ${{ inputs.show-details }}
EVENT: ${{ github.event_name }}
SHA: ${{ github.sha }}
BEFORE: ${{ github.event.before }}
BASE_SHA: ${{ github.event.pull_request.base.sha }}
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
run: |
set -euo pipefail
tmp="${RUNNER_TEMP:-/tmp}/secret-scan"
mkdir -p "$tmp"
url="https://github.com/gitleaks/gitleaks/releases/download/v${GITLEAKS_VERSION}/gitleaks_${GITLEAKS_VERSION}_linux_x64.tar.gz"
curl -fsSL --retry 3 -o "$tmp/gitleaks.tgz" "$url"
echo "${GITLEAKS_SHA256} $tmp/gitleaks.tgz" | sha256sum -c --quiet -
tar -xzf "$tmp/gitleaks.tgz" -C "$tmp" gitleaks
gl="$tmp/gitleaks"
is_commit() { [ -n "$1" ] && ! [[ "$1" =~ ^0+$ ]] && git cat-file -e "$1^{commit}" 2>/dev/null; }
scope=(dir .)
what="current tree"
case "$EVENT" in
pull_request|pull_request_target)
if is_commit "$BASE_SHA" && is_commit "$HEAD_SHA"; then
scope=(git "--log-opts=${BASE_SHA}..${HEAD_SHA}" .)
what="commits ${BASE_SHA:0:8}..${HEAD_SHA:0:8}"
fi ;;
push)
if is_commit "$BEFORE" && is_commit "$SHA"; then
scope=(git "--log-opts=${BEFORE}..${SHA}" .)
what="commits ${BEFORE:0:8}..${SHA:0:8}"
fi ;;
esac
args=(--no-banner --no-color --redact=100 --exit-code=3 --log-level=error
--report-format=json --report-path="$tmp/report.json")
if [ "$SHOW_DETAILS" = "true" ]; then args+=(--verbose); fi
echo "Scanning ${what} with gitleaks ${GITLEAKS_VERSION}"
set +e
"$gl" "${scope[@]}" "${args[@]}" >"$tmp/out.log" 2>&1
rc=$?
set -e
case "$rc" in
0)
echo "No secrets found." ;;
3)
n=$(grep -o '"RuleID"' "$tmp/report.json" | wc -l)
if [ "$SHOW_DETAILS" = "true" ]; then cat "$tmp/out.log"; fi
echo "::error::Secret scan found ${n} possible secret(s) in the ${what}."
echo "Details are not printed here because this log may be public."
echo "To see them, run gitleaks on your checkout, for example:"
img="ghcr.io/gitleaks/gitleaks:v${GITLEAKS_VERSION}"
echo " docker run --rm -v \"\$PWD:/repo\" ${img} ${scope[0]} --redact -v /repo"
echo "If it is a real secret, rotate it first: removing it from git does not make it safe."
echo "If it is a false positive, add an allowlist entry to .gitleaks.toml"
echo "(see docs/ci-cd/secret-scanning.md in DevOps/software-infrastructure)."
exit 1 ;;
*)
cat "$tmp/out.log"
echo "::error::gitleaks failed to run (exit ${rc})."
exit "$rc" ;;
esac