Sub topic 14
Keeping secrets out of your configuration files
This chapter assumes you know how property precedence works, how environment variables map to property names through relaxed binding, and how spring.config.import loads extra files.
Why secrets need separate handling
Anything in application.yaml is packaged into the jar and committed to version control. A database password in that file stays in the git history even after you delete it. The rule is simple: the repository holds the name of a secret, and the environment supplies its value.
Reference a secret with a placeholder
Spring resolves ${...} placeholders in property values against all property sources. The file declares where the value comes from, and the value itself comes from outside.
spring:
datasource:
url: jdbc:postgresql://localhost:5432/app
username: app
password: ${DB_PASSWORD}Set the variable when you start the app:
DB_PASSWORD=s3cret java -jar app.jarIf DB_PASSWORD is missing and has no default, startup fails with a "Could not resolve placeholder" error. That failure is useful, because a misconfigured deployment stops immediately. Avoid ${DB_PASSWORD:}, which silently falls back to an empty string.
Mounted secret files with configtree
Container platforms such as Kubernetes and Docker often mount each secret as a file. The configtree: import turns each file in a directory into a property. The file name is the key and the file content is the value.
spring:
config:
import: "optional:configtree:/run/secrets/"
datasource:
password: ${db.password}If /run/secrets/db.password contains the password, db.password becomes a property. Mounted files avoid the exposure that environment variables have. Variables can show up in crash dumps, in child processes, and in diagnostics tools.
Local overrides that are never committed
For development, import an optional file and add it to .gitignore:
spring:
config:
import: "optional:file:./local-overrides.yaml"The optional: prefix keeps the app starting when the file is absent, as on a CI server. Imported values take precedence over the file that imports them.
Common mistakes
- Passing secrets as command-line arguments. Arguments such as
--spring.datasource.password=...are visible in the process list (ps) and in shell history. - Committing "temporary" secrets. Rotate any secret that has reached a repository. Removing it in a later commit does not help.
- Logging configuration objects. A record like
MailPropertiesprints every field in itstoString(). Keep secret fields out of such records, or overridetoString().
For production, a dedicated secret manager such as Vault, AWS Secrets Manager, or a Kubernetes Secret is the safer source. Spring Cloud integrations can import from some of these through spring.config.import. A manager adds rotation, access control, and an audit trail that plain environment variables lack.
Exercise
Your application.yaml contains password: ${DB_PASSWORD}. The app is deployed without setting DB_PASSWORD. What happens at startup, and why is that behavior preferable to using ${DB_PASSWORD:}?
Show answer
Startup fails because Spring cannot resolve the placeholder, so the misconfiguration is caught immediately. With ${DB_PASSWORD:} the value silently becomes an empty string, and the app would start and then fail later when it tries to connect, or connect with no password.