Secrets Management & CI OIDC
Environment variables vs secret managers, secret rotation, what to do if a secret is committed to Git, and replacing long-lived CI keys with OIDC.
TL;DR
Keep secrets out of Git, client bundles, and logs. Use .env (gitignored) only for local development, store production secrets in a secret manager with rotation, and use OIDC federation in CI/CD instead of long-lived cloud keys. If a secret ever hits Git, revoke and rotate it first.
Why it exists
Hardcoded API keys and leaked .env files remain one of the fastest ways attackers compromise cloud accounts and databases. Automated scanners scrape public commits within seconds of a push, so relying on "I'll delete it in the next commit" never works.
Env vars vs secret managers
- Local dev (
.env.local): Fine on a developer laptop when listed in.gitignoreand paired with a committed.env.examplecontaining dummy placeholders. - Production (
Secret Manager): Store runtime secrets in a dedicated manager (Cloudflare Secrets, AWS Secrets Manager, GCP Secret Manager, Vault) so you get access audit logs, least-privilege IAM, and rotation without redeploying images or committing files. - Static sites (like this repo): Any
NEXT_PUBLIC_*variable is baked intoout/HTML/JS—never put private tokens there. In this repo's.github/workflows/inbox.yml, the only secret used is the ephemeral per-jobGITHUB_TOKENwithpermissions: issues: write.
CI secrets with OIDC (no long-lived cloud keys)
Instead of storing static AWS_ACCESS_KEY_ID / AWS_SECRET_ACCESS_KEY secrets in GitHub:
- Grant the workflow
permissions: id-token: write(andcontents: read). - CI requests a short-lived OIDC JWT signed by GitHub (
sub: repo:owner/name:ref:refs/heads/main). - Your cloud IAM role trusts that exact
subclaim and issues temporary credentials that expire in minutes.
# UNSAFE: long-lived static cloud keys stored in CI secrets (not run)
# env:
# AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
# AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
# SAFE: short-lived OIDC federation scoped to a branch/environment (not run)
permissions:
id-token: write
contents: read
steps:
- uses: aws-actions/configure-aws-credentials@<40-char-sha>
with:
role-to-assume: arn:aws:iam::123456789012:role/deploy-prod
aws-region: ap-southeast-1What to do when a secret leaks into Git
- Revoke and rotate immediately: Assume the secret was copied the moment it was pushed. Disable the old key and issue a new one.
- Audit access logs: Check cloud/DB logs for unauthorized usage during the exposure window.
- Purge history (optional cleanup, never a substitute for rotation): Use
git filter-repoif you must scrub the blob from history, then force-push and ask collaborators to re-clone. - Prevent recurrence: Enable GitHub secret scanning + push protection and pre-commit hooks (
gitleaks).
Gotchas
git rm .env && git commitleaves the secret in history: Anyone withgit log -pcan still read the old blob.- Printing
envin CI debug steps: Never runprintenvor dumpprocess.envin CI logs; even masked secrets can leak if base64-encoded or transformed. - Sharing one DB password across dev, staging, and prod: A compromise in dev immediately opens production.
Production checklist
-
.env*files are in.gitignore; only.env.example(with fake values) is committed. - Secret scanning / push protection is enabled on the repository.
- Production secrets live in a secret manager with documented rotation ownership.
- Cloud deployments from CI use OIDC short-lived tokens instead of static IAM keys.
Check yourself
Check Yourself
1. You accidentally pushed an API key to a public GitHub repo 2 minutes ago. What must you do first?
2. How does OIDC improve CI/CD deployment security compared to static cloud secrets?
Sources
Related Notes
Rate Limiting & Brute-Force Protection
Token bucket vs fixed/sliding window algorithms, edge vs application enforcement, choosing rate-limit keys, and returning 429 Too Many Requests with Retry-After.
Software Supply Chain Security
Securing dependencies and CI/CD pipelines with lockfiles, SHA-pinned GitHub Actions (with this repo as an honest example), pnpm audit, Renovate/Dependabot, SBOMs, and SLSA provenance.