Secrets management
Secrets are credentials that grant access: API keys, DB passwords, tokens. Never in source control, ideally never in plain env vars, always rotatable.
Secrets are credentials that grant access: API keys, database passwords, signing keys, OAuth client secrets, webhook secrets. If it could let an attacker do something you don't want them to do, it's a secret.
The hierarchy of secret storage, worst to best.
- Hardcoded in source. Catastrophic. Git history is forever.
- .env file committed to git. Same problem.
- .env file in .gitignore. Better, but the file still exists on every dev's machine, ends up in Docker images, leaks via CI logs.
- Environment variables set by deploy process. Standard. Visible in
/proc/<pid>/environ, in crash dumps, in subprocess inheritance. - Secret manager (AWS Secrets Manager, GCP Secret Manager, Azure Key Vault, HashiCorp Vault). Pulled at runtime. Audited. Rotatable. The right answer for production.
- Hardware Security Module (HSM) for the most sensitive keys (signing keys, root CA). Key never leaves the device.
The four practices that actually matter.
- Pull secrets at runtime from a manager. App authenticates via cloud IAM (no static credentials needed), fetches secrets, caches in memory.
- Rotate regularly. DB passwords every 90 days, signing keys yearly, OAuth client secrets when team changes. Automated rotation > manual rotation > no rotation.
- Audit access. Every secret read is logged. Alert on anomalous patterns (3am access, new IP, new principal).
- Scope by environment. Dev and prod secrets are completely separate, with separate IAM. Devs cannot read prod secrets, even by accident.
For local development.
- direnv or .envrc with .env files (gitignored). Fine for dev.
- 1Password CLI or Doppler for the secrets that match prod (Stripe test keys, etc).
- Never the production secrets on dev machines.
When a secret leaks, the only safe response is rotation. Auditing what was accessed during the leak window is second priority. Hoping it wasn't used is not a strategy.
Learn more
- Docs12-Factor App: Config12factor.net
- Docs
- DocsHashiCorp VaultHashiCorp