שימוש בלקוחות OAuth בהתאמה אישית עם IAP

במאמר הזה מוסבר מתי ואיך להשתמש בלקוחות 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 :

  1. נכנסים לדף Branding של OAuth במסוף Google Cloud :

    מעבר לדף Branding

  2. לוחצים על שנתחיל?.

  3. בקטע App name, מזינים את שם האפליקציה למסך ההסכמה.

  4. בשדה User support email, מזינים את כתובת האימייל של האדמין לתמיכה.

  5. בקטע Audience, בוחרים באפשרות Internal כדי להגביל את הגישה למשתמשים בארגון, או באפשרות External כדי לאפשר גישה למשתמשים מחוץ לארגון.

  6. בקטע Contact information, מזינים את כתובת האימייל של האדמין שאליו צריך לפנות לגבי האפליקציות שמוגנות על ידי לקוחות ה-OAuth. אתם מגדירים את לקוחות ה-OAuth בשלב מאוחר יותר.

  7. כדי ליצור את הגדרת ה-OAuth, לוחצים על Create.

הגדרת לקוחות OAuth בהתאמה אישית ב-IAP

בקטע הזה מוסבר איך להגדיר לקוחות OAuth בהתאמה אישית ב-IAP.

אפשר ליצור לקוח OAuth בהתאמה אישית ל-IAP באמצעות מסוףGoogle Cloud בדרכים הבאות:

לא משנה באיזו שיטה אתם משתמשים, ודאו שהגדרתם את דף המיתוג לפני שאתם יוצרים את לקוח ה-OAuth. הפורמט הנדרש של ה-URI להפניה מופיע בשתי השיטות.

יצירת לקוח OAuth בהתאמה אישית

בקטע הזה מוסבר איך ליצור לקוחות OAuth בהתאמה אישית באמצעות מסוףGoogle Cloud . אפשר להגדיר לקוחות OAuth בהתאמה אישית ל-IAP בכל רמה בהיררכיית המשאבים.

כדי ליצור לקוחות OAuth בהתאמה אישית למשאב באמצעות מסוף Google Cloud , אפשר להשתמש באחת מהשיטות הבאות:

שיטה 1: יצירת לקוח OAuth באמצעות מסוף Google Cloud

השיטה הזו מתאימה כשרוצים ליצור את לקוח ה-OAuth בנפרד, למשל לפני שמגדירים את IAP למשאבים. כדי לעשות את זה, צריך להוסיף באופן ידני את ה-URI להפניה אוטומטית של IAP.

  1. במסוף Google Cloud , נכנסים לדף APIs & Services > Credentials.

  2. לוחצים על Create Credentials ובוחרים באפשרות OAuth client ID.

  3. בקטע Application type, בוחרים באפשרות אפליקציית אינטרנט.

  4. בשדה Name, מזינים שם תיאורי ללקוח ה-OAuth.

  5. לוחצים על יצירה. חשוב לזכור את מזהה הלקוח שנוצר.

  6. אחרי שיוצרים את לקוח ה-OAuth, עוברים לקטע Authorized redirect URIs בדף הפרטים של לקוח ה-OAuth החדש.

  7. לוחצים על Add URI ומזינים את Authorized redirect URI הבא, שספציפי ל-IAP:

    https://iap.googleapis.com/v1/oauth/clientIds/YOUR_CLIENT_ID:handleRedirect

    מחליפים את YOUR_CLIENT_ID במזהה הלקוח שקיבלתם בשלב הקודם.

  8. לוחצים על Save.

שיטה 2: יצירת לקוח OAuth באמצעות פרטי כניסה שנוצרו באופן אוטומטי על ידי IAP

בשיטה הזו, IAP יוצר ומגדיר בשבילכם את לקוח OAuth ומוסיף באופן אוטומטי את ה-URI הנדרש להפניה אוטומטית.

  1. נכנסים לדף IAP במסוף Google Cloud .

    כניסה לדף IAP

  2. בכרטיסייה Applications, ברשימת המשאבים, מוצאים את המשאב שרוצים להגדיר.

    למשאבים ברמת הפרויקט:

    • יוצרים את לקוח ה-OAuth באמצעות Google Cloud המסוף:

      1. בעמודה פעולות, לוחצים על אפשרויות נוספות > הגדרות.

      2. בתיבת הדו-שיח Settings, בוחרים באפשרות Custom OAuth.

      3. אם לא הגדרתם מסך הסכמה, בצעו את הפעולות הבאות:

        1. לוחצים על Configure consent screen.

        2. פועלים לפי ההוראות להגדרת דף המיתוג שמופיעות למעלה.

      4. בתיבת הדו-שיח של הגדרות IAP, לוחצים על Auto Generate Credentials. ‫IAP יוצר לקוח OAuth חדש וסוד לשימוש עם המשאב הזה. בפלטפורמת האימות של Google, השדה Authorized redirect URIs מכיל רשומה בפורמט הבא:

        https://iap.googleapis.com/v1/oauth/clientIds/CLIENT_ID:handleRedirect
      5. כדי לגשת למזהה הלקוח ולסוד הלקוח, לוחצים על הורדת פרטי הכניסה. פרטי הכניסה נשמרים בקובץ בפורמט JSON. הקובץ מכיל פרטי כניסה רגישים לגישה למשאבים, לכן חשוב לוודא שהוא מאובטח או למחוק אותו.

      6. כדי לשמור את הגדרות ה-OAuth של IAP ולהחיל את לקוח ה-OAuth על IAP, לוחצים על Save.

