使用 IAM 身份验证向 API Gateway 发出 SigV4 签名请求时,如何排查签名不匹配错误?

2 分钟阅读
0

签名版本 4(SigV4)对 Amazon API Gateway 的签名请求失败,返回了 403 响应和一个错误。该错误类似于以下内容:"The request signature we calculated does not match the signature you provided.Check your AWS Secret Access Key and signing method.”(我们计算的请求签名与您提供的签名不匹配。请检查您的 AWS 秘密访问密钥和签名方法。)

简短描述

在以下情况下,使用 AWS 身份和访问管理 (IAM) 身份验证的 API Gateway API 端点可能会返回 403 错误:

  • API 请求未签名,并且 API 请求使用 IAM 身份验证。
  • 用于签署请求的 IAM 凭证不正确或无权调用 API。
  • 已签名的 API 请求的签名与 API Gateway API 端点的签名不匹配。
  • API 请求标头不正确。

解决方法

IAM 身份验证

确保使用 IAM 身份验证的 API 请求是使用 SigV4 签署的。如果 API 请求未签名,则您可能会收到以下错误提示:“Missing Authentication Token.”(缺少身份验证令牌。)

IAM 凭证

验证访问密钥和秘密密钥的身份验证凭证是否正确。如果访问密钥不正确,则您可能会收到以下错误提示:“The security token included in the request is invalid.”(请求中包含的安全令牌无效)

确保用于签署请求的 IAM 实体具有 execute-api:Invoke 权限。如果 IAM 实体没有 execute-api:Invoke 权限,则您可能会收到以下错误提示:“User: arn:aws:iam::xxxxxxxxxxxx:user/username is not authorized to perform: execute-api:Invoke on resource”(用户 :arn: aws: iam:: xxxxxxxxxx: user/username 无权在资源上执行: execute-api: Invoke)

签名不匹配

如果秘密访问密钥不正确,则您可能会收到以下错误提示:“The request signature we calculated does not match the signature you provided.”(我们计算的请求签名与您提供的签名不匹配)

秘密访问密钥必须与凭证参数中的访问密钥 ID 相匹配。有关说明,请参阅如何为 API Gateway REST API 激活 IAM 身份验证?中的发送请求以测试身份验证设置部分

确保您按照 SigV4 签名过程的说明进行操作。如果签名计算中的任何值不正确,则您可能会收到以下错误提示:“The request signature we calculated does not match the signature you provided.”(我们计算的请求签名与您提供的签名不匹配)

当 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 网关标头,只需要主机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

如果凭证密钥丢失或不正确,您可能会收到以下错误提示:“Authorization header requires ‘Credential’ parameter.Authorization header requires 'Signature' parameter."(授权标头需要“凭证”参数。授权标头需要“签名”参数。)

确保 SigV4 授权请求还包括使用 HTTP 日期x-amz-date 标头的请求日期。


相关信息

AWS SDK 中的代码示例

如何排查 API Gateway 中的 HTTP 403 错误?

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