配置账号连接器

本文档介绍如何使用 Developer Connect 账号连接器的高级配置选项。

账号连接器是一项 Developer Connect 功能,可帮助您将 Google Cloud 账号与非 Google 开发者工具提供商上的个人账号相关联。用户可以通过为账号连接器选择适当的范围来指定账号连接器要访问的数据类型。为组织创建账号连接器连接后,该组织内的用户可以使用该连接来授权其个人账号与该连接一起使用。

您可以使用 Developer Connect 预配置的 OAuth 客户端 (例如 GitHub 或 GitLab)配置账号连接器,也可以 使用自己的自定义 OAuth 客户端(例如 本地 GitHub Enterprise 实例)。

准备工作

  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. 启用 Developer Connect 也会启用 Secret Manager API

  9. 可选:创建客户管理的 加密密钥 (CMEK),用于加密 Developer Connect 创建的身份验证密钥。

所需角色

如需获得创建账号连接器所需的权限,请让管理员向您授予项目的 Developer Connect OAuth Admin (roles/developerconnect.oauthAdmin) IAM 角色。如需详细了解如何授予角色,请参阅管理对项目、文件夹和组织的访问权限

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

使用可用提供商创建账号连接器

以下步骤介绍了如何为内置提供商创建账号连接器。如果您想创建账号连接器并指定自定义 OAuth 客户端,请参阅 使用自定义 OAuth 客户端创建账号连接器

Google Cloud 控制台

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

    前往 Developer Connect

  2. 选择账号连接器

  3. 点击创建连接器

  4. 选择要在其中创建此账号连接器的区域

  5. 为账号连接器指定一个名称

  6. 对于配置类型 ,选择预配置的 OAuth 客户端

  7. 选择 OAuth 提供商。

  8. 选择是否启用 Developer Connect 代理

    启用代理后,Developer Connect 会代表您向提供商发送 Git 命令或发出 HTTP 调用(或同时执行这两项操作)。了解详情

  9. 选择要为此账号连接器授予 Developer Connect 的范围。

    范围决定了 Developer Connect 可以在 所选提供商的服务中执行哪些操作。范围因提供商而异。您必须至少选择一个范围,除非提供方没有范围。

    根据提供方不同,可能无法选择任何范围。您可以手动添加范围。此外,可能还需要范围 。

  10. 点击创建 以完成此账号连接器的创建。

Google Cloud CLI

如需创建账号连接器,请运行以下命令:

   gcloud alpha developer-connect account-connectors create my-ac \
       --location=LOCATION \
       --provider-oauth-config-system-id=PROVIDER \
       --provider-oauth-config-scopes=SCOPES \
       --project=PROJECT_ID

在此命令中,请替换以下内容:

  • LOCATION 替换为您要在其中创建此账号连接器的区域。

  • PROVIDER 替换为您要连接的 SCM 提供商:

    GITHUBGITLABBITBUCKET_CLOUD

  • PROJECT_ID 替换为您要在其中创建此账号连接器的项目的项目 ID(而不是项目编号)。

使用自定义 OAuth 客户端创建账号连接器

以下步骤介绍了如何配置账号连接器以使用您指定的 OAuth 客户端。如果您想使用内置 提供商创建账号连接器,请参阅 使用可用提供商创建账号连接器

Google Cloud 控制台

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

    前往 Developer Connect

  2. 选择账号连接器

  3. 点击创建连接器

  4. 选择要在其中创建此账号连接器的区域

  5. 为账号连接器指定一个名称

  6. 对于配置类型 ,选择自定义 OAuth 客户端

    创建账号连接器 界面会显示重定向 URI。

  7. 复制重定向 URI

    该 URI 的格式如下:

    https://developerconnect.google.com/redirect/custom/projects/<project_number>/locations/<location>/accountConnectors/<account_connector_id>

    在此 URI 中,<project_number> 是项目编号,而不是项目 ID。

  8. 使用复制的重定向 URI 为您选择的 SCM 创建自定义应用,然后返回此表单以提供 OAuth 客户端的详细信息:

    1. 选择提供商。

    2. Host URI 字段中,提供源代码管理系统的 URI。

    3. Authorization URI 字段中,输入授权服务器的地址。此地址采用以下格式:

      对于 GitHub Enterprise:https://<host_name>/login/oauth/authorize 对于 GitLab Enterprise:https://<host_name>/oauth/authorize 对于 BitBucket Data Center:https://<host_name>/rest/oauth2/latest/authorize

    4. Token URI 字段中,提供用于将授权代码交换为访问令牌的 URI。此地址采用以下格式:

      对于 GitHub Enterprise:https://<host_name>/login/oauth/access_token 对于 GitLab Enterprise:https://<host_name>/oauth/access_token 对于 BitBucket Data Center:https://<host_name>/rest/oauth2/latest/token

    5. 对于客户端 ID,请提供 OAuth 应用的客户端 ID。

    6. 提供客户端密钥 ,以便使用授权服务器对应用进行身份验证。

    7. 如果您的账号连接器将使用 Developer Connect 代理来发出 Git 命令,请选择启用 Developer Connect 代理

    8. 对于范围,请从可用范围列表中进行选择。

    9. (可选)配置网络和安全性:

      1. 选择此账号连接器是使用公共互联网还是专用网络。

        如果是专用网络,请提供以下信息:

        • 要使用的 CA 证书

        • Service Directory 服务的信息:

        您可以标识 Service Directory 服务的项目,也可以指定区域、命名空间和服务名称。

      2. 为此应用启用或停用 PKCE。

        您的 OAuth 服务必须支持 PKCE。

    10. 点击创建 以完成此账号连接器的创建。

