Bitbucket Pipelines OIDC to Google Cloud: "Permission 'iam.serviceAccounts.getAccessToken' denied"
With Workload Identity Federation, a Pipelines step trades its OpenID Connect token for a short-lived Google Cloud credential and impersonates a service account. When Google does not accept the binding between the token and that service account, the step stops with this:
Permission 'iam.serviceAccounts.getAccessToken' denied on resource (or it may not exist).
The same text, with a 403, is in a thread on the Google Cloud Community forum, Workload Identity Federation : AccessDeniedException: 403 Permission 'iam.serviceAccounts.getAccessToken', where granting the Workload Identity User role was not enough and the poster suspected the attribute mapping or the audience. The thread has no marked answer. The steps below are what Google's and Atlassian's documentation say about each part.
What a Bitbucket token carries
Atlassian's example token in Deploy on AWS using Bitbucket Pipelines OpenID Connect (shortened):
{
"sub": "{1de489be-ce6a-42a0-a8c8-eadbf1174ac7}:{bd715740-c970-486b-b68a-b421ec2a1f8b}:{759de0c6-eaee-4eaa-b7a6-c507eec759a7}",
"aud": "ari:cloud:bitbucket::workspace/922e118c-f34f-4226-a897-0e0afbec553d",
"iss": "https://api.bitbucket.org/2.0/workspaces/bbcitest/pipelines-config/identity/oidc",
"repositoryUuid": "{1de489be-ce6a-42a0-a8c8-eadbf1174ac7}",
"stepUuid": "{759de0c6-eaee-4eaa-b7a6-c507eec759a7}"
}
The issuer and the audience are the Identity provider URL and the Audience on the repository's OpenID Connect page under Pipelines. The sub ends in the UUID of the step, which is new every time the step runs (Atlassian's report BCLOUD-22038 says so and lists no workaround).
While you sort out the Google Cloud binding: Pipeline Inbox for Bitbucket Pipelines, a plugin for JetBrains IDEs, lists the pipelines of every Bitbucket Cloud repository you have in a tool window of the IDE, with the failed step and the command it stopped at.
The provider: issuer, audience, attributes
Google's Configure Workload Identity Federation with other identity providers creates the provider with create-oidc. For Bitbucket:
gcloud iam workload-identity-pools providers create-oidc PROVIDER_ID \
--location="global" \
--workload-identity-pool="POOL_ID" \
--issuer-uri="https://api.bitbucket.org/2.0/workspaces/WORKSPACE/pipelines-config/identity/oidc" \
--allowed-audiences="ari:cloud:bitbucket::workspace/WORKSPACE_UUID" \
--attribute-mapping="google.subject=assertion.sub,attribute.repository=assertion.repositoryUuid"
--allowed-audienceshas to be the audience of the Bitbucket token. Google's page says that, unless a custom audience is configured, the token'saudmust be the provider's full resource URL (https://iam.googleapis.com/projects/PROJECT_NUMBER/locations/global/workloadIdentityPools/POOL_ID/providers/PROVIDER_ID), which Bitbucket does not send by default. Either list the Bitbucket value, as above, or setoidc: audiences:in the YAML to that URL (Atlassian's Integrate Pipelines with resource servers using OIDC).google.subjectis required, must be unique and cannot exceed 127 characters (Workload Identity Federation). A Bitbucketsubis 77 characters without an environment and 116 with one, so it fits.attribute.repositoryis a custom attribute (attribute.NAME, up to 50 are allowed). It carries the repository UUID, which does not change from step to step.
The binding: match the attribute, not the subject
Google's principal identifiers come in two kinds. principal://.../subject/SUBJECT names one exact google.subject, which here would be one step of one run. principalSet://.../attribute.ATTRIBUTE_NAME/ATTRIBUTE_VALUE names every identity with that attribute value, so it keeps working. Impersonating a service account needs the Workload Identity User role on that account:
gcloud iam service-accounts add-iam-policy-binding SERVICE_ACCOUNT_EMAIL \
--role="roles/iam.workloadIdentityUser" \
--member="principalSet://iam.googleapis.com/projects/PROJECT_NUMBER/locations/global/workloadIdentityPools/POOL_ID/attribute.repository/{REPOSITORY_UUID}"
Three details that give this error when wrong:
- The UUID keeps its curly braces, as in the token's
repositoryUuid. The binding has to hold the same string the mapping produces. - Google's page says to use the project number, not the project ID, in member identifiers.
- The binding goes on the service account the step impersonates, and that is the account named when the credential file is made (next section).
The pipeline
The step writes the token to a file and makes a credential configuration that reads it. The flags --credential-source-file and --output-file and the gcloud auth login --cred-file call are from Google's page; --service-account selects the account to impersonate.
image: google/cloud-sdk
pipelines:
default:
- step:
oidc: true
script:
- echo $BITBUCKET_STEP_OIDC_TOKEN > $(pwd)/oidc-token
- |
gcloud iam workload-identity-pools create-cred-config \
projects/PROJECT_NUMBER/locations/global/workloadIdentityPools/POOL_ID/providers/PROVIDER_ID \
--service-account=SERVICE_ACCOUNT_EMAIL \
--credential-source-file=$(pwd)/oidc-token \
--output-file=$(pwd)/gcp-credentials.json
- gcloud auth login --cred-file=$(pwd)/gcp-credentials.json
- gcloud auth list
Each step gets its own token, so write the file in the step that uses it. Do not commit oidc-token or gcp-credentials.json.
If the provider has an attribute condition
A condition is a CEL expression on assertion and the mapped attributes; the credential is accepted only when it is true (Google's Workload Identity Federation). To limit the provider to one repository, add --attribute-condition="attribute.repository=='{REPOSITORY_UUID}'" to the create-oidc command.
Bitbucket Pipelines in IntelliJ IDEA
Pipeline Inbox for Bitbucket Pipelines, a plugin by Lesstep for JetBrains IDEs 2024.1 or later, lists the pipelines of all your Bitbucket Cloud repositories in an IDE tool window, with their steps, the failed step and the command it stopped at, and the step's log as Bitbucket writes it. The failed gcloud command and its last output come first in the step, so the permission message is the first thing you see. The list, steps and logs are free; running, re-running and stopping pipelines are Pro (US$1.50 a month, with a 30-day trial).
Get it on JetBrains Marketplace
Atlassian, Bitbucket and Bitbucket Pipelines are trademarks of Atlassian; Google Cloud is a trademark of Google LLC; IntelliJ IDEA is a trademark of JetBrains s.r.o. This page is not affiliated with or endorsed by Atlassian, Google or JetBrains.
More: Test results not showing · A variable is empty · "Host key verification failed" (SSH) · "Cannot connect to the Docker daemon" · "authorization denied by plugin pipelines" (Docker) · "Container 'docker' exceeded memory limit." · Pipelines did not run after a push · "Container 'Build' exceeded memory limit." · "Exceeded build time limit of 120 minutes" · The cache is not used · "unexpected system error" · "command not found" · Submodules not cloned · "We couldn't clone the repository" · "No online runner available" · "No space left on device" · "Step exceeded processing limits" · The artifact is missing in the next step · The build passed before and fails now · Build setup takes too long · "You've run out of build minutes" · "Permission denied to update branch" · "You have reached your pull rate limit" (Docker Hub) · "There is an error in your bitbucket-pipelines.yml" · "This pipeline was paused" (deployments) · "Connection refused" to a service (MySQL, PostgreSQL, Redis) · "Container 'build' failed to pull image" · The scheduled pipeline did not run · Git commands that fail in a shallow clone (origin/main, git describe) · Git LFS files that arrive as pointers, and LFS pushes from a step · A step skipped by condition: changesets (includePaths) · Parallel steps that keep running after one fails (fail-fast) · Bitbucket Pipelines plugins for IntelliJ IDEA, compared · "Not authorized to perform sts:AssumeRoleWithWebIdentity" (OIDC to AWS) · "AADSTS70021" OIDC to Azure (Entra ID) · "Cannot perform an interactive login from a non TTY device" (docker login) · 日本語