All guides

Service account tokens in Kubernetes: bound and short-lived

A service account token stored in a Secret does not expire and does not rotate. A token from the TokenRequest API expires, carries an audience and is bound to an object such as a pod. Pods get the second kind by default.

A person holding a phone above a laptop keyboard

Two kinds of token

Secret-based tokens are the legacy mechanism. The token is stored in a Secret of type kubernetes.io/service-account-token. These tokens do not expire and do not rotate. In versions before v1.24, Kubernetes created a permanent token automatically for each service account. The documentation no longer recommends this method, because of the risks of static, long-lived credentials: once such a token is disclosed, it can be misused.

Bound tokens come from the TokenRequest API, stable since Kubernetes v1.22. They are signed JSON Web Tokens. When one is presented, the API server checks its signature, its expiry, the object references in its claims and its audience.

A token can be bound to a Pod, a Secret or a Node. The token then stores the object's name and UID as claims. If the referenced object no longer exists, the request is not authenticated.

What a pod gets by default

By default, the ServiceAccount admission controller adds a projected volume to a new pod and mounts it at /var/run/secrets/kubernetes.io/serviceaccount. The volume has three sources: the token, a ConfigMap with the certificate authority bundle, and the name of the pod's namespace.

The kubelet fetches the token with the TokenRequest API. The token is bound to the pod and has the API server as its audience. Its life ends with the pod's deletion or at the end of a defined lifespan, 1 hour unless you change it. The kubelet refreshes the token before it expires.

For projected tokens, the kubelet requests rotation when the token is older than 80% of its time to live, or older than 24 hours. Reloading the token after a rotation is the application's job. The documentation gives loading it on a schedule, for example once every 5 minutes, as one way to do that.

Tokens for another audience

The audience and the validity duration are not configurable on the default token. To set them, project a token yourself. This is the documented example, a token with the audience vault that is valid for two hours:

YAML

apiVersion: v1
kind: Pod
metadata:
  name: nginx
spec:
  containers:
  - image: nginx
    name: nginx
    volumeMounts:
    - mountPath: /var/run/secrets/tokens
      name: vault-token
  serviceAccountName: build-robot
  volumes:
  - name: vault-token
    projected:
      sources:
      - serviceAccountToken:
          path: vault-token
          expirationSeconds: 7200
          audience: vault

expirationSeconds is 1 hour by default, and 10 minutes is the lowest value it accepts. A recipient of a token must identify itself with an identifier named in the token's audience, and otherwise should reject the token.

The token becomes invalid against the API when the pod or the ServiceAccount is deleted. A service that validates the JWT offline does not see that: for it, the token stays valid until it expires. The documentation says that clients who need assurance about the bound claims must use the TokenReview API.

Pods that never call the API

The application security checklist says to set automountServiceAccountToken to false on every pod that has no specific need to reach the Kubernetes API. The documented example sets it on the ServiceAccount:

YAML

apiVersion: v1
kind: ServiceAccount
metadata:
  name: build-robot
automountServiceAccountToken: false

The same field exists in the pod spec. If both are set, the pod spec takes precedence.

Tokens for people and scripts

kubectl create token requests a time-limited token through the TokenRequest API. Its reference page shows these commands:

Shell

kubectl create token myapp
kubectl create token myapp --duration 10m
kubectl create token myapp --audience https://example.com
kubectl create token myapp --bound-object-kind Secret --bound-object-name mysecret

The lifetime you request with --duration is a request only: the server may issue a token that lives longer or shorter. The supported kinds for --bound-object-kind are Node, Pod and Secret.

Kubernetes keeps no central record of the tokens it issues, which means a single short-lived token cannot be revoked. Deleting and re-creating the ServiceAccount changes its UID and invalidates all tokens that were created for it. Deleting a pod expires the bound tokens of that pod.

How to find old Secret-based tokens

A legacy token is a Secret of type kubernetes.io/service-account-token. This pipeline lists them with their last-used date:

Shell

kubectl get secrets --all-namespaces -o json | jq -r '.items[]
  | select(.type == "kubernetes.io/service-account-token")
  | [.metadata.namespace, .metadata.name,
     .metadata.labels["kubernetes.io/legacy-token-last-used"] // "-"]
  | @tsv'

The control plane sets the label kubernetes.io/legacy-token-last-used to the date it last saw a request authenticated with the token. The label is absent if the token was last used before the cluster gained this tracking, which was added in v1.26. A second label, kubernetes.io/legacy-token-invalid-since, marks an auto-generated token as invalid for authentication.

Kubernetes treats a token Secret as auto-generated when the secrets field of its ServiceAccount references it. Beginning with v1.29, the control plane marks such a token invalid if it stays unused for a period that defaults to one year. If it stays unused for that period again, the control plane purges it. For Secrets you create by hand, the documentation says not to reference them in that secrets field, or they are cleaned up the same way.

To invalidate a legacy token yourself, delete its Secret:

Shell

kubectl delete secret name-of-secret

Cleanup steps

  1. List the Secret-based tokens and their last-used dates.
  2. Move external callers to short-lived tokens. The Kubernetes project recommends against a long-lived token in a Secret for applications outside the cluster, and names TokenRequest as a way to obtain short-lived tokens for them.
  3. Delete the Secrets that nothing uses.
  4. Set automountServiceAccountToken: false where pods do not need the Kubernetes API.
  5. Follow the checklist on identities: avoid the default ServiceAccount and create a ServiceAccount for each workload or microservice.

Sources

Want a second pair of eyes on yours? We do this work for a living.

Book a call

More guides