Skip to main content

Workload identity federation

Workload identity federation lets a machine that already has an identity, such as a GitHub Actions job, a Kubernetes service account or a cloud function, get a Spanway virtual key without anyone storing one. The workload sends the OIDC token its platform gives it to the gateway, and the gateway answers with a virtual key that expires after a few minutes. It follows RFC 8693 OAuth 2.0 Token Exchange. There is no long-lived secret to leak, rotate or forget in a CI settings page.

It needs a Spanway enterprise license that includes the workload_identity feature. Without it the token route answers 403. If your services can send their JWT on every request instead, JWT authentication does that without minting keys. Token exchange is for clients that only know how to send a virtual key.

A real-world scenario​

A web team runs a deploy job on every merge to main that asks a model to write the release summary. Today the job reads a Spanway key from the repository secrets, and the security team wants that key gone. The admin adds a trust policy for the repository's main branch, and the job swaps the OIDC token GitHub Actions already gives it for a key that lives ten minutes. Nothing is stored, a fork cannot use it, and every key the job ever minted shows the policy that minted it in the audit log.

Steps​

  1. Add a trust policy under general_settings.workload_identity for the workload, naming the issuer, the audience the token must carry, the subjects you trust and the team the minted key belongs to. Nothing is trusted until a policy names its issuer.

    general_settings:
    workload_identity:
    token_ttl_seconds: 900
    trust:
    - name: deploy
    issuer: https://token.actions.githubusercontent.com
    jwks_url: https://token.actions.githubusercontent.com/.well-known/jwks
    audience: https://gateway.example.com
    subjects: ["repo:acme/web:ref:refs/heads/main", "repo:acme/web:environment:prod"]
    claims: {repository_owner: acme}
    team_id: ci-team
    models: [claude-sonnet]
    max_budget: 5
    token_ttl_seconds: 600
  2. Make sure the team exists. In the Admin UI open Teams and create ci-team if it is not there, or call POST /team/new. The exchange fails with server_error when the policy names a team that does not exist.

  3. Reload the config, or restart the gateway, so it reads the policy. The policies are read on every exchange, so a config reload is enough.

  4. In the workload, ask the platform for an OIDC token with the gateway's audience and post it to the token route. The route takes no API key, because the OIDC token is the credential.

    OIDC_TOKEN=$(curl -s -H "Authorization: bearer $ACTIONS_ID_TOKEN_REQUEST_TOKEN" \
    "$ACTIONS_ID_TOKEN_REQUEST_URL&audience=https://gateway.example.com" | jq -r .value)

    curl -s https://gateway.example.com/v1/workload-identity/token \
    -d grant_type=urn:ietf:params:oauth:grant-type:token-exchange \
    -d subject_token_type=urn:ietf:params:oauth:token-type:jwt \
    --data-urlencode subject_token="$OIDC_TOKEN"
  5. Use access_token from the answer as the virtual key until expires_in seconds have passed, then exchange a new token.

  6. Open Virtual Keys in the Admin UI. The Created By column is hidden by default, so turn it on from the column picker, or open the key and read Created By. The minted key shows workload_identity:deploy there, with the team, models, budget and expiry the policy gave it.

Expected result​

{
"access_token": "sk-...",
"issued_token_type": "urn:ietf:params:oauth:token-type:access_token",
"token_type": "Bearer",
"expires_in": 600
}

The job calls the gateway with Authorization: Bearer sk-... for the next ten minutes, spend lands on ci-team, and the key stops working when it expires. The response carries Cache-Control: no-store and is never cached.

Policy fields​

