Skip to content
Application identity

Configure Kubernetes Workload Identity

Authenticate Kubernetes pods to Keycard with a projected service account token.

A pod gets a short-lived service account token from Kubernetes, with your Zone as its audience. It sends the token to Keycard along with the Client ID of the Application credential it authenticates as. Keycard checks the token’s signature with the cluster’s public keys, then checks its audience, subject, and claims. This works for managed clusters, such as EKS, GKE, and AKS, and for private clusters that Keycard can’t reach.

  • Familiarity with the concepts in Workload Identity
  • A Keycard Zone and an Application for the workload
  • Your Zone issuer URL, from Settings → Connection in Keycard Console
  • Kubernetes 1.22 or later, and kubectl 1.24 or later with access that can read the cluster’s service account issuer discovery endpoints, such as a cluster admin
  • The Keycard CLI, signed in with keycard auth signin --org <org-id>
  • jq

Read the cluster’s issuer and signing keys

Section titled “Read the cluster’s issuer and signing keys”
Terminal window
CLUSTER_ISSUER=$(kubectl get --raw /.well-known/openid-configuration | jq -r .issuer)
kubectl get --raw /openid/v1/jwks > jwks.json

The issuer is the API server’s --service-account-issuer, and every service account token carries it as iss. jwks.json holds the public keys the API server verifies service account tokens with. It holds no private key material.

Create one Provider per cluster. If Keycard can reach the cluster’s issuer, as on EKS, GKE, and AKS with the OIDC issuer turned on, set only the issuer, for example in Keycard Console. Keycard then reads the current keys from it. For a cluster Keycard can’t reach, store the cluster’s public keys on the Provider as jwks:

Terminal window
jq -n --arg issuer "$CLUSTER_ISSUER" --slurpfile jwks jwks.json '{
name: "Kubernetes <cluster-name>",
identifier: "kubernetes-<cluster-name>",
protocols: { oauth2: { issuer: $issuer, jwks: $jwks[0] } }
}' > provider.json
keycard agent api /zones/<zone-id>/providers --org <org-id> -X POST < provider.json
Field Value
identifier Any value unique in the Zone. The issuer works if no other cluster in the Zone shares it. Clusters built with kubeadm or kind all default to https://kubernetes.default.svc.cluster.local, so give each its own identifier.
protocols.oauth2.issuer The cluster’s issuer. A token’s iss must match it exactly.
protocols.oauth2.jwks The cluster’s public keys, from jwks.json. Keycard verifies tokens with these keys only.

The response’s id is the Provider ID for the credential below.

  1. Add the credential to the Application

    Terminal window
    keycard agent api /zones/<zone-id>/application-credentials --org <org-id> -X POST -d '{
    "type": "token",
    "version": 2,
    "application_id": "<application-id>",
    "provider_id": "<cluster-provider-id>",
    "subject": "system:serviceaccount:<namespace>:<service-account>",
    "authentication_condition": "claims[\"kubernetes.io\"].serviceaccount.uid == \"<service-account-uid>\""
    }'
    Field Value
    subject The pod’s service account. Match exactly, or end with * for a prefix match: system:serviceaccount:<namespace>:* trusts every service account in the namespace.
    allowed_audience Omitted, so it defaults to your Zone issuer URL. The pod requests its token with that audience.
    authentication_condition Pins the service account’s UID: kubectl get serviceaccount <service-account> -n <namespace> -o jsonpath='{.metadata.uid}'. A service account deleted and recreated with the same name gets a new UID.

    Kubernetes-specific claims sit under the kubernetes.io key, including namespace, serviceaccount.name, serviceaccount.uid, pod.name, pod.uid, and node.name. For the full claim schema and an example token, see Kubernetes’ service account private claims.

  2. Copy the credential’s Client ID

    The response’s identifier is the credential’s Client ID. The workload sends it as client_id. It is not a secret.

  1. Mount a projected token with the Keycard audience

    apiVersion: v1
    kind: Pod
    metadata:
    name: <pod-name>
    namespace: <namespace>
    spec:
    serviceAccountName: <service-account>
    containers:
    - name: app
    image: <image>
    env:
    - name: KEYCARD_ISSUER
    value: <keycard-issuer-url>
    - name: KEYCARD_CLIENT_ID
    value: <credential-id>
    - name: KEYCARD_RESOURCE_URL
    value: <resource-url>
    volumeMounts:
    - name: keycard-token
    mountPath: /var/run/secrets/keycard
    readOnly: true
    volumes:
    - name: keycard-token
    projected:
    sources:
    - serviceAccountToken:
    audience: <keycard-issuer-url>
    expirationSeconds: 3600
    path: token

    The kubelet refreshes the token before it expires. The minimum expirationSeconds is 600.

  2. Read the token and exchange it

    Read the token file on each request so rotated tokens are picked up. Send it as client_assertion, with the credential ID as client_id, in a client_credentials grant:

    Terminal window
    pip install keycardai-oauth
    import asyncio
    import os
    from keycardai.oauth import AsyncClient, GrantType
    from keycardai.oauth.server import FileTokenSource
    token_source = FileTokenSource(token_file_path="/var/run/secrets/keycard/token")
    async def get_access_token() -> str:
    async with AsyncClient(os.environ["KEYCARD_ISSUER"]) as client:
    response = await client.client_credentials_grant(
    client_assertion=await token_source.identity_token(),
    client_assertion_type=GrantType.JWT_BEARER_CLIENT_ASSERTION,
    client_id=os.environ["KEYCARD_CLIENT_ID"],
    resource=os.environ["KEYCARD_RESOURCE_URL"],
    )
    return response.access_token
    access_token = asyncio.run(get_access_token())

Run the workload. The exchange returns an access token, and a credentials:issue event shows on the Application’s Activity tab in Keycard Console.

Keycard returns invalid_client with Invalid client assertion when the signature, issuer, audience, subject, or condition doesn’t match. The Audit Log records details on the failure. Decode the token to compare its claims against the Provider and credential:

Terminal window
kubectl exec <pod-name> -n <namespace> -- cat /var/run/secrets/keycard/token \
| jq -R 'split(".")[1] | gsub("-";"+") | gsub("_";"/") | @base64d | fromjson | {iss, aud, sub, uid: .["kubernetes.io"].serviceaccount.uid}'
  • iss doesn’t match the Provider: the Provider’s issuer differs from the cluster’s. Compare it with kubectl get --raw /.well-known/openid-configuration | jq -r .issuer. When kube-apiserver has several --service-account-issuer flags, the first one signs tokens.
  • Signature fails after key rotation: the Provider’s key set doesn’t hold the key that signed the token. Read the cluster’s keys again and update the Provider’s jwks with all of them.
  • uid doesn’t match: the service account was recreated. Update the condition with the new UID.

Creating or updating the Provider returns 400 when Keycard can’t verify with a key, such as an RSA key under 2048 bits or a key that holds private key material. The error message names the first key it rejects.