Skip to main content

Configuring OpenID Connect in Google Cloud Platform

Use OpenID Connect within your workflows to authenticate with Google Cloud Platform.

Resumen

OpenID Connect (OIDC) permite que tus flujos de trabajo de GitHub Actions accedan a recursos en la Plataforma de Google Cloud (GCP) sin necesidad de almacenar sus credenciales como secretos de GitHub de larga duración.

Esta guía te proporciona un resumen de cómo configurar GCP para que confíe en el OIDC de GitHub como una entidad federada e incluye un ejemplo de flujo de trabajo para la acción google-github-actions/auth que utiliza tokens para autenticarse al GCP para acceder a los recursos.

Prerrequisitos

  • Para aprender los conceptos básicos de cómo GitHub utiliza OpenID Connect (OIDC) y su arquitectura y beneficios, consulta la sección "Acerca del fortalecimiento de seguridad con OpenID Connect".

  • Before proceeding, you must plan your security strategy to ensure that access tokens are only allocated in a predictable way. To control how your cloud provider issues access tokens, you must define at least one condition, so that untrusted repositories can’t request access tokens for your cloud resources. For more information, see "Configuring the OIDC trust with the cloud."

Adding a Google Cloud Workload Identity Provider

To configure the OIDC identity provider in GCP, you will need to perform the following configuration. For instructions on making these changes, refer to the GCP documentation.

  1. Create a new identity pool.
  2. Configure the mapping and add conditions.
  3. Connect the new pool to a service account.

Orientación adicional para configurar el proveedor de identidad:

Actualizar tu flujo de trabajo de GitHub Actions

To update your workflows for OIDC, you will need to make two changes to your YAML:

  1. Agregar ajustes de permisos para el token.
  2. Use the google-github-actions/auth action to exchange the OIDC token (JWT) for a cloud access token.

Agregar ajustes de permisos

El flujo de trabajo requerirá una configuración de permissions con un valor de id-token definido. Si solo necesitas recuperar un token de OIDC para un solo job, entonces este permiso puede configurarse dentro de dicho job. Por ejemplo:

YAML
permissions:
  id-token: write

Puede que necesites especificar permisos adicionales aquí, dependiendo de los requisitos de tu flujo de trabajo.

Requesting the access token

The google-github-actions/auth action receives a JWT from the GitHub OIDC provider, and then requests an access token from GCP. For more information, see the GCP documentation.

This example has a job called Get_OIDC_ID_token that uses actions to request a list of services from GCP.

  • <example-workload-identity-provider>: Replace this with the path to your identity provider in GCP. For example, projects/<example-project-id>/locations/global/workloadIdentityPools/<name-of-pool/providers/<name-of-provider>
  • <example-service-account>: Replace this with the name of your service account in GCP.
  • <project-id>: Replace this with the ID of your GCP project.

This action exchanges a GitHub OIDC token for a Google Cloud access token, using Workload Identity Federation.

YAML
name: List services in GCP
on:
  pull_request:
    branches:
      - main

permissions:
  id-token: write

jobs:
  Get_OIDC_ID_token:
    runs-on: ubuntu-latest
    steps:
    - id: 'auth'
      name: 'Authenticate to GCP'
      uses: 'google-github-actions/auth@v0.3.1'
      with:
          create_credentials_file: 'true'
          workload_identity_provider: '<example-workload-identity-provider>'
          service_account: '<example-service-account>'
    - id: 'gcloud'
      name: 'gcloud'
      run: |-
        gcloud auth login --brief --cred-file="${{ steps.auth.outputs.credentials_file_path }}"
        gcloud services list