google-cloud-platform - 服务帐号将自定义指标写入 GCP 中的 Stackdriver 的正确 IAM 角色是什么

标签 google-cloud-platform stackdriver google-cloud-python

我创建了一个服务帐户并提供了 JSON 格式的私钥 (/adc.json)。它可以通过 Client.from_service_account_json 函数加载到 google-cloud python 客户端。但是当我尝试调用监控 API 来编写自定义指标时,它收到如下所示的 403 错误。

In [1]: from google.cloud import monitoring

In [2]: c = monitoring.Client.from_service_account_json('/adc.json')

In [6]: resource = client.resource('gce_instance', labels={'instance_id': '1234567890123456789', 'zone': 'us-central1-f'})

In [7]: metric = client.metric(type_='custom.googleapis.com/my_metric', labels={'status': 'successful'})

In [9]: from datetime import datetime

In [10]: end_time = datetime.utcnow()

In [11]: client.write_point(metric=metric, resource=resource, value=3.14, end_time=end_time)
---------------------------------------------------------------------------
Forbidden                                 Traceback (most recent call last)
<ipython-input-11-b030f6399aa2> in <module>()
----> 1 client.write_point(metric=metric, resource=resource, value=3.14, end_time=end_time)

/usr/local/lib/python3.5/site-packages/google/cloud/monitoring/client.py in write_point(self, metric, resource, value, end_time, start_time)
    599         timeseries = self.time_series(
    600             metric, resource, value, end_time, start_time)
--> 601         self.write_time_series([timeseries])

/usr/local/lib/python3.5/site-packages/google/cloud/monitoring/client.py in write_time_series(self, timeseries_list)
    544                            for timeseries in timeseries_list]
    545         self._connection.api_request(method='POST', path=path,
--> 546                                      data={'timeSeries': timeseries_dict})
    547 
    548     def write_point(self, metric, resource, value,

/usr/local/lib/python3.5/site-packages/google/cloud/_http.py in api_request(self, method, path, query_params, data, content_type, headers, api_base_url, api_version, expect_json, _target_object)
    301         if not 200 <= response.status < 300:
    302             raise make_exception(response, content,
--> 303                                  error_info=method + ' ' + url)
    304 
    305         string_or_bytes = (six.binary_type, six.text_type)

Forbidden: 403 User is not authorized to access the project monitoring records. (POST https://monitoring.googleapis.com/v3/projects/MY-PROJECT/timeSeries/)

在 GCP 的访问控制面板中,我没有看到 Stackdriver Monitoring API 的特定预定义角色范围。请参阅下面的屏幕截图: enter image description here

我尝试过Project ViewerService Account Actor预定义角色,但都不起作用。我对于是否为该服务帐户分配项目编辑角色犹豫不决,因为感觉对于 Stackdriver 专用服务帐户凭据来说,它的范围太广了。那么分配给该服务帐户的正确角色应该是什么?谢谢。

最佳答案

您说得对,它太宽泛了,我们正在研究更细粒度的角色,但是,截至今天,“项目编辑器”是正确的角色。

如果您在 GCE 虚拟机上运行并省略私钥,则 Stackdriver 监控代理将默认尝试使用虚拟机的默认服务帐户。只要虚拟机具有 https://www.googleapis.com/auth/monitoring.write 范围(目前所有 GCE 虚拟机都应默认启用此功能),此功能就可以工作。请参阅this page了解代理需要哪些凭据的详细说明。

关于google-cloud-platform - 服务帐号将自定义指标写入 GCP 中的 Stackdriver 的正确 IAM 角色是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43281954/

相关文章:

python - 从开发者控制台读取 Google App Engine 上的应用程序日志

google-cloud-platform - 从 winston 登录到 stackdriver

python - 使用 gcloud-python 在 Google Cloud Storage 中设置元数据

machine-learning - Python命名实体识别查找特定实体

google-cloud-storage - 如何使用 Google Cloud Storage 进行 rsync 并将某些文件设置为 gzipped?

python-3.x - Google Cloud Datastore python本地开发找不到数据存储模块

python - 使用Python SDK时如何防止GCS自动解压对象?

python - 属性错误 : module 'boto' has no attribute 'plugin'

python - 如何将日志记录 'tags' 添加到 stackdriver

python - 谷歌云存储 + Python : Any way to list obj in certain folder in GCS?