跳至內容

在使用 IAM 驗證對 API Gateway 提出 SigV4 簽署請求時,如何對簽章不相符錯誤進行疑難排解?

3 分的閱讀內容
0

當我使用 AWS Identity and Access Management (IAM) 驗證對 Amazon API Gateway 提出第 4 版簽署程序 (SigV4) 簽署請求時,收到了 403 錯誤。

解決方法

使用 IAM 驗證的 API Gateway 端點因多種原因傳回 403 錯誤。請根據您收到的錯誤訊息,透過以下各節找出原因並解決。

確認 IAM 驗證

確認使用 IAM 驗證的 API 請求是透過 SigV4 簽署。若要簽署請求,請遵循 SigV4 簽署流程。如果 API 請求未簽署,您會收到下列錯誤:

"Missing Authentication Token."

若要解決此錯誤,請新增缺少的簽章並重新傳送請求。

驗證 IAM 認證

驗證存取金鑰與私密金鑰是否正確。若要檢查您的憑證,請完成下列步驟:

  1. 開啟 IAM 主控台。
  2. 在導覽窗格中,選擇 Users (使用者)。
  3. 選取您的使用者。
  4. 選擇 Security credentials (安全憑證) 索引標籤以檢視您的存取金鑰。

如果存取金鑰不正確,您會收到下列錯誤:

"The security token included in the request is invalid."

若要解決此錯誤,請確認用於簽署請求的 IAM 實體具有 execute-api:Invoke 許可。若要新增許可,請將包含 execute-api:Invoke 動作的 IAM 政策附加到 IAM 實體。如需政策範例,請參閱 IAM policy examples for API execution permissions (API 執行許可的 IAM 政策範例)。

如果缺少 execute-api:Invoke 許可,請求將失敗並出現下列錯誤:

<region><api-id>"User: arn:aws:iam::xxxxxxxxxxxx:user/username is not authorized to perform: execute-api:Invoke on resource: arn:aws:execute-api::xxxxxxxxxxxx:/stage/method/path because no identity-based policy allows the execute-api:Invoke action."

解決簽章不相符

注意: 如果您在執行 AWS Command Line Interface (AWS CLI) 命令時收到錯誤訊息,請參閱 Troubleshooting errors for the AWS CLI (對 AWS CLI 錯誤進行疑難排解)。此外,請確認您使用的是最新版 AWS CLI。

如果私密存取金鑰不正確,您會收到以下錯誤:

"The request signature we calculated does not match the signature you provided."

私密存取金鑰與存取金鑰為成對出現。若要驗證私密存取金鑰與存取金鑰,並找出簽署過程中的任何潛在問題,請使用 curl 命令測試請求。

針對 IAM 使用者,請執行下列 curl 命令:

curl -v --aws-sigv4 'aws:amz:REGION:execute-api' --user 'AWS_ACCESS_KEY_ID:AWS_SECRET_ACCESS_KEY' API_Gateway_endpoint

注意: 請將 REGION 替換為您的 AWS 區域、將 AWS_ACCESS_KEY_ID 替換為您的 IAM 使用者存取金鑰 ID、將 AWS_SECRET_ACCESS_KEY 替換為您的 IAM 使用者私密存取金鑰,並將 API_Gateway_endpoint 替換為您的 API Gateway 端點網址。

針對 IAM 角色,請先透過執行下列 assume-role AWS CLI 命令來取得臨時憑證:

aws sts assume-role --role-arn ROLE_ARN --role-session-name SESSION_NAME

注意: 請將 ROLE_ARN 替換為您想要承擔之 IAM 角色的 Amazon Resource Name (ARN),並將 SESSION_NAME 替換為用於識別該工作階段的名稱。

接著,使用上一個動作中傳回的臨時憑證來執行下列命令:

curl -v --aws-sigv4 'aws:amz:REGION:execute-api' --user 'AWS_ACCESS_KEY_ID:AWS_SECRET_ACCESS_KEY' --header "x-amz-security-token: AWS_SESSION_TOKEN" API_Gateway_endpoint

注意: 請將 REGION 替換為您的 AWS 區域、將 AWS_ACCESS_KEY_ID 替換為來自 assume-role 輸出的存取金鑰 ID、將 AWS_SECRET_ACCESS_KEY 替換為來自 assume-role 輸出的私密存取金鑰、將 AWS_SESSION_TOKEN 替換為來自 assume-role 輸出的工作階段權杖,並將 API_Gateway_endpoint 替換為您的 API Gateway 端點網址。

當 API Gateway 收到經簽署的請求時,便會重新計算簽章。如果請求參數或承載與產生原始簽章的內容不同,API Gateway 將獲得不同的結果。

將從您的簽署流程獲得的正式請求與待簽署字串,與錯誤訊息中傳回的值進行比較。如果存在任何差異,請修改簽署流程。有關修正簽署流程的指引,請參閱 Troubleshoot Signature Version 4 signing for AWS API requests (對 AWS API 請求的第 4 版簽署程序簽署作業進行疑難排解)。

當您將正式請求與錯誤訊息中傳回的值進行比較時,請參考下列範例:

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 requests
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
  • SignedHeaders
  • Signature

缺少參數或參數不正確會導致下列錯誤:

  • "Authorization header requires 'Credential' parameter."
  • "Authorization header requires 'Signature' parameter."
  • "Authorization header requires 'SignedHeaders' parameter."

確認 SigV4 授權請求包含請求日期。若要新增日期,請在請求中包含 HTTP Date 標頭或 x-amz-date 標頭。如需更多資訊,請參閱 Create a signed AWS API request (建立已簽署的 AWS API 請求)。如果缺少日期標頭,您會收到下列錯誤:

"Authorization header requires existence of either a 'X-Amz-Date' or a 'Date' header."

AWS 官方已更新 8 個月前