Autenticare gli utenti con gli Account Google
Questa pagina ti guida nel deployment di un'applicazione dell'ambiente standard o flessibile di App Engine e nella sua protezione con Identity-Aware Proxy (IAP). La guida rapida include un codice campione per un'app web dell'ambiente standard di App Engine che verifica il nome di un utente che ha eseguito l'accesso. Questa guida rapida utilizza Cloud Shell per clonare ed eseguire il deployment dell'applicazione di esempio. Puoi utilizzare questa guida rapida per attivare IAP per la tua app dell'ambiente standard App Engine o dell'ambiente flessibile App Engine.
Se prevedi di pubblicare risorse da una rete CDN (Content Delivery Network), consulta la guida alle best practice per informazioni importanti.
Quando un'applicazione App Engine è composta da più servizi, è possibile configurare diverse autorizzazioni IAP sui diversi servizi, ad esempio rendendo solo alcuni dei servizi accessibili pubblicamente mantenendo gli altri protetti.
Per seguire le indicazioni dettagliate per questa attività direttamente nella Google Cloud console, fai clic su Procedura guidata:
Prima di iniziare
- Sign in to your Google Cloud account. If you're new to Google Cloud, create an account to evaluate how our products perform in real-world scenarios. New customers also get $300 in free credits to run, test, and deploy workloads.
-
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
(
roles/resourcemanager.projectCreator
), which contains theresourcemanager.projects.create
permission. Learn how to grant roles.
-
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
(
roles/resourcemanager.projectCreator
), which contains theresourcemanager.projects.create
permission. Learn how to grant roles.
-
Verify that billing is enabled for your Google Cloud project.
Prima di iniziare
Per abilitare IAP per App Engine, devi disporre di:
- Un progetto della console Google Cloud con la fatturazione abilitata.
Se non hai ancora configurato l'istanza App Engine, consulta Deployment di App Engine per una procedura dettagliata completa.
IAP utilizza un client OAuth gestito da Google per autenticare gli utenti. Solo gli utenti all'interno dell'organizzazione possono accedere all'applicazione abilitata per IAP-app. Se vuoi consentire l'accesso a utenti esterni all'organizzazione, consulta Attivare IAP per applicazioni esterne.
Attivazione di IAP
Console
Il client OAuth gestito da Google non è disponibile quando abiliti IAP utilizzando la console Google Cloud .
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 accounts with the IAP-Secured Web App User role on the project will be given access.
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
- Click the URL that appears and sign in.
- 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 applications that you want to protect with IAP.
gcloud config set project PROJECT_ID
-
To enable IAP, run the following command.
gcloud iap web enable --resource-type=app-engine --versions=version
-
Add principals who should have the IAP-secured Web App user role to the project.
gcloud projects add-iam-policy-binding PROJECT_ID \ --member=PRINCIPAL_IDENTIFIER \ --role=roles/iap.httpsResourceAccessor
- Replace PROJECT_ID with your project ID.
- Replace PRINCIPAL_IDENTIFIER with the necessary principals. This can be a
type of domain, group, serviceAccount, or user. For example,
user:myemail@example.com
.
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
Run the following command to prepare a
settings.json
file.cat << EOF > settings.json { "iap": { "enabled":true } } EOF
Run 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://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.
Testare l'autenticazione utente
Accedi all'URL dell'app da un Account Google che hai aggiunto a IAP con il ruolo Utente applicazione web con protezione IAP come descritto sopra. Dovresti avere accesso senza limitazioni all'app.
Utilizza una finestra in modalità incognito su Chrome per raggiungere l'app e accedere quando richiesto. Se provi ad accedere all'app con un account non autorizzato con il ruolo Utente app web protetta da IAP, verrà visualizzato un messaggio che ti comunica che non hai accesso.
Passaggi successivi
- Scopri di più su Recupero dell'identità dell'utente e sviluppa la tua app App Engine.