Skip to main content

More Info:

Kubernetes supports mounting secrets as data volumes or as environment variables. Minimize the use of environment variable secrets.

Risk Level

High

Address

Security

Compliance Standards

  • CIS Kubernetes

Triage and Remediation

Remediation

Manual Steps

  1. List all Secrets and identify likely sensitive ones
    Run on any machine with kubectl access:
    Note Secrets that likely contain credentials or keys (names including password, token, key, cert, secret, etc.) for deeper review in the next steps.
  2. Identify Pods that consume Secrets as environment variables
    Run:
    This shows Pods/containers with env entries sourced from Secrets. Capture the namespace, pod, container, env var name, and secret name.
  3. Identify Pods that use Secrets as files (preferred)
    Run:
    This shows Pods that mount Secrets as volumes. Use this as a reference “good pattern” when planning changes away from env var usage.
  4. Review affected Deployments/Workloads and application behavior
    For one affected Pod from step 2, find and inspect its controller (e.g., Deployment):
    In deployment-inspect.yaml, locate containers using env.valueFrom.secretKeyRef. Assess with the application owner whether the app can be modified to read these values from files (mounted Secret volume) instead of environment variables.
  5. Plan and implement manifest changes to use Secret volumes instead of env vars
    For each agreed change:
    • Edit the workload manifest (Deployment/StatefulSet/Job, etc.) to:
      • Add a volumes entry referencing the Secret:
      • Mount it into the container:
      • Remove or phase out env entries that reference the same Secret, once the application has been updated to read from /var/run/secrets/app/.... Apply the updated manifest from any machine with kubectl:
  6. Verify reduced use of secrets in environment variables
    After changes roll out, re-run the environment-variable usage check:
    Confirm that pods for the updated workloads no longer appear, and that the corresponding pods show Secret volume mounts (step 3) instead.

Using kubectl

1. List all pods that use environment variables from Secrets

Run on: any machine with kubectl access.
Indicates a problem:
Any env: or envFrom: line where fromSecret= or secretRef= is non-empty shows a container using a Secret via environment variables. These are candidates to convert to file-based mounts.

2. Identify workload types using Secret env vars (Deployments, StatefulSets, etc.)

Indicates a problem:
Any listed workload has at least one container using a Secret via env or envFrom. These should be reviewed and, where feasible, changed to use volume-mounted Secrets.

3. Compare with existing Secret volume mounts (to see if an alternative already exists)

Indicates a problem/opportunity:
If a workload shows both volumeSecret: and env/envFrom lines referring to the same Secret, it is already mounting the Secret as a file but still using it via env vars. This is a strong candidate to update the application code/config to read from the mounted file instead of the env var.

4. Focus on a specific namespace (optional, for detailed review)

Manually review each container’s env and envFrom sections for secretKeyRef/secretRef usage versus any volumes and volumeMounts using secret:.Indicates a problem:
Containers that consume Secrets only via env/envFrom and have no Secret volume mounts, or use both but still rely on env vars.

5. Verification step after any changes

After you refactor workloads to use file-based Secrets, re-run:
Desired outcome:
The output is empty or only includes workloads that you have consciously decided must continue using Secret environment variables (document the justification).

Additional Reading: