搜尋資源的 IAM 允許政策

您可以搜尋專案、資料夾或組織中資源的 IAM 允許政策,並使用查詢篩選傳回的結果。

事前準備

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

    啟用 Cloud Asset Inventory API

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

建構查詢

建構查詢前,建議先從「不」指定查詢的搜尋要求開始。使用完整回應中的欄位和值,透過搜尋查詢語法建立查詢,並加以調整,直到傳回所需結果。

如要進一步瞭解查詢中可使用的欄位,請參閱 IamPolicySearchResult 參考文件。

建構查詢時,請注意下列限制:

  • policy 欄位是巢狀物件,因此只能與 : 運算子搭配使用。

  • 部分資產類型無法搜尋。請參閱「資源類型」,確認搜尋 API 是否不支援某項服務。

您也可以使用下列額外欄位,將結果限制在特定主體類型、權限或角色:

欄位 說明
memberTypes

包含下列其中一種 IAM 主體類型:

  • user
  • group
  • domain
  • serviceAccount
  • allUsers
  • allAuthenticatedUsers
範例
memberTypes=user
policy.role.permissions

包含特定 IAM 權限。

範例
policy.role.permissions=storage.buckets.create
roles

包含特定 IAM 角色。

範例
roles=roles/storage.objectAdmin

搜尋 IAM 允許政策

建構查詢前,建議先從「不」指定查詢的搜尋要求開始。使用完整回應中的欄位和值,透過搜尋查詢語法建立查詢,並加以調整,直到傳回所需結果。

控制台

如要搜尋 IAM 允許政策中繼資料,請完成下列步驟。

  1. 前往 Google Cloud 控制台的「Asset Inventory」(資產目錄) 頁面。

    前往 Asset Inventory

  2. 變更為要搜尋的專案、資料夾或組織。
  3. 按一下「IAM policy」(IAM 政策) 分頁標籤。
  4. 如要搜尋允許政策,請在「Filter」(篩選器) 欄位中輸入查詢。如要瞭解如何編寫搜尋查詢,請參閱「搜尋查詢語法」。

    如要輕鬆建構查詢,請按一下「篩選器」搜尋欄位,顯示可搜尋的欄位並新增至查詢。

執行搜尋後,符合查詢條件的允許政策會列在「Results」(結果) 表格中。按兩下查詢即可編輯,或使用「Filter results」(篩選結果) 窗格套用快速「Query presets」(查詢預設),或依特定條件限制搜尋結果。

如要以 Google Cloud CLI 指令的形式查看查詢,請按一下「View query」(查看查詢)。

如要匯出結果,請按一下「下載 CSV」。

gcloud

gcloud asset search-all-iam-policies \
    --scope=SCOPE_PATH \
    --query="QUERY" \
    --asset-types=ASSET_TYPE_1,ASSET_TYPE_2,... \
    --order-by="ORDER_BY"

提供以下這些值:

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

    • projects/PROJECT_ID,其中 PROJECT_ID 是專案 ID,該專案包含您要搜尋的 IAM 允許政策資產。
    • projects/PROJECT_NUMBER,其中 PROJECT_NUMBER 是專案編號,該專案包含您要搜尋的身分與存取權管理允許政策資產。

      如何找出 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 是資料夾 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 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))"
  • QUERY:選用。查詢運算式。如果未指定或留空,系統會在指定範圍內搜尋所有資源。如要瞭解如何撰寫搜尋查詢,請參閱「搜尋查詢語法」。
  • ASSET_TYPE_#:選用。以半形逗號分隔的可搜尋資產類型清單。支援與 RE2 相容的規則運算式。如果規則運算式與任何支援的資產類型都不相符,系統會傳回 INVALID_ARGUMENT 錯誤。如未指定 --asset-types,系統會傳回所有資產類型。
  • ORDER_BY:選用。以半形逗號分隔的欄位清單,用於指定結果的排序順序。預設為遞增排序。在欄位名稱後方加上 DESC,表示遞減順序。如需可排序的欄位清單,請參閱參考文件。

