连接到 Secure Source Manager

本页面介绍了如何创建与 Secure Source Manager 的连接,以及如何关联到 Secure Source Manager 代码库。借助此集成,您可以使用 Developer Connect 功能,例如查看 Secure Source Manager 代码库的信息中心视图,以及使用 Developer Connect 代理执行 git 操作(例如安全读取操作)。

准备工作

  1. 登录您的 Google 账号。

    如果您还没有 Google 账号,请 注册新账号

  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. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Developer Connect 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

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

  6. Verify that billing is enabled for your Google Cloud project.

  7. Enable the Developer Connect 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

  8. 可选:创建客户管理的 加密密钥 (CMEK),用于加密 Developer Connect 创建的身份验证 Secret。
  9. 可选:如需使用本指南中的命令行说明,请完成以下步骤:
    1. 安装 Google Cloud CLI。如果您之前安装了 gcloud CLI,请运行 gcloud components update 以确保您拥有最新版本。
    2. 通过运行以下命令创建 Developer Connect 服务账号 ,其中 PROJECT_ID 是您的 Google Cloud 项目 ID
              gcloud beta services identity create \
                  --service=developerconnect.googleapis.com \
                  --project=PROJECT_ID
              

Secure Source Manager 要求

  • 确保您已将可正常运行的 Secure Source Manager 实例 部署到某个 Google Cloud 区域。
  • Developer Connect 连接和 Secure Source Manager 实例必须位于同一区域。
  • 向 Developer Connect 服务代理 (P4SA) 授予 Secure Source Manager 实例项目的 roles/securesourcemanager.developerConnectLinker 角色。

所需角色

如需获得创建连接和关联所需的权限,请让您的管理员为您授予以下 IAM 角色:

  • 如需创建连接并关联到代码库,请在您要创建 Developer Connect 资源的项目中,授予Developer Connect Admin (roles/developerconnect.admin) 角色。
  • 如需向 Developer Connect 服务代理授予权限,请在 Secure Source Manager 运行的项目中,授予Project IAM Admin (roles/resourcemanager.projectIamAdmin) 角色。

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

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

创建连接

本部分介绍了如何在 Developer Connect 和 Secure Source Manager 之间创建连接。如果您使用的是 Google Cloud 控制台,则可以在完成连接设置后开始 添加与代码库的关联。

如需创建新的 Secure Source Manager 连接,请选择以下选项之一:

控制台

  1. 在 Google Cloud 控制台中,打开 Developer Connect

    前往 Developer Connect

  2. 点击创建连接

  3. 选择 Secure Source Manager 作为您的来源提供方。

  4. 对于区域,请为您的连接选择一个区域。此区域必须与您的 Secure Source Manager 实例所在的区域相同。

  5. 对于名称,请输入连接的名称。

  6. 实例 列表中,选择一个实例。

  7. 点击显示更多 以查看可选配置设置。

  8. 可选:如需停用 Developer Connect 代理,请清除启用 Developer Connect 代理 复选框。

  9. 点击继续

  10. 您可以立即关联到代码库,也可以稍后通过修改连接来执行此操作。如需关联到代码库,请从列表中选择代码库。

  11. 点击关联

Developer Connect 会创建代码库关联,并在 控制台 Google Cloud 中显示这些关联。

gcloud

如需使用 Google Cloud CLI 创建 Developer Connect 连接,请运行以下命令:

gcloud developer-connect connections create CONNECTION_ID \
  --location=REGION \
  --project=CONNECTION_PROJECT_ID \
  --secure-source-manager-instance-config=projects/INSTANCE_PROJECT_ID/locations/REGION/instances/INSTANCE_ID

其中:

  • CONNECTION_ID 是您要创建的 Developer Connect 连接的 ID。
  • REGION 是连接的区域。此区域必须与您的 Secure Source Manager 实例所在的区域相同。
  • CONNECTION_PROJECT_ID 是您要创建连接的 Google Cloud 项目 ID。
  • INSTANCE_PROJECT_ID 是您的 Secure Source Manager 实例所在的 Google Cloud 项目 ID。
  • INSTANCE_ID 是您的 Secure Source Manager 实例的 ID。

curl

如需使用 curl 创建 Developer Connect 连接,请进行以下 API 调用:

curl -H "Authorization: Bearer $(gcloud auth print-access-token)" \
https://developerconnect.googleapis.com/v1/projects/CONNECTION_PROJECT_ID/locations/REGION/connections?connection_id=CONNECTION_ID \
-X POST -H "Content-Type: application/json" \
-d '{"secure_source_manager_instance_config":{"instance":"projects/INSTANCE_PROJECT_ID/locations/REGION/instances/INSTANCE_ID"}}'

其中:

  • CONNECTION_ID 是您要创建的 Developer Connect 连接的 ID。
  • REGION 是连接的区域。此区域必须与您的 Secure Source Manager 实例所在的区域相同。
  • CONNECTION_PROJECT_ID 是您要创建连接的 Google Cloud 项目 ID。
  • INSTANCE_PROJECT_ID 是您的 Secure Source Manager 实例所在的 Google Cloud 项目 ID。
  • INSTANCE_ID 是您的 Secure Source Manager 实例的 ID。

与 Secure Source Manager 建立连接后,您可以关联到代码库。您可以稍后重复执行这些步骤,以便根据需要关联其他代码库。

如需在现有 Secure Source Manager 连接上创建代码库关联,请选择以下选项之一:

控制台

  1. 在 Google Cloud 控制台中打开代码库 页面。

    打开代码库 页面

  2. 点击关联代码库

    关联 Git 代码库 窗格随即打开。

  3. 在连接列表中,选择您的 Secure Source Manager 连接。

  4. 点击继续

  5. 在代码库列表中,选择要关联到的代码库。

  6. 点击创建

Developer Connect 会创建代码库关联,并在 控制台 Google Cloud 中显示这些关联。

gcloud

如需详细了解如何使用 gcloud 管理 Git 代码库关联,请参阅 gcloud developer-connect connections git-repository-links

在 Cloud Build 中使用 Developer Connect 代理

如需在 Cloud Build 中使用 Developer Connect 代理,请执行以下步骤:

  1. 在 Developer Connect 项目中,向 Secure Source Manager 服务代理授予 roles/developerconnect.viewer 角色,或任何具有 developerconnect.gitRepositoryLinks.get 权限的角色。
  2. 将 Secure Source Manager 代码库关联到 Developer Connect gitRepositoryLink。如需查看相关说明,请参阅 关联到 Secure Source Manager 代码库
  3. 在代码库根目录的 .cloudbuild/trigger.yaml 文件中,添加一个新条目以指向目标 gitRepositoryLink 资源。

    例如:

    triggers:
    - name: test-trigger
      project: my-project
      configFilePath: cloudbuild.yaml
      eventType: push
      serviceAccount: projects/my-project/serviceAccounts/my-sa@my-project.iam.gserviceaccount.com
      devConnectGitRepositoryLink: projects/my-project/locations/us-central1/connections/my-ssm-connection/gitRepositoryLinks/my-repo-link
    

操作范围和限制

配置连接后,您可以使用 Developer Connect 代理 URI 对 Secure Source Manager 实例执行安全 git 读取操作,例如 clone

Developer Connect 代理不支持 git push 操作。系统会阻止使用 DevConnect P4SA 的身份进行的写入操作。

您可以更新连接的其他配置,例如启用或停用 Developer Connect 代理,但无法将现有连接指向其他 Secure Source Manager 实例。

后续步骤