Scrivio Interview library
ScenarioIntermediate6 min read · updated 12 November 2026

Kubernetes mounts secrets as files under /etc/secrets/db/username and /etc/secrets/db/password. How would you make them available as db.username and db.password in Spring Boot?

We add spring.config.import=optional:configtree:/etc/secrets/ to the configuration. Spring Boot then turns the folder and file names under that directory into property names, so /etc/secrets/db/username becomes db.username, and each file's content becomes the value. We should check the files for trailing newlines or other whitespace, and remember that the files are read at startup, so a rotated secret needs a restart or a refresh mechanism to take effect.

Why this question is asked

Kubernetes delivers secrets to pods in two ways: as environment variables or as files in a mounted volume. Files are often preferred, because environment variables are easier to leak through process listings, crash reports and child processes, and because mounted files can be updated without recreating the pod. The application then has to read those files, and Spring Boot's config tree support does this without any custom code. Interviewers ask this scenario because it checks practical knowledge of how Kubernetes and Spring Boot meet. In this answer, we will recap how Kubernetes mounts a secret, add the import, see how the names are formed, deal with the trailing newline issue, and look at what happens when the secret is rotated.

1How Kubernetes Mounts the Secret

A Kubernetes Secret is an object holding key-value pairs. When a pod mounts it as a volume, Kubernetes creates one file per key in the mount directory, named after the key, with the value as its content. So a secret with keys username and password, mounted at /etc/secrets/db, produces exactly the two files from the question.

YAML
apiVersion: v1
kind: Secret
metadata:
name: orders-db
stringData:
username: orders_app
password: s3cr3t
---
# in the pod spec
volumes:
- name: db-secret
secret:
secretName: orders-db
containers:
- name: orders
volumeMounts:
- name: db-secret
mountPath: /etc/secrets/db
readOnly: true
# inside the container
# /etc/secrets/db/username -> orders_app
# /etc/secrets/db/password -> s3cr3t

2Importing the Directory as a Config Tree

A config tree is Spring Boot's name for exactly this layout: a directory where file names are keys and file contents are values. One line imports it. We point it at the parent directory, /etc/secrets/, so that the subdirectory name db becomes the first part of each property name.

YAML
# application.yml
spring:
config:
import: "optional:configtree:/etc/secrets/"
# the properties we get
# db.username = orders_app
# db.password = s3cr3t
# use them like any property
spring:
datasource:
username: ${db.username}
password: ${db.password}
Step throughFrom mounted files to properties
  1. Kubernetes mounts the Secretone file per key under /etc/secrets/db
  2. Spring Boot reads configtree:/etc/secrets/walks the directory tree at startup
  3. Path becomes the namedb/username -> db.username
  4. Content becomes the valueorders_app
  5. Placeholders resolvespring.datasource.username = ${db.username}

5 steps in order. Play them, or step through them one at a time.

We mark the import optional: so that the same jar still starts on a developer machine without /etc/secrets, where local values come from elsewhere. In production we might prefer to leave out optional:, so a missing mount fails startup clearly rather than producing a confusing database login error later; that is a choice per environment, and the import can live in a profile-specific document.

3How the Names Are Formed

Spring Boot builds the property name from the path below the imported directory, joining folder and file names with dots. That is why we import /etc/secrets/ and not /etc/secrets/db/: importing the deeper directory would give properties called username and password, without the db prefix.

There is one more form worth knowing. If several secrets are mounted in sibling folders and we want each folder treated as its own tree, a wildcard such as configtree:/etc/secrets/*/ imports each subdirectory separately. Notice that this drops the folder name: /etc/secrets/db/username would then become just username. For this question we want the folder name in the key, so the plain import of the parent directory is the right one.

Which import gives which name
ImportFileProperty
configtree:/etc/secrets//etc/secrets/db/usernamedb.username
configtree:/etc/secrets/db//etc/secrets/db/usernameusername
configtree:/etc/secrets/*//etc/secrets/db/usernameusername

4Watch the File Contents: Trailing Newlines

