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

本文档介绍了如何使用员工身份联合创建 Secure Source Manager 实例,以便与外部身份提供方 (IdP) 搭配使用。

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

如需了解 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.

    Go to project selector

  3. Enable the Secure Source Manager API.

    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.

    Go to project selector

  8. Enable the Secure Source Manager API.

    Enable the API

  9. Install the Google Cloud CLI.

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

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

    gcloud init
  12. 安装 beta Google Cloud CLI 组件:
    gcloud components install beta
  13. 数据加密

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

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

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

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

    其中,PROJECT_ID 是您将创建安全源代码管理器的项目的项目 ID。

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

    设置员工身份联合

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

    1. 创建员工身份联合池

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

      • google.subject
      • google.email

    创建实例

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

    • 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 分钟。

    检查创建操作的状态

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

    • 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 源代码管理中的说明操作。

    后续步骤