查看可針對資源授予的角色

將 Identity and Access Management (IAM) 角色授予資源使用者前,您可能想知道有哪些角色可授予特定資源。

事前準備

  • Enable the IAM API.

    Enable the API

  • 設定驗證方法。

    Select the tab for how you plan to use the samples on this page:

    Console

    When you use the Google Cloud console to access Google Cloud services and APIs, you don't need to set up authentication.

    gcloud

      In the Google Cloud console, activate Cloud Shell.

      Activate Cloud Shell

      At the bottom of the Google Cloud console, a Cloud Shell session starts and displays a command-line prompt. Cloud Shell is a shell environment with the Google Cloud CLI already installed and with values already set for your current project. It can take a few seconds for the session to initialize.

      C#

      如要在本機開發環境中使用本頁的 .NET 範例,請安裝並初始化 gcloud CLI,然後使用使用者憑證設定應用程式預設憑證。

      1. Install the Google Cloud CLI.

      2. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

      3. To initialize the gcloud CLI, run the following command:

        gcloud init
      4. If you're using a local shell, then create local authentication credentials for your user account:

        gcloud auth application-default login

        You don't need to do this if you're using Cloud Shell.

        If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.

      詳情請參閱 Google Cloud 驗證說明文件中的「 為本機開發環境設定 ADC」。

      C++

      如要在本機開發環境中使用本頁的 C++ 範例,請安裝並初始化 gcloud CLI,然後使用使用者憑證設定應用程式預設憑證。

      1. Install the Google Cloud CLI.

      2. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

      3. To initialize the gcloud CLI, run the following command:

        gcloud init
      4. If you're using a local shell, then create local authentication credentials for your user account:

        gcloud auth application-default login

        You don't need to do this if you're using Cloud Shell.

        If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.

      詳情請參閱 Google Cloud 驗證說明文件中的「 為本機開發環境設定 ADC」。

      Go

      如要在本機開發環境中使用本頁的 Go 範例,請安裝並初始化 gcloud CLI,然後使用使用者憑證設定應用程式預設憑證。

      1. Install the Google Cloud CLI.

      2. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

      3. To initialize the gcloud CLI, run the following command:

        gcloud init
      4. If you're using a local shell, then create local authentication credentials for your user account:

        gcloud auth application-default login

        You don't need to do this if you're using Cloud Shell.

        If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.

      詳情請參閱 Google Cloud 驗證說明文件中的「 為本機開發環境設定 ADC」。

      Java

      如要在本機開發環境中使用本頁的 Java 範例,請安裝並初始化 gcloud CLI,然後使用使用者憑證設定應用程式預設憑證。

      1. Install the Google Cloud CLI.

      2. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

      3. To initialize the gcloud CLI, run the following command:

        gcloud init
      4. If you're using a local shell, then create local authentication credentials for your user account:

        gcloud auth application-default login

        You don't need to do this if you're using Cloud Shell.

        If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.

      詳情請參閱 Google Cloud 驗證說明文件中的「 為本機開發環境設定 ADC」。

      Python

      如要在本機開發環境中使用本頁的 Python 範例,請安裝並初始化 gcloud CLI,然後使用使用者憑證設定應用程式預設憑證。

      1. Install the Google Cloud CLI.

      2. If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

      3. To initialize the gcloud CLI, run the following command:

        gcloud init
      4. If you're using a local shell, then create local authentication credentials for your user account:

        gcloud auth application-default login

        You don't need to do this if you're using Cloud Shell.

        If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.

      詳情請參閱 Google Cloud 驗證說明文件中的「 為本機開發環境設定 ADC」。

      REST

      如要在本機開發環境中使用本頁的 REST API 範例,請使用您提供給 gcloud CLI 的憑證。

        After installing the Google Cloud CLI, initialize it by running the following command:

        gcloud init

        If you're using an external identity provider (IdP), you must first sign in to the gcloud CLI with your federated identity.

      詳情請參閱 Google Cloud 驗證說明文件中的「Authenticate for using REST」。

必要的角色

如要取得列出可授予角色的權限,請要求管理員為您授予要列出可授予角色的資源的安全性審查者 (roles/iam.securityReviewer) IAM 角色。如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和機構的存取權」。

這個角色具備所有資源類型的 getIamPolicy 權限。如要列出可授予的角色,您必須具備要列出可授予角色資源的 getIamPolicy 權限。舉例來說,如要列出專案可授予的角色,您必須具備 resourcemanager.projects.getIamPolicy 權限。

您或許還可透過自訂角色或其他預先定義的角色取得這些權限。

瞭解可以授予哪些角色

