Skip to content

Commit 560b4cb

Browse files
authored
APIGW: fix Host regex to allow hyphen and remove restriction (#12549)
1 parent 2ed4e11 commit 560b4cb

File tree

4 files changed

+33
-32
lines changed

4 files changed

+33
-32
lines changed

localstack-core/localstack/services/apigateway/next_gen/execute_api/router.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def __init__(self, router: Router[Handler] = None, handler: ApiGatewayEndpoint =
124124

125125
def register_routes(self) -> None:
126126
LOG.debug("Registering API Gateway routes.")
127-
host_pattern = "<regex('[^-]+'):api_id><regex('(-vpce-[^.]+)?'):vpce_suffix>.execute-api.<regex('.*'):server>"
127+
host_pattern = "<regex('.+?'):api_id><regex('(-vpce-[^.]+)?'):vpce_suffix>.execute-api.<regex('.*'):server>"
128128
deprecated_route_endpoint = deprecated_endpoint(
129129
endpoint=self.handler,
130130
previous_path="/restapis/<api_id>/<stage>/_user_request_",

localstack-core/localstack/services/apigateway/next_gen/provider.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
from localstack.aws.api import CommonServiceException, RequestContext, handler
22
from localstack.aws.api.apigateway import (
3-
BadRequestException,
43
CacheClusterSize,
5-
CreateRestApiRequest,
64
CreateStageRequest,
75
Deployment,
86
DeploymentCanarySettings,
@@ -14,14 +12,12 @@
1412
NotFoundException,
1513
NullableBoolean,
1614
NullableInteger,
17-
RestApi,
1815
Stage,
1916
StatusCode,
2017
String,
2118
TestInvokeMethodRequest,
2219
TestInvokeMethodResponse,
2320
)
24-
from localstack.constants import TAG_KEY_CUSTOM_ID
2521
from localstack.services.apigateway.helpers import (
2622
get_apigateway_store,
2723
get_moto_rest_api,
@@ -60,16 +56,6 @@ def on_after_init(self):
6056
apply_patches()
6157
self.router.register_routes()
6258

63-
@handler("CreateRestApi", expand=False)
64-
def create_rest_api(self, context: RequestContext, request: CreateRestApiRequest) -> RestApi:
65-
if "-" in request.get("tags", {}).get(TAG_KEY_CUSTOM_ID, ""):
66-
raise BadRequestException(
67-
f"The '{TAG_KEY_CUSTOM_ID}' tag cannot contain the '-' character."
68-
)
69-
70-
response = super().create_rest_api(context, request)
71-
return response
72-
7359
@handler("DeleteRestApi")
7460
def delete_rest_api(self, context: RequestContext, rest_api_id: String, **kwargs) -> None:
7561
super().delete_rest_api(context, rest_api_id, **kwargs)

tests/aws/services/apigateway/test_apigateway_api.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
from localstack_snapshot.snapshots.transformer import KeyValueBasedTransformer, SortingTransformer
1111

1212
from localstack.aws.api.apigateway import PutMode
13-
from localstack.constants import TAG_KEY_CUSTOM_ID
1413
from localstack.testing.aws.util import is_aws_cloud
1514
from localstack.testing.pytest import markers
1615
from localstack.utils.files import load_file
@@ -200,22 +199,6 @@ def test_create_rest_api_with_tags(self, apigw_create_rest_api, snapshot, aws_cl
200199
response = aws_client.apigateway.get_rest_apis()
201200
snapshot.match("get-rest-apis-w-tags", response)
202201

203-
@markers.aws.only_localstack
204-
def test_create_rest_api_with_custom_id_tag(self, apigw_create_rest_api, aws_client):
205-
custom_id_tag = "testid123"
206-
response = apigw_create_rest_api(
207-
name="my_api", description="this is my api", tags={TAG_KEY_CUSTOM_ID: custom_id_tag}
208-
)
209-
api_id = response["id"]
210-
assert api_id == custom_id_tag
211-
212-
with pytest.raises(aws_client.apigateway.exceptions.BadRequestException):
213-
apigw_create_rest_api(
214-
name="my_api",
215-
description="bad custom id",
216-
tags={TAG_KEY_CUSTOM_ID: "bad-custom-id-hyphen"},
217-
)
218-
219202
@markers.aws.validated
220203
def test_update_rest_api_operation_add_remove(
221204
self, apigw_create_rest_api, snapshot, aws_client

tests/aws/services/apigateway/test_apigateway_common.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from botocore.exceptions import ClientError
99

1010
from localstack.aws.api.lambda_ import Runtime
11+
from localstack.constants import TAG_KEY_CUSTOM_ID
1112
from localstack.testing.aws.util import is_aws_cloud
1213
from localstack.testing.pytest import markers
1314
from localstack.utils.aws.arns import get_partition, parse_arn
@@ -1787,3 +1788,34 @@ def test_api_not_existing(self, aws_client, create_rest_apigw, snapshot):
17871788
assert _response.json() == {
17881789
"message": "The API id '404api' does not correspond to a deployed API Gateway API"
17891790
}
1791+
1792+
@markers.aws.only_localstack
1793+
def test_routing_with_custom_api_id(self, aws_client, create_rest_apigw):
1794+
custom_id = "custom-api-id"
1795+
api_id, _, root_id = create_rest_apigw(
1796+
name="test custom id routing", tags={TAG_KEY_CUSTOM_ID: custom_id}
1797+
)
1798+
1799+
resource = aws_client.apigateway.create_resource(
1800+
restApiId=api_id, parentId=root_id, pathPart="part1"
1801+
)
1802+
hardcoded_resource_id = resource["id"]
1803+
1804+
response_template_get = {"statusCode": 200, "message": "routing ok"}
1805+
_create_mock_integration_with_200_response_template(
1806+
aws_client, api_id, hardcoded_resource_id, "GET", response_template_get
1807+
)
1808+
1809+
stage_name = "dev"
1810+
aws_client.apigateway.create_deployment(restApiId=api_id, stageName=stage_name)
1811+
1812+
url = api_invoke_url(api_id=api_id, stage=stage_name, path="/part1")
1813+
response = requests.get(url)
1814+
assert response.ok
1815+
assert response.json()["message"] == "routing ok"
1816+
1817+
# Validated test living here: `test_create_execute_api_vpc_endpoint`
1818+
vpce_url = url.replace(custom_id, f"{custom_id}-vpce-aabbaabbaabbaabba")
1819+
response = requests.get(vpce_url)
1820+
assert response.ok
1821+
assert response.json()["message"] == "routing ok"

0 commit comments

Comments
 (0)