Skip to content

fix: ApiKeyRequired is not working as expected in api event when there is no Auth section for the Api #1328

@ShreyaGangishetty

Description

@ShreyaGangishetty

Description:
SAM throws a 5xx exception when there is no Auth section for API and ApiKeyRequired is set at the function level.

Steps to reproduce the issue:

  1. Deploy the below template using SAM
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31

Resources:
  MyFunctionWithApiKeyRequired:
    Type: AWS::Serverless::Function
    Properties:
      InlineCode: |
         exports.handler = async (event) => {
          console.log("Hello from MyAuthFunction")
           return {
           statusCode: 200,
           body: JSON.stringify(event),
           headers: {}
           }
         }
      Handler: index.handler
      Runtime: nodejs8.10
      Events:
        MyApiWithApiKeyRequired:
          Type: Api
          Properties:
            Path: /hello
            Method: get
            Auth:
              ApiKeyRequired: false

Observed result:

Traceback (most recent call last):
  File "/var/task/handlers.py", line 67, in transform
    output_fragment = translator.translate(copy.deepcopy(input_fragment), parameter_values=parameter_values)
  File "/var/task/samtranslator/translator/translator.py", line 83, in translate
    translated = macro.to_cloudformation(**kwargs)
  File "/var/task/samtranslator/model/sam_resources.py", line 133, in to_cloudformation
    lambda_alias=lambda_alias)
  File "/var/task/samtranslator/model/sam_resources.py", line 357, in _generate_event_resources
    resources += eventsource.to_cloudformation(**kwargs)
  File "/var/task/samtranslator/model/eventsources/push.py", line 534, in to_cloudformation
    self._add_swagger_integration(explicit_api, function)
  File "/var/task/samtranslator/model/eventsources/push.py", line 635, in _add_swagger_integration
    if apikey_required_setting_is_false and not api_auth.get('ApiKeyRequired'):
AttributeError: 'NoneType' object has no attribute 'get'

Expected result:
throw an ERROR for ApiKeyRequired:false if there is no ApiKeyRequired:true set for the api and set the required auth in security for the function path if ApiKeyRequired:true at function level and not present 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