Sub topic 4
Projected tokens: audience, expiry, and binding to the Pod
This chapter assumes you can apply YAML with kubectl, know what a ServiceAccount is, and have granted it a Role as in the previous chapters. Here you will see how modern Kubernetes issues the tokens a Pod uses to prove its identity.
Why tokens changed
Older clusters created a Secret holding a JWT (JSON Web Token) for every ServiceAccount. That token never expired, worked against any service, and stayed valid after the Pod was deleted. Anyone who copied it kept access.
Current clusters use the TokenRequest API instead. The API server issues a token that has three properties:
- Audience (
aud): the service the token is meant for. A service should reject tokens that do not name it. - Expiry (
exp): a limited lifetime. The kubelet refreshes the token before it runs out. - Binding: the token is tied to an object, usually the Pod. When the Pod is deleted, the token stops being valid.
Mounting a projected token
A projected volume with a serviceAccountToken source asks the kubelet to request a token for you and write it to a file.
apiVersion: v1
kind: Pod
metadata:
name: token-demo
namespace: demo
spec:
serviceAccountName: app-reader
containers:
- name: app
image: registry.example.com/app:1.0
volumeMounts:
- name: cloud-token
mountPath: /var/run/secrets/tokens
readOnly: true
volumes:
- name: cloud-token
projected:
sources:
- serviceAccountToken:
path: cloud-token
audience: sts.amazonaws.com
expirationSeconds: 3600The fields mean the following.
| Field | Meaning |
|---|---|
| `path` | File name inside the mount. Here: `/var/run/secrets/tokens/cloud-token`. |
| `audience` | Value placed in the `aud` claim. If omitted, it defaults to the API server's own audience. |
| `expirationSeconds` | Requested lifetime. The minimum is 600 seconds. The API server may shorten or cap the value. |
The kubelet starts refreshing the token when it is about 80% through its lifetime, or when it is older than 24 hours. It rewrites the file. Your application must therefore re-read the file periodically instead of reading it once at startup.
Requesting a token by hand
You can ask for a token without a Pod. This is useful for testing and for scripts.
kubectl auth can-i list configmaps -n demo --as=system:serviceaccount:demo:app-reader
kubectl auth can-i delete pods -n demo --as=system:serviceaccount:demo:app-reader
kubectl create token app-reader -n demo --audience=sts.amazonaws.comThe first command should print yes and the second no, given the Role from the previous chapter. The third prints a JWT with the sts.amazonaws.com audience. Without --audience it is issued for the API server.
To inspect a token, decode the middle section of the JWT. Look at these claims:
iss: the cluster's issuer URL.sub:system:serviceaccount:<namespace>:<name>.aud: the audience you requested.exp: the expiry time as a Unix timestamp.kubernetes.io: extra data, including the bound Pod name and UID.
What binding changes
Because the token records the Pod's UID, the API server checks that the Pod still exists on each use. Deleting the Pod invalidates the token at once, without waiting for exp. Compare this with a legacy Secret token, which lives until someone deletes the Secret or the ServiceAccount.
Exercise
A Pod reads its projected token once at startup and caches it. The Pod runs fine for the first hour and then calls begin failing with authentication errors. What is the cause, and what is the fix?
Show answer
The token has an expirationSeconds of 3600, so it expires after one hour. The kubelet refreshes the file on disk, but the application keeps using the old cached copy. Fix the application to re-read the token file before each request, or at least periodically.
Exercise
You run kubectl create token app-reader -n demo and send the result to a cloud provider that expects the audience sts.amazonaws.com. The provider rejects it. Why, and how do you correct it?
Show answer
Without --audience, the token's aud claim is for the Kubernetes API server, not the cloud provider. Re-run the command with --audience=sts.amazonaws.com.