Skip to content
Application identity

Configure SPIFFE Workload Identity

Authenticate SPIFFE workloads to Keycard with a JWT-SVID.

A SPIFFE workload gets a short-lived JWT-SVID from the SPIRE Agent, 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 its signature with the keys your SPIRE OIDC Discovery Provider publishes, then checks its audience, subject, and claims. Keycard accepts JWT-SVIDs, not X.509-SVIDs.

  • Familiarity with the concepts in Workload Identity, and with SPIFFE’s concepts docs
  • A Keycard Zone and an Application for the workload
  • Your Zone issuer URL, from Settings → Connection in Keycard Console
  • The Keycard CLI, signed in with keycard auth signin --org <org-id>
  • SPIRE Server and Agent, with a registration entry for the workload and its SPIFFE ID
  • The SPIRE OIDC Discovery Provider, reachable from the public internet over HTTPS

In server.conf, set an issuer that matches the OIDC Discovery Provider’s public URL:

server {
trust_domain = "<trust-domain>"
jwt_issuer = "https://<oidc-discovery-domain>"
}

<oidc-discovery-domain> must appear in the OIDC Discovery Provider’s domains list. The issuer and the trust domain are independent: the trust domain scopes the SPIFFE ID, and the issuer is where Keycard fetches signing keys.

Create one Provider per SPIRE issuer:

Terminal window
keycard agent api /zones/<zone-id>/providers --org <org-id> -X POST -d '{
"name": "SPIRE <trust-domain>",
"identifier": "https://<oidc-discovery-domain>",
"protocols": {
"oauth2": {
"issuer": "https://<oidc-discovery-domain>"
}
}
}'

Keycard discovers the signing keys from https://<oidc-discovery-domain>/.well-known/openid-configuration and picks up rotated keys automatically.

  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": "<spire-provider-id>",
    "subject": "spiffe://<trust-domain>/<workload-path>"
    }'
    Field Value
    subject The workload’s SPIFFE ID. Match exactly, or end with * for a prefix match: spiffe://<trust-domain>/ns/<namespace>/* trusts every workload under that path.
    allowed_audience Omitted, so it defaults to your Zone issuer URL. The workload requests its JWT-SVID with that audience, and it must be the only one.
    authentication_condition Optional. A CEL expression over the JWT-SVID’s claims that must evaluate to true. You can usually omit it: only your SPIRE Server signs for this issuer, so subject pins the workload.

    For every claim in a JWT-SVID, see the JWT-SVID specification.

  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. Run spiffe-helper

    spiffe-helper fetches a JWT-SVID from the SPIRE Agent, writes it to a file, and refreshes it before it expires. Configure it to request your Zone issuer URL as the audience:

    agent_address = "<spire-agent-socket>"
    cert_dir = "/var/run/secrets/keycard"
    daemon_mode = true
    jwt_svids = [{ jwt_audience = "<keycard-issuer-url>", jwt_svid_file_name = "token" }]

    Run it alongside the workload: as a sidecar in Kubernetes, sharing a memory-backed emptyDir mounted at /var/run/secrets/keycard, or as a system service on a VM.

  2. Set environment variables

    Variable Value
    KEYCARD_ISSUER Your Zone issuer URL
    KEYCARD_CLIENT_ID The credential’s Client ID
    KEYCARD_RESOURCE_URL The Resource the workload needs a token for
  3. Read the JWT-SVID and exchange it

    Read the token file on each request so rotated JWT-SVIDs are picked up. Send it as client_assertion, with the credential’s Client 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
    svid_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 svid_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, audience, subject, or condition doesn’t match. The Audit Log records details on the failure. Decode the JWT-SVID to compare its claims against the Provider and credential:

Terminal window
jq -R 'split(".")[1] | gsub("-";"+") | gsub("_";"/") | @base64d | fromjson | {iss, aud, sub}' \
< /var/run/secrets/keycard/token
  • sub doesn’t match: the subject is the SPIFFE ID from the workload’s registration entry. Update subject, or use a trailing * for a path prefix.
  • iss doesn’t match the Provider: set jwt_issuer in server.conf to the OIDC Discovery Provider’s public URL, and use that URL as the Provider’s issuer.
  • Keycard can’t fetch signing keys: https://<oidc-discovery-domain>/.well-known/openid-configuration must be reachable from the public internet with a valid TLS certificate.
  • aud has more than one value: Keycard needs exactly one. Remove extra audiences from the spiffe-helper config.
  • Wrong Client ID: KEYCARD_CLIENT_ID isn’t the Client ID of a Workload Identity credential in this Zone, or KEYCARD_ISSUER names another Zone.
  • No token file: run spire-agent api fetch jwt -audience "$KEYCARD_ISSUER" -socketPath <spire-agent-socket>. If it fails, check the workload’s registration entry.