From 2da9b4554822db41288c56ca1301ad75dc8faddc Mon Sep 17 00:00:00 2001 From: Fede Luzzi Date: Wed, 28 Oct 2026 14:22:24 -0300 Subject: [PATCH] feat(release): v6.4.0b2 - support Mosh SSH backend for jumphosts & capture, silence google.api_core FutureWarning, update pdoc - Allow Mosh nodes to act as single or chained jumphosts via their underlying SSH TCP backend (ssh -W) with strict mosh_ssh_options allowlist validation and IPv6 bracket formatting. - Allow RemoteCapture (conn capture) on Mosh nodes using reverse SSH tunnels (force_ssh=True, allow_reverse_tunnel_port) and isolated tags deepcopy restoration. - Suppress google.api_core._python_version_support FutureWarning during lazy Google Drive SDK imports in sync_service.py. - Add unit and security test coverage in test_core.py, test_capture.py, and test_sync.py. - Bump version to 6.4.0b2 and regenerate pdoc HTML documentation. --- README.md | 6 +- connpy/__init__.py | 6 +- connpy/_version.py | 2 +- connpy/cli/help_text.py | 4 +- connpy/core.py | 440 ++++++++++++++++++++++++++++---- connpy/core_plugins/capture.py | 163 +++++++----- connpy/services/sync_service.py | 87 ++++--- connpy/tests/test_capture.py | 187 ++++++++++++++ connpy/tests/test_core.py | 269 +++++++++++++++++-- connpy/tests/test_sync.py | 174 ++++++++++++- docs/connpy/cli/help_text.html | 4 +- docs/connpy/index.html | 405 ++++++++++++++++++++++++----- 12 files changed, 1514 insertions(+), 233 deletions(-) diff --git a/README.md b/README.md index 5a263c0..58bb362 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@

-# Connpy (v6.4.0b1) +# Connpy (v6.4.0b2) [![](https://img.shields.io/pypi/v/connpy.svg?style=flat-square)](https://pypi.org/pypi/connpy/) [![](https://img.shields.io/pypi/pyversions/connpy.svg?style=flat-square)](https://pypi.org/pypi/connpy/) [![](https://img.shields.io/pypi/dm/connpy.svg?style=flat-square&cacheSeconds=86400)](https://pypi.org/pypi/connpy/) @@ -144,7 +144,7 @@ Connect to various architectures using native protocols: * **AWS SSM**: Connect to EC2 instances using Instance IDs as hosts. ### 4b. Jumphosts -Support for single or chained intermediate gateway nodes (SSH, SSM, kubectl, or docker jumphosts) to tunnel traffic safely into target environments. +Support for single or chained intermediate gateway nodes (SSH, Mosh, SSM, kubectl, or docker jumphosts) to tunnel traffic safely into target environments. ### 4c. Debug Mode, Keepalive, Logging Track connection steps (`conn --debug node`), set idle keepalive intervals (`conn config --keepalive `), or define dynamic output log files using variables like `${unique}`, `${host}`, `${port}`, `${user}`, `${protocol}`, or `${date 'format'}`. @@ -153,7 +153,7 @@ Track connection steps (`conn --debug node`), set idle keepalive intervals (`con --- ## 5. 🖥️ Remote Capture (conn capture - Core Plugin) -Perform remote packet capture (`tcpdump`) on hosts over secure SSH reverse tunnels and stream packets live into your local Wireshark GUI: +Perform remote packet capture (`tcpdump`) on hosts over secure SSH or Mosh reverse tunnels and stream packets live into your local Wireshark GUI: ```bash conn capture router1 eth0 -w -f "port 80" ``` diff --git a/connpy/__init__.py b/connpy/__init__.py index ff995c8..75418a7 100644 --- a/connpy/__init__.py +++ b/connpy/__init__.py @@ -5,7 +5,7 @@

