Sub topic 4
Handling secrets safely
This chapter assumes you can build a Spring Boot JAR, know how profiles and environment variables feed application.properties, and have used java -jar from a shell. Here you will learn where secrets must not live, where they can live, and how to check that the app is not leaking them.
What counts as a secret
A secret is any value that grants access if someone else reads it. Examples are database passwords, API keys, signing keys, and OAuth client secrets. A database host name is configuration. The password for that database is a secret.
The executable JAR is copied to build servers, artifact repositories, and container registries. Anything inside it is readable by everyone who can read those places. So the rule is simple: the artifact contains no secrets.
Where secrets must not live
- In
application.propertiesorapplication.ymlinside the JAR. - In the Git repository, including "temporary" commits that are later deleted. Git history keeps them.
- In a Dockerfile
ENVline or build argument. Both are visible in the image metadata. - On the command line, such as
--spring.datasource.password=.... Other users on the machine can see it withps, and it may end up in shell history.
Passing secrets at run time
The chapter on configuration showed that Spring Boot reads placeholders such as ${DB_PASSWORD} from the environment. The packaged file holds only the name of the variable:
spring.datasource.url=${DB_URL}
spring.datasource.password=${DB_PASSWORD}If DB_PASSWORD is missing, the application fails at startup with an unresolved placeholder error. That is the behavior you want, because the failure shows up at deploy time and not at the first query.
Each platform has its own place to supply the value:
| Platform | Where the secret lives | How the app receives it |
|---|---|---|
| Linux server with systemd | A root-owned file such as `/etc/myapp/env` | `EnvironmentFile=` in the unit |
| Docker | `--env-file` or an orchestrator secret | Environment variable or mounted file |
| Kubernetes | A `Secret` object | Environment variable or mounted volume |
| Cloud | A secrets manager | Injected by the platform, or read by the app at start |
On a server, restrict the file so only the service account can read it:
sudo install -m 600 -o myapp -g myapp /dev/null /etc/myapp/env
sudo nano /etc/myapp/envEnvironment variables or mounted files
Environment variables are easy to use, but child processes inherit them and crash dumps or debug pages can expose them. Files are tighter, since you control their permissions. Spring Boot can import a directory of files, where each file name is a property name and its content is the value:
spring.config.import=optional:configtree:/run/secrets/With this setting, a file /run/secrets/db.password becomes the property db.password. Kubernetes and Docker both mount secrets in this layout. Reference the property in the usual way, for example spring.datasource.password=${db.password}.
Keeping secrets out of logs and endpoints
Even a well-placed secret can leak through the application itself.
- Do not log configuration objects or full connection strings.
- Spring Boot's Actuator
envandconfigpropsendpoints mask values for keys that look sensitive, but you should not rely on that. Expose only what you need, as the earlier example does withhealth,info. - Never enable the
envendpoint on a public port.
Common mistakes
- Committing a
.envfile. Add it to.gitignorebefore you create it. - Using one password for every environment. Use separate credentials for dev, test, and production so a leak in one does not open the others.
- Baking a production profile file with credentials into the image because "it is a private registry".
- Never rotating secrets. Design the app so a restart picks up a new value without a rebuild.
Exercise
A teammate puts the production database password in application-prod.properties and builds the JAR. Why is this a problem, and what should replace it?
Show answer
The password is now inside the artifact, so anyone with access to the repository, build server, or registry can read it, and it stays in Git history. Replace the value with a placeholder such as ${DB_PASSWORD}, supply the real value at run time through an environment variable or a mounted secret file, and rotate the exposed password.
Exercise
What does spring.config.import=optional:configtree:/run/secrets/ do, and what is one advantage over environment variables?
Show answer
It loads each file in that directory as a property, with the file name as the key and the file content as the value. An advantage is that file permissions control who can read the value, and the secret is not inherited by child processes the way environment variables are.