From 441bf54ac18a544ed7e7eaaedde0cc212a10302d Mon Sep 17 00:00:00 2001 From: Fede Luzzi Date: Tue, 17 Nov 2026 13:44:52 -0300 Subject: [PATCH] chore(release): bump version to v6.5.0b1 and update documentation --- README.md | 11 +- connpy/__init__.py | 11 +- connpy/_version.py | 2 +- docs/connpy/auth_utils.html | 1514 ++++++++ docs/connpy/cli/config_handler.html | 168 +- docs/connpy/cli/forms.html | 204 +- docs/connpy/cli/validators.html | 133 +- docs/connpy/grpc_layer/server.html | 74 +- docs/connpy/index.html | 3732 +++++++++++++++---- docs/connpy/mcp_server.html | 239 +- docs/connpy/services/exceptions.html | 1 + docs/connpy/services/execution_service.html | 469 ++- docs/connpy/services/index.html | 1501 ++++++-- docs/connpy/services/node_service.html | 518 ++- docs/connpy/services/profile_service.html | 513 ++- docs/connpy/services/provider.html | 54 +- 16 files changed, 7507 insertions(+), 1637 deletions(-) create mode 100644 docs/connpy/auth_utils.html diff --git a/README.md b/README.md index 4d71746..37f47b7 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@

-# Connpy (v6.4.0b4) +# Connpy (v6.5.0b1) [![](https://img.shields.io/pypi/v/connpy.svg?style=flat-square)](https://pypi.org/pypi/connpy/) [![](https://img.shields.io/pypi/pyversions/connpy.svg?style=flat-square)](https://pypi.org/pypi/connpy/) [![](https://img.shields.io/pypi/dm/connpy.svg?style=flat-square&cacheSeconds=86400)](https://pypi.org/pypi/connpy/) @@ -137,6 +137,12 @@ Customize connection settings dynamically using tags. Configure per-node setting tags: { "os": "cisco_ios", "prompt": ".*#", "screen_length_command": "terminal length 0" } ``` +### 3f. Credential Fallback Chain, `compose:` & Auth Budgets +Configure automatic backup credentials (`fallback`) on nodes or `@profile` templates so both interactive (`conn `) and unattended (`conn run` / MCP) sessions switch seamlessly when primary TACACS+/AAA authentication fails (opt-out via `tags.allow_fallback: false`). +* **Fallback Formats**: Accepts `@profile`, explicit JSON list/dict entries (`[{"user": "backup", "password": "..."}]`), or `compose:` expressions. Nodes referencing multiple profiles inherit `fallback` with priority from `user`/`password` `@profile` references. +* **Password Composition (`compose:`)**: Concatenate secrets and literals explicitly using `compose:@prof1+@prof2+"suffix"` (use `@@` to escape leading literal `@`). +* **Anti-Lockout Guardrails (`conn config`)**: Enforce strict attempt bounds via `--max-auth-prompts`, `--max-total-auth-attempts`, `--max-hop-host-attempts`, `--max-global-user-attempts`, and `--disable-fallback-on-insecure-hostkey`. + --- @@ -157,6 +163,9 @@ Support for single or chained intermediate gateway nodes (SSH, Mosh, SSM, kubect ### 4c. Debug Mode, Keepalive, Logging Track connection steps (`conn --debug node`), set idle keepalive intervals (`conn config --keepalive `), or define dynamic output log files using variables like `${unique}`, `${host}`, `${port}`, `${user}`, `${protocol}`, or `${date 'format'}`. +### 4d. Adaptive Multi-Hop Timeouts +Differentiate `--connect-timeout` and `--auth-timeout` stages (`conn run` / `conn config`), automatically computing topological per-hop latency budgets for chained `SSM` and `Jumphost` tunnels with bounded progress extensions on slow handshakes. + --- diff --git a/connpy/__init__.py b/connpy/__init__.py index 840d225..e2501e1 100644 --- a/connpy/__init__.py +++ b/connpy/__init__.py @@ -5,7 +5,7 @@

-# Connpy (v6.4.0b4) +# Connpy (v6.5.0b1) [![](https://img.shields.io/pypi/v/connpy.svg?style=flat-square)](https://pypi.org/pypi/connpy/) [![](https://img.shields.io/pypi/pyversions/connpy.svg?style=flat-square)](https://pypi.org/pypi/connpy/) [![](https://img.shields.io/pypi/dm/connpy.svg?style=flat-square&cacheSeconds=86400)](https://pypi.org/pypi/connpy/) @@ -139,6 +139,12 @@ Customize connection settings dynamically using tags. Configure per-node setting tags: { "os": "cisco_ios", "prompt": ".*#", "screen_length_command": "terminal length 0" } ``` +### 3f. Credential Fallback Chain, `compose:` & Auth Budgets +Configure automatic backup credentials (`fallback`) on nodes or `@profile` templates so both interactive (`conn `) and unattended (`conn run` / MCP) sessions switch seamlessly when primary TACACS+/AAA authentication fails (opt-out via `tags.allow_fallback: false`). +* **Fallback Formats**: Accepts `@profile`, explicit JSON list/dict entries (`[{"user": "backup", "password": "..."}]`), or `compose:` expressions. Nodes referencing multiple profiles inherit `fallback` with priority from `user`/`password` `@profile` references. +* **Password Composition (`compose:`)**: Concatenate secrets and literals explicitly using `compose:@prof1+@prof2+"suffix"` (use `@@` to escape leading literal `@`). +* **Anti-Lockout Guardrails (`conn config`)**: Enforce strict attempt bounds via `--max-auth-prompts`, `--max-total-auth-attempts`, `--max-hop-host-attempts`, `--max-global-user-attempts`, and `--disable-fallback-on-insecure-hostkey`. + --- @@ -159,6 +165,9 @@ Support for single or chained intermediate gateway nodes (SSH, Mosh, SSM, kubect ### 4c. Debug Mode, Keepalive, Logging Track connection steps (`conn --debug node`), set idle keepalive intervals (`conn config --keepalive `), or define dynamic output log files using variables like `${unique}`, `${host}`, `${port}`, `${user}`, `${protocol}`, or `${date 'format'}`. +### 4d. Adaptive Multi-Hop Timeouts +Differentiate `--connect-timeout` and `--auth-timeout` stages (`conn run` / `conn config`), automatically computing topological per-hop latency budgets for chained `SSM` and `Jumphost` tunnels with bounded progress extensions on slow handshakes. + --- diff --git a/connpy/_version.py b/connpy/_version.py index 410dcb6..57dbf65 100644 --- a/connpy/_version.py +++ b/connpy/_version.py @@ -1 +1 @@ -__version__ = "6.4.0b4" +__version__ = "6.5.0b1" diff --git a/docs/connpy/auth_utils.html b/docs/connpy/auth_utils.html new file mode 100644 index 0000000..dfb90a5 --- /dev/null +++ b/docs/connpy/auth_utils.html @@ -0,0 +1,1514 @@ + + + + + + +connpy.auth_utils API documentation + + + + + + + + + + + +
+
+
+

Module connpy.auth_utils

+
+
+

Pure authentication, composition, masking, sanitization, and rate-limit helpers.

+
+
+
+
+
+
+

Functions

+
+
+def clear_exception_frames(exc: BaseException | None) ‑> None +
+
+
+ +Expand source code + +
def clear_exception_frames(exc: BaseException | None) -> None:
+    """Clear local variables on all frames in an exception's traceback chain."""
+    seen: set[int] = set()
+    stack: list[BaseException] = []
+    if exc is not None:
+        stack.append(exc)
+    while stack:
+        cur = stack.pop()
+        obj_id = id(cur)
+        if obj_id in seen:
+            continue
+        seen.add(obj_id)
+        cause = getattr(cur, "__cause__", None)
+        ctx = getattr(cur, "__context__", None)
+        if cause is not None:
+            stack.append(cause)
+        if ctx is not None:
+            stack.append(ctx)
+
+        tb = getattr(cur, "__traceback__", None)
+        if tb is not None:
+            with suppress(RuntimeError):
+                traceback.clear_frames(tb)
+
+        with suppress(AttributeError, TypeError):
+            cur.__traceback__ = None
+        with suppress(AttributeError, TypeError):
+            cur.__cause__ = None
+        with suppress(AttributeError, TypeError):
+            cur.__context__ = None
+
+

Clear local variables on all frames in an exception's traceback chain.

+
+
+def extract_compose_profile_refs(expr: str) ‑> set[str] +
+
+
+ +Expand source code + +
def extract_compose_profile_refs(expr: str) -> set[str]:
+    """Return profile names referenced via @profile tokens in a compose: expression."""
+    if not isinstance(expr, str) or not expr.startswith("compose:"):
+        return set()
+    if len(expr) > MAX_COMPOSE_EXPR_LEN:
+        return set()
+    body = expr[len("compose:") :]
+    if not body or not body.strip():
+        return set()
+    try:
+        tokens = _tokenize_compose_body(body)
+    except SanitizedAuthError:
+        return set()
+    return {tok_val for tok_type, tok_val in tokens if tok_type == "profile_ref"}
+
+

Return profile names referenced via @profile tokens in a compose: expression.

+
+
+def extract_profile_refs_from_value(value: Any) ‑> set[str] +
+
+
+ +Expand source code + +
def extract_profile_refs_from_value(value: Any) -> set[str]:
+    """Extract all @profile names referenced directly or via compose:/fallback in a value."""
+    return _extract_profile_refs_internal(value, set())
+
+

Extract all @profile names referenced directly or via compose:/fallback in a value.

+
+
+def has_insecure_hostkey_checking(options: str = '', tags: Any = None, hop_metas: Iterable[dict[str, Any]] = ()) ‑> bool +
+
+
+ +Expand source code + +
def has_insecure_hostkey_checking(
+    options: str = "",
+    tags: Any = None,
+    hop_metas: Iterable[dict[str, Any]] = (),
+) -> bool:
+    """Return True if StrictHostKeyChecking=no/off or UserKnownHostsFile=/dev/null is configured."""
+    candidates: list[str] = []
+    if options:
+        candidates.append(str(options))
+    if isinstance(tags, dict):
+        for k in ("mosh_ssh_options", "ssh_options"):
+            v = tags.get(k)
+            if v:
+                candidates.append(str(v))
+    for hop in hop_metas or ():
+        if isinstance(hop, dict):
+            h_opts = hop.get("options", "")
+            if h_opts:
+                candidates.append(str(h_opts))
+            h_tags = hop.get("tags")
+            if isinstance(h_tags, dict):
+                for k in ("mosh_ssh_options", "ssh_options"):
+                    v = h_tags.get(k)
+                    if v:
+                        candidates.append(str(v))
+    return any(_has_insecure_ssh_options(c) for c in candidates)
+
+

Return True if StrictHostKeyChecking=no/off or UserKnownHostsFile=/dev/null is configured.

+
+
+def host_exact_re(host: str) ‑> re.Pattern[str] +
+
+
+ +Expand source code + +
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"(?<![A-Za-z0-9._-]){escaped}(?![A-Za-z0-9._-])(?=$|[\s:\]'\"#>$),])",
+        re.IGNORECASE | re.ASCII,
+    )
+
+

Compile an exact host boundary regex with zero FQDN subdomain wildcard risk.

+
+
+def is_compose_expression(value: Any) ‑> bool +
+
+
+ +Expand source code + +
def is_compose_expression(value: Any) -> bool:
+    """Return True if value is a string with the explicit compose: prefix."""
+    return isinstance(value, str) and value.startswith("compose:")
+
+

Return True if value is a string with the explicit compose: prefix.

+
+
+def is_rsa_blob(value: Any) ‑> bool +
+
+
+ +Expand source code + +
def is_rsa_blob(value: Any) -> bool:
+    """Return True if value matches an encrypted RSA bytes literal string."""
+    return isinstance(value, str) and bool(_RSA_BLOB_RE.match(value))
+
+

Return True if value matches an encrypted RSA bytes literal string.

+
+
+def mask_node_secrets(obj: Any) ‑> Any +
+
+
+ +Expand source code + +
def mask_node_secrets(obj: Any) -> Any:
+    """Recursively mask sensitive node/profile credentials with '***' using a closed key list."""
+    visiting: set[int] = set()
+
+    def _recurse(val: Any, *, in_fallback_list: bool = False) -> Any:
+        if isinstance(val, dict):
+            obj_id = id(val)
+            if obj_id in visiting:
+                return "***"
+            visiting.add(obj_id)
+            try:
+                out: dict[Any, Any] = {}
+                for k, v in val.items():
+                    k_lower = k.lower() if isinstance(k, str) else str(k).lower()
+                    if _mask_secret_key(k_lower):
+                        out[k] = "***"
+                    elif k_lower in ("fallback", "_fallback"):
+                        if isinstance(v, (dict, list, tuple)):
+                            out[k] = _recurse(v, in_fallback_list=True)
+                        elif v in ("", None):
+                            out[k] = v
+                        else:
+                            out[k] = "***"
+                    else:
+                        out[k] = _recurse(v, in_fallback_list=False)
+                return out
+            finally:
+                visiting.discard(obj_id)
+
+        if isinstance(val, list):
+            obj_id = id(val)
+            if obj_id in visiting:
+                return "***"
+            visiting.add(obj_id)
+            try:
+                return [
+                    "***"
+                    if (in_fallback_list and isinstance(item, str) and item != "")
+                    else _recurse(item, in_fallback_list=in_fallback_list)
+                    for item in val
+                ]
+            finally:
+                visiting.discard(obj_id)
+
+        if isinstance(val, tuple):
+            obj_id = id(val)
+            if obj_id in visiting:
+                return "***"
+            visiting.add(obj_id)
+            try:
+                return tuple(
+                    "***"
+                    if (in_fallback_list and isinstance(item, str) and item != "")
+                    else _recurse(item, in_fallback_list=in_fallback_list)
+                    for item in val
+                )
+            finally:
+                visiting.discard(obj_id)
+
+        return val
+
+    return _recurse(obj)
+
+

Recursively mask sensitive node/profile credentials with '***' using a closed key list.

+
+
+def normalize_prompt_line(before_bytes: bytes, after_bytes: bytes) ‑> str +
+
+
+ +Expand source code + +
def normalize_prompt_line(before_bytes: bytes, after_bytes: bytes) -> str:
+    """Return the normalized final prompt line stripped of ANSI and control sequences."""
+    combined = (before_bytes or b"") + (after_bytes or b"")
+    text = combined.decode("utf-8", errors="replace")
+    text = _ANSI_OSC_RE.sub("", text)
+    text = _ANSI_CSI_RE.sub("", text)
+    text = text.replace("\r\n", "\n").replace("\r", "\n")
+    lines = [line.strip() for line in text.split("\n") if line.strip()]
+    if not lines:
+        return ""
+    return _CONTROL_CHAR_RE.sub("", lines[-1]).strip()
+
+

Return the normalized final prompt line stripped of ANSI and control sequences.

+
+
+def parse_compose_expression(expr: str,
*,
profiles: dict[str, Any] | None = None,
decrypt_fn: Callable[[str], str] | None = None,
max_depth: int = 5,
max_variants: int = 10) ‑> list[str]
+
+
+
+ +Expand source code + +
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,
+    )
+
+

Parse and evaluate a compose: password expression into up to max_variants strings.

+
+
+def raise_sanitized_auth_error(orig_exc: BaseException | None = None,
*,
code: str = 'AUTH_FAILED',
secrets: Iterable[str] = (),
context_label: str = '') ‑> NoReturn
+
+
+
+ +Expand source code + +
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
+
+

Clear original frames and raise SanitizedAuthError with cause and context suppressed.

+
+
+def redact_known_secrets(text: str, secrets: Iterable[str] = (), *, min_len: int = 4) ‑> str +
+
+
+ +Expand source code + +
def redact_known_secrets(
+    text: str,
+    secrets: Iterable[str] = (),
+    *,
+    min_len: int = 4,
+) -> str:
+    """Replace any non-trivial secret strings in text with '***'."""
+    if not text or not secrets:
+        return text
+    out = str(text)
+    unique_secrets = sorted(
+        {s for s in secrets if isinstance(s, str) and len(s) >= min_len},
+        key=len,
+        reverse=True,
+    )
+    for sec in unique_secrets:
+        out = out.replace(sec, "***")
+    return out
+
+

Replace any non-trivial secret strings in text with '***'.

+
+
+def resolve_fallback_entries(raw_fallback: Any, profiles: dict[str, Any] | None, *, max_entries: int = 5) ‑> list[dict[str, typing.Any]] +
+
+
+ +Expand source code + +
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,
+    )
+
+

Normalize fallback entries and resolve any @profile references safely.

+
+
+def sanitize_exception(orig_exc: BaseException | None,
*,
code: str = 'AUTH_FAILED',
secrets: Iterable[str] = (),
context_label: str = '',
clear_orig_frames: bool = True) ‑> SanitizedAuthError
+
+
+
+ +Expand source code + +
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)
+
+

Convert any exception into a SanitizedAuthError without leaking secrets or tracebacks.

+
+
+def sanitize_log_message(message: str, *, secrets: Iterable[str] = ()) ‑> str +
+
+
+ +Expand source code + +
def sanitize_log_message(message: str, *, secrets: Iterable[str] = ()) -> str:
+    """Strip ANSI sequences, control chars, and known secrets from a log line."""
+    text = str(message) if message is not None else ""
+    text = redact_known_secrets(text, secrets=secrets, min_len=1)
+    text = _ANSI_OSC_RE.sub("", text)
+    text = _ANSI_CSI_RE.sub("", text)
+    text = _CONTROL_CHAR_RE.sub("", text)
+    return redact_known_secrets(text, secrets=secrets, min_len=1)
+
+

Strip ANSI sequences, control chars, and known secrets from a log line.

+
+
+def validate_auth_budget_tags(tags: Any) ‑> None +
+
+
+ +Expand source code + +
def validate_auth_budget_tags(tags: Any) -> None:
+    """Validate optional auth budget and policy keys in a node/profile tags dict."""
+    if not isinstance(tags, dict):
+        return
+    if "max_auth_prompts" in tags and tags["max_auth_prompts"] is not None:
+        validate_strict_int_range(
+            tags["max_auth_prompts"],
+            "tags.max_auth_prompts",
+            MIN_AUTH_PROMPTS,
+            MAX_AUTH_PROMPTS_CAP,
+        )
+    if "max_total_auth_attempts" in tags and tags["max_total_auth_attempts"] is not None:
+        validate_strict_int_range(
+            tags["max_total_auth_attempts"],
+            "tags.max_total_auth_attempts",
+            MIN_TOTAL_AUTH_ATTEMPTS,
+            MAX_TOTAL_AUTH_ATTEMPTS_CAP,
+        )
+    if "max_hop_host_attempts" in tags and tags["max_hop_host_attempts"] is not None:
+        validate_strict_int_range(
+            tags["max_hop_host_attempts"],
+            "tags.max_hop_host_attempts",
+            MIN_HOP_HOST_ATTEMPTS,
+            MAX_HOP_HOST_ATTEMPTS_CAP,
+        )
+    if "max_global_user_attempts" in tags and tags["max_global_user_attempts"] is not None:
+        validate_strict_int_range(
+            tags["max_global_user_attempts"],
+            "tags.max_global_user_attempts",
+            1,
+            1000,
+        )
+    for bool_key in (
+        "allow_fallback",
+        "allow_ambiguous_single_hop_prompt",
+        "disable_fallback_on_insecure_hostkey",
+    ):
+        if bool_key in tags and tags[bool_key] is not None and not isinstance(tags[bool_key], bool):
+            raise ValueError(f"Invalid tags.{bool_key}: expected boolean")
+    if "fallback" in tags and tags["fallback"] not in (None, "", []):
+        validate_fallback_entries(tags["fallback"])
+
+

Validate optional auth budget and policy keys in a node/profile tags dict.

+
+
+def validate_compose_syntax(expr: str) ‑> None +
+
+
+ +Expand source code + +
def validate_compose_syntax(expr: str) -> None:
+    """Validate compose: syntax and length without resolving profiles or decrypting."""
+    if not isinstance(expr, str) or len(expr) > MAX_COMPOSE_EXPR_LEN:
+        raise_sanitized_auth_error(
+            code="AUTH_FAILED",
+            context_label="compose expression exceeds maximum length",
+        )
+    body = expr.removeprefix("compose:")
+    if not body or not body.strip():
+        raise_sanitized_auth_error(
+            code="AUTH_FAILED",
+            context_label="empty compose expression",
+        )
+    _tokenize_compose_body(body)
+
+

Validate compose: syntax and length without resolving profiles or decrypting.

+
+
+def validate_config_auth_settings(cfg: Any) ‑> None +
+
+
+ +Expand source code + +
def validate_config_auth_settings(cfg: Any) -> None:
+    """Validate global config auth budget and policy keys."""
+    if not isinstance(cfg, dict):
+        raise TypeError("Config section must be a dictionary")
+    if "max_auth_prompts" in cfg and cfg["max_auth_prompts"] is not None:
+        validate_strict_int_range(
+            cfg["max_auth_prompts"],
+            "config.max_auth_prompts",
+            MIN_AUTH_PROMPTS,
+            MAX_AUTH_PROMPTS_CAP,
+        )
+    if "max_total_auth_attempts" in cfg and cfg["max_total_auth_attempts"] is not None:
+        validate_strict_int_range(
+            cfg["max_total_auth_attempts"],
+            "config.max_total_auth_attempts",
+            MIN_TOTAL_AUTH_ATTEMPTS,
+            MAX_TOTAL_AUTH_ATTEMPTS_CAP,
+        )
+    if "max_hop_host_attempts" in cfg and cfg["max_hop_host_attempts"] is not None:
+        validate_strict_int_range(
+            cfg["max_hop_host_attempts"],
+            "config.max_hop_host_attempts",
+            MIN_HOP_HOST_ATTEMPTS,
+            MAX_HOP_HOST_ATTEMPTS_CAP,
+        )
+    if "max_global_user_attempts" in cfg and cfg["max_global_user_attempts"] is not None:
+        validate_strict_int_range(
+            cfg["max_global_user_attempts"],
+            "config.max_global_user_attempts",
+            1,
+            1000,
+        )
+    if (
+        "disable_fallback_on_insecure_hostkey" in cfg
+        and cfg["disable_fallback_on_insecure_hostkey"] is not None
+        and not isinstance(cfg["disable_fallback_on_insecure_hostkey"], bool)
+    ):
+        raise ValueError("Invalid config.disable_fallback_on_insecure_hostkey: expected boolean")
+
+

Validate global config auth budget and policy keys.

+
+
+def validate_fallback_entries(raw_fallback: Any, *, max_entries: int = 5) ‑> list[dict[str, typing.Any] | str] +
+
+
+ +Expand source code + +
def validate_fallback_entries(
+    raw_fallback: Any,
+    *,
+    max_entries: int = MAX_FALLBACK_ENTRIES,
+) -> list[dict[str, Any] | str]:
+    """Validate and normalize fallback config entries without expanding profiles."""
+    if raw_fallback in (None, "", []):
+        return []
+
+    parsed = raw_fallback
+    if isinstance(raw_fallback, str):
+        stripped = raw_fallback.strip()
+        if not stripped:
+            return []
+        if stripped.startswith(("[", "{")):
+            parsed = validate_json_depth_and_size(stripped)
+        else:
+            if stripped.startswith("compose:"):
+                validate_compose_syntax(stripped)
+            return [stripped]
+
+    if isinstance(parsed, dict):
+        items: list[Any] = [parsed]
+    elif isinstance(parsed, (list, tuple)):
+        items = list(parsed)
+    else:
+        raise TypeError(
+            "Fallback must be a list of credential dicts/strings, a dict, @profile, or JSON"
+        )
+
+    if len(items) > max_entries:
+        raise ValueError(f"Fallback exceeds maximum of {max_entries} entries")
+
+    normalized: list[dict[str, Any] | str] = []
+    allowed_entry_keys = {"user", "password"}
+    for idx, entry in enumerate(items):
+        if isinstance(entry, str):
+            if not entry:
+                raise ValueError(f"Fallback entry {idx} cannot be empty")
+            if entry.startswith("compose:"):
+                validate_compose_syntax(entry)
+            normalized.append(entry)
+        elif isinstance(entry, dict):
+            unknown_keys = set(entry.keys()) - allowed_entry_keys
+            if unknown_keys:
+                raise ValueError(
+                    f"Unsupported keys in fallback entry {idx}: {sorted(unknown_keys)}"
+                )
+            user_val = entry.get("user", "")
+            pw_val = entry.get("password", "")
+            if user_val is not None and not isinstance(user_val, str):
+                raise ValueError(f"Fallback entry {idx} 'user' must be a string")
+            if isinstance(pw_val, (list, tuple)):
+                if not pw_val or not all(isinstance(p, str) and p != "" for p in pw_val):
+                    raise ValueError(
+                        f"Fallback entry {idx} 'password' list must contain non-empty strings"
+                    )
+                for p in pw_val:
+                    if p.startswith("compose:"):
+                        validate_compose_syntax(p)
+            elif pw_val is not None and not isinstance(pw_val, str):
+                raise ValueError(f"Fallback entry {idx} 'password' must be a string or list")
+            elif isinstance(pw_val, str) and pw_val.startswith("compose:"):
+                validate_compose_syntax(pw_val)
+
+            if not (user_val or pw_val):
+                raise ValueError(
+                    f"Fallback entry {idx} must specify 'user' and/or 'password'"
+                )
+            normalized.append(deepcopy(entry))
+        else:
+            raise TypeError(f"Fallback entry {idx} must be a dict or string")
+
+    return normalized
+
+

Validate and normalize fallback config entries without expanding profiles.

+
+
+def validate_json_depth_and_size(raw_or_obj: Any, *, max_bytes: int = 8192, max_depth: int = 4) ‑> Any +
+
+
+ +Expand source code + +
def validate_json_depth_and_size(
+    raw_or_obj: Any,
+    *,
+    max_bytes: int = MAX_JSON_INPUT_BYTES,
+    max_depth: int = MAX_JSON_INPUT_DEPTH,
+) -> Any:
+    """Parse (if str) and enforce byte length and nesting depth limits on JSON inputs."""
+    if isinstance(raw_or_obj, str):
+        if len(raw_or_obj) > max_bytes or len(raw_or_obj.encode("utf-8", errors="replace")) > max_bytes:
+            raise ValueError(f"JSON input exceeds maximum size of {max_bytes} bytes")
+        try:
+            parsed = json.loads(raw_or_obj)
+        except RecursionError:
+            raise ValueError(
+                f"JSON nesting exceeds maximum depth of {max_depth}"
+            ) from None
+    else:
+        parsed = raw_or_obj
+    _check_depth(parsed, max_depth, 0)
+    return parsed
+
+

Parse (if str) and enforce byte length and nesting depth limits on JSON inputs.

+
+
+def validate_strict_int_range(value: Any, name: str, min_val: int, max_val: int) ‑> int +
+
+
+ +Expand source code + +
def validate_strict_int_range(
+    value: Any,
+    name: str,
+    min_val: int,
+    max_val: int,
+) -> int:
+    """Validate that value is a strict plain int (not bool/float/str) in [min_val, max_val]."""
+    if type(value) is not int or value < min_val or value > max_val:
+        raise ValueError(
+            f"Invalid {name}: expected integer in [{min_val}, {max_val}], got {value!r}"
+        )
+    return value
+
+

Validate that value is a strict plain int (not bool/float/str) in [min_val, max_val].

+
+
+
+
+

Classes

