Q&A-22Review a design that stores secrets in application environment variables.
Answer
Verdict: ship it only as a stopgap. Environment variables are the weakest of the three common delivery paths. They are readable by any process running as the same UID, copied into every child process, and captured verbatim by core dumps and most crash reporters. Move to workload identity plus an SDK fetch, or a tmpfs file written by a sidecar/agent.
Assumptions I'd state up front: Linux containers on Kubernetes 1.29+, secrets sourced from a manager (Vault or AWS Secrets Manager), app runs as non-root, no hostPID/hostNetwork, and the review is about delivery, not the secret's own entropy.
Why env leaks: the mechanism
On Linux /proc/<pid>/environ is mode 0400 owned by the process's real UID. Every process in the container running as that UID (a shell from kubectl exec, a sidecar sharing the UID, a compromised dependency) can cat it. fork/exec copies environ into children, so a psql or curl subprocess carries the secret too. A core dump is a snapshot of the address space, so the secret is inside it; many distros pipe core_pattern to systemd-coredump, which writes /var/lib/systemd/coredump: check its mode and retention. And docker inspect prints Config.Env to anyone with Docker API access.
| Surface | Who reads it | Fix |
|---|---|---|
/proc/<pid>/environ | any process with the same UID, or root | run non-root, drop CAP_SYS_PTRACE, no shared-UID sidecar |
| child process env | anything the app execs | pass secrets on stdin or an fd, not env |
| core dump | whoever owns core_pattern output | ulimit -c 0, disable crash-reporter env capture |
| pod spec / etcd | anyone with get pod, or an etcd backup | use secretKeyRef, enable encryption at rest |
| image history | anyone who can pull the image | BuildKit --mount=type=secret, never ARG + RUN |
| APM / error events | vendor plus dashboard users | verify SDK env capture, redact |
Trade-offs: the deciding criteria
Three axes: rotation latency, blast radius, and who can read the value.
- Env var. Rotation requires a pod restart, because env is fixed at container start: updating the Secret does not change a running container's env. Blast radius is every process in the container.
- Mounted Secret volume. The kubelet projects updates, but the delay is up to the sync period (default 1 min) plus cache propagation, and the app must re-read the file. Blast radius is anything that can read the mount path.
- SDK fetch with workload identity (IRSA, GKE Workload Identity, Vault Agent). No long-lived credential sits in the pod; Vault dynamic database credentials carry a role-level TTL you set (commonly 1h), and AWS Secrets Manager rotation has an
AutomaticallyAfterDaysminimum of 1 day. Blast radius is the pod's identity, revocable centrally.
Cost matters at scale. Secrets Manager is $0.40 per secret per month plus $0.05 per 10,000 GetSecretValue calls. Two hundred pods fetching once at startup is 200 calls: about $0.001. Fetching per request at 500 rps is 43.2M calls/day, roughly $216/day, so cache in memory with a TTL.
When env is acceptable: short-lived, single-tenant, non-root, no child processes, no crash reporting, and a rotation path that tolerates restarts. Otherwise no.
Failure modes and how to detect them
- Secret baked at build time.
docker history --no-trunc <image> | grep -i secretshows it. - Child-process leak. In a test pod, exec in and run
ps eww -p <pid>; the env of processes you own is printed. - Core-dump capture. In staging, set
ulimit -c unlimited, trigger a crash, then grep the dump for the secret value. - Rotation that never takes. Change the secret, wait two minutes,
kubectl execandcat /proc/1/environ. If the old value is still there, the app never restarted. - APM capture. Send a test exception and grep the event payload for the value.
Curated: · Written: · Reviewed:
Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.