Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Servers, nginx and Docker

GitHub Actions cannot get a Google Cloud token through Workload Identity Federation

ErrorUnable to acquire impersonated credentials

Short answer

The auth step trades a GitHub OIDC token for a Google token, then impersonates a service account. It fails when the job cannot request the OIDC token, when the provider's attribute mapping or condition rejects the token, or when the service account lacks a Workload Identity User binding for your repository.

  • Applies to google-github-actions/auth, Workload Identity Federation, GitHub Actions
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Check which step fails. A federated-token message points to the pool and provider; an OAuth or impersonation message points to the service account binding.

    gcloud iam service-accounts get-iam-policy SA_NAME@PROJECT_ID.iam.gserviceaccount.com
  2. 2Match what you see, then jump to its fix

    1. 1The workflow has no permissions block, or one that sets only contentsThe job is not allowed to request a GitHub ID tokenFix
    2. 2The policy from the first step has no roles/iam.workloadIdentityUser member with a principalSet for your poolThe service account has no Workload Identity User binding for the repositoryFix
    3. 3Failed to generate Google Cloud federated tokenThe provider rejects the token (mapping or condition mismatch)Fix

What does the error look like?

  • Unable to acquire impersonated credentials
  • No access token or invalid expiration in response
  • Permission 'iam.serviceAccounts.getAccessToken' denied
  • Failed to generate Google Cloud federated token

Cause 1 of 3

The job is not allowed to request a GitHub ID token

1Check

Federation needs the job to fetch an OIDC token from GitHub. Open the workflow and look for id-token under permissions.

grep -n -A3 permissions .github/workflows/*.yml

2Fix

Add id-token write to the job, next to contents read.

permissions:
  contents: 'read'
  id-token: 'write'

3Verify

Re-run the job; the auth step should get past token creation.

git commit --allow-empty -m 'retry auth' && git push

Confirmed by github.com

Did this fix it?

Cause 2 of 3

The service account has no Workload Identity User binding for the repository

1Check

Impersonation works only if the service account lets the federated identity act as it. The member must use the project number, not the project ID.

gcloud iam service-accounts get-iam-policy SA_NAME@PROJECT_ID.iam.gserviceaccount.com

2Fix

Bind the role to a principalSet that matches an attribute you mapped, such as the repository.

gcloud iam service-accounts add-iam-policy-binding SA_NAME@PROJECT_ID.iam.gserviceaccount.com \
  --role=roles/iam.workloadIdentityUser \
  --member="principalSet://iam.googleapis.com/projects/PROJECT_NUMBER/locations/global/workloadIdentityPools/POOL_ID/attribute.repository/OWNER/REPO"

3Verify

Read the policy again and re-run the workflow.

gcloud iam service-accounts get-iam-policy SA_NAME@PROJECT_ID.iam.gserviceaccount.com

Confirmed by docs.cloud.google.com

Did this fix it?

Cause 3 of 3

The provider rejects the token (mapping or condition mismatch)

1Check

A claim used in an attribute condition or in a principalSet must be mapped to an attribute first, and its capitalization must match the GitHub token exactly.

grep -n -B2 -A4 workload_identity_provider .github/workflows/*.yml

2Fix

Map every claim you use, and make the condition match your real owner name. The documented provider setup is shown here; adjust the existing provider in the console or recreate it with the same fields.

gcloud iam workload-identity-pools providers create-oidc PROVIDER_ID \
  --location="global" \
  --workload-identity-pool="POOL_ID" \
  --issuer-uri="https://token.actions.githubusercontent.com/" \
  --attribute-mapping="google.subject=assertion.sub,attribute.repository=assertion.repository,attribute.repository_owner=assertion.repository_owner" \
  --attribute-condition="assertion.repository_owner=='OWNER'"

3Verify

Re-run the workflow; the federated token step should pass.

git commit --allow-empty -m 'retry auth' && git push

Confirmed by github.com

Did this fix it?

How do I stop it coming back?

  • Set the id-token permission on the job that authenticates, not only at the top of a different job.
  • Use the project number in workload identity paths and the project ID in service account emails.
  • Always keep an attribute condition that limits the provider to your own organization.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Related fixes

Servers

docker pull or push to ghcr.io fails with 'denied'

Error response from daemon: denied

ghcr.io refuses a pull or push when Docker is not logged in, when the token lacks the package scope, or when the workflow's GITHUB_TOKEN has no packages permission or no access to that package.

4 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.