FieldMeaning
nameThe policy's name. It is recorded on every key it mints and can be sent as scope to pick this policy.
issuerThe exact iss the token must carry.
jwks_urlThe https address of the issuer's signing keys.
audienceThe aud the token must carry. Ask your platform for a token with this audience.
subjectsThe sub values that are trusted, as shell style patterns. * matches any run of characters, including :, ? matches one character and [abc] matches one of the listed characters. So repo:acme/web:ref:refs/heads/* trusts every branch of one repository, while repo:acme/web:* also trusts repo:acme/web:pull_request, the subject of pull request runs, which a fork can trigger. Keep patterns as narrow as the scenario needs.
claimsOther claims the token must carry with exactly these values.
team_idThe team the minted key belongs to, so it gets the team's models, budget and limits.
allowed_routesThe routes or route sets the minted key may call, such as ["llm_api_routes"].
modelsOptional models the key may call. Empty means the team's models.
max_budgetOptional spend limit of each minted key in USD.
token_ttl_secondsOptional lifetime of the key for this policy, 60 to 3,600. It can only be shorter than the gateway's token_ttl_seconds, which defaults to 900.

Every policy must name a team_id, an allowed_routes list or both, so a minted key is never broader than you chose.

The exchange request​

MethodPathSummary
POST/v1/workload-identity/tokenExchange a workload's OIDC token for a short-lived virtual key

The form fields follow RFC 8693. grant_type must be urn:ietf:params:oauth:grant-type:token-exchange, subject_token is the OIDC token, and subject_token_type is urn:ietf:params:oauth:token-type:jwt or urn:ietf:params:oauth:token-type:id_token. requested_token_type is optional and can only be the access token type. When more than one policy matches the same token, send scope with the name of the policy to use, otherwise the exchange is refused.

What is checked​

The gateway fails closed. It refuses the exchange with 400 and invalid_grant unless the token is signed by a key from the policy's jwks_url with an asymmetric algorithm (RS, PS or ES), its iss and aud match the policy, it carries exp, iat and sub, it has not expired (with 30 seconds of clock skew), and its subject and claims match the policy. Tokens signed with a shared secret or with no signature are refused. Error bodies use the OAuth error and error_description fields and never echo the token.

Audit and retention​

Every minted key gets a created audit row on the key table, written as workload_identity:<policy name>, with the event workload_identity_token_exchange, the policy, issuer, subject, team, the granted models, routes and budget, and the expiry, when audit logging is on. The key's metadata keeps the same policy, issuer and subject. Both rows record the address the request came from as source_ip. Every refused exchange is written to the gateway log with the reason and the issuer, quoted and cut to 200 characters. When the token names the issuer of one of your trust policies but is still refused, because its signature, audience, expiry, subject or claims do not match or the key could not be created, the refusal also writes a blocked audit row as workload_identity:unverified with the error code, the reason, the issuer and the caller's address, never the token, so the security team can spot a workload, or someone else, repeatedly trying tokens that look like yours. A request with a wrong form field, a token that is not a JWT or a token from an issuer no policy trusts writes no audit row, only the log line, so anonymous callers cannot fill the audit log with junk.

Minted keys are ordinary virtual keys that expire. An hourly job deletes them once they have been expired for longer than expired_key_retention_hours (24 by default, 0 deletes them at the next run, up to 8760). Only keys created by a workload identity policy are deleted: the key must name workload_identity:<policy> as its creator and carry the policy under workload_identity in its metadata. Keys made by people have neither, so they are never deleted. Both markers are reserved: /key/generate refuses a workload_identity entry in metadata and refuses callers whose user id starts with workload_identity:, and /key/update refuses to add the entry to a key that does not already carry it, each with a 400. A minted key keeps its marker through later updates. These keys are removed outright and are not moved to the deleted keys archive. Each deletion writes an audit row as workload_identity_cleanup, and the job runs on one instance at a time. Spend logs and audit rows for a deleted key are kept and follow their own retention settings.

general_settings:
workload_identity:
expired_key_retention_hours: 72

Errors​

What you seeWhyFix
403 naming the workload_identity feature.The license does not include the feature.Ask for a license with workload_identity, see enterprise.
400 invalid_grant, the subject token is not trusted by any workload identity policyNo policy names the token's iss, or the subject or claims match no policy.Compare the token's iss, sub and claims with the policies.
400 invalid_grant, the subject token failed validation: <reasons>A policy trusts the issuer but the token failed a check. The reasons are check names: InvalidAudienceError (wrong aud), ExpiredSignatureError (expired), InvalidIssuerError, MissingRequiredClaimError (no exp, iat or sub), InvalidAlgorithmError (a shared secret or no signature), MissingSubject or an unknown signing key.Request the token with the gateway's audience and a fresh expiry, and check the policy's jwks_url.
400 invalid_grant, more than one trust policy matches the subject token, name one in scopeMore than one policy trusts the token.Send scope=<policy name> in the form.
400 invalid_grant, trust policy <scope> does not match the subject tokenscope names a policy that does not trust this token.Send the right policy name, or leave scope out.
400 unsupported_grant_type, grant_type must be urn:ietf:params:oauth:grant-type:token-exchangegrant_type is not the token exchange URN.Send grant_type=urn:ietf:params:oauth:grant-type:token-exchange.
400 invalid_request, subject_token_type must be ... or requested_token_type can only be ...subject_token_type or requested_token_type is not one of the accepted URNs.Use the jwt or id_token subject type and leave requested_token_type out.
422 with a detail list naming subject_token, grant_type or subject_token_typeA form field is missing or empty. This answer is the gateway's standard validation body, not an OAuth error body.Send all three fields, with subject_token URL encoded.
500 server_error, the gateway could not create the keyThe policy names a team that does not exist, or the gateway has no database. The gateway log has the cause.Read the workload identity key for trust policy log line, then create the team or connect a database.
Every token exchange answers 500, and the hourly cleanup job logs a validation error naming workload_identity. The gateway itself still starts, because the policies are read when they are first used.A policy has no team_id or allowed_routes, two policies share a name, or jwks_url is not https.Fix the policy as the validation error says, then reload the config.

Logs​

LevelLineMeaningAction
WARNINGworkload identity token exchange refused: <code> (<description>) issuer='<issuer>'An exchange was refused. The issuer comes from the unverified token, so it is quoted and cut to 200 characters.Normal for a wrong token now and then. Many in a row from one issuer usually mean a policy or audience mismatch. Lines for issuers no policy trusts appear only here, never in the audit log.
WARNINGworkload identity key for trust policy <name> failed: <reason>The token was trusted but the key could not be created.Create the team the policy names, or fix the database connection.
INFOworkload identity: deleted <n> expired keysThe hourly cleanup removed expired minted keys.None. Change expired_key_retention_hours to keep them longer.

Settings​

The trust policies and both lifetimes live in the config file under general_settings.workload_identity, and the Admin UI has no page for them, see the settings reference. Only a platform admin who owns the config file should change them.