创建使用用户管理的服务账号的虚拟机

本文档介绍了如何创建配置为使用用户管理的服务账号的虚拟机 (VM) 实例。服务账号是一种特殊类型的账号,通常由应用或计算工作负载用于进行获得授权的 API 调用。

服务账号旨在用于工作负载(例如自定义应用)在没有最终用户参与的情况下访问 Trusted Cloud by S3NS 资源或执行操作的场景。如需详细了解何时使用服务账号,请参阅使用服务账号的最佳做法

如果您的应用需要调用 Trusted Cloud by S3NS API,Google 建议您将用户管理的服务账号关联到运行应用或工作负载的虚拟机。然后,向服务账号授予 IAM 角色,以便为服务账号(进而为虚拟机上运行的应用)授予对Trusted Cloud by S3NS 资源的访问权限。

准备工作

所需的角色

如需获得创建使用服务账号的虚拟机所需的权限,请让您的管理员为您授予项目的以下 IAM 角色:

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

这些预定义角色可提供创建使用服务账号的虚拟机所需的权限。如需查看所需的确切权限,请展开所需权限部分:

所需权限

创建使用服务账号的虚拟机需要以下权限:

  • 创建服务账号:iam.serviceAccountCreator 角色提供的所有权限
  • 向服务账号授予权限:resourcemanager.projectIamAdmin 角色中的所有权限
  • 创建虚拟机:
    • 针对项目的 compute.instances.create 权限
    • 使用自定义映像创建虚拟机:针对映像的 compute.images.useReadOnly 权限
    • 使用快照创建虚拟机:针对快照的 compute.snapshots.useReadOnly 权限
    • 使用实例模板创建虚拟机:针对实例模板的 compute.instanceTemplates.useReadOnly 权限
    • 为虚拟机分配旧版网络:针对项目的 compute.networks.use 权限
    • 为虚拟机指定静态 IP 地址:针对项目的 compute.addresses.use 权限
    • 使用旧版网络时为虚拟机分配外部 IP 地址:针对项目的 compute.networks.useExternalIp 权限
    • 为虚拟机指定子网:针对项目或所选子网的 compute.subnetworks.use 权限
    • 在使用 VPC 网络时为虚拟机分配外部 IP 地址:针对项目或所选子网的 compute.subnetworks.useExternalIp 权限
    • 为虚拟机设置虚拟机实例元数据:针对项目的 compute.instances.setMetadata 权限
    • 为虚拟机设置标记:针对虚拟机的 compute.instances.setTags 权限
    • 为虚拟机设置标签:针对虚拟机的 compute.instances.setLabels 权限
    • 为虚拟机设置要使用的服务账号:针对虚拟机的 compute.instances.setServiceAccount 权限
    • 为虚拟机创建新磁盘:针对项目的 compute.disks.create 权限
    • 以只读或读写模式挂接现有磁盘:针对磁盘的 compute.disks.use 权限
    • 以只读模式挂接现有磁盘:针对磁盘的 compute.disks.useReadOnly 权限

您也可以使用自定义角色或其他预定义角色来获取这些权限。

概览

