More Info:
Secrets exposed as environment variables are more likely to be leaked through logs, child processes, or crash dumps. Prefer mounting secrets as files for improved confidentiality.Risk Level
MediumAddress
SecurityCompliance Standards
- CIS EKS
Triage and Remediation
- Remediation
Remediation
Manual Steps
Manual Steps
-
List all pods using secrets as environment variables (run on any machine with kubectl access):
-
For each affected workload (Deployment/StatefulSet/DaemonSet/Job/CronJob), export its manifest (run on any machine with kubectl access, example for a Deployment):
-
In the saved manifest file, manually:
- Under
spec.template.spec, add or extend avolumesentry to mount the secret: - Under the container spec, add or extend
volumeMounts: - Remove any
envorenvFromentries that usesecretKeyReffor that secret, and adjust your application code/config to read from the mounted file path instead.
- Under
-
Apply the updated manifest (this will roll pods; run on any machine with kubectl access):
-
Repeat steps 2–4 for each listed resource kind (e.g.,
statefulset,daemonset,job,cronjob) that usessecretKeyRefinenvorenvFrom. -
Verify no workloads are using secrets as environment variables (run on any machine with kubectl access):
Using kubectl
Using kubectl
On any machine with kubectl access:Pick one workload (example below uses a Deployment in namespace Change it to mount the secret as a volume and have the app read from the file path instead of the env var. Remove the After this change, application code must read Repeat steps 2–4 for each workload found in step 1.
- Identify pods using secrets as environment variables
default named my-app).- Export its manifest for editing
- Edit the manifest to replace env-based secrets with mounted secret files
my-app-deploy.yaml, locate the container using secretKeyRef, for example:env entry and add volumeMounts and a volumes section like:/etc/secrets/db/password instead of DB_PASSWORD. This code change is outside kubectl’s scope and must be done in the application.- Apply the updated manifest
- Verification (cluster-wide)
Automation
Automation

