Skip to content

Commit 68f1a8a

Browse files
praneetapShreya
authored andcommitted
fix: custom statements regression bug (#1183)
1 parent 5844286 commit 68f1a8a

File tree

5 files changed

+247
-33
lines changed

5 files changed

+247
-33
lines changed

samtranslator/swagger/swagger.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -994,7 +994,9 @@ def _add_custom_statement(self, custom_statements):
994994
statement = self.resource_policy['Statement']
995995
if not isinstance(statement, list):
996996
statement = [statement]
997-
statement.extend(custom_statements)
997+
for s in custom_statements:
998+
if s not in statement:
999+
statement.append(s)
9981000
self.resource_policy['Statement'] = statement
9991001

10001002
def add_request_parameters_to_method(self, path, method_name, request_parameters):

tests/translator/input/api_with_resource_policy.yaml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,21 @@ Resources:
2121
Properties:
2222
RestApiId:
2323
Ref: ExplicitApi
24-
Path: /
24+
Path: /one
2525
Method: get
26+
PostHtml:
27+
Type: Api
28+
Properties:
29+
RestApiId:
30+
Ref: ExplicitApi
31+
Path: /two
32+
Method: post
33+
PutHtml:
34+
Type: Api
35+
Properties:
36+
RestApiId:
37+
Ref: ExplicitApi
38+
Path: /three
39+
Method: put
2640

2741

tests/translator/output/api_with_resource_policy.json

Lines changed: 79 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,37 @@
2323
]
2424
}
2525
},
26+
"ExplicitApiDeploymentd2036decb3": {
27+
"Type": "AWS::ApiGateway::Deployment",
28+
"Properties": {
29+
"RestApiId": {
30+
"Ref": "ExplicitApi"
31+
},
32+
"Description": "RestApi deployment id: d2036decb36ce147b7738a97c419737ea7e02555",
33+
"StageName": "Stage"
34+
}
35+
},
36+
"ExplicitApiFunctionPutHtmlPermissionProd": {
37+
"Type": "AWS::Lambda::Permission",
38+
"Properties": {
39+
"Action": "lambda:invokeFunction",
40+
"Principal": "apigateway.amazonaws.com",
41+
"FunctionName": {
42+
"Ref": "ExplicitApiFunction"
43+
},
44+
"SourceArn": {
45+
"Fn::Sub": [
46+
"arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${__ApiId__}/${__Stage__}/PUT/three",
47+
{
48+
"__Stage__": "*",
49+
"__ApiId__": {
50+
"Ref": "ExplicitApi"
51+
}
52+
}
53+
]
54+
}
55+
}
56+
},
2657
"ExplicitApi": {
2758
"Type": "AWS::ApiGateway::RestApi",
2859
"Properties": {
@@ -34,7 +65,7 @@
3465
}
3566
},
3667
"paths": {
37-
"/": {
68+
"/one": {
3869
"get": {
3970
"x-amazon-apigateway-integration": {
4071
"httpMethod": "POST",
@@ -45,6 +76,30 @@
4576
},
4677
"responses": {}
4778
}
79+
},
80+
"/three": {
81+
"put": {
82+
"x-amazon-apigateway-integration": {
83+
"httpMethod": "POST",
84+
"type": "aws_proxy",
85+
"uri": {
86+
"Fn::Sub": "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${ExplicitApiFunction.Arn}/invocations"
87+
}
88+
},
89+
"responses": {}
90+
}
91+
},
92+
"/two": {
93+
"post": {
94+
"x-amazon-apigateway-integration": {
95+
"httpMethod": "POST",
96+
"type": "aws_proxy",
97+
"uri": {
98+
"Fn::Sub": "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${ExplicitApiFunction.Arn}/invocations"
99+
}
100+
},
101+
"responses": {}
102+
}
48103
}
49104
},
50105
"swagger": "2.0",
@@ -62,28 +117,39 @@
62117
}
63118
}
64119
},
65-
"ExplicitApiDeploymentcdee3f2ed1": {
66-
"Type": "AWS::ApiGateway::Deployment",
67-
"Properties": {
68-
"RestApiId": {
69-
"Ref": "ExplicitApi"
70-
},
71-
"Description": "RestApi deployment id: cdee3f2ed1733103d54a04f635e1acc120e714c1",
72-
"StageName": "Stage"
73-
}
74-
},
75120
"ExplicitApiProdStage": {
76121
"Type": "AWS::ApiGateway::Stage",
77122
"Properties": {
78123
"DeploymentId": {
79-
"Ref": "ExplicitApiDeploymentcdee3f2ed1"
124+
"Ref": "ExplicitApiDeploymentd2036decb3"
80125
},
81126
"RestApiId": {
82127
"Ref": "ExplicitApi"
83128
},
84129
"StageName": "Prod"
85130
}
86131
},
132+
"ExplicitApiFunctionPostHtmlPermissionProd": {
133+
"Type": "AWS::Lambda::Permission",
134+
"Properties": {
135+
"Action": "lambda:invokeFunction",
136+
"Principal": "apigateway.amazonaws.com",
137+
"FunctionName": {
138+
"Ref": "ExplicitApiFunction"
139+
},
140+
"SourceArn": {
141+
"Fn::Sub": [
142+
"arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${__ApiId__}/${__Stage__}/POST/two",
143+
{
144+
"__Stage__": "*",
145+
"__ApiId__": {
146+
"Ref": "ExplicitApi"
147+
}
148+
}
149+
]
150+
}
151+
}
152+
},
87153
"ExplicitApiFunctionRole": {
88154
"Type": "AWS::IAM::Role",
89155
"Properties": {
@@ -118,7 +184,7 @@
118184
},
119185
"SourceArn": {
120186
"Fn::Sub": [
121-
"arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${__ApiId__}/${__Stage__}/GET/",
187+
"arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${__ApiId__}/${__Stage__}/GET/one",
122188
{
123189
"__Stage__": "*",
124190
"__ApiId__": {

tests/translator/output/aws-cn/api_with_resource_policy.json

Lines changed: 79 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,37 @@
2323
]
2424
}
2525
},
26+
"ExplicitApiFunctionPutHtmlPermissionProd": {
27+
"Type": "AWS::Lambda::Permission",
28+
"Properties": {
29+
"Action": "lambda:invokeFunction",
30+
"Principal": "apigateway.amazonaws.com",
31+
"FunctionName": {
32+
"Ref": "ExplicitApiFunction"
33+
},
34+
"SourceArn": {
35+
"Fn::Sub": [
36+
"arn:aws-cn:execute-api:${AWS::Region}:${AWS::AccountId}:${__ApiId__}/${__Stage__}/PUT/three",
37+
{
38+
"__Stage__": "*",
39+
"__ApiId__": {
40+
"Ref": "ExplicitApi"
41+
}
42+
}
43+
]
44+
}
45+
}
46+
},
47+
"ExplicitApiDeployment09f793ab58": {
48+
"Type": "AWS::ApiGateway::Deployment",
49+
"Properties": {
50+
"RestApiId": {
51+
"Ref": "ExplicitApi"
52+
},
53+
"Description": "RestApi deployment id: 09f793ab583fe684660829cf209f0e28340df577",
54+
"StageName": "Stage"
55+
}
56+
},
2657
"ExplicitApi": {
2758
"Type": "AWS::ApiGateway::RestApi",
2859
"Properties": {
@@ -34,7 +65,7 @@
3465
}
3566
},
3667
"paths": {
37-
"/": {
68+
"/one": {
3869
"get": {
3970
"x-amazon-apigateway-integration": {
4071
"httpMethod": "POST",
@@ -45,6 +76,30 @@
4576
},
4677
"responses": {}
4778
}
79+
},
80+
"/three": {
81+
"put": {
82+
"x-amazon-apigateway-integration": {
83+
"httpMethod": "POST",
84+
"type": "aws_proxy",
85+
"uri": {
86+
"Fn::Sub": "arn:aws-cn:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${ExplicitApiFunction.Arn}/invocations"
87+
}
88+
},
89+
"responses": {}
90+
}
91+
},
92+
"/two": {
93+
"post": {
94+
"x-amazon-apigateway-integration": {
95+
"httpMethod": "POST",
96+
"type": "aws_proxy",
97+
"uri": {
98+
"Fn::Sub": "arn:aws-cn:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${ExplicitApiFunction.Arn}/invocations"
99+
}
100+
},
101+
"responses": {}
102+
}
48103
}
49104
},
50105
"swagger": "2.0",
@@ -74,14 +129,35 @@
74129
"Type": "AWS::ApiGateway::Stage",
75130
"Properties": {
76131
"DeploymentId": {
77-
"Ref": "ExplicitApiDeployment3f65c799d9"
132+
"Ref": "ExplicitApiDeployment09f793ab58"
78133
},
79134
"RestApiId": {
80135
"Ref": "ExplicitApi"
81136
},
82137
"StageName": "Prod"
83138
}
84139
},
140+
"ExplicitApiFunctionPostHtmlPermissionProd": {
141+
"Type": "AWS::Lambda::Permission",
142+
"Properties": {
143+
"Action": "lambda:invokeFunction",
144+
"Principal": "apigateway.amazonaws.com",
145+
"FunctionName": {
146+
"Ref": "ExplicitApiFunction"
147+
},
148+
"SourceArn": {
149+
"Fn::Sub": [
150+
"arn:aws-cn:execute-api:${AWS::Region}:${AWS::AccountId}:${__ApiId__}/${__Stage__}/POST/two",
151+
{
152+
"__Stage__": "*",
153+
"__ApiId__": {
154+
"Ref": "ExplicitApi"
155+
}
156+
}
157+
]
158+
}
159+
}
160+
},
85161
"ExplicitApiFunctionRole": {
86162
"Type": "AWS::IAM::Role",
87163
"Properties": {
@@ -116,7 +192,7 @@
116192
},
117193
"SourceArn": {
118194
"Fn::Sub": [
119-
"arn:aws-cn:execute-api:${AWS::Region}:${AWS::AccountId}:${__ApiId__}/${__Stage__}/GET/",
195+
"arn:aws-cn:execute-api:${AWS::Region}:${AWS::AccountId}:${__ApiId__}/${__Stage__}/GET/one",
120196
{
121197
"__Stage__": "*",
122198
"__ApiId__": {
@@ -126,16 +202,6 @@
126202
]
127203
}
128204
}
129-
},
130-
"ExplicitApiDeployment3f65c799d9": {
131-
"Type": "AWS::ApiGateway::Deployment",
132-
"Properties": {
133-
"RestApiId": {
134-
"Ref": "ExplicitApi"
135-
},
136-
"Description": "RestApi deployment id: 3f65c799d9b1cd1ec9957134eb76169e7d79d54b",
137-
"StageName": "Stage"
138-
}
139205
}
140206
}
141207
}

0 commit comments

Comments
 (0)