TExcelTAD
Home › Secrets & Policy as Code: Vault, SOPS, OPA & Conftest › Secret Scanning Beyond the Repository

Secret Scanning Beyond the Repository

The CI/CD course runs gitleaks on Git history, with custom rules, allowlists and baselines, and TruffleHog with verification. This lesson points the same scanner at the places teams forget: logs, build outputs and unpacked artifacts, and adds a rule for the secret linkly cares about most, its database URL.

A rule for connection strings

Database URLs with an embedded password (postgres://user:pass@host/db) are one of the most common leaks, and the default rules don't always flag them (as the CI/CD lesson showed). A custom rule with a capture group reports only the password part as the secret:

bash · try it yourself
cat > .gitleaks.toml <<'TOML'
[extend]
useDefault = true

[[rules]]
id = "postgres-url-password"
description = "Password embedded in a PostgreSQL connection URL"
regex = '''postgres(?:ql)?://[^:/\s]+:([^@\s]{6,})@'''
secretGroup = 1
keywords = ["postgres://", "postgresql://"]

[[rules.allowlists]]
description = "local development defaults"
regexes = ['''^(postgres|changeme|password)
  

'']
TOML

mkdir -p build logs
cat > build/config.yaml <<'YAML'
database_url: postgres://linkly:Xk29-pQm8-vR4z@db.prod.internal:5432/linkly
YAML
cat > build/dev.yaml <<'YAML'
database_url: postgres://linkly:postgres@localhost:5432/linkly
YAML
cat > logs/app.log <<'LOG'
{"level":"INFO","msg":"starting","addr":":8080"}
{"level":"ERROR","msg":"connect failed","dsn":"postgresql://linkly:Xk29-pQm8-vR4z@10.0.3.17:5432/linkly"}
LOG

echo "### files"
gitleaks dir build --config .gitleaks.toml --no-banner --redact -r files.json > /dev/null 2>&1; echo "exit $?"
jq -r '.[] | "\(.RuleID)  \(.File):\(.StartLine)  secret=\(.Secret)"' files.json
echo "### application logs"
gitleaks dir logs --config .gitleaks.toml --no-banner --redact -r log.json > /dev/null 2>&1; echo "exit $?"
jq -r '.[] | "\(.RuleID)  line \(.StartLine)  match=\(.Match)"' log.json
### files
exit 1
postgres-url-password  build/config.yaml:1  secret=REDACTED
### application logs
exit 1
postgres-url-password  line 2  match=postgresql://linkly:REDACTED@
  • secretGroup = 1 makes the password the "secret"; with --redact, reports and logs show REDACTED instead of it, so the scan report itself doesn't become a leak.
  • The rule-level allowlist ignores the well-known development password in dev.yaml.
  • Logs are just files to gitleaks. For streams, gitleaks stdin scans anything you can pipe: a CI job log downloaded through the API, kubectl logs, a support bundle.

Where else to scan

Target How
Container images export the file system (docker export, crane export, or Trivy's built-in secret scanner: trivy image --scanners secret) and run gitleaks dir on it
CI logs fetch the job logs (GitHub: gh run view --log; GitLab: the jobs API) and pipe them into gitleaks stdin on a schedule
Terraform plans scan show -json output before uploading it as an artifact, or better, keep secrets out with write-only arguments (Conftest on Terraform Plans)
Wikis, tickets, chat the platforms' own scanners (GitHub/GitLab secret scanning on issues, commercial scanners for Slack and Confluence)
Developer laptops a pre-commit hook: gitleaks git --pre-commit --staged .

The gate: fail closed, report safely

In a pipeline, a scan step should:

  1. fail the job on a finding (exit code 1 is the default; --exit-code changes it),
  2. redact findings in logs and reports (--redact),
  3. upload a machine-readable report (--report-format sarif for code-scanning UIs),
  4. and route real findings to a human who can rotate the secret. A scanner that finds a live credential has only told you that rotation is now urgent.

Test yourself

What does secretGroup = 1 do in a gitleaks rule?

  1. Limits the rule to one file
  2. Marks capture group 1 as the secret, so only that part is reported and redacted
  3. Groups findings by author
  4. Runs the rule first

Why pass --redact in CI?

  1. It speeds up scanning
  2. So the scanner's own log and report don't print the secret it found
  3. It deletes the secret from Git
  4. It rotates the secret