您可以使用 --format 和 --flatten 旗標,設定 gcloud CLI 輸出的格式。

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

範例

執行下列指令,取得 my-project 專案中所有 Compute Engine 執行個體 (compute.googleapis.com/Instance) 的清單,並透過 IAM 允許政策將這些執行個體繫結至使用者 alex@example.com。結果會依資源 (resource DESC) 遞減排序。

gcloud asset search-all-iam-policies \
    --scope=projects/my-project \
    --query="policy:\"user:alex@example.com\"" \
    --asset-types=compute.googleapis.com/Instance \
    --order-by="resource DESC"

回應範例

---
assetType: compute.googleapis.com/Instance
folders:
- folders/0000000000000
organization: organizations/0000000000000
policy:
  bindings:
  - members:
    - user:alex@example.com
    role: roles/compute.viewer
  - members:
    - user:alex@example.com
    role: roles/editor
  - members:
    - user:alex@example.com
    role: roles/owner
project: projects/0000000000000
resource: //compute.googleapis.com/projects/my-project/zones/us-central1-a/instances/debian

REST

HTTP 方法和網址:

POST https://cloudasset.googleapis.com/v1/SCOPE_PATH:searchAllIamPolicies

JSON 要求主體:

{
  "query": "QUERY",
  "assetTypes": [
    "ASSET_TYPE_1",
    "ASSET_TYPE_2",
    "..."
  ],
  "orderBy": "ORDER_BY",
  "pageSize": "PAGE_SIZE",
  "pageToken": "PAGE_TOKEN"
}

提供以下這些值:

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

    • projects/PROJECT_ID,其中 PROJECT_ID 是專案 ID,該專案包含您要搜尋的 IAM 允許政策資產。
    • projects/PROJECT_NUMBER,其中 PROJECT_NUMBER 是專案編號,該專案包含您要搜尋的身分與存取權管理允許政策資產。

      如何找出 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 是資料夾 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 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))"
  • QUERY:選用。查詢運算式。如果未指定或留空,系統會在指定範圍內搜尋所有資源。如要瞭解如何撰寫搜尋查詢,請參閱「搜尋查詢語法」。
  • ASSET_TYPE_#:選用。可搜尋的素材資源類型陣列。支援與 RE2 相容的規則運算式。如果規則運算式與任何支援的資產類型都不相符,系統會傳回 INVALID_ARGUMENT 錯誤。如未指定 assetTypes,系統會傳回所有資產類型。
  • ORDER_BY:選用。以半形逗號分隔的欄位清單,用於指定結果的排序順序。預設為遞增排序。在欄位名稱後方加上 DESC,表示遞減順序。如需可排序的欄位清單,請參閱參考文件。
  • PAGE_SIZE:選用。每頁傳回的結果數量。上限為 500。如果值設為 0 或負值,系統會選取適當的預設值。系統會傳回 nextPageToken,用於擷取後續結果。
  • PAGE_TOKEN:選用。較長的要求回應會分成多個頁面。如未指定 pageToken,系統會傳回第一頁。如要呼叫後續頁面,請將前一個回應的 nextPageToken 做為 pageToken 值。

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

指令範例

執行下列其中一項指令,即可取得 my-project 專案中所有 Compute Engine 執行個體 (compute.googleapis.com/Instance) 的清單,並透過 IAM 允許政策將這些執行個體繫結至使用者 alex@example.com。結果會依資源 (resource DESC) 遞減排序。

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 '{
            "query": "policy:\"user:alex@example.com\"",
            "assetTypes": ["compute.googleapis.com/Instance"],
            "orderBy": "resource DESC"
          }' \
     https://cloudasset.googleapis.com/v1/projects/my-project:searchAllIamPolicies

PowerShell (Windows)

$cred = gcloud auth print-access-token

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


