1

私はサービスアカウントを作成し、JSON形式(/adc.json)のプライベートキーを提供しました。それはClient.from_service_account_json機能を介してグーグルクラウドのpythonクライアントにロードすることができます。しかし、カスタムメトリックを作成するためにMonitoring APIを呼び出そうとすると、以下のように403エラーが発生します。 GCPのアクセスコントロールパネルでサービスアカウントがGCPのStackdriverにカスタムメトリックを書き込むための適切なIAMの役割

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': '123456789', '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/) 

、私はのStackdriverモニタリングAPIのための具体的な定義済みの役割のスコープを見ていません。私はProject ViewerService Account Actor事前に定義された役割を試していない、どちらも働いてきた enter image description here

:下のスクリーンショットを参照してください。 Stackdriver専用のサービスアカウント資格情報の範囲が広すぎるように感じるため、Project Editorロールをこのサービスアカウントに割り当てることは躊躇しています。では、このサービスアカウントに割り当てる正しい役割は何ですか。ありがとう。

答えて

2

あなたはそれが広すぎると思います。より細かい役割に取り組んでいますが、今日の時点では、「プロジェクトエディタ」が正しい役割です。

GCE VMで実行していてプライベートキーを省略した場合、Stackdriver監視エージェントは、デフォルトでVMのデフォルトサービスアカウントを使用しようとします。 VMがhttps://www.googleapis.com/auth/monitoring.writeのスコープを持つ限り、これは機能します(これは最近、すべてのGCE VMではデフォルトでオンになっているはずです)。エージェントに必要な資格情報の詳細については、this pageを参照してください。

関連する問題