【问题标题】:SAM Local doesn't appear to be running Authorizer functionsSAM Local 似乎没有运行 Authorizer 功能
【发布时间】:2019-12-29 03:32:37
【问题描述】:

我刚刚开始使用SAM Local,但在尝试为我的端点配置授权器功能时又遇到了一个问题。

我一直在查看 main SAM documentation 以了解如何设置 Auth 函数,但每当我尝试使用 sam local start-api 在本地运行 API 时,它运行良好,但好像它甚至没有尝试运行身份验证函数。

我尝试在 Global.API 中定义 Auth 以及在 SAM 的 template.yaml

的 Resources 部分中定义 API 资源
# template.yaml
Globals:
  Function:
    Timeout: 3
    CodeUri: src/
    Runtime: nodejs8.10
  Api:
    Auth:                        # Option #1: Defining it globally
      DefaultAuthorizer: CustomJWTAuthorizer
      Authorizers:
        CustomJWTAuthorizer:
          FunctionArn: !GetAtt AuthFunction.Arn    
Resources:
  UserApi:
    Auth:                        # Option #2: Defining it as an API resource
      Authorizers:
        MyLambdaTokenAuth:
          FunctionPayloadType: TOKEN
          FunctionArn: !GetAtt AuthFunction.Arn
      DefaultAuthorizer: MyLambdaTokenAuth
  GetUserFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: handler.getUser
      Events:
        GetUser:
          Type: Api
          Properties:
            Path: /users/{userId}
            Method: get
            Auth:                    # Option #3: Define it on the function level
              Authorizer: AuthFunction
            RestApiId:
                Ref: UserApi
  AuthFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: handler.authorize

我尝试将事件打印到控制台,并且可以看到 event.requestContext 只是填充了虚拟数据,而不是在实时推送时传递它:

  // console.log(event)

  ...  
  resource: '/users/{userId}', 
  requestContext:     { resourceId: '123456',
     apiId: '1234567890',
     resourcePath: '/users/{userId}',
     httpMethod: 'GET',
     requestId: 'c6af9ac6-7b61-11e6-9a41-93e8deadbeef',
     accountId: '123456789012',
     stage: null,
     identity: 
      { apiKey: null,
        userArn: null,
        cognitoAuthenticationType: null,
        caller: null,
        userAgent: 'Custom User Agent String',
        user: null,
        cognitoIdentityPoolId: null,
        cognitoAuthenticationProvider: null,
        sourceIp: '127.0.0.1',
        accountId: null },
   extendedRequestId: null,
   path: '/users/{userId}' },
   ...

【问题讨论】:

    标签: localhost aws-sam-cli aws-sam


    【解决方案1】:

    不幸的是,在本地运行代码时,AWS SAM CLI 尚不支持授权方。但是有一个开放的功能请求来添加对它的支持:https://github.com/awslabs/aws-sam-cli/issues/137

    【讨论】:

      【解决方案2】:

      当您现在在本地运行时,SAM 不支持自定义授权方(SAM 版本 0.48)。所以你不能使用“sam local start-api”来测试它。

      但它支持 SAM YAML 模板,您可以使用 SAM CLI 构建自定义授权方并将其部署到您的 AWS 云 Api 网关。如果您的 YAML 设置正确,它可以正常工作。

      【讨论】:

        猜你喜欢
        • 2021-12-22
        • 1970-01-01
        • 1970-01-01
        • 2015-09-22
        • 2021-11-27
        • 1970-01-01
        • 2022-12-04
        • 2022-10-07
        • 1970-01-01
        相关资源
        最近更新 更多