查看有效的 IAM 政策

在 IAM 中,「有效政策」說明資源如何繼承資源階層中的所有父項和祖系政策。

Cloud Asset Inventory 僅支援擷取允許政策。不支援下列存取控管形式:

事前準備

  1. 在執行 Cloud Asset Inventory 指令的專案中,啟用 Cloud Asset Inventory API。

    啟用 Cloud Asset Inventory API

  2. 確認帳戶具備呼叫 Cloud Asset Inventory API 的正確角色。如要瞭解各個呼叫類型的個別權限,請參閱「權限」。

查看資源的有效政策

gcloud

gcloud asset get-effective-iam-policy \
    --scope=SCOPE_PATH \
    --names=ASSET_NAME_1,ASSET_NAME_2,...

提供以下這些值:

  • SCOPE_PATH:請使用下列其中一個值:

    • projects/PROJECT_ID,其中 PROJECT_ID 是要檢查有效 IAM 政策的頂層專案 ID。
    • projects/PROJECT_NUMBER,其中 PROJECT_NUMBER 是專案編號,代表要從哪個頂層專案檢查有效的 IAM 政策。

      如何找出 Google Cloud 專案編號

      Google Cloud 控制台

      如要找出 Google Cloud 專案編號,請完成下列步驟:

      1. 前往 Google Cloud 控制台的「Welcome」(歡迎) 頁面。

        前往「歡迎」頁面

      2. 按一下選單列中的「切換器」清單方塊。
      3. 從清單方塊中選取貴機構,然後搜尋專案名稱。 專案名稱、專案編號和專案 ID 會顯示在「歡迎」標題附近。

        最多會顯示 4,000 項資源。如果找不到所需專案,請前往「Manage resources」(管理資源) 頁面,然後使用該專案的名稱篩選清單。

      gcloud CLI

      您可以使用下列指令擷取 Google Cloud 專案編號:

      gcloud projects describe PROJECT_ID --format="value(projectNumber)"
    • folders/FOLDER_ID,其中 FOLDER_ID 是要檢查有效 IAM 政策的頂層資料夾 ID。

      如何找出 Google Cloud 資料夾的 ID

      Google Cloud 控制台

      如要找出 Google Cloud 資料夾的 ID,請完成下列步驟:

      1. 前往 Google Cloud 控制台。

        前往 Google Cloud 控制台

      2. 按一下選單列中的「切換器」清單方塊。
      3. 從清單方塊中選取貴機構。
      4. 搜尋資料夾名稱。資料夾 ID 會顯示在資料夾名稱旁邊。

      gcloud CLI

      您可以使用下列指令,擷取位於組織層級的 Google Cloud 資料夾 ID:

      gcloud resource-manager folders list \
          --organization=$(gcloud organizations describe ORGANIZATION_NAME \
            --format="value(name.segment(1))") \
          --filter='"DISPLAY_NAME":"TOP_LEVEL_FOLDER_NAME"' \
          --format="value(ID)"

      其中 TOP_LEVEL_FOLDER_NAME 是資料夾名稱的部分或完整字串比對。移除 --format 旗標,即可查看找到的資料夾詳細資訊。

      上述指令不會傳回資料夾內子資料夾的 ID。如要這麼做,請使用頂層資料夾的 ID 執行下列指令:

      gcloud resource-manager folders list --folder=FOLDER_ID
    • organizations/ORGANIZATION_ID,其中 ORGANIZATION_ID 是組織 ID,代表要檢查有效 IAM 政策的最高層級。

      如何找出 Google Cloud 組織的 ID

      Google Cloud 控制台

      如要找出 Google Cloud 組織的 ID,請完成下列步驟:

      1. 前往 Google Cloud 控制台。

        前往 Google Cloud 控制台

      2. 按一下選單列中的「切換器」清單方塊。
      3. 從清單方塊中選取貴機構。
      4. 按一下「全部」分頁標籤。組織 ID 會顯示在組織名稱旁邊。

      gcloud CLI

      您可以使用下列指令,擷取 Google Cloud 組織的 ID:

      gcloud organizations describe ORGANIZATION_NAME --format="value(name.segment(1))"
  • ASSET_NAME_#:以半形逗號分隔的資產全名清單。

一次最多可擷取 20 項資產的政策。

如要瞭解所有選項,請參閱 gcloud CLI 參考資料。

範例

執行下列指令,查看 my-instance Compute Engine 執行個體上從 ID 為 my-organization-id 的組織繼承的有效 IAM 政策。

如何找出 Google Cloud 組織的 ID

Google Cloud 控制台

