ノート: GitHubホストランナーは、現在GitHub Enterprise Serverでサポートされていません。 GitHubパブリックロードマップで、計画されている将来のサポートに関する詳しい情報を見ることができます。
概要
OpenID Connect (OIDC) allows your GitHub Actions workflows to access resources in Google Cloud Platform (GCP), without needing to store the GCP credentials as long-lived GitHub secrets.
This guide gives an overview of how to configure GCP to trust GitHub's OIDC as a federated identity, and includes a workflow example for the google-github-actions/auth
action that uses tokens to authenticate to GCP and access resources.
必要な環境
-
GitHubのOpenID Connect (OIDC)の利用方法と、そのアーキテクチャ及び利点の基本概念を学ぶには、「OpenID Connectでのセキュリティ強化について」を参照してください。
-
先に進む前に、アクセストークンが予測できるやり方でのみ割り当てられるよう、セキュリティの戦略を計画しなければなりません。 クラウドプロバイダーによるアクセストークンの発行を制御するためには、信頼できないリポジトリがクラウドのリソースに対するアクセストークンをリクエストできないよう、少なくとも1つの条件を定義しなければなりません。 詳しい情報については「クラウドとのOIDCトラストの設定」を参照してください。
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.
- Create a new identity pool.
- Configure the mapping and add conditions.
- Connect the new pool to a service account.
Additional guidance for configuring the identity provider:
- For security hardening, make sure you've reviewed "Configuring the OIDC trust with the cloud". For an example, see "Configuring the subject in your cloud provider".
- For the service account to be available for configuration, it needs to be assigned to the
roles/iam.workloadIdentityUser
role. For more information, see the GCP documentation. - The Issuer URL to use:
https://HOSTNAME/_services/token
GitHub Actions ワークフローを更新する
To update your workflows for OIDC, you will need to make two changes to your YAML:
- Add permissions settings for the token.
- Use the
google-github-actions/auth
action to exchange the OIDC token (JWT) for a cloud access token.
Adding permissions settings
ジョブもしくはワークフローの実行にはid-token: write
のpermissions
設定が必要です。 id-token
のpermissions
の設定がread
あるいはnone
に設定されていると、OIDC JWT IDトークンをリクエストすることはできません。
id-token: write
と設定すると、JWTはGitHubのOIDCプロバイダから以下のいずれかのアプローチを使ってリクエストできます。
- ランナー上の環境変数を使う(
ACTIONS_ID_TOKEN_REQUEST_URL
及びACTIONS_ID_TOKEN_REQUEST_TOKEN
)。 - Actionsツールキットから
getIDToken()
を使う。
1つのジョブのためにOIDCトークンをフェッチしなければならないだけなら、この権限はそのジョブ内で設定できます。 例:
permissions:
id-token: write
ワークフローの必要に応じて、ここで追加の権限を指定する必要があるかもしれません。
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.
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