בדף הזה מוסבר איך לאבטח מכונה של Compute Engine באמצעות שרת proxy לאימות זהויות (IAP).
לפני שמתחילים
כדי להפעיל את IAP ב-Compute Engine, צריך:
- פרויקט במסוף עם חיוב מופעל. Google Cloud
- קבוצה של מכונה וירטואלית אחת או יותר של Compute Engine, שמוגשת על ידי מאזן עומסים.
- שם דומיין שרשום לכתובת של מאזן העומסים.
- קוד האפליקציה לאימות שלכל הבקשות יש זהות.
אם עדיין לא הגדרתם את המכונה של Compute Engine, תוכלו להיעזר במדריך המלא בנושא הגדרת IAP ל-Compute Engine.
מערכת IAP משתמשת בלקוח OAuth שמנוהל על ידי Google כדי לאמת את המשתמשים. רק משתמשים בארגון יכולים לגשת לאפליקציה עם רכישות מתוך האפליקציה. אם רוצים לאפשר גישה למשתמשים מחוץ לארגון, אפשר לעיין במאמר בנושא הפעלת IAP לאפליקציות חיצוניות.
אפשר להפעיל את IAP בשירות קצה עורפי של Compute Engine או בכלל העברה של Compute Engine. כשמפעילים IAP בשירות לקצה העורפי של Compute Engine, רק השירות הזה מוגן על ידי IAP. כשמפעילים את IAP בכלל העברה ב-Compute Engine, כל המכונות ב-Compute Engine שמאחורי כלל ההעברה מוגנות על ידי IAP.
הפעלת IAP בכלל העברה
אפשר להפעיל את IAP בכלל העברה באמצעות מסגרת כללי ההרשאה של מאזן העומסים.
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
EOF
- 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
- 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": {} } } EOF 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.
אחרי שמפעילים את IAP בכלל העברה, אפשר להחיל הרשאות על משאבים.
הפעלת IAP בשירות לקצה העורפי של Compute Engine
אפשר להפעיל את IAP בשירות לקצה העורפי של Compute Engine דרך השירות הזה.
console
לקוח ה-OAuth שמנוהל על ידי Google לא זמין כשמפעילים את IAP באמצעות Google 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 入口控制器已同步。
如需验证后端服务是否可用,请运行以下 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
לפני שמגדירים את הפרויקט ואת הרכישות מתוך האפליקציה, צריך לוודא שיש לכם גרסה עדכנית של ה-CLI של gcloud. הוראות להתקנת ה-CLI של gcloud מופיעות במאמר התקנת ה-CLI של gcloud.
-
כדי לבצע אימות, משתמשים ב-CLI של Google Cloud ומריצים את הפקודה הבאה.
gcloud auth login - כדי להיכנס, לוחצים על כתובת ה-URL שמופיעה.
- אחרי הכניסה לחשבון, מעתיקים את קוד האימות שמופיע ומדביקים אותו בשורת הפקודה.
-
מריצים את הפקודה הבאה כדי לציין את הפרויקט שמכיל את המשאב שרוצים להגן עליו באמצעות IAP.
gcloud config set project PROJECT_ID -
כדי להפעיל את IAP, מריצים את הפקודה בהיקף גלובלי או אזורי.
היקף גלובלי היקף אזוריgcloud compute backend-services update BACKEND_SERVICE_NAME --global --iap=enabledgcloud compute backend-services update BACKEND_SERVICE_NAME --region REGION_NAME --iap=enabled
אחרי שמפעילים את IAP, אפשר להשתמש ב-CLI של gcloud כדי לשנות את מדיניות הגישה של IAP באמצעות תפקיד ה-IAM roles/iap.httpsResourceAccessor. מידע נוסף על ניהול תפקידים והרשאות
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.