Secrets management
Leaked secrets (API keys, passwords, tokens, certificates) are one of the most common breach causes. Secrets management aims for them to never be in the code or the repo, to be stored encrypted, rotated, and audited. It’s not a solved problem: a secret shows up in a commit, a log, a container image’s environment variable, or a config file almost daily.
Threat model
Section titled “Threat model”A secret is any data whose compromise grants access to something. The risk depends on where it ends up and how long it lives:
- In the repo: a secret in Git stays in the history forever, even if the next commit removes it. A leaked public repo is an immediate disaster; a private one, a bomb waiting for an access leak.
- In logs and error messages: tokens printed in a trace end up in the SIEM, in support, on screens.
- In container images: an
ENV API_KEY=...or a.envfile copied into the image stays in a recoverable layer (see containers). - In environment variables: visible to any process in the container and often in crash dumps.
- Long-lived secret: a key that never expires and never rotates is a secret that, once leaked, stays useful for years.
Hierarchy of solutions
Section titled “Hierarchy of solutions”From worst to best, the path a mature team walks:
- Hardcoded in the code — never. It stays in the history.
.envfile outside the repo — better than nothing, but flat and unrotated.- Orchestrator environment variables — separates secret from code, but visible in the process.
- Secrets manager (Vault, cloud KMS/Secrets Manager) — encrypted at rest, access control, auditing, rotation.
- Dynamic secrets — the manager generates ephemeral credentials on demand (e.g. a DB user/password pair valid for one hour). A secret that doesn’t exist until needed can’t be leaked ahead of time.
flowchart LR
APP["Application"] -->|authenticate identity| VAULT["Secrets manager"]
VAULT -->|validate| IDP["Identity (OIDC / IAM role)"]
VAULT -->|short-TTL ephemeral credential| APP
VAULT -->|rotation / revocation| DB[(Backend: DB, cloud, PKI)]
APP -. uses .-> DB
AUDIT["Audit log"] -.-> VAULT
The key to the pattern: the app doesn’t store the secret; it authenticates with its identity (an IAM role, a pipeline OIDC token) and the manager hands it a short-lived credential.
Detection: secret scanning
Section titled “Detection: secret scanning”Because secrets leak anyway, you have to detect them. Scanning looks for patterns (known key formats) and high entropy (random strings):
# gitleaks over the full historygitleaks detect --source . --report-format sarif --report-path leaks.sarif
# trufflehog, also verifying whether the secret is still livetrufflehog git file://. --only-verifiedIn CI, as a pre-merge gate and as a local hook:
name: secret-scanon: [pull_request]jobs: gitleaks: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 with: { fetch-depth: 0 } # full history - uses: gitleaks/gitleaks-action@v2A pre-commit hook running gitleaks locally stops the secret from even leaving the machine.
Red Team
Section titled “Red Team”Secrets are one of the most profitable targets in any authorized assessment:
- Git history mining:
git log -p, gitleaks, and trufflehog over a repo’s full history (or forks and gists) surface keys the team thinks were deleted. - Images and artifacts: inspecting an image’s layers (
docker history, extracting layers) reveals.env, SSH keys, and tokens copied into the build. - Cloud and CI metadata: from an SSRF or a compromised pipeline, steal the associated role’s token (see cloud metadata and CI/CD).
- Reuse: a leaked long-lived secret gets tried against every service in the org; lack of rotation amplifies the impact.
Blue Team
Section titled “Blue Team”- Prevent at the source: pre-commit hooks + CI gate with scanning. Keeping the secret out is cheaper than rotating it.
- Secrets manager with granular access control, auditing, and, where possible, dynamic secrets.
- Automatic rotation and short TTLs: shrink the window a leak is useful.
- Identity instead of secrets: pipeline OIDC into the cloud (no static keys), IAM roles for services.
- Leak response: if a secret leaks, revoke and rotate it immediately; deleting it from Git history (BFG,
filter-repo) is not enough — assume it’s compromised. - Don’t put it in environment variables if you can mount it as an ephemeral file or fetch it at runtime.
Checklist
Section titled “Checklist”- Zero secrets in code or versioned configuration.
- Secret scanning in pre-commit and CI, over the full history.
- Secrets manager (Vault / KMS / Secrets Manager) as the single source.
- Dynamic or short-lived secrets where the backend allows.
- Identity-based auth (OIDC/IAM), not static keys, in pipelines.
- Automatic rotation and defined TTLs.
- Revoke+rotate procedure for a leak.
- Secrets kept out of logs, error messages, and container images.