本頁面說明如何使用 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 - 在右側面板中,按一下「Add principal」(新增主體)。
-
在顯示的「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 帳戶。
- 從「Roles」(角色) 下拉式選單中選取「Cloud IAP」>「IAP-secured Web App User」(受 IAP 保護的網頁應用程式使用者)。
- 按一下「Save」(儲存)。
正在啟用 IAP
-
在「Identity-Aware Proxy」頁面的「APPLICATIONS」(應用程式) 下方,找出要限制存取權的 instance group 所使用的負載平衡器。如要為資源啟用 IAP,
如要啟用 IAP,請按照下列步驟操作:- 負載平衡器前端設定中至少要有一個通訊協定為 HTTPS。瞭解如何設定負載平衡器。
-
您必須具備
compute.backendServices.update、clientauthconfig.clients.create、clientauthconfig.clients.update和clientauthconfig.clients.getWithSecret權限。這些權限是由角色 (例如專案編輯者角色) 授予。詳情請參閱「管理受 IAP 保護資源的存取權」。
- 在顯示的「Turn on IAP」(啟用 IAP) 視窗中,按一下「Turn On」(啟用),確認要讓 IAP 保護資源。啟用 IAP 後,所有連至負載平衡器的連線都必須提供登入憑證。只有在專案中具備受 IAP 保護的網頁應用程式使用者角色的帳戶,才能取得存取權。
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
執行下列指令,準備
settings.json檔案。cat << EOF > settings.json { "iap": { "enabled":true } } EOF執行下列指令來啟用 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"
啟用 IAP 後,您可以使用 Google Cloud CLI,透過 IAM 角色 roles/iap.httpsResourceAccessor 修改 IAP 存取政策。進一步瞭解如何管理角色和權限。