このページでは、Identity and Access Management(IAM)ポリシーを非同期で分析し、結果を Cloud Storage に書き込む方法について説明します。この機能は、分析結果が Cloud Storage バケットに書き込まれる以外は、IAM ポリシーの分析とほとんど同じです。
始める前に
プロジェクトで Cloud Asset API を有効にする必要があります
API を使用して、これらのクエリを実行する場合は、環境と
gcurl
を設定する必要があります。gcurl
エイリアスを設定するには、次の手順を実行します。Compute Engine インスタンス上の場合は、次のコマンドを実行します。
alias gcurl='curl -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \ -H "Content-Type: application/json" -X POST'
Compute Engine インスタンス上ではない場合は、次のコマンドを実行します。
alias gcurl='curl -H "$(oauth2l header --json CREDENTIALS cloud-platform)" \ -H "Content-Type: application/json" -X POST'
ここで CREDENTIALS は認証情報ファイルのパスです(
~/credentials.json
など)。
AnalyzeIamPolicyLongrunning
を呼び出しています
AnalyzeIamPolicyLongrunning
メソッドを使用すると、指定された Cloud Storage バケットで分析リクエストを発行して結果を取得できます。
gcloud
asset analyze-iam-policy-longrunning
gcloud コマンドを使用して、API 対応プロジェクトで AnalyzeIamPolicyLongrunning
を呼び出せます。Cloud SDK バージョン 314.0.0 以降を実行している必要があります。gcloud version
コマンドでバージョンを確認できます。
gcloud asset analyze-iam-policy-longrunning \ --organization="ORG_ID" \ --full-resource-name="FULL_RESOURCE_NAME" \ --permissions="COMMA_SEPARATED_PERMISSIONS" \ --gcs-output-path="GCS_OUTPUT_PATH"
たとえば、組織 123456789 の Compute Engine インスタンス ipa-gce-instance-2
に対する compute.instances.get
権限と compute.instances.start
権限をどのユーザーが持つかの分析結果を、analysis.json
のファイル名を有する Cloud Storage バケット gcs-bucket-01
に書き込むには:
gcloud asset analyze-iam-policy-longrunning --organization="123456789" \ --full-resource-name="//compute.googleapis.com/projects/project1/zones/us-central1-a/instances/ipa-gce-instance-2" \ --permissions="compute.instances.get,compute.instances.start" \ --gcs-output-path="gs://gcs-bucket-01/analysis.json"
詳しくは以下をご覧ください。
gcloud asset analyze-iam-policy-longrunning --help
REST
IAM ポリシーを分析し、gcurl
エイリアスを使用して結果を書き込みます。
gcurl -d "$JSON_REQUEST" \ "https://cloudasset.googleapis.com/v1/organizations/${YOUR_ORG_ID}:analyzeIamPolicyLongrunning"
ここで
YOUR_ORG_ID は、
123456789
のような組織 ID です。JSON_REQUEST は JSON 形式の分析リクエストです。たとえば、
analysis.json
のファイル名を有する Cloud Storage バケットgcs-bucket-01
向けに、組織 123456789 の Compute Engine インスタンスipa-gce-instance-2
へのcompute.instances.get
権限とcompute.instances.start
権限をどのユーザーが持つかを分析するには:JSON_REQUEST='{ "analysisQuery":{ "scope":"organization/123456789", "resourceSelector":{ "fullResourceName":"//compute.googleapis.com/projects/project1/zones/us-central1-a/instances/ipa-gce-instance-2" }, "accessSelector":{ "permissions":[ "compute.instances.get", "compute.instances.start" ] } }, "outputConfig":{ "gcsDestination":{ "uri":"gs://gcs-bucket-01/analysis.json" } } }'
IAM ポリシー分析結果の表示
IAM ポリシー分析を表示するには:
分析を書き込んだ先の新しいファイルを開きます。
結果では、{identity, role(s)/permission(s), resource}
のタプルとともに、それらのタプルを生成する IAM ポリシーのリストが表示されます。
クエリの構築
クエリのその他のユースケースとオプションについては、IAM ポリシークエリのサンプルの分析をご覧ください。