diff --git a/samtranslator/model/eventsources/push.py b/samtranslator/model/eventsources/push.py index 6473c945a..ba0fcdd4c 100644 --- a/samtranslator/model/eventsources/push.py +++ b/samtranslator/model/eventsources/push.py @@ -621,12 +621,13 @@ def _add_swagger_integration(self, api, function): 'because it wasn\'t defined in the API\'s Authorizers.'.format( authorizer=method_authorizer, method=self.Method, path=self.Path)) - if method_authorizer == 'NONE' and not api_auth.get('DefaultAuthorizer'): - raise InvalidEventException( - self.relative_id, - 'Unable to set Authorizer on API method [{method}] for path [{path}] because \'NONE\' ' - 'is only a valid value when a DefaultAuthorizer on the API is specified.'.format( - method=self.Method, path=self.Path)) + if method_authorizer == 'NONE': + if not api_auth or not api_auth.get('DefaultAuthorizer'): + raise InvalidEventException( + self.relative_id, + 'Unable to set Authorizer on API method [{method}] for path [{path}] because \'NONE\' ' + 'is only a valid value when a DefaultAuthorizer on the API is specified.'.format( + method=self.Method, path=self.Path)) apikey_required_setting = self.Auth.get('ApiKeyRequired') apikey_required_setting_is_false = apikey_required_setting is not None and not apikey_required_setting diff --git a/tests/translator/input/error_function_with_method_auth_and_no_api_auth.yaml b/tests/translator/input/error_function_with_method_auth_and_no_api_auth.yaml new file mode 100644 index 000000000..f7836db46 --- /dev/null +++ b/tests/translator/input/error_function_with_method_auth_and_no_api_auth.yaml @@ -0,0 +1,15 @@ +Resources: + HelloWorldFunction: + Type: AWS::Serverless::Function + Properties: + CodeUri: s3://sam-demo-bucket/hello.zip + Handler: index.handler + Runtime: python3.7 + Events: + HttpGetUserGroupIdUserId: + Type: Api + Properties: + Path: '/users/{groupId}/{userId}' + Method: GET + Auth: + Authorizer: "NONE" \ No newline at end of file diff --git a/tests/translator/output/error_function_with_method_auth_and_no_api_auth.json b/tests/translator/output/error_function_with_method_auth_and_no_api_auth.json new file mode 100644 index 000000000..db69ac7d0 --- /dev/null +++ b/tests/translator/output/error_function_with_method_auth_and_no_api_auth.json @@ -0,0 +1,4 @@ +{ + "errors": [{"errorMessage": "Resource with id [HelloWorldFunction] is invalid. Event with id [HttpGetUserGroupIdUserId] is invalid. Unable to set Authorizer on API method [get] for path [/users/{groupId}/{userId}] because 'NONE' is only a valid value when a DefaultAuthorizer on the API is specified."}], + "errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [HelloWorldFunction] is invalid. Event with id [HttpGetUserGroupIdUserId] is invalid. Unable to set Authorizer on API method [get] for path [/users/{groupId}/{userId}] because 'NONE' is only a valid value when a DefaultAuthorizer on the API is specified." +} \ No newline at end of file diff --git a/tests/translator/test_translator.py b/tests/translator/test_translator.py index 76c14ac9c..eb3b44314 100644 --- a/tests/translator/test_translator.py +++ b/tests/translator/test_translator.py @@ -549,7 +549,8 @@ def _generate_new_deployment_hash(self, logical_id, dict_to_hash, rest_api_to_sw 'error_api_with_invalid_open_api_version_type', 'error_api_with_custom_domains_invalid', 'error_api_with_custom_domains_route53_invalid', - 'error_api_event_import_vaule_reference' + 'error_api_event_import_vaule_reference', + 'error_function_with_method_auth_and_no_api_auth' ]) @patch('boto3.session.Session.region_name', 'ap-southeast-1') @patch('samtranslator.plugins.application.serverless_app_plugin.ServerlessAppPlugin._sar_service_call', mock_sar_service_call)