创建 Private Service Connect 实例

本页面介绍了如何为 Secure Source Manager 创建 Private Service Connect 实例。

Private Service Connect 实例不会向公共互联网公开任何端点。创建 Private Service Connect 实例时,系统会创建端点,以提供对该实例的 SSH (Git) 和 HTTPS(Git、网页界面、API)访问权限。端点必须在您的专用网络上分配 IP 地址。进出 Private Service Connect 实例的所有流量都会通过 Private Service Connect 端点。

如需详细了解如何使用 Private Service Connect 从 VPC 网络内部以私密方式访问托管式服务,请参阅 Private Service Connect

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

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

准备工作

  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. 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. 安装 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. 安装 Google Cloud CLI。

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

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

    gcloud init

所需的角色

如需获得创建 Private Service Connect Secure Source Manager 实例所需的权限,请让管理员为您授予以下 IAM 角色:

创建服务身份和证书授权机构池

本部分介绍了如何使用您自己的 CA 证书。如果您打算在私有实例中使用自定义网域,则必须使用自己的 CA 证书。如需在不使用自定义网域的情况下使用 Google管理的证书,您只需运行 gcloud source-manager instances create 命令,而无需创建服务身份和 CA 池。如需了解详情,请参阅创建 Private Service Connect Secure Source Manager 实例

如果您使用的是自己的 CA 证书,则在创建私有 Secure Source Manager 实例时,必须指定证书授权机构 (CA) 池名称。系统会向 CA 池发送为实例的 HTTPS SSL 证书签名的请求。

  1. 运行以下命令,在您的项目中为 Secure Source Manager API 创建服务身份:

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

    其中,PROJECT_ID 是您的项目 ID。

    输出类似于以下内容:

    Service identity created: service-PROJECT_NUM@gcp-sa-sourcemanager.iam.gserviceaccount.com

    其中,service-PROJECT_NUM@gcp-sa-sourcemanager.iam.gserviceaccount.com 是 Secure Source Manager 服务代理,PROJECT_NUM 是您的项目编号。

  2. 创建证书授权机构 (CA) 池,并启用基于 CSR 的证书请求。

  3. 创建 CA。您可以根据需要创建根 CA 或从属 CA。

    如需详细了解根 CA 和下级 CA 之间的区别,请参阅确定证书授权机构设置

  4. 授予 Secure Source Manager API 服务身份权限,以便在 CA 池中请求新证书:

    gcloud privateca pools add-iam-policy-binding CA_POOL_NAME \
        --location=CA_LOCATION \
        --member='serviceAccount:service-PROJECT_NUMBER@gcp-sa-sourcemanager.iam.gserviceaccount.com' \
        --role='roles/privateca.certificateRequester' \
        --project=CA_PROJECT_ID
    

    替换以下内容:

    • CA_POOL_NAME 替换为您为 CA 池指定的名称。
    • 将 CA_LOCATION 替换为 CA 池的区域或可用区。
    • PROJECT_NUMBER 替换为启用了 Secure Source Manager 的项目的项目编号。如需查找项目编号,请参阅标识项目
    • 将 CA_PROJECT_ID 替换为您在其中创建 CA 池的项目的项目 ID。

创建 Private Service Connect Secure Source Manager 实例

您可以为 HTML、API、Git HTTP 和 Git SSH 端点向私有实例分配自定义网域。自定义网域需要您自己的 CA 证书。因此,在创建具有自定义网域的实例时,您必须提供 CA 池。您只能在创建实例期间使用 API 配置自定义网域。实例创建后,您将无法修改这些设置。

gcloud

使用以下命令创建 Private Service Connect 实例:

gcloud source-manager instances create INSTANCE_ID \
  --region=LOCATION \
  --project=PROJECT_ID \
  --is-private \
  --ca-pool=projects/CA_PROJECT/locations/CA_LOCATION/caPools/CA_POOL_NAME

替换以下内容:

  • 将 INSTANCE_ID 替换为您要为实例指定的名称。
  • 将 LOCATION 替换为要在其中创建实例的区域。如需了解支持的位置,请参阅位置
  • PROJECT_ID 替换为要在其中创建实例的项目的名称。
  • 将 CA_PROJECT 替换为 CA 池项目的名称。如果您使用自己的 CA 证书,则必须提供此参数。
  • 将 CA_LOCATION 替换为您在其中创建 CA 池的区域。 如果您使用自己的 CA 证书,则必须提供此参数。
  • CA_POOL_NAME:CA 池的名称。如果您使用自己的 CA 证书,则必须提供此参数。