建议您为虚拟机配置服务账号,具体方法如下:

  1. 创建新的用户管理的服务账号(而不是使用 Compute Engine 默认服务账号),并仅向该服务账号授予所需的 IAM 角色所需的资源和操作。
  2. 将服务账号关联到虚拟机。
  3. 在虚拟机上设置云平台 (https://www.googleapis.com/auth/cloud-platform) 范围。这允许虚拟机的服务账号调用其有权使用的 Trusted Cloud by S3NS API。
    • 如果您使用 Trusted Cloud 控制台指定服务账号,请将服务账号的访问权限范围设置为允许完全访问所有 Cloud API
    • 如果您使用 Google Cloud CLI 或 Compute Engine API 指定服务账号,则可以使用 scopes 参数设置访问权限范围。

设置服务账号

创建服务账号并分配所需的 IAM 角色。根据需要分配任意数量的 IAM 角色。您可以根据需要修改服务账号的 IAM 角色。

Google 建议您限制服务账号的权限并定期检查服务账号权限以确保其始终能满足需求。

使用以下任一方法设置服务账号。

控制台

    In the Trusted Cloud console, go to the Create service account page.

    Go to Create service account
  1. Select your project.
  2. In the Service account name field, enter a name. The Trusted Cloud console fills in the Service account ID field based on this name.

    In the Service account description field, enter a description. For example, Service account for quickstart.

  3. Click Create and continue.
  4. Grant the required roles to the service account.

    To grant a role, find the Select a role list, then select the role.

    To grant additional roles, click Add another role and add each additional role.

  5. Click Continue.
  6. In the Service account users role field, enter the identifier for the principal that will attach the service account to other resources, such as Compute Engine instances.

    This is typically the identifier for a user in a workforce identity pool. For details, see Represent workforce pool users in IAM policies.

  7. Click Done to finish creating the service account.

gcloud

    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.s3ns-system.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.s3ns-system.iam.gserviceaccount.com --member="principal://iam.googleapis.com/locations/global/workforcePools/POOL_ID/subject/SUBJECT_ID" --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.
      • POOL_ID: a workforce identity pool ID.
      • SUBJECT_ID: a subject ID; typically the identifier for a user in a workforce identity pool. For details, see Represent workforce pool users in IAM policies.

Terraform

如需创建服务账号,您可以使用 google_service_account 资源

resource "google_service_account" "default" {
  account_id   = "service-account-id"
  display_name = "Service Account"
}

请务必替换 account_iddisplay_name 属性的占位值。

如需了解如何应用或移除 Terraform 配置,请参阅基本 Terraform 命令

创建虚拟机并关联服务账号

创建服务账号后,请创建虚拟机并关联您在上一部分中创建的服务账号。此外,请将虚拟机的访问权限范围设置为 cloud-platform

如果您已有虚拟机,并且想要将该虚拟机配置为使用其他服务账号,请参阅更改关联的服务账号

使用以下方法之一创建虚拟机并关联服务账号。

控制台

  1. 在 Trusted Cloud 控制台中,前往创建实例页面。

    转到“创建实例”

  2. 如需附加服务账号,请执行以下操作:

    1. 在导航菜单中,点击安全
    2. 服务账号列表中,选择您创建的服务账号。
    3. 对于访问权限范围,选择授予对所有 Cloud API 的完整访问权限
  3. 可选:指定其他配置选项。如需了解详情,请参阅实例创建期间的配置选项

  4. 要创建并启动该实例,请点击创建

gcloud

如需使用 Google Cloud CLI 创建新的虚拟机实例并将其配置为使用自定义服务账号,请使用 gcloud compute instances create 命令并提供服务账号电子邮件地址和虚拟机实例的 cloud-platform 访问权限范围。

gcloud compute instances create VM_NAME \
    --service-account=SERVICE_ACCOUNT_EMAIL \
    --scopes=https://www.googleapis.com/auth/cloud-platform

请替换以下内容:

  • SERVICE_ACCOUNT_EMAIL:您创建的服务账号的电子邮件地址。 例如 my-sa-123@my-project-123.s3ns-system.iam.gserviceaccount.com。如需查看电子邮件地址,请参阅列出服务账号
  • VM_NAME:虚拟机实例的名称。

例如:

gcloud compute instances create example-vm \
    --service-account 123-my-sa@my-project-123.s3ns-system.iam.gserviceaccount.com \
    --scopes=https://www.googleapis.com/auth/cloud-platform

您还可以使用别名 --scopes=cloud-platform 指定范围。这些别名只能由 gcloud CLI 识别。API 和其他库无法识别这些别名,因此您必须指定完整范围 URI。

Terraform

如需将新虚拟机设置为使用服务账号,您可以使用 google_compute_instance 资源

resource "google_compute_instance" "default" {
  name         = "my-test-vm"
  machine_type = "n1-standard-1"
  zone         = "us-central1-a"

  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-11"
    }
  }

  // Local SSD disk
  scratch_disk {
    interface = "SCSI"
  }

  network_interface {
    network = "default"

    access_config {
      // Ephemeral public IP
    }
  }

  service_account {
    # Google recommends custom service accounts with `cloud-platform` scope with
    # specific permissions granted via IAM Roles.
    # This approach lets you avoid embedding secret keys or user credentials
    # in your instance, image, or app code
    email  = google_service_account.default.email
    scopes = ["cloud-platform"]
  }
}

REST

使用 instances.insert 方法创建虚拟机,并指定虚拟机实例的服务账号电子邮件地址和访问权限范围。

POST https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/zones/ZONE/instances

