Skip to content

Commit 1722d36

Browse files
Shreyapraneetap
authored andcommitted
fix: throw error for apikey false in api event when not defined in api (#1365)
1 parent 127195f commit 1722d36

File tree

4 files changed

+32
-1
lines changed

4 files changed

+32
-1
lines changed

samtranslator/model/eventsources/push.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -680,7 +680,7 @@ def _add_swagger_integration(self, api, function):
680680

681681
apikey_required_setting = self.Auth.get("ApiKeyRequired")
682682
apikey_required_setting_is_false = apikey_required_setting is not None and not apikey_required_setting
683-
if apikey_required_setting_is_false and not api_auth.get("ApiKeyRequired"):
683+
if apikey_required_setting_is_false and (not api_auth or not api_auth.get("ApiKeyRequired")):
684684
raise InvalidEventException(
685685
self.relative_id,
686686
"Unable to set ApiKeyRequired [False] on API method [{method}] for path [{path}] "
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
Resources:
2+
MyApiWithoutAuth:
3+
Type: "AWS::Serverless::Api"
4+
Properties:
5+
StageName: Prod
6+
OpenApiVersion: '3.0.1'
7+
8+
MyFunctionWithApiKeyRequired:
9+
Type: AWS::Serverless::Function
10+
Properties:
11+
CodeUri: s3://bucket/key
12+
Handler: index.handler
13+
Runtime: nodejs12.x
14+
Events:
15+
MyApiWithApiKeyRequired:
16+
Type: Api
17+
Properties:
18+
RestApiId: !Ref MyApiWithoutAuth
19+
Path: /ApiKeyRequiredTrue
20+
Method: get
21+
Auth:
22+
ApiKeyRequired: false
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"errors": [
3+
{
4+
"errorMessage": "Resource with id [MyFunctionWithApiKeyRequired] is invalid. Event with id [MyApiWithApiKeyRequired] is invalid. Unable to set ApiKeyRequired [False] on API method [get] for path [/ApiKeyRequiredTrue] because the related API does not specify any ApiKeyRequired."
5+
}
6+
],
7+
"errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Resource with id [MyFunctionWithApiKeyRequired] is invalid. Event with id [MyApiWithApiKeyRequired] is invalid. Unable to set ApiKeyRequired [False] on API method [get] for path [/ApiKeyRequiredTrue] because the related API does not specify any ApiKeyRequired."
8+
}

tests/translator/test_translator.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,7 @@ def _generate_new_deployment_hash(self, logical_id, dict_to_hash, rest_api_to_sw
603603
"error_http_api_event_multiple_same_path",
604604
"error_function_with_event_dest_invalid",
605605
"error_function_with_event_dest_type",
606+
"error_function_with_api_key_false",
606607
],
607608
)
608609
@patch("boto3.session.Session.region_name", "ap-southeast-1")

0 commit comments

Comments
 (0)