r/platformengineering • • 7d ago

how do you rotate api keys and secrets without anything breaking

every time a key needs to change it feels risky. you update it in one place and some service still has the old one cached and starts failing. do you support two valid keys at the same time for a while or just do it fast and watch the logs. also curious if anyone actually rotates on a schedule or only when something leaks

9 Upvotes

10 comments sorted by

5

u/cyb3rstrik3 6d ago

Green-Blue deployment strategy spin up an identical copy of your production stack with the new keys, then swap the load balancers to the new stack, and you can dispose of the old one.

15

u/Starks1293 7d ago

two valid keys at once, always. doing it fast and watching logs is how u find the one cron job that runs monthly lol

the order that doesnt break things

  • create the new key while the old one still works
  • roll it out to every consumer, and make consumers pick it up without a redeploy if u can (re read the secret on a timer, or on an auth failure fetch again and retry once). that kills the cached old key problem
  • watch usage of the OLD key drop to zero. aws shows last used per access key, most providers have something similar, or log the key id on ur own apis
  • only then revoke the old one, and keep a way to re enable it for a day

this is exactly how secrets manager rotation works for databases, it alternates between two users so one is always valid

on schedule vs only on leak, rotate on a schedule mostly so the process stays exercised. if u only rotate after a leak thats the first time u find out it takes 2 days and breaks 4 services. better still is not having long lived keys at all where u can, oidc for ci, workload identity or irsa for pods, dynamic db creds from vault. whats left gets rotated every 90 days or so

1

u/NoDevelopment206 7d ago

this is really helpful, thanks. the part about watching the old key usage drop to zero before revoking makes sense. never thought about checking last used on the aws side. will try two keys at once first

1

u/Starks1293 6d ago

easy :)

1

u/aModernSage 6d ago

Perfect answer! I'll add to this that practice reduces risks over time because it can point out the holes you need to align and fill.

1

u/Jan_Stepan 2d ago

How do you decide how long to wait before revoking the old key? Zero usage for a day could still miss a job that only runs at the end of the month.

2

u/Starks1293 2d ago

exactly the trap. the wait has to be one full cycle of the slowest thing that uses the key, plus a margin, so for a month end job thats a month and a bit, not a day

what makes the long wait cheap is splitting revoke into two steps. disable first, delete later. on aws an iam access key can be set inactive and switched back on in a second, so u deactivate it once usage looks like zero, watch for auth errors through the next month end, and only delete after that. if the monthly report breaks u flip it back on, fix the job, and try again

and ask before u trust the zero. go through the scheduled stuff that might hold it, crons, month and quarter end reports, backup and dr runbooks, anything a human runs once a year. last used dates only tell u about the past, a job that runs on the 1st hasnt left a trace yet

2

u/nilarrs 5d ago

In kubernetes secrets are mounted as environment vars. Atleast most commonly. You update the secret in kubernetes then restart 1 pod at a time. Envvars only update on pod restart. You can then cycle through high availability one at the time

1

u/throwaway-research1 2d ago

Yep thats also how we do it

1

u/Flashy-Bus1663 21h ago

If you can help it consider moving to like jwt with with like workload identity it even works with 3rd parties assuming oauth endpoints and jwks are public