DEV Community

OnaEiuspkz
OnaEiuspkz

Posted on

Audience-bound Kubernetes tokens outside the cluster

Audience-bound Kubernetes tokens outside the cluster

A Kubernetes service account token is not just a long random string. Modern tokens can carry an audience, an expiration time, and a binding to the requesting object. Those fields change what the token is for. They do not automatically make every consumer outside the cluster safe, because the receiving service still has to validate the issuer, audience, signature, expiry, and the identity claim it accepts.

Kubernetes documents two broad paths for service account tokens. The TokenRequest API produces time-limited, audience-bound tokens, while legacy Secret-based tokens can be long-lived and lack the same properties. The service account documentation notes that a token may have an expiry time, an audience, and a start time depending on how it was issued. [1]

Bind the token to the workload

The service account token administrator documentation explains that bound tokens are associated with a specific Pod and have the API server as an audience. [2] That binding is valuable because a copied token should fail when the bound Pod no longer exists or when the target audience is wrong. The token is not a universal cluster password.

For workloads outside the cluster, do not request an API server token and send it to a random external service. Use a token whose audience is the external service or the workload identity provider, and make the receiving service verify that exact audience. A token minted for the Kubernetes API should not be accepted by a metrics collector, a CI job, or a partner API.

Review every consumer of the projected volume

Projected token volumes can be mounted into Pods with an audience and expiration policy. Check which containers can read the file, which library reads it, and where that library sends it. A sidecar with broad access to the Pod filesystem can read the token even when the main application does not use it. Debug endpoints, crash dumps, and telemetry exporters can copy the token into logs if the application includes the authorization header in diagnostics.

Do not mount a token into a Pod that does not need it. Set the audience to the narrowest service that will verify it. Use a short expiry and allow rotation rather than caching the token forever. If a static long-lived token remains for compatibility, put it on an explicit migration list with an owner and a removal date.

Validate more than the signature

The external service should validate the issuer URL, the signing keys, the audience, the expiration, and, where appropriate, the subject or namespace. Do not accept a token merely because it was signed by the cluster's issuer. A development cluster and a production cluster may share a signing service, so the issuer and audience combination matters.

Keep the token out of URLs and query strings. Use the Authorization header or a dedicated header that the proxy is configured to preserve. If an ingress or service mesh rewrites headers, verify that it does not strip the credential or forward it to a destination outside the trust boundary. Avoid X-Ray-Authorization style exceptions unless the proxy is explicitly designed for them.

Plan revocation and rotation

Deleting a Pod should invalidate a bound token that names that Pod. Update, suspend, or delete the service account when the workload changes. Rotate signing keys according to the cluster's key policy, and test that an old token is rejected after the rotation window. Do not assume that deleting a Secret is enough if a token was copied into an external cache.

For long-running external workers, use an identity federation flow that can refresh scoped credentials. A static token in a configuration file is difficult to revoke and easy to copy into a backup. If federation is not possible, isolate the token in a secret manager and give the worker a narrow API surface.

Build a cross-boundary inventory

List every service account token that leaves the cluster, the issuer, audience, subject, expiry, consumer, owner, and revocation path. Include CI systems, observability agents, backup jobs, and integration tests. A token used by a test job may have production reach if the issuer and audience are shared. Track that risk explicitly.

Run a failure test: stop the bound Pod, rotate the signing key, change the audience, and replay the token against the receiver. The expected result is a rejected request with a safe error. Verify that the rejection is logged without printing the token.

Audience binding makes a stolen token less reusable. It does not remove the need for the receiving service to enforce authorization. The cluster and the external service must agree on the identity, purpose, lifetime, and revocation model before the first request is accepted.

References

[1] Kubernetes, "Service Accounts." https://kubernetes.io/docs/concepts/security/service-accounts/

[2] Kubernetes, "Managing Service Accounts." https://kubernetes.io/docs/reference/access-authn-authz/service-accounts-admin/

Top comments (1)

Collapse
 
lycore profile image
Lycore Development •

The reminder that a token signed by the cluster issuer shouldn't be accepted unless the audience also matches is important, especially when dev and prod might share an issuer. Do you have a preferred way to keep the audience list narrow in practice, for example one projected token per external service rather than reusing a single token across several?