将自定义 OAuth 客户端与 IAP 搭配使用

本文档介绍了何时以及如何使用 Identity-Aware Proxy (IAP)的自定义 OAuth 客户端。

默认情况下,IAP 使用 Google 管理的 OAuth 客户端对用户进行身份验证 。Google 管理的 OAuth 客户端只能用于管理组织内 用户的访问权限。

您必须使用自定义 OAuth 配置才能执行以下操作:

  • 管理组织外用户的 IAP 应用访问权限。
  • 管理不属于 Google Cloud 组织的项目的 Web 应用访问权限。
  • 在身份验证期间的权限请求页面中显示自定义品牌信息。

您可以在 IAP 上或直接在平台上配置自定义 OAuth 客户端。

使用自定义 OAuth 客户端时,您必须配置 OAuth 权限请求页面。 如需在权限请求页面中显示自定义品牌信息,您必须将应用提交给 Google 进行验证。如需详细了解验证 流程,请参阅设置 OAuth 权限请求页面

配置自定义 OAuth 客户端时,您需要负责创建和管理凭据,包括安全存储客户端密钥并在必要时与已获授权的客户端共享该密钥。

比较 Google 管理的 OAuth 客户端和自定义 OAuth 客户端

Google 管理的 OAuth 客户端无法以编程方式访问受 IAP 保护的应用。不过,使用 Google 管理的 OAuth 客户端的受 IAP 保护的应用仍可使用通过 programmatic_clients 设置配置的单独 OAuth 客户端或 服务账号 JWT 以编程方式访问。

下表比较了 Google 管理的 OAuth 客户端和自定义 OAuth 客户端。

Google 管理的 OAuth 客户端 自定义 OAuth 客户端
用户 仅限内部使用 可供内部和外部使用
品牌 Google Cloud 品牌 客户拥有的品牌
OAuth 配置 由 Google 配置 由客户配置
OAuth 凭据 由 Google 管理 由客户管理
应用访问权限 仅限浏览器流程 浏览器流程和以编程方式访问

配置品牌信息页

如需使用 Google Cloud 控制台配置自定义品牌信息页,请执行以下 操作:

  1. 在 Google Cloud 控制台中,前往 OAuth 品牌塑造 页面:

    前往“品牌塑造”

  2. 点击开始使用

  3. 应用名称 中,输入权限请求页面的应用名称。

  4. 用户支持电子邮件地址中,输入管理员支持电子邮件 地址。

  5. 对于受众群体,选择内部以限制 组织内用户的访问权限,或选择外部以允许 组织外用户访问。

  6. 联系信息中,输入管理员电子邮件地址,以便 联系受 OAuth 客户端保护的应用。 您将在后续步骤中配置 OAuth 客户端。

  7. 如需创建 OAuth 配置,请点击创建

在 IAP 中配置自定义 OAuth 客户端

本部分介绍了如何在 IAP 中设置自定义 OAuth 客户端。

您可以使用 Google Cloud 控制台通过以下方式为 IAP 创建自定义 OAuth 客户端:

无论您使用哪种方法,都请确保在创建 OAuth 客户端之前配置品牌信息页。这两种方法都会显示所需的重定向 URI 格式。

创建自定义 OAuth 客户端

本部分介绍了如何使用 Google Cloud 控制台创建自定义 OAuth 客户端。您可以在资源层次结构中的任何级别设置自定义 IAP OAuth 客户端。

如需使用 Google Cloud 控制台为资源创建自定义 OAuth 客户端, 请使用以下方法之一:

方法 1:使用 Google Cloud 控制台创建 OAuth 客户端

如果您想单独创建 OAuth 客户端(例如在为资源配置 IAP 之前),请使用此方法。这需要手动添加 IAP 重定向 URI。

  1. 在 Google Cloud 控制台中,前往 API 和服务 > 凭据

  2. 点击创建凭据 ,然后选择 OAuth 客户端 ID

  3. 应用类型 部分,选择 Web 应用

  4. 名称 字段中,为 OAuth 客户端输入描述性名称。

  5. 点击创建 。记下生成的客户端 ID。

  6. 创建 OAuth 客户端后,前往新创建的 OAuth 客户端的 OAuth 客户端详情页面的已获授权的重定向 URI 部分。

  7. 点击添加 URI,然后输入以下特定于 IAP 的已获授权的重定向 URI

    https://iap.googleapis.com/v1/oauth/clientIds/YOUR_CLIENT_ID:handleRedirect

    YOUR_CLIENT_ID 替换为在上一步中获得的客户端 ID。

  8. 点击保存