+
+
+class AuthAttemptTracker +(*,
ttl_seconds: float = 300,
max_entries: int = 10000,
default_max_host_attempts: int = 6,
default_max_hop_attempts: int = 6,
default_max_global_user_attempts: int | None = None,
clock: Callable[[], float] = <built-in function monotonic>)
+
+
+
+ +Expand source code + +
class AuthAttemptTracker:
+    """Thread-safe intra-process authentication attempt tracker with safe anti-flood eviction."""
+
+    def __init__(
+        self,
+        *,
+        ttl_seconds: float = DEFAULT_TRACKER_TTL_SECONDS,
+        max_entries: int = MAX_TRACKER_ENTRIES,
+        default_max_host_attempts: int = DEFAULT_MAX_TOTAL_AUTH_ATTEMPTS,
+        default_max_hop_attempts: int = DEFAULT_MAX_HOP_HOST_ATTEMPTS,
+        default_max_global_user_attempts: int | None = None,
+        clock: Callable[[], float] = time.monotonic,
+    ) -> None:
+        if isinstance(ttl_seconds, bool) or not isinstance(ttl_seconds, (int, float)):
+            raise TypeError(f"Invalid ttl_seconds: expected number > 0, got {ttl_seconds!r}")
+        ttl_val = float(ttl_seconds)
+        if not math.isfinite(ttl_val) or ttl_val <= 0:
+            raise ValueError(f"Invalid ttl_seconds: expected > 0, got {ttl_seconds!r}")
+        if isinstance(max_entries, bool) or not isinstance(max_entries, int):
+            raise TypeError(f"Invalid max_entries: expected int >= 1, got {max_entries!r}")
+        if max_entries < 1:
+            raise ValueError(f"Invalid max_entries: expected >= 1, got {max_entries!r}")
+        self._ttl_seconds = ttl_val
+        self._max_entries = max_entries
+        self._default_max_host_attempts = validate_strict_int_range(
+            default_max_host_attempts,
+            "default_max_host_attempts",
+            MIN_TOTAL_AUTH_ATTEMPTS,
+            MAX_TOTAL_AUTH_ATTEMPTS_CAP,
+        )
+        self._default_max_hop_attempts = validate_strict_int_range(
+            default_max_hop_attempts,
+            "default_max_hop_attempts",
+            MIN_HOP_HOST_ATTEMPTS,
+            MAX_HOP_HOST_ATTEMPTS_CAP,
+        )
+        self._default_max_global_user_attempts: int | None = (
+            validate_strict_int_range(
+                default_max_global_user_attempts,
+                "default_max_global_user_attempts",
+                1,
+                1000,
+            )
+            if default_max_global_user_attempts is not None
+            else None
+        )
+        self._clock = clock
+        self._lock = threading.Lock()
+        self._unblocked: OrderedDict[Any, _BudgetEntry] = OrderedDict()
+        self._blocked: OrderedDict[Any, _BudgetEntry] = OrderedDict()
+
+    def _purge_expired_unlocked(self, now: float) -> None:
+        for bucket in (self._unblocked, self._blocked):
+            expired_keys = [
+                k for k, entry in bucket.items() if (now - entry.last_ts) >= self._ttl_seconds
+            ]
+            for k in expired_keys:
+                del bucket[k]
+
+    def _evict_if_full_unlocked(self, now: float) -> None:
+        if (len(self._unblocked) + len(self._blocked)) < self._max_entries:
+            return
+        self._purge_expired_unlocked(now)
+        while (len(self._unblocked) + len(self._blocked)) >= self._max_entries:
+            if self._unblocked:
+                self._unblocked.popitem(last=False)
+            elif self._blocked:
+                self._blocked.popitem(last=False)
+            else:
+                break
+
+    def _get_live_entry_unlocked(self, key: Any, now: float) -> _BudgetEntry | None:
+        if key in self._unblocked:
+            entry = self._unblocked[key]
+            if (now - entry.last_ts) >= self._ttl_seconds:
+                del self._unblocked[key]
+                return None
+            return entry
+        if key in self._blocked:
+            entry = self._blocked[key]
+            if (now - entry.last_ts) >= self._ttl_seconds:
+                del self._blocked[key]
+                return None
+            return entry
+        return None
+
+    def _can_acquire_unlocked(self, key: Any, limit: int, now: float) -> bool:
+        if limit <= 0:
+            return False
+        entry = self._get_live_entry_unlocked(key, now)
+        if entry is None:
+            return True
+        if entry.count >= limit:
+            entry.limit = limit
+            if key in self._unblocked:
+                del self._unblocked[key]
+                self._blocked[key] = entry
+            return False
+        return True
+
+    def _commit_acquire_unlocked(self, key: Any, limit: int, now: float) -> None:
+        entry = self._get_live_entry_unlocked(key, now)
+        if entry is None:
+            self._evict_if_full_unlocked(now)
+            new_entry = _BudgetEntry(count=1, limit=limit, last_ts=now)
+            if 1 >= limit:
+                self._blocked[key] = new_entry
+            else:
+                self._unblocked[key] = new_entry
+            return
+
+        entry.count += 1
+        entry.limit = limit
+        entry.last_ts = now
+        if entry.count >= limit:
+            if key in self._unblocked:
+                del self._unblocked[key]
+            self._blocked[key] = entry
+            self._blocked.move_to_end(key)
+        else:
+            if key in self._blocked:
+                del self._blocked[key]
+            self._unblocked[key] = entry
+            self._unblocked.move_to_end(key)
+
+    def try_acquire(self, key: Any, *, limit: int) -> bool:
+        """Attempt to consume 1 attempt slot for key within limit. Returns False if exhausted."""
+        if limit <= 0:
+            return False
+        now = self._clock()
+        with self._lock:
+            if not self._can_acquire_unlocked(key, limit, now):
+                return False
+            self._commit_acquire_unlocked(key, limit, now)
+            return True
+
+    def try_acquire_target_attempt(
+        self,
+        *,
+        host: str,
+        port: str = "",
+        protocol: str = "ssh",
+        user: str = "",
+        host_limit: int | None = None,
+        user_limit: int | None = None,
+    ) -> tuple[bool, str | None]:
+        """Atomically verify and consume both user and host attempt slots under one lock."""
+        eff_host_limit = self._default_max_host_attempts if host_limit is None else int(host_limit)
+        eff_user_limit = self._default_max_global_user_attempts if user_limit is None else user_limit
+        host_key = self._host_key(host, port, protocol)
+        user_key = self._user_key(user) if (user and eff_user_limit is not None) else None
+        now = self._clock()
+        with self._lock:
+            if user_key is not None and not self._can_acquire_unlocked(
+                user_key, int(eff_user_limit), now
+            ):
+                return False, "USER_BUDGET_EXHAUSTED"
+            if not self._can_acquire_unlocked(host_key, eff_host_limit, now):
+                return False, "HOST_BUDGET_EXHAUSTED"
+            if user_key is not None:
+                self._commit_acquire_unlocked(user_key, int(eff_user_limit), now)
+            self._commit_acquire_unlocked(host_key, eff_host_limit, now)
+            return True, None
+
+    def remaining(self, key: Any, *, limit: int) -> int:
+        """Return remaining attempt quota for key."""
+        now = self._clock()
+        with self._lock:
+            entry = self._get_live_entry_unlocked(key, now)
+            if entry is None:
+                return max(0, limit)
+            return max(0, limit - entry.count)
+
+    @staticmethod
+    def _host_key(host: str, port: str = "", protocol: str = "ssh") -> tuple[str, str, str, str]:
+        return ("host", str(host).lower(), str(port or "").strip(), str(protocol or "").lower())
+
+    @staticmethod
+    def _hop_key(host: str, port: str = "", protocol: str = "ssh") -> tuple[str, str, str, str]:
+        return ("hop", str(host).lower(), str(port or "").strip(), str(protocol or "").lower())
+
+    @staticmethod
+    def _user_key(user: str) -> tuple[str, str]:
+        return ("user", str(user))
+
+    def try_acquire_host(
+        self,
+        host: str,
+        port: str = "",
+        protocol: str = "ssh",
+        *,
+        limit: int | None = None,
+    ) -> bool:
+        """Consume 1 target-host authentication attempt if within budget."""
+        eff_limit = self._default_max_host_attempts if limit is None else int(limit)
+        return self.try_acquire(self._host_key(host, port, protocol), limit=eff_limit)
+
+    def try_acquire_hop(
+        self,
+        host: str,
+        port: str = "",
+        protocol: str = "ssh",
+        *,
+        limit: int | None = None,
+    ) -> bool:
+        """Consume 1 jumphost authentication attempt if within budget."""
+        eff_limit = self._default_max_hop_attempts if limit is None else int(limit)
+        return self.try_acquire(self._hop_key(host, port, protocol), limit=eff_limit)
+
+    def try_acquire_user(
+        self,
+        user: str,
+        *,
+        limit: int | None = None,
+    ) -> bool:
+        """Consume 1 global username authentication attempt if a global limit is active."""
+        if not user:
+            return True
+        eff_limit = self._default_max_global_user_attempts if limit is None else limit
+        if eff_limit is None:
+            return True
+        return self.try_acquire(self._user_key(user), limit=int(eff_limit))
+
+    def remaining_host(
+        self,
+        host: str,
+        port: str = "",
+        protocol: str = "ssh",
+        *,
+        limit: int | None = None,
+    ) -> int:
+        """Return remaining target-host attempt quota."""
+        eff_limit = self._default_max_host_attempts if limit is None else int(limit)
+        return self.remaining(self._host_key(host, port, protocol), limit=eff_limit)
+
+    def remaining_hop(
+        self,
+        host: str,
+        port: str = "",
+        protocol: str = "ssh",
+        *,
+        limit: int | None = None,
+    ) -> int:
+        """Return remaining jumphost attempt quota."""
+        eff_limit = self._default_max_hop_attempts if limit is None else int(limit)
+        return self.remaining(self._hop_key(host, port, protocol), limit=eff_limit)
+
+    def remaining_user(
+        self,
+        user: str,
+        *,
+        limit: int | None = None,
+    ) -> int | None:
+        """Return remaining global username quota, or None if unlimited."""
+        eff_limit = self._default_max_global_user_attempts if limit is None else limit
+        if eff_limit is None:
+            return None
+        return self.remaining(self._user_key(user), limit=int(eff_limit))
+
+    def is_blocked(self, key: Any) -> bool:
+        """Return True if key currently has an active exhausted budget entry."""
+        now = self._clock()
+        with self._lock:
+            entry = self._get_live_entry_unlocked(key, now)
+            return entry is not None and entry.count >= entry.limit
+
+    def entry_count(self) -> int:
+        """Return total number of tracked entries (before TTL expiry)."""
+        with self._lock:
+            return len(self._unblocked) + len(self._blocked)
+
+    def reset(self) -> None:
+        """Clear all tracked entries."""
+        with self._lock:
+            self._unblocked.clear()
+            self._blocked.clear()
+
+

Thread-safe intra-process authentication attempt tracker with safe anti-flood eviction.

+

Methods

+
+
+def entry_count(self) ‑> int +
+
+
+ +Expand source code + +
def entry_count(self) -> int:
+    """Return total number of tracked entries (before TTL expiry)."""
+    with self._lock:
+        return len(self._unblocked) + len(self._blocked)
+
+

Return total number of tracked entries (before TTL expiry).

+
+
+def is_blocked(self, key: Any) ‑> bool +
+
+
+ +Expand source code + +
def is_blocked(self, key: Any) -> bool:
+    """Return True if key currently has an active exhausted budget entry."""
+    now = self._clock()
+    with self._lock:
+        entry = self._get_live_entry_unlocked(key, now)
+        return entry is not None and entry.count >= entry.limit
+
+

Return True if key currently has an active exhausted budget entry.

+
+
+def remaining(self, key: Any, *, limit: int) ‑> int +
+
+
+ +Expand source code + +
def remaining(self, key: Any, *, limit: int) -> int:
+    """Return remaining attempt quota for key."""
+    now = self._clock()
+    with self._lock:
+        entry = self._get_live_entry_unlocked(key, now)
+        if entry is None:
+            return max(0, limit)
+        return max(0, limit - entry.count)
+
+

Return remaining attempt quota for key.

+
+
+def remaining_hop(self, host: str, port: str = '', protocol: str = 'ssh', *, limit: int | None = None) ‑> int +
+
+
+ +Expand source code + +
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)
+
+

Return remaining jumphost attempt quota.

+
+
+def remaining_host(self, host: str, port: str = '', protocol: str = 'ssh', *, limit: int | None = None) ‑> int +
+
+
+ +Expand source code + +
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)
+
+

Return remaining target-host attempt quota.

+
+
+def remaining_user(self, user: str, *, limit: int | None = None) ‑> int | None +
+
+
+ +Expand source code + +
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))
+
+

Return remaining global username quota, or None if unlimited.

+
+
+def reset(self) ‑> None +
+
+
+ +Expand source code + +
def reset(self) -> None:
+    """Clear all tracked entries."""
+    with self._lock:
+        self._unblocked.clear()
+        self._blocked.clear()
+
+

Clear all tracked entries.

+
+
+def try_acquire(self, key: Any, *, limit: int) ‑> bool +
+
+
+ +Expand source code + +
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
+
+

Attempt to consume 1 attempt slot for key within limit. Returns False if exhausted.

+
+
+def try_acquire_hop(self, host: str, port: str = '', protocol: str = 'ssh', *, limit: int | None = None) ‑> bool +
+
+
+ +Expand source code + +
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)
+
+

Consume 1 jumphost authentication attempt if within budget.

+
+
+def try_acquire_host(self, host: str, port: str = '', protocol: str = 'ssh', *, limit: int | None = None) ‑> bool +
+
+
+ +Expand source code + +
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)
+
+

Consume 1 target-host authentication attempt if within budget.

+
+
+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]
+
+
+
+ +Expand source code + +
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
+
+

Atomically verify and consume both user and host attempt slots under one lock.

+
+
+def try_acquire_user(self, user: str, *, limit: int | None = None) ‑> bool +
+
+
+ +Expand source code + +
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))
+
+

Consume 1 global username authentication attempt if a global limit is active.

+
+
+
+
+class AuthCandidate +(user: str,
password_spec: str | tuple[str, ...],
is_fallback: bool = False,
label: str = '')
+
+
+
+ +Expand source 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 = ""
+
+

Immutable credential candidate entry for primary or backup authentication.

+

Instance variables

+
+
var is_fallback : bool
+
+
+
+
var label : str
+
+
+
+
var password_spec : str | tuple[str, ...]
+
+
+
+
var user : str
+
+
+
+
+
+
+class RuntimeAuthCandidate +(user: str,
password: str,
has_password: bool,
is_fallback: bool = False,
label: str = '')
+
+
+
+ +Expand source code + +
@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 = ""
+
+

Per-call resolved credential candidate with decrypted secret.

+

Instance variables

+
+
var has_password : bool
+
+
+
+
var is_fallback : bool
+
+
+
+
var label : str
+
+
+
+
var password : str
+
+
+
+
var user : str
+
+
+
+
+
+
+class SanitizedAuthError +(message: str = '', code: str = 'AUTH_FAILED') +
+
+
+ +Expand source code + +
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)))
+
+

Authentication error guaranteed free of secrets and chained tracebacks.

+

Ancestors

+
    +
  • ConnpyError
  • +
  • builtins.Exception
  • +
  • builtins.BaseException
  • +
+
+
+
+
+ +
+ + + diff --git a/docs/connpy/cli/config_handler.html b/docs/connpy/cli/config_handler.html index 88cb308..2af92a8 100644 --- a/docs/connpy/cli/config_handler.html +++ b/docs/connpy/cli/config_handler.html @@ -80,7 +80,12 @@ el.replaceWith(d); "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: @@ -251,7 +256,71 @@ el.replaceWith(d); self.app.services.config_svc.update_setting("shell", shell_cfg) printer.success("Config saved") except (ConnpyError, InvalidConfigurationError) as e: - printer.error(str(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")

Methods