$body = @"
{
  "query": "policy:\"user:alex@example.com\"",
  "assetTypes": ["compute.googleapis.com/Instance"],
  "orderBy": "resource DESC"
}
"@

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

回應範例

{
  "resource": "//compute.googleapis.com/projects/my-project/zones/us-central1-a/instances/debian",
  "project": "projects/0000000000000",
  "policy": {
    "bindings": [
      {
        "role": "roles/compute.viewer",
        "members": [
          "user:alex@example.com"
        ]
      },
      {
        "role": "roles/editor",
        "members": [
          "user:alex@example.com"
        ]
      },
      {
        "role": "roles/owner",
        "members": [
          "user:alex@example.com"
        ]
      }
    ]
  },
  "assetType": "compute.googleapis.com/Instance",
  "folders": [
    "folders/0000000000000"
  ],
  "organization": "organizations/0000000000000"
}

C#

如要瞭解如何安裝及使用 Cloud Asset Inventory 的用戶端程式庫,請參閱「Cloud Asset Inventory 用戶端程式庫」。

如要向 Cloud Asset Inventory 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。


using Google.Api.Gax;
using Google.Cloud.Asset.V1;
using System.Collections.Generic;
using System.Linq;

public class SearchAllIamPoliciesSample
{
    public SearchAllIamPoliciesResponse SearchAllIamPolicies(string scope, string query)
    {
        // Create the client.
        AssetServiceClient client = AssetServiceClient.Create();

        // Build the request.
        SearchAllIamPoliciesRequest request = new SearchAllIamPoliciesRequest
        {
            Scope = scope,
            Query = query,
        };

        // Call the API.
        PagedEnumerable<SearchAllIamPoliciesResponse, IamPolicySearchResult> response = client.SearchAllIamPolicies(request);

        // Return the first page.
        IEnumerable<SearchAllIamPoliciesResponse> byPages = response.AsRawResponses();
        return byPages.First();
    }
}

Go

如要瞭解如何安裝及使用 Cloud Asset Inventory 的用戶端程式庫,請參閱「Cloud Asset Inventory 用戶端程式庫」。

如要向 Cloud Asset Inventory 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。


// Sample search-all-iam-policies searches all IAM policies within the given scope.
package main

import (
	"context"
	"flag"
	"fmt"
	"log"

	asset "cloud.google.com/go/asset/apiv1"
	"cloud.google.com/go/asset/apiv1/assetpb"
	"google.golang.org/api/iterator"
)

func main() {
	scope := flag.String("scope", "", "Scope of the search.")
	query := flag.String("query", "", "Query statement.")
	flag.Parse()
	ctx := context.Background()
	client, err := asset.NewClient(ctx)
	if err != nil {
		log.Fatalf("asset.NewClient: %v", err)
	}
	defer client.Close()

	req := &assetpb.SearchAllIamPoliciesRequest{
		Scope: *scope,
		Query: *query,
	}
	it := client.SearchAllIamPolicies(ctx, req)
	for {
		policy, err := it.Next()
		if err == iterator.Done {
			break
		}
		if err != nil {
			log.Fatal(err)
		}
		fmt.Println(policy)
	}
}

Java

如要瞭解如何安裝及使用 Cloud Asset Inventory 的用戶端程式庫,請參閱「Cloud Asset Inventory 用戶端程式庫」。

如要向 Cloud Asset Inventory 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。

import com.google.api.gax.rpc.ApiException;
import com.google.api.gax.rpc.InvalidArgumentException;
import com.google.cloud.asset.v1.AssetServiceClient;
import com.google.cloud.asset.v1.AssetServiceClient.SearchAllIamPoliciesPagedResponse;
import com.google.cloud.asset.v1.SearchAllIamPoliciesRequest;
import java.io.IOException;

public class SearchAllIamPoliciesExample {

