您可以搜尋專案、資料夾或組織中資源的 IAM 允許政策,並使用查詢篩選傳回的結果。
事前準備
在執行 Cloud Asset Inventory 指令的專案中,啟用 Cloud Asset Inventory API。
確認帳戶具備呼叫 Cloud Asset Inventory API 的正確角色。如要瞭解各個呼叫類型的個別權限,請參閱「權限」。
建構查詢
建構查詢前,建議先從「不」指定查詢的搜尋要求開始。使用完整回應中的欄位和值,透過搜尋查詢語法建立查詢,並加以調整,直到傳回所需結果。
如要進一步瞭解查詢中可使用的欄位,請參閱 IamPolicySearchResult 參考文件。
建構查詢時,請注意下列限制:
您也可以使用下列額外欄位,將結果限制在特定主體類型、權限或角色:
| 欄位 | 說明 |
|---|---|
memberTypes |
包含下列其中一種 IAM 主體類型:
範例memberTypes=user |
policy.role.permissions |
包含特定 IAM 權限。 範例policy.role.permissions=storage.buckets.create |
roles |
包含特定 IAM 角色。 範例roles=roles/storage.objectAdmin |
搜尋 IAM 允許政策
建構查詢前,建議先從「不」指定查詢的搜尋要求開始。使用完整回應中的欄位和值,透過搜尋查詢語法建立查詢,並加以調整,直到傳回所需結果。
控制台
如要搜尋 IAM 允許政策中繼資料,請完成下列步驟。
-
前往 Google Cloud 控制台的「Asset Inventory」(資產目錄) 頁面。
- 變更為要搜尋的專案、資料夾或組織。
- 按一下「IAM policy」(IAM 政策) 分頁標籤。
-
如要搜尋允許政策,請在「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 專案編號,請完成下列步驟:
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,請完成下列步驟:
-
前往 Google Cloud 控制台。
- 按一下選單列中的「切換器」清單方塊。
- 從清單方塊中選取貴機構。
- 搜尋資料夾名稱。資料夾 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,請完成下列步驟:
-
前往 Google Cloud 控制台。
- 按一下選單列中的「切換器」清單方塊。
- 從清單方塊中選取貴機構。
- 按一下「全部」分頁標籤。組織 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 專案編號,請完成下列步驟:
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,請完成下列步驟:
-
前往 Google Cloud 控制台。
- 按一下選單列中的「切換器」清單方塊。
- 從清單方塊中選取貴機構。
- 搜尋資料夾名稱。資料夾 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,請完成下列步驟:
-
前往 Google Cloud 控制台。
- 按一下選單列中的「切換器」清單方塊。
- 從清單方塊中選取貴機構。
- 按一下「全部」分頁標籤。組織 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 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。
Go
如要瞭解如何安裝及使用 Cloud Asset Inventory 的用戶端程式庫,請參閱「Cloud Asset Inventory 用戶端程式庫」。
如要向 Cloud Asset Inventory 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。
Java
如要瞭解如何安裝及使用 Cloud Asset Inventory 的用戶端程式庫,請參閱「Cloud Asset Inventory 用戶端程式庫」。
如要向 Cloud Asset Inventory 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。
Node.js
如要瞭解如何安裝及使用 Cloud Asset Inventory 的用戶端程式庫,請參閱「Cloud Asset Inventory 用戶端程式庫」。
如要向 Cloud Asset Inventory 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。
PHP
如要瞭解如何安裝及使用 Cloud Asset Inventory 的用戶端程式庫,請參閱「Cloud Asset Inventory 用戶端程式庫」。
如要向 Cloud Asset Inventory 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。
Python
如要瞭解如何安裝及使用 Cloud Asset Inventory 的用戶端程式庫,請參閱「Cloud Asset Inventory 用戶端程式庫」。
如要向 Cloud Asset Inventory 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。
小茹
如要瞭解如何安裝及使用 Cloud Asset Inventory 的用戶端程式庫,請參閱「Cloud Asset Inventory 用戶端程式庫」。
如要向 Cloud Asset Inventory 進行驗證,請設定應用程式預設憑證。詳情請參閱「為本機開發環境設定驗證機制」。
其他搜尋範例
下列程式碼範例顯示 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,請完成下列步驟:
-
前往 Google Cloud 控制台。
- 按一下選單列中的「切換器」清單方塊。
- 從清單方塊中選取貴機構。
- 按一下「全部」分頁標籤。組織 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,請完成下列步驟:
-
前往 Google Cloud 控制台。
- 按一下選單列中的「切換器」清單方塊。
- 從清單方塊中選取貴機構。
- 按一下「全部」分頁標籤。組織 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,請完成下列步驟:
-
前往 Google Cloud 控制台。
- 按一下選單列中的「切換器」清單方塊。
- 從清單方塊中選取貴機構。
- 按一下「全部」分頁標籤。組織 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,請完成下列步驟:
-
前往 Google Cloud 控制台。
- 按一下選單列中的「切換器」清單方塊。
- 從清單方塊中選取貴機構。
- 按一下「全部」分頁標籤。組織 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,請完成下列步驟:
-
前往 Google Cloud 控制台。
- 按一下選單列中的「切換器」清單方塊。
- 從清單方塊中選取貴機構。
- 按一下「全部」分頁標籤。組織 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,請完成下列步驟:
-
前往 Google Cloud 控制台。
- 按一下選單列中的「切換器」清單方塊。
- 從清單方塊中選取貴機構。
- 按一下「全部」分頁標籤。組織 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