Video in progress

Where is the right place to keep secrets like passwords and API keys in microservices?

Not in Git and not in application.yml. They live in a dedicated secret store (Vault, a cloud secret manager, a Kubernetes Secret) and are given to the application at startup as an environment variable or a file.

📖 Answer

The most common mistake:

spring:
  datasource:
    password: Prod_Pa$$2024   # stays in the git history forever

Even if you delete the commit later, it stays in repository copies and in the git history. In that case the only correct way out is to rotate the secret.

The right options:

  • HashiCorp Vault: central storage, access permissions, auditing, even temporary (dynamic) database passwords;
  • AWS Secrets Manager, GCP Secret Manager, Azure Key Vault: if you're in the cloud;
  • Kubernetes Secret: simple, but by default it's only base64; you have to set up etcd encryption and RBAC.

In the application only a reference remains:

spring:
  datasource:
    password: ${DB_PASSWORD}

Extra habits: each service sees only the secrets it needs, secrets don't end up in logs (don't expose the Actuator /env endpoint to the outside), and they're rotated from time to time.