排解 Workload Identity 聯盟問題

本頁說明如何解決常見的工作負載身分聯盟錯誤。

Google Cloud API 不接受從 SecurityTokenService 核發的憑證

SecurityTokenService API 傳回的存取權杖是同盟存取權杖。雖然大多數 Google Cloud API 都支援身分同盟,但某些 API 方法可能有限制。如需限制的清單,請參閱「身分同盟:產品和限制」。

如果遇到下列錯誤,可能是您嘗試將同盟存取權杖用於不支援這類權杖的服務。

{
  "error": {
    "code": 401,
    "message": "Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.",
    "status": "UNAUTHENTICATED",
  }
}

如要解決這項錯誤,請呼叫 GenerateAccessToken,將同盟存取權杖換成不受限制的存取權杖。詳情請參閱「使用 Identity 聯盟取得短期憑證」。

將身分識別提供者加入允許清單,以搭配 Workload Identity 聯盟使用

如果您嘗試將不允許的識別資訊提供者設定為 workload identity pool 提供者,會遇到下列錯誤:

FAILED_PRECONDITION: Precondition check failed.
- '@type': type.googleapis.com/google.rpc.PreconditionFailure
  violations:
  - description: "Org Policy violated for value: '{PROVIDER}'."
    subject: orgpolicy:projects/{PROJECT}/locations/global/workloadIdentityPools/{POOL}
    type: constraints/iam.workloadIdentityPoolProviders

如要解決這個問題,請按照「限制身分識別提供者設定」一文中的指示,將身分識別提供者加入許可清單,以便與 Workload Identity Federation 搭配使用。

輸入的 JWK 格式無效

如果您正在設定 OIDC 供應商,並收到 Input JWK is not in a valid json format 錯誤訊息,可能是因為 Google Cloud不支援以自簽憑證保護的端點。具體來說,系統不支援 x5cx5t 欄位,因此必須從 OIDC JWK 移除。

如要解決 JWK 的問題,請按照下列步驟操作:

  1. 編輯 JWK,並移除 x5c (X.509 憑證鏈結) 和 x5t (X.509 憑證 SHA-1 指紋) 欄位。

    {
      "kty": "RSA",
      "use": "sig",
      "kid": "example-key-id",
      "alg": "RS256",
      "n": "base64url-modulus",
      "e": "AQAB"
    }
    
  2. 確認其餘 JWK 欄位的格式正確無誤,如 OIDC 規格所述。

  3. 使用更新後的 JWK 設定 OIDC 供應商。

連線至指定憑證核發機構時發生錯誤

如果收到下列錯誤訊息,可能是因為 Google Cloud 無法擷取 IdP 的 OIDC 中繼資料文件或 JWKS:

{
  "error": "invalid_grant",
  "error_description":"Error connecting to the given credential's issuer."
 }

發生這項錯誤的原因通常是端點未設定為可從公開網際網路連線。如要解決這項錯誤,請確認 OIDC 端點可公開存取,且符合 OIDC 規格。詳情請參閱「準備外部身分識別提供者」。

如果仍收到錯誤訊息,請檢查權杖核發者,以及權杖中的 iss 聲明是否正確。

對應的 google.subject 聲明超過 127 位元組的上限

如果收到下列錯誤訊息,表示 SecurityTokenService API 收到的傳入憑證產生的 google.subject 聲明超出字元限制:

{
  "error": "invalid_request",
  "error_description":"The size of mapped attribute google.subject exceeds the 127 bytes limit. Either modify your attribute mapping or the incoming assertion to produce a mapped attribute that is less than 127 bytes."
 }

如要解決這個問題,請使用 extract 函式移除不必要的字元,並從較長的聲明中擷取不重複的主體 ID,例如:

google.subject=assertion.sub.extract('/users/{sub_claim}')

錯誤 429:要求數量過多

如果您從 SecurityTokenService API 要求權杖時收到 429 Too Many Requests 錯誤,表示您的 Google Cloud 專案已超出 API 的速率配額限制。

錯誤訊息類似於下列內容:

{
  "error": "quota_exceeded",
  "error_description":"The request was throttled due to rate limit: sts.googleapis.com/requests. Please retry after a few seconds."
 }

如要解決這個問題,請找出錯誤訊息中提供的 sts.googleapis.com/requests 指標名稱,在 IAM 配額頁面中找到對應的指標名稱,確認使用百分比超過配額,然後要求提高配額。