Review ServiceAccount token automounting

Disable automatic token mounts for pods that do not need Kubernetes API tokens.

Description

Set automount_service_account_token = false on a Pod or workload template that needs no Kubernetes API token. An explicit Pod setting overrides the ServiceAccount setting.

Potential impact

A token exposed through an application or a debugging tool can be used to access the Kubernetes API with the account’s permissions.

Remediation

  • Check the mounts on a newly created Pod, including any explicitly configured token volumes.
  • If a token is needed, give the workload a dedicated ServiceAccount with only the required permissions and choose the token audience and lifetime for that use.

Examples

Create the something namespace before deploying this DaemonSet and choose a maintained image.

Before

hcl
resource "kubernetes_daemonset" "example" {
  metadata {
    name      = "terraform-example"
    namespace = "something"
  }

  spec {
    selector {
      match_labels = {
        test = "MyExampleApp"
      }
    }

    template {
      metadata {
        labels = {
          test = "MyExampleApp"
        }
      }

      spec {
        automount_service_account_token = true

        container {
          image = "nginx:1.7.8"
          name  = "example"
        }
      }
    }
  }
}

After

hcl
resource "kubernetes_daemonset" "example" {
  metadata {
    name      = "terraform-example"
    namespace = "something"
  }

  spec {
    selector {
      match_labels = {
        test = "MyExampleApp"
      }
    }

    template {
      metadata {
        labels = {
          test = "MyExampleApp"
        }
      }

      spec {
        automount_service_account_token = false

        container {
          image = "nginx:1.7.8"
          name  = "example"
        }
      }
    }
  }
}

Explanation:

  • Before: true in the Pod template allows automatic token mounting.
  • After: New Pods created with false run without the automatic mount. Test any functionality that depends on a token.

References