  // Searches for all the iam policies within the given scope.
  public static void searchAllIamPolicies(String scope, String query) {
    // TODO(developer): Replace these variables before running the sample.
    int pageSize = 0;
    String pageToken = "";

    SearchAllIamPoliciesRequest request =
        SearchAllIamPoliciesRequest.newBuilder()
            .setScope(scope)
            .setQuery(query)
            .setPageSize(pageSize)
            .setPageToken(pageToken)
            .build();

    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests. After completing all of your requests, call
    // the "close" method on the client to safely clean up any remaining background resources.
    try (AssetServiceClient client = AssetServiceClient.create()) {
      SearchAllIamPoliciesPagedResponse response = client.searchAllIamPolicies(request);
      System.out.println("Search completed successfully:\n" + response.getPage().getValues());
    } catch (IOException e) {
      System.out.println(String.format("Failed to create client:%n%s", e.toString()));
    } catch (InvalidArgumentException e) {
      System.out.println(String.format("Invalid request:%n%s", e.toString()));
    } catch (ApiException e) {
      System.out.println(String.format("Error during SearchAllIamPolicies:%n%s", e.toString()));
    }
  }
}

Node.js

如要瞭解如何安裝及使用 Cloud Asset Inventory 的用戶端程式庫,請參閱「Cloud Asset Inventory 用戶端程式庫」。

如要向 Cloud Asset Inventory 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。

/**
 * TODO(developer): Uncomment these variables before running the sample.
 */
// const scope = '';
// const query = '';
// const pageSize = 0;
// const pageToken = '';

const util = require('util');
const {AssetServiceClient} = require('@google-cloud/asset');

const client = new AssetServiceClient();
const projectId = await client.getProjectId();

async function searchAllIamPolicies() {
  const request = {
    scope: `projects/${projectId}`,
    query: query,
    pageSize: pageSize,
    pageToken: pageToken,
  };
  const options = {
    autoPaginate: false,
  };

  // Handle the operation using the promise pattern.
  const result = await client.searchAllIamPolicies(request, options);
  // Do things with with the response.
  console.log(util.inspect(result, {depth: null}));
}

PHP

如要瞭解如何安裝及使用 Cloud Asset Inventory 的用戶端程式庫,請參閱「Cloud Asset Inventory 用戶端程式庫」。

如要向 Cloud Asset Inventory 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。

use Google\Cloud\Asset\V1\Client\AssetServiceClient;
use Google\Cloud\Asset\V1\SearchAllIamPoliciesRequest;

/**
 * @param string $scope      Scope of the search
 * @param string $query      (Optional) Query statement
 * @param int    $pageSize   (Optional) Size of each result page
 * @param string $pageToken  (Optional) Token produced by the preceding call
 */
function search_all_iam_policies(
    string $scope,
    string $query = '',
    int $pageSize = 0,
    string $pageToken = ''
) {
    // Instantiate a client.
    $asset = new AssetServiceClient();

    // Run request
    $request = (new SearchAllIamPoliciesRequest())
        ->setScope($scope)
        ->setQuery($query)
        ->setPageSize($pageSize)
        ->setPageToken($pageToken);
    $response = $asset->searchAllIamPolicies($request);

    // Print the resources that the policies are set on
    foreach ($response->getPage() as $policy) {
        print($policy->getResource() . PHP_EOL);
    }
}

Python

如要瞭解如何安裝及使用 Cloud Asset Inventory 的用戶端程式庫,請參閱「Cloud Asset Inventory 用戶端程式庫」。

如要向 Cloud Asset Inventory 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。

from google.cloud import asset_v1

# TODO scope = 'Scope of the search'
# TODO query = 'Query statement'
# TODO page_size = Size of each result page

client = asset_v1.AssetServiceClient()
response = client.search_all_iam_policies(
    request={"scope": scope, "query": query, "page_size": page_size}
)
for policy in response:
    print(policy)
    break

小茹

如要瞭解如何安裝及使用 Cloud Asset Inventory 的用戶端程式庫,請參閱「Cloud Asset Inventory 用戶端程式庫」。

