Sub topic 6
Legacy secret-based tokens and why they are discouraged
This chapter assumes you know ServiceAccounts, RBAC bindings, and the projected tokens from the previous chapters. It covers the older token mechanism you may still find in existing clusters, and how to replace it.
How legacy tokens worked
Before Kubernetes 1.24, the control plane created a Secret of type kubernetes.io/service-account-token for every ServiceAccount. That Secret held a signed JWT, the cluster CA certificate, and the namespace. Pods received the Secret as a mounted volume.
Since 1.22, Pods get projected tokens instead. Since 1.24, the control plane no longer creates these Secrets automatically. Legacy tokens now exist in three situations:
- The cluster was upgraded from an older version, and the Secrets were never deleted.
- Someone created one by hand for a tool that wants a static credential, such as an external CI system.
- An old manifest or Helm chart still creates one.
Why they are discouraged
A legacy token has properties that projected tokens were designed to remove.
| Property | Legacy Secret token | Projected token |
|---|---|---|
| Lifetime | No expiry | Short, kubelet refreshes it |
| Bound to a Pod | No | Yes, invalid after the Pod is deleted |
| Audience | Only the API server | Custom, such as `sts.amazonaws.com` |
| Storage | Persisted in etcd | Not stored as an object |
| Revocation | Delete the Secret or ServiceAccount | Expires on its own |
The worst problem is the combination of no expiry and storage in etcd. Anyone who can read Secrets in the namespace, or who reads a backup or a log line containing the token, holds a permanent credential. It works until someone deletes the Secret.
Creating one deliberately
You may still need one, for example for a system that cannot request tokens. The control plane fills in the token for you:
apiVersion: v1
kind: Secret
metadata:
name: app-reader-legacy-token
namespace: demo
annotations:
kubernetes.io/service-account.name: app-reader
type: kubernetes.io/service-account-tokenAfter you apply it, the token appears in the token key of the Secret. If you delete the ServiceAccount, the control plane also removes this Secret.
Finding legacy tokens in a cluster
List every Secret of this type across all namespaces:
kubectl get secrets -A --field-selector type=kubernetes.io/service-account-tokenNewer clusters also track when the API server last saw a legacy token used. Look for the kubernetes.io/legacy-token-last-used label on each Secret. A Secret with no recent use is a good candidate for removal. From Kubernetes 1.29, the control plane can also mark long-unused legacy tokens as invalid and later delete them. Check your version's documentation for the exact behavior.
Replacing legacy tokens
- Identify which workload or tool uses the token. Check the Secret's last-used label and your CI configuration.
- For Pods in the cluster, remove the Secret and rely on the projected token. This needs no manifest change, because client libraries read the projected file at
/var/run/secrets/kubernetes.io/serviceaccount/token. - For clients outside the cluster, request a short-lived token with the TokenRequest API:
kubectl create token app-reader -n demo --duration=1h- Delete the old Secret, then confirm that nothing broke before removing similar ones.
If you must keep a long-lived token, give its ServiceAccount a minimal Role, store the Secret where few people can read it, and rotate it on a schedule.
Exercise
A CI system outside the cluster authenticates with a token from a kubernetes.io/service-account-token Secret that was created three years ago. Name two risks, and one way to reduce them.
Show answer
The token never expires, and it is stored in etcd, so anyone with Secret read access or a backup holds a permanent credential. It is also not bound to a Pod or a custom audience. To reduce the risk, have the CI system request short-lived tokens with the TokenRequest API (kubectl create token), or use workload identity federation, then delete the Secret.