More Info:
Secrets exposed as environment variables are more easily leaked through logs, child processes, and crash dumps. Mount secrets as files instead and read them from the filesystem.Risk Level
MediumAddress
SecurityCompliance Standards
- CIS OKE
Triage and Remediation
- Remediation
Remediation
Manual Steps
Manual Steps
-
List pods that reference Secrets in env/envFrom
- On any machine with kubectl access:
- On any machine with kubectl access:
-
Inspect how each affected pod uses Secrets
- For each
<namespace> <pod>from step 1, get its full spec: - Review for
env:andenvFrom:entries withsecretKeyRef/secretRef, and check whether there is already an alternativevolume/volumeMountusing the same Secret.
- For each
-
Review application code / startup scripts for each pod
- For each affected workload (Deployment/StatefulSet/Job, etc.), identify the image and owning controller:
- Using your source control or image documentation, determine whether the application reads these secret values from environment variables, or can instead read from files (e.g., configurable paths, options, or library support).
- For each affected workload (Deployment/StatefulSet/Job, etc.), identify the image and owning controller:
-
Decide and implement code/config changes to use mounted files
- If the application can be changed, update code or configuration to read secrets from known filesystem paths (for example
/var/run/secrets/<name>), and rebuild/publish images as needed. - If the application cannot reasonably be changed (legacy, third‑party, or breaks existing integrations), document the exception and rationale for continuing to use environment variables.
- If the application can be changed, update code or configuration to read secrets from known filesystem paths (for example
-
Refactor pod specs to mount Secrets as files instead of env vars
- For each controller owning affected pods, edit its manifest to:
- Add a Secret volume and mount path. Example (edit with
kubectl editor your GitOps/IaC):
- Add a Secret volume and mount path. Example (edit with
- Apply updated manifests with
kubectl apply -f <file.yaml>from any machine with kubectl access.
- For each controller owning affected pods, edit its manifest to:
-
Verify remaining use of Secrets in environment variables
- After rollouts complete, re-run the evidence-gathering query:
- Confirm that only pods with documented exceptions still appear, and that the rest use mounted Secret volumes instead.
- After rollouts complete, re-run the evidence-gathering query:
Using kubectl
Using kubectl
Using kubectl
Run these commands from any machine withkubectl access.1. List pods that use env or envFrom (cluster-wide)
- Any
enventry that includesvalueFrom.secretKeyRef. - Any
envFromentry that includessecretRef.
2. Inspect a specific pod’s environment for secret usage
Replace<namespace> and <pod>:spec.containers[].env and spec.containers[].envFrom:env[].valueFrom.secretKeyRef→ secret as environment variable (problem).envFrom[].secretRef→ entire secret as environment variables (problem).
spec.volumes and spec.containers[].volumeMounts; if so, environment use is usually unnecessary.3. Identify higher-level controllers using secrets as env
To fix properly, you must usually modify the controller (Deployment, StatefulSet, etc.), not the Pod.Find the owning controller of a pod:- Any
env[].valueFrom.secretKeyRefunderspec.template.spec.containers[]. - Any
envFrom[].secretRefunderspec.template.spec.containers[].
4. Confirm where secrets are used as volumes (for comparison)
- Secrets appear under
spec.volumes[].secret.secretName. - Corresponding
spec.containers[].volumeMounts[].namereference those secret volumes. - No (or minimized) use of the same secrets via
env/envFrom.
- Decide, per application, whether you can change the code/config to read from mounted files instead of environment variables.
- Plan and implement changes in manifests and application code; there is no safe automated
kubectltransformation.
Automation
Automation

