Secrets Management
Rules
- Never hardcode secrets — no API keys, passwords, or tokens in source code
- Never commit secrets — add
.envto.gitignore, use pre-commit hooks to scan - Use environment variables — read secrets from env at runtime, not from config files
- Rotate regularly — rotate keys every 90 days, immediately on suspected breach
Methods
| Method | Use Case | Example |
|---|---|---|
| Env vars | Development, simple deployments | DB_PASSWORD=xxx in .env (not committed) |
| Secret manager | Production | AWS Secrets Manager, HashiCorp Vault |
| CI/CD secrets | Pipeline jobs | GitHub Actions secrets, GitLab CI variables |
| Signed URLs | Short-lived access | S3 presigned URLs, JWT tokens |
Detection
- Use
git secretsortruffleHogas pre-commit hooks - Scan repos for accidental commits with
git log -p --all | grep -i "api_key\|password\|secret" - If a secret is committed: rotate it immediately, then use
git filter-repoto scrub