Video in progress
How do you manage configuration and secrets (passwords, API keys) in a Spring application?
Secrets are neither in the code nor in git. They come from the environment: a Kubernetes Secret, Vault or a cloud secret manager, short-lived and self-rotating if possible. Ordinary configuration is versioned and split per environment.
📖 Answer
There are usually three layers: defaults in the code (application.yml, no secrets), environment settings (profile files or a ConfigMap) and secrets (env variables, a Secret mounted as files, or Vault).
Spring Boot can read the files directly:
spring:
config:
import: "optional:configtree:/etc/secrets/" # each file is one key
With Spring Cloud Vault, static secrets are plain key/value pairs. The more interesting part is dynamic secrets: Vault creates a temporary DB login for each instance and deletes it when it expires. Even if a password leaks, it doesn't live long.
In practice:
- secrets must not end up in logs.
/actuator/envmasks them, but check your own logs: dumping a whole config object withtoStringis common; - each service should see only the secrets it needs;
- decide in advance what happens when a secret changes: does the application restart, or read the new one by itself;
- treat a secret that has reached git history as leaked. Deleting the commit isn't enough, rotate it immediately;
- a scanner like
gitleaksin CI.
Sign up to mark studied questions and save your progress.