本页介绍如何使用 Identity-Aware Proxy (IAP) 保护 Compute Engine 实例。
准备工作
要为 Compute Engine 启用 IAP,您需要以下各项:
- 启用了结算功能的 Google Cloud 控制台项目。
- 由负载平衡器提供服务的一个或多个 Compute Engine 实例。
- 了解如何设置外部 HTTPS 负载均衡器。
- 了解如何设置内部 HTTP 负载均衡器。
- 已注册到负载均衡器地址的域名。
- 用于验证所有请求都拥有一个身份的应用代码。
- 了解如何获取用户的身份。
如果您尚未设置 Compute Engine 实例,请参阅为 Compute Engine 设置 IAP 以查看完整演示。
IAP 使用 Google 管理的 OAuth 客户端对用户进行身份验证。 只有组织内的用户才能访问启用了 IAP 的应用。如果您想允许组织外部的用户访问,请参阅为外部应用启用 IAP。您可以在 Compute Engine 后端服务或 Compute Engine 转发规则上启用 IAP。在 Compute Engine 后端服务上启用 IAP 时,只有该后端服务受 IAP 保护。在 Compute Engine 转发规则上启用 IAP 后,该转发规则背后的所有 Compute Engine 实例都会受到 IAP 的保护。
在转发规则上启用 IAP
您可以使用负载均衡器授权政策框架在转发规则上启用 IAP。
gcloud
- 运行以下命令以准备
policy.yaml文件。 该政策允许 IP 地址范围为10.0.0.0/24的客户端在转发规则上启用 IAP。
$ cat << EOF > policy.yaml
action: CUSTOM
description: authz policy with Cloud IAP
name: AUTHZ_POLICY_NAME
httpRules:
- from:
sources:
- ipBlocks:
- prefix: "10.0.0.0"
length: 24
customProvider:
cloudIap: {}
target:
loadBalancingScheme: EXTERNAL_MANAGED
resources:
- https://www.googleapis.com/compute/v1/projects/PROJECT_ID/regions/LOCATION/forwardingRules/FORWARDING_RULE_ID
EOF
- 运行以下命令,在转发规则上启用 IAP。
gcloud network-security authz-policies import AUTHZ_POLICY_NAME \ --source=policy.yaml \ --location=LOCATION \ --project=PROJECT_ID
替换以下内容:
- PROJECT_ID: Google Cloud 项目 ID。
- LOCATION:资源所在的区域。
- FORWARDING_RULE_ID:转发规则资源的 ID。
- AUTHZ_POLICY_NAME:授权政策的名称。
API
- 运行以下命令以准备
policy.json文件。cat << EOF > policy.json { "name": "AUTHZ_POLICY_NAME", "target": { "loadBalancingScheme": "INTERNAL_MANAGED", "resources": [ "https://www.googleapis.com/compute/v1/projects/PROJECT_ID/regions/LOCATION/forwardingRules/FORWARDING_RULE_ID" ], }, "action": "CUSTOM", "httpRules": [ { "from": { "sources": { "ipBlocks": [ { "prefix": "10.0.0.0", "length": 24 } ] } } } ], "customProvider": { "cloudIap": {} } } EOF 运行以下命令,在转发规则上启用 IAP。
curl -X PATCH \ -H "Authorization: Bearer $(gcloud auth print-access-token)" \ -H "Accept: application/json" \ -H "Content-Type: application/json" \ -d @policy.json \ "https://networksecurity.googleapis.com/v1/projects/PROJECT_ID/locations/LOCATION/authzPolicies"
替换以下内容:
- PROJECT_ID: Google Cloud 项目 ID。
- LOCATION:资源所在的区域。
- FORWARDING_RULE_ID:转发规则资源的 ID。
- AUTHZ_POLICY_NAME:授权政策的名称。
在转发规则上启用 IAP 后,您可以向资源应用权限。
在 Compute Engine 后端服务上启用 IAP
您可以通过 Compute Engine 后端服务在该后端服务上启用 IAP。
控制台
使用Google Cloud 控制台启用 IAP 时,Google 管理的 OAuth 客户端不可用。
如果您尚未配置项目的 OAuth 权限请求页面,系统会提示您进行配置。如需配置 OAuth 权限请求页面,请参阅设置 OAuth 权限请求页面。
如果您运行的是 GKE 1.24 版或更高版本,则可以使用 Kubernetes Gateway API 配置 IAP 和 GKE。为此,请完成以下步骤,然后按照配置 IAP 中的说明操作。请勿配置 BackendConfig。
设置 IAP 访问权限
-
转到 Identity-Aware Proxy 页面。
转到 Identity-Aware Proxy 页面 - 选择要使用 IAP 保护的项目。
-
选中要授予访问权限的资源旁边的复选框。
如果您没有看到资源,请确保已创建资源,并且 BackendConfig 和 Compute Engine Ingress 控制器已同步。
如需验证后端服务是否可用,请运行以下 gcloud 命令:
gcloud compute backend-services list - 在右侧面板上,点击添加主账号。
-
在显示的添加主账号对话框中,输入群组或个人(应拥有项目的 IAP-secured Web App User 角色)的电子邮件地址。
以下种类的主账号可以具有此角色:
- Google 账号:user@gmail.com
- Google 网站论坛:admins@googlegroups.com
- 服务账号:server@example.gserviceaccount.com
- Google Workspace 网域:example.com
请务必添加您有权访问的 Google 账号。
- 从角色下拉列表中选择 Cloud IAP > 受 IAP 保护的网页应用用户。
- 点击保存。
启用 IAP
-
在 Identity-Aware Proxy 页面上的应用下,找到为要限制访问的 instance group 提供服务的负载均衡器。如需为某资源开启 IAP,请
如需启用 IAP,请执行以下操作:- 负载均衡器前端配置中至少一个协议必须是 HTTPS。了解如何设置负载均衡器。
-
您需要
compute.backendServices.update、clientauthconfig.clients.create、clientauthconfig.clients.update和clientauthconfig.clients.getWithSecret权限。这些权限由 Project Editor 等角色授予。如需了解详情,请参阅管理对受 IAP 保护的资源的访问权限。
- 在出现的开启 IAP 窗口中,点击开启以确认您想用 IAP 保护资源。 开启 IAP 后,对于与负载均衡器的所有连接,它都要求提供登录凭证。 只有具有项目的 IAP-Secured Web App User 角色的账号才能获得访问权限。
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.
-
To authenticate, use the Google Cloud CLI and run the following command.
gcloud auth login - To sign in, follow the URL that appears.
- After you sign in, copy the verification code that appears and paste it in the command line.
-
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 -
To enable IAP, run either the globally or regionally scoped command.
Global scope Regional scopegcloud compute backend-services update BACKEND_SERVICE_NAME --global --iap=enabledgcloud compute backend-services update BACKEND_SERVICE_NAME --region REGION_NAME --iap=enabled
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
Run the following command to prepare a
settings.jsonfile.cat << EOF > settings.json { "iap": { "enabled":true } } EOFRun the following command to enable IAP.
curl -X PATCH \ -H "Authorization: Bearer $(gcloud auth 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 Google Cloud CLI to modify the
IAP access policy using the IAM role
roles/iap.httpsResourceAccessor. Learn more about
managing roles and permissions.