1515from localstack import config as localstack_config
1616from localstack .aws .spec import load_service
1717from localstack .config import external_service_url
18- from localstack .constants import AWS_REGION_US_EAST_1 , DOCKER_IMAGE_NAME_PRO , LOCALHOST_HOSTNAME
18+ from localstack .constants import (
19+ AWS_REGION_US_EAST_1 ,
20+ DOCKER_IMAGE_NAME_PRO ,
21+ LOCALHOST_HOSTNAME ,
22+ )
1923from localstack .http import Request
2024from localstack .pro .core .bootstrap .licensingv2 import (
2125 ENV_LOCALSTACK_API_KEY ,
2529from localstack .utils .bootstrap import setup_logging
2630from localstack .utils .collections import select_attributes
2731from localstack .utils .container_utils .container_client import PortMappings
28- from localstack .utils .docker_utils import DOCKER_CLIENT , reserve_available_container_port
32+ from localstack .utils .docker_utils import (
33+ DOCKER_CLIENT ,
34+ reserve_available_container_port ,
35+ )
2936from localstack .utils .files import new_tmp_file , save_file
3037from localstack .utils .functions import run_safe
3138from localstack .utils .net import get_docker_host_from_container , get_free_tcp_port
3946from aws_proxy .shared .constants import HEADER_HOST_ORIGINAL
4047from aws_proxy .shared .models import AddProxyRequest , ProxyConfig
4148
42- from .http2_server import run_server
43-
4449LOG = logging .getLogger (__name__ )
4550LOG .setLevel (logging .INFO )
4651if localstack_config .DEBUG :
@@ -66,9 +71,14 @@ def __init__(self, config: ProxyConfig, port: int = None):
6671 super ().__init__ (port = port )
6772
6873 def do_run (self ):
74+ # note: keep import here, to avoid runtime errors
75+ from .http2_server import run_server
76+
6977 self .register_in_instance ()
7078 bind_host = self .config .get ("bind_host" ) or DEFAULT_BIND_HOST
71- proxy = run_server (port = self .port , bind_addresses = [bind_host ], handler = self .proxy_request )
79+ proxy = run_server (
80+ port = self .port , bind_addresses = [bind_host ], handler = self .proxy_request
81+ )
7282 proxy .join ()
7383
7484 def proxy_request (self , request : Request , data : bytes ) -> Response :
@@ -109,7 +119,9 @@ def proxy_request(self, request: Request, data: bytes) -> Response:
109119 # adjust request dict and fix certain edge cases in the request
110120 self ._adjust_request_dict (service_name , request_dict )
111121
112- headers_truncated = {k : truncate (to_str (v )) for k , v in dict (aws_request .headers ).items ()}
122+ headers_truncated = {
123+ k : truncate (to_str (v )) for k , v in dict (aws_request .headers ).items ()
124+ }
113125 LOG .debug (
114126 "Sending request for service %s to AWS: %s %s - %s - %s" ,
115127 service_name ,
@@ -138,7 +150,9 @@ def proxy_request(self, request: Request, data: bytes) -> Response:
138150 return response
139151 except Exception as e :
140152 if LOG .isEnabledFor (logging .DEBUG ):
141- LOG .exception ("Error when making request to AWS service %s: %s" , service_name , e )
153+ LOG .exception (
154+ "Error when making request to AWS service %s: %s" , service_name , e
155+ )
142156 return requests_response ("" , status_code = 400 )
143157
144158 def register_in_instance (self ):
@@ -224,7 +238,10 @@ def _adjust_request_dict(self, service_name: str, request_dict: Dict):
224238 body_str = run_safe (lambda : to_str (req_body )) or ""
225239
226240 # TODO: this custom fix should not be required - investigate and remove!
227- if "<CreateBucketConfiguration" in body_str and "LocationConstraint" not in body_str :
241+ if (
242+ "<CreateBucketConfiguration" in body_str
243+ and "LocationConstraint" not in body_str
244+ ):
228245 region = request_dict ["context" ]["client_region" ]
229246 if region == AWS_REGION_US_EAST_1 :
230247 request_dict ["body" ] = ""
@@ -238,15 +255,19 @@ def _adjust_request_dict(self, service_name: str, request_dict: Dict):
238255 account_id = self ._query_account_id_from_aws ()
239256 if "QueueUrl" in req_body :
240257 queue_name = req_body ["QueueUrl" ].split ("/" )[- 1 ]
241- req_body ["QueueUrl" ] = f"https://queue.amazonaws.com/{ account_id } /{ queue_name } "
258+ req_body ["QueueUrl" ] = (
259+ f"https://queue.amazonaws.com/{ account_id } /{ queue_name } "
260+ )
242261 if "QueueOwnerAWSAccountId" in req_body :
243262 req_body ["QueueOwnerAWSAccountId" ] = account_id
244263 if service_name == "sqs" and request_dict .get ("url" ):
245264 req_json = run_safe (lambda : json .loads (body_str )) or {}
246265 account_id = self ._query_account_id_from_aws ()
247266 queue_name = req_json .get ("QueueName" )
248267 if account_id and queue_name :
249- request_dict ["url" ] = f"https://queue.amazonaws.com/{ account_id } /{ queue_name } "
268+ request_dict ["url" ] = (
269+ f"https://queue.amazonaws.com/{ account_id } /{ queue_name } "
270+ )
250271 req_json ["QueueOwnerAWSAccountId" ] = account_id
251272 request_dict ["body" ] = to_bytes (json .dumps (req_json ))
252273
@@ -256,7 +277,9 @@ def _fix_headers(self, request: Request, service_name: str):
256277 host = request .headers .get ("Host" ) or ""
257278 regex = r"^(https?://)?([0-9.]+|localhost)(:[0-9]+)?"
258279 if re .match (regex , host ):
259- request .headers ["Host" ] = re .sub (regex , rf"\1s3.{ LOCALHOST_HOSTNAME } " , host )
280+ request .headers ["Host" ] = re .sub (
281+ regex , rf"\1s3.{ LOCALHOST_HOSTNAME } " , host
282+ )
260283 request .headers .pop ("Content-Length" , None )
261284 request .headers .pop ("x-localstack-request-url" , None )
262285 request .headers .pop ("X-Forwarded-For" , None )
@@ -311,7 +334,9 @@ def start_aws_auth_proxy_in_container(
311334 # should consider building pre-baked images for the extension in the future. Also,
312335 # the new packaged CLI binary can help us gain more stability over time...
313336
314- logging .getLogger ("localstack.utils.container_utils.docker_cmd_client" ).setLevel (logging .INFO )
337+ logging .getLogger ("localstack.utils.container_utils.docker_cmd_client" ).setLevel (
338+ logging .INFO
339+ )
315340 logging .getLogger ("localstack.utils.docker_utils" ).setLevel (logging .INFO )
316341 logging .getLogger ("localstack.utils.run" ).setLevel (logging .INFO )
317342
@@ -328,13 +353,18 @@ def start_aws_auth_proxy_in_container(
328353 image_name = DOCKER_IMAGE_NAME_PRO
329354 # add host mapping for localstack.cloud to localhost to prevent the health check from failing
330355 additional_flags = (
331- repl_config .PROXY_DOCKER_FLAGS + " --add-host=localhost.localstack.cloud:host-gateway"
356+ repl_config .PROXY_DOCKER_FLAGS
357+ + " --add-host=localhost.localstack.cloud:host-gateway"
332358 )
333359 DOCKER_CLIENT .create_container (
334360 image_name ,
335361 name = container_name ,
336362 entrypoint = "" ,
337- command = ["bash" , "-c" , f"touch { CONTAINER_LOG_FILE } ; tail -f { CONTAINER_LOG_FILE } " ],
363+ command = [
364+ "bash" ,
365+ "-c" ,
366+ f"touch { CONTAINER_LOG_FILE } ; tail -f { CONTAINER_LOG_FILE } " ,
367+ ],
338368 ports = ports ,
339369 additional_flags = additional_flags ,
340370 )
@@ -388,7 +418,10 @@ def start_aws_auth_proxy_in_container(
388418 command = f"{ venv_activate } ; localstack aws proxy -c { CONTAINER_CONFIG_FILE } -p { port } --host 0.0.0.0 > { CONTAINER_LOG_FILE } 2>&1"
389419 if use_docker_sdk_command :
390420 DOCKER_CLIENT .exec_in_container (
391- container_name , command = ["bash" , "-c" , command ], env_vars = env_vars , interactive = True
421+ container_name ,
422+ command = ["bash" , "-c" , command ],
423+ env_vars = env_vars ,
424+ interactive = True ,
392425 )
393426 else :
394427 env_vars_list = []
0 commit comments