Skip to content

Commit d16cd21

Browse files
authored
fix: Check type of resource type is a string (#2252)
* Check type of resource type is a string * Condense logic * Black reformat
1 parent d2cf4b7 commit d16cd21

File tree

6 files changed

+217
-1
lines changed

6 files changed

+217
-1
lines changed

samtranslator/model/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -489,7 +489,7 @@ def __init__(self, *modules):
489489
self.resource_types[resource_class.resource_type] = resource_class
490490

491491
def can_resolve(self, resource_dict):
492-
if not isinstance(resource_dict, dict) or "Type" not in resource_dict:
492+
if not isinstance(resource_dict, dict) or not isinstance(resource_dict.get("Type"), string_types):
493493
return False
494494

495495
return resource_dict["Type"] in self.resource_types
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Resources:
2+
FunctionInvalid:
3+
Type:
4+
AWS::Serverless::Function: invalid_field
5+
Properties:
6+
CodeUri: s3://sam-demo-bucket/member_portal.zip
7+
Handler: index.gethtml
8+
Runtime: nodejs12.x
9+
FunctionValid:
10+
Type: AWS::Serverless::Function
11+
Properties:
12+
CodeUri: s3://sam-demo-bucket/member_portal.zip
13+
Handler: index.gethtml
14+
Runtime: nodejs12.x
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{
2+
"Resources": {
3+
"FunctionInvalid": {
4+
"Type": {
5+
"AWS::Serverless::Function": "invalid_field"
6+
},
7+
"Properties": {
8+
"CodeUri": "s3://sam-demo-bucket/member_portal.zip",
9+
"Handler": "index.gethtml",
10+
"Runtime": "nodejs12.x"
11+
}
12+
},
13+
"FunctionValid": {
14+
"Type": "AWS::Lambda::Function",
15+
"Properties": {
16+
"Code": {
17+
"S3Bucket": "sam-demo-bucket",
18+
"S3Key": "member_portal.zip"
19+
},
20+
"Handler": "index.gethtml",
21+
"Role": {
22+
"Fn::GetAtt": [
23+
"FunctionValidRole",
24+
"Arn"
25+
]
26+
},
27+
"Runtime": "nodejs12.x",
28+
"Tags": [
29+
{
30+
"Key": "lambda:createdBy",
31+
"Value": "SAM"
32+
}
33+
]
34+
}
35+
},
36+
"FunctionValidRole": {
37+
"Type": "AWS::IAM::Role",
38+
"Properties": {
39+
"AssumeRolePolicyDocument": {
40+
"Version": "2012-10-17",
41+
"Statement": [
42+
{
43+
"Action": [
44+
"sts:AssumeRole"
45+
],
46+
"Effect": "Allow",
47+
"Principal": {
48+
"Service": [
49+
"lambda.amazonaws.com"
50+
]
51+
}
52+
}
53+
]
54+
},
55+
"ManagedPolicyArns": [
56+
"arn:aws-cn:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
57+
],
58+
"Tags": [
59+
{
60+
"Key": "lambda:createdBy",
61+
"Value": "SAM"
62+
}
63+
]
64+
}
65+
}
66+
}
67+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{
2+
"Resources": {
3+
"FunctionInvalid": {
4+
"Type": {
5+
"AWS::Serverless::Function": "invalid_field"
6+
},
7+
"Properties": {
8+
"CodeUri": "s3://sam-demo-bucket/member_portal.zip",
9+
"Handler": "index.gethtml",
10+
"Runtime": "nodejs12.x"
11+
}
12+
},
13+
"FunctionValid": {
14+
"Type": "AWS::Lambda::Function",
15+
"Properties": {
16+
"Code": {
17+
"S3Bucket": "sam-demo-bucket",
18+
"S3Key": "member_portal.zip"
19+
},
20+
"Handler": "index.gethtml",
21+
"Role": {
22+
"Fn::GetAtt": [
23+
"FunctionValidRole",
24+
"Arn"
25+
]
26+
},
27+
"Runtime": "nodejs12.x",
28+
"Tags": [
29+
{
30+
"Key": "lambda:createdBy",
31+
"Value": "SAM"
32+
}
33+
]
34+
}
35+
},
36+
"FunctionValidRole": {
37+
"Type": "AWS::IAM::Role",
38+
"Properties": {
39+
"AssumeRolePolicyDocument": {
40+
"Version": "2012-10-17",
41+
"Statement": [
42+
{
43+
"Action": [
44+
"sts:AssumeRole"
45+
],
46+
"Effect": "Allow",
47+
"Principal": {
48+
"Service": [
49+
"lambda.amazonaws.com"
50+
]
51+
}
52+
}
53+
]
54+
},
55+
"ManagedPolicyArns": [
56+
"arn:aws-us-gov:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
57+
],
58+
"Tags": [
59+
{
60+
"Key": "lambda:createdBy",
61+
"Value": "SAM"
62+
}
63+
]
64+
}
65+
}
66+
}
67+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
{
2+
"Resources": {
3+
"FunctionInvalid": {
4+
"Type": {
5+
"AWS::Serverless::Function": "invalid_field"
6+
},
7+
"Properties": {
8+
"CodeUri": "s3://sam-demo-bucket/member_portal.zip",
9+
"Handler": "index.gethtml",
10+
"Runtime": "nodejs12.x"
11+
}
12+
},
13+
"FunctionValid": {
14+
"Type": "AWS::Lambda::Function",
15+
"Properties": {
16+
"Code": {
17+
"S3Bucket": "sam-demo-bucket",
18+
"S3Key": "member_portal.zip"
19+
},
20+
"Handler": "index.gethtml",
21+
"Role": {
22+
"Fn::GetAtt": [
23+
"FunctionValidRole",
24+
"Arn"
25+
]
26+
},
27+
"Runtime": "nodejs12.x",
28+
"Tags": [
29+
{
30+
"Key": "lambda:createdBy",
31+
"Value": "SAM"
32+
}
33+
]
34+
}
35+
},
36+
"FunctionValidRole": {
37+
"Type": "AWS::IAM::Role",
38+
"Properties": {
39+
"AssumeRolePolicyDocument": {
40+
"Version": "2012-10-17",
41+
"Statement": [
42+
{
43+
"Action": [
44+
"sts:AssumeRole"
45+
],
46+
"Effect": "Allow",
47+
"Principal": {
48+
"Service": [
49+
"lambda.amazonaws.com"
50+
]
51+
}
52+
}
53+
]
54+
},
55+
"ManagedPolicyArns": [
56+
"arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
57+
],
58+
"Tags": [
59+
{
60+
"Key": "lambda:createdBy",
61+
"Value": "SAM"
62+
}
63+
]
64+
}
65+
}
66+
}
67+
}

tests/translator/test_translator.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -282,6 +282,7 @@ class TestTranslatorEndToEnd(AbstractTestTranslator):
282282
"function_with_mq_virtual_host",
283283
"simpletable",
284284
"simpletable_with_sse",
285+
"resource_with_invalid_type",
285286
"implicit_api",
286287
"explicit_api",
287288
"api_description",

0 commit comments

Comments
 (0)