AWS와 거의 유사하게 IAM에서 만든 Account에 OIDC 인증이 가능하도록 세팅하는 구조이다.
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의 다양한 리소스 접근을 하는데 있어서 편하다.
'PaaS > CI CD' 카테고리의 다른 글
(Gitlab) Merge Request시 delete source branch가 체크되어 있는 경우 (1) | 2023.12.14 |
---|---|
Gitlab 의 Project내의 특정 파일을 Access Token 만으로 접근하여 다운로드 (1) | 2023.12.08 |
App of Apps 패턴으로 배포하기. 그리고 나의 생각 (0) | 2023.11.15 |
Argo Rollout으로 Canary 배포하기 (0) | 2023.11.15 |
Argo Rollout으로 Blue/Green 배포하기 (0) | 2023.11.15 |