通过AWS SAM模板为API Gateway中的一个方法资源终端节点禁用安全性

时间:2020-01-08 15:36:57

标签: amazon-web-services swagger amazon-cloudformation aws-api-gateway aws-serverless

我正在使用AWS Serverless创建具有Lambda函数支持的API网关。

我定义了以下资源和方法:

/projects
   -> GET (should require API key)
   -> OPTIONS (should not, since it is used for CORS preflight)

我在CORS方面遇到问题,需要API密钥。前端客户端代码在启动预检CORS 403 Forbidden请求时收到OPTIONS错误,因为AWS管理控制台中的API Key Required设置为{{ 1}}方法。

我想专门针对True请求禁用安全性,但保留所有其他方法(OPTIONSOPTIONS等)的安全性。这是我的资源定义(您可以看到我在GET对象中设置了默认的POST

ApiKeyRequired: true

我知道Swagger documentation说我可以通过为每个资源方法添加一个Auth对象来覆盖安全性。此SO post还建议我可以通过将 MyApi: Type: 'AWS::Serverless::Api' Name: MyApi Properties: Auth: AddDefaultAuthorizerToCorsPreflight: true ApiKeyRequired: true # sets for all methods Cors: AllowCredentials: true AllowHeaders: '"Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token"' AllowMethods: '"POST,GET,OPTION"' AllowOrigin: '"*"' MaxAge: '"600"' StageName: !Ref StageName DefinitionBody: swagger: 2.0 info: title: !Sub API-Lambda-${StageName} description: "API for MyApi" version: "1.0.0" paths: /projects: get: produces: - application/json responses: "200": description: OK x-amazon-apigateway-any-method: produces: - application/json x-amazon-apigateway-integration: httpMethod: post type: aws_proxy uri: Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${GetAllProjectsFunction.Arn}/invocations options: consumes: - application/json produces: - application/json responses: '200': description: 200 response headers: Access-Control-Allow-Origin: type: string Access-Control-Allow-Methods: type: string Access-Control-Allow-Headers: type: string x-amazon-apigateway-integration: responses: default: statusCode: 200 responseParameters: method.response.header.Access-Control-Allow-Methods: "'DELETE,GET,HEAD,OPTIONS,PATCH,POST,PUT'" method.response.header.Access-Control-Allow-Headers: "'Content-Type,mode,Authorization,X-Amz-Date,X-Api-Key,X-Amz-Security-Token'" method.response.header.Access-Control-Allow-Origin: "'*'" passthroughBehavior: when_no_match requestTemplates: application/json: "{\"statusCode\": 200}" type: mock /projects/{userId}: get: responses: "200": description: OK x-amazon-apigateway-any-method: produces: - application/json x-amazon-apigateway-integration: httpMethod: post type: aws_proxy uri: Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${GetProjectsForUserFunction.Arn}/invocations options: consumes: - application/json responses: '200': description: 200 response headers: Access-Control-Allow-Origin: type: string Access-Control-Allow-Methods: type: string Access-Control-Allow-Headers: type: string x-amazon-apigateway-integration: responses: default: statusCode: 200 responseParameters: method.response.header.Access-Control-Allow-Methods: "'DELETE,GET,HEAD,OPTIONS,PATCH,POST,PUT'" method.response.header.Access-Control-Allow-Headers: "'Content-Type,mode,Authorization,X-Amz-Date,X-Api-Key,X-Amz-Security-Token'" method.response.header.Access-Control-Allow-Origin: "'*'" passthroughBehavior: when_no_match requestTemplates: application/json: "{\"statusCode\": 200}" type: mock 对象设置为空列表来禁用安全性。

但是,我尝试了以下方法:

security

还要简单地使security为None对象:

        options:
          consumes:
            - application/json
          produces:
            - application/json
          security:
            -
          responses: ...

在两种情况下,尝试使用security进行部署时都会收到以下错误:

正在等待更改集的创建。错误:无法创建 堆栈的变更集:my-app,例如:服务员ChangeSetCreateComplete 失败:服务员遇到终端失败状态,状态:失败。 原因:转换AWS :: Serverless-2016-10-31失败,原因:内部 转换失败。

我的 options: consumes: - application/json produces: - application/json security: responses: ... 定义似乎是错误的。如何为一种资源方法(即aws sam deploy方法)禁用安全性?

更新:

我使用以下语法来部署模板:

security

但是,即使在部署之后,我的控制台中仍然有此内容:

enter image description here

老实说,我现在很茫然,因为使用常规OPTIONS资源(只需将 options: consumes: - application/json produces: - application/json security: - {} responses: 设置为true)很容易做到这一点。

2 个答案:

答案 0 :(得分:1)

不好,但是我认为您必须在每个OPTIONS方法上禁用api_key -使用openapi提供方法的定义,并在其中跳过/忽略“安全性”键

答案 1 :(得分:0)

您可以简单地设置AddDefaultAuthorizerToCorsPreflight: false,这将导致OPTIONS请求不受保护。

请参阅文档的这一部分:

如果设置了DefaultAuthorizer和Cors属性,则设置AddDefaultAuthorizerToCorsPreflight将导致默认授权者被添加到OpenAPI部分的Options属性中。

参考:https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-property-api-apiauth.html