Personalizzare una configurazione OAuth per abilitare l'IAP

Questo documento descrive quando e come personalizzare una configurazione OAuth per Identity-Aware Proxy (IAP).

IAP utilizza un client OAuth gestito da Google per autenticare gli utenti.

Il client OAuth gestito da Google limita l'accesso agli utenti all'interno della stessa organizzazione quando accedono ad applicazioni con abilitazione IAP tramite un browser.

Quando utilizzare una configurazione OAuth personalizzata

Devi utilizzare una configurazione OAuth personalizzata per:

  • Per consentire l'accesso alle applicazioni con abilitazione IAP per gli utenti esterni all'organizzazione.
  • Per visualizzare le informazioni del tuo brand durante l'autenticazione.
  • Per attivare l'accesso programmatico alla tua applicazione.

Quando personalizzi la configurazione OAuth, devi configurare la schermata per il consenso OAuth. Ciò richiede che le informazioni sul branding della tua applicazione vengano sottoposte alla procedura di verifica di Google. Per ulteriori informazioni sulla procedura di verifica, consulta Configurare la schermata di consenso OAuth.

Sei responsabile della creazione e della gestione delle credenziali per un client OAuth personalizzato. Ciò include la memorizzazione sicura del client secret e la sua condivisione con gli utenti autorizzati, se necessario.

Confronto tra client OAuth gestito da Google e client OAuth personalizzato

I client OAuth gestiti da Google non possono accedere in modo programmatico alle applicazioni protette da IAP. Tuttavia, è ancora possibile accedere in modo programmatico alle applicazioni protette da IAP che utilizzano il client OAuth gestito da Google utilizzando un client OAuth separato configurato tramite l'impostazione programmatic_clients o un token JWT dell'account di servizio.

La tabella seguente mostra un confronto tra il client OAuth gestito da Google e un client OAuth personalizzato.

Client OAuth gestito da Google Client OAuth personalizzato
Utenti Solo interno Interni ed esterni
Brand Google Cloud brand Brand di proprietà del cliente
Configurazione di OAuth Configurato da Google Configurato dal cliente
Credenziali OAuth Gestita da Google Gestita dal cliente
Accesso alle applicazioni Solo flusso del browser Flusso del browser e accesso programmatico
.

Attivare gli acquisti in-app utilizzando una configurazione client OAuth personalizzata

Le seguenti sezioni spiegano come attivare IAP utilizzando una configurazione del client OAuth personalizzata per risorse diverse.

App Engine

Console

File di inclusione dinamico

Se non hai configurato la schermata per il consenso OAuth del progetto, ti verrà chiesto di farlo. Per configurare la schermata per il consenso OAuth, consulta Configurare la schermata per il consenso OAuth.

Configurare l'accesso IAP

  1. Vai alla pagina Identity-Aware Proxy.
    Vai alla pagina di Identity-Aware Proxy
  2. Seleziona il progetto che vuoi proteggere con IAP.
  3. Seleziona la casella di controllo accanto alla risorsa a cui vuoi concedere l'accesso.
  4. Nel riquadro laterale a destra, fai clic su Aggiungi entità.
  5. Nella finestra di dialogo Aggiungi entità che viene visualizzata, inserisci gli indirizzi email dei gruppi o delle persone che devono avere il ruolo Utente applicazione web con protezione IAP per il progetto.

    I seguenti tipi di entità possono avere questo ruolo:

    • Account Google: user@gmail.com
    • Gruppo Google: admins@googlegroups.com
    • Account di servizio: server@example.gserviceaccount.com
    • Dominio Google Workspace: example.com

    Assicurati di aggiungere un Account Google a cui hai accesso.

  6. Seleziona Cloud IAP > Utente applicazione web con protezione IAP dall'elenco a discesa Ruoli.
  7. Fai clic su Salva.

Attivazione di IAP

  1. Nella pagina Identity-Aware Proxy, in APPLICAZIONI, trova l'applicazione a cui vuoi limitare l'accesso. Per attivare IAP per una risorsa,
  2. Nella finestra Attiva IAP visualizzata, fai clic su Attiva per confermare che vuoi che la risorsa venga protetta da IAP. Dopo aver attivato IAP, sono necessarie le credenziali di accesso per tutte le connessioni al bilanciatore del carico. Solo agli account con il ruolo Utente applicazione web con protezione IAP nel progetto verrà fornito l'accesso.

