Skip to content

Commit b61f38f

Browse files
committed
fix: default 'DefinitionBody' is conflict with disableExecuteApiEndpoint
1 parent edaeb52 commit b61f38f

11 files changed

+107
-12
lines changed

samtranslator/model/api/http_api_generator.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def __init__(
4848
passthrough_resource_attributes=None,
4949
domain=None,
5050
fail_on_warnings=False,
51-
disable_execute_api_endpoint=False,
51+
disable_execute_api_endpoint=None,
5252
):
5353
"""Constructs an API Generator class that generates API Gateway resources
5454
@@ -106,8 +106,8 @@ def _construct_http_api(self):
106106
if self.fail_on_warnings:
107107
http_api.FailOnWarnings = self.fail_on_warnings
108108

109-
if self.disable_execute_api_endpoint:
110-
http_api.DisableExecuteApiEndpoint = self.disable_execute_api_endpoint
109+
if self.disable_execute_api_endpoint is not None:
110+
self._add_endpoint_configuration()
111111

112112
if self.definition_uri:
113113
http_api.BodyS3Location = self._construct_body_s3_dict()
@@ -123,6 +123,23 @@ def _construct_http_api(self):
123123

124124
return http_api
125125

126+
def _add_endpoint_configuration(self):
127+
"""Add disableExecuteApiEndpoint if it is set in SAM
128+
HttpApi doesn't have vpcEndpointIds
129+
130+
"""
131+
if isinstance(self.disable_execute_api_endpoint, bool) and not self.definition_body:
132+
raise InvalidResourceException(
133+
self.logical_id, "Cors works only with inline OpenApi specified in 'DefinitionBody' property."
134+
)
135+
editor = OpenApiEditor(self.definition_body)
136+
# if CORS is set in both definition_body and as a CorsConfiguration property,
137+
# SAM merges and overrides the cors headers in definition_body with headers of CorsConfiguration
138+
editor.add_endpoint_config(self.disable_execute_api_endpoint)
139+
140+
# Assign the OpenApi back to template
141+
self.definition_body = editor.openapi
142+
126143
def _add_cors(self):
127144
"""
128145
Add CORS configuration if CORSConfiguration property is set in SAM.

samtranslator/open_api/open_api.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class OpenApiEditor(object):
2020
_X_APIGW_INTEGRATION = "x-amazon-apigateway-integration"
2121
_X_APIGW_TAG_VALUE = "x-amazon-apigateway-tag-value"
2222
_X_APIGW_CORS = "x-amazon-apigateway-cors"
23+
_X_APIGW_ENDPOINT_CONFIG = "x-amazon-apigateway-endpoint-configuration"
2324
_CONDITIONAL_IF = "Fn::If"
2425
_X_ANY_METHOD = "x-amazon-apigateway-any-method"
2526
_ALL_HTTP_METHODS = ["OPTIONS", "GET", "HEAD", "POST", "PUT", "DELETE", "PATCH"]
@@ -427,6 +428,23 @@ def add_tags(self, tags):
427428
tag = {"name": name, self._X_APIGW_TAG_VALUE: value}
428429
self.tags.append(tag)
429430

431+
def add_endpoint_config(self, disable_execute_api_endpoint):
432+
"""Add endpoint configuration to _X_APIGW_ENDPOINT_CONFIG header in open api definition
433+
434+
Following this guide:
435+
https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-swagger-extensions-endpoint-configuration.html
436+
https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigatewayv2-api.html#cfn-apigatewayv2-api-disableexecuteapiendpoint
437+
438+
:param boolean disable_execute_api_endpoint: Specifies whether clients can invoke your API by using the default execute-api endpoint.
439+
440+
"""
441+
DISABLE_EXECUTE_API_ENDPOINT = "disableExecuteApiEndpoint"
442+
endpoint_configuration = self._doc.get(self._X_APIGW_ENDPOINT_CONFIG, dict())
443+
444+
endpoint_configuration[DISABLE_EXECUTE_API_ENDPOINT] = disable_execute_api_endpoint
445+
446+
self._doc[self._X_APIGW_ENDPOINT_CONFIG] = endpoint_configuration
447+
430448
def add_cors(
431449
self,
432450
allow_origins,

tests/translator/input/api_with_basic_custom_domain_intrinsics_http.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ Parameters:
2020
Default: another-api-v2-truststore-version
2121
Type: String
2222

23+
MyDisableExecuteApiEndpoint:
24+
Default: false
25+
Type: Bool
26+
2327
Resources:
2428
MyFunction:
2529
Condition: C1
@@ -53,6 +57,7 @@ Resources:
5357
Type: AWS::Serverless::HttpApi
5458
Properties:
5559
StageName: Prod
60+
DisableExecuteApiEndpoint: !Ref MyDisableExecuteApiEndpoint
5661
Domain:
5762
DomainName: !Sub 'example-${AWS::Region}.com'
5863
CertificateArn: !Ref MyDomainCert
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Resources:
2+
MyApi:
3+
Type: AWS::Serverless::HttpApi
4+
Properties:
5+
DisableExecuteApiEndpoint: String
6+
StageName: Prod
7+
Domain:
8+
DomainName: "sam-example.com"
9+
CertificateArn: "arn:aws:acm:us-east-1:123455353535:certificate/6c911401-620d-4d41-b89e-366c238bb2f3"
10+
EndpointConfiguration: REGIONAL
11+
SecurityPolicy: TLS_1_2
12+
BasePath: ["/basic", "/begin-here"]
13+
Route53:
14+
HostedZoneName: sam-example.com.

tests/translator/output/api_with_basic_custom_domain_http.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,6 @@
175175
"MyApi": {
176176
"Type": "AWS::ApiGatewayV2::Api",
177177
"Properties": {
178-
"DisableExecuteApiEndpoint": true,
179178
"Body": {
180179
"info": {
181180
"version": "1.0",
@@ -218,7 +217,10 @@
218217
"name": "httpapi:createdBy",
219218
"x-amazon-apigateway-tag-value": "SAM"
220219
}
221-
]
220+
],
221+
"x-amazon-apigateway-endpoint-configuration": {
222+
"disableExecuteApiEndpoint": true
223+
}
222224
}
223225
}
224226
}

