透過 Google 帳戶驗證使用者
本快速入門導覽課程說明如何啟用 Identity-Aware Proxy (IAP),保護 App Engine 標準或彈性環境應用程式,並授予使用者 Google 帳戶存取權。
如果您預計從內容傳遞聯播網 (CDN) 提供資源,請參閱快取最佳做法指南取得重要資訊。
如果 App Engine 應用程式包含多項服務,您可以為不同服務設定不同的 IAP 權限,包括只允許部分服務公開存取,同時保護其他服務。
如要直接在 Google Cloud 控制台中,按照這項工作的逐步指南操作,請按一下「Guide me」(逐步引導):
事前準備
- 登入 Google Cloud 帳戶。如果您是 Google Cloud新手,歡迎 建立帳戶,親自評估產品在實際工作環境中的成效。新客戶還能獲得價值 $300 美元的免費抵免額,可用於執行、測試及部署工作負載。
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.
-
Verify that billing is enabled for your Google Cloud project.
-
In the Google Cloud console, on the project selector page, select or create a Google Cloud project.
Roles required to select or create a project
- Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
-
Create a project: To create a project, you need the Project Creator role
(
roles/resourcemanager.projectCreator), which contains theresourcemanager.projects.createpermission. Learn how to grant roles.
-
If you're using an existing project for this guide, verify that you have the permissions required to complete this guide. If you created a new project, then you already have the required permissions.
-
Verify that billing is enabled for your Google Cloud project.
-
如要為 App Engine 啟用 IAP,請設定 App Engine 執行個體。如果您尚未設定 App Engine 執行個體,請參閱部署至 App Engine 一文,取得完整逐步操作說明。
根據預設,IAP 會使用 Google 代管的 OAuth 用戶端,這需要專案屬於某個機構,且只允許內部使用者存取。
必要的角色
如要取得透過 Google 帳戶驗證使用者所需的權限,請要求管理員授予您專案的「IAP 政策管理員」 (roles/iap.policyAdmin) IAM 角色。如要進一步瞭解如何授予角色,請參閱「管理專案、資料夾和組織的存取權」。
啟用 IAP
控制台
如果專案不屬於任何機構,您必須使用自訂 OAuth 憑證。
If you haven't configured your project's OAuth consent screen, you'll be prompted to do so. To configure your OAuth consent screen, see Setting up your OAuth consent screen.
Setting up IAP access
-
Go to the
Identity-Aware Proxy page.
Go to the Identity-Aware Proxy page - Select the project you want to secure with IAP.
- Select the checkbox next to the resource you want to grant access to.
- On the right side panel, click Add principal.
-
In the Add principals dialog that appears, enter the email addresses of groups or
individuals who should have the IAP-secured Web App User role for the project.
The following kinds of principals can have this role:
- Google Account: user@gmail.com
- Google Group: admins@googlegroups.com
- Service account: server@example.gserviceaccount.com
- Google Workspace domain: example.com
Make sure to add a Google Account that you have access to.
- Select Cloud IAP > IAP-secured Web App User from the Roles drop-down list.
- Click Save.
Turning on IAP
-
On the Identity-Aware Proxy page, under Applications,
find the application you want to restrict
access to. To turn on IAP for a resource,
-
In the Turn on IAP window that appears, click Turn On to
confirm that you want IAP to secure your resource. After you turn on
IAP, it requires login credentials for all connections to your load balancer.
Only principals with the IAP-Secured Web App User (
roles/iap.httpsResourceAccessor) role on the project will be given access.
gcloud
您需要最新版的 gcloud CLI,才能設定專案與 IAP。如需如何安裝 gcloud CLI 的操作說明,請參閱「安裝 gcloud CLI」。
-
如要進行驗證,請使用 Google Cloud CLI 執行下列指令。
gcloud auth login - 按一下顯示的網址並登入。
- 登入之後,複製顯示的驗證碼並貼到指令列中。
-
執行下列指令,指定要使用 IAP 保護的應用程式所屬專案。
gcloud config set project PROJECT_ID -
如要啟用 IAP,請執行下列指令。
gcloud iap web enable --resource-type=app-engine --versions=version -
將應擁有專案「受 IAP 保護的網頁應用程式使用者」角色的主體新增至專案。
gcloud projects add-iam-policy-binding PROJECT_ID \ --member=PRINCIPAL_IDENTIFIER \ --role=roles/iap.httpsResourceAccessor- 將 PROJECT_ID 替換為專案 ID。
- 將 PRINCIPAL_IDENTIFIER 替換為必要主體。可以是網域、群組、服務帳戶或使用者類型。例如:
user:myemail@example.com。
啟用 IAP 之後,您可以使用 gcloud CLI,以 IAM 角色 roles/iap.httpsResourceAccessor 修改 IAP 存取權政策。進一步瞭解如何管理角色與權限。
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://appengine.googleapis.com/v1/apps/PROJECT_ID?updateMask=iap"
啟用 IAP 後,您可以使用 Google Cloud CLI,以 IAM 角色 roles/iap.httpsResourceAccessor 修改 IAP 存取權政策。進一步瞭解如何管理角色與權限。
您可以在專案中啟用 App Engine 服務的 IAP。如要公開存取部分服務,並要求其他服務進行驗證,您可以在服務之間使用不同的存取層級。詳情請參閱「使用 IAP 管理存取權」。如要公開存取服務,請將「受 IAP 保護的網頁應用程式使用者」角色授予 allAuthenticatedUsers。
測試使用者驗證
授予角色後,主體即可透過 IAP 驗證存取應用程式。使用已獲授「受 IAP 保護的網頁應用程式使用者」
roles/iap.httpsResourceAccessor角色的 Google 帳戶存取應用程式網址。如要測試存取權,請在 Chrome 中開啟無痕視窗,存取應用程式網址,並在系統提示時登入。獲得 「IAP-secured Web App User (
roles/iap.httpsResourceAccessor)」 角色的使用者,只要通過驗證即可存取應用程式。如果使用者 未獲得必要角色或驗證失敗,就無法存取 應用程式。
後續步驟
- 瞭解如何取得使用者身分識別資訊,並開發自己的 App Engine 應用程式。