Skip to content

Commit bd3fe59

Browse files
authored
fix: default 'DefinitionBody' is conflict with disableExecuteApiEndpoint (#1790)
* fix: default 'DefinitionBody' is conflict with disableExecuteApiEndpoint * update swagger format * Add comment to explain why definitionbody is always defined * Add comments
1 parent 32df09b commit bd3fe59

11 files changed

+117
-6
lines changed

samtranslator/model/api/http_api_generator.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def __init__(
4949
domain=None,
5050
fail_on_warnings=False,
5151
description=None,
52-
disable_execute_api_endpoint=False,
52+
disable_execute_api_endpoint=None,
5353
):
5454
"""Constructs an API Generator class that generates API Gateway resources
5555
@@ -109,8 +109,8 @@ def _construct_http_api(self):
109109
if self.fail_on_warnings:
110110
http_api.FailOnWarnings = self.fail_on_warnings
111111

112-
if self.disable_execute_api_endpoint:
113-
http_api.DisableExecuteApiEndpoint = self.disable_execute_api_endpoint
112+
if self.disable_execute_api_endpoint is not None:
113+
self._add_endpoint_configuration()
114114

115115
if self.definition_uri:
116116
http_api.BodyS3Location = self._construct_body_s3_dict()
@@ -129,6 +129,32 @@ def _construct_http_api(self):
129129

130130
return http_api
131131

132+
def _add_endpoint_configuration(self):
133+
"""Add disableExecuteApiEndpoint if it is set in SAM
134+
HttpApi doesn't have vpcEndpointIds
135+
136+
Note:
137+
DisableExecuteApiEndpoint as a property of AWS::ApiGatewayV2::Api needs both DefinitionBody and
138+
DefinitionUri to be None. However, if neither DefinitionUri nor DefinitionBody are specified,
139+
SAM will generate a openapi definition body based on template configuration.
140+
https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/sam-resource-api.html#sam-api-definitionbody
141+
For this reason, we always put DisableExecuteApiEndpoint into openapi object.
142+
143+
"""
144+
if self.disable_execute_api_endpoint and not self.definition_body:
145+
raise InvalidResourceException(
146+
self.logical_id, "DisableExecuteApiEndpoint works only within 'DefinitionBody' property."
147+
)
148+
editor = OpenApiEditor(self.definition_body)
149+
150+
# if DisableExecuteApiEndpoint is set in both definition_body and as a property,
151+
# SAM merges and overrides the disableExecuteApiEndpoint in definition_body with headers of
152+
# "x-amazon-apigateway-endpoint-configuration"
153+
editor.add_endpoint_config(self.disable_execute_api_endpoint)
154+
155+
# Assign the OpenApi back to template
156+
self.definition_body = editor.openapi
157+
132158
def _add_cors(self):
133159
"""
134160
Add CORS configuration if CORSConfiguration property is set in SAM.

samtranslator/open_api/open_api.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ 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"
24+
_SERVERS = "servers"
2325
_CONDITIONAL_IF = "Fn::If"
2426
_X_ANY_METHOD = "x-amazon-apigateway-any-method"
2527
_ALL_HTTP_METHODS = ["OPTIONS", "GET", "HEAD", "POST", "PUT", "DELETE", "PATCH"]
@@ -427,6 +429,27 @@ def add_tags(self, tags):
427429
tag = {"name": name, self._X_APIGW_TAG_VALUE: value}
428430
self.tags.append(tag)
429431

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

tests/translator/input/api_with_basic_custom_domain_intrinsics_http.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ Resources:
5353
Type: AWS::Serverless::HttpApi
5454
Properties:
5555
StageName: Prod
56+
DisableExecuteApiEndpoint: False
5657
Domain:
5758
DomainName: !Sub 'example-${AWS::Region}.com'
5859
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: 7 additions & 1 deletion
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,6 +217,13 @@
218217
"name": "httpapi:createdBy",
219218
"x-amazon-apigateway-tag-value": "SAM"
220219
}
220+
],
221+
"servers": [
222+
{
223+
"x-amazon-apigateway-endpoint-configuration": {
224+
"disableExecuteApiEndpoint": true
225+
}
226+
}
221227
]
222228
}
223229
}

tests/translator/output/api_with_basic_custom_domain_intrinsics_http.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,13 @@
217217
"name": "httpapi:createdBy",
218218
"x-amazon-apigateway-tag-value": "SAM"
219219
}
220+
],
221+
"servers": [
222+
{
223+
"x-amazon-apigateway-endpoint-configuration": {
224+
"disableExecuteApiEndpoint": false
225+
}
226+
}
220227
]
221228
}
222229
},

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

Lines changed: 7 additions & 1 deletion
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,6 +217,13 @@
218217
"name": "httpapi:createdBy",
219218
"x-amazon-apigateway-tag-value": "SAM"
220219
}
220+
],
221+
"servers": [
222+
{
223+
"x-amazon-apigateway-endpoint-configuration": {
224+
"disableExecuteApiEndpoint": true
225+
}
226+
}
221227
]
222228
}
223229
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,13 @@
306306
"name": "httpapi:createdBy",
307307
"x-amazon-apigateway-tag-value": "SAM"
308308
}
309+
],
310+
"servers": [
311+
{
312+
"x-amazon-apigateway-endpoint-configuration": {
313+
"disableExecuteApiEndpoint": false
314+
}
315+
}
309316
]
310317
}
311318
},

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

Lines changed: 7 additions & 1 deletion
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,6 +217,13 @@
218217
"name": "httpapi:createdBy",
219218
"x-amazon-apigateway-tag-value": "SAM"
220219
}
220+
],
221+
"servers": [
222+
{
223+
"x-amazon-apigateway-endpoint-configuration": {
224+
"disableExecuteApiEndpoint": true
225+
}
226+
}
221227
]
222228
}
223229
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,13 @@
306306
"name": "httpapi:createdBy",
307307
"x-amazon-apigateway-tag-value": "SAM"
308308
}
309+
],
310+
"servers": [
311+
{
312+
"x-amazon-apigateway-endpoint-configuration": {
313+
"disableExecuteApiEndpoint": false
314+
}
315+
}
309316
]
310317
}
311318
},

0 commit comments

Comments
 (0)