| 8 | SINGLE_CASE_TIMEOUT = 600 |
| 9 | |
| 10 | class CloudflaredCli: |
| 11 | def __init__(self, config, config_path, logger): |
| 12 | self.basecmd = [config.cloudflared_binary, "tunnel"] |
| 13 | if config_path is not None: |
| 14 | self.basecmd += ["--config", str(config_path)] |
| 15 | origincert = get_config_from_file()["origincert"] |
| 16 | if origincert: |
| 17 | self.basecmd += ["--origincert", origincert] |
| 18 | self.logger = logger |
| 19 | |
| 20 | def _run_command(self, subcmd, subcmd_name, needs_to_pass=True): |
| 21 | cmd = self.basecmd + subcmd |
| 22 | # timeout limits the time a subprocess can run. This is useful to guard against running a tunnel when |
| 23 | # command/args are in wrong order. |
| 24 | result = run_subprocess(cmd, subcmd_name, self.logger, check=needs_to_pass, capture_output=True, timeout=15) |
| 25 | return result |
| 26 | |
| 27 | def list_tunnels(self): |
| 28 | cmd_args = ["list", "--output", "json"] |
| 29 | listed = self._run_command(cmd_args, "list") |
| 30 | return json.loads(listed.stdout) |
| 31 | |
| 32 | def get_management_token(self, config, config_path, resource): |
| 33 | basecmd = [config.cloudflared_binary] |
| 34 | if config_path is not None: |
| 35 | basecmd += ["--config", str(config_path)] |
| 36 | origincert = get_config_from_file()["origincert"] |
| 37 | if origincert: |
| 38 | basecmd += ["--origincert", origincert] |
| 39 | |
| 40 | cmd_args = ["management", "token", "--resource", resource, config.get_tunnel_id()] |
| 41 | cmd = basecmd + cmd_args |
| 42 | result = run_subprocess(cmd, "token", self.logger, check=True, capture_output=True, timeout=15) |
| 43 | return json.loads(result.stdout.decode("utf-8").strip())["token"] |
| 44 | |
| 45 | def get_tail_token(self, config, config_path): |
| 46 | """ |
| 47 | Get management token using the 'tail token' command. |
| 48 | Returns a token scoped for 'logs' resource. |
| 49 | """ |
| 50 | basecmd = [config.cloudflared_binary] |
| 51 | if config_path is not None: |
| 52 | basecmd += ["--config", str(config_path)] |
| 53 | origincert = get_config_from_file()["origincert"] |
| 54 | if origincert: |
| 55 | basecmd += ["--origincert", origincert] |
| 56 | |
| 57 | cmd_args = ["tail", "token", config.get_tunnel_id()] |
| 58 | cmd = basecmd + cmd_args |
| 59 | result = run_subprocess(cmd, "tail-token", self.logger, check=True, capture_output=True, timeout=15) |
| 60 | return json.loads(result.stdout.decode("utf-8").strip())["token"] |
| 61 | |
| 62 | def get_management_url(self, path, config, config_path, resource): |
| 63 | access_jwt = self.get_management_token(config, config_path, resource) |
| 64 | connector_id = get_tunnel_connector_id() |
| 65 | return f"https://{MANAGEMENT_HOST_NAME}/{path}?connector_id={connector_id}&access_token={access_jwt}" |
| 66 | |
| 67 | def get_management_wsurl(self, path, config, config_path, resource): |
no outgoing calls
searching dependent graphs…