Nesta página, você aprenderá como proteger uma instância do Compute Engine com o Identity-Aware Proxy (IAP).
Antes de começar
Para ativar o IAP para o Compute Engine, você precisará destes elementos:
- Um projeto do console do Google Cloud com o faturamento ativado.
Um grupo de uma ou mais instâncias do Compute Engine, exibidas por um balanceador de carga.
Saiba mais sobre Como configurar um balanceador de carga HTTPS externo.
Saiba mais sobre como configurar um balanceador de carga HTTP interno.
um nome de domínio registrado no endereço do balanceador de carga.
o código do aplicativo para verificar se todas as solicitações têm uma identidade.
- Saiba mais sobre como conseguir a identidade do usuário.
Se você ainda não configurou a instância do Compute Engine, consulte Como configurar o IAP para o Compute Engine para um tutorial completo.
O IAP usa um cliente OAuth gerenciado pelo Google para autenticar usuários. Somente os usuários da organização podem acessar o aplicativo com o IAP ativado. Se você quiser permitir o acesso a usuários de fora da sua organização, consulte Ativar o IAP para aplicativos externos.É possível ativar o IAP em um serviço de back-end do Compute Engine ou em uma regra de encaminhamento do Compute Engine. Quando você ativa o IAP em um serviço de back-end do Compute Engine, somente esse serviço é protegido pelo IAP. Quando você ativa o IAP em uma regra de encaminhamento do Compute Engine, todas as instâncias do Compute Engine por trás da regra de encaminhamento são protegidas pelo IAP.
Ativar o IAP em uma regra de encaminhamento
É possível ativar o IAP em uma regra de encaminhamento usando o framework de políticas de autorização do balanceador de carga.
gcloud
Run the following command to prepare a
policy.yamlfile. The policy allows clients with an IP address range of10.0.0.0/24to enable IAP on a forwarding rule.$ 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 EOFRun the following command to enable IAP on a forwarding rule.
gcloud network-security authz-policies import AUTHZ_POLICY_NAME \ --source=policy.yaml \ --location=LOCATION \ --project=PROJECT_ID
Replace the following:
PROJECT_ID: The Google Cloud project ID.
LOCATION: The region that the resource is located in.
FORWARDING_RULE_ID: The ID of the forwarding rule resource.
AUTHZ_POLICY_NAME: The name of the authorization policy.
API
Run the following command to prepare a
policy.jsonfile.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": {} } } EOFRun the following command to enable IAP on a forwarding rule.
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"
Replace the following:
PROJECT_ID: The Google Cloud project ID.
LOCATION: The region that the resource is located in.
FORWARDING_RULE_ID: The ID of the forwarding rule resource.
AUTHZ_POLICY_NAME: The name of the authorization policy.
Depois de ativar o IAP em uma regra de encaminhamento, é possível aplicar permissões aos recursos.
Ativar o IAP em um serviço de back-end do Compute Engine
É possível ativar o IAP em um serviço de back-end do Compute Engine por meio desse serviço.
Console
O cliente OAuth gerenciado pelo Google não está disponível ao ativar o IAP usando o console doGoogle Cloud .
如果您尚未配置项目的 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.
- 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.