More Info:
Kubernetes supports mounting secrets as data volumes or as environment variables. Minimize the use of environment variable secrets.Risk Level
HighAddress
SecurityCompliance Standards
- CIS Kubernetes
Triage and Remediation
- Remediation
Remediation
Manual Steps
Manual Steps
-
List all Secrets and identify likely sensitive ones
Run on any machine withkubectlaccess:Note Secrets that likely contain credentials or keys (names includingpassword,token,key,cert,secret, etc.) for deeper review in the next steps. -
Identify Pods that consume Secrets as environment variables
Run:This shows Pods/containers withenventries sourced from Secrets. Capture the namespace, pod, container, env var name, and secret name. -
Identify Pods that use Secrets as files (preferred)
Run:This shows Pods that mount Secrets as volumes. Use this as a reference “good pattern” when planning changes away from env var usage. -
Review affected Deployments/Workloads and application behavior
For one affected Pod from step 2, find and inspect its controller (e.g., Deployment):Indeployment-inspect.yaml, locate containers usingenv.valueFrom.secretKeyRef. Assess with the application owner whether the app can be modified to read these values from files (mounted Secret volume) instead of environment variables. -
Plan and implement manifest changes to use Secret volumes instead of env vars
For each agreed change:- Edit the workload manifest (Deployment/StatefulSet/Job, etc.) to:
- Add a
volumesentry referencing the Secret: - Mount it into the container:
- Remove or phase out
enventries that reference the same Secret, once the application has been updated to read from/var/run/secrets/app/.... Apply the updated manifest from any machine withkubectl:
- Add a
- Edit the workload manifest (Deployment/StatefulSet/Job, etc.) to:
-
Verify reduced use of secrets in environment variables
After changes roll out, re-run the environment-variable usage check:Confirm that pods for the updated workloads no longer appear, and that the corresponding pods show Secret volume mounts (step 3) instead.
Using kubectl
Using kubectl
Using kubectl
1. List all pods that use environment variables from Secrets
Run on: any machine withkubectl access.Any
env: or envFrom: line where fromSecret= or secretRef= is non-empty shows a container using a Secret via environment variables. These are candidates to convert to file-based mounts.2. Identify workload types using Secret env vars (Deployments, StatefulSets, etc.)
Any listed workload has at least one container using a Secret via
env or envFrom. These should be reviewed and, where feasible, changed to use volume-mounted Secrets.3. Compare with existing Secret volume mounts (to see if an alternative already exists)
If a workload shows both
volumeSecret: and env/envFrom lines referring to the same Secret, it is already mounting the Secret as a file but still using it via env vars. This is a strong candidate to update the application code/config to read from the mounted file instead of the env var.4. Focus on a specific namespace (optional, for detailed review)
env and envFrom sections for secretKeyRef/secretRef usage versus any volumes and volumeMounts using secret:.Indicates a problem:Containers that consume Secrets only via
env/envFrom and have no Secret volume mounts, or use both but still rely on env vars.5. Verification step after any changes
After you refactor workloads to use file-based Secrets, re-run:The output is empty or only includes workloads that you have consciously decided must continue using Secret environment variables (document the justification).
Automation
Automation

