排解 Workload Identity 聯盟問題

本頁說明如何解決常見的 Workload Identity Federation 錯誤。

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,將同盟存取權杖換成不受限制的存取權杖。詳情請參閱「透過身分聯盟取得短期憑證」。

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

如果您嘗試將不允許的識別資訊提供者設定為 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不支援以自簽憑證保護的端點。具體來說,系統不支援 x5c 和 x5t 欄位,因此必須從 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 聲明是否正確。

提供的 AWS 要求未包含所有必要標頭

如果您透過 SecurityTokenService API 要求權杖時收到下列錯誤,表示 AWS GetCallerIdentity 權杖中的 headers 欄位缺少必要標頭,或包含不支援的標頭:

{
  "error": "invalid_grant",
  "error_description": "The given AWS request doesn't contain all the required headers."
}

GetCallerIdentity 權杖的 headers 欄位必須只包含下列 HTTP 要求標頭:

  • Authorization:要求簽章。
  • host:url 欄位的主機名稱,例如 sts.amazonaws.com。
  • x-amz-date:您傳送要求的時間,格式為 ISO 8601 Basic 字串。
  • x-goog-cloud-target-resource:Workload Identity Pool 提供者的完整資源名稱。
  • x-amz-security-token:工作階段符記。如果您使用臨時安全憑證,則必須提供這個值。

如果 Signature Version 4 簽署者在經簽署的要求中加入其他標頭 (例如 x-amz-content-sha256),也會發生這個錯誤。如要解決這個問題,請確認 GetCallerIdentity 要求的主體為空白,且簽署要求的 headers 欄位和 SignedHeaders 欄位只包含支援的標頭。詳情請參閱「使用 REST API 驗證工作負載」。

對應的 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 配額頁面中找到對應的指標名稱,確認使用百分比超過配額,然後要求提高配額。