Review exposure of Secrets in environment variables

Control how Secrets are delivered and how logs and diagnostic tools handle them.

Description

env.value_from.secret_key_ref and env_from.secret_ref pass Secret values through the environment without writing the values into the configuration. At runtime, logs, debugging output or process dumps can still record them.

Potential impact

An exposed password or key can grant access to other systems. A process with read access to a mounted Secret file can also see its contents.

Remediation

  • If the app reads files, mount only the needed keys read-only in the containers that use them. Check file permissions and the path the app reads.
  • If environment variables are required, redact their values in output and restrict Secret reads and Pod debugging. Restart containers during rotation so they receive the new values.

Examples

Create db-secret in the Pod’s namespace. Use a maintained image and configure the app to read the value. The nginx image shown does not use it automatically.

Before

hcl
resource "kubernetes_pod" "example" {
  metadata {
    name = "app"
  }

  spec {
    container {
      name  = "web"
      image = "nginx:1.7.9"

      env {
        name = "DB_PASSWORD"

        value_from {
          secret_key_ref {
            name = "db-secret"
            key  = "password"
          }
        }
      }
    }
  }
}

After

hcl
resource "kubernetes_pod" "example" {
  metadata {
    name = "app"
  }

  spec {
    container {
      name  = "web"
      image = "nginx:1.7.9"

      volume_mount {
        name       = "db-secret"
        mount_path = "/var/run/secrets/db"
        read_only  = true
      }
    }

    volume {
      name = "db-secret"

      secret {
        secret_name = "db-secret"
      }
    }
  }
}

Explanation:

  • Before: The password key supplies the DB_PASSWORD environment variable.
  • After: All db-secret keys are mounted read-only under /var/run/secrets/db. Verify that the app can read the password file.

References