Skip to main content

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

Medium

Address

Security

Compliance Standards

  • CIS OKE

Triage and Remediation

Remediation

Manual Steps

  1. List pods that reference Secrets in env/envFrom
    • On any machine with kubectl access:
  2. Inspect how each affected pod uses Secrets
    • For each <namespace> <pod> from step 1, get its full spec:
    • Review for env: and envFrom: entries with secretKeyRef / secretRef, and check whether there is already an alternative volume / volumeMount using the same Secret.
  3. 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).
  4. 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.
  5. 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 edit or your GitOps/IaC):
    • Apply updated manifests with kubectl apply -f <file.yaml> from any machine with kubectl access.
  6. 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.

Using kubectl

Run these commands from any machine with kubectl access.

1. List pods that use env or envFrom (cluster-wide)

Problem indication:
  • Any env entry that includes valueFrom.secretKeyRef.
  • Any envFrom entry that includes secretRef.
These are containers consuming secrets via environment variables.

2. Inspect a specific pod’s environment for secret usage

Replace <namespace> and <pod>:
Check under spec.containers[].env and spec.containers[].envFrom:
  • env[].valueFrom.secretKeyRef → secret as environment variable (problem).
  • envFrom[].secretRef → entire secret as environment variables (problem).
Also confirm whether the same pod (or deployment) already mounts the secret as a volume under 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:
Then inspect the controller spec:
Problem indication in these specs:
  • Any env[].valueFrom.secretKeyRef under spec.template.spec.containers[].
  • Any envFrom[].secretRef under spec.template.spec.containers[].

4. Confirm where secrets are used as volumes (for comparison)

Healthy indication:
  • Secrets appear under spec.volumes[].secret.secretName.
  • Corresponding spec.containers[].volumeMounts[].name reference those secret volumes.
  • No (or minimized) use of the same secrets via env/envFrom.
Human review required:
  • 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 kubectl transformation.