Skip to content

Commit debc56a

Browse files
committed
minor refactoring
1 parent 6369d91 commit debc56a

File tree

4 files changed

+28
-28
lines changed

4 files changed

+28
-28
lines changed

aws-proxy/aws_proxy/client/auth_proxy.py

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@
1212
import boto3
1313
import requests
1414
from botocore.awsrequest import AWSPreparedRequest
15-
from botocore.model import OperationModel
15+
from botocore.model import OperationModel, ServiceModel
16+
from botocore.session import get_session as get_botocore_session
17+
from localstack.aws.protocol.parser import create_parser
18+
from localstack.aws.spec import load_service
1619
from localstack import config as localstack_config
1720
from localstack.config import external_service_url
1821
from localstack.constants import (
@@ -43,18 +46,14 @@
4346
from aws_proxy import config as repl_config
4447
from aws_proxy.client.utils import truncate_content
4548
from aws_proxy.config import HANDLER_PATH_PROXIES
46-
from aws_proxy.shared.constants import HEADER_HOST_ORIGINAL
49+
from aws_proxy.shared.constants import HEADER_HOST_ORIGINAL, SERVICE_NAME_MAPPING
4750
from aws_proxy.shared.models import AddProxyRequest, ProxyConfig
4851

4952
LOG = logging.getLogger(__name__)
5053
LOG.setLevel(logging.INFO)
5154
if localstack_config.DEBUG:
5255
LOG.setLevel(logging.DEBUG)
5356

54-
# Mapping from AWS service signing names to boto3 client names
55-
SERVICE_NAME_MAPPING = {
56-
"monitoring": "cloudwatch",
57-
}
5857

5958
# TODO make configurable
6059
CLI_PIP_PACKAGE = "localstack-extension-aws-proxy"
@@ -197,13 +196,19 @@ def deregister_from_instance(self):
197196
def _parse_aws_request(
198197
self, request: Request, service_name: str, region_name: str, client
199198
) -> Tuple[OperationModel, AWSPreparedRequest, Dict]:
200-
from localstack.aws.protocol.parser import create_parser
199+
# Use LocalStack's load_service for parsing, as it handles multi-protocol detection
200+
# from request headers (e.g., detecting JSON vs RPC v2 CBOR from content-type).
201+
localstack_service_model = load_service(service_name)
202+
parser = create_parser(localstack_service_model)
203+
ls_operation_model, parsed_request = parser.parse(request)
204+
205+
# Get botocore's operation model for making the actual AWS request,
206+
# as botocore client methods expect botocore model objects.
207+
botocore_service_model = self._get_botocore_service_model(service_name)
208+
operation_model = botocore_service_model.operation_model(
209+
ls_operation_model.name
210+
)
201211

202-
# Use botocore's service model to ensure protocol compatibility
203-
# (LocalStack's load_service may return newer protocol versions that don't match the client)
204-
service_model = self._get_botocore_service_model(service_name)
205-
parser = create_parser(service_model)
206-
operation_model, parsed_request = parser.parse(request)
207212
request_context = {
208213
"client_region": region_name,
209214
"has_streaming_input": operation_model.has_streaming_input,
@@ -348,10 +353,7 @@ def _get_botocore_service_model(service_name: str):
348353
load_service() to ensure protocol compatibility, as LocalStack may use newer protocol
349354
versions (e.g., smithy-rpc-v2-cbor) while clients use older protocols (e.g., query).
350355
"""
351-
import botocore.session
352-
from botocore.model import ServiceModel
353-
354-
session = botocore.session.get_session()
356+
session = get_botocore_session()
355357
loader = session.get_component("data_loader")
356358
api_data = loader.load_service_model(service_name, "service-2")
357359
return ServiceModel(api_data)

aws-proxy/aws_proxy/server/aws_request_forwarder.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
except ImportError:
2626
from localstack.constants import TEST_AWS_ACCESS_KEY_ID
2727

28-
from aws_proxy.shared.constants import HEADER_HOST_ORIGINAL
28+
from aws_proxy.shared.constants import HEADER_HOST_ORIGINAL, SERVICE_NAME_MAPPING
2929
from aws_proxy.shared.models import ProxyInstance, ProxyServiceConfig
3030

3131
LOG = logging.getLogger(__name__)
@@ -295,12 +295,7 @@ def _get_resource_names(cls, service_config: ProxyServiceConfig) -> list[str]:
295295

296296
@classmethod
297297
def _get_canonical_service_name(cls, service_name: str) -> str:
298-
# Map internal/signing service names to boto3 client names
299-
mapping = {
300-
"sqs-query": "sqs",
301-
"monitoring": "cloudwatch",
302-
}
303-
return mapping.get(service_name, service_name)
298+
return SERVICE_NAME_MAPPING.get(service_name, service_name)
304299

305300
def _reconstruct_request_body(
306301
self, context: RequestContext, content_type: str

aws-proxy/aws_proxy/server/extension.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99
from localstack.services.internal import get_internal_apis
1010

11-
from aws_proxy.server.request_handler import WebApp
11+
from aws_proxy.server.aws_request_forwarder import AwsProxyHandler
12+
from aws_proxy.server.request_handler import RequestHandler, WebApp
1213

1314
LOG = logging.getLogger(__name__)
1415

@@ -27,8 +28,6 @@ def on_extension_load(self):
2728
)
2829

2930
def update_gateway_routes(self, router: http.Router[http.RouteHandler]):
30-
from aws_proxy.server.request_handler import RequestHandler
31-
3231
super().update_gateway_routes(router)
3332

3433
LOG.info("AWS Proxy: adding routes to activate extension")
@@ -38,7 +37,5 @@ def collect_routes(self, routes: list[t.Any]):
3837
routes.append(WebApp())
3938

4039
def update_request_handlers(self, handlers: CompositeHandler):
41-
from aws_proxy.server.aws_request_forwarder import AwsProxyHandler
42-
4340
LOG.debug("AWS Proxy: adding AWS proxy handler to the request chain")
4441
handlers.handlers.append(AwsProxyHandler())
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
11
# header name for the original request host name forwarded in the request to the target proxy handler
22
HEADER_HOST_ORIGINAL = "x-ls-host-original"
3+
4+
# Mapping from AWS service signing names to boto3 client names
5+
SERVICE_NAME_MAPPING = {
6+
"monitoring": "cloudwatch",
7+
"sqs-query": "sqs",
8+
}

0 commit comments

Comments
 (0)