方法 2:使用 IAP 自动生成凭据创建 OAuth 客户端

使用此方法可让 IAP 为您创建和配置 OAuth 客户端,并自动添加所需的重定向 URI。

  1. 在 Google Cloud 控制台中,前往 IAP 页面。

    前往 IAP

  2. 应用 标签页的资源列表中,找到要配置的资源。

    对于项目级资源,请执行以下操作:

    • 使用 Google Cloud 控制台创建 OAuth 客户端

      1. 操作 列中,依次点击 更多选项 > 设置

      2. 设置 对话框中,选择 OAuth 自定义

      3. 如果您尚未配置权限请求页面,请执行以下操作:

        1. 点击配置权限请求页面

        2. 按照本文档前面提供的说明配置品牌信息页

      4. 在 IAP 设置对话框中,点击自动生成凭据 。IAP 会生成新的 OAuth 客户端和密钥以供此资源使用。在 Google Auth Platform 中,已获授权的重定向 URI 字段包含以下格式的条目:

        https://iap.googleapis.com/v1/oauth/clientIds/CLIENT_ID:handleRedirect
      5. 如需访问客户端 ID 和密钥,请点击下载凭据 。 凭据会保存在 JSON 格式的文件中。由于该文件包含用于访问资源的敏感凭据,因此请确保该文件受到保护或已删除。

      6. 如需保存 IAP OAuth 配置并将 OAuth 客户端应用于 IAP,请点击保存

将自定义 OAuth 客户端应用于 IAP

本部分介绍了如何将 OAuth 客户端应用于 IAP。您可以使用此方法,而不是直接在平台上应用客户端。

  1. 如需创建自定义 OAuth 客户端,请按照 Google Cloud 本文档前面提供的控制台说明进行操作。

  2. 应用自定义 OAuth 客户端。

    gcloud

    如需使用 gcloud CLI 应用自定义 OAuth 客户端,请执行以下操作:

    1. 创建设置 YAML 文件。

      cat << EOF > iap-oauth.yaml
      accessSettings:
      oauthSettings:
        clientId: CLIENT_ID
        clientSecret: CLIENT_SECRET
      EOF
      

      替换以下内容:

      • CLIENT_ID:您之前生成的 OAuth 凭据中的客户端 ID。
      • CLIENT_SECRET:您之前生成的 OAuth 凭据中的客户端密钥。
    2. 如需设置 OAuth 配置,请执行以下操作之一:

      • 如需在项目级层设置 OAuth 配置,请运行以下命令:
      gcloud iap settings set iap-oauth.yaml

      如需在资源层次结构的其他级别设置配置,请使用以下标志之一,而不是 --project 标志。 在资源层次结构的某个级别设置自定义 OAuth 客户端会为在该级别运行的所有服务提供相同的自定义品牌信息。

      * <pre>--folder=<var>FOLDER_ID</var></pre>
      * <code>--organization=<var>ORGANIZATION_ID</var></code>
      
      • 如需在特定服务上设置配置,请运行以下命令:
      gcloud iap settings set iap-oauth.yaml \
          --project=PROJECT_ID \
          --resource-type= RESOURCE_TYPE \
          --region=REGION \
          --service=SERVICE_NAME

      替换以下内容:

      • PROJECT_ID:项目资源的 ID。如需在其他级别设置配置,请使用以下标志之一,而不是 --project 标志:

        • --folder=FOLDER_ID
        • --organization=ORGANIZATION_ID
      • RESOURCE_TYPE:替换为以下资源类型之一,具体取决于资源:

        • app-engine
        • backend-services
        • cloud-run
        • compute
        • folder
        • forwarding-rule
        • iap_web
        • organization
      • REGION:您在其中运行 Cloud Run 服务的区域。

      • SERVICE_NAME:服务的名称。

    Terraform

    如需使用 Terraform 应用自定义 OAuth 客户端,请执行以下操作:

    resource "google_iap_settings" "iap_settings" {
    name = IAP_RESOURCE_NAME
    access_settings {
      oauth_settings {
          oauth_client_id = CLIENT_ID
          oauth_client_secret = CLIENT_SECRET
      }
    }
    }
    

    替换以下内容:

    • IAP_RESOURCE_NAME:服务的 iap_settings资源的资源名称,格式如下: projects/PROJECT_NUMBER/iap_web/REGION/services/SERVICE_NAME
    • CLIENT_ID:您之前生成的 OAuth 凭据中的客户端 ID
    • CLIENT_SECRET:您之前生成的 OAuth 凭据中的客户端密钥

    REST API

    1. 创建设置 JSON 文件。

      cat << EOF > iap-oauth.json
      {
      "accessSettings": {
        "oauthSettings": {
          "clientId": "CLIENT_ID",
          "clientSecret": "CLIENT_SECRET"
        }
      }
      }
      EOF

      替换以下内容:

      • CLIENT_ID:您之前生成的 OAuth 凭据中的客户端 ID。
      • CLIENT_SECRET:您之前生成的 OAuth 凭据中的客户端密钥。
    2. 应用设置文件。

      curl -X PATCH \
      -H "Authorization: Bearer $(gcloud auth print-access-token)"
      -H "Content-Type: application/yaml" \
      "https://iap.googleapis.com/v1/projects/cb-managed-ingress-demo/iap_web/forwarding_rule-us-central1/services/psc-fr:iapSettings?update_mask=iapSettings.accessSettings.oauthSettings.oauthClientId,iapSettings.accessSettings.oauthSettings.oauthClientSecret" \
      -d @iap-oauth.json
      

