Video in progress

As microservices multiply, how do you organize storing and rotating secrets (DB passwords, API keys)?

Secrets are kept not in code or images but in a dedicated secret store (Vault, a cloud secret manager), and a service gets only its own through its identity. Where possible, short-lived dynamic credentials, with rotation automatic, without a restart or with a rolling restart.

📖 Answer

The starting state is often like this: a password in application-prod.yml, or in CI variables, or a base64-encoded Kubernetes Secret in git. Base64 is not encryption.

The model I build:

A central store. HashiCorp Vault or AWS Secrets Manager / GCP Secret Manager. Access is audited, and permissions depend on the service identity: order-service reads only secret/order/*.

How the service authenticates. Through the Kubernetes ServiceAccount token (Vault's kubernetes auth or the cloud's workload identity). So there is no "a secret to get the secret" problem.

Delivery to the application. Options:

  • External Secrets Operator syncs from the store into a Kubernetes Secret, and the application reads it as an env var or a file, so the application knows nothing about Vault;
  • Spring Cloud Vault: the application reads directly and can refresh dynamic credentials.

Dynamic credentials. For the DB, Vault creates a separate user per pod, with a 24-hour lifetime for example. A leaked password doesn't live long and you can tell which pod it came from. But you need to get the connection pool's switch to the new credentials right. There are subtleties here, like recreating Hikari or making maxLifetime shorter than the lease.

Rotation. Manual rotation happens once a year and is frightening. With automatic rotation, two versions must be valid in parallel for a while (the old and new API key), so nothing breaks during a rolling restart.

Also: secrets must not end up in logs (toString(), lock down the actuator /env), and a scanner that searches git history for secrets (gitleaks) should run in CI.