如要向 Cloud Asset Inventory 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。

require "google/cloud/asset"

# scope = 'SCOPE_OF_THE_QUERY'
# query = 'QUERY_STATEMENT'
# page_size = 'SIZE_OF_EACH_RESULT_PAGE'
# page_token = 'TOKEN_PRODUCED_BY_THE_PRECEDING_CALL'
asset_service = Google::Cloud::Asset.asset_service

response = asset_service.search_all_iam_policies(
  scope:      scope,
  query:      query,
  page_size:  page_size,
  page_token: page_token
)
# Do things with the response
response.page.each do |policy|
  puts policy
end

其他搜尋範例

下列程式碼範例顯示 gcloud 和 REST 的特定搜尋查詢,協助您建構自己的搜尋。

設有 IAM 允許政策的資源

下列範例說明如何搜尋 my-project 專案中具有 IAM 允許政策的所有資源。

gcloud

gcloud asset search-all-iam-policies \
    --scope=projects/my-project \
    --flatten="policy.bindings[].members[]" \
    --format="table(resource, policy.bindings.role, policy.bindings.members)"

REST

HTTP 方法和網址:

GET https://cloudasset.googleapis.com/v1/projects/my-project:searchAllIamPolicies

指令範例

curl (Linux、macOS 或 Cloud Shell)

curl -X GET \
     -H "Authorization: Bearer $(gcloud auth print-access-token)" \
     https://cloudasset.googleapis.com/v1/projects/my-project:searchAllIamPolicies

PowerShell (Windows)

$cred = gcloud auth print-access-token

$headers = @{ 
  "Authorization" = "Bearer $cred"
}


