创建 Secure Source Manager 实例以搭配联合身份使用

了解如何创建将员工身份联合与外部身份提供方 (IdP) 搭配使用的 Secure Source Manager 实例。

如需详细了解员工身份联合,请参阅员工身份联合

如需了解 Secure Source Manager 中员工身份联合的限制,请参阅身份联合:产品和限制

Secure Source Manager 是一项单租户服务。单个 Secure Source Manager 实例应仅包含来自一个Google Cloud 客户的用户,除非有合同关系的多家公司需要使用单个实例进行协作。

如果您与多家公司合作,并希望与他们协作处理源代码,建议您为每家公司创建一个单独的实例。

准备工作

  1. Sign in to your Google Account.

    If you don't already have one, sign up for a new account.

  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Enable the Secure Source Manager 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

  4. Install the Google Cloud CLI.

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

  6. 如需初始化 gcloud CLI,请运行以下命令:

    gcloud init
  7. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  8. Enable the Secure Source Manager 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

  9. Install the Google Cloud CLI.

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

  11. 如需初始化 gcloud CLI,请运行以下命令:

    gcloud init
  12. 数据加密

    默认情况下, Google Cloud 会使用Google-owned and Google-managed encryption keys自动加密静态数据。如果您对保护数据的密钥有特定的合规性或监管要求,则可以创建使用客户管理的加密密钥 (CMEK) 加密的 Secure Source Manager 实例。

    请勿在实例 ID 或标签键值对中存储敏感数据,因为这些数据不会使用 CMEK 加密。

    如果您要在项目中创建第一个 Secure Source Manager 实例,则必须运行以下命令手动创建 Secure Source Manager 服务代理:

    gcloud services identity create \
        --service=securesourcemanager.googleapis.com \
        --project=PROJECT_ID
    

    其中,PROJECT_ID 是您在其中创建 Secure Source Manager 实例的项目的 ID。

    创建每个产品、每个项目的服务账号 (P4SA) 后,您必须向正文 service-PROJECT-NUMBER@gcp-sa-sourcemanager.iam.gserviceaccount.com授予 Secure Source Manager 服务代理角色 (roles/securesourcemanager.serviceAgent),否则实例创建会失败。

    设置员工身份联合

    在创建实例之前,您必须设置员工身份联合池和提供方。

    1. 创建员工身份联合池

    2. 使用以下属性映射配置员工身份池提供方

      • google.subject
      • google.email

    创建实例

    gcloud

    在使用下面的命令数据之前,请先进行以下替换:

    • INSTANCE_ID:要创建的实例的名称。
    • LOCATION:您希望创建实例的区域。如需了解支持的位置,请参阅位置
    • PROJECT_ID:您要在其中创建实例的项目的 ID。

    执行以下命令:

    Linux、macOS 或 Cloud Shell

    gcloud source-manager instances create INSTANCE_ID --region=LOCATION --project=PROJECT_ID --enable-workforce-identity-federation

    Windows (PowerShell)

    gcloud source-manager instances create INSTANCE_ID --region=LOCATION --project=PROJECT_ID --enable-workforce-identity-federation

    Windows (cmd.exe)

    gcloud source-manager instances create INSTANCE_ID --region=LOCATION --project=PROJECT_ID --enable-workforce-identity-federation

    系统会启动长时间运行的创建实例操作。输出如下所示:

      Create request issued for [my-instance].
      done: false
      metadata:
        '@type': type.googleapis.com/google.cloud.securesourcemanager.v1.OperationMetadata
        apiVersion: v1
        createTime: '2023-02-27T20:57:52.315609549Z'
        requestedCancellation: false
        target: projects/my-project/locations/us-central1/instances/my-instance
        verb: create
      name: projects/my-project/locations/us-central1/operations/operation-1234567894561-5ec69948c0f2b-60dd727f-a9b97a2e
    

    其中,projects/my-project/locations/us-central1/operations/operation-1234567894561-5ec69948c0f2b-60dd727f-a9b97a2eOPERATION_NAME

    记录 OPERATION_NAME,因为您需要使用它来检查操作的状态。

    创建实例最多可能需要 60 分钟。

    API

    在运行此命令之前,请先替换以下值:

    • INSTANCE_ID:要创建的实例的名称。
    • LOCATION:您希望创建实例的区域。如需了解受支持的位置,请参阅位置
    • PROJECT_ID:您要在其中创建实例的项目的 ID。

    运行以下命令:

    curl \
        -X POST -H "Authorization: Bearer $(gcloud auth print-access-token)" \
        https://securesourcemanager.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/instances?instance_id=INSTANCE_ID \
        -H "Content-Type: application/json" \
        -d "{workforce_identity_federation_config: {enabled: true}}"
    
    

    系统会启动长时间运行的创建实例操作。输出将类似以下内容:

    {
    "name": "OPERATION_NAME",
    "metadata": {
      "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.OperationMetadata",
      "createTime": "2022-11-01T14:31:32.420469714Z",
      "target": "projects/PROJECT_ID/locations/LOCATION/instances/INSTANCE_ID",
      "verb": "create",
      "requestedCancellation": false,
      "apiVersion": "v1"
    },
    "done": false
    }
    

    此示例输出包括以下值:

    • OPERATION_NAME:操作名称,例如 projects/my-project/locations/us-central1/operations/operation-1234567894561-5ec69948c0f2b-60dd727f-a9b97a2e
    • PROJECT_ID: Google Cloud 项目。
    • LOCATION:实例所在的区域。

    记录 OPERATION_NAME,因为您需要使用它来检查操作的状态。

    创建实例最多可能需要 60 分钟。

    Terraform

    以下示例展示了如何创建启用了员工身份联合的 Secure Source Manager 实例:

    resource "google_secure_source_manager_instance" "instance" {
      instance_id = "INSTANCE_ID"
      location    = "LOCATION"
      project     = "PROJECT_ID"
      workforce_identity_federation_config {
        enabled = true
      }
    }
    

    查看操作状态

    gcloud

    在使用下面的命令数据之前,请先进行以下替换:

    • OPERATION_NAME:创建命令响应中的操作名称。
    • LOCATION:实例区域。

    执行以下命令:

    Linux、macOS 或 Cloud Shell

    gcloud source-manager operations describe OPERATION_NAME --region=LOCATION

    Windows (PowerShell)

    gcloud source-manager operations describe OPERATION_NAME --region=LOCATION

    Windows (cmd.exe)

    gcloud source-manager operations describe OPERATION_NAME --region=LOCATION

    实例准备就绪后,响应将类似于以下内容:

      "name": "projects/my-project/locations/us-central1/operations/operation-123456789012-5ec69948c0f2b-60dd727f-a9b97a2e",
      "metadata": {
        "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.OperationMetadata",
        "createTime": "2022-11-01T14:31:32.420469714Z",
        "endTime": "2022-11-01T14:48:34.140378114Z",
        "target": "projects/my-project/locations/us-central1/instances/my-instance",
        "verb": "create",
        "requestedCancellation": false,
        "apiVersion": "v1"
      },
      "done": true,
      "response": {
        "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.Instance",
        "name": "projects/my-project/locations/us-central1/instances/my-instance",
        "createTime": "2022-11-01T14:31:32.416413630Z",
        "updateTime": "2022-11-01T14:31:32.416413630Z",
        "labels": {
          "ldap": "user",
          "source": "manual"
        },
        "state": "ACTIVE",
        "hostConfig": {
          "html": "my-instance-098765432109.us-central1.sourcemanager.dev",
          "api": "my-instance-098765432109-api.us-central1.sourcemanager.dev",
          "gitHttp": "my-instance-098765432109-git.us-central1.sourcemanager.dev"
        }
      }
    

    其中 my-instance-098765432109.us-central1.sourcemanager.dev 是实例 HTML 网址。

    API

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

    • OPERATION_NAME:创建命令响应中的操作名称。

    HTTP 方法和网址:

    GET https://securesourcemanager.googleapis.com/v1/OPERATION_NAME

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

    实例准备就绪后,响应将类似于以下内容:

      "name": "projects/my-project/locations/us-central1/operations/operation-123456789012-5ec69948c0f2b-60dd727f-a9b97a2e",
      "metadata": {
        "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.OperationMetadata",
        "createTime": "2024-10-28T15:37:39.009812863Z",
        "endTime": "2024-10-28T16:10:58.416640259Z",
        "target": "projects/my-project/locations/us-central1/instances/my-instance",
        "verb": "create",
        "requestedCancellation": false,
        "apiVersion": "v1"
      },
      "done": true,
      "response": {
        "@type": "type.googleapis.com/google.cloud.securesourcemanager.v1.Instance",
        "name": "projects/my-project/locations/us-central1/instances/my-instance",
        "createTime": "2024-10-28T15:37:39.004550840Z",
        "updateTime": "2024-10-28T15:37:39.867857246Z",
        "state": "ACTIVE",
        "hostConfig": {
          "html": "my-instance-326202322590.us-central1.sourcemanager.dev",
          "api": "my-instance-326202322590-api.us-central1.sourcemanager.dev",
          "gitHttp": "my-instance-326202322590-git.us-central1.sourcemanager.dev",
          "gitSsh": "my-instance-326202322590-ssh.us-central1.sourcemanager.dev"
        },
        "workforceIdentityFederationConfig": {
          "enabled": true
        }
      }
    

    其中 my-instance-098765432109.us-central1.sourcemanager.dev 是实例 HTML 网址。

    从成功检查状态响应中复制 HTML 网址。您需要此网址才能通过实例的网页界面访问该实例。

    访问实例

    创建实例后,您可以使用浏览器通过其网页界面访问该实例。

    1. 如需通过网页界面访问 Secure Source Manager 实例,请将以下网址复制到浏览器地址栏中。

      INSTANCE_ID-PROJECT_NUMBER.LOCATION.sourcemanager.dev

      请替换以下内容:

      • INSTANCE_ID 替换为实例名称。
      • PROJECT_NUMBER 替换为实例的 Google Cloud项目编号。如需了解如何标识项目,请参阅标识项目
      • LOCATION 替换为实例的区域。

      系统会显示一个 OAuth 界面,要求您提供提供方名称。

    2. 按以下格式输入员工身份提供方的名称:

      locations/global/workforcePools/POOL_ID/providers/PROVIDER_ID
      

      替换以下内容:

      • POOL_ID 替换为您的员工身份联合池 ID。
      • PROVIDER_ID 替换为您的员工身份联合提供方 ID。

      如需详细了解员工身份联合身份池和提供方,请参阅管理员工身份联合身份池和提供方

      系统会提示您使用提供商凭据登录。

    3. 使用提供商凭据登录。

    4. Secure Source Manager 网页界面随即打开。您可以在此处创建和查看代码库、问题和拉取请求。

      员工身份联合池中的会话时长到期后,您必须通过网页界面重新登录。

    创建 gcloud CLI 登录配置

    如需使用员工身份池凭据通过 gcloud CLI 命令或 Git 命令向 Secure Source Manager 实例进行身份验证,您必须使用员工身份池创建登录配置。

    如需创建员工身份池登录配置,请按照为员工身份联合获取短期令牌中的说明,使用 gcloud CLI 创建基于浏览器的登录。

    完成身份验证后,您可以使用 Git 命令与 Secure Source Manager 实例进行交互。如需开始使用 Git,请参阅使用 Git 源代码管理

    后续步骤