Activer IAP pour Compute Engine

Cette page explique comment sécuriser une instance Compute Engine avec Identity-Aware Proxy (IAP).

Avant de commencer

Pour activer IAP pour Compute Engine, vous devez disposer des éléments suivants :

  • Un projet de la console Google Cloud avec la facturation activée.
  • Un nom de domaine enregistré à l'adresse de votre équilibreur de charge

  • Un code d'application pour vérifier que toutes les requêtes ont une identité

Si votre instance Compute Engine n'est pas déjà configurée, consultez la page Configurer IAP pour Compute Engine pour obtenir une présentation complète.

IAP utilise un client OAuth géré par Google pour authentifier les utilisateurs. Seuls les utilisateurs de l'organisation peuvent accéder à l'application compatible avec IAP. Si vous souhaitez autoriser l'accès aux utilisateurs extérieurs à votre organisation, consultez Activer IAP pour les applications externes.

Vous pouvez activer IAP sur un service de backend Compute Engine ou sur une règle de transfert Compute Engine. Lorsque vous activez IAP sur un service de backend Compute Engine, seul ce service de backend est protégé par IAP. Lorsque vous activez IAP sur une règle de transfert Compute Engine, toutes les instances Compute Engine derrière la règle de transfert sont protégées par IAP.

Activer IAP sur une règle de transfert

Vous pouvez activer IAP sur une règle de transfert à l'aide du framework des stratégies d'autorisation de l'équilibreur de charge.

gcloud

  1. Run the following command to prepare a policy.yaml file. The policy allows clients with an IP address range of 10.0.0.0/24 to 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
    EOF
    
  2. Run 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

  1. Run the following command to prepare a policy.json file.

    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
    
  2. Run 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.

Après avoir activé IAP sur une règle de transfert, vous pouvez appliquer des autorisations aux ressources.

Activer IAP sur un service de backend Compute Engine

Vous pouvez activer IAP sur un service de backend Compute Engine via ce service de backend.

Console

Le client OAuth géré par Google n'est pas disponible lorsque vous activez IAP à l'aide de la consoleGoogle Cloud .

如果您尚未配置项目的 OAuth 权限请求页面,系统会提示您进行配置。如需配置 OAuth 权限请求页面,请参阅设置 OAuth 权限请求页面。

如果您运行的是 GKE 1.24 版或更高版本,则可以使用 Kubernetes Gateway API 配置 IAP 和 GKE。为此,请完成以下步骤,然后按照配置 IAP 中的说明操作。请勿配置 BackendConfig。

设置 IAP 访问权限

  1. 转到 Identity-Aware Proxy 页面。
    转到 Identity-Aware Proxy 页面
  2. 选择要使用 IAP 保护的项目。
  3. 选中要授予访问权限的资源旁边的复选框。

    如果您没有看到资源,请确保已创建资源,并且 BackendConfig 和 Compute Engine Ingress 控制器已同步。

    如需验证后端服务是否可用,请运行以下 gcloud 命令:

    gcloud compute backend-services list
  4. 在右侧面板上,点击添加主账号。
  5. 在显示的添加主账号对话框中,输入群组或个人(应拥有项目的 IAP-secured Web App User 角色)的电子邮件地址。

    以下种类的主账号可以具有此角色:

    • Google 账号:user@gmail.com
    • Google 网站论坛:admins@googlegroups.com
    • 服务账号:server@example.
    • Google Workspace 网域:example.com

    请务必添加您有权访问的 Google 账号。

  6. 从角色下拉列表中选择 Cloud IAP > 受 IAP 保护的网页应用用户。
  7. 点击保存。

启用 IAP

  1. 在 Identity-Aware Proxy 页面上的应用下,找到为要限制访问的 instance group 提供服务的负载均衡器。如需为某资源开启 IAP,请
    如需启用 IAP,请执行以下操作:
    • 负载均衡器前端配置中至少一个协议必须是 HTTPS。了解如何设置负载均衡器。
    • 您需要 compute.backendServices.update、clientauthconfig.clients.create、clientauthconfig.clients.update 和 clientauthconfig.clients.getWithSecret 权限。这些权限由 Project Editor 等角色授予。如需了解详情,请参阅管理对受 IAP 保护的资源的访问权限。
  2. 在出现的开启 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.

  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. To enable IAP, run either the globally or regionally scoped command.

    Global scope
    gcloud compute backend-services update BACKEND_SERVICE_NAME --global --iap=enabled
    Regional scope
    gcloud 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

  1. Exécutez la commande suivante pour préparer un fichier settings.json.

    cat << EOF > settings.json
    {
    "iap":
      {
        "enabled":true
      }
    }
    EOF
    

  2. Exécutez la commande suivante pour activer 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"
    

Une fois IAP activé, vous pouvez utiliser la Google Cloud CLI pour modifier la stratégie d'accès IAP à l'aide du rôle IAM roles/iap.httpsResourceAccessor. Découvrez comment gérer les rôles et les autorisations.