Secrets Management
Live vaults, not hardcoded hope
Secrets — API keys, DB passwords, tokens — need a live store with rotation and audit, not environment files. Secrets Manager, Key Vault, and Secret Manager share the pattern: versioned values, rotation schedules, granular access, and client caching with graceful re-read.
What you will learn
- Move secrets out of code/config into a managed store
- Configure rotation + audit + minimum access
- Integrate at runtime (env, mount, SDK) without downtime
A secrets manager stores name→versioned value, rotates on schedule, grants access via identity, and records every read. Runtime clients fetch or cache; rotation causes a version bump that downstream systems re-read safely.
What is this?
Products: AWS Secrets Manager, Azure Key Vault (secrets), GCP Secret Manager, HashiCorp Vault (flexible + dynamic leases), Kubernetes + External Secrets/SealedSecrets. Features: versioning, rotation (lambda-trigger auto-rotation; Key Vault near-expiry notifications), access control (IAM/KV RBAC/SM policies), secret rotation handlers (DB creds + app creds), replication (cross-region), audit (get/set/describe), recovery (soft delete/deletion protection), caching (SDKs cache with TTL; files mounted at the kubelet). Anti-secrets: env vars in actions/config files, base64 'duh' secrets, image-layer history, notebook leakage. Dynamic secrets: Vault short-lived leases (TTL-scoped DB creds).
Why do you need it?
- Hardcoded secrets rotate never; a store makes rotation a function call.
- Audit trails point to exactly who/what read a secret — irreplaceable in incidents.
- Dynamic leases shrink the lifetime window of any leaked credential.
- K8s-native integration keeps secrets out of images and git history.
Mental model
A bank's vault with scheduled re-keying and a guard logbook
Secrets manager
The vault rotates its combination on a date (rotation), everyone with a key signs the logbook (audit), and nobody walks around with the master combo pinned to their shirt (no hardcoded secrets).
How it works
- You create a secret with versions (current + previous).
- Identity-checked callers read the secret (SDK/CLI/mount).
- Rotation handler updates a new version atomically; applications observe the version change.
- Pipeline/CI fetches secrets at build via integration (actions/vault role), not repo files.
- K8s: External Secrets syncs store values into a Secret object on schedule; pods mount files/env.
Secret lifecycle.
Examples
Runtime read with version tolerance (SDK-ish)
# app on boot: fetch once, cache for TTL, re-fetch on version changeconst { secretValue } = await secrets.getSecretValue({ SecretId: 'prod/db' })let cached = secretValue// rotation-aware:if (versionChanged(cached)) cached = await secrets.getSecretValue(...)# or kube: mount -> file watcher re-reads on rotationCaching with a version check turns rotation into a seamless background event for your app.
In production
- DB credentials rotated weekly with a lambda; app pools reconnect on version bump.
- CI fetch from store with ephemeral audience instead of yaml 'db_password:'.
- K8s workloads get Secrets via External Secrets sync, rotated without pod restarts.
Best practices
- Treat any secret found in a repo/log as compromised; rotate it now.
- Scope secret permissions to the exact caller/service; never 'all secrets' access.
- Rotation must be tested: rotate in staging, watch re-reads + reconnects.
- Enable deletion protection/soft delete and cross-region replication where compliance needs it.
- Mount secrets as files or envs via the runtime, never as image layers.
Common mistakes
- S3 bucket with a JSON of secrets because 'it works'.
- Rotation handler that breaks app pools (no re-read strategy) → first rotation = outage.
- One omnipotent secret owner token used everywhere (no per-secret audit).
- Secrets Manager without deletion protection → a wrong `delete-secret` is an incident.
Debugging tips
- 'No permission' reading secret → check the identity's IAM/KeyVault policy for that secret's name, not the account.
- Rotation changed but app still uses old value → caching TTL; implement version re-read.
- Secret shows previous version → rotation handler timing/failure; inspect function logs.
- Cross-region replication lag → replication status field per secret.
Interview checkpoint
Hands-on lab
- Create + version a secret locally, with rotation simulation
- Read current value with a version-aware caching app
- Simulate rotation and prove a clean re-read
Rotate a Secret Without the App Blinking
40 min · intermediate
Quick quiz
The first action after finding a secret in git is:
Deletion protection exists to: