我該如何使用 IAM 身分驗證為 API Gateway Sigv4 簽章不符的錯誤進行疑難排解?

2 分的閱讀內容
0

對 Amazon API Gateway 的第 4 版簽署程序 (SigV4) 簽署請求失敗,出現 403 回應以及錯誤。錯誤類似於以下內容:「我們計算的請求簽章不符合您提供的簽章。請檢查您的 AWS 私密存取金鑰和簽署方法。」

簡短描述

如有以下情況,使用 AWS Identity and Access Management (IAM) 身分驗證的 API Gateway API 端點可能會傳回 403 錯誤:

  • API 請求未經簽署,且 API 請求使用 IAM 身分驗證。
  • 用來簽署請求的 IAM 憑證不正確或沒有叫用 API 的權限。
  • 已簽署 API 請求的簽章與 API Gateway API 端點的簽章不符。
  • API 請求的標頭不正確。

解決方案

IAM 身分驗證

確保使用 IAM 身分驗證的 API 請求已使用 Sigv4 簽署。如果未簽署 API 請求,您可能會收到以下錯誤:「缺少身分驗證權杖」。

IAM 憑證

確認存取金鑰和私密金鑰的憑證正確無誤。如果存取金鑰不正確,您可能會收到以下錯誤:「請求中包含的安全權杖無效」。

確保用於簽署請求的 IAM 實體具有execute-api:Invoke 許可。如果 IAM 實體沒有 執行 API: 叫 用權限,您可能會收到下列錯誤訊息:「使用者:arn:aws:iam::xxxxxxxxxxxx:user/username 未獲授權而無法在資源上執行:execute-api:Invoke」

簽章不相符

如果私密存取金鑰不正確,您可能會收到以下錯誤:「我們計算的請求簽名與您提供的簽名不相符」。

私密存取金鑰必須與 Credential (憑證) 參數中的存取金鑰 ID 相符。如需指示,請遵循「如何為 API Gateway REST API 啟用 IAM 身分驗證?」中的傳送請求以測試身分驗證設定章節中的步驟。

請確定您已遵循 Sigv4 簽署程序的指示。如果簽名計算中的任何值不正確,您可能會收到以下錯誤:「我們計算的請求簽名與您提供的簽名不相符」。

當 API Gateway 收到已簽署的請求時,它會重新計算簽章。如果值有差異,API Gateway 會取得不同的簽章。將正規請求和字符串與已簽署請求和錯誤訊息中的值進行比較。如果有任何差異,請修改簽署程序。

範例正規請求:

GET                                                      -------- HTTP method
/                                                        -------- Path. For API stage endpoint, it should be /{stage-name}/{resource-path}
                                                         -------- Query string key-value pair. Leave it blank if the request doesn't have any query string
content-type:application/json                            -------- header key-value pair. One header per line
host:0123456789.execute-api.us-east-1.amazonaws.com      -------- host and x-amz-date are required headers for all signed request                       
x-amz-date:20220806T024003Z                              

content-type;host;x-amz-date                             -------- A list of signed headers
d167e99c53f15b0c105101d468ae35a3dc9187839ca081095e340f3649a04501        -------- hash of the payload

範例正規錯誤回應:

<ErrorResponse xmlns="https://iam.amazonaws.com/doc/2010-05-08/">
  <Error>
    <Type>Sender</Type>
    <Code>SignatureDoesNotMatch</Code>
    <Message>The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details.

The canonical string for this request should have been 'GET / Action=ListGroupsForUser&MaxItems=100&UserName=Test&Version=2010-05-08&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential
=AKIAIOSFODNN7EXAMPLE%2F20120223%2Fus-east-1%2Fiam%2Faws4_request&X-Amz-Date=20120223T063000Z&X-Amz-SignedHeaders=host
host:iam.amazonaws.com

host
<hashed-value>'

The String-to-Sign should have been
'AWS4-HMAC-SHA256
20120223T063000Z
20120223/us-east-1/iam/aws4_request
<hashed-value>'
</Message>
  </Error>
  <RequestId>4ced6e96-5de8-11e1-aa78-a56908bdf8eb</RequestId>
</ErrorResponse>

**注意:**對於 API Gateway 標頭,只需要 host (託管) 和 x-amz-date 標頭。

API 請求標頭

請確定 Sigv4 授權標頭包含類似下列內容的正確憑證金鑰:

Authorization: AWS4-HMAC-SHA256 
Credential=AKIAIOSFODNN7EXAMPLE/20130524/us-east-1/s3/aws4_request, 
SignedHeaders=host;range;x-amz-date,
Signature=example-generated-signature

如果憑證金鑰遺失或不正確,您可能會收到下列錯誤:「授權標頭需要 'Credential' 參數。授權標頭需要 'Signature' 參數」。

請確定 Sigv4 授權請求也包含使用 HTTP Date (HTTP 日期) 或 x-amz-date 標頭的請求日期。


相關資訊

AWS SDK 中的程式碼範例

如何針對 API Gateway 的 HTTP 403 錯誤進行疑難排解?

AWS 官方
AWS 官方已更新 1 年前