Jose A. Matarán
Jose A. Matarán

Reputation: 1346

Issues with GCP OIDC Integration in Bitbucket Pipelines

I'm trying to set up integration between Bitbucket Pipelines and GCP using OIDC to access GCP resources (e.g., list GCP storage buckets). Despite following all the steps outlined in the Atlassian Community guide, I encounter the following error:

ERROR: (gcloud.storage.buckets.list) There was a problem refreshing your current auth tokens: ('Unable to acquire impersonated credentials', '{\n  "error": {\n    "code": 403,\n    "message": "Permission 'iam.serviceAccounts.getAccessToken' denied on resource (or it may not exist).",\n    "status": "PERMISSION_DENIED",\n    "details": [\n      {\n        "@type": "type.googleapis.com/google.rpc.ErrorInfo",\n        "reason": "IAM_PERMISSION_DENIED",\n        "domain": "iam.googleapis.com",\n        "metadata": {\n          "permission": "iam.serviceAccounts.getAccessToken"\n        }\n      }\n    ]\n  }\n}\n')
Please run:
  $ gcloud auth login
to obtain new credentials.
If you have already logged in with a different account, run:
  $ gcloud config set account ACCOUNT
to select an already authenticated account to use. 

Steps I followed:

  1. Created a Workload Identity Pool in GCP:

    gcloud beta iam workload-identity-pools create bitbucket-pipelines-oidc-demo \
      --location="global" \
      --description="A workload identity pool for Bitbucket Pipelines" \
      --display-name="bitbucket-pipelines-oidc-demo" 
    
  2. Created an OIDC Provider

    gcloud beta iam workload-identity-pools providers create-oidc bitbucket-oidc-idp \
      --workload-identity-pool="bitbucket-pipelines-oidc-demo" \
      --issuer-uri="https://api.bitbucket.org/2.0/workspaces/my-workspace/pipelines-config/identity/oidc" \
      --location="global" \
      --attribute-mapping="google.subject=assertion.sub,attribute.workspace_uuid=assertion.workspaceUuid" \
      --allowed-audiences="ari:cloud:bitbucket::workspace/my-workspace-uuid"
    
    
  3. Created a Service Account in GCP

     gcloud iam service-accounts create my-service-account \
      --display-name="Service account for OIDC integration"
    
    
  4. Bound the Service Account to the Workload Identity Pool.

    gcloud iam service-accounts add-iam-policy-binding [email protected] \
      --role="roles/iam.workloadIdentityUser" \
      --member="principalSet://iam.googleapis.com/projects/my-project-number/locations/global/workloadIdentityPools/bitbucket-pipelines-oidc-demo/attribute.workspace_uuid/my-workspace-uuid"
    
  5. Granted Permissions to the Service Account

     gcloud projects add-iam-policy-binding my-project \
     --member="serviceAccount:[email protected]" \
     --role="roles/storage.viewer"
    
    
  6. Apply (serviceAccountTokenCreator)

    gcloud projects add-iam-policy-binding my-project \
      --member="serviceAccount:[email protected]" \
      --role="roles/iam.serviceAccountTokenCreator"
    
    
  7. Configured the Bitbucket Pipeline. Here is my bitbucket-pipelines.yml file

      image: google/cloud-sdk:alpine
    
      pipelines:
        default:
          - step:
              name: Test OIDC with GCP
              oidc: true
              script:
                # Save OIDC token to a file
                - echo -n "${BITBUCKET_STEP_OIDC_TOKEN}" > /tmp/gcp_access_token.out
    
                # Create GCP credentials
                - |
                  gcloud iam workload-identity-pools create-cred-config \
                    projects/my-project-number/locations/global/workloadIdentityPools/bitbucket-pipelines-oidc-demo/providers/bitbucket-oidc-idp \
                    --service-account="[email protected]" \
                    --output-file=/tmp/sts-creds.json \
                    --credential-source-file=/tmp/gcp_access_token.out
    
                # Export credentials
                - export GOOGLE_APPLICATION_CREDENTIALS=/tmp/sts-creds.json
    
                # Authenticate and list buckets
                - gcloud auth login --cred-file=/tmp/sts-creds.json
                - gcloud storage buckets list

Observed Issue:

Questions:

Upvotes: 1

Views: 150

Answers (0)

Related Questions