Skip to content
Application identity

Configure GCP Workload Identity

Authenticate Google Cloud workloads to Keycard with a service account identity token.

A Google Cloud workload gets a short-lived ID token for its service account from the metadata server, 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 Google’s public keys, then checks its audience, subject, and claims. This works on Compute Engine, Cloud Run, Cloud Run functions, App Engine, and GKE pods linked to a Google service account.

  • Familiarity with the concepts in Workload Identity, and with Google Cloud’s ID token 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 dedicated service account for the workload, and its unique ID: gcloud iam service-accounts describe <service-account-email> --format='value(uniqueId)'

Create one Provider per Zone for Google’s issuer. Every Google Cloud project shares it. If the Zone already has one, for example for Google sign-in, use it.

Terminal window
keycard agent api /zones/<zone-id>/providers --org <org-id> -X POST -d '{
"name": "Google Cloud",
"identifier": "https://accounts.google.com",
"protocols": {
"oauth2": {
"issuer": "https://accounts.google.com"
}
}
}'

Keycard discovers Google’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": "<google-provider-id>",
    "subject": "<service-account-unique-id>",
    "authentication_condition": "claims.email == \"<service-account-email>\""
    }'
    Field Value
    subject The service account’s unique ID. It is numeric, globally unique, and never reused.
    allowed_audience Omitted, so it defaults to your Zone issuer URL. The workload requests its token with that audience.
    authentication_condition Pins the service account’s email, so the credential names a readable account.

    For an example token and every claim Google includes, see Google’s instance identity token contents.

  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 the workload as the service account

    Attach the service account to the Compute Engine instance or Cloud Run service. On GKE, bind the pod’s Kubernetes service account to it. The metadata server issues tokens for this service account.

  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. Request an ID token and exchange it

    Request an ID token from the metadata server with your Zone issuer URL as the audience. Then 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 GCPMetadataTokenSource
    async def get_access_token() -> str:
    issuer = os.environ["KEYCARD_ISSUER"]
    gcp_token = await GCPMetadataTokenSource(audience=issuer).identity_token()
    async with AsyncClient(issuer) as client:
    response = await client.client_credentials_grant(
    client_assertion=gcp_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 audience, subject, 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 credential:

Terminal window
curl -sSfG -H "Metadata-Flavor: Google" --data-urlencode "audience=$KEYCARD_ISSUER" -d format=full \
"http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/identity" \
| jq -R 'split(".")[1] | gsub("-";"+") | gsub("_";"/") | @base64d | fromjson | {aud, sub, email}'
  • sub doesn’t match: the workload runs as a different service account, often the Compute Engine default. Attach the dedicated service account. Don’t set subject to the Compute Engine default service account: every workload without its own account runs as it.
  • email is missing: the condition fails. Decode the token to check its claims, or remove the condition and rely on subject.
  • Metadata server unreachable: the workload isn’t running on Google Cloud. The metadata server is only available inside Google Cloud.
  • Metadata server returns an error on GKE: the Kubernetes service account has no iam.gke.io/gcp-service-account annotation. See step 1.