ã¨ã ã¹ãªã¼ã¨ã³ã¸ãã¢ãªã³ã°ã°ã«ã¼ã AIã»æ©æ¢°å¦ç¿ãã¼ã ã®ç¬¹å·ã§ãã 趣å³ã¯ãã¹ã±ã¨çãã¬ã§ããã®ã¨ããã¯NBAã¯ãªãã·ã¼ãºã³ã§ããã代ããã«ã¦ã¼ããã¹ã±ãçãä¸ãã£ã¦ãã¦ãNBAã«æ¥ã¦ããªãè¯ããã¬ã¼ã¤ã¼ããããããããã ãªã¼ã¨æããªããè¦ã¦ãã¾ãã
ä»åã¯ããããªãã¯ã¯ã©ã¦ãã¸ã®èªè¨¼ã«å¿ è¦ãªç§å¯æ å ±ãGitLabèªä½ã«æ ¼ç´ãããã¨ãªããå®å ¨ã«èªè¨¼ããæ¹æ³ã«ã¤ãã¦ç´¹ä»ãã¾ãã
- CI/CDã®å®è¡æã®ãããªãã¯ã¯ã©ã¦ãã«å¯¾ããèªè¨¼
- ãã¤ã¼ããªææ³ã¨ãã®åé¡ç¹
- OpenID Connectãç¨ããèªè¨¼
- Terraformã§ãããªãã¯ã¯ã©ã¦ãå´ã®è¨å®ãè¨è¿°ãã
- GitLab CI/CDã§èªè¨¼ãã
- ã¾ã¨ã
- We are hiring!
CI/CDã®å®è¡æã®ãããªãã¯ã¯ã©ã¦ãã«å¯¾ããèªè¨¼
å¼ç¤¾ã§ã¯ã³ã¼ã管çã«GitLabãå©ç¨ããCI/CDãªã©ã¯ GitLab CI/CD ã§å®è¡ãã¦ãã¾ãã ãããã®å®è£ ã«ã¯æ§ã ãªå·¥å¤«ããã¦ãã¾ãã
CI/CDã®å®è¡ã®éã«ã¯ãGoogle CloudããAWSãªã©ã®ãããªãã¯ã¯ã©ã¦ãã¸ã®èªè¨¼/èªå¯ãå¿ è¦ãªã±ã¼ã¹ããããå ·ä½çã«ã¯ä»¥ä¸ã®ãããªã±ã¼ã¹ãããã¾ãã
- Terraformã®planãapply
- ECS, GKEãªã©ã¸ã®ã¢ããªã±ã¼ã·ã§ã³ã®ãªãªã¼ã¹
ä¸è¨ã®ãããªã±ã¼ã¹ã§ã¯ãæ¯è¼çå¼·ã権éãå¿ è¦ãªãã¨ããããèªè¨¼ã®ããã«ç¨ããç§å¯æ å ±ã®ç®¡çã¯ã¨ã¦ãéè¦ã§ãã
ãã¤ã¼ããªææ³ã¨ãã®åé¡ç¹
ãã¤ã¼ããªæ¹æ³ã¨ãã¦ãGoogle Cloudã§ããã°ãservice accountãçºè¡ãããã®json keyã GitLab CI/CD variables ã«ç»é²ãã¦ãCIå®è¡æã«èªè¨¼ã«å©ç¨ããæ¹æ³ãèãããã¾ãã ä¾ãã°ãTerraformã®å®è¡ã®ã±ã¼ã¹ã§ã¯ã以ä¸ã®ãããªå®è£ ã«ãªãã§ãããã
plan:dev: stage: plan allow_failure: true before_script: - echo ${GOOGLE_KEY_DEV} > /etc/key.json - export GOOGLE_APPLICATION_CREDENTIALS=/etc/key.json script: - cd terraform/service/service_name/dev - | tfenv install terraform init terraform plan
以ä¸ã®é¨åã§variableã«ç»é²ãã GOOGLE_KEY_DEV
ãåç
§ããèªè¨¼ã«å©ç¨ãã¦ãã¾ãã
- echo ${GOOGLE_KEY_DEV} > /etc/key.json
ãã®ææ³ã§ã¯ããããªãã¯ã¯ã©ã¦ãã®å¤ã«json keyãªã©ã®ç§å¯æ å ±ãåãåºãã¦å©ç¨ãããã¨ãªãããããããæ¼æ´©ãããã¨ã¯å¤§ããªåé¡ã«ç¹ããã¾ãã å ·ä½çãªä¾ã¨ãã¦ãä½ãã®æåã«ãã°ã«åºåããããçµç¹å¤æ´ãªã©ã§æ¨©éãæã£ã¦ããã¹ãã§ãªã人ç©ãå½è©²æ å ±ãè¦ã¦ãã¾ããªã©ãããã¾ãã
å®æçã«ç§å¯æ å ±ããã¼ãã¼ã·ã§ã³ãã¦è¢«å®³ãæ¸ããæ¹æ³ãããã¾ãããæéããããä¸ã«ãæ大ã§ãã¼ãã¼ã·ã§ã³æéåã®æéã¯ãã®æ å ±ã使ãæ¾é¡ã¨ãããã¨ã«ãªããæ¬è³ªçã«åé¡ã解決ããã¦ãã¾ããã ã¾ãããã¼ãã¼ã·ã§ã³èªä½ãå®è¡ã§ãã人ãçµãã¹ãã§ãããç¹å®ã®roleã®äººã«ã¯ã¼ã¯ãã¼ããéä¸ããã¡ã«ãªã£ã¦ãã¾ããã¨ããã¡ãªããã§ãã
OpenID Connectãç¨ããèªè¨¼
ä¸è¨ã®åé¡ã解決ãããããGitLabã®OpenID Connect (OIDC) æ©è½ãå©ç¨ããç§å¯æ å ±ãGitLabã«æ ¼ç´ããã«èªè¨¼/èªå¯ããä»çµã¿ãæ§ç¯ãã¾ãã
ä»çµã¿ãæ§ç¯ããéã«æ³¨æããç¹ã¨ãã¦ããã®ä»çµã¿ã«å©ç¨ããGitLabã®predefined variableã§ãã CI_JOB_JWT_V2
ã¯alpha statusã®æ©è½ã§ãããä»å¾ã®updateã§ä½¿ããªããªãå¯è½æ§ãããã¨ãããã¨ãããã¾ãï¼ãã®issueã解決ãããã°GAã«ãªãã¨ã®è¨è¼ããããå人çã«ã¯æ¥½è¦³è¦ãã¦ãã¾ãï¼ã
ãã§ã«å¼ç¤¾ã®ä¸é¨ã®ãã¼ã ã§ããã®ä»çµã¿ãæ§ç¯ãCI/CDã®æ©è½ãæ¬çªã«å¯¾ãã¦ã使ã£ã¦ãã¾ãããå©ç¨ããéã¯ä»æ§å¤æ´ãªã©ã«æ°ãã¤ãã¦ãã ããã
èªè¨¼æã®GitLabã¨ãããªãã¯ã¯ã©ã¦ãã®ããã¨ãã¯ä»¥ä¸ã®ããã«ãªã£ã¦ãã¾ãã
å ·ä½çãªã¹ãããã¨ãã¦ã¯ã
- 対象ã®GitLabãOIDC providerã¨ãã¦ã¯ã©ã¦ãå´ã«ç»é²
- é©åã«æ¨©éãçµã£ãroleãã¯ã©ã¦ãå´ã«ç¨æ
- CIã¸ã§ãã
CI_JOB_JWT_V2
ãä»ä¸ãã¦ã¯ã©ã¦ãå´ã®èªè¨¼APIã«å¯¾ãã¦ãªã¯ã¨ã¹ããéã - ã¯ã©ã¦ãå´ããã¼ã¯ã³ãæ¤æ»ãã¦ãåé¡ãªããã°ãä¸æçãªèªè¨¼æ å ±ãã¬ã¹ãã³ã¹ã¨ãã¦è¿ããã
ã¨ãªã£ã¦ãã¾ãã
以éã§ã¯ãä¸è¨ã®OIDCèªè¨¼ãGoogle Cloudã¨Amazon Web Services (AWS) ããããã§ã©ã®ããã«å®ç¾ãããã«ã¤ãã¦èª¬æãã¾ãã
Terraformã§ãããªãã¯ã¯ã©ã¦ãå´ã®è¨å®ãè¨è¿°ãã
OIDCã®æ©è½ã使ãããããããªãã¯ã¯ã©ã¦ãå´ã®è¨å®ãTerraformã§æ¸ããã¨ãèãã¾ãã 以ä¸ã§ã¯ãGoogle Cloudã¨AWS ããããã®å ´åã®ãµã³ãã«ãç´¹ä»ãã¾ãã
Google Cloudã®å ´å
Google Cloudã®å ´åã¯ãWorkload Identity Federation ã®ä»çµã¿ã使ãã¾ãã CIå®è¡ã®é½åº¦ãGitLabã¨Google Cloudã®éã§ããã¨ããããç¹å®ã®service accountã«å¯¾å¿ããæå¹æéã®çãèªè¨¼æ å ±ãçºè¡ãã¦èªè¨¼ãã¾ãã ãã®ä»çµã¿ãå®ç¾ããããã«ã以ä¸ã®3ã¤ã®resourceãæ§ç¯ããå¿ è¦ãããã¾ãã
- Workload Identity Pool
- Workload Identity Pool Provider
- Workload Identityã¦ã¼ã¶ã¼ãã¼ã«ãä»ä¸ããservice account
ãµã³ãã«ã³ã¼ãã¯ä»¥ä¸ã§ãã
æããããã ãµã³ãã«ã³ã¼ãã表示ãã
以ä¸ã®ãµã³ãã«ã³ã¼ãã¯ å ¬å¼ã®ãµã³ãã« ããã¨ã«ä¸é¨ãæ¹å¤ãã¦ãã¾ãã
locals { gitlab_project_id = "xxxx" gitlab_url = "https://gitlab.example.com" project = "xxxxxxxxx" service_account_email = "[email protected]" } # 1. Workload Identity Pool ã®ä½æ resource "google_iam_workload_identity_pool" "gitlab_pool" { workload_identity_pool_id = "gitlab" project = local.project } # 2. Workload Identity Pool Provider ã®ä½æ resource "google_iam_workload_identity_pool_provider" "gitlab_provider_jwt" { workload_identity_pool_id = google_iam_workload_identity_pool.gitlab_pool.workload_identity_pool_id workload_identity_pool_provider_id = "gitlab-jwt" project = local.project attribute_mapping = { "google.subject" = "assertion.sub", "attribute.aud" = "assertion.aud", "attribute.project_path" = "assertion.project_path", "attribute.project_id" = "assertion.project_id", "attribute.group_id" = "assertion.project_path.startsWith('group_a/') ? 'group_a' : ''" "attribute.ref" = "assertion.ref", } attribute_condition = oidc { issuer_uri = local.gitlab_url allowed_audiences = [local.gitlab_url] } } # 3. service accountã¸ã® Workload Identityã¦ã¼ã¶ãã¼ã«ã®ä»ä¸ resource "google_service_account_iam_member" "gitlab_runner_oidc" { service_account_id = "projects/${local.project}/serviceAccounts/${local.service_account_email}" role = "roles/iam.workloadIdentityUser" # GitLabä¸ã®ç¹å®ã®ããã¸ã§ã¯ãããã®ã¿å©ç¨å¯è½ãªããã«çµã£ã¦ãã member = "principalSet://iam.googleapis.com/${google_iam_workload_identity_pool.gitlab_pool.name}/attribute.project_id/${local.gitlab_project_id}" }
ä¸è¨ã³ã¼ããè¦ãã¨ãé¢ç½ãç¹ã2ã¤ããã¾ãã
ã¾ãæåã«ãWorkload Identity Pool Providerã®è¨å®ã®ä¸ã®attribute_mappingã¨ãããããã¯ã§ãã ããã§ã¯GitLabã®JWTã«å«ã¾ããå±æ§ã¨ãGoogleãã¼ã¯ã³å±æ§ã®ãããã³ã°ãè¨å®ãã¾ãã GitLabã®JWTã«å«ã¾ããå±æ§ã¯ä»¥ä¸ã®ããã¥ã¡ã³ãã«ç¤ºããã¦ãã¾ãã
å±æ§ã®ãããã³ã°ã«ã¯ Common Expression Language (CEL) ãå©ç¨ã§ããã®ã§ã ãããç¨ãã¦ã«ã¹ã¿ã å±æ§ãä½ããã¨ãã§ãã¾ãã ä¸è¨ã®ä¾ã§ã¯ã以ä¸ã®é¨åã§ãã®æ©è½ã使ã£ã¦ãã¾ãã
# group_aé ä¸ã®ããã¸ã§ã¯ãã§ããã° "group_a"ããã以å¤ã§ããã°ç©ºã«ãªã "attribute.group_id" = "assertion.project_path.startsWith('group_a/') ? 'group_a' : ''"
GitLabã®JWT ã«group_idã¨ããå±æ§ã¯ããã¾ããããproject_pathã®å±æ§ãå©ç¨ãã CELã使ã£ã¦ãproject_pathãgroup_a/ã§å§ã¾ã£ã¦ããå ´åã¯ãgroup_idãgroup_aã¨ãããã以å¤ã空ã«ãããã¨ããå±æ§ãä½ã£ã¦ãã¾ãã ããã§è¨å®ãããããã³ã°ã使ã£ã¦ããã®Workload Identity Poolãå©ç¨ã§ãããã©ãããå¶éãããã¨ãã§ãã¾ãã詳細ã¯ä»¥ä¸ã®ããã¥ã¡ã³ããã覧ãã ããã
2ã¤ç®ã«ãservice accountã«ã¤ãã¦ãç¹å®ã®æ¡ä»¶ã®ä¸ã§ããèªè¨¼æ å ±ãçºè¡ãããªãããã«å¶éãã¦ããç¹ã§ãã ããã¯ã以ä¸ã®é¨åã該å½ãã¾ãããã®service accountã¯GitLabã®å±æ§ã®ä¸ã®project_idã使ã£ã¦å¶éãå®æ½ãã¦ãããç¹å®ã®ããã¸ã§ã¯ãã®CIããã®èªè¨¼è¦æ±ä»¥å¤ã§ã¯èªè¨¼ã§ããªãããã«ãã¦ãã¾ãã
# GitLabä¸ã®ç¹å®ã®ããã¸ã§ã¯ãããã®ã¿å©ç¨å¯è½ãªããã«çµã£ã¦ãã member = "principalSet://iam.googleapis.com/${google_iam_workload_identity_pool.gitlab_pool.name}/attribute.project_id/${local.gitlab_project_id}"
ä¸è¨ã¯ãã¡ããä»ã®å±æ§ã使ã£ã¦å¶éãããããã¨ãã§ããã®ã§ãä¾ãã°ããæ¬çªããã¸ã§ã¯ãã®service accountã¯ãmainãã©ã³ãããã®èªè¨¼è¦æ±ã§ããèªè¨¼ãããªãããªã©ã®å¶éã§ãããå³å¯ãªæ¨©éåå²ãå®ç¾ã§ãã¾ãã
AWSã®å ´å
AWSã®ã±ã¼ã¹ãGoogle Cloudã¨ã»ã¼åæ§ã«å®ç¾ã§ãã¾ãã ãµã³ãã«ã³ã¼ãã¯ä»¥ä¸ã§ãã
æããããã ãµã³ãã«ã³ã¼ãã表示ãã
以ä¸ã®ãµã³ãã«ã³ã¼ãã¯ å ¬å¼ã®ãµã³ãã«ã³ã¼ã ããã¨ã«ä¸é¨ãæ¹å¤ãã¦ãã¾ãã
locals { gitlab_project_id = "xxxx" gitlab_url = "https://gitlab.example.com" aud_value = "https://gitlab.example.com" match_field = "sub" match_value = ["project_path:group_a/*:ref_type:branch:ref:main"] assume_role_arn = ["arn:aws:iam::9999:policy/name_of_policy"] } data "tls_certificate" "gitlab" { url = local.gitlab_url } # 1. GitLabãOIDC providerã¨ãã¦ç»é² resource "aws_iam_openid_connect_provider" "gitlab" { url = local.gitlab_url client_id_list = [local.aud_value] thumbprint_list = ["${data.tls_certificate.gitlab.certificates.0.sha1_fingerprint}"] } # 2. roleã®ããªã·ã¼ãä½æ data "aws_iam_policy_document" "assume-role-policy" { statement { actions = ["sts:AssumeRoleWithWebIdentity"] principals { type = "Federated" identifiers = [aws_iam_openid_connect_provider.gitlab.arn] } condition { test = "StringEquals" variable = "${aws_iam_openid_connect_provider.gitlab.url}:${local.match_field}" values = local.match_value } } } # 3. ããªã·ã¼ããã¼ã«ã«ä»ä¸ resource "aws_iam_role" "gitlab_ci" { name_prefix = "GitLabCI" assume_role_policy = data.aws_iam_policy_document.assume-role-policy.json managed_policy_arns = local.assume_role_arn }
AWSã®å ´åã§ããGoogle Cloudã®ã±ã¼ã¹ã¨åæ§ã®å¶éãããããã¨ãã§ãã¾ãã
以ä¸ã®é¨åã§ã¯ãGitLabã®å±æ§æ
å ±ããã¨ã«assume roleãå¯è½ãªèªè¨¼è¦æ±ã®æ¡ä»¶ãè¦å®ãã¦ãã¾ãã
å
·ä½çã«ã¯ãmatch_valueã§æå®ããã "project_path:group_a/*:ref_type:branch:ref:main"
ã«å¶éãã¦ããã
group_a
é
ä¸ã®ããã¸ã§ã¯ãã®mainãã©ã³ãããããã¯ãããCIã¸ã§ãã®èªè¨¼è¦æ±ã®ã¿ã«çµã£ã¦assume roleãå¯è½ã«ãªã£ã¦ãã¾ãã
condition { test = "StringEquals" variable = "${aws_iam_openid_connect_provider.gitlab.url}:${local.match_field}" values = local.match_value }
GitLab CI/CDã§èªè¨¼ãã
ããã¾ã§ã§ããããªãã¯ã¯ã©ã¦ãå´ã®æºåãã§ããã®ã§ã次ã¯GitLabå´ã®ã¸ã§ãå®è¡æã«èªè¨¼ããæ¹æ³ã«ã¤ãã¦èª¬æãã¾ãã
Google Cloudã®å ´å
äºåã«ä»¥ä¸ã®æ å ±ãGitLab CI/CD varibalesãªã©ã«ç»é²ãã¦ããã¾ãã
- WORKLOAD_IDENTITY_PROVIDER: workload identity providerã®ID
- SERVICE_ACCOUNT_EMAIL: service accountã®email
ãããã®æ å ±ã¯ç§å¯æ å ±ã§ã¯ãªããä»®ã«ããããæµåºãã¦ããã®æ å ±ã ãã§ã¯èªè¨¼ãããã¨ã¯ã§ããªãã®ã§ãGitLabã¸ã®ç»é²ã¯åé¡ããã¾ãã (ãã¡ããã¿ã ãã«ç¤¾å¤ãªã©ã«å ¬éããå¿ è¦ã¯ããã¾ãã)ã ãããããTerraformã®è¨å®ã®ä¸ã§outputãã¦ããã¨applyæã«åºåãããã®ã§ãã³ããããæã«ä¾¿å©ã§ãã
.gitlab-ci.ymlã®ä¸ã§ã®èªè¨¼ã¹ãããã®å®è£ ã¯ä»¥ä¸ã®ã¨ããã§ãã
auth: before_script: - echo ${CI_JOB_JWT_V2} > .ci_job_jwt_file - gcloud iam workload-identity-pools create-cred-config ${WORKLOAD_IDENTITY_PROVIDER} --service-account="${SERVICE_ACCOUNT_EMAIL}" --output-file=.gcp_temp_cred.json --credential-source-file=.ci_job_jwt_file - export GOOGLE_APPLICATION_CREDENTIALS=`pwd`/.gcp_temp_cred.json - gcloud auth login --cred-file="${GOOGLE_APPLICATION_CREDENTIALS}" script: - do something
gcloud iam workload-identity-pools ã³ãã³ãã«ãå
ã«å®ç¾©ããworkload identity pool providerãservice accountã®æ
å ±ã¨ã
predefined varibaleã§ããCI_JOB_JWT_V2
ã渡ãã¦ãèªè¨¼æ
å ±ãåå¾ãã¦ãã¾ãã
次ã«ãä½æãããèªè¨¼æ
å ±ãå©ç¨ãã¦gcloud auth login ã³ãã³ãã§èªè¨¼ãã¦ãã¾ãã
GOOGLE_APPLICATION_CRENDENTIALS
å¤æ°ã¸ã®exportã¯ããããããã©ã«ãã§åç
§ãããã¼ã«ãå¤ãã®ã§ããã£ã¦ããã¨å¾ç¶ã®å¦çã§æ¥½ãã§ããããã¾ãã
AWSã®å ´å
AWSã®å ´åã¯ãäºåã«ä»¥ä¸ã®æ å ±ãç»é²ãã¦ããã¾ãã
- ROLE_ARN: assume roleããroleã®arn
ãã¡ããoutputãã¦ããã¨ä¾¿å©ã§ãã
.gitlab-ci.ymlã®å®è£ ã¯ä»¥ä¸ã®ããã«ãªãã¾ãã
auth: before_script: - STS=($(aws sts assume-role-with-web-identity --role-arn ${ROLE_ARN} --role-session-name "GitLabRunner-${CI_PROJECT_ID}-${CI_PIPELINE_ID}" --web-identity-token "${CI_JOB_JWT_V2}" --query 'Credentials.[AccessKeyId,SecretAccessKey,SessionToken]' --output text)) - export AWS_ACCESS_KEY_ID="${STS[0]}" - export AWS_SECRET_ACCESS_KEY="${STS[1]}" - export AWS_SESSION_TOKEN="${STS[2]}" script: - do something
ä¸è¨ã§ã¯ãaws sts assume-role-with-web-identity ã³ãã³ãã«ãå
ã«å®ç¾©ããrole arnã¨ãCI_JOB_JWT_V2
ã渡ãã¦ãèªè¨¼æ
å ±ãåå¾ãã¦ãã¾ãã
--role-session-name
ãªãã·ã§ã³ã«CI_PROJECT_ID
, CI_PIPELINE_ID
ã渡ãã¦ããã®ã¯ãcaller identityã®arnãä¸æã«ããããã§ãï¼ãããéè¤ããã¨ã¨ã©ã¼ã¨ãªãèªè¨¼ã§ããªãï¼ã
èªè¨¼ã¹ãããã®å ±éå
çè ãæå±ããAIã»æ©æ¢°å¦ç¿ãã¼ã ã§ã¯ãä¸è¨ã®before_scripté¨åãGitLabã®includeæ©è½ ãç¨ãã¦å ±éåããããããã®ããã¸ã§ã¯ãã§å©ç¨ãã¦ãã¾ãã èªè¨¼é¨åãå¿ è¦ãªå¤æ°ãå®ç¾©ããã ãã§ç°¡åã«å©ç¨ã§ããGitLabãªã©ã®ã¢ãããã¼ãã§ä¿®æ£ãå¿ è¦ã«ãªã£ãã±ã¼ã¹ã§ãå¤ãã®å ´åã¯å ±éåããå¦çã®ã¿ã®ä¿®æ£ã§æ¸ãã¨ããå¹æãããã¾ãã includeæ©è½ã®è©³ç´°ã¯ä»¥ä¸ã®è¨äºãã覧ãã ããã
ã¾ã¨ã
ä»åã¯GitLabä¸ã§CI/CDãå®è¡ããéã«ããããªãã¯ã¯ã©ã¦ãã®èªè¨¼æ å ±ãGitLabã«æ ¼ç´ãããã¨ãªããå®å ¨ã«èªè¨¼ããæ¹æ³ã«ã¤ãã¦ç´¹ä»ãã¾ããã ãããªãã¯ã¯ã©ã¦ãå´ã®è¨å®ã«ã¤ãã¦ãã³ã¼ãåããããã¨ã§æéãå°ãªããå®å¿ãã¦CIãå®è¡ã§ããããã«ãªãã¾ããã
We are hiring!
ã¨ã ã¹ãªã¼ã§ã¯ãã»ãã¥ã¢ã§ã¤ã±ã¦ãCI/CDãæ§ç¯ãããã¨ã«èå³ãããã¨ã³ã¸ãã¢ãåéä¸ã§ãã
社å å¤åãããã·ã¹ãã ã®éçºãæ½çã®å®æ½ã«ããä¸ã®ä¸ã«ã¤ã³ãã¯ããä¸ããæ©ä¼ãå¤æ°ããã¾ãã®ã§ãæ¯éæããã¯ï¼ã¨ããæ¹ã¯ã«ã¸ã¥ã¢ã«é¢è«ããå¿åãå¾ ã¡ãã¦ãã¾ãï¼