tests/translator/output/api_with_basic_custom_domain_intrinsics_http.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
"MyMTLSVersionHTTP": {
2424
"Default": "another-api-v2-truststore-version",
2525
"Type": "String"
26+
},
27+
"MyDisableExecuteApiEndpoint": {
28+
"Default": false,
29+
"Type": "Bool"
2630
}
2731
},
2832
"Resources": {
@@ -217,7 +221,12 @@
217221
"name": "httpapi:createdBy",
218222
"x-amazon-apigateway-tag-value": "SAM"
219223
}
220-
]
224+
],
225+
"x-amazon-apigateway-endpoint-configuration": {
226+
"disableExecuteApiEndpoint": {
227+
"Ref": "MyDisableExecuteApiEndpoint"
228+
}
229+
}
221230
}
222231
},
223232
"Condition": "C1"

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,6 @@
175175
"MyApi": {
176176
"Type": "AWS::ApiGatewayV2::Api",
177177
"Properties": {
178-
"DisableExecuteApiEndpoint": true,
179178
"Body": {
180179
"info": {
181180
"version": "1.0",
@@ -218,7 +217,10 @@
218217
"name": "httpapi:createdBy",
219218
"x-amazon-apigateway-tag-value": "SAM"
220219
}
221-
]
220+
],
221+
"x-amazon-apigateway-endpoint-configuration": {
222+
"disableExecuteApiEndpoint": true
223+
}
222224
}
223225
}
224226
}

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
"MyMTLSVersionHTTP": {
2424
"Default": "another-api-v2-truststore-version",
2525
"Type": "String"
26+
},
27+
"MyDisableExecuteApiEndpoint": {
28+
"Default": false,
29+
"Type": "Bool"
2630
}
2731
},
2832
"Resources": {
@@ -306,7 +310,12 @@
306310
"name": "httpapi:createdBy",
307311
"x-amazon-apigateway-tag-value": "SAM"
308312
}
309-
]
313+
],
314+
"x-amazon-apigateway-endpoint-configuration": {
315+
"disableExecuteApiEndpoint": {
316+
"Ref": "MyDisableExecuteApiEndpoint"
317+
}
318+
}
310319
}
311320
},
312321
"Condition": "C1"

tests/translator/output/aws-us-gov/api_with_basic_custom_domain_http.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,6 @@
175175
"MyApi": {
176176
"Type": "AWS::ApiGatewayV2::Api",
177177
"Properties": {
178-
"DisableExecuteApiEndpoint": true,
179178
"Body": {
180179
"info": {
181180
"version": "1.0",
@@ -218,7 +217,10 @@
218217
"name": "httpapi:createdBy",
219218
"x-amazon-apigateway-tag-value": "SAM"
220219
}
221-
]
220+
],
221+
"x-amazon-apigateway-endpoint-configuration": {
222+
"disableExecuteApiEndpoint": true
223+
}
222224
}
223225
}
224226
}

tests/translator/output/aws-us-gov/api_with_basic_custom_domain_intrinsics_http.json

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
"MyMTLSVersionHTTP": {
2424
"Default": "another-api-v2-truststore-version",
2525
"Type": "String"
26+
},
27+
"MyDisableExecuteApiEndpoint": {
28+
"Default": false,
29+
"Type": "Bool"
2630
}
2731
},
2832
"Resources": {
@@ -306,7 +310,12 @@
306310
"name": "httpapi:createdBy",
307311
"x-amazon-apigateway-tag-value": "SAM"
308312
}
309-
]
313+
],
314+
"x-amazon-apigateway-endpoint-configuration": {
315+
"disableExecuteApiEndpoint": {
316+
"Ref": "MyDisableExecuteApiEndpoint"
317+
}
318+
}
310319
}
311320
},
312321
"Condition": "C1"

0 commit comments

Comments
 (0)