Keeping Filters and Sorting Consistent Across Pages
When a client reads many records from an API, the server returns them in pages instead of one large response. A filter…
A program outside Google Cloud needs two things to call a Cloud Run service protected by Identity and Access Management (IAM): a network path to the service and a Google-signed OpenID Connect (OIDC) ID token. An ID token is a short-lived credential that identifies the caller and names the service it is meant for. Cloud Run checks the token, then asks IAM whether that identity has permission to call the service. Cloud Run’s authentication overview describes these checks.
For production callers, such as continuous integration (CI) jobs or workloads on another cloud, use Workload Identity Federation (WIF). WIF lets an external identity provider establish the caller’s identity in Google Cloud without distributing a long-lived service-account key. The external caller then requests an ID token for a Google service account (an identity meant for software) that has permission to call the Cloud Run service. You can use a local gcloud command for development and testing, but do not use a developer’s general-purpose user token in production. Google’s WIF guidance explains how an external identity can access Google Cloud.
Cloud Run checks whether the caller has the Cloud Run Invoker role, roles/run.invoker, which grants permission to call the service receiving the request. Grant that role to the identity represented by the request’s token, usually a dedicated service account with only the permissions it needs. Attach the role directly to the receiving service instead of another service or the project itself.
For example, an administrator can bind a caller service account to the target service with:
gcloud run services add-iam-policy-binding RECEIVING_SERVICE \
--member="serviceAccount:CALLER_SERVICE_ACCOUNT" \
--role="roles/run.invoker"
Replace the placeholders with the receiving service’s name and the email address of the service account that will call it. Select the right Google Cloud project and region before running the command. Google’s service-to-service authentication instructions show this binding and explain how Cloud Run authorizes callers.
WIF links the identity of an external workload, such as a CI job or a workload on AWS or Azure, to a Google service account. The workload signs in with its own identity provider, and Google exchanges that provider’s credential for short-lived Google credentials. The workload then uses those credentials to request an ID token as the service account.
Set up a WIF provider for the external identity provider, then limit which workloads it trusts to those that should call the service. Grant roles/iam.workloadIdentityUser to the federated principal (the external identity recognized through WIF) on the service account it will impersonate as its Google Cloud identity. Restrict this grant to the specific external identities that need it instead of every member of the identity pool. Google’s WIF best practices explain this access restriction.
The service account needs roles/run.invoker on the receiving Cloud Run service. The identity that asks the IAM Credentials API (Google’s API for creating credentials) to generate an ID token also needs the iam.serviceAccounts.getOpenIdToken permission, which Google includes in roles/iam.serviceAccountOpenIdTokenCreator. Because the exact WIF setup varies by external provider, follow Google Cloud’s current instructions for your provider.
Have the workload call the IAM Credentials API’s generateIdToken method with the external credentials and the service account it is authorized to impersonate. Set the token’s audience (the value that names the target service) to the receiving service’s URL. Use generateIdToken here rather than requesting an OAuth access token, because an access token does not replace the documented service-to-service ID-token flow.
In an ID token, the aud field holds the audience, which names the service the token is meant for. Use the receiving service’s Google-generated run.app URL as the audience. Leave the route path out of the audience: send the request to the full endpoint URL, including its path, but set the audience to only the service URL.
For example, during development you can ask gcloud to print an ID token for an impersonated service account:
SERVICE_URL="https://SERVICE-URL"
ID_TOKEN="$(gcloud auth print-identity-token \
--impersonate-service-account="SERVICE_ACCOUNT_EMAIL" \
--audiences="$SERVICE_URL")"
curl -H "Authorization: Bearer $ID_TOKEN" \
"$SERVICE_URL/path"
The gcloud reference documents the --audiences and --impersonate-service-account flags. A caller who uses the impersonation flag needs roles/iam.serviceAccountTokenCreator on the service account because the CLI impersonation flow must create an access token. Google’s service-account role guidance explains how this permission differs from the role for direct ID-token creation. Without impersonation, gcloud auth print-identity-token uses the active account, and Google limits generic user ID tokens to development. The ID-token instructions describe that restriction.
Cloud Run accepts the ID token in either Authorization: Bearer ID_TOKEN or X-Serverless-Authorization: Bearer ID_TOKEN. If your service uses the Authorization header for its own authentication, put the Cloud Run token in X-Serverless-Authorization. When both headers contain tokens, Cloud Run checks X-Serverless-Authorization for its authentication.
If a client needs an audience other than the generated service URL, add that value as a custom audience on the Cloud Run service. A custom domain or route path alone does not automatically become the audience. Cloud Run’s custom-audience instructions explain when to set a different value.
A valid token does not provide a network path to an unreachable service. Cloud Run checks ingress (which controls where network traffic can enter) before it checks IAM. With ingress set to all, a caller can reach the run.app endpoint over the internet, while IAM can still require an authorized token. With internal, a caller on the public internet cannot reach the service directly. With internal-and-cloud-load-balancing, an internet caller must send requests through an external Application Load Balancer, and Cloud Run blocks direct requests to the run.app URL. Cloud Run’s ingress documentation describes these paths.
If the service is internal-only, an ID token from an external machine does not create a network path to it. The caller must use a permitted path, such as the documented load-balancer route, or a suitable private connection. Keep the IAM check wherever the service requires callers to authenticate.
ID tokens expire about an hour after creation. Have the caller request a fresh token instead of storing one as a permanent credential. If the client caches tokens, it can read the token’s exp claim (which records when the token expires) to decide when to refresh.
Avoid downloading a service-account key when WIF works for the caller. Anyone who holds a key can keep authenticating as the service account, so protect it carefully if you must use one. Never put a key in source control, and follow Google’s service-account key guidance.
When a request fails, check these points in order. First, confirm that ingress lets the caller reach the service. Next, check that the token’s audience matches the receiving service. Then verify that the token has not expired. Finally, confirm that the identity in the token has roles/run.invoker on that service. For a federated caller, also check that the external identity is allowed to impersonate the intended service account.
Give Vroni a GitHub issue, bug report, spec, or rough idea. It reads the repo, plans the change, writes code, runs checks, and works toward a review-ready pull request.
Take a look at vroni.com