AWS API 网关自定义授权方 AuthorizerConfigurationException

AWS API Gateway Custom Authorizer AuthorizerConfigurationException

对于 Kinesis 流,我使用 AWS API 网关创建了一个代理 API。我为代理添加了使用 python Lambda 的自定义授权方。 在发布 lambda 函数并部署 API 之后,我能够使用网关测试功能成功测试 API。我可以在 cloudwatch 中看到日志,其中包含来自自定义 auth lambda 函数的详细打印。认证成功后,API网关将记录推送到我的Kinesis流

但是,当我从 Chrome Postman 客户端调用相同的 API 时,我得到 500 Internal Server Error 并且响应 headers 包括 X-Cache → 来自云端的错误,x-amzn-ErrorType → AuthorizerConfigurationException

Lambda 身份验证函数 returns 允许对我的 API 执行请求的策略。返回的政策文件是:

            {
              "policyDocument": {
                "Version": "2012-10-17",
                "Statement": [
                  {
                    "Action": "execute-api:Invoke",
                    "Resource": [
                      "arn:aws:execute-api:us-east-1:1234567:myapiId/staging/POST/*"
                    ],
                    "Effect": "Allow"
                  }
                ]
              },
              "principalId": "Foo"
            }

为什么请求从 Chrome 或 curl 失败,但相同的 API 测试从 API 网关正常工作?

AuthorizerConfigurationException 通常表示 API 网关由于权限错误未能调用您的授权方。

请确保您已正确配置要由 API 网关调用的函数。一个简单的重置方法是删除该功能并将其重新添加到您的授权方。然后控制台将提示您添加必要的权限。

找出导致问题的原因。从 python lambda 函数,我返回了一个 json 字符串实例。相反,它应该是 json 对象。 奇怪的是,当我测试 API from API Gateway "test" 功能时,相同的 lambda 函数没有出错。但是当从互联网调用 API 时(curl 或 chrome)它失败了。

#return policy_string ... this is incorrect.
return json.loads(policy_string)

就我而言,我没有返回格式正确的 IAM 策略文档。我的 Authorizer 函数对如何从请求中获取某些参数做了错误的假设,默认结果不是正确的策略(这是我的具体情况)。我设法使用 CloudWatch 日志服务调试它,传统的日志记录指令来自我的函数代码。

我遇到了同样的错误,在我的例子中是一个 nodejs 函数,我添加了一个上下文键作为数组。

{
  policyDocument: {
  Version: '2012-10-17',
  Statement: [{
    Action: 'execute-api:Invoke',
    Effect: effect,
    Resource: `${arn.split('/').slice(0, 2).join('/')}/*`,
  }],
},
context: {
  roles: ['admin']
}

正如医生所说:

You can access the stringKey, numberKey, or booleanKey value (for example, "value", "1", or "true") of the context map in a mapping template by calling $context.authorizer.stringKey, $context.authorizer.numberKey, or $context.authorizer.booleanKey, respectively. The returned values are all stringified. Notice that you cannot set a JSON object or array as a valid value of any key in the context map.

删除角色密钥,它正在工作。