Prompt
How do I build a system for automatic secret rotation?
Latest observation
Building an automatic secret rotation system is mostly about combining inventory, secure storage, orchestration, validation, and rollback. The right design depends on what kinds of secrets you mean: database passwords, API keys, cloud access keys, TLS certs, signing keys, etc. But the core pattern is similar.
1) Define the rotation model
First decide:
- What secrets rotate: DB creds, service-to-service tokens, OAuth client secrets, SSH keys, certs, etc.
- Rotation frequency: every 30/60/90 days, or event-based
- Who consumes them: apps, jobs, humans, third-party vendors
- Downtime tolerance: can you do dual-validity rotation, or must it be instant?
- Revocation requirements: must old secrets stop working immediately?
A good system usually supports:
- scheduled rotation
- manual emergency rotation
- on-demand rotation after compromise
- per-secret policies
2) Use a secret manager as the source of truth
Do not store secrets in code, CI logs, or config files.
Use a centralized secret manager such as:
- HashiCorp Vault
- AWS Secrets Manager
- Azure Key Vault
- Google Secret Manager
- CyberArk, Doppler, 1Password Secrets Automation, etc.
Your system should:
- generate/store new versions
- track metadata: owner, expiry, last rotated, rotation policy
- audit every access and rotation
- expose an API for automation
3) Build a rotation controller
You need a service or workflow engine that coordinates rotations. This can be:
- a scheduled job
- a workflow engine like Airflow, Argo Workflows, Temporal, Step Functions
- a controller/operator in Kubernetes
- a custom service with a queue and workers
The controller should:
- identify secrets due for rotation
- call the target system to create a new credential
- store the new secret
- deploy or distribute it to consumers
- validate consumers are using the new secret
- revoke the old secret
- record success/failure and metrics
4) Support “create new before revoke old”
The safest pattern is overlap rotation:
- Create a new secret
- Update consumers to accept/use the new secret
- Confirm traffic succeeds
- Revoke the old secret
This avoids outages if consumers lag behind.
Examples:
- Database users: create a new password for the same user, or create a second user and switch apps
- API keys: issue new key, deploy, then revoke old key
- Certificates: issue new cert, reload service, wait for expiry, remove old cert
- JWT signing keys: publish new public key, sign new tokens with new private key, keep old key until old tokens expire
If the target system supports multiple active credentials, use that. If not, rotation is riskier.
5) Standardize integrations for each secret type
Each secret type needs a plugin/adapter with three operations:
- generate() or create_new()
- apply() to consumers or target system
- revoke()
Example adapter interfaces:
rotate_db_password(db_instance, user)rotate_aws_access_key(iam_user)rotate_tls_certificate(service_name)rotate_slack_app_secret(app_id)
This keeps the controller generic and lets you plug in per-system logic.
6) Make distribution automatic
Secret rotation fails when apps don’t pick up the new value.
Common delivery methods:
- app pulls from secret manager at startup
- sidecar/agent fetches and refreshes secrets
- Kubernetes External Secrets / CSI driver
- environment variable injection during deploy
- config reload via SIGHUP or hot reload
- service mesh or identity-based auth to reduce static secrets entirely
Best practice: avoid long-lived static secrets where possible. Prefer short-lived tokens or workload identity.
7) Validate before revocation
Before removing the old secret, verify:
- new credential works
- all critical consumers have switched
- health checks pass
- error rates didn’t increase
- audit logs show old credential no longer used
You can do:
- synthetic transactions
- canary rollout
- staged deployment
- token usage monitoring
If validation fails, rollback by:
- keeping old secret active longer
- re-deploying consumers
- reissuing another secret if the new one leaked or was misconfigured
8) Handle rollback and failure states
Rotation workflows should be idempotent and resumable.
Track states like:
pendingnew_secret_createddistributedvalidatedrevoked_oldfailed
If the process dies midway, it should resume safely.
Design for:
- retries with backoff
- dead-lettering or manual intervention
- partial success
- versioned secret records
- emergency disable of auto-rotation
9) Add policy and governance
Secrets should be rotated based on policy:
- max age
- owner/team
- environment
- risk level
- compliance requirements
Also keep:
- audit trail of who/what rotated it
- approval workflow for sensitive secrets
- break-glass access for incidents
- notifications to owners before rotation
10) Secure the rotation system itself
This system becomes highly privileged, so protect it carefully.
Requirements:
- least privilege for rotation workers
- separate roles per environment
- no plaintext secrets in logs
- encryption in transit and at rest
- strong auth to secret manager APIs
- short-lived credentials for the rotator itself
- isolated network access
- tamper-evident audit logs
11) Prefer short-lived credentials when possible
In many cases, you can avoid rotation entirely by using:
- cloud IAM roles
- workload identity
- OIDC federation
- mTLS with dynamic certs
- ephemeral DB credentials from Vault
- signed, short-lived access tokens
This reduces operational burden and attack surface.
12) A practical reference architecture
A common setup looks like this:
- Secret Manager as source of truth
- Rotation Orchestrator on a schedule
- Secret-type adapters for each target system
- Deployment mechanism for consumers
- Monitoring/alerting for failures and drift
- Audit log for compliance
- Approval workflow for high-risk rotations
Flow:
- Scheduler finds secrets nearing expiry
- Orchestrator invokes adapter
- Adapter creates new secret and stores versioned copy
- Consumers pull or receive update
- Health checks confirm success
- Adapter revokes old secret
- System emits audit event and metrics
13) Start small
A good rollout strategy:
- Pick one secret type, like DB credentials
- Implement one adapter
- Add monitoring and manual override
- Pilot in non-prod
- Expand to prod with canary rotation
- Add more secret types and policy automation
14) Common pitfalls
- revoking old secret too early
- apps caching secrets too long
- secrets in logs or env dumps
- no ownership metadata
- no rollback plan
- rotation jobs requiring too many privileges
- treating all secret types the same
- forgetting third-party systems that can’t rotate automatically
If you want, I can also provide:
- a reference architecture diagram
- a Kubernetes-based implementation
- a Terraform + AWS Secrets Manager example
- a sample rotation workflow/pseudocode