Skip to content

Commit 09835af

Browse files
authored
Merge pull request #235 from adanaja/morosi-fix
Fix RestApi referencing $default in LambdaPermission
2 parents e887671 + 5ef82f6 commit 09835af

File tree

8 files changed

+45
-48
lines changed

8 files changed

+45
-48
lines changed

src/e3/aws/troposphere/apigateway/__init__.py

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
from troposphere import AWSObject
2424
from troposphere.certificatemanager import Certificate, DomainValidationOption
2525
import json
26-
import re
2726

2827
if TYPE_CHECKING:
2928
from e3.aws.troposphere import Stack
@@ -195,6 +194,9 @@ class _AliasTargetAttributes(TypedDict):
195194
DNSName: str
196195
HostedZoneId: str
197196

197+
# The default stage name
198+
DEFAULT_STAGE_NAME = "$default"
199+
198200
def __init__(
199201
self,
200202
name: str,
@@ -240,7 +242,9 @@ def __init__(
240242
self.authorizers: dict[str, Any] = {}
241243
# By default, make sure to have a $default stage
242244
self.stages_config = (
243-
stages_config if stages_config else [StageConfiguration("$default")]
245+
stages_config
246+
if stages_config
247+
else [StageConfiguration(self.DEFAULT_STAGE_NAME)]
244248
)
245249

246250
@cached_property
@@ -697,6 +701,9 @@ def resources(self, stack: Stack) -> list[AWSObject]:
697701
class RestApi(Api):
698702
"""Rest API support."""
699703

704+
# Apigateway v1 only allows a-zA-Z0-9_
705+
DEFAULT_STAGE_NAME = "default"
706+
700707
def __init__(
701708
self,
702709
name: str,
@@ -878,8 +885,7 @@ def declare_stage(
878885
DeploymentId=Ref(deployment_name),
879886
Description=f"stage {stage_name}",
880887
MethodSettings=[method_settings],
881-
# Stage name only allows a-zA-Z0-9_
882-
StageName=re.sub("[^a-zA-Z0-9_]", "", stage_name),
888+
StageName=stage_name,
883889
**parameters,
884890
)
885891
)
@@ -948,12 +954,7 @@ def _declare_method(
948954
for config in self.stages_config:
949955
result.append(
950956
awslambda.Permission(
951-
# Retain old behavior for the $default stage
952-
name_to_id(
953-
"{}-{}LambdaPermission".format(
954-
id_prefix, "" if config.name == "$default" else config.name
955-
)
956-
),
957+
name_to_id(f"{id_prefix}-{config.name}LambdaPermission"),
957958
Action="lambda:InvokeFunction",
958959
FunctionName=lambda_arn,
959960
Principal="apigateway.amazonaws.com",
@@ -1006,11 +1007,7 @@ def _declare_api_mapping(
10061007
apigateway.BasePathMapping(
10071008
# Retain old behavior for the $default stage
10081009
name_to_id(
1009-
"{}{}-{}BasePathMapping".format(
1010-
self.name,
1011-
domain_name.DomainName,
1012-
"" if config.name == "$default" else config.name,
1013-
)
1010+
f"{self.name}{domain_name.DomainName}-{config.name}BasePathMapping"
10141011
),
10151012
**mapping_params,
10161013
)

tests/tests_e3_aws/troposphere/apigateway/apigateway_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -529,7 +529,7 @@ def test_rest_api_stages(stack: Stack, lambda_fun: PyFunction) -> None:
529529
Method("ANY"),
530530
],
531531
stages_config=[
532-
StageConfiguration("$default"),
532+
StageConfiguration("default"),
533533
StageConfiguration(
534534
"beta", api_mapping_key="beta", variables={"somevar": "somevalue"}
535535
),
@@ -574,7 +574,7 @@ def test_rest_api_lambda_alias(stack: Stack, lambda_fun: PyFunction) -> None:
574574
],
575575
stages_config=[
576576
StageConfiguration(
577-
"$default",
577+
"default",
578578
lambda_arn_permission=lambda_aliases.blue.ref,
579579
variables={"lambdaAlias": lambda_aliases.blue.name},
580580
),
@@ -667,7 +667,7 @@ def test_rest_api_custom_domain_stages(stack: Stack, lambda_fun: PyFunction) ->
667667
Method("ANY", authorizer_name="testauthorizer"),
668668
],
669669
stages_config=[
670-
StageConfiguration("$default"),
670+
StageConfiguration("default"),
671671
StageConfiguration(
672672
"beta", api_mapping_key="beta", variables={"somevar": "somevalue"}
673673
),

tests/tests_e3_aws/troposphere/apigateway/apigatewayv1_test.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
},
4646
"TestapiDefaultDeployment": {
4747
"Properties": {
48-
"Description": "Deployment resource of $default stage",
48+
"Description": "Deployment resource of default stage",
4949
"RestApiId": {
5050
"Ref": "Testapi"
5151
}
@@ -72,7 +72,7 @@
7272
"DeploymentId": {
7373
"Ref": "TestapiDefaultDeployment"
7474
},
75-
"Description": "stage $default",
75+
"Description": "stage default",
7676
"MethodSettings": [
7777
{
7878
"ResourcePath": "/*",
@@ -116,7 +116,7 @@
116116
},
117117
"Type": "AWS::ApiGateway::Method"
118118
},
119-
"TestapiANYLambdaPermission": {
119+
"TestapiANYDefaultLambdaPermission": {
120120
"Properties": {
121121
"Action": "lambda:InvokeFunction",
122122
"FunctionName": {
@@ -125,7 +125,7 @@
125125
"Principal": "apigateway.amazonaws.com",
126126
"SourceArn": {
127127
"Fn::Sub": [
128-
"arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${api}/$default/${method}/*",
128+
"arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${api}/default/${method}/*",
129129
{
130130
"api": {
131131
"Ref": "Testapi"

tests/tests_e3_aws/troposphere/apigateway/apigatewayv1_test_custom_domain.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
},
7272
"TestapiDefaultDeployment": {
7373
"Properties": {
74-
"Description": "Deployment resource of $default stage",
74+
"Description": "Deployment resource of default stage",
7575
"RestApiId": {
7676
"Ref": "Testapi"
7777
}
@@ -98,7 +98,7 @@
9898
"DeploymentId": {
9999
"Ref": "TestapiDefaultDeployment"
100100
},
101-
"Description": "stage $default",
101+
"Description": "stage default",
102102
"MethodSettings": [
103103
{
104104
"ResourcePath": "/*",
@@ -145,7 +145,7 @@
145145
},
146146
"Type": "AWS::ApiGateway::Method"
147147
},
148-
"TestapiANYLambdaPermission": {
148+
"TestapiANYDefaultLambdaPermission": {
149149
"Properties": {
150150
"Action": "lambda:InvokeFunction",
151151
"FunctionName": {
@@ -154,7 +154,7 @@
154154
"Principal": "apigateway.amazonaws.com",
155155
"SourceArn": {
156156
"Fn::Sub": [
157-
"arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${api}/$default/${method}/*",
157+
"arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${api}/default/${method}/*",
158158
{
159159
"api": {
160160
"Ref": "Testapi"
@@ -188,7 +188,7 @@
188188
},
189189
"Type": "AWS::ApiGateway::DomainName"
190190
},
191-
"TestapiapiexamplecomBasePathMapping": {
191+
"TestapiapiexamplecomDefaultBasePathMapping": {
192192
"Properties": {
193193
"DomainName": {
194194
"Ref": "TestapiapiexamplecomDomain"

tests/tests_e3_aws/troposphere/apigateway/apigatewayv1_test_custom_domain_stages.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@
7171
},
7272
"TestapiDefaultDeployment": {
7373
"Properties": {
74-
"Description": "Deployment resource of $default stage",
74+
"Description": "Deployment resource of default stage",
7575
"RestApiId": {
7676
"Ref": "Testapi"
7777
}
@@ -98,7 +98,7 @@
9898
"DeploymentId": {
9999
"Ref": "TestapiDefaultDeployment"
100100
},
101-
"Description": "stage $default",
101+
"Description": "stage default",
102102
"MethodSettings": [
103103
{
104104
"ResourcePath": "/*",
@@ -212,7 +212,7 @@
212212
},
213213
"Type": "AWS::Lambda::Permission"
214214
},
215-
"TestapiANYLambdaPermission": {
215+
"TestapiANYDefaultLambdaPermission": {
216216
"Properties": {
217217
"Action": "lambda:InvokeFunction",
218218
"FunctionName": {
@@ -221,7 +221,7 @@
221221
"Principal": "apigateway.amazonaws.com",
222222
"SourceArn": {
223223
"Fn::Sub": [
224-
"arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${api}/$default/${method}/*",
224+
"arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${api}/default/${method}/*",
225225
{
226226
"api": {
227227
"Ref": "Testapi"
@@ -255,7 +255,7 @@
255255
},
256256
"Type": "AWS::ApiGateway::DomainName"
257257
},
258-
"TestapiapiexamplecomBasePathMapping": {
258+
"TestapiapiexamplecomDefaultBasePathMapping": {
259259
"Properties": {
260260
"DomainName": {
261261
"Ref": "TestapiapiexamplecomDomain"

tests/tests_e3_aws/troposphere/apigateway/apigatewayv1_test_lambda_alias.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
},
9292
"TestapiDefaultDeployment": {
9393
"Properties": {
94-
"Description": "Deployment resource of $default stage",
94+
"Description": "Deployment resource of default stage",
9595
"RestApiId": {
9696
"Ref": "Testapi"
9797
}
@@ -118,7 +118,7 @@
118118
"DeploymentId": {
119119
"Ref": "TestapiDefaultDeployment"
120120
},
121-
"Description": "stage $default",
121+
"Description": "stage default",
122122
"MethodSettings": [
123123
{
124124
"ResourcePath": "/*",
@@ -177,7 +177,7 @@
177177
},
178178
"Type": "AWS::Lambda::Permission"
179179
},
180-
"TestapiANYLambdaPermission": {
180+
"TestapiANYDefaultLambdaPermission": {
181181
"Properties": {
182182
"Action": "lambda:InvokeFunction",
183183
"FunctionName": {
@@ -186,7 +186,7 @@
186186
"Principal": "apigateway.amazonaws.com",
187187
"SourceArn": {
188188
"Fn::Sub": [
189-
"arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${api}/$default/${method}/*",
189+
"arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${api}/default/${method}/*",
190190
{
191191
"api": {
192192
"Ref": "Testapi"

tests/tests_e3_aws/troposphere/apigateway/apigatewayv1_test_nested_resources.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
},
7676
"TestapiDefaultDeployment": {
7777
"Properties": {
78-
"Description": "Deployment resource of $default stage",
78+
"Description": "Deployment resource of default stage",
7979
"RestApiId": {
8080
"Ref": "Testapi"
8181
}
@@ -104,7 +104,7 @@
104104
"DeploymentId": {
105105
"Ref": "TestapiDefaultDeployment"
106106
},
107-
"Description": "stage $default",
107+
"Description": "stage default",
108108
"MethodSettings": [
109109
{
110110
"ResourcePath": "/*",
@@ -208,7 +208,7 @@
208208
},
209209
"Type": "AWS::ApiGateway::Method"
210210
},
211-
"TestapiAccountsANYLambdaPermission": {
211+
"TestapiAccountsANYDefaultLambdaPermission": {
212212
"Properties": {
213213
"Action": "lambda:InvokeFunction",
214214
"FunctionName": {
@@ -217,7 +217,7 @@
217217
"Principal": "apigateway.amazonaws.com",
218218
"SourceArn": {
219219
"Fn::Sub": [
220-
"arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${api}/$default/${method}/accounts",
220+
"arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${api}/default/${method}/accounts",
221221
{
222222
"api": {
223223
"Ref": "Testapi"
@@ -229,7 +229,7 @@
229229
},
230230
"Type": "AWS::Lambda::Permission"
231231
},
232-
"TestapiProductsANYLambdaPermission": {
232+
"TestapiProductsANYDefaultLambdaPermission": {
233233
"Properties": {
234234
"Action": "lambda:InvokeFunction",
235235
"FunctionName": {
@@ -238,7 +238,7 @@
238238
"Principal": "apigateway.amazonaws.com",
239239
"SourceArn": {
240240
"Fn::Sub": [
241-
"arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${api}/$default/${method}/products",
241+
"arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${api}/default/${method}/products",
242242
{
243243
"api": {
244244
"Ref": "Testapi"
@@ -250,7 +250,7 @@
250250
},
251251
"Type": "AWS::Lambda::Permission"
252252
},
253-
"TestapiProductsAbcdANYLambdaPermission": {
253+
"TestapiProductsAbcdANYDefaultLambdaPermission": {
254254
"Properties": {
255255
"Action": "lambda:InvokeFunction",
256256
"FunctionName": {
@@ -259,7 +259,7 @@
259259
"Principal": "apigateway.amazonaws.com",
260260
"SourceArn": {
261261
"Fn::Sub": [
262-
"arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${api}/$default/${method}/products/abcd",
262+
"arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${api}/default/${method}/products/abcd",
263263
{
264264
"api": {
265265
"Ref": "Testapi"

tests/tests_e3_aws/troposphere/apigateway/apigatewayv1_test_stages.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191
},
9292
"TestapiDefaultDeployment": {
9393
"Properties": {
94-
"Description": "Deployment resource of $default stage",
94+
"Description": "Deployment resource of default stage",
9595
"RestApiId": {
9696
"Ref": "Testapi"
9797
}
@@ -118,7 +118,7 @@
118118
"DeploymentId": {
119119
"Ref": "TestapiDefaultDeployment"
120120
},
121-
"Description": "stage $default",
121+
"Description": "stage default",
122122
"MethodSettings": [
123123
{
124124
"ResourcePath": "/*",
@@ -183,7 +183,7 @@
183183
},
184184
"Type": "AWS::Lambda::Permission"
185185
},
186-
"TestapiANYLambdaPermission": {
186+
"TestapiANYDefaultLambdaPermission": {
187187
"Properties": {
188188
"Action": "lambda:InvokeFunction",
189189
"FunctionName": {
@@ -192,7 +192,7 @@
192192
"Principal": "apigateway.amazonaws.com",
193193
"SourceArn": {
194194
"Fn::Sub": [
195-
"arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${api}/$default/${method}/*",
195+
"arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${api}/default/${method}/*",
196196
{
197197
"api": {
198198
"Ref": "Testapi"

0 commit comments

Comments
 (0)