Skip to main content

More Info:

Secrets injected as environment variables are more likely to be exposed through logs, child processes or crash dumps than secrets mounted as files. Applications should read secrets from mounted secret files.

Risk Level

Medium

Address

Security

Compliance Standards

  • CIS GKE

Triage and Remediation

Remediation

Manual Steps

  1. List all workloads that reference secrets via environment variables
    • Run on: any machine with kubectl access
  2. Inspect a specific Pod/Deployment to identify which env vars use secrets
    • Run on: any machine with kubectl access
      Replace NAMESPACE and NAME as needed.
  3. Design the file-based secret mount (no change yet)
    • Run on: any machine with kubectl access
      In /tmp/deployment-NAMESPACE-NAME.yaml, note:
    • The secretKeyRef.name (the Secret object name)
    • Each secretKeyRef.key used as an environment variable
      Decide a mount path, e.g. /var/run/secrets/APPNAME, and expected filenames, e.g. matching the key names.
  4. Modify the workload manifest to mount the secret as files and stop using env vars
    • Run on: any machine with kubectl access
      Edit /tmp/deployment-NAMESPACE-NAME.yaml:
    • In spec.template.spec.volumes, add:
    • In the relevant container under spec.template.spec.containers[]:
      • Add a volumeMounts entry:
      • Remove or comment out the env entries that use secretKeyRef. Save the file, then apply:
  5. Update application code/configuration to read secrets from files
    • Run on: application source/deployment pipeline (outside cluster)
      Change the application so it reads secret values from the mounted files (for example, /var/run/secrets/APPNAME/SECRET_KEY) instead of environment variables. Rebuild and redeploy the image if necessary, then ensure the Deployment uses the updated image:
  6. Verify no remaining environment-variable secret references
    • Run on: any machine with kubectl access
On any machine with kubectl access:
  1. Identify pods using secrets as environment variables
  1. For each affected workload, get the controller manifest (Deployment, StatefulSet, etc.)
Example for a Deployment:
  1. Edit the manifest locally to:
  • Remove env entries that use secretKeyRef
  • Add a volume that references the Secret
  • Mount that volume into the container
Minimal pattern (replace placeholders):
  1. Apply the updated manifest
Repeat for all controllers (Deployment/StatefulSet/DaemonSet/CronJob/Job) using env-based secrets.
  1. Verification