gcloud

Prima di configurare il progetto e gli acquisti in-app, devi disporre di una versione aggiornata di gcloud CLI. Per istruzioni su come installare gcloud CLI, consulta Installare gcloud CLI.

  1. Per l'autenticazione, utilizza Google Cloud CLI ed esegui il comando seguente.
    gcloud auth login
  2. Per accedere, segui l'URL visualizzato.
  3. Dopo aver eseguito l'accesso, copia il codice di verifica visualizzato e incollalo nella riga di comando.
  4. Esegui il seguente comando per specificare il progetto contenente la risorsa che vuoi proteggere con IAP.
    gcloud config set project PROJECT_ID
  5. Segui le istruzioni riportate in Creare client OAuth per gli acquisti in-app per configurare la schermata di consenso OAuth e creare il client OAuth.
  6. Salva l'ID client OAuth e il client secret.
  7. Per abilitare gli acquisti in-app, esegui il seguente comando.
    gcloud iap web enable \
        --oauth2-client-id=CLIENT_ID \
        --oauth2-client-secret=CLIENT_SECRET \
        --resource-type=app-engine

Dopo aver attivato l'IAP, puoi utilizzare l'interfaccia a riga di comando gcloud per modificare il criterio di accesso IAP utilizzando il ruolo IAMroles/iap.httpsResourceAccessor. Scopri di più sulla gestione di ruoli e autorizzazioni.

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.

Compute Engine

Console

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.

If you are running GKE clusters version 1.24 or later, you can configure IAP and GKE by using the Kubernetes Gateway API. To do so, complete the following steps and then follow the instructions in Configure IAP. Do not configure BackendConfig.

Setting up IAP access

  1. Go to the Identity-Aware Proxy page.
    Go to the Identity-Aware Proxy page
  2. Select the project you want to secure with IAP.
  3. Select the checkbox next to the resource you want to grant access to.

    If you don't see a resource, ensure that the resource is created and that the BackendConfig Compute Engine ingress controller is synced.

    To verify that the backend service is available, run the following gcloud command:

    gcloud compute backend-services list
  4. On the right side panel, click Add principal.
  5. 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.

  6. Select Cloud IAP > IAP-secured Web App User from the Roles drop-down list.
  7. Click Save.

Turning on IAP

  1. On the Identity-Aware Proxy page, under APPLICATIONS, find the load balancer that serves the instance group you want to restrict access to. To turn on IAP for a resource,
    To enable IAP:
    • At least one protocol in the load balancer frontend configuration must be HTTPS. Learn about setting up a load balancer.
    • You need the compute.backendServices.update, clientauthconfig.clients.create, and clientauthconfig.clients.getWithSecret permissions. These permissions are granted by roles, such as the Project Editor role. To learn more, see Managing access to IAP-secured resources.
  2. 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 accounts with the IAP-Secured Web App User role on the project will be given access.

gcloud

Prima di configurare il progetto e l'IAP, devi disporre di una versione aggiornata del riga di comando gcloud. Per istruzioni su come installare gcloud CLI, consulta Installare gcloud CLI.

  1. Per l'autenticazione, utilizza Google Cloud CLI ed esegui il comando seguente.
    gcloud auth login
  2. Per accedere, segui l'URL visualizzato.
  3. Dopo aver eseguito l'accesso, copia il codice di verifica visualizzato e incollalo nella riga di comando.
  4. Esegui il seguente comando per specificare il progetto contenente la risorsa che vuoi proteggere con IAP.
    gcloud config set project PROJECT_ID
  5. Segui le istruzioni riportate in Creare client OAuth per gli acquisti in-app per configurare la schermata di consenso OAuth e creare il client OAuth.
  6. Salva l'ID client OAuth e il client secret.
  7. Per attivare gli acquisti in-app, esegui il comando a livello globale o regionale.

    Ambito globale
    gcloud compute backend-services update BACKEND_SERVICE_NAME \
        --global \
        --iap=enabled,oauth2-client-id=CLIENT_ID,oauth2-client-secret=CLIENT_SECRET
    Ambito regionale
    gcloud compute backend-services update BACKEND_SERVICE_NAME \
        --region REGION_NAME \
        --iap=enabled,oauth2-client-id=CLIENT_ID,oauth2-client-secret=CLIENT_SECRET
        

Dopo aver attivato l'IAP, puoi utilizzare l'interfaccia a riga di comando gcloud per modificare il criterio di accesso IAP utilizzando il ruolo IAMroles/iap.httpsResourceAccessor. Scopri di più sulla gestione di ruoli e autorizzazioni.

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.

Cloud Run

Console

If you haven't configured your project's OAuth consent screen, you're prompted to do so. To configure your OAuth consent screen, see Setting up your OAuth consent screen

Setting up IAP access

  1. Open the Identity-Aware Proxy page.
    Go to Identity-Aware Proxy
  2. Select the project you want to secure with IAP.
  3. Under Applications, select the checkbox next to the load balancer backend service to which you want to add members.
  4. On the right side panel, click Add member.
  5. In the Add members dialog, enter the accounts of groups or individuals who should have the IAP-secured Web App User role for the project. The following kinds of accounts can be members:

    • Google Account: user@gmail.com - This can also be a Google Workspace account, such as user@google.com or some other Google Workspace domain.
    • Google Group: admins@googlegroups.com
    • Service account: server@example.gserviceaccount.com
    • Google Workspace domain: example.com
  6. Select Cloud IAP > IAP-secured Web App User from the Roles list.

  7. Click Save.

Turning on IAP

  1. On the IAP page, under Applications, find the load balancer backend service to which you want to restrict access. Click the IAP toggle to enable IAP on a resource.
  2. 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 accounts with the IAP-Secured Web App User role on the project will be given access.
  3. To authorize IAP to send traffic to the backend Cloud Run service, follow the instructions at Add principals to a service to add the following principle and role.

    • Principal: service-[PROJECT-NUMBER]@gcp-sa-iap.iam.gserviceaccount.com
    • Role: Cloud Run Invoker

gcloud

  1. Segui le istruzioni riportate in Creare client OAuth per gli acquisti in-app per configurare la schermata di consenso OAuth e creare il client OAuth.
  2. Salva l'ID client OAuth e il client secret.
  3. Se non l'hai ancora fatto, crea un account di servizio eseguendo il seguente comando. Se hai già creato un account di servizio, l'esecuzione del comando non crea account di servizio duplicati.
    gcloud beta services identity create \
        --service=iap.googleapis.com --project=PROJECT_ID
    
  4. Concedi l'autorizzazione invoker all'account di servizio creato nel passaggio precedente eseguendo il seguente comando.
    gcloud run services add-iam-policy-binding SERVICE-NAME \
        --member='serviceAccount:service-PROJECT-NUMBER@gcp-sa-iap.iam.gserviceaccount.com' \
        --role='roles/run.invoker'
    
  5. Abilita IAP eseguendo il comando a livello globale o regionale, a seconda che il servizio di backend del bilanciatore del carico sia globale o regionale. Utilizza l'ID client e il secret OAuth del passaggio precedente.

    Ambito globale

    gcloud compute backend-services update BACKEND_SERVICE_NAME \
        --global \
        --iap=enabled,oauth2-client-id=CLIENT_ID,oauth2-client-secret=CLIENT_SECRET
    

    Ambito regionale

    gcloud compute backend-services update BACKEND_SERVICE_NAME \
        --region REGION_NAME \
        --iap=enabled,oauth2-client-id=CLIENT_ID,oauth2-client-secret=CLIENT_SECRET
    
    Sostituisci quanto segue:

    • BACKEND_SERVICE_NAME: il nome del servizio di backend.
    • CLIENT_ID: l'ID client OAuth del passaggio precedente.
    • CLIENT_SECRET: il client secret OAuth del passaggio precedente.
    • REGION_NAME: la regione in cui vuoi attivare gli acquisti in-app.

Dopo aver attivato l'IAP, puoi utilizzare la CLI Google Cloud per modificare il criterio di accesso IAP utilizzando il ruolo Identity and Access Management roles/iap.httpsResourceAccessor. Per ulteriori informazioni, consulta Gestire i ruoli e le autorizzazioni.

