Skip to content

Commit ad56f53

Browse files
committed
minor refactoring
1 parent 6369d91 commit ad56f53

File tree

4 files changed

+15
-24
lines changed

4 files changed

+15
-24
lines changed

aws-proxy/aws_proxy/client/auth_proxy.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
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
1618
from localstack import config as localstack_config
1719
from localstack.config import external_service_url
1820
from localstack.constants import (
@@ -43,18 +45,14 @@
4345
from aws_proxy import config as repl_config
4446
from aws_proxy.client.utils import truncate_content
4547
from aws_proxy.config import HANDLER_PATH_PROXIES
46-
from aws_proxy.shared.constants import HEADER_HOST_ORIGINAL
48+
from aws_proxy.shared.constants import HEADER_HOST_ORIGINAL, SERVICE_NAME_MAPPING
4749
from aws_proxy.shared.models import AddProxyRequest, ProxyConfig
4850

4951
LOG = logging.getLogger(__name__)
5052
LOG.setLevel(logging.INFO)
5153
if localstack_config.DEBUG:
5254
LOG.setLevel(logging.DEBUG)
5355

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

5957
# TODO make configurable
6058
CLI_PIP_PACKAGE = "localstack-extension-aws-proxy"
@@ -197,8 +195,6 @@ def deregister_from_instance(self):
197195
def _parse_aws_request(
198196
self, request: Request, service_name: str, region_name: str, client
199197
) -> Tuple[OperationModel, AWSPreparedRequest, Dict]:
200-
from localstack.aws.protocol.parser import create_parser
201-
202198
# Use botocore's service model to ensure protocol compatibility
203199
# (LocalStack's load_service may return newer protocol versions that don't match the client)
204200
service_model = self._get_botocore_service_model(service_name)
@@ -348,10 +344,7 @@ def _get_botocore_service_model(service_name: str):
348344
load_service() to ensure protocol compatibility, as LocalStack may use newer protocol
349345
versions (e.g., smithy-rpc-v2-cbor) while clients use older protocols (e.g., query).
350346
"""
351-
import botocore.session
352-
from botocore.model import ServiceModel
353-
354-
session = botocore.session.get_session()
347+
session = get_botocore_session()
355348
loader = session.get_component("data_loader")
356349
api_data = loader.load_service_model(service_name, "service-2")
357350
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)