Secrets Management Best Practices: Vault, Cloud Secret Managers and Rotation
Stop leaking credentials: where secrets should live, dynamic secrets with HashiCorp Vault, cloud secret managers, rotation, secret scanning and CI/CD handlin...
Published December 6, 2026
API keys in source code, database passwords in .env files copied between laptops, cloud keys pasted into CI settings and never rotated: secrets sprawl is behind a large share of breaches. Secrets management means storing credentials in a dedicated system, delivering them to workloads securely at runtime, rotating them, and detecting leaks quickly.
The concept
A secrets manager centralises storage with encryption, access policies, audit logs and versioning. Options include HashiCorp Vault (or its open-source fork OpenBao), AWS Secrets Manager, Google Secret Manager, Azure Key Vault and Kubernetes integrations like External Secrets Operator. Applications authenticate with their workload identity (IAM role, Kubernetes service account, OIDC) rather than a bootstrap password.
The strongest pattern is dynamic secrets: instead of a long-lived shared database password, the secrets manager creates a unique credential per application instance with a short lease and revokes it automatically. Where secrets must be static, rotate them on a schedule and on any suspected exposure. Pair this with secret scanning in repositories and CI to catch leaks before (or right after) they happen.
Dynamic secrets
Where secrets should (not) live
| Where secrets live | Risk | Better |
|---|---|---|
| Source code | Leaks via git history forever | Never; scan and block |
| .env files on laptops | Copied, shared, stale | Fetched from secrets manager per environment |
| CI variables (long-lived cloud keys) | Exfiltration from pipelines | OIDC federation, short-lived credentials |
| Kubernetes Secret YAML in git | base64 is not encryption | External Secrets / SOPS / Sealed Secrets |
| Secrets manager with workload identity | Low | Target state |
Getting there
- Inventory secrets and their owners
- Move them into a secrets manager
- Authenticate workloads via identity, not passwords
- Enable rotation (or dynamic secrets)
- Turn on secret scanning with push protection
Practical examples
Read a secret at runtime (AWS)
The app's IAM role grants access; no key in code.
import boto3, json
sm = boto3.client("secretsmanager", region_name="ap-south-1")
creds = json.loads(sm.get_secret_value(SecretId="prod/orders/db")["SecretString"])Vault dynamic database role
Credentials are generated on demand with a TTL.
vault secrets enable database
vault write database/config/orders plugin_name=postgresql-database-plugin \
connection_url="postgresql://{{username}}:{{password}}@db:5432/orders" \
allowed_roles="app" username="vault" password="..."
vault write database/roles/app db_name=orders default_ttl=1h max_ttl=24h \
creation_statements="CREATE ROLE \"{{name}}\" WITH LOGIN PASSWORD '{{password}}' VALID UNTIL '{{expiration}}'; GRANT app_rw TO \"{{name}}\";"
vault read database/creds/appScan for committed secrets
Run locally and in CI; enable push protection on your git host.
gitleaks detect --source . --redact
trufflehog git file://. --only-verifiedCommon mistakes
- Committing secrets and "fixing" it by deleting the file (it remains in history).
- Sharing one credential across many services and environments.
- Never rotating secrets because rotation would break things.
- Printing secrets in CI logs or application logs.
Key takeaways
- Store secrets in a dedicated manager with policies and audit logs.
- Authenticate workloads by identity; prefer dynamic, short-lived secrets.
- Rotate regularly and immediately after exposure.
- Scan repositories and block secret pushes.
FAQ
What should I do if a secret is committed?
Revoke and rotate it immediately, then clean history if needed; assume it was copied the moment it was pushed.
Is a .env file always bad?
For local development with non-production values it is fine; production secrets should come from a secrets manager.
Vault or a cloud secret manager?
Cloud managers are simpler within one cloud; Vault/OpenBao suits multi-cloud, dynamic secrets across many systems and advanced policies.