Google Kubernetes Engine

Console

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.

If you are running GKE clusters version 1.24 or later, you can configure IAP and GKE by using the Kubernetes Gateway API. To do so, complete the following steps and then follow the instructions in Configure IAP. Do not configure BackendConfig.

Setting up IAP access

  1. Go to the Identity-Aware Proxy page.
    Go to the Identity-Aware Proxy page
  2. Select the project you want to secure with IAP.
  3. Select the checkbox next to the resource you want to grant access to.

    If you don't see a resource, ensure that the resource is created and that the BackendConfig Compute Engine ingress controller is synced.

    To verify that the backend service is available, run the following gcloud command:

    gcloud compute backend-services list
  4. On the right side panel, click Add principal.
  5. 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.

  6. Select Cloud IAP > IAP-secured Web App User from the Roles drop-down list.
  7. Click Save.

Turning on IAP

  1. On the Identity-Aware Proxy page, under APPLICATIONS, find the load balancer that serves the instance group you want to restrict access to. To turn on IAP for a resource,
    To enable IAP:
    • At least one protocol in the load balancer frontend configuration must be HTTPS. Learn about setting up a load balancer.
    • You need the compute.backendServices.update, clientauthconfig.clients.create, and clientauthconfig.clients.getWithSecret permissions. These permissions are granted by roles, such as the Project Editor role. To learn more, see Managing access to IAP-secured resources.
  2. 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 accounts with the IAP-Secured Web App User role on the project will be given access.

GKE

Configura BackendConfig

Se esegui cluster GKE 1.24 o versioni successive, puoi configurare IAP e GKE utilizzando l'API Kubernetes Gateway. Per le istruzioni, vedi Configurare gli acquisti in-app.

  1. Segui le istruzioni riportate in Creare client OAuth per gli acquisti in-app per configurare la schermata di consenso OAuth e creare il client OAuth.

  2. Crea un secret Kubernetes per eseguire il wrapping del client OAuth.

    kubectl create secret generic MY_SECRET --from-literal=client_id=CLIENT_ID \
      --from-literal=client_secret=CLIENT_SECRET
    
    Sostituisci quanto segue:

    • MY_SECRET: il nome del secret da creare
    • CLIENT_ID: l'ID client OAuth
    • CLIENT_SECRET: il client secret OAuth

    Dovresti ricevere una conferma, come l'output seguente, che indica che il segreto è stato creato correttamente:

    secret "MY_SECRET" created
    

  3. Aggiungi le credenziali OAuth a BackendConfig.

    apiVersion: cloud.google.com/v1
    kind: BackendConfig
    metadata:
      name: CONFIG_DEFAULT
      namespace: my-namespace
    spec:
    iap:
      enabled: true
      oauthclientCredentials:
        secretName: MY_SECRET
    

  4. Abilita l'IAP associando le porte di servizio a BackendConfig. Consulta Associare BackendConfig a Ingress. Un modo per effettuare questa associazione è impostare tutte le porte per il servizio come predefinite per BackendConfig, aggiungendo la seguente annotazione alla risorsa di servizio:

    metadata:
      annotations:
          beta.cloud.google.com/backend-config: '{"default": "CONFIG_DEFAULT"}}'
    

Dopo aver attivato l'IAP, puoi utilizzare l'interfaccia alla gcloud CLI per modificare il criterio di accesso IAP utilizzando il ruolo IAMroles/iap.httpsResourceAccessor. Scopri di più sulla gestione di ruoli e autorizzazioni.

Risoluzione dei problemi

Se il valore secretName a cui hai fatto riferimento non esiste o non è strutturato correttamente, viene visualizzato uno dei seguenti messaggi di errore:

  • BackendConfig default/config-default is not valid: error retrieving secret "foo": secrets "foo" not found. Per risolvere questo errore, assicurati di aver creato correttamente il secret di Kubernetes, come descritto nel passaggio 2.

  • BackendConfig default/config-default is not valid: secret "foo" missing client_secret data. Per risolvere questo errore, assicurati di aver creato correttamente le credenziali OAuth. Inoltre, assicurati di aver fatto riferimento alle chiavi client_id e client_secret corrette.