向 Cloud Workstations API 进行身份验证

本文档介绍了如何以编程方式向 Cloud Workstations 进行身份验证。方法 向 Cloud Workstations 进行身份验证取决于您用于访问 API 的接口 以及运行代码的环境

如果您刚开始接触 Cloud Workstations API,请务必 请先阅读以下部分: <ph type="x-smartling-placeholder">

如需详细了解 Google Cloud 身份验证,请参阅身份验证概览

API 访问权限

Cloud Workstations 支持编程访问。您可以通过以下方式访问 API:

客户端库

Cloud Workstations 客户端库提供 Cloud Workstations 身份验证的高级语言支持 以编程方式 为了对 Google Cloud API 的调用进行身份验证,客户端库支持应用默认凭据 (ADC);这些库会在一组定义的位置查找凭据,并使用这些凭据对发送到 API 的请求进行身份验证。借助 ADC,您可以在各种环境(例如本地开发或生产环境)中为您的应用提供凭据,而无需修改应用代码。

Google Cloud CLI

当您使用 gcloud CLI 访问 Cloud Workstations 以用户的身份登录 gcloud CLI 账号,该账号用于提供 gcloud CLI 命令所使用的凭据。

如果您的组织的安全政策阻止用户账号获取所需的权限,您可以使用服务账号模拟

如需了解详情,请参阅 使用 gcloud CLI 进行身份验证。 如需详细了解如何将 gcloud CLI 与 Cloud Workstations 搭配使用, 请参阅 gcloudstations

REST

您可以向 Cloud Workstations API 使用您的 gcloud CLI 凭据或使用 应用默认凭据。 如需详细了解针对 REST 请求的身份验证,请参阅 进行身份验证以使用 REST。 如需了解凭据类型,请参阅 gcloud CLI 凭据和 ADC 凭据

为 Cloud Workstations 设置身份验证

设置身份验证的方式取决于代码在其中运行的环境。

以下是最常用的身份验证设置选项。如需了解有关身份验证的更多选项和信息,请参阅身份验证方法

在完成这些说明之前,您必须先完成 Cloud Workstations(如准备工作中所述)。

对于本地开发环境

您可以通过以下方式为本地开发环境设置凭据:

客户端库或第三方工具

在本地环境中设置应用默认凭据 (ADC)

  1. Install the Google Cloud CLI, then initialize it by running the following command:

    gcloud init
  2. Create local authentication credentials for your user account:

    gcloud auth application-default login

    登录屏幕随即出现。在您登录后,您的凭据会存储在 ADC 使用的本地凭据文件中。

如需详细了解如何在本地环境中使用 ADC,请参阅本地开发环境

从命令行发出的 REST 请求

从命令行发出 REST 请求时 可以使用您的 gcloud CLI 凭据,只需添加 gcloud auth print-access-token 作为发送请求的命令的一部分。

以下示例列出了指定项目的服务账号。您可以对任何 REST 请求使用相同的模式。

在使用任何请求数据之前,请先进行以下替换:

  • PROJECT_ID:您的 Google Cloud 项目 ID。

如需发送您的请求,请展开以下选项之一:

 

如需详细了解如何使用 REST 和 gRPC 进行身份验证,请参阅使用 REST 时进行身份验证。如需了解您的本地 ADC 凭据和 gcloud CLI 凭据,请参阅 gcloud CLI 凭据和 ADC 凭据

在 Google Cloud 上

如需对 Google Cloud 上运行的工作负载进行身份验证,请使用关联到代码在其中运行的计算资源(例如 Compute Engine 虚拟机 [VM] 实例)的服务账号的凭据。对于在 Google Cloud 计算资源上运行的代码,这是首选的身份验证方法。

对于大多数服务,您必须在创建将运行代码的资源时关联服务账号;您之后无法添加或替换服务账号。Compute Engine 是一个例外,它允许您随时将服务账号关联到虚拟机实例。

使用 gcloud CLI 创建服务账号并将其关联到 资源:

  1. Install the Google Cloud CLI, then initialize it by running the following command:

    gcloud init
  2. Set up authentication:

    1. Create the service account:

      gcloud iam service-accounts create SERVICE_ACCOUNT_NAME

      Replace SERVICE_ACCOUNT_NAME with a name for the service account.

    2. To provide access to your project and your resources, grant a role to the service account:

      gcloud projects add-iam-policy-binding PROJECT_ID --member="serviceAccount:SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com" --role=ROLE

      Replace the following:

      • SERVICE_ACCOUNT_NAME: the name of the service account
      • PROJECT_ID: the project ID where you created the service account
      • ROLE: the role to grant
    3. To grant another role to the service account, run the command as you did in the previous step.
    4. Grant the required role to the principal that will attach the service account to other resources.

      gcloud iam service-accounts add-iam-policy-binding SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com --member="user:USER_EMAIL" --role=roles/iam.serviceAccountUser

      Replace the following:

      • SERVICE_ACCOUNT_NAME: the name of the service account
      • PROJECT_ID: the project ID where you created the service account
      • USER_EMAIL: the email address for a Google Account
  3. 创建将运行代码的资源,并将服务账号关联到该资源。例如,如果您使用 Compute Engine:

    Create a Compute Engine instance. Configure the instance as follows:
    • INSTANCE_NAME 替换为您偏好的实例名称。
    • --zone 标志设置为您要在其中创建实例的可用区
    • --service-account 标志设置为您创建的服务账号的电子邮件地址。
    • gcloud compute instances create INSTANCE_NAME --zone=ZONE --service-account=SERVICE_ACCOUNT_EMAIL

如需详细了解如何向 Google API 进行身份验证,请参阅身份验证方法

在本地或在其他云服务提供商上

如需在 Google Cloud 外部设置身份验证,首选方法是使用工作负载身份联合。如需了解详情,请参阅身份验证文档中的本地或其他云服务提供商

Cloud Workstations 访问权限控制

向 Cloud Workstations 验证身份后,您必须有权访问 Google Cloud 资源。Cloud Workstations 使用 使用 Identity and Access Management (IAM) 进行授权。

如需详细了解 Cloud Workstations 角色,请参阅 使用 IAM 进行访问权限控制。 如需详细了解 IAM 和授权,请参阅 IAM 概览

后续步骤