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:
-
The
adminrole in your Limrun organization. -
The service account your workload runs as, and its unique numeric ID. Print the ID with:
gcloud iam service-accounts describe SERVICE_ACCOUNT_EMAIL --format='value(uniqueId)'
Create the provider
Google signs ID tokens for every service account in every project, so the provider condition must name your service account.
-
Open console.limrun.com, go to Settings, then Workload identity federation, and click the + button next to Identity providers.
-
In Issuer, choose Google Cloud Platform (GCP). The preset fills in the issuer
https://accounts.google.comand its signing keys athttps://www.googleapis.com/oauth2/v3/certs. -
Keep Token audience as
limrun. The workload requests its token with this audience. -
In Provider condition (CEL), replace
SERVICE_ACCOUNT_IDwith 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. -
Click Create provider.
Create a binding
-
In the provider's card, click the + button next to Bindings.
-
Enter a name and a Claim condition (CEL). The provider condition already selects the service account, so
trueadmits it. On Compute Engine, a token requested withformat=fullalso carries the instance's project, zone, and ID, so you can narrow by them:assertion.google.compute_engine.project_id == 'my-project' -
Enter the permissions the workload needs, one per line, such as
xcode:*:all. -
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 whoamiaudience=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=limrunThe 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.
Let workloads on Google Cloud call Limrun with their service account's ID token instead of a stored API key.
Was this guide helpful?