def start_tunneling_fake_service(local_host, local_port, namespace, kube_config_path, context): """ Run the client.sh script that sets up a remote tunnel from the cluster back to the fake backend components running locally. """ proc = subprocess.Popen( ["/bin/bash", f"{SCRIPT_DIR}/tunnel/client.sh"], env={ "KUBECONFIG": kube_config_path, "KUBE_CONTEXT": context or "", "LOCAL_HOST": local_host, "LOCAL_PORT": str(local_port), "REMOTE_PORT": str(local_port), "NAMESPACE": namespace, "PATH": os.environ.get("PATH"), }, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, ) get_output = pull_from_reader_in_background(proc.stdout) def term_func(): proc.terminate() proc.wait() return term_func, get_output
def socat_https_proxy(container, target_host, target_port, source_host, bind_addr): cert = "/%s.cert" % source_host key = "/%s.key" % source_host socat_bin = DOCKERFILES_DIR / "socat" stopped = False socket_path = "/tmp/scratch/%s-%s" % (source_host, container.id[:12]) # Keep the socat instance in the container running across container # restarts def keep_running_in_container(cont, sock): while not stopped: try: cont.exec_run([ "socat", "-v", "OPENSSL-LISTEN:443,cert=%s,key=%s,verify=0,bind=%s,fork" % (cert, key, bind_addr), "UNIX-CONNECT:%s" % sock, ]) except docker.errors.APIError: print("socat died, restarting...") time.sleep(0.1) threading.Thread(target=keep_running_in_container, args=(container, socket_path), daemon=True).start() # pylint: disable=consider-using-with proc = retry_on_ebadf(lambda: subprocess.Popen( [ socat_bin, "-v", "UNIX-LISTEN:%s,fork" % socket_path, "TCP4:%s:%d" % (target_host, target_port) ], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, close_fds=False, ))() get_local_out = pull_from_reader_in_background(proc.stdout) try: yield finally: stopped = True # The socat instance in the container will die with the container proc.kill() print(get_local_out())