Invoke-WebRequest `
  -Method GET `
  -Headers $headers `
  -Uri "https://cloudasset.googleapis.com/v1/projects/my-project:searchAllIamPolicies" | Select-Object -Expand Content

具有 IAM 允許政策的具名資源

以下範例說明如何搜尋名稱中含有 example 且具備 IAM 允許政策的所有資源。

gcloud

gcloud asset search-all-iam-policies \
    --scope=projects/my-project \
    --query="resource:example"

REST

HTTP 方法和網址:

POST https://cloudasset.googleapis.com/v1/projects/my-project:searchAllIamPolicies

JSON 要求主體:

{
  "pageSize": 1,
  "query": "resource:example"
}

指令範例

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 '{
            "pageSize": 1,
            "query": "resource:example"
          }' \
     https://cloudasset.googleapis.com/v1/projects/my-project:searchAllIamPolicies

PowerShell (Windows)

$cred = gcloud auth print-access-token

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


$body = @"
{
  "pageSize": 1,
  "query": "resource:example"
}
"@

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

專案、資料夾和組織的 IAM 允許政策

下列範例說明如何搜尋 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

gcloud asset search-all-iam-policies \
    --scope=organizations/my-organization-id \
    --asset-types=cloudresourcemanager.*

REST

HTTP 方法和網址:

POST https://cloudasset.googleapis.com/v1/organizations/my-organization-id:searchAllIamPolicies

JSON 要求主體:

{
  "assetTypes": "cloudresourcemanager.*",
  "pageSize": 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 '{
            "assetTypes": "cloudresourcemanager.*",
            "pageSize": 1,
          }' \
     https://cloudasset.googleapis.com/v1/organizations/my-organization-id:searchAllIamPolicies

PowerShell (Windows)

$cred = gcloud auth print-access-token

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


$body = @"
{
  "assetTypes": "cloudresourcemanager.*",
  "pageSize": 1,
}
"@

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

專案擁有者

以下範例說明如何搜尋在專案 my-project 中具備擁有者角色 (roles/owner) 的主體。

這項要求只會傳回已獲派專案擁有者角色的主體。不包括透過政策繼承取得擁有者角色的主體。

gcloud

gcloud asset search-all-iam-policies \
    --scope=projects/my-project \
    --query="roles:roles/owner" \
    --asset-types=cloudresourcemanager.* \
    --flatten="policy.bindings[].members[]" \
    --format="table(policy.bindings.members)"

REST

HTTP 方法和網址:

POST https://cloudasset.googleapis.com/v1/projects/my-project:searchAllIamPolicies

JSON 要求主體:

{
  "assetTypes": "cloudresourcemanager.*",
  "pageSize": 1,
  "query": "roles:roles/owner"
}

指令範例

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 '{
            "assetTypes": "cloudresourcemanager.*",
            "pageSize": 1,
            "query": "roles:roles/owner"
          }' \
     https://cloudasset.googleapis.com/v1/projects/my-project:searchAllIamPolicies

PowerShell (Windows)

$cred = gcloud auth print-access-token

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


$body = @"
{
  "assetTypes": "cloudresourcemanager.*",
  "pageSize": 1,
  "query": "roles:roles/owner"
}
"@

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

主體具備擁有者角色的專案

下列範例說明如何搜尋 alex@example.com 在 ID 為 my-organization-id 的組織中,擁有擁有者角色 (roles/owner) 的專案。

這項要求只會傳回已授予 alex@example.com 擁有者角色的專案。不包含 alex@example.com 繼承擁有者角色的專案。

如何找出 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

gcloud asset search-all-iam-policies \
    --scope=organizations/my-organization-id \
    --query="policy:(roles/owner alex@example.com)" \
    --asset-types=cloudresourcemanager.googleapis.com/Project \
    --format="table(resource)"

REST

HTTP 方法和網址:

POST https://cloudasset.googleapis.com/v1/organizations/my-organization-id:searchAllIamPolicies

JSON 要求主體:

{
  "assetTypes": "cloudresourcemanager.googleapis.com/Project",
  "pageSize": 1,
  "query": "policy:(roles/owner alex@example.com)"
}

指令範例

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 '{
            "assetTypes": "cloudresourcemanager.googleapis.com/Project",
            "pageSize": 1,
            "query": "policy:(roles/owner alex@example.com)"
          }' \
     https://cloudasset.googleapis.com/v1/organizations/my-organization-id:searchAllIamPolicies

PowerShell (Windows)

$cred = gcloud auth print-access-token

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


$body = @"
{
  "assetTypes": "cloudresourcemanager.googleapis.com/Project",
  "pageSize": 1,
  "query": "policy:(roles/owner alex@example.com)"
}
"@

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

主體在專案中具備的角色

以下範例說明如何搜尋 alex@example.com 在 my-project 專案中的角色。

這項要求只會傳回 alex@example.com 在專案中獲得的角色。不含 alex@example.com 透過政策繼承沿用的角色。

gcloud

gcloud asset search-all-iam-policies \
    --scope=projects/my-project \
    --query="policy:alex@example.com" \
    --asset-types=cloudresourcemanager.googleapis.com/Project \
    --flatten="policy.bindings[]" \
    --format="table(policy.bindings.role)"

REST

HTTP 方法和網址:

POST https://cloudasset.googleapis.com/v1/projects/my-project:searchAllIamPolicies

JSON 要求主體:

{
  "assetTypes": "cloudresourcemanager.googleapis.com/Project",
  "pageSize": 1,
  "query": "policy:alex@example.com"
}

指令範例

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 '{
            "assetTypes": "cloudresourcemanager.googleapis.com/Project",
            "pageSize": 1,
            "query": "policy:alex@example.com"
          }' \
     https://cloudasset.googleapis.com/v1/projects/my-project:searchAllIamPolicies

PowerShell (Windows)

$cred = gcloud auth print-access-token

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


$body = @"
{
  "assetTypes": "cloudresourcemanager.googleapis.com/Project",
  "pageSize": 1,
  "query": "policy:alex@example.com"
}
"@

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

主體對專案擁有的權限

以下範例說明如何搜尋 alex@example.com 在 my-project 專案中擁有的權限。

這項要求只會傳回 alex@example.com 在專案中擁有的權限。不含 alex@example.com 透過政策繼承沿用的權限。

gcloud

gcloud asset search-all-iam-policies \
    --scope=projects/my-project \
    --query="policy:alex@example.com policy.role.permissions:\"\"" \
    --asset-types=cloudresourcemanager.* \
    --format="default(explanation.matchedPermissions)"

REST

HTTP 方法和網址:

POST https://cloudasset.googleapis.com/v1/projects/my-project:searchAllIamPolicies

JSON 要求主體:

{
  "assetTypes": "cloudresourcemanager.*",
  "pageSize": 1,
  "query": "policy:alex@example.com  policy.role.permissions:\"\""
}

指令範例

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 '{
            "assetTypes": "cloudresourcemanager.*",
            "pageSize": 1,
            "query": "policy:alex@example.com  policy.role.permissions:\"\""
          }' \
     https://cloudasset.googleapis.com/v1/projects/my-project:searchAllIamPolicies

PowerShell (Windows)

$cred = gcloud auth print-access-token

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


$body = @"
{
  "assetTypes": "cloudresourcemanager.*",
  "pageSize": 1,
  "query": "policy:alex@example.com  policy.role.permissions:\"\""
}
"@

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

可存取 Cloud Storage bucket 的主體

以下範例說明如何搜尋可存取 my-project 專案中 Cloud Storage bucket 的主體。

gcloud

gcloud asset search-all-iam-policies \
    --scope=projects/my-project \
    --query="policy.role.permissions:storage.buckets" \
    --asset-types=cloudresourcemanager.* \
    --flatten="policy.bindings[].members[]" \
    --format="table(policy.bindings.members)"

REST

HTTP 方法和網址:

POST https://cloudasset.googleapis.com/v1/projects/my-project:searchAllIamPolicies

JSON 要求主體:

{
  "assetTypes": "cloudresourcemanager.*",
  "pageSize": 1,
  "query": "policy.role.permissions:storage.buckets"
}

指令範例

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 '{
            "assetTypes": "cloudresourcemanager.*",
            "pageSize": 1,
            "query": "policy.role.permissions:storage.buckets"
          }' \
     https://cloudasset.googleapis.com/v1/projects/my-project:searchAllIamPolicies

PowerShell (Windows)

$cred = gcloud auth print-access-token

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


$body = @"
{
  "assetTypes": "cloudresourcemanager.*",
  "pageSize": 1,
  "query": "policy.role.permissions:storage.buckets"
}
"@

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

具備擁有者角色的服務帳戶

以下範例說明如何搜尋組織中具有擁有者角色 (roles/owner) 的服務帳戶,其中 my-organization-id。您可以使用這項查詢,協助降低風險。

如何找出 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

本範例使用 `grep` 指令,該指令可在 Cloud Shell 和 Unix 類作業系統中使用。

gcloud asset search-all-iam-policies \
    --scope=organizations/my-organization-id \
    --query="policy:(roles/owner serviceAccount)" \
    --flatten="policy.bindings[].members[]" \
    --format="table(resource.segment(3):label=RESOURCE_TYPE, resource.basename():label=RESOURCE, policy.bindings.members)" |
    grep serviceAccount

REST

HTTP 方法和網址:

POST https://cloudasset.googleapis.com/v1/organizations/my-organization-id:searchAllIamPolicies

JSON 要求主體:

{
  "pageSize": 1,
  "query": "policy:(roles/owner serviceAccount)"
}

指令範例

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 '{
            "pageSize": 1,
            "query": "policy:(roles/owner serviceAccount)"
          }' \
     https://cloudasset.googleapis.com/v1/organizations/my-organization-id:searchAllIamPolicies

PowerShell (Windows)

$cred = gcloud auth print-access-token

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


$body = @"
{
  "pageSize": 1,
  "query": "policy:(roles/owner serviceAccount)"
}
"@

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

角色授予網域的資源

以下範例說明如何搜尋 ID 為 my-organization-id 的組織中,授予 example.com 網域角色的資源。

如何找出 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

gcloud asset search-all-iam-policies \
    --scope=organizations/my-organization-id \
    --query="policy:\"domain:example.com\"" \
    --flatten="policy.bindings[]" \
    --format="table(resource, policy.bindings.role)"

REST

HTTP 方法和網址:

POST https://cloudasset.googleapis.com/v1/organizations/my-organization-id:searchAllIamPolicies

JSON 要求主體:

{
  "pageSize": 1,
  "query": "policy:\"domain:DOMAIN_NAME\""
}

指令範例

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 '{
            "pageSize": 1,
            "query": "policy:\"domain:DOMAIN_NAME\""
          }' \
     https://cloudasset.googleapis.com/v1/organizations/my-organization-id:searchAllIamPolicies

PowerShell (Windows)

$cred = gcloud auth print-access-token

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


$body = @"
{
  "pageSize": 1,
  "query": "policy:\"domain:DOMAIN_NAME\""
}
"@

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

已將角色授予公開對象的資源

下列範例說明如何在 ID 為 my-organization-id 的組織中,搜尋已授予公開角色的資源。

如何找出 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

gcloud asset search-all-iam-policies \
    --scope=organizations/my-organization-id \
    --query="memberTypes:(allUsers OR allAuthenticatedUsers)" \
    --format="table(resource)"

REST

HTTP 方法和網址:

POST https://cloudasset.googleapis.com/v1/organizations/my-organization-id:searchAllIamPolicies

JSON 要求主體:

{
  "pageSize": 1,
  "query": "memberTypes:(allUsers OR allAuthenticatedUsers)"
}

指令範例

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 '{
            "pageSize": 1,
            "query": "memberTypes:(allUsers OR allAuthenticatedUsers)"
          }' \
     https://cloudasset.googleapis.com/v1/organizations/my-organization-id:searchAllIamPolicies

PowerShell (Windows)

$cred = gcloud auth print-access-token

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


$body = @"
{
  "pageSize": 1,
  "query": "memberTypes:(allUsers OR allAuthenticatedUsers)"
}
"@

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

可變更組織中 IAM 允許政策的主體

下列範例說明如何搜尋可變更組織中 IAM 允許政策的主體,組織 ID 為 my-organization-id。

如何找出 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

gcloud asset search-all-iam-policies \
    --scope=organizations/my-organization-id \
    --query="policy.role.permissions:(resourcemanager.organizations.setIamPolicy OR resourcemanager.folders.setIamPolicy OR resourcemanager.projects.setIamPolicy)" \
    --format="json(resource, policy.bindings, explanation.matchedPermissions)"

REST

HTTP 方法和網址:

POST https://cloudasset.googleapis.com/v1/organizations/my-organization-id:searchAllIamPolicies

JSON 要求主體:

{
  "pageSize": 1,
  "query": "policy.role.permissions:(resourcemanager.organizations.setIamPolicy OR resourcemanager.folders.setIamPolicy OR resourcemanager.projects.setIamPolicy)"
}

指令範例

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 '{
            "pageSize": 1,
            "query": "policy.role.permissions:(resourcemanager.organizations.setIamPolicy OR resourcemanager.folders.setIamPolicy OR resourcemanager.projects.setIamPolicy)"
          }' \
     https://cloudasset.googleapis.com/v1/organizations/my-organization-id:searchAllIamPolicies

PowerShell (Windows)

$cred = gcloud auth print-access-token

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


$body = @"
{
  "pageSize": 1,
  "query": "policy.role.permissions:(resourcemanager.organizations.setIamPolicy OR resourcemanager.folders.setIamPolicy OR resourcemanager.projects.setIamPolicy)"
}
"@

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