Abilitazione di IAP per GKE

Questa pagina spiega come proteggere un'istanza Google Kubernetes Engine (GKE) con Identity-Aware Proxy (IAP).

Panoramica

IAP è integrato tramite Ingress per GKE. Questa integrazione ti consente di controllare l'accesso a livello di risorsa per i dipendenti anziché utilizzare una VPN.

In un cluster GKE, il traffico in entrata viene gestito dal bilanciamento del carico HTTP(S), un componente di Cloud Load Balancing. Il bilanciatore del carico HTTP(S) viene in genere configurato dal controller Kubernetes Ingress. Il controller Ingress riceve le informazioni di configurazione da un oggetto Ingress Kubernetes associato a uno o più oggetti Service. Ogni oggetto Service contiene informazioni di routing utilizzate per indirizzare una richiesta in arrivo a un determinato pod e a una determinata porta.

A partire dalla versione 1.10.5-gke.3 di Kubernetes, puoi aggiungere la configurazione per il bilanciatore del carico associando un servizio a un oggetto BackendConfig. BackendConfig è una definizione di risorsa personalizzata (CRD) definita nel repository kubernetes/ingress-gce.

Il controller Kubernetes Ingress legge le informazioni di configurazione da BackendConfig e configura il bilanciatore del carico di conseguenza. Un elemento BackendConfig contiene informazioni di configurazione specifiche per Cloud Load Balancing e ti consente di definire una configurazione separata per ogni servizio di backend del bilanciamento del carico HTTP(S).

Prima di iniziare

Per abilitare IAP per GKE, devi disporre di quanto segue:

  • Un Google Cloud progetto della console con la fatturazione abilitata.
  • Un gruppo di una o più istanze GKE, gestito da un bilanciatore del carico HTTPS. Il bilanciatore del carico deve essere creato automaticamente quando crei un oggetto Ingress in un cluster GKE.
  • Un nome di dominio registrato all'indirizzo del bilanciatore del carico.
  • Il codice dell'app per verificare che tutte le richieste abbiano un'identità.

IAP utilizza un client OAuth gestito da Google per autenticare gli utenti. Solo gli utenti all'interno dell'organizzazione possono accedere all'applicazione con funzionalità IAP. Se vuoi consentire l'accesso a utenti esterni all'organizzazione, consulta Attivare l'IAP per le applicazioni esterne.

Attivazione di IAP

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.

Configuring BackendConfig

You can configure a BackendConfig for IAP by adding an iap block.

Adding an iap block to the BackendConfig

To configure the BackendConfig for IAP, you need to specify the enabled value. Ensure that you have the compute.backendServices.update permission and add the iap block to BackendConfig.

For GKE versions 1.16.8-gke.3 and higher, use the cloud.google.com/v1 API version. If you are using an earlier GKE version, use cloud.google.com/v1beta1.

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

Associate a service port with a BackendConfig

You also need to associate Service ports with your BackendConfig to trigger turning on IAP. 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:
    beta.cloud.google.com/backend-config: '{"default": "CONFIG_DEFAULT"}'

Verify the BackendConfig

To test the configuration, run kubectl get event. If you see the message "no BackendConfig for service port exists", then you successfully associated a service port with your BackendConfig, but the BackendConfig resource wasn't found. This error can occur if you haven't created the BackendConfig resource, created it in the wrong namespace, or misspelled the reference in the Service annotation.

Turning IAP off

To turn IAP off, you must set enabled to false in the BackendConfig. If you delete the IAP block from BackendConfig, the settings will persist. For example, if IAP is enabled and you delete the block then IAP will still be turned on.

Passaggi successivi