Skip to content

Commit 4dfb963

Browse files
authored
Add validation for swagger security when ApiKeyRequired is true (#2456)
1 parent 90b55e5 commit 4dfb963

File tree

4 files changed

+60
-3
lines changed

4 files changed

+60
-3
lines changed

samtranslator/swagger/swagger.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,10 @@ def set_path_default_authorizer(
647647
# We want to ensure only a single Authorizer security entry exists while keeping everything else
648648
for security in existing_security:
649649
SwaggerEditor.validate_is_dict(
650-
security, "{} in Security for path {} is not a valid dictionary.".format(security, path)
650+
security,
651+
"{} in Security for path {} method {} is not a valid dictionary.".format(
652+
security, path, method_name
653+
),
651654
)
652655
if authorizer_names.isdisjoint(security.keys()):
653656
existing_non_authorizer_security.append(security)
@@ -703,7 +706,7 @@ def set_path_default_apikey_required(self, path):
703706
:param string path: Path name
704707
"""
705708

706-
for _, method_definition in self.iter_on_all_methods_for_path(path):
709+
for method_name, method_definition in self.iter_on_all_methods_for_path(path):
707710
existing_security = method_definition.get("security", [])
708711
apikey_security_names = set(["api_key", "api_key_false"])
709712
existing_non_apikey_security = []
@@ -714,6 +717,12 @@ def set_path_default_apikey_required(self, path):
714717
# (e.g. sigv4 (AWS_IAM), authorizers, NONE (marker for ignoring default authorizer))
715718
# We want to ensure only a single ApiKey security entry exists while keeping everything else
716719
for security in existing_security:
720+
SwaggerEditor.validate_is_dict(
721+
security,
722+
"{} in Security for path {} method {} is not a valid dictionary.".format(
723+
security, path, method_name
724+
),
725+
)
717726
if apikey_security_names.isdisjoint(security.keys()):
718727
existing_non_apikey_security.append(security)
719728
else:
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
transformId: AWS::Serverless-2016-10-31
2+
AWSTemplateFormatVersion: '2010-09-09'
3+
Resources:
4+
AuthFunction:
5+
Type: AWS::Serverless::Function
6+
AccessingPartyAPI:
7+
Type: AWS::Serverless::Api
8+
Properties:
9+
EndpointConfiguration: REGIONAL
10+
StageName: demo
11+
Auth:
12+
ApiKeyRequired: true
13+
14+
DefinitionBody:
15+
paths:
16+
"/path":
17+
put:
18+
responses:
19+
'201':
20+
content:
21+
application/json:
22+
schema:
23+
"$ref": "abcd"
24+
x-amazon-apigateway-integration:
25+
contentHandling: CONVERT_TO_TEXT
26+
responses:
27+
default:
28+
statusCode: '200'
29+
uri:
30+
Fn::Sub: foobar
31+
httpMethod: POST
32+
passthroughBehavior: when_no_match
33+
type: aws_proxy
34+
requestBody:
35+
content:
36+
application/json:
37+
schema:
38+
required:
39+
- readoutId
40+
- status
41+
type: object
42+
security:
43+
- []
44+
45+
openapi: 3.0.3
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{
2-
"errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Structure of the SAM template is invalid. CustomAuthorizer in Security for path /path is not a valid dictionary."
2+
"errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Structure of the SAM template is invalid. CustomAuthorizer in Security for path /path method put is not a valid dictionary."
33
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"errorMessage": "Invalid Serverless Application Specification document. Number of errors found: 1. Structure of the SAM template is invalid. [] in Security for path /path method put is not a valid dictionary."
3+
}

0 commit comments

Comments
 (0)