Google Cloud CLI

  ```sh
  gcloud alpha developer-connect account-connectors create my-ac \
    --location=LOCATION \
    --project=PROJECT_ID \
    --custom-provider-oauth-config-system-id=PROVIDER \
    --custom-oauth-config-host-uri= HOST_URI \
    --custom-oauth-config-auth-uri=AUTH_URI \
    --custom-provider-oauth-config-scopes=SCOPES \
    --custom-oauth-config-token-uri=TOKEN_URI \
    --custom-oauth-config-client-id=CLIENT_ID \
    --custom-oauth-config-client-secret=SECRET

  ```

在此命令中,请替换以下内容:

  • LOCATION 替换为您要在其中创建此账号连接器的区域。

  • PROJECT_ID 替换为您要在其中创建此账号连接器的项目的项目 ID(而不是项目编号)。

  • PROVIDER 替换为您要连接的 SCM 提供商:

    GITHUB_ENTERPRISEGITLAB_ENTERPRISEBITBUCKET_DATA_CENTER

  • HOST_URI 替换为源代码管理系统的网址。

  • AUTH_URI 替换为用于向您选择的 SCM 提供商进行身份验证的网址。

    例如:

    • GitHub Enterprise:https://<host_name>/login/oauth/authorize

    • GitLab 自主托管:https://<host_name>/oauth/authorize

    • BitBucket Data Center:https://<host_name>/rest/oauth2/latest/authorize

  • SCOPES 替换为要为此账号连接器添加的所有提供方 OAuth 范围的英文逗号分隔列表。

  • TOKEN_URI 替换为用于将授权代码交换为访问令牌的 URI。

  • CLIENT_ID 替换为 OAuth 应用的客户端 ID。

  • SECRET 替换为您要连接的 SCMS 提供的密钥。

关联账号

创建账号连接器后,您仍需要将 Developer Connect 与您在提供方处的账号相关联,然后才能使用账号连接器。

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

    前往 Developer Connect

  2. 选择账号连接器

    账号连接器 页面会列出您在项目中拥有的所有账号连接器。

  3. 点击您要关联提供商账号的账号连接器的名称。

    系统会显示连接器详细信息 页面。

  4. 点击关联账号

    系统会显示 OAuth 身份验证对话框。

  5. 在对话框中提供信息,以便向提供方进行身份验证。

  6. 点击授权

更新账号连接器

您可以更新现有账号连接器,但只能更新该连接器的范围、切换代理设置的开启或关闭状态,或者同时执行这两项操作。如果您更新了给定账号连接器的范围,则该连接器的所有现有用户都会被移除。

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

    前往 Developer Connect

    Developer Connect 会显示 Git 代码库 页面。

  2. 选择账号连接器

    账号连接器 页面会列出您在项目中拥有的所有账号连接器。

  3. 点击您要更新的账号连接器的名称。

    系统会显示连接器详细信息 页面。

  4. 点击修改

  5. 范围 列表中,选择您要为此账号连接器允许的范围。

  6. 点击保存

删除账号连接器

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

    前往 Developer Connect

    Developer Connect 会显示 Git 代码库 页面。

  2. 选择账号连接器

    账号连接器 页面会列出您在项目中拥有的所有账号连接器。

  3. 更多 菜单中,选择删除

    系统会显示连接器详细信息 页面。

  4. 点击修改

后续步骤