Skip to content
Application identity

Configure Azure Workload Identity

Authenticate Azure workloads to Keycard with a managed identity token.

An Azure workload gets a short-lived Microsoft Entra ID token for its managed identity. 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 your tenant’s public keys, then checks its audience and claims. This works on Azure VMs, App Service, Azure Functions, Container Apps, and other services with a managed identity.

  • Familiarity with the concepts in Workload Identity, and with Azure’s managed identities 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>
  • A managed identity attached to the workload, and its principal (object) ID: az identity show --resource-group <resource-group> --name <identity-name> --query principalId -o tsv for a user-assigned identity. The samples don’t name an identity, so they use the workload’s system-assigned identity when it has one. To use a user-assigned identity, pass its client ID when you request the token.
  • Permission to register applications in your Entra tenant, and your tenant ID

Register an Entra application to act as Keycard’s audience, so no other service accepts these tokens, and set it to issue v2.0 tokens:

Terminal window
APP_ID=$(az ad app create --display-name "Keycard workload audience <zone-name>" --query appId -o tsv)
az ad app update --id "$APP_ID" \
--identifier-uris "api://$APP_ID" \
--set api.requestedAccessTokenVersion=2
az ad sp create --id "$APP_ID"

$APP_ID is the app’s client ID. v2.0 tokens carry it as aud and are issued by https://login.microsoftonline.com/<tenant-id>/v2.0.

Create one Provider per Entra tenant:

Terminal window
keycard agent api /zones/<zone-id>/providers --org <org-id> -X POST -d '{
"name": "Entra ID <tenant-id>",
"identifier": "https://login.microsoftonline.com/<tenant-id>/v2.0",
"protocols": {
"oauth2": {
"issuer": "https://login.microsoftonline.com/<tenant-id>/v2.0"
}
}
}'

Keycard discovers the tenant’s signing keys from the issuer.

  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": "<entra-provider-id>",
    "allowed_audience": "<app-client-id>",
    "authentication_condition": "claims.oid == \"<managed-identity-principal-id>\""
    }'
    Field Value
    subject Omitted. Entra can issue a different sub for the same identity in each application. Pinning oid, the identity’s object ID, is safer and works for managed identities.
    allowed_audience The Entra app’s client ID. Entra sets aud to it, not to your Zone issuer URL.
    authentication_condition Pins the managed identity’s principal ID. oid is immutable and never reused.

    For every claim in an Entra access token, see Microsoft’s access token claims reference.

  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. Set environment variables

    Set these on the workload, for example in the App Service or Container Apps configuration:

    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
  2. Request a managed identity token and exchange it

    Request a token for api://<app-client-id>. Then send it as client_assertion, with the credential’s Client ID as client_id, in a client_credentials grant:

    Terminal window
    pip install azure-identity keycardai-oauth
    import os
    from azure.identity import ManagedIdentityCredential
    from keycardai.oauth import Client, GrantType
    azure_token = ManagedIdentityCredential().get_token("api://<app-client-id>/.default").token
    with Client(os.environ["KEYCARD_ISSUER"]) as client:
    response = client.client_credentials_grant(
    client_assertion=azure_token,
    client_assertion_type=GrantType.JWT_BEARER_CLIENT_ASSERTION,
    client_id=os.environ["KEYCARD_CLIENT_ID"],
    resource=os.environ["KEYCARD_RESOURCE_URL"],
    )
    access_token = response.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 issuer, audience, or condition doesn’t match. The Audit Log records details on the failure. Decode a token from the workload to compare its claims against the Provider and credential:

Terminal window
# On an Azure VM. Elsewhere, print the token your code requests.
AZURE_TOKEN=$(curl -sSfG -H "Metadata: true" \
-d api-version=2018-02-01 \
--data-urlencode "resource=api://<app-client-id>" \
"http://169.254.169.254/metadata/identity/oauth2/token" | jq -r .access_token)
echo "$AZURE_TOKEN" \
| jq -R 'split(".")[1] | gsub("-";"+") | gsub("_";"/") | @base64d | fromjson | {iss, aud, oid, ver}'
  • ver is 1.0: iss is https://sts.windows.net/<tenant-id>/ and aud is api://<app-client-id>. Set api.requestedAccessTokenVersion to 2 on the Entra app. A workload that already has a v1.0 token can keep getting it for up to 24 hours.
  • oid doesn’t match: the workload used a different identity, often the system-assigned identity instead of a user-assigned one. Pass the user-assigned identity’s client ID when requesting the token.
  • AADSTS500011 or AADSTS50001 when requesting the token: the Entra app has no service principal in the tenant. Run az ad sp create --id <app-client-id>.