【问题标题】:API Gateway Authorizer is not being called未调用 API Gateway Authorizer
【发布时间】:2020-07-11 07:33:43
【问题描述】:

我创建了一些 Lambda 函数并使用 SAM 部署它们。部署成功,但在尝试到达端点时,我总是获得

{ message: "Unauthorized" }

即使我使用 Authentication 标头发送正确的 Bearer 令牌。 然后,如果我去授权者并运行测试,它会顺利通过并在 CloudWatch 中生成日志,但是当我从前端应用程序或 REST 客户端应用程序向端点运行请求时,我收到未经授权的消息并检查 CloudWatch,有不是授权函数的执行。

另外,从 Lambda 配置中检查 Authorizer 函数,我可以看到 API Gateway Trigger 中有错误,但不知道是什么意思。

我使用 AWS 提供的指南创建了授权函数:https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-use-lambda-authorizer.html#api-gateway-lambda-authorizer-lambda-function-create

分享我的 SAM 配置

Resources:
 SomeAPI:
    Type: AWS::Serverless::Api
    Properties:
      StageName: Prod
      TracingEnabled: true
      Auth:
        DefaultAuthorizer: MyLambdaTokenAuthorizer
        Authorizers:
          MyLambdaTokenAuthorizer:
            FunctionArn: !GetAtt AuthorizerFunction.Arn

  GetActivityStreamFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: get-activity-stream/ 
      Handler: index.handler
      Layers:
        - !Ref DepencenciesLayer
        - !Ref DatabaseLayer
      Events:
        GetActivityStream:
          Type: Api
          Properties:
            RestApiId: !Ref SomeAPI
            Path: /activity-stream
            Method: get

  ## Authorizer Function
  AuthorizerFunction:
    Type: AWS::Serverless::Function
    Properties:
      Layers:
        - !Ref DepencenciesLayer
      CodeUri: authorizer/ 
      Handler: index.handler

关于Authorizer返回的响应,它会发送API Gateway请求的所有参数

{
      principalId: decoded.sub,
      policyDocument: getPolicyDocument("Allow", params.methodArn),
      context: { scope: decoded.scope }
}

我使用的运行时是nodejs12.x,下面是我从 AWS 控制台获得的一些屏幕截图。

【问题讨论】:

  • 您可以尝试在您的 /activity-stream 路由下的 API 网关中打开方法请求 -> 在授权方下拉列表中:选择任何其他值(无或其他授权方)并点击保存,然后继续通过相同的过程并重新选择您的授权人。它应该提示您它将重新添加允许调用您的授权函数的权限。这以前对我有用。

标签: amazon-web-services aws-lambda authorization aws-api-gateway


【解决方案1】:

在互联网上测试了很多小时后,当我发现政策文件有误时,我开始有所改进:

之前

  const policyDocument = {
    "Version": "2012-10-17",
    "Statement": [
      {
        "Effect": effect,
        "Action": "execute-api:Invoke",
        "Resource": resource
      }
    ]
  };

之后

  const policyDocument = {
    "Version": "2012-10-17",
    "Statement": [
      {
        "Effect": effect,
        "Action": [
          "execute-api:Invoke"
        ],
        "Resource": [
          resource
        ]
      }
    ]
  };

ActionResource 应该是数组。此外,我将resource 变量设置为'*' 而不是event.methodArn,因为在缓存授权时,缓存的策略仅匹配到达的第一个端点,而下一次调用其他端点会导致错误:user not authorized for the method requested

其他更改在template.yaml

Resources:
  SomeAPI:
    Type: AWS::Serverless::Api
      Auth:
        DefaultAuthorizer: MyLambdaTokenAuthorizer
        AddDefaultAuthorizerToCorsPreflight: false
        Authorizers:
          MyLambdaTokenAuthorizer:
            FunctionArn: !GetAtt AuthorizerFunction.Arn
            Identity:
              Header: Authorization
              ValidationExpression: Bearer.*

AddDefaultAuthorizerToCorsPreflightfalse 值进行预检调用 (OPTIONS),不被验证,因为预检请求是由浏览器完成的,否则来自 Axios 的所有调用也会失败。

【讨论】:

    【解决方案2】:

    我也有同样的事情。 如果没有调用授权者 Lambda,我认为是因为 auth 被缓存或配置认为请求中没有身份材料。

    查看https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/api_gateway_authorizer#identity_source 设置:只有某些标头被认为包含身份材料,它们被用作缓存键

    【讨论】:

      猜你喜欢
      • 2020-03-17
      • 1970-01-01
      • 1970-01-01
      • 2020-09-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-20
      • 1970-01-01
      相关资源
      最近更新 更多