如果角色包含了某個資源類型的任何權限,就能針對這類資源或其上層授予這個角色。舉例來說,storage.admin 角色授予 storage.buckets.getstorage.objects.get API 的權限,因此可以針對「Storage Buckets」和「Storage Objects」資源類型授予這個角色。

此外,角色也能在其權限定義適用的資源類型「上層」授予。換句話說,您可以在 Google Cloud 資源階層中較高的資源上,授予較低層級資源的角色。以 storage.admin 角色為例,除了「Storage Buckets」外,您也能在專案或機構層級授予這個角色。

角色授予的權限只會影響指定層級或較低層級的資源,不會影響較高層級或同一層級的資源。此外,針對資源授予角色後,系統只會授予適用於指定資源的權限,而不論角色的名稱、說明或其中含有的其他權限。舉例來說,在專案層級將 resourcemanager.organizationAdmin 角色 (授予 resourcemanager.projects.list 權限) 指派給使用者,只會授予該使用者特定專案的權限。而不會允許他們列出或管理機構中的所有專案。 同理可證,對特定 Compute Engine 執行個體指派 compute.admin 角色,只會授予適用於這個執行個體的權限,而不會包含專案中的其他項目。

列出可授予的角色

您可以使用 Google Cloud 控制台、Google Cloud CLI、IAM API 或 IAM 用戶端程式庫,列出可授予的角色。

Google Cloud 主控台一律會列出您檢視資源的所有可授權角色。Google Cloud CLI、IAM API 和用戶端程式庫只會列出已啟用 API 的可授權角色。

控制台

如要查看專案、資料夾或機構可授予的角色,請按照下列步驟操作:

  1. 前往 Google Cloud 控制台的「IAM」頁面。

    前往「IAM」頁面

  2. 按一下頁面頂端的「選取專案」

  3. 選取要查看可授權角色的專案、資料夾或機構。

  4. 按一下「授予存取權」

  5. 按一下「選擇角色」。這個選單會顯示您可針對這項資源授予的所有角色,包括自訂角色。

如要查看其他資源類型可授予的角色,請按照下列步驟操作:

  1. 前往 Google Cloud 控制台,找出您要查看可授予角色的資源,並前往該資源的頁面。

    舉例來說,如要管理 Compute Engine 執行個體的存取權,請前往「VM instances」(VM 執行個體) 頁面。

    前往 VM 執行個體

  2. 找出要查看可授予角色的資源,然後勾選旁邊的核取方塊。

  3. 確認資訊面板顯示在畫面上。如果沒有顯示,請按一下「顯示資訊面板」

  4. 按一下「新增主體」

  5. 按一下「選擇角色」。這個選單會顯示您可針對這項資源授予的所有角色,包括自訂角色。

gcloud

您可以使用 gcloud iam list-grantable-roles 指令,傳回可套用到特定資源的所有角色清單。

gcloud iam list-grantable-roles full-resource-name

視需要的資源,系統可能會傳回大量的角色。如要限制結果,可以指定篩選運算式

輸出結果如下所示:

description: Full control of all Compute Engine resources.
name: roles/compute.admin
title: Compute Admin
---
description: Full control of Compute Engine instance resources.
name: roles/compute.instanceAdmin
title: Compute Instance Admin

# Additional results here...

C++

如要瞭解如何安裝及使用 IAM 的用戶端程式庫,請參閱 IAM 用戶端程式庫。 詳情請參閱 IAM C++ API 參考說明文件

如要向 IAM 進行驗證,請設定應用程式預設憑證。 詳情請參閱「事前準備」。

namespace iam = ::google::cloud::iam_admin_v1;
[](std::string const& resource) {
  iam::IAMClient client(iam::MakeIAMConnection());
  int count = 0;
  for (auto& role : client.QueryGrantableRoles(resource)) {
    if (!role) throw std::move(role).status();
    std::cout << "Role successfully retrieved: " << role->name() << "\n";
    ++count;
  }
  if (count == 0) {
    std::cout << "No grantable roles found in resource: " << resource << "\n";
  }
}

C#

如要瞭解如何安裝及使用 IAM 的用戶端程式庫,請參閱 IAM 用戶端程式庫。 詳情請參閱 IAM C# API 參考說明文件

如要向 IAM 進行驗證,請設定應用程式預設憑證。 詳情請參閱「事前準備」。


using System;
using System.Collections.Generic;
using Google.Apis.Auth.OAuth2;
using Google.Apis.Iam.v1;
using Google.Apis.Iam.v1.Data;

public partial class CustomRoles
{
    public static IList<Role> ViewGrantableRoles(string fullResourceName)
    {
        var credential = GoogleCredential.GetApplicationDefault()
            .CreateScoped(IamService.Scope.CloudPlatform);
        var service = new IamService(new IamService.Initializer
        {
            HttpClientInitializer = credential
        });

        var request = new QueryGrantableRolesRequest
        {
            FullResourceName = fullResourceName
        };
        var response = service.Roles.QueryGrantableRoles(request).Execute();
        foreach (var role in response.Roles)
        {
            Console.WriteLine("Title: " + role.Title);
            Console.WriteLine("Name: " + role.Name);
            Console.WriteLine("Description: " + role.Description);
            Console.WriteLine();
        }
        return response.Roles;
    }
}

Go

如要瞭解如何安裝及使用 IAM 的用戶端程式庫,請參閱 IAM 用戶端程式庫。 詳情請參閱 IAM Go API 參考說明文件

如要向 IAM 進行驗證,請設定應用程式預設憑證。 詳情請參閱「事前準備」。

import (
	"context"
	"fmt"
	"io"

	iam "google.golang.org/api/iam/v1"
)

// viewGrantableRoles lists roles grantable on a resource.
func viewGrantableRoles(w io.Writer, fullResourceName string) ([]*iam.Role, error) {
	ctx := context.Background()
	service, err := iam.NewService(ctx)
	if err != nil {
		return nil, fmt.Errorf("iam.NewService: %w", err)
	}

	request := &iam.QueryGrantableRolesRequest{
		FullResourceName: fullResourceName,
	}
	response, err := service.Roles.QueryGrantableRoles(request).Do()
	if err != nil {
		return nil, fmt.Errorf("Roles.QueryGrantableRoles: %w", err)
	}
	for _, role := range response.Roles {
		fmt.Fprintf(w, "Found grantable role: %v\n", role.Name)
	}
	return response.Roles, err
}

Java

如要瞭解如何安裝及使用 IAM 的用戶端程式庫,請參閱 IAM 用戶端程式庫。 詳情請參閱 IAM Java API 參考說明文件

如要向 IAM 進行驗證,請設定應用程式預設憑證。 詳情請參閱「事前準備」。

QueryGrantableRolesRequest request = new QueryGrantableRolesRequest();
request.setFullResourceName(fullResourceName);

QueryGrantableRolesResponse response = service.roles().queryGrantableRoles(request).execute();

for (Role role : response.getRoles()) {
  System.out.println("Title: " + role.getTitle());
  System.out.println("Name: " + role.getName());
  System.out.println("Description: " + role.getDescription());
  System.out.println();
}

Python

如要瞭解如何安裝及使用 IAM 的用戶端程式庫,請參閱 IAM 用戶端程式庫。 詳情請參閱 IAM Python API 參考說明文件

如要向 IAM 進行驗證,請設定應用程式預設憑證。 詳情請參閱「事前準備」。

def view_grantable_roles(full_resource_name: str) -> None:
    roles = (
        service.roles()
        .queryGrantableRoles(body={"fullResourceName": full_resource_name})
        .execute()
    )

    for role in roles["roles"]:
        if "title" in role:
            print("Title: " + role["title"])
        print("Name: " + role["name"])
        if "description" in role:
            print("Description: " + role["description"])
        print(" ")

REST

roles.queryGrantableRoles 方法會傳回可授予資源的所有角色清單。

使用任何要求資料之前,請先替換以下項目:

  • FULL_RESOURCE_NAME:由服務名稱和資源路徑組成的 URI。如需範例,請參閱「完整資源名稱」。

HTTP 方法和網址:

POST https://iam.googleapis.com/v1/roles:queryGrantableRoles

JSON 要求主體:

{
  "fullResourceName": "FULL_RESOURCE_NAME"
}

如要傳送要求,請展開以下其中一個選項:

您應該會收到如下的 JSON 回應:

{
  "roles": [
    {
      "name": "roles/compute.admin",
      "title": "Compute Admin",
      "description": "Full control of all Compute Engine resources."
    },
    {
      "name": "roles/compute.instanceAdmin",
      "title": "Compute Instance Admin (beta)",
      "description": "Full control of Compute Engine instance resources."
    }
  ]
}

在以上的範例中,「完整資源名稱」是一種無配置的 URI,由 DNS 相容的 API 服務名稱和資源路徑所組成。

舉例來說,如要傳回可針對專案授予的所有角色,請使用:

//cloudresourcemanager.googleapis.com/projects/project-id

較低層級的資源擁有較詳細的完整名稱。例如使用以下指令,傳回可針對 Compute Engine 執行個體授予的所有角色:

//compute.googleapis.com/projects/project-id/zones/zone-name/instances/instance-id

後續步驟