Llim.run

Authenticate Google Cloud workloads with workload identity federation

A workload on Google Cloud can get an ID token for its service account and exchange it for a short-lived Limrun access token. No Limrun API key is stored in your project.

Before you start

You need:

Create the provider

Google signs ID tokens for every service account in every project, so the provider condition must name your service account.

  1. Open console.limrun.com, go to Settings, then Workload identity federation, and click the + button next to Identity providers.

  2. In Issuer, choose Google Cloud Platform (GCP). The preset fills in the issuer https://accounts.google.com and its signing keys at https://www.googleapis.com/oauth2/v3/certs.

  3. Keep Token audience as limrun. The workload requests its token with this audience.

  4. In Provider condition (CEL), replace SERVICE_ACCOUNT_ID with the service account's unique ID:

    assertion.sub == 'SERVICE_ACCOUNT_ID'

    Google puts the service account's unique ID in sub. Use it rather than the email: a service account deleted and created again with the same name gets a new ID.

  5. Click Create provider.

Create a binding

  1. In the provider's card, click the + button next to Bindings.

  2. Enter a name and a Claim condition (CEL). The provider condition already selects the service account, so true admits it. On Compute Engine, a token requested with format=full also carries the instance's project, zone, and ID, so you can narrow by them:

    assertion.google.compute_engine.project_id == 'my-project'
  3. Enter the permissions the workload needs, one per line, such as xcode:*:all.

  4. Click Create binding, expand Setup, and copy the binding ID.

Use the token in the workload

On Compute Engine, Cloud Run, and Cloud Build, the metadata server returns an ID token for the attached service account. The tokens are valid for one hour, and a Limrun access token never outlives its source token. This script keeps a fresh token in a file, which the CLI reads again whenever it renews:

export LIMRUN_WORKLOAD_IDENTITY_BINDING_ID=binding_REPLACE_WITH_BINDING_ID
export LIMRUN_WORKLOAD_IDENTITY_TOKEN_FILE=/tmp/limrun.jwt

refresh() {
  curl -sS --fail-with-body -H "Metadata-Flavor: Google" \
    'http://metadata/computeMetadata/v1/instance/service-accounts/default/identity?audience=limrun' \
    > "$LIMRUN_WORKLOAD_IDENTITY_TOKEN_FILE.tmp"
  mv "$LIMRUN_WORKLOAD_IDENTITY_TOKEN_FILE.tmp" "$LIMRUN_WORKLOAD_IDENTITY_TOKEN_FILE"
}
refresh
(while sleep 1800; do refresh; done) &

lim whoami

audience=limrun must match the provider's audience. Add &format=full on Compute Engine to include the instance claims. lim whoami prints your organization and the binding's permissions.

In code, fetch the token from the same metadata URL in a token function, and pass it as the TypeScript SDK's workloadToken, the Python SDK's workload_token, or the Go SDK's TokenSource.

Outside Google Cloud

From a machine with gcloud credentials, such as a CI runner, impersonate the service account to print a token:

gcloud auth print-identity-token \
  --impersonate-service-account=SERVICE_ACCOUNT_EMAIL \
  --audiences=limrun

The account running gcloud needs the Service Account OpenID Connect Identity Token Creator role (roles/iam.serviceAccountOpenIdTokenCreator) on that service account. Pass the output as LIMRUN_WORKLOAD_IDENTITY_TOKEN, or write it to the token file as above.

On GKE, you can also use the cluster's own service account tokens. See Kubernetes.

See Google's ID token guide for the Google side.