Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f9c0349

Browse files
committedSep 23, 2024·
forward original host and fix logic for path based addressing
1 parent 0bcbb53 commit f9c0349

File tree

3 files changed

+18
-3
lines changed

3 files changed

+18
-3
lines changed
 

‎aws-replicator/aws_replicator/client/auth_proxy.py

+14-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from localstack import config as localstack_config
1616
from localstack.aws.spec import load_service
1717
from localstack.config import external_service_url
18-
from localstack.constants import AWS_REGION_US_EAST_1, DOCKER_IMAGE_NAME_PRO
18+
from localstack.constants import AWS_REGION_US_EAST_1, DOCKER_IMAGE_NAME_PRO, LOCALHOST_HOSTNAME
1919
from localstack.http import Request
2020
from localstack.utils.aws.aws_responses import requests_response
2121
from localstack.utils.bootstrap import setup_logging
@@ -32,6 +32,7 @@
3232
from aws_replicator import config as repl_config
3333
from aws_replicator.client.utils import truncate_content
3434
from aws_replicator.config import HANDLER_PATH_PROXIES
35+
from aws_replicator.shared.constants import HEADER_HOST_ORIGINAL
3536
from aws_replicator.shared.models import AddProxyRequest, ProxyConfig
3637

3738
from .http2_server import run_server
@@ -106,6 +107,7 @@ def proxy_request(self, request: Request, data: bytes) -> Response:
106107

107108
# fix headers (e.g., "Host") and create client
108109
self._fix_headers(request, service_name)
110+
self._fix_host_and_path(request, service_name)
109111

110112
# create request and request dict
111113
operation_model, aws_request, request_dict = self._parse_aws_request(
@@ -262,14 +264,24 @@ def _fix_headers(self, request: Request, service_name: str):
262264
host = request.headers.get("Host") or ""
263265
regex = r"^(https?://)?([0-9.]+|localhost)(:[0-9]+)?"
264266
if re.match(regex, host):
265-
request.headers["Host"] = re.sub(regex, r"\1s3.localhost.localstack.cloud", host)
267+
request.headers["Host"] = re.sub(regex, rf"\1s3.{LOCALHOST_HOSTNAME}", host)
266268
request.headers.pop("Content-Length", None)
267269
request.headers.pop("x-localstack-request-url", None)
268270
request.headers.pop("X-Forwarded-For", None)
269271
request.headers.pop("X-Localstack-Tgt-Api", None)
270272
request.headers.pop("X-Moto-Account-Id", None)
271273
request.headers.pop("Remote-Addr", None)
272274

275+
def _fix_host_and_path(self, request: Request, service_name: str):
276+
if service_name == "s3":
277+
# fix the path and Host header, to avoid bucket addressing issues
278+
host = request.headers.pop(HEADER_HOST_ORIGINAL, None)
279+
host = host or request.headers.get("Host") or ""
280+
match = re.match(rf"(.+)\.s3\.{LOCALHOST_HOSTNAME}", host)
281+
if match:
282+
# prepend the bucket name (extracted from the host) to the path of the request (path-based addressing)
283+
request.path = f"/{match.group(1)}{request.path}"
284+
273285
def _extract_region_and_service(self, headers) -> Optional[Tuple[str, str]]:
274286
auth_header = headers.pop("Authorization", "")
275287
parts = auth_header.split("Credential=", maxsplit=1)

‎aws-replicator/aws_replicator/server/aws_request_forwarder.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
except ImportError:
2323
from localstack.constants import TEST_AWS_ACCESS_KEY_ID
2424

25+
from aws_replicator.shared.constants import HEADER_HOST_ORIGINAL
2526
from aws_replicator.shared.models import ProxyInstance, ProxyServiceConfig
2627

2728
LOG = logging.getLogger(__name__)
@@ -145,7 +146,7 @@ def forward_request(self, context: RequestContext, proxy: ProxyInstance) -> requ
145146

146147
result = None
147148
try:
148-
headers.pop("Host", None)
149+
headers[HEADER_HOST_ORIGINAL] = headers.pop("Host", None)
149150
headers.pop("Content-Length", None)
150151
ctype = headers.get("Content-Type")
151152
data = b""
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# header name for the original request host name forwarded in the request to the target proxy handler
2+
HEADER_HOST_ORIGINAL = "x-ls-host-original"

0 commit comments

Comments
 (0)