Skip to main content

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

Medium

Address

Security

Compliance Standards

  • CIS Kubernetes

Triage and Remediation

Remediation

Manual Steps

  1. Identify pods using Secret environment variables
    • Run this on any machine with kubectl access:
    • Save the list for review.
  2. Inspect pod specs and confirm how each Secret is used
    • For each NAMESPACE POD from 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).
  3. 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>).
  4. Refactor manifests to mount Secrets as volumes instead of env vars
    • On any machine with kubectl access, 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 / envFrom entries that reference the same Secret keys, and update container args/config to use file paths instead of env vars.
    • Apply the updated manifest:
  5. 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).
  6. 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.
Review each relevant namespace (or all, if unsure):
In the output, a Pod is potentially problematic if:
  • Under spec.containers[].env[] you see valueFrom.secretKeyRef, for example:
  • Or under spec.initContainers[].env[] you see the same pattern.
  • Or envFrom.secretRef is used:
These indicate Secrets are injected as environment variables.To focus specifically on Pods that use secretKeyRef in env or envFrom:
Any matches under env: or envFrom: signal Pods that should be reviewed for possible migration to file-based secret mounts.For a single Pod you are investigating:
Again, look specifically for:
  • spec.containers[].env[].valueFrom.secretKeyRef
  • spec.containers[].envFrom[].secretRef
  • spec.initContainers[].env[].valueFrom.secretKeyRef
  • spec.initContainers[].envFrom[].secretRef
Presence of these fields means the Pod is using Secrets as environment variables and should be considered for refactoring so the application reads Secrets from mounted files instead.
How to interpret the output
  • 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.