-# Connpy (v6.4.0b1) +# Connpy (v6.4.0b2) [![](https://img.shields.io/pypi/v/connpy.svg?style=flat-square)](https://pypi.org/pypi/connpy/) [![](https://img.shields.io/pypi/pyversions/connpy.svg?style=flat-square)](https://pypi.org/pypi/connpy/) [![](https://img.shields.io/pypi/dm/connpy.svg?style=flat-square&cacheSeconds=86400)](https://pypi.org/pypi/connpy/) @@ -146,7 +146,7 @@ Connect to various architectures using native protocols: * **AWS SSM**: Connect to EC2 instances using Instance IDs as hosts. ### 4b. Jumphosts -Support for single or chained intermediate gateway nodes (SSH, SSM, kubectl, or docker jumphosts) to tunnel traffic safely into target environments. +Support for single or chained intermediate gateway nodes (SSH, Mosh, SSM, kubectl, or docker jumphosts) to tunnel traffic safely into target environments. ### 4c. Debug Mode, Keepalive, Logging Track connection steps (`conn --debug node`), set idle keepalive intervals (`conn config --keepalive `), or define dynamic output log files using variables like `${unique}`, `${host}`, `${port}`, `${user}`, `${protocol}`, or `${date 'format'}`. @@ -155,7 +155,7 @@ Track connection steps (`conn --debug node`), set idle keepalive intervals (`con --- ## 5. 🖥️ Remote Capture (conn capture - Core Plugin) -Perform remote packet capture (`tcpdump`) on hosts over secure SSH reverse tunnels and stream packets live into your local Wireshark GUI: +Perform remote packet capture (`tcpdump`) on hosts over secure SSH or Mosh reverse tunnels and stream packets live into your local Wireshark GUI: ```bash conn capture router1 eth0 -w -f "port 80" ``` diff --git a/connpy/_version.py b/connpy/_version.py index 5cd848d..1950894 100644 --- a/connpy/_version.py +++ b/connpy/_version.py @@ -1 +1 @@ -__version__ = "6.4.0b1" +__version__ = "6.4.0b2" diff --git a/connpy/cli/help_text.py b/connpy/cli/help_text.py index 429bb04..bb40687 100644 --- a/connpy/cli/help_text.py +++ b/connpy/cli/help_text.py @@ -29,7 +29,7 @@ Here are some important instructions and tips for configuring your new node: 2. **Available Protocols and Apps**: - ssh - - mosh (requires remote mosh-server and UDP ports 60000-61000 open; --sftp uses SSH TCP) + - mosh (requires remote mosh-server and UDP ports 60000-61000 open; supports SSH backend fallback for jumphosts, capture, and --sftp) - telnet - kubectl (`kubectl exec`) - docker (`docker exec`) @@ -62,7 +62,7 @@ Here are some important instructions and tips for configuring your new node: - `nc_command`: Replaces the default `nc` command used when bridging connections through Docker or Kubernetes (e.g., `ip netns exec global-vrf nc`). - `mosh_udp_port`: Server-side UDP port or range (`PORT` or `PORT1:PORT2`) passed as `-p` to `mosh`. - `mosh_server`: Path to the `mosh-server` binary on the remote host (`--server`). - - `mosh_ssh_options`: Additional SSH options injected into `mosh --ssh="ssh ..."` and reused when connecting via `--sftp` or running batch commands (`run`/`test`). + - `mosh_ssh_options`: Additional SSH options injected into `mosh --ssh="ssh ..."` and reused when connecting via `--sftp`, as a jumphost, for remote capture, or running batch commands (`run`/`test`). """ if type == "bashcompletion": return ''' diff --git a/connpy/core.py b/connpy/core.py index 309ea09..a4de4f5 100755 --- a/connpy/core.py +++ b/connpy/core.py @@ -2,6 +2,7 @@ #Imports import os import re +import ipaddress import pexpect import shlex import ast @@ -41,6 +42,48 @@ def copilot_terminal_mode(): except Exception: pass +_ALLOWED_SSH_NOARG_FLAGS = frozenset({ + "-4", "-6", "-A", "-C", "-X", "-Y", "-a", "-q", "-t", "-v", "-vv", "-vvv", "-x", +}) +_NON_TUNNEL_SSH_FLAGS = frozenset({ + "-A", "-X", "-Y", "-t", "-v", "-vv", "-vvv", "-x", +}) +_ALLOWED_SSH_ARG_FLAGS = frozenset({ + "-c", "-i", "-m", +}) +_ALLOWED_SSH_O_KEYS = frozenset({ + "addkeystoagent", + "ciphers", + "compression", + "connectionattempts", + "connecttimeout", + "fingerprinthash", + "forwardagent", + "gssapiauthentication", + "hostkeyalgorithms", + "identitiesonly", + "identityfile", + "ipqos", + "kbdinteractiveauthentication", + "kexalgorithms", + "loglevel", + "macs", + "numberofpasswordprompts", + "preferredauthentications", + "pubkeyacceptedalgorithms", + "pubkeyacceptedkeytypes", + "pubkeyauthentication", + "serveralivecountmax", + "serveraliveinterval", + "stricthostkeychecking", + "tcpkeepalive", + "userknownhostsfile", + "verifyhostkeydns", +}) +_SAFE_SSH_PATH_RE = re.compile(r"^[A-Za-z0-9._/@:,+~= -]+$", re.ASCII) +_SAFE_SSH_O_VALUE_RE = re.compile(r"^[A-Za-z0-9._/@:,+~=-]+$", re.ASCII) +_LEGACY_SSH_UNSAFE_RE = re.compile(r'[\s\x00-\x1f\x7f;|&$`><"\']') + #functions and classes @ClassHook class node: @@ -109,7 +152,7 @@ class node: profile = re.search("^@(.*)", str(attr[key])) if profile and config != '': try: - setattr(self,key,config.profiles[profile.group(1)][key]) + setattr(self,key,deepcopy(config.profiles[profile.group(1)][key])) except KeyError: setattr(self,key,"") elif attr[key] == '' and key == "protocol": @@ -152,7 +195,7 @@ class node: profile = re.search("^@(.*)", str(jh_dict[key])) if profile: try: - jh_dict[key] = config.profiles[profile.group(1)][key] + jh_dict[key] = deepcopy(config.profiles[profile.group(1)][key]) except KeyError: jh_dict[key] = "" elif jh_dict[key] == '' and key == "protocol": @@ -173,6 +216,242 @@ class node: jh_dict["password"] = [jh_dict["password"]] return jh_dict + @staticmethod + def _validate_legacy_ssh_hop_fields(host, user, port): + """Minimal injection guard for legacy SSH jumphost fields (preserves DOMAIN\\user, %scope, etc.).""" + host_s = str(host) if host is not None else "" + if host_s.startswith("-"): + raise ValueError("Host cannot start with '-'") + if not host_s or _LEGACY_SSH_UNSAFE_RE.search(host_s): + raise ValueError(f"Invalid characters in host: '{host}'") + host_bare = ( + host_s[1:-1] + if (host_s.startswith("[") and host_s.endswith("]")) + else host_s + ) + if not host_bare or host_bare.startswith("-"): + raise ValueError(f"Invalid characters in host: '{host}'") + + user_s = str(user) if user is not None else "" + if user_s.startswith("-"): + raise ValueError("User cannot start with '-'") + if user_s and _LEGACY_SSH_UNSAFE_RE.search(user_s): + raise ValueError(f"Invalid characters in user: '{user}'") + + port_out = "" + if port != "" and port is not None: + if isinstance(port, (bool, float)): + raise ValueError(f"Invalid TCP SSH port '{port}'") + port_s = str(port).strip() + if port_s: + if ( + not port_s.isascii() + or not re.fullmatch(r"[0-9]+", port_s, re.ASCII) + or not (1 <= int(port_s) <= 65535) + ): + raise ValueError(f"Invalid TCP SSH port '{port}'") + port_out = port_s + + return host_bare, port_out + + @staticmethod + def _validate_mosh_ssh_fields( + host, + user, + port, + tags, + *, + strip_non_tunnel_flags=False, + allow_reverse_tunnel_port=None, + ): + """Validate host, user, TCP SSH port, and mosh_ssh_options for Mosh SSH backend. + + Accepts IPv4, hostnames, and bare or bracketed IPv6 literals (including + IPv4-mapped IPv6 like ::ffff:1.2.3.4, without scope-id '%'). + Returns: + tuple[str, str, str]: (host_bare_str, validated_port_str, validated_ssh_opts_str) + """ + host_s = str(host) if host is not None else "" + if host_s.startswith("-"): + raise ValueError("Host cannot start with '-'") + if not host_s: + raise ValueError(f"Invalid characters in host: '{host}'") + + if (host_s.startswith("[") and host_s.endswith("]")) or ":" in host_s: + inner = host_s[1:-1] if (host_s.startswith("[") and host_s.endswith("]")) else host_s + if "%" in inner or not re.fullmatch(r"[0-9A-Fa-f:.]+", inner, re.ASCII): + raise ValueError(f"Invalid characters in host: '{host}'") + try: + ipaddress.IPv6Address(inner) + except ValueError as exc: + raise ValueError(f"Invalid characters in host: '{host}'") from exc + host_bare = inner + else: + if not re.fullmatch(r"[A-Za-z0-9._-]+", host_s, re.ASCII): + raise ValueError(f"Invalid characters in host: '{host}'") + host_bare = host_s + + user_s = str(user) if user is not None else "" + if user_s.startswith("-"): + raise ValueError("User cannot start with '-'") + if user_s and not re.fullmatch( + r"[A-Za-z0-9._-]+(?:@[A-Za-z0-9._-]+)?", user_s, re.ASCII + ): + raise ValueError(f"Invalid characters in user: '{user}'") + + port_out = "" + if port != "" and port is not None: + if isinstance(port, (bool, float)): + raise ValueError(f"Invalid TCP SSH port '{port}'") + port_s = str(port).strip() + if port_s: + if ( + not port_s.isascii() + or not re.fullmatch(r"[0-9]+", port_s, re.ASCII) + or not (1 <= int(port_s) <= 65535) + ): + raise ValueError(f"Invalid TCP SSH port '{port}'") + port_out = port_s + + allowed_r_port = None + if allow_reverse_tunnel_port is not None: + r_port_s = str(allow_reverse_tunnel_port).strip() + if ( + isinstance(allow_reverse_tunnel_port, (bool, float)) + or not r_port_s.isascii() + or not re.fullmatch(r"[0-9]+", r_port_s, re.ASCII) + or not (1 <= int(r_port_s) <= 65535) + ): + raise ValueError( + f"Invalid allow_reverse_tunnel_port '{allow_reverse_tunnel_port}'" + ) + allowed_r_port = r_port_s + + opts_out = "" + raw_opts = tags.get("mosh_ssh_options", "") if isinstance(tags, dict) else "" + if raw_opts: + ssh_opts = str(raw_opts).strip() + if "\x00" in ssh_opts or "\n" in ssh_opts or "\r" in ssh_opts: + raise ValueError("Disallowed control characters in mosh_ssh_options") + if ssh_opts: + try: + tokens = shlex.split(ssh_opts) + except ValueError as exc: + raise ValueError( + "Invalid mosh_ssh_options: unbalanced quotes" + ) from exc + r_count = 0 + exit_fwd_count = 0 + out_tokens = [] + idx = 0 + # Normative branch evaluation order: + # 1. NOARG -> 2. ARG separate -> 3. ARG attached -> 4. -R -> 5. -o/-oXXX -> 6. other + while idx < len(tokens): + tok = tokens[idx] + if tok in _ALLOWED_SSH_NOARG_FLAGS: + if not (strip_non_tunnel_flags and tok in _NON_TUNNEL_SSH_FLAGS): + out_tokens.append(tok) + idx += 1 + elif tok in _ALLOWED_SSH_ARG_FLAGS: + if idx + 1 >= len(tokens) or not tokens[idx + 1].strip(): + raise ValueError( + f"Missing argument for ssh option '{tok}' in mosh_ssh_options" + ) + arg_val = tokens[idx + 1] + val_re = _SAFE_SSH_PATH_RE if tok == "-i" else _SAFE_SSH_O_VALUE_RE + if arg_val.startswith("-") or not val_re.fullmatch(arg_val): + raise ValueError( + f"Invalid argument for ssh option '{tok}' in mosh_ssh_options" + ) + out_tokens.extend([tok, arg_val]) + idx += 2 + elif tok[:2] in _ALLOWED_SSH_ARG_FLAGS and len(tok) > 2: + flag_prefix = tok[:2] + arg_val = tok[2:] + val_re = _SAFE_SSH_PATH_RE if flag_prefix == "-i" else _SAFE_SSH_O_VALUE_RE + if ( + not arg_val.strip() + or arg_val.startswith("-") + or not val_re.fullmatch(arg_val) + ): + raise ValueError( + f"Invalid argument for ssh option '{flag_prefix}' in mosh_ssh_options" + ) + out_tokens.append(tok) + idx += 1 + elif tok == "-R": + if ( + allowed_r_port is None + or r_count >= 1 + or idx + 1 >= len(tokens) + or tokens[idx + 1] + != f"{allowed_r_port}:localhost:{allowed_r_port}" + ): + raise ValueError( + f"Disallowed ssh option in mosh_ssh_options: '{tok}'" + ) + r_count += 1 + out_tokens.extend([tok, tokens[idx + 1]]) + idx += 2 + elif tok == "-o" or tok.startswith("-o"): + if tok == "-o": + if idx + 1 >= len(tokens) or not tokens[idx + 1].strip(): + raise ValueError( + "Missing argument for ssh option '-o' in mosh_ssh_options" + ) + opt_spec = tokens[idx + 1] + step = 2 + else: + opt_spec = tok[2:] + step = 1 + if re.search(r"(?:localcommand|proxycommand)", opt_spec, re.IGNORECASE): + raise ValueError( + "mosh_ssh_options cannot contain LocalCommand or ProxyCommand" + ) + m = re.fullmatch(r"([A-Za-z0-9]+)=(.+)", opt_spec, re.ASCII) + if ( + not m + or not m.group(2).strip() + or not _SAFE_SSH_O_VALUE_RE.fullmatch(m.group(2).strip()) + ): + raise ValueError( + f"Disallowed -o directive in mosh_ssh_options: '{opt_spec}'" + ) + key_raw = m.group(1) + key_lower = key_raw.lower() + val_str = m.group(2).strip() + if key_lower == "exitonforwardfailure": + if ( + allowed_r_port is None + or exit_fwd_count >= 1 + or val_str.lower() != "yes" + ): + raise ValueError( + f"Disallowed -o directive in mosh_ssh_options: '{opt_spec}'" + ) + exit_fwd_count += 1 + elif key_lower not in _ALLOWED_SSH_O_KEYS: + raise ValueError( + f"Disallowed -o directive in mosh_ssh_options: '{opt_spec}'" + ) + if not (strip_non_tunnel_flags and key_lower == "forwardagent"): + if tok == "-o": + out_tokens.extend(["-o", f"{key_raw}={val_str}"]) + else: + out_tokens.append(f"-o{key_raw}={val_str}") + idx += step + else: + if re.search(r"(?:localcommand|proxycommand)", tok, re.IGNORECASE): + raise ValueError( + "mosh_ssh_options cannot contain LocalCommand or ProxyCommand" + ) + raise ValueError( + f"Disallowed ssh option in mosh_ssh_options: '{tok}'" + ) + opts_out = shlex.join(out_tokens) + + return host_bare, port_out, opts_out + def _build_jumphost_chain(self, jumphost_name, config, visited=None, depth=0, target_host="%h", target_port="%p"): '''Recursively build ProxyCommand for chained jumphosts. @@ -197,24 +476,39 @@ class node: jh = self._resolve_jumphost_data(jh, config) if jh["protocol"] == "mosh": - raise ValueError( - f"Jumphost '{jumphost_name}' uses protocol 'mosh' which cannot be used as a jumphost." + jh_host, jh_port, jh_opts = self._validate_mosh_ssh_fields( + jh.get("host", ""), + jh.get("user", ""), + jh.get("port", ""), + jh.get("tags", ""), + strip_non_tunnel_flags=True, ) + elif jh["protocol"] == "ssh": + jh_host, jh_port = self._validate_legacy_ssh_hop_fields( + jh.get("host", ""), + jh.get("user", ""), + jh.get("port", ""), + ) + jh_opts = jh.get("options", "") + else: + jh_host = jh.get("host", "") + jh_port = str(jh["port"]).strip() if jh.get("port") not in ("", None) else "" + jh_opts = jh.get("options", "") passwords = [] inner_proxy_opt = "" # Recursively resolve inner jumphost if jh.get("jumphost", "") != "": - if jh["protocol"] not in ["ssh"]: + if jh["protocol"] not in ["ssh", "mosh"]: raise ValueError( f"Jumphost '{jumphost_name}' uses protocol '{jh['protocol']}' " f"which does not support chained jumphosts. " - f"Only SSH jumphosts can have their own jumphosts." + f"Only SSH or Mosh jumphosts can have their own jumphosts." ) - parent_port = jh["port"] if jh["port"] != "" else "22" + parent_port = jh_port if jh_port != "" else "22" inner_raw_cmd, inner_passwords = self._build_jumphost_chain( - jh["jumphost"], config, visited, depth + 1, target_host=jh["host"], target_port=parent_port + jh["jumphost"], config, visited, depth + 1, target_host=jh_host, target_port=parent_port ) passwords = inner_passwords escaped = inner_raw_cmd.replace('\\', '\\\\').replace('"', '\\"') @@ -225,17 +519,31 @@ class node: passwords = passwords + jh["password"] t_port = target_port if target_port != "" else "22" + if jh["protocol"] in ["ssh", "mosh"]: + if target_host == "%h" and t_port == "%p": + w_target = "%h" + w_port = "%p" + else: + t_host_bare, t_port_norm = self._validate_legacy_ssh_hop_fields( + target_host, "", t_port + ) + w_target = f"[{t_host_bare}]" if ":" in t_host_bare else t_host_bare + w_port = t_port_norm if t_port_norm != "" else "22" + else: + w_target = target_host + w_port = t_port # Build raw command based on protocol - if jh["protocol"] == "ssh": - cmd = f"ssh -W {target_host}:{t_port}" + if jh["protocol"] in ["ssh", "mosh"]: + cmd = f"ssh -W {w_target}:{w_port}" if inner_proxy_opt: cmd += f" {inner_proxy_opt}" - if jh["port"] != '': - cmd += f" -p {jh['port']}" - if jh["options"] != '': - cmd += f" {jh['options']}" - user_host = f"{jh['user']}@{jh['host']}" if jh['user'] != '' else jh['host'] + if jh_port != "": + cmd += f" -p {jh_port}" + if jh_opts != "": + cmd += f" {jh_opts}" + jh_user = str(jh.get("user", "") or "") + user_host = f"{jh_user}@{jh_host}" if jh_user else jh_host cmd += f" {user_host}" elif jh["protocol"] == "ssm": ssm_target = jh["host"] @@ -1133,52 +1441,62 @@ class node: return connect @MethodHook - def _generate_ssh_sftp_cmd(self, force_ssh=False): - is_from_mosh = getattr(self, "_orig_protocol", "") == "mosh" - if is_from_mosh: - if str(self.host).startswith("-"): - raise ValueError("Host cannot start with '-'") - if self.user and str(self.user).startswith("-"): - raise ValueError("User cannot start with '-'") - + def _generate_ssh_sftp_cmd(self, force_ssh=False, allow_reverse_tunnel_port=None): + is_from_mosh = ( + self.protocol == "mosh" + or getattr(self, "_orig_protocol", "") == "mosh" + ) proto = "ssh" if force_ssh else self.protocol cmd = proto - if self.port != "" and self.port is not None: - if is_from_mosh: - port_s = str(self.port).strip() - if port_s: - if not re.fullmatch(r"^\d+$", port_s) or not (1 <= int(port_s) <= 65535): - raise ValueError(f"Invalid TCP SSH port '{self.port}'") - cmd += (" -p " if proto == "ssh" else " -P ") + port_s + if is_from_mosh: + host_bare, port_s, raw_opts = self._validate_mosh_ssh_fields( + self.host, + self.user, + self.port, + self.tags, + allow_reverse_tunnel_port=allow_reverse_tunnel_port, + ) + if port_s: + cmd += (" -p " if proto == "ssh" else " -P ") + port_s + if raw_opts and proto == "sftp": + sftp_tokens = [] + toks = shlex.split(raw_opts) + i = 0 + while i < len(toks): + t = toks[i] + if t in ("-X", "-x", "-Y", "-t", "-A", "-a"): + i += 1 + elif t == "-m": + i += 2 + elif t.startswith("-m") and len(t) > 2: + i += 1 + else: + sftp_tokens.append(t) + i += 1 + opts = shlex.join(sftp_tokens) else: + opts = str(raw_opts).strip() if raw_opts else "" + if opts: + cmd += " " + opts + target_h = f"[{host_bare}]" if (proto == "sftp" and ":" in host_bare) else host_bare + else: + if self.port != "" and self.port is not None: if proto == "ssh": cmd += " -p " + str(self.port) elif proto == "sftp": cmd += " -P " + str(self.port) - - if is_from_mosh: - raw_opts = self.tags.get("mosh_ssh_options", "") if isinstance(self.tags, dict) else "" - if raw_opts: - ssh_opts = str(raw_opts).strip() - if re.search(r'(?:LocalCommand|ProxyCommand)', ssh_opts, re.IGNORECASE): - raise ValueError("mosh_ssh_options cannot contain LocalCommand or ProxyCommand") - try: - shlex.split(ssh_opts) - except ValueError as exc: - raise ValueError("Invalid mosh_ssh_options: unbalanced quotes") from exc - else: raw_opts = self.options + if raw_opts: + opts = str(raw_opts).strip() + if proto == "sftp": + opts = re.sub(r'(? None: + """Suppress Google API Core Python version EOL FutureWarning. + + Intentionally inserts an 'ignore' filter at the front of warnings.filters + (overriding prior general filters such as -W error or PYTHONWARNINGS only + for this exact message, category, and module). CPython deduplicates + identical filter tuples in warnings.filters, making repeated calls + idempotent without growing the filter list. + """ + warnings.filterwarnings( + "ignore", + message=_GOOGLE_PY_VERSION_WARNING_MESSAGE, + category=FutureWarning, + module=_GOOGLE_PY_VERSION_WARNING_MODULE, + ) + + +_silence_google_python_version_warning() + + def __getattr__(name: str): - if name == "Credentials": - from google.oauth2.credentials import Credentials - return Credentials - elif name == "Request": - from google.auth.transport.requests import Request - return Request - elif name == "build": - from googleapiclient.discovery import build - return build - elif name == "RefreshError": - from google.auth.exceptions import RefreshError - return RefreshError - elif name == "InstalledAppFlow": - from google_auth_oauthlib.flow import InstalledAppFlow - return InstalledAppFlow - elif name == "MediaFileUpload": - from googleapiclient.http import MediaFileUpload - return MediaFileUpload - elif name == "MediaIoBaseDownload": - from googleapiclient.http import MediaIoBaseDownload - return MediaIoBaseDownload - elif name == "HttpError": - from googleapiclient.errors import HttpError - return HttpError - raise AttributeError(f"module '{__name__}' has no attribute '{name}'") + target = _GOOGLE_LAZY_IMPORTS.get(name) + if target is None: + raise AttributeError(f"module '{__name__}' has no attribute '{name}'") + _silence_google_python_version_warning() + module_name, attr_name = target + module = importlib.import_module(module_name) + value = getattr(module, attr_name) + globals()[name] = value + return value def _get_google_libs(): mod = sys.modules[__name__] diff --git a/connpy/tests/test_capture.py b/connpy/tests/test_capture.py index 14c1b86..4823b08 100644 --- a/connpy/tests/test_capture.py +++ b/connpy/tests/test_capture.py @@ -54,3 +54,190 @@ class TestRemoteCapture: port = rc._find_free_port(20000, 30000) assert 20000 <= port <= 30000 assert mock_is_in_use.call_count == 3 + + def test_capture_module_lazy_loading_and_init_protocols(self, mock_connapp, RemoteCapture): + from connpy.core_plugins.capture import Parser + p = Parser() + assert "SSH or Mosh" in p.parser.format_help() + + # Protocol ssh succeeds + mock_connapp.services.nodes.list_nodes.return_value = ["ssh_node"] + mock_connapp.services.nodes.get_node_details.return_value = {"host": "127.0.0.1", "protocol": "ssh"} + ssh_node = MagicMock() + ssh_node.protocol = "ssh" + ssh_node.unique = "ssh_node" + mock_connapp.node.return_value = ssh_node + rc_ssh = RemoteCapture(mock_connapp, "ssh_node", "eth0") + assert rc_ssh.node.protocol == "ssh" + + # Protocol mosh succeeds + mock_connapp.services.nodes.list_nodes.return_value = ["mosh_node"] + mock_connapp.services.nodes.get_node_details.return_value = {"host": "127.0.0.1", "protocol": "mosh"} + mosh_node = MagicMock() + mosh_node.protocol = "mosh" + mosh_node.unique = "mosh_node" + mock_connapp.node.return_value = mosh_node + rc_mosh = RemoteCapture(mock_connapp, "mosh_node", "eth0") + assert rc_mosh.node.protocol == "mosh" + + # Disallowed protocols exit with 2 + for proto in ["telnet", "docker", "ssm", "kubectl", "local"]: + bad_node = MagicMock() + bad_node.protocol = proto + bad_node.unique = f"{proto}_node" + mock_connapp.node.return_value = bad_node + mock_connapp.services.nodes.list_nodes.return_value = [f"{proto}_node"] + mock_connapp.services.nodes.get_node_details.return_value = {"host": "127.0.0.1", "protocol": proto} + with patch("connpy.printer.error") as mock_err: + with pytest.raises(SystemExit) as exc: + RemoteCapture(mock_connapp, f"{proto}_node", "eth0") + assert exc.value.code == 2 + assert mock_err.called + + def test_run_mosh_node_prepends_tunnel_into_copied_tags_forces_ssh_and_restores_state(self, mock_connapp, RemoteCapture): + orig_tags = {"mosh_ssh_options": "-i /tmp/key -4"} + orig_options = "" + node_mock = MagicMock() + node_mock.protocol = "mosh" + node_mock.unique = "mosh_box" + node_mock.host = "10.0.0.1" + node_mock.user = "admin" + node_mock.port = "22" + node_mock.tags = orig_tags + node_mock.options = orig_options + + connect_tags_observed = [] + connect_call_args = [] + + def fake_connect(*args, **kwargs): + connect_tags_observed.append(dict(node_mock.tags)) + connect_call_args.append((args, kwargs)) + return True + + node_mock._connect.side_effect = fake_connect + node_mock.child = MagicMock() + + mock_connapp.services.nodes.list_nodes.return_value = ["mosh_box"] + mock_connapp.services.nodes.get_node_details.return_value = {"host": "10.0.0.1", "protocol": "mosh"} + mock_connapp.node.return_value = node_mock + + rc = RemoteCapture(mock_connapp, "mosh_box", "eth0") + with patch.object(rc, "_find_free_port", return_value=25555), \ + patch.object(rc, "_detect_sudo_requirement", return_value=False), \ + patch.object(rc, "_start_local_listener"), \ + patch.object(rc, "_sendline_until_connected", return_value=True), \ + patch("time.sleep"): + def stop_loop(*_a, **_kw): + rc.listener_active = False + rc.listener_conn = None + rc._start_local_listener.side_effect = stop_loop + rc.run() + + # Verify force_ssh and allow_reverse_tunnel_port + assert len(connect_call_args) == 1 + _, kwargs = connect_call_args[0] + assert kwargs.get("force_ssh") is True + assert kwargs.get("allow_reverse_tunnel_port") == 25555 + + # Verify mosh_ssh_options prepended tunnel options into copied tags + assert len(connect_tags_observed) == 1 + assert connect_tags_observed[0]["mosh_ssh_options"] == "-o ExitOnForwardFailure=yes -R 25555:localhost:25555 -i /tmp/key -4" + + # Verify exact identity restoration (is) + assert node_mock.tags is orig_tags + assert node_mock.options is orig_options + + # Case: tags is non-dict (e.g. empty string) + node_mock.tags = "" + node_mock.options = orig_options + with patch.object(rc, "_find_free_port", return_value=25555), \ + patch.object(rc, "_detect_sudo_requirement", return_value=False), \ + patch.object(rc, "_start_local_listener"), \ + patch.object(rc, "_sendline_until_connected", return_value=True), \ + patch("time.sleep"): + rc._start_local_listener.side_effect = stop_loop + rc.run() + assert node_mock.tags == "" + assert node_mock.options is orig_options + + # State restoration when _find_free_port fails + node_mock.tags = orig_tags + node_mock.options = orig_options + with patch.object(rc, "_find_free_port", side_effect=RuntimeError("port err")): + with pytest.raises(RuntimeError): + rc.run() + assert node_mock.tags is orig_tags + assert node_mock.options is orig_options + + # State restoration when _connect() fails (connection error) + node_mock.tags = orig_tags + node_mock.options = orig_options + node_mock._connect.side_effect = None + node_mock._connect.return_value = "Connection refused" + with patch.object(rc, "_find_free_port", return_value=25555), \ + patch("connpy.printer.error") as mock_err: + with pytest.raises(SystemExit) as exc: + rc.run() + assert exc.value.code == 1 + mock_err.assert_called_once() + assert "Could not connect" in mock_err.call_args[0][0] + assert node_mock.tags is orig_tags + assert node_mock.options is orig_options + + # State restoration on KeyboardInterrupt + node_mock.tags = orig_tags + node_mock.options = orig_options + node_mock._connect.return_value = True + rc.listener_conn = None + with patch.object(rc, "_find_free_port", return_value=25555), \ + patch.object(rc, "_detect_sudo_requirement", return_value=False), \ + patch.object(rc, "_start_local_listener") as mock_listener, \ + patch.object(rc, "_sendline_until_connected", return_value=True), \ + patch("time.sleep", side_effect=KeyboardInterrupt): + def kb_listener(*_a, **_kw): + rc.listener_active = False + rc.listener_conn = None + mock_listener.side_effect = kb_listener + rc.run() + assert node_mock.tags is orig_tags + assert node_mock.options is orig_options + + # State restoration on SystemExit + node_mock.tags = orig_tags + node_mock.options = orig_options + rc.listener_conn = None + with patch.object(rc, "_find_free_port", return_value=25555), \ + patch.object(rc, "_detect_sudo_requirement", side_effect=SystemExit(9)): + with pytest.raises(SystemExit) as exc: + rc.run() + assert exc.value.code == 9 + assert node_mock.tags is orig_tags + assert node_mock.options is orig_options + + # Invalid Mosh SSH configuration causes sys.exit(2) and single printer.error + bad_tags = {"mosh_ssh_options": "-o ProxyCommand=id"} + node_mock.tags = bad_tags + node_mock.options = orig_options + with patch.object(rc, "_find_free_port", return_value=25555), \ + patch("connpy.printer.error") as mock_err: + with pytest.raises(SystemExit) as exc: + rc.run() + assert exc.value.code == 2 + mock_err.assert_called_once() + assert "Invalid Mosh SSH configuration" in mock_err.call_args[0][0] + assert node_mock.tags is bad_tags + assert node_mock.options is orig_options + + def test_help_text_and_docstring_mention_mosh_jumphost_and_capture(self): + import connpy + from connpy.cli.help_text import get_instructions + + help_add = get_instructions("add").lower() + assert "mosh" in help_add + assert "jumphost" in help_add + assert "capture" in help_add + + pkg_doc = (connpy.__doc__ or "").lower() + assert "mosh" in pkg_doc + assert "jumphost" in pkg_doc + assert "capture" in pkg_doc diff --git a/connpy/tests/test_core.py b/connpy/tests/test_core.py index 94a2329..1b93317 100644 --- a/connpy/tests/test_core.py +++ b/connpy/tests/test_core.py @@ -3,6 +3,7 @@ import json import os import io import re +import shlex import pytest from unittest.mock import patch, MagicMock, PropertyMock from copy import deepcopy @@ -930,45 +931,273 @@ class TestMoshProtocol: assert "-p 2222" in ssh_token[0] assert tokens[-1] == "root@10.0.1.1" - def test_mosh_as_single_or_chained_jumphost_raises(self, tmp_config_dir): - """A mosh node cannot be used as a direct or chained jumphost.""" + def test_mosh_as_single_and_chained_jumphost_uses_ssh_backend(self, tmp_config_dir): + """Mosh nodes can be used as single and multi-hop jumphosts via SSH backend.""" from connpy.core import node + + def extract_proxy_cmd(tokens): + for i, tok in enumerate(tokens): + if tok.startswith("ProxyCommand="): + return tok.split("ProxyCommand=", 1)[1] + if tok == "-o" and i + 1 < len(tokens) and tokens[i + 1].startswith("ProxyCommand="): + return tokens[i + 1].split("ProxyCommand=", 1)[1] + raise ValueError("ProxyCommand not found in tokens") + + # 1. Golden String in single hop (1-hop) nodes1 = { "mosh_bastion": { - "host": "10.0.0.1", "protocol": "mosh", "port": "", + "host": "10.0.0.1", "protocol": "mosh", "port": "2222", "user": "admin", "password": "", "options": "", - "logs": "", "tags": "", "jumphost": "", "type": "connection" + "logs": "", "tags": {"mosh_ssh_options": "-i '/path A/key.pem' -4 -v -o StrictHostKeyChecking=no"}, + "jumphost": "", "type": "connection" }, - "dest": { - "host": "10.0.1.1", "protocol": "ssh", "port": "", + "dest1": { + "host": "10.0.1.1", "protocol": "ssh", "port": "22", "user": "root", "password": "", "options": "", "logs": "", "tags": "", "jumphost": "mosh_bastion", "type": "connection" } } config1 = self._make_config_with_nodes(tmp_config_dir, nodes1) - with pytest.raises(ValueError, match="cannot be used as a jumphost"): - node("dest", "10.0.1.1", jumphost="mosh_bastion", config=config1) + dest1 = node("dest1", **{k: v for k, v in nodes1["dest1"].items() if k != "type"}, config=config1) + expected_golden_1hop = ( + 'ssh -p 22 -o ProxyCommand="ssh -W %h:%p -p 2222 -i \'/path A/key.pem\' ' + '-4 -o StrictHostKeyChecking=no admin@10.0.0.1" root@10.0.1.1' + ) + assert dest1._get_cmd() == expected_golden_1hop + # 2. Golden String and 2-level unescaping in chained jumphost (2-hop) with spaces in paths nodes2 = { - "mosh_bastion": { - "host": "10.0.0.1", "protocol": "mosh", "port": "", - "user": "admin", "password": "", "options": "", - "logs": "", "tags": "", "jumphost": "", "type": "connection" + "hop2_mosh": { + "host": "10.0.0.2", "protocol": "mosh", "port": "2202", + "user": "user2", "password": "", "options": "", + "logs": "", "tags": {"mosh_ssh_options": "-i '/path B/key.pem' -6"}, + "jumphost": "", "type": "connection" }, - "bastionA": { - "host": "10.0.0.2", "protocol": "ssh", "port": "", - "user": "userA", "password": "", "options": "", - "logs": "", "tags": "", "jumphost": "mosh_bastion", "type": "connection" + "hop1_mosh": { + "host": "10.0.0.1", "protocol": "mosh", "port": "2201", + "user": "user1", "password": "", "options": "", + "logs": "", "tags": {"mosh_ssh_options": "-i '/path A/key.pem' -4"}, + "jumphost": "hop2_mosh", "type": "connection" }, - "dest": { + "dest2": { "host": "10.0.1.1", "protocol": "ssh", "port": "", "user": "root", "password": "", "options": "", - "logs": "", "tags": "", "jumphost": "bastionA", "type": "connection" + "logs": "", "tags": "", "jumphost": "hop1_mosh", "type": "connection" } } config2 = self._make_config_with_nodes(tmp_config_dir, nodes2) - with pytest.raises(ValueError, match="cannot be used as a jumphost"): - node("dest", "10.0.1.1", jumphost="bastionA", config=config2) + dest2 = node("dest2", **{k: v for k, v in nodes2["dest2"].items() if k != "type"}, config=config2) + expected_hop2_raw = "ssh -W 10.0.0.1:2201 -p 2202 -i '/path B/key.pem' -6 user2@10.0.0.2" + expected_hop1_raw = ( + f'ssh -W %h:%p -o ProxyCommand="{expected_hop2_raw.replace(chr(34), chr(92)+chr(34))}" ' + f"-p 2201 -i '/path A/key.pem' -4 user1@10.0.0.1" + ) + expected_dest2_cmd = ( + f'ssh -o ProxyCommand="{expected_hop1_raw.replace(chr(92), chr(92)*2).replace(chr(34), chr(92)+chr(34))}" root@10.0.1.1' + ) + assert dest2._get_cmd() == expected_dest2_cmd + + outer_tokens = shlex.split(dest2._get_cmd()) + hop1_proxy_cmd = extract_proxy_cmd(outer_tokens) + hop1_tokens = shlex.split(hop1_proxy_cmd) + assert hop1_tokens[hop1_tokens.index("-i") + 1] == "/path A/key.pem" + hop2_proxy_cmd = extract_proxy_cmd(hop1_tokens) + hop2_tokens = shlex.split(hop2_proxy_cmd) + assert hop2_tokens[hop2_tokens.index("-i") + 1] == "/path B/key.pem" + + # 2b. Chained mosh -> ssh hop where ssh hop has options with double quotes unescapes cleanly across 2 levels + nodes_mosh_ssh = { + "hop2_ssh": { + "host": "10.0.0.2", "protocol": "ssh", "port": "2202", + "user": "user2", "password": "", "options": '-i "/path C/key.pem"', + "logs": "", "tags": "", "jumphost": "", "type": "connection" + }, + "hop1_mosh": { + "host": "10.0.0.1", "protocol": "mosh", "port": "2201", + "user": "user1", "password": "", "options": "", + "logs": "", "tags": {"mosh_ssh_options": "-i '/path A/key.pem' -4"}, + "jumphost": "hop2_ssh", "type": "connection" + }, + "dest_mosh_ssh": { + "host": "10.0.1.1", "protocol": "ssh", "port": "", + "user": "root", "password": "", "options": "", + "logs": "", "tags": "", "jumphost": "hop1_mosh", "type": "connection" + } + } + config_mosh_ssh = self._make_config_with_nodes(tmp_config_dir, nodes_mosh_ssh) + dest_mosh_ssh = node("dest_mosh_ssh", **{k: v for k, v in nodes_mosh_ssh["dest_mosh_ssh"].items() if k != "type"}, config=config_mosh_ssh) + outer_tokens_ms = shlex.split(dest_mosh_ssh._get_cmd()) + hop1_proxy_cmd_ms = extract_proxy_cmd(outer_tokens_ms) + hop1_tokens_ms = shlex.split(hop1_proxy_cmd_ms) + assert hop1_tokens_ms[hop1_tokens_ms.index("-i") + 1] == "/path A/key.pem" + hop2_proxy_cmd_ms = extract_proxy_cmd(hop1_tokens_ms) + hop2_tokens_ms = shlex.split(hop2_proxy_cmd_ms) + assert hop2_tokens_ms[hop2_tokens_ms.index("-i") + 1] == "/path C/key.pem" + + # 3. IPv6 formats (::1, [::1], ::ffff:1.2.3.4) and dest -> mosh(IPv6) -> ssh + assert node._validate_mosh_ssh_fields("::1", "u", "22", {})[0] == "::1" + assert node._validate_mosh_ssh_fields("[::1]", "u", "22", {})[0] == "::1" + assert node._validate_mosh_ssh_fields("::ffff:1.2.3.4", "u", "22", {})[0] == "::ffff:1.2.3.4" + + nodes_ipv6 = { + "bastion_ssh": { + "host": "192.168.1.1", "protocol": "ssh", "port": "", + "user": "buser", "password": "", "options": "", + "logs": "", "tags": "", "jumphost": "", "type": "connection" + }, + "mosh_ipv6": { + "host": "2001:db8::1", "protocol": "mosh", "port": "2222", + "user": "muser", "password": "", "options": "", + "logs": "", "tags": "", "jumphost": "bastion_ssh", "type": "connection" + }, + "dest_ipv6": { + "host": "10.0.0.5", "protocol": "ssh", "port": "", + "user": "root", "password": "", "options": "", + "logs": "", "tags": "", "jumphost": "mosh_ipv6", "type": "connection" + } + } + config_ipv6 = self._make_config_with_nodes(tmp_config_dir, nodes_ipv6) + dest_ipv6 = node("dest_ipv6", **{k: v for k, v in nodes_ipv6["dest_ipv6"].items() if k != "type"}, config=config_ipv6) + assert "-W [2001:db8::1]:2222" in dest_ipv6._get_cmd() + + # 4. sftp over IPv6 filtering -Y and -m + nodes_sftp = { + "mosh_sftp": { + "host": "2001:db8::1", "protocol": "mosh", "port": "2222", + "user": "muser", "password": "", "options": "", + "logs": "", "tags": {"mosh_ssh_options": "-Y -m hmac-sha2-256 -i /tmp/key"}, + "jumphost": "", "type": "connection" + } + } + config_sftp = self._make_config_with_nodes(tmp_config_dir, nodes_sftp) + n_sftp = node("mosh_sftp", **{k: v for k, v in nodes_sftp["mosh_sftp"].items() if k != "type"}, config=config_sftp) + n_sftp.protocol = "sftp" + cmd_sftp = n_sftp._get_cmd() + assert "-Y" not in cmd_sftp + assert "-m" not in cmd_sftp + assert "hmac-sha2-256" not in cmd_sftp + assert "-i /tmp/key" in cmd_sftp + assert "muser@[2001:db8::1]" in cmd_sftp + assert "-P 2222" in cmd_sftp + + # 5. @profile isolation with deepcopy + profiles = { + "default": {"protocol": "ssh"}, + "prof1": { + "protocol": "mosh", "host": "10.0.0.1", "port": "22", "user": "u", + "password": "", "options": "", "logs": "", + "tags": {"mosh_ssh_options": "-i /tmp/key"}, "jumphost": "" + } + } + config_prof = self._make_config_with_nodes(tmp_config_dir, {}, profiles=profiles) + n_prof = node("node_prof", "@prof1", tags="@prof1", config=config_prof) + n_prof.tags["mosh_ssh_options"] = "-i /tmp/mutated" + assert config_prof.profiles["prof1"]["tags"]["mosh_ssh_options"] == "-i /tmp/key" + + jh_dict = {"tags": "@prof1", "password": ""} + resolved_jh = node._resolve_jumphost_data(jh_dict, config_prof) + resolved_jh["tags"]["mosh_ssh_options"] = "-i /tmp/mutated_jh" + assert config_prof.profiles["prof1"]["tags"]["mosh_ssh_options"] == "-i /tmp/key" + + # 6. Retrocompatibility of legacy ssh hops + nodes_legacy = { + "legacy_hop": { + "host": "fe80::1%eth0", "protocol": "ssh", "port": "22", + "user": "DOMAIN\\user", "password": "", "options": "-o StrictHostKeyChecking=no", + "logs": "", "tags": "", "jumphost": "", "type": "connection" + }, + "dest_legacy": { + "host": "10.0.0.10", "protocol": "ssh", "port": "22", + "user": "root", "password": "", "options": "", + "logs": "", "tags": "", "jumphost": "legacy_hop", "type": "connection" + } + } + config_legacy = self._make_config_with_nodes(tmp_config_dir, nodes_legacy) + dest_legacy = node("dest_legacy", **{k: v for k, v in nodes_legacy["dest_legacy"].items() if k != "type"}, config=config_legacy) + cmd_legacy = dest_legacy._get_cmd() + assert ( + "DOMAIN\\\\user@fe80::1%eth0" in cmd_legacy + or "DOMAIN\\user@fe80::1%eth0" in cmd_legacy + ) + assert "-o StrictHostKeyChecking=no" in cmd_legacy + + # _validate_legacy_ssh_hop_fields rejects unsafe host/user containing spaces or semicolons + for bad_h in ["10.0.0.1 22", "10.0.0.1;id"]: + with pytest.raises(ValueError, match="Invalid characters in host"): + node._validate_legacy_ssh_hop_fields(bad_h, "user", "22") + for bad_u in ["user name", "user;id"]: + with pytest.raises(ValueError, match="Invalid characters in user"): + node._validate_legacy_ssh_hop_fields("10.0.0.1", bad_u, "22") + + # 7. Chains mosh <-> ssh with cycle detection and max depth 5 + nodes_cycle = { + "mosh_a": {"host": "10.0.0.1", "protocol": "mosh", "jumphost": "ssh_b", "password": "", "options": "", "logs": "", "tags": "", "user": "", "port": "", "type": "connection"}, + "ssh_b": {"host": "10.0.0.2", "protocol": "ssh", "jumphost": "mosh_a", "password": "", "options": "", "logs": "", "tags": "", "user": "", "port": "", "type": "connection"}, + "dest_cycle": {"host": "10.0.0.3", "protocol": "ssh", "jumphost": "mosh_a", "password": "", "options": "", "logs": "", "tags": "", "user": "", "port": "", "type": "connection"} + } + config_cycle = self._make_config_with_nodes(tmp_config_dir, nodes_cycle) + with pytest.raises(ValueError, match="Circular jumphost reference detected"): + node("dest_cycle", **{k: v for k, v in nodes_cycle["dest_cycle"].items() if k != "type"}, config=config_cycle) + + nodes_depth = {} + for i in range(1, 7): + proto = "mosh" if i % 2 == 1 else "ssh" + jh = f"hop_{i+1}" if i < 6 else "" + nodes_depth[f"hop_{i}"] = {"host": f"10.0.0.{i}", "protocol": proto, "jumphost": jh, "password": "", "options": "", "logs": "", "tags": "", "user": "", "port": "", "type": "connection"} + nodes_depth["dest_depth"] = {"host": "10.0.0.100", "protocol": "ssh", "jumphost": "hop_1", "password": "", "options": "", "logs": "", "tags": "", "user": "", "port": "", "type": "connection"} + config_depth = self._make_config_with_nodes(tmp_config_dir, nodes_depth) + with pytest.raises(ValueError, match="Jumphost chain exceeds maximum depth of 5 hops"): + node("dest_depth", **{k: v for k, v in nodes_depth["dest_depth"].items() if k != "type"}, config=config_depth) + + @pytest.mark.parametrize("host,user,port,tags,expected_err", [ + ("-oProxyCommand=x", "admin", "22", {}, "Host cannot start with '-'"), + ("10.0.0.1", "-x", "22", {}, "User cannot start with '-'"), + ("10.0.0.1 22", "admin", "22", {}, "Invalid characters in host"), + ("\n10.0.0.1", "admin", "22", {}, "Invalid characters in host"), + ("10.0.0.1;id", "admin", "22", {}, "Invalid characters in host"), + ("1:2:3", "admin", "22", {}, "Invalid characters in host"), + ("[::1%eth0]", "admin", "22", {}, "Invalid characters in host"), + ("[::1%;id]", "admin", "22", {}, "Invalid characters in host"), + ("10.0.0.1", "user;id", "22", {}, "Invalid characters in user"), + ("10.0.0.1", "user name", "22", {}, "Invalid characters in user"), + ("10.0.0.1", "user\x00", "22", {}, "Invalid characters in user"), + ("10.0.0.1", "admin", 0, {}, "Invalid TCP SSH port"), + ("10.0.0.1", "admin", "0", {}, "Invalid TCP SSH port"), + ("10.0.0.1", "admin", " 0 ", {}, "Invalid TCP SSH port"), + ("10.0.0.1", "admin", -1, {}, "Invalid TCP SSH port"), + ("10.0.0.1", "admin", 65536, {}, "Invalid TCP SSH port"), + ("10.0.0.1", "admin", 70000, {}, "Invalid TCP SSH port"), + ("10.0.0.1", "admin", 22.0, {}, "Invalid TCP SSH port"), + ("10.0.0.1", "admin", True, {}, "Invalid TCP SSH port"), + ("10.0.0.1", "admin", False, {}, "Invalid TCP SSH port"), + ("10.0.0.1", "admin", "٢٢", {}, "Invalid TCP SSH port"), + ("10.0.0.1", "admin", "22", {"mosh_ssh_options": "-o LocalCommand=id"}, "LocalCommand or ProxyCommand"), + ("10.0.0.1", "admin", "22", {"mosh_ssh_options": "-o ProxyCommand=id"}, "LocalCommand or ProxyCommand"), + ("10.0.0.1", "admin", "22", {"mosh_ssh_options": "-o StrictHostKeyChecking=yes;id"}, "Disallowed -o directive"), + ("10.0.0.1", "admin", "22", {"mosh_ssh_options": "-o ProxyJump=host"}, "Disallowed -o directive"), + ("10.0.0.1", "admin", "22", {"mosh_ssh_options": "-D 1080"}, "Disallowed ssh option"), + ("10.0.0.1", "admin", "22", {"mosh_ssh_options": "-L 8080:localhost:80"}, "Disallowed ssh option"), + ("10.0.0.1", "admin", "22", {"mosh_ssh_options": "-R 2222:localhost:2222 -R 2222:localhost:2222"}, "Disallowed ssh option"), + ("10.0.0.1", "admin", "22", {"mosh_ssh_options": "-o ExitOnForwardFailure=no"}, "Disallowed -o directive"), + ("10.0.0.1", "admin", "22", {"mosh_ssh_options": "opt\x00ion"}, "Disallowed control characters"), + ("10.0.0.1", "admin", "22", {"mosh_ssh_options": "opt\nion"}, "Disallowed control characters"), + ("10.0.0.1", "admin", "22", {"mosh_ssh_options": "opt\rion"}, "Disallowed control characters"), + ("10.0.0.1", "admin", "22", {"mosh_ssh_options": '-i "unclosed'}, "unbalanced quotes"), + ]) + def test_mosh_jumphost_security_validations(self, host, user, port, tags, expected_err): + from connpy.core import node + with pytest.raises(ValueError, match=expected_err): + node._validate_mosh_ssh_fields(host, user, port, tags) + + def test_force_ssh_without_allow_reverse_tunnel_port_rejects_tunnel_options(self): + from connpy.core import node + n = node("mosh1", "10.0.0.1", user="admin", protocol="mosh", tags={"mosh_ssh_options": "-o ExitOnForwardFailure=yes -R 2222:localhost:2222"}) + with pytest.raises(ValueError, match="Disallowed -o directive in mosh_ssh_options"): + n._get_cmd(force_ssh=True) + + n2 = node("mosh2", "10.0.0.1", user="admin", protocol="mosh", tags={"mosh_ssh_options": "-R 2222:localhost:2222"}) + with pytest.raises(ValueError, match="Disallowed ssh option in mosh_ssh_options: '-R'"): + n2._get_cmd(force_ssh=True) def test_mosh_sftp_override_and_ssh_options_isolation(self, tmp_config_dir): """Switching a mosh node to sftp preserves _orig_protocol and uses mosh_ssh_options.""" diff --git a/connpy/tests/test_sync.py b/connpy/tests/test_sync.py index 53d5935..2abd946 100644 --- a/connpy/tests/test_sync.py +++ b/connpy/tests/test_sync.py @@ -1,7 +1,15 @@ """Tests for connpy.services.sync_service""" -import pytest +import datetime import os +import sys +import warnings +from collections import namedtuple +from types import SimpleNamespace from unittest.mock import MagicMock, patch + +import pytest + +from connpy.services import sync_service from connpy.services.sync_service import SyncService @pytest.fixture @@ -101,3 +109,167 @@ class TestSyncService: assert len(files) == 1 assert files[0]["id"] == "1" assert files[0]["timestamp"] == "1000" + + def test_google_api_core_python_version_future_warning_is_silenced_and_idempotent( + self, + ): + with warnings.catch_warnings(record=True) as recorded: + warnings.resetwarnings() + warnings.simplefilter("always") + sync_service._silence_google_python_version_warning() + sync_service._silence_google_python_version_warning() + + count = sum( + 1 + for f in warnings.filters + if f[0] == "ignore" + and f[2] is FutureWarning + and getattr(f[1], "pattern", None) + == sync_service._GOOGLE_PY_VERSION_WARNING_MESSAGE + and getattr(f[3], "pattern", None) + == sync_service._GOOGLE_PY_VERSION_WARNING_MODULE + ) + assert count == 1 + + warnings.warn_explicit( + "You are using a Python version (3.11.2) which Google will stop supporting...", + FutureWarning, + filename="fake_file.py", + lineno=1, + module="google.api_core._python_version_support", + registry={}, + ) + warnings.warn_explicit( + "You are using a Python version (3.11.2)\nwhich Google will stop supporting...", + FutureWarning, + filename="fake_file.py", + lineno=1, + module="google.api_core._python_version_support", + registry={}, + ) + assert len(recorded) == 0 + + # 4 negative controls with registry={} + warnings.warn_explicit( + "Other deprecation", + FutureWarning, + filename="fake_file.py", + lineno=1, + module="google.api_core._python_version_support", + registry={}, + ) + warnings.warn_explicit( + "You are using a Python version (3.11.2) which Google will stop supporting...", + FutureWarning, + filename="fake_file.py", + lineno=1, + module="google.api_core.other", + registry={}, + ) + warnings.warn_explicit( + "You are using a Python version (3.11.2) which Google will stop supporting...", + FutureWarning, + filename="fake_file.py", + lineno=1, + module="other.module", + registry={}, + ) + warnings.warn_explicit( + "You are using a Python version (3.11.2) which Google will stop supporting...", + DeprecationWarning, + filename="fake_file.py", + lineno=1, + module="google.api_core._python_version_support", + registry={}, + ) + assert len(recorded) == 4 + + def test_real_google_api_core_check_python_version_is_silenced(self): + pv_mod = pytest.importorskip("google.api_core._python_version_support") + version_info_cls = namedtuple("version_info", ["major", "minor", "micro"]) + fake_sys = SimpleNamespace( + version_info=version_info_cls(3, 11, 2), + version="3.11.2", + ) + with patch.object(pv_mod, "sys", fake_sys): + with warnings.catch_warnings(record=True) as raw_recorded: + warnings.resetwarnings() + warnings.simplefilter("always") + try: + pv_mod.check_python_version(today=datetime.date(2026, 10, 28)) + except (AttributeError, TypeError) as exc: + pytest.skip(f"Incompatible google-api-core internals: {exc}") + if not any(issubclass(w.category, FutureWarning) for w in raw_recorded): + pytest.skip( + "Version table in installed google-api-core did not emit warning for 3.11.2 on 2026-10-28" + ) + + with warnings.catch_warnings(record=True) as filtered_recorded: + warnings.resetwarnings() + warnings.simplefilter("always") + sync_service._silence_google_python_version_warning() + try: + pv_mod.check_python_version(today=datetime.date(2026, 10, 28)) + except (AttributeError, TypeError) as exc: + pytest.skip(f"Incompatible google-api-core internals: {exc}") + assert len(filtered_recorded) == 0 + + @pytest.mark.parametrize( + "attr_name,module_name,target_attr", + [(k, v[0], v[1]) for k, v in sync_service._GOOGLE_LAZY_IMPORTS.items()], + ) + def test_getattr_lazy_symbols_reapply_filter_cache_and_unknown_raises( + self, attr_name, module_name, target_attr + ): + had_attr = attr_name in sync_service.__dict__ + saved_val = sync_service.__dict__.get(attr_name) + if had_attr: + delattr(sync_service, attr_name) + + try: + sentinel_obj = object() + fake_mod = MagicMock(**{target_attr: sentinel_obj}) + with patch.dict(sys.modules, {module_name: fake_mod}): + with warnings.catch_warnings(record=True) as recorded: + warnings.resetwarnings() + warnings.simplefilter("always") + + resolved = getattr(sync_service, attr_name) + assert resolved is sentinel_obj + assert sync_service.__dict__[attr_name] is sentinel_obj + + warnings.warn_explicit( + "You are using a Python version (3.11.2) which Google will stop supporting...", + FutureWarning, + filename="fake_file.py", + lineno=1, + module="google.api_core._python_version_support", + registry={}, + ) + assert len(recorded) == 0 + + warnings.warn_explicit( + "Unrelated future warning", + FutureWarning, + filename="other.py", + lineno=1, + module="other.module", + registry={}, + ) + assert len(recorded) == 1 + finally: + if had_attr: + setattr(sync_service, attr_name, saved_val) + elif attr_name in sync_service.__dict__: + delattr(sync_service, attr_name) + + with patch( + "connpy.services.sync_service._silence_google_python_version_warning" + ) as mock_silence: + with pytest.raises( + AttributeError, + match="module 'connpy.services.sync_service' has no attribute 'non_existent_attr'", + ): + sync_service.__getattr__("non_existent_attr") + mock_silence.assert_not_called() + assert "non_existent_attr" not in sync_service.__dict__ diff --git a/docs/connpy/cli/help_text.html b/docs/connpy/cli/help_text.html index eaa2cc0..5f1b7a3 100644 --- a/docs/connpy/cli/help_text.html +++ b/docs/connpy/cli/help_text.html @@ -91,7 +91,7 @@ Here are some important instructions and tips for configuring your new node: 2. **Available Protocols and Apps**: - ssh - - mosh (requires remote mosh-server and UDP ports 60000-61000 open; --sftp uses SSH TCP) + - mosh (requires remote mosh-server and UDP ports 60000-61000 open; supports SSH backend fallback for jumphosts, capture, and --sftp) - telnet - kubectl (`kubectl exec`) - docker (`docker exec`) @@ -124,7 +124,7 @@ Here are some important instructions and tips for configuring your new node: - `nc_command`: Replaces the default `nc` command used when bridging connections through Docker or Kubernetes (e.g., `ip netns exec global-vrf nc`). - `mosh_udp_port`: Server-side UDP port or range (`PORT` or `PORT1:PORT2`) passed as `-p` to `mosh`. - `mosh_server`: Path to the `mosh-server` binary on the remote host (`--server`). - - `mosh_ssh_options`: Additional SSH options injected into `mosh --ssh="ssh ..."` and reused when connecting via `--sftp` or running batch commands (`run`/`test`). + - `mosh_ssh_options`: Additional SSH options injected into `mosh --ssh="ssh ..."` and reused when connecting via `--sftp`, as a jumphost, for remote capture, or running batch commands (`run`/`test`). """ if type == "bashcompletion": return ''' diff --git a/docs/connpy/index.html b/docs/connpy/index.html index 95dbe58..51170f9 100644 --- a/docs/connpy/index.html +++ b/docs/connpy/index.html @@ -41,7 +41,7 @@ el.replaceWith(d);