The value is the content of the file, and files often end with a newline, especially when the secret was created from a file with kubectl create secret --from-file or written by a script with echo. Recent Spring Boot versions trim a single trailing newline from config tree values, but other stray characters, such as a carriage return from a Windows editor or a trailing space, can still end up in the value; check your version and the actual bytes.

Shell
kubectl exec deploy/orders -- cat -A /etc/secrets/db/password
# s3cr3t^M$ <- a carriage return (^M) before the newline
# Recreate the secret without stray characters
kubectl create secret generic orders-db \
--from-literal=username=orders_app \
--from-literal=password='s3cr3t' --dry-run=client -o yaml | kubectl apply -f -

A password with an invisible character fails authentication with a message that looks like a wrong password, which can waste hours. Looking at the bytes with cat -A or od -c is the quickest way to rule it out.

5Rotation: Read at Startup

Config tree values are read once, when the application starts, and copied into the Environment. Kubernetes does update the files in a mounted secret volume some time after the Secret changes (but not for volumes mounted with subPath), yet the running application keeps the old values, and the connection pool keeps connecting with the old password.

So rotation needs a plan. The simplest is a rolling restart after the secret changes, for example kubectl rollout restart deployment/orders, triggered by the rotation process or by a tool that watches secrets. During the switch, the database should accept both the old and the new password for a while, so pods with either can connect. For frequent rotation or short-lived credentials, a refresh mechanism, such as Spring Cloud Vault's lease handling or a driver that reads the credential on each new connection, is the better fit.

6Follow-up questions

Why not simply use environment variables from the secret?

We can, with env and secretKeyRef, and Spring Boot maps DB_USERNAME to db.username. Files are often preferred because environment variables are inherited by child processes, can appear in crash reports and diagnostic dumps, and cannot be updated without recreating the pod.

Are config tree values masked in the Actuator?

In Spring Boot 3 all values in the env and configprops endpoints are masked by default, including these. Once show-values is enabled, Spring Boot 3 shows them as they are unless a SanitizingFunction masks them, so it is safest not to show values on services that hold secrets.

What if the secret is also needed by a non-Spring sidecar?

The same mounted files can be read by any process in the pod. That is another advantage of files: one Secret mount serves the Java application through the config tree and other tools through their own file reading.

7Conclusion

In this answer, we made Kubernetes secrets mounted as files available as db.username and db.password by adding spring.config.import=optional:configtree:/etc/secrets/. Spring Boot turns the folder and file names into the property name and the file content into the value, so we import the parent directory to keep the db prefix. We check the file bytes for stray whitespace, and since the files are read at startup, rotation needs a rolling restart or a refresh mechanism.

What to say in the interviewAbout 90 seconds aloud

Open with

I would add spring.config.import with optional:configtree:/etc/secrets/ to the application configuration. A config tree is a directory where each file name is a property key and the file content is the value, which is exactly how Kubernetes mounts a Secret as a volume.

Then, if they want more

Spring Boot joins the folder and file names below the imported directory with dots, so /etc/secrets/db/username becomes db.username and the password file becomes db.password. That is why I import the parent directory, not /etc/secrets/db/, which would lose the db prefix, and why I would not use the wildcard form here.

Then I can refer to them as normal properties, for example spring.datasource.username set to the db.username placeholder. I use optional so the jar still starts locally without the mount, though in production I might drop it so a missing mount fails clearly. Two practical things matter.

First, the value is the file's exact content, so stray characters like a carriage return or a trailing space become part of the password. Recent Boot versions trim a single trailing newline, but I would check the bytes with cat -A if authentication fails. Second, the files are read once at startup.

Kubernetes updates the mounted files after a Secret changes, but the running service keeps the old values, so rotation needs a rolling restart, with the database accepting both passwords for a while, or a refresh mechanism like Vault leases.

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 "Kubernetes mounts secrets as files under /etc/secrets/db/username and...": https://scrivio-interview-library.netlify.app/spring-boot/externalized-configuration/ea7e70da6c/