# Managing Secrets and Configuration

> Keep API keys, database passwords and signing keys out of code and rotate them safely. Free notes, key concepts, resources and a mini project.

Canonical: https://www.internhack.in/roadmaps/tracks/backend/secrets-management
Last updated: 2026-11-28

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](https://www.internhack.in/roadmaps/tracks/backend/git-version-control), [Where Backends Run: Hosting Options](https://www.internhack.in/roadmaps/tracks/backend/hosting-basics)

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 `.env` in `.gitignore` and commit a `.env.example` with 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](https://cheatsheetseries.owasp.org/cheatsheets/Secrets_Management_Cheat_Sheet.html) (OWASP)
- [HashiCorp Vault documentation](https://developer.hashicorp.com/vault/docs) (HashiCorp)
- [The Twelve-Factor App: Config](https://12factor.net/config) (12factor.net)

Previous: [Common Vulnerabilities: OWASP Top 10](https://www.internhack.in/roadmaps/tracks/backend/owasp-top-ten) · Next: [Unit Testing Fundamentals](https://www.internhack.in/roadmaps/tracks/backend/unit-testing)

## More in Defending Your Backend

- [Storing Passwords and Hashing](https://www.internhack.in/roadmaps/tracks/backend/password-hashing)
- [HTTPS and TLS](https://www.internhack.in/roadmaps/tracks/backend/tls-https)
- [CORS, CSP and Security Headers](https://www.internhack.in/roadmaps/tracks/backend/browser-facing-security)
- [Common Vulnerabilities: OWASP Top 10](https://www.internhack.in/roadmaps/tracks/backend/owasp-top-ten)
- [Managing Secrets and Configuration](https://www.internhack.in/roadmaps/tracks/backend/secrets-management)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Backend Developer Roadmap](https://www.internhack.in/roadmaps/tracks/backend)
