PaaS/CI CD

Github Actions의 Workflow에서 GCP(Google Cloud Platform) 인증받기

armyost 2023. 12. 7. 21:40
728x90

AWS와 거의 유사하게 IAM에서 만든 Account에 OIDC 인증이 가능하도록 세팅하는 구조이다. 

 관련링크 : https://cloud.google.com/blog/products/identity-security/enabling-keyless-authentication-from-github-actions

 

Enabling keyless authentication from GitHub Actions | Google Cloud Blog

Authenticate from GitHub Actions to create and manage Google Cloud resources using Workload Identity Federation.

cloud.google.com

 

Github Actions 파이프라인과 샘플 어플리케이션은 아래 Github Repo에 있으니 참고

Github주소 : https://github.com/armyost/testPagePy

 

GitHub - armyost/testPagePy: This is showing python application for piloting Github Actions pipeline.

This is showing python application for piloting Github Actions pipeline. - GitHub - armyost/testPagePy: This is showing python application for piloting Github Actions pipeline.

github.com


작업순서

1. Workload Identity Pool 만들기

$ gcloud iam workload-identity-pools create "k8s-pool" --project="sample-prj-407212" --location="global" --display-name="k8s Pool"

 

 

2. Workload Identity Pool 에서 OIDC Provider 정의하기

$ gcloud iam workload-identity-pools providers create-oidc "k8s-provider" \
  --project="sample-prj-407212" \
  --location="global" \
  --workload-identity-pool="k8s-pool" \
  --display-name="k8s provider" \
  --attribute-mapping="google.subject=assertion.sub,attribute.actor=assertion.actor,attribute.aud=assertion.aud,attribute.repository=assertion.repository" \
  --issuer-uri="https://token.actions.githubusercontent.com"

※ 여기서 주의할 점은 attribute-mapping이다. 인증요청자가 attribute 설정에 맞추어 요청할 수 있게끔 세팅한다. 필자의 경우 repository 주소로 검열할 것이다. 

 

3. IAM Service Account 생성

$ gcloud iam service-accounts create github-actions-sa \
--display-name="Service account used by WIF POC" \
--project sample-prj-407212

 

4. 생성한 IAM Service Account에 권한 부여

 

5. 3에서 만든 Service Account에 2에서 정의한 OIDC를 입힌다. 

$ gcloud iam service-accounts add-iam-policy-binding "github-actions-sa@sample-prj-407212.iam.gserviceaccount.com" \
  --project="sample-prj-407212" \
  --role="roles/iam.workloadIdentityUser" \
  --member="principalSet://iam.googleapis.com/projects/442221788601/locations/global/workloadIdentityPools/k8s-pool/attribute.repository/armyost/testPagePy"

 

6. 이제 Github Actions Pipeline을 수정한다. 

- permissions 에서 id-token 권한을 write로 정의한다.

 

- Secret에 SERVICE_ACCOUNT 이메일 전체를 등록하고, WORKLOAD_IDENTITY_PROVIDER_ID 전체를 등록한다. 

 

WORKLOAD_IDENTITY_PROVIDER_ID :
   projects/4XXXX01/locations/global/workloadIdentityPools/k8s-pool/providers/k8s-provider'

 

SERVICE_ACCOUNT :
  github-actions-sa@sample-prj-407212.iahttp://m.gserviceaccount.com

- github actions에서 파이프라인에 다음과 같이 세팅하면 끝

# Option 1) GCP Login
    - name: Set up GCP Credential
      uses: 'google-github-actions/auth@v1'
      with:
        token_format: 'access_token'
        workload_identity_provider: ${{ secrets.WORKLOAD_IDENTITY_PROVIDER_ID }}
        service_account: ${{ secrets.SERVICE_ACCOUNT }}

 


정상 인증 확인

 

↑이렇게 Credential 로그인을 해두면 GCP의 다양한 리소스 접근을 하는데 있어서 편하다.