我有一个服务帐户,可以在Google容器生成器上触发构建。这工作正常,但现在我想使用该服务帐户检索构建日志。
以下是获取日志的代码(使用google-auto-auth包获取令牌,这部分在其他地方运行良好,所以我真的不认为这是问题):
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);
});
目前,虽然服务帐户可以访问以下角色,但是401 Unauthorized已失败:
这是错误:
{
"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
}
}
知道为什么403的请求失败了吗?它可能来自缺失的范围吗?到目前为止我只设置scopes: 'https://www.googleapis.com/auth/cloud-platform'
。
答案 0 :(得分:6)
要查看构建日志,除了具有构建器编辑器角色外,有问题的服务帐户还必须是项目中的查看器。
role/viewer
包含角色role/cloudbuild.builds.viewer
role/cloudbuild.builds.editor
作为最后一步,您可能需要:(不必要)
请同时查看:docs