Managing Secrets and Configuration
Keep API keys, database passwords and signing keys out of code and rotate them safely.
Intermediate · about 3 hours · difficulty 2/5
Learn first: Version Control with Git, Where Backends Run: Hosting Options
A secret is anything that grants access: database passwords, API keys, JWT signing keys, OAuth client secrets, cloud credentials. Leaked secrets are one of the most common causes of real breaches, often through a public Git repository.
The basics
- Never commit secrets. Keep
.envin.gitignoreand commit a.env.examplewith placeholder values. - Load secrets from environment variables or a secrets manager at runtime (twelve-factor style), and validate them at startup so the app fails fast when one is missing.
- Use different secrets per environment; development keys should never work in production.
Secret managers
Cloud options include AWS Secrets Manager / Parameter Store, GCP Secret Manager and Azure Key Vault; HashiCorp Vault and Doppler or Infisical are popular platform-neutral tools. They offer access control, audit logs, versioning and rotation. In Kubernetes, remember that plain Secrets are only base64-encoded; enable encryption at rest or use an external secrets operator.
Rotation and response
- Design for rotation: support two valid keys during a changeover.
- If a secret leaks, rotate it immediately; deleting the commit is not enough because it may already be cloned or scraped.
- Enable secret scanning in your repo host and in CI.
Least privilege
Give each service its own credentials with only the permissions it needs, so a leak has a small blast radius.
You are ready to move on when your app reads every secret from the environment or a manager, fails clearly on a missing value, and you have practiced rotating a database password without downtime.
Key concepts
- Environment variables and .env files
- Startup config validation
- Cloud secret managers
- HashiCorp Vault
- Secret rotation
- Secret scanning
- Least-privilege credentials
Mini project
Move all secrets in a project into environment variables validated with a schema at startup, enable secret scanning on the repo, and write a rotation procedure for the database password.
Check yourself
You accidentally pushed an API key to a public repo and force-pushed to remove it. Is the problem solved? What should you do?
Free resources
- OWASP Secrets Management Cheat Sheet (OWASP)
- HashiCorp Vault documentation (HashiCorp)
- The Twelve-Factor App: Config (12factor.net)