API

使用以下命令创建 Private Service Connect 实例:

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 '{"private_config":{"is_private":true,"ca_pool":"projects/CA_PROJECT/locations/CA_LOCATION/caPools/CA_POOL_NAME","custom_host_config":{"api":"API_CUSTOM_DOMAIN", "html":"HTML_CUSTOM_DOMAIN", "git_ssh":"GIT_SSH_CUSTOM_DOMAIN", "git_http":"GIT_HTTP_CUSTOM_DOMAIN"}}}'

替换以下内容:

  • 将 INSTANCE_ID 替换为您要为实例指定的名称。
  • 将 LOCATION 替换为要在其中创建实例的区域。如需了解支持的位置,请参阅位置
  • PROJECT_ID 替换为要在其中创建实例的项目的名称。
  • 将 CA_PROJECT 替换为 CA 池项目的名称。如果您使用自己的 CA 证书或自定义网域,则必须提供此参数。
  • 将 CA_LOCATION 替换为您在其中创建 CA 池的区域。 如果您使用自己的 CA 证书或自定义网域,则必须提供此参数。
  • CA_POOL_NAME:CA 池的名称。如果您使用自己的 CA 证书或自定义网域,则必须提供此参数。
  • API_CUSTOM_DOMAIN,其中包含用于 API 访问的自定义网域,例如 api.source.example.com。仅在您使用自定义网域时需要。
  • 将 HTML_CUSTOM_DOMAIN 替换为用于访问 Web 界面 (UI) 的自定义网域,例如 source.example.com。仅当您使用自定义网域时才需要。
  • 将 GIT_SSH_CUSTOM_DOMAIN 替换为您的自定义网域,以用于 Git SSH 访问,例如 ssh.source.example.com。仅当您使用自定义网域时才需要。
  • 将 GIT_HTTP_CUSTOM_DOMAIN 替换为您的自定义网域,以用于 Git HTTP 访问,例如 git.source.example.com。仅当您使用自定义网域时才需要。

Terraform

如需使用 Terraform 创建 Private Service Connect 实例,请使用 google_secure_source_manager_instance 资源并将 private_config.is_private 字段设置为 true

长时间运行的实例创建操作开始。创建实例最多需要 60 分钟。输出类似于以下内容:

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-a9b97a2e 是 OPERATION_NAME。

您需要 OPERATION_NAME 才能检查操作的状态。

如需检查操作的状态,请运行以下命令:

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

替换以下内容:

  • OPERATION_NAME 替换为创建命令响应中的操作名称。
  • 将 LOCATION 替换为要在其中创建实例的区域。如需了解支持的位置,请参阅位置

创建实例后,输出中会列出 Private Service Connect 端点 httpServiceAttachmentsshServiceAttachment,以及 htmlapigitHttpgitSsh 的主机名。

访问私有实例

您可以通过创建基于转发规则的 Private Service Connect 端点或创建基于负载平衡器的 Private Service Connect 后端来访问 Private Service Connect 实例。

基于负载平衡器的 Private Service Connect 后端更加灵活,可让您与其他 Google Cloud 服务(如 Cloud Build)集成,但更加复杂,并且会产生额外费用。

基于转发规则的 Private Service Connect 端点更简单且费用更低。不过,您无法连接到 Cloud Build 或其他Google Cloud 服务。

如需为 Private Service Connect 实例设置基于负载平衡器的 Private Service Connect 后端,并将其连接到 Cloud Build,请参阅将 Cloud Build 连接到 Private Service Connect 实例

