Skip to content

fix: throw proper errors when Authorizer: NONE is set in api event and there are no authorizers defined in api #1240

@ShreyaGangishetty

Description

@ShreyaGangishetty

Description:
If there is no Authorizer for AWS::Serverless::Api resource and you set Authorizer: NONE at api event level sam doesn't throw proper errors.

Sample Template:

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function
    Properties:
      InlineCode: |
        def handler (event, context):
          print("hello") 
      Handler: index.handler
      Runtime: python3.7
      Events:
        HttpGetUserGroupIdUserId:
          Type: Api
          Properties:
            Path: '/users/{groupId}/{userId}'
            Method: GET
            Auth:
              Authorizer: "NONE"

Observed result:

Traceback (most recent call last):
  File "bin/sam-translate.py", line 141, in <module>
    transform_template(input_file_path, output_file_path)
  File "bin/sam-translate.py", line 98, in transform_template
    sam_template, {}, ManagedPolicyLoader(iam_client))
  File "/Users/shreygan/git/serverless-application-model/bin/../samtranslator/translator/transform.py", line 16, in transform
    return translator.translate(input_fragment, parameter_values=parameter_values)
  File "/Users/shreygan/git/serverless-application-model/bin/../samtranslator/translator/translator.py", line 120, in translate
    translated = macro.to_cloudformation(**kwargs)
  File "/Users/shreygan/git/serverless-application-model/bin/../samtranslator/model/sam_resources.py", line 125, in to_cloudformation
    lambda_alias=lambda_alias)
  File "/Users/shreygan/git/serverless-application-model/bin/../samtranslator/model/sam_resources.py", line 335, in _generate_event_resources
    resources += eventsource.to_cloudformation(**kwargs)
  File "/Users/shreygan/git/serverless-application-model/bin/../samtranslator/model/eventsources/push.py", line 534, in to_cloudformation
    self._add_swagger_integration(explicit_api, function)
  File "/Users/shreygan/git/serverless-application-model/bin/../samtranslator/model/eventsources/push.py", line 626, in _add_swagger_integration
    if method_authorizer == 'NONE' and not api_auth.get('DefaultAuthorizer'):
AttributeError: 'NoneType' object has no attribute 'get'

Expected result:
Throw a proper error saying Auth cannot be set to NONE in Api Event when there is no DefaultAuthorizer/Authorizer at Api level

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions