Skip to Content
Mengty Learns
Security

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.

how-tobeginnerTested with asvs 5.0.0 (V13), owasp_top_10 2025 (A02, A04)Updated Nov 26, 2026

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 .gitignore and paired with a committed .env.example containing 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 into out/ HTML/JS—never put private tokens there. In this repo's .github/workflows/inbox.yml, the only secret used is the ephemeral per-job GITHUB_TOKEN with permissions: 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:

  1. Grant the workflow permissions: id-token: write (and contents: read).
  2. CI requests a short-lived OIDC JWT signed by GitHub (sub: repo:owner/name:ref:refs/heads/main).
  3. Your cloud IAM role trusts that exact sub claim 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-1

What to do when a secret leaks into Git

  1. Revoke and rotate immediately: Assume the secret was copied the moment it was pushed. Disable the old key and issue a new one.
  2. Audit access logs: Check cloud/DB logs for unauthorized usage during the exposure window.
  3. Purge history (optional cleanup, never a substitute for rotation): Use git filter-repo if you must scrub the blob from history, then force-push and ask collaborators to re-clone.
  4. Prevent recurrence: Enable GitHub secret scanning + push protection and pre-commit hooks (gitleaks).

Gotchas

  • git rm .env && git commit leaves the secret in history: Anyone with git log -p can still read the old blob.
  • Printing env in CI debug steps: Never run printenv or dump process.env in 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