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
@@ -68,7 +75,9 @@ def __init__(self, config: ProxyConfig, port: int = None):
6875 def do_run (self ):
6976 self .register_in_instance ()
7077 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 )
78+ proxy = run_server (
79+ port = self .port , bind_addresses = [bind_host ], handler = self .proxy_request
80+ )
7281 proxy .join ()
7382
7483 def proxy_request (self , request : Request , data : bytes ) -> Response :
@@ -109,7 +118,9 @@ def proxy_request(self, request: Request, data: bytes) -> Response:
109118 # adjust request dict and fix certain edge cases in the request
110119 self ._adjust_request_dict (service_name , request_dict )
111120
112- headers_truncated = {k : truncate (to_str (v )) for k , v in dict (aws_request .headers ).items ()}
121+ headers_truncated = {
122+ k : truncate (to_str (v )) for k , v in dict (aws_request .headers ).items ()
123+ }
113124 LOG .debug (
114125 "Sending request for service %s to AWS: %s %s - %s - %s" ,
115126 service_name ,
@@ -138,7 +149,9 @@ def proxy_request(self, request: Request, data: bytes) -> Response:
138149 return response
139150 except Exception as e :
140151 if LOG .isEnabledFor (logging .DEBUG ):
141- LOG .exception ("Error when making request to AWS service %s: %s" , service_name , e )
152+ LOG .exception (
153+ "Error when making request to AWS service %s: %s" , service_name , e
154+ )
142155 return requests_response ("" , status_code = 400 )
143156
144157 def register_in_instance (self ):
@@ -224,7 +237,10 @@ def _adjust_request_dict(self, service_name: str, request_dict: Dict):
224237 body_str = run_safe (lambda : to_str (req_body )) or ""
225238
226239 # TODO: this custom fix should not be required - investigate and remove!
227- if "<CreateBucketConfiguration" in body_str and "LocationConstraint" not in body_str :
240+ if (
241+ "<CreateBucketConfiguration" in body_str
242+ and "LocationConstraint" not in body_str
243+ ):
228244 region = request_dict ["context" ]["client_region" ]
229245 if region == AWS_REGION_US_EAST_1 :
230246 request_dict ["body" ] = ""
@@ -238,15 +254,19 @@ def _adjust_request_dict(self, service_name: str, request_dict: Dict):
238254 account_id = self ._query_account_id_from_aws ()
239255 if "QueueUrl" in req_body :
240256 queue_name = req_body ["QueueUrl" ].split ("/" )[- 1 ]
241- req_body ["QueueUrl" ] = f"https://queue.amazonaws.com/{ account_id } /{ queue_name } "
257+ req_body ["QueueUrl" ] = (
258+ f"https://queue.amazonaws.com/{ account_id } /{ queue_name } "
259+ )
242260 if "QueueOwnerAWSAccountId" in req_body :
243261 req_body ["QueueOwnerAWSAccountId" ] = account_id
244262 if service_name == "sqs" and request_dict .get ("url" ):
245263 req_json = run_safe (lambda : json .loads (body_str )) or {}
246264 account_id = self ._query_account_id_from_aws ()
247265 queue_name = req_json .get ("QueueName" )
248266 if account_id and queue_name :
249- request_dict ["url" ] = f"https://queue.amazonaws.com/{ account_id } /{ queue_name } "
267+ request_dict ["url" ] = (
268+ f"https://queue.amazonaws.com/{ account_id } /{ queue_name } "
269+ )
250270 req_json ["QueueOwnerAWSAccountId" ] = account_id
251271 request_dict ["body" ] = to_bytes (json .dumps (req_json ))
252272
@@ -256,7 +276,9 @@ def _fix_headers(self, request: Request, service_name: str):
256276 host = request .headers .get ("Host" ) or ""
257277 regex = r"^(https?://)?([0-9.]+|localhost)(:[0-9]+)?"
258278 if re .match (regex , host ):
259- request .headers ["Host" ] = re .sub (regex , rf"\1s3.{ LOCALHOST_HOSTNAME } " , host )
279+ request .headers ["Host" ] = re .sub (
280+ regex , rf"\1s3.{ LOCALHOST_HOSTNAME } " , host
281+ )
260282 request .headers .pop ("Content-Length" , None )
261283 request .headers .pop ("x-localstack-request-url" , None )
262284 request .headers .pop ("X-Forwarded-For" , None )
@@ -311,7 +333,9 @@ def start_aws_auth_proxy_in_container(
311333 # should consider building pre-baked images for the extension in the future. Also,
312334 # the new packaged CLI binary can help us gain more stability over time...
313335
314- logging .getLogger ("localstack.utils.container_utils.docker_cmd_client" ).setLevel (logging .INFO )
336+ logging .getLogger ("localstack.utils.container_utils.docker_cmd_client" ).setLevel (
337+ logging .INFO
338+ )
315339 logging .getLogger ("localstack.utils.docker_utils" ).setLevel (logging .INFO )
316340 logging .getLogger ("localstack.utils.run" ).setLevel (logging .INFO )
317341
@@ -328,13 +352,18 @@ def start_aws_auth_proxy_in_container(
328352 image_name = DOCKER_IMAGE_NAME_PRO
329353 # add host mapping for localstack.cloud to localhost to prevent the health check from failing
330354 additional_flags = (
331- repl_config .PROXY_DOCKER_FLAGS + " --add-host=localhost.localstack.cloud:host-gateway"
355+ repl_config .PROXY_DOCKER_FLAGS
356+ + " --add-host=localhost.localstack.cloud:host-gateway"
332357 )
333358 DOCKER_CLIENT .create_container (
334359 image_name ,
335360 name = container_name ,
336361 entrypoint = "" ,
337- command = ["bash" , "-c" , f"touch { CONTAINER_LOG_FILE } ; tail -f { CONTAINER_LOG_FILE } " ],
362+ command = [
363+ "bash" ,
364+ "-c" ,
365+ f"touch { CONTAINER_LOG_FILE } ; tail -f { CONTAINER_LOG_FILE } " ,
366+ ],
338367 ports = ports ,
339368 additional_flags = additional_flags ,
340369 )
@@ -388,7 +417,10 @@ def start_aws_auth_proxy_in_container(
388417 command = f"{ venv_activate } ; localstack aws proxy -c { CONTAINER_CONFIG_FILE } -p { port } --host 0.0.0.0 > { CONTAINER_LOG_FILE } 2>&1"
389418 if use_docker_sdk_command :
390419 DOCKER_CLIENT .exec_in_container (
391- container_name , command = ["bash" , "-c" , command ], env_vars = env_vars , interactive = True
420+ container_name ,
421+ command = ["bash" , "-c" , command ],
422+ env_vars = env_vars ,
423+ interactive = True ,
392424 )
393425 else :
394426 env_vars_list = []
0 commit comments