@@ -285,7 +354,12 @@ el.replaceWith(d); "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: @@ -333,6 +407,64 @@ el.replaceWith(d);
+
+def set_auth_budget(self, args) +
+
+
+ +Expand source code + +
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_case(self, args)
@@ -367,6 +499,32 @@ el.replaceWith(d);
+
+def set_disable_fallback_on_insecure_hostkey(self, args) +
+
+
+ +Expand source code + +
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")
+
+
+
def set_fzf(self, args)
@@ -577,11 +735,13 @@ el.replaceWith(d);
  • ConfigHandler

    -
      +
      • dispatch
      • set_ai_config
      • +
      • set_auth_budget
      • set_case
      • set_configfolder
      • +
      • set_disable_fallback_on_insecure_hostkey
      • set_fzf
      • set_idletime
      • set_remote_host
      • diff --git a/docs/connpy/cli/forms.html b/docs/connpy/cli/forms.html index b2d1a73..f17e0e7 100644 --- a/docs/connpy/cli/forms.html +++ b/docs/connpy/cli/forms.html @@ -72,6 +72,7 @@ el.replaceWith(d); 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): @@ -82,11 +83,13 @@ el.replaceWith(d); 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"])) @@ -124,11 +127,13 @@ el.replaceWith(d); 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 not edit.get("fallback"): + 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")] @@ -141,13 +146,29 @@ el.replaceWith(d); passa = inquirer.prompt(passq) if passa is None: return False - answer["password"] = passa["password"].split(",") + answer["password"] = [p.strip() for p in passa["password"].split(",") if p.strip()] 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 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 "") + fbq = [ + inquirer.Text( + "fallback", + message="Add fallback credentials (JSON list/dict, @profile, or compose:)", + validate=self.validators.fallback_validation, + default=def_fb_str.replace("{", "{{").replace("}", "}}"), + ) + ] + fba = inquirer.prompt(fbq) + if fba is None: + return False + answer["fallback"] = _parse_fallback_input(fba["fallback"]) + + if "tags" in answer and answer["tags"] and not str(answer["tags"]).startswith("@"): + answer["tags"] = _parse_tags_input(answer["tags"]) + result = {**uniques, **answer, **node} result["type"] = "connection" return result @@ -160,11 +181,13 @@ el.replaceWith(d); 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"])) @@ -202,18 +225,25 @@ el.replaceWith(d); 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 @@ -232,27 +262,43 @@ el.replaceWith(d); 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"])) - + 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")] passa = inquirer.prompt(passq) + if passa is None: + return False answer["password"] = self.app.services.config_svc.encrypt_password(passa["password"]) elif answer["password"] == "Profiles": passq = [(inquirer.Text("password", message="Set a @profile or a comma separated list of @profiles", validate=self.validators.pass_validation))] passa = inquirer.prompt(passq) - answer["password"] = passa["password"].split(",") + if passa is None: + return False + answer["password"] = [p.strip() for p in passa["password"].split(",") if p.strip()] elif answer["password"] == "No Password": answer["password"] = "" - + + fbq = [ + inquirer.Text( + "fallback", + message="Add fallback credentials (JSON list/dict, @profile, or compose:)", + validate=self.validators.fallback_validation, + ) + ] + fba = inquirer.prompt(fbq) + if fba is None: + return False + answer["fallback"] = _parse_fallback_input(fba["fallback"]) + 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"]) + return answer def mcp_wizard(self, mcp_servers): @@ -454,27 +500,43 @@ el.replaceWith(d); 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"])) - + 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")] passa = inquirer.prompt(passq) + if passa is None: + return False answer["password"] = self.app.services.config_svc.encrypt_password(passa["password"]) elif answer["password"] == "Profiles": passq = [(inquirer.Text("password", message="Set a @profile or a comma separated list of @profiles", validate=self.validators.pass_validation))] passa = inquirer.prompt(passq) - answer["password"] = passa["password"].split(",") + if passa is None: + return False + answer["password"] = [p.strip() for p in passa["password"].split(",") if p.strip()] elif answer["password"] == "No Password": answer["password"] = "" - + + fbq = [ + inquirer.Text( + "fallback", + message="Add fallback credentials (JSON list/dict, @profile, or compose:)", + validate=self.validators.fallback_validation, + ) + ] + fba = inquirer.prompt(fbq) + if fba is None: + return False + answer["fallback"] = _parse_fallback_input(fba["fallback"]) + 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"]) + return answer
        @@ -499,6 +561,7 @@ el.replaceWith(d); 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)
        @@ -519,11 +582,13 @@ el.replaceWith(d); 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"])) @@ -561,11 +626,13 @@ el.replaceWith(d); 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 not edit.get("fallback"): + 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")] @@ -578,13 +645,29 @@ el.replaceWith(d); passa = inquirer.prompt(passq) if passa is None: return False - answer["password"] = passa["password"].split(",") + answer["password"] = [p.strip() for p in passa["password"].split(",") if p.strip()] 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 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 "") + fbq = [ + inquirer.Text( + "fallback", + message="Add fallback credentials (JSON list/dict, @profile, or compose:)", + validate=self.validators.fallback_validation, + default=def_fb_str.replace("{", "{{").replace("}", "}}"), + ) + ] + fba = inquirer.prompt(fbq) + if fba is None: + return False + answer["fallback"] = _parse_fallback_input(fba["fallback"]) + + if "tags" in answer and answer["tags"] and not str(answer["tags"]).startswith("@"): + answer["tags"] = _parse_tags_input(answer["tags"]) + result = {**uniques, **answer, **node} result["type"] = "connection" return result @@ -607,11 +690,13 @@ el.replaceWith(d); 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"])) @@ -649,18 +734,25 @@ el.replaceWith(d); 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 diff --git a/docs/connpy/cli/validators.html b/docs/connpy/cli/validators.html index 1c7c41f..dfc55fc 100644 --- a/docs/connpy/cli/validators.html +++ b/docs/connpy/cli/validators.html @@ -106,35 +106,80 @@ el.replaceWith(d); 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): @@ -273,6 +318,43 @@ el.replaceWith(d);
        +
        +def fallback_validation(self, answers, current) +
        +
        +
        + +Expand source code + +
        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 host_validation(self, answers, current, regex='^.+$')
        @@ -319,6 +401,12 @@ el.replaceWith(d); Expand source code
        def pass_validation(self, answers, current, regex = "(^@.+$)"):
        +    if is_compose_expression(current):
        +        try:
        +            validate_compose_syntax(current)
        +        except SanitizedAuthError as exc:
        +            _raise_val_err(str(exc))
        +        return True
             profiles = current.split(",")
             for i in profiles:
                 if not re.match(regex, i) or i[1:] not in self.app.profiles:
        @@ -413,13 +501,7 @@ el.replaceWith(d);
         
         
        def profile_tags_validation(self, answers, current):
             if current != "":
        -        isdict = False
        -        try:
        -            isdict = ast.literal_eval(current)
        -        except Exception:
        -            pass
        -        if not isinstance (isdict, dict):
        -            _raise_val_err("Tags should be a python dictionary.".format(current))
        +        return self._validate_tags_dict(current)
             return True
        @@ -455,13 +537,7 @@ el.replaceWith(d); if current[1:] not in self.app.profiles: _raise_val_err("Profile {} don't exist".format(current)) elif current != "": - isdict = False - try: - isdict = ast.literal_eval(current) - except Exception: - pass - if not isinstance (isdict, dict): - _raise_val_err("Tags should be a python dictionary.".format(current)) + return self._validate_tags_dict(current) return True
        @@ -490,6 +566,7 @@ el.replaceWith(d);
      • bulk_host_validation
      • bulk_node_validation
      • default_validation
      • +
      • fallback_validation
      • host_validation
      • jumphost_validation
      • pass_validation
      • diff --git a/docs/connpy/grpc_layer/server.html b/docs/connpy/grpc_layer/server.html index 5782d20..089efed 100644 --- a/docs/connpy/grpc_layer/server.html +++ b/docs/connpy/grpc_layer/server.html @@ -55,6 +55,7 @@ el.replaceWith(d);
        def handle_errors(func):
             import inspect
             if inspect.isgeneratorfunction(func):
        +        @functools.wraps(func)
                 def wrapper(*args, **kwargs):
                     try:
                         for item in func(*args, **kwargs):
        @@ -75,6 +76,7 @@ el.replaceWith(d);
                         printer.clear_thread_state()
                 return wrapper
             else:
        +        @functools.wraps(func)
                 def wrapper(*args, **kwargs):
                     try:
                         return func(*args, **kwargs)
        @@ -1425,8 +1427,24 @@ interceptor chooses to service this RPC, or None otherwise.

        self.server_console.print(f"[debug][DEBUG][/debug] gRPC interact_node request for: [bold cyan]{unique_id}[/bold cyan]") if first_req.connection_params_json: - import json - params = json.loads(first_req.connection_params_json) + try: + params = validate_json_depth_and_size( + first_req.connection_params_json, + max_bytes=MAX_JSON_INPUT_BYTES, + max_depth=MAX_JSON_INPUT_DEPTH, + ) + if not isinstance(params, dict): + raise TypeError("connection_params_json must be a JSON object") + if "fallback" in params and params["fallback"] not in (None, "", []): + validate_fallback_entries( + params["fallback"], + max_entries=MAX_FALLBACK_ENTRIES, + ) + if "tags" in params and params["tags"] not in ("", None): + validate_auth_budget_tags(params["tags"]) + except (ValueError, TypeError, SanitizedAuthError) as exc: + context.abort(grpc.StatusCode.INVALID_ARGUMENT, str(exc)) + base_node_id = params.get("base_node") # Valid attributes that a node object accepts valid_attrs = ['host', 'options', 'logs', 'password', 'port', 'protocol', 'user', 'jumphost'] @@ -1444,6 +1462,8 @@ interceptor chooses to service this RPC, or None otherwise.

        for attr in valid_attrs: if attr in params: device[attr] = params[attr] + if "fallback" in params: + device["fallback"] = params["fallback"] if "tags" in params: device_tags = device.get("tags", {}) @@ -1457,20 +1477,30 @@ interceptor chooses to service this RPC, or None otherwise.

        else: # base_node not found, fall back to dynamic node_name = params.get("name", fallback_id) - n = node(node_name, host=params.get("host", ""), config=user_config) - for attr in valid_attrs: - if attr in params: - setattr(n, attr, params[attr]) + dyn_kwargs = {k: params[k] for k in valid_attrs if k in params and k != "host"} + if "fallback" in params: + dyn_kwargs["fallback"] = params["fallback"] if "tags" in params: - n.tags = params["tags"] + dyn_kwargs["tags"] = params["tags"] + n = node( + node_name, + host=params.get("host", ""), + config=user_config, + **dyn_kwargs, + ) else: node_name = params.get("name", fallback_id) - n = node(node_name, host=params.get("host", ""), config=user_config) - for attr in valid_attrs: - if attr in params: - setattr(n, attr, params[attr]) + dyn_kwargs = {k: params[k] for k in valid_attrs if k in params and k != "host"} + if "fallback" in params: + dyn_kwargs["fallback"] = params["fallback"] if "tags" in params: - n.tags = params["tags"] + dyn_kwargs["tags"] = params["tags"] + n = node( + node_name, + host=params.get("host", ""), + config=user_config, + **dyn_kwargs, + ) n._orig_protocol = n.protocol if sftp: n.protocol = "sftp" @@ -1491,9 +1521,11 @@ interceptor chooses to service this RPC, or None otherwise.

        debug_chunks = [] if debug: from io import StringIO + from rich.console import Console as RichConsole - from ..printer import connpy_theme + from .. import printer as _printer + from ..printer import connpy_theme def remote_logger(msg_type, message): buf = StringIO() @@ -1514,7 +1546,14 @@ interceptor chooses to service this RPC, or None otherwise.

        else: remote_logger = None - connect = n._connect(debug=debug, logger=remote_logger) + try: + connect = n._connect( + debug=debug, + logger=remote_logger, + attempt_tracker=getattr(provider, "auth_tracker", None), + ) + except Exception as exc: # noqa: BLE001 + connect = str(sanitize_exception(exc, code="AUTH_FAILED")) # Send debug output to client before checking result (always show the command) for chunk in debug_chunks: @@ -1845,10 +1884,9 @@ interceptor chooses to service this RPC, or None otherwise.

        continue asyncio.run(n._async_interact_loop(remote_stream, resize_callback, copilot_handler=remote_copilot_handler)) - except Exception as e: - import traceback - print(f"[ERROR in run_async_loop] {e}") - traceback.print_exc() + except Exception as e: # noqa: BLE001 + sanitized_err = sanitize_exception(e, code="TRANSPORT_ERROR", context_label="interact stream") + print(f"[ERROR in run_async_loop] {sanitized_err}") finally: n._teardown_interact_environment() response_queue.put(None) # Signal EOF diff --git a/docs/connpy/index.html b/docs/connpy/index.html index b90da42..56ffaa1 100644 --- a/docs/connpy/index.html +++ b/docs/connpy/index.html @@ -41,7 +41,7 @@ el.replaceWith(d);

        App Logo

        -

        Connpy (v6.4.0b4)

        +

        Connpy (v6.5.0b1)

        @@ -142,6 +142,11 @@ conn -a profile profile_name

        # Custom tags dictionary (YANG / VSR context)
         tags: { "os": "cisco_ios", "prompt": ".*#", "screen_length_command": "terminal length 0" }
         
        +

        3f. Credential Fallback Chain, compose: & Auth Budgets

        +

        Configure automatic backup credentials (fallback) on nodes or @profile templates so both interactive (conn <node>) and unattended (conn run / MCP) sessions switch seamlessly when primary TACACS+/AAA authentication fails (opt-out via tags.allow_fallback: false). +* Fallback Formats: Accepts @profile, explicit JSON list/dict entries ([{"user": "backup", "password": "..."}]), or compose: expressions. Nodes referencing multiple profiles inherit fallback with priority from user/password @profile references. +* Password Composition (compose:): Concatenate secrets and literals explicitly using compose:@prof1+@prof2+"suffix" (use @@ to escape leading literal @). +* Anti-Lockout Guardrails (conn config): Enforce strict attempt bounds via --max-auth-prompts, --max-total-auth-attempts, --max-hop-host-attempts, --max-global-user-attempts, and --disable-fallback-on-insecure-hostkey.


        4. 🔌 Protocols & Connection Features

        4a. SSH / SFTP / Mosh / Telnet / kubectl / Docker / AWS SSM

        @@ -156,6 +161,8 @@ tags: { "os": "cisco_ios", "prompt": ".*#&quo

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

        4c. Debug Mode, Keepalive, Logging

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

        +

        4d. Adaptive Multi-Hop Timeouts

        +

        Differentiate --connect-timeout and --auth-timeout stages (conn run / conn config), automatically computing topological per-hop latency budgets for chained SSM and Jumphost tunnels with bounded progress extensions on slow handshakes.


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

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

        @@ -270,6 +277,10 @@ print(response)
        +
        connpy.auth_utils
        +
        +

        Pure authentication, composition, masking, sanitization, and rate-limit helpers.

        +
        connpy.cli
        @@ -739,6 +750,8 @@ class configfile: ''' def __init__(self, conf = None, key = None, shared_config = None): + self._mutation_lock = threading.RLock() + self._cache_dirty = False self._shared_config = shared_config ''' @@ -806,10 +819,10 @@ class configfile: json.dump(old_data, f) shutil.move(legacy_file, legacy_file + ".backup") printer.success(f"Migrated legacy config ({len(old_data.get('connections',{}))} folders/nodes) into YAML and Cache successfully!") - except Exception as e: + except Exception as e: # noqa: BLE001 if os.path.exists(self.file): - try: os.remove(self.file) - except OSError: pass + with suppress(OSError): + os.remove(self.file) printer.warning(f"Failed to migrate legacy config: {e}") else: # Custom path (common in tests): isolate everything to the conf parent directory @@ -904,12 +917,99 @@ class configfile: return val if val is not None else default + def _validate_config_entry_unlocked(self, entry): + """Strictly validate tags, fallback, and password compose syntax on a node or profile entry.""" + if not isinstance(entry, dict): + return + if "tags" in entry and entry["tags"] not in ("", None): + validate_auth_budget_tags(entry["tags"]) + if "fallback" in entry and entry["fallback"] not in (None, "", []): + validate_fallback_entries(entry["fallback"]) + pw = entry.get("password") + if is_compose_expression(pw): + validate_compose_syntax(pw) + elif isinstance(pw, (list, tuple)): + for item in pw: + if is_compose_expression(item): + validate_compose_syntax(item) + + def _walk_connections_validate_unlocked(self, container): + """Recursively validate all connection dicts inside nested folders/subfolders.""" + if not isinstance(container, dict): + raise TypeError("Connections container must be a dict") + for key, val in container.items(): + if key == "type" or not isinstance(val, dict): + continue + entry_type = val.get("type") + if entry_type in ("folder", "subfolder"): + self._walk_connections_validate_unlocked(val) + else: + self._validate_config_entry_unlocked(val) + + def _validate_config_strict(self, data): + """Strictly validate config structure, budgets, and fallback rules; raises on error.""" + if not isinstance(data, dict): + raise TypeError("Config root must be a dict") + required = {"config", "connections", "profiles"} + if not required.issubset(data.keys()): + raise ValueError("Config missing required sections") + if not isinstance(data["config"], dict): + raise TypeError("'config' section must be a dict") + if not isinstance(data["connections"], dict): + raise TypeError("'connections' section must be a dict") + if not isinstance(data["profiles"], dict): + raise TypeError("'profiles' section must be a dict") + validate_config_auth_settings(data["config"]) + self._walk_connections_validate_unlocked(data["connections"]) + for prof_val in data["profiles"].values(): + if not isinstance(prof_val, dict): + raise TypeError("Each profile entry must be a dict") + self._validate_config_entry_unlocked(prof_val) + def _validate_config(self, data): """Verify config data has the required structure.""" - if not isinstance(data, dict): + try: + self._validate_config_strict(data) + except (ValueError, TypeError, SanitizedAuthError): return False - required = {"config", "connections", "profiles"} - return required.issubset(data.keys()) + return True + + def _atomic_write_text_unlocked(self, target_path, writer): + """Atomically write text file with symlink refusal, 0o600 mode, fsync, and os.replace.""" + if os.path.islink(target_path): + raise OSError(f"Refusing to write through symlink: {target_path}") + target_dir = os.path.dirname(os.path.abspath(target_path)) or "." + os.makedirs(target_dir, exist_ok=True) + fd, tmp_path = tempfile.mkstemp( + dir=target_dir, + prefix=f".{os.path.basename(target_path)}.", + suffix=".tmp", + ) + try: + with os.fdopen(fd, "w", encoding="utf-8") as f: + writer(f) + f.flush() + os.fsync(f.fileno()) + os.chmod(tmp_path, 0o600) + os.replace(tmp_path, target_path) + except BaseException: + with suppress(OSError): + os.unlink(tmp_path) + raise + + def _fsync_dir_nonfatal(self, file_path): + target_dir = os.path.dirname(os.path.abspath(file_path)) or "." + try: + dir_fd = os.open(target_dir, os.O_RDONLY) + except OSError: + return + try: + os.fsync(dir_fd) + except OSError: + return + finally: + with suppress(OSError): + os.close(dir_fd) def _loadconfig(self, conf): #Loads config file using dual cache @@ -928,17 +1028,23 @@ class configfile: data = json.load(f) if self._validate_config(data): # Re-write the YAML from good cache - with open(conf, 'w') as f: - yaml.dump(data, f, Dumper=NoAliasDumper, default_flow_style=False, sort_keys=False) + with suppress(OSError): + self._atomic_write_text_unlocked( + conf, + lambda fp: yaml.dump( + data, + fp, + Dumper=NoAliasDumper, + default_flow_style=False, + sort_keys=False, + ), + ) return data # Both broken or no cache - create fresh printer.error("Config file is corrupt and no valid cache exists. Creating default config.") return self._createconfig(conf) - try: - with open(self.cachefile, 'w') as f: - json.dump(data, f) - except Exception: - pass + with suppress(OSError, TypeError, ValueError): + self._atomic_write_text_unlocked(self.cachefile, lambda fp: json.dump(data, fp)) return data else: with open(self.cachefile, 'r') as f: @@ -956,61 +1062,152 @@ class configfile: def _createconfig(self, conf): #Create config file (always writes defaults, safe for recovery) - defaultconfig = {'config': {'case': False, 'idletime': 30, 'fzf': False}, 'connections': {}, 'profiles': { "default": { "host":"", "protocol":"ssh", "port":"", "user":"", "password":"", "options":"", "logs":"", "tags": "", "jumphost":""}}} - with open(conf, "w") as f: - yaml.dump(defaultconfig, f, Dumper=NoAliasDumper, default_flow_style=False, sort_keys=False) - os.chmod(conf, 0o600) - try: - with open(self.cachefile, 'w') as f: - json.dump(defaultconfig, f) - except Exception: - pass + defaultconfig = {'config': {'case': False, 'idletime': 30, 'fzf': False}, 'connections': {}, 'profiles': { "default": { "host":"", "protocol":"ssh", "port":"", "user":"", "password":"", "options":"", "logs":"", "tags": "", "jumphost":"", "fallback": ""}}} + self._atomic_write_text_unlocked( + conf, + lambda f: yaml.dump(defaultconfig, f, Dumper=NoAliasDumper, default_flow_style=False, sort_keys=False), + ) + with suppress(OSError, TypeError, ValueError): + self._atomic_write_text_unlocked(self.cachefile, lambda f: json.dump(defaultconfig, f)) return defaultconfig + def _saveconfig_unlocked(self, conf, *, _raise_on_error=False): + # Save config file atomically inside _mutation_lock without invoking hooks or printer + newconfig = { + "config": self.config, + "connections": self.connections, + "profiles": self.profiles, + } + try: + self._atomic_write_text_unlocked( + conf, + lambda f: yaml.dump( + newconfig, + f, + Dumper=NoAliasDumper, + default_flow_style=False, + sort_keys=False, + ), + ) + except (OSError, TypeError, yaml.YAMLError) as e: + if _raise_on_error: + raise + return 1, f"Failed to save config: {e}" + + # YAML is safely committed on disk; fsync dir and sidecar caches are non-fatal + self._fsync_dir_nonfatal(conf) + try: + self._atomic_write_text_unlocked(self.cachefile, lambda f: json.dump(newconfig, f)) + self._generate_nodes_cache_unlocked(_raise_on_error=True) + self._cache_dirty = False + except (OSError, TypeError, ValueError): + self._cache_dirty = True + return 0, None + + def _run_saveconfig_post_hooks(self, conf, result): + """Run @MethodHook post-hooks registered for _saveconfig outside _mutation_lock.""" + cls_desc = type(self).__dict__.get("_saveconfig") + cls_post = list(cls_desc.post_hooks) if isinstance(cls_desc, MethodHook) else [] + inst_post = ( + self.__dict__.get("_method_post_hooks", {}).get("_saveconfig", []) + if isinstance(self.__dict__.get("_method_post_hooks"), dict) + else [] + ) + all_post = cls_post + [h for h in inst_post if h not in cls_post] + for hook in all_post: + try: + result = hook(self, conf, result=result) + except Exception as e: # noqa: BLE001 + hook_name = getattr(hook, "__name__", str(hook)) + printer.error(f"_saveconfig Post-hook {hook_name} raised an exception: {e}") + return result + @MethodHook def _saveconfig(self, conf): #Save config file atomically to prevent corruption - newconfig = {"config":{}, "connections": {}, "profiles": {}} - newconfig["config"] = self.config - newconfig["connections"] = self.connections - newconfig["profiles"] = self.profiles - tmpfile = conf + '.tmp' - try: - with open(tmpfile, "w") as f: - yaml.dump(newconfig, f, Dumper=NoAliasDumper, default_flow_style=False, sort_keys=False) - # Atomic replace: only overwrite original if write succeeded - shutil.move(tmpfile, conf) - with open(self.cachefile, "w") as f: - json.dump(newconfig, f) - self._generate_nodes_cache() - except (IOError, OSError) as e: - printer.error(f"Failed to save config: {e}") - # Clean up temp file if it exists - if os.path.exists(tmpfile): - try: - os.remove(tmpfile) - except OSError: - pass - return 1 - return 0 + with self._mutation_lock: + rc, err_msg = self._saveconfig_unlocked(conf, _raise_on_error=False) + if err_msg: + printer.error(err_msg) + return rc - def _generate_nodes_cache(self, nodes=None, folders=None, profiles=None): + def transactional_update(self, mutator): + """Run mutator atomically inside _mutation_lock with in-place RAM rollback on error.""" + save_desc = type(self).__dict__.get("_saveconfig") + mocked_save = "_saveconfig" in self.__dict__ or not isinstance(save_desc, MethodHook) + with self._mutation_lock: + snap_config = deepcopy(self.config) + snap_connections = deepcopy(self.connections) + snap_profiles = deepcopy(self.profiles) + try: + result = mutator(self) + self._validate_config_strict( + { + "config": self.config, + "connections": self.connections, + "profiles": self.profiles, + } + ) + if mocked_save: + rc = self._saveconfig(self.file) + if rc != 0: + raise OSError("Failed to save config") + else: + self._saveconfig_unlocked(self.file, _raise_on_error=True) + except BaseException: + self.config.clear() + self.config.update(snap_config) + self.connections.clear() + self.connections.update(snap_connections) + self.profiles.clear() + self.profiles.update(snap_profiles) + raise + if not mocked_save: + self._run_saveconfig_post_hooks(self.file, 0) + return result + + def refresh_cache(self): + """Rebuild JSON and fzf sidecar caches atomically; returns True on success.""" + with self._mutation_lock: + snapshot = { + "config": self.config, + "connections": self.connections, + "profiles": self.profiles, + } + try: + self._atomic_write_text_unlocked(self.cachefile, lambda f: json.dump(snapshot, f)) + self._generate_nodes_cache_unlocked(_raise_on_error=True) + self._cache_dirty = False + return True + except (OSError, TypeError, ValueError): + self._cache_dirty = True + return False + + def _generate_nodes_cache_unlocked(self, nodes=None, folders=None, profiles=None, *, _raise_on_error=False): try: if nodes is None: - nodes = self._getallnodes() + nodes = self._getallnodes_unlocked() if folders is None: - folders = self._getallfolders() + folders = self._getallfolders_unlocked() if profiles is None: profiles = list(self.profiles.keys()) - - with open(self.fzf_cachefile, "w") as f: - f.write("\n".join(nodes)) - with open(self.folders_cachefile, "w") as f: - f.write("\n".join(folders)) - with open(self.profiles_cachefile, "w") as f: - f.write("\n".join(profiles)) - except Exception: - pass + + self._atomic_write_text_unlocked(self.fzf_cachefile, lambda f: f.write("\n".join(nodes))) + self._atomic_write_text_unlocked(self.folders_cachefile, lambda f: f.write("\n".join(folders))) + self._atomic_write_text_unlocked(self.profiles_cachefile, lambda f: f.write("\n".join(profiles))) + except (OSError, TypeError, ValueError): + if _raise_on_error: + raise + self._cache_dirty = True + + def _generate_nodes_cache(self, nodes=None, folders=None, profiles=None): + with self._mutation_lock: + self._generate_nodes_cache_unlocked( + nodes=nodes, + folders=folders, + profiles=profiles, + _raise_on_error=False, + ) def _createkey(self, keyfile): @@ -1023,8 +1220,7 @@ class configfile: os.chmod(keyfile, 0o600) return key - @MethodHook - def _explode_unique(self, unique): + def _explode_unique_unlocked(self, unique): #Divide unique name into folder, subfolder and id uniques = unique.split("@") if not unique.startswith("@"): @@ -1045,30 +1241,193 @@ class configfile: return result @MethodHook - def getitem(self, unique, keys = None, extract = False): - ''' - Get an node or a group of nodes from configfile which can be passed to node/nodes class + def _explode_unique(self, unique): + return self._explode_unique_unlocked(unique) - ### Parameters: + def _resolve_profile_attr_unlocked(self, prof_name, key, *, depth=0, visited=frozenset()): + if prof_name in visited or depth >= 5: + return None + prof = self.profiles.get(prof_name) + if not isinstance(prof, dict) or key not in prof: + return None + val = prof[key] + if key == "password": + if isinstance(val, str): + if is_compose_expression(val): + return val + if val.startswith("@") and val[1:] in self.profiles: + sub = self._resolve_profile_attr_unlocked( + val[1:], + key, + depth=depth + 1, + visited=visited | {prof_name}, + ) + return sub if sub is not None else val + return val + if isinstance(val, list): + resolved_list = [] + for item in val: + if ( + isinstance(item, str) + and not is_compose_expression(item) + and item.startswith("@") + and item[1:] in self.profiles + ): + sub = self._resolve_profile_attr_unlocked( + item[1:], + "password", + depth=depth + 1, + visited=visited | {prof_name}, + ) + if isinstance(sub, list): + resolved_list.extend(deepcopy(sub)) + elif sub is not None: + resolved_list.append(deepcopy(sub)) + else: + resolved_list.append(item) + else: + resolved_list.append(deepcopy(item)) + return resolved_list + return deepcopy(val) - - unique (str): Unique name of the node or folder in config using - connection manager style: node[@subfolder][@folder] - or [@subfolder]@folder + if isinstance(val, str) and val.startswith("@") and val[1:] in self.profiles: + sub = self._resolve_profile_attr_unlocked( + val[1:], + key, + depth=depth + 1, + visited=visited | {prof_name}, + ) + return sub if sub is not None else "" + return deepcopy(val) - ### Optional Parameters: + def _find_inherited_profile_fallback_unlocked(self, raw_user, raw_pw): + """Inherit fallback from a primary @profile referenced by node user/password.""" + candidates = [] + for raw_val, field in ((raw_user, "user"), (raw_pw, "password")): + if isinstance(raw_val, (list, tuple)): + for item in raw_val: + candidates.append((item, field)) + else: + candidates.append((raw_val, field)) - - keys (list): In case you pass a folder as unique, you can filter - nodes inside the folder passing a list. - - extract (bool): If True, extract information from profiles. - Default False. + for candidate_val, field in candidates: + if ( + isinstance(candidate_val, str) + and not is_compose_expression(candidate_val) + and candidate_val.startswith("@") + and "+" not in candidate_val + ): + prof_name = candidate_val[1:] + visited = set() + while prof_name and prof_name not in visited and len(visited) < MAX_PROFILE_DEPTH: + visited.add(prof_name) + prof = self.profiles.get(prof_name) + if not isinstance(prof, dict): + break + prof_fb = prof.get("fallback") + if prof_fb in (None, "", []) and isinstance(prof.get("tags"), dict): + prof_fb = prof["tags"].get("fallback") + if prof_fb not in (None, "", []): + return deepcopy(prof_fb) + next_val = prof.get(field) + if isinstance(next_val, (list, tuple)): + next_ref = next( + ( + item + for item in next_val + if isinstance(item, str) + and not is_compose_expression(item) + and item.startswith("@") + and "+" not in item + ), + None, + ) + prof_name = next_ref[1:] if next_ref else "" + elif ( + isinstance(next_val, str) + and not is_compose_expression(next_val) + and next_val.startswith("@") + and "+" not in next_val + ): + prof_name = next_val[1:] + else: + break + return None - ### Returns: + def _extract_node_fields_unlocked(self, node_dict): + """Resolve @profile references on a copied node dict, keeping legacy core.node(**cfg) contract.""" + raw_user = node_dict.get("user") + raw_pw = node_dict.get("password") + raw_fb = node_dict.pop("fallback", None) - dict: Dictionary containing information of node or multiple - dictionaries of multiple nodes. + for key, value in list(node_dict.items()): + if key == "password": + if isinstance(value, str): + if is_compose_expression(value): + continue + if value.startswith("@") and value[1:] in self.profiles: + resolved = self._resolve_profile_attr_unlocked(value[1:], "password") + node_dict["password"] = resolved if resolved is not None else "" + elif isinstance(value, list): + resolved_pw = [] + for item in value: + if ( + isinstance(item, str) + and not is_compose_expression(item) + and item.startswith("@") + and item[1:] in self.profiles + ): + sub = self._resolve_profile_attr_unlocked(item[1:], "password") + if isinstance(sub, list): + resolved_pw.extend(deepcopy(sub)) + elif sub is not None: + resolved_pw.append(deepcopy(sub)) + else: + resolved_pw.append("") + else: + resolved_pw.append(deepcopy(item)) + node_dict["password"] = resolved_pw + continue - ''' - uniques = self._explode_unique(unique) + if isinstance(value, str) and value.startswith("@"): + ref_name = value[1:] + if ref_name in self.profiles: + res = self._resolve_profile_attr_unlocked(ref_name, key) + node_dict[key] = res if res is not None else "" + else: + node_dict[key] = "" + elif value == "" and key == "protocol": + default_proto = self.profiles.get("default", {}).get("protocol", "ssh") + node_dict[key] = default_proto if default_proto else "ssh" + + tags_fb_present = ( + isinstance(node_dict.get("tags"), dict) + and node_dict["tags"].get("fallback") not in (None, "", []) + ) + if raw_fb in (None, "", []) and not tags_fb_present: + raw_fb = self._find_inherited_profile_fallback_unlocked(raw_user, raw_pw) + + resolved_fb = [] + if raw_fb not in (None, "", []): + resolved_fb = resolve_fallback_entries(raw_fb, self.profiles) + + if resolved_fb: + tags_val = node_dict.get("tags", "") + if isinstance(tags_val, dict): + tags_copy = deepcopy(tags_val) + tags_copy["_fallback"] = resolved_fb + node_dict["tags"] = tags_copy + elif tags_val in ("", None): + node_dict["tags"] = {"_fallback": resolved_fb} + else: + node_dict["tags"] = {"_raw": tags_val, "_fallback": resolved_fb} + elif isinstance(node_dict.get("tags"), dict) and node_dict["tags"].get("fallback") not in (None, "", []): + tags_copy = deepcopy(node_dict["tags"]) + tags_copy["_fallback"] = resolve_fallback_entries(tags_copy["fallback"], self.profiles) + node_dict["tags"] = tags_copy + + def getitem_unlocked(self, unique, keys=None, extract=False): + uniques = self._explode_unique_unlocked(unique) if unique.startswith("@"): if uniques.keys() >= {"folder", "subfolder"}: folder = self.connections[uniques["folder"]][uniques["subfolder"]] @@ -1076,59 +1435,41 @@ class configfile: folder = self.connections[uniques["folder"]] newfolder = deepcopy(folder) newfolder.pop("type") - for node_name in folder.keys(): + for node_name in list(folder.keys()): if node_name == "type": continue - if "type" in newfolder[node_name].keys(): + if "type" in newfolder[node_name]: if newfolder[node_name]["type"] == "subfolder": newfolder.pop(node_name) else: newfolder[node_name].pop("type") - - if keys != None: - newfolder = dict((k, newfolder[k]) for k in keys) - + + if keys is not None: + newfolder = {k: newfolder[k] for k in keys} + if extract: - for node_name, node_keys in newfolder.items(): - for key, value in node_keys.items(): - profile = re.search("^@(.*)", str(value)) - if profile: - try: - newfolder[node_name][key] = self.profiles[profile.group(1)][key] - except KeyError: - newfolder[node_name][key] = "" - elif value == '' and key == "protocol": - try: - newfolder[node_name][key] = self.profiles["default"][key] - except KeyError: - newfolder[node_name][key] = "ssh" - - newfolder = {"{}{}".format(k,unique):v for k,v in newfolder.items()} - return newfolder + for node_keys in newfolder.values(): + self._extract_node_fields_unlocked(node_keys) + + return {f"{k}{unique}": v for k, v in newfolder.items()} + + if uniques.keys() >= {"folder", "subfolder"}: + node = self.connections[uniques["folder"]][uniques["subfolder"]][uniques["id"]] + elif "folder" in uniques: + node = self.connections[uniques["folder"]][uniques["id"]] else: - if uniques.keys() >= {"folder", "subfolder"}: - node = self.connections[uniques["folder"]][uniques["subfolder"]][uniques["id"]] - elif "folder" in uniques.keys(): - node = self.connections[uniques["folder"]][uniques["id"]] - else: - node = self.connections[uniques["id"]] - newnode = deepcopy(node) - newnode.pop("type") - - if extract: - for key, value in newnode.items(): - profile = re.search("^@(.*)", str(value)) - if profile: - try: - newnode[key] = self.profiles[profile.group(1)][key] - except KeyError: - newnode[key] = "" - elif value == '' and key == "protocol": - try: - newnode[key] = self.profiles["default"][key] - except KeyError: - newnode[key] = "ssh" - return newnode + node = self.connections[uniques["id"]] + newnode = deepcopy(node) + newnode.pop("type") + if extract: + self._extract_node_fields_unlocked(newnode) + return newnode + + @MethodHook + def getitem(self, unique, keys = None, extract = False): + """Get a node or folder of nodes from configfile.""" + with self._mutation_lock: + return self.getitem_unlocked(unique, keys=keys, extract=extract) @MethodHook def getitems(self, uniques, extract = False): @@ -1152,47 +1493,94 @@ class configfile: dictionaries of multiple nodes. ''' - nodes = {} if isinstance(uniques, str): uniques = [uniques] - for i in uniques: - if i.startswith("@"): - if not self.config["case"]: - i = i.lower() - this = self.getitem(i, extract = extract) - nodes.update(this) - else: - if not self.config["case"]: - i = i.lower() - this = self.getitem(i, extract = extract) - nodes[i] = this - return nodes + with self._mutation_lock: + nodes = {} + case_sensitive = self.config.get("case", False) + for item in uniques: + target = item if case_sensitive else item.lower() + if target.startswith("@"): + nodes.update(self.getitem_unlocked(target, extract=extract)) + else: + nodes[target] = self.getitem_unlocked(target, extract=extract) + return nodes + def _connections_add_unlocked( + self, + *, + id, + host, + folder='', + subfolder='', + options='', + logs='', + password='', + port='', + protocol='', + user='', + tags='', + jumphost='', + fallback='', + type="connection", + ): + entry = { + "host": host, + "options": options, + "logs": logs, + "password": password, + "port": port, + "protocol": protocol, + "user": user, + "tags": tags, + "jumphost": jumphost, + "fallback": fallback, + "type": type, + } + if folder == '': + self.connections[id] = entry + elif subfolder == '': + self.connections[folder][id] = entry + else: + self.connections[folder][subfolder][id] = entry @MethodHook - def _connections_add(self,*, id, host, folder='', subfolder='', options='', logs='', password='', port='', protocol='', user='', tags='', jumphost='', type = "connection" ): + def _connections_add(self,*, id, host, folder='', subfolder='', options='', logs='', password='', port='', protocol='', user='', tags='', jumphost='', fallback='', type = "connection" ): #Add connection from config - if folder == '': - self.connections[id] = {"host": host, "options": options, "logs": logs, "password": password, "port": port, "protocol": protocol, "user": user, "tags": tags,"jumphost": jumphost,"type": type} - elif folder != '' and subfolder == '': - self.connections[folder][id] = {"host": host, "options": options, "logs": logs, "password": password, "port": port, "protocol": protocol, "user": user, "tags": tags, "jumphost": jumphost, "type": type} - elif folder != '' and subfolder != '': - self.connections[folder][subfolder][id] = {"host": host, "options": options, "logs": logs, "password": password, "port": port, "protocol": protocol, "user": user, "tags": tags, "jumphost": jumphost, "type": type} + with self._mutation_lock: + self._connections_add_unlocked( + id=id, + host=host, + folder=folder, + subfolder=subfolder, + options=options, + logs=logs, + password=password, + port=port, + protocol=protocol, + user=user, + tags=tags, + jumphost=jumphost, + fallback=fallback, + type=type, + ) + def _connections_del_unlocked(self, *, id, folder='', subfolder=''): + if folder == '': + del self.connections[id] + elif subfolder == '': + del self.connections[folder][id] + else: + del self.connections[folder][subfolder][id] + @MethodHook def _connections_del(self,*, id, folder='', subfolder=''): #Delete connection from config - if folder == '': - del self.connections[id] - elif folder != '' and subfolder == '': - del self.connections[folder][id] - elif folder != '' and subfolder != '': - del self.connections[folder][subfolder][id] + with self._mutation_lock: + self._connections_del_unlocked(id=id, folder=folder, subfolder=subfolder) - @MethodHook - def _folder_add(self,*, folder, subfolder = ''): - #Add Folder from config + def _folder_add_unlocked(self, *, folder, subfolder=''): if subfolder == '': if folder not in self.connections: self.connections[folder] = {"type": "folder"} @@ -1201,125 +1589,197 @@ class configfile: self.connections[folder][subfolder] = {"type": "subfolder"} @MethodHook - def _folder_del(self,*, folder, subfolder=''): - #Delete folder from config + def _folder_add(self,*, folder, subfolder = ''): + #Add Folder from config + with self._mutation_lock: + self._folder_add_unlocked(folder=folder, subfolder=subfolder) + + def _folder_del_unlocked(self, *, folder, subfolder=''): if subfolder == '': del self.connections[folder] else: del self.connections[folder][subfolder] + @MethodHook + def _folder_del(self,*, folder, subfolder=''): + #Delete folder from config + with self._mutation_lock: + self._folder_del_unlocked(folder=folder, subfolder=subfolder) + + def _profiles_add_unlocked(self, *, id, host='', options='', logs='', password='', port='', protocol='', user='', tags='', jumphost='', fallback=''): + self.profiles[id] = { + "host": host, + "options": options, + "logs": logs, + "password": password, + "port": port, + "protocol": protocol, + "user": user, + "tags": tags, + "jumphost": jumphost, + "fallback": fallback, + } @MethodHook - def _profiles_add(self,*, id, host = '', options='', logs='', password='', port='', protocol='', user='', tags='', jumphost='' ): + def _profiles_add(self,*, id, host = '', options='', logs='', password='', port='', protocol='', user='', tags='', jumphost='', fallback=''): #Add profile from config - self.profiles[id] = {"host": host, "options": options, "logs": logs, "password": password, "port": port, "protocol": protocol, "user": user, "tags": tags, "jumphost": jumphost} - + with self._mutation_lock: + self._profiles_add_unlocked( + id=id, + host=host, + options=options, + logs=logs, + password=password, + port=port, + protocol=protocol, + user=user, + tags=tags, + jumphost=jumphost, + fallback=fallback, + ) + + def _profiles_del_unlocked(self, *, id): + del self.profiles[id] @MethodHook def _profiles_del(self,*, id ): #Delete profile from config - del self.profiles[id] - - @MethodHook - def _getallnodes(self, filter = None): - #get all nodes on configfile + with self._mutation_lock: + self._profiles_del_unlocked(id=id) + + @staticmethod + def _normalize_filter_arg(filter_arg): + if not filter_arg: + return [] + if isinstance(filter_arg, str): + return [filter_arg] + if isinstance(filter_arg, list): + return [item for item in filter_arg if isinstance(item, str)] + return None + + def _getallnodes_unlocked(self, filter=None): nodes = [] - layer1 = [k for k,v in self.connections.items() if isinstance(v, dict) and v.get("type") == "connection"] - folders = [k for k,v in self.connections.items() if isinstance(v, dict) and v.get("type") == "folder"] + layer1 = [k for k, v in self.connections.items() if isinstance(v, dict) and v.get("type") == "connection"] + folders = [k for k, v in self.connections.items() if isinstance(v, dict) and v.get("type") == "folder"] nodes.extend(layer1) for f in folders: - layer2 = [k + "@" + f for k,v in self.connections[f].items() if isinstance(v, dict) and v.get("type") == "connection"] + layer2 = [k + "@" + f for k, v in self.connections[f].items() if isinstance(v, dict) and v.get("type") == "connection"] nodes.extend(layer2) - subfolders = [k for k,v in self.connections[f].items() if isinstance(v, dict) and v.get("type") == "subfolder"] + subfolders = [k for k, v in self.connections[f].items() if isinstance(v, dict) and v.get("type") == "subfolder"] for s in subfolders: - layer3 = [k + "@" + s + "@" + f for k,v in self.connections[f][s].items() if isinstance(v, dict) and v.get("type") == "connection"] + layer3 = [k + "@" + s + "@" + f for k, v in self.connections[f][s].items() if isinstance(v, dict) and v.get("type") == "connection"] nodes.extend(layer3) if filter: - flat_filter = [] - if isinstance(filter, str): - flat_filter = [filter] - elif isinstance(filter, list): - for item in filter: - if isinstance(item, str): - flat_filter.append(item) - else: - printer.error("Filter must be a string or a list of strings") - sys.exit(1) + flat_filter = self._normalize_filter_arg(filter) flags = re.IGNORECASE if not self.config.get("case", False) else 0 nodes = [item for item in nodes if any(re.search(pattern, item, flags) for pattern in flat_filter)] return nodes @MethodHook - def _getallnodesfull(self, filter = None, extract = True): - #get all nodes on configfile with all their attributes. + def _getallnodes(self, filter = None): + #get all nodes on configfile + if filter and self._normalize_filter_arg(filter) is None: + printer.error("Filter must be a string or a list of strings") + sys.exit(1) + with self._mutation_lock: + return self._getallnodes_unlocked(filter=filter) + + def _getallnodesfull_unlocked(self, filter=None, extract=True): nodes = {} - layer1 = {k:v for k,v in self.connections.items() if isinstance(v, dict) and v.get("type") == "connection"} - folders = [k for k,v in self.connections.items() if isinstance(v, dict) and v.get("type") == "folder"] + layer1 = {k: deepcopy(v) for k, v in self.connections.items() if isinstance(v, dict) and v.get("type") == "connection"} + folders = [k for k, v in self.connections.items() if isinstance(v, dict) and v.get("type") == "folder"] nodes.update(layer1) for f in folders: - layer2 = {k + "@" + f:v for k,v in self.connections[f].items() if isinstance(v, dict) and v.get("type") == "connection"} + layer2 = {k + "@" + f: deepcopy(v) for k, v in self.connections[f].items() if isinstance(v, dict) and v.get("type") == "connection"} nodes.update(layer2) - subfolders = [k for k,v in self.connections[f].items() if isinstance(v, dict) and v.get("type") == "subfolder"] + subfolders = [k for k, v in self.connections[f].items() if isinstance(v, dict) and v.get("type") == "subfolder"] for s in subfolders: - layer3 = {k + "@" + s + "@" + f:v for k,v in self.connections[f][s].items() if isinstance(v, dict) and v.get("type") == "connection"} + layer3 = {k + "@" + s + "@" + f: deepcopy(v) for k, v in self.connections[f][s].items() if isinstance(v, dict) and v.get("type") == "connection"} nodes.update(layer3) if filter: - flat_filter = [] - if isinstance(filter, str): - flat_filter = [filter] - elif isinstance(filter, list): - for item in filter: - if isinstance(item, str): - flat_filter.append(item) - else: - printer.error("Filter must be a string or a list of strings") - sys.exit(1) + flat_filter = self._normalize_filter_arg(filter) flat_filter = ["^(?!.*@).+$" if item == "@" else item for item in flat_filter] nodes = {k: v for k, v in nodes.items() if any(re.search(pattern, k) for pattern in flat_filter)} if extract: - for node, keys in nodes.items(): - for key, value in keys.items(): - profile = re.search("^@(.*)", str(value)) - if profile: - try: - nodes[node][key] = self.profiles[profile.group(1)][key] - except KeyError: - nodes[node][key] = "" - elif value == '' and key == "protocol": - try: - nodes[node][key] = self.profiles["default"][key] - except KeyError: - nodes[node][key] = "ssh" + for node_dict in nodes.values(): + self._extract_node_fields_unlocked(node_dict) return nodes - @MethodHook - def _getallfolders(self): - #get all folders on configfile - folders = ["@" + k for k,v in self.connections.items() if isinstance(v, dict) and v.get("type") == "folder"] + def _getallnodesfull(self, filter = None, extract = True): + #get all nodes on configfile with all their attributes. + if filter and self._normalize_filter_arg(filter) is None: + printer.error("Filter must be a string or a list of strings") + sys.exit(1) + with self._mutation_lock: + return self._getallnodesfull_unlocked(filter=filter, extract=extract) + + def _getallfolders_unlocked(self): + folders = ["@" + k for k, v in self.connections.items() if isinstance(v, dict) and v.get("type") == "folder"] subfolders = [] for f in folders: - s = ["@" + k + f for k,v in self.connections[f[1:]].items() if isinstance(v, dict) and v.get("type") == "subfolder"] + s = ["@" + k + f for k, v in self.connections[f[1:]].items() if isinstance(v, dict) and v.get("type") == "subfolder"] subfolders.extend(s) folders.extend(subfolders) return folders @MethodHook - def _profileused(self, profile): - #Return all the nodes that uses this profile. + def _getallfolders(self): + #get all folders on configfile + with self._mutation_lock: + return self._getallfolders_unlocked() + + def _profileused_unlocked(self, profile): + target_profiles = {profile} + changed = True + while changed: + changed = False + for pname, pval in self.profiles.items(): + if ( + pname not in target_profiles + and isinstance(pval, dict) + and (extract_profile_refs_from_value(pval) & target_profiles) + ): + target_profiles.add(pname) + changed = True + nodes = [] - layer1 = [k for k,v in self.connections.items() if isinstance(v, dict) and v.get("type") == "connection" and ("@" + profile in v.values() or ( isinstance(v.get("password"),list) and "@" + profile in v.get("password")))] - folders = [k for k,v in self.connections.items() if isinstance(v, dict) and v.get("type") == "folder"] + layer1 = [ + k + for k, v in self.connections.items() + if isinstance(v, dict) + and v.get("type") == "connection" + and bool(extract_profile_refs_from_value(v) & target_profiles) + ] + folders = [k for k, v in self.connections.items() if isinstance(v, dict) and v.get("type") == "folder"] nodes.extend(layer1) for f in folders: - layer2 = [k + "@" + f for k,v in self.connections[f].items() if isinstance(v, dict) and v.get("type") == "connection" and ("@" + profile in v.values() or ( isinstance(v.get("password"),list) and "@" + profile in v.get("password")))] + layer2 = [ + k + "@" + f + for k, v in self.connections[f].items() + if isinstance(v, dict) + and v.get("type") == "connection" + and bool(extract_profile_refs_from_value(v) & target_profiles) + ] nodes.extend(layer2) - subfolders = [k for k,v in self.connections[f].items() if isinstance(v, dict) and v.get("type") == "subfolder"] + subfolders = [k for k, v in self.connections[f].items() if isinstance(v, dict) and v.get("type") == "subfolder"] for s in subfolders: - layer3 = [k + "@" + s + "@" + f for k,v in self.connections[f][s].items() if isinstance(v, dict) and v.get("type") == "connection" and ("@" + profile in v.values() or ( isinstance(v.get("password"),list) and "@" + profile in v.get("password")))] + layer3 = [ + k + "@" + s + "@" + f + for k, v in self.connections[f][s].items() + if isinstance(v, dict) + and v.get("type") == "connection" + and bool(extract_profile_refs_from_value(v) & target_profiles) + ] nodes.extend(layer3) return nodes + @MethodHook + def _profileused(self, profile): + #Return all the nodes that uses this profile. + with self._mutation_lock: + return self._profileused_unlocked(profile) + @MethodHook def encrypt(self, password, keyfile=None): ''' @@ -1514,29 +1974,22 @@ def encrypt(self, password, keyfile=None):
        @MethodHook
         def getitem(self, unique, keys = None, extract = False):
        -    '''
        -    Get an node or a group of nodes from configfile which can be passed to node/nodes class
        -
        -    ### Parameters:  
        -
        -        - unique (str): Unique name of the node or folder in config using
        -                        connection manager style: node[@subfolder][@folder]
        -                        or [@subfolder]@folder
        -
        -    ### Optional Parameters:  
        -
        -        - keys (list): In case you pass a folder as unique, you can filter
        -                       nodes inside the folder passing a list.
        -        - extract (bool): If True, extract information from profiles. 
        -                          Default False.
        -
        -    ### Returns:  
        -
        -        dict: Dictionary containing information of node or multiple 
        -              dictionaries of multiple nodes.
        -
        -    '''
        -    uniques = self._explode_unique(unique)
        +    """Get a node or folder of nodes from configfile."""
        +    with self._mutation_lock:
        +        return self.getitem_unlocked(unique, keys=keys, extract=extract)
        + +

        Get a node or folder of nodes from configfile.

        +
        +
        +def getitem_unlocked(self, unique, keys=None, extract=False) +
        +
        +
        + +Expand source code + +
        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"]]
        @@ -1544,76 +1997,37 @@ def getitem(self, unique, keys = None, extract = False):
                     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
        -

        Get an node or a group of nodes from configfile which can be passed to node/nodes class

        -

        Parameters:

        -
        - unique (str): Unique name of the node or folder in config using
        -                connection manager style: node[@subfolder][@folder]
        -                or [@subfolder]@folder
        -
        -

        Optional Parameters:

        -
        - keys (list): In case you pass a folder as unique, you can filter
        -               nodes inside the folder passing a list.
        -- extract (bool): If True, extract information from profiles. 
        -                  Default False.
        -
        -

        Returns:

        -
        dict: Dictionary containing information of node or multiple 
        -      dictionaries of multiple nodes.
        -
        +
        def getitems(self, uniques, extract=False) @@ -1645,21 +2059,18 @@ def getitems(self, uniques, extract = False): dictionaries of multiple nodes. ''' - nodes = {} if isinstance(uniques, str): uniques = [uniques] - for i in uniques: - if i.startswith("@"): - if not self.config["case"]: - i = i.lower() - this = self.getitem(i, extract = extract) - nodes.update(this) - else: - if not self.config["case"]: - i = i.lower() - this = self.getitem(i, extract = extract) - nodes[i] = this - return nodes + with self._mutation_lock: + nodes = {} + case_sensitive = self.config.get("case", False) + for item in uniques: + target = item if case_sensitive else item.lower() + if target.startswith("@"): + nodes.update(self.getitem_unlocked(target, extract=extract)) + else: + nodes[target] = self.getitem_unlocked(target, extract=extract) + return nodes

        Get a group of nodes from configfile which can be passed to node/nodes class

        Parameters:

        @@ -1676,11 +2087,83 @@ def getitems(self, uniques, extract = False): dictionaries of multiple nodes.
        +
        +def refresh_cache(self) +
        +
        +
        + +Expand source code + +
        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
        +
        +

        Rebuild JSON and fzf sidecar caches atomically; returns True on success.

        +
        +
        +def transactional_update(self, mutator) +
        +
        +
        + +Expand source code + +
        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
        +
        +

        Run mutator atomically inside _mutation_lock with in-place RAM rollback on error.

        +
        class node -(unique,
        host,
        options='',
        logs='',
        password='',
        port='',
        protocol='',
        user='',
        config='',
        tags='',
        jumphost='',
        allow_unsafe_options=None)
        +(unique,
        host,
        options='',
        logs='',
        password='',
        port='',
        protocol='',
        user='',
        config='',
        tags='',
        jumphost='',
        allow_unsafe_options=None,
        fallback=None,
        disable_fallback_on_insecure_hostkey=None)
        @@ -1705,7 +2188,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: @@ -1757,34 +2256,132 @@ 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 in attr: - profile = re.search("^@(.*)", str(attr[key])) + 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[key] == '' and key == "protocol": + 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: - setattr(self,key,attr[key]) + setattr(self, key, "ssh") + else: + setattr(self, key, attr_val) self._orig_protocol = self.protocol - if isinstance(password,list): + if isinstance(password, list): self.password = [] - for i, s in enumerate(password): - profile = re.search("^@(.*)", password[i]) - if profile and config != '': - self.password.append(config.profiles[profile.group(1)]["password"]) + for item in 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(password[i]) + 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 != '': - raw_cmd, jh_passwords = self._build_jumphost_chain(self.jumphost, config) + chain_res = self._build_jumphost_chain(self.jumphost, config, include_meta=True) + if len(chain_res) == 3: + raw_cmd, jh_passwords, self._hop_chain_meta = chain_res + 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: @@ -1792,38 +2389,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 in jh_dict: - profile = re.search("^@(.*)", str(jh_dict[key])) + 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_dict[key] == '' and key == "protocol": + 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: @@ -1861,13 +2519,12 @@ class node: if isinstance(self.port, (bool, float)): raise ValueError(f"Invalid TCP SSH port '{self.port}'") port_s = str(self.port).strip() - if port_s: - if ( - not port_s.isascii() - or not re.fullmatch(r"[0-9]+", port_s, re.ASCII) - or not (1 <= int(port_s) <= 65535) - ): - raise ValueError(f"Invalid TCP SSH port '{self.port}'") + if port_s and ( + not port_s.isascii() + or not re.fullmatch(r"[0-9]+", port_s, re.ASCII) + or not (1 <= int(port_s) <= 65535) + ): + raise ValueError(f"Invalid TCP SSH port '{self.port}'") def _validate_options_and_safety(self): """Validate options string for unbalanced quotes and dangerous control characters.""" @@ -2135,17 +2792,151 @@ class node: return host_bare, port_out, opts_out - def _build_jumphost_chain(self, jumphost_name, config, visited=None, depth=0, target_host="%h", target_port="%p"): - '''Recursively build ProxyCommand for chained jumphosts. + @staticmethod + def _validate_and_prepare_ssh_options( + raw_opts, + *, + field_name: str = "options", + strip_non_tunnel_flags: bool = False, + allow_unsafe: bool = False, + connect_timeout: int | None = None, + ) -> str: + """Validate SSH options against _ALLOWED_SSH_O_KEYS and optionally inject ConnectTimeout/ServerAliveInterval.""" + out_tokens: list[str] = [] + if raw_opts: + ssh_opts = str(raw_opts).strip() + if _CONTROL_CHAR_RE.search(ssh_opts): + raise ValueError(f"Disallowed control characters in {field_name}") + if ssh_opts: + try: + tokens = shlex.split(ssh_opts) + except ValueError as exc: + raise ValueError(f"Invalid {field_name}: unbalanced quotes") from exc + if allow_unsafe: + out_tokens = list(tokens) + else: + idx = 0 + while idx < len(tokens): + tok = tokens[idx] + if tok in _ALLOWED_SSH_NOARG_FLAGS: + if not (strip_non_tunnel_flags and tok in _NON_TUNNEL_SSH_FLAGS): + out_tokens.append(tok) + idx += 1 + elif tok in _ALLOWED_SSH_ARG_FLAGS: + if idx + 1 >= len(tokens) or not tokens[idx + 1].strip(): + raise ValueError( + f"Missing argument for ssh option '{tok}' in {field_name}" + ) + arg_val = tokens[idx + 1] + val_re = _SAFE_SSH_PATH_RE if tok == "-i" else _SAFE_SSH_O_VALUE_RE + if arg_val.startswith("-") or not val_re.fullmatch(arg_val): + raise ValueError( + f"Invalid argument for ssh option '{tok}' in {field_name}" + ) + out_tokens.extend([tok, arg_val]) + idx += 2 + elif tok[:2] in _ALLOWED_SSH_ARG_FLAGS and len(tok) > 2: + flag_prefix = tok[:2] + arg_val = tok[2:] + val_re = _SAFE_SSH_PATH_RE if flag_prefix == "-i" else _SAFE_SSH_O_VALUE_RE + if ( + not arg_val.strip() + or arg_val.startswith("-") + or not val_re.fullmatch(arg_val) + ): + raise ValueError( + f"Invalid argument for ssh option '{flag_prefix}' in {field_name}" + ) + out_tokens.append(tok) + idx += 1 + elif tok == "-o" or tok.startswith("-o"): + if tok == "-o": + if idx + 1 >= len(tokens) or not tokens[idx + 1].strip(): + raise ValueError( + f"Missing argument for ssh option '-o' in {field_name}" + ) + opt_spec = tokens[idx + 1] + step = 2 + else: + opt_spec = tok[2:] + step = 1 + if re.search( + r"(?:localcommand|proxycommand|permitlocalcommand|knownhostscommand)", + opt_spec, + re.IGNORECASE, + ): + raise ValueError( + f"{field_name} cannot contain LocalCommand or ProxyCommand" + ) + m = re.fullmatch(r"([A-Za-z0-9]+)=(.+)", opt_spec, re.ASCII) + if ( + not m + or not m.group(2).strip() + or not _SAFE_SSH_O_VALUE_RE.fullmatch(m.group(2).strip()) + ): + raise ValueError( + f"Disallowed -o directive in {field_name}: '{opt_spec}'" + ) + key_raw = m.group(1) + key_lower = key_raw.lower() + val_str = m.group(2).strip() + if key_lower not in _ALLOWED_SSH_O_KEYS: + raise ValueError( + f"Disallowed -o directive in {field_name}: '{opt_spec}'" + ) + if not (strip_non_tunnel_flags and key_lower == "forwardagent"): + if tok == "-o": + out_tokens.extend(["-o", f"{key_raw}={val_str}"]) + else: + out_tokens.append(f"-o{key_raw}={val_str}") + idx += step + else: + if re.search( + r"(?:localcommand|proxycommand|permitlocalcommand|knownhostscommand)", + tok, + re.IGNORECASE, + ): + raise ValueError( + f"{field_name} cannot contain LocalCommand or ProxyCommand" + ) + raise ValueError( + f"Disallowed ssh option in {field_name}: '{tok}'" + ) + + if connect_timeout is not None: + ct_val = validate_strict_timeout_int(connect_timeout, "connect_timeout") + existing_keys = _extract_ssh_o_keys(out_tokens) + if "connecttimeout" not in existing_keys and "connecttimeout" in _ALLOWED_SSH_O_KEYS: + out_tokens.extend(["-o", f"ConnectTimeout={ct_val}"]) + if ( + "serveraliveinterval" not in existing_keys + and "serveraliveinterval" in _ALLOWED_SSH_O_KEYS + ): + out_tokens.extend(["-o", "ServerAliveInterval=15"]) + + return shlex.join(out_tokens) if out_tokens else "" + + def _build_jumphost_chain( + self, + jumphost_name, + config, + visited=None, + depth=0, + target_host="%h", + target_port="%p", + *, + include_meta=False, + connect_timeout=None, + ): + """Recursively build ProxyCommand for chained jumphosts. Returns: - tuple: (raw_proxy_command, passwords_list) - - raw_proxy_command: Command string to embed in ProxyCommand - - passwords_list: Ordered passwords (innermost first) + tuple: (raw_proxy_command, passwords_list) when include_meta=False, or + (raw_proxy_command, passwords_list, hop_meta) when include_meta=True. Raises: - ValueError: On circular references or exceeding max depth (5). - ''' + ValueError: On circular references, invalid hop fields, or exceeding max depth (5). + """ if depth >= 5: raise ValueError("Jumphost chain exceeds maximum depth of 5 hops") if visited is None: @@ -2158,7 +2949,23 @@ class node: jh = config.getitem(jumphost_name) jh = self._resolve_jumphost_data(jh, config) - if jh["protocol"] == "mosh": + # Unconditional control-character guard on all hop metadata fields + for fld_name in ("host", "user", "port"): + fld_val = jh.get(fld_name) + if fld_val not in (None, "") and _CONTROL_CHAR_RE.search(str(fld_val)): + raise ValueError(f"Control characters disallowed in jumphost {fld_name}") + jh_tags = jh.get("tags") + if isinstance(jh_tags, dict): + for tag_fld in ("profile", "region"): + tag_val = jh_tags.get(tag_fld) + if tag_val not in (None, "") and _CONTROL_CHAR_RE.search(str(tag_val)): + raise ValueError(f"Control characters disallowed in jumphost {tag_fld}") + + if connect_timeout is not None: + connect_timeout = validate_strict_timeout_int(connect_timeout, "connect_timeout") + + jh_proto = jh.get("protocol", "ssh") + if jh_proto == "mosh": jh_host, jh_port, jh_opts = self._validate_mosh_ssh_fields( jh.get("host", ""), jh.get("user", ""), @@ -2166,43 +2973,70 @@ class node: jh.get("tags", ""), strip_non_tunnel_flags=True, ) - elif jh["protocol"] == "ssh": + if connect_timeout is not None: + jh_opts = self._validate_and_prepare_ssh_options( + jh_opts, + field_name="mosh_ssh_options", + strip_non_tunnel_flags=True, + connect_timeout=connect_timeout, + ) + elif jh_proto == "ssh": jh_host, jh_port = self._validate_legacy_ssh_hop_fields( jh.get("host", ""), jh.get("user", ""), jh.get("port", ""), ) - jh_opts = jh.get("options", "") + jh_opts = self._validate_and_prepare_ssh_options( + jh.get("options", ""), + field_name="jumphost options", + allow_unsafe=self._is_unsafe_allowed(), + connect_timeout=connect_timeout, + ) else: - jh_host = jh.get("host", "") + jh_host = str(jh.get("host", "") or "") jh_port = str(jh["port"]).strip() if jh.get("port") not in ("", None) else "" jh_opts = jh.get("options", "") passwords = [] inner_proxy_opt = "" + inner_meta: list[dict[str, str]] = [] # Recursively resolve inner jumphost if jh.get("jumphost", "") != "": - if jh["protocol"] not in ["ssh", "mosh"]: + if jh_proto not in ["ssh", "mosh"]: raise ValueError( - f"Jumphost '{jumphost_name}' uses protocol '{jh['protocol']}' " - f"which does not support chained jumphosts. " - f"Only SSH or Mosh jumphosts can have their own jumphosts." + f"Jumphost '{jumphost_name}' uses protocol '{jh_proto}' " + "which does not support chained jumphosts. " + "Only SSH or Mosh jumphosts can have their own jumphosts." ) parent_port = jh_port if jh_port != "" else "22" - inner_raw_cmd, inner_passwords = self._build_jumphost_chain( - jh["jumphost"], config, visited, depth + 1, target_host=jh_host, target_port=parent_port + inner_res = self._build_jumphost_chain( + jh["jumphost"], + config, + visited, + depth + 1, + target_host=jh_host, + target_port=parent_port, + include_meta=True, + connect_timeout=connect_timeout, ) + if len(inner_res) == 3: + inner_raw_cmd, inner_passwords, inner_meta = inner_res + else: + inner_raw_cmd, inner_passwords = inner_res + inner_meta = [] passwords = inner_passwords - escaped = inner_raw_cmd.replace('\\', '\\\\').replace('"', '\\"') + escaped = inner_raw_cmd.replace("\\", "\\\\").replace('"', '\\"') inner_proxy_opt = f'-o ProxyCommand="{escaped}"' # Collect this hop's passwords if jh["password"] != [""]: passwords = passwords + jh["password"] + hop_meta = [*inner_meta, {"name": jumphost_name, "protocol": jh_proto}] + t_port = target_port if target_port != "" else "22" - if jh["protocol"] in ["ssh", "mosh"]: + if jh_proto in ["ssh", "mosh", "ssm"]: if target_host == "%h" and t_port == "%p": w_target = "%h" w_port = "%p" @@ -2217,7 +3051,7 @@ class node: w_port = t_port # Build raw command based on protocol - if jh["protocol"] in ["ssh", "mosh"]: + if jh_proto in ["ssh", "mosh"]: cmd = f"ssh -W {w_target}:{w_port}" if inner_proxy_opt: cmd += f" {inner_proxy_opt}" @@ -2228,38 +3062,111 @@ class node: jh_user = str(jh.get("user", "") or "") user_host = f"{jh_user}@{jh_host}" if jh_user else jh_host cmd += f" {user_host}" - elif jh["protocol"] == "ssm": - ssm_target = jh["host"] - ssm_cmd = f"aws ssm start-session --target {ssm_target} --document-name AWS-StartSSHSession --parameters 'portNumber=22'" - if isinstance(jh.get("tags"), dict): - if "profile" in jh["tags"]: - ssm_cmd += f" --profile {jh['tags']['profile']}" - if "region" in jh["tags"]: - ssm_cmd += f" --region {jh['tags']['region']}" - if jh["options"] != '': - ssm_cmd += f" {jh['options']}" - bastion_user_part = f"{jh['user']}@{ssm_target}" if jh['user'] else ssm_target - ssh_opts = "" - if isinstance(jh.get("tags"), dict) and "ssh_options" in jh["tags"]: - ssh_opts = f" {jh['tags']['ssh_options']}" - cmd = f"ssh{ssh_opts} -o ProxyCommand='{ssm_cmd}' -W {target_host}:{t_port} {bastion_user_part}" - elif jh["protocol"] in ["kubectl", "docker"]: + elif jh_proto == "ssm": + ssm_target = str(jh.get("host", "") or "") + if ( + ".." in ssm_target + or ssm_target.startswith("-") + or not _SSM_TARGET_RE.fullmatch(ssm_target) + ): + raise ValueError(f"Invalid SSM jumphost target: '{ssm_target}'") + + ssm_user = str(jh.get("user", "") or "") + if ssm_user and (ssm_user.startswith("-") or _LEGACY_SSH_UNSAFE_RE.search(ssm_user)): + raise ValueError(f"Invalid characters in SSM jumphost user: '{ssm_user}'") + + ssm_parts = [ + "aws", + "ssm", + "start-session", + "--target", + ssm_target, + "--document-name", + "AWS-StartSSHSession", + "--parameters", + "portNumber=22", + ] + if isinstance(jh_tags, dict): + for tag_key in ("profile", "region"): + raw_val = jh_tags.get(tag_key) + if raw_val not in (None, ""): + tag_str = str(raw_val) + if ( + ".." in tag_str + or tag_str.startswith("-") + or not _SSM_TAG_RE.fullmatch(tag_str) + ): + raise ValueError(f"Invalid SSM jumphost {tag_key}: '{tag_str}'") + ssm_parts.extend([f"--{tag_key}", tag_str]) + + ssm_opts_raw = str(jh.get("options", "") or "").strip() + if ssm_opts_raw: + if _CONTROL_CHAR_RE.search(ssm_opts_raw) or ( + not self._is_unsafe_allowed() and _SSM_UNSAFE_OPTIONS_RE.search(ssm_opts_raw) + ): + raise ValueError("Invalid characters in SSM jumphost options") + try: + ssm_opt_tokens = shlex.split(ssm_opts_raw) + except ValueError as exc: + raise ValueError("Invalid SSM jumphost options: unbalanced quotes") from exc + ssm_parts.extend(ssm_opt_tokens) + + ssm_cmd = shlex.join(ssm_parts) + raw_ssh_opts = jh_tags.get("ssh_options", "") if isinstance(jh_tags, dict) else "" + ssh_opts = self._validate_and_prepare_ssh_options( + raw_ssh_opts, + field_name="ssm ssh_options", + strip_non_tunnel_flags=True, + allow_unsafe=self._is_unsafe_allowed(), + connect_timeout=connect_timeout, + ) + bastion_user_part = f"{ssm_user}@{ssm_target}" if ssm_user else ssm_target + outer_parts = ["ssh"] + if ssh_opts: + outer_parts.extend(shlex.split(ssh_opts)) + outer_parts.extend( + ["-o", f"ProxyCommand={ssm_cmd}", "-W", f"{w_target}:{w_port}", bastion_user_part] + ) + cmd = shlex.join(outer_parts) + elif jh_proto in ["kubectl", "docker"]: + if not jh_host or jh_host.startswith("-") or _LEGACY_SSH_UNSAFE_RE.search(jh_host): + raise ValueError(f"Invalid characters in {jh_proto} jumphost host: '{jh_host}'") nc_cmd = "nc" - if isinstance(jh.get("tags"), dict) and "nc_command" in jh["tags"]: - nc_cmd = jh["tags"]["nc_command"] - if jh["protocol"] == "kubectl": + if isinstance(jh_tags, dict) and "nc_command" in jh_tags: + nc_cmd = str(jh_tags["nc_command"]) + if _CONTROL_CHAR_RE.search(nc_cmd) or ( + not self._is_unsafe_allowed() and _SSM_UNSAFE_OPTIONS_RE.search(nc_cmd) + ): + raise ValueError(f"Invalid nc_command in {jh_proto} jumphost") + jh_opt_joined = "" + jh_opt_str = str(jh.get("options", "") or "").strip() + if jh_opt_str: + if _CONTROL_CHAR_RE.search(jh_opt_str): + raise ValueError(f"Control characters disallowed in {jh_proto} options") + if not self._is_unsafe_allowed() and _SSM_UNSAFE_OPTIONS_RE.search(jh_opt_str): + raise ValueError(f"Invalid characters in {jh_proto} options") + try: + jh_opt_tokens = shlex.split(jh_opt_str) + except ValueError as exc: + raise ValueError(f"Invalid {jh_proto} options: unbalanced quotes") from exc + jh_opt_joined = shlex.join(jh_opt_tokens) + if jh_proto == "kubectl": cmd = "kubectl exec " - if jh["options"] != '': - cmd += f"{jh['options']} " - cmd += f"{jh['host']} -i -- {nc_cmd} {target_host} {t_port}" + if jh_opt_joined: + cmd += f"{jh_opt_joined} " + cmd += f"{jh_host} -i -- {nc_cmd} {w_target} {w_port}" else: cmd = "docker " - if jh["options"] != '': - cmd += f"{jh['options']} " - cmd += f"exec -i {jh['host']} {nc_cmd} {target_host} {t_port}" + if jh_opt_joined: + cmd += f"{jh_opt_joined} " + cmd += f"exec -i {jh_host} {nc_cmd} {w_target} {w_port}" else: + if include_meta: + return "", passwords, inner_meta return "", passwords + if include_meta: + return cmd, passwords, hop_meta return cmd, passwords @MethodHook @@ -2269,8 +3176,8 @@ class node: if keyfile is None: keyfile = self.key if keyfile is not None: - from Crypto.PublicKey import RSA from Crypto.Cipher import PKCS1_OAEP + from Crypto.PublicKey import RSA with open(keyfile) as f: key = RSA.import_key(f.read()) decryptor = PKCS1_OAEP.new(key) @@ -2281,7 +3188,7 @@ class node: try: decrypted = decryptor.decrypt(ast.literal_eval(passwd)).decode("utf-8") dpass.append(decrypted) - except Exception: + except Exception: # noqa: BLE001 printer.error("Decryption failed: Missing or corrupted key.") printer.info("Verify your RSA key and configuration settings.") sys.exit(1) @@ -2292,7 +3199,7 @@ class node: @MethodHook def _logfile(self, logfile = None): # translate logs variables and generate logs path. - if logfile == None: + if logfile is None: logfile = self.logs logfile = logfile.replace("${unique}", self.unique) logfile = logfile.replace("${host}", self.host) @@ -2310,23 +3217,23 @@ class node: """Remove special ascii characters and process terminal cursor movements to clean logs.""" from .utils import log_cleaner - if var == False: + if not var: try: - with open(logfile, "r") as f: + with open(logfile) as f: t = f.read() - except: + except Exception: # noqa: BLE001 return else: t = logfile result = log_cleaner(t) - if var == False: + if not var: try: with open(logfile, "w") as f: f.write(result) - except: - pass + except Exception as exc: # noqa: BLE001 + _LOGGER.debug("Failed writing cleaned log %s: %s", logfile, exc) return else: return result @@ -2357,7 +3264,6 @@ class node: def _keepalive(self): #Send keepalive ctrl+e when idletime passed without new inputs on interact self.lastinput = time() - t = threading.current_thread() while True: if time() - self.lastinput >= self.idletime: self.child.sendcontrol("e") @@ -2383,28 +3289,31 @@ class node: self.child.logfile_read = self.mylog # Only start disk-logging tasks if logfile is configured - if 'logfile' in dir(self): - if not async_mode: - # Start the _savelog thread (sync mode) - log_thread = threading.Thread(target=self._savelog) - log_thread.daemon = True - log_thread.start() - if 'missingtext' in dir(self): - print(self.child.after.decode(), end='') + if hasattr(self, "logfile") and not async_mode: + # Start the _savelog thread (sync mode) + log_thread = threading.Thread(target=self._savelog) + log_thread.daemon = True + log_thread.start() + if getattr(self, "missingtext", False) and not async_mode: + print(self.child.after.decode(), end="") if self.idletime > 0 and not async_mode: x = threading.Thread(target=self._keepalive) x.daemon = True x.start() - if debug: - if 'mylog' in dir(self): - if not async_mode: - print(self.mylog.getvalue().decode()) + if debug and hasattr(self, "mylog") and not async_mode: + print(self.mylog.getvalue().decode()) def _teardown_interact_environment(self): if 'logfile' in dir(self) and hasattr(self, 'mylog'): with open(self.logfile, "w") as f: f.write(self._logclean(self.mylog.getvalue().decode(), True)) + def _write_clean_logfile(self, raw_log: str) -> None: + """Clean and synchronously write raw log output to self.logfile.""" + cleaned_log = self._logclean(raw_log, True) + with open(self.logfile, "w") as f: + f.write(cleaned_log) + def _is_child_connpy_active(self, child_fd: int) -> bool: if self.protocol != "local": return False @@ -2416,8 +3325,6 @@ class node: if os.path.exists(cmdline_path): with open(cmdline_path, "rb") as f: raw_args = f.read().split(b"\x00") - cmdline_str = " ".join([arg.decode(errors="ignore") for arg in raw_args if arg]) - is_active = False for raw_arg in raw_args: arg_str = raw_arg.decode(errors="ignore") @@ -2429,8 +3336,8 @@ class node: break return is_active - except Exception: - pass + except Exception as exc: # noqa: BLE001 + _LOGGER.debug("Failed checking active connpy process: %s", exc) return False async def _async_interact_loop(self, local_stream, resize_callback, copilot_handler=None): @@ -2534,8 +3441,8 @@ class node: if hasattr(self, 'current_local_stream') and self.current_local_stream is not None: try: await self.current_local_stream.write(f'\x1b]133;B;{pos}\x07'.encode()) - except Exception: - pass + except Exception as exc: # noqa: BLE001 + _LOGGER.debug("Failed writing OSC 133 marker: %s", exc) try: os.write(child_fd, clean_data) @@ -2576,7 +3483,8 @@ class node: skip_newlines = False combined_data = stripped else: - if has_eof: break + if has_eof: + break continue await local_stream.write(combined_data) @@ -2593,8 +3501,8 @@ class node: try: self.child.sendcontrol("e") self.lastinput = time() - except Exception: - pass + except Exception as exc: # noqa: BLE001 + _LOGGER.debug("Keepalive sendcontrol failed: %s", exc) async def savelog_task(): prev_size = 0 @@ -2603,14 +3511,12 @@ class node: current_size = self.mylog.tell() if current_size != prev_size: try: - # Move heavy log cleaning to a thread to avoid freezing the interaction loop + # Move heavy log cleaning & file I/O to a thread to avoid freezing the interaction loop raw_log = self.mylog.getvalue().decode(errors='replace') - cleaned_log = await asyncio.to_thread(self._logclean, raw_log, True) - with open(self.logfile, "w") as f: - f.write(cleaned_log) + await asyncio.to_thread(self._write_clean_logfile, raw_log) prev_size = current_size - except Exception: - pass + except Exception as exc: # noqa: BLE001 + _LOGGER.debug("Periodic savelog sync failed: %s", exc) async def pwd_tracker_task(): import socket @@ -2626,16 +3532,16 @@ class node: last_cwd = new_cwd try: os.chdir(new_cwd) - except Exception: - pass + except Exception as exc: # noqa: BLE001 + _LOGGER.debug("Failed syncing cwd: %s", exc) try: await local_stream.write(f"\033]7;file://{hostname}{new_cwd}\007".encode()) - except Exception: - pass + except Exception as exc: # noqa: BLE001 + _LOGGER.debug("Failed writing OSC 7 update: %s", exc) if isinstance(self.tags, dict): self.tags["cwd"] = new_cwd - except Exception: - pass + except Exception as exc: # noqa: BLE001 + _LOGGER.debug("pwd_tracker_task iteration failed: %s", exc) try: # We wait for either the user (ingress) or the child (egress) to finish @@ -2650,7 +3556,7 @@ class node: if hasattr(self, 'logfile') and hasattr(self, 'mylog'): tasks.append(asyncio.create_task(savelog_task())) - done, pending = await asyncio.wait( + done, _pending = await asyncio.wait( [tasks[0], tasks[1]], return_when=asyncio.FIRST_COMPLETED ) @@ -2660,7 +3566,7 @@ class node: if tasks[0] in done and tasks[1] not in done: try: await asyncio.wait_for(tasks[1], timeout=0.2) - except (asyncio.TimeoutError, asyncio.CancelledError): + except (asyncio.TimeoutError, TimeoutError, asyncio.CancelledError): # noqa: UP041 pass for t in tasks: @@ -2671,19 +3577,17 @@ class node: if hasattr(self, 'logfile') and hasattr(self, 'mylog'): try: raw_log = self.mylog.getvalue().decode(errors='replace') - cleaned_log = await asyncio.to_thread(self._logclean, raw_log, True) - with open(self.logfile, "w") as f: - f.write(cleaned_log) - except Exception: - pass + await asyncio.to_thread(self._write_clean_logfile, raw_log) + except Exception as exc: # noqa: BLE001 + _LOGGER.debug("Final savelog sync failed: %s", exc) finally: loop.remove_reader(child_fd) try: flags = fcntl.fcntl(child_fd, fcntl.F_GETFL) fcntl.fcntl(child_fd, fcntl.F_SETFL, flags & ~os.O_NONBLOCK) - except Exception: - pass + except Exception as exc: # noqa: BLE001 + _LOGGER.debug("Failed restoring blocking mode on child_fd: %s", exc) finally: self.current_local_stream = None local_stream.teardown() @@ -2705,8 +3609,8 @@ class node: slc = self.tags.get("screen_length_command") if hasattr(self, 'tags') and isinstance(self.tags, dict) else None to_send = list(commands) - if slc and slc not in to_send: # avoid duplicates if already there - to_send.insert(0, slc) + if slc and slc not in to_send: # avoid duplicates if already there + to_send.insert(0, slc) # 2. Inject one by one for cmd in to_send: @@ -2717,8 +3621,8 @@ class node: if hasattr(self, 'current_local_stream') and self.current_local_stream is not None: try: await self.current_local_stream.write(f'\x1b]133;B;{log_pos}\x07'.encode()) - except Exception: - pass + except Exception as exc: # noqa: BLE001 + _LOGGER.debug("Failed writing command boundary marker: %s", exc) # Write physically to PTY os.write(child_fd, (cmd + "\n").encode()) @@ -2740,7 +3644,7 @@ class node: Allows multiplexing I/O and handling SIGWINCH events locally without blocking. ''' connect = self._connect(debug=debug, logger=logger) - if connect == True: + if connect is True: try: self._setup_interact_environment(debug=debug, logger=logger, async_mode=True) @@ -2749,8 +3653,8 @@ class node: def resize_callback(rows, cols): try: self.child.setwinsize(rows, cols) - except Exception: - pass + except Exception as exc: # noqa: BLE001 + _LOGGER.debug("Failed resizing child window: %s", exc) # Build local copilot handler copilot_handler = self._build_local_copilot_handler() @@ -2762,7 +3666,7 @@ class node: if logger: logger("error", str(connect)) else: - printer.error(f"Connection failed: {str(connect)}") + printer.error(f"Connection failed: {connect!s}") sys.exit(1) def _build_local_copilot_handler(self): @@ -2772,8 +3676,6 @@ class node: def _copilot_handler(self, config): """Unified copilot handler for local session.""" - import asyncio - import os async def handler(buffer, node_info, stream, child_fd, cmd_byte_positions=None): try: @@ -2838,7 +3740,7 @@ class node: last_len = len(cur_bytes) clean_txt = self._logclean(cur_bytes.decode(errors='replace'), True).strip() - lines = [l.strip() for l in clean_txt.split('\n') if l.strip()] + lines = [line.strip() for line in clean_txt.split('\n') if line.strip()] last_line = lines[-1] if lines else "" if quiet_count >= 2 and re.search(prompt_pattern, last_line): break @@ -2857,7 +3759,7 @@ class node: stream._loop.add_reader(stream.stdin_fd, stream._read_ready) os.write(child_fd, b'\x15\r') - except Exception as e: + except Exception as e: # noqa: BLE001 import traceback print(f"\n[ERROR in Copilot Handler] {e}", flush=True) traceback.print_exc() @@ -2866,7 +3768,22 @@ class node: return handler @MethodHook - def run(self, commands, vars = None,*, folder = '', prompt = r'>$|#$|\$$|>.$|#.$|\$.$', stdout = False, timeout = 10, logger = None): + def run( + self, + commands, + vars=None, + *, + folder='', + prompt=r'>$|#$|\$$|>.$|#.$|\$.$', + stdout=False, + timeout=None, + logger=None, + connect_timeout=None, + auth_timeout=None, + _default_cmd_timeout=10, + unattended=True, + attempt_tracker=None, + ): ''' Run a command or list of commands on the node and return the output. @@ -2907,23 +3824,51 @@ class node: str: Output of the commands you ran on the node. ''' - force_ssh = (self.protocol == "mosh") - connect = self._connect(timeout=timeout, logger=logger, force_ssh=force_ssh) + force_ssh = self.protocol == "mosh" + effective_protocol = "ssh" if force_ssh else self.protocol + try: + _, _, eff_cmd, _, _ = resolve_timeouts( + effective_protocol, + self.tags, + getattr(self, "_hop_chain_meta", None) or [], + timeout=timeout, + connect_timeout=connect_timeout, + auth_timeout=auth_timeout, + _default_cmd_timeout=_default_cmd_timeout, + ) + except ValueError as e: + err = f"Connection failed code: 12\nInvalid timeout configuration: {e}" + self.output = err + self.status = 1 + if logger: + logger("error", f"Connection failed: {err}") + return err + + connect = self._connect( + timeout=timeout, + logger=logger, + force_ssh=force_ssh, + 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 == True: + if connect is True: if logger: - effective_protocol = "ssh" if force_ssh else self.protocol port_str = f":{self.port}" if self.port and effective_protocol not in ["ssm", "kubectl", "docker"] else "" logger("success", f"Connected to {self.unique} at {self.host}{port_str} via: {effective_protocol}") - if "prompt" in self.tags: + if isinstance(self.tags, dict) and "prompt" in self.tags: prompt = self.tags["prompt"] - expects = [prompt, pexpect.EOF, pexpect.TIMEOUT] + prompt_pat = re.compile(prompt.encode("utf-8"), re.DOTALL) + expects = [prompt_pat, pexpect.EOF, pexpect.TIMEOUT] output = '' - status = '' + result = 0 if not isinstance(commands, list): commands = [commands] - if "screen_length_command" in self.tags: + if isinstance(self.tags, dict) and "screen_length_command" in self.tags: commands.insert(0, self.tags["screen_length_command"]) self.mylog = io.BytesIO() self.child.logfile_read = self.mylog @@ -2935,34 +3880,33 @@ class node: self.output = f"Error: Variable {e} not defined in task or inventory" self.status = 1 return self.output - result = self.child.expect(expects, timeout = timeout) + result = self.child.expect(expects, timeout=eff_cmd) # Only set terminal size on devices without a # screen_length_command (e.g. Linux/bash servers). # Routers already disable pagination via that command. # After setwinsize, consume any SIGWINCH re-render # prompt (~40ms on bash) with a short timeout. - if c == commands[0] and "screen_length_command" not in self.tags: + if c == commands[0] and (not isinstance(self.tags, dict) or "screen_length_command" not in self.tags): try: self.child.setwinsize(65535, 65535) - except Exception: + except Exception: # noqa: BLE001 try: self.child.setwinsize(10000, 10000) - except Exception: - pass - self.child.expect(expects, timeout = 1) + except Exception as exc: # noqa: BLE001 + _LOGGER.debug("Fallback setwinsize failed in run: %s", exc) + self.child.expect(expects, timeout=1) self.child.sendline(c) if result == 2: break - if not result == 2: - result = self.child.expect(expects, timeout = timeout) + if result != 2: + result = self.child.expect(expects, timeout=eff_cmd) self.child.close() - output = self._logclean(self.mylog.getvalue().decode(), True) + output = self._logclean(self.mylog.getvalue().decode("utf-8", errors="replace"), True) if logger: logger("output", output) if folder != '': with open(folder + "/" + self.unique + "_" + now + ".txt", "w") as f: f.write(output) - f.close() self.output = output if result == 2: self.status = 2 @@ -2977,12 +3921,25 @@ class node: if folder != '': with open(folder + "/" + self.unique + "_" + now + ".txt", "w") as f: f.write(connect) - - f.close() return connect @MethodHook - def test(self, commands, expected, vars = None,*, folder = '', prompt = r'>$|#$|\$$|>.$|#.$|\$.$', timeout = 10, logger = None): + def test( + self, + commands, + expected, + vars=None, + *, + folder='', + prompt=r'>$|#$|\$$|>.$|#.$|\$.$', + timeout=None, + logger=None, + connect_timeout=None, + auth_timeout=None, + _default_cmd_timeout=10, + unattended=True, + attempt_tracker=None, + ): ''' Run a command or list of commands on the node, then check if expected value appears on the output after the last command. @@ -3026,23 +3983,53 @@ class node: ''' now = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S") - force_ssh = (self.protocol == "mosh") - connect = self._connect(timeout=timeout, logger=logger, force_ssh=force_ssh) - if connect == True: + force_ssh = self.protocol == "mosh" + effective_protocol = "ssh" if force_ssh else self.protocol + try: + _, _, eff_cmd, _, _ = resolve_timeouts( + effective_protocol, + self.tags, + getattr(self, "_hop_chain_meta", None) or [], + timeout=timeout, + connect_timeout=connect_timeout, + auth_timeout=auth_timeout, + _default_cmd_timeout=_default_cmd_timeout, + ) + except ValueError as e: + err = f"Connection failed code: 12\nInvalid timeout configuration: {e}" + self.result = None + self.output = err + self.status = 1 + if logger: + logger("error", f"Connection failed: {err}") + return err + + connect = self._connect( + timeout=timeout, + logger=logger, + force_ssh=force_ssh, + 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: - effective_protocol = "ssh" if force_ssh else self.protocol port_str = f":{self.port}" if self.port and effective_protocol not in ["ssm", "kubectl", "docker"] else "" logger("success", f"Connected to {self.unique} at {self.host}{port_str} via: {effective_protocol}") - if "prompt" in self.tags: + if isinstance(self.tags, dict) and "prompt" in self.tags: prompt = self.tags["prompt"] - expects = [prompt, pexpect.EOF, pexpect.TIMEOUT] + prompt_pat = re.compile(prompt.encode("utf-8"), re.DOTALL) + expects = [prompt_pat, pexpect.EOF, pexpect.TIMEOUT] output = '' + result = 0 if not isinstance(commands, list): commands = [commands] if not isinstance(expected, list): expected = [expected] - if "screen_length_command" in self.tags: + if isinstance(self.tags, dict) and "screen_length_command" in self.tags: commands.insert(0, self.tags["screen_length_command"]) self.mylog = io.BytesIO() self.child.logfile_read = self.mylog @@ -3054,29 +4041,28 @@ class node: self.output = f"Error: Variable {e} not defined in task or inventory" self.status = 1 return self.output - result = self.child.expect(expects, timeout = timeout) - if c == commands[0] and "screen_length_command" not in self.tags: + result = self.child.expect(expects, timeout=eff_cmd) + if c == commands[0] and (not isinstance(self.tags, dict) or "screen_length_command" not in self.tags): try: self.child.setwinsize(65535, 65535) - except Exception: + except Exception: # noqa: BLE001 try: self.child.setwinsize(10000, 10000) - except Exception: - pass - self.child.expect(expects, timeout = 1) + except Exception as exc: # noqa: BLE001 + _LOGGER.debug("Fallback setwinsize failed in test: %s", exc) + self.child.expect(expects, timeout=1) self.child.sendline(c) if result == 2: break - if not result == 2: - result = self.child.expect(expects, timeout = timeout) + if result != 2: + result = self.child.expect(expects, timeout=eff_cmd) self.child.close() - output = self._logclean(self.mylog.getvalue().decode(), True) + output = self._logclean(self.mylog.getvalue().decode("utf-8", errors="replace"), True) if logger: logger("output", output) if folder != '': with open(folder + "/" + self.unique + "_" + now + ".txt", "w") as f: f.write(output) - f.close() self.output = output if result in [0, 1]: # lastcommand = commands[-1] @@ -3123,8 +4109,53 @@ class node: self.status = 1 return connect + @staticmethod + def _inject_ssh_timeouts(raw_opts: str, connect_timeout: int | None) -> str: + """Append -o ConnectTimeout and -o ServerAliveInterval=15 unless already present.""" + opts_str = str(raw_opts).strip() if raw_opts else "" + if connect_timeout is None: + return opts_str + ct_val = validate_strict_timeout_int(connect_timeout, "connect_timeout") + tokens = shlex.split(opts_str) if opts_str else [] + existing_keys = _extract_ssh_o_keys(tokens) + if "connecttimeout" not in existing_keys and "connecttimeout" in _ALLOWED_SSH_O_KEYS: + tokens.extend(["-o", f"ConnectTimeout={ct_val}"]) + if ( + "serveraliveinterval" not in existing_keys + and "serveraliveinterval" in _ALLOWED_SSH_O_KEYS + ): + tokens.extend(["-o", "ServerAliveInterval=15"]) + return shlex.join(tokens) + + def _effective_jumphost_opt(self, connect_timeout: int | None = None) -> str: + """Return ProxyCommand option, rebuilding hop chain with connect_timeout when set.""" + if ( + connect_timeout is not None + and getattr(self, "_jumphost_name", "") + and getattr(self, "config", "") != "" + ): + chain_res = self._build_jumphost_chain( + self._jumphost_name, + self.config, + include_meta=True, + connect_timeout=connect_timeout, + ) + raw_cmd = chain_res[0] + if raw_cmd: + escaped = raw_cmd.replace("\\", "\\\\").replace('"', '\\"') + return f'-o ProxyCommand="{escaped}"' + return "" + return self.jumphost + @MethodHook - def _generate_ssh_sftp_cmd(self, force_ssh=False, allow_reverse_tunnel_port=None): + def _generate_ssh_sftp_cmd( + self, + force_ssh=False, + allow_reverse_tunnel_port=None, + *, + connect_timeout=None, + user_override=None, + ): self._validate_base_fields(check_port=True) is_from_mosh = ( self.protocol == "mosh" @@ -3133,11 +4164,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, @@ -3162,6 +4198,7 @@ class node: opts = shlex.join(sftp_tokens) else: opts = str(raw_opts).strip() if raw_opts else "" + opts = self._inject_ssh_timeouts(opts, connect_timeout) if opts: cmd += " " + opts target_h = f"[{host_bare}]" if (proto == "sftp" and ":" in host_bare) else host_bare @@ -3172,30 +4209,34 @@ class node: elif proto == "sftp": cmd += " -P " + str(self.port) raw_opts = self.options + opts = "" if raw_opts: opts = str(raw_opts).strip() if proto == "sftp": opts = re.sub(r'(?<!\S)-[XxtTAaNf]\b', '', opts).strip() - if opts: - cmd += " " + opts + opts = self._inject_ssh_timeouts(opts, connect_timeout) + if opts: + cmd += " " + opts target_h = self.host - if self.jumphost: - cmd += " " + self.jumphost - user_host = f"{self.user}@{target_h}" if self.user else target_h + jh_opt = self._effective_jumphost_opt(connect_timeout=connect_timeout) + if jh_opt: + cmd += " " + jh_opt + 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): + 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): @@ -3219,19 +4260,27 @@ class node: if not re.fullmatch(r"^\d+$", port_s) or not (1 <= int(port_s) <= 65535): raise ValueError(f"Invalid TCP SSH port '{self.port}'") ssh_parts.append(f"-p {port_s}") + ssh_opts = "" if isinstance(self.tags, dict) and self.tags.get("mosh_ssh_options"): ssh_opts = str(self.tags["mosh_ssh_options"]).strip() if ssh_opts: - if not self._is_unsafe_allowed(): - if re.search(r'(?:LocalCommand|ProxyCommand|PermitLocalCommand|KnownHostsCommand)', ssh_opts, re.IGNORECASE): - raise ValueError("mosh_ssh_options cannot contain LocalCommand or ProxyCommand") + if not self._is_unsafe_allowed() and re.search( + r'(?:LocalCommand|ProxyCommand|PermitLocalCommand|KnownHostsCommand)', + ssh_opts, + re.IGNORECASE, + ): + raise ValueError("mosh_ssh_options cannot contain LocalCommand or ProxyCommand") try: shlex.split(ssh_opts) except ValueError as exc: raise ValueError("Invalid mosh_ssh_options: unbalanced quotes") from exc - ssh_parts.append(ssh_opts) - if self.jumphost: - ssh_parts.append(self.jumphost) + if connect_timeout is not None: + ssh_opts = self._inject_ssh_timeouts(ssh_opts, connect_timeout) + if ssh_opts: + ssh_parts.append(ssh_opts) + jh_opt = self._effective_jumphost_opt(connect_timeout=connect_timeout) + if jh_opt: + ssh_parts.append(jh_opt) if len(ssh_parts) > 1: raw_ssh = " ".join(ssh_parts) @@ -3242,16 +4291,19 @@ class node: if opts: if re.search(r'--(?:ssh|server|client)(?:=|\s|$)', opts): raise ValueError("Options cannot override --ssh, --server or --client for mosh") - if not self._is_unsafe_allowed(): - if re.search(r'(?:LocalCommand|ProxyCommand|PermitLocalCommand|KnownHostsCommand)', opts, re.IGNORECASE): - raise ValueError("Options cannot contain LocalCommand or ProxyCommand") + if not self._is_unsafe_allowed() and re.search( + r'(?:LocalCommand|ProxyCommand|PermitLocalCommand|KnownHostsCommand)', + opts, + re.IGNORECASE, + ): + raise ValueError("Options cannot contain LocalCommand or ProxyCommand") try: shlex.split(opts) except ValueError as exc: 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) @@ -3325,14 +4377,26 @@ class node: @MethodHook - def _get_cmd(self, force_ssh=False, allow_reverse_tunnel_port=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() + 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": @@ -3347,7 +4411,7 @@ class node: printer.error(f"Invalid protocol: {self.protocol}") sys.exit(1) - def _spawn_child(self, args, protocol=None): + def _spawn_child(self, args, protocol=None, encoding=None, *, _secrets=()): if protocol is None: protocol = getattr(self, "protocol", "") env = None @@ -3359,29 +4423,315 @@ class node: ): env["LC_ALL"] = "C.UTF-8" env.setdefault("TERM", "xterm-256color") + spawn_kwargs = {} + if env is not None: + spawn_kwargs["env"] = env + if encoding is not None: + spawn_kwargs["encoding"] = encoding try: - if env is not None: - return pexpect.spawn(args[0], args[1:], env=env), None - return pexpect.spawn(args[0], args[1:]), None - except pexpect.exceptions.ExceptionPexpect as e: - return None, f"Connection failed: {e}" + return pexpect.spawn(args[0], args[1:], **spawn_kwargs), None + 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 '<default>'}" + ) + 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( self, debug=False, - timeout=10, + timeout=None, max_attempts=3, logger=None, force_ssh=False, allow_reverse_tunnel_port=None, + *, + connect_timeout=None, + auth_timeout=None, + _default_cmd_timeout=10, + _default_max_total_seconds=None, + clock=_time_mod.monotonic, + unattended=False, + attempt_tracker=None, ): + self.__dict__.pop("missingtext", 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" - timeout = max(timeout, 15) + 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: + validate_strict_timeout_int( + max_attempts, + "max_attempts", + min_val=1, + max_val=MAX_STAGE_TIMEOUT_CAP, + ) + eff_ct, eff_at, _eff_cmd, max_ext, max_total = resolve_timeouts( + effective_protocol, + self.tags, + hop_meta, + timeout=timeout, + connect_timeout=connect_timeout, + auth_timeout=auth_timeout, + _default_cmd_timeout=_default_cmd_timeout, + _default_max_total_seconds=_default_max_total_seconds, + ) + 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() @@ -3391,186 +4741,873 @@ class node: return err self.child = child from pexpect import fdpexpect + self.raw_child = fdpexpect.fdspawn(self.child.child_fd) if self.logs != '': self.logfile = self._logfile() return True - cmd = self._get_cmd( - force_ssh=force_ssh, - allow_reverse_tunnel_port=allow_reverse_tunnel_port, + 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 [] + 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", ()), + ) + ): + 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) + + has_configured_passwords = any(c.has_password for c in runtime_candidates) + use_auth_chain = bool(unattended or has_configured_passwords) + 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 = '[p|P]assword:|[u|U]sername:' if effective_protocol != 'telnet' else '[p|P]assword:' - prompt_unanchored = "|".join([t[:-1] if t.endswith("$") else t for t in prompt.split("|")]) - mosh_prompt = r"(?:" + prompt_unanchored + r")\s*(?:\[[0-9;?]*[a-zA-Z])*$" + 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])*$" + + def _precompile_user_pat(pat_str: str): + try: + pat_str.encode("ascii") + except UnicodeEncodeError: + return re.compile(pat_str.encode("utf-8"), re.DOTALL) + return pat_str + + user_prompt_pat = _precompile_user_pat(prompt) + mosh_prompt_pat = _precompile_user_pat(mosh_prompt) expects = { - "ssh": ['yes/no', 'refused', 'supported', 'Invalid|[u|U]sage: ssh', 'ssh-keygen.*\"', 'timeout|timed.out', 'unavailable', 'closed', password_prompt, prompt, 'suspend', pexpect.EOF, pexpect.TIMEOUT, "No route to host", "resolve hostname", "no matching", "[b|B]ad (owner|permissions)"], - "sftp": ['yes/no', 'refused', 'supported', 'Invalid|[u|U]sage: sftp', 'ssh-keygen.*\"', 'timeout|timed.out', 'unavailable', 'closed', password_prompt, prompt, 'suspend', pexpect.EOF, pexpect.TIMEOUT, "No route to host", "resolve hostname", "no matching", "[b|B]ad (owner|permissions)"], - "telnet": ['[u|U]sername:', 'refused', 'supported', 'invalid|unrecognized option', 'ssh-keygen.*\"', 'timeout|timed.out', 'unavailable', 'closed', password_prompt, prompt, 'suspend', pexpect.EOF, pexpect.TIMEOUT, "No route to host", "resolve hostname", "no matching", "[b|B]ad (owner|permissions)"], - "kubectl": ['[u|U]sername:', '[r|R]efused', '[E|e]rror', 'DEPRECATED', pexpect.TIMEOUT, password_prompt, prompt, pexpect.EOF, "expired|invalid"], - "docker": ['[u|U]sername:', 'Cannot', '[E|e]rror', 'failed', 'not a docker command', 'unknown', 'unable to resolve', pexpect.TIMEOUT, password_prompt, prompt, pexpect.EOF], - "ssm": ['[u|U]sername:', 'Cannot', '[E|e]rror', 'failed', 'SessionManagerPlugin', '[u|U]nknown', 'unable to resolve', pexpect.TIMEOUT, password_prompt, prompt, pexpect.EOF] + "ssh": [ + 'yes/no', + r'(?m:^(?:(?:ssh|sftp):[^\r\n]{1,256} )?Connection refused(?:\r?\n))', + r'(?im:^(?:(?:ssh|sftp|debug\d+):[^\r\n]{1,256}not supported|Permission denied \([^)\r\n]{1,256}\)\.)(?:\r?\n))', + r'(?im:^(?:(?:ssh|sftp):[^\r\n]{1,256}Invalid|usage:\s*(?:ssh|sftp)\b[^\r\n]{0,256})(?:\r?\n))', + 'ssh-keygen.*"', + r'(?im:^(?:(?:ssh|sftp):[^\r\n]{1,256}(?:Operation timed out|Connection timed out)|kex_exchange_identification:[^\r\n]{1,256}timed out|Connection timed out during banner exchange)(?:\r?\n))', + r'(?im:^(?:ssh|sftp|ssh_exchange_identification):[^\r\n]{1,256}(?:Resource temporarily unavailable|Name or service not known|Temporary failure in name resolution|No address associated with hostname|unavailable)(?:\r?\n))', + r'(?im:^(?:Connection to [^\r\n]{1,256} closed(?: by remote host)?\.?|Connection closed by (?:remote host|foreign host|authenticating user [^\r\n]{1,128}|[^\r\n]{1,256} port \d{1,5})|(?:kex|ssh)_exchange_identification: Connection closed[^\r\n]{0,256})(?:\r?\n))', + password_prompt, + user_prompt_pat, + 'suspend', + pexpect.EOF, + pexpect.TIMEOUT, + r'(?im:^(?:(?:ssh|sftp):[^\r\n]{1,256})?No route to host(?:\r?\n))', + r'(?im:^(?:ssh|sftp):[^\r\n]{1,256}resolve hostname[^\r\n]{0,256}(?:\r?\n))', + r'(?im:^(?:(?:ssh|sftp):|Unable to negotiate with )[^\r\n]{1,256}no matching[^\r\n]{0,256}(?:\r?\n))', + r'(?im:^Bad (?:owner|permissions)[^\r\n]{0,256}(?:\r?\n))', + ], + "sftp": [ + 'yes/no', + r'(?m:^(?:(?:ssh|sftp):[^\r\n]{1,256} )?Connection refused(?:\r?\n))', + r'(?im:^(?:(?:ssh|sftp|debug\d+):[^\r\n]{1,256}not supported|Permission denied \([^)\r\n]{1,256}\)\.)(?:\r?\n))', + r'(?im:^(?:(?:ssh|sftp):[^\r\n]{1,256}Invalid|usage:\s*(?:ssh|sftp)\b[^\r\n]{0,256})(?:\r?\n))', + 'ssh-keygen.*"', + r'(?im:^(?:(?:ssh|sftp):[^\r\n]{1,256}(?:Operation timed out|Connection timed out)|kex_exchange_identification:[^\r\n]{1,256}timed out|Connection timed out during banner exchange)(?:\r?\n))', + r'(?im:^(?:ssh|sftp|ssh_exchange_identification):[^\r\n]{1,256}(?:Resource temporarily unavailable|Name or service not known|Temporary failure in name resolution|No address associated with hostname|unavailable)(?:\r?\n))', + r'(?im:^(?:Connection to [^\r\n]{1,256} closed(?: by remote host)?\.?|Connection closed by (?:remote host|foreign host|authenticating user [^\r\n]{1,128}|[^\r\n]{1,256} port \d{1,5})|(?:kex|ssh)_exchange_identification: Connection closed[^\r\n]{0,256})(?:\r?\n))', + password_prompt, + user_prompt_pat, + 'suspend', + pexpect.EOF, + pexpect.TIMEOUT, + r'(?im:^(?:(?:ssh|sftp):[^\r\n]{1,256})?No route to host(?:\r?\n))', + r'(?im:^(?:ssh|sftp):[^\r\n]{1,256}resolve hostname[^\r\n]{0,256}(?:\r?\n))', + r'(?im:^(?:(?:ssh|sftp):|Unable to negotiate with )[^\r\n]{1,256}no matching[^\r\n]{0,256}(?:\r?\n))', + r'(?im:^Bad (?:owner|permissions)[^\r\n]{0,256}(?:\r?\n))', + ], + "telnet": [ + r'[uU]sername:', + r'(?im:^(?:telnet:[^\r\n]{1,256} )?(?:Unable to connect to remote host: )?Connection refused(?:\r?\n))', + r'(?im:^(?:telnet:[^\r\n]{1,256}not supported|[^\r\n]{0,64}Authentication not supported)(?:\r?\n))', + r'(?im:^(?:telnet:|usage:\s*telnet\b)[^\r\n]{0,256}(?:invalid|unrecognized option)[^\r\n]{0,256}(?:\r?\n))', + 'ssh-keygen.*"', + r'(?im:^(?:telnet:[^\r\n]{1,256}(?:Operation timed out|Connection timed out)|Connection timed out)(?:\r?\n))', + r'(?im:^(?:telnet:[^\r\n]{1,256}(?:Name or service not known|Temporary failure in name resolution|unavailable)|[^\r\n]{0,128}: Name or service not known)(?:\r?\n))', + r'(?im:^(?:Connection closed by (?:foreign host|remote host)|telnet:[^\r\n]{1,256}closed)(?:\r?\n))', + password_prompt, + user_prompt_pat, + 'suspend', + pexpect.EOF, + pexpect.TIMEOUT, + r'(?im:^(?:telnet:[^\r\n]{1,256})?No route to host(?:\r?\n))', + r'(?im:^telnet:[^\r\n]{1,256}resolve hostname[^\r\n]{0,256}(?:\r?\n))', + r'(?im:^telnet:[^\r\n]{1,256}no matching[^\r\n]{0,256}(?:\r?\n))', + r'(?im:^Bad (?:owner|permissions)[^\r\n]{0,256}(?:\r?\n))', + ], + "kubectl": [ + r'[uU]sername:', + r'[rR]efused', + r'[Ee]rror', + 'DEPRECATED', + pexpect.TIMEOUT, + password_prompt, + user_prompt_pat, + pexpect.EOF, + "expired|invalid", + ], + "docker": [ + r'[uU]sername:', + 'Cannot', + r'[Ee]rror', + 'failed', + 'not a docker command', + 'unknown', + 'unable to resolve', + pexpect.TIMEOUT, + password_prompt, + user_prompt_pat, + pexpect.EOF, + ], + "ssm": [ + r'[uU]sername:', + r'(?im:^(?:Cannot (?:perform start-session|connect to instance|start session)[^\r\n]{0,256})(?:\r?\n))', + r'(?im:^(?:An error occurred \([^)\r\n]{1,128}\)[^\r\n]{0,256}|aws: error:[^\r\n]{1,512}|Error: (?:Target|An error|Unable|The security token|Invalid|AccessDenied|SessionManager)[^\r\n]{1,256})(?:\r?\n))', + r'(?im:^(?:TargetNotConnected[^\r\n]{0,256}|(?:SessionManagerPlugin|Starting session|An error occurred)[^\r\n]{0,256}failed[^\r\n]{0,256})(?:\r?\n))', + 'SessionManagerPlugin', + r'(?im:^(?:Unknown(?:Error|ClientException)[^\r\n]{0,256}|An error occurred \([^)\r\n]{0,128}Unknown[^)\r\n]{0,128}\)[^\r\n]{0,256}|aws: [^\r\n]{1,256}invalid choice[^\r\n]{0,256})(?:\r?\n))', + r'(?im:^(?:Could not resolve host|Unable to locate credentials|Unable to resolve [^\r\n]{1,256})(?:\r?\n))', + pexpect.TIMEOUT, + password_prompt, + user_prompt_pat, + pexpect.EOF, + ], } 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, # 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) - "[b|B]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) + error_indices = { "ssh": [1, 2, 3, 4, 5, 6, 7, 12, 13, 14, 15, 16], "sftp": [1, 2, 3, 4, 5, 6, 7, 12, 13, 14, 15, 16], "telnet": [1, 2, 3, 4, 5, 6, 7, 12, 13, 14, 15, 16], - "kubectl": [1, 2, 3, 4, 8], # Define error indices for kube - "docker": [1, 2, 3, 4, 5, 6, 7], # Define error indices for docker + "kubectl": [1, 2, 3, 4, 8], + "docker": [1, 2, 3, 4, 5, 6, 7], "ssm": [1, 2, 3, 4, 5, 6, 7], - "mosh": [1, 2, 3, 4, 5, 6, 7, 12, 13, 14, 15, 16, 17, 18, 19] + "mosh": [1, 2, 3, 4, 5, 6, 7, 12, 13, 14, 15, 16, 17, 18, 19], } eof_indices = { "ssh": [8, 9, 10, 11], "sftp": [8, 9, 10, 11], "telnet": [8, 9, 10, 11], - "kubectl": [5, 6, 7], # Define eof indices for kube - "docker": [8, 9, 10], # Define eof indices for docker + "kubectl": [5, 6, 7], + "docker": [8, 9, 10], "ssm": [8, 9, 10], - "mosh": [8, 9, 10, 11] + "mosh": [8, 9, 10, 11], } initial_indices = { "ssh": [0], "sftp": [0], "telnet": [0], - "kubectl": [0], # Define special indices for kube - "docker": [0], # Define special indices for docker + "kubectl": [0], + "docker": [0], "ssm": [0], - "mosh": [0] + "mosh": [0], } - attempts = 1 - handshake_progressed = False - while attempts <= max_attempts: + def _to_bytes(val) -> bytes: + if val is None: + return b"" + if isinstance(val, bytes): + return val + return str(val).encode("utf-8", errors="replace") + + def _to_str(val) -> str: + if val is None: + return "" + if isinstance(val, bytes): + 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", ()) + + 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 use_auth_chain: + manual_user = ( + (runtime_candidates[0].user if runtime_candidates else "") + or str(self.user or "") + ) + if manual_user: + child_obj.sendline(manual_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 use_auth_chain: + 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( + f"Connection failed code: 12\nConnection timeout (global deadline {max_total}s exceeded)", + raw_secrets, + ) + + 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 err - if isinstance(self.tags, dict) and self.tags.get("console"): - child.sendline() - if debug: - if logger: - logger("debug", f"Command:\n{cmd}") - self.mylog = io.BytesIO() - self.mylog.write(f"[i] [DEBUG] Command:\r\n {cmd}\r\n".encode()) - child.logfile_read = self.mylog + return _redact_known_secrets(err, raw_secrets) + connected_ok = False + local_mylog = None + 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: + if logger: + logger("debug", f"Command:\n{_redact_known_secrets(cmd, raw_secrets)}") + local_mylog = io.BytesIO() + self.mylog = 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 + + st["stage"] = "connect" + progress_extensions = 0 + st["last_observed_bytes_len"] = 0 - endloop = False - for i in range(len(passwords) if passwords else 1): while True: - results = child.expect(expects[effective_protocol], timeout=timeout) - results_value = expects[effective_protocol][results] - + remaining = deadline - clock() + if remaining <= 0: + return _redact_known_secrets( + f"Connection failed code: 12\nConnection timeout (global deadline {max_total}s exceeded)", + raw_secrets, + ) + 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) > stream_limit: + return _redact_known_secrets( + 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 + 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) - 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]: - try: - child.terminate() - except Exception: - pass - can_retry = ( - results_value == pexpect.TIMEOUT - and attempts != max_attempts - and not (effective_protocol == "mosh" and handshake_progressed) - ) - if can_retry: - attempts += 1 - endloop = True - break - else: - def _to_str(val): - if val is None: - return "" - if hasattr(val, "decode"): - return val.decode() - return str(val) - after = "Connection timeout" if results_value == pexpect.TIMEOUT else _to_str(child.after) - before = _to_str(child.before) - readline_val = _to_str(child.readline()) + if results_value == pexpect.TIMEOUT: + cur_len = len(before_bytes) + 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 st["handshake_progressed"]) + ) + if can_extend: + progress_extensions += 1 + if not budget_warned: + budget_warned = True + if logger: + logger( + "warning", + f"Slow {effective_protocol} handshake making progress; extending connect wait (deadline {max_total}s)", + ) + continue + + if ( + use_auth_chain + and st["stage"] == "auth" + and st["target_creds_sent"] + 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 + + 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: + st["connect_retries"] = int(st["connect_retries"]) + 1 + next_action = "retry_connect" + break + + before_str = _to_str(before_bytes) if effective_protocol == "mosh": - after = self._logclean(after, True) - before = self._logclean(before, True) - readline_val = self._logclean(readline_val, True) - if effective_protocol == "mosh" and results == 18 and ("Nothing received from server" in after or "Nothing received from server" in before): - err_msg = after.strip() if "Nothing received from server" in after else before.strip() - return f"Connection failed code: {results}\n{err_msg} — UDP traffic is blocked or unreachable. Verify firewall rules for UDP ports (60000-61000 or mosh_udp_port)." - return f"Connection failed code: {results}\n{before.lstrip()}{after}{readline_val}".rstrip() - - elif results in eof_indices[effective_protocol]: - if results_value == password_prompt: - if passwords: - child.sendline(passwords[i]) - handshake_progressed = True + before_str = self._logclean(before_str, True) + if remaining <= 0: + after_str = f"Connection timeout (global deadline {max_total}s exceeded)" else: - self.missingtext = True + after_str = "Connection timeout" + return _redact_known_secrets( + f"Connection failed code: {results}\n{before_str.lstrip()}{after_str}".rstrip(), + raw_secrets, + ) + + is_perm_denied = ( + effective_protocol in ("ssh", "sftp", "mosh") + and results == 2 + and b"permission denied" in (before_bytes + after_bytes).lower() + ) + if ( + use_auth_chain + 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 - elif results_value == "suspend": + + 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": + after = self._logclean(after, True) + before = self._logclean(before, True) + readline_val = self._logclean(readline_val, True) + if ( + effective_protocol == "mosh" + and results == 18 + and ("Nothing received from server" in after or "Nothing received from server" in before) + ): + err_msg = ( + after.strip() + if "Nothing received from server" in after + else before.strip() + ) + return _redact_known_secrets( + f"Connection failed code: {results}\n{err_msg} — UDP traffic is blocked or unreachable. Verify firewall rules for UDP ports (60000-61000 or mosh_udp_port).", + raw_secrets, + ) + return _redact_known_secrets( + f"Connection failed code: {results}\n{before.lstrip()}{after}{readline_val}".rstrip(), + raw_secrets, + ) + + elif results in eof_indices[effective_protocol]: + if results == pw_prompt_idx: + 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: + no_pending_hop_pws = all( + hop_pw_idx[i] >= len(hop_runtime_passwords[i]) + for i in range(hop_cursor, len(jumphost_hops)) + ) + if ( + st["target_creds_sent"] + or hop_cursor >= len(jumphost_hops) + or no_pending_hop_pws + ): + 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, + ) + ) + hop_obj = jumphost_hops[hop_cursor] + 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[hop_cursor][hop_pw_idx[hop_cursor]] + ) + hop_pw_idx[hop_cursor] += 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) + 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 ( + use_auth_chain + 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": + b_str = self._logclean(b_str, True) + a_str = self._logclean(a_str, True) + return _redact_known_secrets( + 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 use_auth_chain 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: - endloop = True + 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 endloop: - break - if results_value == pexpect.TIMEOUT: - continue - else: - break - if isinstance(self.tags, dict) and self.tags.get("post_connect_commands"): - cmds = self.tags.get("post_connect_commands") - commands = [cmds] if isinstance(cmds, str) else cmds - for command in commands: - child.sendline(command) - sleep(1) - child.readline(0) - self.child = child - from pexpect import fdpexpect - self.raw_child = fdpexpect.fdspawn(self.child.child_fd) - return True + if next_action in ("retry_connect", "next_candidate"): + continue + + if isinstance(self.tags, dict) and self.tags.get("post_connect_commands"): + cmds = self.tags.get("post_connect_commands") + post_commands = [cmds] if isinstance(cmds, str) else cmds + for command in post_commands: + child.sendline(command) + sleep(1) + child.readline(0) + self.child = child + from pexpect import fdpexpect + + 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: + child.logfile_read = None + except Exception as exc: # noqa: BLE001 + _LOGGER.debug("Cleanup logfile_read reset failed: %s", type(exc).__name__) + try: + child.terminate(force=True) + except Exception as exc: # noqa: BLE001 + _LOGGER.debug("Cleanup terminate failed: %s", type(exc).__name__) + if local_mylog is not None and not getattr(local_mylog, "closed", False): + try: + local_mylog.close() + except Exception as exc: # noqa: BLE001 + _LOGGER.debug("Cleanup local_mylog close failed: %s", type(exc).__name__)

        This class generates a node object. Containts all the information and methods to connect and interact with a device using ssh or telnet.

        Attributes:

        @@ -3644,8 +5681,8 @@ async def inject_commands(self, commands, child_fd, on_inject=None): slc = self.tags.get("screen_length_command") if hasattr(self, 'tags') and isinstance(self.tags, dict) else None to_send = list(commands) - if slc and slc not in to_send: # avoid duplicates if already there - to_send.insert(0, slc) + if slc and slc not in to_send: # avoid duplicates if already there + to_send.insert(0, slc) # 2. Inject one by one for cmd in to_send: @@ -3656,8 +5693,8 @@ async def inject_commands(self, commands, child_fd, on_inject=None): if hasattr(self, 'current_local_stream') and self.current_local_stream is not None: try: await self.current_local_stream.write(f'\x1b]133;B;{log_pos}\x07'.encode()) - except Exception: - pass + except Exception as exc: # noqa: BLE001 + _LOGGER.debug("Failed writing command boundary marker: %s", exc) # Write physically to PTY os.write(child_fd, (cmd + "\n").encode()) @@ -3690,7 +5727,7 @@ def interact(self, debug=False, logger=None): Allows multiplexing I/O and handling SIGWINCH events locally without blocking. ''' connect = self._connect(debug=debug, logger=logger) - if connect == True: + if connect is True: try: self._setup_interact_environment(debug=debug, logger=logger, async_mode=True) @@ -3699,8 +5736,8 @@ def interact(self, debug=False, logger=None): def resize_callback(rows, cols): try: self.child.setwinsize(rows, cols) - except Exception: - pass + except Exception as exc: # noqa: BLE001 + _LOGGER.debug("Failed resizing child window: %s", exc) # Build local copilot handler copilot_handler = self._build_local_copilot_handler() @@ -3712,14 +5749,14 @@ def interact(self, debug=False, logger=None): if logger: logger("error", str(connect)) else: - printer.error(f"Connection failed: {str(connect)}") + printer.error(f"Connection failed: {connect!s}") sys.exit(1)

        Asynchronous interactive session using Smart Tunnel architecture. Allows multiplexing I/O and handling SIGWINCH events locally without blocking.

        -def run(self,
        commands,
        vars=None,
        *,
        folder='',
        prompt='>$|#$|\\$$|>.$|#.$|\\$.$',
        stdout=False,
        timeout=10,
        logger=None)
        +def run(self,
        commands,
        vars=None,
        *,
        folder='',
        prompt='>$|#$|\\$$|>.$|#.$|\\$.$',
        stdout=False,
        timeout=None,
        logger=None,
        connect_timeout=None,
        auth_timeout=None,
        unattended=True,
        attempt_tracker=None)
        @@ -3727,7 +5764,22 @@ Allows multiplexing I/O and handling SIGWINCH events locally without blocking.Expand source code
        @MethodHook
        -def run(self, commands, vars = None,*, folder = '', prompt = r'>$|#$|\$$|>.$|#.$|\$.$', stdout = False, timeout = 10, logger = None):
        +def run(
        +    self,
        +    commands,
        +    vars=None,
        +    *,
        +    folder='',
        +    prompt=r'>$|#$|\$$|>.$|#.$|\$.$',
        +    stdout=False,
        +    timeout=None,
        +    logger=None,
        +    connect_timeout=None,
        +    auth_timeout=None,
        +    _default_cmd_timeout=10,
        +    unattended=True,
        +    attempt_tracker=None,
        +):
             '''
             Run a command or list of commands on the node and return the output.
         
        @@ -3768,23 +5820,51 @@ def run(self, commands, vars = None,*, folder = '', prompt = r'>$
                 str: Output of the commands you ran on the node.
         
             '''
        -    force_ssh = (self.protocol == "mosh")
        -    connect = self._connect(timeout=timeout, logger=logger, force_ssh=force_ssh)
        +    force_ssh = self.protocol == "mosh"
        +    effective_protocol = "ssh" if force_ssh else self.protocol
        +    try:
        +        _, _, eff_cmd, _, _ = resolve_timeouts(
        +            effective_protocol,
        +            self.tags,
        +            getattr(self, "_hop_chain_meta", None) or [],
        +            timeout=timeout,
        +            connect_timeout=connect_timeout,
        +            auth_timeout=auth_timeout,
        +            _default_cmd_timeout=_default_cmd_timeout,
        +        )
        +    except ValueError as e:
        +        err = f"Connection failed code: 12\nInvalid timeout configuration: {e}"
        +        self.output = err
        +        self.status = 1
        +        if logger:
        +            logger("error", f"Connection failed: {err}")
        +        return err
        +
        +    connect = self._connect(
        +        timeout=timeout,
        +        logger=logger,
        +        force_ssh=force_ssh,
        +        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 == True:
        +    if connect is True:
                 if logger:
        -            effective_protocol = "ssh" if force_ssh else self.protocol
                     port_str = f":{self.port}" if self.port and effective_protocol not in ["ssm", "kubectl", "docker"] else ""
                     logger("success", f"Connected to {self.unique} at {self.host}{port_str} via: {effective_protocol}")
         
        -        if "prompt" in self.tags:
        +        if isinstance(self.tags, dict) and "prompt" in self.tags:
                     prompt = self.tags["prompt"]
        -        expects = [prompt, pexpect.EOF, pexpect.TIMEOUT]
        +        prompt_pat = re.compile(prompt.encode("utf-8"), re.DOTALL)
        +        expects = [prompt_pat, pexpect.EOF, pexpect.TIMEOUT]
                 output = ''
        -        status = ''
        +        result = 0
                 if not isinstance(commands, list):
                     commands = [commands]
        -        if "screen_length_command" in self.tags:
        +        if isinstance(self.tags, dict) and "screen_length_command" in self.tags:
                     commands.insert(0, self.tags["screen_length_command"])
                 self.mylog = io.BytesIO()
                 self.child.logfile_read = self.mylog
        @@ -3796,34 +5876,33 @@ def run(self, commands, vars = None,*, folder = '', prompt = r'>$
                             self.output = f"Error: Variable {e} not defined in task or inventory"
                             self.status = 1
                             return self.output
        -            result = self.child.expect(expects, timeout = timeout)
        +            result = self.child.expect(expects, timeout=eff_cmd)
                     # Only set terminal size on devices without a
                     # screen_length_command (e.g. Linux/bash servers).
                     # Routers already disable pagination via that command.
                     # After setwinsize, consume any SIGWINCH re-render
                     # prompt (~40ms on bash) with a short timeout.
        -            if c == commands[0] and "screen_length_command" not in self.tags:
        +            if c == commands[0] and (not isinstance(self.tags, dict) or "screen_length_command" not in self.tags):
                         try:
                             self.child.setwinsize(65535, 65535)
        -                except Exception:
        +                except Exception:  # noqa: BLE001
                             try:
                                 self.child.setwinsize(10000, 10000)
        -                    except Exception:
        -                        pass
        -                self.child.expect(expects, timeout = 1)
        +                    except Exception as exc:  # noqa: BLE001
        +                        _LOGGER.debug("Fallback setwinsize failed in run: %s", exc)
        +                self.child.expect(expects, timeout=1)
                     self.child.sendline(c)
                     if result == 2:
                         break
        -        if not result == 2:
        -            result = self.child.expect(expects, timeout = timeout)
        +        if result != 2:
        +            result = self.child.expect(expects, timeout=eff_cmd)
                 self.child.close()
        -        output = self._logclean(self.mylog.getvalue().decode(), True)
        +        output = self._logclean(self.mylog.getvalue().decode("utf-8", errors="replace"), True)
                 if logger:
                     logger("output", output)
                 if folder != '':
                     with open(folder + "/" + self.unique + "_" + now + ".txt", "w") as f:
                         f.write(output)
        -                f.close()
                 self.output = output
                 if result == 2:
                     self.status = 2
        @@ -3838,8 +5917,6 @@ def run(self, commands, vars = None,*, folder = '', prompt = r'>$
                 if folder != '':
                     with open(folder + "/" + self.unique + "_" + now + ".txt", "w") as f:
                         f.write(connect)
        -
        -                f.close()
                 return connect

        Run a command or list of commands on the node and return the output.

        @@ -3876,7 +5953,7 @@ def run(self, commands, vars = None,*, folder = '', prompt = r'>$
        -def test(self,
        commands,
        expected,
        vars=None,
        *,
        folder='',
        prompt='>$|#$|\\$$|>.$|#.$|\\$.$',
        timeout=10,
        logger=None)
        +def test(self,
        commands,
        expected,
        vars=None,
        *,
        folder='',
        prompt='>$|#$|\\$$|>.$|#.$|\\$.$',
        timeout=None,
        logger=None,
        connect_timeout=None,
        auth_timeout=None,
        unattended=True,
        attempt_tracker=None)
        @@ -3884,7 +5961,22 @@ def run(self, commands, vars = None,*, folder = '', prompt = r'>$ Expand source code
        @MethodHook
        -def test(self, commands, expected, vars = None,*, folder = '', prompt = r'>$|#$|\$$|>.$|#.$|\$.$', timeout = 10, logger = None):
        +def test(
        +    self,
        +    commands,
        +    expected,
        +    vars=None,
        +    *,
        +    folder='',
        +    prompt=r'>$|#$|\$$|>.$|#.$|\$.$',
        +    timeout=None,
        +    logger=None,
        +    connect_timeout=None,
        +    auth_timeout=None,
        +    _default_cmd_timeout=10,
        +    unattended=True,
        +    attempt_tracker=None,
        +):
             '''
             Run a command or list of commands on the node, then check if expected value appears on the output after the last command.
         
        @@ -3928,23 +6020,53 @@ def test(self, commands, expected, vars = None,*, folder = '', prompt =
         
             '''
             now = datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S")
        -    force_ssh = (self.protocol == "mosh")
        -    connect = self._connect(timeout=timeout, logger=logger, force_ssh=force_ssh)
        -    if connect == True:
        +    force_ssh = self.protocol == "mosh"
        +    effective_protocol = "ssh" if force_ssh else self.protocol
        +    try:
        +        _, _, eff_cmd, _, _ = resolve_timeouts(
        +            effective_protocol,
        +            self.tags,
        +            getattr(self, "_hop_chain_meta", None) or [],
        +            timeout=timeout,
        +            connect_timeout=connect_timeout,
        +            auth_timeout=auth_timeout,
        +            _default_cmd_timeout=_default_cmd_timeout,
        +        )
        +    except ValueError as e:
        +        err = f"Connection failed code: 12\nInvalid timeout configuration: {e}"
        +        self.result = None
        +        self.output = err
        +        self.status = 1
        +        if logger:
        +            logger("error", f"Connection failed: {err}")
        +        return err
        +
        +    connect = self._connect(
        +        timeout=timeout,
        +        logger=logger,
        +        force_ssh=force_ssh,
        +        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:
        -            effective_protocol = "ssh" if force_ssh else self.protocol
                     port_str = f":{self.port}" if self.port and effective_protocol not in ["ssm", "kubectl", "docker"] else ""
                     logger("success", f"Connected to {self.unique} at {self.host}{port_str} via: {effective_protocol}")
         
        -        if "prompt" in self.tags:
        +        if isinstance(self.tags, dict) and "prompt" in self.tags:
                     prompt = self.tags["prompt"]
        -        expects = [prompt, pexpect.EOF, pexpect.TIMEOUT]
        +        prompt_pat = re.compile(prompt.encode("utf-8"), re.DOTALL)
        +        expects = [prompt_pat, pexpect.EOF, pexpect.TIMEOUT]
                 output = ''
        +        result = 0
                 if not isinstance(commands, list):
                     commands = [commands]
                 if not isinstance(expected, list):
                     expected = [expected]
        -        if "screen_length_command" in self.tags:
        +        if isinstance(self.tags, dict) and "screen_length_command" in self.tags:
                     commands.insert(0, self.tags["screen_length_command"])
                 self.mylog = io.BytesIO()
                 self.child.logfile_read = self.mylog
        @@ -3956,29 +6078,28 @@ def test(self, commands, expected, vars = None,*, folder = '', prompt =
                             self.output = f"Error: Variable {e} not defined in task or inventory"
                             self.status = 1
                             return self.output
        -            result = self.child.expect(expects, timeout = timeout)
        -            if c == commands[0] and "screen_length_command" not in self.tags:
        +            result = self.child.expect(expects, timeout=eff_cmd)
        +            if c == commands[0] and (not isinstance(self.tags, dict) or "screen_length_command" not in self.tags):
                         try:
                             self.child.setwinsize(65535, 65535)
        -                except Exception:
        +                except Exception:  # noqa: BLE001
                             try:
                                 self.child.setwinsize(10000, 10000)
        -                    except Exception:
        -                        pass
        -                self.child.expect(expects, timeout = 1)
        +                    except Exception as exc:  # noqa: BLE001
        +                        _LOGGER.debug("Fallback setwinsize failed in test: %s", exc)
        +                self.child.expect(expects, timeout=1)
                     self.child.sendline(c)
                     if result == 2:
                         break
        -        if not result == 2:
        -            result = self.child.expect(expects, timeout = timeout)
        +        if result != 2:
        +            result = self.child.expect(expects, timeout=eff_cmd)
                 self.child.close()
        -        output = self._logclean(self.mylog.getvalue().decode(), True)
        +        output = self._logclean(self.mylog.getvalue().decode("utf-8", errors="replace"), True)
                 if logger:
                     logger("output", output)
                 if folder != '':
                     with open(folder + "/" + self.unique + "_" + now + ".txt", "w") as f:
                         f.write(output)
        -                f.close()
                 self.output = output
                 if result in [0, 1]:
                     # lastcommand = commands[-1]
        @@ -4118,21 +6239,37 @@ class nodes:
                 '''
                 self.nodelist = []
                 self.config = config
        -        for n in nodes:
        -            this = node(n, **nodes[n], 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)
        +            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, commands, vars = None,*, folder = None, prompt = None, stdout = None, parallel = 10, timeout = None, on_complete = None, logger = None):
        +    def run(
        +        self,
        +        commands,
        +        vars=None,
        +        *,
        +        folder=None,
        +        prompt=None,
        +        stdout=None,
        +        parallel=10,
        +        timeout=None,
        +        on_complete=None,
        +        logger=None,
        +        connect_timeout=None,
        +        auth_timeout=None,
        +        _default_cmd_timeout=10,
        +        unattended=True,
        +        attempt_tracker=None,
        +    ):
                 '''
                 Run a command or list of commands on all the nodes in nodelist.
         
        @@ -4188,17 +6325,26 @@ class nodes:
                 args = {}
                 nodesargs = {}
                 args["commands"] = commands
        -        if folder != None:
        +        if folder is not None:
                     args["folder"] = folder
                     Path(folder).mkdir(parents=True, exist_ok=True)
        -        if prompt != None:
        +        if prompt is not None:
                     args["prompt"] = prompt
        -        if stdout != None and on_complete is None:
        +        if stdout is not None and on_complete is None:
                     args["stdout"] = stdout
        -        if timeout != None:
        +        if timeout is not None:
                     args["timeout"] = timeout
        +        if connect_timeout is not None:
        +            args["connect_timeout"] = connect_timeout
        +        if auth_timeout is not None:
        +            args["auth_timeout"] = auth_timeout
        +        if _default_cmd_timeout != 10:
        +            args["_default_cmd_timeout"] = _default_cmd_timeout
        +        if unattended is not True:
        +            args["unattended"] = unattended
                 output = {}
                 status = {}
        +        fallback_used = {}
                 tasks = []
         
                 def _run_node(node_obj, node_args, callback):
        @@ -4209,9 +6355,11 @@ class nodes:
         
                 for n in self.nodelist:
                     nodesargs[n.unique] = deepcopy(args)
        -            if vars != None:
        +            if attempt_tracker is not None:
        +                nodesargs[n.unique]["attempt_tracker"] = attempt_tracker
        +            if vars is not None:
                         nodesargs[n.unique]["vars"] = {}
        -                if "__global__" in vars.keys():
        +                if "__global__" in vars:
                             nodesargs[n.unique]["vars"].update(vars["__global__"])
                         for var_key, var_val in vars.items():
                             if var_key == "__global__":
        @@ -4241,12 +6389,31 @@ 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
        -    def test(self, commands, expected, vars = None,*, folder = None, prompt = None, parallel = 10, timeout = None, on_complete = None, logger = None):
        +    def test(
        +        self,
        +        commands,
        +        expected,
        +        vars=None,
        +        *,
        +        folder=None,
        +        prompt=None,
        +        parallel=10,
        +        timeout=None,
        +        on_complete=None,
        +        logger=None,
        +        connect_timeout=None,
        +        auth_timeout=None,
        +        _default_cmd_timeout=10,
        +        unattended=True,
        +        attempt_tracker=None,
        +    ):
                 '''
                 Run a command or list of commands on all the nodes in nodelist, then check if expected value appears on the output after the last command.
         
        @@ -4301,16 +6468,25 @@ class nodes:
                 nodesargs = {}
                 args["commands"] = commands
                 args["expected"] = expected
        -        if folder != None:
        +        if folder is not None:
                     args["folder"] = folder
                     Path(folder).mkdir(parents=True, exist_ok=True)
        -        if prompt != None:
        +        if prompt is not None:
                     args["prompt"] = prompt
        -        if timeout != None:
        +        if timeout is not None:
                     args["timeout"] = timeout
        +        if connect_timeout is not None:
        +            args["connect_timeout"] = connect_timeout
        +        if auth_timeout is not None:
        +            args["auth_timeout"] = auth_timeout
        +        if _default_cmd_timeout != 10:
        +            args["_default_cmd_timeout"] = _default_cmd_timeout
        +        if unattended is not True:
        +            args["unattended"] = unattended
                 output = {}
                 result = {}
                 status = {}
        +        fallback_used = {}
                 tasks = []
         
                 def _test_node(node_obj, node_args, callback):
        @@ -4321,9 +6497,11 @@ class nodes:
         
                 for n in self.nodelist:
                     nodesargs[n.unique] = deepcopy(args)
        -            if vars != None:
        +            if attempt_tracker is not None:
        +                nodesargs[n.unique]["attempt_tracker"] = attempt_tracker
        +            if vars is not None:
                         nodesargs[n.unique]["vars"] = {}
        -                if "__global__" in vars.keys():
        +                if "__global__" in vars:
                             nodesargs[n.unique]["vars"].update(vars["__global__"])
                         for var_key, var_val in vars.items():
                             if var_key == "__global__":
        @@ -4351,9 +6529,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

        This class generates a nodes object. Contains a list of node class objects and methods to run multiple tasks on nodes simultaneously.

        @@ -4394,7 +6574,7 @@ class nodes:

        Methods

        -def run(self,
        commands,
        vars=None,
        *,
        folder=None,
        prompt=None,
        stdout=None,
        parallel=10,
        timeout=None,
        on_complete=None,
        logger=None)
        +def run(self,
        commands,
        vars=None,
        *,
        folder=None,
        prompt=None,
        stdout=None,
        parallel=10,
        timeout=None,
        on_complete=None,
        logger=None,
        connect_timeout=None,
        auth_timeout=None,
        unattended=True,
        attempt_tracker=None)
        @@ -4402,7 +6582,24 @@ class nodes: Expand source code
        @MethodHook
        -def run(self, commands, vars = None,*, folder = None, prompt = None, stdout = None, parallel = 10, timeout = None, on_complete = None, logger = None):
        +def run(
        +    self,
        +    commands,
        +    vars=None,
        +    *,
        +    folder=None,
        +    prompt=None,
        +    stdout=None,
        +    parallel=10,
        +    timeout=None,
        +    on_complete=None,
        +    logger=None,
        +    connect_timeout=None,
        +    auth_timeout=None,
        +    _default_cmd_timeout=10,
        +    unattended=True,
        +    attempt_tracker=None,
        +):
             '''
             Run a command or list of commands on all the nodes in nodelist.
         
        @@ -4458,17 +6655,26 @@ def run(self, commands, vars = None,*, folder = None, prompt = None, stdout = No
             args = {}
             nodesargs = {}
             args["commands"] = commands
        -    if folder != None:
        +    if folder is not None:
                 args["folder"] = folder
                 Path(folder).mkdir(parents=True, exist_ok=True)
        -    if prompt != None:
        +    if prompt is not None:
                 args["prompt"] = prompt
        -    if stdout != None and on_complete is None:
        +    if stdout is not None and on_complete is None:
                 args["stdout"] = stdout
        -    if timeout != None:
        +    if timeout is not None:
                 args["timeout"] = timeout
        +    if connect_timeout is not None:
        +        args["connect_timeout"] = connect_timeout
        +    if auth_timeout is not None:
        +        args["auth_timeout"] = auth_timeout
        +    if _default_cmd_timeout != 10:
        +        args["_default_cmd_timeout"] = _default_cmd_timeout
        +    if unattended is not True:
        +        args["unattended"] = unattended
             output = {}
             status = {}
        +    fallback_used = {}
             tasks = []
         
             def _run_node(node_obj, node_args, callback):
        @@ -4479,9 +6685,11 @@ def run(self, commands, vars = None,*, folder = None, prompt = None, stdout = No
         
             for n in self.nodelist:
                 nodesargs[n.unique] = deepcopy(args)
        -        if vars != None:
        +        if attempt_tracker is not None:
        +            nodesargs[n.unique]["attempt_tracker"] = attempt_tracker
        +        if vars is not None:
                     nodesargs[n.unique]["vars"] = {}
        -            if "__global__" in vars.keys():
        +            if "__global__" in vars:
                         nodesargs[n.unique]["vars"].update(vars["__global__"])
                     for var_key, var_val in vars.items():
                         if var_key == "__global__":
        @@ -4511,8 +6719,10 @@ def run(self, commands, vars = None,*, folder = None, prompt = None, stdout = No
             for i in self.nodelist:
                 output[i.unique] = i.output
                 status[i.unique] = i.status
        +        fallback_used[i.unique] = bool(getattr(i, "last_fallback_used", False))
             self.output = output
             self.status = status
        +    self.fallback_used = fallback_used
             return output

        Run a command or list of commands on all the nodes in nodelist.

        @@ -4561,7 +6771,7 @@ def run(self, commands, vars = None,*, folder = None, prompt = None, stdout = No
        -def test(self,
        commands,
        expected,
        vars=None,
        *,
        folder=None,
        prompt=None,
        parallel=10,
        timeout=None,
        on_complete=None,
        logger=None)
        +def test(self,
        commands,
        expected,
        vars=None,
        *,
        folder=None,
        prompt=None,
        parallel=10,
        timeout=None,
        on_complete=None,
        logger=None,
        connect_timeout=None,
        auth_timeout=None,
        unattended=True,
        attempt_tracker=None)
        @@ -4569,7 +6779,24 @@ def run(self, commands, vars = None,*, folder = None, prompt = None, stdout = No Expand source code
        @MethodHook
        -def test(self, commands, expected, vars = None,*, folder = None, prompt = None, parallel = 10, timeout = None, on_complete = None, logger = None):
        +def test(
        +    self,
        +    commands,
        +    expected,
        +    vars=None,
        +    *,
        +    folder=None,
        +    prompt=None,
        +    parallel=10,
        +    timeout=None,
        +    on_complete=None,
        +    logger=None,
        +    connect_timeout=None,
        +    auth_timeout=None,
        +    _default_cmd_timeout=10,
        +    unattended=True,
        +    attempt_tracker=None,
        +):
             '''
             Run a command or list of commands on all the nodes in nodelist, then check if expected value appears on the output after the last command.
         
        @@ -4624,16 +6851,25 @@ def test(self, commands, expected, vars = None,*, folder = None, prompt = None,
             nodesargs = {}
             args["commands"] = commands
             args["expected"] = expected
        -    if folder != None:
        +    if folder is not None:
                 args["folder"] = folder
                 Path(folder).mkdir(parents=True, exist_ok=True)
        -    if prompt != None:
        +    if prompt is not None:
                 args["prompt"] = prompt
        -    if timeout != None:
        +    if timeout is not None:
                 args["timeout"] = timeout
        +    if connect_timeout is not None:
        +        args["connect_timeout"] = connect_timeout
        +    if auth_timeout is not None:
        +        args["auth_timeout"] = auth_timeout
        +    if _default_cmd_timeout != 10:
        +        args["_default_cmd_timeout"] = _default_cmd_timeout
        +    if unattended is not True:
        +        args["unattended"] = unattended
             output = {}
             result = {}
             status = {}
        +    fallback_used = {}
             tasks = []
         
             def _test_node(node_obj, node_args, callback):
        @@ -4644,9 +6880,11 @@ def test(self, commands, expected, vars = None,*, folder = None, prompt = None,
         
             for n in self.nodelist:
                 nodesargs[n.unique] = deepcopy(args)
        -        if vars != None:
        +        if attempt_tracker is not None:
        +            nodesargs[n.unique]["attempt_tracker"] = attempt_tracker
        +        if vars is not None:
                     nodesargs[n.unique]["vars"] = {}
        -            if "__global__" in vars.keys():
        +            if "__global__" in vars:
                         nodesargs[n.unique]["vars"].update(vars["__global__"])
                     for var_key, var_val in vars.items():
                         if var_key == "__global__":
        @@ -4674,9 +6912,11 @@ def test(self, commands, expected, vars = None,*, folder = None, prompt = None,
                 result[i.unique] = i.result
                 output[i.unique] = i.output
                 status[i.unique] = i.status
        +        fallback_used[i.unique] = bool(getattr(i, "last_fallback_used", False))
             self.output = output
             self.result = result
             self.status = status
        +    self.fallback_used = fallback_used
             return result

        Run a command or list of commands on all the nodes in nodelist, then check if expected value appears on the output after the last command.

        @@ -4730,7 +6970,7 @@ def test(self, commands, expected, vars = None,*, folder = None, prompt = None,
        @@ -1057,8 +1113,11 @@ el.replaceWith(d); 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/docs/connpy/services/exceptions.html b/docs/connpy/services/exceptions.html index 459d464..497ae0c 100644 --- a/docs/connpy/services/exceptions.html +++ b/docs/connpy/services/exceptions.html @@ -67,6 +67,7 @@ el.replaceWith(d);

      Subclasses

        +
      • SanitizedAuthError
      • ExecutionError
      • InvalidConfigurationError
      • NodeAlreadyExistsError
      • diff --git a/docs/connpy/services/execution_service.html b/docs/connpy/services/execution_service.html index 36ef9cf..d996bc9 100644 --- a/docs/connpy/services/execution_service.html +++ b/docs/connpy/services/execution_service.html @@ -48,7 +48,7 @@ el.replaceWith(d);
        class ExecutionService -(config=None) +(config=None, remote_stub=None, attempt_tracker=None)
        @@ -58,104 +58,239 @@ el.replaceWith(d);
        class ExecutionService(BaseService):
             """Business logic for executing commands on nodes and running automation scripts."""
         
        -    def run_commands(
        -        self, 
        -        nodes_filter: str, 
        -        commands: List[str], 
        -        variables: Optional[Dict[str, Any]] = None,
        -        parallel: int = 10,
        -        timeout: int = 20,
        -        folder: Optional[str] = None,
        -        prompt: Optional[str] = None,
        -        on_node_complete: Optional[Callable] = None,
        -        logger: Optional[Callable] = None,
        -        name: Optional[str] = None
        -    ) -> Dict[str, str]:
        +    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."""
        +        if getattr(self, "remote_stub", None) is not None:
        +            return True
        +        cfg = getattr(self.config, "config", None)
        +        return isinstance(cfg, dict) and cfg.get("service_mode") == "remote"
        +
        +    def _ensure_backend_supports_stage_timeouts(
        +        self,
        +        connect_timeout: int | None = None,
        +        auth_timeout: int | None = None,
        +    ) -> None:
        +        """Raise ConnpyError if per-stage timeout overrides are used on gRPC remote backend."""
        +        if self._is_remote_backend() and (connect_timeout is not None or auth_timeout is not None):
        +            raise ConnpyError(
        +                "connect_timeout and auth_timeout overrides are not supported in remote gRPC mode"
        +            )
        +
        +    def _get_remote_stub(self):
        +        """Return the configured or lazily constructed ExecutionStub for remote gRPC mode."""
        +        if getattr(self, "remote_stub", None) is not None:
        +            return self.remote_stub
        +        cfg = getattr(self.config, "config", None)
        +        remote_host = cfg.get("remote_host") if isinstance(cfg, dict) else None
        +        if not remote_host:
        +            raise ConnpyError("Remote host must be specified in remote mode")
        +
        +        import grpc
        +
        +        from connpy.grpc_layer.stubs import AuthClientInterceptor, ExecutionStub
        +
        +        def get_token():
        +            env_token = os.environ.get("CONNPY_TOKEN")
        +            if env_token:
        +                return env_token
        +            defaultdir = getattr(self.config, "defaultdir", None)
        +            if defaultdir:
        +                token_path = os.path.join(defaultdir, ".token")
        +                if os.path.exists(token_path):
        +                    with suppress(OSError), open(token_path) as f:
        +                        return f.read().strip()
        +            return None
        +
        +        channel = grpc.insecure_channel(remote_host)
        +        channel = grpc.intercept_channel(channel, AuthClientInterceptor(get_token))
        +        return ExecutionStub(channel, remote_host=remote_host)
        +
        +    def run_commands(
        +        self,
        +        nodes_filter: str,
        +        commands: list[str],
        +        variables: dict[str, Any] | None = None,
        +        parallel: int = 10,
        +        timeout: int | None = None,
        +        folder: str | None = None,
        +        prompt: str | None = None,
        +        on_node_complete: Callable | None = None,
        +        logger: Callable | None = None,
        +        name: str | None = None,
        +        connect_timeout: int | None = None,
        +        auth_timeout: int | None = None,
        +    ) -> dict[str, Any]:
                 """Execute commands on a set of nodes."""
                 try:
        +            self._ensure_backend_supports_stage_timeouts(
        +                connect_timeout=connect_timeout,
        +                auth_timeout=auth_timeout,
        +            )
        +
        +            if self._is_remote_backend():
        +                stub = self._get_remote_stub()
        +                return stub.run_commands(
        +                    nodes_filter=nodes_filter,
        +                    commands=commands,
        +                    variables=variables,
        +                    parallel=parallel,
        +                    timeout=timeout if timeout is not None else 20,
        +                    folder=folder,
        +                    prompt=prompt,
        +                    on_node_complete=on_node_complete,
        +                    logger=logger,
        +                    name=name or "",
        +                )
        +
                     matched_names = self.config._getallnodes(nodes_filter)
                     if not matched_names:
                         raise ConnpyError(f"No nodes found matching filter: {nodes_filter}")
        -            
        +
                     node_data = self.config.getitems(matched_names, extract=True)
                     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
        -            )
         
        -            # Combine output and status for the caller
        +            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
        +
        +            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)
        +                    "status": executor.status.get(unique, 1),
        +                    "fallback_used": fb_used,
                         }
        +                self.last_fallback_used[unique] = fb_used
         
                     return full_results
        -        except Exception as e:
        -            raise ConnpyError(f"Execution failed: {e}")
        +        except ConnpyError:
        +            raise
        +        except Exception as e:  # noqa: BLE001
        +            raise_sanitized_auth_error(e, code="AUTH_FAILED", context_label="execution failed")
         
             def test_commands(
                 self,
                 nodes_filter: str,
        -        commands: List[str],
        -        expected: List[str],
        -        variables: Optional[Dict[str, Any]] = None,
        +        commands: list[str],
        +        expected: list[str],
        +        variables: dict[str, Any] | None = None,
                 parallel: int = 10,
        -        timeout: int = 20,
        -        folder: Optional[str] = None,
        -        prompt: Optional[str] = None,
        -        on_node_complete: Optional[Callable] = None,
        -        logger: Optional[Callable] = None,
        -        name: Optional[str] = None
        -    ) -> Dict[str, Dict[str, bool]]:
        -
        +        timeout: int | None = None,
        +        folder: str | None = None,
        +        prompt: str | None = None,
        +        on_node_complete: Callable | None = None,
        +        logger: Callable | None = None,
        +        name: str | None = None,
        +        connect_timeout: int | None = None,
        +        auth_timeout: int | None = None,
        +    ) -> dict[str, dict[str, bool]]:
                 """Run commands and verify expected output on a set of nodes."""
                 try:
        +            self._ensure_backend_supports_stage_timeouts(
        +                connect_timeout=connect_timeout,
        +                auth_timeout=auth_timeout,
        +            )
        +
        +            if self._is_remote_backend():
        +                stub = self._get_remote_stub()
        +                return stub.test_commands(
        +                    nodes_filter=nodes_filter,
        +                    commands=commands,
        +                    expected=expected,
        +                    variables=variables,
        +                    parallel=parallel,
        +                    timeout=timeout if timeout is not None else 20,
        +                    folder=folder or "",
        +                    prompt=prompt,
        +                    on_node_complete=on_node_complete,
        +                    logger=logger,
        +                    name=name or "",
        +                )
        +
                     matched_names = self.config._getallnodes(nodes_filter)
                     if not matched_names:
                         raise ConnpyError(f"No nodes found matching filter: {nodes_filter}")
        -            
        +
                     node_data = self.config.getitems(matched_names, extract=True)
                     executor = Nodes(node_data, config=self.config)
                     self.last_executor = executor
        -            
        -            results = executor.test(
        -                commands=commands,
        -                expected=expected,
        -                vars=variables,
        -                parallel=parallel,
        -                timeout=timeout,
        -                folder=folder,
        -                prompt=prompt,
        -                on_complete=on_node_complete,
        -                logger=logger
        -            )
        -            return results
        -        except Exception as e:
        -            raise ConnpyError(f"Testing failed: {e}")
         
        -    def run_cli_script(self, nodes_filter: str, script_path: str, parallel: int = 10) -> Dict[str, str]:
        +            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:  # 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."""
                 if not os.path.exists(script_path):
                     raise ConnpyError(f"Script file not found: {script_path}")
        -            
        +
                 try:
        -            with open(script_path, "r") as f:
        +            with open(script_path) as f:
                         commands = [line.strip() for line in f if line.strip()]
        -        except Exception as e:
        -            raise ConnpyError(f"Failed to read script {script_path}: {e}")
        -            
        +        except OSError:
        +            raise ConnpyError("Failed to read script file") from None
        +
                 return self.run_commands(nodes_filter, commands, parallel=parallel)

        Business logic for executing commands on nodes and running automation scripts.

        @@ -173,30 +308,30 @@ el.replaceWith(d);

        Methods

        -def run_cli_script(self, nodes_filter: str, script_path: str, parallel: int = 10) ‑> Dict[str, str] +def run_cli_script(self, nodes_filter: str, script_path: str, parallel: int = 10) ‑> dict[str, str]
        Expand source code -
        def run_cli_script(self, nodes_filter: str, script_path: str, parallel: int = 10) -> Dict[str, str]:
        +
        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."""
             if not os.path.exists(script_path):
                 raise ConnpyError(f"Script file not found: {script_path}")
        -        
        +
             try:
        -        with open(script_path, "r") as f:
        +        with open(script_path) as f:
                     commands = [line.strip() for line in f if line.strip()]
        -    except Exception as e:
        -        raise ConnpyError(f"Failed to read script {script_path}: {e}")
        -        
        +    except OSError:
        +        raise ConnpyError("Failed to read script file") from None
        +
             return self.run_commands(nodes_filter, commands, parallel=parallel)

        Run a plain-text script containing one command per line.

        -def run_commands(self,
        nodes_filter: str,
        commands: List[str],
        variables: Dict[str, Any] | None = None,
        parallel: int = 10,
        timeout: int = 20,
        folder: str | None = None,
        prompt: str | None = None,
        on_node_complete: Callable | None = None,
        logger: Callable | None = None,
        name: str | None = None) ‑> Dict[str, str]
        +def run_commands(self,
        nodes_filter: str,
        commands: list[str],
        variables: dict[str, typing.Any] | None = None,
        parallel: int = 10,
        timeout: int | None = None,
        folder: str | None = None,
        prompt: str | None = None,
        on_node_complete: collections.abc.Callable | None = None,
        logger: collections.abc.Callable | None = None,
        name: str | None = None,
        connect_timeout: int | None = None,
        auth_timeout: int | None = None) ‑> dict[str, typing.Any]
        @@ -204,56 +339,96 @@ el.replaceWith(d); Expand source code
        def run_commands(
        -    self, 
        -    nodes_filter: str, 
        -    commands: List[str], 
        -    variables: Optional[Dict[str, Any]] = None,
        +    self,
        +    nodes_filter: str,
        +    commands: list[str],
        +    variables: dict[str, Any] | None = None,
             parallel: int = 10,
        -    timeout: int = 20,
        -    folder: Optional[str] = None,
        -    prompt: Optional[str] = None,
        -    on_node_complete: Optional[Callable] = None,
        -    logger: Optional[Callable] = None,
        -    name: Optional[str] = None
        -) -> Dict[str, str]:
        -
        +    timeout: int | None = None,
        +    folder: str | None = None,
        +    prompt: str | None = None,
        +    on_node_complete: Callable | None = None,
        +    logger: Callable | None = None,
        +    name: str | None = None,
        +    connect_timeout: int | None = None,
        +    auth_timeout: int | None = None,
        +) -> dict[str, Any]:
             """Execute commands on a set of nodes."""
             try:
        +        self._ensure_backend_supports_stage_timeouts(
        +            connect_timeout=connect_timeout,
        +            auth_timeout=auth_timeout,
        +        )
        +
        +        if self._is_remote_backend():
        +            stub = self._get_remote_stub()
        +            return stub.run_commands(
        +                nodes_filter=nodes_filter,
        +                commands=commands,
        +                variables=variables,
        +                parallel=parallel,
        +                timeout=timeout if timeout is not None else 20,
        +                folder=folder,
        +                prompt=prompt,
        +                on_node_complete=on_node_complete,
        +                logger=logger,
        +                name=name or "",
        +            )
        +
                 matched_names = self.config._getallnodes(nodes_filter)
                 if not matched_names:
                     raise ConnpyError(f"No nodes found matching filter: {nodes_filter}")
        -        
        +
                 node_data = self.config.getitems(matched_names, extract=True)
                 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
        -        )
         
        -        # Combine output and status for the caller
        +        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
        +
        +        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)
        +                "status": executor.status.get(unique, 1),
        +                "fallback_used": fb_used,
                     }
        +            self.last_fallback_used[unique] = fb_used
         
                 return full_results
        -    except Exception as e:
        -        raise ConnpyError(f"Execution failed: {e}")
        + except ConnpyError: + raise + except Exception as e: # noqa: BLE001 + raise_sanitized_auth_error(e, code="AUTH_FAILED", context_label="execution failed")

        Execute commands on a set of nodes.

        -def test_commands(self,
        nodes_filter: str,
        commands: List[str],
        expected: List[str],
        variables: Dict[str, Any] | None = None,
        parallel: int = 10,
        timeout: int = 20,
        folder: str | None = None,
        prompt: str | None = None,
        on_node_complete: Callable | None = None,
        logger: Callable | None = None,
        name: str | None = None) ‑> Dict[str, Dict[str, bool]]
        +def test_commands(self,
        nodes_filter: str,
        commands: list[str],
        expected: list[str],
        variables: dict[str, typing.Any] | None = None,
        parallel: int = 10,
        timeout: int | None = None,
        folder: str | None = None,
        prompt: str | None = None,
        on_node_complete: collections.abc.Callable | None = None,
        logger: collections.abc.Callable | None = None,
        name: str | None = None,
        connect_timeout: int | None = None,
        auth_timeout: int | None = None) ‑> dict[str, dict[str, bool]]
        @@ -263,42 +438,82 @@ el.replaceWith(d);
        def test_commands(
             self,
             nodes_filter: str,
        -    commands: List[str],
        -    expected: List[str],
        -    variables: Optional[Dict[str, Any]] = None,
        +    commands: list[str],
        +    expected: list[str],
        +    variables: dict[str, Any] | None = None,
             parallel: int = 10,
        -    timeout: int = 20,
        -    folder: Optional[str] = None,
        -    prompt: Optional[str] = None,
        -    on_node_complete: Optional[Callable] = None,
        -    logger: Optional[Callable] = None,
        -    name: Optional[str] = None
        -) -> Dict[str, Dict[str, bool]]:
        -
        +    timeout: int | None = None,
        +    folder: str | None = None,
        +    prompt: str | None = None,
        +    on_node_complete: Callable | None = None,
        +    logger: Callable | None = None,
        +    name: str | None = None,
        +    connect_timeout: int | None = None,
        +    auth_timeout: int | None = None,
        +) -> dict[str, dict[str, bool]]:
             """Run commands and verify expected output on a set of nodes."""
             try:
        +        self._ensure_backend_supports_stage_timeouts(
        +            connect_timeout=connect_timeout,
        +            auth_timeout=auth_timeout,
        +        )
        +
        +        if self._is_remote_backend():
        +            stub = self._get_remote_stub()
        +            return stub.test_commands(
        +                nodes_filter=nodes_filter,
        +                commands=commands,
        +                expected=expected,
        +                variables=variables,
        +                parallel=parallel,
        +                timeout=timeout if timeout is not None else 20,
        +                folder=folder or "",
        +                prompt=prompt,
        +                on_node_complete=on_node_complete,
        +                logger=logger,
        +                name=name or "",
        +            )
        +
                 matched_names = self.config._getallnodes(nodes_filter)
                 if not matched_names:
                     raise ConnpyError(f"No nodes found matching filter: {nodes_filter}")
        -        
        +
                 node_data = self.config.getitems(matched_names, extract=True)
                 executor = Nodes(node_data, config=self.config)
                 self.last_executor = executor
        -        
        -        results = executor.test(
        -            commands=commands,
        -            expected=expected,
        -            vars=variables,
        -            parallel=parallel,
        -            timeout=timeout,
        -            folder=folder,
        -            prompt=prompt,
        -            on_complete=on_node_complete,
        -            logger=logger
        -        )
        -        return results
        -    except Exception as e:
        -        raise ConnpyError(f"Testing failed: {e}")
        + + 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: # noqa: BLE001 + raise_sanitized_auth_error(e, code="AUTH_FAILED", context_label="testing failed")

        Run commands and verify expected output on a set of nodes.

        diff --git a/docs/connpy/services/index.html b/docs/connpy/services/index.html index 61d9385..894d3ef 100644 --- a/docs/connpy/services/index.html +++ b/docs/connpy/services/index.html @@ -1255,6 +1255,7 @@ el.replaceWith(d);

      Subclasses

        +
      • SanitizedAuthError
      • ExecutionError
      • InvalidConfigurationError
      • NodeAlreadyExistsError
      • @@ -1287,7 +1288,7 @@ el.replaceWith(d);
        class ExecutionService -(config=None) +(config=None, remote_stub=None, attempt_tracker=None)
        @@ -1297,104 +1298,239 @@ el.replaceWith(d);
        class ExecutionService(BaseService):
             """Business logic for executing commands on nodes and running automation scripts."""
         
        -    def run_commands(
        -        self, 
        -        nodes_filter: str, 
        -        commands: List[str], 
        -        variables: Optional[Dict[str, Any]] = None,
        -        parallel: int = 10,
        -        timeout: int = 20,
        -        folder: Optional[str] = None,
        -        prompt: Optional[str] = None,
        -        on_node_complete: Optional[Callable] = None,
        -        logger: Optional[Callable] = None,
        -        name: Optional[str] = None
        -    ) -> Dict[str, str]:
        +    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."""
        +        if getattr(self, "remote_stub", None) is not None:
        +            return True
        +        cfg = getattr(self.config, "config", None)
        +        return isinstance(cfg, dict) and cfg.get("service_mode") == "remote"
        +
        +    def _ensure_backend_supports_stage_timeouts(
        +        self,
        +        connect_timeout: int | None = None,
        +        auth_timeout: int | None = None,
        +    ) -> None:
        +        """Raise ConnpyError if per-stage timeout overrides are used on gRPC remote backend."""
        +        if self._is_remote_backend() and (connect_timeout is not None or auth_timeout is not None):
        +            raise ConnpyError(
        +                "connect_timeout and auth_timeout overrides are not supported in remote gRPC mode"
        +            )
        +
        +    def _get_remote_stub(self):
        +        """Return the configured or lazily constructed ExecutionStub for remote gRPC mode."""
        +        if getattr(self, "remote_stub", None) is not None:
        +            return self.remote_stub
        +        cfg = getattr(self.config, "config", None)
        +        remote_host = cfg.get("remote_host") if isinstance(cfg, dict) else None
        +        if not remote_host:
        +            raise ConnpyError("Remote host must be specified in remote mode")
        +
        +        import grpc
        +
        +        from connpy.grpc_layer.stubs import AuthClientInterceptor, ExecutionStub
        +
        +        def get_token():
        +            env_token = os.environ.get("CONNPY_TOKEN")
        +            if env_token:
        +                return env_token
        +            defaultdir = getattr(self.config, "defaultdir", None)
        +            if defaultdir:
        +                token_path = os.path.join(defaultdir, ".token")
        +                if os.path.exists(token_path):
        +                    with suppress(OSError), open(token_path) as f:
        +                        return f.read().strip()
        +            return None
        +
        +        channel = grpc.insecure_channel(remote_host)
        +        channel = grpc.intercept_channel(channel, AuthClientInterceptor(get_token))
        +        return ExecutionStub(channel, remote_host=remote_host)
        +
        +    def run_commands(
        +        self,
        +        nodes_filter: str,
        +        commands: list[str],
        +        variables: dict[str, Any] | None = None,
        +        parallel: int = 10,
        +        timeout: int | None = None,
        +        folder: str | None = None,
        +        prompt: str | None = None,
        +        on_node_complete: Callable | None = None,
        +        logger: Callable | None = None,
        +        name: str | None = None,
        +        connect_timeout: int | None = None,
        +        auth_timeout: int | None = None,
        +    ) -> dict[str, Any]:
                 """Execute commands on a set of nodes."""
                 try:
        +            self._ensure_backend_supports_stage_timeouts(
        +                connect_timeout=connect_timeout,
        +                auth_timeout=auth_timeout,
        +            )
        +
        +            if self._is_remote_backend():
        +                stub = self._get_remote_stub()
        +                return stub.run_commands(
        +                    nodes_filter=nodes_filter,
        +                    commands=commands,
        +                    variables=variables,
        +                    parallel=parallel,
        +                    timeout=timeout if timeout is not None else 20,
        +                    folder=folder,
        +                    prompt=prompt,
        +                    on_node_complete=on_node_complete,
        +                    logger=logger,
        +                    name=name or "",
        +                )
        +
                     matched_names = self.config._getallnodes(nodes_filter)
                     if not matched_names:
                         raise ConnpyError(f"No nodes found matching filter: {nodes_filter}")
        -            
        +
                     node_data = self.config.getitems(matched_names, extract=True)
                     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
        -            )
         
        -            # Combine output and status for the caller
        +            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
        +
        +            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)
        +                    "status": executor.status.get(unique, 1),
        +                    "fallback_used": fb_used,
                         }
        +                self.last_fallback_used[unique] = fb_used
         
                     return full_results
        -        except Exception as e:
        -            raise ConnpyError(f"Execution failed: {e}")
        +        except ConnpyError:
        +            raise
        +        except Exception as e:  # noqa: BLE001
        +            raise_sanitized_auth_error(e, code="AUTH_FAILED", context_label="execution failed")
         
             def test_commands(
                 self,
                 nodes_filter: str,
        -        commands: List[str],
        -        expected: List[str],
        -        variables: Optional[Dict[str, Any]] = None,
        +        commands: list[str],
        +        expected: list[str],
        +        variables: dict[str, Any] | None = None,
                 parallel: int = 10,
        -        timeout: int = 20,
        -        folder: Optional[str] = None,
        -        prompt: Optional[str] = None,
        -        on_node_complete: Optional[Callable] = None,
        -        logger: Optional[Callable] = None,
        -        name: Optional[str] = None
        -    ) -> Dict[str, Dict[str, bool]]:
        -
        +        timeout: int | None = None,
        +        folder: str | None = None,
        +        prompt: str | None = None,
        +        on_node_complete: Callable | None = None,
        +        logger: Callable | None = None,
        +        name: str | None = None,
        +        connect_timeout: int | None = None,
        +        auth_timeout: int | None = None,
        +    ) -> dict[str, dict[str, bool]]:
                 """Run commands and verify expected output on a set of nodes."""
                 try:
        +            self._ensure_backend_supports_stage_timeouts(
        +                connect_timeout=connect_timeout,
        +                auth_timeout=auth_timeout,
        +            )
        +
        +            if self._is_remote_backend():
        +                stub = self._get_remote_stub()
        +                return stub.test_commands(
        +                    nodes_filter=nodes_filter,
        +                    commands=commands,
        +                    expected=expected,
        +                    variables=variables,
        +                    parallel=parallel,
        +                    timeout=timeout if timeout is not None else 20,
        +                    folder=folder or "",
        +                    prompt=prompt,
        +                    on_node_complete=on_node_complete,
        +                    logger=logger,
        +                    name=name or "",
        +                )
        +
                     matched_names = self.config._getallnodes(nodes_filter)
                     if not matched_names:
                         raise ConnpyError(f"No nodes found matching filter: {nodes_filter}")
        -            
        +
                     node_data = self.config.getitems(matched_names, extract=True)
                     executor = Nodes(node_data, config=self.config)
                     self.last_executor = executor
        -            
        -            results = executor.test(
        -                commands=commands,
        -                expected=expected,
        -                vars=variables,
        -                parallel=parallel,
        -                timeout=timeout,
        -                folder=folder,
        -                prompt=prompt,
        -                on_complete=on_node_complete,
        -                logger=logger
        -            )
        -            return results
        -        except Exception as e:
        -            raise ConnpyError(f"Testing failed: {e}")
         
        -    def run_cli_script(self, nodes_filter: str, script_path: str, parallel: int = 10) -> Dict[str, str]:
        +            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:  # 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."""
                 if not os.path.exists(script_path):
                     raise ConnpyError(f"Script file not found: {script_path}")
        -            
        +
                 try:
        -            with open(script_path, "r") as f:
        +            with open(script_path) as f:
                         commands = [line.strip() for line in f if line.strip()]
        -        except Exception as e:
        -            raise ConnpyError(f"Failed to read script {script_path}: {e}")
        -            
        +        except OSError:
        +            raise ConnpyError("Failed to read script file") from None
        +
                 return self.run_commands(nodes_filter, commands, parallel=parallel)

        Business logic for executing commands on nodes and running automation scripts.

        @@ -1412,30 +1548,30 @@ el.replaceWith(d);

        Methods

        -def run_cli_script(self, nodes_filter: str, script_path: str, parallel: int = 10) ‑> Dict[str, str] +def run_cli_script(self, nodes_filter: str, script_path: str, parallel: int = 10) ‑> dict[str, str]
        Expand source code -
        def run_cli_script(self, nodes_filter: str, script_path: str, parallel: int = 10) -> Dict[str, str]:
        +
        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."""
             if not os.path.exists(script_path):
                 raise ConnpyError(f"Script file not found: {script_path}")
        -        
        +
             try:
        -        with open(script_path, "r") as f:
        +        with open(script_path) as f:
                     commands = [line.strip() for line in f if line.strip()]
        -    except Exception as e:
        -        raise ConnpyError(f"Failed to read script {script_path}: {e}")
        -        
        +    except OSError:
        +        raise ConnpyError("Failed to read script file") from None
        +
             return self.run_commands(nodes_filter, commands, parallel=parallel)

        Run a plain-text script containing one command per line.

        -def run_commands(self,
        nodes_filter: str,
        commands: List[str],
        variables: Dict[str, Any] | None = None,
        parallel: int = 10,
        timeout: int = 20,
        folder: str | None = None,
        prompt: str | None = None,
        on_node_complete: Callable | None = None,
        logger: Callable | None = None,
        name: str | None = None) ‑> Dict[str, str]
        +def run_commands(self,
        nodes_filter: str,
        commands: list[str],
        variables: dict[str, typing.Any] | None = None,
        parallel: int = 10,
        timeout: int | None = None,
        folder: str | None = None,
        prompt: str | None = None,
        on_node_complete: collections.abc.Callable | None = None,
        logger: collections.abc.Callable | None = None,
        name: str | None = None,
        connect_timeout: int | None = None,
        auth_timeout: int | None = None) ‑> dict[str, typing.Any]
        @@ -1443,56 +1579,96 @@ el.replaceWith(d); Expand source code
        def run_commands(
        -    self, 
        -    nodes_filter: str, 
        -    commands: List[str], 
        -    variables: Optional[Dict[str, Any]] = None,
        +    self,
        +    nodes_filter: str,
        +    commands: list[str],
        +    variables: dict[str, Any] | None = None,
             parallel: int = 10,
        -    timeout: int = 20,
        -    folder: Optional[str] = None,
        -    prompt: Optional[str] = None,
        -    on_node_complete: Optional[Callable] = None,
        -    logger: Optional[Callable] = None,
        -    name: Optional[str] = None
        -) -> Dict[str, str]:
        -
        +    timeout: int | None = None,
        +    folder: str | None = None,
        +    prompt: str | None = None,
        +    on_node_complete: Callable | None = None,
        +    logger: Callable | None = None,
        +    name: str | None = None,
        +    connect_timeout: int | None = None,
        +    auth_timeout: int | None = None,
        +) -> dict[str, Any]:
             """Execute commands on a set of nodes."""
             try:
        +        self._ensure_backend_supports_stage_timeouts(
        +            connect_timeout=connect_timeout,
        +            auth_timeout=auth_timeout,
        +        )
        +
        +        if self._is_remote_backend():
        +            stub = self._get_remote_stub()
        +            return stub.run_commands(
        +                nodes_filter=nodes_filter,
        +                commands=commands,
        +                variables=variables,
        +                parallel=parallel,
        +                timeout=timeout if timeout is not None else 20,
        +                folder=folder,
        +                prompt=prompt,
        +                on_node_complete=on_node_complete,
        +                logger=logger,
        +                name=name or "",
        +            )
        +
                 matched_names = self.config._getallnodes(nodes_filter)
                 if not matched_names:
                     raise ConnpyError(f"No nodes found matching filter: {nodes_filter}")
        -        
        +
                 node_data = self.config.getitems(matched_names, extract=True)
                 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
        -        )
         
        -        # Combine output and status for the caller
        +        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
        +
        +        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)
        +                "status": executor.status.get(unique, 1),
        +                "fallback_used": fb_used,
                     }
        +            self.last_fallback_used[unique] = fb_used
         
                 return full_results
        -    except Exception as e:
        -        raise ConnpyError(f"Execution failed: {e}")
        + except ConnpyError: + raise + except Exception as e: # noqa: BLE001 + raise_sanitized_auth_error(e, code="AUTH_FAILED", context_label="execution failed")

        Execute commands on a set of nodes.

        -def test_commands(self,
        nodes_filter: str,
        commands: List[str],
        expected: List[str],
        variables: Dict[str, Any] | None = None,
        parallel: int = 10,
        timeout: int = 20,
        folder: str | None = None,
        prompt: str | None = None,
        on_node_complete: Callable | None = None,
        logger: Callable | None = None,
        name: str | None = None) ‑> Dict[str, Dict[str, bool]]
        +def test_commands(self,
        nodes_filter: str,
        commands: list[str],
        expected: list[str],
        variables: dict[str, typing.Any] | None = None,
        parallel: int = 10,
        timeout: int | None = None,
        folder: str | None = None,
        prompt: str | None = None,
        on_node_complete: collections.abc.Callable | None = None,
        logger: collections.abc.Callable | None = None,
        name: str | None = None,
        connect_timeout: int | None = None,
        auth_timeout: int | None = None) ‑> dict[str, dict[str, bool]]
        @@ -1502,42 +1678,82 @@ el.replaceWith(d);
        def test_commands(
             self,
             nodes_filter: str,
        -    commands: List[str],
        -    expected: List[str],
        -    variables: Optional[Dict[str, Any]] = None,
        +    commands: list[str],
        +    expected: list[str],
        +    variables: dict[str, Any] | None = None,
             parallel: int = 10,
        -    timeout: int = 20,
        -    folder: Optional[str] = None,
        -    prompt: Optional[str] = None,
        -    on_node_complete: Optional[Callable] = None,
        -    logger: Optional[Callable] = None,
        -    name: Optional[str] = None
        -) -> Dict[str, Dict[str, bool]]:
        -
        +    timeout: int | None = None,
        +    folder: str | None = None,
        +    prompt: str | None = None,
        +    on_node_complete: Callable | None = None,
        +    logger: Callable | None = None,
        +    name: str | None = None,
        +    connect_timeout: int | None = None,
        +    auth_timeout: int | None = None,
        +) -> dict[str, dict[str, bool]]:
             """Run commands and verify expected output on a set of nodes."""
             try:
        +        self._ensure_backend_supports_stage_timeouts(
        +            connect_timeout=connect_timeout,
        +            auth_timeout=auth_timeout,
        +        )
        +
        +        if self._is_remote_backend():
        +            stub = self._get_remote_stub()
        +            return stub.test_commands(
        +                nodes_filter=nodes_filter,
        +                commands=commands,
        +                expected=expected,
        +                variables=variables,
        +                parallel=parallel,
        +                timeout=timeout if timeout is not None else 20,
        +                folder=folder or "",
        +                prompt=prompt,
        +                on_node_complete=on_node_complete,
        +                logger=logger,
        +                name=name or "",
        +            )
        +
                 matched_names = self.config._getallnodes(nodes_filter)
                 if not matched_names:
                     raise ConnpyError(f"No nodes found matching filter: {nodes_filter}")
        -        
        +
                 node_data = self.config.getitems(matched_names, extract=True)
                 executor = Nodes(node_data, config=self.config)
                 self.last_executor = executor
        -        
        -        results = executor.test(
        -            commands=commands,
        -            expected=expected,
        -            vars=variables,
        -            parallel=parallel,
        -            timeout=timeout,
        -            folder=folder,
        -            prompt=prompt,
        -            on_complete=on_node_complete,
        -            logger=logger
        -        )
        -        return results
        -    except Exception as e:
        -        raise ConnpyError(f"Testing failed: {e}")
        + + 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: # noqa: BLE001 + raise_sanitized_auth_error(e, code="AUTH_FAILED", context_label="testing failed")

        Run commands and verify expected output on a set of nodes.

        @@ -1910,9 +2126,53 @@ el.replaceWith(d); Expand source code
        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."""
        @@ -1989,7 +2249,7 @@ el.replaceWith(d);
                         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."""
        @@ -2019,58 +2279,127 @@ el.replaceWith(d);
                 """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:
        @@ -2081,23 +2410,40 @@ el.replaceWith(d);
                     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"
        @@ -2107,50 +2453,109 @@ el.replaceWith(d);
             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)
        @@ -2160,13 +2565,28 @@ el.replaceWith(d);
         
                     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)
        @@ -2204,37 +2624,69 @@ el.replaceWith(d);
             """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)

        Logic for adding a new node or folder to configuration.

        @@ -2249,25 +2701,61 @@ el.replaceWith(d);
        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)
        @@ -2277,7 +2765,7 @@ el.replaceWith(d);
         
                 self.config._connections_add(**node_data)
                 count += 1
        -        
        +
             if count > 0:
                 self.config._saveconfig(self.config.file)
             return count
        @@ -2295,16 +2783,33 @@ el.replaceWith(d);
        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"
        @@ -2323,6 +2828,20 @@ el.replaceWith(d);
         
         
        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:
        @@ -2333,7 +2852,7 @@ el.replaceWith(d);
                 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)
        @@ -2363,6 +2882,21 @@ el.replaceWith(d);
        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)
        @@ -2416,7 +2950,7 @@ el.replaceWith(d); 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

        Return full configuration dictionary for a specific node.

        @@ -2518,27 +3052,50 @@ el.replaceWith(d);
        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)

        Move or copy a node.

        @@ -2553,18 +3110,27 @@ el.replaceWith(d);
        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)
        @@ -3418,61 +3984,194 @@ el.replaceWith(d);
        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
        -        referenced_profiles = []
        -        for value in resolved.values():
        -            if isinstance(value, str) and value.startswith("@"):
        -                referenced_profiles.append(value[1:])
        -            elif isinstance(value, list):
        -                for item in value:
        -                    if isinstance(item, str) and item.startswith("@"):
        -                        referenced_profiles.append(item[1:])
        -        
        -        # 2. Resolve explicit references
        +    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)
        +
        +        auth_referenced_profiles = []
        +        other_referenced_profiles = []
                 for key, value in resolved.items():
        +            if key == "fallback":
        +                continue
        +            target_refs = (
        +                auth_referenced_profiles if key in ("user", "password") else other_referenced_profiles
        +            )
        +            if isinstance(value, str):
        +                if not is_compose_expression(value) and value.startswith("@"):
        +                    ref = value[1:]
        +                    if key != "password" or ref in profiles:
        +                        target_refs.append(ref)
        +            elif isinstance(value, (list, tuple)):
        +                for item in value:
        +                    if (
        +                        isinstance(item, str)
        +                        and not is_compose_expression(item)
        +                        and item.startswith("@")
        +                    ):
        +                        ref = item[1:]
        +                        if key != "password" or ref in profiles:
        +                            target_refs.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] = ""
        @@ -3481,68 +4180,87 @@ el.replaceWith(d);
                         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)
        -                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
         
        -        # 4. Handle default protocol
        -        if resolved.get("protocol") == "" or resolved.get("protocol") is None:
        +        ordered_profiles = list(dict.fromkeys(auth_referenced_profiles + other_referenced_profiles))
        +        for base_profile_name in ordered_profiles:
        +            with suppress(ProfileNotFoundError):
        +                base_profile = _get_resolved_ref(base_profile_name)
        +                for key, value in base_profile.items():
        +                    if (key not in resolved or resolved[key] in ("", [], None)) and (
        +                        value not in ("", [], None) or key not in resolved
        +                    ):
        +                        resolved[key] = deepcopy(value)
        +
        +        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)
        @@ -3570,13 +4288,20 @@ el.replaceWith(d);
        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)
        @@ -3592,17 +4317,31 @@ el.replaceWith(d);
        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)
        @@ -3616,14 +4355,24 @@ el.replaceWith(d); Expand source code -
        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

        Get the profile dictionary, optionally resolved.

        @@ -3638,15 +4387,14 @@ el.replaceWith(d);
        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

        List all profile names, optionally filtered.

        @@ -3659,26 +4407,98 @@ el.replaceWith(d); Expand source code -
        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
        -    referenced_profiles = []
        -    for value in resolved.values():
        -        if isinstance(value, str) and value.startswith("@"):
        -            referenced_profiles.append(value[1:])
        -        elif isinstance(value, list):
        -            for item in value:
        -                if isinstance(item, str) and item.startswith("@"):
        -                    referenced_profiles.append(item[1:])
        -    
        -    # 2. Resolve explicit references
        +
        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)
        +
        +    auth_referenced_profiles = []
        +    other_referenced_profiles = []
             for key, value in resolved.items():
        +        if key == "fallback":
        +            continue
        +        target_refs = (
        +            auth_referenced_profiles if key in ("user", "password") else other_referenced_profiles
        +        )
        +        if isinstance(value, str):
        +            if not is_compose_expression(value) and value.startswith("@"):
        +                ref = value[1:]
        +                if key != "password" or ref in profiles:
        +                    target_refs.append(ref)
        +        elif isinstance(value, (list, tuple)):
        +            for item in value:
        +                if (
        +                    isinstance(item, str)
        +                    and not is_compose_expression(item)
        +                    and item.startswith("@")
        +                ):
        +                    ref = item[1:]
        +                    if key != "password" or ref in profiles:
        +                        target_refs.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] = ""
        @@ -3687,39 +4507,34 @@ el.replaceWith(d);
                     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)
        -            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
         
        -    # 4. Handle default protocol
        -    if resolved.get("protocol") == "" or resolved.get("protocol") is None:
        +    ordered_profiles = list(dict.fromkeys(auth_referenced_profiles + other_referenced_profiles))
        +    for base_profile_name in ordered_profiles:
        +        with suppress(ProfileNotFoundError):
        +            base_profile = _get_resolved_ref(base_profile_name)
        +            for key, value in base_profile.items():
        +                if (key not in resolved or resolved[key] in ("", [], None)) and (
        +                    value not in ("", [], None) or key not in resolved
        +                ):
        +                    resolved[key] = deepcopy(value)
        +
        +    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
        -

        Resolve profile references (@profile) in node data and handle inheritance.

        +

        Resolve profile references (@profile) in node data while preserving compose and scalar secrets.

        def update_profile(self, name, data) @@ -3731,18 +4546,28 @@ el.replaceWith(d);
        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/docs/connpy/services/node_service.html b/docs/connpy/services/node_service.html index 13c94ad..ca3182f 100644 --- a/docs/connpy/services/node_service.html +++ b/docs/connpy/services/node_service.html @@ -56,9 +56,53 @@ el.replaceWith(d); Expand source code
        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."""
        @@ -135,7 +179,7 @@ el.replaceWith(d);
                         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."""
        @@ -165,58 +209,127 @@ el.replaceWith(d);
                 """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:
        @@ -227,23 +340,40 @@ el.replaceWith(d);
                     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"
        @@ -253,50 +383,109 @@ el.replaceWith(d);
             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)
        @@ -306,13 +495,28 @@ el.replaceWith(d);
         
                     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)
        @@ -350,37 +554,69 @@ el.replaceWith(d);
             """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)

        Logic for adding a new node or folder to configuration.

        @@ -395,25 +631,61 @@ el.replaceWith(d);
        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)
        @@ -423,7 +695,7 @@ el.replaceWith(d);
         
                 self.config._connections_add(**node_data)
                 count += 1
        -        
        +
             if count > 0:
                 self.config._saveconfig(self.config.file)
             return count
        @@ -441,16 +713,33 @@ el.replaceWith(d);
        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"
        @@ -469,6 +758,20 @@ el.replaceWith(d);
         
         
        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:
        @@ -479,7 +782,7 @@ el.replaceWith(d);
                 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)
        @@ -509,6 +812,21 @@ el.replaceWith(d);
        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)
        @@ -562,7 +880,7 @@ el.replaceWith(d); 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

        Return full configuration dictionary for a specific node.

        @@ -664,27 +982,50 @@ el.replaceWith(d);
        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)

        Move or copy a node.

        @@ -699,18 +1040,27 @@ el.replaceWith(d);
        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)
        diff --git a/docs/connpy/services/profile_service.html b/docs/connpy/services/profile_service.html index 148d624..c04d659 100644 --- a/docs/connpy/services/profile_service.html +++ b/docs/connpy/services/profile_service.html @@ -58,61 +58,194 @@ el.replaceWith(d);
        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
        -        referenced_profiles = []
        -        for value in resolved.values():
        -            if isinstance(value, str) and value.startswith("@"):
        -                referenced_profiles.append(value[1:])
        -            elif isinstance(value, list):
        -                for item in value:
        -                    if isinstance(item, str) and item.startswith("@"):
        -                        referenced_profiles.append(item[1:])
        -        
        -        # 2. Resolve explicit references
        +    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)
        +
        +        auth_referenced_profiles = []
        +        other_referenced_profiles = []
                 for key, value in resolved.items():
        +            if key == "fallback":
        +                continue
        +            target_refs = (
        +                auth_referenced_profiles if key in ("user", "password") else other_referenced_profiles
        +            )
        +            if isinstance(value, str):
        +                if not is_compose_expression(value) and value.startswith("@"):
        +                    ref = value[1:]
        +                    if key != "password" or ref in profiles:
        +                        target_refs.append(ref)
        +            elif isinstance(value, (list, tuple)):
        +                for item in value:
        +                    if (
        +                        isinstance(item, str)
        +                        and not is_compose_expression(item)
        +                        and item.startswith("@")
        +                    ):
        +                        ref = item[1:]
        +                        if key != "password" or ref in profiles:
        +                            target_refs.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] = ""
        @@ -121,68 +254,87 @@ el.replaceWith(d);
                         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)
        -                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
         
        -        # 4. Handle default protocol
        -        if resolved.get("protocol") == "" or resolved.get("protocol") is None:
        +        ordered_profiles = list(dict.fromkeys(auth_referenced_profiles + other_referenced_profiles))
        +        for base_profile_name in ordered_profiles:
        +            with suppress(ProfileNotFoundError):
        +                base_profile = _get_resolved_ref(base_profile_name)
        +                for key, value in base_profile.items():
        +                    if (key not in resolved or resolved[key] in ("", [], None)) and (
        +                        value not in ("", [], None) or key not in resolved
        +                    ):
        +                        resolved[key] = deepcopy(value)
        +
        +        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)
        @@ -210,13 +362,20 @@ el.replaceWith(d);
        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)
        @@ -232,17 +391,31 @@ el.replaceWith(d);
        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)
        @@ -256,14 +429,24 @@ el.replaceWith(d); Expand source code -
        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

        Get the profile dictionary, optionally resolved.

        @@ -278,15 +461,14 @@ el.replaceWith(d);
        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

        List all profile names, optionally filtered.

        @@ -299,26 +481,98 @@ el.replaceWith(d); Expand source code -
        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
        -    referenced_profiles = []
        -    for value in resolved.values():
        -        if isinstance(value, str) and value.startswith("@"):
        -            referenced_profiles.append(value[1:])
        -        elif isinstance(value, list):
        -            for item in value:
        -                if isinstance(item, str) and item.startswith("@"):
        -                    referenced_profiles.append(item[1:])
        -    
        -    # 2. Resolve explicit references
        +
        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)
        +
        +    auth_referenced_profiles = []
        +    other_referenced_profiles = []
             for key, value in resolved.items():
        +        if key == "fallback":
        +            continue
        +        target_refs = (
        +            auth_referenced_profiles if key in ("user", "password") else other_referenced_profiles
        +        )
        +        if isinstance(value, str):
        +            if not is_compose_expression(value) and value.startswith("@"):
        +                ref = value[1:]
        +                if key != "password" or ref in profiles:
        +                    target_refs.append(ref)
        +        elif isinstance(value, (list, tuple)):
        +            for item in value:
        +                if (
        +                    isinstance(item, str)
        +                    and not is_compose_expression(item)
        +                    and item.startswith("@")
        +                ):
        +                    ref = item[1:]
        +                    if key != "password" or ref in profiles:
        +                        target_refs.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] = ""
        @@ -327,39 +581,34 @@ el.replaceWith(d);
                     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)
        -            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
         
        -    # 4. Handle default protocol
        -    if resolved.get("protocol") == "" or resolved.get("protocol") is None:
        +    ordered_profiles = list(dict.fromkeys(auth_referenced_profiles + other_referenced_profiles))
        +    for base_profile_name in ordered_profiles:
        +        with suppress(ProfileNotFoundError):
        +            base_profile = _get_resolved_ref(base_profile_name)
        +            for key, value in base_profile.items():
        +                if (key not in resolved or resolved[key] in ("", [], None)) and (
        +                    value not in ("", [], None) or key not in resolved
        +                ):
        +                    resolved[key] = deepcopy(value)
        +
        +    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
        -

        Resolve profile references (@profile) in node data and handle inheritance.

        +

        Resolve profile references (@profile) in node data while preserving compose and scalar secrets.

        def update_profile(self, name, data) @@ -371,18 +620,28 @@ el.replaceWith(d);
        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/docs/connpy/services/provider.html b/docs/connpy/services/provider.html index 1d461f0..997ed9f 100644 --- a/docs/connpy/services/provider.html +++ b/docs/connpy/services/provider.html @@ -74,11 +74,14 @@ el.replaceWith(d);
        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
        @@ -94,47 +97,54 @@ el.replaceWith(d);
                     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)
        @@ -170,7 +180,10 @@ el.replaceWith(d);
             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
        @@ -249,7 +262,10 @@ def ai(self):
         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