如要找出 Google Cloud 組織的 ID,請完成下列步驟:

  1. 前往 Google Cloud 控制台。

    前往 Google Cloud 控制台

  2. 按一下選單列中的「切換器」清單方塊。
  3. 從清單方塊中選取貴機構。
  4. 按一下「全部」分頁標籤。組織 ID 會顯示在組織名稱旁邊。

gcloud CLI

您可以使用下列指令,擷取 Google Cloud 組織的 ID:

gcloud organizations describe ORGANIZATION_NAME --format="value(name.segment(1))"
gcloud asset get-effective-iam-policy \
    --scope=organizations/my-organization-id \
    --names=//compute.googleapis.com/projects/my-project/zones/us-central1-a/instances/my-instance

回應範例

policyResults:
- fullResourceName: //compute.googleapis.com/projects/my-project/zones/us-central1-a/instances/my-instance
  policies:
  - attachedResource: //cloudresourcemanager.googleapis.com/projects/my-project
    policy:
      bindings:
      - members:
        - user:alex@example.com
        - user:bola@example.com
        role: roles/cloudkms.admin
      - members:
        - serviceAccount:000000000000-compute@developer.gserviceaccount.com
        role: roles/editor
  - attachedResource: //cloudresourcemanager.googleapis.com/folders/000000000000
    policy:
      bindings:
      - members:
        - user:cruz@example.com
        role: roles/resourcemanager.folderAdmin
      - members:
        - user:dani@example.com
        role: roles/resourcemanager.folderEditor
  - attachedResource: //cloudresourcemanager.googleapis.com/organizations/0000000000000
    policy:
      bindings:
      - members:
        - user:hao@example.com
        role: organizations/0000000000000/roles/CustomProjectAdmin
      - members:
        - serviceAccount:000000000000@cloudservices.gserviceaccount.com
        - user:ira@example.com
        - user:jie@example.com
        role: roles/accesscontextmanager.policyAdmin

REST

HTTP 方法和網址:

POST https://cloudasset.googleapis.com/v1/SCOPE_PATH/effectiveIamPolicies:batchGet

JSON 要求主體:

{
  "names": [
    "ASSET_NAME_1",
    "ASSET_NAME_2",
    "..."
  ]
}

提供以下這些值:

  • SCOPE_PATH:請使用下列其中一個值:

    • projects/PROJECT_ID,其中 PROJECT_ID 是要檢查有效 IAM 政策的頂層專案 ID。
    • projects/PROJECT_NUMBER,其中 PROJECT_NUMBER 是專案編號,代表要從哪個頂層專案檢查有效的 IAM 政策。

      如何找出 Google Cloud 專案編號

      Google Cloud 控制台

      如要找出 Google Cloud 專案編號,請完成下列步驟:

      1. 前往 Google Cloud 控制台的「Welcome」(歡迎) 頁面。

        前往「歡迎」頁面

      2. 按一下選單列中的「切換器」清單方塊。
      3. 從清單方塊中選取貴機構,然後搜尋專案名稱。 專案名稱、專案編號和專案 ID 會顯示在「歡迎」標題附近。

        最多會顯示 4,000 項資源。如果找不到所需專案, 請前往「 管理資源」頁面, 然後使用該專案的名稱篩選清單。

      gcloud CLI

      您可以使用下列指令擷取 Google Cloud 專案編號:

      gcloud projects describe PROJECT_ID --format="value(projectNumber)"
    • folders/FOLDER_ID,其中 FOLDER_ID 是要檢查有效 IAM 政策的頂層資料夾 ID。

      如何找出 Google Cloud 資料夾的 ID

      Google Cloud 控制台

      如要找出 Google Cloud 資料夾的 ID,請完成下列步驟:

      1. 前往 Google Cloud 控制台。

        前往 Google Cloud 控制台

      2. 按一下選單列中的「切換器」清單方塊。
      3. 從清單方塊中選取貴機構。
      4. 搜尋資料夾名稱。資料夾 ID 會顯示在資料夾名稱旁邊。

      gcloud CLI

      您可以使用下列指令,擷取位於組織層級的 Google Cloud 資料夾 ID:

      gcloud resource-manager folders list \
          --organization=$(gcloud organizations describe ORGANIZATION_NAME \
            --format="value(name.segment(1))") \
          --filter='"DISPLAY_NAME":"TOP_LEVEL_FOLDER_NAME"' \
          --format="value(ID)"

      其中 TOP_LEVEL_FOLDER_NAME 是資料夾名稱的部分或完整字串比對。移除 --format 旗標,即可查看找到的資料夾詳細資訊。

      上述指令不會傳回資料夾內子資料夾的 ID。如要這麼做,請使用頂層資料夾的 ID 執行下列指令:

      gcloud resource-manager folders list --folder=FOLDER_ID
    • organizations/ORGANIZATION_ID,其中 ORGANIZATION_ID 是組織 ID,代表要檢查有效 IAM 政策的最高層級。

      如何找出 Google Cloud 組織的 ID

      Google Cloud 控制台

      如要找出 Google Cloud 組織的 ID,請完成下列步驟:

      1. 前往 Google Cloud 控制台。

        前往 Google Cloud 控制台

      2. 按一下選單列中的「切換器」清單方塊。
      3. 從清單方塊中選取貴機構。
      4. 按一下「全部」分頁標籤。組織 ID 會顯示在組織名稱旁邊。

      gcloud CLI

      您可以使用下列指令,擷取 Google Cloud 組織的 ID:

      gcloud organizations describe ORGANIZATION_NAME --format="value(name.segment(1))"
  • ASSET_NAME_#:資產全名陣列。

