swigganicks
swigganicks

Reputation: 1231

Unable to submit build to Cloud Build due to permissions error

When submitting a Cloud Build run via gcloud builds submit ... I'm getting a forbidden error saying I don't have access to the bucket(s). There are 2 places where buckets are normally involved in submitting a Cloud Build, the staging and logs bucket. I specified the buckets for each as buckets (the same one, just different folders) that I do have access too so the command looks like this:

gcloud builds submit 
--gcs-log-dir $my_bucket/logs 
--gcs-source-staging-dir $my_bucket/source  

The error I get is:

ERROR: (gcloud.builds.submit) 403: The user is forbidden from accessing the bucket [$my_bucket]: Please check your organization's policy.

I re-ran with --log-http and --verbosity debug and the expanded error shows the real reason:

DEBUG: https://storageapis.google.com "GET /storage/v1/b/$my_bucket?alt=json"
... 
{
  "error": {
    "code": 403,
    "message": "$user does not have serviceusage.services.use access to the Google Cloud Project."
  }
}

I did some digging and see that's this error shows up when supplying a quota/billing project with the request (in addition to not having service consumer role). I confirmed this when inspecting the request's HTTP headers which included X-Goog-User-Project: $my_project.

What's weird is that I have access to objects in this bucket and can run gsutil/HTTP commands just fine which are using the same API endpoints with the difference being that gsutil doesn't include that user project in the request.

Is there a way to submit a build that doesn't include the project so that I don't need serviceusage.services.use permission? I tried unsetting the project in my gcloud config but it prompted me that I needed to either set it or pass it with --project flag.

edit: the bucket isn't "requester pays" enabled either which is why gsutil and client libraries work fine

Upvotes: 9

Views: 16983

Answers (6)

Cristian Q
Cristian Q

Reputation: 647

In my case, I was working on a different project than the one my service account had permissions for, the following command was enough:

gcloud config set project [PROJECT_ID_SA]

Upvotes: 0

girorme
girorme

Reputation: 361

My issue with this message was literally a permission gap. To solve the problem you can visit the service-usage-access-control-reference page (there you can find the serviceusage.services.use permission that you need).

Based on permissions there, you can use for example the roles/serviceusage.serviceUsageAdmin permission in your service account.

In my case i did the following:

gcloud projects add-iam-policy-binding PROJECT_ID --member="serviceAccount:YOUR_SA" --role="roles/serviceusage.serviceUsageAdmin"

You can use different permissions based on your needs, for example: roles/serviceusage.serviceUsageConsumer

Finishing the command i was able to run gh action (gcloud builds submit) using the service account.

Service account creation ref: service accounts create docs

Upvotes: 1

According to the GCP documentation:

To run gcloud builds commands, users with only cloudbuild.builds.viewer or cloudbuild.builds.editor roles also require the serviceusage.services.use permission. To give this permission to the user, grant them the serviceusage.serviceUsageConsumer role.

Edit your user on IAM & Admin choosing your user and type "Service Usage Consumer".

However, review your policies and roles because I beliave that this option is for clean users created from the scratch without any other permissions than Object Storage roles.

Upvotes: 6

Orville
Orville

Reputation: 557

I had a similar error log, I'm running gcloud with docker image google/cloud-sdk:alpine:

googlecloudsdk.command_lib.builds.submit_util.BucketForbiddenError: The user is forbidden from accessing the bucket [xxxx]. Please check your organization's policy or if the user has the "serviceusage.services.use" permission. Giving the user Owner, Editor, or Viewer roles may also fix this issue. Alternatively, use the --no-source option and access your source code via a different method.

I added the Role Cloud Build Service Agent to my service account.

Full list of roles the service account:

  • Cloud Run Admin
  • Storage Insights Collector Service
  • Storage Object Creator
  • Storage Object Viewer

Gcloud version:

Google Cloud SDK 453.0.0
bq 2.0.98
bundled-python3-unix 3.9.17
core 2023.10.27
gcloud-crc32c 1.0.0
gsutil 5.27

Upvotes: 0

humbert
humbert

Reputation: 99

The only reason why you are having this error is you have to enable your billing in order to build your bucket.

I have enabled it when I was trying the tutorial by clicking the "Create a Cloud Storage Bucket" under Getting started at the left side of your Dashboard. Just follow the instructions and you will see the "Enable Billing". Once you have enabled the Billing, you don't need to finish the Tutorial. Go back to your work and run the

$ gcloud build submit

and it's done!

Upvotes: 9

ab_zmi
ab_zmi

Reputation: 66

I'm not sure you can run a cloud build without specifying a project. As far as I know, gcloud commands run within a project so it's needed.

If you want to use a different service account you can use service account impersonation adding --impersonate-service-account flag.

For this gcloud invocation, all API requests will be made as the given service account instead of the currently selected account.

Upvotes: 2

Related Questions