החלת לקוחות OAuth בהתאמה אישית על IAP

בקטע הזה מוסבר איך להחיל לקוחות OAuth על IAP. אתם יכולים להשתמש בשיטה הזו במקום להחיל את הלקוחות ישירות על הפלטפורמה שלכם.

  1. כדי ליצור את לקוח ה-OAuth בהתאמה אישית, פועלים לפי ההוראות לGoogle Cloud מסוף שמופיעות למעלה.

  2. מחילים את לקוח ה-OAuth בהתאמה אישית.

    gcloud

    כדי להחיל את לקוח ה-OAuth בהתאמה אישית באמצעות ה-CLI של gcloud:

    1. יוצרים קובץ YAML של הגדרות.

      cat << EOF > iap-oauth.yaml
      accessSettings:
      oauthSettings:
        clientId: CLIENT_ID
        clientSecret: CLIENT_SECRET
      EOF
      

      מחליפים את מה שכתוב בשדות הבאים:

      • ‫CLIENT_ID: מזהה הלקוח מפרטי הכניסה ל-OAuth שיצרתם קודם.

      • ‫CLIENT_SECRET: סוד הלקוח מפרטי הכניסה ל-OAuth שיצרתם קודם.

    2. כדי להגדיר את 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-engine

        • backend-services

        • cloud-run

        • compute

        • folder

        • forwarding-rule

        • iap_web

        • organization

      • ‫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

    1. יוצרים קובץ JSON של הגדרות.

      cat << EOF > iap-oauth.json
      {
      "accessSettings": {
        "oauthSettings": {
          "clientId": "CLIENT_ID",
          "clientSecret": "CLIENT_SECRET"
        }
      }
      }
      EOF

      מחליפים את מה שכתוב בשדות הבאים:

      • ‫CLIENT_ID: מזהה הלקוח מפרטי הכניסה ל-OAuth שיצרתם קודם.

      • ‫CLIENT_SECRET: סוד הלקוח מפרטי הכניסה ל-OAuth שיצרתם קודם.

    2. מחילים את קובץ ההגדרות.

      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.

  1. To authenticate, use the Google Cloud CLI and run the following command.
    gcloud auth login
  2. To sign in, follow the URL that appears.
  3. After you sign in, copy the verification code that appears and paste it in the command line.
  4. 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
  5. Follow the instructions in Creating OAuth clients for IAP to configure the OAuth consent screen and create the OAuth client.
  6. Save the OAuth client ID and secret.
  7. 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

  1. Follow the instructions in Creating OAuth clients for IAP to configure the OAuth consent screen and create the OAuth client.

  2. Save the OAuth client ID and secret.

  3. Run the following command to prepare a settings.json file.

    cat << EOF > settings.json
    {
    "iap":
      {
        "enabled": true,
        "oauth2ClientId": "CLIENT_ID",
        "oauth2ClientSecret":" CLIENT_SECRET"
      }
    }
    EOF
    

  4. Run 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.

  1. Follow the instructions in Creating OAuth clients for IAP to configure the OAuth consent screen and create the OAuth client.

  2. Create a Kubernetes Secret to wrap the OAuth client.

    kubectl create secret generic MY_SECRET --from-literal=client_id=CLIENT_ID \
      --from-literal=client_secret=CLIENT_SECRET
    
    Replace the following:

    • MY_SECRET: The name of the secret to create

    • CLIENT_ID: The OAuth client ID

    • CLIENT_SECRET: The OAuth client secret

    You should receive confirmation, like the following output, that the Secret was successfully created:

    secret "MY_SECRET" created
    

  3. 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_SECRET
    

  4. Enable 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 correct client_id and client_secret keys.

שירות לקצה העורפי של מאזן העומסים

בקטע הזה מוסבר למשתמשי 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.

  1. To authenticate, use the Google Cloud CLI and run the following command.
    gcloud auth login
  2. To sign in, follow the URL that appears.
  3. After you sign in, copy the verification code that appears and paste it in the command line.
  4. 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
  5. Follow the instructions in Creating OAuth clients for IAP. to configure the OAuth consent screen and create the OAuth client.
  6. Save the OAuth client ID and secret.
  7. To enable IAP, run either the globally or regionally scoped command.

    Global scope
    gcloud compute backend-services update BACKEND_SERVICE_NAME \
        --global \
        --iap=enabled,oauth2-client-id=CLIENT_ID,oauth2-client-secret=CLIENT_SECRET
    Regional scope
    gcloud 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

  1. Follow the instructions in Creating OAuth clients for IAP to configure the OAuth consent screen and create the OAuth client.

  2. Save the OAuth client ID and secret.

  3. Run the following command to prepare a settings.json file.

    cat << EOF > settings.json
    {
    "iap":
      {
        "enabled": true,
        "oauth2ClientId": "CLIENT_ID",
        "oauth2ClientSecret": "CLIENT_SECRET"
      }
    }
    EOF
    

  4. Run 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 משתמש בו כדי להגן על השירות. כדי לעשות את זה:

  1. בדף IAP, בכרטיסייה Applications, מוצגות האפליקציות שמנוהלות על ידי IAP.

  2. נכנסים לכתובת ה-URL של אחת מהאפליקציות. אם אתם נכנסים לאפליקציה בפעם הראשונה מאז שהגדרתם את מסך ההסכמה, יוצג מסך ההסכמה שהגדרתם קודם.