本页介绍如何使用 Identity-Aware Proxy (IAP) 保护 App Engine 实例。
准备工作
如需为 App Engine 启用 IAP,您需要满足以下条件:
- Google Cloud 启用了结算功能的控制台项目。
如果您尚未设置 App Engine 实例,请参阅部署 App Engine 以查看完整演示。
IAP 使用 Google 管理的 OAuth 客户端对用户进行身份验证。只有组织内的用户才能访问启用了 IAP 的应用。 如果您想允许组织外部的用户访问,请参阅为外部应用启用 IAP。
启用 IAP
控制台
使用 Google Cloud 控制台启用 IAP 时,Google 管理的 OAuth 客户端不可用。
如果您尚未配置项目的 OAuth 同意屏幕,系统会提示您执行此操作。如需配置 OAuth 同意屏幕,请参阅设置 OAuth 同意屏幕。
设置 IAP 访问权限
-
转到 Identity-Aware Proxy 页面。
转到 Identity-Aware Proxy 页面 - 选择要使用 IAP 保护的项目。
- 选中您要授予访问权限的资源旁边的复选框。
- 在右侧面板上,点击添加主账号。
-
在显示的添加主账号对话框中,输入群组或个人(应拥有项目的 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 页面的 APPLICATIONS(应用)下方,找到要限制访问的应用。如需为某资源开启 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
- Click the URL that appears and sign in.
- 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 applications that you want to protect with IAP.
gcloud config set project PROJECT_ID
-
To enable IAP, run the following command.
gcloud iap web enable --resource-type=app-engine --versions=version
-
Add principals who should have the IAP-secured Web App user role to the project.
gcloud projects add-iam-policy-binding PROJECT_ID \ --member=PRINCIPAL_IDENTIFIER \ --role=roles/iap.httpsResourceAccessor
- Replace PROJECT_ID with your project ID.
- Replace PRINCIPAL_IDENTIFIER with the necessary principals. This can be a
type of domain, group, serviceAccount, or user. For example,
user:myemail@example.com
.
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.json
file.cat << EOF > settings.json { "iap": { "enabled":true } } EOF
Run 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://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.
测试用户身份验证
使用您添加到 IAP 且拥有 IAP-secured Web App User 角色的 Google 账号访问应用网址,如上所述。您应该对该应用具有不受限制的访问权限。
使用 Chrome 中的无痕式窗口访问该应用,并根据提示登录。如果您尝试使用未经 IAP-secured Web App User 角色授权的账号访问该应用,则会看到一条消息,提示您无权访问。