More Info:
Secrets exposed as environment variables are more easily leaked through logs and process inspection than secrets mounted as files. Prefer file mounts.Risk Level
MediumAddress
SecurityCompliance Standards
- CIS Kubernetes
Triage and Remediation
- Remediation
Remediation
Manual Steps
Manual Steps
-
Identify pods using Secret environment variables
- Run this on any machine with
kubectlaccess: - Save the list for review.
- Run this on any machine with
-
Inspect pod specs and confirm how each Secret is used
- For each
NAMESPACE PODfrom step 1, inspect full spec: - In
spec.containers[*].env/envFrom, note:- Secret name and keys.
- Whether the same Secret is already mounted as a volume.
- Application expectations (e.g., variable names used by the app).
- For each
-
Decide whether each Secret can be consumed via file instead of env
- For each env-based Secret:
- Review application code/config (and documentation) to see if it can read from a file path (config option, CLI flag, or code change).
- If the app cannot be changed (e.g., third-party image requiring env vars), document this as an accepted exception with justification and skip to the next Secret.
- If it can be changed, plan the mount path and per-key filenames (e.g.,
/var/run/secrets/<secret-name>/<key>).
- For each env-based Secret:
-
Refactor manifests to mount Secrets as volumes instead of env vars
- On any machine with
kubectlaccess, edit the workload manifests (Deployment/StatefulSet/DaemonSet/Job/CronJob, not the running Pod) to:- Add a Secret volume:
- Mount it into each container:
- Remove the corresponding
env/envFromentries that reference the same Secret keys, and update container args/config to use file paths instead of env vars.
- Add a Secret volume:
- Apply the updated manifest:
- On any machine with
-
Roll out and validate application behavior
- Ensure updated workloads are running:
- Confirm the application reads secrets from files (e.g., health checks succeed, functional tests pass, or logs indicate successful secret loading).
- Ensure updated workloads are running:
-
Re-audit to confirm reduced use of Secret environment variables
- Re-run the detection from step 1:
- Verify that only documented exceptions remain and that all other workloads now consume Secrets via mounted files.
- Re-run the detection from step 1:
Using kubectl
Using kubectl
- Under
spec.containers[].env[]you seevalueFrom.secretKeyRef, for example: - Or under
spec.initContainers[].env[]you see the same pattern. - Or
envFrom.secretRefis used:
secretKeyRef in env or envFrom:env: or envFrom: signal Pods that should be reviewed for possible migration to file-based secret mounts.For a single Pod you are investigating:spec.containers[].env[].valueFrom.secretKeyRefspec.containers[].envFrom[].secretRefspec.initContainers[].env[].valueFrom.secretKeyRefspec.initContainers[].envFrom[].secretRef
Automation
Automation
- Any line in the first table (
env[].valueFrom.secretKeyRef) or second table (envFrom[].secretRef) indicates a Pod/container that is exposing a Secret via environment variables. - These Pods are candidates for remediation: refactor the application to read from Secrets mounted as files instead of env vars.
- The summary section helps you see which namespaces have the largest number of such Pods so you can prioritize review.

