במאמר הזה מוסבר מתי ואיך להשתמש בלקוחות OAuth בהתאמה אישית לשרת proxy לאימות זהויות (IAP).
כברירת מחדל, IAP משתמש בלקוחות OAuth שמנוהלים על ידי Google כדי לאמת משתמשים. אפשר להשתמש בלקוחות OAuth שמנוהלים על ידי Google רק כדי לנהל את הגישה של משתמשים פנימיים בתוך הארגון.
אתם צריכים להשתמש בהגדרת OAuth בהתאמה אישית כדי:
לנהל את הגישה של משתמשים חיצוניים (מחוץ לארגון) לאפליקציות שמופעל בהן IAP.
ניהול הגישה לאפליקציות אינטרנט שנמצאות בפרויקטים שלא שייכים לארגון ב-Google Cloud .
הצגת פרטי מותג בהתאמה אישית במסכי הסכמה במהלך האימות.
אתם יכולים להגדיר לקוחות OAuth בהתאמה אישית ב-IAP או ישירות בפלטפורמה שלכם.
כשמשתמשים בלקוחות OAuth בהתאמה אישית, צריך להגדיר את מסך ההסכמה של OAuth. כדי שהמיתוג המותאם אישית יופיע במסך ההסכמה, צריך לשלוח את האפליקציה לאימות על ידי Google. למידע נוסף על תהליך האימות, קראו את המאמר הגדרת מסך הסכמה ל-OAuth.
כשאתם מגדירים לקוחות OAuth בהתאמה אישית, אתם אחראים ליצור ולנהל את פרטי הכניסה, כולל אחסון מאובטח של סוד הלקוח ושיתוף שלו עם לקוחות מורשים לפי הצורך.
השוואה בין לקוחות OAuth בניהול Google לבין לקוחות OAuth בהתאמה אישית
לקוחות OAuth שמנוהלים על ידי Google לא יכולים לגשת באופן פרוגרמטי לאפליקציות שמוגנות באמצעות IAP. עם זאת, עדיין אפשר לגשת באופן פרוגרמטי לאפליקציות שמוגנות באמצעות IAP ומשתמשות בלקוח OAuth שמנוהל על ידי Google, באמצעות לקוח OAuth נפרד שמוגדר דרך ההגדרה programmatic_clients או באמצעות JWT של חשבון שירות.
בטבלה הבאה מוצגת השוואה בין לקוח OAuth בניהול Google לבין לקוח OAuth בהתאמה אישית.
| לקוח OAuth בניהול Google | לקוח OAuth בהתאמה אישית | |
|---|---|---|
| משתמשים | פנימי בלבד | פנימי וחיצוני |
| מותג | Google Cloud מותג | מותג בבעלות הלקוח |
| הגדרת OAuth | הגדרות של Google | הגדרות הלקוח |
| פרטי כניסה של OAuth | בניהול Google | בניהול הלקוח |
| גישה לאפליקציות | תהליך בדפדפן בלבד | תהליך בדפדפן וגישה פרוגרמטית |
הגדרת דף המיתוג
כדי להגדיר את דף המיתוג בהתאמה אישית באמצעות מסוף Google Cloud :
נכנסים לדף Branding של OAuth במסוף Google Cloud :
לוחצים על שנתחיל?.
בקטע App name, מזינים את שם האפליקציה למסך ההסכמה.
בשדה User support email, מזינים את כתובת האימייל של האדמין לתמיכה.
בקטע Audience, בוחרים באפשרות Internal כדי להגביל את הגישה למשתמשים בארגון, או באפשרות External כדי לאפשר גישה למשתמשים מחוץ לארגון.
בקטע Contact information, מזינים את כתובת האימייל של האדמין שאליו צריך לפנות לגבי האפליקציות שמוגנות על ידי לקוחות ה-OAuth. אתם מגדירים את לקוחות ה-OAuth בשלב מאוחר יותר.
כדי ליצור את הגדרת ה-OAuth, לוחצים על Create.
הגדרת לקוחות OAuth בהתאמה אישית ב-IAP
בקטע הזה מוסבר איך להגדיר לקוחות OAuth בהתאמה אישית ב-IAP.
אפשר ליצור לקוח OAuth בהתאמה אישית ל-IAP באמצעות מסוףGoogle Cloud בדרכים הבאות:
יצירת לקוח OAuth באופן ידני: צריך להוסיף את ה-URI להפניה אוטומטית לפני שמגדירים את IAP.
יצירת לקוח OAuth באופן אוטומטי באמצעות פרטי כניסה שנוצרו אוטומטית ב-IAP: המערכת יוצרת ומגדירה את לקוח ה-OAuth בשבילכם, ומוסיפה באופן אוטומטי את כתובת ה-URI הנדרשת להפניה אוטומטית.
לא משנה באיזו שיטה אתם משתמשים, ודאו שהגדרתם את דף המיתוג לפני שאתם יוצרים את לקוח ה-OAuth. הפורמט הנדרש של ה-URI להפניה מופיע בשתי השיטות.
יצירת לקוח OAuth בהתאמה אישית
בקטע הזה מוסבר איך ליצור לקוחות OAuth בהתאמה אישית באמצעות מסוףGoogle Cloud . אפשר להגדיר לקוחות OAuth בהתאמה אישית ל-IAP בכל רמה בהיררכיית המשאבים.
כדי ליצור לקוחות OAuth בהתאמה אישית למשאב באמצעות מסוף Google Cloud , אפשר להשתמש באחת מהשיטות הבאות:
שיטה 1: יצירת לקוח OAuth באמצעות מסוף Google Cloud
השיטה הזו מתאימה כשרוצים ליצור את לקוח ה-OAuth בנפרד, למשל לפני שמגדירים את IAP למשאבים. כדי לעשות את זה, צריך להוסיף באופן ידני את ה-URI להפניה אוטומטית של IAP.
במסוף Google Cloud , נכנסים לדף APIs & Services > Credentials.
לוחצים על Create Credentials ובוחרים באפשרות OAuth client ID.
בקטע Application type, בוחרים באפשרות אפליקציית אינטרנט.
בשדה Name, מזינים שם תיאורי ללקוח ה-OAuth.
לוחצים על יצירה. חשוב לזכור את מזהה הלקוח שנוצר.
אחרי שיוצרים את לקוח ה-OAuth, עוברים לקטע Authorized redirect URIs בדף הפרטים של לקוח ה-OAuth החדש.
לוחצים על Add URI ומזינים את Authorized redirect URI הבא, שספציפי ל-IAP:
https://iap.googleapis.com/v1/oauth/clientIds/YOUR_CLIENT_ID:handleRedirect
מחליפים את
YOUR_CLIENT_IDבמזהה הלקוח שקיבלתם בשלב הקודם.לוחצים על Save.
שיטה 2: יצירת לקוח OAuth באמצעות פרטי כניסה שנוצרו באופן אוטומטי על ידי IAP
בשיטה הזו, IAP יוצר ומגדיר בשבילכם את לקוח OAuth ומוסיף באופן אוטומטי את ה-URI הנדרש להפניה אוטומטית.
נכנסים לדף IAP במסוף Google Cloud .
בכרטיסייה Applications, ברשימת המשאבים, מוצאים את המשאב שרוצים להגדיר.
למשאבים ברמת הפרויקט:
יוצרים את לקוח ה-OAuth באמצעות Google Cloud המסוף:
בעמודה פעולות, לוחצים על אפשרויות נוספות > הגדרות.
בתיבת הדו-שיח Settings, בוחרים באפשרות Custom OAuth.
אם לא הגדרתם מסך הסכמה, בצעו את הפעולות הבאות:
לוחצים על Configure consent screen.
פועלים לפי ההוראות להגדרת דף המיתוג שמופיעות למעלה.
בתיבת הדו-שיח של הגדרות IAP, לוחצים על Auto Generate Credentials. IAP יוצר לקוח OAuth חדש וסוד לשימוש עם המשאב הזה. בפלטפורמת האימות של Google, השדה Authorized redirect URIs מכיל רשומה בפורמט הבא:
https://iap.googleapis.com/v1/oauth/clientIds/CLIENT_ID:handleRedirect
כדי לגשת למזהה הלקוח ולסוד הלקוח, לוחצים על הורדת פרטי הכניסה. פרטי הכניסה נשמרים בקובץ בפורמט JSON. הקובץ מכיל פרטי כניסה רגישים לגישה למשאבים, לכן חשוב לוודא שהוא מאובטח או למחוק אותו.
כדי לשמור את הגדרות ה-OAuth של IAP ולהחיל את לקוח ה-OAuth על IAP, לוחצים על Save.
החלת לקוחות OAuth בהתאמה אישית על IAP
בקטע הזה מוסבר איך להחיל לקוחות OAuth על IAP. אתם יכולים להשתמש בשיטה הזו במקום להחיל את הלקוחות ישירות על הפלטפורמה שלכם.
כדי ליצור את לקוח ה-OAuth בהתאמה אישית, פועלים לפי ההוראות לGoogle Cloud מסוף שמופיעות למעלה.
מחילים את לקוח ה-OAuth בהתאמה אישית.
gcloud
כדי להחיל את לקוח ה-OAuth בהתאמה אישית באמצעות ה-CLI של gcloud:
יוצרים קובץ YAML של הגדרות.
cat << EOF > iap-oauth.yaml accessSettings: oauthSettings: clientId: CLIENT_ID clientSecret: CLIENT_SECRET EOF
מחליפים את מה שכתוב בשדות הבאים:
CLIENT_ID: מזהה הלקוח מפרטי הכניסה ל-OAuth שיצרתם קודם.
CLIENT_SECRET: סוד הלקוח מפרטי הכניסה ל-OAuth שיצרתם קודם.
כדי להגדיר את OAuth, מבצעים אחת מהפעולות הבאות:
- כדי להגדיר את ה-OAuth ברמת הפרויקט, מריצים את הפקודה הבאה:
gcloud iap settings set iap-oauth.yaml
כדי להגדיר את התצורה ברמה אחרת של היררכיית המשאבים, צריך להשתמש באחד מהדגלים הבאים במקום בדגל
--project. כשמגדירים לקוחות OAuth בהתאמה אישית ברמה מסוימת בהיררכיית המשאבים, כל השירותים שפועלים באותה רמה מקבלים את אותו מיתוג בהתאמה אישית.--folder=FOLDER_ID--organization=ORGANIZATION_IDכדי להגדיר את התצורה בשירות ספציפי, מריצים את הפקודה הבאה:
gcloud iap settings set iap-oauth.yaml \ --project=PROJECT_ID \ --resource-type= RESOURCE_TYPE \ --region=REGION \ --service=SERVICE_NAME
מחליפים את מה שכתוב בשדות הבאים:
PROJECT_ID: המזהה של משאב הפרויקט. כדי להגדיר את התצורה ברמה אחרת, משתמשים באחד מהדגלים הבאים במקום בדגל--project:--folder=FOLDER_ID--organization=ORGANIZATION_ID
RESOURCE_TYPE: מחליפים באחד מסוגי המשאבים הבאים, בהתאם למשאב:app-enginebackend-servicescloud-runcomputefolderforwarding-ruleiap_weborganization
REGION: האזור שבו אתם מריצים את שירות Cloud Run.
SERVICE_NAME: שם השירות.
Terraform
כדי להחיל את לקוח ה-OAuth בהתאמה אישית באמצעות Terraform:
resource "google_iap_settings" "iap_settings" { name = IAP_RESOURCE_NAME access_settings { oauth_settings { oauth_client_id = CLIENT_ID oauth_client_secret = CLIENT_SECRET } } }מחליפים את מה שכתוב בשדות הבאים:
IAP_RESOURCE_NAME: שם המשאב של המשאבiap_settingsשל השירות, בפורמט הבא:projects/PROJECT_NUMBER/iap_web/REGION/services/SERVICE_NAME
CLIENT_ID: מזהה הלקוח מפרטי הכניסה ל-OAuth שיצרתם קודם
CLIENT_SECRET: סוד הלקוח מפרטי הכניסה ל-OAuth שיצרתם קודם
API בארכיטקטורת REST
יוצרים קובץ JSON של הגדרות.
cat << EOF > iap-oauth.json { "accessSettings": { "oauthSettings": { "clientId": "CLIENT_ID", "clientSecret": "CLIENT_SECRET" } } } EOF
מחליפים את מה שכתוב בשדות הבאים:
CLIENT_ID: מזהה הלקוח מפרטי הכניסה ל-OAuth שיצרתם קודם.
CLIENT_SECRET: סוד הלקוח מפרטי הכניסה ל-OAuth שיצרתם קודם.
מחילים את קובץ ההגדרות.
curl -X PATCH \ -H "Authorization: Bearer $(gcloud auth print-access-token)" -H "Content-Type: application/yaml" \ "https://iap.googleapis.com/v1/projects/cb-managed-ingress-demo/iap_web/forwarding_rule-us-central1/services/psc-fr:iapSettings?update_mask=iapSettings.accessSettings.oauthSettings.oauthClientId,iapSettings.accessSettings.oauthSettings.oauthClientSecret" \ -d @iap-oauth.json
כדי לבדוק שאפליקציות האינטרנט מוגנות באמצעות IAP עם לקוחות OAuth, קראו את המאמר בדיקת הגישה.
הגדרה מדור קודם של לקוחות OAuth בהתאמה אישית במשאב
בקטעים הבאים מתוארות שיטות קודמות להגדרת לקוחות OAuth בהתאמה אישית ב-IAP לסוגי משאבים מסוימים. אם השתמשתם בשיטה שמתוארת למעלה, אתם יכולים לדלג על החלק הזה.
App Engine
בקטע הזה מוסבר איך להפעיל לקוחות OAuth בהתאמה אישית ב-App Engine.
gcloud
Before you set up your project and IAP, you need an up-to-date version of 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 - Follow the instructions in Creating OAuth clients for IAP to configure the OAuth consent screen and create the OAuth client.
- Save the OAuth client ID and secret.
-
To enable IAP, run the following command.
gcloud iap web enable \ --oauth2-client-id=CLIENT_ID \ --oauth2-client-secret=CLIENT_SECRET \ --resource-type=app-engine
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
Follow the instructions in Creating OAuth clients for IAP to configure the OAuth consent screen and create the OAuth client.
Save the OAuth client ID and secret.
Run the following command to prepare a
settings.jsonfile.cat << EOF > settings.json { "iap": { "enabled": true, "oauth2ClientId": "CLIENT_ID", "oauth2ClientSecret":" CLIENT_SECRET" } } EOFRun the following command to enable IAP.
curl -X PATCH \ -H "Authorization: Bearer $(gcloud auth application-default 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"
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.
GKE
בקטע הזה מוסבר איך להפעיל לקוחות OAuth בהתאמה אישית ב-GKE.
Configure the BackendConfig
If you are running GKE clusters version 1.24 or later, you can configure IAP and GKE by using the Kubernetes Gateway API. See Configure IAP for instructions.
Follow the instructions in Creating OAuth clients for IAP to configure the OAuth consent screen and create the OAuth client.
Create a Kubernetes Secret to wrap the OAuth client.
Replace the following:kubectl create secret generic MY_SECRET --from-literal=client_id=CLIENT_ID \ --from-literal=client_secret=CLIENT_SECRET
MY_SECRET: The name of the secret to createCLIENT_ID: The OAuth client IDCLIENT_SECRET: The OAuth client secret
You should receive confirmation, like the following output, that the Secret was successfully created:
secret "MY_SECRET" created
Add the OAuth credentials to the BackendConfig.
apiVersion: cloud.google.com/v1 kind: BackendConfig metadata: name: CONFIG_DEFAULT namespace: my-namespace spec: iap: enabled: true oauthclientCredentials: secretName: MY_SECRETEnable IAP by associating Service ports with your BackendConfig. See Associating BackendConfig with your Ingress. One way to make this association is to make all ports for the service default to your BackendConfig, which you can do by adding the following annotation to your Service resource:
metadata: annotations: cloud.google.com/backend-config: '{"default": "CONFIG_DEFAULT"}'
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.
Troubleshooting
If the secretName you referenced doesn't exist or isn't structured
properly, one of the following error messages will display:
BackendConfig default/config-default is not valid: error retrieving secret "foo": secrets "foo" not found.To resolve this error, make sure that you've created the Kubernetes Secret correctly as described in step 2.BackendConfig default/config-default is not valid: secret "foo" missing client_secret data.To resolve this error, make sure that you've created the OAuth credentials correctly. Also, make sure that you referenced the correctclient_idandclient_secretkeys.
שירות לקצה העורפי של מאזן העומסים
בקטע הזה מוסבר למשתמשי Compute Engine ו-Cloud Run איך להגדיר לקוחות OAuth ב-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
- Follow the instructions in Creating OAuth clients for IAP. to configure the OAuth consent screen and create the OAuth client.
- Save the OAuth client ID and secret.
-
To enable IAP, run either the globally or regionally scoped command.
Global scope Regional scopegcloud compute backend-services update BACKEND_SERVICE_NAME \ --global \ --iap=enabled,oauth2-client-id=CLIENT_ID,oauth2-client-secret=CLIENT_SECRETgcloud compute backend-services update BACKEND_SERVICE_NAME \ --region REGION_NAME \ --iap=enabled,oauth2-client-id=CLIENT_ID,oauth2-client-secret=CLIENT_SECRET
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
Follow the instructions in Creating OAuth clients for IAP to configure the OAuth consent screen and create the OAuth client.
Save the OAuth client ID and secret.
Run the following command to prepare a
settings.jsonfile.cat << EOF > settings.json { "iap": { "enabled": true, "oauth2ClientId": "CLIENT_ID", "oauth2ClientSecret": "CLIENT_SECRET" } } EOFRun the following command to enable IAP.
curl -X PATCH \ -H "Authorization: Bearer $(gcloud auth application-default 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 gcloud CLI to modify the
IAP access policy using the IAM role
roles/iap.httpsResourceAccessor. Learn more about
managing roles and permissions.
בדיקת גישה
אחרי שמגדירים את לקוח OAuth בהתאמה אישית, אפשר לבדוק אם IAP משתמש בו כדי להגן על השירות. כדי לעשות את זה:
בדף IAP, בכרטיסייה Applications, מוצגות האפליקציות שמנוהלות על ידי IAP.
נכנסים לכתובת ה-URL של אחת מהאפליקציות. אם אתם נכנסים לאפליקציה בפעם הראשונה מאז שהגדרתם את מסך ההסכמה, יוצג מסך ההסכמה שהגדרתם קודם.