Skip to content

Rotate API Secrets Without Downtime

Every security checklist says to rotate secrets regularly, and after anyone who knew them leaves. Most teams don't, because rotation has a reputation: change the secret, and every server that still has the old one fails until it is redeployed. So the secret from three years ago is still in production, in two CI systems and a former contractor's laptop.

Rotation only becomes routine when it can't cause an outage. Here is how that works.

The problem with a single secret

If an account has exactly one secret, rotating it is a race. The moment the new one is issued, the old one stops working, and every client is broken until it has the new one. With ten servers, a cron job and a CI pipeline, that window is minutes at best.

The fix: an overlap window

Let the old and the new secret both work for a while. Clients move over one by one, at their own pace, and nothing breaks.

ObjectsGrid service accounts, the accounts your servers and scripts use, work this way:

curl -X POST https://apis.objectsgrid.com/serviceaccounts/<id>/secret \
  -H 'X-ObjectsGrid-AccessPoint: EU' \
  -H "Authorization: Bearer $ADMIN_TOKEN"

The answer holds the new secret. The replaced secret keeps working for 24 hours, until previousSecretExpiresAt. Rotating is never forced on you: you choose when.

A rotation in four steps

  1. Rotate, and store the new secret in your secret manager.
  2. Roll it out: redeploy or restart the servers and jobs that use the account. Each picks up the new secret whenever it next starts; until then the old one still works.
  3. Check that every server and job using the account has restarted with the new secret, well within the 24 hours. One you missed fails to sign in once the window ends, so keep an eye on your services' logs then.
  4. Done. After 24 hours the old secret stops working on its own.

Never more than two

A second rotation inside the window ends the previous secret at once, so at most two secrets work at any time. That matters when a secret has leaked: rotate twice, and the leaked one is dead immediately, rather than in 24 hours. Use it only for that, since clients still on the oldest secret fail at once too.

One account per job

Rotation is only as cheap as its blast radius. If one service account is shared by the import job, the storefront and the reporting script, rotating it touches all three. Give each job or server its own service account, with only the tags it needs:

{"type": "secret", "description": "Nightly product import", "tags": [{"name": "role", "val": "importer"}]}

Then you can rotate, or disable, one job's access without touching the others, and the access logs tell you which job did what.

Keep secrets where they belong

  • Never in a browser or mobile app. Anything shipped to a device can be read. Visitors who aren't signed in use a public service account, which has no secret, and your access policy gives it only what every visitor may see.
  • In a secret manager, not in code, .env files in a repository, or CI logs.
  • Exchanged for short-lived tokens. A service account trades its id and secret for an access token that lasts an hour, and sends the token, not the secret, with each request. The secret crosses the network once an hour, not with every call. Token requests are free.
  • Changed only by a rotation. Lists of service accounts never include secrets, and a PUT keeps the secret whatever it sends.

Make it a habit

Put a rotation in the calendar, every 90 days for example, and when someone with access leaves. With an overlap window, one account per job and the access logs to check, it is a ten-minute task. See Authentication for how service accounts sign in.