def clear_exception_frames(exc: BaseException | None) -> None:
+ """Clear local variables on all frames in an exception's traceback chain."""
+ seen: set[int] = set()
+ stack: list[BaseException] = []
+ if exc is not None:
+ stack.append(exc)
+ while stack:
+ cur = stack.pop()
+ obj_id = id(cur)
+ if obj_id in seen:
+ continue
+ seen.add(obj_id)
+ cause = getattr(cur, "__cause__", None)
+ ctx = getattr(cur, "__context__", None)
+ if cause is not None:
+ stack.append(cause)
+ if ctx is not None:
+ stack.append(ctx)
+
+ tb = getattr(cur, "__traceback__", None)
+ if tb is not None:
+ with suppress(RuntimeError):
+ traceback.clear_frames(tb)
+
+ with suppress(AttributeError, TypeError):
+ cur.__traceback__ = None
+ with suppress(AttributeError, TypeError):
+ cur.__cause__ = None
+ with suppress(AttributeError, TypeError):
+ cur.__context__ = None
+
+
Clear local variables on all frames in an exception's traceback chain.
def extract_compose_profile_refs(expr: str) -> set[str]:
+ """Return profile names referenced via @profile tokens in a compose: expression."""
+ if not isinstance(expr, str) or not expr.startswith("compose:"):
+ return set()
+ if len(expr) > MAX_COMPOSE_EXPR_LEN:
+ return set()
+ body = expr[len("compose:") :]
+ if not body or not body.strip():
+ return set()
+ try:
+ tokens = _tokenize_compose_body(body)
+ except SanitizedAuthError:
+ return set()
+ return {tok_val for tok_type, tok_val in tokens if tok_type == "profile_ref"}
+
+
Return profile names referenced via @profile tokens in a compose: expression.
def extract_profile_refs_from_value(value: Any) -> set[str]:
+ """Extract all @profile names referenced directly or via compose:/fallback in a value."""
+ return _extract_profile_refs_internal(value, set())
+
+
Extract all @profile names referenced directly or via compose:/fallback in a value.
def has_insecure_hostkey_checking(
+ options: str = "",
+ tags: Any = None,
+ hop_metas: Iterable[dict[str, Any]] = (),
+) -> bool:
+ """Return True if StrictHostKeyChecking=no/off or UserKnownHostsFile=/dev/null is configured."""
+ candidates: list[str] = []
+ if options:
+ candidates.append(str(options))
+ if isinstance(tags, dict):
+ for k in ("mosh_ssh_options", "ssh_options"):
+ v = tags.get(k)
+ if v:
+ candidates.append(str(v))
+ for hop in hop_metas or ():
+ if isinstance(hop, dict):
+ h_opts = hop.get("options", "")
+ if h_opts:
+ candidates.append(str(h_opts))
+ h_tags = hop.get("tags")
+ if isinstance(h_tags, dict):
+ for k in ("mosh_ssh_options", "ssh_options"):
+ v = h_tags.get(k)
+ if v:
+ candidates.append(str(v))
+ return any(_has_insecure_ssh_options(c) for c in candidates)
+
+
Return True if StrictHostKeyChecking=no/off or UserKnownHostsFile=/dev/null is configured.
Compile an exact host boundary regex with zero FQDN subdomain wildcard risk.
+
+
+def is_compose_expression(value: Any) ‑> bool
+
+
+
+
+Expand source code
+
+
def is_compose_expression(value: Any) -> bool:
+ """Return True if value is a string with the explicit compose: prefix."""
+ return isinstance(value, str) and value.startswith("compose:")
+
+
Return True if value is a string with the explicit compose: prefix.
+
+
+def is_rsa_blob(value: Any) ‑> bool
+
+
+
+
+Expand source code
+
+
def is_rsa_blob(value: Any) -> bool:
+ """Return True if value matches an encrypted RSA bytes literal string."""
+ return isinstance(value, str) and bool(_RSA_BLOB_RE.match(value))
+
+
Return True if value matches an encrypted RSA bytes literal string.
+
+
+def mask_node_secrets(obj: Any) ‑> Any
+
+
+
+
+Expand source code
+
+
def mask_node_secrets(obj: Any) -> Any:
+ """Recursively mask sensitive node/profile credentials with '***' using a closed key list."""
+ visiting: set[int] = set()
+
+ def _recurse(val: Any, *, in_fallback_list: bool = False) -> Any:
+ if isinstance(val, dict):
+ obj_id = id(val)
+ if obj_id in visiting:
+ return "***"
+ visiting.add(obj_id)
+ try:
+ out: dict[Any, Any] = {}
+ for k, v in val.items():
+ k_lower = k.lower() if isinstance(k, str) else str(k).lower()
+ if _mask_secret_key(k_lower):
+ out[k] = "***"
+ elif k_lower in ("fallback", "_fallback"):
+ if isinstance(v, (dict, list, tuple)):
+ out[k] = _recurse(v, in_fallback_list=True)
+ elif v in ("", None):
+ out[k] = v
+ else:
+ out[k] = "***"
+ else:
+ out[k] = _recurse(v, in_fallback_list=False)
+ return out
+ finally:
+ visiting.discard(obj_id)
+
+ if isinstance(val, list):
+ obj_id = id(val)
+ if obj_id in visiting:
+ return "***"
+ visiting.add(obj_id)
+ try:
+ return [
+ "***"
+ if (in_fallback_list and isinstance(item, str) and item != "")
+ else _recurse(item, in_fallback_list=in_fallback_list)
+ for item in val
+ ]
+ finally:
+ visiting.discard(obj_id)
+
+ if isinstance(val, tuple):
+ obj_id = id(val)
+ if obj_id in visiting:
+ return "***"
+ visiting.add(obj_id)
+ try:
+ return tuple(
+ "***"
+ if (in_fallback_list and isinstance(item, str) and item != "")
+ else _recurse(item, in_fallback_list=in_fallback_list)
+ for item in val
+ )
+ finally:
+ visiting.discard(obj_id)
+
+ return val
+
+ return _recurse(obj)
+
+
Recursively mask sensitive node/profile credentials with '***' using a closed key list.
def normalize_prompt_line(before_bytes: bytes, after_bytes: bytes) -> str:
+ """Return the normalized final prompt line stripped of ANSI and control sequences."""
+ combined = (before_bytes or b"") + (after_bytes or b"")
+ text = combined.decode("utf-8", errors="replace")
+ text = _ANSI_OSC_RE.sub("", text)
+ text = _ANSI_CSI_RE.sub("", text)
+ text = text.replace("\r\n", "\n").replace("\r", "\n")
+ lines = [line.strip() for line in text.split("\n") if line.strip()]
+ if not lines:
+ return ""
+ return _CONTROL_CHAR_RE.sub("", lines[-1]).strip()
+
+
Return the normalized final prompt line stripped of ANSI and control sequences.
def redact_known_secrets(
+ text: str,
+ secrets: Iterable[str] = (),
+ *,
+ min_len: int = 4,
+) -> str:
+ """Replace any non-trivial secret strings in text with '***'."""
+ if not text or not secrets:
+ return text
+ out = str(text)
+ unique_secrets = sorted(
+ {s for s in secrets if isinstance(s, str) and len(s) >= min_len},
+ key=len,
+ reverse=True,
+ )
+ for sec in unique_secrets:
+ out = out.replace(sec, "***")
+ return out
+
+
Replace any non-trivial secret strings in text with '***'.
def sanitize_log_message(message: str, *, secrets: Iterable[str] = ()) -> str:
+ """Strip ANSI sequences, control chars, and known secrets from a log line."""
+ text = str(message) if message is not None else ""
+ text = redact_known_secrets(text, secrets=secrets, min_len=1)
+ text = _ANSI_OSC_RE.sub("", text)
+ text = _ANSI_CSI_RE.sub("", text)
+ text = _CONTROL_CHAR_RE.sub("", text)
+ return redact_known_secrets(text, secrets=secrets, min_len=1)
+
+
Strip ANSI sequences, control chars, and known secrets from a log line.
def validate_auth_budget_tags(tags: Any) -> None:
+ """Validate optional auth budget and policy keys in a node/profile tags dict."""
+ if not isinstance(tags, dict):
+ return
+ if "max_auth_prompts" in tags and tags["max_auth_prompts"] is not None:
+ validate_strict_int_range(
+ tags["max_auth_prompts"],
+ "tags.max_auth_prompts",
+ MIN_AUTH_PROMPTS,
+ MAX_AUTH_PROMPTS_CAP,
+ )
+ if "max_total_auth_attempts" in tags and tags["max_total_auth_attempts"] is not None:
+ validate_strict_int_range(
+ tags["max_total_auth_attempts"],
+ "tags.max_total_auth_attempts",
+ MIN_TOTAL_AUTH_ATTEMPTS,
+ MAX_TOTAL_AUTH_ATTEMPTS_CAP,
+ )
+ if "max_hop_host_attempts" in tags and tags["max_hop_host_attempts"] is not None:
+ validate_strict_int_range(
+ tags["max_hop_host_attempts"],
+ "tags.max_hop_host_attempts",
+ MIN_HOP_HOST_ATTEMPTS,
+ MAX_HOP_HOST_ATTEMPTS_CAP,
+ )
+ if "max_global_user_attempts" in tags and tags["max_global_user_attempts"] is not None:
+ validate_strict_int_range(
+ tags["max_global_user_attempts"],
+ "tags.max_global_user_attempts",
+ 1,
+ 1000,
+ )
+ for bool_key in (
+ "allow_fallback",
+ "allow_ambiguous_single_hop_prompt",
+ "disable_fallback_on_insecure_hostkey",
+ ):
+ if bool_key in tags and tags[bool_key] is not None and not isinstance(tags[bool_key], bool):
+ raise ValueError(f"Invalid tags.{bool_key}: expected boolean")
+ if "fallback" in tags and tags["fallback"] not in (None, "", []):
+ validate_fallback_entries(tags["fallback"])
+
+
Validate optional auth budget and policy keys in a node/profile tags dict.
+
+
+def validate_compose_syntax(expr: str) ‑> None
+
+
+
+
+Expand source code
+
+
def validate_compose_syntax(expr: str) -> None:
+ """Validate compose: syntax and length without resolving profiles or decrypting."""
+ if not isinstance(expr, str) or len(expr) > MAX_COMPOSE_EXPR_LEN:
+ raise_sanitized_auth_error(
+ code="AUTH_FAILED",
+ context_label="compose expression exceeds maximum length",
+ )
+ body = expr.removeprefix("compose:")
+ if not body or not body.strip():
+ raise_sanitized_auth_error(
+ code="AUTH_FAILED",
+ context_label="empty compose expression",
+ )
+ _tokenize_compose_body(body)
+
+
Validate compose: syntax and length without resolving profiles or decrypting.
def validate_config_auth_settings(cfg: Any) -> None:
+ """Validate global config auth budget and policy keys."""
+ if not isinstance(cfg, dict):
+ raise TypeError("Config section must be a dictionary")
+ if "max_auth_prompts" in cfg and cfg["max_auth_prompts"] is not None:
+ validate_strict_int_range(
+ cfg["max_auth_prompts"],
+ "config.max_auth_prompts",
+ MIN_AUTH_PROMPTS,
+ MAX_AUTH_PROMPTS_CAP,
+ )
+ if "max_total_auth_attempts" in cfg and cfg["max_total_auth_attempts"] is not None:
+ validate_strict_int_range(
+ cfg["max_total_auth_attempts"],
+ "config.max_total_auth_attempts",
+ MIN_TOTAL_AUTH_ATTEMPTS,
+ MAX_TOTAL_AUTH_ATTEMPTS_CAP,
+ )
+ if "max_hop_host_attempts" in cfg and cfg["max_hop_host_attempts"] is not None:
+ validate_strict_int_range(
+ cfg["max_hop_host_attempts"],
+ "config.max_hop_host_attempts",
+ MIN_HOP_HOST_ATTEMPTS,
+ MAX_HOP_HOST_ATTEMPTS_CAP,
+ )
+ if "max_global_user_attempts" in cfg and cfg["max_global_user_attempts"] is not None:
+ validate_strict_int_range(
+ cfg["max_global_user_attempts"],
+ "config.max_global_user_attempts",
+ 1,
+ 1000,
+ )
+ if (
+ "disable_fallback_on_insecure_hostkey" in cfg
+ and cfg["disable_fallback_on_insecure_hostkey"] is not None
+ and not isinstance(cfg["disable_fallback_on_insecure_hostkey"], bool)
+ ):
+ raise ValueError("Invalid config.disable_fallback_on_insecure_hostkey: expected boolean")
+
+
Validate global config auth budget and policy keys.
def validate_fallback_entries(
+ raw_fallback: Any,
+ *,
+ max_entries: int = MAX_FALLBACK_ENTRIES,
+) -> list[dict[str, Any] | str]:
+ """Validate and normalize fallback config entries without expanding profiles."""
+ if raw_fallback in (None, "", []):
+ return []
+
+ parsed = raw_fallback
+ if isinstance(raw_fallback, str):
+ stripped = raw_fallback.strip()
+ if not stripped:
+ return []
+ if stripped.startswith(("[", "{")):
+ parsed = validate_json_depth_and_size(stripped)
+ else:
+ if stripped.startswith("compose:"):
+ validate_compose_syntax(stripped)
+ return [stripped]
+
+ if isinstance(parsed, dict):
+ items: list[Any] = [parsed]
+ elif isinstance(parsed, (list, tuple)):
+ items = list(parsed)
+ else:
+ raise TypeError(
+ "Fallback must be a list of credential dicts/strings, a dict, @profile, or JSON"
+ )
+
+ if len(items) > max_entries:
+ raise ValueError(f"Fallback exceeds maximum of {max_entries} entries")
+
+ normalized: list[dict[str, Any] | str] = []
+ allowed_entry_keys = {"user", "password"}
+ for idx, entry in enumerate(items):
+ if isinstance(entry, str):
+ if not entry:
+ raise ValueError(f"Fallback entry {idx} cannot be empty")
+ if entry.startswith("compose:"):
+ validate_compose_syntax(entry)
+ normalized.append(entry)
+ elif isinstance(entry, dict):
+ unknown_keys = set(entry.keys()) - allowed_entry_keys
+ if unknown_keys:
+ raise ValueError(
+ f"Unsupported keys in fallback entry {idx}: {sorted(unknown_keys)}"
+ )
+ user_val = entry.get("user", "")
+ pw_val = entry.get("password", "")
+ if user_val is not None and not isinstance(user_val, str):
+ raise ValueError(f"Fallback entry {idx} 'user' must be a string")
+ if isinstance(pw_val, (list, tuple)):
+ if not pw_val or not all(isinstance(p, str) and p != "" for p in pw_val):
+ raise ValueError(
+ f"Fallback entry {idx} 'password' list must contain non-empty strings"
+ )
+ for p in pw_val:
+ if p.startswith("compose:"):
+ validate_compose_syntax(p)
+ elif pw_val is not None and not isinstance(pw_val, str):
+ raise ValueError(f"Fallback entry {idx} 'password' must be a string or list")
+ elif isinstance(pw_val, str) and pw_val.startswith("compose:"):
+ validate_compose_syntax(pw_val)
+
+ if not (user_val or pw_val):
+ raise ValueError(
+ f"Fallback entry {idx} must specify 'user' and/or 'password'"
+ )
+ normalized.append(deepcopy(entry))
+ else:
+ raise TypeError(f"Fallback entry {idx} must be a dict or string")
+
+ return normalized
+
+
Validate and normalize fallback config entries without expanding profiles.
+
+
+def validate_json_depth_and_size(raw_or_obj: Any, *, max_bytes: int = 8192, max_depth: int = 4) ‑> Any
+
+
+
+
+Expand source code
+
+
def validate_json_depth_and_size(
+ raw_or_obj: Any,
+ *,
+ max_bytes: int = MAX_JSON_INPUT_BYTES,
+ max_depth: int = MAX_JSON_INPUT_DEPTH,
+) -> Any:
+ """Parse (if str) and enforce byte length and nesting depth limits on JSON inputs."""
+ if isinstance(raw_or_obj, str):
+ if len(raw_or_obj) > max_bytes or len(raw_or_obj.encode("utf-8", errors="replace")) > max_bytes:
+ raise ValueError(f"JSON input exceeds maximum size of {max_bytes} bytes")
+ try:
+ parsed = json.loads(raw_or_obj)
+ except RecursionError:
+ raise ValueError(
+ f"JSON nesting exceeds maximum depth of {max_depth}"
+ ) from None
+ else:
+ parsed = raw_or_obj
+ _check_depth(parsed, max_depth, 0)
+ return parsed
+
+
Parse (if str) and enforce byte length and nesting depth limits on JSON inputs.
def validate_strict_int_range(
+ value: Any,
+ name: str,
+ min_val: int,
+ max_val: int,
+) -> int:
+ """Validate that value is a strict plain int (not bool/float/str) in [min_val, max_val]."""
+ if type(value) is not int or value < min_val or value > max_val:
+ raise ValueError(
+ f"Invalid {name}: expected integer in [{min_val}, {max_val}], got {value!r}"
+ )
+ return value
+
+
Validate that value is a strict plain int (not bool/float/str) in [min_val, max_val].
+
+
+
+
+
Classes
+
+
+class AuthAttemptTracker
+(*, ttl_seconds: float = 300, max_entries: int = 10000, default_max_host_attempts: int = 6, default_max_hop_attempts: int = 6, default_max_global_user_attempts: int | None = None, clock: Callable[[], float] = <built-in function monotonic>)
+
+
+
+
+Expand source code
+
+
class AuthAttemptTracker:
+ """Thread-safe intra-process authentication attempt tracker with safe anti-flood eviction."""
+
+ def __init__(
+ self,
+ *,
+ ttl_seconds: float = DEFAULT_TRACKER_TTL_SECONDS,
+ max_entries: int = MAX_TRACKER_ENTRIES,
+ default_max_host_attempts: int = DEFAULT_MAX_TOTAL_AUTH_ATTEMPTS,
+ default_max_hop_attempts: int = DEFAULT_MAX_HOP_HOST_ATTEMPTS,
+ default_max_global_user_attempts: int | None = None,
+ clock: Callable[[], float] = time.monotonic,
+ ) -> None:
+ if isinstance(ttl_seconds, bool) or not isinstance(ttl_seconds, (int, float)):
+ raise TypeError(f"Invalid ttl_seconds: expected number > 0, got {ttl_seconds!r}")
+ ttl_val = float(ttl_seconds)
+ if not math.isfinite(ttl_val) or ttl_val <= 0:
+ raise ValueError(f"Invalid ttl_seconds: expected > 0, got {ttl_seconds!r}")
+ if isinstance(max_entries, bool) or not isinstance(max_entries, int):
+ raise TypeError(f"Invalid max_entries: expected int >= 1, got {max_entries!r}")
+ if max_entries < 1:
+ raise ValueError(f"Invalid max_entries: expected >= 1, got {max_entries!r}")
+ self._ttl_seconds = ttl_val
+ self._max_entries = max_entries
+ self._default_max_host_attempts = validate_strict_int_range(
+ default_max_host_attempts,
+ "default_max_host_attempts",
+ MIN_TOTAL_AUTH_ATTEMPTS,
+ MAX_TOTAL_AUTH_ATTEMPTS_CAP,
+ )
+ self._default_max_hop_attempts = validate_strict_int_range(
+ default_max_hop_attempts,
+ "default_max_hop_attempts",
+ MIN_HOP_HOST_ATTEMPTS,
+ MAX_HOP_HOST_ATTEMPTS_CAP,
+ )
+ self._default_max_global_user_attempts: int | None = (
+ validate_strict_int_range(
+ default_max_global_user_attempts,
+ "default_max_global_user_attempts",
+ 1,
+ 1000,
+ )
+ if default_max_global_user_attempts is not None
+ else None
+ )
+ self._clock = clock
+ self._lock = threading.Lock()
+ self._unblocked: OrderedDict[Any, _BudgetEntry] = OrderedDict()
+ self._blocked: OrderedDict[Any, _BudgetEntry] = OrderedDict()
+
+ def _purge_expired_unlocked(self, now: float) -> None:
+ for bucket in (self._unblocked, self._blocked):
+ expired_keys = [
+ k for k, entry in bucket.items() if (now - entry.last_ts) >= self._ttl_seconds
+ ]
+ for k in expired_keys:
+ del bucket[k]
+
+ def _evict_if_full_unlocked(self, now: float) -> None:
+ if (len(self._unblocked) + len(self._blocked)) < self._max_entries:
+ return
+ self._purge_expired_unlocked(now)
+ while (len(self._unblocked) + len(self._blocked)) >= self._max_entries:
+ if self._unblocked:
+ self._unblocked.popitem(last=False)
+ elif self._blocked:
+ self._blocked.popitem(last=False)
+ else:
+ break
+
+ def _get_live_entry_unlocked(self, key: Any, now: float) -> _BudgetEntry | None:
+ if key in self._unblocked:
+ entry = self._unblocked[key]
+ if (now - entry.last_ts) >= self._ttl_seconds:
+ del self._unblocked[key]
+ return None
+ return entry
+ if key in self._blocked:
+ entry = self._blocked[key]
+ if (now - entry.last_ts) >= self._ttl_seconds:
+ del self._blocked[key]
+ return None
+ return entry
+ return None
+
+ def _can_acquire_unlocked(self, key: Any, limit: int, now: float) -> bool:
+ if limit <= 0:
+ return False
+ entry = self._get_live_entry_unlocked(key, now)
+ if entry is None:
+ return True
+ if entry.count >= limit:
+ entry.limit = limit
+ if key in self._unblocked:
+ del self._unblocked[key]
+ self._blocked[key] = entry
+ return False
+ return True
+
+ def _commit_acquire_unlocked(self, key: Any, limit: int, now: float) -> None:
+ entry = self._get_live_entry_unlocked(key, now)
+ if entry is None:
+ self._evict_if_full_unlocked(now)
+ new_entry = _BudgetEntry(count=1, limit=limit, last_ts=now)
+ if 1 >= limit:
+ self._blocked[key] = new_entry
+ else:
+ self._unblocked[key] = new_entry
+ return
+
+ entry.count += 1
+ entry.limit = limit
+ entry.last_ts = now
+ if entry.count >= limit:
+ if key in self._unblocked:
+ del self._unblocked[key]
+ self._blocked[key] = entry
+ self._blocked.move_to_end(key)
+ else:
+ if key in self._blocked:
+ del self._blocked[key]
+ self._unblocked[key] = entry
+ self._unblocked.move_to_end(key)
+
+ def try_acquire(self, key: Any, *, limit: int) -> bool:
+ """Attempt to consume 1 attempt slot for key within limit. Returns False if exhausted."""
+ if limit <= 0:
+ return False
+ now = self._clock()
+ with self._lock:
+ if not self._can_acquire_unlocked(key, limit, now):
+ return False
+ self._commit_acquire_unlocked(key, limit, now)
+ return True
+
+ def try_acquire_target_attempt(
+ self,
+ *,
+ host: str,
+ port: str = "",
+ protocol: str = "ssh",
+ user: str = "",
+ host_limit: int | None = None,
+ user_limit: int | None = None,
+ ) -> tuple[bool, str | None]:
+ """Atomically verify and consume both user and host attempt slots under one lock."""
+ eff_host_limit = self._default_max_host_attempts if host_limit is None else int(host_limit)
+ eff_user_limit = self._default_max_global_user_attempts if user_limit is None else user_limit
+ host_key = self._host_key(host, port, protocol)
+ user_key = self._user_key(user) if (user and eff_user_limit is not None) else None
+ now = self._clock()
+ with self._lock:
+ if user_key is not None and not self._can_acquire_unlocked(
+ user_key, int(eff_user_limit), now
+ ):
+ return False, "USER_BUDGET_EXHAUSTED"
+ if not self._can_acquire_unlocked(host_key, eff_host_limit, now):
+ return False, "HOST_BUDGET_EXHAUSTED"
+ if user_key is not None:
+ self._commit_acquire_unlocked(user_key, int(eff_user_limit), now)
+ self._commit_acquire_unlocked(host_key, eff_host_limit, now)
+ return True, None
+
+ def remaining(self, key: Any, *, limit: int) -> int:
+ """Return remaining attempt quota for key."""
+ now = self._clock()
+ with self._lock:
+ entry = self._get_live_entry_unlocked(key, now)
+ if entry is None:
+ return max(0, limit)
+ return max(0, limit - entry.count)
+
+ @staticmethod
+ def _host_key(host: str, port: str = "", protocol: str = "ssh") -> tuple[str, str, str, str]:
+ return ("host", str(host).lower(), str(port or "").strip(), str(protocol or "").lower())
+
+ @staticmethod
+ def _hop_key(host: str, port: str = "", protocol: str = "ssh") -> tuple[str, str, str, str]:
+ return ("hop", str(host).lower(), str(port or "").strip(), str(protocol or "").lower())
+
+ @staticmethod
+ def _user_key(user: str) -> tuple[str, str]:
+ return ("user", str(user))
+
+ def try_acquire_host(
+ self,
+ host: str,
+ port: str = "",
+ protocol: str = "ssh",
+ *,
+ limit: int | None = None,
+ ) -> bool:
+ """Consume 1 target-host authentication attempt if within budget."""
+ eff_limit = self._default_max_host_attempts if limit is None else int(limit)
+ return self.try_acquire(self._host_key(host, port, protocol), limit=eff_limit)
+
+ def try_acquire_hop(
+ self,
+ host: str,
+ port: str = "",
+ protocol: str = "ssh",
+ *,
+ limit: int | None = None,
+ ) -> bool:
+ """Consume 1 jumphost authentication attempt if within budget."""
+ eff_limit = self._default_max_hop_attempts if limit is None else int(limit)
+ return self.try_acquire(self._hop_key(host, port, protocol), limit=eff_limit)
+
+ def try_acquire_user(
+ self,
+ user: str,
+ *,
+ limit: int | None = None,
+ ) -> bool:
+ """Consume 1 global username authentication attempt if a global limit is active."""
+ if not user:
+ return True
+ eff_limit = self._default_max_global_user_attempts if limit is None else limit
+ if eff_limit is None:
+ return True
+ return self.try_acquire(self._user_key(user), limit=int(eff_limit))
+
+ def remaining_host(
+ self,
+ host: str,
+ port: str = "",
+ protocol: str = "ssh",
+ *,
+ limit: int | None = None,
+ ) -> int:
+ """Return remaining target-host attempt quota."""
+ eff_limit = self._default_max_host_attempts if limit is None else int(limit)
+ return self.remaining(self._host_key(host, port, protocol), limit=eff_limit)
+
+ def remaining_hop(
+ self,
+ host: str,
+ port: str = "",
+ protocol: str = "ssh",
+ *,
+ limit: int | None = None,
+ ) -> int:
+ """Return remaining jumphost attempt quota."""
+ eff_limit = self._default_max_hop_attempts if limit is None else int(limit)
+ return self.remaining(self._hop_key(host, port, protocol), limit=eff_limit)
+
+ def remaining_user(
+ self,
+ user: str,
+ *,
+ limit: int | None = None,
+ ) -> int | None:
+ """Return remaining global username quota, or None if unlimited."""
+ eff_limit = self._default_max_global_user_attempts if limit is None else limit
+ if eff_limit is None:
+ return None
+ return self.remaining(self._user_key(user), limit=int(eff_limit))
+
+ def is_blocked(self, key: Any) -> bool:
+ """Return True if key currently has an active exhausted budget entry."""
+ now = self._clock()
+ with self._lock:
+ entry = self._get_live_entry_unlocked(key, now)
+ return entry is not None and entry.count >= entry.limit
+
+ def entry_count(self) -> int:
+ """Return total number of tracked entries (before TTL expiry)."""
+ with self._lock:
+ return len(self._unblocked) + len(self._blocked)
+
+ def reset(self) -> None:
+ """Clear all tracked entries."""
+ with self._lock:
+ self._unblocked.clear()
+ self._blocked.clear()
+
+
Thread-safe intra-process authentication attempt tracker with safe anti-flood eviction.
+
Methods
+
+
+def entry_count(self) ‑> int
+
+
+
+
+Expand source code
+
+
def entry_count(self) -> int:
+ """Return total number of tracked entries (before TTL expiry)."""
+ with self._lock:
+ return len(self._unblocked) + len(self._blocked)
+
+
Return total number of tracked entries (before TTL expiry).
+
+
+def is_blocked(self, key: Any) ‑> bool
+
+
+
+
+Expand source code
+
+
def is_blocked(self, key: Any) -> bool:
+ """Return True if key currently has an active exhausted budget entry."""
+ now = self._clock()
+ with self._lock:
+ entry = self._get_live_entry_unlocked(key, now)
+ return entry is not None and entry.count >= entry.limit
+
+
Return True if key currently has an active exhausted budget entry.
+
+
+def remaining(self, key: Any, *, limit: int) ‑> int
+
+
+
+
+Expand source code
+
+
def remaining(self, key: Any, *, limit: int) -> int:
+ """Return remaining attempt quota for key."""
+ now = self._clock()
+ with self._lock:
+ entry = self._get_live_entry_unlocked(key, now)
+ if entry is None:
+ return max(0, limit)
+ return max(0, limit - entry.count)
def pass_validation(self, answers, current, regex = "(^@.+$)"):
+ if is_compose_expression(current):
+ try:
+ validate_compose_syntax(current)
+ except SanitizedAuthError as exc:
+ _raise_val_err(str(exc))
+ return True
profiles = current.split(",")
for i in profiles:
if not re.match(regex, i) or i[1:] not in self.app.profiles:
@@ -413,13 +501,7 @@ el.replaceWith(d);
def profile_tags_validation(self, answers, current):
if current != "":
- isdict = False
- try:
- isdict = ast.literal_eval(current)
- except Exception:
- pass
- if not isinstance (isdict, dict):
- _raise_val_err("Tags should be a python dictionary.".format(current))
+ return self._validate_tags_dict(current)
return True
@@ -455,13 +537,7 @@ el.replaceWith(d);
if current[1:] not in self.app.profiles:
_raise_val_err("Profile {} don't exist".format(current))
elif current != "":
- isdict = False
- try:
- isdict = ast.literal_eval(current)
- except Exception:
- pass
- if not isinstance (isdict, dict):
- _raise_val_err("Tags should be a python dictionary.".format(current))
+ return self._validate_tags_dict(current)
return True
def handle_errors(func):
import inspect
if inspect.isgeneratorfunction(func):
+ @functools.wraps(func)
def wrapper(*args, **kwargs):
try:
for item in func(*args, **kwargs):
@@ -75,6 +76,7 @@ el.replaceWith(d);
printer.clear_thread_state()
return wrapper
else:
+ @functools.wraps(func)
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
@@ -1425,8 +1427,24 @@ interceptor chooses to service this RPC, or None otherwise.
self.server_console.print(f"[debug][DEBUG][/debug] gRPC interact_node request for: [bold cyan]{unique_id}[/bold cyan]")
if first_req.connection_params_json:
- import json
- params = json.loads(first_req.connection_params_json)
+ try:
+ params = validate_json_depth_and_size(
+ first_req.connection_params_json,
+ max_bytes=MAX_JSON_INPUT_BYTES,
+ max_depth=MAX_JSON_INPUT_DEPTH,
+ )
+ if not isinstance(params, dict):
+ raise TypeError("connection_params_json must be a JSON object")
+ if "fallback" in params and params["fallback"] not in (None, "", []):
+ validate_fallback_entries(
+ params["fallback"],
+ max_entries=MAX_FALLBACK_ENTRIES,
+ )
+ if "tags" in params and params["tags"] not in ("", None):
+ validate_auth_budget_tags(params["tags"])
+ except (ValueError, TypeError, SanitizedAuthError) as exc:
+ context.abort(grpc.StatusCode.INVALID_ARGUMENT, str(exc))
+
base_node_id = params.get("base_node")
# Valid attributes that a node object accepts
valid_attrs = ['host', 'options', 'logs', 'password', 'port', 'protocol', 'user', 'jumphost']
@@ -1444,6 +1462,8 @@ interceptor chooses to service this RPC, or None otherwise.
for attr in valid_attrs:
if attr in params:
device[attr] = params[attr]
+ if "fallback" in params:
+ device["fallback"] = params["fallback"]
if "tags" in params:
device_tags = device.get("tags", {})
@@ -1457,20 +1477,30 @@ interceptor chooses to service this RPC, or None otherwise.
else:
# base_node not found, fall back to dynamic
node_name = params.get("name", fallback_id)
- n = node(node_name, host=params.get("host", ""), config=user_config)
- for attr in valid_attrs:
- if attr in params:
- setattr(n, attr, params[attr])
+ dyn_kwargs = {k: params[k] for k in valid_attrs if k in params and k != "host"}
+ if "fallback" in params:
+ dyn_kwargs["fallback"] = params["fallback"]
if "tags" in params:
- n.tags = params["tags"]
+ dyn_kwargs["tags"] = params["tags"]
+ n = node(
+ node_name,
+ host=params.get("host", ""),
+ config=user_config,
+ **dyn_kwargs,
+ )
else:
node_name = params.get("name", fallback_id)
- n = node(node_name, host=params.get("host", ""), config=user_config)
- for attr in valid_attrs:
- if attr in params:
- setattr(n, attr, params[attr])
+ dyn_kwargs = {k: params[k] for k in valid_attrs if k in params and k != "host"}
+ if "fallback" in params:
+ dyn_kwargs["fallback"] = params["fallback"]
if "tags" in params:
- n.tags = params["tags"]
+ dyn_kwargs["tags"] = params["tags"]
+ n = node(
+ node_name,
+ host=params.get("host", ""),
+ config=user_config,
+ **dyn_kwargs,
+ )
n._orig_protocol = n.protocol
if sftp:
n.protocol = "sftp"
@@ -1491,9 +1521,11 @@ interceptor chooses to service this RPC, or None otherwise.
debug_chunks = []
if debug:
from io import StringIO
+
from rich.console import Console as RichConsole
- from ..printer import connpy_theme
+
from .. import printer as _printer
+ from ..printer import connpy_theme
def remote_logger(msg_type, message):
buf = StringIO()
@@ -1514,7 +1546,14 @@ interceptor chooses to service this RPC, or None otherwise.
else:
remote_logger = None
- connect = n._connect(debug=debug, logger=remote_logger)
+ try:
+ connect = n._connect(
+ debug=debug,
+ logger=remote_logger,
+ attempt_tracker=getattr(provider, "auth_tracker", None),
+ )
+ except Exception as exc: # noqa: BLE001
+ connect = str(sanitize_exception(exc, code="AUTH_FAILED"))
# Send debug output to client before checking result (always show the command)
for chunk in debug_chunks:
@@ -1845,10 +1884,9 @@ interceptor chooses to service this RPC, or None otherwise.
continue
asyncio.run(n._async_interact_loop(remote_stream, resize_callback, copilot_handler=remote_copilot_handler))
- except Exception as e:
- import traceback
- print(f"[ERROR in run_async_loop] {e}")
- traceback.print_exc()
+ except Exception as e: # noqa: BLE001
+ sanitized_err = sanitize_exception(e, code="TRANSPORT_ERROR", context_label="interact stream")
+ print(f"[ERROR in run_async_loop] {sanitized_err}")
finally:
n._teardown_interact_environment()
response_queue.put(None) # Signal EOF
diff --git a/docs/connpy/index.html b/docs/connpy/index.html
index b90da42..56ffaa1 100644
--- a/docs/connpy/index.html
+++ b/docs/connpy/index.html
@@ -41,7 +41,7 @@ el.replaceWith(d);
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'}.
+
4d. Adaptive Multi-Hop Timeouts
+
Differentiate --connect-timeout and --auth-timeout stages (conn run / conn config), automatically computing topological per-hop latency budgets for chained SSM and Jumphost tunnels with bounded progress extensions on slow handshakes.
5. 🖥️ Remote Capture (conn capture - Core Plugin)
Perform remote packet capture (tcpdump) on hosts over secure SSH or Mosh reverse tunnels and stream packets live into your local Wireshark GUI:
@@ -739,6 +750,8 @@ class configfile:
'''
def __init__(self, conf = None, key = None, shared_config = None):
+ self._mutation_lock = threading.RLock()
+ self._cache_dirty = False
self._shared_config = shared_config
'''
@@ -806,10 +819,10 @@ class configfile:
json.dump(old_data, f)
shutil.move(legacy_file, legacy_file + ".backup")
printer.success(f"Migrated legacy config ({len(old_data.get('connections',{}))} folders/nodes) into YAML and Cache successfully!")
- except Exception as e:
+ except Exception as e: # noqa: BLE001
if os.path.exists(self.file):
- try: os.remove(self.file)
- except OSError: pass
+ with suppress(OSError):
+ os.remove(self.file)
printer.warning(f"Failed to migrate legacy config: {e}")
else:
# Custom path (common in tests): isolate everything to the conf parent directory
@@ -904,12 +917,99 @@ class configfile:
return val if val is not None else default
+ def _validate_config_entry_unlocked(self, entry):
+ """Strictly validate tags, fallback, and password compose syntax on a node or profile entry."""
+ if not isinstance(entry, dict):
+ return
+ if "tags" in entry and entry["tags"] not in ("", None):
+ validate_auth_budget_tags(entry["tags"])
+ if "fallback" in entry and entry["fallback"] not in (None, "", []):
+ validate_fallback_entries(entry["fallback"])
+ pw = entry.get("password")
+ if is_compose_expression(pw):
+ validate_compose_syntax(pw)
+ elif isinstance(pw, (list, tuple)):
+ for item in pw:
+ if is_compose_expression(item):
+ validate_compose_syntax(item)
+
+ def _walk_connections_validate_unlocked(self, container):
+ """Recursively validate all connection dicts inside nested folders/subfolders."""
+ if not isinstance(container, dict):
+ raise TypeError("Connections container must be a dict")
+ for key, val in container.items():
+ if key == "type" or not isinstance(val, dict):
+ continue
+ entry_type = val.get("type")
+ if entry_type in ("folder", "subfolder"):
+ self._walk_connections_validate_unlocked(val)
+ else:
+ self._validate_config_entry_unlocked(val)
+
+ def _validate_config_strict(self, data):
+ """Strictly validate config structure, budgets, and fallback rules; raises on error."""
+ if not isinstance(data, dict):
+ raise TypeError("Config root must be a dict")
+ required = {"config", "connections", "profiles"}
+ if not required.issubset(data.keys()):
+ raise ValueError("Config missing required sections")
+ if not isinstance(data["config"], dict):
+ raise TypeError("'config' section must be a dict")
+ if not isinstance(data["connections"], dict):
+ raise TypeError("'connections' section must be a dict")
+ if not isinstance(data["profiles"], dict):
+ raise TypeError("'profiles' section must be a dict")
+ validate_config_auth_settings(data["config"])
+ self._walk_connections_validate_unlocked(data["connections"])
+ for prof_val in data["profiles"].values():
+ if not isinstance(prof_val, dict):
+ raise TypeError("Each profile entry must be a dict")
+ self._validate_config_entry_unlocked(prof_val)
+
def _validate_config(self, data):
"""Verify config data has the required structure."""
- if not isinstance(data, dict):
+ try:
+ self._validate_config_strict(data)
+ except (ValueError, TypeError, SanitizedAuthError):
return False
- required = {"config", "connections", "profiles"}
- return required.issubset(data.keys())
+ return True
+
+ def _atomic_write_text_unlocked(self, target_path, writer):
+ """Atomically write text file with symlink refusal, 0o600 mode, fsync, and os.replace."""
+ if os.path.islink(target_path):
+ raise OSError(f"Refusing to write through symlink: {target_path}")
+ target_dir = os.path.dirname(os.path.abspath(target_path)) or "."
+ os.makedirs(target_dir, exist_ok=True)
+ fd, tmp_path = tempfile.mkstemp(
+ dir=target_dir,
+ prefix=f".{os.path.basename(target_path)}.",
+ suffix=".tmp",
+ )
+ try:
+ with os.fdopen(fd, "w", encoding="utf-8") as f:
+ writer(f)
+ f.flush()
+ os.fsync(f.fileno())
+ os.chmod(tmp_path, 0o600)
+ os.replace(tmp_path, target_path)
+ except BaseException:
+ with suppress(OSError):
+ os.unlink(tmp_path)
+ raise
+
+ def _fsync_dir_nonfatal(self, file_path):
+ target_dir = os.path.dirname(os.path.abspath(file_path)) or "."
+ try:
+ dir_fd = os.open(target_dir, os.O_RDONLY)
+ except OSError:
+ return
+ try:
+ os.fsync(dir_fd)
+ except OSError:
+ return
+ finally:
+ with suppress(OSError):
+ os.close(dir_fd)
def _loadconfig(self, conf):
#Loads config file using dual cache
@@ -928,17 +1028,23 @@ class configfile:
data = json.load(f)
if self._validate_config(data):
# Re-write the YAML from good cache
- with open(conf, 'w') as f:
- yaml.dump(data, f, Dumper=NoAliasDumper, default_flow_style=False, sort_keys=False)
+ with suppress(OSError):
+ self._atomic_write_text_unlocked(
+ conf,
+ lambda fp: yaml.dump(
+ data,
+ fp,
+ Dumper=NoAliasDumper,
+ default_flow_style=False,
+ sort_keys=False,
+ ),
+ )
return data
# Both broken or no cache - create fresh
printer.error("Config file is corrupt and no valid cache exists. Creating default config.")
return self._createconfig(conf)
- try:
- with open(self.cachefile, 'w') as f:
- json.dump(data, f)
- except Exception:
- pass
+ with suppress(OSError, TypeError, ValueError):
+ self._atomic_write_text_unlocked(self.cachefile, lambda fp: json.dump(data, fp))
return data
else:
with open(self.cachefile, 'r') as f:
@@ -956,61 +1062,152 @@ class configfile:
def _createconfig(self, conf):
#Create config file (always writes defaults, safe for recovery)
- defaultconfig = {'config': {'case': False, 'idletime': 30, 'fzf': False}, 'connections': {}, 'profiles': { "default": { "host":"", "protocol":"ssh", "port":"", "user":"", "password":"", "options":"", "logs":"", "tags": "", "jumphost":""}}}
- with open(conf, "w") as f:
- yaml.dump(defaultconfig, f, Dumper=NoAliasDumper, default_flow_style=False, sort_keys=False)
- os.chmod(conf, 0o600)
- try:
- with open(self.cachefile, 'w') as f:
- json.dump(defaultconfig, f)
- except Exception:
- pass
+ defaultconfig = {'config': {'case': False, 'idletime': 30, 'fzf': False}, 'connections': {}, 'profiles': { "default": { "host":"", "protocol":"ssh", "port":"", "user":"", "password":"", "options":"", "logs":"", "tags": "", "jumphost":"", "fallback": ""}}}
+ self._atomic_write_text_unlocked(
+ conf,
+ lambda f: yaml.dump(defaultconfig, f, Dumper=NoAliasDumper, default_flow_style=False, sort_keys=False),
+ )
+ with suppress(OSError, TypeError, ValueError):
+ self._atomic_write_text_unlocked(self.cachefile, lambda f: json.dump(defaultconfig, f))
return defaultconfig
+ def _saveconfig_unlocked(self, conf, *, _raise_on_error=False):
+ # Save config file atomically inside _mutation_lock without invoking hooks or printer
+ newconfig = {
+ "config": self.config,
+ "connections": self.connections,
+ "profiles": self.profiles,
+ }
+ try:
+ self._atomic_write_text_unlocked(
+ conf,
+ lambda f: yaml.dump(
+ newconfig,
+ f,
+ Dumper=NoAliasDumper,
+ default_flow_style=False,
+ sort_keys=False,
+ ),
+ )
+ except (OSError, TypeError, yaml.YAMLError) as e:
+ if _raise_on_error:
+ raise
+ return 1, f"Failed to save config: {e}"
+
+ # YAML is safely committed on disk; fsync dir and sidecar caches are non-fatal
+ self._fsync_dir_nonfatal(conf)
+ try:
+ self._atomic_write_text_unlocked(self.cachefile, lambda f: json.dump(newconfig, f))
+ self._generate_nodes_cache_unlocked(_raise_on_error=True)
+ self._cache_dirty = False
+ except (OSError, TypeError, ValueError):
+ self._cache_dirty = True
+ return 0, None
+
+ def _run_saveconfig_post_hooks(self, conf, result):
+ """Run @MethodHook post-hooks registered for _saveconfig outside _mutation_lock."""
+ cls_desc = type(self).__dict__.get("_saveconfig")
+ cls_post = list(cls_desc.post_hooks) if isinstance(cls_desc, MethodHook) else []
+ inst_post = (
+ self.__dict__.get("_method_post_hooks", {}).get("_saveconfig", [])
+ if isinstance(self.__dict__.get("_method_post_hooks"), dict)
+ else []
+ )
+ all_post = cls_post + [h for h in inst_post if h not in cls_post]
+ for hook in all_post:
+ try:
+ result = hook(self, conf, result=result)
+ except Exception as e: # noqa: BLE001
+ hook_name = getattr(hook, "__name__", str(hook))
+ printer.error(f"_saveconfig Post-hook {hook_name} raised an exception: {e}")
+ return result
+
@MethodHook
def _saveconfig(self, conf):
#Save config file atomically to prevent corruption
- newconfig = {"config":{}, "connections": {}, "profiles": {}}
- newconfig["config"] = self.config
- newconfig["connections"] = self.connections
- newconfig["profiles"] = self.profiles
- tmpfile = conf + '.tmp'
- try:
- with open(tmpfile, "w") as f:
- yaml.dump(newconfig, f, Dumper=NoAliasDumper, default_flow_style=False, sort_keys=False)
- # Atomic replace: only overwrite original if write succeeded
- shutil.move(tmpfile, conf)
- with open(self.cachefile, "w") as f:
- json.dump(newconfig, f)
- self._generate_nodes_cache()
- except (IOError, OSError) as e:
- printer.error(f"Failed to save config: {e}")
- # Clean up temp file if it exists
- if os.path.exists(tmpfile):
- try:
- os.remove(tmpfile)
- except OSError:
- pass
- return 1
- return 0
+ with self._mutation_lock:
+ rc, err_msg = self._saveconfig_unlocked(conf, _raise_on_error=False)
+ if err_msg:
+ printer.error(err_msg)
+ return rc
- def _generate_nodes_cache(self, nodes=None, folders=None, profiles=None):
+ def transactional_update(self, mutator):
+ """Run mutator atomically inside _mutation_lock with in-place RAM rollback on error."""
+ save_desc = type(self).__dict__.get("_saveconfig")
+ mocked_save = "_saveconfig" in self.__dict__ or not isinstance(save_desc, MethodHook)
+ with self._mutation_lock:
+ snap_config = deepcopy(self.config)
+ snap_connections = deepcopy(self.connections)
+ snap_profiles = deepcopy(self.profiles)
+ try:
+ result = mutator(self)
+ self._validate_config_strict(
+ {
+ "config": self.config,
+ "connections": self.connections,
+ "profiles": self.profiles,
+ }
+ )
+ if mocked_save:
+ rc = self._saveconfig(self.file)
+ if rc != 0:
+ raise OSError("Failed to save config")
+ else:
+ self._saveconfig_unlocked(self.file, _raise_on_error=True)
+ except BaseException:
+ self.config.clear()
+ self.config.update(snap_config)
+ self.connections.clear()
+ self.connections.update(snap_connections)
+ self.profiles.clear()
+ self.profiles.update(snap_profiles)
+ raise
+ if not mocked_save:
+ self._run_saveconfig_post_hooks(self.file, 0)
+ return result
+
+ def refresh_cache(self):
+ """Rebuild JSON and fzf sidecar caches atomically; returns True on success."""
+ with self._mutation_lock:
+ snapshot = {
+ "config": self.config,
+ "connections": self.connections,
+ "profiles": self.profiles,
+ }
+ try:
+ self._atomic_write_text_unlocked(self.cachefile, lambda f: json.dump(snapshot, f))
+ self._generate_nodes_cache_unlocked(_raise_on_error=True)
+ self._cache_dirty = False
+ return True
+ except (OSError, TypeError, ValueError):
+ self._cache_dirty = True
+ return False
+
+ def _generate_nodes_cache_unlocked(self, nodes=None, folders=None, profiles=None, *, _raise_on_error=False):
try:
if nodes is None:
- nodes = self._getallnodes()
+ nodes = self._getallnodes_unlocked()
if folders is None:
- folders = self._getallfolders()
+ folders = self._getallfolders_unlocked()
if profiles is None:
profiles = list(self.profiles.keys())
-
- with open(self.fzf_cachefile, "w") as f:
- f.write("\n".join(nodes))
- with open(self.folders_cachefile, "w") as f:
- f.write("\n".join(folders))
- with open(self.profiles_cachefile, "w") as f:
- f.write("\n".join(profiles))
- except Exception:
- pass
+
+ self._atomic_write_text_unlocked(self.fzf_cachefile, lambda f: f.write("\n".join(nodes)))
+ self._atomic_write_text_unlocked(self.folders_cachefile, lambda f: f.write("\n".join(folders)))
+ self._atomic_write_text_unlocked(self.profiles_cachefile, lambda f: f.write("\n".join(profiles)))
+ except (OSError, TypeError, ValueError):
+ if _raise_on_error:
+ raise
+ self._cache_dirty = True
+
+ def _generate_nodes_cache(self, nodes=None, folders=None, profiles=None):
+ with self._mutation_lock:
+ self._generate_nodes_cache_unlocked(
+ nodes=nodes,
+ folders=folders,
+ profiles=profiles,
+ _raise_on_error=False,
+ )
def _createkey(self, keyfile):
@@ -1023,8 +1220,7 @@ class configfile:
os.chmod(keyfile, 0o600)
return key
- @MethodHook
- def _explode_unique(self, unique):
+ def _explode_unique_unlocked(self, unique):
#Divide unique name into folder, subfolder and id
uniques = unique.split("@")
if not unique.startswith("@"):
@@ -1045,30 +1241,193 @@ class configfile:
return result
@MethodHook
- def getitem(self, unique, keys = None, extract = False):
- '''
- Get an node or a group of nodes from configfile which can be passed to node/nodes class
+ def _explode_unique(self, unique):
+ return self._explode_unique_unlocked(unique)
- ### Parameters:
+ def _resolve_profile_attr_unlocked(self, prof_name, key, *, depth=0, visited=frozenset()):
+ if prof_name in visited or depth >= 5:
+ return None
+ prof = self.profiles.get(prof_name)
+ if not isinstance(prof, dict) or key not in prof:
+ return None
+ val = prof[key]
+ if key == "password":
+ if isinstance(val, str):
+ if is_compose_expression(val):
+ return val
+ if val.startswith("@") and val[1:] in self.profiles:
+ sub = self._resolve_profile_attr_unlocked(
+ val[1:],
+ key,
+ depth=depth + 1,
+ visited=visited | {prof_name},
+ )
+ return sub if sub is not None else val
+ return val
+ if isinstance(val, list):
+ resolved_list = []
+ for item in val:
+ if (
+ isinstance(item, str)
+ and not is_compose_expression(item)
+ and item.startswith("@")
+ and item[1:] in self.profiles
+ ):
+ sub = self._resolve_profile_attr_unlocked(
+ item[1:],
+ "password",
+ depth=depth + 1,
+ visited=visited | {prof_name},
+ )
+ if isinstance(sub, list):
+ resolved_list.extend(deepcopy(sub))
+ elif sub is not None:
+ resolved_list.append(deepcopy(sub))
+ else:
+ resolved_list.append(item)
+ else:
+ resolved_list.append(deepcopy(item))
+ return resolved_list
+ return deepcopy(val)
- - unique (str): Unique name of the node or folder in config using
- connection manager style: node[@subfolder][@folder]
- or [@subfolder]@folder
+ if isinstance(val, str) and val.startswith("@") and val[1:] in self.profiles:
+ sub = self._resolve_profile_attr_unlocked(
+ val[1:],
+ key,
+ depth=depth + 1,
+ visited=visited | {prof_name},
+ )
+ return sub if sub is not None else ""
+ return deepcopy(val)
- ### Optional Parameters:
+ def _find_inherited_profile_fallback_unlocked(self, raw_user, raw_pw):
+ """Inherit fallback from a primary @profile referenced by node user/password."""
+ candidates = []
+ for raw_val, field in ((raw_user, "user"), (raw_pw, "password")):
+ if isinstance(raw_val, (list, tuple)):
+ for item in raw_val:
+ candidates.append((item, field))
+ else:
+ candidates.append((raw_val, field))
- - keys (list): In case you pass a folder as unique, you can filter
- nodes inside the folder passing a list.
- - extract (bool): If True, extract information from profiles.
- Default False.
+ for candidate_val, field in candidates:
+ if (
+ isinstance(candidate_val, str)
+ and not is_compose_expression(candidate_val)
+ and candidate_val.startswith("@")
+ and "+" not in candidate_val
+ ):
+ prof_name = candidate_val[1:]
+ visited = set()
+ while prof_name and prof_name not in visited and len(visited) < MAX_PROFILE_DEPTH:
+ visited.add(prof_name)
+ prof = self.profiles.get(prof_name)
+ if not isinstance(prof, dict):
+ break
+ prof_fb = prof.get("fallback")
+ if prof_fb in (None, "", []) and isinstance(prof.get("tags"), dict):
+ prof_fb = prof["tags"].get("fallback")
+ if prof_fb not in (None, "", []):
+ return deepcopy(prof_fb)
+ next_val = prof.get(field)
+ if isinstance(next_val, (list, tuple)):
+ next_ref = next(
+ (
+ item
+ for item in next_val
+ if isinstance(item, str)
+ and not is_compose_expression(item)
+ and item.startswith("@")
+ and "+" not in item
+ ),
+ None,
+ )
+ prof_name = next_ref[1:] if next_ref else ""
+ elif (
+ isinstance(next_val, str)
+ and not is_compose_expression(next_val)
+ and next_val.startswith("@")
+ and "+" not in next_val
+ ):
+ prof_name = next_val[1:]
+ else:
+ break
+ return None
- ### Returns:
+ def _extract_node_fields_unlocked(self, node_dict):
+ """Resolve @profile references on a copied node dict, keeping legacy core.node(**cfg) contract."""
+ raw_user = node_dict.get("user")
+ raw_pw = node_dict.get("password")
+ raw_fb = node_dict.pop("fallback", None)
- dict: Dictionary containing information of node or multiple
- dictionaries of multiple nodes.
+ for key, value in list(node_dict.items()):
+ if key == "password":
+ if isinstance(value, str):
+ if is_compose_expression(value):
+ continue
+ if value.startswith("@") and value[1:] in self.profiles:
+ resolved = self._resolve_profile_attr_unlocked(value[1:], "password")
+ node_dict["password"] = resolved if resolved is not None else ""
+ elif isinstance(value, list):
+ resolved_pw = []
+ for item in value:
+ if (
+ isinstance(item, str)
+ and not is_compose_expression(item)
+ and item.startswith("@")
+ and item[1:] in self.profiles
+ ):
+ sub = self._resolve_profile_attr_unlocked(item[1:], "password")
+ if isinstance(sub, list):
+ resolved_pw.extend(deepcopy(sub))
+ elif sub is not None:
+ resolved_pw.append(deepcopy(sub))
+ else:
+ resolved_pw.append("")
+ else:
+ resolved_pw.append(deepcopy(item))
+ node_dict["password"] = resolved_pw
+ continue
- '''
- uniques = self._explode_unique(unique)
+ if isinstance(value, str) and value.startswith("@"):
+ ref_name = value[1:]
+ if ref_name in self.profiles:
+ res = self._resolve_profile_attr_unlocked(ref_name, key)
+ node_dict[key] = res if res is not None else ""
+ else:
+ node_dict[key] = ""
+ elif value == "" and key == "protocol":
+ default_proto = self.profiles.get("default", {}).get("protocol", "ssh")
+ node_dict[key] = default_proto if default_proto else "ssh"
+
+ tags_fb_present = (
+ isinstance(node_dict.get("tags"), dict)
+ and node_dict["tags"].get("fallback") not in (None, "", [])
+ )
+ if raw_fb in (None, "", []) and not tags_fb_present:
+ raw_fb = self._find_inherited_profile_fallback_unlocked(raw_user, raw_pw)
+
+ resolved_fb = []
+ if raw_fb not in (None, "", []):
+ resolved_fb = resolve_fallback_entries(raw_fb, self.profiles)
+
+ if resolved_fb:
+ tags_val = node_dict.get("tags", "")
+ if isinstance(tags_val, dict):
+ tags_copy = deepcopy(tags_val)
+ tags_copy["_fallback"] = resolved_fb
+ node_dict["tags"] = tags_copy
+ elif tags_val in ("", None):
+ node_dict["tags"] = {"_fallback": resolved_fb}
+ else:
+ node_dict["tags"] = {"_raw": tags_val, "_fallback": resolved_fb}
+ elif isinstance(node_dict.get("tags"), dict) and node_dict["tags"].get("fallback") not in (None, "", []):
+ tags_copy = deepcopy(node_dict["tags"])
+ tags_copy["_fallback"] = resolve_fallback_entries(tags_copy["fallback"], self.profiles)
+ node_dict["tags"] = tags_copy
+
+ def getitem_unlocked(self, unique, keys=None, extract=False):
+ uniques = self._explode_unique_unlocked(unique)
if unique.startswith("@"):
if uniques.keys() >= {"folder", "subfolder"}:
folder = self.connections[uniques["folder"]][uniques["subfolder"]]
@@ -1076,59 +1435,41 @@ class configfile:
folder = self.connections[uniques["folder"]]
newfolder = deepcopy(folder)
newfolder.pop("type")
- for node_name in folder.keys():
+ for node_name in list(folder.keys()):
if node_name == "type":
continue
- if "type" in newfolder[node_name].keys():
+ if "type" in newfolder[node_name]:
if newfolder[node_name]["type"] == "subfolder":
newfolder.pop(node_name)
else:
newfolder[node_name].pop("type")
-
- if keys != None:
- newfolder = dict((k, newfolder[k]) for k in keys)
-
+
+ if keys is not None:
+ newfolder = {k: newfolder[k] for k in keys}
+
if extract:
- for node_name, node_keys in newfolder.items():
- for key, value in node_keys.items():
- profile = re.search("^@(.*)", str(value))
- if profile:
- try:
- newfolder[node_name][key] = self.profiles[profile.group(1)][key]
- except KeyError:
- newfolder[node_name][key] = ""
- elif value == '' and key == "protocol":
- try:
- newfolder[node_name][key] = self.profiles["default"][key]
- except KeyError:
- newfolder[node_name][key] = "ssh"
-
- newfolder = {"{}{}".format(k,unique):v for k,v in newfolder.items()}
- return newfolder
+ for node_keys in newfolder.values():
+ self._extract_node_fields_unlocked(node_keys)
+
+ return {f"{k}{unique}": v for k, v in newfolder.items()}
+
+ if uniques.keys() >= {"folder", "subfolder"}:
+ node = self.connections[uniques["folder"]][uniques["subfolder"]][uniques["id"]]
+ elif "folder" in uniques:
+ node = self.connections[uniques["folder"]][uniques["id"]]
else:
- if uniques.keys() >= {"folder", "subfolder"}:
- node = self.connections[uniques["folder"]][uniques["subfolder"]][uniques["id"]]
- elif "folder" in uniques.keys():
- node = self.connections[uniques["folder"]][uniques["id"]]
- else:
- node = self.connections[uniques["id"]]
- newnode = deepcopy(node)
- newnode.pop("type")
-
- if extract:
- for key, value in newnode.items():
- profile = re.search("^@(.*)", str(value))
- if profile:
- try:
- newnode[key] = self.profiles[profile.group(1)][key]
- except KeyError:
- newnode[key] = ""
- elif value == '' and key == "protocol":
- try:
- newnode[key] = self.profiles["default"][key]
- except KeyError:
- newnode[key] = "ssh"
- return newnode
+ node = self.connections[uniques["id"]]
+ newnode = deepcopy(node)
+ newnode.pop("type")
+ if extract:
+ self._extract_node_fields_unlocked(newnode)
+ return newnode
+
+ @MethodHook
+ def getitem(self, unique, keys = None, extract = False):
+ """Get a node or folder of nodes from configfile."""
+ with self._mutation_lock:
+ return self.getitem_unlocked(unique, keys=keys, extract=extract)
@MethodHook
def getitems(self, uniques, extract = False):
@@ -1152,47 +1493,94 @@ class configfile:
dictionaries of multiple nodes.
'''
- nodes = {}
if isinstance(uniques, str):
uniques = [uniques]
- for i in uniques:
- if i.startswith("@"):
- if not self.config["case"]:
- i = i.lower()
- this = self.getitem(i, extract = extract)
- nodes.update(this)
- else:
- if not self.config["case"]:
- i = i.lower()
- this = self.getitem(i, extract = extract)
- nodes[i] = this
- return nodes
+ with self._mutation_lock:
+ nodes = {}
+ case_sensitive = self.config.get("case", False)
+ for item in uniques:
+ target = item if case_sensitive else item.lower()
+ if target.startswith("@"):
+ nodes.update(self.getitem_unlocked(target, extract=extract))
+ else:
+ nodes[target] = self.getitem_unlocked(target, extract=extract)
+ return nodes
+ def _connections_add_unlocked(
+ self,
+ *,
+ id,
+ host,
+ folder='',
+ subfolder='',
+ options='',
+ logs='',
+ password='',
+ port='',
+ protocol='',
+ user='',
+ tags='',
+ jumphost='',
+ fallback='',
+ type="connection",
+ ):
+ entry = {
+ "host": host,
+ "options": options,
+ "logs": logs,
+ "password": password,
+ "port": port,
+ "protocol": protocol,
+ "user": user,
+ "tags": tags,
+ "jumphost": jumphost,
+ "fallback": fallback,
+ "type": type,
+ }
+ if folder == '':
+ self.connections[id] = entry
+ elif subfolder == '':
+ self.connections[folder][id] = entry
+ else:
+ self.connections[folder][subfolder][id] = entry
@MethodHook
- def _connections_add(self,*, id, host, folder='', subfolder='', options='', logs='', password='', port='', protocol='', user='', tags='', jumphost='', type = "connection" ):
+ def _connections_add(self,*, id, host, folder='', subfolder='', options='', logs='', password='', port='', protocol='', user='', tags='', jumphost='', fallback='', type = "connection" ):
#Add connection from config
- if folder == '':
- self.connections[id] = {"host": host, "options": options, "logs": logs, "password": password, "port": port, "protocol": protocol, "user": user, "tags": tags,"jumphost": jumphost,"type": type}
- elif folder != '' and subfolder == '':
- self.connections[folder][id] = {"host": host, "options": options, "logs": logs, "password": password, "port": port, "protocol": protocol, "user": user, "tags": tags, "jumphost": jumphost, "type": type}
- elif folder != '' and subfolder != '':
- self.connections[folder][subfolder][id] = {"host": host, "options": options, "logs": logs, "password": password, "port": port, "protocol": protocol, "user": user, "tags": tags, "jumphost": jumphost, "type": type}
+ with self._mutation_lock:
+ self._connections_add_unlocked(
+ id=id,
+ host=host,
+ folder=folder,
+ subfolder=subfolder,
+ options=options,
+ logs=logs,
+ password=password,
+ port=port,
+ protocol=protocol,
+ user=user,
+ tags=tags,
+ jumphost=jumphost,
+ fallback=fallback,
+ type=type,
+ )
+ def _connections_del_unlocked(self, *, id, folder='', subfolder=''):
+ if folder == '':
+ del self.connections[id]
+ elif subfolder == '':
+ del self.connections[folder][id]
+ else:
+ del self.connections[folder][subfolder][id]
+
@MethodHook
def _connections_del(self,*, id, folder='', subfolder=''):
#Delete connection from config
- if folder == '':
- del self.connections[id]
- elif folder != '' and subfolder == '':
- del self.connections[folder][id]
- elif folder != '' and subfolder != '':
- del self.connections[folder][subfolder][id]
+ with self._mutation_lock:
+ self._connections_del_unlocked(id=id, folder=folder, subfolder=subfolder)
- @MethodHook
- def _folder_add(self,*, folder, subfolder = ''):
- #Add Folder from config
+ def _folder_add_unlocked(self, *, folder, subfolder=''):
if subfolder == '':
if folder not in self.connections:
self.connections[folder] = {"type": "folder"}
@@ -1201,125 +1589,197 @@ class configfile:
self.connections[folder][subfolder] = {"type": "subfolder"}
@MethodHook
- def _folder_del(self,*, folder, subfolder=''):
- #Delete folder from config
+ def _folder_add(self,*, folder, subfolder = ''):
+ #Add Folder from config
+ with self._mutation_lock:
+ self._folder_add_unlocked(folder=folder, subfolder=subfolder)
+
+ def _folder_del_unlocked(self, *, folder, subfolder=''):
if subfolder == '':
del self.connections[folder]
else:
del self.connections[folder][subfolder]
+ @MethodHook
+ def _folder_del(self,*, folder, subfolder=''):
+ #Delete folder from config
+ with self._mutation_lock:
+ self._folder_del_unlocked(folder=folder, subfolder=subfolder)
+
+ def _profiles_add_unlocked(self, *, id, host='', options='', logs='', password='', port='', protocol='', user='', tags='', jumphost='', fallback=''):
+ self.profiles[id] = {
+ "host": host,
+ "options": options,
+ "logs": logs,
+ "password": password,
+ "port": port,
+ "protocol": protocol,
+ "user": user,
+ "tags": tags,
+ "jumphost": jumphost,
+ "fallback": fallback,
+ }
@MethodHook
- def _profiles_add(self,*, id, host = '', options='', logs='', password='', port='', protocol='', user='', tags='', jumphost='' ):
+ def _profiles_add(self,*, id, host = '', options='', logs='', password='', port='', protocol='', user='', tags='', jumphost='', fallback=''):
#Add profile from config
- self.profiles[id] = {"host": host, "options": options, "logs": logs, "password": password, "port": port, "protocol": protocol, "user": user, "tags": tags, "jumphost": jumphost}
-
+ with self._mutation_lock:
+ self._profiles_add_unlocked(
+ id=id,
+ host=host,
+ options=options,
+ logs=logs,
+ password=password,
+ port=port,
+ protocol=protocol,
+ user=user,
+ tags=tags,
+ jumphost=jumphost,
+ fallback=fallback,
+ )
+
+ def _profiles_del_unlocked(self, *, id):
+ del self.profiles[id]
@MethodHook
def _profiles_del(self,*, id ):
#Delete profile from config
- del self.profiles[id]
-
- @MethodHook
- def _getallnodes(self, filter = None):
- #get all nodes on configfile
+ with self._mutation_lock:
+ self._profiles_del_unlocked(id=id)
+
+ @staticmethod
+ def _normalize_filter_arg(filter_arg):
+ if not filter_arg:
+ return []
+ if isinstance(filter_arg, str):
+ return [filter_arg]
+ if isinstance(filter_arg, list):
+ return [item for item in filter_arg if isinstance(item, str)]
+ return None
+
+ def _getallnodes_unlocked(self, filter=None):
nodes = []
- layer1 = [k for k,v in self.connections.items() if isinstance(v, dict) and v.get("type") == "connection"]
- folders = [k for k,v in self.connections.items() if isinstance(v, dict) and v.get("type") == "folder"]
+ layer1 = [k for k, v in self.connections.items() if isinstance(v, dict) and v.get("type") == "connection"]
+ folders = [k for k, v in self.connections.items() if isinstance(v, dict) and v.get("type") == "folder"]
nodes.extend(layer1)
for f in folders:
- layer2 = [k + "@" + f for k,v in self.connections[f].items() if isinstance(v, dict) and v.get("type") == "connection"]
+ layer2 = [k + "@" + f for k, v in self.connections[f].items() if isinstance(v, dict) and v.get("type") == "connection"]
nodes.extend(layer2)
- subfolders = [k for k,v in self.connections[f].items() if isinstance(v, dict) and v.get("type") == "subfolder"]
+ subfolders = [k for k, v in self.connections[f].items() if isinstance(v, dict) and v.get("type") == "subfolder"]
for s in subfolders:
- layer3 = [k + "@" + s + "@" + f for k,v in self.connections[f][s].items() if isinstance(v, dict) and v.get("type") == "connection"]
+ layer3 = [k + "@" + s + "@" + f for k, v in self.connections[f][s].items() if isinstance(v, dict) and v.get("type") == "connection"]
nodes.extend(layer3)
if filter:
- flat_filter = []
- if isinstance(filter, str):
- flat_filter = [filter]
- elif isinstance(filter, list):
- for item in filter:
- if isinstance(item, str):
- flat_filter.append(item)
- else:
- printer.error("Filter must be a string or a list of strings")
- sys.exit(1)
+ flat_filter = self._normalize_filter_arg(filter)
flags = re.IGNORECASE if not self.config.get("case", False) else 0
nodes = [item for item in nodes if any(re.search(pattern, item, flags) for pattern in flat_filter)]
return nodes
@MethodHook
- def _getallnodesfull(self, filter = None, extract = True):
- #get all nodes on configfile with all their attributes.
+ def _getallnodes(self, filter = None):
+ #get all nodes on configfile
+ if filter and self._normalize_filter_arg(filter) is None:
+ printer.error("Filter must be a string or a list of strings")
+ sys.exit(1)
+ with self._mutation_lock:
+ return self._getallnodes_unlocked(filter=filter)
+
+ def _getallnodesfull_unlocked(self, filter=None, extract=True):
nodes = {}
- layer1 = {k:v for k,v in self.connections.items() if isinstance(v, dict) and v.get("type") == "connection"}
- folders = [k for k,v in self.connections.items() if isinstance(v, dict) and v.get("type") == "folder"]
+ layer1 = {k: deepcopy(v) for k, v in self.connections.items() if isinstance(v, dict) and v.get("type") == "connection"}
+ folders = [k for k, v in self.connections.items() if isinstance(v, dict) and v.get("type") == "folder"]
nodes.update(layer1)
for f in folders:
- layer2 = {k + "@" + f:v for k,v in self.connections[f].items() if isinstance(v, dict) and v.get("type") == "connection"}
+ layer2 = {k + "@" + f: deepcopy(v) for k, v in self.connections[f].items() if isinstance(v, dict) and v.get("type") == "connection"}
nodes.update(layer2)
- subfolders = [k for k,v in self.connections[f].items() if isinstance(v, dict) and v.get("type") == "subfolder"]
+ subfolders = [k for k, v in self.connections[f].items() if isinstance(v, dict) and v.get("type") == "subfolder"]
for s in subfolders:
- layer3 = {k + "@" + s + "@" + f:v for k,v in self.connections[f][s].items() if isinstance(v, dict) and v.get("type") == "connection"}
+ layer3 = {k + "@" + s + "@" + f: deepcopy(v) for k, v in self.connections[f][s].items() if isinstance(v, dict) and v.get("type") == "connection"}
nodes.update(layer3)
if filter:
- flat_filter = []
- if isinstance(filter, str):
- flat_filter = [filter]
- elif isinstance(filter, list):
- for item in filter:
- if isinstance(item, str):
- flat_filter.append(item)
- else:
- printer.error("Filter must be a string or a list of strings")
- sys.exit(1)
+ flat_filter = self._normalize_filter_arg(filter)
flat_filter = ["^(?!.*@).+$" if item == "@" else item for item in flat_filter]
nodes = {k: v for k, v in nodes.items() if any(re.search(pattern, k) for pattern in flat_filter)}
if extract:
- for node, keys in nodes.items():
- for key, value in keys.items():
- profile = re.search("^@(.*)", str(value))
- if profile:
- try:
- nodes[node][key] = self.profiles[profile.group(1)][key]
- except KeyError:
- nodes[node][key] = ""
- elif value == '' and key == "protocol":
- try:
- nodes[node][key] = self.profiles["default"][key]
- except KeyError:
- nodes[node][key] = "ssh"
+ for node_dict in nodes.values():
+ self._extract_node_fields_unlocked(node_dict)
return nodes
-
@MethodHook
- def _getallfolders(self):
- #get all folders on configfile
- folders = ["@" + k for k,v in self.connections.items() if isinstance(v, dict) and v.get("type") == "folder"]
+ def _getallnodesfull(self, filter = None, extract = True):
+ #get all nodes on configfile with all their attributes.
+ if filter and self._normalize_filter_arg(filter) is None:
+ printer.error("Filter must be a string or a list of strings")
+ sys.exit(1)
+ with self._mutation_lock:
+ return self._getallnodesfull_unlocked(filter=filter, extract=extract)
+
+ def _getallfolders_unlocked(self):
+ folders = ["@" + k for k, v in self.connections.items() if isinstance(v, dict) and v.get("type") == "folder"]
subfolders = []
for f in folders:
- s = ["@" + k + f for k,v in self.connections[f[1:]].items() if isinstance(v, dict) and v.get("type") == "subfolder"]
+ s = ["@" + k + f for k, v in self.connections[f[1:]].items() if isinstance(v, dict) and v.get("type") == "subfolder"]
subfolders.extend(s)
folders.extend(subfolders)
return folders
@MethodHook
- def _profileused(self, profile):
- #Return all the nodes that uses this profile.
+ def _getallfolders(self):
+ #get all folders on configfile
+ with self._mutation_lock:
+ return self._getallfolders_unlocked()
+
+ def _profileused_unlocked(self, profile):
+ target_profiles = {profile}
+ changed = True
+ while changed:
+ changed = False
+ for pname, pval in self.profiles.items():
+ if (
+ pname not in target_profiles
+ and isinstance(pval, dict)
+ and (extract_profile_refs_from_value(pval) & target_profiles)
+ ):
+ target_profiles.add(pname)
+ changed = True
+
nodes = []
- layer1 = [k for k,v in self.connections.items() if isinstance(v, dict) and v.get("type") == "connection" and ("@" + profile in v.values() or ( isinstance(v.get("password"),list) and "@" + profile in v.get("password")))]
- folders = [k for k,v in self.connections.items() if isinstance(v, dict) and v.get("type") == "folder"]
+ layer1 = [
+ k
+ for k, v in self.connections.items()
+ if isinstance(v, dict)
+ and v.get("type") == "connection"
+ and bool(extract_profile_refs_from_value(v) & target_profiles)
+ ]
+ folders = [k for k, v in self.connections.items() if isinstance(v, dict) and v.get("type") == "folder"]
nodes.extend(layer1)
for f in folders:
- layer2 = [k + "@" + f for k,v in self.connections[f].items() if isinstance(v, dict) and v.get("type") == "connection" and ("@" + profile in v.values() or ( isinstance(v.get("password"),list) and "@" + profile in v.get("password")))]
+ layer2 = [
+ k + "@" + f
+ for k, v in self.connections[f].items()
+ if isinstance(v, dict)
+ and v.get("type") == "connection"
+ and bool(extract_profile_refs_from_value(v) & target_profiles)
+ ]
nodes.extend(layer2)
- subfolders = [k for k,v in self.connections[f].items() if isinstance(v, dict) and v.get("type") == "subfolder"]
+ subfolders = [k for k, v in self.connections[f].items() if isinstance(v, dict) and v.get("type") == "subfolder"]
for s in subfolders:
- layer3 = [k + "@" + s + "@" + f for k,v in self.connections[f][s].items() if isinstance(v, dict) and v.get("type") == "connection" and ("@" + profile in v.values() or ( isinstance(v.get("password"),list) and "@" + profile in v.get("password")))]
+ layer3 = [
+ k + "@" + s + "@" + f
+ for k, v in self.connections[f][s].items()
+ if isinstance(v, dict)
+ and v.get("type") == "connection"
+ and bool(extract_profile_refs_from_value(v) & target_profiles)
+ ]
nodes.extend(layer3)
return nodes
+ @MethodHook
+ def _profileused(self, profile):
+ #Return all the nodes that uses this profile.
+ with self._mutation_lock:
+ return self._profileused_unlocked(profile)
+
@MethodHook
def encrypt(self, password, keyfile=None):
'''
@@ -1514,29 +1974,22 @@ def encrypt(self, password, keyfile=None):
@MethodHook
def getitem(self, unique, keys = None, extract = False):
- '''
- Get an node or a group of nodes from configfile which can be passed to node/nodes class
-
- ### Parameters:
-
- - unique (str): Unique name of the node or folder in config using
- connection manager style: node[@subfolder][@folder]
- or [@subfolder]@folder
-
- ### Optional Parameters:
-
- - keys (list): In case you pass a folder as unique, you can filter
- nodes inside the folder passing a list.
- - extract (bool): If True, extract information from profiles.
- Default False.
-
- ### Returns:
-
- dict: Dictionary containing information of node or multiple
- dictionaries of multiple nodes.
-
- '''
- uniques = self._explode_unique(unique)
+ """Get a node or folder of nodes from configfile."""
+ with self._mutation_lock:
+ return self.getitem_unlocked(unique, keys=keys, extract=extract)
Get an node or a group of nodes from configfile which can be passed to node/nodes class
-
Parameters:
-
- unique (str): Unique name of the node or folder in config using
- connection manager style: node[@subfolder][@folder]
- or [@subfolder]@folder
-
-
Optional Parameters:
-
- keys (list): In case you pass a folder as unique, you can filter
- nodes inside the folder passing a list.
-- extract (bool): If True, extract information from profiles.
- Default False.
-
-
Returns:
-
dict: Dictionary containing information of node or multiple
- dictionaries of multiple nodes.
-
+
def getitems(self, uniques, extract=False)
@@ -1645,21 +2059,18 @@ def getitems(self, uniques, extract = False):
dictionaries of multiple nodes.
'''
- nodes = {}
if isinstance(uniques, str):
uniques = [uniques]
- for i in uniques:
- if i.startswith("@"):
- if not self.config["case"]:
- i = i.lower()
- this = self.getitem(i, extract = extract)
- nodes.update(this)
- else:
- if not self.config["case"]:
- i = i.lower()
- this = self.getitem(i, extract = extract)
- nodes[i] = this
- return nodes
+ with self._mutation_lock:
+ nodes = {}
+ case_sensitive = self.config.get("case", False)
+ for item in uniques:
+ target = item if case_sensitive else item.lower()
+ if target.startswith("@"):
+ nodes.update(self.getitem_unlocked(target, extract=extract))
+ else:
+ nodes[target] = self.getitem_unlocked(target, extract=extract)
+ return nodes
Get a group of nodes from configfile which can be passed to node/nodes class
@@ -4402,7 +6582,24 @@ class nodes:
Expand source code
@MethodHook
-def run(self, commands, vars = None,*, folder = None, prompt = None, stdout = None, parallel = 10, timeout = None, on_complete = None, logger = None):
+def run(
+ self,
+ commands,
+ vars=None,
+ *,
+ folder=None,
+ prompt=None,
+ stdout=None,
+ parallel=10,
+ timeout=None,
+ on_complete=None,
+ logger=None,
+ connect_timeout=None,
+ auth_timeout=None,
+ _default_cmd_timeout=10,
+ unattended=True,
+ attempt_tracker=None,
+):
'''
Run a command or list of commands on all the nodes in nodelist.
@@ -4458,17 +6655,26 @@ def run(self, commands, vars = None,*, folder = None, prompt = None, stdout = No
args = {}
nodesargs = {}
args["commands"] = commands
- if folder != None:
+ if folder is not None:
args["folder"] = folder
Path(folder).mkdir(parents=True, exist_ok=True)
- if prompt != None:
+ if prompt is not None:
args["prompt"] = prompt
- if stdout != None and on_complete is None:
+ if stdout is not None and on_complete is None:
args["stdout"] = stdout
- if timeout != None:
+ if timeout is not None:
args["timeout"] = timeout
+ if connect_timeout is not None:
+ args["connect_timeout"] = connect_timeout
+ if auth_timeout is not None:
+ args["auth_timeout"] = auth_timeout
+ if _default_cmd_timeout != 10:
+ args["_default_cmd_timeout"] = _default_cmd_timeout
+ if unattended is not True:
+ args["unattended"] = unattended
output = {}
status = {}
+ fallback_used = {}
tasks = []
def _run_node(node_obj, node_args, callback):
@@ -4479,9 +6685,11 @@ def run(self, commands, vars = None,*, folder = None, prompt = None, stdout = No
for n in self.nodelist:
nodesargs[n.unique] = deepcopy(args)
- if vars != None:
+ if attempt_tracker is not None:
+ nodesargs[n.unique]["attempt_tracker"] = attempt_tracker
+ if vars is not None:
nodesargs[n.unique]["vars"] = {}
- if "__global__" in vars.keys():
+ if "__global__" in vars:
nodesargs[n.unique]["vars"].update(vars["__global__"])
for var_key, var_val in vars.items():
if var_key == "__global__":
@@ -4511,8 +6719,10 @@ def run(self, commands, vars = None,*, folder = None, prompt = None, stdout = No
for i in self.nodelist:
output[i.unique] = i.output
status[i.unique] = i.status
+ fallback_used[i.unique] = bool(getattr(i, "last_fallback_used", False))
self.output = output
self.status = status
+ self.fallback_used = fallback_used
return output
Run a command or list of commands on all the nodes in nodelist.
@@ -4561,7 +6771,7 @@ def run(self, commands, vars = None,*, folder = None, prompt = None, stdout = No
@MethodHook
-def test(self, commands, expected, vars = None,*, folder = None, prompt = None, parallel = 10, timeout = None, on_complete = None, logger = None):
+def test(
+ self,
+ commands,
+ expected,
+ vars=None,
+ *,
+ folder=None,
+ prompt=None,
+ parallel=10,
+ timeout=None,
+ on_complete=None,
+ logger=None,
+ connect_timeout=None,
+ auth_timeout=None,
+ _default_cmd_timeout=10,
+ unattended=True,
+ attempt_tracker=None,
+):
'''
Run a command or list of commands on all the nodes in nodelist, then check if expected value appears on the output after the last command.
@@ -4624,16 +6851,25 @@ def test(self, commands, expected, vars = None,*, folder = None, prompt = None,
nodesargs = {}
args["commands"] = commands
args["expected"] = expected
- if folder != None:
+ if folder is not None:
args["folder"] = folder
Path(folder).mkdir(parents=True, exist_ok=True)
- if prompt != None:
+ if prompt is not None:
args["prompt"] = prompt
- if timeout != None:
+ if timeout is not None:
args["timeout"] = timeout
+ if connect_timeout is not None:
+ args["connect_timeout"] = connect_timeout
+ if auth_timeout is not None:
+ args["auth_timeout"] = auth_timeout
+ if _default_cmd_timeout != 10:
+ args["_default_cmd_timeout"] = _default_cmd_timeout
+ if unattended is not True:
+ args["unattended"] = unattended
output = {}
result = {}
status = {}
+ fallback_used = {}
tasks = []
def _test_node(node_obj, node_args, callback):
@@ -4644,9 +6880,11 @@ def test(self, commands, expected, vars = None,*, folder = None, prompt = None,
for n in self.nodelist:
nodesargs[n.unique] = deepcopy(args)
- if vars != None:
+ if attempt_tracker is not None:
+ nodesargs[n.unique]["attempt_tracker"] = attempt_tracker
+ if vars is not None:
nodesargs[n.unique]["vars"] = {}
- if "__global__" in vars.keys():
+ if "__global__" in vars:
nodesargs[n.unique]["vars"].update(vars["__global__"])
for var_key, var_val in vars.items():
if var_key == "__global__":
@@ -4674,9 +6912,11 @@ def test(self, commands, expected, vars = None,*, folder = None, prompt = None,
result[i.unique] = i.result
output[i.unique] = i.output
status[i.unique] = i.status
+ fallback_used[i.unique] = bool(getattr(i, "last_fallback_used", False))
self.output = output
self.result = result
self.status = status
+ self.fallback_used = fallback_used
return result
Run a command or list of commands on all the nodes in nodelist, then check if expected value appears on the output after the last command.