本页面介绍如何异步分析 Identity and Access Management (IAM) 政策并将结果写入 Cloud Storage。此功能基本等同于分析 IAM 政策,不同之处在于分析结果会写入 Cloud Storage 存储分区。
准备工作
您必须为项目启用 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
权限的分析结果写入 Cloud Storage 存储分区 gcs-bucket-01
,文件名为 analysis.json
:
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
使用 gcurl
别名分析 IAM 政策并写入结果。
gcurl -d "$JSON_REQUEST" \ "https://cloudasset.googleapis.com/v1/organizations/${YOUR_ORG_ID}:analyzeIamPolicyLongrunning"
其中:
YOUR_ORG_ID 是组织 ID,例如:
123456789
JSON_REQUEST 是 JSON 格式的分析请求。例如,分析谁拥有组织 123456789 下的 Compute Engine 实例
ipa-gce-instance-2
的compute.instances.get
和compute.instances.start
权限,并将分析结果写入 Cloud Storage 存储分区gcs-bucket-01
,文件名为analysis.json
: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 政策查询示例。