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.
This commit is contained in:
fluzzi32 committed 2026-10-28 14:22:24 -03:00
1 parent 5f2aa958a2
commit 2da9b45548
12 files changed
+1444 -163

No files matched your search

+3 -3
View File
@@ -3,7 +3,7 @@
</p>
# 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 <seconds>`), 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"
```
+3 -3
View File
@@ -5,7 +5,7 @@
</p>
# 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 <seconds>`), 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"
```
+1 -1
View File
@@ -1 +1 @@
__version__ = "6.4.0b1"
__version__ = "6.4.0b2"
+2 -2
View File
@@ -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 '''
+378 -46
View File
@@ -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()
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:
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 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":
# Strip SSH-only flags that sftp doesn't support
opts = re.sub(r'(?<!\S)-[XxtTAaNf]\b', '', opts).strip()
if opts:
cmd += " " + opts
target_h = self.host
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
@@ -1287,9 +1605,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":
@@ -1326,7 +1647,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
@@ -1347,7 +1676,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()
+52 -5
View File
@@ -5,7 +5,7 @@ class Parser:
def __init__(self):
self.parser = argparse.ArgumentParser(description="Capture packets remotely using a saved SSH node", epilog="All unknown arguments will be passed to tcpdump.")
self.parser.add_argument("node", nargs='?', help="Name of the saved node (must use SSH)")
self.parser.add_argument("node", nargs='?', help="Name of the saved node (must use SSH or Mosh)")
self.parser.add_argument("interface", nargs='?', help="Network interface to capture on")
self.parser.add_argument("--ns", "--namespace", dest="namespace", help="Optional network namespace")
self.parser.add_argument("-w","--wireshark", action="store_true", help="Open live capture in Wireshark")
@@ -28,6 +28,7 @@ class Parser:
class Entrypoint:
@staticmethod
def get_remote_capture_class():
from copy import deepcopy
import subprocess
import random
import socket
@@ -35,6 +36,7 @@ class Entrypoint:
import threading
from pexpect import TIMEOUT
from connpy import printer
from connpy.core import node as CoreNode
class RemoteCapture:
def __init__(self, connapp, node_name, interface, namespace=None, use_wireshark=False, tcpdump_filter=None, tcpdump_args=None):
@@ -64,8 +66,8 @@ class Entrypoint:
node_data = self.connapp.services.nodes.get_node_details(matches[0])
self.node = self.connapp.node(matches[0], **node_data, config=self.connapp.config)
if self.node.protocol != "ssh":
printer.error(f"Node '{self.node.unique}' must be an SSH connection.")
if self.node.protocol not in ("ssh", "mosh"):
printer.error(f"Node '{self.node.unique}' must be an SSH or Mosh connection.")
sys.exit(2)
settings = self.connapp.services.config_svc.get_settings()
@@ -285,10 +287,52 @@ class Entrypoint:
printer.error("Wireshark path not set in config.\nUse '--set-wireshark-path /full/path/to/wireshark' to configure it.")
sys.exit(1)
orig_options = self.node.options
orig_tags = self.node.tags
try:
self.local_port = self._find_free_port()
self.node.options += f" -o ExitOnForwardFailure=yes -R {self.local_port}:localhost:{self.local_port}"
tunnel_opts = f"-o ExitOnForwardFailure=yes -R {self.local_port}:localhost:{self.local_port}"
force_ssh = self.node.protocol == "mosh"
if force_ssh:
try:
CoreNode._validate_mosh_ssh_fields(
self.node.host,
self.node.user,
self.node.port,
self.node.tags,
allow_reverse_tunnel_port=None,
)
tags_copy = (
deepcopy(self.node.tags)
if isinstance(self.node.tags, dict)
else {}
)
existing_opts = str(tags_copy.get("mosh_ssh_options", "")).strip()
tags_copy["mosh_ssh_options"] = (
f"{tunnel_opts} {existing_opts}".strip()
if existing_opts
else tunnel_opts
)
CoreNode._validate_mosh_ssh_fields(
self.node.host,
self.node.user,
self.node.port,
tags_copy,
allow_reverse_tunnel_port=self.local_port,
)
except ValueError as exc:
printer.error(
f"Invalid Mosh SSH configuration for node '{self.node.unique}': {exc}"
)
sys.exit(2)
self.node.tags = tags_copy
connection = self.node._connect(
force_ssh=True, allow_reverse_tunnel_port=self.local_port
)
else:
self.node.options = f"{self.node.options} {tunnel_opts}"
connection = self.node._connect()
if connection is not True:
printer.error(f"Could not connect to {self.node.unique}\n{connection}")
sys.exit(1)
@@ -342,6 +386,9 @@ class Entrypoint:
except OSError: pass
if hasattr(self.node, "child"):
self.node.child.close(force=True)
finally:
self.node.options = orig_options
self.node.tags = orig_tags
return RemoteCapture
+55 -30
View File
@@ -1,39 +1,64 @@
import sys
import os
import time
import zipfile
import tempfile
import importlib
import io
import yaml
import os
import sys
import tempfile
import threading
import time
import warnings
import zipfile
from datetime import datetime
import yaml
_GOOGLE_PY_VERSION_WARNING_MESSAGE = (
r"(?s)^You are using a Python version .* Google will .*"
)
_GOOGLE_PY_VERSION_WARNING_MODULE = (
r"^google\.api_core\._python_version_support$"
)
_GOOGLE_LAZY_IMPORTS: dict[str, tuple[str, str]] = {
"Credentials": ("google.oauth2.credentials", "Credentials"),
"Request": ("google.auth.transport.requests", "Request"),
"build": ("googleapiclient.discovery", "build"),
"RefreshError": ("google.auth.exceptions", "RefreshError"),
"InstalledAppFlow": ("google_auth_oauthlib.flow", "InstalledAppFlow"),
"MediaFileUpload": ("googleapiclient.http", "MediaFileUpload"),
"MediaIoBaseDownload": ("googleapiclient.http", "MediaIoBaseDownload"),
"HttpError": ("googleapiclient.errors", "HttpError"),
}
def _silence_google_python_version_warning() -> 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
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__]
+187
View File
@@ -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
+249 -20
View File
@@ -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."""
+173 -1
View File
@@ -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__
+2 -2
View File
@@ -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=&#34;ssh ...&#34;` and reused when connecting via `--sftp` or running batch commands (`run`/`test`).
- `mosh_ssh_options`: Additional SSH options injected into `mosh --ssh=&#34;ssh ...&#34;` and reused when connecting via `--sftp`, as a jumphost, for remote capture, or running batch commands (`run`/`test`).
&#34;&#34;&#34;
if type == &#34;bashcompletion&#34;:
return &#39;&#39;&#39;
+339 -50
View File
@@ -41,7 +41,7 @@ el.replaceWith(d);
<p align="center">
<img src="https://nginx.gederico.dynu.net/images/CONNPY-resized.png" alt="App Logo">
</p>
<h1 id="connpy-v640b1">Connpy (v6.4.0b1)</h1>
<h1 id="connpy-v640b2">Connpy (v6.4.0b2)</h1>
<p><a href="https://pypi.org/pypi/connpy/"><img alt="" src="https://img.shields.io/pypi/v/connpy.svg?style=flat-square"></a>
<a href="https://pypi.org/pypi/connpy/"><img alt="" src="https://img.shields.io/pypi/pyversions/connpy.svg?style=flat-square"></a>
<a href="https://pypi.org/pypi/connpy/"><img alt="" src="https://img.shields.io/pypi/dm/connpy.svg?style=flat-square&amp;cacheSeconds=86400"></a>
@@ -145,12 +145,12 @@ tags: { &quot;os&quot;: &quot;cisco_ios&quot;, &quot;prompt&quot;: &quot;.*#&quo
* <strong>Kubernetes (kubectl)</strong>: Connect to pods (namespace customizable via options).
* <strong>AWS SSM</strong>: Connect to EC2 instances using Instance IDs as hosts.</p>
<h3 id="4b-jumphosts">4b. Jumphosts</h3>
<p>Support for single or chained intermediate gateway nodes (SSH, SSM, kubectl, or docker jumphosts) to tunnel traffic safely into target environments.</p>
<p>Support for single or chained intermediate gateway nodes (SSH, Mosh, SSM, kubectl, or docker jumphosts) to tunnel traffic safely into target environments.</p>
<h3 id="4c-debug-mode-keepalive-logging">4c. Debug Mode, Keepalive, Logging</h3>
<p>Track connection steps (<code>conn --debug node</code>), set idle keepalive intervals (<code>conn config --keepalive &lt;seconds&gt;</code>), or define dynamic output log files using variables like <code>${unique}</code>, <code>${host}</code>, <code>${port}</code>, <code>${user}</code>, <code>${protocol}</code>, or <code>${date 'format'}</code>.</p>
<hr>
<h2 id="5-remote-capture-conn-capture-core-plugin">5. 🖥️ Remote Capture (conn capture - Core Plugin)</h2>
<p>Perform remote packet capture (<code>tcpdump</code>) on hosts over secure SSH reverse tunnels and stream packets live into your local Wireshark GUI:</p>
<p>Perform remote packet capture (<code>tcpdump</code>) on hosts over secure SSH or Mosh reverse tunnels and stream packets live into your local Wireshark GUI:</p>
<pre><code class="language-bash">conn capture router1 eth0 -w -f &quot;port 80&quot;
</code></pre>
<ul>
@@ -1742,7 +1742,7 @@ class node:
profile = re.search(&#34;^@(.*)&#34;, str(attr[key]))
if profile and config != &#39;&#39;:
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,&#34;&#34;)
elif attr[key] == &#39;&#39; and key == &#34;protocol&#34;:
@@ -1785,7 +1785,7 @@ class node:
profile = re.search(&#34;^@(.*)&#34;, 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] = &#34;&#34;
elif jh_dict[key] == &#39;&#39; and key == &#34;protocol&#34;:
@@ -1806,6 +1806,242 @@ class node:
jh_dict[&#34;password&#34;] = [jh_dict[&#34;password&#34;]]
return jh_dict
@staticmethod
def _validate_legacy_ssh_hop_fields(host, user, port):
&#34;&#34;&#34;Minimal injection guard for legacy SSH jumphost fields (preserves DOMAIN\\user, %scope, etc.).&#34;&#34;&#34;
host_s = str(host) if host is not None else &#34;&#34;
if host_s.startswith(&#34;-&#34;):
raise ValueError(&#34;Host cannot start with &#39;-&#39;&#34;)
if not host_s or _LEGACY_SSH_UNSAFE_RE.search(host_s):
raise ValueError(f&#34;Invalid characters in host: &#39;{host}&#39;&#34;)
host_bare = (
host_s[1:-1]
if (host_s.startswith(&#34;[&#34;) and host_s.endswith(&#34;]&#34;))
else host_s
)
if not host_bare or host_bare.startswith(&#34;-&#34;):
raise ValueError(f&#34;Invalid characters in host: &#39;{host}&#39;&#34;)
user_s = str(user) if user is not None else &#34;&#34;
if user_s.startswith(&#34;-&#34;):
raise ValueError(&#34;User cannot start with &#39;-&#39;&#34;)
if user_s and _LEGACY_SSH_UNSAFE_RE.search(user_s):
raise ValueError(f&#34;Invalid characters in user: &#39;{user}&#39;&#34;)
port_out = &#34;&#34;
if port != &#34;&#34; and port is not None:
if isinstance(port, (bool, float)):
raise ValueError(f&#34;Invalid TCP SSH port &#39;{port}&#39;&#34;)
port_s = str(port).strip()
if port_s:
if (
not port_s.isascii()
or not re.fullmatch(r&#34;[0-9]+&#34;, port_s, re.ASCII)
or not (1 &lt;= int(port_s) &lt;= 65535)
):
raise ValueError(f&#34;Invalid TCP SSH port &#39;{port}&#39;&#34;)
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,
):
&#34;&#34;&#34;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 &#39;%&#39;).
Returns:
tuple[str, str, str]: (host_bare_str, validated_port_str, validated_ssh_opts_str)
&#34;&#34;&#34;
host_s = str(host) if host is not None else &#34;&#34;
if host_s.startswith(&#34;-&#34;):
raise ValueError(&#34;Host cannot start with &#39;-&#39;&#34;)
if not host_s:
raise ValueError(f&#34;Invalid characters in host: &#39;{host}&#39;&#34;)
if (host_s.startswith(&#34;[&#34;) and host_s.endswith(&#34;]&#34;)) or &#34;:&#34; in host_s:
inner = host_s[1:-1] if (host_s.startswith(&#34;[&#34;) and host_s.endswith(&#34;]&#34;)) else host_s
if &#34;%&#34; in inner or not re.fullmatch(r&#34;[0-9A-Fa-f:.]+&#34;, inner, re.ASCII):
raise ValueError(f&#34;Invalid characters in host: &#39;{host}&#39;&#34;)
try:
ipaddress.IPv6Address(inner)
except ValueError as exc:
raise ValueError(f&#34;Invalid characters in host: &#39;{host}&#39;&#34;) from exc
host_bare = inner
else:
if not re.fullmatch(r&#34;[A-Za-z0-9._-]+&#34;, host_s, re.ASCII):
raise ValueError(f&#34;Invalid characters in host: &#39;{host}&#39;&#34;)
host_bare = host_s
user_s = str(user) if user is not None else &#34;&#34;
if user_s.startswith(&#34;-&#34;):
raise ValueError(&#34;User cannot start with &#39;-&#39;&#34;)
if user_s and not re.fullmatch(
r&#34;[A-Za-z0-9._-]+(?:@[A-Za-z0-9._-]+)?&#34;, user_s, re.ASCII
):
raise ValueError(f&#34;Invalid characters in user: &#39;{user}&#39;&#34;)
port_out = &#34;&#34;
if port != &#34;&#34; and port is not None:
if isinstance(port, (bool, float)):
raise ValueError(f&#34;Invalid TCP SSH port &#39;{port}&#39;&#34;)
port_s = str(port).strip()
if port_s:
if (
not port_s.isascii()
or not re.fullmatch(r&#34;[0-9]+&#34;, port_s, re.ASCII)
or not (1 &lt;= int(port_s) &lt;= 65535)
):
raise ValueError(f&#34;Invalid TCP SSH port &#39;{port}&#39;&#34;)
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&#34;[0-9]+&#34;, r_port_s, re.ASCII)
or not (1 &lt;= int(r_port_s) &lt;= 65535)
):
raise ValueError(
f&#34;Invalid allow_reverse_tunnel_port &#39;{allow_reverse_tunnel_port}&#39;&#34;
)
allowed_r_port = r_port_s
opts_out = &#34;&#34;
raw_opts = tags.get(&#34;mosh_ssh_options&#34;, &#34;&#34;) if isinstance(tags, dict) else &#34;&#34;
if raw_opts:
ssh_opts = str(raw_opts).strip()
if &#34;\x00&#34; in ssh_opts or &#34;\n&#34; in ssh_opts or &#34;\r&#34; in ssh_opts:
raise ValueError(&#34;Disallowed control characters in mosh_ssh_options&#34;)
if ssh_opts:
try:
tokens = shlex.split(ssh_opts)
except ValueError as exc:
raise ValueError(
&#34;Invalid mosh_ssh_options: unbalanced quotes&#34;
) from exc
r_count = 0
exit_fwd_count = 0
out_tokens = []
idx = 0
# Normative branch evaluation order:
# 1. NOARG -&gt; 2. ARG separate -&gt; 3. ARG attached -&gt; 4. -R -&gt; 5. -o/-oXXX -&gt; 6. other
while idx &lt; 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 &gt;= len(tokens) or not tokens[idx + 1].strip():
raise ValueError(
f&#34;Missing argument for ssh option &#39;{tok}&#39; in mosh_ssh_options&#34;
)
arg_val = tokens[idx + 1]
val_re = _SAFE_SSH_PATH_RE if tok == &#34;-i&#34; else _SAFE_SSH_O_VALUE_RE
if arg_val.startswith(&#34;-&#34;) or not val_re.fullmatch(arg_val):
raise ValueError(
f&#34;Invalid argument for ssh option &#39;{tok}&#39; in mosh_ssh_options&#34;
)
out_tokens.extend([tok, arg_val])
idx += 2
elif tok[:2] in _ALLOWED_SSH_ARG_FLAGS and len(tok) &gt; 2:
flag_prefix = tok[:2]
arg_val = tok[2:]
val_re = _SAFE_SSH_PATH_RE if flag_prefix == &#34;-i&#34; else _SAFE_SSH_O_VALUE_RE
if (
not arg_val.strip()
or arg_val.startswith(&#34;-&#34;)
or not val_re.fullmatch(arg_val)
):
raise ValueError(
f&#34;Invalid argument for ssh option &#39;{flag_prefix}&#39; in mosh_ssh_options&#34;
)
out_tokens.append(tok)
idx += 1
elif tok == &#34;-R&#34;:
if (
allowed_r_port is None
or r_count &gt;= 1
or idx + 1 &gt;= len(tokens)
or tokens[idx + 1]
!= f&#34;{allowed_r_port}:localhost:{allowed_r_port}&#34;
):
raise ValueError(
f&#34;Disallowed ssh option in mosh_ssh_options: &#39;{tok}&#39;&#34;
)
r_count += 1
out_tokens.extend([tok, tokens[idx + 1]])
idx += 2
elif tok == &#34;-o&#34; or tok.startswith(&#34;-o&#34;):
if tok == &#34;-o&#34;:
if idx + 1 &gt;= len(tokens) or not tokens[idx + 1].strip():
raise ValueError(
&#34;Missing argument for ssh option &#39;-o&#39; in mosh_ssh_options&#34;
)
opt_spec = tokens[idx + 1]
step = 2
else:
opt_spec = tok[2:]
step = 1
if re.search(r&#34;(?:localcommand|proxycommand)&#34;, opt_spec, re.IGNORECASE):
raise ValueError(
&#34;mosh_ssh_options cannot contain LocalCommand or ProxyCommand&#34;
)
m = re.fullmatch(r&#34;([A-Za-z0-9]+)=(.+)&#34;, 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&#34;Disallowed -o directive in mosh_ssh_options: &#39;{opt_spec}&#39;&#34;
)
key_raw = m.group(1)
key_lower = key_raw.lower()
val_str = m.group(2).strip()
if key_lower == &#34;exitonforwardfailure&#34;:
if (
allowed_r_port is None
or exit_fwd_count &gt;= 1
or val_str.lower() != &#34;yes&#34;
):
raise ValueError(
f&#34;Disallowed -o directive in mosh_ssh_options: &#39;{opt_spec}&#39;&#34;
)
exit_fwd_count += 1
elif key_lower not in _ALLOWED_SSH_O_KEYS:
raise ValueError(
f&#34;Disallowed -o directive in mosh_ssh_options: &#39;{opt_spec}&#39;&#34;
)
if not (strip_non_tunnel_flags and key_lower == &#34;forwardagent&#34;):
if tok == &#34;-o&#34;:
out_tokens.extend([&#34;-o&#34;, f&#34;{key_raw}={val_str}&#34;])
else:
out_tokens.append(f&#34;-o{key_raw}={val_str}&#34;)
idx += step
else:
if re.search(r&#34;(?:localcommand|proxycommand)&#34;, tok, re.IGNORECASE):
raise ValueError(
&#34;mosh_ssh_options cannot contain LocalCommand or ProxyCommand&#34;
)
raise ValueError(
f&#34;Disallowed ssh option in mosh_ssh_options: &#39;{tok}&#39;&#34;
)
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=&#34;%h&#34;, target_port=&#34;%p&#34;):
&#39;&#39;&#39;Recursively build ProxyCommand for chained jumphosts.
@@ -1830,24 +2066,39 @@ class node:
jh = self._resolve_jumphost_data(jh, config)
if jh[&#34;protocol&#34;] == &#34;mosh&#34;:
raise ValueError(
f&#34;Jumphost &#39;{jumphost_name}&#39; uses protocol &#39;mosh&#39; which cannot be used as a jumphost.&#34;
jh_host, jh_port, jh_opts = self._validate_mosh_ssh_fields(
jh.get(&#34;host&#34;, &#34;&#34;),
jh.get(&#34;user&#34;, &#34;&#34;),
jh.get(&#34;port&#34;, &#34;&#34;),
jh.get(&#34;tags&#34;, &#34;&#34;),
strip_non_tunnel_flags=True,
)
elif jh[&#34;protocol&#34;] == &#34;ssh&#34;:
jh_host, jh_port = self._validate_legacy_ssh_hop_fields(
jh.get(&#34;host&#34;, &#34;&#34;),
jh.get(&#34;user&#34;, &#34;&#34;),
jh.get(&#34;port&#34;, &#34;&#34;),
)
jh_opts = jh.get(&#34;options&#34;, &#34;&#34;)
else:
jh_host = jh.get(&#34;host&#34;, &#34;&#34;)
jh_port = str(jh[&#34;port&#34;]).strip() if jh.get(&#34;port&#34;) not in (&#34;&#34;, None) else &#34;&#34;
jh_opts = jh.get(&#34;options&#34;, &#34;&#34;)
passwords = []
inner_proxy_opt = &#34;&#34;
# Recursively resolve inner jumphost
if jh.get(&#34;jumphost&#34;, &#34;&#34;) != &#34;&#34;:
if jh[&#34;protocol&#34;] not in [&#34;ssh&#34;]:
if jh[&#34;protocol&#34;] not in [&#34;ssh&#34;, &#34;mosh&#34;]:
raise ValueError(
f&#34;Jumphost &#39;{jumphost_name}&#39; uses protocol &#39;{jh[&#39;protocol&#39;]}&#39; &#34;
f&#34;which does not support chained jumphosts. &#34;
f&#34;Only SSH jumphosts can have their own jumphosts.&#34;
f&#34;Only SSH or Mosh jumphosts can have their own jumphosts.&#34;
)
parent_port = jh[&#34;port&#34;] if jh[&#34;port&#34;] != &#34;&#34; else &#34;22&#34;
parent_port = jh_port if jh_port != &#34;&#34; else &#34;22&#34;
inner_raw_cmd, inner_passwords = self._build_jumphost_chain(
jh[&#34;jumphost&#34;], config, visited, depth + 1, target_host=jh[&#34;host&#34;], target_port=parent_port
jh[&#34;jumphost&#34;], config, visited, depth + 1, target_host=jh_host, target_port=parent_port
)
passwords = inner_passwords
escaped = inner_raw_cmd.replace(&#39;\\&#39;, &#39;\\\\&#39;).replace(&#39;&#34;&#39;, &#39;\\&#34;&#39;)
@@ -1858,17 +2109,31 @@ class node:
passwords = passwords + jh[&#34;password&#34;]
t_port = target_port if target_port != &#34;&#34; else &#34;22&#34;
if jh[&#34;protocol&#34;] in [&#34;ssh&#34;, &#34;mosh&#34;]:
if target_host == &#34;%h&#34; and t_port == &#34;%p&#34;:
w_target = &#34;%h&#34;
w_port = &#34;%p&#34;
else:
t_host_bare, t_port_norm = self._validate_legacy_ssh_hop_fields(
target_host, &#34;&#34;, t_port
)
w_target = f&#34;[{t_host_bare}]&#34; if &#34;:&#34; in t_host_bare else t_host_bare
w_port = t_port_norm if t_port_norm != &#34;&#34; else &#34;22&#34;
else:
w_target = target_host
w_port = t_port
# Build raw command based on protocol
if jh[&#34;protocol&#34;] == &#34;ssh&#34;:
cmd = f&#34;ssh -W {target_host}:{t_port}&#34;
if jh[&#34;protocol&#34;] in [&#34;ssh&#34;, &#34;mosh&#34;]:
cmd = f&#34;ssh -W {w_target}:{w_port}&#34;
if inner_proxy_opt:
cmd += f&#34; {inner_proxy_opt}&#34;
if jh[&#34;port&#34;] != &#39;&#39;:
cmd += f&#34; -p {jh[&#39;port&#39;]}&#34;
if jh[&#34;options&#34;] != &#39;&#39;:
cmd += f&#34; {jh[&#39;options&#39;]}&#34;
user_host = f&#34;{jh[&#39;user&#39;]}@{jh[&#39;host&#39;]}&#34; if jh[&#39;user&#39;] != &#39;&#39; else jh[&#39;host&#39;]
if jh_port != &#34;&#34;:
cmd += f&#34; -p {jh_port}&#34;
if jh_opts != &#34;&#34;:
cmd += f&#34; {jh_opts}&#34;
jh_user = str(jh.get(&#34;user&#34;, &#34;&#34;) or &#34;&#34;)
user_host = f&#34;{jh_user}@{jh_host}&#34; if jh_user else jh_host
cmd += f&#34; {user_host}&#34;
elif jh[&#34;protocol&#34;] == &#34;ssm&#34;:
ssm_target = jh[&#34;host&#34;]
@@ -2766,52 +3031,62 @@ class node:
return connect
@MethodHook
def _generate_ssh_sftp_cmd(self, force_ssh=False):
is_from_mosh = getattr(self, &#34;_orig_protocol&#34;, &#34;&#34;) == &#34;mosh&#34;
if is_from_mosh:
if str(self.host).startswith(&#34;-&#34;):
raise ValueError(&#34;Host cannot start with &#39;-&#39;&#34;)
if self.user and str(self.user).startswith(&#34;-&#34;):
raise ValueError(&#34;User cannot start with &#39;-&#39;&#34;)
def _generate_ssh_sftp_cmd(self, force_ssh=False, allow_reverse_tunnel_port=None):
is_from_mosh = (
self.protocol == &#34;mosh&#34;
or getattr(self, &#34;_orig_protocol&#34;, &#34;&#34;) == &#34;mosh&#34;
)
proto = &#34;ssh&#34; if force_ssh else self.protocol
cmd = proto
if self.port != &#34;&#34; and self.port is not None:
if is_from_mosh:
port_s = str(self.port).strip()
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:
if not re.fullmatch(r&#34;^\d+$&#34;, port_s) or not (1 &lt;= int(port_s) &lt;= 65535):
raise ValueError(f&#34;Invalid TCP SSH port &#39;{self.port}&#39;&#34;)
cmd += (&#34; -p &#34; if proto == &#34;ssh&#34; else &#34; -P &#34;) + port_s
if raw_opts and proto == &#34;sftp&#34;:
sftp_tokens = []
toks = shlex.split(raw_opts)
i = 0
while i &lt; len(toks):
t = toks[i]
if t in (&#34;-X&#34;, &#34;-x&#34;, &#34;-Y&#34;, &#34;-t&#34;, &#34;-A&#34;, &#34;-a&#34;):
i += 1
elif t == &#34;-m&#34;:
i += 2
elif t.startswith(&#34;-m&#34;) and len(t) &gt; 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 &#34;&#34;
if opts:
cmd += &#34; &#34; + opts
target_h = f&#34;[{host_bare}]&#34; if (proto == &#34;sftp&#34; and &#34;:&#34; in host_bare) else host_bare
else:
if self.port != &#34;&#34; and self.port is not None:
if proto == &#34;ssh&#34;:
cmd += &#34; -p &#34; + str(self.port)
elif proto == &#34;sftp&#34;:
cmd += &#34; -P &#34; + str(self.port)
if is_from_mosh:
raw_opts = self.tags.get(&#34;mosh_ssh_options&#34;, &#34;&#34;) if isinstance(self.tags, dict) else &#34;&#34;
if raw_opts:
ssh_opts = str(raw_opts).strip()
if re.search(r&#39;(?:LocalCommand|ProxyCommand)&#39;, ssh_opts, re.IGNORECASE):
raise ValueError(&#34;mosh_ssh_options cannot contain LocalCommand or ProxyCommand&#34;)
try:
shlex.split(ssh_opts)
except ValueError as exc:
raise ValueError(&#34;Invalid mosh_ssh_options: unbalanced quotes&#34;) from exc
else:
raw_opts = self.options
if raw_opts:
opts = str(raw_opts).strip()
if proto == &#34;sftp&#34;:
# Strip SSH-only flags that sftp doesn&#39;t support
opts = re.sub(r&#39;(?&lt;!\S)-[XxtTAaNf]\b&#39;, &#39;&#39;, opts).strip()
if opts:
cmd += &#34; &#34; + opts
target_h = self.host
if self.jumphost:
cmd += &#34; &#34; + self.jumphost
user_host = f&#34;{self.user}@{self.host}&#34; if self.user else self.host
user_host = f&#34;{self.user}@{target_h}&#34; if self.user else target_h
cmd += f&#34; {user_host}&#34;
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 [&#34;ssh&#34;, &#34;sftp&#34;]:
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 == &#34;mosh&#34;:
return self._generate_mosh_cmd()
elif self.protocol == &#34;telnet&#34;:
@@ -2959,7 +3237,15 @@ class node:
return None, f&#34;Connection failed: {e}&#34;
@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 = &#34;ssh&#34; if force_ssh else self.protocol
if effective_protocol == &#34;mosh&#34;:
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 != &#39;&#39;:
self.logfile = self._logfile()
@@ -4309,7 +4598,7 @@ def test(self, commands, expected, vars = None,*, folder = None, prompt = None,
<nav id="sidebar">
<div class="toc">
<ul>
<li><a href="#connpy-v640b1">Connpy (v6.4.0b1)</a><ul>
<li><a href="#connpy-v640b2">Connpy (v6.4.0b2)</a><ul>
<li><a href="#1-ai-system">1. 🤖 AI System</a><ul>
<li><a href="#1a-terminal-copilot-ctrlspace">1a. Terminal Copilot (Ctrl+Space)</a></li>
<li><a href="#1b-ai-chat-conn-ai">1b. AI Chat (conn ai)</a></li>