Skip to content

Commit 35416f6

Browse files
xrendankeetonian
authored andcommitted
fix: fix CORS options method when DefaultAuthorizer is used (#958)
1 parent 1bb186c commit 35416f6

11 files changed

+2202
-9
lines changed

samtranslator/model/api/api_generator.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020
# Default the Cors Properties to '*' wildcard and False AllowCredentials. Other properties are actually Optional
2121
CorsProperties.__new__.__defaults__ = (None, None, _CORS_WILDCARD, None, False)
2222

23-
AuthProperties = namedtuple("_AuthProperties", ["Authorizers", "DefaultAuthorizer", "InvokeRole"])
24-
AuthProperties.__new__.__defaults__ = (None, None, None)
23+
AuthProperties = namedtuple("_AuthProperties",
24+
["Authorizers", "DefaultAuthorizer", "InvokeRole", "AddDefaultAuthorizerToCorsPreflight"])
25+
AuthProperties.__new__.__defaults__ = (None, None, None, True)
2526

2627
GatewayResponseProperties = ["ResponseParameters", "ResponseTemplates", "StatusCode"]
2728

@@ -308,7 +309,8 @@ def _add_auth(self):
308309

309310
if authorizers:
310311
swagger_editor.add_authorizers(authorizers)
311-
self._set_default_authorizer(swagger_editor, authorizers, auth_properties.DefaultAuthorizer)
312+
self._set_default_authorizer(swagger_editor, authorizers, auth_properties.DefaultAuthorizer,
313+
auth_properties.AddDefaultAuthorizerToCorsPreflight)
312314

313315
# Assign the Swagger back to template
314316

@@ -508,7 +510,8 @@ def _construct_authorizer_lambda_permission(self):
508510

509511
return permissions
510512

511-
def _set_default_authorizer(self, swagger_editor, authorizers, default_authorizer):
513+
def _set_default_authorizer(self, swagger_editor, authorizers, default_authorizer,
514+
add_default_auth_to_preflight=True):
512515
if not default_authorizer:
513516
return
514517

@@ -517,7 +520,8 @@ def _set_default_authorizer(self, swagger_editor, authorizers, default_authorize
517520
default_authorizer + "' was not defined in 'Authorizers'")
518521

519522
for path in swagger_editor.iter_on_path():
520-
swagger_editor.set_path_default_authorizer(path, default_authorizer, authorizers=authorizers)
523+
swagger_editor.set_path_default_authorizer(path, default_authorizer, authorizers=authorizers,
524+
add_default_auth_to_preflight=add_default_auth_to_preflight)
521525

522526
def _set_endpoint_configuration(self, rest_api, value):
523527
"""

samtranslator/swagger/swagger.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,8 @@ def add_authorizers(self, authorizers):
400400
for authorizer_name, authorizer in authorizers.items():
401401
self.security_definitions[authorizer_name] = authorizer.generate_swagger()
402402

403-
def set_path_default_authorizer(self, path, default_authorizer, authorizers):
403+
def set_path_default_authorizer(self, path, default_authorizer, authorizers,
404+
add_default_auth_to_preflight=True):
404405
"""
405406
Sets the DefaultAuthorizer for each method on this path. The DefaultAuthorizer won't be set if an Authorizer
406407
was defined at the Function/Path/Method level
@@ -409,14 +410,18 @@ def set_path_default_authorizer(self, path, default_authorizer, authorizers):
409410
:param string default_authorizer: Name of the authorizer to use as the default. Must be a key in the
410411
authorizers param.
411412
:param list authorizers: List of Authorizer configurations defined on the related Api.
413+
:param bool add_default_auth_to_preflight: Bool of whether to add the default
414+
authorizer to OPTIONS preflight requests.
412415
"""
413416

414417
for method_name, method in self.get_path(path).items():
418+
normalized_method_name = self._normalize_method_name(method_name)
415419
# Excluding paramters section
416-
if method_name == "parameters":
420+
if normalized_method_name == "parameters":
417421
continue
418-
self.set_method_authorizer(path, method_name, default_authorizer, authorizers,
419-
default_authorizer=default_authorizer, is_default=True)
422+
if add_default_auth_to_preflight or normalized_method_name != "options":
423+
self.set_method_authorizer(path, method_name, default_authorizer, authorizers,
424+
default_authorizer=default_authorizer, is_default=True)
420425

421426
def add_auth_to_method(self, path, method_name, auth, api):
422427
"""
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
Globals:
2+
Api:
3+
Cors: "origins"
4+
5+
Resources:
6+
ApiFunction:
7+
Type: AWS::Serverless::Function
8+
Properties:
9+
CodeUri: s3://sam-demo-bucket/member_portal.zip
10+
Handler: index.gethtml
11+
Runtime: nodejs4.3
12+
Events:
13+
GetHtml:
14+
Type: Api
15+
Properties:
16+
Path: /
17+
Method: get
18+
RestApiId: !Ref ServerlessApi
19+
20+
PostHtml:
21+
Type: Api
22+
Properties:
23+
Path: /
24+
Method: post
25+
RestApiId: !Ref ServerlessApi
26+
27+
28+
ServerlessApi:
29+
Type: AWS::Serverless::Api
30+
Properties:
31+
StageName: Prod
32+
Auth:
33+
AddDefaultAuthorizerToCorsPreflight: False
34+
DefaultAuthorizer: MyLambdaRequestAuth
35+
Authorizers:
36+
MyLambdaRequestAuth:
37+
FunctionPayloadType: REQUEST
38+
FunctionArn: !GetAtt MyAuthFn.Arn
39+
Identity:
40+
Headers:
41+
- Authorization1
42+
43+
MyAuthFn:
44+
Type: AWS::Serverless::Function
45+
Properties:
46+
CodeUri: s3://bucket/key
47+
Handler: index.handler
48+
Runtime: nodejs8.10
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
Globals:
2+
Api:
3+
Cors: "origins"
4+
5+
Resources:
6+
ApiFunction:
7+
Type: AWS::Serverless::Function
8+
Properties:
9+
CodeUri: s3://sam-demo-bucket/member_portal.zip
10+
Handler: index.gethtml
11+
Runtime: nodejs4.3
12+
Events:
13+
GetHtml:
14+
Type: Api
15+
Properties:
16+
Path: /
17+
Method: get
18+
RestApiId: !Ref ServerlessApi
19+
20+
PostHtml:
21+
Type: Api
22+
Properties:
23+
Path: /
24+
Method: post
25+
RestApiId: !Ref ServerlessApi
26+
27+
28+
ServerlessApi:
29+
Type: AWS::Serverless::Api
30+
Properties:
31+
StageName: Prod
32+
Auth:
33+
DefaultAuthorizer: MyLambdaRequestAuth
34+
Authorizers:
35+
MyLambdaRequestAuth:
36+
FunctionPayloadType: REQUEST
37+
FunctionArn: !GetAtt MyAuthFn.Arn
38+
Identity:
39+
Headers:
40+
- Authorization1
41+
42+
MyAuthFn:
43+
Type: AWS::Serverless::Function
44+
Properties:
45+
CodeUri: s3://bucket/key
46+
Handler: index.handler
47+
Runtime: nodejs8.10

0 commit comments

Comments
 (0)