一次最多可擷取 20 項資產的政策。

如要瞭解所有選項,請參閱 REST 參考資料。

指令範例

執行下列其中一項指令,查看 my-instance Compute Engine 執行個體上繼承自 ID 為 my-organization-id 的組織的有效 IAM 政策。

如何找出 Google Cloud 組織的 ID

Google Cloud 控制台

如要找出 Google Cloud 組織的 ID,請完成下列步驟:

  1. 前往 Google Cloud 控制台。

    前往 Google Cloud 控制台

  2. 按一下選單列中的「切換器」清單方塊。
  3. 從清單方塊中選取貴機構。
  4. 按一下「全部」分頁標籤。組織 ID 會顯示在組織名稱旁邊。

gcloud CLI

您可以使用下列指令,擷取 Google Cloud 組織的 ID:

gcloud organizations describe ORGANIZATION_NAME --format="value(name.segment(1))"

curl (Linux、macOS 或 Cloud Shell)

curl -X POST \
     -H "X-HTTP-Method-Override: GET" \
     -H "Authorization: Bearer $(gcloud auth print-access-token)" \
     -H "Content-Type: application/json; charset=utf-8" \
     -d '{
            "names": [
              "//compute.googleapis.com/projects/my-project/zones/us-central1-a/instances/my-instance"
            ]
          }' \
     https://cloudasset.googleapis.com/v1/organizations/my-organization-id/effectiveIamPolicies:batchGet

PowerShell (Windows)

$cred = gcloud auth print-access-token

$headers = @{ 
  "X-HTTP-Method-Override" = "GET";
  "Authorization" = "Bearer $cred"
}


$body = @"
{
  "names": [
    "//compute.googleapis.com/projects/my-project/zones/us-central1-a/instances/my-instance"
  ]
}
"@

Invoke-WebRequest `
  -Method POST `
  -Headers $headers `
  -ContentType: "application/json; charset=utf-8" `
  -Body $body `
  -Uri "https://cloudasset.googleapis.com/v1/organizations/my-organization-id/effectiveIamPolicies:batchGet" | Select-Object -Expand Content

回應範例

{
  "policyResults": [
    {
      "fullResourceName": "//compute.googleapis.com/projects/my-project/zones/us-central1-a/instances/instance-1",
      "policies": [
        {
          "attachedResource": "//cloudresourcemanager.googleapis.com/projects/my-project",
          "policy": {
            "bindings": [
              {
                "role": "roles/cloudkms.admin",
                "members": [
                  "user:alex@example.com",
                  "user:bola@example.com"
                ]
              },
              {
                "role": "roles/editor",
                "members": [
                  "serviceAccount:000000000000-compute@developer.gserviceaccount.com"
                ]
              }
            ]
          }
        },
        {
          "attachedResource": "//cloudresourcemanager.googleapis.com/folders/000000000000",
          "policy": {
            "bindings": [
              {
                "role": "roles/resourcemanager.folderAdmin",
                "members": [
                  "user:cruz@example.com"
                ]
              },
              {
                "role": "roles/resourcemanager.folderEditor",
                "members": [
                  "user:dani@example.com"
                ]
              }
            ]
          }
        },
        {
          "attachedResource": "//cloudresourcemanager.googleapis.com/organizations/000000000000",
          "policy": {
            "bindings": [
              {
                "role": "organizations/1060291660910/roles/CustomProjectAdmin",
                "members": [
                  "user:hao@example.com"
                ]
              },
              {
                "role": "roles/accesscontextmanager.policyAdmin",
                "members": [
                  "serviceAccount:000000000000@cloudservices.gserviceaccount.com",
                  "user:ira@example.com",
                  "user:jie@example.com"
                ]
              }
            ]
          }
        }
      ]
    }
  ]
}