cloudflare/cloudflared
Publicmirrored from https://github.com/cloudflare/cloudflaredAvailable
component-tests/cli.py
127lines · modeblame
e921ab35Devin Carr4 years ago | 1 | import json |
| 2 | import subprocess | |
| 3 | from time import sleep | |
| 4 | | |
ee5e447dDevin Carr3 years ago | 5 | from constants import MANAGEMENT_HOST_NAME |
e921ab35Devin Carr4 years ago | 6 | from setup import get_config_from_file |
ee5e447dDevin Carr3 years ago | 7 | from util import get_tunnel_connector_id |
e921ab35Devin Carr4 years ago | 8 | |
| 9 | SINGLE_CASE_TIMEOUT = 600 | |
| 10 | | |
| 11 | class CloudflaredCli: | |
| 12 | def __init__(self, config, config_path, logger): | |
| 13 | self.basecmd = [config.cloudflared_binary, "tunnel"] | |
| 14 | if config_path is not None: | |
| 15 | self.basecmd += ["--config", str(config_path)] | |
| 16 | origincert = get_config_from_file()["origincert"] | |
| 17 | if origincert: | |
| 18 | self.basecmd += ["--origincert", origincert] | |
| 19 | self.logger = logger | |
| 20 | | |
| 21 | def _run_command(self, subcmd, subcmd_name, needs_to_pass=True): | |
| 22 | cmd = self.basecmd + subcmd | |
| 23 | # timeout limits the time a subprocess can run. This is useful to guard against running a tunnel when | |
| 24 | # command/args are in wrong order. | |
| 25 | result = run_subprocess(cmd, subcmd_name, self.logger, check=needs_to_pass, capture_output=True, timeout=15) | |
| 26 | return result | |
| 27 | | |
| 28 | def list_tunnels(self): | |
| 29 | cmd_args = ["list", "--output", "json"] | |
| 30 | listed = self._run_command(cmd_args, "list") | |
| 31 | return json.loads(listed.stdout) | |
| 32 | | |
5e212a6bSudarsan Reddy3 years ago | 33 | def get_management_token(self, config, config_path): |
| 34 | basecmd = [config.cloudflared_binary] | |
| 35 | if config_path is not None: | |
| 36 | basecmd += ["--config", str(config_path)] | |
| 37 | origincert = get_config_from_file()["origincert"] | |
| 38 | if origincert: | |
| 39 | basecmd += ["--origincert", origincert] | |
| 40 | | |
| 41 | cmd_args = ["tail", "token", config.get_tunnel_id()] | |
| 42 | cmd = basecmd + cmd_args | |
| 43 | result = run_subprocess(cmd, "token", self.logger, check=True, capture_output=True, timeout=15) | |
| 44 | return json.loads(result.stdout.decode("utf-8").strip())["token"] | |
ee5e447dDevin Carr3 years ago | 45 | |
| 46 | def get_management_url(self, path, config, config_path): | |
| 47 | access_jwt = self.get_management_token(config, config_path) | |
| 48 | connector_id = get_tunnel_connector_id() | |
| 49 | return f"https://{MANAGEMENT_HOST_NAME}/{path}?connector_id={connector_id}&access_token={access_jwt}" | |
| 50 | | |
| 51 | def get_management_wsurl(self, path, config, config_path): | |
| 52 | access_jwt = self.get_management_token(config, config_path) | |
| 53 | connector_id = get_tunnel_connector_id() | |
| 54 | return f"wss://{MANAGEMENT_HOST_NAME}/{path}?connector_id={connector_id}&access_token={access_jwt}" | |
5e212a6bSudarsan Reddy3 years ago | 55 | |
| 56 | def get_connector_id(self, config): | |
| 57 | op = self.get_tunnel_info(config.get_tunnel_id()) | |
| 58 | connectors = [] | |
| 59 | for conn in op["conns"]: | |
| 60 | connectors.append(conn["id"]) | |
| 61 | return connectors | |
| 62 | | |
e921ab35Devin Carr4 years ago | 63 | def get_tunnel_info(self, tunnel_id): |
| 64 | info = self._run_command(["info", "--output", "json", tunnel_id], "info") | |
| 65 | return json.loads(info.stdout) | |
| 66 | | |
| 67 | def __enter__(self): | |
| 68 | self.basecmd += ["run"] | |
| 69 | self.process = subprocess.Popen(self.basecmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | |
| 70 | self.logger.info(f"Run cmd {self.basecmd}") | |
| 71 | return self.process | |
| 72 | | |
| 73 | def __exit__(self, exc_type, exc_value, exc_traceback): | |
| 74 | terminate_gracefully(self.process, self.logger, self.basecmd) | |
| 75 | self.logger.debug(f"{self.basecmd} logs: {self.process.stderr.read()}") | |
| 76 | | |
| 77 | | |
| 78 | def terminate_gracefully(process, logger, cmd): | |
| 79 | process.terminate() | |
| 80 | process_terminated = wait_for_terminate(process) | |
| 81 | if not process_terminated: | |
| 82 | process.kill() | |
| 83 | logger.warning(f"{cmd}: cloudflared did not terminate within wait period. Killing process. logs: \ | |
| 84 | stdout: {process.stdout.read()}, stderr: {process.stderr.read()}") | |
| 85 | | |
| 86 | | |
| 87 | def wait_for_terminate(opened_subprocess, attempts=10, poll_interval=1): | |
| 88 | """ | |
| 89 | wait_for_terminate polls the opened_subprocess every x seconds for a given number of attempts. | |
| 90 | It returns true if the subprocess was terminated and false if it didn't. | |
| 91 | """ | |
| 92 | for _ in range(attempts): | |
| 93 | if _is_process_stopped(opened_subprocess): | |
| 94 | return True | |
| 95 | sleep(poll_interval) | |
| 96 | return False | |
| 97 | | |
| 98 | | |
| 99 | def _is_process_stopped(process): | |
| 100 | return process.poll() is not None | |
| 101 | | |
| 102 | | |
| 103 | def cert_path(): | |
| 104 | return get_config_from_file()["origincert"] | |
| 105 | | |
| 106 | | |
| 107 | class SubprocessError(Exception): | |
| 108 | def __init__(self, program, exit_code, cause): | |
| 109 | self.program = program | |
| 110 | self.exit_code = exit_code | |
| 111 | self.cause = cause | |
| 112 | | |
| 113 | | |
| 114 | def run_subprocess(cmd, cmd_name, logger, timeout=SINGLE_CASE_TIMEOUT, **kargs): | |
| 115 | kargs["timeout"] = timeout | |
| 116 | try: | |
| 117 | result = subprocess.run(cmd, **kargs) | |
| 118 | logger.debug(f"{cmd} log: {result.stdout}", extra={"cmd": cmd_name}) | |
| 119 | return result | |
| 120 | except subprocess.CalledProcessError as e: | |
| 121 | err = f"{cmd} return exit code {e.returncode}, stderr" + e.stderr.decode("utf-8") | |
| 122 | logger.error(err, extra={"cmd": cmd_name, "return_code": e.returncode}) | |
| 123 | raise SubprocessError(cmd[0], e.returncode, e) | |
| 124 | except subprocess.TimeoutExpired as e: | |
| 125 | err = f"{cmd} timeout after {e.timeout} seconds, stdout: {e.stdout}, stderr: {e.stderr}" | |
| 126 | logger.error(err, extra={"cmd": cmd_name, "return_code": "timeout"}) | |
| 127 | raise e |