Habilita IAP para GKE

Identity-Aware Proxy (IAP) te permite controlar el acceso a nivel de recurso a las cargas de trabajo de Google Kubernetes Engine (GKE) sin una VPN. Puedes otorgar roles de Identity and Access Management (IAM), configurar y verificar un BackendConfig para los servicios respaldados por Ingress, y activar o desactivar IAP.

Descripción general

IAP está integrado a través de Ingress para GKE. Esta integración te permite controlar el acceso a nivel de los recursos para los empleados en lugar de usar una VPN.

En un clúster de GKE, el tráfico entrante se controla con el balanceo de cargas de HTTP(S), un componente de Cloud Load Balancing. Por lo general, el balanceador de cargas de HTTP(S) se configura con el controlador de Ingress de Kubernetes. El controlador de Ingress obtiene información de configuración de un objeto Ingress de Kubernetes que está asociado con uno o más objetos Service. Cada objeto de servicio contiene información de enrutamiento que se usa para direccionar una solicitud entrante a un pod o puerto específico.

A partir de la versión Kubernetes 1.10.5-gke.3, tienes la posibilidad de agregar configuraciones al balanceador de cargas mediante la asociación de un servicio con un objeto de BackendConfig. BackendConfig es una definición de recurso personalizado que se define en el repositorio kubernetes/ingress-gce.

El controlador de Ingress de Kubernetes lee la información de configuración del BackendConfig y configura el balanceador de cargas según corresponda. Un BackendConfig contiene información de configuración específica de Cloud Load Balancing y te permite definir una configuración independiente para cada servicio de backend del balanceo de cargas de HTTP(S).

Antes de comenzar

Con el fin de habilitar IAP para GKE, necesitas lo siguiente:

  • Un proyecto de la consola de Google Cloud con la facturación habilitada
  • Un grupo de una o más instancias de GKE entregadas mediante un balanceador de cargas de HTTPS. El balanceador de cargas debería crearse de forma automática cuando generas un objeto Ingress en un clúster de GKE

  • Un nombre de dominio registrado con la dirección de tu balanceador de cargas

  • El código de la app para verificar que todas las solicitudes tengan una identidad (aprende a obtener la identidad del usuario)

IAP usa un cliente de OAuth administrado por Google para autenticar a los usuarios. Solo los usuarios de la organización pueden acceder a la aplicación habilitada para IAP. Si quieres permitir el acceso a usuarios ajenos a tu organización, consulta Habilita IAP para aplicaciones externas.

Habilita 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 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 and Compute Engine ingress controller are 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:
    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.

¿Qué sigue?