Skip to main content

More Info:

Secrets exposed as environment variables are more easily leaked via process listings, logs, and child processes. Mounting secrets as files reduces this exposure.

Risk Level

Medium

Address

Security

Compliance Standards

  • CIS AKS

Triage and Remediation

Remediation

Manual Steps

  1. Identify pods using secrets as environment variables
    • Run on: any machine with kubectl access
    • Note each NAMESPACE and NAME you need to fix (usually Deployments, StatefulSets, DaemonSets, Jobs, CronJobs, Pods).
  2. Inspect one affected workload’s manifest
    • Run on: any machine with kubectl access
      Replace NAMESPACE and NAME with values from step 1:
    (If the resource is not a Deployment, change deployment to its kind in lowercase, e.g. statefulset, daemonset, job, pod.)
  3. Edit the manifest to mount secrets as files instead of env variables
    • Run on: any machine with kubectl access
    • Open the file:
    • In each container spec:
      • Remove env and envFrom entries that use secretKeyRef. Example to delete:
      • Add a volumeMounts entry pointing to a new secret-backed volume, and add that volume under spec.template.spec.volumes (or spec.volumes for a Pod). Example pattern to add:
    • Coordinate with the application team so the application reads from the mounted files (e.g. /var/run/secrets/db/password) instead of environment variables before applying.
  4. Apply the updated manifest
    • Run on: any machine with kubectl access
    • Repeat steps 2–4 for each affected resource from step 1.
  5. (Optional) Confirm pods are using secret volumes and not env-based secrets
    • Run on: any machine with kubectl access
  6. Verify cluster-wide that env-based secret references are removed
    • Run on: any machine with kubectl access
On any machine with kubectl access:
  1. Identify the affected workloads
For each listed Deployment/StatefulSet/DaemonSet/Pod, get its full manifest:
  1. Edit the manifest to use secrets as files
In the downloaded YAML:
  • Under each affected container, remove any env or envFrom entries that reference secretKeyRef.
  • Add a volume that sources the Secret.
  • Add a volumeMount to mount that volume into the container.
Example transformation (before):
After refactor:
Application code must be updated to read from the mounted files, e.g. /var/run/secrets/db/password instead of an environment variable.
  1. Apply the updated manifest
Repeat for each affected Deployment/StatefulSet/DaemonSet/Pod.
  1. Verification
Run the same audit command and confirm it reports no remaining references: