创建和管理自定义角色

本页面介绍如何创建和管理 Identity and Access Management (IAM) 自定义角色。管理角色的操作包括修改、停用、列出、删除和取消删除角色。

准备工作

  • Enable the IAM API.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    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,然后使用您的用户凭证设置应用默认凭证。

      安装 Google Cloud CLI。

      如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI

      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

    C++

    如需在本地开发环境中使用本页面上的 C++ 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭证设置应用默认凭证。

      安装 Google Cloud CLI。

      如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI

      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,然后使用您的用户凭证设置应用默认凭证。

      安装 Google Cloud CLI。

      如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI

      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

    Java

    如需在本地开发环境中使用本页面上的 Java 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭证设置应用默认凭证。

      安装 Google Cloud CLI。

      如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI

      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

    Python

    如需在本地开发环境中使用本页面上的 Python 示例,请安装并初始化 gcloud CLI,然后使用您的用户凭证设置应用默认凭证。

      安装 Google Cloud CLI。

      如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI

      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

    REST

    如需在本地开发环境中使用本页面上的 REST API 示例,请使用您提供给 gcloud CLI 的凭证。

      安装 Google Cloud CLI。

      如果您使用的是外部身份提供方 (IdP),则必须先使用联合身份登录 gcloud CLI

    如需了解详情,请参阅 Google Cloud 身份验证文档中的使用 REST 时进行身份验证

  • 了解 Google Cloud 资源层次结构

  • 阅读了解 IAM 自定义角色

所需的角色

如需获得创建和管理自定义角色所需的权限,请让管理员向您授予以下 IAM 角色:

如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限

您也可以通过自定义角色或其他预定义角色来获取所需的权限。

查看项目、文件夹和组织的可用权限

您可以为整个组织或该组织中的特定项目创建自定义角色。自定义角色可用的权限取决于您创建该角色的位置。例如,如果有某项权限只能在组织级层使用,那么您便无法在项目级层自定义角色中添加该权限。

如需检查组织级层和项目级自定义角色可以使用哪些权限,您可以使用 gcloud CLI 或 Identity and Access Management API,列出特定组织或项目中可用的权限。例如,您可以获取可用于在项目中创建的自定义角色的所有权限。

某些权限可能对您不可见或不可用于自定义角色,即使这些权限在自定义角色中受支持也是如此。例如,如果您尚未为服务启用 API,则可能无法在自定义角色中使用某项权限。

如需详细了解可以添加到自定义角色的权限,请参阅支持的权限

gcloud

  1. 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.

  2. 使用 gcloud iam list-testable-permissions 命令获取特定项目或组织中自定义角色可用的权限列表。响应会列出您可以在该项目或组织的自定义角色中使用的权限。

    要列出项目或组织的自定义角色中可用的权限,请运行此命令:

    gcloud iam list-testable-permissions FULL_RESOURCE_NAME \
        --filter="customRolesSupportLevel!=NOT_SUPPORTED"

    FULL_RESOURCE_NAME 替换为以下某个值:

    • 项目://cloudresourcemanager.googleapis.com/projects/PROJECT_ID(例如 //cloudresourcemanager.googleapis.com/projects/my-project

    • 组织://cloudresourcemanager.googleapis.com/organizations/NUMERIC_ID(例如 //cloudresourcemanager.googleapis.com/organizations/123456789012

    结果会指明自定义角色是否支持每个权限。 没有 customRolesSupportLevel 字段的权限完全受支持。

    list-testable-permissions 命令可能会返回数百个结果。以下部分示例显示了每个结果的格式:

    ---
    name: appengine.applications.create
    stage: GA
    ---
    customRolesSupportLevel: TESTING
    name: appengine.applications.disable
    stage: GA
    ---
    name: appengine.applications.get
    stage: GA
    ---
    name: appengine.applications.update
    stage: GA
    ---
    name: appengine.instances.delete
    stage: GA
    ---
    name: appengine.instances.get
    stage: GA
    ---
    
  3. C++

    如需了解如何安装和使用 IAM 客户端库,请参阅 IAM 客户端库。 如需了解详情,请参阅 IAM C++ API 参考文档

    如需向 IAM 进行身份验证,请设置应用默认凭据。 如需了解详情,请参阅准备工作

    namespace iam = ::google::cloud::iam_admin_v1;
    [](std::string const& resource) {
      iam::IAMClient client(iam::MakeIAMConnection());
      google::iam::admin::v1::QueryTestablePermissionsRequest request;
      request.set_full_resource_name(resource);
      int count = 0;
      for (auto& permission : client.QueryTestablePermissions(request)) {
        if (!permission) throw std::move(permission).status();
        std::cout << "Permission successfully retrieved: " << permission->name()
                  << "\n";
        ++count;
      }
      if (count == 0) {
        std::cout << "No testable permissions found in resource: " << resource
                  << "\n";
      }
    }

    C#

    如需了解如何安装和使用 IAM 客户端库,请参阅 IAM 客户端库。 如需了解详情,请参阅 IAM