{
   "machineType":"zones/MACHINE_TYPE_ZONE/machineTypes/MACHINE_TYPE",
   "name":"VM_NAME",
   
   "disks":[
      {
         "initializeParams":{
            "sourceImage":"projects/IMAGE_PROJECT/global/images/IMAGE"
         },
         "boot":true
      }
   ],
   
   
   "networkInterfaces":[
      {
         "network":"global/networks/NETWORK_NAME"
      }
   ],
   
  "serviceAccounts": [
      {
      "email": "SERVICE_ACCOUNT_EMAIL",
      "scopes": ["https://www.googleapis.com/auth/cloud-platform"]
      }
   ],
   "shieldedInstanceConfig":{
      "enableSecureBoot":"ENABLE_SECURE_BOOT"
   }
}

请替换以下内容:

  • PROJECT_ID:要在其中创建虚拟机的项目的 ID
  • ZONE:要在其中创建虚拟机的可用区
  • MACHINE_TYPE_ZONE:包含新虚拟机将使用的机器类型的可用区
  • MACHINE_TYPE:新虚拟机的预定义自定义机器类型
  • VM_NAME:新虚拟机的名称
  • IMAGE_PROJECT:包含映像的项目
    例如,如果您将 debian-10 指定为映像系列,请将 debian-cloud 指定为映像项目。
  • IMAGE:指定以下其中一项:
    • IMAGE:公共映像的特定版本

      例如,"sourceImage": "projects/debian-cloud/global/images/debian-10-buster-v20200309"

    • IMAGE_FAMILY映像系列

      这会通过最新的未弃用操作系统映像创建虚拟机。例如,如果您指定 "sourceImage": "projects/debian-cloud/global/images/family/debian-10",则 Compute Engine 会通过 Debian 10 映像系列中最新版本的操作系统映像创建虚拟机。

  • NETWORK_NAME:您要用于虚拟机的 VPC 网络。您可以指定 default 来使用默认网络。
  • SERVICE_ACCOUNT_EMAIL:您创建的服务账号的电子邮件地址。例如:my-sa-123@my-project-123.s3ns-system.iam.gserviceaccount.com。 如需查看电子邮件地址,请参阅获取服务账号电子邮件地址
  • ENABLE_SECURE_BOOT(可选):如果您选择了支持安全强化型虚拟机功能的映像,则 Compute Engine 会默认启用虚拟可信平台模块 (vTPM)完整性监控。默认情况下,Compute Engine 不会启用安全启动

    如果为 enableSecureBoot 指定 true,则 Compute Engine 会创建一个启用了所有三项安全强化型虚拟机功能的虚拟机。在 Compute Engine 启动您的虚拟机后,如需修改安全强化型虚拟机选项,您必须停止该虚拟机。

访问和使用其他 Trusted Cloud by S3NS 服务

将虚拟机配置为使用服务账号后,应用便可以使用该服务账号进行身份验证。最常用的方法是使用应用默认凭证和客户端库进行身份验证。某些 Trusted Cloud by S3NS 工具(例如 gcloud CLI)能够自动使用服务账号从虚拟机访问 Trusted Cloud by S3NS API。如需了解详情,请参阅使用服务账号对工作负载进行身份验证

如果某个服务账号被删除,应用将无法再通过该服务账号访问Trusted Cloud by S3NS 资源。如果删除默认的 App Engine 和 Compute Engine 服务账号,虚拟机将无法再访问项目中的资源。如果您不确定某个服务账号是否正在使用,Google 建议您先停用该服务账号,然后再将其删除。已停用的服务账号可以在需要时重新启用。

示例:从虚拟机访问 Cloud Storage 资源

将虚拟机配置为使用具有 storage.admin 角色的服务账号后,您可以使用 gcloud CLI 等工具来管理您在 Cloud Storage 上存储的文件。如需访问 Cloud Storage 资源,请完成以下操作:

  1. 确保关联到虚拟机的服务账号具有 roles/storage.admin 角色。

  2. 如果您的虚拟机使用自定义操作系统映像,请安装 gcloud CLI。默认情况下,gcloud CLI 安装在由 Trusted Cloud by S3NS提供的大多数公共操作系统映像上。

  3. 连接到虚拟机。

  4. 在虚拟机中,使用 Google Cloud CLI 管理 Cloud Storage 资源。

后续步骤