App Logo

-

Connpy (v6.4.0b1)

+

Connpy (v6.4.0b2)

@@ -145,12 +145,12 @@ tags: { "os": "cisco_ios", "prompt": ".*#&quo * Kubernetes (kubectl): Connect to pods (namespace customizable via options). * AWS SSM: Connect to EC2 instances using Instance IDs as hosts.

4b. Jumphosts

-

Support for single or chained intermediate gateway nodes (SSH, SSM, kubectl, or docker jumphosts) to tunnel traffic safely into target environments.

+

Support for single or chained intermediate gateway nodes (SSH, Mosh, SSM, kubectl, or docker jumphosts) to tunnel traffic safely into target environments.

4c. Debug Mode, Keepalive, Logging

Track connection steps (conn --debug node), set idle keepalive intervals (conn config --keepalive <seconds>), or define dynamic output log files using variables like ${unique}, ${host}, ${port}, ${user}, ${protocol}, or ${date 'format'}.


5. 🖥️ Remote Capture (conn capture - Core Plugin)

-

Perform remote packet capture (tcpdump) on hosts over secure SSH reverse tunnels and stream packets live into your local Wireshark GUI:

+

Perform remote packet capture (tcpdump) on hosts over secure SSH or Mosh reverse tunnels and stream packets live into your local Wireshark GUI:

conn capture router1 eth0 -w -f "port 80"
 
    @@ -1742,7 +1742,7 @@ class node: profile = re.search("^@(.*)", str(attr[key])) if profile and config != '': try: - setattr(self,key,config.profiles[profile.group(1)][key]) + setattr(self,key,deepcopy(config.profiles[profile.group(1)][key])) except KeyError: setattr(self,key,"") elif attr[key] == '' and key == "protocol": @@ -1785,7 +1785,7 @@ class node: profile = re.search("^@(.*)", str(jh_dict[key])) if profile: try: - jh_dict[key] = config.profiles[profile.group(1)][key] + jh_dict[key] = deepcopy(config.profiles[profile.group(1)][key]) except KeyError: jh_dict[key] = "" elif jh_dict[key] == '' and key == "protocol": @@ -1806,6 +1806,242 @@ class node: jh_dict["password"] = [jh_dict["password"]] return jh_dict + @staticmethod + def _validate_legacy_ssh_hop_fields(host, user, port): + """Minimal injection guard for legacy SSH jumphost fields (preserves DOMAIN\\user, %scope, etc.).""" + host_s = str(host) if host is not None else "" + if host_s.startswith("-"): + raise ValueError("Host cannot start with '-'") + if not host_s or _LEGACY_SSH_UNSAFE_RE.search(host_s): + raise ValueError(f"Invalid characters in host: '{host}'") + host_bare = ( + host_s[1:-1] + if (host_s.startswith("[") and host_s.endswith("]")) + else host_s + ) + if not host_bare or host_bare.startswith("-"): + raise ValueError(f"Invalid characters in host: '{host}'") + + user_s = str(user) if user is not None else "" + if user_s.startswith("-"): + raise ValueError("User cannot start with '-'") + if user_s and _LEGACY_SSH_UNSAFE_RE.search(user_s): + raise ValueError(f"Invalid characters in user: '{user}'") + + port_out = "" + if port != "" and port is not None: + if isinstance(port, (bool, float)): + raise ValueError(f"Invalid TCP SSH port '{port}'") + port_s = str(port).strip() + if port_s: + if ( + not port_s.isascii() + or not re.fullmatch(r"[0-9]+", port_s, re.ASCII) + or not (1 <= int(port_s) <= 65535) + ): + raise ValueError(f"Invalid TCP SSH port '{port}'") + port_out = port_s + + return host_bare, port_out + + @staticmethod + def _validate_mosh_ssh_fields( + host, + user, + port, + tags, + *, + strip_non_tunnel_flags=False, + allow_reverse_tunnel_port=None, + ): + """Validate host, user, TCP SSH port, and mosh_ssh_options for Mosh SSH backend. + + Accepts IPv4, hostnames, and bare or bracketed IPv6 literals (including + IPv4-mapped IPv6 like ::ffff:1.2.3.4, without scope-id '%'). + Returns: + tuple[str, str, str]: (host_bare_str, validated_port_str, validated_ssh_opts_str) + """ + host_s = str(host) if host is not None else "" + if host_s.startswith("-"): + raise ValueError("Host cannot start with '-'") + if not host_s: + raise ValueError(f"Invalid characters in host: '{host}'") + + if (host_s.startswith("[") and host_s.endswith("]")) or ":" in host_s: + inner = host_s[1:-1] if (host_s.startswith("[") and host_s.endswith("]")) else host_s + if "%" in inner or not re.fullmatch(r"[0-9A-Fa-f:.]+", inner, re.ASCII): + raise ValueError(f"Invalid characters in host: '{host}'") + try: + ipaddress.IPv6Address(inner) + except ValueError as exc: + raise ValueError(f"Invalid characters in host: '{host}'") from exc + host_bare = inner + else: + if not re.fullmatch(r"[A-Za-z0-9._-]+", host_s, re.ASCII): + raise ValueError(f"Invalid characters in host: '{host}'") + host_bare = host_s + + user_s = str(user) if user is not None else "" + if user_s.startswith("-"): + raise ValueError("User cannot start with '-'") + if user_s and not re.fullmatch( + r"[A-Za-z0-9._-]+(?:@[A-Za-z0-9._-]+)?", user_s, re.ASCII + ): + raise ValueError(f"Invalid characters in user: '{user}'") + + port_out = "" + if port != "" and port is not None: + if isinstance(port, (bool, float)): + raise ValueError(f"Invalid TCP SSH port '{port}'") + port_s = str(port).strip() + if port_s: + if ( + not port_s.isascii() + or not re.fullmatch(r"[0-9]+", port_s, re.ASCII) + or not (1 <= int(port_s) <= 65535) + ): + raise ValueError(f"Invalid TCP SSH port '{port}'") + port_out = port_s + + allowed_r_port = None + if allow_reverse_tunnel_port is not None: + r_port_s = str(allow_reverse_tunnel_port).strip() + if ( + isinstance(allow_reverse_tunnel_port, (bool, float)) + or not r_port_s.isascii() + or not re.fullmatch(r"[0-9]+", r_port_s, re.ASCII) + or not (1 <= int(r_port_s) <= 65535) + ): + raise ValueError( + f"Invalid allow_reverse_tunnel_port '{allow_reverse_tunnel_port}'" + ) + allowed_r_port = r_port_s + + opts_out = "" + raw_opts = tags.get("mosh_ssh_options", "") if isinstance(tags, dict) else "" + if raw_opts: + ssh_opts = str(raw_opts).strip() + if "\x00" in ssh_opts or "\n" in ssh_opts or "\r" in ssh_opts: + raise ValueError("Disallowed control characters in mosh_ssh_options") + if ssh_opts: + try: + tokens = shlex.split(ssh_opts) + except ValueError as exc: + raise ValueError( + "Invalid mosh_ssh_options: unbalanced quotes" + ) from exc + r_count = 0 + exit_fwd_count = 0 + out_tokens = [] + idx = 0 + # Normative branch evaluation order: + # 1. NOARG -> 2. ARG separate -> 3. ARG attached -> 4. -R -> 5. -o/-oXXX -> 6. other + while idx < len(tokens): + tok = tokens[idx] + if tok in _ALLOWED_SSH_NOARG_FLAGS: + if not (strip_non_tunnel_flags and tok in _NON_TUNNEL_SSH_FLAGS): + out_tokens.append(tok) + idx += 1 + elif tok in _ALLOWED_SSH_ARG_FLAGS: + if idx + 1 >= len(tokens) or not tokens[idx + 1].strip(): + raise ValueError( + f"Missing argument for ssh option '{tok}' in mosh_ssh_options" + ) + arg_val = tokens[idx + 1] + val_re = _SAFE_SSH_PATH_RE if tok == "-i" else _SAFE_SSH_O_VALUE_RE + if arg_val.startswith("-") or not val_re.fullmatch(arg_val): + raise ValueError( + f"Invalid argument for ssh option '{tok}' in mosh_ssh_options" + ) + out_tokens.extend([tok, arg_val]) + idx += 2 + elif tok[:2] in _ALLOWED_SSH_ARG_FLAGS and len(tok) > 2: + flag_prefix = tok[:2] + arg_val = tok[2:] + val_re = _SAFE_SSH_PATH_RE if flag_prefix == "-i" else _SAFE_SSH_O_VALUE_RE + if ( + not arg_val.strip() + or arg_val.startswith("-") + or not val_re.fullmatch(arg_val) + ): + raise ValueError( + f"Invalid argument for ssh option '{flag_prefix}' in mosh_ssh_options" + ) + out_tokens.append(tok) + idx += 1 + elif tok == "-R": + if ( + allowed_r_port is None + or r_count >= 1 + or idx + 1 >= len(tokens) + or tokens[idx + 1] + != f"{allowed_r_port}:localhost:{allowed_r_port}" + ): + raise ValueError( + f"Disallowed ssh option in mosh_ssh_options: '{tok}'" + ) + r_count += 1 + out_tokens.extend([tok, tokens[idx + 1]]) + idx += 2 + elif tok == "-o" or tok.startswith("-o"): + if tok == "-o": + if idx + 1 >= len(tokens) or not tokens[idx + 1].strip(): + raise ValueError( + "Missing argument for ssh option '-o' in mosh_ssh_options" + ) + opt_spec = tokens[idx + 1] + step = 2 + else: + opt_spec = tok[2:] + step = 1 + if re.search(r"(?:localcommand|proxycommand)", opt_spec, re.IGNORECASE): + raise ValueError( + "mosh_ssh_options cannot contain LocalCommand or ProxyCommand" + ) + m = re.fullmatch(r"([A-Za-z0-9]+)=(.+)", opt_spec, re.ASCII) + if ( + not m + or not m.group(2).strip() + or not _SAFE_SSH_O_VALUE_RE.fullmatch(m.group(2).strip()) + ): + raise ValueError( + f"Disallowed -o directive in mosh_ssh_options: '{opt_spec}'" + ) + key_raw = m.group(1) + key_lower = key_raw.lower() + val_str = m.group(2).strip() + if key_lower == "exitonforwardfailure": + if ( + allowed_r_port is None + or exit_fwd_count >= 1 + or val_str.lower() != "yes" + ): + raise ValueError( + f"Disallowed -o directive in mosh_ssh_options: '{opt_spec}'" + ) + exit_fwd_count += 1 + elif key_lower not in _ALLOWED_SSH_O_KEYS: + raise ValueError( + f"Disallowed -o directive in mosh_ssh_options: '{opt_spec}'" + ) + if not (strip_non_tunnel_flags and key_lower == "forwardagent"): + if tok == "-o": + out_tokens.extend(["-o", f"{key_raw}={val_str}"]) + else: + out_tokens.append(f"-o{key_raw}={val_str}") + idx += step + else: + if re.search(r"(?:localcommand|proxycommand)", tok, re.IGNORECASE): + raise ValueError( + "mosh_ssh_options cannot contain LocalCommand or ProxyCommand" + ) + raise ValueError( + f"Disallowed ssh option in mosh_ssh_options: '{tok}'" + ) + opts_out = shlex.join(out_tokens) + + return host_bare, port_out, opts_out + def _build_jumphost_chain(self, jumphost_name, config, visited=None, depth=0, target_host="%h", target_port="%p"): '''Recursively build ProxyCommand for chained jumphosts. @@ -1830,24 +2066,39 @@ class node: jh = self._resolve_jumphost_data(jh, config) if jh["protocol"] == "mosh": - raise ValueError( - f"Jumphost '{jumphost_name}' uses protocol 'mosh' which cannot be used as a jumphost." + jh_host, jh_port, jh_opts = self._validate_mosh_ssh_fields( + jh.get("host", ""), + jh.get("user", ""), + jh.get("port", ""), + jh.get("tags", ""), + strip_non_tunnel_flags=True, ) + elif jh["protocol"] == "ssh": + jh_host, jh_port = self._validate_legacy_ssh_hop_fields( + jh.get("host", ""), + jh.get("user", ""), + jh.get("port", ""), + ) + jh_opts = jh.get("options", "") + else: + jh_host = jh.get("host", "") + jh_port = str(jh["port"]).strip() if jh.get("port") not in ("", None) else "" + jh_opts = jh.get("options", "") passwords = [] inner_proxy_opt = "" # Recursively resolve inner jumphost if jh.get("jumphost", "") != "": - if jh["protocol"] not in ["ssh"]: + if jh["protocol"] not in ["ssh", "mosh"]: raise ValueError( f"Jumphost '{jumphost_name}' uses protocol '{jh['protocol']}' " f"which does not support chained jumphosts. " - f"Only SSH jumphosts can have their own jumphosts." + f"Only SSH or Mosh jumphosts can have their own jumphosts." ) - parent_port = jh["port"] if jh["port"] != "" else "22" + parent_port = jh_port if jh_port != "" else "22" inner_raw_cmd, inner_passwords = self._build_jumphost_chain( - jh["jumphost"], config, visited, depth + 1, target_host=jh["host"], target_port=parent_port + jh["jumphost"], config, visited, depth + 1, target_host=jh_host, target_port=parent_port ) passwords = inner_passwords escaped = inner_raw_cmd.replace('\\', '\\\\').replace('"', '\\"') @@ -1858,17 +2109,31 @@ class node: passwords = passwords + jh["password"] t_port = target_port if target_port != "" else "22" + if jh["protocol"] in ["ssh", "mosh"]: + if target_host == "%h" and t_port == "%p": + w_target = "%h" + w_port = "%p" + else: + t_host_bare, t_port_norm = self._validate_legacy_ssh_hop_fields( + target_host, "", t_port + ) + w_target = f"[{t_host_bare}]" if ":" in t_host_bare else t_host_bare + w_port = t_port_norm if t_port_norm != "" else "22" + else: + w_target = target_host + w_port = t_port # Build raw command based on protocol - if jh["protocol"] == "ssh": - cmd = f"ssh -W {target_host}:{t_port}" + if jh["protocol"] in ["ssh", "mosh"]: + cmd = f"ssh -W {w_target}:{w_port}" if inner_proxy_opt: cmd += f" {inner_proxy_opt}" - if jh["port"] != '': - cmd += f" -p {jh['port']}" - if jh["options"] != '': - cmd += f" {jh['options']}" - user_host = f"{jh['user']}@{jh['host']}" if jh['user'] != '' else jh['host'] + if jh_port != "": + cmd += f" -p {jh_port}" + if jh_opts != "": + cmd += f" {jh_opts}" + jh_user = str(jh.get("user", "") or "") + user_host = f"{jh_user}@{jh_host}" if jh_user else jh_host cmd += f" {user_host}" elif jh["protocol"] == "ssm": ssm_target = jh["host"] @@ -2766,52 +3031,62 @@ class node: return connect @MethodHook - def _generate_ssh_sftp_cmd(self, force_ssh=False): - is_from_mosh = getattr(self, "_orig_protocol", "") == "mosh" - if is_from_mosh: - if str(self.host).startswith("-"): - raise ValueError("Host cannot start with '-'") - if self.user and str(self.user).startswith("-"): - raise ValueError("User cannot start with '-'") - + def _generate_ssh_sftp_cmd(self, force_ssh=False, allow_reverse_tunnel_port=None): + is_from_mosh = ( + self.protocol == "mosh" + or getattr(self, "_orig_protocol", "") == "mosh" + ) proto = "ssh" if force_ssh else self.protocol cmd = proto - if self.port != "" and self.port is not None: - if is_from_mosh: - port_s = str(self.port).strip() - if port_s: - if not re.fullmatch(r"^\d+$", port_s) or not (1 <= int(port_s) <= 65535): - raise ValueError(f"Invalid TCP SSH port '{self.port}'") - cmd += (" -p " if proto == "ssh" else " -P ") + port_s + if is_from_mosh: + host_bare, port_s, raw_opts = self._validate_mosh_ssh_fields( + self.host, + self.user, + self.port, + self.tags, + allow_reverse_tunnel_port=allow_reverse_tunnel_port, + ) + if port_s: + cmd += (" -p " if proto == "ssh" else " -P ") + port_s + if raw_opts and proto == "sftp": + sftp_tokens = [] + toks = shlex.split(raw_opts) + i = 0 + while i < len(toks): + t = toks[i] + if t in ("-X", "-x", "-Y", "-t", "-A", "-a"): + i += 1 + elif t == "-m": + i += 2 + elif t.startswith("-m") and len(t) > 2: + i += 1 + else: + sftp_tokens.append(t) + i += 1 + opts = shlex.join(sftp_tokens) else: + opts = str(raw_opts).strip() if raw_opts else "" + if opts: + cmd += " " + opts + target_h = f"[{host_bare}]" if (proto == "sftp" and ":" in host_bare) else host_bare + else: + if self.port != "" and self.port is not None: if proto == "ssh": cmd += " -p " + str(self.port) elif proto == "sftp": cmd += " -P " + str(self.port) - - if is_from_mosh: - raw_opts = self.tags.get("mosh_ssh_options", "") if isinstance(self.tags, dict) else "" - if raw_opts: - ssh_opts = str(raw_opts).strip() - if re.search(r'(?:LocalCommand|ProxyCommand)', ssh_opts, re.IGNORECASE): - raise ValueError("mosh_ssh_options cannot contain LocalCommand or ProxyCommand") - try: - shlex.split(ssh_opts) - except ValueError as exc: - raise ValueError("Invalid mosh_ssh_options: unbalanced quotes") from exc - else: raw_opts = self.options + if raw_opts: + opts = str(raw_opts).strip() + if proto == "sftp": + opts = re.sub(r'(?<!\S)-[XxtTAaNf]\b', '', opts).strip() + if opts: + cmd += " " + opts + target_h = self.host - if raw_opts: - opts = str(raw_opts).strip() - if proto == "sftp": - # Strip SSH-only flags that sftp doesn't support - opts = re.sub(r'(?<!\S)-[XxtTAaNf]\b', '', opts).strip() - if opts: - cmd += " " + opts if self.jumphost: cmd += " " + self.jumphost - user_host = f"{self.user}@{self.host}" if self.user else self.host + user_host = f"{self.user}@{target_h}" if self.user else target_h cmd += f" {user_host}" return cmd @@ -2920,9 +3195,12 @@ class node: @MethodHook - def _get_cmd(self, force_ssh=False): + def _get_cmd(self, force_ssh=False, allow_reverse_tunnel_port=None): if force_ssh or self.protocol in ["ssh", "sftp"]: - return self._generate_ssh_sftp_cmd(force_ssh=force_ssh) + return self._generate_ssh_sftp_cmd( + force_ssh=force_ssh, + allow_reverse_tunnel_port=allow_reverse_tunnel_port, + ) elif self.protocol == "mosh": return self._generate_mosh_cmd() elif self.protocol == "telnet": @@ -2959,7 +3237,15 @@ class node: return None, f"Connection failed: {e}" @MethodHook - def _connect(self, debug=False, timeout=10, max_attempts=3, logger=None, force_ssh=False): + def _connect( + self, + debug=False, + timeout=10, + max_attempts=3, + logger=None, + force_ssh=False, + allow_reverse_tunnel_port=None, + ): effective_protocol = "ssh" if force_ssh else self.protocol if effective_protocol == "mosh": import shutil @@ -2980,7 +3266,10 @@ class node: self.logfile = self._logfile() return True - cmd = self._get_cmd(force_ssh=force_ssh) + cmd = self._get_cmd( + force_ssh=force_ssh, + allow_reverse_tunnel_port=allow_reverse_tunnel_port, + ) passwords = self._passtx(self.password) if self.password and any(self.password) else [] if self.logs != '': self.logfile = self._logfile() @@ -4309,7 +4598,7 @@ def test(self, commands, expected, vars = None,*, folder = None, prompt = None,