如需测试您的 Web 应用是否受到 IAP 和 OAuth 客户端的保护,请参阅测试访问权限

在资源上配置自定义 OAuth 客户端的旧版方法

以下部分介绍了为某些资源类型在 IAP 中配置自定义 OAuth 客户端的旧版方法。如果您使用了本文档前面介绍的方法,则可以跳过本 部分。

App Engine

本部分介绍了如何在 App Engine 上启用自定义 OAuth 客户端。

gcloud

在设置项目和 IAP 之前,您需要最新版本的 gcloud CLI。如需了解如何安装 gcloud CLI, 请参阅 安装 gcloud CLI

  1. 如需进行身份验证,请使用 Google Cloud CLI 并运行以下命令。
    gcloud auth login
  2. 如需登录,请按照显示的网址操作。
  3. 登录后,复制显示的验证码并将其粘贴到命令行中。
  4. 运行以下命令,指定包含要使用 IAP 保护的资源的项目。
    gcloud config set project PROJECT_ID
  5. 按照 为 IAP 创建 OAuth 客户端 中的说明配置 OAuth 权限请求页面和创建 OAuth 客户端。
  6. 保存 OAuth 客户端 ID 和 Secret。
  7. 如需启用 IAP,请运行以下命令。
    gcloud iap web enable \
        --oauth2-client-id=CLIENT_ID \
        --oauth2-client-secret=CLIENT_SECRET \
        --resource-type=app-engine

启用 IAP 后,您可以使用 gcloud CLI 通过 IAM 角色 roles/iap.httpsResourceAccessor 修改 IAP 访问权限政策。详细了解如何 管理角色和权限

API

  1. Follow the instructions in Creating OAuth clients for IAP to configure the OAuth consent screen and create the OAuth client.

  2. Save the OAuth client ID and secret.

  3. Run the following command to prepare a settings.json file.

    cat << EOF > settings.json
    {
    "iap":
      {
        "enabled": true,
        "oauth2ClientId": "CLIENT_ID",
        "oauth2ClientSecret":" CLIENT_SECRET"
      }
    }
    EOF
    

  4. Run the following command to enable IAP.

    curl -X PATCH \
    -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
    -H "Accept: application/json" \
    -H "Content-Type: application/json" \
    -d @settings.json \
    "https://appengine.googleapis.com/v1/apps/PROJECT_ID?updateMask=iap"
    

After you enable IAP, you can use the Google Cloud CLI to modify the IAP access policy using the IAM role roles/iap.httpsResourceAccessor. Learn more about managing roles and permissions.

GKE

本部分介绍了如何在 GKE 上启用自定义 OAuth 客户端。

Configure the BackendConfig

