r/platformengineering • • 13d ago

how do you handle secrets for preview environments per pr

we want a preview env for every pr but the secrets part feels messy. sharing the staging secrets is easy but it feels risky since anyone can open a pr. curious if people make fake values per preview or give each one a scoped short lived secret.

4 Upvotes

4 comments sorted by

2

u/Torutofu_Raeva 13d ago

e the deploy job runs the workflow from main, otherwise a pr can just edit the yaml to print whatever secret it's handed

2

u/Tinasour 12d ago

I dont know how you are generating environments, but i started using terraforms write only attribute more and more, new database, a random ephemeral string from terraform, writeonly written to secret manager, ansible terraform action depending on write only version which reads from secret manager to create the user, ecs mounts secret manager

And yes my workplace is small enough to not care for vault. Maybe vaılt has a better fit for this

1

u/LittleEnvironment970 6d ago

both tbh. fakes or sandbox keys for third party apis and for your own db a short lived cred per preview with a ttl, so it's useless once the env is gone, an infisical lease or one hush security issues to the preview's pods. github already keeps secrets from fork prs so dont undo that with pull_request_target

0

u/Starks1293 13d ago

id treat previews as their own environment tier and never hand them staging secrets. a preview runs code nobody has reviewed yet, so assume it leaks whatever it gets

what tends to work

  • generate per preview stuff at deploy time. the pipeline creates a db (or a branch of one, neon and planetscale do this nicely) with its own user and password just for that pr, and drops it when the pr closes
  • third party apis get test mode or sandbox keys only (stripe test keys etc). one shared preview key with tight limits is fine since its worth nothing
  • cloud access through oidc instead of stored keys. github actions → an aws role whose trust policy only matches pull_request runs from ur repo, with permissions on preview-* resources and nothing else. tokens last an hour and theres nothing to steal
  • if its k8s, external secrets operator with a preview/ path in vault or secrets manager, and each preview namespace can only read that path. vaults database secrets engine gives u creds with a ttl if u want them to expire on their own
  • prs from forks dont get secrets on pull_request in github actions anyway. dont work around that with pull_request_target plus checking out the pr code, thats the classic way people get their secrets stolen. gate fork previews behind a label a maintainer adds

and wire the teardown to pr close so nothing lingers, revoke the db user and delete the namespace