簡體   English   中英

Gitlab 連接到 GCP 工作負載身份聯合返回 invalid_grant

[英]Gitlab connection to GCP Workload Identity Federation return invalid_grant

昨天我看到 Gitlab 為 ci/cd 上的作業啟用了 OIDC JWT 令牌 我知道 CI_JOB_JWT_V2 被標記為 alpha 功能。

我試圖在帶有 gcloud cli 的 Gitlab 運行器上將它與 Workflow Identity Federation(WIF) 一起使用,但出現錯誤。 當嘗試通過 STS API 執行此操作時,我遇到了同樣的錯誤。 我錯過了什么?

{
    "error": "invalid_grant",
    "error_description": "The audience in ID Token [https://gitlab.com] does not match the expected audience."
}

我的 Gitlab JWT 令牌在解碼后看起來大多像這樣(ofc 沒有細節)

{
  "namespace_id": "1111111111",
  "namespace_path": "xxxxxxx/yyyyyyyy/zzzzzzzzzzz",
  "project_id": "<project_id>",
  "project_path": "xxxxxxx/yyyyyyyy/zzzzzzzzzzz/hf_service",
  "user_id": "<user_id>",
  "user_login": "<username>",
  "user_email": "<user_email>",
  "pipeline_id": "456971569",
  "pipeline_source": "push",
  "job_id": "2019605390",
  "ref": "develop",
  "ref_type": "branch",
  "ref_protected": "true",
  "environment": "develop",
  "environment_protected": "false",
  "jti": "<jti>",
  "iss": "https://gitlab.com",
  "iat": <number>,
  "nbf": <number>,
  "exp": <number>,
  "sub": "project_path:xxxxxxx/yyyyyyyy/zzzzzzzzzzz/hf_service:ref_type:branch:ref:develop",
  "aud": "https://gitlab.com"
}

在 GCP 控制台中,我有一個 WIF 池,其中一個提供者設置為 OIDC,名為 gitlab 和來自https-configuration-known/gitlab.com/ 的發行者 url

我試圖讓服務帳戶訪問整個池,但沒有區別。 為此 SA 創建的配置如下所示

{
  "type": "external_account",
  "audience": "//iam.googleapis.com/projects/<projectnumber>/locations/global/workloadIdentityPools/<poolname>/providers/gitlab",
  "subject_token_type": "urn:ietf:params:oauth:token-type:jwt",
  "token_url": "https://sts.googleapis.com/v1/token",
  "service_account_impersonation_url": "https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/gitlab-deployer@<projectid>.iam.gserviceaccount.com:generateAccessToken",
  "credential_source": {
    "file": "gitlab_token",
    "format": {
      "type": "text"
    }
  }
}

默認情況下,工作負載身份聯合要求aud聲明包含工作負載身份池提供者的 URL 這個 URL 看起來像這樣:

https://iam.googleapis.com/projects/PROJECT_NUMBER/locations/global/workloadIdentityPools/POOL_ID/providers/PROVIDER_ID

但是您的令牌似乎使用https://gitlab.com作為觀眾。

重新配置 GitHub 以使用工作負載身份池提供程序 URL 作為受眾,或通過運行重新配置池以使用自定義受眾

gcloud iam workload-identity-pools providers update-oidc ... \
  --allowed-audiences=https://gitlab.com

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM