diff --git a/connpy/auth_utils.py b/connpy/auth_utils.py new file mode 100644 index 0000000..b0a22b4 --- /dev/null +++ b/connpy/auth_utils.py @@ -0,0 +1,1599 @@ +"""Pure authentication, composition, masking, sanitization, and rate-limit helpers.""" + +from __future__ import annotations + +import ast +import importlib +import importlib.util +import json +import math +import re +import shlex +import sys +import threading +import time +import traceback +from collections import OrderedDict +from collections.abc import Callable, Iterable +from contextlib import suppress +from copy import deepcopy +from dataclasses import dataclass +from pathlib import Path +from typing import Any, NoReturn + + +def _load_connpy_error() -> type[Exception]: + mod_name = "connpy.services.exceptions" + existing = sys.modules.get(mod_name) + if existing is not None and hasattr(existing, "ConnpyError"): + return existing.ConnpyError + if "connpy.services" not in sys.modules: + exc_path = Path(__file__).resolve().parent / "services" / "exceptions.py" + spec = importlib.util.spec_from_file_location(mod_name, exc_path) + if spec is not None and spec.loader is not None: + mod = importlib.util.module_from_spec(spec) + sys.modules[mod_name] = mod + spec.loader.exec_module(mod) + return mod.ConnpyError + mod = importlib.import_module(mod_name) + return mod.ConnpyError + + +ConnpyError = _load_connpy_error() + +MAX_FALLBACK_ENTRIES = 5 +MAX_PROFILE_DEPTH = 5 +MAX_COMPOSE_VARIANTS = 10 +MAX_COMPOSE_EXPR_LEN = 4096 +MAX_AUTH_STREAM_BYTES = 16384 +MAX_TRACKER_ENTRIES = 10000 +DEFAULT_MAX_AUTH_PROMPTS = 2 +DEFAULT_MAX_TOTAL_AUTH_ATTEMPTS = 6 +DEFAULT_MAX_HOP_HOST_ATTEMPTS = 6 +MAX_EOF_FALLBACK_ADVANCES = 2 +DEFAULT_TRACKER_TTL_SECONDS = 300 + +MIN_AUTH_PROMPTS = 1 +MAX_AUTH_PROMPTS_CAP = 5 +MIN_TOTAL_AUTH_ATTEMPTS = 1 +MAX_TOTAL_AUTH_ATTEMPTS_CAP = 10 +MIN_HOP_HOST_ATTEMPTS = 1 +MAX_HOP_HOST_ATTEMPTS_CAP = 10 +MAX_JSON_INPUT_BYTES = 8192 +MAX_JSON_INPUT_DEPTH = 4 + +SAFE_ERROR_TEMPLATES: dict[str, str] = { + "AUTH_FAILED": "[AUTH_FAILED] Authentication failed", + "AUTH_TIMEOUT": "[AUTH_TIMEOUT] Authentication timed out", + "JUMP_AUTH_FAILED": "[JUMP_AUTH_FAILED] Jumphost authentication failed", + "TRANSPORT_ERROR": "[TRANSPORT_ERROR] Transport connection failed", + "HOST_BUDGET_EXHAUSTED": "[HOST_BUDGET_EXHAUSTED] Host authentication attempt budget exhausted", + "USER_BUDGET_EXHAUSTED": "[USER_BUDGET_EXHAUSTED] User authentication attempt budget exhausted", + "EOF_BUDGET_EXHAUSTED": "[EOF_BUDGET_EXHAUSTED] EOF fallback advance budget exhausted", + "NO_CREDS": "[NO_CREDS] No usable authentication credentials available", + "SPAWN_ERROR": "[SPAWN_ERROR] Failed to spawn transport process", +} + +_CONTROL_CHAR_RE = re.compile(r"[\x00-\x1f\x7f]") +_ANSI_OSC_RE = re.compile(r"\x1b\][^\x07\x1b]*(?:\x07|\x1b\\)") +_ANSI_CSI_RE = re.compile(r"\x1b\[[0-9;?]*[ -/]*[@-~]|\x1b[@-Z\\-_]") +_PROFILE_REF_BODY_RE = re.compile(r"[A-Za-z0-9_.$:/-]+", re.ASCII) +_RSA_BLOB_RE = re.compile(r"""^b(["']).+\1$""", re.DOTALL) +_INSECURE_HOSTKEY_RE = re.compile( + r"(?:^|\s)(?:-o\s*)?(?:stricthostkeychecking\s*=\s*(?:no|off)|userknownhostsfile\s*=\s*/dev/null)(?=\s|$)", + re.IGNORECASE, +) + +_NON_SECRET_KEYS: frozenset[str] = frozenset({ + "tokens_per_min", + "max_tokens", + "token_count", + "ssh_key_path", + "keyfile", + "fallback_used", + "allow_fallback", + "allow_ambiguous_single_hop_prompt", + "disable_fallback_on_insecure_hostkey", + "max_auth_prompts", + "max_total_auth_attempts", + "max_hop_host_attempts", + "max_global_user_attempts", + "token_prefix", +}) + +_MASKED_SECRET_EXACT_KEYS: frozenset[str] = frozenset({ + "password", + "passwords", + "passphrase", + "passphrases", + "secret", + "secrets", + "token", + "tokens", + "api_token", + "auth_token", + "access_token", + "refresh_token", + "id_token", + "bearer_token", + "session_token", + "pat_token", + "api_key", + "apikey", + "client_secret", + "private_key", + "engineer_api_key", + "architect_api_key", + "engineer_auth", + "architect_auth", + "admin_password", + "tacacs_password", + "local_password", + "backup_password", + "enable_secret", + "enable_password", + "ssh_passphrase", + "user_password", + "jump_password", + "jumphost_password", + "credentials", +}) + +_MASKED_SECRET_SUFFIXES: tuple[str, ...] = ( + "_password", + "_passwords", + "_passphrase", + "_passphrases", + "_secret", + "_secrets", + "_token", + "_api_key", +) + + +class SanitizedAuthError(ConnpyError): + """Authentication error guaranteed free of secrets and chained tracebacks.""" + + __suppress_context__ = True + + def __init__(self, message: str = "", code: str = "AUTH_FAILED") -> None: + super().__init__(message) + self.code = code + self.__suppress_context__ = True + self.__cause__ = None + + @property + def __context__(self) -> None: + """Never retain or expose an underlying exception context.""" + return None + + @__context__.setter + def __context__(self, _value: BaseException | None) -> None: + return + + def __reduce__(self) -> tuple[type[SanitizedAuthError], tuple[str, str]]: + msg = str(self.args[0]) if self.args else "" + return (self.__class__, (msg, str(self.code))) + + +@dataclass(frozen=True) +class AuthCandidate: + """Immutable credential candidate entry for primary or backup authentication.""" + + user: str + password_spec: str | tuple[str, ...] + is_fallback: bool = False + label: str = "" + + +@dataclass(frozen=True) +class RuntimeAuthCandidate: + """Per-call resolved credential candidate with decrypted secret.""" + + user: str + password: str + has_password: bool + is_fallback: bool = False + label: str = "" + + +@dataclass +class _BudgetEntry: + """Mutable internal counter entry stored inside AuthAttemptTracker.""" + + count: int + limit: int + last_ts: float + + +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 + + +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 + + +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) + + +def sanitize_exception( + orig_exc: BaseException | None, + *, + code: str = "AUTH_FAILED", + secrets: Iterable[str] = (), + context_label: str = "", + clear_orig_frames: bool = True, +) -> SanitizedAuthError: + """Convert any exception into a SanitizedAuthError without leaking secrets or tracebacks.""" + resolved_code = code if code in SAFE_ERROR_TEMPLATES else "AUTH_FAILED" + try: + if isinstance(orig_exc, SanitizedAuthError): + existing_code = ( + orig_exc.code if orig_exc.code in SAFE_ERROR_TEMPLATES else resolved_code + ) + safe_existing = sanitize_log_message(str(orig_exc), secrets=secrets).strip() + msg = safe_existing or SAFE_ERROR_TEMPLATES[existing_code] + return SanitizedAuthError(msg, code=existing_code) + + msg = SAFE_ERROR_TEMPLATES[resolved_code] + if context_label: + safe_ctx = sanitize_log_message(context_label, secrets=secrets).strip() + if safe_ctx: + msg = f"{msg} ({safe_ctx})" + return SanitizedAuthError(msg, code=resolved_code) + finally: + if clear_orig_frames and orig_exc is not None: + clear_exception_frames(orig_exc) + + +def raise_sanitized_auth_error( + orig_exc: BaseException | None = None, + *, + code: str = "AUTH_FAILED", + secrets: Iterable[str] = (), + context_label: str = "", +) -> NoReturn: + """Clear original frames and raise SanitizedAuthError with cause and context suppressed.""" + err: SanitizedAuthError | None = None + try: + err = sanitize_exception( + orig_exc, + code=code, + secrets=secrets, + context_label=context_label, + clear_orig_frames=True, + ) + finally: + if orig_exc is not None: + clear_exception_frames(orig_exc) + del orig_exc + del secrets + err.__context__ = None + err.__cause__ = None + raise err from None + + +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 + + +def _check_depth(obj: Any, max_depth: int, cur_depth: int = 0) -> None: + """Verify container nesting depth does not exceed max_depth iteratively.""" + stack: list[tuple[Any, int]] = [(obj, cur_depth)] + while stack: + cur_obj, depth = stack.pop() + if isinstance(cur_obj, (dict, list, tuple)): + next_depth = depth + 1 + if next_depth > max_depth: + raise ValueError(f"JSON nesting exceeds maximum depth of {max_depth}") + if isinstance(cur_obj, dict): + for k, v in cur_obj.items(): + if not isinstance(k, str): + raise TypeError("JSON object keys must be strings") + stack.append((v, next_depth)) + else: + for item in cur_obj: + stack.append((item, next_depth)) + + +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 + + +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)) + + +def _tokenize_compose_body(body: str) -> list[tuple[str, str]]: + """Tokenize compose expression body in linear O(n) time following EBNF precedence.""" + tokens: list[tuple[str, str]] = [] + n = len(body) + i = 0 + expect_term = True + + while i < n: + if body[i].isspace(): + i += 1 + continue + + if not expect_term: + if body[i] != "+": + raise_sanitized_auth_error( + code="AUTH_FAILED", + context_label="invalid compose syntax: expected '+'", + ) + i += 1 + expect_term = True + continue + + # 1. rsa_blob (b'...' / b"...") evaluated before bare_lit + if body[i] == "b" and i + 1 < n and body[i + 1] in ("'", '"'): + quote = body[i + 1] + j = i + 2 + closed = False + while j < n: + if body[j] == "\\": + if j + 1 >= n: + raise_sanitized_auth_error( + code="AUTH_FAILED", + context_label="unterminated escape in rsa_blob", + ) + j += 2 + elif body[j] == quote: + j += 1 + closed = True + break + else: + j += 1 + if not closed: + raise_sanitized_auth_error( + code="AUTH_FAILED", + context_label="unterminated rsa_blob", + ) + if j < n and not body[j].isspace() and body[j] != "+": + raise_sanitized_auth_error( + code="AUTH_FAILED", + context_label="unexpected trailing characters after rsa_blob", + ) + blob_str = body[i:j] + try: + parsed_blob = ast.literal_eval(blob_str) + except Exception as exc: # noqa: BLE001 + raise_sanitized_auth_error( + exc, + code="AUTH_FAILED", + context_label="invalid rsa_blob literal", + ) + if not isinstance(parsed_blob, bytes): + raise_sanitized_auth_error( + code="AUTH_FAILED", + context_label="invalid rsa_blob type", + ) + tokens.append(("rsa_blob", blob_str)) + i = j + expect_term = False + continue + + # 2. escaped_lit (@@...) + if body.startswith("@@", i): + j = i + 2 + while j < n and not body[j].isspace() and body[j] != "+": + if body[j] in ("'", '"'): + raise_sanitized_auth_error( + code="AUTH_FAILED", + context_label="unexpected quote in escaped_lit", + ) + j += 1 + tokens.append(("escaped_lit", "@" + body[i + 2 : j])) + i = j + expect_term = False + continue + + # 3. profile_ref (@[A-Za-z0-9_.$:/-]+) + if body[i] == "@": + match = _PROFILE_REF_BODY_RE.match(body, i + 1) + if match is None: + raise_sanitized_auth_error( + code="AUTH_FAILED", + context_label="invalid profile reference in compose", + ) + end = match.end() + if end < n and not body[end].isspace() and body[end] != "+": + raise_sanitized_auth_error( + code="AUTH_FAILED", + context_label="invalid characters in profile reference", + ) + tokens.append(("profile_ref", match.group(0))) + i = end + expect_term = False + continue + + # 4. quoted_lit ('...' or "...") + if body[i] in ("'", '"'): + quote = body[i] + j = i + 1 + chars: list[str] = [] + closed = False + while j < n: + if body[j] == "\\": + if j + 1 >= n: + raise_sanitized_auth_error( + code="AUTH_FAILED", + context_label="unterminated escape in quoted_lit", + ) + nxt = body[j + 1] + if nxt in (quote, "\\"): + chars.append(nxt) + else: + chars.append("\\" + nxt) + j += 2 + elif body[j] == quote: + j += 1 + closed = True + break + else: + chars.append(body[j]) + j += 1 + if not closed: + raise_sanitized_auth_error( + code="AUTH_FAILED", + context_label="unterminated quoted literal in compose", + ) + if j < n and not body[j].isspace() and body[j] != "+": + raise_sanitized_auth_error( + code="AUTH_FAILED", + context_label="unexpected characters after quoted literal", + ) + tokens.append(("quoted_lit", "".join(chars))) + i = j + expect_term = False + continue + + # 5. bare_lit (without '+', whitespace, initial '@', or quotes) + if body[i] == "+": + raise_sanitized_auth_error( + code="AUTH_FAILED", + context_label="unexpected '+' operator in compose", + ) + j = i + while j < n and not body[j].isspace() and body[j] != "+": + if body[j] in ("'", '"'): + raise_sanitized_auth_error( + code="AUTH_FAILED", + context_label="unexpected quote in bare literal", + ) + j += 1 + tokens.append(("bare_lit", body[i:j])) + i = j + expect_term = False + + if expect_term or not tokens: + raise_sanitized_auth_error( + code="AUTH_FAILED", + context_label="empty or incomplete compose expression", + ) + return tokens + + +def _resolve_compose_internal( + expr: str, + *, + profiles: dict[str, Any] | None, + decrypt_fn: Callable[[str], str] | None, + depth: int, + visited: frozenset[str], + max_depth: int, + max_variants: int, +) -> list[str]: + 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", + ) + + tokens = _tokenize_compose_body(body) + prof_map = profiles if isinstance(profiles, dict) else {} + variants: list[str] = [""] + + for tok_type, tok_val in tokens: + if tok_type in ("escaped_lit", "quoted_lit", "bare_lit"): + term_vals = [tok_val] + elif tok_type == "rsa_blob": + if decrypt_fn is None: + raise_sanitized_auth_error( + code="NO_CREDS", + context_label="cannot decrypt compose rsa_blob without decrypt_fn", + ) + san_err: SanitizedAuthError | None = None + decrypted: Any = None + try: + decrypted = decrypt_fn(tok_val) + except Exception as exc: # noqa: BLE001 + san_err = sanitize_exception( + exc, + code="AUTH_FAILED", + context_label="failed to decrypt compose rsa_blob", + ) + if san_err is not None: + san_err.__context__ = None + raise san_err from None + term_vals = [str(decrypted)] + elif tok_type == "profile_ref": + if tok_val in visited: + raise_sanitized_auth_error( + code="AUTH_FAILED", + context_label="circular profile reference in compose", + ) + if depth + 1 > max_depth: + raise_sanitized_auth_error( + code="AUTH_FAILED", + context_label="compose profile recursion depth exceeded", + ) + prof_data = prof_map.get(tok_val) + if not isinstance(prof_data, dict): + raise_sanitized_auth_error( + code="NO_CREDS", + context_label="referenced compose profile not found", + ) + raw_pw = prof_data.get("password") + if raw_pw in (None, "", []): + raise_sanitized_auth_error( + code="NO_CREDS", + context_label="referenced compose profile has empty password", + ) + raw_items = list(raw_pw) if isinstance(raw_pw, (list, tuple)) else [raw_pw] + term_vals = [] + next_visited = visited | {tok_val} + for item in raw_items: + if not isinstance(item, str) or item == "": + raise_sanitized_auth_error( + code="NO_CREDS", + context_label="invalid password entry in compose profile", + ) + if item.startswith("compose:"): + sub_vals = _resolve_compose_internal( + item, + profiles=prof_map, + decrypt_fn=decrypt_fn, + depth=depth + 1, + visited=next_visited, + max_depth=max_depth, + max_variants=max_variants, + ) + term_vals.extend(sub_vals) + elif ( + item.startswith("@") + and _PROFILE_REF_BODY_RE.fullmatch(item[1:]) + and item[1:] in prof_map + ): + sub_vals = _resolve_compose_internal( + f"compose:@{item[1:]}", + profiles=prof_map, + decrypt_fn=decrypt_fn, + depth=depth + 1, + visited=next_visited, + max_depth=max_depth, + max_variants=max_variants, + ) + term_vals.extend(sub_vals) + elif is_rsa_blob(item): + if decrypt_fn is None: + raise_sanitized_auth_error( + code="NO_CREDS", + context_label="cannot decrypt profile password rsa_blob without decrypt_fn", + ) + san_item_err: SanitizedAuthError | None = None + dec_item: Any = None + try: + dec_item = decrypt_fn(item) + except Exception as exc: # noqa: BLE001 + san_item_err = sanitize_exception( + exc, + code="AUTH_FAILED", + context_label="failed to decrypt profile password in compose", + ) + if san_item_err is not None: + san_item_err.__context__ = None + raise san_item_err from None + term_vals.append(str(dec_item)) + else: + term_vals.append(item) + if len(term_vals) > max_variants: + raise_sanitized_auth_error( + code="AUTH_FAILED", + context_label="compose variants limit exceeded", + ) + else: + raise_sanitized_auth_error( + code="AUTH_FAILED", + context_label="unknown token in compose expression", + ) + + if len(variants) * len(term_vals) > max_variants: + raise_sanitized_auth_error( + code="AUTH_FAILED", + context_label="compose variants limit exceeded", + ) + variants = [prefix + suffix for prefix in variants for suffix in term_vals] + + return variants + + +def parse_compose_expression( + expr: str, + *, + profiles: dict[str, Any] | None = None, + decrypt_fn: Callable[[str], str] | None = None, + max_depth: int = MAX_PROFILE_DEPTH, + max_variants: int = MAX_COMPOSE_VARIANTS, +) -> list[str]: + """Parse and evaluate a compose: password expression into up to max_variants strings.""" + return _resolve_compose_internal( + expr, + profiles=profiles, + decrypt_fn=decrypt_fn, + depth=0, + visited=frozenset(), + max_depth=max_depth, + max_variants=max_variants, + ) + + +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) + + +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"} + + +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:") + + +def _mask_secret_key(key_lower: str) -> bool: + if key_lower in _NON_SECRET_KEYS: + return False + if key_lower in _MASKED_SECRET_EXACT_KEYS: + return True + return key_lower.endswith(_MASKED_SECRET_SUFFIXES) + + +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) + + +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 + + +def _is_profile_ref(value: Any) -> bool: + """Return True if value matches @profile reference syntax (not compose or literal).""" + return ( + isinstance(value, str) + and value.startswith("@") + and bool(_PROFILE_REF_BODY_RE.fullmatch(value[1:])) + ) + + +def _resolve_profile_field( + profiles: dict[str, Any], + prof_name: str, + field: str, + *, + depth: int = 0, + visited: frozenset[str] = frozenset(), +) -> Any: + if prof_name in visited or depth >= MAX_PROFILE_DEPTH: + raise ValueError(f"Circular or overly deep profile reference: {prof_name}") + if prof_name not in profiles or profiles[prof_name] is None: + raise ValueError(f"Referenced profile does not exist: {prof_name}") + prof = profiles[prof_name] + if not isinstance(prof, dict): + raise TypeError(f"Invalid profile definition for {prof_name!r}: expected dict") + val = prof.get(field, "") + next_visited = visited | {prof_name} + if _is_profile_ref(val): + return _resolve_profile_field( + profiles, + val[1:], + field, + depth=depth + 1, + visited=next_visited, + ) + if field == "password" and isinstance(val, (list, tuple)): + resolved_items: list[Any] = [] + for item in val: + if _is_profile_ref(item): + sub = _resolve_profile_field( + profiles, + item[1:], + field, + depth=depth + 1, + visited=next_visited, + ) + if isinstance(sub, list): + resolved_items.extend(deepcopy(sub)) + elif sub is not None: + resolved_items.append(deepcopy(sub)) + else: + resolved_items.append(deepcopy(item)) + return resolved_items + return deepcopy(val) + + +def _resolve_fallback_entries_internal( + raw_fallback: Any, + profiles: dict[str, Any] | None, + *, + max_entries: int = MAX_FALLBACK_ENTRIES, + _depth: int = 0, + _visited: frozenset[str] = frozenset(), +) -> list[dict[str, Any]]: + entries = validate_fallback_entries(raw_fallback, max_entries=max_entries) + if not entries: + return [] + prof_map = profiles if isinstance(profiles, dict) else {} + resolved: list[dict[str, Any]] = [] + + for entry in entries: + if isinstance(entry, str): + if entry.startswith("compose:"): + resolved.append({"user": "", "password": entry}) + elif _is_profile_ref(entry): + ref_name = entry[1:] + if ref_name in _visited or _depth >= MAX_PROFILE_DEPTH: + raise ValueError( + f"Circular or overly deep fallback profile reference: {ref_name}" + ) + if ref_name not in prof_map or prof_map[ref_name] is None: + raise ValueError(f"Referenced fallback profile does not exist: {ref_name}") + if not isinstance(prof_map[ref_name], dict): + raise TypeError( + f"Invalid fallback profile definition for {ref_name!r}: expected dict" + ) + + prof_obj = prof_map[ref_name] + u = _resolve_profile_field( + prof_map, ref_name, "user", depth=_depth, visited=_visited + ) + p = _resolve_profile_field( + prof_map, ref_name, "password", depth=_depth, visited=_visited + ) + own_user = str(u) if u not in (None, "") else "" + own_pw: Any = p if p not in (None, "", []) else "" + has_own = ( + bool(own_user) + or (isinstance(own_pw, list) and bool(own_pw)) + or (isinstance(own_pw, str) and own_pw != "") + ) + prof_fb = prof_obj.get("fallback") + if prof_fb not in (None, "", []): + if has_own: + if len(resolved) >= max_entries: + raise ValueError( + f"Fallback exceeds maximum of {max_entries} entries" + ) + resolved.append({"user": own_user, "password": own_pw}) + sub_entries = _resolve_fallback_entries_internal( + prof_fb, + prof_map, + max_entries=max_entries, + _depth=_depth + 1, + _visited=_visited | {ref_name}, + ) + if not has_own and not sub_entries: + raise ValueError( + f"Referenced fallback profile {ref_name!r} has no credentials" + ) + for sub in sub_entries: + if len(resolved) >= max_entries: + raise ValueError( + f"Fallback exceeds maximum of {max_entries} entries" + ) + resolved.append(sub) + else: + if not has_own: + raise ValueError( + f"Referenced fallback profile {ref_name!r} has no credentials" + ) + if len(resolved) >= max_entries: + raise ValueError( + f"Fallback exceeds maximum of {max_entries} entries" + ) + resolved.append({"user": own_user, "password": own_pw}) + else: + resolved.append({"user": "", "password": entry}) + elif isinstance(entry, dict): + user_raw = entry.get("user", "") + pw_raw = entry.get("password", "") + if _is_profile_ref(user_raw): + u_val = _resolve_profile_field( + prof_map, user_raw[1:], "user", depth=_depth, visited=_visited + ) + user_res = str(u_val) if u_val not in (None, "") else "" + else: + user_res = user_raw or "" + + if isinstance(pw_raw, (list, tuple)): + pw_res_list: list[str] = [] + for p_item in pw_raw: + if _is_profile_ref(p_item): + resolved_pw = _resolve_profile_field( + prof_map, p_item[1:], "password", depth=_depth, visited=_visited + ) + if isinstance(resolved_pw, list): + pw_res_list.extend(str(x) for x in resolved_pw if x != "") + elif resolved_pw not in (None, ""): + pw_res_list.append(str(resolved_pw)) + else: + pw_res_list.append(str(p_item)) + if not pw_res_list: + raise ValueError("Fallback entry password list resolved to empty") + pw_res: str | list[str] = pw_res_list + elif _is_profile_ref(pw_raw): + resolved_pw = _resolve_profile_field( + prof_map, pw_raw[1:], "password", depth=_depth, visited=_visited + ) + pw_res = resolved_pw if resolved_pw is not None else "" + else: + pw_res = pw_raw if pw_raw is not None else "" + + resolved.append({"user": user_res, "password": pw_res}) + + if len(resolved) > max_entries: + raise ValueError(f"Fallback exceeds maximum of {max_entries} entries") + + return resolved + + +def resolve_fallback_entries( + raw_fallback: Any, + profiles: dict[str, Any] | None, + *, + max_entries: int = MAX_FALLBACK_ENTRIES, +) -> list[dict[str, Any]]: + """Normalize fallback entries and resolve any @profile references safely.""" + return _resolve_fallback_entries_internal( + raw_fallback, + profiles, + max_entries=max_entries, + ) + + +def _extract_profile_refs_internal(value: Any, seen: set[int]) -> set[str]: + refs: set[str] = set() + if isinstance(value, str): + stripped = value.strip() + if stripped.startswith("compose:"): + refs.update(extract_compose_profile_refs(stripped)) + elif stripped.startswith("@") and _PROFILE_REF_BODY_RE.fullmatch(stripped[1:]): + refs.add(stripped[1:]) + elif ( + stripped.startswith(("[", "{")) + and len(stripped) <= MAX_JSON_INPUT_BYTES + and len(stripped.encode("utf-8", errors="replace")) <= MAX_JSON_INPUT_BYTES + ): + with suppress(ValueError, TypeError, RecursionError): + parsed = json.loads(stripped) + refs.update(_extract_profile_refs_internal(parsed, seen)) + elif isinstance(value, (list, tuple, dict)): + obj_id = id(value) + if obj_id in seen: + return refs + seen.add(obj_id) + try: + items = value.values() if isinstance(value, dict) else value + for item in items: + with suppress(RecursionError): + refs.update(_extract_profile_refs_internal(item, seen)) + finally: + seen.discard(obj_id) + return refs + + +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()) + + +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"]) + + +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") + + +def host_exact_re(host: str) -> re.Pattern[str]: + """Compile an exact host boundary regex with zero FQDN subdomain wildcard risk.""" + escaped = re.escape(str(host)) + return re.compile( + rf"(?$),])", + re.IGNORECASE | re.ASCII, + ) + + +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() + + +def _is_insecure_ssh_opt_spec(spec: str) -> bool: + norm = re.sub(r"\s+", " ", spec.strip()).lower() + if "=" in norm: + key, val = norm.split("=", 1) + else: + parts = norm.split(None, 1) + if len(parts) != 2: + return False + key, val = parts[0], parts[1] + key = key.strip().strip("\"'") + val = val.strip().strip("\"'") + return (key == "stricthostkeychecking" and val in ("no", "off")) or ( + key == "userknownhostsfile" and val == "/dev/null" + ) + + +def _has_insecure_ssh_options(raw_opts: str) -> bool: + if _INSECURE_HOSTKEY_RE.search(raw_opts): + return True + try: + tokens = shlex.split(raw_opts) + except ValueError: + tokens = raw_opts.split() + + n = len(tokens) + for i, tok in enumerate(tokens): + if tok.lower() == "-o": + if i + 1 < n and _is_insecure_ssh_opt_spec(tokens[i + 1]): + return True + if i + 2 < n and _is_insecure_ssh_opt_spec(f"{tokens[i + 1]} {tokens[i + 2]}"): + return True + continue + if tok[:2].lower() == "-o" and len(tok) > 2: + opt_body = tok[2:] + if _is_insecure_ssh_opt_spec(opt_body): + return True + if i + 1 < n and _is_insecure_ssh_opt_spec(f"{opt_body} {tokens[i + 1]}"): + return True + continue + if _is_insecure_ssh_opt_spec(tok): + return True + if i + 1 < n and _is_insecure_ssh_opt_spec(f"{tok} {tokens[i + 1]}"): + return True + return False + + +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) + + +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() diff --git a/connpy/cli/config_handler.py b/connpy/cli/config_handler.py index eeb0fea..5209a32 100644 --- a/connpy/cli/config_handler.py +++ b/connpy/cli/config_handler.py @@ -1,9 +1,22 @@ +import re import sys + import yaml + from .. import printer +from ..auth_utils import ( + MAX_AUTH_PROMPTS_CAP, + MAX_HOP_HOST_ATTEMPTS_CAP, + MAX_TOTAL_AUTH_ATTEMPTS_CAP, + MIN_AUTH_PROMPTS, + MIN_HOP_HOST_ATTEMPTS, + MIN_TOTAL_AUTH_ATTEMPTS, + validate_strict_int_range, +) from ..services.exceptions import ConnpyError, InvalidConfigurationError from .help_text import get_instructions + class ConfigHandler: def __init__(self, app): self.app = app @@ -29,7 +42,12 @@ class ConfigHandler: "sync_remote": self.set_sync_remote, "shell_command": self.set_shell_config, "shell_prompt": self.set_shell_config, - "shell_os": self.set_shell_config + "shell_os": self.set_shell_config, + "max_auth_prompts": self.set_auth_budget, + "max_total_auth_attempts": self.set_auth_budget, + "max_hop_host_attempts": self.set_auth_budget, + "max_global_user_attempts": self.set_auth_budget, + "disable_fallback_on_insecure_hostkey": self.set_disable_fallback_on_insecure_hostkey, } handler = actions.get(getattr(args, "command", None)) if handler: @@ -202,3 +220,67 @@ class ConfigHandler: except (ConnpyError, InvalidConfigurationError) as e: printer.error(str(e)) + def set_auth_budget(self, args): + key = args.command + raw_val = args.data[0] if isinstance(args.data, list) else args.data + bounds_map = { + "max_auth_prompts": (MIN_AUTH_PROMPTS, MAX_AUTH_PROMPTS_CAP), + "max_total_auth_attempts": (MIN_TOTAL_AUTH_ATTEMPTS, MAX_TOTAL_AUTH_ATTEMPTS_CAP), + "max_hop_host_attempts": (MIN_HOP_HOST_ATTEMPTS, MAX_HOP_HOST_ATTEMPTS_CAP), + "max_global_user_attempts": (1, 1000), + } + if key not in bounds_map: + raise InvalidConfigurationError(f"Unknown auth budget setting: {key}") + + if isinstance(raw_val, bool): + msg = f"Invalid {key}: boolean is not permitted" + printer.error(msg) + raise InvalidConfigurationError(msg) + + if isinstance(raw_val, int): + val = raw_val + elif isinstance(raw_val, str) and re.fullmatch(r"[+-]?\d+", raw_val.strip()): + val = int(raw_val.strip()) + else: + msg = f"Invalid {key}: expected integer, got {raw_val!r}" + printer.error(msg) + raise InvalidConfigurationError(msg) + + if val == 0: + cfg_obj = self.app.config + if not hasattr(cfg_obj, "assert_called") and callable( + getattr(cfg_obj, "transactional_update", None) + ): + cfg_obj.transactional_update(lambda c: c.config.pop(key, None)) + else: + cfg_obj.config.pop(key, None) + cfg_obj._saveconfig(cfg_obj.file) + printer.success("Config saved") + return + + min_v, max_v = bounds_map[key] + try: + validate_strict_int_range(val, key, min_v, max_v) + except ValueError as exc: + printer.error(str(exc)) + raise InvalidConfigurationError(str(exc)) from None + + self.app.services.config_svc.update_setting(key, val) + printer.success("Config saved") + + def set_disable_fallback_on_insecure_hostkey(self, args): + raw_val = args.data[0] if isinstance(args.data, list) else args.data + if isinstance(raw_val, bool): + val = raw_val + elif isinstance(raw_val, str) and raw_val.strip().lower() in ("true", "false"): + val = raw_val.strip().lower() == "true" + else: + msg = "Invalid disable_fallback_on_insecure_hostkey: expected 'true' or 'false'" + printer.error(msg) + raise InvalidConfigurationError(msg) + self.app.services.config_svc.update_setting( + "disable_fallback_on_insecure_hostkey", + val, + ) + printer.success("Config saved") + diff --git a/connpy/cli/forms.py b/connpy/cli/forms.py index 8c7e820..c553722 100644 --- a/connpy/cli/forms.py +++ b/connpy/cli/forms.py @@ -1,6 +1,70 @@ import ast +import json + +from ..auth_utils import ( + MAX_FALLBACK_ENTRIES, + MAX_JSON_INPUT_BYTES, + MAX_JSON_INPUT_DEPTH, + validate_auth_budget_tags, + validate_fallback_entries, + validate_json_depth_and_size, +) from .validators import Validators + +def _parse_tags_input(raw_tags): + if raw_tags in ("", None): + return "" + if isinstance(raw_tags, dict): + validate_json_depth_and_size( + raw_tags, + max_bytes=MAX_JSON_INPUT_BYTES, + max_depth=MAX_JSON_INPUT_DEPTH, + ) + validate_auth_budget_tags(raw_tags) + return raw_tags + s = str(raw_tags) + if s.startswith("@"): + return s + if len(s.encode("utf-8", errors="replace")) > MAX_JSON_INPUT_BYTES: + raise ValueError(f"Tags exceed maximum size of {MAX_JSON_INPUT_BYTES} bytes") + try: + parsed = json.loads(s) + except (ValueError, TypeError): + parsed = ast.literal_eval(s) + validate_json_depth_and_size( + parsed, + max_bytes=MAX_JSON_INPUT_BYTES, + max_depth=MAX_JSON_INPUT_DEPTH, + ) + validate_auth_budget_tags(parsed) + return parsed + + +def _parse_fallback_input(raw_fb): + if raw_fb in (None, "", []): + return "" + if isinstance(raw_fb, str): + stripped = raw_fb.strip() + if not stripped: + return "" + if stripped.startswith(("[", "{")): + parsed = validate_json_depth_and_size( + stripped, + max_bytes=MAX_JSON_INPUT_BYTES, + max_depth=MAX_JSON_INPUT_DEPTH, + ) + return validate_fallback_entries(parsed, max_entries=MAX_FALLBACK_ENTRIES) + validate_fallback_entries(stripped, max_entries=MAX_FALLBACK_ENTRIES) + return stripped + validate_json_depth_and_size( + raw_fb, + max_bytes=MAX_JSON_INPUT_BYTES, + max_depth=MAX_JSON_INPUT_DEPTH, + ) + return validate_fallback_entries(raw_fb, max_entries=MAX_FALLBACK_ENTRIES) + + class Forms: def __init__(self, app): self.app = app @@ -18,6 +82,7 @@ class Forms: questions.append(inquirer.Confirm("jumphost", message="Edit jumphost?")) questions.append(inquirer.Confirm("user", message="Edit User?")) questions.append(inquirer.Confirm("password", message="Edit password?")) + questions.append(inquirer.Confirm("fallback", message="Edit fallback credentials?")) return inquirer.prompt(questions) def questions_nodes(self, unique, uniques=None, edit=None): @@ -28,11 +93,13 @@ class Forms: defaults["tags"] = "" if "jumphost" not in defaults: defaults["jumphost"] = "" - except Exception: - defaults = {"host": "", "protocol": "", "port": "", "user": "", "options": "", "logs": "", "tags": "", "password": "", "jumphost": ""} + if "fallback" not in defaults: + defaults["fallback"] = "" + except Exception: # noqa: BLE001 + defaults = {"host": "", "protocol": "", "port": "", "user": "", "options": "", "logs": "", "tags": "", "password": "", "jumphost": "", "fallback": ""} node = {} if edit is None: - edit = {"host": True, "protocol": True, "port": True, "user": True, "password": True, "options": True, "logs": True, "tags": True, "jumphost": True} + edit = {"host": True, "protocol": True, "port": True, "user": True, "password": True, "options": True, "logs": True, "tags": True, "jumphost": True, "fallback": True} questions = [] if edit["host"]: questions.append(inquirer.Text("host", message="Add Hostname or IP", validate=self.validators.host_validation, default=defaults["host"])) @@ -70,11 +137,17 @@ class Forms: questions.append(inquirer.List("password", message="Password: Use a local password, no password or a list of profiles to reference?", choices=["Local Password", "Profiles", "No Password"])) else: node["password"] = defaults["password"] - + if edit.get("fallback"): + raw_def_fb = defaults.get("fallback", "") + def_fb_str = json.dumps(raw_def_fb) if isinstance(raw_def_fb, (list, dict)) else str(raw_def_fb or "") + questions.append(inquirer.Text("fallback", message="Add fallback credentials (JSON list/dict, @profile, or compose:)", validate=self.validators.fallback_validation, default=def_fb_str.replace("{", "{{").replace("}", "}}"))) + else: + node["fallback"] = defaults.get("fallback", "") + answer = inquirer.prompt(questions) if answer is None: return False - + if "password" in answer: if answer["password"] == "Local Password": passq = [inquirer.Password("password", message="Set Password")] @@ -90,10 +163,12 @@ class Forms: answer["password"] = passa["password"].split(",") elif answer["password"] == "No Password": answer["password"] = "" - - if "tags" in answer and not answer["tags"].startswith("@") and answer["tags"]: - answer["tags"] = ast.literal_eval(answer["tags"]) - + + if "tags" in answer and answer["tags"] and not str(answer["tags"]).startswith("@"): + answer["tags"] = _parse_tags_input(answer["tags"]) + if "fallback" in answer: + answer["fallback"] = _parse_fallback_input(answer["fallback"]) + result = {**uniques, **answer, **node} result["type"] = "connection" return result @@ -106,11 +181,13 @@ class Forms: defaults["tags"] = "" if "jumphost" not in defaults: defaults["jumphost"] = "" - except Exception: - defaults = {"host": "", "protocol": "", "port": "", "user": "", "options": "", "logs": "", "tags": "", "jumphost": ""} + if "fallback" not in defaults: + defaults["fallback"] = "" + except Exception: # noqa: BLE001 + defaults = {"host": "", "protocol": "", "port": "", "user": "", "options": "", "logs": "", "tags": "", "jumphost": "", "fallback": ""} profile = {} if edit is None: - edit = {"host": True, "protocol": True, "port": True, "user": True, "password": True, "options": True, "logs": True, "tags": True, "jumphost": True} + edit = {"host": True, "protocol": True, "port": True, "user": True, "password": True, "options": True, "logs": True, "tags": True, "jumphost": True, "fallback": True} questions = [] if edit["host"]: questions.append(inquirer.Text("host", message="Add Hostname or IP", default=defaults["host"])) @@ -148,18 +225,25 @@ class Forms: questions.append(inquirer.Password("password", message="Set Password")) else: profile["password"] = defaults["password"] - + if edit.get("fallback"): + raw_def_fb = defaults.get("fallback", "") + def_fb_str = json.dumps(raw_def_fb) if isinstance(raw_def_fb, (list, dict)) else str(raw_def_fb or "") + questions.append(inquirer.Text("fallback", message="Add fallback credentials (JSON list/dict, @profile, or compose:)", validate=self.validators.fallback_validation, default=def_fb_str.replace("{", "{{").replace("}", "}}"))) + else: + profile["fallback"] = defaults.get("fallback", "") + answer = inquirer.prompt(questions) if answer is None: return False - - if "password" in answer: - if answer["password"] != "": - answer["password"] = self.app.services.config_svc.encrypt_password(answer["password"]) - + + if "password" in answer and answer["password"] != "": + answer["password"] = self.app.services.config_svc.encrypt_password(answer["password"]) + if "tags" in answer and answer["tags"]: - answer["tags"] = ast.literal_eval(answer["tags"]) - + answer["tags"] = _parse_tags_input(answer["tags"]) + if "fallback" in answer: + answer["fallback"] = _parse_fallback_input(answer["fallback"]) + result = {**answer, **profile} result["id"] = unique return result @@ -178,11 +262,12 @@ class Forms: questions.append(inquirer.Text("jumphost", message="Add Jumphost node", validate=self.validators.jumphost_validation)) questions.append(inquirer.Text("user", message="Pick username", validate=self.validators.default_validation)) questions.append(inquirer.List("password", message="Password: Use a local password, no password or a list of profiles to reference?", choices=["Local Password", "Profiles", "No Password"])) - + questions.append(inquirer.Text("fallback", message="Add fallback credentials (JSON list/dict, @profile, or compose:)", validate=self.validators.fallback_validation)) + answer = inquirer.prompt(questions) if answer is None: return False - + if "password" in answer: if answer["password"] == "Local Password": passq = [inquirer.Password("password", message="Set Password")] @@ -194,11 +279,13 @@ class Forms: answer["password"] = passa["password"].split(",") elif answer["password"] == "No Password": answer["password"] = "" - + answer["type"] = "connection" - if "tags" in answer and not answer["tags"].startswith("@") and answer["tags"]: - answer["tags"] = ast.literal_eval(answer["tags"]) - + if "tags" in answer and answer["tags"] and not str(answer["tags"]).startswith("@"): + answer["tags"] = _parse_tags_input(answer["tags"]) + if "fallback" in answer: + answer["fallback"] = _parse_fallback_input(answer["fallback"]) + return answer def mcp_wizard(self, mcp_servers): diff --git a/connpy/cli/validators.py b/connpy/cli/validators.py index b9577af..15a03b7 100644 --- a/connpy/cli/validators.py +++ b/connpy/cli/validators.py @@ -1,5 +1,19 @@ -import re import ast +import json +import re + +from ..auth_utils import ( + MAX_FALLBACK_ENTRIES, + MAX_JSON_INPUT_BYTES, + MAX_JSON_INPUT_DEPTH, + SanitizedAuthError, + is_compose_expression, + validate_auth_budget_tags, + validate_compose_syntax, + validate_fallback_entries, + validate_json_depth_and_size, +) + def _raise_val_err(reason): import inquirer @@ -56,35 +70,80 @@ class Validators: return True 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: _raise_val_err("Profile {} don't exist".format(i)) return True + def _validate_tags_dict(self, current: str): + if len(current.encode("utf-8", errors="replace")) > MAX_JSON_INPUT_BYTES: + _raise_val_err(f"Tags exceed maximum size of {MAX_JSON_INPUT_BYTES} bytes") + parsed = None + try: + parsed = json.loads(current) + except (ValueError, TypeError): + try: + parsed = ast.literal_eval(current) + except (ValueError, TypeError, SyntaxError, MemoryError, RecursionError): + parsed = None + if not isinstance(parsed, dict): + _raise_val_err("Tags should be a python dictionary.") + try: + validate_json_depth_and_size( + parsed, + max_bytes=MAX_JSON_INPUT_BYTES, + max_depth=MAX_JSON_INPUT_DEPTH, + ) + validate_auth_budget_tags(parsed) + except (ValueError, TypeError, SanitizedAuthError) as exc: + _raise_val_err(str(exc)) + return True + def tags_validation(self, answers, current): if current.startswith("@"): 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 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 + + def fallback_validation(self, answers, current): + if current in (None, ""): + return True + stripped = str(current).strip() + if not stripped: + return True + if len(stripped.encode("utf-8", errors="replace")) > MAX_JSON_INPUT_BYTES: + _raise_val_err(f"Fallback exceeds maximum size of {MAX_JSON_INPUT_BYTES} bytes") + if stripped.startswith("@") and "+" not in stripped: + if stripped[1:] not in self.app.profiles: + _raise_val_err(f"Profile {stripped} don't exist") + return True + try: + if is_compose_expression(stripped): + validate_compose_syntax(stripped) + return True + if stripped.startswith(("[", "{")): + validate_json_depth_and_size( + stripped, + max_bytes=MAX_JSON_INPUT_BYTES, + max_depth=MAX_JSON_INPUT_DEPTH, + ) + validate_fallback_entries(stripped, max_entries=MAX_FALLBACK_ENTRIES) + except (ValueError, TypeError, SanitizedAuthError) as exc: + _raise_val_err(str(exc)) return True def jumphost_validation(self, answers, current): diff --git a/connpy/configfile.py b/connpy/configfile.py index fa9e605..24ba2bf 100755 --- a/connpy/configfile.py +++ b/connpy/configfile.py @@ -1,15 +1,31 @@ #!/usr/bin/env python3 -#Imports import json import os import re -import sys -import yaml import shutil -from pathlib import Path +import sys +import tempfile +import threading +from contextlib import suppress from copy import deepcopy -from .hooks import MethodHook, ClassHook +from pathlib import Path + +import yaml + from . import printer +from .auth_utils import ( + MAX_PROFILE_DEPTH, + SanitizedAuthError, + extract_profile_refs_from_value, + is_compose_expression, + resolve_fallback_entries, + validate_auth_budget_tags, + validate_compose_syntax, + validate_config_auth_settings, + validate_fallback_entries, +) +from .hooks import ClassHook, MethodHook + class NoAliasDumper(yaml.SafeDumper): def ignore_aliases(self, data): @@ -42,6 +58,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 ''' @@ -109,10 +127,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 @@ -207,12 +225,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 @@ -231,17 +336,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: @@ -259,61 +370,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): @@ -326,8 +528,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("@"): @@ -348,30 +549,172 @@ 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.""" + for candidate_val, field in ((raw_user, "user"), (raw_pw, "password")): + 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, 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 - - 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. + 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) - ### Returns: + 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 - dict: Dictionary containing information of node or multiple - dictionaries of multiple nodes. + 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" - ''' - uniques = self._explode_unique(unique) + 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"]] @@ -379,59 +722,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): @@ -455,47 +780,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"} @@ -504,125 +876,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): ''' diff --git a/connpy/connapp.py b/connpy/connapp.py index 98203b2..1096bfb 100755 --- a/connpy/connapp.py +++ b/connpy/connapp.py @@ -1,20 +1,20 @@ #!/usr/bin/env python3 -#Imports +import argparse import os import re -import argparse import sys -import yaml -import sys -from .core import node,nodes -from ._version import __version__ -from . import printer -from .api import start_api,stop_api,debug_api +import yaml +from rich_argparse import RichHelpFormatter + +from . import printer +from .api import debug_api, start_api, stop_api +from .cli.help_text import get_help +from .cli.helpers import folders_completer, nodes_completer +from .core import node, nodes from .plugins import Plugins from .services.exceptions import ConnpyError, ProfileNotFoundError, ReservedNameError -from rich_argparse import RichHelpFormatter # Bridge rich-argparse with our design system RichHelpFormatter.console = printer.console RichHelpFormatter.styles.update({ @@ -28,14 +28,6 @@ RichHelpFormatter.styles.update({ }) RichHelpFormatter.group_name_formatter = str.upper -from .cli import ( - NodeHandler, ProfileHandler, ConfigHandler, RunHandler, - AIHandler, APIHandler, PluginHandler, ImportExportHandler, - ContextHandler, SSOHandler -) -from .cli.helpers import nodes_completer, folders_completer, profiles_completer -from .cli.help_text import get_help - console = printer.console #functions and classes @@ -389,6 +381,11 @@ class connapp: configcrud.add_argument("--shell-command", dest="shell_command", nargs=1, action=self._store_type, help="Set default shell command", metavar="COMMAND") configcrud.add_argument("--shell-prompt", dest="shell_prompt", nargs=1, action=self._store_type, help="Set shell prompt regex for AI", metavar="REGEX") configcrud.add_argument("--shell-os", dest="shell_os", nargs=1, action=self._store_type, help="Set shell OS hint for AI", metavar="OS") + configcrud.add_argument("--max-auth-prompts", dest="max_auth_prompts", nargs=1, action=self._store_type, help="Set max auth prompts per connection (1-5, 0 to reset)", type=int, metavar="INT") + configcrud.add_argument("--max-total-auth-attempts", dest="max_total_auth_attempts", nargs=1, action=self._store_type, help="Set max total auth attempts per host (1-10, 0 to reset)", type=int, metavar="INT") + configcrud.add_argument("--max-hop-host-attempts", dest="max_hop_host_attempts", nargs=1, action=self._store_type, help="Set max auth attempts per jumphost (1-10, 0 to reset)", type=int, metavar="INT") + configcrud.add_argument("--max-global-user-attempts", dest="max_global_user_attempts", nargs=1, action=self._store_type, help="Set max global auth attempts per user (1-1000, 0 to reset)", type=int, metavar="INT") + configcrud.add_argument("--disable-fallback-on-insecure-hostkey", dest="disable_fallback_on_insecure_hostkey", nargs=1, action=self._store_type, help="Disable AAA fallback when StrictHostKeyChecking=no", choices=["true", "false"]) configparser.add_argument("--trusted-commands", dest="trusted_commands", nargs=1, action=self._store_type, help="Set custom trusted commands regexes (comma separated)", metavar="REGEX,REGEX") configparser.set_defaults(func=self._config.dispatch) diff --git a/connpy/core.py b/connpy/core.py index 3b716fe..1b4afd8 100755 --- a/connpy/core.py +++ b/connpy/core.py @@ -1,5 +1,4 @@ #!/usr/bin/env python3 -#Imports import ast import asyncio import datetime @@ -10,10 +9,11 @@ import logging import os import re import shlex +import shutil import sys import threading import time as _time_mod -from contextlib import contextmanager +from contextlib import contextmanager, suppress from copy import deepcopy from pathlib import Path from time import sleep, time @@ -21,6 +21,34 @@ from time import sleep, time import pexpect from . import printer +from .auth_utils import ( + DEFAULT_MAX_AUTH_PROMPTS, + DEFAULT_MAX_HOP_HOST_ATTEMPTS, + DEFAULT_MAX_TOTAL_AUTH_ATTEMPTS, + MAX_AUTH_PROMPTS_CAP, + MAX_AUTH_STREAM_BYTES, + MAX_COMPOSE_VARIANTS, + MAX_EOF_FALLBACK_ADVANCES, + MAX_HOP_HOST_ATTEMPTS_CAP, + MAX_PROFILE_DEPTH, + MAX_TOTAL_AUTH_ATTEMPTS_CAP, + MIN_AUTH_PROMPTS, + MIN_HOP_HOST_ATTEMPTS, + MIN_TOTAL_AUTH_ATTEMPTS, + AuthAttemptTracker, + AuthCandidate, + RuntimeAuthCandidate, + SanitizedAuthError, + has_insecure_hostkey_checking, + host_exact_re, + is_compose_expression, + normalize_prompt_line, + parse_compose_expression, + resolve_fallback_entries, + sanitize_exception, + sanitize_log_message, + validate_strict_int_range, +) from .hooks import ClassHook, MethodHook from .tunnels import LocalStream @@ -310,7 +338,23 @@ class node: ''' - def __init__(self, unique, host, options='', logs='', password='', port='', protocol='', user='', config='', tags='', jumphost='', allow_unsafe_options=None): + def __init__( + self, + unique, + host, + options='', + logs='', + password='', + port='', + protocol='', + user='', + config='', + tags='', + jumphost='', + allow_unsafe_options=None, + fallback=None, + disable_fallback_on_insecure_hostkey=None, + ): ''' ### Parameters: @@ -362,17 +406,18 @@ class node: else: self.allow_unsafe_options = False self.unique = unique + profiles_map = getattr(config, "profiles", {}) if (config != '' and config is not None) else {} attr = {"host": host, "logs": logs, "options":options, "port": port, "protocol": protocol, "user": user, "tags": tags, "jumphost": jumphost} for key, attr_val in attr.items(): profile = re.search("^@(.*)", str(attr_val)) if profile and config != '': try: - setattr(self, key, deepcopy(config.profiles[profile.group(1)][key])) + setattr(self, key, deepcopy(profiles_map[profile.group(1)][key])) except KeyError: setattr(self, key, "") elif attr_val == '' and key == "protocol": try: - setattr(self, key, config.profiles["default"][key]) + setattr(self, key, profiles_map["default"][key]) except (KeyError, AttributeError): setattr(self, key, "ssh") else: @@ -381,15 +426,104 @@ class node: if isinstance(password, list): self.password = [] for item in password: - profile = re.search("^@(.*)", item) - if profile and config != '': - self.password.append(config.profiles[profile.group(1)]["password"]) + if ( + isinstance(item, str) + and not is_compose_expression(item) + and item.startswith("@") + and config != '' + and item[1:] in profiles_map + ): + self.password.append(deepcopy(profiles_map[item[1:]].get("password", ""))) else: self.password.append(item) else: - self.password = [password] + if ( + isinstance(password, str) + and not is_compose_expression(password) + and password.startswith("@") + and config != '' + and password[1:] in profiles_map + ): + resolved_pw = profiles_map[password[1:]].get("password", "") + self.password = deepcopy(resolved_pw) if isinstance(resolved_pw, list) else [resolved_pw] + else: + self.password = [password] + + target_passwords_snapshot = list(self.password) + + raw_fallback = fallback + if raw_fallback in (None, "", []) and isinstance(self.tags, dict): + if "_fallback" in self.tags: + raw_fallback = self.tags.pop("_fallback", None) + elif "fallback" in self.tags: + raw_fallback = self.tags.get("fallback") + + if raw_fallback not in (None, "", []): + resolved_fb_entries = resolve_fallback_entries(raw_fallback, profiles_map) + else: + resolved_fb_entries = [] + + candidates: list[AuthCandidate] = [] + primary_specs = tuple( + p for p in target_passwords_snapshot if isinstance(p, str) and p != "" + ) + if primary_specs: + candidates.append( + AuthCandidate( + user=str(self.user or ""), + password_spec=primary_specs if len(primary_specs) > 1 else primary_specs[0], + is_fallback=False, + label="primary", + ) + ) + elif self.user: + candidates.append( + AuthCandidate( + user=str(self.user or ""), + password_spec="", + is_fallback=False, + label="primary", + ) + ) + + for fb_idx, entry in enumerate(resolved_fb_entries, start=1): + fb_user = str(entry.get("user") or self.user or "") + fb_pw = entry.get("password", "") + if isinstance(fb_pw, list): + pw_spec: str | tuple[str, ...] = tuple(str(x) for x in fb_pw if x != "") + else: + pw_spec = str(fb_pw or "") + candidates.append( + AuthCandidate( + user=fb_user, + password_spec=pw_spec, + is_fallback=True, + label=f"fallback#{fb_idx}", + ) + ) + self._auth_candidates: tuple[AuthCandidate, ...] = tuple(candidates) + + if disable_fallback_on_insecure_hostkey is not None: + self.disable_fallback_on_insecure_hostkey = bool(disable_fallback_on_insecure_hostkey) + elif isinstance(self.tags, dict) and "disable_fallback_on_insecure_hostkey" in self.tags: + self.disable_fallback_on_insecure_hostkey = bool( + self.tags["disable_fallback_on_insecure_hostkey"] + ) + elif ( + config != '' + and config is not None + and hasattr(config, "config") + and isinstance(config.config, dict) + ): + self.disable_fallback_on_insecure_hostkey = bool( + config.config.get("disable_fallback_on_insecure_hostkey", False) + ) + else: + self.disable_fallback_on_insecure_hostkey = False + self._jumphost_name = self.jumphost if (self.jumphost != "" and config != '') else "" self._hop_chain_meta = [] + self._jumphost_hops: tuple[dict, ...] = () if self.jumphost != "" and config != '': chain_res = self._build_jumphost_chain(self.jumphost, config, include_meta=True) if len(chain_res) == 3: @@ -397,6 +531,7 @@ class node: else: raw_cmd, jh_passwords = chain_res self._hop_chain_meta = [] + self._jumphost_hops = tuple(self._collect_jumphost_hops(self._jumphost_name, config)) if jh_passwords: self.password = jh_passwords + self.password if raw_cmd: @@ -404,38 +539,99 @@ class node: self.jumphost = f'-o ProxyCommand="{escaped}"' else: self.jumphost = "" - + + self.last_fallback_used = False self.output = "" self.status = 1 self.result = {} self.cmd_byte_positions = [(0, None)] + def _collect_jumphost_hops(self, jumphost_name, config, visited=None, depth=0): + """Collect structured hop metadata in SSH ProxyCommand prompt order.""" + if depth >= 5: + return [] + if visited is None: + visited = [] + if jumphost_name in visited: + return [] + visited = [*visited, jumphost_name] + try: + jh = config.getitem(jumphost_name) + except Exception: # noqa: BLE001 + return [] + jh = self._resolve_jumphost_data(deepcopy(jh), config) + hops = [] + if jh.get("jumphost", "") != "": + hops.extend( + self._collect_jumphost_hops( + jh["jumphost"], + config, + visited, + depth + 1, + ) + ) + jh_tags = jh.get("tags") + resolved_fqdn = "" + if isinstance(jh_tags, dict) and jh_tags.get("fqdn"): + resolved_fqdn = str(jh_tags["fqdn"]) + hops.append( + { + "name": jumphost_name, + "host": str(jh.get("host", "") or ""), + "port": str(jh.get("port", "") or ""), + "protocol": str(jh.get("protocol", "ssh") or "ssh"), + "user": str(jh.get("user", "") or ""), + "options": str(jh.get("options", "") or ""), + "tags": deepcopy(jh_tags) if isinstance(jh_tags, dict) else jh_tags, + "resolved_fqdn": resolved_fqdn, + "passwords": tuple(p for p in (jh.get("password") or []) if p != ""), + } + ) + return hops + @staticmethod def _resolve_jumphost_data(jh_dict, config): '''Resolve @profile references and normalize passwords in a jumphost dict.''' - for key, jh_val in jh_dict.items(): + profiles_map = getattr(config, "profiles", {}) if config else {} + for key, jh_val in list(jh_dict.items()): + if key == "password": + continue profile = re.search("^@(.*)", str(jh_val)) if profile: try: - jh_dict[key] = deepcopy(config.profiles[profile.group(1)][key]) + jh_dict[key] = deepcopy(profiles_map[profile.group(1)][key]) except KeyError: jh_dict[key] = "" elif jh_val == '' and key == "protocol": try: - jh_dict[key] = config.profiles["default"][key] + jh_dict[key] = profiles_map["default"][key] except KeyError: jh_dict[key] = "ssh" - if isinstance(jh_dict["password"], list): + raw_pw = jh_dict.get("password", "") + if isinstance(raw_pw, list): resolved = [] - for p in jh_dict["password"]: - profile = re.search("^@(.*)", p) - if profile: - resolved.append(config.profiles[profile.group(1)]["password"]) + for p in raw_pw: + if ( + isinstance(p, str) + and not is_compose_expression(p) + and p.startswith("@") + and p[1:] in profiles_map + ): + resolved.append(deepcopy(profiles_map[p[1:]].get("password", ""))) else: resolved.append(p) jh_dict["password"] = resolved else: - jh_dict["password"] = [jh_dict["password"]] + if ( + isinstance(raw_pw, str) + and not is_compose_expression(raw_pw) + and raw_pw.startswith("@") + and raw_pw[1:] in profiles_map + ): + res = profiles_map[raw_pw[1:]].get("password", "") + jh_dict["password"] = deepcopy(res) if isinstance(res, list) else [res] + else: + jh_dict["password"] = [raw_pw] return jh_dict def _is_unsafe_allowed(self) -> bool: @@ -1735,6 +1931,8 @@ class node: connect_timeout=None, auth_timeout=None, _default_cmd_timeout=10, + unattended=True, + attempt_tracker=None, ): ''' Run a command or list of commands on the node and return the output. @@ -1803,6 +2001,8 @@ class node: connect_timeout=connect_timeout, auth_timeout=auth_timeout, _default_cmd_timeout=_default_cmd_timeout, + unattended=unattended, + attempt_tracker=attempt_tracker, ) now = datetime.datetime.now().strftime('%Y-%m-%d_%H%M%S') if connect is True: @@ -1887,6 +2087,8 @@ class node: connect_timeout=None, auth_timeout=None, _default_cmd_timeout=10, + unattended=True, + attempt_tracker=None, ): ''' Run a command or list of commands on the node, then check if expected value appears on the output after the last command. @@ -1959,6 +2161,8 @@ class node: connect_timeout=connect_timeout, auth_timeout=auth_timeout, _default_cmd_timeout=_default_cmd_timeout, + unattended=unattended, + attempt_tracker=attempt_tracker, ) if connect is True: if logger: @@ -2100,6 +2304,7 @@ class node: allow_reverse_tunnel_port=None, *, connect_timeout=None, + user_override=None, ): self._validate_base_fields(check_port=True) is_from_mosh = ( @@ -2109,11 +2314,16 @@ class node: if not is_from_mosh: self._validate_ssh_options() proto = "ssh" if force_ssh else self.protocol + eff_user = self.user if user_override is None else user_override + if eff_user: + u_s = str(eff_user) + if u_s.startswith("-") or _LEGACY_SSH_UNSAFE_RE.search(u_s): + raise ValueError(f"Invalid characters in user: '{u_s}'") cmd = proto if is_from_mosh: host_bare, port_s, raw_opts = self._validate_mosh_ssh_fields( self.host, - self.user, + eff_user, self.port, self.tags, allow_reverse_tunnel_port=allow_reverse_tunnel_port, @@ -2162,20 +2372,21 @@ class node: jh_opt = self._effective_jumphost_opt(connect_timeout=connect_timeout) if jh_opt: cmd += " " + jh_opt - user_host = f"{self.user}@{target_h}" if self.user else target_h + user_host = f"{eff_user}@{target_h}" if eff_user else target_h cmd += f" {user_host}" return cmd @MethodHook - def _generate_mosh_cmd(self, *, connect_timeout=None): + def _generate_mosh_cmd(self, *, connect_timeout=None, user_override=None): + eff_user = self.user if user_override is None else user_override if str(self.host).startswith("-"): raise ValueError("Host cannot start with '-'") - if self.user and str(self.user).startswith("-"): + if eff_user and str(eff_user).startswith("-"): raise ValueError("User cannot start with '-'") if not self.host or re.search(r'[\s;|&$`><"\'\\]', str(self.host)): raise ValueError(f"Invalid characters in host: '{self.host}'") - if self.user and re.search(r'[\s;|&$`><"\'\\]', str(self.user)): - raise ValueError(f"Invalid characters in user: '{self.user}'") + if eff_user and re.search(r'[\s;|&$`><"\'\\]', str(eff_user)): + raise ValueError(f"Invalid characters in user: '{eff_user}'") cmd_parts = ["mosh"] if isinstance(self.tags, dict): @@ -2242,7 +2453,7 @@ class node: raise ValueError("Invalid options: unbalanced quotes") from exc cmd_parts.append(opts) - user_host = f"{self.user}@{self.host}" if self.user else self.host + user_host = f"{eff_user}@{self.host}" if eff_user else self.host cmd_parts.extend(["--", user_host]) return " ".join(cmd_parts) @@ -2316,15 +2527,26 @@ class node: @MethodHook - def _get_cmd(self, force_ssh=False, allow_reverse_tunnel_port=None, *, connect_timeout=None): + def _get_cmd( + self, + force_ssh=False, + allow_reverse_tunnel_port=None, + *, + connect_timeout=None, + user_override=None, + ): if force_ssh or self.protocol in ["ssh", "sftp"]: return self._generate_ssh_sftp_cmd( force_ssh=force_ssh, allow_reverse_tunnel_port=allow_reverse_tunnel_port, connect_timeout=connect_timeout, + user_override=user_override, ) elif self.protocol == "mosh": - return self._generate_mosh_cmd(connect_timeout=connect_timeout) + return self._generate_mosh_cmd( + connect_timeout=connect_timeout, + user_override=user_override, + ) elif self.protocol == "telnet": return self._generate_telnet_cmd() elif self.protocol == "kubectl": @@ -2339,7 +2561,7 @@ class node: printer.error(f"Invalid protocol: {self.protocol}") sys.exit(1) - def _spawn_child(self, args, protocol=None, encoding=None): + def _spawn_child(self, args, protocol=None, encoding=None, *, _secrets=()): if protocol is None: protocol = getattr(self, "protocol", "") env = None @@ -2358,8 +2580,248 @@ class node: spawn_kwargs["encoding"] = encoding try: return pexpect.spawn(args[0], args[1:], **spawn_kwargs), None - except pexpect.exceptions.ExceptionPexpect as e: - return None, f"Connection failed: {e}" + except Exception as exc: # noqa: BLE001 + sanitized = sanitize_exception( + exc, + code="SPAWN_ERROR", + secrets=_secrets, + context_label="Connection failed", + ) + return None, str(sanitized) + + def _extract_auth_budgets(self) -> tuple[int, int | None, int | None, int | None]: + cfg_dict = ( + self.config.config + if (self.config and hasattr(self.config, "config") and isinstance(self.config.config, dict)) + else {} + ) + tags_dict = self.tags if isinstance(self.tags, dict) else {} + + def _pick(key: str, default_val): + if key in tags_dict and tags_dict[key] is not None: + return tags_dict[key] + if key in cfg_dict and cfg_dict[key] is not None: + return cfg_dict[key] + return default_val + + max_auth_prompts = validate_strict_int_range( + _pick("max_auth_prompts", DEFAULT_MAX_AUTH_PROMPTS), + "max_auth_prompts", + MIN_AUTH_PROMPTS, + MAX_AUTH_PROMPTS_CAP, + ) + raw_total = _pick("max_total_auth_attempts", None) + max_total_auth_attempts = ( + None + if raw_total is None + else validate_strict_int_range( + raw_total, + "max_total_auth_attempts", + MIN_TOTAL_AUTH_ATTEMPTS, + MAX_TOTAL_AUTH_ATTEMPTS_CAP, + ) + ) + raw_hop = _pick("max_hop_host_attempts", None) + max_hop_host_attempts = ( + None + if raw_hop is None + else validate_strict_int_range( + raw_hop, + "max_hop_host_attempts", + MIN_HOP_HOST_ATTEMPTS, + MAX_HOP_HOST_ATTEMPTS_CAP, + ) + ) + raw_global_user = _pick("max_global_user_attempts", None) + max_global_user_attempts = ( + None + if raw_global_user is None + else validate_strict_int_range( + raw_global_user, + "max_global_user_attempts", + 1, + 1000, + ) + ) + return ( + max_auth_prompts, + max_total_auth_attempts, + max_hop_host_attempts, + max_global_user_attempts, + ) + + @staticmethod + def _emit_aaa_fallback_log( + logger, + *, + unique: str, + host: str, + protocol: str, + user: str, + label: str, + secrets: list[str], + ) -> None: + raw_msg = ( + f"[AAA-FALLBACK] Activating backup credentials ({label}) " + f"for node={unique} host={host} proto={protocol} user={user or ''}" + ) + safe_msg = sanitize_log_message(raw_msg, secrets=secrets) + if logger: + try: + logger("warning", safe_msg) + return + except Exception: # noqa: BLE001 + _LOGGER.debug("Fallback logger failed; falling back to stderr") + with suppress(Exception): + sys.stderr.write(safe_msg + "\n") + sys.stderr.flush() + + def _build_local_decryptor(self): + decryptor_cache = {} + + def _decrypt_one(cipher_text: str) -> str: + if not re.match(r'^b["\'].+["\']$', cipher_text, re.DOTALL): + return cipher_text + if "fn" not in decryptor_cache: + keyfile = getattr(self, "key", None) + if not keyfile: + raise SanitizedAuthError("No RSA key configured", code="AUTH_FAILED") + from Crypto.Cipher import PKCS1_OAEP + from Crypto.PublicKey import RSA + + with open(keyfile) as kf: + rsa_key = RSA.import_key(kf.read()) + decryptor_cache["fn"] = PKCS1_OAEP.new(rsa_key) + raw_bytes = ast.literal_eval(cipher_text) + return decryptor_cache["fn"].decrypt(raw_bytes).decode("utf-8") + + return _decrypt_one + + def _resolve_runtime_credentials( + self, + *, + allow_fallback: bool, + unattended: bool, + ) -> tuple[list[RuntimeAuthCandidate], list[list[str]], list[str]]: + profiles_map = ( + getattr(self.config, "profiles", {}) + if (getattr(self, "config", "") != "" and self.config is not None) + else {} + ) + local_decrypt = self._build_local_decryptor() + passtx_desc = type(self).__dict__.get("_passtx") + passtx_mocked = "_passtx" in self.__dict__ or not isinstance(passtx_desc, MethodHook) + + def _decrypt_plain_list(specs: list[str]) -> list[str]: + if not specs: + return [] + if passtx_mocked or not unattended: + return list(self._passtx(specs)) + return [local_decrypt(s) for s in specs] + + def _expand_spec(spec: str | tuple[str, ...]) -> list[str]: + items = list(spec) if isinstance(spec, (tuple, list)) else ([spec] if spec != "" else []) + out: list[str] = [] + for item in items: + if not isinstance(item, str) or item == "": + continue + if is_compose_expression(item): + variants = parse_compose_expression( + item, + profiles=profiles_map, + decrypt_fn=local_decrypt, + max_depth=MAX_PROFILE_DEPTH, + max_variants=MAX_COMPOSE_VARIANTS, + ) + out.extend(variants) + else: + out.extend(_decrypt_plain_list([item])) + return out + + candidates_src = list(getattr(self, "_auth_candidates", ())) + if not candidates_src: + # Compatibility if a subclass or test mutated self.password directly + raw_pw_list = self.password if isinstance(self.password, list) else [self.password] + non_empty = tuple(p for p in raw_pw_list if isinstance(p, str) and p != "") + if non_empty or self.user: + candidates_src = [ + AuthCandidate( + user=str(self.user or ""), + password_spec=non_empty if len(non_empty) > 1 else (non_empty[0] if non_empty else ""), + is_fallback=False, + label="primary", + ) + ] + + runtime_candidates: list[RuntimeAuthCandidate] = [] + raw_secrets: list[str] = [] + + for cand in candidates_src: + if cand.is_fallback and not allow_fallback: + continue + if isinstance(cand.password_spec, tuple): + raw_secrets.extend(s for s in cand.password_spec if isinstance(s, str) and s) + elif isinstance(cand.password_spec, str) and cand.password_spec: + raw_secrets.append(cand.password_spec) + + expanded_pws = _expand_spec(cand.password_spec) + raw_secrets.extend(p for p in expanded_pws if isinstance(p, str) and p) + if expanded_pws: + for pw_val in expanded_pws: + runtime_candidates.append( + RuntimeAuthCandidate( + user=cand.user, + password=pw_val, + has_password=True, + is_fallback=cand.is_fallback, + label=cand.label, + ) + ) + else: + runtime_candidates.append( + RuntimeAuthCandidate( + user=cand.user, + password="", + has_password=False, + is_fallback=cand.is_fallback, + label=cand.label, + ) + ) + + hop_runtime_passwords: list[list[str]] = [] + for hop in getattr(self, "_jumphost_hops", ()): + h_pws = _expand_spec(hop.get("passwords", ())) + raw_secrets.extend(p for p in h_pws if isinstance(p, str) and p) + hop_runtime_passwords.append(h_pws) + + return runtime_candidates, hop_runtime_passwords, raw_secrets + + def _disambiguate_prompt(self, line: str) -> tuple[str, int | None]: + """Classify password prompt line strictly using exact host boundaries.""" + tags_dict = self.tags if isinstance(self.tags, dict) else {} + target_names = [str(self.host)] + if tags_dict.get("fqdn"): + target_names.append(str(tags_dict["fqdn"])) + matched_target = any( + host_exact_re(h).search(line) for h in target_names if h + ) + + matched_hops: list[int] = [] + for idx, hop in enumerate(getattr(self, "_jumphost_hops", ())): + h_names = [hop.get("host", "")] + if hop.get("resolved_fqdn"): + h_names.append(str(hop["resolved_fqdn"])) + if any(host_exact_re(h).search(line) for h in h_names if h): + matched_hops.append(idx) + + total_matches = len(matched_hops) + (1 if matched_target else 0) + if total_matches > 1: + return "conflict", None + if matched_hops: + return "hop", matched_hops[0] + if matched_target: + return "target", None + return "ambiguous", None @MethodHook def _connect( @@ -2376,13 +2838,13 @@ class node: _default_cmd_timeout=10, _default_max_total_seconds=None, clock=_time_mod.monotonic, + unattended=False, + attempt_tracker=None, ): + self.last_fallback_used = False effective_protocol = "ssh" if force_ssh else self.protocol - if effective_protocol == "mosh": - import shutil - - if shutil.which("mosh") is None: - return "Connection failed: mosh binary not found in PATH" + if effective_protocol == "mosh" and shutil.which("mosh") is None: + return "Connection failed: mosh binary not found in PATH" hop_meta = getattr(self, "_hop_chain_meta", None) or [] try: @@ -2405,6 +2867,21 @@ class node: except ValueError as e: return f"Connection failed code: 12\nInvalid timeout configuration: {e}" + try: + ( + max_auth_prompts, + max_total_auth_attempts, + max_hop_host_attempts, + max_global_user_attempts, + ) = self._extract_auth_budgets() + except ValueError as exc: + san = sanitize_exception( + exc, + code="AUTH_FAILED", + context_label="invalid auth budget config", + ) + return str(san) + if effective_protocol == "local": cmd = self._get_cmd() args = shlex.split(cmd) @@ -2419,25 +2896,51 @@ class node: self.logfile = self._logfile() return True - cmd = self._get_cmd( - force_ssh=force_ssh, - allow_reverse_tunnel_port=allow_reverse_tunnel_port, - connect_timeout=eff_ct, + cfg_dict = ( + self.config.config + if (self.config and hasattr(self.config, "config") and isinstance(self.config.config, dict)) + else {} ) - passwords = self._passtx(self.password) if self.password and any(self.password) else [] - raw_secrets = [ - s - for s in ( - list(passwords) - + (list(self.password) if isinstance(self.password, list) else []) + tags_dict = self.tags if isinstance(self.tags, dict) else {} + allow_fallback = not (tags_dict.get("allow_fallback") is False) + disable_insecure_hk = bool( + getattr(self, "disable_fallback_on_insecure_hostkey", False) + or tags_dict.get("disable_fallback_on_insecure_hostkey") + or cfg_dict.get("disable_fallback_on_insecure_hostkey") + ) + if ( + allow_fallback + and disable_insecure_hk + and has_insecure_hostkey_checking( + self.options, + self.tags, + getattr(self, "_jumphost_hops", ()), ) - if isinstance(s, str) and s - ] + ): + allow_fallback = False + + raw_secrets: list[str] = [] + try: + runtime_candidates, hop_runtime_passwords, raw_secrets = self._resolve_runtime_credentials( + allow_fallback=allow_fallback, + unattended=unattended, + ) + except Exception as exc: # noqa: BLE001 + san = sanitize_exception( + exc, + code="AUTH_FAILED", + secrets=raw_secrets, + context_label="credential resolution failed", + ) + return str(san) + if self.logs != '': self.logfile = self._logfile() default_prompt = r'>$|#$|\$$|>.$|#.$|\$.$' - prompt = self.tags.get("prompt", default_prompt) if isinstance(self.tags, dict) else default_prompt - password_prompt = r'[pP]assword:|[uU]sername:' if effective_protocol != 'telnet' else r'[pP]assword:' + prompt = tags_dict.get("prompt", default_prompt) if tags_dict else default_prompt + password_prompt = ( + r'[pP]assword:|[uU]sername:' if effective_protocol != 'telnet' else r'[pP]assword:' + ) prompt_unanchored = "|".join(t.removesuffix("$") for t in prompt.split("|")) mosh_prompt = r"(?:" + prompt_unanchored + r")\s*(?:\x1b\[[0-9;?]*[a-zA-Z])*$" @@ -2548,26 +3051,26 @@ class node: ], } expects["mosh"] = [ - 'yes/no', # 0 (initial) - r'(?:ssh: connect to host .* port .*: Connection refused|Connection refused)', # 1 (error) - r'(?:ssh|mosh): .*supported', # 2 (error) - r'(?:(?:ssh|mosh): (?:Invalid|usage)|Unknown option:)', # 3 (error) - 'ssh-keygen.*"', # 4 (error) - r'(?:ssh: connect to host .* port .*: Connection timed out|mosh: .* timed out)', # 5 (error) - r'(?:ssh|mosh): .*unavailable', # 6 (error) - r'(?:Connection to .* closed|Connection closed by remote host)', # 7 (error) - password_prompt, # 8 (eof) - mosh_prompt_pat, # 9 (eof) - 'suspend', # 10 (eof) - pexpect.EOF, # 11 (eof) - pexpect.TIMEOUT, # 12 (error) - "No route to host", # 13 (error) - "resolve hostname", # 14 (error) - r'(?:ssh|mosh): .*no matching', # 15 (error) - r"[bB]ad (owner|permissions)", # 16 (error) - "mosh-server:.*not found|command not found", # 17 (error) - r"(?:Did not find mosh server startup message|(?:mosh: )?Nothing received from server on UDP port \d+\.?|Nothing received from server)", # 18 (error) - "needs a UTF-8 native locale to run|requires a UTF-8 locale|locale.*not available", # 19 (error) + 'yes/no', + r'(?:ssh: connect to host .* port .*: Connection refused|Connection refused)', + r'(?:(?:ssh|mosh): .*supported|Permission denied \([^)\r\n]{1,256}\)\.)', + r'(?:(?:ssh|mosh): (?:Invalid|usage)|Unknown option:)', + 'ssh-keygen.*"', + r'(?:ssh: connect to host .* port .*: Connection timed out|mosh: .* timed out)', + r'(?:ssh|mosh): .*unavailable', + r'(?:Connection to .* closed|Connection closed by remote host)', + password_prompt, + mosh_prompt_pat, + 'suspend', + pexpect.EOF, + pexpect.TIMEOUT, + "No route to host", + "resolve hostname", + r'(?:ssh|mosh): .*no matching', + r"[bB]ad (owner|permissions)", + "mosh-server:.*not found|command not found", + r"(?:Did not find mosh server startup message|(?:mosh: )?Nothing received from server on UDP port \d+\.?|Nothing received from server)", + "needs a UTF-8 native locale to run|requires a UTF-8 locale|locale.*not available", ] compiled_expects = compile_expects(expects) @@ -2616,13 +3119,214 @@ class node: return val.decode("utf-8", errors="replace") return str(val) + tracker = ( + attempt_tracker + if attempt_tracker is not None + else AuthAttemptTracker( + default_max_host_attempts=DEFAULT_MAX_TOTAL_AUTH_ATTEMPTS, + default_max_hop_attempts=DEFAULT_MAX_HOP_HOST_ATTEMPTS, + clock=clock, + ) + ) deadline = clock() + max_total budget_warned = False proto_expects = compiled_expects[effective_protocol] pw_prompt_idx = 5 if effective_protocol == "kubectl" else 8 suspend_idx = 10 if effective_protocol in ("ssh", "sftp", "telnet", "mosh") else None + stream_limit = MAX_AUTH_STREAM_BYTES if unattended else MAX_PRE_AUTH_BUFFER_BYTES + jumphost_hops = getattr(self, "_jumphost_hops", ()) - for attempts in range(1, max_attempts + 1): + st: dict[str, object] = { + "cand_idx": 0, + "connect_retries": 0, + "eof_fallback_advances": 0, + "target_creds_sent": False, + "cur_cand": None, + "cur_user": "", + "handshake_progressed": False, + "prompts_this_conn": 0, + "cand_sent_pw_this_conn": False, + "logged_fallback_this_cand": False, + "stage": "connect", + "last_observed_bytes_len": 0, + } + + def _handle_username_prompt(child_obj) -> tuple[bool, str | None]: + if not unattended: + if self.user: + child_obj.sendline(self.user) + st["last_observed_bytes_len"] = 0 + return False, None + self.missingtext = True + return True, None + + cand_idx_cur = int(st["cand_idx"]) + prompts_cur = int(st["prompts_this_conn"]) + cur_cand = st["cur_cand"] + + if st["cand_sent_pw_this_conn"]: + if (cand_idx_cur + 1) >= len(runtime_candidates): + return False, str(sanitize_exception(None, code="AUTH_FAILED", secrets=raw_secrets)) + if prompts_cur >= max_auth_prompts: + st["cand_idx"] = cand_idx_cur + 1 + return False, "__NEXT__:next_candidate" + cand_idx_cur += 1 + st["cand_idx"] = cand_idx_cur + cur_cand = runtime_candidates[cand_idx_cur] + st["cur_cand"] = cur_cand + st["cur_user"] = cur_cand.user + st["cand_sent_pw_this_conn"] = False + st["logged_fallback_this_cand"] = False + + u_to_send = cur_cand.user if cur_cand is not None else "" + if not u_to_send: + if (cand_idx_cur + 1) < len(runtime_candidates) and runtime_candidates[cand_idx_cur + 1].user: + cand_idx_cur += 1 + st["cand_idx"] = cand_idx_cur + cur_cand = runtime_candidates[cand_idx_cur] + st["cur_cand"] = cur_cand + st["cur_user"] = cur_cand.user + u_to_send = cur_cand.user + st["logged_fallback_this_cand"] = False + else: + return False, str(sanitize_exception(None, code="NO_CREDS", secrets=raw_secrets)) + + if not tracker.try_acquire_user(u_to_send, limit=max_global_user_attempts): + return False, str( + sanitize_exception(None, code="USER_BUDGET_EXHAUSTED", secrets=raw_secrets) + ) + + if cur_cand is not None and cur_cand.is_fallback and not st["logged_fallback_this_cand"]: + self._emit_aaa_fallback_log( + logger, + unique=str(self.unique), + host=str(self.host), + protocol=effective_protocol, + user=u_to_send, + label=cur_cand.label, + secrets=raw_secrets, + ) + self.last_fallback_used = True + st["logged_fallback_this_cand"] = True + + child_obj.sendline(u_to_send) + st["handshake_progressed"] = True + st["stage"] = "auth" + st["last_observed_bytes_len"] = 0 + return False, None + + def _handle_target_password_prompt(child_obj) -> tuple[bool, str | None]: + cand_idx_cur = int(st["cand_idx"]) + prompts_cur = int(st["prompts_this_conn"]) + cur_cand = st["cur_cand"] + cur_user = str(st["cur_user"]) + + if not unattended: + if cur_cand is not None and cur_cand.has_password and not st["cand_sent_pw_this_conn"]: + child_obj.sendline(cur_cand.password) + st["cand_sent_pw_this_conn"] = True + st["target_creds_sent"] = True + st["handshake_progressed"] = True + st["stage"] = "auth" + st["last_observed_bytes_len"] = 0 + return False, None + if (cand_idx_cur + 1) < len(runtime_candidates) and not runtime_candidates[cand_idx_cur + 1].is_fallback: + cand_idx_cur += 1 + st["cand_idx"] = cand_idx_cur + cur_cand = runtime_candidates[cand_idx_cur] + st["cur_cand"] = cur_cand + if cur_cand.has_password: + child_obj.sendline(cur_cand.password) + st["cand_sent_pw_this_conn"] = True + st["target_creds_sent"] = True + st["handshake_progressed"] = True + st["stage"] = "auth" + st["last_observed_bytes_len"] = 0 + return False, None + self.missingtext = True + return True, None + + if st["cand_sent_pw_this_conn"]: + if (cand_idx_cur + 1) >= len(runtime_candidates): + return False, str(sanitize_exception(None, code="AUTH_FAILED", secrets=raw_secrets)) + nxt = runtime_candidates[cand_idx_cur + 1] + if nxt.user == cur_user and prompts_cur < max_auth_prompts: + cand_idx_cur += 1 + st["cand_idx"] = cand_idx_cur + cur_cand = nxt + st["cur_cand"] = cur_cand + st["cand_sent_pw_this_conn"] = False + st["logged_fallback_this_cand"] = False + else: + st["cand_idx"] = cand_idx_cur + 1 + return False, "__NEXT__:next_candidate" + + if cur_cand is None or not cur_cand.has_password: + if (cand_idx_cur + 1) < len(runtime_candidates): + nxt = runtime_candidates[cand_idx_cur + 1] + if nxt.user == cur_user and prompts_cur < max_auth_prompts: + cand_idx_cur += 1 + st["cand_idx"] = cand_idx_cur + cur_cand = nxt + st["cur_cand"] = cur_cand + st["cand_sent_pw_this_conn"] = False + st["logged_fallback_this_cand"] = False + else: + st["cand_idx"] = cand_idx_cur + 1 + return False, "__NEXT__:next_candidate" + else: + return False, str(sanitize_exception(None, code="NO_CREDS", secrets=raw_secrets)) + + if not cur_cand.has_password: + return False, str(sanitize_exception(None, code="NO_CREDS", secrets=raw_secrets)) + + if prompts_cur >= max_auth_prompts: + if (cand_idx_cur + 1) < len(runtime_candidates): + st["cand_idx"] = cand_idx_cur + 1 + return False, "__NEXT__:next_candidate" + return False, str(sanitize_exception(None, code="AUTH_FAILED", secrets=raw_secrets)) + + eff_u = cur_cand.user or str(self.user or "") + acquired, budget_err_code = tracker.try_acquire_target_attempt( + host=str(self.host), + port=str(self.port or ""), + protocol=effective_protocol, + user=eff_u, + host_limit=max_total_auth_attempts, + user_limit=max_global_user_attempts, + ) + if not acquired: + return False, str( + sanitize_exception( + None, + code=budget_err_code or "HOST_BUDGET_EXHAUSTED", + secrets=raw_secrets, + ) + ) + + if cur_cand.is_fallback and not st["logged_fallback_this_cand"]: + self._emit_aaa_fallback_log( + logger, + unique=str(self.unique), + host=str(self.host), + protocol=effective_protocol, + user=eff_u, + label=cur_cand.label, + secrets=raw_secrets, + ) + self.last_fallback_used = True + st["logged_fallback_this_cand"] = True + + child_obj.sendline(cur_cand.password) + st["cand_sent_pw_this_conn"] = True + st["target_creds_sent"] = True + st["prompts_this_conn"] = prompts_cur + 1 + st["handshake_progressed"] = True + st["stage"] = "auth" + st["last_observed_bytes_len"] = 0 + return False, None + + while True: remaining = deadline - clock() if remaining <= 0: return _redact_known_secrets( @@ -2630,16 +3334,50 @@ class node: raw_secrets, ) - handshake_progressed = False + cand_idx_val = int(st["cand_idx"]) + cur_cand = runtime_candidates[cand_idx_val] if cand_idx_val < len(runtime_candidates) else None + cur_user = cur_cand.user if cur_cand is not None else str(self.user or "") + st["cur_cand"] = cur_cand + st["cur_user"] = cur_user + user_override = ( + cur_user + if (cand_idx_val > 0 or (cur_user and cur_user != str(self.user or ""))) + else None + ) + + cmd = self._get_cmd( + force_ssh=force_ssh, + allow_reverse_tunnel_port=allow_reverse_tunnel_port, + connect_timeout=eff_ct, + user_override=user_override, + ) + + st["handshake_progressed"] = False args = shlex.split(cmd) - child, err = self._spawn_child(args, protocol=effective_protocol) + child, err = self._spawn_child( + args, + protocol=effective_protocol, + _secrets=raw_secrets, + ) if err: return _redact_known_secrets(err, raw_secrets) connected_ok = False local_mylog = None - retry_attempt = False + next_action: str | None = None + hop_pw_idx = [0] * len(jumphost_hops) + hop_cursor = 0 + st["prompts_this_conn"] = 0 + st["cand_sent_pw_this_conn"] = False + st["logged_fallback_this_cand"] = False + transport_error_seen = False + try: + child.logfile = None + child.logfile_send = None + if unattended: + child.logfile_read = None + if isinstance(self.tags, dict) and self.tags.get("console"): child.sendline() if debug: @@ -2647,13 +3385,14 @@ class node: logger("debug", f"Command:\n{_redact_known_secrets(cmd, raw_secrets)}") local_mylog = io.BytesIO() self.mylog = local_mylog - local_mylog.write(f"[i] [DEBUG] Command:\r\n {cmd}\r\n".encode()) - child.logfile_read = local_mylog + redacted_cmd = _redact_known_secrets(cmd, raw_secrets) + local_mylog.write(f"[i] [DEBUG] Command:\r\n {redacted_cmd}\r\n".encode()) + if not unattended: + child.logfile_read = local_mylog - pw_idx = 0 - stage = "connect" + st["stage"] = "connect" progress_extensions = 0 - last_observed_bytes_len = 0 + st["last_observed_bytes_len"] = 0 while True: remaining = deadline - clock() @@ -2662,42 +3401,45 @@ class node: f"Connection failed code: 12\nConnection timeout (global deadline {max_total}s exceeded)", raw_secrets, ) - stage_timeout = eff_ct if stage == "connect" else eff_at + stage_timeout = eff_ct if st["stage"] == "connect" else eff_at expect_timeout = max(0.001, min(stage_timeout, remaining)) results = child.expect(proto_expects, timeout=expect_timeout) results_value = proto_expects[results] before_bytes = _to_bytes(getattr(child, "before", b"")) + after_bytes = _to_bytes(getattr(child, "after", b"")) - if len(before_bytes) > MAX_PRE_AUTH_BUFFER_BYTES: + if len(before_bytes) > stream_limit: return _redact_known_secrets( - f"Connection failed code: 12\nPre-auth buffer exceeded maximum size ({MAX_PRE_AUTH_BUFFER_BYTES} bytes)", + f"Connection failed code: 12\nPre-auth buffer exceeded maximum size ({stream_limit} bytes)", raw_secrets, ) if results in initial_indices[effective_protocol]: if effective_protocol in ["ssh", "sftp", "mosh"]: child.sendline('yes') - handshake_progressed = True - last_observed_bytes_len = 0 + st["handshake_progressed"] = True + st["last_observed_bytes_len"] = 0 elif effective_protocol in ["telnet", "kubectl", "docker", "ssm"]: - if self.user: - child.sendline(self.user) - last_observed_bytes_len = 0 - else: - self.missingtext = True + should_break, msg_or_action = _handle_username_prompt(child) + if msg_or_action == "__NEXT__:next_candidate": + next_action = "next_candidate" + break + if msg_or_action is not None: + return msg_or_action + if should_break: break elif results in error_indices[effective_protocol]: if results_value == pexpect.TIMEOUT: cur_len = len(before_bytes) - bytes_grew = cur_len > last_observed_bytes_len - last_observed_bytes_len = cur_len + bytes_grew = cur_len > int(st["last_observed_bytes_len"]) + st["last_observed_bytes_len"] = cur_len remaining = deadline - clock() can_extend = ( bytes_grew and progress_extensions < max_ext and remaining > 0 - and not (effective_protocol == "mosh" and handshake_progressed) + and not (effective_protocol == "mosh" and st["handshake_progressed"]) ) if can_extend: progress_extensions += 1 @@ -2710,13 +3452,26 @@ class node: ) continue - can_retry = ( - attempts < max_attempts + if ( + unattended + and st["stage"] == "auth" + and st["target_creds_sent"] + and (int(st["cand_idx"]) + 1) < len(runtime_candidates) and remaining > 0 - and not (effective_protocol == "mosh" and handshake_progressed) + ): + st["cand_idx"] = int(st["cand_idx"]) + 1 + next_action = "next_candidate" + break + + can_retry = ( + not st["target_creds_sent"] + and (int(st["connect_retries"]) + 1) < max_attempts + and remaining > 0 + and not (effective_protocol == "mosh" and st["handshake_progressed"]) ) if can_retry: - retry_attempt = True + st["connect_retries"] = int(st["connect_retries"]) + 1 + next_action = "retry_connect" break before_str = _to_str(before_bytes) @@ -2731,7 +3486,23 @@ class node: raw_secrets, ) - after = _to_str(getattr(child, "after", b"")) + is_perm_denied = ( + effective_protocol in ("ssh", "sftp", "mosh") + and results == 2 + and b"permission denied" in (before_bytes + after_bytes).lower() + ) + if ( + unattended + and is_perm_denied + and (int(st["cand_idx"]) + 1) < len(runtime_candidates) + and remaining > 0 + ): + st["cand_idx"] = int(st["cand_idx"]) + 1 + next_action = "next_candidate" + break + + transport_error_seen = not is_perm_denied + after = _to_str(after_bytes) before = _to_str(before_bytes) readline_val = _to_str(child.readline()) if effective_protocol == "mosh": @@ -2759,28 +3530,120 @@ class node: elif results in eof_indices[effective_protocol]: if results == pw_prompt_idx: - if passwords and pw_idx < len(passwords): - child.sendline(passwords[pw_idx]) - pw_idx += 1 - handshake_progressed = True - stage = "auth" - last_observed_bytes_len = 0 - else: - self.missingtext = True + after_lower = after_bytes.lower() + if b"username" in after_lower and b"password" not in after_lower: + should_break, msg_or_action = _handle_username_prompt(child) + if msg_or_action == "__NEXT__:next_candidate": + next_action = "next_candidate" + break + if msg_or_action is not None: + return msg_or_action + if should_break: + break + continue + + if jumphost_hops: + prompt_line = normalize_prompt_line(before_bytes, after_bytes) + kind, hop_idx = self._disambiguate_prompt(prompt_line) + if kind == "conflict": + return str( + sanitize_exception(None, code="JUMP_AUTH_FAILED", secrets=raw_secrets) + ) + if kind == "hop": + assert hop_idx is not None + if st["target_creds_sent"] or hop_idx < hop_cursor: + return str( + sanitize_exception(None, code="JUMP_AUTH_FAILED", secrets=raw_secrets) + ) + hop_cursor = hop_idx + hop_obj = jumphost_hops[hop_idx] + hop_pws = hop_runtime_passwords[hop_idx] + if hop_pw_idx[hop_idx] >= len(hop_pws): + return str( + sanitize_exception(None, code="JUMP_AUTH_FAILED", secrets=raw_secrets) + ) + if not tracker.try_acquire_hop( + hop_obj["host"], + hop_obj["port"], + hop_obj["protocol"], + limit=max_hop_host_attempts, + ): + return str( + sanitize_exception(None, code="HOST_BUDGET_EXHAUSTED", secrets=raw_secrets) + ) + child.sendline(hop_pws[hop_pw_idx[hop_idx]]) + hop_pw_idx[hop_idx] += 1 + st["handshake_progressed"] = True + st["last_observed_bytes_len"] = 0 + continue + if kind == "target": + hop_cursor = len(jumphost_hops) + else: + allow_ambig = bool( + tags_dict.get("allow_ambiguous_single_hop_prompt", False) + ) + if len(jumphost_hops) > 1 or not allow_ambig: + return str( + sanitize_exception(None, code="JUMP_AUTH_FAILED", secrets=raw_secrets) + ) + if not st["target_creds_sent"] and hop_pw_idx[0] < len(hop_runtime_passwords[0]): + hop_obj = jumphost_hops[0] + if not tracker.try_acquire_hop( + hop_obj["host"], + hop_obj["port"], + hop_obj["protocol"], + limit=max_hop_host_attempts, + ): + return str( + sanitize_exception( + None, + code="HOST_BUDGET_EXHAUSTED", + secrets=raw_secrets, + ) + ) + child.sendline(hop_runtime_passwords[0][hop_pw_idx[0]]) + hop_pw_idx[0] += 1 + st["handshake_progressed"] = True + st["last_observed_bytes_len"] = 0 + continue + + should_break, msg_or_action = _handle_target_password_prompt(child) + if msg_or_action == "__NEXT__:next_candidate": + next_action = "next_candidate" break + if msg_or_action is not None: + return msg_or_action + if should_break: + break + elif suspend_idx is not None and results == suspend_idx: child.sendline("\r") sleep(2) - last_observed_bytes_len = 0 + st["last_observed_bytes_len"] = 0 elif results_value == pexpect.EOF: norm_buf = ( before_bytes.replace(b"\r\n", b"\n").replace(b"\r", b"\n") + b"\n" ) + eof_perm_denied = False for err_idx in error_indices[effective_protocol]: err_pat = proto_expects[err_idx] if isinstance(err_pat, re.Pattern): m = err_pat.search(norm_buf) if m: + is_pd = ( + effective_protocol in ("ssh", "sftp", "mosh") + and err_idx == 2 + and b"permission denied" in m.group(0).lower() + ) + if ( + unattended + and is_pd + and (int(st["cand_idx"]) + 1) < len(runtime_candidates) + and remaining > 0 + ): + eof_perm_denied = True + break + transport_error_seen = not is_pd b_str = _to_str(norm_buf[: m.start()]) a_str = _to_str(m.group(0)) if effective_protocol == "mosh": @@ -2790,16 +3653,70 @@ class node: f"Connection failed code: {err_idx}\n{b_str.lstrip()}{a_str}".rstrip(), raw_secrets, ) + if eof_perm_denied: + st["cand_idx"] = int(st["cand_idx"]) + 1 + next_action = "next_candidate" + break + + if unattended and not transport_error_seen: + if st["target_creds_sent"]: + if (int(st["cand_idx"]) + 1) < len(runtime_candidates): + eof_adv = int(st["eof_fallback_advances"]) + if eof_adv < MAX_EOF_FALLBACK_ADVANCES: + st["eof_fallback_advances"] = eof_adv + 1 + st["cand_idx"] = int(st["cand_idx"]) + 1 + next_action = "next_candidate" + break + return str( + sanitize_exception( + None, + code="EOF_BUDGET_EXHAUSTED", + secrets=raw_secrets, + ) + ) + return str( + sanitize_exception( + None, + code="AUTH_FAILED", + secrets=raw_secrets, + ) + ) + if effective_protocol != "local": + return str( + sanitize_exception( + None, + code="TRANSPORT_ERROR", + secrets=raw_secrets, + ) + ) + try: child.sendline() except Exception as exc: # noqa: BLE001 _LOGGER.debug("EOF fallback sendline ignored: %s", type(exc).__name__) break else: + cur_cand_now = st["cur_cand"] + if ( + cur_cand_now is not None + and cur_cand_now.is_fallback + and not st["logged_fallback_this_cand"] + ): + self._emit_aaa_fallback_log( + logger, + unique=str(self.unique), + host=str(self.host), + protocol=effective_protocol, + user=str(st["cur_user"]), + label=cur_cand_now.label, + secrets=raw_secrets, + ) + self.last_fallback_used = True + st["logged_fallback_this_cand"] = True child.sendline() break - if retry_attempt: + if next_action in ("retry_connect", "next_candidate"): continue if isinstance(self.tags, dict) and self.tags.get("post_connect_commands"): @@ -2815,6 +3732,14 @@ class node: self.raw_child = fdpexpect.fdspawn(self.child.child_fd) connected_ok = True return True + except Exception as exc: # noqa: BLE001 + san = sanitize_exception( + exc, + code="AUTH_FAILED", + secrets=raw_secrets, + context_label="unexpected connection error", + ) + return str(san) finally: if not connected_ok: try: @@ -2877,19 +3802,18 @@ class nodes: ''' self.nodelist = [] self.config = config + self.fallback_used = {} for n, node_cfg in nodes.items(): this = node(n, **node_cfg, config=config) self.nodelist.append(this) setattr(self, n, this) - @MethodHook def _splitlist(self, lst, n): #split a list in lists of n members. for i in range(0, len(lst), n): yield lst[i:i + n] - @MethodHook def run( self, @@ -2906,6 +3830,8 @@ class nodes: 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. @@ -2977,8 +3903,11 @@ class nodes: 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): @@ -2989,6 +3918,8 @@ class nodes: for n in self.nodelist: nodesargs[n.unique] = deepcopy(args) + 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: @@ -3021,8 +3952,10 @@ class nodes: 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 @MethodHook @@ -3041,6 +3974,8 @@ class nodes: 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. @@ -3109,9 +4044,12 @@ class nodes: 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): @@ -3122,6 +4060,8 @@ class nodes: for n in self.nodelist: nodesargs[n.unique] = deepcopy(args) + 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: @@ -3152,9 +4092,11 @@ class nodes: 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 # script diff --git a/connpy/grpc_layer/server.py b/connpy/grpc_layer/server.py index 55be642..41d9dbf 100644 --- a/connpy/grpc_layer/server.py +++ b/connpy/grpc_layer/server.py @@ -1,26 +1,37 @@ -import grpc -from concurrent import futures -from google.protobuf.empty_pb2 import Empty -import os -import ctypes -import threading import contextvars import datetime +import functools +import json +import os +from concurrent import futures + +import grpc +from google.protobuf.empty_pb2 import Empty # Suppress harmless but noisy gRPC fork() warnings from pexpect child processes os.environ["GRPC_VERBOSITY"] = "NONE" os.environ["GRPC_ENABLE_FORK_SUPPORT"] = "0" -from . import connpy_pb2, connpy_pb2_grpc, remote_plugin_pb2, remote_plugin_pb2_grpc -import json -from .utils import to_value, from_value, to_struct, from_struct -from ..services.exceptions import ConnpyError from .. import printer +from ..auth_utils import ( + MAX_FALLBACK_ENTRIES, + MAX_JSON_INPUT_BYTES, + MAX_JSON_INPUT_DEPTH, + SanitizedAuthError, + sanitize_exception, + validate_auth_budget_tags, + validate_fallback_entries, + validate_json_depth_and_size, +) +from ..services.exceptions import ConnpyError +from . import connpy_pb2, connpy_pb2_grpc, remote_plugin_pb2, remote_plugin_pb2_grpc +from .utils import from_struct, from_value, to_struct, to_value _current_user = contextvars.ContextVar("current_user", default=None) def handle_errors(func): import inspect if inspect.isgeneratorfunction(func): + @functools.wraps(func) def wrapper(*args, **kwargs): try: for item in func(*args, **kwargs): @@ -41,6 +52,7 @@ def handle_errors(func): printer.clear_thread_state() return wrapper else: + @functools.wraps(func) def wrapper(*args, **kwargs): try: return func(*args, **kwargs) @@ -115,8 +127,24 @@ class NodeServicer(connpy_pb2_grpc.NodeServiceServicer): 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'] @@ -134,6 +162,8 @@ class NodeServicer(connpy_pb2_grpc.NodeServiceServicer): 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", {}) @@ -147,20 +177,30 @@ class NodeServicer(connpy_pb2_grpc.NodeServiceServicer): 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" @@ -181,9 +221,11 @@ class NodeServicer(connpy_pb2_grpc.NodeServiceServicer): 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() @@ -204,7 +246,14 @@ class NodeServicer(connpy_pb2_grpc.NodeServiceServicer): 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: @@ -535,10 +584,9 @@ class NodeServicer(connpy_pb2_grpc.NodeServiceServicer): 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/connpy/mcp_server.py b/connpy/mcp_server.py index cbc894e..e35935e 100644 --- a/connpy/mcp_server.py +++ b/connpy/mcp_server.py @@ -6,6 +6,20 @@ from typing import Any, ClassVar import yaml +from .auth_utils import ( + MAX_FALLBACK_ENTRIES, + MAX_JSON_INPUT_BYTES, + MAX_JSON_INPUT_DEPTH, + SanitizedAuthError, + is_compose_expression, + mask_node_secrets, + sanitize_exception, + validate_auth_budget_tags, + validate_compose_syntax, + validate_fallback_entries, + validate_json_depth_and_size, +) + try: from mcp.server.mcpserver import MCPServer except ImportError: @@ -124,20 +138,7 @@ class ConnpyMCPServer: def _mask_secrets(self, val: Any) -> Any: """Recursively mask passwords, passphrases, secrets, and auth tokens with '***'.""" - if isinstance(val, dict): - masked = {} - for k, v in val.items(): - k_lower = str(k).lower() - if any(sec in k_lower for sec in ["password", "passphrase", "secret", "token"]): - masked[k] = "***" - elif isinstance(v, (dict, list)): - masked[k] = self._mask_secrets(v) - else: - masked[k] = v - return masked - if isinstance(val, list): - return [self._mask_secrets(item) for item in val] - return val + return mask_node_secrets(val) # ------------------------------------------------------------------------- # Level 1: Read-Only Inventory and Context Tools @@ -196,6 +197,31 @@ class ConnpyMCPServer: # Level 2: Execution, Testing, and Playbooks Tools # ------------------------------------------------------------------------- + @staticmethod + def _parse_json_list_or_lines(raw_val: list[str] | str, *, allow_empty_str: bool = False) -> list[str]: + if isinstance(raw_val, str): + if len(raw_val.encode("utf-8", errors="replace")) > MAX_JSON_INPUT_BYTES: + raise ValueError(f"Input exceeds maximum size of {MAX_JSON_INPUT_BYTES} bytes") + stripped = raw_val.strip() + if stripped.startswith(("[", "{")): + parsed = validate_json_depth_and_size( + stripped, + max_bytes=MAX_JSON_INPUT_BYTES, + max_depth=MAX_JSON_INPUT_DEPTH, + ) + return parsed if isinstance(parsed, list) else [raw_val] + return ([raw_val] if raw_val else []) if allow_empty_str else [ + c.strip() for c in raw_val.splitlines() if c.strip() + ] + if isinstance(raw_val, list): + validate_json_depth_and_size( + raw_val, + max_bytes=MAX_JSON_INPUT_BYTES, + max_depth=MAX_JSON_INPUT_DEPTH, + ) + return raw_val + return [str(raw_val)] if (raw_val or not allow_empty_str) else [] + def run_commands( self, nodes_filter: str, @@ -210,17 +236,10 @@ class ConnpyMCPServer: if not self.services or not hasattr(self.services, "execution"): return {"success": False, "error": "Execution service unavailable."} - # Normalize commands list - if isinstance(commands, str): - try: - import json - - parsed = json.loads(commands) - commands = parsed if isinstance(parsed, list) else [commands] - except Exception: # noqa: BLE001 - commands = [c.strip() for c in commands.splitlines() if c.strip()] - elif not isinstance(commands, list): - commands = [str(commands)] + try: + commands = self._parse_json_list_or_lines(commands, allow_empty_str=False) + except ValueError as exc: + return {"success": False, "error": str(exc)} # Check safety if not self.trust: @@ -239,9 +258,24 @@ class ConnpyMCPServer: parallel=par_count, timeout=timeout, ) - return {"success": True, "results": results} + fb_used = False + if isinstance(results, dict): + fb_used = any( + isinstance(v, dict) and bool(v.get("fallback_used", False)) + for v in results.values() + ) + if not fb_used: + fb_map = getattr(self.services.execution, "last_fallback_used", {}) + if isinstance(fb_map, dict): + fb_used = any(bool(v) for v in fb_map.values()) + return {"success": True, "results": results, "fallback_used": fb_used} except Exception as e: # noqa: BLE001 - return {"success": False, "error": str(e)} + san = sanitize_exception( + e, + code="AUTH_FAILED", + context_label="command execution failed", + ) + return {"success": False, "error": str(san)} def test_commands( self, @@ -255,29 +289,11 @@ class ConnpyMCPServer: if not self.services or not hasattr(self.services, "execution"): return {"success": False, "error": "Execution service unavailable."} - # Normalize commands - if isinstance(commands, str): - try: - import json - - parsed = json.loads(commands) - commands = parsed if isinstance(parsed, list) else [commands] - except Exception: # noqa: BLE001 - commands = [c.strip() for c in commands.splitlines() if c.strip()] - elif not isinstance(commands, list): - commands = [str(commands)] - - # Normalize expected - if isinstance(expected, str): - try: - import json - - parsed = json.loads(expected) - expected = parsed if isinstance(parsed, list) else ([expected] if expected else []) - except Exception: # noqa: BLE001 - expected = [expected] if expected else [] - elif not isinstance(expected, list): - expected = [str(expected)] if expected else [] + try: + commands = self._parse_json_list_or_lines(commands, allow_empty_str=False) + expected = self._parse_json_list_or_lines(expected, allow_empty_str=True) + except ValueError as exc: + return {"success": False, "error": str(exc)} # Check safety if not self.trust: @@ -297,9 +313,16 @@ class ConnpyMCPServer: parallel=par_count, timeout=timeout, ) - return {"success": True, "results": results} + fb_map = getattr(self.services.execution, "last_fallback_used", {}) + fb_used = any(bool(v) for v in fb_map.values()) if isinstance(fb_map, dict) else False + return {"success": True, "results": results, "fallback_used": fb_used} except Exception as e: # noqa: BLE001 - return {"success": False, "error": str(e)} + san = sanitize_exception( + e, + code="AUTH_FAILED", + context_label="test execution failed", + ) + return {"success": False, "error": str(san)} def validate_playbook(self, playbook_yaml: str) -> dict: """Validate the syntax, schema, and tasks of a Connpy YAML playbook.""" @@ -427,6 +450,47 @@ class ConnpyMCPServer: # Level 3: Inventory Mutation Tools (CRUD - Requires trust=True) # ------------------------------------------------------------------------- + @staticmethod + def _validate_and_normalize_node_payload(node_data: dict[str, Any]) -> dict[str, Any]: + validate_json_depth_and_size( + node_data, + max_bytes=MAX_JSON_INPUT_BYTES, + max_depth=MAX_JSON_INPUT_DEPTH, + ) + normalized = node_data.copy() + if "tags" in normalized and normalized["tags"] not in ("", None): + tags_val = normalized["tags"] + if isinstance(tags_val, str) and tags_val.strip().startswith("{"): + tags_val = validate_json_depth_and_size( + tags_val, + max_bytes=MAX_JSON_INPUT_BYTES, + max_depth=MAX_JSON_INPUT_DEPTH, + ) + normalized["tags"] = tags_val + validate_auth_budget_tags(tags_val) + + if "fallback" in normalized and normalized["fallback"] not in (None, "", []): + fb_val = normalized["fallback"] + if isinstance(fb_val, str) and fb_val.strip().startswith(("[", "{")): + fb_val = validate_json_depth_and_size( + fb_val, + max_bytes=MAX_JSON_INPUT_BYTES, + max_depth=MAX_JSON_INPUT_DEPTH, + ) + normalized["fallback"] = validate_fallback_entries( + fb_val, + max_entries=MAX_FALLBACK_ENTRIES, + ) + + pw = normalized.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) + return normalized + def add_node( self, unique_id: str, data: dict | None = None, is_folder: bool = False, **kwargs ) -> dict: @@ -444,11 +508,14 @@ class ConnpyMCPServer: node_data.update(kwargs) try: + node_data = self._validate_and_normalize_node_payload(node_data) self.services.nodes.add_node(unique_id, node_data, is_folder=is_folder) return { "success": True, "message": f"{'Folder' if is_folder else 'Node'} '{unique_id}' created successfully.", } + except (ValueError, TypeError, SanitizedAuthError) as exc: + return {"success": False, "error": str(exc)} except Exception as e: # noqa: BLE001 return {"success": False, "error": str(e)} @@ -467,8 +534,11 @@ class ConnpyMCPServer: node_updates.update(kwargs) try: + node_updates = self._validate_and_normalize_node_payload(node_updates) self.services.nodes.update_node(unique_id, node_updates) return {"success": True, "message": f"Node '{unique_id}' updated successfully."} + except (ValueError, TypeError, SanitizedAuthError) as exc: + return {"success": False, "error": str(exc)} except Exception as e: # noqa: BLE001 return {"success": False, "error": str(e)} diff --git a/connpy/services/execution_service.py b/connpy/services/execution_service.py index a7ffcf8..b91a580 100644 --- a/connpy/services/execution_service.py +++ b/connpy/services/execution_service.py @@ -1,7 +1,10 @@ +import inspect import os from collections.abc import Callable +from contextlib import suppress from typing import Any +from connpy.auth_utils import AuthAttemptTracker, raise_sanitized_auth_error from connpy.core import nodes as Nodes from .base import BaseService @@ -11,9 +14,13 @@ from .exceptions import ConnpyError class ExecutionService(BaseService): """Business logic for executing commands on nodes and running automation scripts.""" - def __init__(self, config=None, remote_stub=None): + def __init__(self, config=None, remote_stub=None, attempt_tracker=None): super().__init__(config) self.remote_stub = remote_stub + self.attempt_tracker = ( + attempt_tracker if attempt_tracker is not None else AuthAttemptTracker() + ) + self.last_fallback_used: dict[str, bool] = {} def _is_remote_backend(self) -> bool: """Return True when execution is configured to run against a remote gRPC backend.""" @@ -54,11 +61,8 @@ class ExecutionService(BaseService): if defaultdir: token_path = os.path.join(defaultdir, ".token") if os.path.exists(token_path): - try: - with open(token_path) as f: - return f.read().strip() - except OSError: - pass + with suppress(OSError), open(token_path) as f: + return f.read().strip() return None channel = grpc.insecure_channel(remote_host) @@ -110,33 +114,47 @@ class ExecutionService(BaseService): executor = Nodes(node_data, config=self.config) self.last_executor = executor - results = executor.run( - commands=commands, - vars=variables, - parallel=parallel, - timeout=timeout, - folder=folder, - prompt=prompt, - on_complete=on_node_complete, - logger=logger, - connect_timeout=connect_timeout, - auth_timeout=auth_timeout, - _default_cmd_timeout=20, - ) + run_kwargs: dict[str, Any] = { + "commands": commands, + "vars": variables, + "parallel": parallel, + "timeout": timeout, + "folder": folder, + "prompt": prompt, + "on_complete": on_node_complete, + "logger": logger, + "connect_timeout": connect_timeout, + "auth_timeout": auth_timeout, + "_default_cmd_timeout": 20, + } + run_params = inspect.signature(executor.run).parameters + if "unattended" in run_params: + run_kwargs["unattended"] = True + if "attempt_tracker" in run_params: + run_kwargs["attempt_tracker"] = self.attempt_tracker - # Combine output and status for the caller + results = executor.run(**run_kwargs) + + fb_map = getattr(executor, "fallback_used", {}) + fb_dict = fb_map if isinstance(fb_map, dict) else {} + + # Combine output, status, and fallback metadata for the caller full_results = {} + self.last_fallback_used = {} for unique in results: + fb_used = bool(fb_dict.get(unique, False)) full_results[unique] = { "output": results[unique], "status": executor.status.get(unique, 1), + "fallback_used": fb_used, } + self.last_fallback_used[unique] = fb_used return full_results except ConnpyError: raise - except Exception as e: - raise ConnpyError(f"Execution failed: {e}") from e + except Exception as e: # noqa: BLE001 + raise_sanitized_auth_error(e, code="AUTH_FAILED", context_label="execution failed") def test_commands( self, @@ -185,24 +203,38 @@ class ExecutionService(BaseService): executor = Nodes(node_data, config=self.config) self.last_executor = executor - return executor.test( - commands=commands, - expected=expected, - vars=variables, - parallel=parallel, - timeout=timeout, - folder=folder, - prompt=prompt, - on_complete=on_node_complete, - logger=logger, - connect_timeout=connect_timeout, - auth_timeout=auth_timeout, - _default_cmd_timeout=20, - ) + test_kwargs: dict[str, Any] = { + "commands": commands, + "expected": expected, + "vars": variables, + "parallel": parallel, + "timeout": timeout, + "folder": folder, + "prompt": prompt, + "on_complete": on_node_complete, + "logger": logger, + "connect_timeout": connect_timeout, + "auth_timeout": auth_timeout, + "_default_cmd_timeout": 20, + } + test_params = inspect.signature(executor.test).parameters + if "unattended" in test_params: + test_kwargs["unattended"] = True + if "attempt_tracker" in test_params: + test_kwargs["attempt_tracker"] = self.attempt_tracker + + res = executor.test(**test_kwargs) + fb_map = getattr(executor, "fallback_used", {}) + fb_dict = fb_map if isinstance(fb_map, dict) else {} + if isinstance(res, dict): + self.last_fallback_used = {u: bool(fb_dict.get(u, False)) for u in res} + else: + self.last_fallback_used = {} + return res except ConnpyError: raise - except Exception as e: - raise ConnpyError(f"Testing failed: {e}") from e + except Exception as e: # noqa: BLE001 + raise_sanitized_auth_error(e, code="AUTH_FAILED", context_label="testing failed") def run_cli_script(self, nodes_filter: str, script_path: str, parallel: int = 10) -> dict[str, str]: """Run a plain-text script containing one command per line.""" @@ -212,8 +244,8 @@ class ExecutionService(BaseService): try: with open(script_path) as f: commands = [line.strip() for line in f if line.strip()] - except OSError as e: - raise ConnpyError(f"Failed to read script {script_path}: {e}") from e + except OSError: + raise ConnpyError("Failed to read script file") from None return self.run_commands(nodes_filter, commands, parallel=parallel) diff --git a/connpy/services/node_service.py b/connpy/services/node_service.py index cb78f68..6704e1c 100644 --- a/connpy/services/node_service.py +++ b/connpy/services/node_service.py @@ -1,14 +1,73 @@ +import inspect import re +from copy import deepcopy +from typing import ClassVar + from .base import BaseService from .exceptions import ( - NodeNotFoundError, NodeAlreadyExistsError, - InvalidConfigurationError, ReservedNameError + InvalidConfigurationError, + NodeAlreadyExistsError, + NodeNotFoundError, + ReservedNameError, ) + +def _supports_transactional_update(cfg): + return ( + cfg is not None + and not hasattr(cfg, "assert_called") + and callable(getattr(cfg, "transactional_update", None)) + ) + + class NodeService(BaseService): + _CONNECTION_FIELDS: ClassVar[frozenset[str]] = frozenset({ + "id", + "host", + "folder", + "subfolder", + "options", + "logs", + "password", + "port", + "protocol", + "user", + "tags", + "jumphost", + "fallback", + "type", + }) + def __init__(self, config=None): super().__init__(config) + @staticmethod + def _validate_node_payload(data): + if not isinstance(data, dict): + return + from ..auth_utils import ( + SanitizedAuthError, + is_compose_expression, + validate_auth_budget_tags, + validate_compose_syntax, + validate_fallback_entries, + ) + + try: + if "tags" in data and data["tags"] not in ("", None): + validate_auth_budget_tags(data["tags"]) + if "fallback" in data and data["fallback"] not in (None, "", []): + validate_fallback_entries(data["fallback"]) + pw = data.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) + except (ValueError, TypeError, SanitizedAuthError) as exc: + raise InvalidConfigurationError(str(exc)) from None + def list_nodes(self, filter_str=None, format_str=None): """Return a listed filtered by regex match and formatted if needed.""" @@ -85,7 +144,7 @@ class NodeService(BaseService): raise NodeNotFoundError(f"Node '{unique_id}' not found.") return details except (KeyError, TypeError): - raise NodeNotFoundError(f"Node '{unique_id}' not found.") + raise NodeNotFoundError(f"Node '{unique_id}' not found.") from None def explode_unique(self, unique_id): """Explode a unique ID into a dictionary of its parts.""" @@ -115,58 +174,127 @@ class NodeService(BaseService): """Logic for adding a new node or folder to configuration.""" if not is_folder: self._validate_node_name(unique_id) - + self._validate_node_payload(data) + + if _supports_transactional_update(self.config): + def _mut(cfg): + all_nodes = cfg._getallnodes_unlocked() + all_folders = cfg._getallfolders_unlocked() + if is_folder: + if unique_id in all_folders: + raise NodeAlreadyExistsError(f"Folder '{unique_id}' already exists.") + uniques = cfg._explode_unique_unlocked(unique_id) + if not uniques: + raise InvalidConfigurationError(f"Invalid folder name '{unique_id}'.") + if "subfolder" in uniques: + parent_folder = f"@{uniques['folder']}" + if parent_folder not in all_folders: + raise NodeNotFoundError(f"Folder '{parent_folder}' not found.") + cfg._folder_add_unlocked(**uniques) + else: + if unique_id in all_nodes: + raise NodeAlreadyExistsError(f"Node '{unique_id}' already exists.") + node_folder = unique_id.partition("@")[2] + if node_folder: + parent_folder = f"@{node_folder}" + if parent_folder not in all_folders: + raise NodeNotFoundError(f"Folder '{parent_folder}' not found.") + payload = deepcopy(data) + if "id" not in payload: + uniques = cfg._explode_unique_unlocked(unique_id) + if uniques and "id" in uniques: + payload["id"] = uniques["id"] + cfg._connections_add_unlocked(**payload) + + self.config.transactional_update(_mut) + return + all_nodes = self.config._getallnodes() all_folders = self.config._getallfolders() - + if is_folder: if unique_id in all_folders: raise NodeAlreadyExistsError(f"Folder '{unique_id}' already exists.") uniques = self.config._explode_unique(unique_id) if not uniques: raise InvalidConfigurationError(f"Invalid folder name '{unique_id}'.") - - # Check if parent folder exists when creating a subfolder + if "subfolder" in uniques: self.validate_parent_folder(unique_id, is_folder=True) - + self.config._folder_add(**uniques) self.config._saveconfig(self.config.file) else: if unique_id in all_nodes: raise NodeAlreadyExistsError(f"Node '{unique_id}' already exists.") - - # Check if parent folder exists when creating a node in a folder + self.validate_parent_folder(unique_id) - - # Ensure 'id' is in data for config._connections_add - if "id" not in data: + + payload = deepcopy(data) + if "id" not in payload: uniques = self.config._explode_unique(unique_id) if uniques and "id" in uniques: - data["id"] = uniques["id"] - - self.config._connections_add(**data) + payload["id"] = uniques["id"] + + self.config._connections_add(**payload) self.config._saveconfig(self.config.file) + def _build_update_payload(self, unique_id, data, existing, uniques): + if isinstance(existing, dict): + merged = deepcopy(existing) + if isinstance(uniques, dict): + merged.update({k: v for k, v in uniques.items() if v}) + merged.update(deepcopy(data)) + filtered = {k: v for k, v in merged.items() if k in self._CONNECTION_FIELDS} + else: + filtered = deepcopy(data) + if "id" not in filtered and isinstance(uniques, dict) and "id" in uniques: + filtered["id"] = uniques["id"] + self._validate_node_payload(filtered) + return filtered + def update_node(self, unique_id, data, save=True): """Explicitly update an existing node.""" + if _supports_transactional_update(self.config) and save: + def _mut(cfg): + all_nodes = cfg._getallnodes_unlocked() + if unique_id not in all_nodes: + raise NodeNotFoundError(f"Node '{unique_id}' not found.") + uniques = cfg._explode_unique_unlocked(unique_id) + existing = cfg.getitem_unlocked(unique_id, extract=False) + payload = self._build_update_payload(unique_id, data, existing, uniques) + cfg._connections_add_unlocked(**payload) + + self.config.transactional_update(_mut) + return + all_nodes = self.config._getallnodes() if unique_id not in all_nodes: raise NodeNotFoundError(f"Node '{unique_id}' not found.") - - # Ensure 'id' is in data for config._connections_add - if "id" not in data: - uniques = self.config._explode_unique(unique_id) - if uniques: - data["id"] = uniques["id"] - - # config._connections_add actually handles updates if ID exists correctly - self.config._connections_add(**data) + + uniques = self.config._explode_unique(unique_id) + existing = self.config.getitem(unique_id, extract=False) + payload = self._build_update_payload(unique_id, data, existing, uniques) + self.config._connections_add(**payload) if save: self.config._saveconfig(self.config.file) def delete_node(self, unique_id, is_folder=False, save=True): """Logic for deleting a node or folder.""" + if _supports_transactional_update(self.config) and save: + def _mut(cfg): + uniques = cfg._explode_unique_unlocked(unique_id) + if not uniques: + label = "Folder" if is_folder else "Node" + raise NodeNotFoundError(f"{label} '{unique_id}' not found or invalid.") + if is_folder: + cfg._folder_del_unlocked(**uniques) + else: + cfg._connections_del_unlocked(**uniques) + + self.config.transactional_update(_mut) + return + if is_folder: uniques = self.config._explode_unique(unique_id) if not uniques: @@ -177,23 +305,40 @@ class NodeService(BaseService): if not uniques: raise NodeNotFoundError(f"Node '{unique_id}' not found or invalid.") self.config._connections_del(**uniques) - + if save: self.config._saveconfig(self.config.file) def connect_node(self, unique_id, sftp=False, debug=False, logger=None): """Interact with a node directly.""" from connpy.core import node + + from ..auth_utils import resolve_fallback_entries from .profile_service import ProfileService - + node_data = self.config.getitem(unique_id, extract=False) if not node_data: raise NodeNotFoundError(f"Node '{unique_id}' not found.") - - # Resolve profiles + profile_service = ProfileService(self.config) resolved_data = profile_service.resolve_node_data(node_data) - + + node_params = inspect.signature(node.__init__).parameters + if "fallback" not in node_params and "fallback" in resolved_data: + raw_fb = resolved_data.pop("fallback", None) + if raw_fb not in (None, "", []): + resolved_fb = resolve_fallback_entries(raw_fb, self.config.profiles) + if resolved_fb: + tags_val = resolved_data.get("tags", "") + if isinstance(tags_val, dict): + tags_copy = deepcopy(tags_val) + tags_copy["_fallback"] = resolved_fb + resolved_data["tags"] = tags_copy + elif tags_val in ("", None): + resolved_data["tags"] = {"_fallback": resolved_fb} + else: + resolved_data["tags"] = {"_raw": tags_val, "_fallback": resolved_fb} + n = node(unique_id, **resolved_data, config=self.config) if sftp: n.protocol = "sftp" @@ -203,50 +348,109 @@ class NodeService(BaseService): def move_node(self, src_id, dst_id, copy=False): """Move or copy a node.""" self._validate_node_name(dst_id) - + + if _supports_transactional_update(self.config): + def _mut(cfg): + try: + node_data = cfg.getitem_unlocked(src_id) + except (KeyError, TypeError): + node_data = None + if not node_data: + raise NodeNotFoundError(f"Source node '{src_id}' not found.") + if dst_id in cfg._getallnodes_unlocked(): + raise NodeAlreadyExistsError(f"Destination node '{dst_id}' already exists.") + new_uniques = cfg._explode_unique_unlocked(dst_id) + if not new_uniques: + raise InvalidConfigurationError(f"Invalid destination format '{dst_id}'.") + new_node_data = deepcopy(node_data) + new_node_data.update(new_uniques) + cfg._connections_add_unlocked(**new_node_data) + if not copy: + src_uniques = cfg._explode_unique_unlocked(src_id) + cfg._connections_del_unlocked(**src_uniques) + + self.config.transactional_update(_mut) + return + node_data = self.config.getitem(src_id) if not node_data: raise NodeNotFoundError(f"Source node '{src_id}' not found.") - + if dst_id in self.config._getallnodes(): raise NodeAlreadyExistsError(f"Destination node '{dst_id}' already exists.") - + new_uniques = self.config._explode_unique(dst_id) if not new_uniques: raise InvalidConfigurationError(f"Invalid destination format '{dst_id}'.") - + new_node_data = node_data.copy() new_node_data.update(new_uniques) - + self.config._connections_add(**new_node_data) - + if not copy: src_uniques = self.config._explode_unique(src_id) self.config._connections_del(**src_uniques) - + self.config._saveconfig(self.config.file) def bulk_add(self, ids, hosts, common_data): """Add multiple nodes with shared common configuration.""" + self._validate_node_payload(common_data) + + if _supports_transactional_update(self.config): + def _build_entries(cfg): + all_nodes = cfg._getallnodes_unlocked() + planned = [] + for i, uid in enumerate(ids): + if uid in all_nodes: + continue + try: + self._validate_node_name(uid) + except ReservedNameError: + continue + host = hosts[i] if i < len(hosts) else hosts[0] + uniques = cfg._explode_unique_unlocked(uid) + if not uniques: + continue + node_data = deepcopy(common_data) + node_data.pop("ids", None) + node_data.pop("location", None) + node_data.update(uniques) + node_data["host"] = host + node_data["type"] = "connection" + planned.append(node_data) + return planned + + with self.config._mutation_lock: + if not _build_entries(self.config): + return 0 + + def _mut(cfg): + entries = _build_entries(cfg) + for node_data in entries: + cfg._connections_add_unlocked(**node_data) + return len(entries) + + return self.config.transactional_update(_mut) + count = 0 all_nodes = self.config._getallnodes() - + for i, uid in enumerate(ids): if uid in all_nodes: continue - + try: self._validate_node_name(uid) except ReservedNameError: - # For bulk, we might want to just skip or log. - # CLI caller will handle if it wants to be strict. continue - + host = hosts[i] if i < len(hosts) else hosts[0] uniques = self.config._explode_unique(uid) if not uniques: continue - + node_data = common_data.copy() node_data.pop("ids", None) node_data.pop("location", None) @@ -256,13 +460,28 @@ class NodeService(BaseService): self.config._connections_add(**node_data) count += 1 - + if count > 0: self.config._saveconfig(self.config.file) return count def full_replace(self, connections, profiles): """Replace all connections and profiles with new data.""" + from ..auth_utils import SanitizedAuthError + + if _supports_transactional_update(self.config): + def _mut(cfg): + cfg.connections.clear() + cfg.connections.update(deepcopy(connections)) + cfg.profiles.clear() + cfg.profiles.update(deepcopy(profiles)) + + try: + self.config.transactional_update(_mut) + except (ValueError, TypeError, SanitizedAuthError) as exc: + raise InvalidConfigurationError(str(exc)) from None + return + self.config.connections = connections self.config.profiles = profiles self.config._saveconfig(self.config.file) diff --git a/connpy/services/profile_service.py b/connpy/services/profile_service.py index dc2dfeb..a927077 100644 --- a/connpy/services/profile_service.py +++ b/connpy/services/profile_service.py @@ -1,64 +1,212 @@ +from contextlib import suppress +from copy import deepcopy +from typing import ClassVar + from .base import BaseService -from .exceptions import ProfileNotFoundError, ProfileAlreadyExistsError, InvalidConfigurationError +from .exceptions import ( + InvalidConfigurationError, + ProfileAlreadyExistsError, + ProfileNotFoundError, +) + +MAX_PROFILE_DEPTH = 5 + + +def _supports_transactional_update(cfg): + return ( + cfg is not None + and not hasattr(cfg, "assert_called") + and callable(getattr(cfg, "transactional_update", None)) + ) + class ProfileService(BaseService): """Business logic for node profiles management.""" + _ALLOWED_KEYS: ClassVar[frozenset[str]] = frozenset({ + "host", + "options", + "logs", + "password", + "port", + "protocol", + "user", + "tags", + "jumphost", + "fallback", + }) + + @staticmethod + def _validate_profile_payload(data): + from ..auth_utils import ( + SanitizedAuthError, + is_compose_expression, + validate_auth_budget_tags, + validate_compose_syntax, + validate_fallback_entries, + ) + + try: + if "tags" in data and data["tags"] not in ("", None): + validate_auth_budget_tags(data["tags"]) + if "fallback" in data and data["fallback"] not in (None, "", []): + validate_fallback_entries(data["fallback"]) + pw = data.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) + except (ValueError, TypeError, SanitizedAuthError) as exc: + raise InvalidConfigurationError(str(exc)) from None + + def _get_profiles_snapshot(self): + lock = getattr(self.config, "_mutation_lock", None) + if lock is not None and not hasattr(self.config, "assert_called"): + with lock: + return deepcopy(self.config.profiles) + return deepcopy(self.config.profiles) + def list_profiles(self, filter_str=None): """List all profile names, optionally filtered.""" - profiles = list(self.config.profiles.keys()) + profiles = list(self._get_profiles_snapshot().keys()) case_sensitive = self.config.config.get("case", False) - + if filter_str: if not case_sensitive: f_str = filter_str.lower() return [p for p in profiles if f_str in p.lower()] - else: - return [p for p in profiles if filter_str in p] + return [p for p in profiles if filter_str in p] return profiles - def get_profile(self, name, resolve=True): + def get_profile(self, name, resolve=True, *, _visited=frozenset(), _depth=0): """Get the profile dictionary, optionally resolved.""" - profile = self.config.profiles.get(name) + if _depth >= MAX_PROFILE_DEPTH or name in _visited: + raise InvalidConfigurationError( + f"Circular or overly deep profile reference involving '{name}'." + ) + profiles = self._get_profiles_snapshot() + profile = profiles.get(name) if not profile: raise ProfileNotFoundError(f"Profile '{name}' not found.") - + if resolve: - return self.resolve_node_data(profile) + return self.resolve_node_data( + profile, + _visited=_visited | {name}, + _depth=_depth + 1, + _profiles_snapshot=profiles, + ) return profile def add_profile(self, name, data): """Add a new profile.""" + filtered_data = {k: deepcopy(v) for k, v in data.items() if k in self._ALLOWED_KEYS} + self._validate_profile_payload(filtered_data) + + if _supports_transactional_update(self.config): + def _mut(cfg): + if name in cfg.profiles: + raise ProfileAlreadyExistsError(f"Profile '{name}' already exists.") + cfg._profiles_add_unlocked(id=name, **filtered_data) + + self.config.transactional_update(_mut) + return + if name in self.config.profiles: raise ProfileAlreadyExistsError(f"Profile '{name}' already exists.") - - # Filter data to match _profiles_add signature and ensure id is passed - allowed_keys = {"host", "options", "logs", "password", "port", "protocol", "user", "tags", "jumphost"} - filtered_data = {k: v for k, v in data.items() if k in allowed_keys} - self.config._profiles_add(id=name, **filtered_data) self.config._saveconfig(self.config.file) - def resolve_node_data(self, node_data): - """Resolve profile references (@profile) in node data and handle inheritance.""" - resolved = node_data.copy() - - # 1. Identify all referenced profiles to support inheritance + def resolve_node_data( + self, + node_data, + *, + _visited=frozenset(), + _depth=0, + _profiles_snapshot=None, + ): + """Resolve profile references (@profile) in node data while preserving compose and scalar secrets.""" + from ..auth_utils import is_compose_expression + + if _depth > MAX_PROFILE_DEPTH: + raise InvalidConfigurationError("Profile inheritance exceeds maximum depth.") + + profiles = ( + _profiles_snapshot + if _profiles_snapshot is not None + else self._get_profiles_snapshot() + ) + resolved = deepcopy(node_data) + referenced_profiles = [] - for value in resolved.values(): - if isinstance(value, str) and value.startswith("@"): - referenced_profiles.append(value[1:]) + for key, value in resolved.items(): + if key == "fallback": + continue + if isinstance(value, str): + if not is_compose_expression(value) and value.startswith("@"): + ref = value[1:] + if key != "password" or ref in profiles: + referenced_profiles.append(ref) elif isinstance(value, list): for item in value: - if isinstance(item, str) and item.startswith("@"): - referenced_profiles.append(item[1:]) - - # 2. Resolve explicit references - for key, value in resolved.items(): + if ( + isinstance(item, str) + and not is_compose_expression(item) + and item.startswith("@") + ): + ref = item[1:] + if key != "password" or ref in profiles: + referenced_profiles.append(ref) + + def _get_resolved_ref(prof_name): + if prof_name in _visited or _depth >= MAX_PROFILE_DEPTH: + raise InvalidConfigurationError( + f"Circular or overly deep profile reference involving '{prof_name}'." + ) + prof_raw = profiles.get(prof_name) + if not prof_raw: + raise ProfileNotFoundError(f"Profile '{prof_name}' not found.") + return self.resolve_node_data( + prof_raw, + _visited=_visited | {prof_name}, + _depth=_depth + 1, + _profiles_snapshot=profiles, + ) + + for key, value in list(resolved.items()): + if key == "fallback": + continue + if key == "password": + if isinstance(value, str): + if is_compose_expression(value): + continue + if value.startswith("@") and value[1:] in profiles: + prof_obj = _get_resolved_ref(value[1:]) + resolved["password"] = prof_obj.get("password", "") + elif isinstance(value, list): + resolved_list = [] + for item in value: + if ( + isinstance(item, str) + and not is_compose_expression(item) + and item.startswith("@") + and item[1:] in profiles + ): + with suppress(ProfileNotFoundError): + prof_obj = _get_resolved_ref(item[1:]) + if "password" in prof_obj: + resolved_list.append(prof_obj["password"]) + else: + resolved_list.append(item) + resolved["password"] = resolved_list + continue + if isinstance(value, str) and value.startswith("@"): profile_name = value[1:] try: - profile = self.get_profile(profile_name, resolve=True) + profile = _get_resolved_ref(profile_name) resolved[key] = profile.get(key, "") except ProfileNotFoundError: resolved[key] = "" @@ -67,68 +215,85 @@ class ProfileService(BaseService): for item in value: if isinstance(item, str) and item.startswith("@"): profile_name = item[1:] - try: - profile = self.get_profile(profile_name, resolve=True) + with suppress(ProfileNotFoundError): + profile = _get_resolved_ref(profile_name) if "password" in profile: resolved_list.append(profile["password"]) - except ProfileNotFoundError: - pass else: resolved_list.append(item) resolved[key] = resolved_list - - # 3. Inheritance: Fill empty keys from the first referenced profile + if referenced_profiles: base_profile_name = referenced_profiles[0] - try: - base_profile = self.get_profile(base_profile_name, resolve=True) + with suppress(ProfileNotFoundError): + base_profile = _get_resolved_ref(base_profile_name) for key, value in base_profile.items(): - # Fill if key is missing or empty - if key not in resolved or resolved[key] == "" or resolved[key] == [] or resolved[key] is None: - resolved[key] = value - except ProfileNotFoundError: - pass + if key not in resolved or resolved[key] in ("", [], None): + resolved[key] = deepcopy(value) - # 4. Handle default protocol - if resolved.get("protocol") == "" or resolved.get("protocol") is None: + if resolved.get("protocol") in ("", None): try: - default_profile = self.get_profile("default", resolve=True) - resolved["protocol"] = default_profile.get("protocol", "ssh") + default_profile = _get_resolved_ref("default") + resolved["protocol"] = default_profile.get("protocol", "ssh") or "ssh" except ProfileNotFoundError: resolved["protocol"] = "ssh" - + return resolved def delete_profile(self, name): """Delete an existing profile, with safety checks.""" + if _supports_transactional_update(self.config): + def _mut(cfg): + if name not in cfg.profiles: + raise ProfileNotFoundError(f"Profile '{name}' not found.") + if name == "default": + raise InvalidConfigurationError("Cannot delete the 'default' profile.") + used_by = cfg._profileused_unlocked(name) + if used_by: + raise InvalidConfigurationError( + f"Profile '{name}' is used by nodes: {', '.join(used_by)}" + ) + cfg._profiles_del_unlocked(id=name) + + self.config.transactional_update(_mut) + return + if name not in self.config.profiles: raise ProfileNotFoundError(f"Profile '{name}' not found.") - if name == "default": raise InvalidConfigurationError("Cannot delete the 'default' profile.") - used_by = self.config._profileused(name) if used_by: - # We return the list of nodes using it so the UI can inform the user - raise InvalidConfigurationError(f"Profile '{name}' is used by nodes: {', '.join(used_by)}") - + raise InvalidConfigurationError( + f"Profile '{name}' is used by nodes: {', '.join(used_by)}" + ) self.config._profiles_del(id=name) self.config._saveconfig(self.config.file) def update_profile(self, name, data): """Update an existing profile.""" + if _supports_transactional_update(self.config): + def _mut(cfg): + if name not in cfg.profiles: + raise ProfileNotFoundError(f"Profile '{name}' not found.") + updated_data = deepcopy(cfg.profiles[name]) + updated_data.update(deepcopy(data)) + filtered_data = { + k: v for k, v in updated_data.items() if k in self._ALLOWED_KEYS + } + self._validate_profile_payload(filtered_data) + cfg._profiles_add_unlocked(id=name, **filtered_data) + + self.config.transactional_update(_mut) + return + if name not in self.config.profiles: raise ProfileNotFoundError(f"Profile '{name}' not found.") - - # Merge with existing data existing = self.get_profile(name, resolve=False) - updated_data = existing.copy() + updated_data = deepcopy(existing) updated_data.update(data) - - # Filter data to match _profiles_add signature - allowed_keys = {"host", "options", "logs", "password", "port", "protocol", "user", "tags", "jumphost"} - filtered_data = {k: v for k, v in updated_data.items() if k in allowed_keys} - + filtered_data = {k: v for k, v in updated_data.items() if k in self._ALLOWED_KEYS} + self._validate_profile_payload(filtered_data) self.config._profiles_add(id=name, **filtered_data) self.config._saveconfig(self.config.file) diff --git a/connpy/services/provider.py b/connpy/services/provider.py index a403d4d..140c048 100644 --- a/connpy/services/provider.py +++ b/connpy/services/provider.py @@ -1,5 +1,9 @@ +import inspect +from contextlib import suppress + from .exceptions import InvalidConfigurationError + class RemoteStub: def __getattr__(self, name): raise NotImplementedError( @@ -7,13 +11,17 @@ class RemoteStub: "Use local mode or wait for the gRPC implementation." ) + class ServiceProvider: """Dynamic service backend. Transparently provides local or remote services.""" - + def __init__(self, config, mode="local", remote_host=None): + from ..auth_utils import AuthAttemptTracker + self.mode = mode self.config = config self.remote_host = remote_host + self.auth_tracker = AuthAttemptTracker() self._system = None self._execution = None self._import_export = None @@ -29,47 +37,54 @@ class ServiceProvider: raise ValueError(f"Unknown service mode: {mode}") def _init_local(self): - from .node_service import NodeService - from .profile_service import ProfileService from .config_service import ConfigService - from .plugin_service import PluginService from .context_service import ContextService - + from .node_service import NodeService + from .plugin_service import PluginService + from .profile_service import ProfileService + self.nodes = NodeService(self.config) self.profiles = ProfileService(self.config) self.config_svc = ConfigService(self.config) self.plugins = PluginService(self.config) self.context = ContextService(self.config) - + def _init_remote(self): # Allow ConfigService to work locally so the user can revert the mode from .config_service import ConfigService from .context_service import ContextService + self.config_svc = ConfigService(self.config) self.context = ContextService(self.config) - + if not self.remote_host: raise InvalidConfigurationError("Remote host must be specified in remote mode") - import grpc import os + + import grpc + from ..grpc_layer.stubs import ( - NodeStub, ProfileStub, PluginStub, AIStub, - ExecutionStub, ImportExportStub, SystemStub, - ConfigStub, AuthClientInterceptor, AuthStub + AIStub, + AuthClientInterceptor, + AuthStub, + ConfigStub, + ExecutionStub, + ImportExportStub, + NodeStub, + PluginStub, + ProfileStub, + SystemStub, ) - + def get_token(): env_token = os.environ.get("CONNPY_TOKEN") if env_token: return env_token token_path = os.path.join(self.config.defaultdir, ".token") if os.path.exists(token_path): - try: - with open(token_path, "r") as f: - return f.read().strip() - except Exception: - pass + with suppress(OSError, UnicodeDecodeError), open(token_path) as f: + return f.read().strip() return None channel = grpc.insecure_channel(self.remote_host) @@ -105,7 +120,10 @@ class ServiceProvider: def execution(self): if self._execution is None and self.mode == "local": from .execution_service import ExecutionService - self._execution = ExecutionService(self.config) + if "attempt_tracker" in inspect.signature(ExecutionService.__init__).parameters: + self._execution = ExecutionService(self.config, attempt_tracker=self.auth_tracker) + else: + self._execution = ExecutionService(self.config) return self._execution @execution.setter diff --git a/connpy/tests/test_auth_fallback_chain.py b/connpy/tests/test_auth_fallback_chain.py new file mode 100644 index 0000000..d95abb3 --- /dev/null +++ b/connpy/tests/test_auth_fallback_chain.py @@ -0,0 +1,1801 @@ +"""Comprehensive TDD suite for spec-05-auth-fallback-chain.""" + +from __future__ import annotations + +import ast +import inspect +import json +import os +import pickle +import random +import string +import subprocess +import sys +import textwrap +import threading +import traceback +from pathlib import Path +from unittest.mock import MagicMock, patch + +import pytest + +from connpy import auth_utils +from connpy.auth_utils import ( + DEFAULT_TRACKER_TTL_SECONDS, + MAX_AUTH_STREAM_BYTES, + MAX_COMPOSE_EXPR_LEN, + MAX_COMPOSE_VARIANTS, + MAX_EOF_FALLBACK_ADVANCES, + MAX_FALLBACK_ENTRIES, + MAX_PROFILE_DEPTH, + SAFE_ERROR_TEMPLATES, + AuthAttemptTracker, + SanitizedAuthError, + extract_profile_refs_from_value, + has_insecure_hostkey_checking, + host_exact_re, + mask_node_secrets, + normalize_prompt_line, + parse_compose_expression, + raise_sanitized_auth_error, + resolve_fallback_entries, + sanitize_exception, + sanitize_log_message, + validate_fallback_entries, + validate_json_depth_and_size, +) +from connpy.cli.config_handler import ConfigHandler +from connpy.cli.forms import Forms +from connpy.cli.validators import Validators +from connpy.configfile import configfile +from connpy.connapp import connapp +from connpy.core import node as NodeClass +from connpy.core import nodes as NodesClass +from connpy.grpc_layer import connpy_pb2 +from connpy.grpc_layer.server import NodeServicer +from connpy.mcp_server import ConnpyMCPServer +from connpy.services.exceptions import InvalidConfigurationError +from connpy.services.execution_service import ExecutionService +from connpy.services.node_service import NodeService +from connpy.services.profile_service import ProfileService +from connpy.services.provider import ServiceProvider + + +class RaisingStdin: + """Test stdin stub that fails immediately if unattended execution attempts any read.""" + + def read(self, *args, **kwargs): + raise AssertionError("stdin.read() MUST NOT be invoked during unattended execution") + + def readline(self, *args, **kwargs): + raise AssertionError("stdin.readline() MUST NOT be invoked during unattended execution") + + def readlines(self, *args, **kwargs): + raise AssertionError("stdin.readlines() MUST NOT be invoked during unattended execution") + + def __iter__(self): + raise AssertionError("stdin.__iter__() MUST NOT be invoked during unattended execution") + + def isatty(self): + return False + + +class FakeClock: + """Deterministic monotonic clock for AuthAttemptTracker and timeout budget tests.""" + + def __init__(self, initial: float = 100.0) -> None: + self.now = float(initial) + + def __call__(self) -> float: + return self.now + + def advance(self, seconds: float) -> float: + self.now += float(seconds) + return self.now + + +def _make_mock_child( + *, + expect_fn, + child_fd: int = 99, +) -> MagicMock: + child = MagicMock() + child.child_fd = child_fd + child.before = b"" + child.after = b"" + child.buffer = b"" + child.logfile = None + child.logfile_send = None + child.logfile_read = None + child.expect.side_effect = expect_fn + child.readline.return_value = b"" + return child + + +def _make_config(tmp_path: Path, data: dict | None = None) -> configfile: + conf_path = tmp_path / "config.yaml" + key_path = tmp_path / ".osk" + cfg = configfile(conf=str(conf_path), key=str(key_path)) + if data: + def _init_mut(c): + if "config" in data: + c.config.update(data["config"]) + if "profiles" in data: + c.profiles.update(data["profiles"]) + if "connections" in data: + c.connections.update(data["connections"]) + + cfg.transactional_update(_init_mut) + return cfg + + +def _encrypt_with_config(cfg: configfile, plaintext: str) -> str: + return cfg.encrypt(plaintext, keyfile=cfg.key) + + +# --------------------------------------------------------------------------- +# 1. Configfile contract, _extract, profile refs & Profile/NodeService +# --------------------------------------------------------------------------- + + +def test_configfile_getitem_extract_and_core_node_contract(tmp_path: Path) -> None: + cfg = _make_config( + tmp_path, + { + "profiles": { + "default": { + "host": "", + "protocol": "ssh", + "port": "", + "user": "", + "password": "", + "options": "", + "logs": "", + "tags": "", + "jumphost": "", + "fallback": "", + }, + "tacacs": { + "host": "", + "protocol": "ssh", + "port": "", + "user": "tac_admin", + "password": "tac_secret_password", + "options": "", + "logs": "", + "tags": "", + "jumphost": "", + "fallback": "", + }, + "local_bak": { + "host": "", + "protocol": "ssh", + "port": "", + "user": "loc_admin", + "password": "loc_secret_password", + "options": "", + "logs": "", + "tags": "", + "jumphost": "", + "fallback": "", + }, + } + }, + ) + cfg._connections_add( + id="r1", + host="10.0.0.1", + user="@tacacs", + password="@tacacs", + tags={"env": "lab"}, + fallback=["@local_bak", {"user": "emerg", "password": "compose:@local_bak+'-9'"}], + ) + cfg._saveconfig(cfg.file) + + raw_item = cfg.getitem("r1", extract=False) + assert raw_item["fallback"] == [ + "@local_bak", + {"user": "emerg", "password": "compose:@local_bak+'-9'"}, + ] + + extracted = cfg.getitem("r1", extract=True) + assert "fallback" not in extracted + assert "_fallback" in extracted["tags"] + assert extracted["tags"]["env"] == "lab" + assert len(extracted["tags"]["_fallback"]) == 2 + assert extracted["tags"]["_fallback"][0]["user"] == "loc_admin" + assert extracted["tags"]["_fallback"][1]["password"] == "compose:@local_bak+'-9'" + + # Original stored YAML/dict tags are untouched + assert "_fallback" not in cfg.connections["r1"]["tags"] + + n = NodeClass("r1", **extracted, config=cfg) + assert [c.user for c in n._auth_candidates] == ["tac_admin", "loc_admin", "emerg"] + assert [c.is_fallback for c in n._auth_candidates] == [False, True, True] + + # _getallnodesfull(extract=True) does not mutate cfg.connections in-place + full = cfg._getallnodesfull(extract=True) + assert "r1" in full + assert "_fallback" not in cfg.connections["r1"]["tags"] + + +def test_profileused_detects_compose_fallback_and_ignores_scalar_at(tmp_path: Path) -> None: + cfg = _make_config( + tmp_path, + { + "profiles": { + "default": { + "host": "", + "protocol": "ssh", + "port": "", + "user": "", + "password": "", + "options": "", + "logs": "", + "tags": "", + "jumphost": "", + "fallback": "", + }, + "tac": { + "host": "", + "protocol": "ssh", + "port": "", + "user": "u1", + "password": "p1", + "options": "", + "logs": "", + "tags": "", + "jumphost": "", + "fallback": "", + }, + "pin": { + "host": "", + "protocol": "ssh", + "port": "", + "user": "u2", + "password": "99", + "options": "", + "logs": "", + "tags": "", + "jumphost": "", + "fallback": "", + }, + "indirect": { + "host": "", + "protocol": "ssh", + "port": "", + "user": "u3", + "password": "@tac", + "options": "", + "logs": "", + "tags": "", + "jumphost": "", + "fallback": "", + }, + } + }, + ) + cfg._connections_add(id="n_compose", host="10.0.0.1", password="compose:@tac+@pin") + cfg._connections_add( + id="n_fb", + host="10.0.0.2", + fallback=[{"user": "b", "password": "@pin"}], + ) + cfg._connections_add(id="n_indirect", host="10.0.0.3", password="@indirect") + cfg._connections_add(id="n_literal", host="10.0.0.4", password="@foo+bar") + cfg._saveconfig(cfg.file) + + assert sorted(cfg._profileused("pin")) == ["n_compose", "n_fb"] + assert sorted(cfg._profileused("tac")) == ["n_compose", "n_indirect"] + assert cfg._profileused("foo") == [] + assert extract_profile_refs_from_value("@foo+bar") == set() + + +def test_profile_service_and_node_service_preserve_scalar_and_fallback(tmp_path: Path) -> None: + cfg = _make_config(tmp_path) + p_svc = ProfileService(cfg) + n_svc = NodeService(cfg) + + p_svc.add_profile("ops", {"user": "ops_u", "password": "ops_p", "fallback": ["@default"]}) + n_svc.add_node( + "r_scalar", + {"host": "10.0.0.9", "user": "u", "password": "@foo+bar", "fallback": ["@ops"]}, + ) + n_svc.update_node("r_scalar", {"fallback": [{"user": "bak", "password": "secret123"}]}) + + details = n_svc.get_node_details("r_scalar") + assert details["password"] == "@foo+bar" + assert details["fallback"] == [{"user": "bak", "password": "secret123"}] + + resolved = p_svc.resolve_node_data(details) + assert resolved["password"] == "@foo+bar" + + with pytest.raises(InvalidConfigurationError): + n_svc.update_node( + "r_scalar", + {"fallback": [{"user": f"u{i}", "password": "p"} for i in range(6)]}, + ) + # Previous state rolled back cleanly + assert n_svc.get_node_details("r_scalar")["fallback"] == [{"user": "bak", "password": "secret123"}] + + with pytest.raises(InvalidConfigurationError): + p_svc.add_profile("bad_tag", {"tags": {"max_auth_prompts": 9}}) + + p_svc.add_profile("cyc_a", {"password": "@cyc_b"}) + p_svc.add_profile("cyc_b", {"password": "@cyc_a"}) + with pytest.raises(InvalidConfigurationError): + p_svc.get_profile("cyc_a", resolve=True) + + +# --------------------------------------------------------------------------- +# 2. Unattended Multi-Protocol State Machine, No Stdin, Logging & Anti-Spoof +# --------------------------------------------------------------------------- + + +@pytest.mark.parametrize("protocol", ["ssh", "sftp", "telnet", "kubectl", "docker", "ssm", "mosh"]) +def test_unattended_protocols_fallback_without_stdin( + tmp_path: Path, + monkeypatch: pytest.MonkeyPatch, + protocol: str, +) -> None: + monkeypatch.setattr(sys, "stdin", RaisingStdin()) + monkeypatch.setattr("connpy.core.sleep", lambda _s: None) + + events: list[tuple[str, str]] = [] + children_created: list[MagicMock] = [] + connection_num = 0 + + def _logger(level: str, msg: str) -> None: + events.append(("log", f"{level}:{msg}")) + + def _spawn_side_effect(*_args, **_kwargs): + nonlocal connection_num + connection_num += 1 + conn_id = connection_num + step = {"idx": 0} + + def _expect(pats, timeout=None): + s = step["idx"] + step["idx"] += 1 + if protocol in ("ssh", "sftp", "mosh"): + if conn_id == 1: + if s == 0: + child.before = b"" + child.after = b"Password:" + return 8 + child.before = b"" + child.after = b"Permission denied (publickey,password).\n" + return 2 + if s == 0: + child.before = b"" + child.after = b"Password:" + return 8 + if s == 1: + child.before = b"" + child.after = b"r1#" + return 9 + child.before = b"" + child.after = b"r1#" + return 0 + else: + # telnet / kubectl / docker / ssm: Username: -> Password: -> prompt or retry + if s == 0: + child.before = b"" + child.after = b"Username:" + return 0 + if s == 1: + child.before = b"" + child.after = b"Password:" + return 5 if protocol == "kubectl" else 8 + if s == 2: + child.before = b"" + child.after = b"Username:" + return 0 + if s == 3: + child.before = b"" + child.after = b"Password:" + return 5 if protocol == "kubectl" else 8 + if s == 4: + child.before = b"" + child.after = b"r1#" + return 6 if protocol == "kubectl" else 9 + child.before = b"" + child.after = b"r1#" + return 0 + + child = _make_mock_child(expect_fn=_expect) + + def _sendline(val=""): + text = val.decode("utf-8", "replace") if isinstance(val, bytes) else str(val) + events.append(("sendline", text)) + + child.sendline.side_effect = _sendline + children_created.append(child) + return child + + cfg = _make_config(tmp_path) + cfg._connections_add( + id="dev1", + host="i-0123456789abcdef0" if protocol == "ssm" else "router1", + protocol=protocol, + user="tac_user", + password="TACACS_PRIMARY_SECRET", + fallback=[{"user": "local_user", "password": "LOCAL_BACKUP_SECRET"}], + ) + cfg._saveconfig(cfg.file) + + with ( + patch("connpy.core.shutil.which", return_value="/usr/bin/mosh"), + patch("connpy.core.pexpect.spawn", side_effect=_spawn_side_effect), + patch("pexpect.fdpexpect.fdspawn", return_value=MagicMock()), + ): + svc = ExecutionService(cfg) + res = svc.run_commands("dev1", ["show version"], logger=_logger) + + assert res["dev1"]["status"] == 0 + assert res["dev1"]["fallback_used"] is True + assert svc.last_fallback_used == {"dev1": True} + + # Pre-AUTH_OK hygiene: logfile* remained None on child during _connect + for ch in children_created: + assert ch.logfile is None + assert ch.logfile_send is None + + # [AAA-FALLBACK] logged strictly BEFORE sendline of backup credentials, with zero secret leak + log_indices = [i for i, (kind, payload) in enumerate(events) if kind == "log" and "[AAA-FALLBACK]" in payload] + assert log_indices + backup_send_indices = [ + i + for i, (kind, payload) in enumerate(events) + if kind == "sendline" and payload in ("local_user", "LOCAL_BACKUP_SECRET") + ] + assert backup_send_indices + assert log_indices[0] < backup_send_indices[0] + for kind, payload in events: + if kind == "log": + assert "TACACS_PRIMARY_SECRET" not in payload + assert "LOCAL_BACKUP_SECRET" not in payload + + +def test_immutability_and_fail_open_stderr_logging( + tmp_path: Path, + monkeypatch: pytest.MonkeyPatch, + capsys: pytest.CaptureFixture[str], +) -> None: + monkeypatch.setattr(sys, "stdin", RaisingStdin()) + n = NodeClass( + "r_immut", + host="10.0.0.5", + user="prim_u", + password="PRIMARY_PW_VALUE", + fallback=[{"user": "bak_u", "password": "BACKUP_PW_VALUE"}], + ) + orig_candidates = n._auth_candidates + orig_hops = n._jumphost_hops + + conn_no = 0 + + def _spawn(*_args, **_kwargs): + nonlocal conn_no + conn_no += 1 + cno = conn_no + step = {"i": 0} + + def _expect(_pats, timeout=None): + idx = step["i"] + step["i"] += 1 + if cno == 1: + if idx == 0: + ch.after = b"Password:" + return 8 + ch.after = b"Permission denied (publickey,password).\n" + return 2 + if idx == 0: + ch.after = b"Password:" + return 8 + ch.after = b"r_immut#" + return 9 + + ch = _make_mock_child(expect_fn=_expect) + return ch + + def _exploding_logger(_lvl: str, _msg: str) -> None: + raise RuntimeError("logger sink exploded") + + with ( + patch("connpy.core.pexpect.spawn", side_effect=_spawn), + patch("pexpect.fdpexpect.fdspawn", return_value=MagicMock()), + ): + ok = n._connect(logger=_exploding_logger, unattended=True) + + assert ok is True + assert n.last_fallback_used is True + captured = capsys.readouterr() + assert "[AAA-FALLBACK]" in captured.err + assert "BACKUP_PW_VALUE" not in captured.err + assert "PRIMARY_PW_VALUE" not in captured.err + + # Strict immutability preserved + assert n.user == "prim_u" + assert n.password == ["PRIMARY_PW_VALUE"] + assert n._auth_candidates == orig_candidates + assert n._jumphost_hops == orig_hops + + +def test_allow_fallback_opt_out_and_no_creds_zero_stdin( + tmp_path: Path, + monkeypatch: pytest.MonkeyPatch, +) -> None: + monkeypatch.setattr(sys, "stdin", RaisingStdin()) + n = NodeClass( + "r_optout", + host="10.0.0.8", + user="u1", + password="pw1", + tags={"allow_fallback": False}, + fallback=[{"user": "bak", "password": "pw2"}], + ) + sent: list[str] = [] + + def _spawn(*_args, **_kwargs): + step = {"i": 0} + + def _expect(_pats, timeout=None): + idx = step["i"] + step["i"] += 1 + if idx == 0: + ch.after = b"Password:" + return 8 + ch.after = b"Permission denied (publickey,password).\n" + return 2 + + ch = _make_mock_child(expect_fn=_expect) + ch.sendline.side_effect = lambda v="": sent.append(str(v)) + return ch + + with patch("connpy.core.pexpect.spawn", side_effect=_spawn): + res = n._connect(unattended=True) + + assert res is not True + assert n.last_fallback_used is False + assert sent == ["pw1"] + + # Missing creds returns [NO_CREDS] without reading stdin + n_empty = NodeClass("r_nocreds", host="10.0.0.9", user="", password="") + with patch("connpy.core.pexpect.spawn", side_effect=_spawn): + res_empty = n_empty._connect(unattended=True) + assert "[NO_CREDS]" in str(res_empty) + + +def test_host_exact_re_and_jumphost_anti_spoofing(tmp_path: Path) -> None: + pat = host_exact_re("host1") + assert pat.search("user@host1's password:") + assert pat.search("[host1]: Password:") + assert not pat.search("user@host10's password:") + assert not pat.search("user@myhost1's password:") + assert not pat.search("user@host1.attacker.com's password:") + assert not pat.search("host1.attacker.com: Password:") + + # ANSI/CSI/OSC + CR overwrite stripping + dirty = b"\x1b]0;spoof\x07attacker@evil\r\x1b[31madmin@host1's password:\x1b[0m " + assert normalize_prompt_line(dirty, b"") == "admin@host1's password:" + + cfg = _make_config(tmp_path) + cfg._connections_add(id="j1", host="jump1", user="ju1", password="jp1") + cfg._connections_add(id="j2", host="jump2", user="ju2", password="jp2", jumphost="j1") + cfg._connections_add(id="tgt", host="host1", user="tu", password="tp", jumphost="j2") + cfg._saveconfig(cfg.file) + + # 1. Spoofing two hosts on the same line -> JUMP_AUTH_FAILED without sending + n2 = NodeClass("tgt", **cfg.getitem("tgt", extract=True), config=cfg) + sent_items: list[str] = [] + + def _spawn_conflict(*_a, **_kw): + ch = _make_mock_child( + expect_fn=lambda _p, timeout=None: ( + setattr(ch, "before", b"jump1 "), + setattr(ch, "after", b"host1 Password:"), + 8, + )[-1] + ) + ch.sendline.side_effect = lambda v="": sent_items.append(str(v)) + return ch + + with patch("connpy.core.pexpect.spawn", side_effect=_spawn_conflict): + r_conflict = n2._connect(unattended=True) + assert "[JUMP_AUTH_FAILED]" in str(r_conflict) + assert sent_items == [] + + # 2. Ambiguous generic prompt on single-hop fails closed unless opt-in + cfg._connections_add(id="tgt1", host="target1", user="tu", password="tp", jumphost="j1") + n_strict = NodeClass("tgt1", **cfg.getitem("tgt1", extract=True), config=cfg) + + def _spawn_generic(*_a, **_kw): + step = {"i": 0} + + def _exp(_p, timeout=None): + i = step["i"] + step["i"] += 1 + if i == 0: + ch.before = b"" + ch.after = b"Password:" + return 8 + if i == 1: + ch.before = b"" + ch.after = b"Password:" + return 8 + ch.before = b"" + ch.after = b"tgt1#" + return 9 + + ch = _make_mock_child(expect_fn=_exp) + ch.sendline.side_effect = lambda v="": sent_items.append(str(v)) + return ch + + with patch("connpy.core.pexpect.spawn", side_effect=_spawn_generic): + r_ambig = n_strict._connect(unattended=True) + assert "[JUMP_AUTH_FAILED]" in str(r_ambig) + + n_optin = NodeClass( + "tgt1", + **{**cfg.getitem("tgt1", extract=True), "tags": {"allow_ambiguous_single_hop_prompt": True}}, + config=cfg, + ) + sent_items.clear() + with ( + patch("connpy.core.pexpect.spawn", side_effect=_spawn_generic), + patch("pexpect.fdpexpect.fdspawn", return_value=MagicMock()), + ): + r_ok = n_optin._connect(unattended=True) + assert r_ok is True + assert sent_items == ["jp1", "tp", ""] + + # 3. Backwards hop regression (jump2 prompted then jump1 again) -> JUMP_AUTH_FAILED + def _spawn_regress(*_a, **_kw): + step = {"i": 0} + + def _exp(_p, timeout=None): + i = step["i"] + step["i"] += 1 + if i == 0: + ch.before = b"ju2@jump2's " + ch.after = b"password:" + return 8 + ch.before = b"ju1@jump1's " + ch.after = b"password:" + return 8 + + ch = _make_mock_child(expect_fn=_exp) + return ch + + with patch("connpy.core.pexpect.spawn", side_effect=_spawn_regress): + r_reg = n2._connect(unattended=True) + assert "[JUMP_AUTH_FAILED]" in str(r_reg) + + +def test_eof_budget_and_transport_error_no_fallback_and_stream_cap() -> None: + # Transport error must NOT trigger fallback + n_tr = NodeClass( + "r_ref", + host="10.0.0.10", + user="u", + password="p1", + fallback=[{"user": "u2", "password": "p2"}], + ) + calls = 0 + + def _spawn_refused(*_a, **_kw): + nonlocal calls + calls += 1 + ch = _make_mock_child( + expect_fn=lambda _p, timeout=None: ( + setattr(ch, "before", b""), + setattr(ch, "after", b"ssh: connect to host 10.0.0.10 port 22: Connection refused\n"), + 1, + )[-1] + ) + return ch + + with patch("connpy.core.pexpect.spawn", side_effect=_spawn_refused): + r_tr = n_tr._connect(unattended=True) + assert "Connection refused" in str(r_tr) + assert n_tr.last_fallback_used is False + assert calls == 1 + + # EOF advance cap: max 2 advances, 3rd EOF triggers EOF_BUDGET_EXHAUSTED + n_eof = NodeClass( + "r_eof", + host="10.0.0.11", + user="u", + password="p0", + fallback=[ + {"user": "u", "password": "p1"}, + {"user": "u", "password": "p2"}, + {"user": "u", "password": "p3"}, + {"user": "u", "password": "p4"}, + ], + ) + + def _spawn_eof(*_a, **_kw): + step = {"i": 0} + ch = None + + def _exp(_p, timeout=None): + i = step["i"] + step["i"] += 1 + if i == 0: + ch.after = b"Password:" + return 8 + ch.before = b"" + ch.after = b"" + return 11 + + ch = _make_mock_child(expect_fn=_exp) + return ch + + with patch("connpy.core.pexpect.spawn", side_effect=_spawn_eof): + r_eof = n_eof._connect(unattended=True) + assert "[EOF_BUDGET_EXHAUSTED]" in str(r_eof) + + # Pre-auth stream cap MAX_AUTH_STREAM_BYTES = 16384 in unattended mode + def _spawn_big_buf(*_a, **_kw): + ch = _make_mock_child( + expect_fn=lambda _p, timeout=None: ( + setattr(ch, "before", b"A" * (MAX_AUTH_STREAM_BYTES + 1)), + setattr(ch, "after", b"Password:"), + 8, + )[-1] + ) + return ch + + with patch("connpy.core.pexpect.spawn", side_effect=_spawn_big_buf): + r_big = n_eof._connect(unattended=True) + assert "16384" in str(r_big) + + +# --------------------------------------------------------------------------- +# 3. AuthAttemptTracker, LRU Anti-Flood Eviction & Concurrency +# --------------------------------------------------------------------------- + + +def test_tracker_anti_flood_eviction_ttl_and_concurrency() -> None: + clock = FakeClock(1000.0) + tracker = AuthAttemptTracker( + ttl_seconds=DEFAULT_TRACKER_TTL_SECONDS, + max_entries=10, + default_max_host_attempts=2, + clock=clock, + ) + + # Block target host A + assert tracker.try_acquire_host("blocked.host", "22", "ssh", limit=2) is True + assert tracker.try_acquire_host("blocked.host", "22", "ssh", limit=2) is True + assert tracker.try_acquire_host("blocked.host", "22", "ssh", limit=2) is False + + # Flood with 50 distinct unblocked hosts (limit=5, only 1 attempt each) + for i in range(50): + assert tracker.try_acquire_host(f"flood-{i}", "22", "ssh", limit=5) is True + + assert tracker.entry_count() <= 10 + # Blocked entry MUST NOT have been evicted by unblocked flood + assert tracker.remaining_host("blocked.host", "22", "ssh", limit=2) == 0 + assert tracker.try_acquire_host("blocked.host", "22", "ssh", limit=2) is False + + # Advance past TTL -> blocked entry expires cleanly + clock.advance(DEFAULT_TRACKER_TTL_SECONDS + 1) + assert tracker.remaining_host("blocked.host", "22", "ssh", limit=2) == 2 + assert tracker.try_acquire_host("blocked.host", "22", "ssh", limit=2) is True + + # Multi-host isolation + global user budget + t2 = AuthAttemptTracker(default_max_global_user_attempts=2) + assert t2.try_acquire_user("shared_admin") is True + assert t2.try_acquire_user("shared_admin") is True + assert t2.try_acquire_user("shared_admin") is False + assert t2.try_acquire_host("host_a", "22", "ssh", limit=2) is True + assert t2.try_acquire_host("host_b", "22", "ssh", limit=2) is True + + # Concurrent barrier test + t_conc = AuthAttemptTracker(default_max_host_attempts=5) + barrier = threading.Barrier(12) + results: list[bool] = [] + lock = threading.Lock() + + def _worker(): + barrier.wait(timeout=5) + ok = t_conc.try_acquire_host("conc.host", "22", "ssh", limit=5) + with lock: + results.append(ok) + + threads = [threading.Thread(target=_worker) for _ in range(12)] + for t in threads: + t.start() + for t in threads: + t.join(timeout=5) + + assert sum(1 for r in results if r) == 5 + assert len(results) == 12 + + +def test_max_attempts_does_not_multiply_post_auth_and_budget_codes() -> None: + tracker = AuthAttemptTracker(default_max_host_attempts=2) + sent_pw: list[str] = [] + + n = NodeClass( + "r_budget", + host="10.0.0.20", + user="u", + password="p1", + fallback=[{"user": "u", "password": "p2"}, {"user": "u", "password": "p3"}], + ) + + def _spawn_reject(*_a, **_kw): + step = {"i": 0} + + def _exp(_p, timeout=None): + i = step["i"] + step["i"] += 1 + if i == 0: + ch.after = b"Password:" + return 8 + ch.after = b"Permission denied (publickey,password).\n" + return 2 + + ch = _make_mock_child(expect_fn=_exp) + ch.sendline.side_effect = lambda v="": sent_pw.append(str(v)) + return ch + + with patch("connpy.core.pexpect.spawn", side_effect=_spawn_reject): + res = n._connect(max_attempts=3, unattended=True, attempt_tracker=tracker) + + assert "[HOST_BUDGET_EXHAUSTED]" in str(res) + # Sent ONLY p1 and p2 (total=2); stopped cleanly before p3 without max_attempts=3 multiplying attempts + assert sent_pw == ["p1", "p2"] + + # Global user budget code check + t_user = AuthAttemptTracker() + assert t_user.try_acquire_user("locked_u", limit=1) is True + n_u = NodeClass( + "r_ub", + host="10.0.0.21", + user="locked_u", + password="p1", + tags={"max_global_user_attempts": 1}, + ) + with patch("connpy.core.pexpect.spawn", side_effect=_spawn_reject): + res_u = n_u._connect(unattended=True, attempt_tracker=t_user) + assert "[USER_BUDGET_EXHAUSTED]" in str(res_u) + + +# --------------------------------------------------------------------------- +# 4. Golden Rule Sanitization, Pickle, Dynamic Traceback & AST Allowlist +# --------------------------------------------------------------------------- + + +def test_sanitized_auth_error_pickle_and_dynamic_traceback_clearing() -> None: + err = SanitizedAuthError(SAFE_ERROR_TEMPLATES["AUTH_FAILED"], code="AUTH_FAILED") + assert err.__suppress_context__ is True + restored = pickle.loads(pickle.dumps(err)) + assert isinstance(restored, SanitizedAuthError) + assert restored.code == "AUTH_FAILED" + assert str(restored) == SAFE_ERROR_TEMPLATES["AUTH_FAILED"] + + secret_token = "TopSecretValue!#9911" + + def _boom(): + local_password = secret_token + raise ValueError(f"boom with {local_password}") + + orig_exc: BaseException | None = None + sanitized_err: SanitizedAuthError | None = None + try: + try: + _boom() + except ValueError as exc: + orig_exc = exc + raise_sanitized_auth_error(exc, code="AUTH_FAILED", secrets=[secret_token]) + except SanitizedAuthError as captured: + sanitized_err = captured + + assert sanitized_err is not None + assert sanitized_err.__cause__ is None + assert sanitized_err.__context__ is None + assert sanitized_err.__suppress_context__ is True + formatted = "".join( + traceback.format_exception(type(sanitized_err), sanitized_err, sanitized_err.__traceback__) + ) + assert secret_token not in formatted + assert "ValueError" not in formatted + + # Original exception's frames were cleared cleanly + assert orig_exc is not None + assert orig_exc.__traceback__ is None + + # Exiting except before raising also keeps __context__ None at VM level + pending_err: SanitizedAuthError | None = None + captured_outer: SanitizedAuthError | None = None + try: + try: + _boom() + except ValueError as exc: + pending_err = sanitize_exception(exc, code="AUTH_FAILED", secrets=[secret_token]) + if pending_err is not None: + pending_err.__context__ = None + raise pending_err from None + except SanitizedAuthError as outer: + captured_outer = outer + assert captured_outer is not None + assert captured_outer.__context__ is None + assert captured_outer.__cause__ is None + assert captured_outer.__suppress_context__ is True + + +def test_ast_allowlist_and_no_raw_traceback_or_stdin_in_auth_surfaces() -> None: + forbidden_tb_attrs = {"print_exc", "format_exc", "format_exception", "print_tb"} + targets: list[tuple[str, object]] = [ + ("auth_utils", auth_utils), + ("core._spawn_child", NodeClass._spawn_child), + ("core._connect", NodeClass._connect), + ("core.run", NodeClass.run), + ("core.test", NodeClass.test), + ("ExecutionService", ExecutionService), + ("NodeServicer.interact_node", NodeServicer.interact_node), + ("ConnpyMCPServer.run_commands", ConnpyMCPServer.run_commands), + ("ConnpyMCPServer.test_commands", ConnpyMCPServer.test_commands), + ] + + for label, obj in targets: + target_obj = getattr(obj, "func", obj) + if inspect.isfunction(target_obj) or inspect.ismethod(target_obj): + target_obj = inspect.unwrap(target_obj) + src = inspect.getsource(target_obj) + tree = ast.parse(textwrap.dedent(src)) + for node_ast in ast.walk(tree): + if isinstance(node_ast, ast.Attribute) and node_ast.attr in forbidden_tb_attrs: + pytest.fail(f"Forbidden traceback call '.{node_ast.attr}' found in {label}") + if ( + isinstance(node_ast, ast.Raise) + and node_ast.cause is not None + and not (isinstance(node_ast.cause, ast.Constant) and node_ast.cause.value is None) + ): + pytest.fail(f"Unsafe `raise ... from ` found in {label}") + if ( + label in ("core._connect", "core.run", "core.test") + and isinstance(node_ast, ast.Attribute) + and node_ast.attr == "stdin" + ): + pytest.fail(f"Prohibited `stdin` access found in {label}") + + +# --------------------------------------------------------------------------- +# 5. EBNF compose: Golden Cases + Fuzz, Lock Order & Atomic Transactions +# --------------------------------------------------------------------------- + + +def test_compose_ebnf_golden_cases_and_fuzz(tmp_path: Path) -> None: + cfg = _make_config( + tmp_path, + { + "profiles": { + "p1": {"password": "Base"}, + "p2": {"password": ["A", "B"]}, + "pin": {"password": "99"}, + "self_ref": {"password": "compose:@self_ref+1"}, + "cyc_a": {"password": "compose:@cyc_b+1"}, + "cyc_b": {"password": "compose:@cyc_a+2"}, + "d5": {"password": "compose:@d4+5"}, + "d4": {"password": "compose:@d3+4"}, + "d3": {"password": "compose:@d2+3"}, + "d2": {"password": "compose:@d1+2"}, + "d1": {"password": "X"}, + "d6": {"password": "compose:@d5+6"}, + "empty_pw": {"password": ""}, + } + }, + ) + rsa_blob = _encrypt_with_config(cfg, "RSA_SECRET") + + def _dec(blob: str) -> str: + return NodeClass("x", host="1.1.1.1", config=cfg)._passtx([blob])[0] + + # Golden cases + assert parse_compose_expression(f"compose:{rsa_blob}+'-suffix'", profiles=cfg.profiles, decrypt_fn=_dec) == [ + "RSA_SECRET-suffix" + ] + assert parse_compose_expression("compose:@@foo+'-'+'bar'", profiles=cfg.profiles) == ["@foo-bar"] + assert parse_compose_expression("compose:'@foo+bar'", profiles=cfg.profiles) == ["@foo+bar"] + assert parse_compose_expression("compose:@@", profiles=cfg.profiles) == ["@"] + assert parse_compose_expression("compose:@p1+@p2+@pin", profiles=cfg.profiles) == [ + "BaseA99", + "BaseB99", + ] + assert parse_compose_expression("compose:'x+y'+\" z\"", profiles=cfg.profiles) == ["x+y z"] + assert parse_compose_expression("compose:@p1+@p1", profiles=cfg.profiles) == ["BaseBase"] + assert parse_compose_expression("compose:@d5", profiles=cfg.profiles) == ["X2345"] + + # Too deep (depth 6 > 5) + with pytest.raises(SanitizedAuthError): + parse_compose_expression("compose:@d6", profiles=cfg.profiles) + + # Cycle + with pytest.raises(SanitizedAuthError): + parse_compose_expression("compose:@cyc_a", profiles=cfg.profiles) + + # Empty profile -> NO_CREDS + with pytest.raises(SanitizedAuthError) as exc_info: + parse_compose_expression("compose:@empty_pw+1", profiles=cfg.profiles) + assert exc_info.value.code == "NO_CREDS" + + # Variants cap > 10 + cfg.profiles["many"] = {"password": [f"v{i}" for i in range(6)]} + with pytest.raises(SanitizedAuthError): + parse_compose_expression("compose:@many+@p2", profiles=cfg.profiles) + + # Max expr len > 4096 + with pytest.raises(SanitizedAuthError): + parse_compose_expression("compose:" + ("a" * (MAX_COMPOSE_EXPR_LEN + 1)), profiles=cfg.profiles) + + # Property / fuzz test: random valid/malformed expressions never hang (linear O(n)) + rng = random.Random(42) + alphabet = string.ascii_letters + "+@'\" \\" + for _ in range(150): + expr = "compose:" + "".join(rng.choice(alphabet) for _ in range(rng.randint(0, 64))) + try: + res = parse_compose_expression(expr, profiles=cfg.profiles) + assert isinstance(res, list) + assert len(res) <= MAX_COMPOSE_VARIANTS + except SanitizedAuthError: + pass + + # Scalar password WITHOUT compose: NEVER triggers parser + n_scalar = NodeClass("rs", host="10.0.0.1", user="u", password="@foo+bar", config=cfg) + assert n_scalar.password == ["@foo+bar"] + n_admin = NodeClass("ra", host="10.0.0.2", user="u", password="Admin+@2026", config=cfg) + assert n_admin.password == ["Admin+@2026"] + + +def test_lock_ordering_no_deadlock_atomic_rollback_and_cache_dirty(tmp_path: Path) -> None: + cfg = _make_config(tmp_path) + p_svc = ProfileService(cfg) + p_svc.add_profile("p1", {"user": "u1", "password": "pw1"}) + + # 1. Verify @MethodHook on _saveconfig does NOT hold _mutation_lock (no deadlock under Barrier) + barrier = threading.Barrier(2) + errors: list[BaseException] = [] + + def _hook(self_cfg, conf_path, result=None): + barrier.wait(timeout=5) + _ = p_svc.get_profile("p1", resolve=False) + return result + + cfg._saveconfig.register_post_hook(_hook) + + def _concurrent_reader(): + try: + barrier.wait(timeout=5) + for _ in range(5): + p_svc.list_profiles() + cfg._getallnodes() + except BaseException as exc: # noqa: BLE001 + errors.append(exc) + + t = threading.Thread(target=_concurrent_reader) + t.start() + p_svc.add_profile("p2", {"user": "u2", "password": "pw2"}) + t.join(timeout=5) + assert errors == [] + + # Clear hook for subsequent checks + if _hook in cfg._method_post_hooks.get("_saveconfig", []): + cfg._method_post_hooks["_saveconfig"].remove(_hook) + + # 2. In-place rollback preserves id(cfg.connections) & YAML on KeyboardInterrupt / IOError + orig_conn_id = id(cfg.connections) + cfg._connections_add(id="stable", host="1.1.1.1") + cfg._saveconfig(cfg.file) + + with pytest.raises(KeyboardInterrupt): + def _abort(c): + c.connections["temp"] = {"host": "2.2.2.2", "type": "connection"} + raise KeyboardInterrupt() + + cfg.transactional_update(_abort) + assert id(cfg.connections) == orig_conn_id + assert "temp" not in cfg.connections + + with patch("connpy.configfile.yaml.dump", side_effect=OSError("disk full")), pytest.raises(OSError): + cfg.transactional_update( + lambda c: c._connections_add_unlocked(id="failed_node", host="9.9.9.9") + ) + assert id(cfg.connections) == orig_conn_id + assert "failed_node" not in cfg.connections + + # 3. Symlink refused + file permissions 0o600 + sym_target = tmp_path / "evil.yaml" + sym_target.write_text("orig", encoding="utf-8") + sym_conf = tmp_path / "sym_config.yaml" + sym_conf.symlink_to(sym_target) + with pytest.raises(OSError): + cfg._atomic_write_text_unlocked(str(sym_conf), lambda fp: fp.write("pwned")) + assert sym_target.read_text(encoding="utf-8") == "orig" + assert (os.stat(cfg.file).st_mode & 0o777) == 0o600 + + # 4. Post-replace cache write failure sets _cache_dirty=True WITHOUT reverting RAM; reads don't loop + with patch.object(cfg, "_generate_nodes_cache_unlocked", side_effect=OSError("cache disk err")): + cfg.transactional_update(lambda c: c._connections_add_unlocked(id="kept", host="3.3.3.3")) + assert cfg._cache_dirty is True + assert "kept" in cfg.connections + assert cfg.getitem("kept")["host"] == "3.3.3.3" # read succeeds & keeps _cache_dirty without infinite loop + assert cfg._cache_dirty is True + assert cfg.refresh_cache() is True + assert cfg._cache_dirty is False + + # 5. Strict border validation in YAML _validate_config + base_doc = {"config": {"case": False}, "connections": {}, "profiles": {}} + assert cfg._validate_config(base_doc) is True + for bad_prompts in (0, -1, True, False, "2", 6): + bad = {"config": {"max_auth_prompts": bad_prompts}, "connections": {}, "profiles": {}} + assert cfg._validate_config(bad) is False + for bad_total in (0, -1, True, "5", 11): + bad = {"config": {"max_total_auth_attempts": bad_total}, "connections": {}, "profiles": {}} + assert cfg._validate_config(bad) is False + assert cfg._validate_config({"config": {"disable_fallback_on_insecure_hostkey": "yes"}, "connections": {}, "profiles": {}}) is False + + +# --------------------------------------------------------------------------- +# 6. Fase C Surfaces: CLI, Validators, Forms, gRPC, MCP & Security Policy +# --------------------------------------------------------------------------- + + +def test_cli_validators_forms_and_config_handler_borders( + tmp_path: Path, + monkeypatch: pytest.MonkeyPatch, +) -> None: + monkeypatch.setenv("HOME", str(tmp_path)) + cfg = _make_config( + tmp_path, + { + "profiles": { + "default": { + "host": "", + "protocol": "ssh", + "port": "", + "user": "", + "password": "", + "options": "", + "logs": "", + "tags": "", + "jumphost": "", + "fallback": "", + }, + "ops": { + "host": "", + "protocol": "ssh", + "port": "", + "user": "u", + "password": "p", + "options": "", + "logs": "", + "tags": "", + "jumphost": "", + "fallback": "", + }, + } + }, + ) + app = MagicMock() + app.config = cfg + app.profiles = list(cfg.profiles.keys()) + app.nodes_list = [] + app.services = ServiceProvider(cfg) + + val = Validators(app) + assert val.fallback_validation({}, "@ops") is True + assert val.fallback_validation({}, "compose:@ops+'1'") is True + assert val.fallback_validation({}, '[{"user": "u", "password": "p"}]') is True + assert val.tags_validation({}, "{'max_auth_prompts': 5, 'allow_fallback': False}") is True + + import inquirer + + with pytest.raises(inquirer.errors.ValidationError): + val.fallback_validation({}, json.dumps([{"user": f"u{i}", "password": "p"} for i in range(6)])) + with pytest.raises(inquirer.errors.ValidationError): + val.fallback_validation({}, "x" * 9000) + with pytest.raises(inquirer.errors.ValidationError): + val.tags_validation({}, "{'max_auth_prompts': 0}") + with pytest.raises(inquirer.errors.ValidationError): + val.tags_validation({}, json.dumps({"a": {"b": {"c": {"d": {"e": 1}}}}})) + + # CLI argparse + ConfigHandler border checks + 0 reset + app_real = connapp(cfg) + parser, _ = app_real.get_parser() + + parsed = parser.parse_args(["config", "--max-auth-prompts", "4"]) + assert parsed.command == "max_auth_prompts" + assert parsed.data == [4] + + handler = ConfigHandler(app_real) + handler.dispatch(parsed) + assert cfg.config["max_auth_prompts"] == 4 + + # 0 unsets key + parsed_zero = parser.parse_args(["config", "--max-auth-prompts", "0"]) + handler.dispatch(parsed_zero) + assert "max_auth_prompts" not in cfg.config + + for bad_arg in (-1, 6, True, "abc"): + ns = MagicMock(command="max_auth_prompts", data=[bad_arg]) + with pytest.raises(InvalidConfigurationError): + handler.dispatch(ns) + + parsed_hk = parser.parse_args(["config", "--disable-fallback-on-insecure-hostkey", "true"]) + handler.dispatch(parsed_hk) + assert cfg.config["disable_fallback_on_insecure_hostkey"] is True + + # Verify insecure hostkey blocks fallback only when flag is enabled + n_hk = NodeClass( + "r_hk", + host="10.0.0.50", + user="u", + password="p1", + options="-o StrictHostKeyChecking=no", + fallback=[{"user": "u", "password": "p2"}], + config=cfg, + ) + assert has_insecure_hostkey_checking(n_hk.options, n_hk.tags) is True + sent_hk: list[str] = [] + + def _spawn_hk(*_a, **_kw): + step = {"i": 0} + + def _exp(_p, timeout=None): + i = step["i"] + step["i"] += 1 + if i == 0: + ch.after = b"Password:" + return 8 + ch.after = b"Permission denied (publickey,password).\n" + return 2 + + ch = _make_mock_child(expect_fn=_exp) + ch.sendline.side_effect = lambda v="": sent_hk.append(str(v)) + return ch + + with patch("connpy.core.pexpect.spawn", side_effect=_spawn_hk): + res_hk = n_hk._connect(unattended=True) + assert res_hk is not True + assert sent_hk == ["p1"] + + # Forms edits include fallback cleanly + forms = Forms(app) + with patch("inquirer.prompt") as m_prompt: + m_prompt.return_value = { + "ids": "r_bulk", + "location": "", + "host": "10.1.1.1", + "protocol": "ssh", + "port": "22", + "options": "", + "logs": "", + "tags": "{'max_auth_prompts': 2}", + "jumphost": "", + "user": "u1", + "password": "No Password", + "fallback": '[{"user": "u2", "password": "p2"}]', + } + bulk_ans = forms.questions_bulk() + assert bulk_ans["fallback"] == [{"user": "u2", "password": "p2"}] + + +def test_mcp_masking_limits_sanitization_and_grpc_interact(tmp_path: Path) -> None: + # Closed secret masking check + cycle + non-str keys + cyc: dict[object, object] = { + "password": "p", + "fallback": [{"user": "u", "password": "secret"}, "@local"], + "api_token": "tok", + "custom_secret": "s", + "tokens_per_min": 100, + "max_tokens": 500, + "token_count": 12, + "ssh_key_path": "/home/u/.ssh/id_ed25519", + "keyfile": "/path/.osk", + 42: "ok", + } + cyc["self"] = cyc + masked = mask_node_secrets(cyc) + assert masked["password"] == "***" + assert masked["api_token"] == "***" + assert masked["custom_secret"] == "***" + assert masked["fallback"] == [{"user": "u", "password": "***"}, "***"] + assert masked["tokens_per_min"] == 100 + assert masked["max_tokens"] == 500 + assert masked["token_count"] == 12 + assert masked["ssh_key_path"] == "/home/u/.ssh/id_ed25519" + assert masked["keyfile"] == "/path/.osk" + assert masked[42] == "ok" + assert masked["self"] == "***" + + cfg = _make_config(tmp_path) + svc_provider = ServiceProvider(cfg) + mcp = ConnpyMCPServer(config=cfg, services=svc_provider, trust=True) + + assert mcp._mask_secrets({"password": "abc", "tokens_per_min": 5}) == { + "password": "***", + "tokens_per_min": 5, + } + + # Add/update size, depth & fallback count enforcement + too_deep = json.dumps({"a": {"b": {"c": {"d": {"e": 1}}}}}) + res_add = mcp.add_node("r1", {"host": "1.1.1.1", "tags": too_deep}) + assert res_add["success"] is False + + res_cmd_big = mcp.run_commands("r1", "a" * 9000) + assert res_cmd_big["success"] is False + + # Sanitized error on unexpected exception during run_commands + secret_pw = "UltraSecretMCP#77" + with patch.object( + svc_provider.execution, + "run_commands", + side_effect=RuntimeError(f"boom {secret_pw}"), + ): + err_run = mcp.run_commands("r1", ["show ver"]) + assert err_run["success"] is False + assert secret_pw not in err_run["error"] + assert "[AUTH_FAILED]" in err_run["error"] + + # fallback_used propagation in MCP test_commands + with patch.object(svc_provider.execution, "test_commands", return_value={"r1": {"ok": True}}): + svc_provider.execution.last_fallback_used = {"r1": True} + test_res = mcp.test_commands("r1", ["show ver"], ["ok"]) + assert test_res["success"] is True + assert test_res["fallback_used"] is True + + # gRPC NodeServicer.interact_node validation on connection_params_json + class _AbortError(Exception): + pass + + servicer = NodeServicer(provider=svc_provider) + ctx = MagicMock() + ctx.abort.side_effect = _AbortError("aborted") + + bad_req = connpy_pb2.InteractRequest( + id="dyn", + connection_params_json=json.dumps({"host": "1.1.1.1", "fallback": [{"user": f"u{i}", "password": "p"} for i in range(6)]}), + ) + with pytest.raises(_AbortError, match="aborted"): + list(servicer.interact_node(iter([bad_req]), ctx)) + ctx.abort.assert_called_with( + pytest.importorskip("grpc").StatusCode.INVALID_ARGUMENT, + "Fallback exceeds maximum of 5 entries", + ) + + +def test_compose_syntax_helpers_and_json_depth_limits() -> None: + with pytest.raises(ValueError): + validate_json_depth_and_size("[" * 6 + "1" + "]" * 6, max_depth=4) + with pytest.raises(ValueError): + validate_fallback_entries([{"user": "u", "password": "p"}] * (MAX_FALLBACK_ENTRIES + 1)) + assert resolve_fallback_entries(None, {}) == [] + assert sanitize_log_message("\x1b[31mhi\x07\npw=SecRet123", secrets=["SecRet123"]) == "hipw=***" + assert sanitize_exception(ValueError("bad pw"), secrets=["bad"]).code == "AUTH_FAILED" + assert MAX_PROFILE_DEPTH == 5 + assert MAX_COMPOSE_VARIANTS == 10 + assert MAX_EOF_FALLBACK_ADVANCES == 2 + + +def test_nodes_group_fallback_tracking(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.setattr(sys, "stdin", RaisingStdin()) + monkeypatch.setattr("connpy.core.sleep", lambda _s: None) + cfg = _make_config(tmp_path) + cfg._connections_add( + id="n1", + host="10.0.0.101", + user="tac", + password="p1", + fallback=[{"user": "loc", "password": "p2"}], + ) + cfg._saveconfig(cfg.file) + + conn_no = 0 + + def _spawn(*_a, **_kw): + nonlocal conn_no + conn_no += 1 + cno = conn_no + step = {"i": 0} + + def _exp(_p, timeout=None): + i = step["i"] + step["i"] += 1 + if cno == 1: + if i == 0: + ch.after = b"Password:" + return 8 + ch.after = b"Permission denied (publickey,password).\n" + return 2 + if i == 0: + ch.after = b"Password:" + return 8 + if i == 1: + ch.after = b"n1#" + return 9 + ch.after = b"n1#" + return 0 + + ch = _make_mock_child(expect_fn=_exp) + return ch + + extracted = cfg.getitems(["n1"], extract=True) + executor = NodesClass(extracted, config=cfg) + with ( + patch("connpy.core.pexpect.spawn", side_effect=_spawn), + patch("pexpect.fdpexpect.fdspawn", return_value=MagicMock()), + ): + executor.run(["show version"], parallel=1) + assert executor.status["n1"] == 0 + assert executor.fallback_used == {"n1": True} + + +def test_signoff_fallback_inheritance_fail_closed_json_masking_and_atomic_budgets( + tmp_path: Path, +) -> None: + # 1a. @profile in fallback with own credentials + sub-fallback emits own first then sub-entries + profiles_combo = { + "tac_combo": {"user": "tac_u", "password": "tac_p", "fallback": ["@bak1", "@bak2"]}, + "bak1": {"user": "b1_u", "password": "b1_p"}, + "bak2": {"user": "b2_u", "password": "b2_p"}, + "empty_chain": {"user": "", "password": "", "fallback": ["@bak1"]}, + "cyc_a": {"user": "@cyc_b", "password": "p"}, + "cyc_b": {"user": "@cyc_a", "password": "p"}, + } + res_combo = resolve_fallback_entries(["@tac_combo"], profiles_combo) + assert res_combo == [ + {"user": "tac_u", "password": "tac_p"}, + {"user": "b1_u", "password": "b1_p"}, + {"user": "b2_u", "password": "b2_p"}, + ] + assert resolve_fallback_entries(["@empty_chain"], profiles_combo) == [ + {"user": "b1_u", "password": "b1_p"} + ] + + # Combined own + sub-fallback > MAX_FALLBACK_ENTRIES (5) fails closed + profiles_cap = { + "own_plus_4": { + "user": "u0", + "password": "p0", + "fallback": [{"user": f"u{i}", "password": f"p{i}"} for i in range(1, 5)], + }, + "own_plus_5": { + "user": "u0", + "password": "p0", + "fallback": [{"user": f"u{i}", "password": f"p{i}"} for i in range(1, 6)], + }, + } + assert len(resolve_fallback_entries(["@own_plus_4"], profiles_cap)) == MAX_FALLBACK_ENTRIES + with pytest.raises(ValueError, match="exceeds maximum"): + resolve_fallback_entries(["@own_plus_5"], profiles_cap) + + # 1b. configfile.getitem(extract=True) inherits fallback from primary @profile when node omits it + cfg = _make_config( + tmp_path, + { + "profiles": { + "default": { + "host": "", + "protocol": "ssh", + "port": "", + "user": "", + "password": "", + "options": "", + "logs": "", + "tags": "", + "jumphost": "", + "fallback": "", + }, + "tacacs": { + "host": "", + "protocol": "ssh", + "port": "", + "user": "tac_u", + "password": "tac_secret_password", + "options": "", + "logs": "", + "tags": "", + "jumphost": "", + "fallback": ["@local_bak"], + }, + "local_bak": { + "host": "", + "protocol": "ssh", + "port": "", + "user": "loc_u", + "password": "loc_secret_password", + "options": "", + "logs": "", + "tags": "", + "jumphost": "", + "fallback": "", + }, + } + }, + ) + cfg._connections_add(id="r_inherit", host="10.0.0.10", user="@tacacs", password="@tacacs") + cfg._connections_add( + id="r_pw_inherit", + host="10.0.0.11", + user="custom_u", + password="@tacacs", + ) + cfg._connections_add( + id="r_explicit_fb", + host="10.0.0.12", + user="@tacacs", + password="@tacacs", + fallback=[{"user": "override_u", "password": "override_pw"}], + ) + cfg._saveconfig(cfg.file) + + ext_inherit = cfg.getitem("r_inherit", extract=True) + assert ext_inherit["user"] == "tac_u" + assert ext_inherit["password"] == "tac_secret_password" + assert ext_inherit["tags"]["_fallback"] == [ + {"user": "loc_u", "password": "loc_secret_password"} + ] + assert "tags" not in cfg.connections["r_inherit"] or "_fallback" not in str( + cfg.connections["r_inherit"].get("tags", "") + ) + n_inherit = NodeClass("r_inherit", **ext_inherit, config=cfg) + assert [c.user for c in n_inherit._auth_candidates] == ["tac_u", "loc_u"] + assert [c.is_fallback for c in n_inherit._auth_candidates] == [False, True] + + ext_pw = cfg.getitem("r_pw_inherit", extract=True) + assert ext_pw["tags"]["_fallback"] == [{"user": "loc_u", "password": "loc_secret_password"}] + ext_exp = cfg.getitem("r_explicit_fb", extract=True) + assert ext_exp["tags"]["_fallback"] == [{"user": "override_u", "password": "override_pw"}] + + # 2. Fail-closed on missing refs, cycles, depth, rsa_blob without decrypt_fn, and empty list passwords + for bad_fb in ( + ["@missing"], + [{"user": "@missing", "password": "pw"}], + [{"user": "u", "password": "@missing"}], + [{"user": "u", "password": ["@missing"]}], + ): + with pytest.raises(ValueError): + resolve_fallback_entries(bad_fb, profiles_combo) + + with pytest.raises(ValueError, match="Circular"): + auth_utils._resolve_profile_field(profiles_combo, "cyc_a", "user") + with pytest.raises(ValueError, match="Circular"): + auth_utils._resolve_profile_field({"p": {"password": "x"}}, "p", "password", depth=MAX_PROFILE_DEPTH) + + rsa_blob = _encrypt_with_config(cfg, "RSA_SECRET") + with pytest.raises(SanitizedAuthError) as exc_rsa: + parse_compose_expression(f"compose:{rsa_blob}+'1'", profiles=cfg.profiles, decrypt_fn=None) + assert exc_rsa.value.code == "NO_CREDS" + + with pytest.raises(SanitizedAuthError) as exc_rsa_prof: + parse_compose_expression( + "compose:@rsa_prof", + profiles={"rsa_prof": {"password": rsa_blob}}, + decrypt_fn=None, + ) + assert exc_rsa_prof.value.code == "NO_CREDS" + + with pytest.raises(ValueError, match="non-empty strings"): + validate_fallback_entries([{"user": "u", "password": ["ok", ""]}]) + + # Literal @foo+bar still allowed inside fallback entries + assert resolve_fallback_entries([{"user": "u", "password": "@foo+bar"}], {}) == [ + {"user": "u", "password": "@foo+bar"} + ] + + # 3. Control chars stripped BEFORE redacting (< 4 char + split secrets) & __context__ is None on compose decrypt error + assert sanitize_log_message("pw=p\x071\x1b[31m ok", secrets=["p1"]) == "pw=*** ok" + assert ( + sanitize_log_message("pw=Sec\x00\nRet99\x1b[32m!", secrets=["SecRet99"]) == "pw=***!" + ) + + def _bad_decrypt(_blob: str) -> str: + secret = "InnerKeySecret#99" + raise RuntimeError(f"decrypt error {secret}") + + with pytest.raises(SanitizedAuthError) as dec_err_info: + parse_compose_expression(f"compose:{rsa_blob}", profiles={}, decrypt_fn=_bad_decrypt) + assert dec_err_info.value.__context__ is None + assert dec_err_info.value.__cause__ is None + assert dec_err_info.value.__suppress_context__ is True + assert "InnerKeySecret" not in str(dec_err_info.value) + + # Double-pass log redaction (both before and after control-char/ANSI stripping) + assert sanitize_log_message("pw=\x07Sec!", secrets=["\x07Sec"]) == "pw=***!" + + # 4. RecursionError protection & fail-closed malformed JSON in top-level fallback + deep_json = "[" * 1500 + "1" + "]" * 1500 + with pytest.raises(ValueError): + validate_json_depth_and_size(deep_json) + + deep_ref_json = "[" * 1500 + '"@tacacs"' + "]" * 1500 + assert extract_profile_refs_from_value(deep_ref_json) == set() + + with pytest.raises(ValueError): + validate_fallback_entries('[{"user":"a","password":"s3cr3t"') + with pytest.raises(ValueError): + validate_fallback_entries("{Admin}123") + assert validate_fallback_entries([{"user": "u", "password": "{Admin}123"}]) == [ + {"user": "u", "password": "{Admin}123"} + ] + with pytest.raises(ValueError): + validate_fallback_entries("[[[[[1]]]]]") + with pytest.raises(ValueError): + validate_fallback_entries("[" + "a" * 9000) + + # 5a. mask_node_secrets masks tags._fallback from fresh getitem(extract=True) + masked_extracted = mask_node_secrets(cfg.getitem("r_inherit", extract=True)) + assert masked_extracted["password"] == "***" + assert masked_extracted["tags"]["_fallback"] == [{"user": "loc_u", "password": "***"}] + + # 5b. AuthAttemptTracker parameter validation & atomic multi-budget acquisition + for bad_ttl in (0, -5, float("nan"), float("inf")): + with pytest.raises(ValueError): + AuthAttemptTracker(ttl_seconds=bad_ttl) + for bad_bool_ttl in (True, False, "10"): + with pytest.raises(TypeError): + AuthAttemptTracker(ttl_seconds=bad_bool_ttl) + for bad_entries in (0, -1): + with pytest.raises(ValueError): + AuthAttemptTracker(max_entries=bad_entries) + for bad_bool_entries in (True, False, "5"): + with pytest.raises(TypeError): + AuthAttemptTracker(max_entries=bad_bool_entries) + + # Exhaust host budget first -> trying (user='shared_u', host='locked.host') must NOT consume user budget + t_atomic = AuthAttemptTracker(default_max_host_attempts=1, default_max_global_user_attempts=1) + assert t_atomic.try_acquire_host("locked.host", "22", "ssh", limit=1) is True + assert t_atomic.remaining_user("shared_u", limit=1) == 1 + + ok, code = t_atomic.try_acquire_target_attempt( + host="locked.host", + port="22", + protocol="ssh", + user="shared_u", + host_limit=1, + user_limit=1, + ) + assert ok is False + assert code == "HOST_BUDGET_EXHAUSTED" + assert t_atomic.remaining_user("shared_u", limit=1) == 1 + + # Full _connect flow: host exhausted must return HOST_BUDGET_EXHAUSTED and leave shared_u intact + n_locked = NodeClass( + "r_locked", + host="locked.host", + port="22", + user="shared_u", + password="secret_pw", + tags={"max_total_auth_attempts": 1, "max_global_user_attempts": 1}, + ) + + def _spawn_prompt(*_a, **_kw): + ch = _make_mock_child( + expect_fn=lambda _p, timeout=None: ( + setattr(ch, "after", b"Password:"), + 8, + )[-1] + ) + return ch + + with patch("connpy.core.pexpect.spawn", side_effect=_spawn_prompt): + res_locked = n_locked._connect(unattended=True, attempt_tracker=t_atomic) + assert "[HOST_BUDGET_EXHAUSTED]" in str(res_locked) + assert t_atomic.remaining_user("shared_u", limit=1) == 1 + + +def test_signoff_ronda2_eof_empty_profiles_host_re_shlex_and_lazy_import() -> None: + # (a) Single-candidate EOF after sending password -> AUTH_FAILED; premature EOF -> TRANSPORT_ERROR + n_single = NodeClass( + "r_eof_single", + host="10.0.0.80", + user="u", + password="p1", + ) + + def _spawn_eof_after_pw(*_a, **_kw): + step = {"i": 0} + ch = None + + def _exp(_p, timeout=None): + i = step["i"] + step["i"] += 1 + if i == 0: + ch.after = b"Password:" + return 8 + ch.before = b"" + ch.after = b"" + return 11 + + ch = _make_mock_child(expect_fn=_exp) + return ch + + with patch("connpy.core.pexpect.spawn", side_effect=_spawn_eof_after_pw): + res_after = n_single._connect(unattended=True) + assert res_after is not True + assert "[AUTH_FAILED]" in str(res_after) + + def _spawn_eof_before_pw(*_a, **_kw): + ch = _make_mock_child( + expect_fn=lambda _p, timeout=None: ( + setattr(ch, "before", b""), + setattr(ch, "after", b""), + 11, + )[-1] + ) + return ch + + with patch("connpy.core.pexpect.spawn", side_effect=_spawn_eof_before_pw): + res_before = n_single._connect(unattended=True) + assert res_before is not True + assert "[TRANSPORT_ERROR]" in str(res_before) + + # (b) Empty referenced fallback profiles must fail closed + with pytest.raises(ValueError, match="no credentials"): + resolve_fallback_entries(["@empty_prof"], {"empty_prof": {"user": "", "password": ""}}) + with pytest.raises(ValueError, match="no credentials"): + resolve_fallback_entries( + ["@empty_sub"], + {"empty_sub": {"user": "", "password": "", "fallback": "[]"}}, + ) + + # (c) host_exact_re boundary coverage + host_re = host_exact_re("host1") + for matching_prompt in ( + "host1#", + "user@host1>", + "user@host1$", + "(user@host1) Password:", + "host1, ", + ): + assert host_re.search(matching_prompt) is not None + for non_matching_prompt in ("host10#", "host1.attacker.com#"): + assert host_re.search(non_matching_prompt) is None + + # (d) shlex-based insecure hostkey detection + for insecure_opt in ( + '-o "StrictHostKeyChecking=no"', + "-o StrictHostKeyChecking no", + "-o StrictHostKeyChecking=off", + "-oStrictHostKeyChecking=no", + "-o UserKnownHostsFile=/dev/null", + "-oUserKnownHostsFile=/dev/null", + ): + assert has_insecure_hostkey_checking(insecure_opt) is True + assert has_insecure_hostkey_checking("-o StrictHostKeyChecking=yes") is False + + # (e) Isolated import of connpy.auth_utils + AuthAttemptTracker strict limit validation + repo_root = str(Path(__file__).resolve().parents[2]) + subprocess.run( + [sys.executable, "-c", "import connpy.auth_utils"], + check=True, + cwd=repo_root, + ) + subprocess.run( + [ + sys.executable, + "-c", + ( + "import sys, connpy.auth_utils as au; " + "assert 'connpy.services' not in sys.modules; " + "from connpy.services.exceptions import ConnpyError; " + "assert issubclass(au.SanitizedAuthError, ConnpyError)" + ), + ], + check=True, + cwd=repo_root, + ) + for bad_limit in (True, False, 0, -1, 11, "2"): + with pytest.raises(ValueError): + AuthAttemptTracker(default_max_host_attempts=bad_limit) + with pytest.raises(ValueError): + AuthAttemptTracker(default_max_hop_attempts=bad_limit) + for bad_user_limit in (True, False, 0, -1, 1001, "2"): + with pytest.raises(ValueError): + AuthAttemptTracker(default_max_global_user_attempts=bad_user_limit)