Google Cloud Logging 身份验证/权限

Google Cloud Logging Authentication / permissions

我正在使用 Golang 库 cloud.google.com/go/logging 并且想要发送运行时日志记录。

已经有一个 GOOGLE_APPLICATION_CREDENTIALS .json 文件 - 并且正在使用 google 存储和 firebase - 所以我知道凭据正在工作。

使用日志记录时,出现错误 "Error 403: The caller does not have permission, forbidden"

应用程序凭据中的帐户是一个服务帐户,我一直在查看 IAM 权限。没有明显的日志记录权限(还有其他堆栈驱动程序权限,用于调试、跟踪等,但这些似乎不起作用)。

那么假设到目前为止我在正确的地方 - 服务帐户需要什么权限才能将日志记录数据发送到 stackdriver 日志记录?

如果我们查看 writing entries to a log 的 API,我们发现需要 IAM 权限 logging.logEntries.create

可以在 Access control guide 找到更详细的文章。

这描述了各种角色,包括:

  • roles/logging.logWriter

根据官方文档:

Using Stackdriver Logging library for Go requires the Cloud IAM Logs Writer role on Google Cloud. Most Google Cloud environments provide this role by default.

1.App 引擎默认授予 Logs Writer 角色。

2.On Google Kubernetes Engine,创建集群时必须添加logging.write访问范围:

3.When 使用 Compute Engine VM 实例,为每个实例添加云平台访问范围。

4.To 在 Google 云之外使用 Stackdriver Logging 库进行 Go,包括 运行 您自己的工作站、数据中心的计算机或 VM 实例上的库另一个云提供商的,您必须将您的 Google 云项目 ID 和适当的服务帐户凭据直接提供给 Go 的 Stackdriver Logging 库。

您可以手动创建和获取服务帐户凭据。指定角色字段时,使用 Logs Writer 角色。有关 Cloud Identity and Access Management 角色的更多信息,请转到访问控制指南。

Setting Up Stackdriver Logging for Go

gcloud iam service-accounts list 
gcloud projects add-iam-policy-binding my-project-123 \
--member serviceAccount:my-sa-123@my-project-123.iam.gserviceaccount.com \
--role roles/logging.logWriter