Halaman ini menjelaskan cara mengamankan instance Compute Engine dengan Identity-Aware Proxy (IAP).
Untuk mengamankan resource yang tidak ada di Google Cloud, lihat Mengamankan aplikasi dan resource lokal.
Sebelum memulai
Untuk mengaktifkan IAP untuk Compute Engine, Anda memerlukan hal berikut:
- Project konsol Google Cloud dengan penagihan diaktifkan.
Grup yang terdiri dari satu atau beberapa instance Compute Engine, yang dilayani oleh load balancer.
Pelajari cara Menyiapkan load balancer HTTPS eksternal.
Pelajari cara menyiapkan load balancer HTTP internal.
Nama domain yang terdaftar ke alamat load balancer Anda.
Kode aplikasi untuk memverifikasi bahwa semua permintaan memiliki identitas.
- Pelajari cara Mendapatkan identitas pengguna.
Jika Anda belum menyiapkan instance Compute Engine, lihat Menyiapkan IAP untuk Compute Engine untuk panduan lengkapnya.
IAP menggunakan klien OAuth yang dikelola Google untuk mengautentikasi pengguna. Hanya pengguna dalam organisasi yang dapat mengakses aplikasi yang mengaktifkan IAP. Jika Anda ingin mengizinkan akses ke pengguna di luar organisasi, lihat Mengaktifkan IAP untuk aplikasi eksternal.Anda dapat mengaktifkan IAP di layanan backend Compute Engine atau di aturan penerusan Compute Engine. Saat Anda mengaktifkan IAP di layanan backend Compute Engine, hanya layanan backend tersebut yang dilindungi oleh IAP. Saat Anda mengaktifkan IAP pada aturan penerusan Compute Engine, semua instance Compute Engine di balik aturan penerusan akan dilindungi oleh IAP.
Mengaktifkan IAP pada aturan penerusan
Anda dapat mengaktifkan IAP pada aturan penerusan menggunakan framework kebijakan otorisasi load balancer.
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.
Setelah mengaktifkan IAP pada aturan penerusan, Anda dapat menerapkan izin ke resource.
Mengaktifkan IAP di layanan backend Compute Engine
Anda dapat mengaktifkan IAP di layanan backend Compute Engine melalui layanan backend tersebut.
console
Klien OAuth yang dikelola Google tidak tersedia saat mengaktifkan IAP menggunakan konsolGoogle 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.
Langkah berikutnya
- Tetapkan aturan konteks yang lebih lengkap dengan menerapkan tingkat akses.
- Lihat Log Akses Kontekstual di Cloud Audit Logs.
- Pelajari IAP lebih lanjut.