66import subprocess
77import sys
88import threading
9- from typing import Dict
9+ from typing import Dict , List , Optional
1010from urllib .parse import urlparse
1111
1212from requests .models import Response
4444from localstack .utils .functions import empty_context_manager
4545from localstack .utils .http import parse_request_data
4646from localstack .utils .http import safe_requests as requests
47- from localstack .utils .net import is_port_open
4847from localstack .utils .run import is_root , run
4948from localstack .utils .server .http2_server import HTTPErrorResponse
5049from localstack .utils .server .proxy_server import start_tcp_proxy
5150from localstack .utils .strings import to_bytes , truncate
52- from localstack .utils .sync import sleep_forever
5351from localstack .utils .threads import TMP_THREADS , start_thread
5452
5553LOG = logging .getLogger (__name__ )
@@ -356,7 +354,6 @@ def is_trace_logging_enabled(headers) -> bool:
356354
357355
358356def do_start_edge (bind_address , port , use_ssl , asynchronous = False ):
359- start_dns_server (asynchronous = True )
360357 if config .LEGACY_EDGE_PROXY :
361358 serve = do_start_edge_proxy
362359 else :
@@ -411,44 +408,11 @@ def ensure_can_use_sudo():
411408def start_component (component : str , port = None ):
412409 if component == "edge" :
413410 return start_edge (port = port )
414- if component == "dns" :
415- return start_dns_server ()
416411 if component == "proxy" :
417412 return start_proxy (port = port )
418413 raise Exception ("Unexpected component name '%s' received during start up" % component )
419414
420415
421- def start_dns_server (asynchronous = False ):
422- try :
423- # start local DNS server, if present
424- from localstack_ext import config as config_ext
425- from localstack_ext .services import dns_server
426-
427- if config_ext .DNS_ADDRESS in config .FALSE_STRINGS :
428- return
429-
430- if is_port_open (PORT_DNS ):
431- return
432-
433- if is_root ():
434- result = dns_server .start_servers ()
435- if not asynchronous :
436- sleep_forever ()
437- return result
438-
439- env_vars = {}
440- for env_var in config .CONFIG_ENV_VARS :
441- if env_var .startswith ("DNS_" ):
442- value = os .environ .get (env_var , None )
443- if value is not None :
444- env_vars [env_var ] = value
445-
446- # note: running in a separate process breaks integration with Route53 (to be fixed for local dev mode!)
447- return run_process_as_sudo ("dns" , PORT_DNS , asynchronous = asynchronous , env_vars = env_vars )
448- except Exception :
449- pass
450-
451-
452416def start_proxy (port , asynchronous = False ):
453417 """
454418 Starts a TCP proxy to perform a low-level forwarding of incoming requests.
@@ -511,17 +475,16 @@ def stop(self, quiet=True):
511475 "EDGE_FORWARD_URL" : config .get_edge_url (),
512476 "EDGE_BIND_HOST" : config .EDGE_BIND_HOST ,
513477 }
514- return run_process_as_sudo ("proxy" , port , env_vars = env_vars , asynchronous = asynchronous )
515-
478+ proxy_module = "localstack.services.edge"
479+ proxy_args = ["proxy" , str (port )]
480+ return run_module_as_sudo (
481+ module = proxy_module , arguments = proxy_args , env_vars = env_vars , asynchronous = asynchronous
482+ )
516483
517- def run_process_as_sudo (component , port , asynchronous = False , env_vars = None ):
518- # make sure we can run sudo commands
519- try :
520- ensure_can_use_sudo ()
521- except Exception as e :
522- LOG .error ("cannot start service on privileged port %s: %s" , port , str (e ))
523- return
524484
485+ def run_module_as_sudo (
486+ module : str , arguments : Optional [List [str ]] = None , asynchronous = False , env_vars = None
487+ ):
525488 # prepare environment
526489 env_vars = env_vars or {}
527490 env_vars ["PYTHONPATH" ] = f".:{ LOCALSTACK_ROOT_FOLDER } "
@@ -530,16 +493,16 @@ def run_process_as_sudo(component, port, asynchronous=False, env_vars=None):
530493 # start the process as sudo
531494 sudo_cmd = "sudo -n"
532495 python_cmd = sys .executable
533- cmd = [
534- sudo_cmd ,
535- env_vars_str ,
536- python_cmd ,
537- "-m" ,
538- "localstack.services.edge" ,
539- component ,
540- str ( port ),
541- ]
542- shell_cmd = " " . join ( cmd )
496+ cmd = [sudo_cmd , env_vars_str , python_cmd , "-m" , module ]
497+ arguments = arguments or []
498+ shell_cmd = " " . join ( cmd + arguments )
499+
500+ # make sure we can run sudo commands
501+ try :
502+ ensure_can_use_sudo ()
503+ except Exception as e :
504+ LOG . error ( "cannot run command as root (%s): %s " , str ( e ), shell_cmd )
505+ return
543506
544507 def run_command (* _ ):
545508 run (shell_cmd , outfile = subprocess .PIPE , print_error = False , env_vars = env_vars )
0 commit comments