I am encountering an issue with my Cloud Build pipeline where it fails to access a secret stored in Secret Manager. Here is the error message I receive:
BUILD FAILURE: Build step failure: build step 0 "gcr.io/cloud-builders/git" failed: failed to access secret version for secret projects/projlumen123/secrets/github_pat/versions/latest: rpc error: code = PermissionDenied desc = Permission 'secretmanager.versions.access' denied for resource 'projects/projlumen123/secrets/github_pat/versions/latest' (or it may not exist).
What I Have Tried
- Permissions:
- Both the Cloud Build default service account (<PROJECT_NUMBER>@cloudbuild.gserviceaccount.com) and my custom service account (projectlumen-sa@projlumen123.iam.gserviceaccount.com) have been explicitly granted the roles/secretmanager.secretAccessor role at the project level and on the specific secret (github_pat).
IAM policy for the github_pat secret includes:bindings: - members: - serviceAccount:<PROJECT_NUMBER>@cloudbuild.gserviceaccount.com - serviceAccount:projectlumen-sa@projlumen123.iam.gserviceaccount.com - serviceAccount:service-<PROJECT_NUMBER>@gcp-sa-cloudbuild.iam.gserviceaccount.com role: roles/secretmanager.secretAccessor
- Secret Configuration:
- The github_pat secret exists in Secret Manager with an enabled version:
gcloud secrets versions list github_pat --project=projlumen123
Output:NAME STATE CREATED 1 enabled <timestamp>
- Cloud Build YAML:
My cloudbuild.yaml uses availableSecrets to securely retrieve secrets:availableSecrets: secretManager: - versionName: "projects/projlumen123/secrets/github_pat/versions/latest" env: "GITHUB_PAT" - versionName: "projects/projlumen123/secrets/firebase_token/versions/latest" env: "FIREBASE_TOKEN"
- Testing Secret Access:
I tested secret access manually using impersonation, and it works fine:gcloud secrets versions access latest --secret="github_pat" \ --impersonate-service-account=<PROJECT_NUMBER>@cloudbuild.gserviceaccount.com \ --project=projlumen123
This command successfully retrieves the secret value. - Cloud Build Service Account:
The Cloud Build default service account has the roles/cloudbuild.builds.builder role and roles/secretmanager.secretAccessor role assigned at both project and secret levels.
My Setup
Questions
- Is there any additional permission (e.g., more granular than roles/secretmanager.secretAccessor) that I need to assign to the Cloud Build default service account?
- Could there be an issue with how availableSecrets is configured in my cloudbuild.yaml file?
- Are there any known issues or limitations with using availableSecrets in Cloud Build?
Any guidance or suggestions would be greatly appreciated! Thank you in advance for your help.