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
MediumAddress
SecurityCompliance Standards
- CIS AKS
Triage and Remediation
- Remediation
Remediation
Manual Steps
Manual Steps
-
Identify pods using secrets as environment variables
- Run on: any machine with kubectl access
- Note each
NAMESPACEandNAMEyou need to fix (usually Deployments, StatefulSets, DaemonSets, Jobs, CronJobs, Pods).
-
Inspect one affected workload’s manifest
- Run on: any machine with kubectl access
ReplaceNAMESPACEandNAMEwith values from step 1:
(If the resource is not a Deployment, changedeploymentto its kind in lowercase, e.g.statefulset,daemonset,job,pod.) - Run on: any machine with kubectl access
-
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
envandenvFromentries that usesecretKeyRef. Example to delete: - Add a
volumeMountsentry pointing to a new secret-backed volume, and add that volume underspec.template.spec.volumes(orspec.volumesfor a Pod). Example pattern to add:
- Remove
- 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.
-
Apply the updated manifest
- Run on: any machine with kubectl access
- Repeat steps 2–4 for each affected resource from step 1.
-
(Optional) Confirm pods are using secret volumes and not env-based secrets
- Run on: any machine with kubectl access
-
Verify cluster-wide that env-based secret references are removed
- Run on: any machine with kubectl access
Using kubectl
Using kubectl
On any machine with kubectl access:For each listed Deployment/StatefulSet/DaemonSet/Pod, get its full manifest:After refactor:Application code must be updated to read from the mounted files, e.g. Repeat for each affected Deployment/StatefulSet/DaemonSet/Pod.
- Identify the affected workloads
- Edit the manifest to use secrets as files
- Under each affected container, remove any
envorenvFromentries that referencesecretKeyRef. - Add a
volumethat sources the Secret. - Add a
volumeMountto mount that volume into the container.
/var/run/secrets/db/password instead of an environment variable.- Apply the updated manifest
- Verification
Automation
Automation

