Views:
Profile applicability: Level 1
Service accounts tokens should not be mounted in pods except where the workload running in the pod explicitly needs to communicate with the API server
Mounting service account tokens inside pods can provide an avenue for privilege escalation attacks where an attacker is able to compromise a single pod in the cluster. Avoiding mounting these tokens removes this attack avenue.
Note
Note
By default, all pods get a service account token mounted in them.

Impact

Pods mounted without service account tokens will not be able to communicate with the API server, except where the resource is available to unauthenticated principals.

Audit

Review pod and service account objects in the cluster and ensure that the option below is set, unless the resource explicitly requires this access:
automountServiceAccountToken: false
echo "=== ServiceAccounts with automountServiceAccountToken=false ==="
kubectl get sa -A -o jsonpath='{range .items[?(@.automountServiceAccountToken==false)]}{.metadata.namespace}{"\t"}{.metadata.name}{"\n"}{end}' \
  | sort | awk -F'\t' '{printf "%-25s %-40s\n", $1, $2}'
echo "=== Pods with automountServiceAccountToken=false ==="
kubectl get pods -A -o jsonpath='{range .items[?(@.spec.automountServiceAccountToken==false)]}{.metadata.namespace}{"\t"}{.metadata.name}{"\n"}{end}' \
  | sort | awk -F'\t' '{printf "%-25s %-40s\n", $1, $2}'
  • kubectl get ... -A scans all namespaces.
  • jsonpath filters for automountServiceAccountToken == false.
  • awk formats the output into aligned columns.
  • If a section is empty, no objects explicitly set automountServiceAccountToken: false, meaning they still auto-mount tokens by default and may need remediation.
Sample Output:
=== ServiceAccounts with automountServiceAccountToken=false ===
NAMESPACE                 SERVICEACCOUNT
-------------------------  ----------------------------------------
default                   default
kube-system               metrics-reader

=== Pods with automountServiceAccountToken=false ===
NAMESPACE                 POD
-------------------------  ----------------------------------------
dev                       api-service
prod                      nginx-test

Remediation

Modify the definition of pods and service accounts which do not need to mount service account tokens to disable it.