GCP Secret Manager Rotation Strategies
What you'll learn: How to automate secret rotation in GCP using Cloud Functions, Pub/Sub triggers, and best practices to keep credentials secure without manual intervention.
Why Automate Secret Rotation?
Think of secret rotation like changing the locks on your house periodically—but imagine doing it manually for thousands of doors. Automation ensures credentials expire and refresh regularly, limiting the damage window if a secret is compromised.
Implementing Automatic Rotation
GCP Secret Manager doesn't have built-in rotation like AWS, so you build it yourself using serverless components:
The Rotation Architecture
- Cloud Scheduler triggers a Pub/Sub topic on a schedule (e.g., every 90 days)
- Pub/Sub publishes a message to initiate rotation
- Cloud Function responds to the message and executes rotation logic:
- Generate a new credential (database password, API key)
- Test the new credential
- Store it as a new version in Secret Manager
- Update dependent services to use the new version
- Optionally delete or disable old versions after grace period
Best Practices
Rotation windows: Implement a grace period where both old and new secrets work simultaneously, allowing services to transition smoothly.
Testing before commit: Always validate new credentials work before storing them—a broken rotation can cause outages.
Audit logging: Enable Cloud Audit Logs to track all rotation events and access patterns.
Stateless rotation functions: Keep rotation logic idempotent so retries don't cause issues.
Service account permissions: Grant rotation functions only the minimum IAM permissions needed (secretmanager.versions.add, not full admin).
Key Takeaway: GCP Secret Manager rotation requires custom automation using Cloud Functions and Pub/Sub, but this gives you complete control over rotation logic, testing, and grace periods to ensure zero-downtime credential updates.