How to verify Kubernetes service account token JWT
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Kubernetes service accounts provide an identity for pods that need to interact with the Kubernetes API. Tokens associated with these accounts are typically JSON Web Tokens (JWTs), which are industry-standard tokens that convey claims and are signed for integrity. Verifying a Kubernetes service account token involves confirming its signature, validity, and claims, effectively ensuring trust and security when dealing with such tokens.
Understanding Kubernetes Service Account Tokens
What are Service Account Tokens?
In Kubernetes, a service account is used to provide an identity for processes that run in a pod. Each service account has an associated secret that holds the account's token, which is a JWT. This token can be used to authenticate with the Kubernetes API.
- Format:
- Tokens are JWTs, formatted as ``
<header>``.``<payload>``.``<signature>``. - The header specifies the type of token and the signing algorithm.
- The payload typically includes the issuer, the subject (the service account), and the audience (Kubernetes API server).
- The signature is used to verify the token's integrity.
- Location:
- Tokens are automatically mounted in pods at
/var/run/secrets/kubernetes.io/serviceaccount/token.
Components of a JWT
- Header:
- iss: Issuer of the token.
- sub: Subject, usually in the form
system:serviceaccount:``<namespace>``:``<serviceaccount>``. - aud: Audience, usually the API server URL.
- exp: Expiration time.
- Verifies the sender, using the issuer's private key and helps in preventing tampering.
- Use the public key from the Kubernetes API server to verify the token's signature.
- The public key is provided by the service account issuer, typically through an endpoint such as an OpenID Connect provider configuration (
/.well-known/openid-configuration). - Issuer (
iss): Verify it matches the expected issuer URI. - Audience (
aud): Ensure it includes the intended audience, usually the API server. - Expiration (
exp): Check if the token hasn't expired. - Fetch the OpenID Configuration to retrieve the public keys.
- Parse the
jwks_urifrom configuration and fetch the JSON Web Key Sets (JWKS) used for signature verification. - Utilize libraries like
jwt-goin Golang,PyJWTin Python, orjsonwebtokenin Node.js for JWT parsing and verification.
Related reading
- How to view logs of failed jobs with kubectl?
- How to view the permissions/roles associated with a specific service account in k8s?
- How to write a chart for imagePullSecret from gcr
- How to write a kubernetes pod configuration to start two containers
- Http Basic Authentication in Java using HttpClient?
- HttpClient and using proxy - constantly getting 407
- How would I set up access to multiple Nodes with a single Service in kubernetes?
- HPA creates more pods than expected

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.