Secrets Management: Vault, AWS Secrets Manager, and Kubernetes Patterns
Fast recall
Quick revision
Core answer
@RefreshScope. Detection: scan repos with gitleaks / trufflehog; quarantine any leaked secret immediately (rotate + audit logs to assess blast radius). Secret hygiene checklist: in a vault, never logged, never in client-side code, least-privilege IAM, rotated, audited.Understanding of modern secret management options, leak handling, rotation
Treating K8s Secrets as encrypted (they're base64); committing .env; no rotation
Discuss Vault dynamic secrets, CSI driver vs External Secrets, secret detection in CI, IAM-based auth for DBs
Interview-ready guide
Interview answer
Never check secrets into Git. Use a dedicated secrets manager: HashiCorp Vault (cloud-agnostic), AWS Secrets Manager, GCP Secret Manager, or Azure Key Vault.
Why secrets management matters
- Secrets: DB creds, API keys, OAuth client secrets, TLS keys, encryption keys, webhook signing, SSH
- Single leak can compromise company
- Real incidents: GitHub Actions logs 2022, Codecov 2021, Uber 2016, Toyota 2022
Three sacred rules
- 1Never in Git — even private repos have too many eyes
- 2Never in images — layers sticky; baked-in secrets survive cleanup
- 3Never in logs/error messages — one printStackTrace() can exfiltrate
Job of secrets manager
- Central storage
- Access control (who reads what)
- Audit (who read what when)
- Rotation
- Versioning
- Leasing (expire automatically)
- Dynamic secrets (on-demand short-lived)
Storage options
HashiCorp Vault (cloud-agnostic gold standard)
- Static KV secrets
- Dynamic secrets: DB/AWS/SSH on-demand, short TTL
- Transit engine: encryption-as-a-service
- Lease-based renewal/revocation
- Auth: Kubernetes SA, AWS IAM, OIDC, LDAP
- Audit log every read/write
Spring Cloud Vault
- spring.cloud.vault.uri, authentication: KUBERNETES, kubernetes.role
- kv.backend: secret, default-context: my-app
- Reads from secret/my-app/* at startup, exposes as properties
- @RefreshScope for rotation
AWS Secrets Manager
- Native RDS/IAM integration
- Lambda-based rotation (RDS has built-in function)
- Versioning with AWSPREVIOUS/CURRENT/PENDING
- Per-secret KMS key
- Resource policies for cross-account
AWS SSM Parameter Store
- Cheaper, simpler; String + SecureString (KMS)
- No built-in rotation
GCP Secret Manager, Azure Key Vault
- Similar in respective clouds
Kubernetes Secrets
- Native but base64-encoded; stored in etcd
- Should be KMS-encrypted at rest
- Intermediary layer, not source of truth
Secrets Store CSI Driver + External Secrets Operator (modern K8s)
- SecretProviderClass: provider=vault, roleName, objects
- Pod mounts secrets as files in /mnt/secrets
- Never pass through K8s Secret resource
- Updated on rotation via CSI refresh
Rotation
Static rotation schedules
- API keys: 30-90 days
- Signing keys: 90-365 days; key rollover (new + old valid during rotation)
- Encryption keys: annual; keep old for decryption
Rotation pattern
- 1Generate new in vault
- 2Add alongside old (two valid)
- 3Deploy to read new (@RefreshScope re-inits beans)
- 4Wait for all instances
- 5Revoke old
- 6Audit no traffic on old
Dynamic secrets (best practice)
- Vault generates DB user on demand: role=my-app → v-my-app-abc123, TTL=1h
- App requests at startup; auto-renews
- Pod dies = creds expire naturally
- Breach = rotate = revoke all issued
IAM-based DB auth (no password at all)
- AWS RDS IAM auth: DB accepts IAM tokens (TTL 15 min); no stored password
- GCP Cloud SQL IAM, Azure AD SQL
- Spring: generateAuthToken before each connection
- Pros: nothing to manage; Cons: AWS-specific; token refresh complexity in pool
mTLS for service-to-service
- Service mesh (Istio, Linkerd) issues/rotates short-lived certs
- No long-lived secrets for internal comms
Secret scoping
- Per-env (dev/staging/prod)
- Per-app IAM roles (least privilege)
- Prod requires elevated approval
- Break-glass procedures logged/reviewed
Leak detection and response
Prevention in CI
- gitleaks-action
- trufflehog filesystem --only-verified
- pre-commit hooks
- GitHub push protection (enterprise)
If a secret leaks
- 1Rotate immediately (assume compromised)
- 2Revoke old in vault
- 3Audit blast radius: scope, resources, accesses (CloudTrail/Vault audit/DB audit)
- 4Check unauthorized: anomalous queries, exfiltration, new users
- 5Rewrite git history (git filter-repo, BFG) - contentious; the secret is copied; rotation + revocation is what matters
- 6IR: timeline, notify per regulations
- 7Post-mortem: how/prevention
Build-time vs runtime
- Build-time (registry creds, signing): short-lived, CI-issued
- Runtime (DB, API): pulled at container startup from vault
- Never bake runtime into image (layer cache + registry exposure)
Logging and redaction
- Override DTO toString() to redact
- Lombok @ToString.Exclude on sensitive fields
Environment-specific
- Local dev: personal vault tokens, short-lived
- CI: OIDC-issued short tokens; no stored GitHub Secrets beyond boot
- Prod: IAM roles / SA-based; no static creds in pipeline
Choice of secret store | Store | Strengths | When | | Vault | Multi-cloud, dynamic, transit | Complex, multi-cloud | | AWS SM | Native RDS integration | AWS-only | | SSM PS | Cheap, simple | Config + some secrets, small | | GCP/Azure | Native integration | Single-cloud | | K8s Secrets + KMS | K8s-native | Small cluster; cache layer |
Common anti-patterns
- 1Secrets in application.yml committed to Git
- 2Env vars in /proc/pid/environ visible to host users
- 3K8s Secrets without KMS encryption at rest
- 4Single secret for many apps (big blast radius)
- 5No expiry/TTL
- 6Rotation requiring restart
- 7CI secrets in logs (masking imperfect; use OIDC)
- 8Hardcoded secret names (rotation = rebuild)
- 9No audit trail
- 10Dev secrets = prod secrets
Ephemeral vs persistent
- Persistent (90-day password): easy, high blast radius
- Ephemeral (1h Vault token): complex, low blast radius
- Industry moving toward ephemeral (Vault, IAM auth, mTLS)
Human factor
- Devs paste secrets in Slack when rushing
- Make right thing easy: CLI/UI fetching per-context
- Blameless leak response; focus on systemic fix
Cost
- Vault cluster: infra + operational
- AWS SM: $0.40/secret/month + API
- SSM PS: free standard, $ advanced
- Per-secret cost matters at thousands
Compliance
- PCI-DSS: card-data secrets need strict controls, rotation, audit
- SOC 2: secrets management controls scrutinized
- GDPR: encryption of personal data; key management
- HIPAA: access audited; separation of duties
Strategic take
- Boring infrastructure work that matters a lot
- Single leaked AWS key can end a company
- 1Pick one store, invest, make default
- 2Automate: CI, CD, rotation
- 3Detect leaks: scanning + push protection + monitoring
- 4Practice IR: simulate leak, time rotation
- 5Build habits: code reviews check secrets; .gitignore local
- 6Move to dynamic/IAM auth — eliminates whole classes of risk
Interview standouts
- Know the options
- Rotation and blast-radius math
- Thoughtful dev experience
- Response plan for inevitable leak
Bottom line
- Never in Git/images/logs
- Central vault with audit + rotation
- Dynamic secrets > static; IAM auth > password
- Detect, respond, learn from leaks
- Automation + culture = durable secret hygiene
Secrets Management: Vault, AWS Secrets Manager, and Kubernetes Patterns
Direct Answer
Never check secrets into Git. Use a dedicated secrets manager: HashiCorp Vault (on-prem / cloud-agnostic), AWS Secrets Manager or Parameter Store, GCP Secret Manager, Azure Key Vault. For Kubernetes: mount secrets via CSI driver (Secrets Store CSI, External Secrets Operator) so they appear as files in the pod without being stored as K8s Secrets (which are only base64-encoded). Inject into Spring Boot via env vars, mounted files, or Spring Cloud Config Vault integration. Rotate regularly: DB credentials via Vault dynamic secrets or RDS-IAM; static app secrets on a schedule (30–90 days). Automate: on rotation, Vault notifies app via webhook / SIGHUP / Spring @RefreshScope. Detection: scan repos with gitleaks / trufflehog; quarantine any leaked secret immediately (rotate + audit logs to assess blast radius). Secret hygiene checklist: in a vault, never logged, never in client-side code, least-privilege IAM, rotated, audited.
Common Mistakes & Pitfalls
Treating K8s Secrets as encrypted (they're base64); committing .env; no rotation
Check your understanding
Answer these without looking back at the article.
- What's wrong with using Kubernetes Secrets as the primary secret store?
- How do dynamic secrets (Vault) reduce blast radius compared to rotated static secrets?
- Walk through the first 30 minutes after discovering an AWS secret leaked in a public repo.
- When would you choose IAM-based DB authentication over stored passwords?
Related interview prep categories
1Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.