If you are running GKE clusters version 1.24 or later, you can configure IAP and GKE by using the Kubernetes Gateway API. See Configure IAP for instructions.

  1. Follow the instructions in Creating OAuth clients for IAP to configure the OAuth consent screen and create the OAuth client.

  2. Create a Kubernetes Secret to wrap the OAuth client.

    kubectl create secret generic MY_SECRET --from-literal=client_id=CLIENT_ID \
      --from-literal=client_secret=CLIENT_SECRET
    
    Replace the following:

    • MY_SECRET: The name of the secret to create
    • CLIENT_ID: The OAuth client ID
    • CLIENT_SECRET: The OAuth client secret

    You should receive confirmation, like the following output, that the Secret was successfully created:

    secret "MY_SECRET" created
    

  3. Add the OAuth credentials to the BackendConfig.

    apiVersion: cloud.google.com/v1
    kind: BackendConfig
    metadata:
      name: CONFIG_DEFAULT
      namespace: my-namespace
    spec:
    iap:
      enabled: true
      oauthclientCredentials:
        secretName: MY_SECRET
    

  4. Enable IAP by associating Service ports with your BackendConfig. See Associating BackendConfig with your Ingress. One way to make this association is to make all ports for the service default to your BackendConfig, which you can do by adding the following annotation to your Service resource:

    metadata:
      annotations:
          beta.cloud.google.com/backend-config: '{"default": "CONFIG_DEFAULT"}}'
    

After you enable IAP, you can use the gcloud CLI to modify the IAP access policy using the IAM role roles/iap.httpsResourceAccessor. Learn more about managing roles and permissions.

Troubleshooting

If the secretName you referenced doesn't exist or isn't structured properly, one of the following error messages will display:

  • BackendConfig default/config-default is not valid: error retrieving secret "foo": secrets "foo" not found. To resolve this error, make sure that you've created the Kubernetes Secret correctly as described in step 2.

  • BackendConfig default/config-default is not valid: secret "foo" missing client_secret data. To resolve this error, make sure that you've created the OAuth credentials correctly. Also, make sure that you referenced the correct client_id and client_secret keys.

负载平衡器后端服务

对于 Compute Engine 和 Cloud Run 用户,本部分介绍了如何在 IAP 中为负载平衡器后端服务设置 OAuth 客户端。

gcloud

Before you set up your project and IAP, you need an up-to-date version of the gcloud CLI. For instructions on how to install the gcloud CLI, see Install the gcloud CLI.

  1. To authenticate, use the Google Cloud CLI and run the following command.
    gcloud auth login
  2. To sign in, follow the URL that appears.
  3. After you sign in, copy the verification code that appears and paste it in the command line.
  4. Run the following command to specify the project that contains the resource that you want to protect with IAP.
    gcloud config set project PROJECT_ID
  5. Follow the instructions in Creating OAuth clients for IAP. to configure the OAuth consent screen and create the OAuth client.
  6. Save the OAuth client ID and secret.
  7. To enable IAP, run either the globally or regionally scoped command.

    Global scope
    gcloud compute backend-services update BACKEND_SERVICE_NAME \
        --global \
        --iap=enabled,oauth2-client-id=CLIENT_ID,oauth2-client-secret=CLIENT_SECRET
    Regional scope
    gcloud compute backend-services update BACKEND_SERVICE_NAME \
        --region REGION_NAME \
        --iap=enabled,oauth2-client-id=CLIENT_ID,oauth2-client-secret=CLIENT_SECRET
        

After you enable IAP, you can use the gcloud CLI to modify the IAP access policy using the IAM role roles/iap.httpsResourceAccessor. Learn more about managing roles and permissions.

API

  1. Follow the instructions in Creating OAuth clients for IAP to configure the OAuth consent screen and create the OAuth client.

  2. Save the OAuth client ID and secret.

  3. Run the following command to prepare a settings.json file.

    cat << EOF > settings.json
    {
    "iap":
      {
        "enabled": true,
        "oauth2ClientId": "CLIENT_ID",
        "oauth2ClientSecret": "CLIENT_SECRET"
      }
    }
    EOF
    

  4. Run the following command to enable IAP.

    curl -X PATCH \
    -H "Authorization: Bearer $(gcloud auth application-default print-access-token)" \
    -H "Accept: application/json" \
    -H "Content-Type: application/json" \
    -d @settings.json \
    "https://compute.googleapis.com/compute/v1/projects/PROJECT_ID/REGION/backendServices/BACKEND_SERVICE_NAME"
    

After you enable IAP, you can use the gcloud CLI to modify the IAP access policy using the IAM role roles/iap.httpsResourceAccessor. Learn more about managing roles and permissions.

测试访问权限

配置自定义 OAuth 客户端后,您可以按照以下步骤测试 IAP 是否使用该客户端来保护您的服务:

  1. 在 IAP 页面的应用 标签页中,查看 IAP 管理的应用。

  2. 访问其中一个应用的网址。如果您是自配置权限请求页面以来首次访问该应用,则会看到您之前配置的权限请求页面。