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.
Prerequisites
Section titled “Prerequisites”- 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
Configure SPIRE Server
Section titled “Configure SPIRE Server”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.
Register the SPIRE issuer as a Provider
Section titled “Register the SPIRE issuer as a Provider”Create one Provider per SPIRE issuer:
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.
Create a token credential
Section titled “Create a token credential”-
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 subjectThe 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_audienceOmitted, 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_conditionOptional. A CEL expression over the JWT-SVID’s claimsthat must evaluate totrue. You can usually omit it: only your SPIRE Server signs for this issuer, sosubjectpins the workload.For every claim in a JWT-SVID, see the JWT-SVID specification.
-
Copy the credential’s Client ID
The response’s
identifieris the credential’s Client ID. The workload sends it asclient_id. It is not a secret.
Configure the workload
Section titled “Configure the workload”-
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 = truejwt_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
emptyDirmounted at/var/run/secrets/keycard, or as a system service on a VM. -
Set environment variables
Variable Value KEYCARD_ISSUERYour Zone issuer URL KEYCARD_CLIENT_IDThe credential’s Client ID KEYCARD_RESOURCE_URLThe Resource the workload needs a token for -
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 asclient_id, in aclient_credentialsgrant:Terminal window pip install keycardai-oauthimport asyncioimport osfrom keycardai.oauth import AsyncClient, GrantTypefrom keycardai.oauth.server import FileTokenSourcesvid_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_tokenaccess_token = asyncio.run(get_access_token())Terminal window npm install @keycardai/oauthimport { ClientCredentialsClient } from "@keycardai/oauth";import { FileTokenSource } from "@keycardai/oauth/server";const svidSource = new FileTokenSource({ tokenFilePath: "/var/run/secrets/keycard/token" });const client = new ClientCredentialsClient(process.env.KEYCARD_ISSUER!);const response = await client.requestToken({clientAssertion: await svidSource.identityToken(),clientAssertionType: "urn:ietf:params:oauth:client-assertion-type:jwt-bearer",clientId: process.env.KEYCARD_CLIENT_ID!,resource: process.env.KEYCARD_RESOURCE_URL!,});const accessToken = response.accessToken;Terminal window go get github.com/keycardai/go-sdk/oauthsvidSource, err := oauth.NewFileTokenSource(oauth.WithFileTokenPath("/var/run/secrets/keycard/token"),)if err != nil {return err}svid, err := svidSource.IdentityToken(ctx)if err != nil {return err}client := oauth.NewClientCredentialsClient(os.Getenv("KEYCARD_ISSUER"))response, err := client.RequestToken(ctx, oauth.ClientCredentialsRequest{ClientID: os.Getenv("KEYCARD_CLIENT_ID"),ClientAssertion: svid,ClientAssertionType: "urn:ietf:params:oauth:client-assertion-type:jwt-bearer",Resource: os.Getenv("KEYCARD_RESOURCE_URL"),})if err != nil {return err}accessToken := response.AccessTokenTerminal window bundle add keycardai-oauthrequire "keycardai/oauth"svid = Keycardai::OAuth::FileTokenSource.new(token_file_path: "/var/run/secrets/keycard/token").identity_tokenclient = Keycardai::OAuth::ClientCredentialsClient.new(issuer: ENV.fetch("KEYCARD_ISSUER"))response = client.request_token(client_id: ENV.fetch("KEYCARD_CLIENT_ID"),client_assertion: svid,client_assertion_type: "urn:ietf:params:oauth:client-assertion-type:jwt-bearer",resource: ENV.fetch("KEYCARD_RESOURCE_URL"))access_token = response.access_tokenTerminal window curl -sS "$KEYCARD_ISSUER/oauth/2/token" \-d grant_type=client_credentials \-d client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer \--data-urlencode "client_assertion=$(cat /var/run/secrets/keycard/token)" \--data-urlencode "client_id=$KEYCARD_CLIENT_ID" \--data-urlencode "resource=$KEYCARD_RESOURCE_URL"
Verify
Section titled “Verify”Run the workload. The exchange returns an access token, and a credentials:issue event shows on the Application’s Activity tab in Keycard Console.
Troubleshooting
Section titled “Troubleshooting”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:
jq -R 'split(".")[1] | gsub("-";"+") | gsub("_";"/") | @base64d | fromjson | {iss, aud, sub}' \ < /var/run/secrets/keycard/tokensubdoesn’t match: the subject is the SPIFFE ID from the workload’s registration entry. Updatesubject, or use a trailing*for a path prefix.issdoesn’t match the Provider: setjwt_issuerinserver.confto 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-configurationmust be reachable from the public internet with a valid TLS certificate. audhas more than one value: Keycard needs exactly one. Remove extra audiences from the spiffe-helper config.- Wrong Client ID:
KEYCARD_CLIENT_IDisn’t the Client ID of a Workload Identity credential in this Zone, orKEYCARD_ISSUERnames 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.