Skip to content

Lesson 3 of 13 · 12m

Service Accounts Are Workload Identity

Projected service account tokens, the never-expiring legacy Secrets, automountServiceAccountToken: false, and exchanging Kubernetes identity for cloud identity.

The pod's passport is mounted as a file

Every pod runs as an identity. Set spec.serviceAccountName and Kubernetes mounts the service account's credentials into the container at /var/run/secrets/kubernetes.io/serviceaccount/ — three files: token, ca.crt, and namespace. Any process in the container that reads token can call the API server as that service account.

Since Kubernetes 1.20+ the mounted token is a bound service account token (BoundServiceAccountTokenVolume, GA in 1.22): a JWT signed by the API server's serviceaccount key, scoped to the api audience, with roughly a one-hour expiry that the kubelet rotates before it dies — the kubelet watches the projected volume and requests a replacement from the TokenRequest API, so a long-running pod's token file is always current. Beside it sit ca.crt, which is how in-cluster clients verify the API server's TLS, and namespace, which is how code learns where it lives. Short-lived, audience-bound, ephemeral. That is the design.

The legacy mechanism is the one that still bites. Before 1.24, every service account automatically got a companion Secret of type kubernetes.io/service-account-token containing a JWT that never expires. KEP-2799 ended auto-creation in 1.24, but clusters upgraded across that boundary still carry those tokens, and they appear in kubectl get secrets output, etcd snapshots, and old backups. They are valid until the Secret object is deleted — no expiry, no rotation. If you find them:

kubectl get secrets -A --field-selector type=kubernetes.io/service-account-token

delete them or explicitly migrate the workload to bound tokens.

The one-line fix most workloads need

Here is the wrong mental model: "service accounts exist so pods can call the API." Reality: most workloads never call the Kubernetes API at all, and still get a token mounted by default — a live credential delivered to whatever ends up executing inside the container, including an attacker's webshell.

spec:
  automountServiceAccountToken: false

Set it on the pod spec or the ServiceAccount itself. Workloads that legitimately call the API get their own dedicated service account with a dedicated, narrow Role. The default service account in a namespace should bind nothing at all.

Need a token anyway? Mint one on demand instead of leaving one lying around:

kubectl create token ci-bot --audience api --duration 10m -n payments

That calls the TokenRequest API (serviceaccounts/token verb) — short TTL, chosen audience, nothing persisted to etcd. The --audience flag is the detail that matters for cloud exchange: the audience claim is what AWS STS or GCP validates, so the same mechanism that vends API-bound tokens also vends sts.amazonaws.com-bound ones.

When the workload needs cloud power

The harder case is AWS/GCP/Azure APIs. The pre-workload-identity answer was "put the AWS keys in a Secret" — a static credential exfiltratable by anyone who can read that namespace's secrets. The current answer exchanges the Kubernetes identity for a cloud identity:

  • EKS IRSA (IAM Roles for Service Accounts): the pod-identity-webhook mutates pods to inject AWS_ROLE_ARN and AWS_WEB_IDENTITY_TOKEN_FILE, and AWS STS swaps the projected SA token for temporary AWS creds via AssumeRoleWithWebIdentity against the cluster's OIDC issuer.
  • GKE Workload Identity: bind the Kubernetes SA to a Google SA with roles/iam.workloadIdentityUser; the metadata server vends Google creds.

Either way, no cloud key ever touches etcd. The blast radius shrinks from "anyone who can read this Secret owns our AWS account" to "whoever controls this one workload's identity."

Credential-harvesting campaigns — TeamTNT's Kubernetes variants are the documented case — enumerate mounted SA tokens and instance metadata first, precisely because that's where the pivotable identity lives. Take the token off the filesystem for workloads that don't need it, keep the grants narrow for those that do, and exchange rather than store for cloud access.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.