Quick revision
The short answer
There are three main options. Environment variables are simple, but on some setups they are visible in process listings, crash dumps and child processes. Mounted files read as a config tree work naturally with Kubernetes Secrets and keep the value out of the image. A secret manager such as Vault or a cloud secret service adds auditing, rotation and even short-lived credentials, at the cost of another critical dependency. Whatever we choose, secrets are never committed to application.yml or baked into images, and the Actuator env and configprops endpoints must keep values masked.
What to remember
In this answer, we compared the ways to supply a database password. Environment variables are simple but visible to process listings, crash dumps and child processes. Mounted files read as a config tree keep the value out of the image and the environment and work naturally with Kubernetes Secrets. A secret manager adds auditing, rotation and short-lived credentials, at the cost of running critical infrastructure. Secrets never go into application.yml or images, and the env and configprops endpoints must keep values masked.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
There are three main options. Environment variables are simple, but on some setups they are visible in process listings, crash dumps and child processes. Mounted files read as a config tree work naturally with Kubernetes Secrets and keep the value out of the image. A secret manager such as Vault or a cloud secret service adds auditing, rotation and even short-lived credentials, at the cost of another critical dependency. Whatever we choose, secrets are never committed to application.yml or baked into images, and the Actuator env and configprops endpoints must keep values masked.
Why this question is asked
Every service that talks to a database needs a password, and how that password reaches the process decides how easily it leaks, how quickly it can be rotated and what an auditor can prove about who used it. Interviewers ask this design question because each option is easy to set up and the differences only show in incidents: a password in a crash dump, a credential that cannot be rotated without downtime, an endpoint that printed every secret. In this answer, we will recap what we are protecting against, then go through environment variables, mounted files, secret managers, the things we must never do, and how to keep Actuator from becoming a leak.
1What We Are Protecting Against
A database password can leak in several ways: someone reads it from source control or an image registry, a diagnostic tool or log prints it, another process on the same host reads it, or an attacker who gets into one pod uses it. A good design reduces how many places the password exists, how many people and processes can read it, and how long a leaked password stays useful.
So we judge each option on four questions: where does the value live at rest, who can read it at runtime, can we rotate it without downtime, and can we audit its use? The table below summarises the answers before we look at each option.
| Option | Exposure | Rotation | Audit |
|---|---|---|---|
| Environment variable | process env, dumps, child processes | restart the pod | only the deploy history |
| Mounted file / config tree | file in the pod, readable by its user | file updates; app needs restart | Kubernetes access to Secret |
| Secret manager (Vault, cloud) | fetched at runtime, scoped by identity | built in, even per-pod credentials | every read logged |
In application.yml or image | everyone with repo or image access | rebuild; old copies remain | none |
2Environment Variables: Simple but Visible
Setting SPRING_DATASOURCE_PASSWORD from a Kubernetes Secret with secretKeyRef is the simplest option, and Spring Boot binds it to spring.datasource.password through relaxed binding. Nothing in the code changes and every platform supports it.
The weakness is visibility. The environment of a process is copied to every child process it starts, appears in some crash reports and diagnostic dumps, can be read from /proc by privileged users on the node, and is shown by kubectl describe or debug tools to anyone allowed to inspect the pod. It also cannot change without restarting the process. For low-risk internal services this may be acceptable; for anything sensitive, files are a better default.
3Mounted Files and Config Trees
Kubernetes can mount the same Secret as files instead, one file per key, and Spring Boot reads them with a config tree import. The value is never in the image, not in the process environment, and only readable by users who can read that file inside the pod.
Files also support rotation better: Kubernetes updates the mounted files after the Secret changes. The application reads them only at startup, so it still needs a rolling restart, but no pod has to be recreated with a new specification. One caution: a Kubernetes Secret is only base64-encoded, not encrypted, unless the cluster encrypts Secrets at rest and access to them is tightly controlled, so this option is as safe as the cluster's Secret handling.
4Secret Managers: Audit, Rotation and Short-Lived Credentials
A secret manager, such as HashiCorp Vault or a cloud provider's secret service, stores secrets centrally, controls who can read each one based on the workload's identity rather than a shared password, and logs every read. Many can also rotate secrets on a schedule, and some can generate a database credential per pod that expires after an hour, so a leaked password becomes useless quickly.
6 messages in order. Play them, or step through them one at a time.
Delivery can also happen outside the application: a Vault agent sidecar or a secrets store CSI driver writes the secret into a mounted file, and the service reads it as a config tree without any Vault dependency in its code. The cost of a secret manager is that it becomes critical infrastructure, it needs high availability and break-glass procedures, and applications using short-lived credentials must handle renewal, including the connection pool reconnecting with new credentials. Check your library versions for the exact property names.
5What We Never Do
We never commit a password to application.yml or any file in the repository, not even for a staging database, and not even encrypted with a key stored next to it. Git history keeps it forever, every clone copies it, and rotating it requires finding every copy. The same applies to images: a password copied into a Dockerfile or a packaged config file is readable by anyone who can pull the image, in every registry and cache it reaches. The jar's configuration should contain only a reference, such as ${db.password}, and the value arrives at deployment time.
Secret scanning in CI and pre-commit hooks catches most accidents before they are pushed, and if a secret does leak, the response is to rotate it at once, because cleaning history does not undo copies that already exist.
6Keeping Actuator From Leaking It
The Actuator env and configprops endpoints can show every resolved property, including spring.datasource.password. Spring Boot 3 masks all values in these endpoints by default, controlled by management.endpoint.env.show-values and management.endpoint.configprops.show-values. We keep them at the default NEVER on services with secrets, or WHEN_AUTHORIZED with a dedicated role on a protected management port, and never ALWAYS. Older Spring Boot versions masked keys by name, such as those containing password; in Spring Boot 3, once values are shown they are shown as they are, unless we register our own SanitizingFunction beans for sensitive keys, so check your version before turning values on.
7Follow-up questions
Which option would you choose for a new service on Kubernetes?
A secret manager as the source of truth, delivered either through Spring Cloud Vault or a CSI driver into a mounted file read as a config tree. That keeps the value out of the environment and the image, gives audit and rotation, and lets us move to short-lived credentials later without changing how the service reads its configuration.
Is a Kubernetes Secret not already a secret store?
It is a basic one. Secrets are base64-encoded and, unless encryption at rest is configured, stored in plain form in etcd, and anyone with permission to read Secrets in the namespace can read them. With encryption at rest, strict access control and no Secrets committed to Git, it is acceptable for many services, but it lacks per-read audit and rotation.
How do you rotate a static password without downtime?
Use two valid credentials during the switch: create the new password or a second user in the database, update the secret, roll the pods so they connect with the new one, check that no connection uses the old one, then remove it. A pool max lifetime shorter than the overlap period ensures old connections close in time.
8Conclusion
In this answer, we compared the ways to supply a database password. Environment variables are simple but visible to process listings, crash dumps and child processes. Mounted files read as a config tree keep the value out of the image and the environment and work naturally with Kubernetes Secrets. A secret manager adds auditing, rotation and short-lived credentials, at the cost of running critical infrastructure. Secrets never go into application.yml or images, and the env and configprops endpoints must keep values masked.
Open with
I see three real options and one we never use. Environment variables, for example SPRING_DATASOURCE_PASSWORD from a Kubernetes Secret, are the simplest. But the value sits in the process environment, so it is copied to child processes, can show up in crash dumps and diagnostic tools, is readable from proc by privileged users, and needs a restart to change.
Then, if they want more
Mounted files are better. Kubernetes mounts the Secret as files and Spring Boot reads them with spring.config.import and configtree, so the value is not in the image or the environment, and the files are updated on rotation, although the app still needs a rolling restart. The strongest option is a secret manager like Vault or a cloud secret service.
Access is based on the workload's identity, every read is audited, rotation is built in, and with dynamic secrets each pod can get a database user that expires after an hour. The cost is that the store becomes critical infrastructure and the app has to handle renewal.
Delivery can be through Spring Cloud Vault or a CSI driver writing a file, which keeps the application simple. What we never do is commit secrets to application.yml or bake them into images, and we add secret scanning in CI.
Finally, I make sure the Actuator env and configprops endpoints keep values masked, which is the default in Spring Boot 3, on a protected management port.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Boot 3
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring Boot reference
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Describe the options for supplying database passwords to a Spring Boot service in...": https://scrivio-interview-library.netlify.app/spring-boot/externalized-configuration/0f603ca94c/