Sub topic 12
Rotating and Revoking Secrets
This chapter assumes you can read a GitHub Actions workflow, know what an API token or cloud credential is, and have stored a secret in your CI platform's secret store. It covers how to replace secrets on a schedule (rotation) and how to kill them on demand (revocation).
Why rotation matters
A secret that never changes is valid for as long as someone holds a copy. Copies end up in old logs, laptops, forks, backups, and the memory of former employees. Rotation limits how long any one copy is useful. Revocation ends a copy's usefulness immediately, for example after you suspect a leak.
The two actions differ in trigger and urgency:
| Action | Trigger | Urgency | Goal |
|---|---|---|---|
| Rotation | Schedule or policy | Planned | Shorten the useful life of any copy |
| Revocation | Suspected or confirmed exposure | Immediate | Make a specific credential stop working now |
The rotation lifecycle
Rotating a secret without breaking deployments requires an overlap period, when both the old and new values are accepted. Without it, any job that starts between the provider change and the CI store update fails.
The order matters. Revoke the old value only after a pipeline run has succeeded with the new one. If the check fails, the old secret is still your fallback.
Automating rotation
Manual rotation is rarely done on time. Prefer these approaches, roughly in order of how much they remove people from the process:
- Eliminate the stored secret. With OIDC federation, the pipeline requests a short-lived credential on each run. There is nothing long-lived to rotate. This is the best option wherever the provider supports it.
- Let a secret manager rotate it. External managers can generate new values on a schedule and update the target system. Jobs fetch the current value at run time, so CI needs no change.
- Script rotation yourself. A scheduled workflow calls the provider's API to create a new key, writes it to the CI store, and deletes the old one.
When you script rotation, the credential used to update the CI store is itself a secret. Give it only the permission to write secrets, and keep it separate from deployment credentials.
Choosing a rotation interval
There is no universal number. Base it on these factors:
- Blast radius. A production database credential justifies a shorter interval than a read-only token for a public API.
- Cost of rotation. If rotation is automated, daily or weekly is realistic. If it is manual, a long interval that actually gets followed beats a short one that is skipped.
- Compliance requirements. Some standards set maximum credential ages. Check yours.
- Staff changes. Rotate when someone with access leaves the team, regardless of schedule.
Revoking a secret
Revocation happens at the provider that issued the secret, not in your CI platform. Deleting a value from the CI secret store only stops your pipelines from using it. Anyone who copied the value can still use it until the provider invalidates it.
A minimal revocation of an AWS IAM access key looks like this:
aws iam update-access-key \
--user-name ci-deployer \
--access-key-id AKIAEXAMPLEKEYID \
--status InactiveDeactivating first, instead of deleting, lets you confirm nothing legitimate depended on the key. If nothing breaks after a short wait, delete it with aws iam delete-access-key. For an urgent leak, skip the wait and deactivate at once.
Common mistakes
- Revoking before updating the pipeline. This causes an outage. Follow the overlap sequence above, except in an emergency.
- Forgetting other consumers. The same token may be used by a teammate's script, a second repository, or a staging environment. Search for it before revoking.
- Rotating only the CI copy. If the secret is also stored in a secret manager, a Kubernetes secret, or a
.envfile, update every location. - No verification step. Always confirm with a real run, such as
aws sts get-caller-identityor a health check.
Exercise
A teammate says, "We found a leaked API token in a log, so I deleted the secret from our GitHub repository settings. We're done." What is wrong with this, and what should happen next?
Show answer
Deleting the secret from GitHub only stops workflows from using it. The token is still valid at the API provider, so anyone who copied it can use it. Next, revoke or deactivate the token at the provider, create a replacement, update the CI secret store, run a pipeline to verify, and check provider audit logs for use of the old token.
Exercise
Why should you deactivate the old credential only after a successful pipeline run with the new one?
Show answer
Until a run succeeds, you do not know the new credential works or that the CI store holds the right value. Keeping the old credential valid during that window gives you a fallback, so a mistake does not cause a deployment outage.