Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access Google Container builder logs with a service account - 403 Forbidden Error

I have a service account that triggers builds on Google Container Builder. This works fine but now I would like to retrieve build logs using that service account.

Here is the code that fetches the log (the token is obtained using google-auto-auth package and this part works well in other places, so I really don't think this is the issue):

var url = logsBucket + '/log-' + buildId + '.txt';
debug('Requesting log at %s', url);
request
  .get(url)
  .set('Authorization', 'Bearer ' + token)
  .end(function(err, res) {
    if (err) return cb(err);
    var log = res.body;
    debug('Received build log : %o', log);
    cb(null, log);
  });

Currently, this fails with 401 Unauthorized although the service account has access to the following roles:

  • Admin kubernetes engine
  • Admin storage
  • Admin objects in storage
  • Cloud container builder
  • Reader Cloud container builder
  • Reader storage objects

This is the error:

{
  "message": "Forbidden",
  "stack": "Error: Forbidden\n    at Request.callback (/app/node_modules/superagent/lib/node/index.js:696:15)\n [...]",
  "status": 403,
  "response": {
    "req": {
      "method": "GET",
      "url": "https://storage.googleapis.com/{PROJECT_ID}.cloudbuild-logs.googleusercontent.com/log-42602b35-af02-4e75-8100-8a3bd0e720fb.txt",
      "headers": {
        "user-agent": "node-superagent/3.8.2",
        "authorization": "Bearer {BEARER_TOKEN}"
      }
    },
    "header": {
      "x-guploader-uploadid": "{SOME-UPLOAD-ID}",
      "content-type": "application/xml; charset=UTF-8",
      "content-length": "337",
      "date": "Wed, 10 Jan 2018 11:06:54 GMT",
      "expires": "Wed, 10 Jan 2018 11:06:54 GMT",
      "cache-control": "private, max-age=0",
      "server": "UploadServer",
      "alt-svc": "...",
      "connection": "close"
    },
    "status": 403
  }
}

Any idea why the request fails with 403 ? Could it come from a missing scope ? I only set scopes: 'https://www.googleapis.com/auth/cloud-platform' so far.

like image 531
Overdrivr Avatar asked Jan 10 '18 11:01

Overdrivr


1 Answers

GCS permissions predate IAM and thus work a little differently.

To view the build logs, the Service Account in question needs to be a Viewer on the project in addition to have the Builder Editor role.

  • role/viewer includes role role/cloudbuild.builds.viewer
  • role/cloudbuild.builds.editor

As a last step you may want to: (Not necessary)

  • Disable the Container Builder API and re-enable it. Doing so should give your service account access to your project again.

Please also look at: docs

like image 124
gokcand Avatar answered Nov 03 '22 11:11

gokcand