基于转发规则设置 Private Service Connect 端点

  1. 如需创建 Private Service Connect 端点,请按照通过端点访问已发布的服务中的说明进行操作。

  2. 如需获取 Secure Source Manager 实例的 HTTP 和 SSH 服务附件的 URI,请运行以下命令:

    gcloud source-manager instances describe INSTANCE_ID \
        --region=LOCATION
    

    替换以下内容:

    • INSTANCE_ID 替换为您的实例名称。如需列出给定区域中的实例,请运行 gcloud source-manager instances list --region=LOCATION
    • 将 LOCATION 替换为您的实例所在的位置。如需了解支持的位置,请参阅位置

    输出包括:

    createTime: '2023-09-22T18:21:35.729454612Z'
    hostConfig:
      api: my-project-012345678901-api.us-central1.p.sourcemanager.dev
      gitHttp: my-project-012345678901-git.us-central1.p.sourcemanager.dev
      gitSsh: my-project-012345678901-ssh.us-central1.p.sourcemanager.dev
      html: my-project-012345678901.us-central1.p.sourcemanager.dev
    name: projects/my-project/locations/us-central1/instances/my-instance
    privateConfig:
      caPool: projects/my-project/locations/us-central1/caPools/my-ca-pool
      httpServiceAttachment: projects/abc12345d1a1234a0a-tp/regions/us-central1/serviceAttachments/http-psc
      isPrivate: true
      sshServiceAttachment: projects/abc12345d1a1234a0a-tp/regions/us-central1/serviceAttachments/ssh-psc
    state: ACTIVE
    updateTime: '2023-09-22T18:39:53.390563549Z'
    
    • projects/abc12345d1a1234a0a-tp/regions/us-central1/serviceAttachments/http-psc 是实例的 HTTP 服务连接 URI。
    • projects/abc12345d1a1234a0a-tp/regions/us-central1/serviceAttachments/ssh-psc 是您实例的 SSH 服务连接 URI。
  3. 创建 Private Service Connect 端点后,您需要设置专用 DNS 记录。如需查看有关设置专用 DNS 记录的说明,请参阅手动配置 DNS。 如果您为实例配置了自定义网域,请创建将自定义网域映射到服务附件的内部 IP 地址的 DNS 记录。否则,请使用实例的 hostConfig 字段中的主机名:

    1. hostConfig.html(或您的 HTML 自定义网域)、hostConfig.api(或您的 API 自定义网域)和 hostConfig.gitHttp(或您的 Git HTTP 自定义网域)的值映射到 HTTP 服务附件的内部 IP 地址。
    2. hostConfig.ssh(或您的 Git SSH 自定义网域)的值映射到 SSH 服务附件的内部 IP 地址。

    例如,在上文的示例输出中,hostConfig.html 的值为 my-project-012345678901.us-central1.p.sourcemanager.dev

使用自定义网域进行 Git 身份验证

如果您为私有实例使用自定义网域,请在每台机器上执行以下客户端配置,以通过 HTTPS 使用 Git。 此配置会将 Git 连接到 gcloud 凭据帮助程序,以针对您的自定义网域进行身份验证。

  1. 配置 Git 以针对自定义 Git HTTP 网域使用 gcloud 帮助程序:

    git config --global credential.'https://GIT_HTTP_CUSTOM_DOMAIN'.helper gcloud.sh
    

    将 GIT_HTTP_CUSTOM_DOMAIN 替换为您为 Git HTTP 访问配置的自定义网域。

  2. gcloud 中授权您的自定义网域:

    gcloud config set core/credentialed_hosted_repo_domains GIT_HTTP_CUSTOM_DOMAIN
    

    如需授权多个网域,请以英文逗号分隔列表的形式提供这些网域。

将 CA 与 Secure Source Manager API 搭配使用

Secure Source Manager 具有控制平面和数据平面。数据平面端点由 Secure Source Manager 实例直接托管,因此您必须连接到实例的 API 主机名才能访问这些端点。

如需使用私有 Secure Source Manager 实例,请使用您在创建服务身份和证书授权机构池部分中创建的 CA。

  1. 如需下载 CA,请运行以下命令:

    gcloud privateca pools get-ca-certs CA_POOL \
        --location LOCATION \
        --output-file=root-cert.pem \
        --project PROJECT
    
  2. 如需向实例发出 API 调用,请将 CA 证书传递给数据平面 Secure Source Manager API。

    例如,以下命令将 CA 证书 root-cert.pem 传递给 Secure Source Manager API,以列出位置 us-central1 中私有实例 my-instance 中的代码库。

    curl \
        --cacert root-cert.pem \
        -H "Authorization: Bearer $(gcloud auth print-access-token)" \
        https://my-instance-01234567890-api.us-central1.sourcemanager.dev/v1/projects/01234567890/locations/us-central1/repositories
    

后续步骤