连接到 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 Admin (roles/developerconnect.admin) 在您要创建 Developer Connect 资源的项目中。
  • 如需向 Developer Connect 服务代理授予权限,您需要具有以下角色: Project IAM Admin (roles/resourcemanager.projectIamAdmin) 在运行 Secure Source Manager 的项目上。

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

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

创建连接

本部分介绍了如何在 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 实例。

后续步骤