為 Compute Engine 啟用 IAP

本頁面說明如何使用 Identity-Aware Proxy (IAP) 保護 Compute Engine 執行個體。

事前準備

如要為 Compute Engine 啟用 IAP,請先準備好下列項目:

如果您尚未設定 Compute Engine 執行個體,請參閱「為 Compute Engine 設定 IAP」一文,取得完整逐步操作說明。

IAP 會使用 Google 代管的 OAuth 用戶端來驗證使用者。只有機構內的使用者可以存取啟用 IAP 的應用程式。如要允許機構外使用者存取,請參閱「為外部應用程式啟用應用程式內購功能」。

您可以在 Compute Engine 後端服務或 Compute Engine 轉送規則上啟用 IAP。在 Compute Engine 後端服務上啟用 IAP 時,只有該後端服務受到 IAP 保護。在 Compute Engine 轉送規則上啟用 IAP 後,轉送規則後方的所有 Compute Engine 執行個體都會受到 IAP 保護。

在轉送規則中啟用 IAP

您可以使用負載平衡器的授權政策架構,在轉送規則中啟用 IAP。

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.1/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.1"
          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
  1. Run the following command to enable IAP on a forwarding rule.
gcloud beta 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.1",
                "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/v1beta1/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.

在轉送規則中啟用 IAP 後,您就可以將權限套用至資源

在 Compute Engine 後端服務上啟用 IAP

您可以透過該後端服務,在 Compute Engine 後端服務上啟用 IAP。

主控台

使用 Google Cloud 主控台啟用 IAP 時,Google 代管的 OAuth 用戶端就無法使用。

如果您尚未設定專案的 OAuth 同意畫面,系統會提示您進行設定。如要設定 OAuth 同意畫面,請參閱「設定 OAuth 同意畫面」。

如果您執行的是 GKE 叢集 1.24 以上版本,可以使用 Kubernetes Gateway API 設定 IAP 和 GKE。如要這樣做,請完成下列步驟,然後按照「設定應用程式內購功能」中的操作說明進行。請勿設定 BackendConfig

設定 IAP 存取權

  1. 前往「Identity-Aware Proxy」頁面
    前往「Identity-Aware Proxy」頁面
  2. 選取要使用 IAP 保護的專案。
  3. 找出您要授予存取權的資源,然後勾選旁邊的核取方塊。

    如果您沒有看到任何資源,請確認資源已建立,且 BackendConfig Compute Engine ingress 控制器已同步。

    如要確認後端服務可用,請執行下列 gcloud 指令:

    gcloud compute backend-services list
  4. 在右側面板中,按一下「Add principal」
  5. 在隨即顯示的「Add principals」(新增主體) 對話方塊中,輸入群組或個別使用者的電子郵件地址,這些群組或個別使用者應擁有專案的「IAP-Secured Web App User」(受 IAP 保護的網路應用程式使用者) 角色。

    以下類型的主體可以擁有這個角色:

    • Google 帳戶:user@gmail.com
    • Google 群組:admins@googlegroups.com
    • 服務帳戶:server@example。gserviceaccount.com
    • Google Workspace 網域:example.com

    請務必新增您可以存取的 Google 帳戶。

  6. 從「Roles」(角色) 下拉式清單中選取「Cloud IAP」>「IAP-secured Web App User」(受 IAP 保護的網路應用程式使用者)
  7. 按一下「Save」(儲存)

正在啟用 IAP

  1. 在「Identity-Aware Proxy」頁面的「APPLICATIONS」下方,找出您要限制存取權的 instance group 所服務的負載平衡器。如要為資源啟用 IAP,請在「IAP」欄中
    如要啟用 IAP:
    • 負載平衡器前端設定中至少一個通訊協定必須為 HTTPS。瞭解如何設定負載平衡器
    • 您需要具備 compute.backendServices.updateclientauthconfig.clients.createclientauthconfig.clients.getWithSecret 權限。這些權限係由諸如專案編輯者角色等角色來授予。詳情請參閱「管理受 IAP 保護資源的存取權」。
  2. 在隨即顯示的「Turn on IAP」(開啟 IAP) 視窗中,按一下「Turn On」(開啟),確認您要使用 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. Run the following command to prepare a settings.json file.

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

  2. 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://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.