Compare commits

..
19 Commits
Author SHA1 Message Date
fluzzi32 693531ed57 docs: regenerate pdoc HTML documentation for v6.5.0b1 2026-11-17 19:16:11 -03:00
fluzzi32 b3925fee07 refactor(spec-05-auth-fallback-chain): senior (eng_flash_high) review refinements 2026-11-17 18:30:32 -03:00
fluzzi32 1b50c7d3e7 feat(spec-05-auth-fallback-chain): worker (eng_flash_medium) implementation 2026-11-17 18:27:57 -03:00
fluzzi32 fc27747c19 feat(spec-05-auth-fallback-chain): worker (eng_flash_medium) implementation 2026-11-17 18:19:51 -03:00
fluzzi32 175e623d99 feat(spec-05-auth-fallback-chain): worker (eng_flash_medium) implementation 2026-11-17 18:01:59 -03:00
fluzzi32 ecaa836b98 refactor(spec-05-auth-fallback-chain): senior (eng_flash_high) review refinements 2026-11-17 16:33:25 -03:00
fluzzi32 f86ce21ee0 feat(spec-05-auth-fallback-chain): worker (eng_flash_medium) implementation 2026-11-17 16:31:05 -03:00
fluzzi32 dddeaf4b03 feat(spec-05-auth-fallback-chain): worker (eng_flash_high) implementation 2026-11-17 14:44:05 -03:00
fluzzi32 441bf54ac1 chore(release): bump version to v6.5.0b1 and update documentation 2026-11-17 13:44:52 -03:00
fluzzi32 c6f260efc9 feat(spec-05-auth-fallback-chain): worker (eng_flash_high) implementation 2026-11-17 12:58:00 -03:00
fluzzi32 d2da49221f Merge feature/spec-05-auth-fallback-chain: add auth fallback config flags to completion tree 2026-11-17 12:13:37 -03:00
fluzzi32 59640d20a4 feat(spec-05-auth-fallback-chain): worker (eng_flash_high) implementation 2026-11-17 12:11:02 -03:00
fluzzi32 ad9dd27316 Merge feature/spec-05-auth-fallback-chain: unattended AAA fallback chain & compose credentials 2026-11-17 11:39:38 -03:00
fluzzi32 89ca1ec621 feat(spec-05-auth-fallback-chain): worker (eng_flash_high) implementation 2026-11-17 11:35:54 -03:00
fluzzi32 d96d8fb1fa feat(spec-05-auth-fallback-chain): worker (eng_flash_high) implementation 2026-11-16 23:03:32 -03:00
fluzzi32 79d28ee6c3 feat(spec-05-auth-fallback-chain): worker (eng_flash_high) implementation 2026-11-16 22:46:44 -03:00
fluzzi32 ef6d1da5e5 Merge feature/spec-04-adaptive-timeouts-ssm: adaptive SSM/jumphost timeouts & hardened expects 2026-11-16 21:25:41 -03:00
fluzzi32 8e388cc6b6 feat(spec-04-adaptive-timeouts-ssm): worker (eng_flash_high) implementation 2026-11-16 21:14:58 -03:00
fluzzi32 c3995ea6a9 feat(spec-04-adaptive-timeouts-ssm): worker (eng_flash_high) implementation 2026-11-16 21:06:07 -03:00
43 changed files with 22705 additions and 2540 deletions

No files matched your search

+10 -1
View File
@@ -3,7 +3,7 @@
</p> </p>
# 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/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/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/) [![](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" } 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`.
--- ---
@@ -157,6 +163,9 @@ Support for single or chained intermediate gateway nodes (SSH, Mosh, SSM, kubect
### 4c. Debug Mode, Keepalive, Logging ### 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'}`. 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.
--- ---
+10 -1
View File
@@ -5,7 +5,7 @@
</p> </p>
# 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/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/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/) [![](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" } 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`.
--- ---
@@ -159,6 +165,9 @@ Support for single or chained intermediate gateway nodes (SSH, Mosh, SSM, kubect
### 4c. Debug Mode, Keepalive, Logging ### 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'}`. 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.
--- ---
+1 -1
View File
@@ -1 +1 @@
__version__ = "6.4.0b4" __version__ = "6.5.0b1"
+1688
View File
File diff suppressed because it is too large. Load diff
+83 -1
View File
@@ -1,9 +1,22 @@
import re
import sys import sys
import yaml import yaml
from .. import printer from .. import printer
from ..auth_utils import (
MAX_AUTH_PROMPTS_CAP,
MAX_HOP_HOST_ATTEMPTS_CAP,
MAX_TOTAL_AUTH_ATTEMPTS_CAP,
MIN_AUTH_PROMPTS,
MIN_HOP_HOST_ATTEMPTS,
MIN_TOTAL_AUTH_ATTEMPTS,
validate_strict_int_range,
)
from ..services.exceptions import ConnpyError, InvalidConfigurationError from ..services.exceptions import ConnpyError, InvalidConfigurationError
from .help_text import get_instructions from .help_text import get_instructions
class ConfigHandler: class ConfigHandler:
def __init__(self, app): def __init__(self, app):
self.app = app self.app = app
@@ -29,7 +42,12 @@ class ConfigHandler:
"sync_remote": self.set_sync_remote, "sync_remote": self.set_sync_remote,
"shell_command": self.set_shell_config, "shell_command": self.set_shell_config,
"shell_prompt": 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)) handler = actions.get(getattr(args, "command", None))
if handler: if handler:
@@ -202,3 +220,67 @@ class ConfigHandler:
except (ConnpyError, InvalidConfigurationError) as e: 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")
+132 -15
View File
@@ -1,6 +1,77 @@
import ast import ast
import json
from ..auth_utils import (
MAX_FALLBACK_ENTRIES,
MAX_JSON_INPUT_BYTES,
MAX_JSON_INPUT_DEPTH,
validate_auth_budget_tags,
validate_fallback_entries,
validate_json_depth_and_size,
)
from .validators import Validators from .validators import Validators
def _parse_tags_input(raw_tags):
if raw_tags in ("", None):
return ""
if isinstance(raw_tags, dict):
validate_json_depth_and_size(
raw_tags,
max_bytes=MAX_JSON_INPUT_BYTES,
max_depth=MAX_JSON_INPUT_DEPTH,
)
validate_auth_budget_tags(raw_tags)
return raw_tags
s = str(raw_tags)
if s.startswith("@"):
return s
if len(s.encode("utf-8", errors="replace")) > MAX_JSON_INPUT_BYTES:
raise ValueError(f"Tags exceed maximum size of {MAX_JSON_INPUT_BYTES} bytes")
try:
parsed = json.loads(s)
except (ValueError, TypeError):
parsed = ast.literal_eval(s)
validate_json_depth_and_size(
parsed,
max_bytes=MAX_JSON_INPUT_BYTES,
max_depth=MAX_JSON_INPUT_DEPTH,
)
validate_auth_budget_tags(parsed)
return parsed
def _parse_fallback_input(raw_fb):
if raw_fb in (None, "", []):
return ""
if isinstance(raw_fb, str):
stripped = raw_fb.strip()
if not stripped:
return ""
if stripped.startswith(("[", "{")):
parsed = validate_json_depth_and_size(
stripped,
max_bytes=MAX_JSON_INPUT_BYTES,
max_depth=MAX_JSON_INPUT_DEPTH,
)
return validate_fallback_entries(parsed, max_entries=MAX_FALLBACK_ENTRIES)
validated = validate_fallback_entries(stripped, max_entries=MAX_FALLBACK_ENTRIES)
if (
stripped.startswith("@")
and not stripped.startswith("@@")
and "," in stripped
and not stripped.startswith("compose:")
):
return validated
return stripped
validate_json_depth_and_size(
raw_fb,
max_bytes=MAX_JSON_INPUT_BYTES,
max_depth=MAX_JSON_INPUT_DEPTH,
)
return validate_fallback_entries(raw_fb, max_entries=MAX_FALLBACK_ENTRIES)
class Forms: class Forms:
def __init__(self, app): def __init__(self, app):
self.app = app self.app = app
@@ -18,6 +89,7 @@ class Forms:
questions.append(inquirer.Confirm("jumphost", message="Edit jumphost?")) questions.append(inquirer.Confirm("jumphost", message="Edit jumphost?"))
questions.append(inquirer.Confirm("user", message="Edit User?")) questions.append(inquirer.Confirm("user", message="Edit User?"))
questions.append(inquirer.Confirm("password", message="Edit password?")) questions.append(inquirer.Confirm("password", message="Edit password?"))
questions.append(inquirer.Confirm("fallback", message="Edit fallback credentials?"))
return inquirer.prompt(questions) return inquirer.prompt(questions)
def questions_nodes(self, unique, uniques=None, edit=None): def questions_nodes(self, unique, uniques=None, edit=None):
@@ -28,11 +100,13 @@ class Forms:
defaults["tags"] = "" defaults["tags"] = ""
if "jumphost" not in defaults: if "jumphost" not in defaults:
defaults["jumphost"] = "" defaults["jumphost"] = ""
except Exception: if "fallback" not in defaults:
defaults = {"host": "", "protocol": "", "port": "", "user": "", "options": "", "logs": "", "tags": "", "password": "", "jumphost": ""} defaults["fallback"] = ""
except Exception: # noqa: BLE001
defaults = {"host": "", "protocol": "", "port": "", "user": "", "options": "", "logs": "", "tags": "", "password": "", "jumphost": "", "fallback": ""}
node = {} node = {}
if edit is None: 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 = [] questions = []
if edit["host"]: if edit["host"]:
questions.append(inquirer.Text("host", message="Add Hostname or IP", validate=self.validators.host_validation, default=defaults["host"])) questions.append(inquirer.Text("host", message="Add Hostname or IP", validate=self.validators.host_validation, default=defaults["host"]))
@@ -70,6 +144,8 @@ class Forms:
questions.append(inquirer.List("password", message="Password: Use a local password, no password or a list of profiles to reference?", choices=["Local Password", "Profiles", "No Password"])) 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: else:
node["password"] = defaults["password"] node["password"] = defaults["password"]
if not edit.get("fallback"):
node["fallback"] = defaults.get("fallback", "")
answer = inquirer.prompt(questions) answer = inquirer.prompt(questions)
if answer is None: if answer is None:
@@ -87,12 +163,28 @@ class Forms:
passa = inquirer.prompt(passq) passa = inquirer.prompt(passq)
if passa is None: if passa is None:
return False 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": elif answer["password"] == "No Password":
answer["password"] = "" answer["password"] = ""
if "tags" in answer and not answer["tags"].startswith("@") and answer["tags"]: if edit.get("fallback"):
answer["tags"] = ast.literal_eval(answer["tags"]) 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 (@profile, @p1,@p2, JSON list/dict, 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 = {**uniques, **answer, **node}
result["type"] = "connection" result["type"] = "connection"
@@ -106,11 +198,13 @@ class Forms:
defaults["tags"] = "" defaults["tags"] = ""
if "jumphost" not in defaults: if "jumphost" not in defaults:
defaults["jumphost"] = "" defaults["jumphost"] = ""
except Exception: if "fallback" not in defaults:
defaults = {"host": "", "protocol": "", "port": "", "user": "", "options": "", "logs": "", "tags": "", "jumphost": ""} defaults["fallback"] = ""
except Exception: # noqa: BLE001
defaults = {"host": "", "protocol": "", "port": "", "user": "", "options": "", "logs": "", "tags": "", "jumphost": "", "fallback": ""}
profile = {} profile = {}
if edit is None: 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 = [] questions = []
if edit["host"]: if edit["host"]:
questions.append(inquirer.Text("host", message="Add Hostname or IP", default=defaults["host"])) questions.append(inquirer.Text("host", message="Add Hostname or IP", default=defaults["host"]))
@@ -148,17 +242,24 @@ class Forms:
questions.append(inquirer.Password("password", message="Set Password")) questions.append(inquirer.Password("password", message="Set Password"))
else: else:
profile["password"] = defaults["password"] 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 (@profile, @p1,@p2, JSON list/dict, or compose:)", validate=self.validators.fallback_validation, default=def_fb_str.replace("{", "{{").replace("}", "}}")))
else:
profile["fallback"] = defaults.get("fallback", "")
answer = inquirer.prompt(questions) answer = inquirer.prompt(questions)
if answer is None: if answer is None:
return False return False
if "password" in answer: if "password" in answer and answer["password"] != "":
if answer["password"] != "":
answer["password"] = self.app.services.config_svc.encrypt_password(answer["password"]) answer["password"] = self.app.services.config_svc.encrypt_password(answer["password"])
if "tags" in answer and answer["tags"]: 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 = {**answer, **profile}
result["id"] = unique result["id"] = unique
@@ -187,17 +288,33 @@ class Forms:
if answer["password"] == "Local Password": if answer["password"] == "Local Password":
passq = [inquirer.Password("password", message="Set Password")] passq = [inquirer.Password("password", message="Set Password")]
passa = inquirer.prompt(passq) passa = inquirer.prompt(passq)
if passa is None:
return False
answer["password"] = self.app.services.config_svc.encrypt_password(passa["password"]) answer["password"] = self.app.services.config_svc.encrypt_password(passa["password"])
elif answer["password"] == "Profiles": elif answer["password"] == "Profiles":
passq = [(inquirer.Text("password", message="Set a @profile or a comma separated list of @profiles", validate=self.validators.pass_validation))] passq = [(inquirer.Text("password", message="Set a @profile or a comma separated list of @profiles", validate=self.validators.pass_validation))]
passa = inquirer.prompt(passq) 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": elif answer["password"] == "No Password":
answer["password"] = "" answer["password"] = ""
fbq = [
inquirer.Text(
"fallback",
message="Add fallback credentials (@profile, @p1,@p2, JSON list/dict, 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" answer["type"] = "connection"
if "tags" in answer and not answer["tags"].startswith("@") and answer["tags"]: if "tags" in answer and answer["tags"] and not str(answer["tags"]).startswith("@"):
answer["tags"] = ast.literal_eval(answer["tags"]) answer["tags"] = _parse_tags_input(answer["tags"])
return answer return answer
+1
View File
@@ -144,6 +144,7 @@ class NodeHandler:
uniques = self.app.services.nodes.explode_unique(args.data) uniques = self.app.services.nodes.explode_unique(args.data)
if not uniques: if not uniques:
raise InvalidConfigurationError(f"Invalid folder {args.data}") raise InvalidConfigurationError(f"Invalid folder {args.data}")
self.app.services.nodes.validate_parent_folder(args.data, is_folder=True)
self.app.services.nodes.add_node(args.data, {}, is_folder=True) self.app.services.nodes.add_node(args.data, {}, is_folder=True)
printer.success(f"{args.data} added successfully") printer.success(f"{args.data} added successfully")
else: else:
+143 -26
View File
@@ -1,5 +1,19 @@
import re
import ast import ast
import json
import re
from ..auth_utils import (
MAX_FALLBACK_ENTRIES,
MAX_JSON_INPUT_BYTES,
MAX_JSON_INPUT_DEPTH,
SanitizedAuthError,
is_compose_expression,
validate_auth_budget_tags,
validate_compose_syntax,
validate_fallback_entries,
validate_json_depth_and_size,
)
def _raise_val_err(reason): def _raise_val_err(reason):
import inquirer import inquirer
@@ -56,50 +70,119 @@ class Validators:
return True return True
def pass_validation(self, answers, current, regex = "(^@.+$)"): 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(",") profiles = current.split(",")
for i in profiles: for i in profiles:
if not re.match(regex, i) or i[1:] not in self.app.profiles: if not re.match(regex, i) or i[1:] not in self.app.profiles:
_raise_val_err("Profile {} don't exist".format(i)) _raise_val_err("Profile {} don't exist".format(i))
return True 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): def tags_validation(self, answers, current):
if current.startswith("@"): if current.startswith("@"):
if current[1:] not in self.app.profiles: if current[1:] not in self.app.profiles:
_raise_val_err("Profile {} don't exist".format(current)) _raise_val_err("Profile {} don't exist".format(current))
elif current != "": elif current != "":
isdict = False return self._validate_tags_dict(current)
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 True return True
def profile_tags_validation(self, answers, current): def profile_tags_validation(self, answers, current):
if current != "": if current != "":
isdict = False return self._validate_tags_dict(current)
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 True 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 stripped.startswith("@@") and "+" not in stripped:
parts = [p.strip() for p in stripped.split(",")]
if any(not p for p in parts):
_raise_val_err("Fallback profile entry cannot be empty")
if len(parts) > MAX_FALLBACK_ENTRIES:
_raise_val_err(f"Fallback exceeds maximum of {MAX_FALLBACK_ENTRIES} entries")
for p in parts:
if not p.startswith("@") or not re.fullmatch(r"[A-Za-z0-9_.$:/-]+", p[1:]):
_raise_val_err(
"Comma-separated fallback entries must be valid @profile references"
)
if p[1:] not in self.app.profiles:
_raise_val_err(f"Profile {p} 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 _get_all_valid_nodes(self) -> list[str]:
valid_nodes = list(getattr(self.app, "nodes_list", []) or [])
if hasattr(self.app, "services") and hasattr(self.app.services, "nodes"):
raw_fn = getattr(self.app.services.nodes, "_get_raw_nodes", None)
if callable(raw_fn) and not hasattr(raw_fn, "assert_called"):
raw_res = raw_fn()
if isinstance(raw_res, (list, tuple, set)):
valid_nodes = list(dict.fromkeys([*raw_res, *valid_nodes]))
return valid_nodes
def _node_exists(self, target: str) -> bool:
valid_nodes = self._get_all_valid_nodes()
if getattr(self.app, "case", True):
return target in valid_nodes
return target.lower() in {n.lower() for n in valid_nodes}
def jumphost_validation(self, answers, current): def jumphost_validation(self, answers, current):
if current.startswith("@"): if current.startswith("@"):
if current[1:] not in self.app.profiles: if current[1:] not in self.app.profiles:
_raise_val_err("Profile {} don't exist".format(current)) _raise_val_err(f"Profile {current} don't exist")
elif current != "": elif current != "" and not self._node_exists(current):
if current not in self.app.nodes_list: _raise_val_err(f"Node {current} don't exist.")
_raise_val_err("Node {} don't exist.".format(current))
return True return True
def profile_jumphost_validation(self, answers, current): def profile_jumphost_validation(self, answers, current):
if current != "": if current != "" and not self._node_exists(current):
if current not in self.app.nodes_list: _raise_val_err(f"Node {current} don't exist.")
_raise_val_err("Node {} don't exist.".format(current))
return True return True
def default_validation(self, answers, current): def default_validation(self, answers, current):
@@ -117,12 +200,46 @@ class Validators:
return True return True
def bulk_folder_validation(self, answers, current): def bulk_folder_validation(self, answers, current):
if not self.app.case: case = getattr(self.app, "case", False)
if not case:
current = current.lower() current = current.lower()
candidate = current candidate = current.split("/")[0] if "/" in current else current
if "/" in current:
candidate = current.split("/")[0] if hasattr(self.app, "services") and hasattr(self.app.services, "context"):
raw_folders = list(getattr(self.app, "folders", []) or [])
if hasattr(self.app.services, "nodes"):
raw_fn = getattr(self.app.services.nodes, "_get_raw_folders", None)
if callable(raw_fn) and not hasattr(raw_fn, "assert_called"):
raw_res = raw_fn()
if isinstance(raw_res, (list, tuple, set)):
raw_folders = list(raw_res)
def _folder_exists_norm(folder_name: str) -> bool:
if case:
return folder_name in raw_folders
return folder_name.lower() in {f.lower() for f in raw_folders}
if current != "" and not _folder_exists_norm(candidate):
_raise_val_err(f"Location {current} don't exist")
ctx_svc = self.app.services.context
if ctx_svc is not None and not ctx_svc.is_default_all_context():
raw_ids = answers.get("ids", "") if isinstance(answers, dict) else ""
for nid in str(raw_ids).split(","):
nid_stripped = nid.strip()
if not nid_stripped:
continue
nid_norm = nid_stripped if case else nid_stripped.lower()
full_id = f"{nid_norm}{candidate}" if candidate else nid_norm
if not ctx_svc.matches_active_context(full_id):
_raise_val_err(
f"Node '{full_id}' does not match active context '{ctx_svc.current_context}'."
)
parent = full_id.partition("@")[2]
if parent and not _folder_exists_norm(f"@{parent}"):
_raise_val_err(f"Location @{parent} don't exist")
return True
matches = list(filter(lambda k: k == candidate, self.app.folders)) matches = list(filter(lambda k: k == candidate, self.app.folders))
if current != "" and len(matches) == 0: if current != "" and len(matches) == 0:
+18 -1
View File
@@ -248,9 +248,26 @@ def _build_tree(nodes, folders, profiles, plugins, configdir):
"--fzf-wrapper": ["bash", "zsh"], "--fzf-wrapper": ["bash", "zsh"],
"--service-mode": ["local", "remote"], "--service-mode": ["local", "remote"],
"--sync-remote": ["true", "false"], "--sync-remote": ["true", "false"],
"--disable-fallback-on-insecure-hostkey": ["true", "false"],
"--help": None, "-h": None, "--help": None, "-h": None,
} }
for opt in ["--keepalive", "--engineer-model", "--engineer-api-key", "--architect-model", "--architect-api-key", "--theme", "--remote", "--trusted-commands", "--shell-command", "--shell-prompt", "--shell-os"]: for opt in [
"--keepalive",
"--engineer-model",
"--engineer-api-key",
"--architect-model",
"--architect-api-key",
"--theme",
"--remote",
"--trusted-commands",
"--shell-command",
"--shell-prompt",
"--shell-os",
"--max-auth-prompts",
"--max-total-auth-attempts",
"--max-hop-host-attempts",
"--max-global-user-attempts",
]:
config_dict[opt] = {"*": config_dict} config_dict[opt] = {"*": config_dict}
config_dict["--configfolder"] = {"__extra__": lambda w: get_cwd(w, "--configfolder", True), "*": config_dict} config_dict["--configfolder"] = {"__extra__": lambda w: get_cwd(w, "--configfolder", True), "*": config_dict}
config_dict["--engineer-auth"] = {"__extra__": lambda w: get_cwd(w, "--engineer-auth"), "*": config_dict} config_dict["--engineer-auth"] = {"__extra__": lambda w: get_cwd(w, "--engineer-auth"), "*": config_dict}
+698 -213
View File
File diff suppressed because it is too large. Load diff
+14 -17
View File
@@ -1,20 +1,20 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
#Imports import argparse
import os import os
import re import re
import argparse
import sys import sys
import yaml
import sys
from .core import node,nodes
from ._version import __version__
from . import printer
from .api import start_api,stop_api,debug_api
import yaml
from rich_argparse import RichHelpFormatter
from . import printer
from .api import debug_api, start_api, stop_api
from .cli.help_text import get_help
from .cli.helpers import folders_completer, nodes_completer
from .core import node, nodes
from .plugins import Plugins from .plugins import Plugins
from .services.exceptions import ConnpyError, ProfileNotFoundError, ReservedNameError from .services.exceptions import ConnpyError, ProfileNotFoundError, ReservedNameError
from rich_argparse import RichHelpFormatter
# Bridge rich-argparse with our design system # Bridge rich-argparse with our design system
RichHelpFormatter.console = printer.console RichHelpFormatter.console = printer.console
RichHelpFormatter.styles.update({ RichHelpFormatter.styles.update({
@@ -28,14 +28,6 @@ RichHelpFormatter.styles.update({
}) })
RichHelpFormatter.group_name_formatter = str.upper RichHelpFormatter.group_name_formatter = str.upper
from .cli import (
NodeHandler, ProfileHandler, ConfigHandler, RunHandler,
AIHandler, APIHandler, PluginHandler, ImportExportHandler,
ContextHandler, SSOHandler
)
from .cli.helpers import nodes_completer, folders_completer, profiles_completer
from .cli.help_text import get_help
console = printer.console console = printer.console
#functions and classes #functions and classes
@@ -389,6 +381,11 @@ class connapp:
configcrud.add_argument("--shell-command", dest="shell_command", nargs=1, action=self._store_type, help="Set default shell command", metavar="COMMAND") configcrud.add_argument("--shell-command", dest="shell_command", nargs=1, action=self._store_type, help="Set default shell command", metavar="COMMAND")
configcrud.add_argument("--shell-prompt", dest="shell_prompt", nargs=1, action=self._store_type, help="Set shell prompt regex for AI", metavar="REGEX") configcrud.add_argument("--shell-prompt", dest="shell_prompt", nargs=1, action=self._store_type, help="Set shell prompt regex for AI", metavar="REGEX")
configcrud.add_argument("--shell-os", dest="shell_os", nargs=1, action=self._store_type, help="Set shell OS hint for AI", metavar="OS") configcrud.add_argument("--shell-os", dest="shell_os", nargs=1, action=self._store_type, help="Set shell OS hint for AI", metavar="OS")
configcrud.add_argument("--max-auth-prompts", dest="max_auth_prompts", nargs=1, action=self._store_type, help="Set max auth prompts per connection (1-5, 0 to reset)", type=int, metavar="INT")
configcrud.add_argument("--max-total-auth-attempts", dest="max_total_auth_attempts", nargs=1, action=self._store_type, help="Set max total auth attempts per host (1-10, 0 to reset)", type=int, metavar="INT")
configcrud.add_argument("--max-hop-host-attempts", dest="max_hop_host_attempts", nargs=1, action=self._store_type, help="Set max auth attempts per jumphost (1-10, 0 to reset)", type=int, metavar="INT")
configcrud.add_argument("--max-global-user-attempts", dest="max_global_user_attempts", nargs=1, action=self._store_type, help="Set max global auth attempts per user (1-1000, 0 to reset)", type=int, metavar="INT")
configcrud.add_argument("--disable-fallback-on-insecure-hostkey", dest="disable_fallback_on_insecure_hostkey", nargs=1, action=self._store_type, help="Disable AAA fallback when StrictHostKeyChecking=no", choices=["true", "false"])
configparser.add_argument("--trusted-commands", dest="trusted_commands", nargs=1, action=self._store_type, help="Set custom trusted commands regexes (comma separated)", metavar="REGEX,REGEX") configparser.add_argument("--trusted-commands", dest="trusted_commands", nargs=1, action=self._store_type, help="Set custom trusted commands regexes (comma separated)", metavar="REGEX,REGEX")
configparser.set_defaults(func=self._config.dispatch) configparser.set_defaults(func=self._config.dispatch)
+2178 -330
View File
File diff suppressed because it is too large. Load diff
+95 -53
View File
@@ -1,26 +1,37 @@
import grpc
from concurrent import futures
from google.protobuf.empty_pb2 import Empty
import os
import ctypes
import threading
import contextvars import contextvars
import datetime import datetime
import functools
import json
import os
from concurrent import futures
import grpc
from google.protobuf.empty_pb2 import Empty
# Suppress harmless but noisy gRPC fork() warnings from pexpect child processes # Suppress harmless but noisy gRPC fork() warnings from pexpect child processes
os.environ["GRPC_VERBOSITY"] = "NONE" os.environ["GRPC_VERBOSITY"] = "NONE"
os.environ["GRPC_ENABLE_FORK_SUPPORT"] = "0" os.environ["GRPC_ENABLE_FORK_SUPPORT"] = "0"
from . import connpy_pb2, connpy_pb2_grpc, remote_plugin_pb2, remote_plugin_pb2_grpc
import json
from .utils import to_value, from_value, to_struct, from_struct
from ..services.exceptions import ConnpyError
from .. import printer from .. import printer
from ..auth_utils import (
MAX_FALLBACK_ENTRIES,
MAX_JSON_INPUT_BYTES,
MAX_JSON_INPUT_DEPTH,
SanitizedAuthError,
sanitize_exception,
validate_auth_budget_tags,
validate_fallback_entries,
validate_json_depth_and_size,
)
from ..services.exceptions import ConnpyError
from . import connpy_pb2, connpy_pb2_grpc, remote_plugin_pb2, remote_plugin_pb2_grpc
from .utils import from_struct, from_value, to_struct, to_value
_current_user = contextvars.ContextVar("current_user", default=None) _current_user = contextvars.ContextVar("current_user", default=None)
def handle_errors(func): def handle_errors(func):
import inspect import inspect
if inspect.isgeneratorfunction(func): if inspect.isgeneratorfunction(func):
@functools.wraps(func)
def wrapper(*args, **kwargs): def wrapper(*args, **kwargs):
try: try:
for item in func(*args, **kwargs): for item in func(*args, **kwargs):
@@ -41,6 +52,7 @@ def handle_errors(func):
printer.clear_thread_state() printer.clear_thread_state()
return wrapper return wrapper
else: else:
@functools.wraps(func)
def wrapper(*args, **kwargs): def wrapper(*args, **kwargs):
try: try:
return func(*args, **kwargs) return func(*args, **kwargs)
@@ -60,11 +72,19 @@ def handle_errors(func):
printer.clear_thread_state() printer.clear_thread_state()
return wrapper return wrapper
class NodeServicer(connpy_pb2_grpc.NodeServiceServicer): def _ensure_server_provider(provider):
def __init__(self, provider, registry=None, debug=False):
if not hasattr(provider, "mode"): if not hasattr(provider, "mode"):
from connpy.services.provider import ServiceProvider from connpy.services.provider import ServiceProvider
provider = ServiceProvider(provider, mode="local")
return ServiceProvider(provider, mode="local", enforce_context=False)
if hasattr(provider, "set_enforce_context") and callable(provider.set_enforce_context):
provider.set_enforce_context(False)
return provider
class NodeServicer(connpy_pb2_grpc.NodeServiceServicer):
def __init__(self, provider, registry=None, debug=False):
provider = _ensure_server_provider(provider)
self._fallback_provider = provider self._fallback_provider = provider
self._registry = registry self._registry = registry
self.server_debug = debug self.server_debug = debug
@@ -115,8 +135,24 @@ class NodeServicer(connpy_pb2_grpc.NodeServiceServicer):
self.server_console.print(f"[debug][DEBUG][/debug] gRPC interact_node request for: [bold cyan]{unique_id}[/bold cyan]") 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: if first_req.connection_params_json:
import json try:
params = json.loads(first_req.connection_params_json) 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") base_node_id = params.get("base_node")
# Valid attributes that a node object accepts # Valid attributes that a node object accepts
valid_attrs = ['host', 'options', 'logs', 'password', 'port', 'protocol', 'user', 'jumphost'] valid_attrs = ['host', 'options', 'logs', 'password', 'port', 'protocol', 'user', 'jumphost']
@@ -134,6 +170,8 @@ class NodeServicer(connpy_pb2_grpc.NodeServiceServicer):
for attr in valid_attrs: for attr in valid_attrs:
if attr in params: if attr in params:
device[attr] = params[attr] device[attr] = params[attr]
if "fallback" in params:
device["fallback"] = params["fallback"]
if "tags" in params: if "tags" in params:
device_tags = device.get("tags", {}) device_tags = device.get("tags", {})
@@ -147,20 +185,30 @@ class NodeServicer(connpy_pb2_grpc.NodeServiceServicer):
else: else:
# base_node not found, fall back to dynamic # base_node not found, fall back to dynamic
node_name = params.get("name", fallback_id) node_name = params.get("name", fallback_id)
n = node(node_name, host=params.get("host", ""), config=user_config) dyn_kwargs = {k: params[k] for k in valid_attrs if k in params and k != "host"}
for attr in valid_attrs: if "fallback" in params:
if attr in params: dyn_kwargs["fallback"] = params["fallback"]
setattr(n, attr, params[attr])
if "tags" in params: 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: else:
node_name = params.get("name", fallback_id) node_name = params.get("name", fallback_id)
n = node(node_name, host=params.get("host", ""), config=user_config) dyn_kwargs = {k: params[k] for k in valid_attrs if k in params and k != "host"}
for attr in valid_attrs: if "fallback" in params:
if attr in params: dyn_kwargs["fallback"] = params["fallback"]
setattr(n, attr, params[attr])
if "tags" in params: 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 n._orig_protocol = n.protocol
if sftp: if sftp:
n.protocol = "sftp" n.protocol = "sftp"
@@ -181,9 +229,11 @@ class NodeServicer(connpy_pb2_grpc.NodeServiceServicer):
debug_chunks = [] debug_chunks = []
if debug: if debug:
from io import StringIO from io import StringIO
from rich.console import Console as RichConsole from rich.console import Console as RichConsole
from ..printer import connpy_theme
from .. import printer as _printer from .. import printer as _printer
from ..printer import connpy_theme
def remote_logger(msg_type, message): def remote_logger(msg_type, message):
buf = StringIO() buf = StringIO()
@@ -204,7 +254,14 @@ class NodeServicer(connpy_pb2_grpc.NodeServiceServicer):
else: else:
remote_logger = None 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) # Send debug output to client before checking result (always show the command)
for chunk in debug_chunks: for chunk in debug_chunks:
@@ -535,10 +592,9 @@ class NodeServicer(connpy_pb2_grpc.NodeServiceServicer):
continue continue
asyncio.run(n._async_interact_loop(remote_stream, resize_callback, copilot_handler=remote_copilot_handler)) asyncio.run(n._async_interact_loop(remote_stream, resize_callback, copilot_handler=remote_copilot_handler))
except Exception as e: except Exception as e: # noqa: BLE001
import traceback sanitized_err = sanitize_exception(e, code="TRANSPORT_ERROR", context_label="interact stream")
print(f"[ERROR in run_async_loop] {e}") print(f"[ERROR in run_async_loop] {sanitized_err}")
traceback.print_exc()
finally: finally:
n._teardown_interact_environment() n._teardown_interact_environment()
response_queue.put(None) # Signal EOF response_queue.put(None) # Signal EOF
@@ -641,9 +697,7 @@ class NodeServicer(connpy_pb2_grpc.NodeServiceServicer):
class ProfileServicer(connpy_pb2_grpc.ProfileServiceServicer): class ProfileServicer(connpy_pb2_grpc.ProfileServiceServicer):
def __init__(self, provider, registry=None): def __init__(self, provider, registry=None):
if not hasattr(provider, "mode"): provider = _ensure_server_provider(provider)
from connpy.services.provider import ServiceProvider
provider = ServiceProvider(provider, mode="local")
self._fallback_provider = provider self._fallback_provider = provider
self._registry = registry self._registry = registry
@@ -695,9 +749,7 @@ class ProfileServicer(connpy_pb2_grpc.ProfileServiceServicer):
class ConfigServicer(connpy_pb2_grpc.ConfigServiceServicer): class ConfigServicer(connpy_pb2_grpc.ConfigServiceServicer):
def __init__(self, provider, registry=None): def __init__(self, provider, registry=None):
if not hasattr(provider, "mode"): provider = _ensure_server_provider(provider)
from connpy.services.provider import ServiceProvider
provider = ServiceProvider(provider, mode="local")
self._fallback_provider = provider self._fallback_provider = provider
self._registry = registry self._registry = registry
@@ -740,9 +792,7 @@ class ConfigServicer(connpy_pb2_grpc.ConfigServiceServicer):
class PluginServicer(connpy_pb2_grpc.PluginServiceServicer, remote_plugin_pb2_grpc.RemotePluginServiceServicer): class PluginServicer(connpy_pb2_grpc.PluginServiceServicer, remote_plugin_pb2_grpc.RemotePluginServiceServicer):
def __init__(self, provider, registry=None): def __init__(self, provider, registry=None):
if not hasattr(provider, "mode"): provider = _ensure_server_provider(provider)
from connpy.services.provider import ServiceProvider
provider = ServiceProvider(provider, mode="local")
self._fallback_provider = provider self._fallback_provider = provider
self._registry = registry self._registry = registry
@@ -798,9 +848,7 @@ class PluginServicer(connpy_pb2_grpc.PluginServiceServicer, remote_plugin_pb2_gr
class ExecutionServicer(connpy_pb2_grpc.ExecutionServiceServicer): class ExecutionServicer(connpy_pb2_grpc.ExecutionServiceServicer):
def __init__(self, provider, registry=None): def __init__(self, provider, registry=None):
if not hasattr(provider, "mode"): provider = _ensure_server_provider(provider)
from connpy.services.provider import ServiceProvider
provider = ServiceProvider(provider, mode="local")
self._fallback_provider = provider self._fallback_provider = provider
self._registry = registry self._registry = registry
@@ -929,9 +977,7 @@ class ExecutionServicer(connpy_pb2_grpc.ExecutionServiceServicer):
class ImportExportServicer(connpy_pb2_grpc.ImportExportServiceServicer): class ImportExportServicer(connpy_pb2_grpc.ImportExportServiceServicer):
def __init__(self, provider, registry=None): def __init__(self, provider, registry=None):
if not hasattr(provider, "mode"): provider = _ensure_server_provider(provider)
from connpy.services.provider import ServiceProvider
provider = ServiceProvider(provider, mode="local")
self._fallback_provider = provider self._fallback_provider = provider
self._registry = registry self._registry = registry
@@ -1064,9 +1110,7 @@ class StatusBridge:
class AIServicer(connpy_pb2_grpc.AIServiceServicer): class AIServicer(connpy_pb2_grpc.AIServiceServicer):
def __init__(self, provider, registry=None, debug=False): def __init__(self, provider, registry=None, debug=False):
if not hasattr(provider, "mode"): provider = _ensure_server_provider(provider)
from connpy.services.provider import ServiceProvider
provider = ServiceProvider(provider, mode="local")
self._fallback_provider = provider self._fallback_provider = provider
self._registry = registry self._registry = registry
self.server_debug = debug self.server_debug = debug
@@ -1351,9 +1395,7 @@ class AIServicer(connpy_pb2_grpc.AIServiceServicer):
class SystemServicer(connpy_pb2_grpc.SystemServiceServicer): class SystemServicer(connpy_pb2_grpc.SystemServiceServicer):
def __init__(self, provider, registry=None): def __init__(self, provider, registry=None):
if not hasattr(provider, "mode"): provider = _ensure_server_provider(provider)
from connpy.services.provider import ServiceProvider
provider = ServiceProvider(provider, mode="local")
self._fallback_provider = provider self._fallback_provider = provider
self._registry = registry self._registry = registry
@@ -1736,7 +1778,7 @@ def serve(config, port=8048, debug=False):
from connpy.grpc_layer.user_registry import UserRegistry from connpy.grpc_layer.user_registry import UserRegistry
from connpy.services.provider import ServiceProvider from connpy.services.provider import ServiceProvider
fallback_provider = ServiceProvider(config, mode="local") fallback_provider = ServiceProvider(config, mode="local", enforce_context=False)
registry = UserRegistry(config.defaultdir) registry = UserRegistry(config.defaultdir)
# Check if trusted_gateway provider is configured if SSO Gateway Secret is present in env # Check if trusted_gateway provider is configured if SSO Gateway Secret is present in env
+151 -21
View File
@@ -1,16 +1,19 @@
import grpc
import queue import queue
import re
import threading import threading
from functools import wraps from functools import wraps
import grpc
from google.protobuf.empty_pb2 import Empty from google.protobuf.empty_pb2 import Empty
from . import connpy_pb2, connpy_pb2_grpc, remote_plugin_pb2, remote_plugin_pb2_grpc
from .utils import to_value, from_value, to_struct, from_struct
from ..services.exceptions import ConnpyError
from ..hooks import MethodHook
from .. import printer from .. import printer
from ..cli.terminal_ui import CopilotInterface from ..auth_utils import MAX_REMOTE_FILTER_NODES
from ..hooks import MethodHook
from ..services.exceptions import ConnpyError, NodeNotFoundError
from ..utils import log_cleaner from ..utils import log_cleaner
from . import connpy_pb2, connpy_pb2_grpc, remote_plugin_pb2, remote_plugin_pb2_grpc
from .utils import from_struct, from_value, to_struct, to_value
def handle_errors(func): def handle_errors(func):
@wraps(func) @wraps(func)
@@ -43,8 +46,48 @@ class NodeStub:
self.remote_host = remote_host self.remote_host = remote_host
self.config = config self.config = config
def _get_context_service(self):
if not getattr(self.config, "_enforce_context", True):
return None
if not isinstance(getattr(self.config, "config", None), dict):
return None
from ..services.context_service import ContextService
return ContextService(self.config)
def _ensure_in_active_context(
self, unique_id: str, *, is_folder: bool = False, not_found_msg: str | None = None
) -> None:
ctx_svc = self._get_context_service()
if (
ctx_svc is not None
and not ctx_svc.is_default_all_context()
and not ctx_svc.matches_active_context(unique_id)
):
if not_found_msg is not None:
raise NodeNotFoundError(not_found_msg)
label = "Folder" if is_folder else "Node"
raise NodeNotFoundError(f"{label} '{unique_id}' not found.")
@handle_errors
def _get_raw_nodes(self) -> list[str]:
req = connpy_pb2.FilterRequest(filter_str="", format_str="")
return from_value(self.stub.list_nodes(req).data) or []
@handle_errors
def _get_raw_folders(self) -> list[str]:
req = connpy_pb2.FilterRequest(filter_str="")
return from_value(self.stub.list_folders(req).data) or []
def _handle_remote_copilot(self, res, request_queue, response_queue, client_buffer_bytes, pause_generator, resume_generator, old_tty): def _handle_remote_copilot(self, res, request_queue, response_queue, client_buffer_bytes, pause_generator, resume_generator, old_tty):
import json, asyncio, termios, sys, tty, queue, os import asyncio
import json
import os
import sys
import termios
import tty
from ..cli.terminal_ui import CopilotInterface
from ..core import copilot_terminal_mode from ..core import copilot_terminal_mode
from . import connpy_pb2 from . import connpy_pb2
@@ -138,6 +181,7 @@ class NodeStub:
@handle_errors @handle_errors
def connect_node(self, unique_id, sftp=False, debug=False, logger=None): def connect_node(self, unique_id, sftp=False, debug=False, logger=None):
self._ensure_in_active_context(unique_id, is_folder=False)
import sys import sys
import select import select
import tty import tty
@@ -429,16 +473,26 @@ class NodeStub:
@handle_errors @handle_errors
def list_nodes(self, filter_str=None, format_str=None): def list_nodes(self, filter_str=None, format_str=None):
req = connpy_pb2.FilterRequest(filter_str=filter_str or "", format_str=format_str or "") req = connpy_pb2.FilterRequest(filter_str=filter_str or "", format_str=format_str or "")
return from_value(self.stub.list_nodes(req).data) or [] nodes = from_value(self.stub.list_nodes(req).data) or []
if not format_str:
ctx_svc = self._get_context_service()
if ctx_svc is not None and not ctx_svc.is_default_all_context():
nodes = ctx_svc.filter_node_list(result=nodes)
return nodes
@MethodHook @MethodHook
@handle_errors @handle_errors
def list_folders(self, filter_str=None): def list_folders(self, filter_str=None):
req = connpy_pb2.FilterRequest(filter_str=filter_str or "") req = connpy_pb2.FilterRequest(filter_str=filter_str or "")
return from_value(self.stub.list_folders(req).data) or [] folders = from_value(self.stub.list_folders(req).data) or []
ctx_svc = self._get_context_service()
if ctx_svc is not None and not ctx_svc.is_default_all_context():
folders = ctx_svc.filter_node_list(result=folders)
return folders
@handle_errors @handle_errors
def get_node_details(self, unique_id): def get_node_details(self, unique_id):
self._ensure_in_active_context(unique_id, is_folder=False)
return from_struct(self.stub.get_node_details(connpy_pb2.IdRequest(id=unique_id)).data) return from_struct(self.stub.get_node_details(connpy_pb2.IdRequest(id=unique_id)).data)
@handle_errors @handle_errors
@@ -446,7 +500,11 @@ class NodeStub:
return from_value(self.stub.explode_unique(connpy_pb2.IdRequest(id=unique_id)).data) return from_value(self.stub.explode_unique(connpy_pb2.IdRequest(id=unique_id)).data)
@handle_errors @handle_errors
def validate_parent_folder(self, unique_id): def validate_parent_folder(self, unique_id, is_folder=False):
ctx_svc = self._get_context_service()
if ctx_svc is not None:
ctx_svc.validate_in_active_context(unique_id, is_folder=is_folder)
if not is_folder:
self.stub.validate_parent_folder(connpy_pb2.IdRequest(id=unique_id)) self.stub.validate_parent_folder(connpy_pb2.IdRequest(id=unique_id))
@handle_errors @handle_errors
@@ -463,7 +521,7 @@ class NodeStub:
# node cache is what matters for fzf. We'll fetch profiles if we can. # node cache is what matters for fzf. We'll fetch profiles if we can.
# For now, let's sync what we have. # For now, let's sync what we have.
if nodes is not None or folders is not None or profiles is not None: if self.config is not None and (nodes is not None or folders is not None or profiles is not None):
self.config._generate_nodes_cache(nodes=nodes, folders=folders, profiles=profiles) self.config._generate_nodes_cache(nodes=nodes, folders=folders, profiles=profiles)
def _trigger_local_cache_sync(self): def _trigger_local_cache_sync(self):
@@ -478,12 +536,16 @@ class NodeStub:
@handle_errors @handle_errors
def add_node(self, unique_id, data, is_folder=False): def add_node(self, unique_id, data, is_folder=False):
ctx_svc = self._get_context_service()
if ctx_svc is not None:
ctx_svc.validate_in_active_context(unique_id, is_folder=is_folder)
req = connpy_pb2.NodeRequest(id=unique_id, data=to_struct(data), is_folder=is_folder) req = connpy_pb2.NodeRequest(id=unique_id, data=to_struct(data), is_folder=is_folder)
self.stub.add_node(req) self.stub.add_node(req)
self._trigger_local_cache_sync() self._trigger_local_cache_sync()
@handle_errors @handle_errors
def update_node(self, unique_id, data, save=True): def update_node(self, unique_id, data, save=True):
self._ensure_in_active_context(unique_id, is_folder=False)
req = connpy_pb2.NodeRequest(id=unique_id, data=to_struct(data), is_folder=False) req = connpy_pb2.NodeRequest(id=unique_id, data=to_struct(data), is_folder=False)
self.stub.update_node(req) self.stub.update_node(req)
if save: if save:
@@ -491,6 +553,7 @@ class NodeStub:
@handle_errors @handle_errors
def delete_node(self, unique_id, is_folder=False, save=True): def delete_node(self, unique_id, is_folder=False, save=True):
self._ensure_in_active_context(unique_id, is_folder=is_folder)
req = connpy_pb2.DeleteRequest(id=unique_id, is_folder=is_folder) req = connpy_pb2.DeleteRequest(id=unique_id, is_folder=is_folder)
self.stub.delete_node(req) self.stub.delete_node(req)
if save: if save:
@@ -498,12 +561,22 @@ class NodeStub:
@handle_errors @handle_errors
def move_node(self, src_id, dst_id, copy=False): def move_node(self, src_id, dst_id, copy=False):
self._ensure_in_active_context(
src_id, is_folder=False, not_found_msg=f"Source node '{src_id}' not found."
)
ctx_svc = self._get_context_service()
if ctx_svc is not None:
ctx_svc.validate_in_active_context(dst_id, is_folder=False)
req = connpy_pb2.MoveRequest(src_id=src_id, dst_id=dst_id, copy=copy) req = connpy_pb2.MoveRequest(src_id=src_id, dst_id=dst_id, copy=copy)
self.stub.move_node(req) self.stub.move_node(req)
self._trigger_local_cache_sync() self._trigger_local_cache_sync()
@handle_errors @handle_errors
def bulk_add(self, ids, hosts, common_data): def bulk_add(self, ids, hosts, common_data):
ctx_svc = self._get_context_service()
if ctx_svc is not None:
for uid in ids:
ctx_svc.validate_in_active_context(uid, is_folder=False)
req = connpy_pb2.BulkRequest(ids=ids, hosts=hosts, common_data=to_struct(common_data)) req = connpy_pb2.BulkRequest(ids=ids, hosts=hosts, common_data=to_struct(common_data))
self.stub.bulk_add(req) self.stub.bulk_add(req)
self._trigger_local_cache_sync() self._trigger_local_cache_sync()
@@ -649,13 +722,58 @@ class PluginStub:
yield chunk.text yield chunk.text
class ExecutionStub: class ExecutionStub:
def __init__(self, channel, remote_host): def __init__(self, channel, remote_host, config=None, node_stub=None):
self.stub = connpy_pb2_grpc.ExecutionServiceStub(channel) self.stub = connpy_pb2_grpc.ExecutionServiceStub(channel)
self.remote_host = remote_host self.remote_host = remote_host
self.config = config
self.node_stub = node_stub
def _get_context_service(self):
if not getattr(self.config, "_enforce_context", True):
return None
if not isinstance(getattr(self.config, "config", None), dict):
return None
from ..services.context_service import ContextService
return ContextService(self.config)
def _filter_candidates_by_filter_str(self, candidates: list[str], nodes_filter: str) -> list[str]:
if not nodes_filter:
return list(candidates)
cfg_dict = getattr(self.config, "config", None)
case = cfg_dict.get("case", False) if isinstance(cfg_dict, dict) else False
flags = 0 if case else re.IGNORECASE
return [c for c in candidates if re.search(nodes_filter, c, flags)]
def _resolve_remote_batches(self, nodes_filter):
ctx_svc = self._get_context_service()
if ctx_svc is not None and not ctx_svc.is_default_all_context():
if isinstance(nodes_filter, (list, tuple)):
permitted = ctx_svc.filter_node_list(result=list(nodes_filter))
else:
candidates = (
self.node_stub.list_nodes(filter_str=nodes_filter)
if self.node_stub is not None
else []
)
candidates = self._filter_candidates_by_filter_str(candidates, nodes_filter)
permitted = ctx_svc.filter_node_list(result=candidates)
if not permitted:
raise ConnpyError(f"No nodes found matching filter: {nodes_filter}")
patterns = [f"^{re.escape(n)}$" for n in permitted]
return [
patterns[i : i + MAX_REMOTE_FILTER_NODES]
for i in range(0, len(patterns), MAX_REMOTE_FILTER_NODES)
]
return [[nodes_filter] if isinstance(nodes_filter, str) else list(nodes_filter)]
@handle_errors @handle_errors
def run_commands(self, nodes_filter, commands, variables=None, parallel=10, timeout=10, folder=None, prompt=None, **kwargs): def run_commands(self, nodes_filter, commands, variables=None, parallel=10, timeout=10, folder=None, prompt=None, **kwargs):
nodes_list = [nodes_filter] if isinstance(nodes_filter, str) else list(nodes_filter) batches = self._resolve_remote_batches(nodes_filter)
final_results = {}
on_complete = kwargs.get("on_node_complete")
for nodes_list in batches:
req = connpy_pb2.RunRequest( req = connpy_pb2.RunRequest(
nodes=nodes_list, nodes=nodes_list,
commands=commands, commands=commands,
@@ -668,9 +786,6 @@ class ExecutionStub:
if variables is not None: if variables is not None:
req.vars.CopyFrom(to_struct(variables)) req.vars.CopyFrom(to_struct(variables))
final_results = {}
on_complete = kwargs.get("on_node_complete")
for response in self.stub.run_commands(req): for response in self.stub.run_commands(req):
if on_complete: if on_complete:
on_complete(response.unique_id, response.output, response.status) on_complete(response.unique_id, response.output, response.status)
@@ -683,7 +798,11 @@ class ExecutionStub:
@handle_errors @handle_errors
def test_commands(self, nodes_filter, commands, expected, variables=None, parallel=10, timeout=10, prompt=None, **kwargs): def test_commands(self, nodes_filter, commands, expected, variables=None, parallel=10, timeout=10, prompt=None, **kwargs):
nodes_list = [nodes_filter] if isinstance(nodes_filter, str) else list(nodes_filter) batches = self._resolve_remote_batches(nodes_filter)
final_results = {}
on_complete = kwargs.get("on_node_complete")
for nodes_list in batches:
req = connpy_pb2.TestRequest( req = connpy_pb2.TestRequest(
nodes=nodes_list, nodes=nodes_list,
commands=commands, commands=commands,
@@ -697,9 +816,6 @@ class ExecutionStub:
if variables is not None: if variables is not None:
req.vars.CopyFrom(to_struct(variables)) req.vars.CopyFrom(to_struct(variables))
final_results = {}
on_complete = kwargs.get("on_node_complete")
for response in self.stub.test_commands(req): for response in self.stub.test_commands(req):
result_dict = from_struct(response.test_result) if response.HasField("test_result") else {} result_dict = from_struct(response.test_result) if response.HasField("test_result") else {}
if on_complete: if on_complete:
@@ -710,7 +826,21 @@ class ExecutionStub:
@handle_errors @handle_errors
def run_cli_script(self, nodes_filter, script_path, parallel=10): def run_cli_script(self, nodes_filter, script_path, parallel=10):
req = connpy_pb2.ScriptRequest(param1=nodes_filter, param2=script_path, parallel=parallel) ctx_svc = self._get_context_service()
effective_filter = nodes_filter
if ctx_svc is not None and not ctx_svc.is_default_all_context():
candidates = (
self.node_stub.list_nodes(filter_str=nodes_filter)
if self.node_stub is not None
else []
)
if isinstance(nodes_filter, str):
candidates = self._filter_candidates_by_filter_str(candidates, nodes_filter)
permitted = ctx_svc.filter_node_list(result=candidates)
if not permitted:
raise ConnpyError(f"No nodes found matching filter: {nodes_filter}")
effective_filter = "|".join(f"^{re.escape(n)}$" for n in permitted)
req = connpy_pb2.ScriptRequest(param1=effective_filter, param2=script_path, parallel=parallel)
return from_struct(self.stub.run_cli_script(req).data) return from_struct(self.stub.run_cli_script(req).data)
class ImportExportStub: class ImportExportStub:
+1 -1
View File
@@ -62,7 +62,7 @@ class UserRegistry:
try: try:
# Attempt a fresh configuration load # Attempt a fresh configuration load
config = configfile(conf=conf_file, shared_config=self._shared_config) config = configfile(conf=conf_file, shared_config=self._shared_config)
new_provider = ServiceProvider(config, mode="local") new_provider = ServiceProvider(config, mode="local", enforce_context=False)
# Successfully loaded, clean up the old provider # Successfully loaded, clean up the old provider
if old_provider: if old_provider:
+120 -50
View File
@@ -6,6 +6,20 @@ from typing import Any, ClassVar
import yaml import yaml
from .auth_utils import (
MAX_FALLBACK_ENTRIES,
MAX_JSON_INPUT_BYTES,
MAX_JSON_INPUT_DEPTH,
SanitizedAuthError,
is_compose_expression,
mask_node_secrets,
sanitize_exception,
validate_auth_budget_tags,
validate_compose_syntax,
validate_fallback_entries,
validate_json_depth_and_size,
)
try: try:
from mcp.server.mcpserver import MCPServer from mcp.server.mcpserver import MCPServer
except ImportError: except ImportError:
@@ -124,20 +138,7 @@ class ConnpyMCPServer:
def _mask_secrets(self, val: Any) -> Any: def _mask_secrets(self, val: Any) -> Any:
"""Recursively mask passwords, passphrases, secrets, and auth tokens with '***'.""" """Recursively mask passwords, passphrases, secrets, and auth tokens with '***'."""
if isinstance(val, dict): return mask_node_secrets(val)
masked = {}
for k, v in val.items():
k_lower = str(k).lower()
if any(sec in k_lower for sec in ["password", "passphrase", "secret", "token"]):
masked[k] = "***"
elif isinstance(v, (dict, list)):
masked[k] = self._mask_secrets(v)
else:
masked[k] = v
return masked
if isinstance(val, list):
return [self._mask_secrets(item) for item in val]
return val
# ------------------------------------------------------------------------- # -------------------------------------------------------------------------
# Level 1: Read-Only Inventory and Context Tools # Level 1: Read-Only Inventory and Context Tools
@@ -196,6 +197,31 @@ class ConnpyMCPServer:
# Level 2: Execution, Testing, and Playbooks Tools # Level 2: Execution, Testing, and Playbooks Tools
# ------------------------------------------------------------------------- # -------------------------------------------------------------------------
@staticmethod
def _parse_json_list_or_lines(raw_val: list[str] | str, *, allow_empty_str: bool = False) -> list[str]:
if isinstance(raw_val, str):
if len(raw_val.encode("utf-8", errors="replace")) > MAX_JSON_INPUT_BYTES:
raise ValueError(f"Input exceeds maximum size of {MAX_JSON_INPUT_BYTES} bytes")
stripped = raw_val.strip()
if stripped.startswith(("[", "{")):
parsed = validate_json_depth_and_size(
stripped,
max_bytes=MAX_JSON_INPUT_BYTES,
max_depth=MAX_JSON_INPUT_DEPTH,
)
return parsed if isinstance(parsed, list) else [raw_val]
return ([raw_val] if raw_val else []) if allow_empty_str else [
c.strip() for c in raw_val.splitlines() if c.strip()
]
if isinstance(raw_val, list):
validate_json_depth_and_size(
raw_val,
max_bytes=MAX_JSON_INPUT_BYTES,
max_depth=MAX_JSON_INPUT_DEPTH,
)
return raw_val
return [str(raw_val)] if (raw_val or not allow_empty_str) else []
def run_commands( def run_commands(
self, self,
nodes_filter: str, nodes_filter: str,
@@ -210,17 +236,10 @@ class ConnpyMCPServer:
if not self.services or not hasattr(self.services, "execution"): if not self.services or not hasattr(self.services, "execution"):
return {"success": False, "error": "Execution service unavailable."} return {"success": False, "error": "Execution service unavailable."}
# Normalize commands list
if isinstance(commands, str):
try: try:
import json commands = self._parse_json_list_or_lines(commands, allow_empty_str=False)
except ValueError as exc:
parsed = json.loads(commands) return {"success": False, "error": str(exc)}
commands = parsed if isinstance(parsed, list) else [commands]
except Exception: # noqa: BLE001
commands = [c.strip() for c in commands.splitlines() if c.strip()]
elif not isinstance(commands, list):
commands = [str(commands)]
# Check safety # Check safety
if not self.trust: if not self.trust:
@@ -239,9 +258,24 @@ class ConnpyMCPServer:
parallel=par_count, parallel=par_count,
timeout=timeout, timeout=timeout,
) )
return {"success": True, "results": results} fb_used = False
if isinstance(results, dict):
fb_used = any(
isinstance(v, dict) and bool(v.get("fallback_used", False))
for v in results.values()
)
if not fb_used:
fb_map = getattr(self.services.execution, "last_fallback_used", {})
if isinstance(fb_map, dict):
fb_used = any(bool(v) for v in fb_map.values())
return {"success": True, "results": results, "fallback_used": fb_used}
except Exception as e: # noqa: BLE001 except Exception as e: # noqa: BLE001
return {"success": False, "error": str(e)} san = sanitize_exception(
e,
code="AUTH_FAILED",
context_label="command execution failed",
)
return {"success": False, "error": str(san)}
def test_commands( def test_commands(
self, self,
@@ -255,29 +289,11 @@ class ConnpyMCPServer:
if not self.services or not hasattr(self.services, "execution"): if not self.services or not hasattr(self.services, "execution"):
return {"success": False, "error": "Execution service unavailable."} return {"success": False, "error": "Execution service unavailable."}
# Normalize commands
if isinstance(commands, str):
try: try:
import json commands = self._parse_json_list_or_lines(commands, allow_empty_str=False)
expected = self._parse_json_list_or_lines(expected, allow_empty_str=True)
parsed = json.loads(commands) except ValueError as exc:
commands = parsed if isinstance(parsed, list) else [commands] return {"success": False, "error": str(exc)}
except Exception: # noqa: BLE001
commands = [c.strip() for c in commands.splitlines() if c.strip()]
elif not isinstance(commands, list):
commands = [str(commands)]
# Normalize expected
if isinstance(expected, str):
try:
import json
parsed = json.loads(expected)
expected = parsed if isinstance(parsed, list) else ([expected] if expected else [])
except Exception: # noqa: BLE001
expected = [expected] if expected else []
elif not isinstance(expected, list):
expected = [str(expected)] if expected else []
# Check safety # Check safety
if not self.trust: if not self.trust:
@@ -297,9 +313,16 @@ class ConnpyMCPServer:
parallel=par_count, parallel=par_count,
timeout=timeout, timeout=timeout,
) )
return {"success": True, "results": results} fb_map = getattr(self.services.execution, "last_fallback_used", {})
fb_used = any(bool(v) for v in fb_map.values()) if isinstance(fb_map, dict) else False
return {"success": True, "results": results, "fallback_used": fb_used}
except Exception as e: # noqa: BLE001 except Exception as e: # noqa: BLE001
return {"success": False, "error": str(e)} san = sanitize_exception(
e,
code="AUTH_FAILED",
context_label="test execution failed",
)
return {"success": False, "error": str(san)}
def validate_playbook(self, playbook_yaml: str) -> dict: def validate_playbook(self, playbook_yaml: str) -> dict:
"""Validate the syntax, schema, and tasks of a Connpy YAML playbook.""" """Validate the syntax, schema, and tasks of a Connpy YAML playbook."""
@@ -427,6 +450,47 @@ class ConnpyMCPServer:
# Level 3: Inventory Mutation Tools (CRUD - Requires trust=True) # Level 3: Inventory Mutation Tools (CRUD - Requires trust=True)
# ------------------------------------------------------------------------- # -------------------------------------------------------------------------
@staticmethod
def _validate_and_normalize_node_payload(node_data: dict[str, Any]) -> dict[str, Any]:
validate_json_depth_and_size(
node_data,
max_bytes=MAX_JSON_INPUT_BYTES,
max_depth=MAX_JSON_INPUT_DEPTH,
)
normalized = node_data.copy()
if "tags" in normalized and normalized["tags"] not in ("", None):
tags_val = normalized["tags"]
if isinstance(tags_val, str) and tags_val.strip().startswith("{"):
tags_val = validate_json_depth_and_size(
tags_val,
max_bytes=MAX_JSON_INPUT_BYTES,
max_depth=MAX_JSON_INPUT_DEPTH,
)
normalized["tags"] = tags_val
validate_auth_budget_tags(tags_val)
if "fallback" in normalized and normalized["fallback"] not in (None, "", []):
fb_val = normalized["fallback"]
if isinstance(fb_val, str) and fb_val.strip().startswith(("[", "{")):
fb_val = validate_json_depth_and_size(
fb_val,
max_bytes=MAX_JSON_INPUT_BYTES,
max_depth=MAX_JSON_INPUT_DEPTH,
)
normalized["fallback"] = validate_fallback_entries(
fb_val,
max_entries=MAX_FALLBACK_ENTRIES,
)
pw = normalized.get("password")
if is_compose_expression(pw):
validate_compose_syntax(pw)
elif isinstance(pw, (list, tuple)):
for item in pw:
if is_compose_expression(item):
validate_compose_syntax(item)
return normalized
def add_node( def add_node(
self, unique_id: str, data: dict | None = None, is_folder: bool = False, **kwargs self, unique_id: str, data: dict | None = None, is_folder: bool = False, **kwargs
) -> dict: ) -> dict:
@@ -444,11 +508,14 @@ class ConnpyMCPServer:
node_data.update(kwargs) node_data.update(kwargs)
try: try:
node_data = self._validate_and_normalize_node_payload(node_data)
self.services.nodes.add_node(unique_id, node_data, is_folder=is_folder) self.services.nodes.add_node(unique_id, node_data, is_folder=is_folder)
return { return {
"success": True, "success": True,
"message": f"{'Folder' if is_folder else 'Node'} '{unique_id}' created successfully.", "message": f"{'Folder' if is_folder else 'Node'} '{unique_id}' created successfully.",
} }
except (ValueError, TypeError, SanitizedAuthError) as exc:
return {"success": False, "error": str(exc)}
except Exception as e: # noqa: BLE001 except Exception as e: # noqa: BLE001
return {"success": False, "error": str(e)} return {"success": False, "error": str(e)}
@@ -467,8 +534,11 @@ class ConnpyMCPServer:
node_updates.update(kwargs) node_updates.update(kwargs)
try: try:
node_updates = self._validate_and_normalize_node_payload(node_updates)
self.services.nodes.update_node(unique_id, node_updates) self.services.nodes.update_node(unique_id, node_updates)
return {"success": True, "message": f"Node '{unique_id}' updated successfully."} 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 except Exception as e: # noqa: BLE001
return {"success": False, "error": str(e)} return {"success": False, "error": str(e)}
+72 -14
View File
@@ -1,21 +1,45 @@
import re import re
from typing import List, Dict, Any from typing import Any
from ..auth_utils import (
MAX_CONTEXT_PATTERN_LEN,
MAX_CONTEXT_REGEXES,
compile_active_context_patterns,
is_default_all_context_cfg,
match_active_context_patterns,
)
from .base import BaseService from .base import BaseService
from ..hooks import MethodHook from .exceptions import InvalidConfigurationError
from .. import printer
class ContextService(BaseService): class ContextService(BaseService):
"""Business logic for managing and applying regex-based contexts locally.""" """Business logic for managing and applying regex-based contexts locally."""
@staticmethod
def _validate_regexes(regexes: Any) -> None:
if not isinstance(regexes, list) or not (1 <= len(regexes) <= MAX_CONTEXT_REGEXES):
raise ValueError(
f"Context regexes must be a list of 1 to {MAX_CONTEXT_REGEXES} patterns"
)
for pat in regexes:
if not isinstance(pat, str) or not (1 <= len(pat) <= MAX_CONTEXT_PATTERN_LEN):
raise ValueError(
f"Each context regex must be a non-empty string up to {MAX_CONTEXT_PATTERN_LEN} characters"
)
try:
re.compile(pat)
except re.error as exc:
raise ValueError(f"Invalid context regex {pat!r}: {exc}") from None
@property @property
def contexts(self) -> Dict[str, List[str]]: def contexts(self) -> dict[str, list[str]]:
return self.config.config.get("contexts", {"all": [".*"]}) return self.config.config.get("contexts", {"all": [".*"]})
@property @property
def current_context(self) -> str: def current_context(self) -> str:
return self.config.config.get("current_context", "all") return self.config.config.get("current_context", "all")
def list_contexts(self) -> List[Dict[str, Any]]: def list_contexts(self) -> list[dict[str, Any]]:
result = [] result = []
for name in self.contexts.keys(): for name in self.contexts.keys():
result.append({ result.append({
@@ -25,27 +49,29 @@ class ContextService(BaseService):
}) })
return result return result
def add_context(self, name: str, regexes: List[str]): def add_context(self, name: str, regexes: list[str]):
if not name.isalnum(): if not name.isalnum():
raise ValueError("Context name must be alphanumeric") raise ValueError("Context name must be alphanumeric")
self._validate_regexes(regexes)
ctxs = self.contexts ctxs = self.contexts
if name in ctxs: if name in ctxs:
raise ValueError(f"Context '{name}' already exists") raise ValueError(f"Context '{name}' already exists")
ctxs[name] = regexes ctxs[name] = list(regexes)
self.config.config["contexts"] = ctxs self.config.config["contexts"] = ctxs
self.config._saveconfig(self.config.file) self.config._saveconfig(self.config.file)
def update_context(self, name: str, regexes: List[str]): def update_context(self, name: str, regexes: list[str]):
if name == "all": if name == "all":
raise ValueError("Cannot modify default context 'all'") raise ValueError("Cannot modify default context 'all'")
self._validate_regexes(regexes)
ctxs = self.contexts ctxs = self.contexts
if name not in ctxs: if name not in ctxs:
raise ValueError(f"Context '{name}' does not exist") raise ValueError(f"Context '{name}' does not exist")
ctxs[name] = regexes ctxs[name] = list(regexes)
self.config.config["contexts"] = ctxs self.config.config["contexts"] = ctxs
self.config._saveconfig(self.config.file) self.config._saveconfig(self.config.file)
@@ -67,15 +93,47 @@ class ContextService(BaseService):
if name not in self.contexts: if name not in self.contexts:
raise ValueError(f"Context '{name}' does not exist") raise ValueError(f"Context '{name}' does not exist")
prev_context = self.current_context
self.config.config["current_context"] = name self.config.config["current_context"] = name
self.config._saveconfig(self.config.file) self.config._saveconfig(self.config.file)
can_refresh = (
self.config._is_context_filtering_enabled_unlocked()
if (
not hasattr(self.config, "assert_called")
and callable(getattr(self.config, "_is_context_filtering_enabled_unlocked", None))
)
else getattr(self.config, "_enforce_context", True)
)
if (
name == "all"
and prev_context != "all"
and can_refresh
and hasattr(self.config, "_generate_nodes_cache")
):
self.config._generate_nodes_cache()
def get_active_regexes(self) -> List[re.Pattern]: def get_active_regexes(self) -> list[re.Pattern]:
patterns = self.contexts.get(self.current_context, [".*"]) return compile_active_context_patterns(getattr(self.config, "config", None))
return [re.compile(p) for p in patterns]
def _match_any(self, node_name: str, patterns: List[re.Pattern]) -> bool: def _match_any(self, node_name: str, patterns: list[re.Pattern]) -> bool:
return any(p.match(node_name) for p in patterns) return match_active_context_patterns(node_name, patterns)
def is_default_all_context(self) -> bool:
return is_default_all_context_cfg(getattr(self.config, "config", None))
def matches_active_context(self, name: str) -> bool:
if self.is_default_all_context():
return isinstance(name, str) and bool(name)
if not isinstance(name, str) or not name:
return False
return match_active_context_patterns(name, self.get_active_regexes())
def validate_in_active_context(self, unique_id: str, is_folder: bool = False) -> None:
if not self.is_default_all_context() and not self.matches_active_context(unique_id):
label = "Folder" if is_folder else "Node"
raise InvalidConfigurationError(
f"{label} '{unique_id}' does not match active context '{self.current_context}'."
)
# Hook handlers for filtering # Hook handlers for filtering
def filter_node_list(self, *args, **kwargs): def filter_node_list(self, *args, **kwargs):
+308 -55
View File
@@ -1,29 +1,215 @@
from typing import List, Dict, Any, Callable, Optional import inspect
import os import os
from .base import BaseService import re
import sys
from collections.abc import Callable
from contextlib import suppress
from typing import Any
from connpy.auth_utils import (
MAX_REMOTE_FILTER_NODES,
AuthAttemptTracker,
raise_sanitized_auth_error,
)
from connpy.core import nodes as Nodes from connpy.core import nodes as Nodes
from .base import BaseService
from .exceptions import ConnpyError from .exceptions import ConnpyError
class ExecutionService(BaseService): class ExecutionService(BaseService):
"""Business logic for executing commands on nodes and running automation scripts.""" """Business logic for executing commands on nodes and running automation scripts."""
def __init__(
self,
config=None,
remote_stub=None,
attempt_tracker=None,
enforce_context: bool = True,
):
super().__init__(config)
self.remote_stub = remote_stub
self.attempt_tracker = (
attempt_tracker if attempt_tracker is not None else AuthAttemptTracker()
)
self.enforce_context = bool(enforce_context)
self.last_fallback_used: dict[str, bool] = {}
def _get_context_service(self):
if (
not getattr(self, "enforce_context", True)
or not getattr(self.config, "_enforce_context", True)
or (len(sys.argv) > 1 and sys.argv[1] == "api")
or not isinstance(getattr(self.config, "config", None), dict)
):
return None
from .context_service import ContextService
return ContextService(self.config)
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_channel(self):
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
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 channel, remote_host
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
from connpy.grpc_layer.stubs import ExecutionStub
channel, remote_host = self._get_remote_channel()
return ExecutionStub(channel, remote_host=remote_host)
def _filter_remote_nodes_filter(self, nodes_filter):
ctx_svc = self._get_context_service()
if ctx_svc is None or ctx_svc.is_default_all_context():
return nodes_filter
if isinstance(nodes_filter, (list, tuple)):
permitted = ctx_svc.filter_node_list(result=list(nodes_filter))
else:
if self.remote_stub is not None and callable(
getattr(self.remote_stub, "list_nodes", None)
):
candidates = self.remote_stub.list_nodes(filter_str=nodes_filter)
elif self.remote_stub is not None and callable(
getattr(getattr(self.remote_stub, "node_stub", None), "list_nodes", None)
):
candidates = self.remote_stub.node_stub.list_nodes(filter_str=nodes_filter)
elif self.remote_stub is None:
from connpy.grpc_layer.stubs import NodeStub
channel, remote_host = self._get_remote_channel()
node_stub = NodeStub(channel, remote_host=remote_host, config=self.config)
candidates = node_stub.list_nodes(filter_str=nodes_filter)
else:
candidates = (
self.config._getallnodes(nodes_filter)
if hasattr(self.config, "_getallnodes")
else []
)
if nodes_filter:
cfg_dict = getattr(self.config, "config", None)
case = cfg_dict.get("case", False) if isinstance(cfg_dict, dict) else False
flags = 0 if case else re.IGNORECASE
candidates = [c for c in candidates if re.search(nodes_filter, c, flags)]
permitted = ctx_svc.filter_node_list(result=candidates)
if not permitted:
raise ConnpyError(f"No nodes found matching filter: {nodes_filter}")
return [f"^{re.escape(n)}$" for n in permitted]
def _build_remote_filter_batches(self, nodes_filter):
filtered = self._filter_remote_nodes_filter(nodes_filter)
if isinstance(filtered, list):
return [
filtered[i : i + MAX_REMOTE_FILTER_NODES]
for i in range(0, len(filtered), MAX_REMOTE_FILTER_NODES)
]
return [filtered]
@staticmethod
def _aggregate_remote_results(batch_results):
if len(batch_results) == 1:
return batch_results[0]
if all(isinstance(r, dict) for r in batch_results):
merged: dict[str, Any] = {}
for r in batch_results:
merged.update(r)
return merged
if all(isinstance(r, list) for r in batch_results):
merged_list: list[Any] = []
for r in batch_results:
merged_list.extend(r)
return merged_list
return batch_results[-1]
def run_commands( def run_commands(
self, self,
nodes_filter: str, nodes_filter: str,
commands: List[str], commands: list[str],
variables: Optional[Dict[str, Any]] = None, variables: dict[str, Any] | None = None,
parallel: int = 10, parallel: int = 10,
timeout: int = 20, timeout: int | None = None,
folder: Optional[str] = None, folder: str | None = None,
prompt: Optional[str] = None, prompt: str | None = None,
on_node_complete: Optional[Callable] = None, on_node_complete: Callable | None = None,
logger: Optional[Callable] = None, logger: Callable | None = None,
name: Optional[str] = None name: str | None = None,
) -> Dict[str, str]: connect_timeout: int | None = None,
auth_timeout: int | None = None,
) -> dict[str, Any]:
"""Execute commands on a set of nodes.""" """Execute commands on a set of nodes."""
try: try:
self._ensure_backend_supports_stage_timeouts(
connect_timeout=connect_timeout,
auth_timeout=auth_timeout,
)
if self._is_remote_backend():
batches = self._build_remote_filter_batches(nodes_filter)
stub = self._get_remote_stub()
batch_results = [
stub.run_commands(
nodes_filter=batch,
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 "",
)
for batch in batches
]
return self._aggregate_remote_results(batch_results)
matched_names = self.config._getallnodes(nodes_filter) matched_names = self.config._getallnodes(nodes_filter)
ctx_svc = self._get_context_service()
if ctx_svc is not None and not ctx_svc.is_default_all_context():
matched_names = ctx_svc.filter_node_list(result=matched_names)
if not matched_names: if not matched_names:
raise ConnpyError(f"No nodes found matching filter: {nodes_filter}") raise ConnpyError(f"No nodes found matching filter: {nodes_filter}")
@@ -31,47 +217,96 @@ class ExecutionService(BaseService):
executor = Nodes(node_data, config=self.config) executor = Nodes(node_data, config=self.config)
self.last_executor = executor self.last_executor = executor
results = executor.run( run_kwargs: dict[str, Any] = {
commands=commands, "commands": commands,
vars=variables, "vars": variables,
parallel=parallel, "parallel": parallel,
timeout=timeout, "timeout": timeout,
folder=folder, "folder": folder,
prompt=prompt, "prompt": prompt,
on_complete=on_node_complete, "on_complete": on_node_complete,
logger=logger "logger": logger,
) "connect_timeout": connect_timeout,
"auth_timeout": auth_timeout,
"_default_cmd_timeout": 20,
}
run_params = inspect.signature(executor.run).parameters
if "unattended" in run_params:
run_kwargs["unattended"] = True
if "attempt_tracker" in run_params:
run_kwargs["attempt_tracker"] = self.attempt_tracker
# Combine output and status for the caller results = executor.run(**run_kwargs)
fb_map = getattr(executor, "fallback_used", {})
fb_dict = fb_map if isinstance(fb_map, dict) else {}
# Combine output, status, and fallback metadata for the caller
full_results = {} full_results = {}
self.last_fallback_used = {}
for unique in results: for unique in results:
fb_used = bool(fb_dict.get(unique, False))
full_results[unique] = { full_results[unique] = {
"output": 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 return full_results
except Exception as e: except ConnpyError:
raise ConnpyError(f"Execution failed: {e}") raise
except Exception as e: # noqa: BLE001
raise_sanitized_auth_error(e, code="AUTH_FAILED", context_label="execution failed")
def test_commands( def test_commands(
self, self,
nodes_filter: str, nodes_filter: str,
commands: List[str], commands: list[str],
expected: List[str], expected: list[str],
variables: Optional[Dict[str, Any]] = None, variables: dict[str, Any] | None = None,
parallel: int = 10, parallel: int = 10,
timeout: int = 20, timeout: int | None = None,
folder: Optional[str] = None, folder: str | None = None,
prompt: Optional[str] = None, prompt: str | None = None,
on_node_complete: Optional[Callable] = None, on_node_complete: Callable | None = None,
logger: Optional[Callable] = None, logger: Callable | None = None,
name: Optional[str] = None name: str | None = None,
) -> Dict[str, Dict[str, bool]]: 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.""" """Run commands and verify expected output on a set of nodes."""
try: try:
self._ensure_backend_supports_stage_timeouts(
connect_timeout=connect_timeout,
auth_timeout=auth_timeout,
)
if self._is_remote_backend():
batches = self._build_remote_filter_batches(nodes_filter)
stub = self._get_remote_stub()
batch_results = [
stub.test_commands(
nodes_filter=batch,
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 "",
)
for batch in batches
]
return self._aggregate_remote_results(batch_results)
matched_names = self.config._getallnodes(nodes_filter) matched_names = self.config._getallnodes(nodes_filter)
ctx_svc = self._get_context_service()
if ctx_svc is not None and not ctx_svc.is_default_all_context():
matched_names = ctx_svc.filter_node_list(result=matched_names)
if not matched_names: if not matched_names:
raise ConnpyError(f"No nodes found matching filter: {nodes_filter}") raise ConnpyError(f"No nodes found matching filter: {nodes_filter}")
@@ -79,31 +314,49 @@ class ExecutionService(BaseService):
executor = Nodes(node_data, config=self.config) executor = Nodes(node_data, config=self.config)
self.last_executor = executor self.last_executor = executor
results = executor.test( test_kwargs: dict[str, Any] = {
commands=commands, "commands": commands,
expected=expected, "expected": expected,
vars=variables, "vars": variables,
parallel=parallel, "parallel": parallel,
timeout=timeout, "timeout": timeout,
folder=folder, "folder": folder,
prompt=prompt, "prompt": prompt,
on_complete=on_node_complete, "on_complete": on_node_complete,
logger=logger "logger": logger,
) "connect_timeout": connect_timeout,
return results "auth_timeout": auth_timeout,
except Exception as e: "_default_cmd_timeout": 20,
raise ConnpyError(f"Testing failed: {e}") }
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
def run_cli_script(self, nodes_filter: str, script_path: str, parallel: int = 10) -> Dict[str, str]: 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.""" """Run a plain-text script containing one command per line."""
if not os.path.exists(script_path): if not os.path.exists(script_path):
raise ConnpyError(f"Script file not found: {script_path}") raise ConnpyError(f"Script file not found: {script_path}")
try: try:
with open(script_path, "r") as f: with open(script_path) as f:
commands = [line.strip() for line in f if line.strip()] commands = [line.strip() for line in f if line.strip()]
except Exception as e: except OSError:
raise ConnpyError(f"Failed to read script {script_path}: {e}") raise ConnpyError("Failed to read script file") from None
return self.run_commands(nodes_filter, commands, parallel=parallel) return self.run_commands(nodes_filter, commands, parallel=parallel)
+146 -26
View File
@@ -1,14 +1,56 @@
from .base import BaseService
import yaml
import os import os
import sys
from copy import deepcopy from copy import deepcopy
from .exceptions import InvalidConfigurationError, NodeNotFoundError, ReservedNameError
import yaml
from ..configfile import NoAliasDumper from ..configfile import NoAliasDumper
from .base import BaseService
from .exceptions import InvalidConfigurationError, NodeNotFoundError
from .node_service import NodeService, _supports_transactional_update
class ImportExportService(BaseService): class ImportExportService(BaseService):
"""Business logic for YAML/JSON inventory import and export.""" """Business logic for YAML/JSON inventory import and export."""
def __init__(self, config=None, enforce_context: bool = True):
super().__init__(config)
self.enforce_context = bool(enforce_context)
def _get_context_service(self, cfg=None):
target_cfg = cfg or self.config
if (
not getattr(self, "enforce_context", True)
or not getattr(target_cfg, "_enforce_context", True)
or (len(sys.argv) > 1 and sys.argv[1] == "api")
or not isinstance(getattr(target_cfg, "config", None), dict)
):
return None
from .context_service import ContextService
return ContextService(target_cfg)
def _get_raw_folders(self, cfg=None) -> list[str]:
target_cfg = cfg or self.config
if target_cfg is None:
return []
if (
not hasattr(target_cfg, "assert_called")
and callable(getattr(target_cfg, "_getallfolders_unlocked", None))
and hasattr(target_cfg, "_mutation_lock")
):
with target_cfg._mutation_lock:
return target_cfg._getallfolders_unlocked()
return target_cfg._getallfolders()
def _folder_exists(self, folder_id: str, raw_folders: list[str], cfg=None) -> bool:
target_cfg = cfg or self.config
cfg_dict = getattr(target_cfg, "config", None)
case = cfg_dict.get("case", False) if isinstance(cfg_dict, dict) else False
if case:
return folder_id in raw_folders
return folder_id.lower() in {f.lower() for f in raw_folders}
def export_to_file(self, file_path, folders=None): def export_to_file(self, file_path, folders=None):
"""Export nodes/folders to a YAML file.""" """Export nodes/folders to a YAML file."""
if os.path.exists(file_path): if os.path.exists(file_path):
@@ -23,16 +65,42 @@ class ImportExportService(BaseService):
def export_to_dict(self, folders=None): def export_to_dict(self, folders=None):
"""Export nodes/folders to a dictionary.""" """Export nodes/folders to a dictionary."""
if not folders: ctx_svc = self._get_context_service()
return deepcopy(self.config.connections) ctx_active = ctx_svc is not None and not ctx_svc.is_default_all_context()
else:
# Validate folders exist
for f in folders:
if f != "@" and f not in self.config._getallfolders():
raise NodeNotFoundError(f"Folder '{f}' not found.")
if not folders and not ctx_active:
return deepcopy(self.config.connections)
raw_folders = self._get_raw_folders()
if folders:
for f in folders:
if f != "@" and (
not self._folder_exists(f, raw_folders)
or (ctx_active and not ctx_svc.matches_active_context(f))
):
raise NodeNotFoundError(f"Folder '{f}' not found.")
flat = self.config._getallnodesfull(folders, extract=False) flat = self.config._getallnodesfull(folders, extract=False)
allowed_folders = []
else:
allowed_folders = ctx_svc.filter_node_list(result=raw_folders)
flat = self.config._getallnodesfull(extract=False)
if ctx_active:
flat = ctx_svc.filter_node_dict(result=flat)
nested = {} nested = {}
for folder_id in allowed_folders:
uniques = self.config._explode_unique(folder_id)
if not uniques or "folder" not in uniques:
continue
f_name = uniques["folder"]
if f_name not in nested:
nested[f_name] = {"type": "folder"}
if "subfolder" in uniques:
s_name = uniques["subfolder"]
if s_name not in nested[f_name]:
nested[f_name][s_name] = {"type": "subfolder"}
for k, v in flat.items(): for k, v in flat.items():
uniques = self.config._explode_unique(k) uniques = self.config._explode_unique(k)
if not uniques: if not uniques:
@@ -77,22 +145,52 @@ class ImportExportService(BaseService):
raise InvalidConfigurationError(f"Failed to read/parse import file: {e}") raise InvalidConfigurationError(f"Failed to read/parse import file: {e}")
def import_from_dict(self, data): def import_from_dict(self, data):
"""Import nodes/folders from a dictionary.""" """Import nodes/folders from a dictionary atomically with context pre-validation."""
if not isinstance(data, dict): if not isinstance(data, dict):
raise InvalidConfigurationError("Invalid import data format: expected a dictionary of nodes.") raise InvalidConfigurationError(
"Invalid import data format: expected a dictionary of nodes."
)
def _traverse_import(node_data, current_folder='', current_subfolder=''): ctx_svc = self._get_context_service()
ctx_active = ctx_svc is not None and not ctx_svc.is_default_all_context()
raw_folders = self._get_raw_folders() if ctx_active else []
planned_ops = []
def _plan_import(node_data, current_folder="", current_subfolder=""):
for k, v in node_data.items(): for k, v in node_data.items():
if k == "type": if k == "type" or not isinstance(v, dict):
continue continue
if isinstance(v, dict):
node_type = v.get("type", "connection") node_type = v.get("type", "connection")
if node_type == "folder": if node_type == "folder":
self.config._folder_add(folder=k) folder_id = f"@{k}"
_traverse_import(v, current_folder=k, current_subfolder='') has_children = any(
isinstance(cv, dict) for ck, cv in v.items() if ck != "type"
)
if ctx_active and (
not has_children
or (
not ctx_svc.matches_active_context(folder_id)
and not self._folder_exists(folder_id, raw_folders)
)
):
ctx_svc.validate_in_active_context(folder_id, is_folder=True)
planned_ops.append(("folder", k, ""))
_plan_import(v, current_folder=k, current_subfolder="")
elif node_type == "subfolder": elif node_type == "subfolder":
self.config._folder_add(folder=current_folder, subfolder=k) folder_id = f"@{k}@{current_folder}"
_traverse_import(v, current_folder=current_folder, current_subfolder=k) has_children = any(
isinstance(cv, dict) for ck, cv in v.items() if ck != "type"
)
if ctx_active and (
not has_children
or (
not ctx_svc.matches_active_context(folder_id)
and not self._folder_exists(folder_id, raw_folders)
)
):
ctx_svc.validate_in_active_context(folder_id, is_folder=True)
planned_ops.append(("folder", current_folder, k))
_plan_import(v, current_folder=current_folder, current_subfolder=k)
elif node_type == "connection": elif node_type == "connection":
unique_id = k unique_id = k
if current_subfolder: if current_subfolder:
@@ -100,16 +198,38 @@ class ImportExportService(BaseService):
elif current_folder: elif current_folder:
unique_id = f"{k}@{current_folder}" unique_id = f"{k}@{current_folder}"
self._validate_node_name(unique_id) self._validate_node_name(unique_id)
NodeService._validate_node_payload(v)
if ctx_svc is not None:
ctx_svc.validate_in_active_context(unique_id, is_folder=False)
kwargs = deepcopy(v) kwargs = deepcopy(v)
kwargs['id'] = k kwargs["id"] = k
kwargs['folder'] = current_folder kwargs["folder"] = current_folder
kwargs['subfolder'] = current_subfolder kwargs["subfolder"] = current_subfolder
planned_ops.append(("connection", kwargs))
self.config._connections_add(**kwargs) _plan_import(data)
if _supports_transactional_update(self.config):
def _mut(cfg):
for op in planned_ops:
if op[0] == "folder":
cfg._folder_add_unlocked(folder=op[1], subfolder=op[2])
else: else:
# Invalid format skip cfg._connections_add_unlocked(**op[1])
pass
_traverse_import(data) self.config.transactional_update(_mut)
return
snap_connections = deepcopy(self.config.connections)
try:
for op in planned_ops:
if op[0] == "folder":
self.config._folder_add(folder=op[1], subfolder=op[2])
else:
self.config._connections_add(**op[1])
self.config._saveconfig(self.config.file) self.config._saveconfig(self.config.file)
except BaseException:
self.config.connections.clear()
self.config.connections.update(snap_connections)
raise
+384 -33
View File
@@ -1,13 +1,169 @@
import inspect
import re import re
import sys
from copy import deepcopy
from typing import ClassVar
from .base import BaseService from .base import BaseService
from .exceptions import ( from .exceptions import (
NodeNotFoundError, NodeAlreadyExistsError, InvalidConfigurationError,
InvalidConfigurationError, ReservedNameError NodeAlreadyExistsError,
NodeNotFoundError,
ReservedNameError,
) )
def _supports_transactional_update(cfg):
return (
cfg is not None
and not hasattr(cfg, "assert_called")
and callable(getattr(cfg, "transactional_update", None))
)
class NodeService(BaseService): class NodeService(BaseService):
def __init__(self, config=None): _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, enforce_context: bool = True):
super().__init__(config) super().__init__(config)
self.enforce_context = bool(enforce_context)
def _get_context_service(self, cfg=None):
target_cfg = cfg or self.config
if (
not getattr(self, "enforce_context", True)
or not getattr(target_cfg, "_enforce_context", True)
or (len(sys.argv) > 1 and sys.argv[1] == "api")
or not isinstance(getattr(target_cfg, "config", None), dict)
):
return None
from .context_service import ContextService
return ContextService(target_cfg)
def _get_raw_folders(self, cfg=None) -> list[str]:
target_cfg = cfg or self.config
if target_cfg is None:
return []
if (
not hasattr(target_cfg, "assert_called")
and callable(getattr(target_cfg, "_getallfolders_unlocked", None))
and hasattr(target_cfg, "_mutation_lock")
):
with target_cfg._mutation_lock:
return target_cfg._getallfolders_unlocked()
return target_cfg._getallfolders()
def _get_raw_nodes(self, cfg=None) -> list[str]:
target_cfg = cfg or self.config
if target_cfg is None:
return []
if (
not hasattr(target_cfg, "assert_called")
and callable(getattr(target_cfg, "_getallnodes_unlocked", None))
and hasattr(target_cfg, "_mutation_lock")
):
with target_cfg._mutation_lock:
return target_cfg._getallnodes_unlocked()
return target_cfg._getallnodes()
def _derive_parent_folder(self, cfg, unique_id: str, is_folder: bool) -> str | None:
explode_fn = (
cfg._explode_unique_unlocked
if (
not hasattr(cfg, "assert_called")
and callable(getattr(cfg, "_explode_unique_unlocked", None))
)
else cfg._explode_unique
)
uniques = explode_fn(unique_id)
if is_folder:
if (
not isinstance(unique_id, str)
or not unique_id.startswith("@")
or not isinstance(uniques, dict)
or "folder" not in uniques
):
raise InvalidConfigurationError(f"Invalid folder name '{unique_id}'.")
if "subfolder" in uniques:
return f"@{uniques['folder']}"
return None
if (
not isinstance(unique_id, str)
or unique_id.startswith("@")
or not isinstance(uniques, dict)
or not uniques.get("id")
):
raise InvalidConfigurationError(f"Invalid node name '{unique_id}'.")
if "subfolder" in uniques and "folder" in uniques:
return f"@{uniques['subfolder']}@{uniques['folder']}"
if "folder" in uniques:
return f"@{uniques['folder']}"
return None
def _folder_exists(self, parent_folder: str, raw_folders: list[str], cfg=None) -> bool:
target_cfg = cfg or self.config
cfg_dict = getattr(target_cfg, "config", None)
case = cfg_dict.get("case", False) if isinstance(cfg_dict, dict) else False
if case:
return parent_folder in raw_folders
return parent_folder.lower() in {f.lower() for f in raw_folders}
def _ensure_in_active_context(
self, unique_id: str, *, is_folder: bool = False, not_found_msg: str | None = None
) -> None:
ctx_svc = self._get_context_service()
if (
ctx_svc is not None
and not ctx_svc.is_default_all_context()
and not ctx_svc.matches_active_context(unique_id)
):
if not_found_msg is not None:
raise NodeNotFoundError(not_found_msg)
label = "Folder" if is_folder else "Node"
raise NodeNotFoundError(f"{label} '{unique_id}' not found.")
@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): def list_nodes(self, filter_str=None, format_str=None):
@@ -19,6 +175,10 @@ class NodeService(BaseService):
flags = re.IGNORECASE if not case_sensitive else 0 flags = re.IGNORECASE if not case_sensitive else 0
nodes = [n for n in nodes if re.search(filter_str, n, flags)] nodes = [n for n in nodes if re.search(filter_str, n, flags)]
ctx_svc = self._get_context_service()
if ctx_svc is not None and not ctx_svc.is_default_all_context():
nodes = ctx_svc.filter_node_list(result=nodes)
if not format_str: if not format_str:
return nodes return nodes
@@ -75,17 +235,21 @@ class NodeService(BaseService):
else: else:
flags = re.IGNORECASE if not case_sensitive else 0 flags = re.IGNORECASE if not case_sensitive else 0
folders = [f for f in folders if re.search(filter_str, f, flags)] folders = [f for f in folders if re.search(filter_str, f, flags)]
ctx_svc = self._get_context_service()
if ctx_svc is not None and not ctx_svc.is_default_all_context():
folders = ctx_svc.filter_node_list(result=folders)
return folders return folders
def get_node_details(self, unique_id): def get_node_details(self, unique_id):
"""Return full configuration dictionary for a specific node.""" """Return full configuration dictionary for a specific node."""
self._ensure_in_active_context(unique_id, is_folder=False)
try: try:
details = self.config.getitem(unique_id) details = self.config.getitem(unique_id)
if not details: if not details:
raise NodeNotFoundError(f"Node '{unique_id}' not found.") raise NodeNotFoundError(f"Node '{unique_id}' not found.")
return details return details
except (KeyError, TypeError): 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): def explode_unique(self, unique_id):
"""Explode a unique ID into a dictionary of its parts.""" """Explode a unique ID into a dictionary of its parts."""
@@ -96,25 +260,54 @@ class NodeService(BaseService):
self.config._generate_nodes_cache(nodes=nodes, folders=folders, profiles=profiles) self.config._generate_nodes_cache(nodes=nodes, folders=folders, profiles=profiles)
def validate_parent_folder(self, unique_id, is_folder=False): def validate_parent_folder(self, unique_id, is_folder=False):
"""Check if parent folder exists for a given node unique ID.""" """Check if unique_id is in active context and its parent folder exists."""
if is_folder: parent_folder = self._derive_parent_folder(self.config, unique_id, is_folder=is_folder)
uniques = self.config._explode_unique(unique_id) ctx_svc = self._get_context_service()
if uniques and "subfolder" in uniques and "folder" in uniques: if ctx_svc is not None:
parent_folder = f"@{uniques['folder']}" ctx_svc.validate_in_active_context(unique_id, is_folder=is_folder)
if parent_folder not in self.config._getallfolders(): if parent_folder is not None and not self._folder_exists(
parent_folder, self._get_raw_folders(), self.config
):
raise NodeNotFoundError(f"Folder '{parent_folder}' not found.") raise NodeNotFoundError(f"Folder '{parent_folder}' not found.")
else:
node_folder = unique_id.partition("@")[2]
if node_folder:
parent_folder = f"@{node_folder}"
if parent_folder not in self.config._getallfolders():
raise NodeNotFoundError(f"Folder '{parent_folder}' not found.")
def add_node(self, unique_id, data, is_folder=False): def add_node(self, unique_id, data, is_folder=False):
"""Logic for adding a new node or folder to configuration.""" """Logic for adding a new node or folder to configuration."""
if not is_folder: if not is_folder:
self._validate_node_name(unique_id) self._validate_node_name(unique_id)
self._validate_node_payload(data)
self.validate_parent_folder(unique_id, is_folder=is_folder)
if _supports_transactional_update(self.config):
def _mut(cfg):
all_nodes = cfg._getallnodes_unlocked()
all_folders = cfg._getallfolders_unlocked()
parent_folder = self._derive_parent_folder(cfg, unique_id, is_folder=is_folder)
ctx_svc = self._get_context_service(cfg)
if ctx_svc is not None:
ctx_svc.validate_in_active_context(unique_id, is_folder=is_folder)
if parent_folder is not None and not self._folder_exists(
parent_folder, all_folders, cfg
):
raise NodeNotFoundError(f"Folder '{parent_folder}' not found.")
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}'.")
cfg._folder_add_unlocked(**uniques)
else:
if unique_id in all_nodes:
raise NodeAlreadyExistsError(f"Node '{unique_id}' already exists.")
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_nodes = self.config._getallnodes()
all_folders = self.config._getallfolders() all_folders = self.config._getallfolders()
@@ -126,7 +319,6 @@ class NodeService(BaseService):
if not uniques: if not uniques:
raise InvalidConfigurationError(f"Invalid folder name '{unique_id}'.") raise InvalidConfigurationError(f"Invalid folder name '{unique_id}'.")
# Check if parent folder exists when creating a subfolder
if "subfolder" in uniques: if "subfolder" in uniques:
self.validate_parent_folder(unique_id, is_folder=True) self.validate_parent_folder(unique_id, is_folder=True)
@@ -136,37 +328,75 @@ class NodeService(BaseService):
if unique_id in all_nodes: if unique_id in all_nodes:
raise NodeAlreadyExistsError(f"Node '{unique_id}' already exists.") 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) self.validate_parent_folder(unique_id)
# Ensure 'id' is in data for config._connections_add payload = deepcopy(data)
if "id" not in data: if "id" not in payload:
uniques = self.config._explode_unique(unique_id) uniques = self.config._explode_unique(unique_id)
if uniques and "id" in uniques: if uniques and "id" in uniques:
data["id"] = uniques["id"] payload["id"] = uniques["id"]
self.config._connections_add(**data) self.config._connections_add(**payload)
self.config._saveconfig(self.config.file) 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): def update_node(self, unique_id, data, save=True):
"""Explicitly update an existing node.""" """Explicitly update an existing node."""
self._ensure_in_active_context(unique_id, is_folder=False)
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() all_nodes = self.config._getallnodes()
if unique_id not in all_nodes: if unique_id not in all_nodes:
raise NodeNotFoundError(f"Node '{unique_id}' not found.") 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) uniques = self.config._explode_unique(unique_id)
if uniques: existing = self.config.getitem(unique_id, extract=False)
data["id"] = uniques["id"] payload = self._build_update_payload(unique_id, data, existing, uniques)
self.config._connections_add(**payload)
# config._connections_add actually handles updates if ID exists correctly
self.config._connections_add(**data)
if save: if save:
self.config._saveconfig(self.config.file) self.config._saveconfig(self.config.file)
def delete_node(self, unique_id, is_folder=False, save=True): def delete_node(self, unique_id, is_folder=False, save=True):
"""Logic for deleting a node or folder.""" """Logic for deleting a node or folder."""
self._ensure_in_active_context(unique_id, is_folder=is_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: if is_folder:
uniques = self.config._explode_unique(unique_id) uniques = self.config._explode_unique(unique_id)
if not uniques: if not uniques:
@@ -183,17 +413,35 @@ class NodeService(BaseService):
def connect_node(self, unique_id, sftp=False, debug=False, logger=None): def connect_node(self, unique_id, sftp=False, debug=False, logger=None):
"""Interact with a node directly.""" """Interact with a node directly."""
self._ensure_in_active_context(unique_id, is_folder=False)
from connpy.core import node from connpy.core import node
from ..auth_utils import resolve_fallback_entries
from .profile_service import ProfileService from .profile_service import ProfileService
node_data = self.config.getitem(unique_id, extract=False) node_data = self.config.getitem(unique_id, extract=False)
if not node_data: if not node_data:
raise NodeNotFoundError(f"Node '{unique_id}' not found.") raise NodeNotFoundError(f"Node '{unique_id}' not found.")
# Resolve profiles
profile_service = ProfileService(self.config) profile_service = ProfileService(self.config)
resolved_data = profile_service.resolve_node_data(node_data) 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) n = node(unique_id, **resolved_data, config=self.config)
if sftp: if sftp:
n.protocol = "sftp" n.protocol = "sftp"
@@ -202,7 +450,48 @@ class NodeService(BaseService):
def move_node(self, src_id, dst_id, copy=False): def move_node(self, src_id, dst_id, copy=False):
"""Move or copy a node.""" """Move or copy a node."""
self._ensure_in_active_context(
src_id, is_folder=False, not_found_msg=f"Source node '{src_id}' not found."
)
self._validate_node_name(dst_id) self._validate_node_name(dst_id)
self.validate_parent_folder(dst_id, is_folder=False)
if _supports_transactional_update(self.config):
def _mut(cfg):
ctx_svc = self._get_context_service(cfg)
if (
ctx_svc is not None
and not ctx_svc.is_default_all_context()
and not ctx_svc.matches_active_context(src_id)
):
raise NodeNotFoundError(f"Source node '{src_id}' not found.")
parent_folder = self._derive_parent_folder(cfg, dst_id, is_folder=False)
if ctx_svc is not None:
ctx_svc.validate_in_active_context(dst_id, is_folder=False)
if parent_folder is not None and not self._folder_exists(
parent_folder, cfg._getallfolders_unlocked(), cfg
):
raise NodeNotFoundError(f"Folder '{parent_folder}' not found.")
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) node_data = self.config.getitem(src_id)
if not node_data: if not node_data:
@@ -228,6 +517,55 @@ class NodeService(BaseService):
def bulk_add(self, ids, hosts, common_data): def bulk_add(self, ids, hosts, common_data):
"""Add multiple nodes with shared common configuration.""" """Add multiple nodes with shared common configuration."""
self._validate_node_payload(common_data)
for uid in ids:
self.validate_parent_folder(uid, is_folder=False)
if _supports_transactional_update(self.config):
def _build_entries(cfg):
all_nodes = cfg._getallnodes_unlocked()
all_folders = cfg._getallfolders_unlocked()
ctx_svc = self._get_context_service(cfg)
planned = []
for i, uid in enumerate(ids):
parent_folder = self._derive_parent_folder(cfg, uid, is_folder=False)
if ctx_svc is not None:
ctx_svc.validate_in_active_context(uid, is_folder=False)
if parent_folder is not None and not self._folder_exists(
parent_folder, all_folders, cfg
):
raise NodeNotFoundError(f"Folder '{parent_folder}' not found.")
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 count = 0
all_nodes = self.config._getallnodes() all_nodes = self.config._getallnodes()
@@ -238,8 +576,6 @@ class NodeService(BaseService):
try: try:
self._validate_node_name(uid) self._validate_node_name(uid)
except ReservedNameError: except ReservedNameError:
# For bulk, we might want to just skip or log.
# CLI caller will handle if it wants to be strict.
continue continue
host = hosts[i] if i < len(hosts) else hosts[0] host = hosts[i] if i < len(hosts) else hosts[0]
@@ -263,6 +599,21 @@ class NodeService(BaseService):
def full_replace(self, connections, profiles): def full_replace(self, connections, profiles):
"""Replace all connections and profiles with new data.""" """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.connections = connections
self.config.profiles = profiles self.config.profiles = profiles
self.config._saveconfig(self.config.file) self.config._saveconfig(self.config.file)
+224 -57
View File
@@ -1,64 +1,211 @@
from contextlib import suppress
from copy import deepcopy
from typing import ClassVar
from .base import BaseService from .base import BaseService
from .exceptions import ProfileNotFoundError, ProfileAlreadyExistsError, InvalidConfigurationError from .exceptions import (
InvalidConfigurationError,
ProfileAlreadyExistsError,
ProfileNotFoundError,
)
MAX_PROFILE_DEPTH = 5
def _supports_transactional_update(cfg):
return (
cfg is not None
and not hasattr(cfg, "assert_called")
and callable(getattr(cfg, "transactional_update", None))
)
class ProfileService(BaseService): class ProfileService(BaseService):
"""Business logic for node profiles management.""" """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): def list_profiles(self, filter_str=None):
"""List all profile names, optionally filtered.""" """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) case_sensitive = self.config.config.get("case", False)
if filter_str: if filter_str:
if not case_sensitive: if not case_sensitive:
f_str = filter_str.lower() f_str = filter_str.lower()
return [p for p in profiles if f_str in p.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 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.""" """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: if not profile:
raise ProfileNotFoundError(f"Profile '{name}' not found.") raise ProfileNotFoundError(f"Profile '{name}' not found.")
if resolve: 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 return profile
def add_profile(self, name, data): def add_profile(self, name, data):
"""Add a new profile.""" """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: if name in self.config.profiles:
raise ProfileAlreadyExistsError(f"Profile '{name}' already exists.") 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._profiles_add(id=name, **filtered_data)
self.config._saveconfig(self.config.file) self.config._saveconfig(self.config.file)
def resolve_node_data(self, node_data): def resolve_node_data(
"""Resolve profile references (@profile) in node data and handle inheritance.""" self,
resolved = node_data.copy() 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
# 1. Identify all referenced profiles to support inheritance if _depth > MAX_PROFILE_DEPTH:
referenced_profiles = [] raise InvalidConfigurationError("Profile inheritance exceeds maximum depth.")
for value in resolved.values():
if isinstance(value, str) and value.startswith("@"): profiles = (
referenced_profiles.append(value[1:]) _profiles_snapshot
elif isinstance(value, list): if _profiles_snapshot is not None
else self._get_profiles_snapshot()
)
resolved = deepcopy(node_data)
auth_referenced_profiles = []
for key in ("user", "password"):
value = resolved.get(key)
if isinstance(value, str):
if not is_compose_expression(value) and value.startswith("@"):
ref = value[1:]
if key != "password" or ref in profiles:
auth_referenced_profiles.append(ref)
elif isinstance(value, (list, tuple)):
for item in value: for item in value:
if isinstance(item, str) and item.startswith("@"): if (
referenced_profiles.append(item[1:]) isinstance(item, str)
and not is_compose_expression(item)
and item.startswith("@")
):
ref = item[1:]
if key != "password" or ref in profiles:
auth_referenced_profiles.append(ref)
def _get_resolved_ref(prof_name):
if prof_name in _visited or _depth >= MAX_PROFILE_DEPTH:
raise InvalidConfigurationError(
f"Circular or overly deep profile reference involving '{prof_name}'."
)
prof_raw = profiles.get(prof_name)
if not prof_raw:
raise ProfileNotFoundError(f"Profile '{prof_name}' not found.")
return self.resolve_node_data(
prof_raw,
_visited=_visited | {prof_name},
_depth=_depth + 1,
_profiles_snapshot=profiles,
)
for key, value in list(resolved.items()):
if key == "fallback":
continue
if key == "password":
if isinstance(value, str):
if is_compose_expression(value):
continue
if value.startswith("@") and value[1:] in profiles:
prof_obj = _get_resolved_ref(value[1:])
resolved["password"] = prof_obj.get("password", "")
elif isinstance(value, list):
resolved_list = []
for item in value:
if (
isinstance(item, str)
and not is_compose_expression(item)
and item.startswith("@")
and item[1:] in profiles
):
with suppress(ProfileNotFoundError):
prof_obj = _get_resolved_ref(item[1:])
if "password" in prof_obj:
resolved_list.append(prof_obj["password"])
else:
resolved_list.append(item)
resolved["password"] = resolved_list
continue
# 2. Resolve explicit references
for key, value in resolved.items():
if isinstance(value, str) and value.startswith("@"): if isinstance(value, str) and value.startswith("@"):
profile_name = value[1:] profile_name = value[1:]
try: try:
profile = self.get_profile(profile_name, resolve=True) profile = _get_resolved_ref(profile_name)
resolved[key] = profile.get(key, "") resolved[key] = profile.get(key, "")
except ProfileNotFoundError: except ProfileNotFoundError:
resolved[key] = "" resolved[key] = ""
@@ -67,33 +214,29 @@ class ProfileService(BaseService):
for item in value: for item in value:
if isinstance(item, str) and item.startswith("@"): if isinstance(item, str) and item.startswith("@"):
profile_name = item[1:] profile_name = item[1:]
try: with suppress(ProfileNotFoundError):
profile = self.get_profile(profile_name, resolve=True) profile = _get_resolved_ref(profile_name)
if "password" in profile: if "password" in profile:
resolved_list.append(profile["password"]) resolved_list.append(profile["password"])
except ProfileNotFoundError:
pass
else: else:
resolved_list.append(item) resolved_list.append(item)
resolved[key] = resolved_list resolved[key] = resolved_list
# 3. Inheritance: Fill empty keys from the first referenced profile dedup_auth_profiles = list(dict.fromkeys(auth_referenced_profiles))
if referenced_profiles: for base_profile_name in dedup_auth_profiles:
base_profile_name = referenced_profiles[0] with suppress(ProfileNotFoundError):
try: base_profile = _get_resolved_ref(base_profile_name)
base_profile = self.get_profile(base_profile_name, resolve=True) for key in ("user", "password", "fallback"):
for key, value in base_profile.items(): val = base_profile.get(key)
# Fill if key is missing or empty if (
if key not in resolved or resolved[key] == "" or resolved[key] == [] or resolved[key] is None: key not in resolved or resolved[key] in ("", [], None)
resolved[key] = value ) and val not in ("", [], None):
except ProfileNotFoundError: resolved[key] = deepcopy(val)
pass
# 4. Handle default protocol if resolved.get("protocol") in ("", None):
if resolved.get("protocol") == "" or resolved.get("protocol") is None:
try: try:
default_profile = self.get_profile("default", resolve=True) default_profile = _get_resolved_ref("default")
resolved["protocol"] = default_profile.get("protocol", "ssh") resolved["protocol"] = default_profile.get("protocol", "ssh") or "ssh"
except ProfileNotFoundError: except ProfileNotFoundError:
resolved["protocol"] = "ssh" resolved["protocol"] = "ssh"
@@ -101,34 +244,58 @@ class ProfileService(BaseService):
def delete_profile(self, name): def delete_profile(self, name):
"""Delete an existing profile, with safety checks.""" """Delete an existing profile, with safety checks."""
if name not in self.config.profiles: if _supports_transactional_update(self.config):
def _mut(cfg):
if name not in cfg.profiles:
raise ProfileNotFoundError(f"Profile '{name}' not found.") raise ProfileNotFoundError(f"Profile '{name}' not found.")
if name == "default": if name == "default":
raise InvalidConfigurationError("Cannot delete the 'default' profile.") 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) used_by = self.config._profileused(name)
if used_by: if used_by:
# We return the list of nodes using it so the UI can inform the user raise InvalidConfigurationError(
raise InvalidConfigurationError(f"Profile '{name}' is used by nodes: {', '.join(used_by)}") f"Profile '{name}' is used by nodes: {', '.join(used_by)}"
)
self.config._profiles_del(id=name) self.config._profiles_del(id=name)
self.config._saveconfig(self.config.file) self.config._saveconfig(self.config.file)
def update_profile(self, name, data): def update_profile(self, name, data):
"""Update an existing profile.""" """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: if name not in self.config.profiles:
raise ProfileNotFoundError(f"Profile '{name}' not found.") raise ProfileNotFoundError(f"Profile '{name}' not found.")
# Merge with existing data
existing = self.get_profile(name, resolve=False) existing = self.get_profile(name, resolve=False)
updated_data = existing.copy() updated_data = deepcopy(existing)
updated_data.update(data) updated_data.update(data)
filtered_data = {k: v for k, v in updated_data.items() if k in self._ALLOWED_KEYS}
# Filter data to match _profiles_add signature self._validate_profile_payload(filtered_data)
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}
self.config._profiles_add(id=name, **filtered_data) self.config._profiles_add(id=name, **filtered_data)
self.config._saveconfig(self.config.file) self.config._saveconfig(self.config.file)
+65 -18
View File
@@ -1,5 +1,9 @@
import inspect
from contextlib import suppress
from .exceptions import InvalidConfigurationError from .exceptions import InvalidConfigurationError
class RemoteStub: class RemoteStub:
def __getattr__(self, name): def __getattr__(self, name):
raise NotImplementedError( raise NotImplementedError(
@@ -7,19 +11,26 @@ class RemoteStub:
"Use local mode or wait for the gRPC implementation." "Use local mode or wait for the gRPC implementation."
) )
class ServiceProvider: class ServiceProvider:
"""Dynamic service backend. Transparently provides local or remote services.""" """Dynamic service backend. Transparently provides local or remote services."""
def __init__(self, config, mode="local", remote_host=None): def __init__(self, config, mode="local", remote_host=None, enforce_context: bool = True):
from ..auth_utils import AuthAttemptTracker
self.mode = mode self.mode = mode
self.config = config self.config = config
self.remote_host = remote_host self.remote_host = remote_host
self.enforce_context = bool(enforce_context)
self.auth_tracker = AuthAttemptTracker()
self.nodes = None
self._system = None self._system = None
self._execution = None self._execution = None
self._import_export = None self._import_export = None
self._sync = None self._sync = None
self._users = None self._users = None
self._ai = None self._ai = None
self.set_enforce_context(self.enforce_context)
if mode == "local": if mode == "local":
self._init_local() self._init_local()
@@ -28,14 +39,26 @@ class ServiceProvider:
else: else:
raise ValueError(f"Unknown service mode: {mode}") raise ValueError(f"Unknown service mode: {mode}")
def _init_local(self): def set_enforce_context(self, enforce_context: bool) -> None:
from .node_service import NodeService self.enforce_context = bool(enforce_context)
from .profile_service import ProfileService if self.config is not None and not hasattr(self.config, "assert_called"):
from .config_service import ConfigService self.config._enforce_context = self.enforce_context
from .plugin_service import PluginService for svc in (
from .context_service import ContextService getattr(self, "nodes", None),
self._execution,
self._import_export,
):
if svc is not None and hasattr(svc, "enforce_context"):
svc.enforce_context = self.enforce_context
self.nodes = NodeService(self.config) def _init_local(self):
from .config_service import ConfigService
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, enforce_context=self.enforce_context)
self.profiles = ProfileService(self.config) self.profiles = ProfileService(self.config)
self.config_svc = ConfigService(self.config) self.config_svc = ConfigService(self.config)
self.plugins = PluginService(self.config) self.plugins = PluginService(self.config)
@@ -45,18 +68,28 @@ class ServiceProvider:
# Allow ConfigService to work locally so the user can revert the mode # Allow ConfigService to work locally so the user can revert the mode
from .config_service import ConfigService from .config_service import ConfigService
from .context_service import ContextService from .context_service import ContextService
self.config_svc = ConfigService(self.config) self.config_svc = ConfigService(self.config)
self.context = ContextService(self.config) self.context = ContextService(self.config)
if not self.remote_host: if not self.remote_host:
raise InvalidConfigurationError("Remote host must be specified in remote mode") raise InvalidConfigurationError("Remote host must be specified in remote mode")
import grpc
import os import os
import grpc
from ..grpc_layer.stubs import ( from ..grpc_layer.stubs import (
NodeStub, ProfileStub, PluginStub, AIStub, AIStub,
ExecutionStub, ImportExportStub, SystemStub, AuthClientInterceptor,
ConfigStub, AuthClientInterceptor, AuthStub AuthStub,
ConfigStub,
ExecutionStub,
ImportExportStub,
NodeStub,
PluginStub,
ProfileStub,
SystemStub,
) )
def get_token(): def get_token():
@@ -65,11 +98,8 @@ class ServiceProvider:
return env_token return env_token
token_path = os.path.join(self.config.defaultdir, ".token") token_path = os.path.join(self.config.defaultdir, ".token")
if os.path.exists(token_path): if os.path.exists(token_path):
try: with suppress(OSError, UnicodeDecodeError), open(token_path) as f:
with open(token_path, "r") as f:
return f.read().strip() return f.read().strip()
except Exception:
pass
return None return None
channel = grpc.insecure_channel(self.remote_host) channel = grpc.insecure_channel(self.remote_host)
@@ -86,7 +116,12 @@ class ServiceProvider:
self.plugins = PluginStub(channel, remote_host=self.remote_host) self.plugins = PluginStub(channel, remote_host=self.remote_host)
self.ai = AIStub(channel, remote_host=self.remote_host) self.ai = AIStub(channel, remote_host=self.remote_host)
self.system = SystemStub(channel, remote_host=self.remote_host) self.system = SystemStub(channel, remote_host=self.remote_host)
self.execution = ExecutionStub(channel, remote_host=self.remote_host) self.execution = ExecutionStub(
channel,
remote_host=self.remote_host,
config=self.config,
node_stub=self.nodes,
)
self.import_export = ImportExportStub(channel, remote_host=self.remote_host) self.import_export = ImportExportStub(channel, remote_host=self.remote_host)
self.auth = AuthStub(channel, remote_host=self.remote_host) self.auth = AuthStub(channel, remote_host=self.remote_host)
@@ -105,7 +140,13 @@ class ServiceProvider:
def execution(self): def execution(self):
if self._execution is None and self.mode == "local": if self._execution is None and self.mode == "local":
from .execution_service import ExecutionService from .execution_service import ExecutionService
self._execution = ExecutionService(self.config) params = inspect.signature(ExecutionService.__init__).parameters
kwargs = {}
if "attempt_tracker" in params:
kwargs["attempt_tracker"] = self.auth_tracker
if "enforce_context" in params:
kwargs["enforce_context"] = self.enforce_context
self._execution = ExecutionService(self.config, **kwargs)
return self._execution return self._execution
@execution.setter @execution.setter
@@ -116,6 +157,12 @@ class ServiceProvider:
def import_export(self): def import_export(self):
if self._import_export is None and self.mode == "local": if self._import_export is None and self.mode == "local":
from .import_export_service import ImportExportService from .import_export_service import ImportExportService
params = inspect.signature(ImportExportService.__init__).parameters
if "enforce_context" in params:
self._import_export = ImportExportService(
self.config, enforce_context=self.enforce_context
)
else:
self._import_export = ImportExportService(self.config) self._import_export = ImportExportService(self.config)
return self._import_export return self._import_export
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
+15
View File
@@ -80,11 +80,22 @@ class TestTreeCompletions:
assert "--shell-command" in config_completions assert "--shell-command" in config_completions
assert "--shell-prompt" in config_completions assert "--shell-prompt" in config_completions
assert "--shell-os" in config_completions assert "--shell-os" in config_completions
assert "--max-auth-prompts" in config_completions
assert "--max-total-auth-attempts" in config_completions
assert "--max-hop-host-attempts" in config_completions
assert "--max-global-user-attempts" in config_completions
assert "--disable-fallback-on-insecure-hostkey" in config_completions
# Resolve when --engineer-auth is chosen in config # Resolve when --engineer-auth is chosen in config
auth_comp = resolve_completion(["config", "--engineer-auth", ""], tree) auth_comp = resolve_completion(["config", "--engineer-auth", ""], tree)
assert isinstance(auth_comp, list) assert isinstance(auth_comp, list)
# Resolve boolean values for --disable-fallback-on-insecure-hostkey
assert resolve_completion(
["config", "--disable-fallback-on-insecure-hostkey", ""],
tree,
) == ["true", "false"]
# Loop back check: # Loop back check:
# e.g., connpy config --engineer-auth some_val # e.g., connpy config --engineer-auth some_val
# should loop back and resolve to config options # should loop back and resolve to config options
@@ -93,6 +104,10 @@ class TestTreeCompletions:
assert "--engineer-auth" in loop_back_comp assert "--engineer-auth" in loop_back_comp
assert "--shell-command" in loop_back_comp assert "--shell-command" in loop_back_comp
auth_budget_loop_back = resolve_completion(["config", "--max-auth-prompts", "3", ""], tree)
assert "--max-total-auth-attempts" in auth_budget_loop_back
assert "--disable-fallback-on-insecure-hostkey" in auth_budget_loop_back
def test_shell_completions(self): def test_shell_completions(self):
from connpy.completion import _build_tree, resolve_completion from connpy.completion import _build_tree, resolve_completion
tree = _build_tree([], [], [], {}, "/tmp") tree = _build_tree([], [], [], {}, "/tmp")
File diff suppressed because it is too large. Load diff
+164 -4
View File
@@ -80,7 +80,12 @@ el.replaceWith(d);
&#34;sync_remote&#34;: self.set_sync_remote, &#34;sync_remote&#34;: self.set_sync_remote,
&#34;shell_command&#34;: self.set_shell_config, &#34;shell_command&#34;: self.set_shell_config,
&#34;shell_prompt&#34;: self.set_shell_config, &#34;shell_prompt&#34;: self.set_shell_config,
&#34;shell_os&#34;: self.set_shell_config &#34;shell_os&#34;: self.set_shell_config,
&#34;max_auth_prompts&#34;: self.set_auth_budget,
&#34;max_total_auth_attempts&#34;: self.set_auth_budget,
&#34;max_hop_host_attempts&#34;: self.set_auth_budget,
&#34;max_global_user_attempts&#34;: self.set_auth_budget,
&#34;disable_fallback_on_insecure_hostkey&#34;: self.set_disable_fallback_on_insecure_hostkey,
} }
handler = actions.get(getattr(args, &#34;command&#34;, None)) handler = actions.get(getattr(args, &#34;command&#34;, None))
if handler: if handler:
@@ -251,7 +256,71 @@ el.replaceWith(d);
self.app.services.config_svc.update_setting(&#34;shell&#34;, shell_cfg) self.app.services.config_svc.update_setting(&#34;shell&#34;, shell_cfg)
printer.success(&#34;Config saved&#34;) printer.success(&#34;Config saved&#34;)
except (ConnpyError, InvalidConfigurationError) as e: except (ConnpyError, InvalidConfigurationError) as e:
printer.error(str(e))</code></pre> 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 = {
&#34;max_auth_prompts&#34;: (MIN_AUTH_PROMPTS, MAX_AUTH_PROMPTS_CAP),
&#34;max_total_auth_attempts&#34;: (MIN_TOTAL_AUTH_ATTEMPTS, MAX_TOTAL_AUTH_ATTEMPTS_CAP),
&#34;max_hop_host_attempts&#34;: (MIN_HOP_HOST_ATTEMPTS, MAX_HOP_HOST_ATTEMPTS_CAP),
&#34;max_global_user_attempts&#34;: (1, 1000),
}
if key not in bounds_map:
raise InvalidConfigurationError(f&#34;Unknown auth budget setting: {key}&#34;)
if isinstance(raw_val, bool):
msg = f&#34;Invalid {key}: boolean is not permitted&#34;
printer.error(msg)
raise InvalidConfigurationError(msg)
if isinstance(raw_val, int):
val = raw_val
elif isinstance(raw_val, str) and re.fullmatch(r&#34;[+-]?\d+&#34;, raw_val.strip()):
val = int(raw_val.strip())
else:
msg = f&#34;Invalid {key}: expected integer, got {raw_val!r}&#34;
printer.error(msg)
raise InvalidConfigurationError(msg)
if val == 0:
cfg_obj = self.app.config
if not hasattr(cfg_obj, &#34;assert_called&#34;) and callable(
getattr(cfg_obj, &#34;transactional_update&#34;, 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(&#34;Config saved&#34;)
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(&#34;Config saved&#34;)
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 (&#34;true&#34;, &#34;false&#34;):
val = raw_val.strip().lower() == &#34;true&#34;
else:
msg = &#34;Invalid disable_fallback_on_insecure_hostkey: expected &#39;true&#39; or &#39;false&#39;&#34;
printer.error(msg)
raise InvalidConfigurationError(msg)
self.app.services.config_svc.update_setting(
&#34;disable_fallback_on_insecure_hostkey&#34;,
val,
)
printer.success(&#34;Config saved&#34;)</code></pre>
</details> </details>
<div class="desc"></div> <div class="desc"></div>
<h3>Methods</h3> <h3>Methods</h3>
@@ -285,7 +354,12 @@ el.replaceWith(d);
&#34;sync_remote&#34;: self.set_sync_remote, &#34;sync_remote&#34;: self.set_sync_remote,
&#34;shell_command&#34;: self.set_shell_config, &#34;shell_command&#34;: self.set_shell_config,
&#34;shell_prompt&#34;: self.set_shell_config, &#34;shell_prompt&#34;: self.set_shell_config,
&#34;shell_os&#34;: self.set_shell_config &#34;shell_os&#34;: self.set_shell_config,
&#34;max_auth_prompts&#34;: self.set_auth_budget,
&#34;max_total_auth_attempts&#34;: self.set_auth_budget,
&#34;max_hop_host_attempts&#34;: self.set_auth_budget,
&#34;max_global_user_attempts&#34;: self.set_auth_budget,
&#34;disable_fallback_on_insecure_hostkey&#34;: self.set_disable_fallback_on_insecure_hostkey,
} }
handler = actions.get(getattr(args, &#34;command&#34;, None)) handler = actions.get(getattr(args, &#34;command&#34;, None))
if handler: if handler:
@@ -333,6 +407,64 @@ el.replaceWith(d);
</details> </details>
<div class="desc"></div> <div class="desc"></div>
</dd> </dd>
<dt id="connpy.cli.config_handler.ConfigHandler.set_auth_budget"><code class="name flex">
<span>def <span class="ident">set_auth_budget</span></span>(<span>self, args)</span>
</code></dt>
<dd>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def set_auth_budget(self, args):
key = args.command
raw_val = args.data[0] if isinstance(args.data, list) else args.data
bounds_map = {
&#34;max_auth_prompts&#34;: (MIN_AUTH_PROMPTS, MAX_AUTH_PROMPTS_CAP),
&#34;max_total_auth_attempts&#34;: (MIN_TOTAL_AUTH_ATTEMPTS, MAX_TOTAL_AUTH_ATTEMPTS_CAP),
&#34;max_hop_host_attempts&#34;: (MIN_HOP_HOST_ATTEMPTS, MAX_HOP_HOST_ATTEMPTS_CAP),
&#34;max_global_user_attempts&#34;: (1, 1000),
}
if key not in bounds_map:
raise InvalidConfigurationError(f&#34;Unknown auth budget setting: {key}&#34;)
if isinstance(raw_val, bool):
msg = f&#34;Invalid {key}: boolean is not permitted&#34;
printer.error(msg)
raise InvalidConfigurationError(msg)
if isinstance(raw_val, int):
val = raw_val
elif isinstance(raw_val, str) and re.fullmatch(r&#34;[+-]?\d+&#34;, raw_val.strip()):
val = int(raw_val.strip())
else:
msg = f&#34;Invalid {key}: expected integer, got {raw_val!r}&#34;
printer.error(msg)
raise InvalidConfigurationError(msg)
if val == 0:
cfg_obj = self.app.config
if not hasattr(cfg_obj, &#34;assert_called&#34;) and callable(
getattr(cfg_obj, &#34;transactional_update&#34;, 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(&#34;Config saved&#34;)
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(&#34;Config saved&#34;)</code></pre>
</details>
<div class="desc"></div>
</dd>
<dt id="connpy.cli.config_handler.ConfigHandler.set_case"><code class="name flex"> <dt id="connpy.cli.config_handler.ConfigHandler.set_case"><code class="name flex">
<span>def <span class="ident">set_case</span></span>(<span>self, args)</span> <span>def <span class="ident">set_case</span></span>(<span>self, args)</span>
</code></dt> </code></dt>
@@ -367,6 +499,32 @@ el.replaceWith(d);
</details> </details>
<div class="desc"></div> <div class="desc"></div>
</dd> </dd>
<dt id="connpy.cli.config_handler.ConfigHandler.set_disable_fallback_on_insecure_hostkey"><code class="name flex">
<span>def <span class="ident">set_disable_fallback_on_insecure_hostkey</span></span>(<span>self, args)</span>
</code></dt>
<dd>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">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 (&#34;true&#34;, &#34;false&#34;):
val = raw_val.strip().lower() == &#34;true&#34;
else:
msg = &#34;Invalid disable_fallback_on_insecure_hostkey: expected &#39;true&#39; or &#39;false&#39;&#34;
printer.error(msg)
raise InvalidConfigurationError(msg)
self.app.services.config_svc.update_setting(
&#34;disable_fallback_on_insecure_hostkey&#34;,
val,
)
printer.success(&#34;Config saved&#34;)</code></pre>
</details>
<div class="desc"></div>
</dd>
<dt id="connpy.cli.config_handler.ConfigHandler.set_fzf"><code class="name flex"> <dt id="connpy.cli.config_handler.ConfigHandler.set_fzf"><code class="name flex">
<span>def <span class="ident">set_fzf</span></span>(<span>self, args)</span> <span>def <span class="ident">set_fzf</span></span>(<span>self, args)</span>
</code></dt> </code></dt>
@@ -577,11 +735,13 @@ el.replaceWith(d);
<ul> <ul>
<li> <li>
<h4><code><a title="connpy.cli.config_handler.ConfigHandler" href="#connpy.cli.config_handler.ConfigHandler">ConfigHandler</a></code></h4> <h4><code><a title="connpy.cli.config_handler.ConfigHandler" href="#connpy.cli.config_handler.ConfigHandler">ConfigHandler</a></code></h4>
<ul class="two-column"> <ul class="">
<li><code><a title="connpy.cli.config_handler.ConfigHandler.dispatch" href="#connpy.cli.config_handler.ConfigHandler.dispatch">dispatch</a></code></li> <li><code><a title="connpy.cli.config_handler.ConfigHandler.dispatch" href="#connpy.cli.config_handler.ConfigHandler.dispatch">dispatch</a></code></li>
<li><code><a title="connpy.cli.config_handler.ConfigHandler.set_ai_config" href="#connpy.cli.config_handler.ConfigHandler.set_ai_config">set_ai_config</a></code></li> <li><code><a title="connpy.cli.config_handler.ConfigHandler.set_ai_config" href="#connpy.cli.config_handler.ConfigHandler.set_ai_config">set_ai_config</a></code></li>
<li><code><a title="connpy.cli.config_handler.ConfigHandler.set_auth_budget" href="#connpy.cli.config_handler.ConfigHandler.set_auth_budget">set_auth_budget</a></code></li>
<li><code><a title="connpy.cli.config_handler.ConfigHandler.set_case" href="#connpy.cli.config_handler.ConfigHandler.set_case">set_case</a></code></li> <li><code><a title="connpy.cli.config_handler.ConfigHandler.set_case" href="#connpy.cli.config_handler.ConfigHandler.set_case">set_case</a></code></li>
<li><code><a title="connpy.cli.config_handler.ConfigHandler.set_configfolder" href="#connpy.cli.config_handler.ConfigHandler.set_configfolder">set_configfolder</a></code></li> <li><code><a title="connpy.cli.config_handler.ConfigHandler.set_configfolder" href="#connpy.cli.config_handler.ConfigHandler.set_configfolder">set_configfolder</a></code></li>
<li><code><a title="connpy.cli.config_handler.ConfigHandler.set_disable_fallback_on_insecure_hostkey" href="#connpy.cli.config_handler.ConfigHandler.set_disable_fallback_on_insecure_hostkey">set_disable_fallback_on_insecure_hostkey</a></code></li>
<li><code><a title="connpy.cli.config_handler.ConfigHandler.set_fzf" href="#connpy.cli.config_handler.ConfigHandler.set_fzf">set_fzf</a></code></li> <li><code><a title="connpy.cli.config_handler.ConfigHandler.set_fzf" href="#connpy.cli.config_handler.ConfigHandler.set_fzf">set_fzf</a></code></li>
<li><code><a title="connpy.cli.config_handler.ConfigHandler.set_idletime" href="#connpy.cli.config_handler.ConfigHandler.set_idletime">set_idletime</a></code></li> <li><code><a title="connpy.cli.config_handler.ConfigHandler.set_idletime" href="#connpy.cli.config_handler.ConfigHandler.set_idletime">set_idletime</a></code></li>
<li><code><a title="connpy.cli.config_handler.ConfigHandler.set_remote_host" href="#connpy.cli.config_handler.ConfigHandler.set_remote_host">set_remote_host</a></code></li> <li><code><a title="connpy.cli.config_handler.ConfigHandler.set_remote_host" href="#connpy.cli.config_handler.ConfigHandler.set_remote_host">set_remote_host</a></code></li>
+122 -30
View File
@@ -72,6 +72,7 @@ el.replaceWith(d);
questions.append(inquirer.Confirm(&#34;jumphost&#34;, message=&#34;Edit jumphost?&#34;)) questions.append(inquirer.Confirm(&#34;jumphost&#34;, message=&#34;Edit jumphost?&#34;))
questions.append(inquirer.Confirm(&#34;user&#34;, message=&#34;Edit User?&#34;)) questions.append(inquirer.Confirm(&#34;user&#34;, message=&#34;Edit User?&#34;))
questions.append(inquirer.Confirm(&#34;password&#34;, message=&#34;Edit password?&#34;)) questions.append(inquirer.Confirm(&#34;password&#34;, message=&#34;Edit password?&#34;))
questions.append(inquirer.Confirm(&#34;fallback&#34;, message=&#34;Edit fallback credentials?&#34;))
return inquirer.prompt(questions) return inquirer.prompt(questions)
def questions_nodes(self, unique, uniques=None, edit=None): def questions_nodes(self, unique, uniques=None, edit=None):
@@ -82,11 +83,13 @@ el.replaceWith(d);
defaults[&#34;tags&#34;] = &#34;&#34; defaults[&#34;tags&#34;] = &#34;&#34;
if &#34;jumphost&#34; not in defaults: if &#34;jumphost&#34; not in defaults:
defaults[&#34;jumphost&#34;] = &#34;&#34; defaults[&#34;jumphost&#34;] = &#34;&#34;
except Exception: if &#34;fallback&#34; not in defaults:
defaults = {&#34;host&#34;: &#34;&#34;, &#34;protocol&#34;: &#34;&#34;, &#34;port&#34;: &#34;&#34;, &#34;user&#34;: &#34;&#34;, &#34;options&#34;: &#34;&#34;, &#34;logs&#34;: &#34;&#34;, &#34;tags&#34;: &#34;&#34;, &#34;password&#34;: &#34;&#34;, &#34;jumphost&#34;: &#34;&#34;} defaults[&#34;fallback&#34;] = &#34;&#34;
except Exception: # noqa: BLE001
defaults = {&#34;host&#34;: &#34;&#34;, &#34;protocol&#34;: &#34;&#34;, &#34;port&#34;: &#34;&#34;, &#34;user&#34;: &#34;&#34;, &#34;options&#34;: &#34;&#34;, &#34;logs&#34;: &#34;&#34;, &#34;tags&#34;: &#34;&#34;, &#34;password&#34;: &#34;&#34;, &#34;jumphost&#34;: &#34;&#34;, &#34;fallback&#34;: &#34;&#34;}
node = {} node = {}
if edit is None: if edit is None:
edit = {&#34;host&#34;: True, &#34;protocol&#34;: True, &#34;port&#34;: True, &#34;user&#34;: True, &#34;password&#34;: True, &#34;options&#34;: True, &#34;logs&#34;: True, &#34;tags&#34;: True, &#34;jumphost&#34;: True} edit = {&#34;host&#34;: True, &#34;protocol&#34;: True, &#34;port&#34;: True, &#34;user&#34;: True, &#34;password&#34;: True, &#34;options&#34;: True, &#34;logs&#34;: True, &#34;tags&#34;: True, &#34;jumphost&#34;: True, &#34;fallback&#34;: True}
questions = [] questions = []
if edit[&#34;host&#34;]: if edit[&#34;host&#34;]:
questions.append(inquirer.Text(&#34;host&#34;, message=&#34;Add Hostname or IP&#34;, validate=self.validators.host_validation, default=defaults[&#34;host&#34;])) questions.append(inquirer.Text(&#34;host&#34;, message=&#34;Add Hostname or IP&#34;, validate=self.validators.host_validation, default=defaults[&#34;host&#34;]))
@@ -124,6 +127,8 @@ el.replaceWith(d);
questions.append(inquirer.List(&#34;password&#34;, message=&#34;Password: Use a local password, no password or a list of profiles to reference?&#34;, choices=[&#34;Local Password&#34;, &#34;Profiles&#34;, &#34;No Password&#34;])) questions.append(inquirer.List(&#34;password&#34;, message=&#34;Password: Use a local password, no password or a list of profiles to reference?&#34;, choices=[&#34;Local Password&#34;, &#34;Profiles&#34;, &#34;No Password&#34;]))
else: else:
node[&#34;password&#34;] = defaults[&#34;password&#34;] node[&#34;password&#34;] = defaults[&#34;password&#34;]
if not edit.get(&#34;fallback&#34;):
node[&#34;fallback&#34;] = defaults.get(&#34;fallback&#34;, &#34;&#34;)
answer = inquirer.prompt(questions) answer = inquirer.prompt(questions)
if answer is None: if answer is None:
@@ -141,12 +146,28 @@ el.replaceWith(d);
passa = inquirer.prompt(passq) passa = inquirer.prompt(passq)
if passa is None: if passa is None:
return False return False
answer[&#34;password&#34;] = passa[&#34;password&#34;].split(&#34;,&#34;) answer[&#34;password&#34;] = [p.strip() for p in passa[&#34;password&#34;].split(&#34;,&#34;) if p.strip()]
elif answer[&#34;password&#34;] == &#34;No Password&#34;: elif answer[&#34;password&#34;] == &#34;No Password&#34;:
answer[&#34;password&#34;] = &#34;&#34; answer[&#34;password&#34;] = &#34;&#34;
if &#34;tags&#34; in answer and not answer[&#34;tags&#34;].startswith(&#34;@&#34;) and answer[&#34;tags&#34;]: if edit.get(&#34;fallback&#34;):
answer[&#34;tags&#34;] = ast.literal_eval(answer[&#34;tags&#34;]) raw_def_fb = defaults.get(&#34;fallback&#34;, &#34;&#34;)
def_fb_str = json.dumps(raw_def_fb) if isinstance(raw_def_fb, (list, dict)) else str(raw_def_fb or &#34;&#34;)
fbq = [
inquirer.Text(
&#34;fallback&#34;,
message=&#34;Add fallback credentials (@profile, @p1,@p2, JSON list/dict, or compose:)&#34;,
validate=self.validators.fallback_validation,
default=def_fb_str.replace(&#34;{&#34;, &#34;{{&#34;).replace(&#34;}&#34;, &#34;}}&#34;),
)
]
fba = inquirer.prompt(fbq)
if fba is None:
return False
answer[&#34;fallback&#34;] = _parse_fallback_input(fba[&#34;fallback&#34;])
if &#34;tags&#34; in answer and answer[&#34;tags&#34;] and not str(answer[&#34;tags&#34;]).startswith(&#34;@&#34;):
answer[&#34;tags&#34;] = _parse_tags_input(answer[&#34;tags&#34;])
result = {**uniques, **answer, **node} result = {**uniques, **answer, **node}
result[&#34;type&#34;] = &#34;connection&#34; result[&#34;type&#34;] = &#34;connection&#34;
@@ -160,11 +181,13 @@ el.replaceWith(d);
defaults[&#34;tags&#34;] = &#34;&#34; defaults[&#34;tags&#34;] = &#34;&#34;
if &#34;jumphost&#34; not in defaults: if &#34;jumphost&#34; not in defaults:
defaults[&#34;jumphost&#34;] = &#34;&#34; defaults[&#34;jumphost&#34;] = &#34;&#34;
except Exception: if &#34;fallback&#34; not in defaults:
defaults = {&#34;host&#34;: &#34;&#34;, &#34;protocol&#34;: &#34;&#34;, &#34;port&#34;: &#34;&#34;, &#34;user&#34;: &#34;&#34;, &#34;options&#34;: &#34;&#34;, &#34;logs&#34;: &#34;&#34;, &#34;tags&#34;: &#34;&#34;, &#34;jumphost&#34;: &#34;&#34;} defaults[&#34;fallback&#34;] = &#34;&#34;
except Exception: # noqa: BLE001
defaults = {&#34;host&#34;: &#34;&#34;, &#34;protocol&#34;: &#34;&#34;, &#34;port&#34;: &#34;&#34;, &#34;user&#34;: &#34;&#34;, &#34;options&#34;: &#34;&#34;, &#34;logs&#34;: &#34;&#34;, &#34;tags&#34;: &#34;&#34;, &#34;jumphost&#34;: &#34;&#34;, &#34;fallback&#34;: &#34;&#34;}
profile = {} profile = {}
if edit is None: if edit is None:
edit = {&#34;host&#34;: True, &#34;protocol&#34;: True, &#34;port&#34;: True, &#34;user&#34;: True, &#34;password&#34;: True, &#34;options&#34;: True, &#34;logs&#34;: True, &#34;tags&#34;: True, &#34;jumphost&#34;: True} edit = {&#34;host&#34;: True, &#34;protocol&#34;: True, &#34;port&#34;: True, &#34;user&#34;: True, &#34;password&#34;: True, &#34;options&#34;: True, &#34;logs&#34;: True, &#34;tags&#34;: True, &#34;jumphost&#34;: True, &#34;fallback&#34;: True}
questions = [] questions = []
if edit[&#34;host&#34;]: if edit[&#34;host&#34;]:
questions.append(inquirer.Text(&#34;host&#34;, message=&#34;Add Hostname or IP&#34;, default=defaults[&#34;host&#34;])) questions.append(inquirer.Text(&#34;host&#34;, message=&#34;Add Hostname or IP&#34;, default=defaults[&#34;host&#34;]))
@@ -202,17 +225,24 @@ el.replaceWith(d);
questions.append(inquirer.Password(&#34;password&#34;, message=&#34;Set Password&#34;)) questions.append(inquirer.Password(&#34;password&#34;, message=&#34;Set Password&#34;))
else: else:
profile[&#34;password&#34;] = defaults[&#34;password&#34;] profile[&#34;password&#34;] = defaults[&#34;password&#34;]
if edit.get(&#34;fallback&#34;):
raw_def_fb = defaults.get(&#34;fallback&#34;, &#34;&#34;)
def_fb_str = json.dumps(raw_def_fb) if isinstance(raw_def_fb, (list, dict)) else str(raw_def_fb or &#34;&#34;)
questions.append(inquirer.Text(&#34;fallback&#34;, message=&#34;Add fallback credentials (@profile, @p1,@p2, JSON list/dict, or compose:)&#34;, validate=self.validators.fallback_validation, default=def_fb_str.replace(&#34;{&#34;, &#34;{{&#34;).replace(&#34;}&#34;, &#34;}}&#34;)))
else:
profile[&#34;fallback&#34;] = defaults.get(&#34;fallback&#34;, &#34;&#34;)
answer = inquirer.prompt(questions) answer = inquirer.prompt(questions)
if answer is None: if answer is None:
return False return False
if &#34;password&#34; in answer: if &#34;password&#34; in answer and answer[&#34;password&#34;] != &#34;&#34;:
if answer[&#34;password&#34;] != &#34;&#34;:
answer[&#34;password&#34;] = self.app.services.config_svc.encrypt_password(answer[&#34;password&#34;]) answer[&#34;password&#34;] = self.app.services.config_svc.encrypt_password(answer[&#34;password&#34;])
if &#34;tags&#34; in answer and answer[&#34;tags&#34;]: if &#34;tags&#34; in answer and answer[&#34;tags&#34;]:
answer[&#34;tags&#34;] = ast.literal_eval(answer[&#34;tags&#34;]) answer[&#34;tags&#34;] = _parse_tags_input(answer[&#34;tags&#34;])
if &#34;fallback&#34; in answer:
answer[&#34;fallback&#34;] = _parse_fallback_input(answer[&#34;fallback&#34;])
result = {**answer, **profile} result = {**answer, **profile}
result[&#34;id&#34;] = unique result[&#34;id&#34;] = unique
@@ -241,17 +271,33 @@ el.replaceWith(d);
if answer[&#34;password&#34;] == &#34;Local Password&#34;: if answer[&#34;password&#34;] == &#34;Local Password&#34;:
passq = [inquirer.Password(&#34;password&#34;, message=&#34;Set Password&#34;)] passq = [inquirer.Password(&#34;password&#34;, message=&#34;Set Password&#34;)]
passa = inquirer.prompt(passq) passa = inquirer.prompt(passq)
if passa is None:
return False
answer[&#34;password&#34;] = self.app.services.config_svc.encrypt_password(passa[&#34;password&#34;]) answer[&#34;password&#34;] = self.app.services.config_svc.encrypt_password(passa[&#34;password&#34;])
elif answer[&#34;password&#34;] == &#34;Profiles&#34;: elif answer[&#34;password&#34;] == &#34;Profiles&#34;:
passq = [(inquirer.Text(&#34;password&#34;, message=&#34;Set a @profile or a comma separated list of @profiles&#34;, validate=self.validators.pass_validation))] passq = [(inquirer.Text(&#34;password&#34;, message=&#34;Set a @profile or a comma separated list of @profiles&#34;, validate=self.validators.pass_validation))]
passa = inquirer.prompt(passq) passa = inquirer.prompt(passq)
answer[&#34;password&#34;] = passa[&#34;password&#34;].split(&#34;,&#34;) if passa is None:
return False
answer[&#34;password&#34;] = [p.strip() for p in passa[&#34;password&#34;].split(&#34;,&#34;) if p.strip()]
elif answer[&#34;password&#34;] == &#34;No Password&#34;: elif answer[&#34;password&#34;] == &#34;No Password&#34;:
answer[&#34;password&#34;] = &#34;&#34; answer[&#34;password&#34;] = &#34;&#34;
fbq = [
inquirer.Text(
&#34;fallback&#34;,
message=&#34;Add fallback credentials (@profile, @p1,@p2, JSON list/dict, or compose:)&#34;,
validate=self.validators.fallback_validation,
)
]
fba = inquirer.prompt(fbq)
if fba is None:
return False
answer[&#34;fallback&#34;] = _parse_fallback_input(fba[&#34;fallback&#34;])
answer[&#34;type&#34;] = &#34;connection&#34; answer[&#34;type&#34;] = &#34;connection&#34;
if &#34;tags&#34; in answer and not answer[&#34;tags&#34;].startswith(&#34;@&#34;) and answer[&#34;tags&#34;]: if &#34;tags&#34; in answer and answer[&#34;tags&#34;] and not str(answer[&#34;tags&#34;]).startswith(&#34;@&#34;):
answer[&#34;tags&#34;] = ast.literal_eval(answer[&#34;tags&#34;]) answer[&#34;tags&#34;] = _parse_tags_input(answer[&#34;tags&#34;])
return answer return answer
@@ -463,17 +509,33 @@ el.replaceWith(d);
if answer[&#34;password&#34;] == &#34;Local Password&#34;: if answer[&#34;password&#34;] == &#34;Local Password&#34;:
passq = [inquirer.Password(&#34;password&#34;, message=&#34;Set Password&#34;)] passq = [inquirer.Password(&#34;password&#34;, message=&#34;Set Password&#34;)]
passa = inquirer.prompt(passq) passa = inquirer.prompt(passq)
if passa is None:
return False
answer[&#34;password&#34;] = self.app.services.config_svc.encrypt_password(passa[&#34;password&#34;]) answer[&#34;password&#34;] = self.app.services.config_svc.encrypt_password(passa[&#34;password&#34;])
elif answer[&#34;password&#34;] == &#34;Profiles&#34;: elif answer[&#34;password&#34;] == &#34;Profiles&#34;:
passq = [(inquirer.Text(&#34;password&#34;, message=&#34;Set a @profile or a comma separated list of @profiles&#34;, validate=self.validators.pass_validation))] passq = [(inquirer.Text(&#34;password&#34;, message=&#34;Set a @profile or a comma separated list of @profiles&#34;, validate=self.validators.pass_validation))]
passa = inquirer.prompt(passq) passa = inquirer.prompt(passq)
answer[&#34;password&#34;] = passa[&#34;password&#34;].split(&#34;,&#34;) if passa is None:
return False
answer[&#34;password&#34;] = [p.strip() for p in passa[&#34;password&#34;].split(&#34;,&#34;) if p.strip()]
elif answer[&#34;password&#34;] == &#34;No Password&#34;: elif answer[&#34;password&#34;] == &#34;No Password&#34;:
answer[&#34;password&#34;] = &#34;&#34; answer[&#34;password&#34;] = &#34;&#34;
fbq = [
inquirer.Text(
&#34;fallback&#34;,
message=&#34;Add fallback credentials (@profile, @p1,@p2, JSON list/dict, or compose:)&#34;,
validate=self.validators.fallback_validation,
)
]
fba = inquirer.prompt(fbq)
if fba is None:
return False
answer[&#34;fallback&#34;] = _parse_fallback_input(fba[&#34;fallback&#34;])
answer[&#34;type&#34;] = &#34;connection&#34; answer[&#34;type&#34;] = &#34;connection&#34;
if &#34;tags&#34; in answer and not answer[&#34;tags&#34;].startswith(&#34;@&#34;) and answer[&#34;tags&#34;]: if &#34;tags&#34; in answer and answer[&#34;tags&#34;] and not str(answer[&#34;tags&#34;]).startswith(&#34;@&#34;):
answer[&#34;tags&#34;] = ast.literal_eval(answer[&#34;tags&#34;]) answer[&#34;tags&#34;] = _parse_tags_input(answer[&#34;tags&#34;])
return answer</code></pre> return answer</code></pre>
</details> </details>
@@ -499,6 +561,7 @@ el.replaceWith(d);
questions.append(inquirer.Confirm(&#34;jumphost&#34;, message=&#34;Edit jumphost?&#34;)) questions.append(inquirer.Confirm(&#34;jumphost&#34;, message=&#34;Edit jumphost?&#34;))
questions.append(inquirer.Confirm(&#34;user&#34;, message=&#34;Edit User?&#34;)) questions.append(inquirer.Confirm(&#34;user&#34;, message=&#34;Edit User?&#34;))
questions.append(inquirer.Confirm(&#34;password&#34;, message=&#34;Edit password?&#34;)) questions.append(inquirer.Confirm(&#34;password&#34;, message=&#34;Edit password?&#34;))
questions.append(inquirer.Confirm(&#34;fallback&#34;, message=&#34;Edit fallback credentials?&#34;))
return inquirer.prompt(questions)</code></pre> return inquirer.prompt(questions)</code></pre>
</details> </details>
<div class="desc"></div> <div class="desc"></div>
@@ -519,11 +582,13 @@ el.replaceWith(d);
defaults[&#34;tags&#34;] = &#34;&#34; defaults[&#34;tags&#34;] = &#34;&#34;
if &#34;jumphost&#34; not in defaults: if &#34;jumphost&#34; not in defaults:
defaults[&#34;jumphost&#34;] = &#34;&#34; defaults[&#34;jumphost&#34;] = &#34;&#34;
except Exception: if &#34;fallback&#34; not in defaults:
defaults = {&#34;host&#34;: &#34;&#34;, &#34;protocol&#34;: &#34;&#34;, &#34;port&#34;: &#34;&#34;, &#34;user&#34;: &#34;&#34;, &#34;options&#34;: &#34;&#34;, &#34;logs&#34;: &#34;&#34;, &#34;tags&#34;: &#34;&#34;, &#34;password&#34;: &#34;&#34;, &#34;jumphost&#34;: &#34;&#34;} defaults[&#34;fallback&#34;] = &#34;&#34;
except Exception: # noqa: BLE001
defaults = {&#34;host&#34;: &#34;&#34;, &#34;protocol&#34;: &#34;&#34;, &#34;port&#34;: &#34;&#34;, &#34;user&#34;: &#34;&#34;, &#34;options&#34;: &#34;&#34;, &#34;logs&#34;: &#34;&#34;, &#34;tags&#34;: &#34;&#34;, &#34;password&#34;: &#34;&#34;, &#34;jumphost&#34;: &#34;&#34;, &#34;fallback&#34;: &#34;&#34;}
node = {} node = {}
if edit is None: if edit is None:
edit = {&#34;host&#34;: True, &#34;protocol&#34;: True, &#34;port&#34;: True, &#34;user&#34;: True, &#34;password&#34;: True, &#34;options&#34;: True, &#34;logs&#34;: True, &#34;tags&#34;: True, &#34;jumphost&#34;: True} edit = {&#34;host&#34;: True, &#34;protocol&#34;: True, &#34;port&#34;: True, &#34;user&#34;: True, &#34;password&#34;: True, &#34;options&#34;: True, &#34;logs&#34;: True, &#34;tags&#34;: True, &#34;jumphost&#34;: True, &#34;fallback&#34;: True}
questions = [] questions = []
if edit[&#34;host&#34;]: if edit[&#34;host&#34;]:
questions.append(inquirer.Text(&#34;host&#34;, message=&#34;Add Hostname or IP&#34;, validate=self.validators.host_validation, default=defaults[&#34;host&#34;])) questions.append(inquirer.Text(&#34;host&#34;, message=&#34;Add Hostname or IP&#34;, validate=self.validators.host_validation, default=defaults[&#34;host&#34;]))
@@ -561,6 +626,8 @@ el.replaceWith(d);
questions.append(inquirer.List(&#34;password&#34;, message=&#34;Password: Use a local password, no password or a list of profiles to reference?&#34;, choices=[&#34;Local Password&#34;, &#34;Profiles&#34;, &#34;No Password&#34;])) questions.append(inquirer.List(&#34;password&#34;, message=&#34;Password: Use a local password, no password or a list of profiles to reference?&#34;, choices=[&#34;Local Password&#34;, &#34;Profiles&#34;, &#34;No Password&#34;]))
else: else:
node[&#34;password&#34;] = defaults[&#34;password&#34;] node[&#34;password&#34;] = defaults[&#34;password&#34;]
if not edit.get(&#34;fallback&#34;):
node[&#34;fallback&#34;] = defaults.get(&#34;fallback&#34;, &#34;&#34;)
answer = inquirer.prompt(questions) answer = inquirer.prompt(questions)
if answer is None: if answer is None:
@@ -578,12 +645,28 @@ el.replaceWith(d);
passa = inquirer.prompt(passq) passa = inquirer.prompt(passq)
if passa is None: if passa is None:
return False return False
answer[&#34;password&#34;] = passa[&#34;password&#34;].split(&#34;,&#34;) answer[&#34;password&#34;] = [p.strip() for p in passa[&#34;password&#34;].split(&#34;,&#34;) if p.strip()]
elif answer[&#34;password&#34;] == &#34;No Password&#34;: elif answer[&#34;password&#34;] == &#34;No Password&#34;:
answer[&#34;password&#34;] = &#34;&#34; answer[&#34;password&#34;] = &#34;&#34;
if &#34;tags&#34; in answer and not answer[&#34;tags&#34;].startswith(&#34;@&#34;) and answer[&#34;tags&#34;]: if edit.get(&#34;fallback&#34;):
answer[&#34;tags&#34;] = ast.literal_eval(answer[&#34;tags&#34;]) raw_def_fb = defaults.get(&#34;fallback&#34;, &#34;&#34;)
def_fb_str = json.dumps(raw_def_fb) if isinstance(raw_def_fb, (list, dict)) else str(raw_def_fb or &#34;&#34;)
fbq = [
inquirer.Text(
&#34;fallback&#34;,
message=&#34;Add fallback credentials (@profile, @p1,@p2, JSON list/dict, or compose:)&#34;,
validate=self.validators.fallback_validation,
default=def_fb_str.replace(&#34;{&#34;, &#34;{{&#34;).replace(&#34;}&#34;, &#34;}}&#34;),
)
]
fba = inquirer.prompt(fbq)
if fba is None:
return False
answer[&#34;fallback&#34;] = _parse_fallback_input(fba[&#34;fallback&#34;])
if &#34;tags&#34; in answer and answer[&#34;tags&#34;] and not str(answer[&#34;tags&#34;]).startswith(&#34;@&#34;):
answer[&#34;tags&#34;] = _parse_tags_input(answer[&#34;tags&#34;])
result = {**uniques, **answer, **node} result = {**uniques, **answer, **node}
result[&#34;type&#34;] = &#34;connection&#34; result[&#34;type&#34;] = &#34;connection&#34;
@@ -607,11 +690,13 @@ el.replaceWith(d);
defaults[&#34;tags&#34;] = &#34;&#34; defaults[&#34;tags&#34;] = &#34;&#34;
if &#34;jumphost&#34; not in defaults: if &#34;jumphost&#34; not in defaults:
defaults[&#34;jumphost&#34;] = &#34;&#34; defaults[&#34;jumphost&#34;] = &#34;&#34;
except Exception: if &#34;fallback&#34; not in defaults:
defaults = {&#34;host&#34;: &#34;&#34;, &#34;protocol&#34;: &#34;&#34;, &#34;port&#34;: &#34;&#34;, &#34;user&#34;: &#34;&#34;, &#34;options&#34;: &#34;&#34;, &#34;logs&#34;: &#34;&#34;, &#34;tags&#34;: &#34;&#34;, &#34;jumphost&#34;: &#34;&#34;} defaults[&#34;fallback&#34;] = &#34;&#34;
except Exception: # noqa: BLE001
defaults = {&#34;host&#34;: &#34;&#34;, &#34;protocol&#34;: &#34;&#34;, &#34;port&#34;: &#34;&#34;, &#34;user&#34;: &#34;&#34;, &#34;options&#34;: &#34;&#34;, &#34;logs&#34;: &#34;&#34;, &#34;tags&#34;: &#34;&#34;, &#34;jumphost&#34;: &#34;&#34;, &#34;fallback&#34;: &#34;&#34;}
profile = {} profile = {}
if edit is None: if edit is None:
edit = {&#34;host&#34;: True, &#34;protocol&#34;: True, &#34;port&#34;: True, &#34;user&#34;: True, &#34;password&#34;: True, &#34;options&#34;: True, &#34;logs&#34;: True, &#34;tags&#34;: True, &#34;jumphost&#34;: True} edit = {&#34;host&#34;: True, &#34;protocol&#34;: True, &#34;port&#34;: True, &#34;user&#34;: True, &#34;password&#34;: True, &#34;options&#34;: True, &#34;logs&#34;: True, &#34;tags&#34;: True, &#34;jumphost&#34;: True, &#34;fallback&#34;: True}
questions = [] questions = []
if edit[&#34;host&#34;]: if edit[&#34;host&#34;]:
questions.append(inquirer.Text(&#34;host&#34;, message=&#34;Add Hostname or IP&#34;, default=defaults[&#34;host&#34;])) questions.append(inquirer.Text(&#34;host&#34;, message=&#34;Add Hostname or IP&#34;, default=defaults[&#34;host&#34;]))
@@ -649,17 +734,24 @@ el.replaceWith(d);
questions.append(inquirer.Password(&#34;password&#34;, message=&#34;Set Password&#34;)) questions.append(inquirer.Password(&#34;password&#34;, message=&#34;Set Password&#34;))
else: else:
profile[&#34;password&#34;] = defaults[&#34;password&#34;] profile[&#34;password&#34;] = defaults[&#34;password&#34;]
if edit.get(&#34;fallback&#34;):
raw_def_fb = defaults.get(&#34;fallback&#34;, &#34;&#34;)
def_fb_str = json.dumps(raw_def_fb) if isinstance(raw_def_fb, (list, dict)) else str(raw_def_fb or &#34;&#34;)
questions.append(inquirer.Text(&#34;fallback&#34;, message=&#34;Add fallback credentials (@profile, @p1,@p2, JSON list/dict, or compose:)&#34;, validate=self.validators.fallback_validation, default=def_fb_str.replace(&#34;{&#34;, &#34;{{&#34;).replace(&#34;}&#34;, &#34;}}&#34;)))
else:
profile[&#34;fallback&#34;] = defaults.get(&#34;fallback&#34;, &#34;&#34;)
answer = inquirer.prompt(questions) answer = inquirer.prompt(questions)
if answer is None: if answer is None:
return False return False
if &#34;password&#34; in answer: if &#34;password&#34; in answer and answer[&#34;password&#34;] != &#34;&#34;:
if answer[&#34;password&#34;] != &#34;&#34;:
answer[&#34;password&#34;] = self.app.services.config_svc.encrypt_password(answer[&#34;password&#34;]) answer[&#34;password&#34;] = self.app.services.config_svc.encrypt_password(answer[&#34;password&#34;])
if &#34;tags&#34; in answer and answer[&#34;tags&#34;]: if &#34;tags&#34; in answer and answer[&#34;tags&#34;]:
answer[&#34;tags&#34;] = ast.literal_eval(answer[&#34;tags&#34;]) answer[&#34;tags&#34;] = _parse_tags_input(answer[&#34;tags&#34;])
if &#34;fallback&#34; in answer:
answer[&#34;fallback&#34;] = _parse_fallback_input(answer[&#34;fallback&#34;])
result = {**answer, **profile} result = {**answer, **profile}
result[&#34;id&#34;] = unique result[&#34;id&#34;] = unique
+2
View File
@@ -193,6 +193,7 @@ el.replaceWith(d);
uniques = self.app.services.nodes.explode_unique(args.data) uniques = self.app.services.nodes.explode_unique(args.data)
if not uniques: if not uniques:
raise InvalidConfigurationError(f&#34;Invalid folder {args.data}&#34;) raise InvalidConfigurationError(f&#34;Invalid folder {args.data}&#34;)
self.app.services.nodes.validate_parent_folder(args.data, is_folder=True)
self.app.services.nodes.add_node(args.data, {}, is_folder=True) self.app.services.nodes.add_node(args.data, {}, is_folder=True)
printer.success(f&#34;{args.data} added successfully&#34;) printer.success(f&#34;{args.data} added successfully&#34;)
else: else:
@@ -364,6 +365,7 @@ def forms(self):
uniques = self.app.services.nodes.explode_unique(args.data) uniques = self.app.services.nodes.explode_unique(args.data)
if not uniques: if not uniques:
raise InvalidConfigurationError(f&#34;Invalid folder {args.data}&#34;) raise InvalidConfigurationError(f&#34;Invalid folder {args.data}&#34;)
self.app.services.nodes.validate_parent_folder(args.data, is_folder=True)
self.app.services.nodes.add_node(args.data, {}, is_folder=True) self.app.services.nodes.add_node(args.data, {}, is_folder=True)
printer.success(f&#34;{args.data} added successfully&#34;) printer.success(f&#34;{args.data} added successfully&#34;)
else: else:
+227 -50
View File
@@ -106,50 +106,119 @@ el.replaceWith(d);
return True return True
def pass_validation(self, answers, current, regex = &#34;(^@.+$)&#34;): def pass_validation(self, answers, current, regex = &#34;(^@.+$)&#34;):
if is_compose_expression(current):
try:
validate_compose_syntax(current)
except SanitizedAuthError as exc:
_raise_val_err(str(exc))
return True
profiles = current.split(&#34;,&#34;) profiles = current.split(&#34;,&#34;)
for i in profiles: for i in profiles:
if not re.match(regex, i) or i[1:] not in self.app.profiles: if not re.match(regex, i) or i[1:] not in self.app.profiles:
_raise_val_err(&#34;Profile {} don&#39;t exist&#34;.format(i)) _raise_val_err(&#34;Profile {} don&#39;t exist&#34;.format(i))
return True return True
def _validate_tags_dict(self, current: str):
if len(current.encode(&#34;utf-8&#34;, errors=&#34;replace&#34;)) &gt; MAX_JSON_INPUT_BYTES:
_raise_val_err(f&#34;Tags exceed maximum size of {MAX_JSON_INPUT_BYTES} bytes&#34;)
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(&#34;Tags should be a python dictionary.&#34;)
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): def tags_validation(self, answers, current):
if current.startswith(&#34;@&#34;): if current.startswith(&#34;@&#34;):
if current[1:] not in self.app.profiles: if current[1:] not in self.app.profiles:
_raise_val_err(&#34;Profile {} don&#39;t exist&#34;.format(current)) _raise_val_err(&#34;Profile {} don&#39;t exist&#34;.format(current))
elif current != &#34;&#34;: elif current != &#34;&#34;:
isdict = False return self._validate_tags_dict(current)
try:
isdict = ast.literal_eval(current)
except Exception:
pass
if not isinstance (isdict, dict):
_raise_val_err(&#34;Tags should be a python dictionary.&#34;.format(current))
return True return True
def profile_tags_validation(self, answers, current): def profile_tags_validation(self, answers, current):
if current != &#34;&#34;: if current != &#34;&#34;:
isdict = False return self._validate_tags_dict(current)
try:
isdict = ast.literal_eval(current)
except Exception:
pass
if not isinstance (isdict, dict):
_raise_val_err(&#34;Tags should be a python dictionary.&#34;.format(current))
return True return True
def fallback_validation(self, answers, current):
if current in (None, &#34;&#34;):
return True
stripped = str(current).strip()
if not stripped:
return True
if len(stripped.encode(&#34;utf-8&#34;, errors=&#34;replace&#34;)) &gt; MAX_JSON_INPUT_BYTES:
_raise_val_err(f&#34;Fallback exceeds maximum size of {MAX_JSON_INPUT_BYTES} bytes&#34;)
if stripped.startswith(&#34;@&#34;) and not stripped.startswith(&#34;@@&#34;) and &#34;+&#34; not in stripped:
parts = [p.strip() for p in stripped.split(&#34;,&#34;)]
if any(not p for p in parts):
_raise_val_err(&#34;Fallback profile entry cannot be empty&#34;)
if len(parts) &gt; MAX_FALLBACK_ENTRIES:
_raise_val_err(f&#34;Fallback exceeds maximum of {MAX_FALLBACK_ENTRIES} entries&#34;)
for p in parts:
if not p.startswith(&#34;@&#34;) or not re.fullmatch(r&#34;[A-Za-z0-9_.$:/-]+&#34;, p[1:]):
_raise_val_err(
&#34;Comma-separated fallback entries must be valid @profile references&#34;
)
if p[1:] not in self.app.profiles:
_raise_val_err(f&#34;Profile {p} don&#39;t exist&#34;)
return True
try:
if is_compose_expression(stripped):
validate_compose_syntax(stripped)
return True
if stripped.startswith((&#34;[&#34;, &#34;{&#34;)):
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 _get_all_valid_nodes(self) -&gt; list[str]:
valid_nodes = list(getattr(self.app, &#34;nodes_list&#34;, []) or [])
if hasattr(self.app, &#34;services&#34;) and hasattr(self.app.services, &#34;nodes&#34;):
raw_fn = getattr(self.app.services.nodes, &#34;_get_raw_nodes&#34;, None)
if callable(raw_fn) and not hasattr(raw_fn, &#34;assert_called&#34;):
raw_res = raw_fn()
if isinstance(raw_res, (list, tuple, set)):
valid_nodes = list(dict.fromkeys([*raw_res, *valid_nodes]))
return valid_nodes
def _node_exists(self, target: str) -&gt; bool:
valid_nodes = self._get_all_valid_nodes()
if getattr(self.app, &#34;case&#34;, True):
return target in valid_nodes
return target.lower() in {n.lower() for n in valid_nodes}
def jumphost_validation(self, answers, current): def jumphost_validation(self, answers, current):
if current.startswith(&#34;@&#34;): if current.startswith(&#34;@&#34;):
if current[1:] not in self.app.profiles: if current[1:] not in self.app.profiles:
_raise_val_err(&#34;Profile {} don&#39;t exist&#34;.format(current)) _raise_val_err(f&#34;Profile {current} don&#39;t exist&#34;)
elif current != &#34;&#34;: elif current != &#34;&#34; and not self._node_exists(current):
if current not in self.app.nodes_list: _raise_val_err(f&#34;Node {current} don&#39;t exist.&#34;)
_raise_val_err(&#34;Node {} don&#39;t exist.&#34;.format(current))
return True return True
def profile_jumphost_validation(self, answers, current): def profile_jumphost_validation(self, answers, current):
if current != &#34;&#34;: if current != &#34;&#34; and not self._node_exists(current):
if current not in self.app.nodes_list: _raise_val_err(f&#34;Node {current} don&#39;t exist.&#34;)
_raise_val_err(&#34;Node {} don&#39;t exist.&#34;.format(current))
return True return True
def default_validation(self, answers, current): def default_validation(self, answers, current):
@@ -167,12 +236,46 @@ el.replaceWith(d);
return True return True
def bulk_folder_validation(self, answers, current): def bulk_folder_validation(self, answers, current):
if not self.app.case: case = getattr(self.app, &#34;case&#34;, False)
if not case:
current = current.lower() current = current.lower()
candidate = current candidate = current.split(&#34;/&#34;)[0] if &#34;/&#34; in current else current
if &#34;/&#34; in current:
candidate = current.split(&#34;/&#34;)[0] if hasattr(self.app, &#34;services&#34;) and hasattr(self.app.services, &#34;context&#34;):
raw_folders = list(getattr(self.app, &#34;folders&#34;, []) or [])
if hasattr(self.app.services, &#34;nodes&#34;):
raw_fn = getattr(self.app.services.nodes, &#34;_get_raw_folders&#34;, None)
if callable(raw_fn) and not hasattr(raw_fn, &#34;assert_called&#34;):
raw_res = raw_fn()
if isinstance(raw_res, (list, tuple, set)):
raw_folders = list(raw_res)
def _folder_exists_norm(folder_name: str) -&gt; bool:
if case:
return folder_name in raw_folders
return folder_name.lower() in {f.lower() for f in raw_folders}
if current != &#34;&#34; and not _folder_exists_norm(candidate):
_raise_val_err(f&#34;Location {current} don&#39;t exist&#34;)
ctx_svc = self.app.services.context
if ctx_svc is not None and not ctx_svc.is_default_all_context():
raw_ids = answers.get(&#34;ids&#34;, &#34;&#34;) if isinstance(answers, dict) else &#34;&#34;
for nid in str(raw_ids).split(&#34;,&#34;):
nid_stripped = nid.strip()
if not nid_stripped:
continue
nid_norm = nid_stripped if case else nid_stripped.lower()
full_id = f&#34;{nid_norm}{candidate}&#34; if candidate else nid_norm
if not ctx_svc.matches_active_context(full_id):
_raise_val_err(
f&#34;Node &#39;{full_id}&#39; does not match active context &#39;{ctx_svc.current_context}&#39;.&#34;
)
parent = full_id.partition(&#34;@&#34;)[2]
if parent and not _folder_exists_norm(f&#34;@{parent}&#34;):
_raise_val_err(f&#34;Location @{parent} don&#39;t exist&#34;)
return True
matches = list(filter(lambda k: k == candidate, self.app.folders)) matches = list(filter(lambda k: k == candidate, self.app.folders))
if current != &#34;&#34; and len(matches) == 0: if current != &#34;&#34; and len(matches) == 0:
@@ -203,12 +306,46 @@ el.replaceWith(d);
<span>Expand source code</span> <span>Expand source code</span>
</summary> </summary>
<pre><code class="python">def bulk_folder_validation(self, answers, current): <pre><code class="python">def bulk_folder_validation(self, answers, current):
if not self.app.case: case = getattr(self.app, &#34;case&#34;, False)
if not case:
current = current.lower() current = current.lower()
candidate = current candidate = current.split(&#34;/&#34;)[0] if &#34;/&#34; in current else current
if &#34;/&#34; in current:
candidate = current.split(&#34;/&#34;)[0] if hasattr(self.app, &#34;services&#34;) and hasattr(self.app.services, &#34;context&#34;):
raw_folders = list(getattr(self.app, &#34;folders&#34;, []) or [])
if hasattr(self.app.services, &#34;nodes&#34;):
raw_fn = getattr(self.app.services.nodes, &#34;_get_raw_folders&#34;, None)
if callable(raw_fn) and not hasattr(raw_fn, &#34;assert_called&#34;):
raw_res = raw_fn()
if isinstance(raw_res, (list, tuple, set)):
raw_folders = list(raw_res)
def _folder_exists_norm(folder_name: str) -&gt; bool:
if case:
return folder_name in raw_folders
return folder_name.lower() in {f.lower() for f in raw_folders}
if current != &#34;&#34; and not _folder_exists_norm(candidate):
_raise_val_err(f&#34;Location {current} don&#39;t exist&#34;)
ctx_svc = self.app.services.context
if ctx_svc is not None and not ctx_svc.is_default_all_context():
raw_ids = answers.get(&#34;ids&#34;, &#34;&#34;) if isinstance(answers, dict) else &#34;&#34;
for nid in str(raw_ids).split(&#34;,&#34;):
nid_stripped = nid.strip()
if not nid_stripped:
continue
nid_norm = nid_stripped if case else nid_stripped.lower()
full_id = f&#34;{nid_norm}{candidate}&#34; if candidate else nid_norm
if not ctx_svc.matches_active_context(full_id):
_raise_val_err(
f&#34;Node &#39;{full_id}&#39; does not match active context &#39;{ctx_svc.current_context}&#39;.&#34;
)
parent = full_id.partition(&#34;@&#34;)[2]
if parent and not _folder_exists_norm(f&#34;@{parent}&#34;):
_raise_val_err(f&#34;Location @{parent} don&#39;t exist&#34;)
return True
matches = list(filter(lambda k: k == candidate, self.app.folders)) matches = list(filter(lambda k: k == candidate, self.app.folders))
if current != &#34;&#34; and len(matches) == 0: if current != &#34;&#34; and len(matches) == 0:
@@ -273,6 +410,53 @@ el.replaceWith(d);
</details> </details>
<div class="desc"></div> <div class="desc"></div>
</dd> </dd>
<dt id="connpy.cli.validators.Validators.fallback_validation"><code class="name flex">
<span>def <span class="ident">fallback_validation</span></span>(<span>self, answers, current)</span>
</code></dt>
<dd>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def fallback_validation(self, answers, current):
if current in (None, &#34;&#34;):
return True
stripped = str(current).strip()
if not stripped:
return True
if len(stripped.encode(&#34;utf-8&#34;, errors=&#34;replace&#34;)) &gt; MAX_JSON_INPUT_BYTES:
_raise_val_err(f&#34;Fallback exceeds maximum size of {MAX_JSON_INPUT_BYTES} bytes&#34;)
if stripped.startswith(&#34;@&#34;) and not stripped.startswith(&#34;@@&#34;) and &#34;+&#34; not in stripped:
parts = [p.strip() for p in stripped.split(&#34;,&#34;)]
if any(not p for p in parts):
_raise_val_err(&#34;Fallback profile entry cannot be empty&#34;)
if len(parts) &gt; MAX_FALLBACK_ENTRIES:
_raise_val_err(f&#34;Fallback exceeds maximum of {MAX_FALLBACK_ENTRIES} entries&#34;)
for p in parts:
if not p.startswith(&#34;@&#34;) or not re.fullmatch(r&#34;[A-Za-z0-9_.$:/-]+&#34;, p[1:]):
_raise_val_err(
&#34;Comma-separated fallback entries must be valid @profile references&#34;
)
if p[1:] not in self.app.profiles:
_raise_val_err(f&#34;Profile {p} don&#39;t exist&#34;)
return True
try:
if is_compose_expression(stripped):
validate_compose_syntax(stripped)
return True
if stripped.startswith((&#34;[&#34;, &#34;{&#34;)):
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</code></pre>
</details>
<div class="desc"></div>
</dd>
<dt id="connpy.cli.validators.Validators.host_validation"><code class="name flex"> <dt id="connpy.cli.validators.Validators.host_validation"><code class="name flex">
<span>def <span class="ident">host_validation</span></span>(<span>self, answers, current, regex='^.+$')</span> <span>def <span class="ident">host_validation</span></span>(<span>self, answers, current, regex='^.+$')</span>
</code></dt> </code></dt>
@@ -302,10 +486,9 @@ el.replaceWith(d);
<pre><code class="python">def jumphost_validation(self, answers, current): <pre><code class="python">def jumphost_validation(self, answers, current):
if current.startswith(&#34;@&#34;): if current.startswith(&#34;@&#34;):
if current[1:] not in self.app.profiles: if current[1:] not in self.app.profiles:
_raise_val_err(&#34;Profile {} don&#39;t exist&#34;.format(current)) _raise_val_err(f&#34;Profile {current} don&#39;t exist&#34;)
elif current != &#34;&#34;: elif current != &#34;&#34; and not self._node_exists(current):
if current not in self.app.nodes_list: _raise_val_err(f&#34;Node {current} don&#39;t exist.&#34;)
_raise_val_err(&#34;Node {} don&#39;t exist.&#34;.format(current))
return True</code></pre> return True</code></pre>
</details> </details>
<div class="desc"></div> <div class="desc"></div>
@@ -319,6 +502,12 @@ el.replaceWith(d);
<span>Expand source code</span> <span>Expand source code</span>
</summary> </summary>
<pre><code class="python">def pass_validation(self, answers, current, regex = &#34;(^@.+$)&#34;): <pre><code class="python">def pass_validation(self, answers, current, regex = &#34;(^@.+$)&#34;):
if is_compose_expression(current):
try:
validate_compose_syntax(current)
except SanitizedAuthError as exc:
_raise_val_err(str(exc))
return True
profiles = current.split(&#34;,&#34;) profiles = current.split(&#34;,&#34;)
for i in profiles: for i in profiles:
if not re.match(regex, i) or i[1:] not in self.app.profiles: if not re.match(regex, i) or i[1:] not in self.app.profiles:
@@ -360,9 +549,8 @@ el.replaceWith(d);
<span>Expand source code</span> <span>Expand source code</span>
</summary> </summary>
<pre><code class="python">def profile_jumphost_validation(self, answers, current): <pre><code class="python">def profile_jumphost_validation(self, answers, current):
if current != &#34;&#34;: if current != &#34;&#34; and not self._node_exists(current):
if current not in self.app.nodes_list: _raise_val_err(f&#34;Node {current} don&#39;t exist.&#34;)
_raise_val_err(&#34;Node {} don&#39;t exist.&#34;.format(current))
return True</code></pre> return True</code></pre>
</details> </details>
<div class="desc"></div> <div class="desc"></div>
@@ -413,13 +601,7 @@ el.replaceWith(d);
</summary> </summary>
<pre><code class="python">def profile_tags_validation(self, answers, current): <pre><code class="python">def profile_tags_validation(self, answers, current):
if current != &#34;&#34;: if current != &#34;&#34;:
isdict = False return self._validate_tags_dict(current)
try:
isdict = ast.literal_eval(current)
except Exception:
pass
if not isinstance (isdict, dict):
_raise_val_err(&#34;Tags should be a python dictionary.&#34;.format(current))
return True</code></pre> return True</code></pre>
</details> </details>
<div class="desc"></div> <div class="desc"></div>
@@ -455,13 +637,7 @@ el.replaceWith(d);
if current[1:] not in self.app.profiles: if current[1:] not in self.app.profiles:
_raise_val_err(&#34;Profile {} don&#39;t exist&#34;.format(current)) _raise_val_err(&#34;Profile {} don&#39;t exist&#34;.format(current))
elif current != &#34;&#34;: elif current != &#34;&#34;:
isdict = False return self._validate_tags_dict(current)
try:
isdict = ast.literal_eval(current)
except Exception:
pass
if not isinstance (isdict, dict):
_raise_val_err(&#34;Tags should be a python dictionary.&#34;.format(current))
return True</code></pre> return True</code></pre>
</details> </details>
<div class="desc"></div> <div class="desc"></div>
@@ -490,6 +666,7 @@ el.replaceWith(d);
<li><code><a title="connpy.cli.validators.Validators.bulk_host_validation" href="#connpy.cli.validators.Validators.bulk_host_validation">bulk_host_validation</a></code></li> <li><code><a title="connpy.cli.validators.Validators.bulk_host_validation" href="#connpy.cli.validators.Validators.bulk_host_validation">bulk_host_validation</a></code></li>
<li><code><a title="connpy.cli.validators.Validators.bulk_node_validation" href="#connpy.cli.validators.Validators.bulk_node_validation">bulk_node_validation</a></code></li> <li><code><a title="connpy.cli.validators.Validators.bulk_node_validation" href="#connpy.cli.validators.Validators.bulk_node_validation">bulk_node_validation</a></code></li>
<li><code><a title="connpy.cli.validators.Validators.default_validation" href="#connpy.cli.validators.Validators.default_validation">default_validation</a></code></li> <li><code><a title="connpy.cli.validators.Validators.default_validation" href="#connpy.cli.validators.Validators.default_validation">default_validation</a></code></li>
<li><code><a title="connpy.cli.validators.Validators.fallback_validation" href="#connpy.cli.validators.Validators.fallback_validation">fallback_validation</a></code></li>
<li><code><a title="connpy.cli.validators.Validators.host_validation" href="#connpy.cli.validators.Validators.host_validation">host_validation</a></code></li> <li><code><a title="connpy.cli.validators.Validators.host_validation" href="#connpy.cli.validators.Validators.host_validation">host_validation</a></code></li>
<li><code><a title="connpy.cli.validators.Validators.jumphost_validation" href="#connpy.cli.validators.Validators.jumphost_validation">jumphost_validation</a></code></li> <li><code><a title="connpy.cli.validators.Validators.jumphost_validation" href="#connpy.cli.validators.Validators.jumphost_validation">jumphost_validation</a></code></li>
<li><code><a title="connpy.cli.validators.Validators.pass_validation" href="#connpy.cli.validators.Validators.pass_validation">pass_validation</a></code></li> <li><code><a title="connpy.cli.validators.Validators.pass_validation" href="#connpy.cli.validators.Validators.pass_validation">pass_validation</a></code></li>
+65 -43
View File
@@ -55,6 +55,7 @@ el.replaceWith(d);
<pre><code class="python">def handle_errors(func): <pre><code class="python">def handle_errors(func):
import inspect import inspect
if inspect.isgeneratorfunction(func): if inspect.isgeneratorfunction(func):
@functools.wraps(func)
def wrapper(*args, **kwargs): def wrapper(*args, **kwargs):
try: try:
for item in func(*args, **kwargs): for item in func(*args, **kwargs):
@@ -75,6 +76,7 @@ el.replaceWith(d);
printer.clear_thread_state() printer.clear_thread_state()
return wrapper return wrapper
else: else:
@functools.wraps(func)
def wrapper(*args, **kwargs): def wrapper(*args, **kwargs):
try: try:
return func(*args, **kwargs) return func(*args, **kwargs)
@@ -108,7 +110,7 @@ el.replaceWith(d);
from connpy.grpc_layer.user_registry import UserRegistry from connpy.grpc_layer.user_registry import UserRegistry
from connpy.services.provider import ServiceProvider from connpy.services.provider import ServiceProvider
fallback_provider = ServiceProvider(config, mode=&#34;local&#34;) fallback_provider = ServiceProvider(config, mode=&#34;local&#34;, enforce_context=False)
registry = UserRegistry(config.defaultdir) registry = UserRegistry(config.defaultdir)
# Check if trusted_gateway provider is configured if SSO Gateway Secret is present in env # Check if trusted_gateway provider is configured if SSO Gateway Secret is present in env
@@ -161,9 +163,7 @@ el.replaceWith(d);
</summary> </summary>
<pre><code class="python">class AIServicer(connpy_pb2_grpc.AIServiceServicer): <pre><code class="python">class AIServicer(connpy_pb2_grpc.AIServiceServicer):
def __init__(self, provider, registry=None, debug=False): def __init__(self, provider, registry=None, debug=False):
if not hasattr(provider, &#34;mode&#34;): provider = _ensure_server_provider(provider)
from connpy.services.provider import ServiceProvider
provider = ServiceProvider(provider, mode=&#34;local&#34;)
self._fallback_provider = provider self._fallback_provider = provider
self._registry = registry self._registry = registry
self.server_debug = debug self.server_debug = debug
@@ -925,9 +925,7 @@ interceptor chooses to service this RPC, or None otherwise.</p></div>
</summary> </summary>
<pre><code class="python">class ConfigServicer(connpy_pb2_grpc.ConfigServiceServicer): <pre><code class="python">class ConfigServicer(connpy_pb2_grpc.ConfigServiceServicer):
def __init__(self, provider, registry=None): def __init__(self, provider, registry=None):
if not hasattr(provider, &#34;mode&#34;): provider = _ensure_server_provider(provider)
from connpy.services.provider import ServiceProvider
provider = ServiceProvider(provider, mode=&#34;local&#34;)
self._fallback_provider = provider self._fallback_provider = provider
self._registry = registry self._registry = registry
@@ -1013,9 +1011,7 @@ def service(self):
</summary> </summary>
<pre><code class="python">class ExecutionServicer(connpy_pb2_grpc.ExecutionServiceServicer): <pre><code class="python">class ExecutionServicer(connpy_pb2_grpc.ExecutionServiceServicer):
def __init__(self, provider, registry=None): def __init__(self, provider, registry=None):
if not hasattr(provider, &#34;mode&#34;): provider = _ensure_server_provider(provider)
from connpy.services.provider import ServiceProvider
provider = ServiceProvider(provider, mode=&#34;local&#34;)
self._fallback_provider = provider self._fallback_provider = provider
self._registry = registry self._registry = registry
@@ -1184,9 +1180,7 @@ def service(self):
</summary> </summary>
<pre><code class="python">class ImportExportServicer(connpy_pb2_grpc.ImportExportServiceServicer): <pre><code class="python">class ImportExportServicer(connpy_pb2_grpc.ImportExportServiceServicer):
def __init__(self, provider, registry=None): def __init__(self, provider, registry=None):
if not hasattr(provider, &#34;mode&#34;): provider = _ensure_server_provider(provider)
from connpy.services.provider import ServiceProvider
provider = ServiceProvider(provider, mode=&#34;local&#34;)
self._fallback_provider = provider self._fallback_provider = provider
self._registry = registry self._registry = registry
@@ -1372,9 +1366,7 @@ interceptor chooses to service this RPC, or None otherwise.</p></div>
</summary> </summary>
<pre><code class="python">class NodeServicer(connpy_pb2_grpc.NodeServiceServicer): <pre><code class="python">class NodeServicer(connpy_pb2_grpc.NodeServiceServicer):
def __init__(self, provider, registry=None, debug=False): def __init__(self, provider, registry=None, debug=False):
if not hasattr(provider, &#34;mode&#34;): provider = _ensure_server_provider(provider)
from connpy.services.provider import ServiceProvider
provider = ServiceProvider(provider, mode=&#34;local&#34;)
self._fallback_provider = provider self._fallback_provider = provider
self._registry = registry self._registry = registry
self.server_debug = debug self.server_debug = debug
@@ -1425,8 +1417,24 @@ interceptor chooses to service this RPC, or None otherwise.</p></div>
self.server_console.print(f&#34;[debug][DEBUG][/debug] gRPC interact_node request for: [bold cyan]{unique_id}[/bold cyan]&#34;) self.server_console.print(f&#34;[debug][DEBUG][/debug] gRPC interact_node request for: [bold cyan]{unique_id}[/bold cyan]&#34;)
if first_req.connection_params_json: if first_req.connection_params_json:
import json try:
params = json.loads(first_req.connection_params_json) 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(&#34;connection_params_json must be a JSON object&#34;)
if &#34;fallback&#34; in params and params[&#34;fallback&#34;] not in (None, &#34;&#34;, []):
validate_fallback_entries(
params[&#34;fallback&#34;],
max_entries=MAX_FALLBACK_ENTRIES,
)
if &#34;tags&#34; in params and params[&#34;tags&#34;] not in (&#34;&#34;, None):
validate_auth_budget_tags(params[&#34;tags&#34;])
except (ValueError, TypeError, SanitizedAuthError) as exc:
context.abort(grpc.StatusCode.INVALID_ARGUMENT, str(exc))
base_node_id = params.get(&#34;base_node&#34;) base_node_id = params.get(&#34;base_node&#34;)
# Valid attributes that a node object accepts # Valid attributes that a node object accepts
valid_attrs = [&#39;host&#39;, &#39;options&#39;, &#39;logs&#39;, &#39;password&#39;, &#39;port&#39;, &#39;protocol&#39;, &#39;user&#39;, &#39;jumphost&#39;] valid_attrs = [&#39;host&#39;, &#39;options&#39;, &#39;logs&#39;, &#39;password&#39;, &#39;port&#39;, &#39;protocol&#39;, &#39;user&#39;, &#39;jumphost&#39;]
@@ -1444,6 +1452,8 @@ interceptor chooses to service this RPC, or None otherwise.</p></div>
for attr in valid_attrs: for attr in valid_attrs:
if attr in params: if attr in params:
device[attr] = params[attr] device[attr] = params[attr]
if &#34;fallback&#34; in params:
device[&#34;fallback&#34;] = params[&#34;fallback&#34;]
if &#34;tags&#34; in params: if &#34;tags&#34; in params:
device_tags = device.get(&#34;tags&#34;, {}) device_tags = device.get(&#34;tags&#34;, {})
@@ -1457,20 +1467,30 @@ interceptor chooses to service this RPC, or None otherwise.</p></div>
else: else:
# base_node not found, fall back to dynamic # base_node not found, fall back to dynamic
node_name = params.get(&#34;name&#34;, fallback_id) node_name = params.get(&#34;name&#34;, fallback_id)
n = node(node_name, host=params.get(&#34;host&#34;, &#34;&#34;), config=user_config) dyn_kwargs = {k: params[k] for k in valid_attrs if k in params and k != &#34;host&#34;}
for attr in valid_attrs: if &#34;fallback&#34; in params:
if attr in params: dyn_kwargs[&#34;fallback&#34;] = params[&#34;fallback&#34;]
setattr(n, attr, params[attr])
if &#34;tags&#34; in params: if &#34;tags&#34; in params:
n.tags = params[&#34;tags&#34;] dyn_kwargs[&#34;tags&#34;] = params[&#34;tags&#34;]
n = node(
node_name,
host=params.get(&#34;host&#34;, &#34;&#34;),
config=user_config,
**dyn_kwargs,
)
else: else:
node_name = params.get(&#34;name&#34;, fallback_id) node_name = params.get(&#34;name&#34;, fallback_id)
n = node(node_name, host=params.get(&#34;host&#34;, &#34;&#34;), config=user_config) dyn_kwargs = {k: params[k] for k in valid_attrs if k in params and k != &#34;host&#34;}
for attr in valid_attrs: if &#34;fallback&#34; in params:
if attr in params: dyn_kwargs[&#34;fallback&#34;] = params[&#34;fallback&#34;]
setattr(n, attr, params[attr])
if &#34;tags&#34; in params: if &#34;tags&#34; in params:
n.tags = params[&#34;tags&#34;] dyn_kwargs[&#34;tags&#34;] = params[&#34;tags&#34;]
n = node(
node_name,
host=params.get(&#34;host&#34;, &#34;&#34;),
config=user_config,
**dyn_kwargs,
)
n._orig_protocol = n.protocol n._orig_protocol = n.protocol
if sftp: if sftp:
n.protocol = &#34;sftp&#34; n.protocol = &#34;sftp&#34;
@@ -1491,9 +1511,11 @@ interceptor chooses to service this RPC, or None otherwise.</p></div>
debug_chunks = [] debug_chunks = []
if debug: if debug:
from io import StringIO from io import StringIO
from rich.console import Console as RichConsole from rich.console import Console as RichConsole
from ..printer import connpy_theme
from .. import printer as _printer from .. import printer as _printer
from ..printer import connpy_theme
def remote_logger(msg_type, message): def remote_logger(msg_type, message):
buf = StringIO() buf = StringIO()
@@ -1514,7 +1536,14 @@ interceptor chooses to service this RPC, or None otherwise.</p></div>
else: else:
remote_logger = None remote_logger = None
connect = n._connect(debug=debug, logger=remote_logger) try:
connect = n._connect(
debug=debug,
logger=remote_logger,
attempt_tracker=getattr(provider, &#34;auth_tracker&#34;, None),
)
except Exception as exc: # noqa: BLE001
connect = str(sanitize_exception(exc, code=&#34;AUTH_FAILED&#34;))
# Send debug output to client before checking result (always show the command) # Send debug output to client before checking result (always show the command)
for chunk in debug_chunks: for chunk in debug_chunks:
@@ -1845,10 +1874,9 @@ interceptor chooses to service this RPC, or None otherwise.</p></div>
continue continue
asyncio.run(n._async_interact_loop(remote_stream, resize_callback, copilot_handler=remote_copilot_handler)) asyncio.run(n._async_interact_loop(remote_stream, resize_callback, copilot_handler=remote_copilot_handler))
except Exception as e: except Exception as e: # noqa: BLE001
import traceback sanitized_err = sanitize_exception(e, code=&#34;TRANSPORT_ERROR&#34;, context_label=&#34;interact stream&#34;)
print(f&#34;[ERROR in run_async_loop] {e}&#34;) print(f&#34;[ERROR in run_async_loop] {sanitized_err}&#34;)
traceback.print_exc()
finally: finally:
n._teardown_interact_environment() n._teardown_interact_environment()
response_queue.put(None) # Signal EOF response_queue.put(None) # Signal EOF
@@ -2003,9 +2031,7 @@ def service(self):
</summary> </summary>
<pre><code class="python">class PluginServicer(connpy_pb2_grpc.PluginServiceServicer, remote_plugin_pb2_grpc.RemotePluginServiceServicer): <pre><code class="python">class PluginServicer(connpy_pb2_grpc.PluginServiceServicer, remote_plugin_pb2_grpc.RemotePluginServiceServicer):
def __init__(self, provider, registry=None): def __init__(self, provider, registry=None):
if not hasattr(provider, &#34;mode&#34;): provider = _ensure_server_provider(provider)
from connpy.services.provider import ServiceProvider
provider = ServiceProvider(provider, mode=&#34;local&#34;)
self._fallback_provider = provider self._fallback_provider = provider
self._registry = registry self._registry = registry
@@ -2110,9 +2136,7 @@ def service(self):
</summary> </summary>
<pre><code class="python">class ProfileServicer(connpy_pb2_grpc.ProfileServiceServicer): <pre><code class="python">class ProfileServicer(connpy_pb2_grpc.ProfileServiceServicer):
def __init__(self, provider, registry=None): def __init__(self, provider, registry=None):
if not hasattr(provider, &#34;mode&#34;): provider = _ensure_server_provider(provider)
from connpy.services.provider import ServiceProvider
provider = ServiceProvider(provider, mode=&#34;local&#34;)
self._fallback_provider = provider self._fallback_provider = provider
self._registry = registry self._registry = registry
@@ -2413,9 +2437,7 @@ def service(self):
</summary> </summary>
<pre><code class="python">class SystemServicer(connpy_pb2_grpc.SystemServiceServicer): <pre><code class="python">class SystemServicer(connpy_pb2_grpc.SystemServiceServicer):
def __init__(self, provider, registry=None): def __init__(self, provider, registry=None):
if not hasattr(provider, &#34;mode&#34;): provider = _ensure_server_provider(provider)
from connpy.services.provider import ServiceProvider
provider = ServiceProvider(provider, mode=&#34;local&#34;)
self._fallback_provider = provider self._fallback_provider = provider
self._registry = registry self._registry = registry
File diff suppressed because it is too large. Load diff
+2 -2
View File
@@ -113,7 +113,7 @@ el.replaceWith(d);
try: try:
# Attempt a fresh configuration load # Attempt a fresh configuration load
config = configfile(conf=conf_file, shared_config=self._shared_config) config = configfile(conf=conf_file, shared_config=self._shared_config)
new_provider = ServiceProvider(config, mode=&#34;local&#34;) new_provider = ServiceProvider(config, mode=&#34;local&#34;, enforce_context=False)
# Successfully loaded, clean up the old provider # Successfully loaded, clean up the old provider
if old_provider: if old_provider:
@@ -221,7 +221,7 @@ el.replaceWith(d);
try: try:
# Attempt a fresh configuration load # Attempt a fresh configuration load
config = configfile(conf=conf_file, shared_config=self._shared_config) config = configfile(conf=conf_file, shared_config=self._shared_config)
new_provider = ServiceProvider(config, mode=&#34;local&#34;) new_provider = ServiceProvider(config, mode=&#34;local&#34;, enforce_context=False)
# Successfully loaded, clean up the old provider # Successfully loaded, clean up the old provider
if old_provider: if old_provider:
+2937 -674
View File
File diff suppressed because it is too large. Load diff
+145 -86
View File
@@ -162,20 +162,7 @@ el.replaceWith(d);
def _mask_secrets(self, val: Any) -&gt; Any: def _mask_secrets(self, val: Any) -&gt; Any:
&#34;&#34;&#34;Recursively mask passwords, passphrases, secrets, and auth tokens with &#39;***&#39;.&#34;&#34;&#34; &#34;&#34;&#34;Recursively mask passwords, passphrases, secrets, and auth tokens with &#39;***&#39;.&#34;&#34;&#34;
if isinstance(val, dict): return mask_node_secrets(val)
masked = {}
for k, v in val.items():
k_lower = str(k).lower()
if any(sec in k_lower for sec in [&#34;password&#34;, &#34;passphrase&#34;, &#34;secret&#34;, &#34;token&#34;]):
masked[k] = &#34;***&#34;
elif isinstance(v, (dict, list)):
masked[k] = self._mask_secrets(v)
else:
masked[k] = v
return masked
if isinstance(val, list):
return [self._mask_secrets(item) for item in val]
return val
# ------------------------------------------------------------------------- # -------------------------------------------------------------------------
# Level 1: Read-Only Inventory and Context Tools # Level 1: Read-Only Inventory and Context Tools
@@ -234,6 +221,31 @@ el.replaceWith(d);
# Level 2: Execution, Testing, and Playbooks Tools # Level 2: Execution, Testing, and Playbooks Tools
# ------------------------------------------------------------------------- # -------------------------------------------------------------------------
@staticmethod
def _parse_json_list_or_lines(raw_val: list[str] | str, *, allow_empty_str: bool = False) -&gt; list[str]:
if isinstance(raw_val, str):
if len(raw_val.encode(&#34;utf-8&#34;, errors=&#34;replace&#34;)) &gt; MAX_JSON_INPUT_BYTES:
raise ValueError(f&#34;Input exceeds maximum size of {MAX_JSON_INPUT_BYTES} bytes&#34;)
stripped = raw_val.strip()
if stripped.startswith((&#34;[&#34;, &#34;{&#34;)):
parsed = validate_json_depth_and_size(
stripped,
max_bytes=MAX_JSON_INPUT_BYTES,
max_depth=MAX_JSON_INPUT_DEPTH,
)
return parsed if isinstance(parsed, list) else [raw_val]
return ([raw_val] if raw_val else []) if allow_empty_str else [
c.strip() for c in raw_val.splitlines() if c.strip()
]
if isinstance(raw_val, list):
validate_json_depth_and_size(
raw_val,
max_bytes=MAX_JSON_INPUT_BYTES,
max_depth=MAX_JSON_INPUT_DEPTH,
)
return raw_val
return [str(raw_val)] if (raw_val or not allow_empty_str) else []
def run_commands( def run_commands(
self, self,
nodes_filter: str, nodes_filter: str,
@@ -248,17 +260,10 @@ el.replaceWith(d);
if not self.services or not hasattr(self.services, &#34;execution&#34;): if not self.services or not hasattr(self.services, &#34;execution&#34;):
return {&#34;success&#34;: False, &#34;error&#34;: &#34;Execution service unavailable.&#34;} return {&#34;success&#34;: False, &#34;error&#34;: &#34;Execution service unavailable.&#34;}
# Normalize commands list
if isinstance(commands, str):
try: try:
import json commands = self._parse_json_list_or_lines(commands, allow_empty_str=False)
except ValueError as exc:
parsed = json.loads(commands) return {&#34;success&#34;: False, &#34;error&#34;: str(exc)}
commands = parsed if isinstance(parsed, list) else [commands]
except Exception: # noqa: BLE001
commands = [c.strip() for c in commands.splitlines() if c.strip()]
elif not isinstance(commands, list):
commands = [str(commands)]
# Check safety # Check safety
if not self.trust: if not self.trust:
@@ -277,9 +282,24 @@ el.replaceWith(d);
parallel=par_count, parallel=par_count,
timeout=timeout, timeout=timeout,
) )
return {&#34;success&#34;: True, &#34;results&#34;: results} fb_used = False
if isinstance(results, dict):
fb_used = any(
isinstance(v, dict) and bool(v.get(&#34;fallback_used&#34;, False))
for v in results.values()
)
if not fb_used:
fb_map = getattr(self.services.execution, &#34;last_fallback_used&#34;, {})
if isinstance(fb_map, dict):
fb_used = any(bool(v) for v in fb_map.values())
return {&#34;success&#34;: True, &#34;results&#34;: results, &#34;fallback_used&#34;: fb_used}
except Exception as e: # noqa: BLE001 except Exception as e: # noqa: BLE001
return {&#34;success&#34;: False, &#34;error&#34;: str(e)} san = sanitize_exception(
e,
code=&#34;AUTH_FAILED&#34;,
context_label=&#34;command execution failed&#34;,
)
return {&#34;success&#34;: False, &#34;error&#34;: str(san)}
def test_commands( def test_commands(
self, self,
@@ -293,29 +313,11 @@ el.replaceWith(d);
if not self.services or not hasattr(self.services, &#34;execution&#34;): if not self.services or not hasattr(self.services, &#34;execution&#34;):
return {&#34;success&#34;: False, &#34;error&#34;: &#34;Execution service unavailable.&#34;} return {&#34;success&#34;: False, &#34;error&#34;: &#34;Execution service unavailable.&#34;}
# Normalize commands
if isinstance(commands, str):
try: try:
import json commands = self._parse_json_list_or_lines(commands, allow_empty_str=False)
expected = self._parse_json_list_or_lines(expected, allow_empty_str=True)
parsed = json.loads(commands) except ValueError as exc:
commands = parsed if isinstance(parsed, list) else [commands] return {&#34;success&#34;: False, &#34;error&#34;: str(exc)}
except Exception: # noqa: BLE001
commands = [c.strip() for c in commands.splitlines() if c.strip()]
elif not isinstance(commands, list):
commands = [str(commands)]
# Normalize expected
if isinstance(expected, str):
try:
import json
parsed = json.loads(expected)
expected = parsed if isinstance(parsed, list) else ([expected] if expected else [])
except Exception: # noqa: BLE001
expected = [expected] if expected else []
elif not isinstance(expected, list):
expected = [str(expected)] if expected else []
# Check safety # Check safety
if not self.trust: if not self.trust:
@@ -335,9 +337,16 @@ el.replaceWith(d);
parallel=par_count, parallel=par_count,
timeout=timeout, timeout=timeout,
) )
return {&#34;success&#34;: True, &#34;results&#34;: results} fb_map = getattr(self.services.execution, &#34;last_fallback_used&#34;, {})
fb_used = any(bool(v) for v in fb_map.values()) if isinstance(fb_map, dict) else False
return {&#34;success&#34;: True, &#34;results&#34;: results, &#34;fallback_used&#34;: fb_used}
except Exception as e: # noqa: BLE001 except Exception as e: # noqa: BLE001
return {&#34;success&#34;: False, &#34;error&#34;: str(e)} san = sanitize_exception(
e,
code=&#34;AUTH_FAILED&#34;,
context_label=&#34;test execution failed&#34;,
)
return {&#34;success&#34;: False, &#34;error&#34;: str(san)}
def validate_playbook(self, playbook_yaml: str) -&gt; dict: def validate_playbook(self, playbook_yaml: str) -&gt; dict:
&#34;&#34;&#34;Validate the syntax, schema, and tasks of a Connpy YAML playbook.&#34;&#34;&#34; &#34;&#34;&#34;Validate the syntax, schema, and tasks of a Connpy YAML playbook.&#34;&#34;&#34;
@@ -465,6 +474,47 @@ el.replaceWith(d);
# Level 3: Inventory Mutation Tools (CRUD - Requires trust=True) # Level 3: Inventory Mutation Tools (CRUD - Requires trust=True)
# ------------------------------------------------------------------------- # -------------------------------------------------------------------------
@staticmethod
def _validate_and_normalize_node_payload(node_data: dict[str, Any]) -&gt; dict[str, Any]:
validate_json_depth_and_size(
node_data,
max_bytes=MAX_JSON_INPUT_BYTES,
max_depth=MAX_JSON_INPUT_DEPTH,
)
normalized = node_data.copy()
if &#34;tags&#34; in normalized and normalized[&#34;tags&#34;] not in (&#34;&#34;, None):
tags_val = normalized[&#34;tags&#34;]
if isinstance(tags_val, str) and tags_val.strip().startswith(&#34;{&#34;):
tags_val = validate_json_depth_and_size(
tags_val,
max_bytes=MAX_JSON_INPUT_BYTES,
max_depth=MAX_JSON_INPUT_DEPTH,
)
normalized[&#34;tags&#34;] = tags_val
validate_auth_budget_tags(tags_val)
if &#34;fallback&#34; in normalized and normalized[&#34;fallback&#34;] not in (None, &#34;&#34;, []):
fb_val = normalized[&#34;fallback&#34;]
if isinstance(fb_val, str) and fb_val.strip().startswith((&#34;[&#34;, &#34;{&#34;)):
fb_val = validate_json_depth_and_size(
fb_val,
max_bytes=MAX_JSON_INPUT_BYTES,
max_depth=MAX_JSON_INPUT_DEPTH,
)
normalized[&#34;fallback&#34;] = validate_fallback_entries(
fb_val,
max_entries=MAX_FALLBACK_ENTRIES,
)
pw = normalized.get(&#34;password&#34;)
if is_compose_expression(pw):
validate_compose_syntax(pw)
elif isinstance(pw, (list, tuple)):
for item in pw:
if is_compose_expression(item):
validate_compose_syntax(item)
return normalized
def add_node( def add_node(
self, unique_id: str, data: dict | None = None, is_folder: bool = False, **kwargs self, unique_id: str, data: dict | None = None, is_folder: bool = False, **kwargs
) -&gt; dict: ) -&gt; dict:
@@ -482,11 +532,14 @@ el.replaceWith(d);
node_data.update(kwargs) node_data.update(kwargs)
try: try:
node_data = self._validate_and_normalize_node_payload(node_data)
self.services.nodes.add_node(unique_id, node_data, is_folder=is_folder) self.services.nodes.add_node(unique_id, node_data, is_folder=is_folder)
return { return {
&#34;success&#34;: True, &#34;success&#34;: True,
&#34;message&#34;: f&#34;{&#39;Folder&#39; if is_folder else &#39;Node&#39;} &#39;{unique_id}&#39; created successfully.&#34;, &#34;message&#34;: f&#34;{&#39;Folder&#39; if is_folder else &#39;Node&#39;} &#39;{unique_id}&#39; created successfully.&#34;,
} }
except (ValueError, TypeError, SanitizedAuthError) as exc:
return {&#34;success&#34;: False, &#34;error&#34;: str(exc)}
except Exception as e: # noqa: BLE001 except Exception as e: # noqa: BLE001
return {&#34;success&#34;: False, &#34;error&#34;: str(e)} return {&#34;success&#34;: False, &#34;error&#34;: str(e)}
@@ -505,8 +558,11 @@ el.replaceWith(d);
node_updates.update(kwargs) node_updates.update(kwargs)
try: try:
node_updates = self._validate_and_normalize_node_payload(node_updates)
self.services.nodes.update_node(unique_id, node_updates) self.services.nodes.update_node(unique_id, node_updates)
return {&#34;success&#34;: True, &#34;message&#34;: f&#34;Node &#39;{unique_id}&#39; updated successfully.&#34;} return {&#34;success&#34;: True, &#34;message&#34;: f&#34;Node &#39;{unique_id}&#39; updated successfully.&#34;}
except (ValueError, TypeError, SanitizedAuthError) as exc:
return {&#34;success&#34;: False, &#34;error&#34;: str(exc)}
except Exception as e: # noqa: BLE001 except Exception as e: # noqa: BLE001
return {&#34;success&#34;: False, &#34;error&#34;: str(e)} return {&#34;success&#34;: False, &#34;error&#34;: str(e)}
@@ -608,11 +664,14 @@ el.replaceWith(d);
node_data.update(kwargs) node_data.update(kwargs)
try: try:
node_data = self._validate_and_normalize_node_payload(node_data)
self.services.nodes.add_node(unique_id, node_data, is_folder=is_folder) self.services.nodes.add_node(unique_id, node_data, is_folder=is_folder)
return { return {
&#34;success&#34;: True, &#34;success&#34;: True,
&#34;message&#34;: f&#34;{&#39;Folder&#39; if is_folder else &#39;Node&#39;} &#39;{unique_id}&#39; created successfully.&#34;, &#34;message&#34;: f&#34;{&#39;Folder&#39; if is_folder else &#39;Node&#39;} &#39;{unique_id}&#39; created successfully.&#34;,
} }
except (ValueError, TypeError, SanitizedAuthError) as exc:
return {&#34;success&#34;: False, &#34;error&#34;: str(exc)}
except Exception as e: # noqa: BLE001 except Exception as e: # noqa: BLE001
return {&#34;success&#34;: False, &#34;error&#34;: str(e)}</code></pre> return {&#34;success&#34;: False, &#34;error&#34;: str(e)}</code></pre>
</details> </details>
@@ -865,17 +924,10 @@ el.replaceWith(d);
if not self.services or not hasattr(self.services, &#34;execution&#34;): if not self.services or not hasattr(self.services, &#34;execution&#34;):
return {&#34;success&#34;: False, &#34;error&#34;: &#34;Execution service unavailable.&#34;} return {&#34;success&#34;: False, &#34;error&#34;: &#34;Execution service unavailable.&#34;}
# Normalize commands list
if isinstance(commands, str):
try: try:
import json commands = self._parse_json_list_or_lines(commands, allow_empty_str=False)
except ValueError as exc:
parsed = json.loads(commands) return {&#34;success&#34;: False, &#34;error&#34;: str(exc)}
commands = parsed if isinstance(parsed, list) else [commands]
except Exception: # noqa: BLE001
commands = [c.strip() for c in commands.splitlines() if c.strip()]
elif not isinstance(commands, list):
commands = [str(commands)]
# Check safety # Check safety
if not self.trust: if not self.trust:
@@ -894,9 +946,24 @@ el.replaceWith(d);
parallel=par_count, parallel=par_count,
timeout=timeout, timeout=timeout,
) )
return {&#34;success&#34;: True, &#34;results&#34;: results} fb_used = False
if isinstance(results, dict):
fb_used = any(
isinstance(v, dict) and bool(v.get(&#34;fallback_used&#34;, False))
for v in results.values()
)
if not fb_used:
fb_map = getattr(self.services.execution, &#34;last_fallback_used&#34;, {})
if isinstance(fb_map, dict):
fb_used = any(bool(v) for v in fb_map.values())
return {&#34;success&#34;: True, &#34;results&#34;: results, &#34;fallback_used&#34;: fb_used}
except Exception as e: # noqa: BLE001 except Exception as e: # noqa: BLE001
return {&#34;success&#34;: False, &#34;error&#34;: str(e)}</code></pre> san = sanitize_exception(
e,
code=&#34;AUTH_FAILED&#34;,
context_label=&#34;command execution failed&#34;,
)
return {&#34;success&#34;: False, &#34;error&#34;: str(san)}</code></pre>
</details> </details>
<div class="desc"><p>Execute commands on nodes matching the filter pattern.</p> <div class="desc"><p>Execute commands on nodes matching the filter pattern.</p>
<p>Unsafe commands are blocked unless trust mode is enabled.</p></div> <p>Unsafe commands are blocked unless trust mode is enabled.</p></div>
@@ -986,29 +1053,11 @@ el.replaceWith(d);
if not self.services or not hasattr(self.services, &#34;execution&#34;): if not self.services or not hasattr(self.services, &#34;execution&#34;):
return {&#34;success&#34;: False, &#34;error&#34;: &#34;Execution service unavailable.&#34;} return {&#34;success&#34;: False, &#34;error&#34;: &#34;Execution service unavailable.&#34;}
# Normalize commands
if isinstance(commands, str):
try: try:
import json commands = self._parse_json_list_or_lines(commands, allow_empty_str=False)
expected = self._parse_json_list_or_lines(expected, allow_empty_str=True)
parsed = json.loads(commands) except ValueError as exc:
commands = parsed if isinstance(parsed, list) else [commands] return {&#34;success&#34;: False, &#34;error&#34;: str(exc)}
except Exception: # noqa: BLE001
commands = [c.strip() for c in commands.splitlines() if c.strip()]
elif not isinstance(commands, list):
commands = [str(commands)]
# Normalize expected
if isinstance(expected, str):
try:
import json
parsed = json.loads(expected)
expected = parsed if isinstance(parsed, list) else ([expected] if expected else [])
except Exception: # noqa: BLE001
expected = [expected] if expected else []
elif not isinstance(expected, list):
expected = [str(expected)] if expected else []
# Check safety # Check safety
if not self.trust: if not self.trust:
@@ -1028,9 +1077,16 @@ el.replaceWith(d);
parallel=par_count, parallel=par_count,
timeout=timeout, timeout=timeout,
) )
return {&#34;success&#34;: True, &#34;results&#34;: results} fb_map = getattr(self.services.execution, &#34;last_fallback_used&#34;, {})
fb_used = any(bool(v) for v in fb_map.values()) if isinstance(fb_map, dict) else False
return {&#34;success&#34;: True, &#34;results&#34;: results, &#34;fallback_used&#34;: fb_used}
except Exception as e: # noqa: BLE001 except Exception as e: # noqa: BLE001
return {&#34;success&#34;: False, &#34;error&#34;: str(e)}</code></pre> san = sanitize_exception(
e,
code=&#34;AUTH_FAILED&#34;,
context_label=&#34;test execution failed&#34;,
)
return {&#34;success&#34;: False, &#34;error&#34;: str(san)}</code></pre>
</details> </details>
<div class="desc"><p>Execute commands and assert expected patterns on nodes matching the filter.</p></div> <div class="desc"><p>Execute commands and assert expected patterns on nodes matching the filter.</p></div>
</dd> </dd>
@@ -1057,8 +1113,11 @@ el.replaceWith(d);
node_updates.update(kwargs) node_updates.update(kwargs)
try: try:
node_updates = self._validate_and_normalize_node_payload(node_updates)
self.services.nodes.update_node(unique_id, node_updates) self.services.nodes.update_node(unique_id, node_updates)
return {&#34;success&#34;: True, &#34;message&#34;: f&#34;Node &#39;{unique_id}&#39; updated successfully.&#34;} return {&#34;success&#34;: True, &#34;message&#34;: f&#34;Node &#39;{unique_id}&#39; updated successfully.&#34;}
except (ValueError, TypeError, SanitizedAuthError) as exc:
return {&#34;success&#34;: False, &#34;error&#34;: str(exc)}
except Exception as e: # noqa: BLE001 except Exception as e: # noqa: BLE001
return {&#34;success&#34;: False, &#34;error&#34;: str(e)}</code></pre> return {&#34;success&#34;: False, &#34;error&#34;: str(e)}</code></pre>
</details> </details>
+144 -27
View File
@@ -58,15 +58,31 @@ el.replaceWith(d);
<pre><code class="python">class ContextService(BaseService): <pre><code class="python">class ContextService(BaseService):
&#34;&#34;&#34;Business logic for managing and applying regex-based contexts locally.&#34;&#34;&#34; &#34;&#34;&#34;Business logic for managing and applying regex-based contexts locally.&#34;&#34;&#34;
@staticmethod
def _validate_regexes(regexes: Any) -&gt; None:
if not isinstance(regexes, list) or not (1 &lt;= len(regexes) &lt;= MAX_CONTEXT_REGEXES):
raise ValueError(
f&#34;Context regexes must be a list of 1 to {MAX_CONTEXT_REGEXES} patterns&#34;
)
for pat in regexes:
if not isinstance(pat, str) or not (1 &lt;= len(pat) &lt;= MAX_CONTEXT_PATTERN_LEN):
raise ValueError(
f&#34;Each context regex must be a non-empty string up to {MAX_CONTEXT_PATTERN_LEN} characters&#34;
)
try:
re.compile(pat)
except re.error as exc:
raise ValueError(f&#34;Invalid context regex {pat!r}: {exc}&#34;) from None
@property @property
def contexts(self) -&gt; Dict[str, List[str]]: def contexts(self) -&gt; dict[str, list[str]]:
return self.config.config.get(&#34;contexts&#34;, {&#34;all&#34;: [&#34;.*&#34;]}) return self.config.config.get(&#34;contexts&#34;, {&#34;all&#34;: [&#34;.*&#34;]})
@property @property
def current_context(self) -&gt; str: def current_context(self) -&gt; str:
return self.config.config.get(&#34;current_context&#34;, &#34;all&#34;) return self.config.config.get(&#34;current_context&#34;, &#34;all&#34;)
def list_contexts(self) -&gt; List[Dict[str, Any]]: def list_contexts(self) -&gt; list[dict[str, Any]]:
result = [] result = []
for name in self.contexts.keys(): for name in self.contexts.keys():
result.append({ result.append({
@@ -76,27 +92,29 @@ el.replaceWith(d);
}) })
return result return result
def add_context(self, name: str, regexes: List[str]): def add_context(self, name: str, regexes: list[str]):
if not name.isalnum(): if not name.isalnum():
raise ValueError(&#34;Context name must be alphanumeric&#34;) raise ValueError(&#34;Context name must be alphanumeric&#34;)
self._validate_regexes(regexes)
ctxs = self.contexts ctxs = self.contexts
if name in ctxs: if name in ctxs:
raise ValueError(f&#34;Context &#39;{name}&#39; already exists&#34;) raise ValueError(f&#34;Context &#39;{name}&#39; already exists&#34;)
ctxs[name] = regexes ctxs[name] = list(regexes)
self.config.config[&#34;contexts&#34;] = ctxs self.config.config[&#34;contexts&#34;] = ctxs
self.config._saveconfig(self.config.file) self.config._saveconfig(self.config.file)
def update_context(self, name: str, regexes: List[str]): def update_context(self, name: str, regexes: list[str]):
if name == &#34;all&#34;: if name == &#34;all&#34;:
raise ValueError(&#34;Cannot modify default context &#39;all&#39;&#34;) raise ValueError(&#34;Cannot modify default context &#39;all&#39;&#34;)
self._validate_regexes(regexes)
ctxs = self.contexts ctxs = self.contexts
if name not in ctxs: if name not in ctxs:
raise ValueError(f&#34;Context &#39;{name}&#39; does not exist&#34;) raise ValueError(f&#34;Context &#39;{name}&#39; does not exist&#34;)
ctxs[name] = regexes ctxs[name] = list(regexes)
self.config.config[&#34;contexts&#34;] = ctxs self.config.config[&#34;contexts&#34;] = ctxs
self.config._saveconfig(self.config.file) self.config._saveconfig(self.config.file)
@@ -118,15 +136,47 @@ el.replaceWith(d);
if name not in self.contexts: if name not in self.contexts:
raise ValueError(f&#34;Context &#39;{name}&#39; does not exist&#34;) raise ValueError(f&#34;Context &#39;{name}&#39; does not exist&#34;)
prev_context = self.current_context
self.config.config[&#34;current_context&#34;] = name self.config.config[&#34;current_context&#34;] = name
self.config._saveconfig(self.config.file) self.config._saveconfig(self.config.file)
can_refresh = (
self.config._is_context_filtering_enabled_unlocked()
if (
not hasattr(self.config, &#34;assert_called&#34;)
and callable(getattr(self.config, &#34;_is_context_filtering_enabled_unlocked&#34;, None))
)
else getattr(self.config, &#34;_enforce_context&#34;, True)
)
if (
name == &#34;all&#34;
and prev_context != &#34;all&#34;
and can_refresh
and hasattr(self.config, &#34;_generate_nodes_cache&#34;)
):
self.config._generate_nodes_cache()
def get_active_regexes(self) -&gt; List[re.Pattern]: def get_active_regexes(self) -&gt; list[re.Pattern]:
patterns = self.contexts.get(self.current_context, [&#34;.*&#34;]) return compile_active_context_patterns(getattr(self.config, &#34;config&#34;, None))
return [re.compile(p) for p in patterns]
def _match_any(self, node_name: str, patterns: List[re.Pattern]) -&gt; bool: def _match_any(self, node_name: str, patterns: list[re.Pattern]) -&gt; bool:
return any(p.match(node_name) for p in patterns) return match_active_context_patterns(node_name, patterns)
def is_default_all_context(self) -&gt; bool:
return is_default_all_context_cfg(getattr(self.config, &#34;config&#34;, None))
def matches_active_context(self, name: str) -&gt; bool:
if self.is_default_all_context():
return isinstance(name, str) and bool(name)
if not isinstance(name, str) or not name:
return False
return match_active_context_patterns(name, self.get_active_regexes())
def validate_in_active_context(self, unique_id: str, is_folder: bool = False) -&gt; None:
if not self.is_default_all_context() and not self.matches_active_context(unique_id):
label = &#34;Folder&#34; if is_folder else &#34;Node&#34;
raise InvalidConfigurationError(
f&#34;{label} &#39;{unique_id}&#39; does not match active context &#39;{self.current_context}&#39;.&#34;
)
# Hook handlers for filtering # Hook handlers for filtering
def filter_node_list(self, *args, **kwargs): def filter_node_list(self, *args, **kwargs):
@@ -151,14 +201,14 @@ el.replaceWith(d);
</ul> </ul>
<h3>Instance variables</h3> <h3>Instance variables</h3>
<dl> <dl>
<dt id="connpy.services.context_service.ContextService.contexts"><code class="name">prop <span class="ident">contexts</span> : Dict[str, List[str]]</code></dt> <dt id="connpy.services.context_service.ContextService.contexts"><code class="name">prop <span class="ident">contexts</span> : dict[str, list[str]]</code></dt>
<dd> <dd>
<details class="source"> <details class="source">
<summary> <summary>
<span>Expand source code</span> <span>Expand source code</span>
</summary> </summary>
<pre><code class="python">@property <pre><code class="python">@property
def contexts(self) -&gt; Dict[str, List[str]]: def contexts(self) -&gt; dict[str, list[str]]:
return self.config.config.get(&#34;contexts&#34;, {&#34;all&#34;: [&#34;.*&#34;]})</code></pre> return self.config.config.get(&#34;contexts&#34;, {&#34;all&#34;: [&#34;.*&#34;]})</code></pre>
</details> </details>
<div class="desc"></div> <div class="desc"></div>
@@ -179,22 +229,23 @@ def current_context(self) -&gt; str:
<h3>Methods</h3> <h3>Methods</h3>
<dl> <dl>
<dt id="connpy.services.context_service.ContextService.add_context"><code class="name flex"> <dt id="connpy.services.context_service.ContextService.add_context"><code class="name flex">
<span>def <span class="ident">add_context</span></span>(<span>self, name: str, regexes: List[str])</span> <span>def <span class="ident">add_context</span></span>(<span>self, name: str, regexes: list[str])</span>
</code></dt> </code></dt>
<dd> <dd>
<details class="source"> <details class="source">
<summary> <summary>
<span>Expand source code</span> <span>Expand source code</span>
</summary> </summary>
<pre><code class="python">def add_context(self, name: str, regexes: List[str]): <pre><code class="python">def add_context(self, name: str, regexes: list[str]):
if not name.isalnum(): if not name.isalnum():
raise ValueError(&#34;Context name must be alphanumeric&#34;) raise ValueError(&#34;Context name must be alphanumeric&#34;)
self._validate_regexes(regexes)
ctxs = self.contexts ctxs = self.contexts
if name in ctxs: if name in ctxs:
raise ValueError(f&#34;Context &#39;{name}&#39; already exists&#34;) raise ValueError(f&#34;Context &#39;{name}&#39; already exists&#34;)
ctxs[name] = regexes ctxs[name] = list(regexes)
self.config.config[&#34;contexts&#34;] = ctxs self.config.config[&#34;contexts&#34;] = ctxs
self.config._saveconfig(self.config.file)</code></pre> self.config._saveconfig(self.config.file)</code></pre>
</details> </details>
@@ -253,28 +304,40 @@ def current_context(self) -&gt; str:
<div class="desc"></div> <div class="desc"></div>
</dd> </dd>
<dt id="connpy.services.context_service.ContextService.get_active_regexes"><code class="name flex"> <dt id="connpy.services.context_service.ContextService.get_active_regexes"><code class="name flex">
<span>def <span class="ident">get_active_regexes</span></span>(<span>self) ‑> List[re.Pattern]</span> <span>def <span class="ident">get_active_regexes</span></span>(<span>self) ‑> list[re.Pattern]</span>
</code></dt> </code></dt>
<dd> <dd>
<details class="source"> <details class="source">
<summary> <summary>
<span>Expand source code</span> <span>Expand source code</span>
</summary> </summary>
<pre><code class="python">def get_active_regexes(self) -&gt; List[re.Pattern]: <pre><code class="python">def get_active_regexes(self) -&gt; list[re.Pattern]:
patterns = self.contexts.get(self.current_context, [&#34;.*&#34;]) return compile_active_context_patterns(getattr(self.config, &#34;config&#34;, None))</code></pre>
return [re.compile(p) for p in patterns]</code></pre> </details>
<div class="desc"></div>
</dd>
<dt id="connpy.services.context_service.ContextService.is_default_all_context"><code class="name flex">
<span>def <span class="ident">is_default_all_context</span></span>(<span>self) ‑> bool</span>
</code></dt>
<dd>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def is_default_all_context(self) -&gt; bool:
return is_default_all_context_cfg(getattr(self.config, &#34;config&#34;, None))</code></pre>
</details> </details>
<div class="desc"></div> <div class="desc"></div>
</dd> </dd>
<dt id="connpy.services.context_service.ContextService.list_contexts"><code class="name flex"> <dt id="connpy.services.context_service.ContextService.list_contexts"><code class="name flex">
<span>def <span class="ident">list_contexts</span></span>(<span>self) ‑> List[Dict[str, Any]]</span> <span>def <span class="ident">list_contexts</span></span>(<span>self) ‑> list[dict[str, typing.Any]]</span>
</code></dt> </code></dt>
<dd> <dd>
<details class="source"> <details class="source">
<summary> <summary>
<span>Expand source code</span> <span>Expand source code</span>
</summary> </summary>
<pre><code class="python">def list_contexts(self) -&gt; List[Dict[str, Any]]: <pre><code class="python">def list_contexts(self) -&gt; list[dict[str, Any]]:
result = [] result = []
for name in self.contexts.keys(): for name in self.contexts.keys():
result.append({ result.append({
@@ -286,6 +349,23 @@ def current_context(self) -&gt; str:
</details> </details>
<div class="desc"></div> <div class="desc"></div>
</dd> </dd>
<dt id="connpy.services.context_service.ContextService.matches_active_context"><code class="name flex">
<span>def <span class="ident">matches_active_context</span></span>(<span>self, name: str) ‑> bool</span>
</code></dt>
<dd>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def matches_active_context(self, name: str) -&gt; bool:
if self.is_default_all_context():
return isinstance(name, str) and bool(name)
if not isinstance(name, str) or not name:
return False
return match_active_context_patterns(name, self.get_active_regexes())</code></pre>
</details>
<div class="desc"></div>
</dd>
<dt id="connpy.services.context_service.ContextService.set_active_context"><code class="name flex"> <dt id="connpy.services.context_service.ContextService.set_active_context"><code class="name flex">
<span>def <span class="ident">set_active_context</span></span>(<span>self, name: str)</span> <span>def <span class="ident">set_active_context</span></span>(<span>self, name: str)</span>
</code></dt> </code></dt>
@@ -298,33 +378,67 @@ def current_context(self) -&gt; str:
if name not in self.contexts: if name not in self.contexts:
raise ValueError(f&#34;Context &#39;{name}&#39; does not exist&#34;) raise ValueError(f&#34;Context &#39;{name}&#39; does not exist&#34;)
prev_context = self.current_context
self.config.config[&#34;current_context&#34;] = name self.config.config[&#34;current_context&#34;] = name
self.config._saveconfig(self.config.file)</code></pre> self.config._saveconfig(self.config.file)
can_refresh = (
self.config._is_context_filtering_enabled_unlocked()
if (
not hasattr(self.config, &#34;assert_called&#34;)
and callable(getattr(self.config, &#34;_is_context_filtering_enabled_unlocked&#34;, None))
)
else getattr(self.config, &#34;_enforce_context&#34;, True)
)
if (
name == &#34;all&#34;
and prev_context != &#34;all&#34;
and can_refresh
and hasattr(self.config, &#34;_generate_nodes_cache&#34;)
):
self.config._generate_nodes_cache()</code></pre>
</details> </details>
<div class="desc"></div> <div class="desc"></div>
</dd> </dd>
<dt id="connpy.services.context_service.ContextService.update_context"><code class="name flex"> <dt id="connpy.services.context_service.ContextService.update_context"><code class="name flex">
<span>def <span class="ident">update_context</span></span>(<span>self, name: str, regexes: List[str])</span> <span>def <span class="ident">update_context</span></span>(<span>self, name: str, regexes: list[str])</span>
</code></dt> </code></dt>
<dd> <dd>
<details class="source"> <details class="source">
<summary> <summary>
<span>Expand source code</span> <span>Expand source code</span>
</summary> </summary>
<pre><code class="python">def update_context(self, name: str, regexes: List[str]): <pre><code class="python">def update_context(self, name: str, regexes: list[str]):
if name == &#34;all&#34;: if name == &#34;all&#34;:
raise ValueError(&#34;Cannot modify default context &#39;all&#39;&#34;) raise ValueError(&#34;Cannot modify default context &#39;all&#39;&#34;)
self._validate_regexes(regexes)
ctxs = self.contexts ctxs = self.contexts
if name not in ctxs: if name not in ctxs:
raise ValueError(f&#34;Context &#39;{name}&#39; does not exist&#34;) raise ValueError(f&#34;Context &#39;{name}&#39; does not exist&#34;)
ctxs[name] = regexes ctxs[name] = list(regexes)
self.config.config[&#34;contexts&#34;] = ctxs self.config.config[&#34;contexts&#34;] = ctxs
self.config._saveconfig(self.config.file)</code></pre> self.config._saveconfig(self.config.file)</code></pre>
</details> </details>
<div class="desc"></div> <div class="desc"></div>
</dd> </dd>
<dt id="connpy.services.context_service.ContextService.validate_in_active_context"><code class="name flex">
<span>def <span class="ident">validate_in_active_context</span></span>(<span>self, unique_id: str, is_folder: bool = False) ‑> None</span>
</code></dt>
<dd>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def validate_in_active_context(self, unique_id: str, is_folder: bool = False) -&gt; None:
if not self.is_default_all_context() and not self.matches_active_context(unique_id):
label = &#34;Folder&#34; if is_folder else &#34;Node&#34;
raise InvalidConfigurationError(
f&#34;{label} &#39;{unique_id}&#39; does not match active context &#39;{self.current_context}&#39;.&#34;
)</code></pre>
</details>
<div class="desc"></div>
</dd>
</dl> </dl>
<h3>Inherited members</h3> <h3>Inherited members</h3>
<ul class="hlist"> <ul class="hlist">
@@ -352,7 +466,7 @@ def current_context(self) -&gt; str:
<ul> <ul>
<li> <li>
<h4><code><a title="connpy.services.context_service.ContextService" href="#connpy.services.context_service.ContextService">ContextService</a></code></h4> <h4><code><a title="connpy.services.context_service.ContextService" href="#connpy.services.context_service.ContextService">ContextService</a></code></h4>
<ul class="two-column"> <ul class="">
<li><code><a title="connpy.services.context_service.ContextService.add_context" href="#connpy.services.context_service.ContextService.add_context">add_context</a></code></li> <li><code><a title="connpy.services.context_service.ContextService.add_context" href="#connpy.services.context_service.ContextService.add_context">add_context</a></code></li>
<li><code><a title="connpy.services.context_service.ContextService.contexts" href="#connpy.services.context_service.ContextService.contexts">contexts</a></code></li> <li><code><a title="connpy.services.context_service.ContextService.contexts" href="#connpy.services.context_service.ContextService.contexts">contexts</a></code></li>
<li><code><a title="connpy.services.context_service.ContextService.current_context" href="#connpy.services.context_service.ContextService.current_context">current_context</a></code></li> <li><code><a title="connpy.services.context_service.ContextService.current_context" href="#connpy.services.context_service.ContextService.current_context">current_context</a></code></li>
@@ -360,9 +474,12 @@ def current_context(self) -&gt; str:
<li><code><a title="connpy.services.context_service.ContextService.filter_node_dict" href="#connpy.services.context_service.ContextService.filter_node_dict">filter_node_dict</a></code></li> <li><code><a title="connpy.services.context_service.ContextService.filter_node_dict" href="#connpy.services.context_service.ContextService.filter_node_dict">filter_node_dict</a></code></li>
<li><code><a title="connpy.services.context_service.ContextService.filter_node_list" href="#connpy.services.context_service.ContextService.filter_node_list">filter_node_list</a></code></li> <li><code><a title="connpy.services.context_service.ContextService.filter_node_list" href="#connpy.services.context_service.ContextService.filter_node_list">filter_node_list</a></code></li>
<li><code><a title="connpy.services.context_service.ContextService.get_active_regexes" href="#connpy.services.context_service.ContextService.get_active_regexes">get_active_regexes</a></code></li> <li><code><a title="connpy.services.context_service.ContextService.get_active_regexes" href="#connpy.services.context_service.ContextService.get_active_regexes">get_active_regexes</a></code></li>
<li><code><a title="connpy.services.context_service.ContextService.is_default_all_context" href="#connpy.services.context_service.ContextService.is_default_all_context">is_default_all_context</a></code></li>
<li><code><a title="connpy.services.context_service.ContextService.list_contexts" href="#connpy.services.context_service.ContextService.list_contexts">list_contexts</a></code></li> <li><code><a title="connpy.services.context_service.ContextService.list_contexts" href="#connpy.services.context_service.ContextService.list_contexts">list_contexts</a></code></li>
<li><code><a title="connpy.services.context_service.ContextService.matches_active_context" href="#connpy.services.context_service.ContextService.matches_active_context">matches_active_context</a></code></li>
<li><code><a title="connpy.services.context_service.ContextService.set_active_context" href="#connpy.services.context_service.ContextService.set_active_context">set_active_context</a></code></li> <li><code><a title="connpy.services.context_service.ContextService.set_active_context" href="#connpy.services.context_service.ContextService.set_active_context">set_active_context</a></code></li>
<li><code><a title="connpy.services.context_service.ContextService.update_context" href="#connpy.services.context_service.ContextService.update_context">update_context</a></code></li> <li><code><a title="connpy.services.context_service.ContextService.update_context" href="#connpy.services.context_service.ContextService.update_context">update_context</a></code></li>
<li><code><a title="connpy.services.context_service.ContextService.validate_in_active_context" href="#connpy.services.context_service.ContextService.validate_in_active_context">validate_in_active_context</a></code></li>
</ul> </ul>
</li> </li>
</ul> </ul>
+1
View File
@@ -67,6 +67,7 @@ el.replaceWith(d);
</ul> </ul>
<h3>Subclasses</h3> <h3>Subclasses</h3>
<ul class="hlist"> <ul class="hlist">
<li><a title="connpy.auth_utils.SanitizedAuthError" href="../auth_utils.html#connpy.auth_utils.SanitizedAuthError">SanitizedAuthError</a></li>
<li><a title="connpy.services.exceptions.ExecutionError" href="#connpy.services.exceptions.ExecutionError">ExecutionError</a></li> <li><a title="connpy.services.exceptions.ExecutionError" href="#connpy.services.exceptions.ExecutionError">ExecutionError</a></li>
<li><a title="connpy.services.exceptions.InvalidConfigurationError" href="#connpy.services.exceptions.InvalidConfigurationError">InvalidConfigurationError</a></li> <li><a title="connpy.services.exceptions.InvalidConfigurationError" href="#connpy.services.exceptions.InvalidConfigurationError">InvalidConfigurationError</a></li>
<li><a title="connpy.services.exceptions.NodeAlreadyExistsError" href="#connpy.services.exceptions.NodeAlreadyExistsError">NodeAlreadyExistsError</a></li> <li><a title="connpy.services.exceptions.NodeAlreadyExistsError" href="#connpy.services.exceptions.NodeAlreadyExistsError">NodeAlreadyExistsError</a></li>
+446 -110
View File
@@ -48,7 +48,7 @@ el.replaceWith(d);
<dl> <dl>
<dt id="connpy.services.execution_service.ExecutionService"><code class="flex name class"> <dt id="connpy.services.execution_service.ExecutionService"><code class="flex name class">
<span>class <span class="ident">ExecutionService</span></span> <span>class <span class="ident">ExecutionService</span></span>
<span>(</span><span>config=None)</span> <span>(</span><span>config=None, remote_stub=None, attempt_tracker=None, enforce_context: bool = True)</span>
</code></dt> </code></dt>
<dd> <dd>
<details class="source"> <details class="source">
@@ -58,23 +58,196 @@ el.replaceWith(d);
<pre><code class="python">class ExecutionService(BaseService): <pre><code class="python">class ExecutionService(BaseService):
&#34;&#34;&#34;Business logic for executing commands on nodes and running automation scripts.&#34;&#34;&#34; &#34;&#34;&#34;Business logic for executing commands on nodes and running automation scripts.&#34;&#34;&#34;
def __init__(
self,
config=None,
remote_stub=None,
attempt_tracker=None,
enforce_context: bool = True,
):
super().__init__(config)
self.remote_stub = remote_stub
self.attempt_tracker = (
attempt_tracker if attempt_tracker is not None else AuthAttemptTracker()
)
self.enforce_context = bool(enforce_context)
self.last_fallback_used: dict[str, bool] = {}
def _get_context_service(self):
if (
not getattr(self, &#34;enforce_context&#34;, True)
or not getattr(self.config, &#34;_enforce_context&#34;, True)
or (len(sys.argv) &gt; 1 and sys.argv[1] == &#34;api&#34;)
or not isinstance(getattr(self.config, &#34;config&#34;, None), dict)
):
return None
from .context_service import ContextService
return ContextService(self.config)
def _is_remote_backend(self) -&gt; bool:
&#34;&#34;&#34;Return True when execution is configured to run against a remote gRPC backend.&#34;&#34;&#34;
if getattr(self, &#34;remote_stub&#34;, None) is not None:
return True
cfg = getattr(self.config, &#34;config&#34;, None)
return isinstance(cfg, dict) and cfg.get(&#34;service_mode&#34;) == &#34;remote&#34;
def _ensure_backend_supports_stage_timeouts(
self,
connect_timeout: int | None = None,
auth_timeout: int | None = None,
) -&gt; None:
&#34;&#34;&#34;Raise ConnpyError if per-stage timeout overrides are used on gRPC remote backend.&#34;&#34;&#34;
if self._is_remote_backend() and (connect_timeout is not None or auth_timeout is not None):
raise ConnpyError(
&#34;connect_timeout and auth_timeout overrides are not supported in remote gRPC mode&#34;
)
def _get_remote_channel(self):
cfg = getattr(self.config, &#34;config&#34;, None)
remote_host = cfg.get(&#34;remote_host&#34;) if isinstance(cfg, dict) else None
if not remote_host:
raise ConnpyError(&#34;Remote host must be specified in remote mode&#34;)
import grpc
from connpy.grpc_layer.stubs import AuthClientInterceptor
def get_token():
env_token = os.environ.get(&#34;CONNPY_TOKEN&#34;)
if env_token:
return env_token
defaultdir = getattr(self.config, &#34;defaultdir&#34;, None)
if defaultdir:
token_path = os.path.join(defaultdir, &#34;.token&#34;)
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 channel, remote_host
def _get_remote_stub(self):
&#34;&#34;&#34;Return the configured or lazily constructed ExecutionStub for remote gRPC mode.&#34;&#34;&#34;
if getattr(self, &#34;remote_stub&#34;, None) is not None:
return self.remote_stub
from connpy.grpc_layer.stubs import ExecutionStub
channel, remote_host = self._get_remote_channel()
return ExecutionStub(channel, remote_host=remote_host)
def _filter_remote_nodes_filter(self, nodes_filter):
ctx_svc = self._get_context_service()
if ctx_svc is None or ctx_svc.is_default_all_context():
return nodes_filter
if isinstance(nodes_filter, (list, tuple)):
permitted = ctx_svc.filter_node_list(result=list(nodes_filter))
else:
if self.remote_stub is not None and callable(
getattr(self.remote_stub, &#34;list_nodes&#34;, None)
):
candidates = self.remote_stub.list_nodes(filter_str=nodes_filter)
elif self.remote_stub is not None and callable(
getattr(getattr(self.remote_stub, &#34;node_stub&#34;, None), &#34;list_nodes&#34;, None)
):
candidates = self.remote_stub.node_stub.list_nodes(filter_str=nodes_filter)
elif self.remote_stub is None:
from connpy.grpc_layer.stubs import NodeStub
channel, remote_host = self._get_remote_channel()
node_stub = NodeStub(channel, remote_host=remote_host, config=self.config)
candidates = node_stub.list_nodes(filter_str=nodes_filter)
else:
candidates = (
self.config._getallnodes(nodes_filter)
if hasattr(self.config, &#34;_getallnodes&#34;)
else []
)
if nodes_filter:
cfg_dict = getattr(self.config, &#34;config&#34;, None)
case = cfg_dict.get(&#34;case&#34;, False) if isinstance(cfg_dict, dict) else False
flags = 0 if case else re.IGNORECASE
candidates = [c for c in candidates if re.search(nodes_filter, c, flags)]
permitted = ctx_svc.filter_node_list(result=candidates)
if not permitted:
raise ConnpyError(f&#34;No nodes found matching filter: {nodes_filter}&#34;)
return [f&#34;^{re.escape(n)}$&#34; for n in permitted]
def _build_remote_filter_batches(self, nodes_filter):
filtered = self._filter_remote_nodes_filter(nodes_filter)
if isinstance(filtered, list):
return [
filtered[i : i + MAX_REMOTE_FILTER_NODES]
for i in range(0, len(filtered), MAX_REMOTE_FILTER_NODES)
]
return [filtered]
@staticmethod
def _aggregate_remote_results(batch_results):
if len(batch_results) == 1:
return batch_results[0]
if all(isinstance(r, dict) for r in batch_results):
merged: dict[str, Any] = {}
for r in batch_results:
merged.update(r)
return merged
if all(isinstance(r, list) for r in batch_results):
merged_list: list[Any] = []
for r in batch_results:
merged_list.extend(r)
return merged_list
return batch_results[-1]
def run_commands( def run_commands(
self, self,
nodes_filter: str, nodes_filter: str,
commands: List[str], commands: list[str],
variables: Optional[Dict[str, Any]] = None, variables: dict[str, Any] | None = None,
parallel: int = 10, parallel: int = 10,
timeout: int = 20, timeout: int | None = None,
folder: Optional[str] = None, folder: str | None = None,
prompt: Optional[str] = None, prompt: str | None = None,
on_node_complete: Optional[Callable] = None, on_node_complete: Callable | None = None,
logger: Optional[Callable] = None, logger: Callable | None = None,
name: Optional[str] = None name: str | None = None,
) -&gt; Dict[str, str]: connect_timeout: int | None = None,
auth_timeout: int | None = None,
) -&gt; dict[str, Any]:
&#34;&#34;&#34;Execute commands on a set of nodes.&#34;&#34;&#34; &#34;&#34;&#34;Execute commands on a set of nodes.&#34;&#34;&#34;
try: try:
self._ensure_backend_supports_stage_timeouts(
connect_timeout=connect_timeout,
auth_timeout=auth_timeout,
)
if self._is_remote_backend():
batches = self._build_remote_filter_batches(nodes_filter)
stub = self._get_remote_stub()
batch_results = [
stub.run_commands(
nodes_filter=batch,
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 &#34;&#34;,
)
for batch in batches
]
return self._aggregate_remote_results(batch_results)
matched_names = self.config._getallnodes(nodes_filter) matched_names = self.config._getallnodes(nodes_filter)
ctx_svc = self._get_context_service()
if ctx_svc is not None and not ctx_svc.is_default_all_context():
matched_names = ctx_svc.filter_node_list(result=matched_names)
if not matched_names: if not matched_names:
raise ConnpyError(f&#34;No nodes found matching filter: {nodes_filter}&#34;) raise ConnpyError(f&#34;No nodes found matching filter: {nodes_filter}&#34;)
@@ -82,47 +255,96 @@ el.replaceWith(d);
executor = Nodes(node_data, config=self.config) executor = Nodes(node_data, config=self.config)
self.last_executor = executor self.last_executor = executor
results = executor.run( run_kwargs: dict[str, Any] = {
commands=commands, &#34;commands&#34;: commands,
vars=variables, &#34;vars&#34;: variables,
parallel=parallel, &#34;parallel&#34;: parallel,
timeout=timeout, &#34;timeout&#34;: timeout,
folder=folder, &#34;folder&#34;: folder,
prompt=prompt, &#34;prompt&#34;: prompt,
on_complete=on_node_complete, &#34;on_complete&#34;: on_node_complete,
logger=logger &#34;logger&#34;: logger,
) &#34;connect_timeout&#34;: connect_timeout,
&#34;auth_timeout&#34;: auth_timeout,
&#34;_default_cmd_timeout&#34;: 20,
}
run_params = inspect.signature(executor.run).parameters
if &#34;unattended&#34; in run_params:
run_kwargs[&#34;unattended&#34;] = True
if &#34;attempt_tracker&#34; in run_params:
run_kwargs[&#34;attempt_tracker&#34;] = self.attempt_tracker
# Combine output and status for the caller results = executor.run(**run_kwargs)
fb_map = getattr(executor, &#34;fallback_used&#34;, {})
fb_dict = fb_map if isinstance(fb_map, dict) else {}
# Combine output, status, and fallback metadata for the caller
full_results = {} full_results = {}
self.last_fallback_used = {}
for unique in results: for unique in results:
fb_used = bool(fb_dict.get(unique, False))
full_results[unique] = { full_results[unique] = {
&#34;output&#34;: results[unique], &#34;output&#34;: results[unique],
&#34;status&#34;: executor.status.get(unique, 1) &#34;status&#34;: executor.status.get(unique, 1),
&#34;fallback_used&#34;: fb_used,
} }
self.last_fallback_used[unique] = fb_used
return full_results return full_results
except Exception as e: except ConnpyError:
raise ConnpyError(f&#34;Execution failed: {e}&#34;) raise
except Exception as e: # noqa: BLE001
raise_sanitized_auth_error(e, code=&#34;AUTH_FAILED&#34;, context_label=&#34;execution failed&#34;)
def test_commands( def test_commands(
self, self,
nodes_filter: str, nodes_filter: str,
commands: List[str], commands: list[str],
expected: List[str], expected: list[str],
variables: Optional[Dict[str, Any]] = None, variables: dict[str, Any] | None = None,
parallel: int = 10, parallel: int = 10,
timeout: int = 20, timeout: int | None = None,
folder: Optional[str] = None, folder: str | None = None,
prompt: Optional[str] = None, prompt: str | None = None,
on_node_complete: Optional[Callable] = None, on_node_complete: Callable | None = None,
logger: Optional[Callable] = None, logger: Callable | None = None,
name: Optional[str] = None name: str | None = None,
) -&gt; Dict[str, Dict[str, bool]]: connect_timeout: int | None = None,
auth_timeout: int | None = None,
) -&gt; dict[str, dict[str, bool]]:
&#34;&#34;&#34;Run commands and verify expected output on a set of nodes.&#34;&#34;&#34; &#34;&#34;&#34;Run commands and verify expected output on a set of nodes.&#34;&#34;&#34;
try: try:
self._ensure_backend_supports_stage_timeouts(
connect_timeout=connect_timeout,
auth_timeout=auth_timeout,
)
if self._is_remote_backend():
batches = self._build_remote_filter_batches(nodes_filter)
stub = self._get_remote_stub()
batch_results = [
stub.test_commands(
nodes_filter=batch,
commands=commands,
expected=expected,
variables=variables,
parallel=parallel,
timeout=timeout if timeout is not None else 20,
folder=folder or &#34;&#34;,
prompt=prompt,
on_node_complete=on_node_complete,
logger=logger,
name=name or &#34;&#34;,
)
for batch in batches
]
return self._aggregate_remote_results(batch_results)
matched_names = self.config._getallnodes(nodes_filter) matched_names = self.config._getallnodes(nodes_filter)
ctx_svc = self._get_context_service()
if ctx_svc is not None and not ctx_svc.is_default_all_context():
matched_names = ctx_svc.filter_node_list(result=matched_names)
if not matched_names: if not matched_names:
raise ConnpyError(f&#34;No nodes found matching filter: {nodes_filter}&#34;) raise ConnpyError(f&#34;No nodes found matching filter: {nodes_filter}&#34;)
@@ -130,31 +352,49 @@ el.replaceWith(d);
executor = Nodes(node_data, config=self.config) executor = Nodes(node_data, config=self.config)
self.last_executor = executor self.last_executor = executor
results = executor.test( test_kwargs: dict[str, Any] = {
commands=commands, &#34;commands&#34;: commands,
expected=expected, &#34;expected&#34;: expected,
vars=variables, &#34;vars&#34;: variables,
parallel=parallel, &#34;parallel&#34;: parallel,
timeout=timeout, &#34;timeout&#34;: timeout,
folder=folder, &#34;folder&#34;: folder,
prompt=prompt, &#34;prompt&#34;: prompt,
on_complete=on_node_complete, &#34;on_complete&#34;: on_node_complete,
logger=logger &#34;logger&#34;: logger,
) &#34;connect_timeout&#34;: connect_timeout,
return results &#34;auth_timeout&#34;: auth_timeout,
except Exception as e: &#34;_default_cmd_timeout&#34;: 20,
raise ConnpyError(f&#34;Testing failed: {e}&#34;) }
test_params = inspect.signature(executor.test).parameters
if &#34;unattended&#34; in test_params:
test_kwargs[&#34;unattended&#34;] = True
if &#34;attempt_tracker&#34; in test_params:
test_kwargs[&#34;attempt_tracker&#34;] = self.attempt_tracker
def run_cli_script(self, nodes_filter: str, script_path: str, parallel: int = 10) -&gt; Dict[str, str]: res = executor.test(**test_kwargs)
fb_map = getattr(executor, &#34;fallback_used&#34;, {})
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=&#34;AUTH_FAILED&#34;, context_label=&#34;testing failed&#34;)
def run_cli_script(self, nodes_filter: str, script_path: str, parallel: int = 10) -&gt; dict[str, str]:
&#34;&#34;&#34;Run a plain-text script containing one command per line.&#34;&#34;&#34; &#34;&#34;&#34;Run a plain-text script containing one command per line.&#34;&#34;&#34;
if not os.path.exists(script_path): if not os.path.exists(script_path):
raise ConnpyError(f&#34;Script file not found: {script_path}&#34;) raise ConnpyError(f&#34;Script file not found: {script_path}&#34;)
try: try:
with open(script_path, &#34;r&#34;) as f: with open(script_path) as f:
commands = [line.strip() for line in f if line.strip()] commands = [line.strip() for line in f if line.strip()]
except Exception as e: except OSError:
raise ConnpyError(f&#34;Failed to read script {script_path}: {e}&#34;) raise ConnpyError(&#34;Failed to read script file&#34;) from None
return self.run_commands(nodes_filter, commands, parallel=parallel)</code></pre> return self.run_commands(nodes_filter, commands, parallel=parallel)</code></pre>
</details> </details>
@@ -173,30 +413,30 @@ el.replaceWith(d);
<h3>Methods</h3> <h3>Methods</h3>
<dl> <dl>
<dt id="connpy.services.execution_service.ExecutionService.run_cli_script"><code class="name flex"> <dt id="connpy.services.execution_service.ExecutionService.run_cli_script"><code class="name flex">
<span>def <span class="ident">run_cli_script</span></span>(<span>self, nodes_filter: str, script_path: str, parallel: int = 10) ‑> Dict[str, str]</span> <span>def <span class="ident">run_cli_script</span></span>(<span>self, nodes_filter: str, script_path: str, parallel: int = 10) ‑> dict[str, str]</span>
</code></dt> </code></dt>
<dd> <dd>
<details class="source"> <details class="source">
<summary> <summary>
<span>Expand source code</span> <span>Expand source code</span>
</summary> </summary>
<pre><code class="python">def run_cli_script(self, nodes_filter: str, script_path: str, parallel: int = 10) -&gt; Dict[str, str]: <pre><code class="python">def run_cli_script(self, nodes_filter: str, script_path: str, parallel: int = 10) -&gt; dict[str, str]:
&#34;&#34;&#34;Run a plain-text script containing one command per line.&#34;&#34;&#34; &#34;&#34;&#34;Run a plain-text script containing one command per line.&#34;&#34;&#34;
if not os.path.exists(script_path): if not os.path.exists(script_path):
raise ConnpyError(f&#34;Script file not found: {script_path}&#34;) raise ConnpyError(f&#34;Script file not found: {script_path}&#34;)
try: try:
with open(script_path, &#34;r&#34;) as f: with open(script_path) as f:
commands = [line.strip() for line in f if line.strip()] commands = [line.strip() for line in f if line.strip()]
except Exception as e: except OSError:
raise ConnpyError(f&#34;Failed to read script {script_path}: {e}&#34;) raise ConnpyError(&#34;Failed to read script file&#34;) from None
return self.run_commands(nodes_filter, commands, parallel=parallel)</code></pre> return self.run_commands(nodes_filter, commands, parallel=parallel)</code></pre>
</details> </details>
<div class="desc"><p>Run a plain-text script containing one command per line.</p></div> <div class="desc"><p>Run a plain-text script containing one command per line.</p></div>
</dd> </dd>
<dt id="connpy.services.execution_service.ExecutionService.run_commands"><code class="name flex"> <dt id="connpy.services.execution_service.ExecutionService.run_commands"><code class="name flex">
<span>def <span class="ident">run_commands</span></span>(<span>self,<br>nodes_filter: str,<br>commands: List[str],<br>variables: Dict[str, Any] | None = None,<br>parallel: int = 10,<br>timeout: int = 20,<br>folder: str | None = None,<br>prompt: str | None = None,<br>on_node_complete: Callable | None = None,<br>logger: Callable | None = None,<br>name: str | None = None) ‑> Dict[str, str]</span> <span>def <span class="ident">run_commands</span></span>(<span>self,<br>nodes_filter: str,<br>commands: list[str],<br>variables: dict[str, typing.Any] | None = None,<br>parallel: int = 10,<br>timeout: int | None = None,<br>folder: str | None = None,<br>prompt: str | None = None,<br>on_node_complete: collections.abc.Callable | None = None,<br>logger: collections.abc.Callable | None = None,<br>name: str | None = None,<br>connect_timeout: int | None = None,<br>auth_timeout: int | None = None) ‑> dict[str, typing.Any]</span>
</code></dt> </code></dt>
<dd> <dd>
<details class="source"> <details class="source">
@@ -206,20 +446,49 @@ el.replaceWith(d);
<pre><code class="python">def run_commands( <pre><code class="python">def run_commands(
self, self,
nodes_filter: str, nodes_filter: str,
commands: List[str], commands: list[str],
variables: Optional[Dict[str, Any]] = None, variables: dict[str, Any] | None = None,
parallel: int = 10, parallel: int = 10,
timeout: int = 20, timeout: int | None = None,
folder: Optional[str] = None, folder: str | None = None,
prompt: Optional[str] = None, prompt: str | None = None,
on_node_complete: Optional[Callable] = None, on_node_complete: Callable | None = None,
logger: Optional[Callable] = None, logger: Callable | None = None,
name: Optional[str] = None name: str | None = None,
) -&gt; Dict[str, str]: connect_timeout: int | None = None,
auth_timeout: int | None = None,
) -&gt; dict[str, Any]:
&#34;&#34;&#34;Execute commands on a set of nodes.&#34;&#34;&#34; &#34;&#34;&#34;Execute commands on a set of nodes.&#34;&#34;&#34;
try: try:
self._ensure_backend_supports_stage_timeouts(
connect_timeout=connect_timeout,
auth_timeout=auth_timeout,
)
if self._is_remote_backend():
batches = self._build_remote_filter_batches(nodes_filter)
stub = self._get_remote_stub()
batch_results = [
stub.run_commands(
nodes_filter=batch,
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 &#34;&#34;,
)
for batch in batches
]
return self._aggregate_remote_results(batch_results)
matched_names = self.config._getallnodes(nodes_filter) matched_names = self.config._getallnodes(nodes_filter)
ctx_svc = self._get_context_service()
if ctx_svc is not None and not ctx_svc.is_default_all_context():
matched_names = ctx_svc.filter_node_list(result=matched_names)
if not matched_names: if not matched_names:
raise ConnpyError(f&#34;No nodes found matching filter: {nodes_filter}&#34;) raise ConnpyError(f&#34;No nodes found matching filter: {nodes_filter}&#34;)
@@ -227,33 +496,52 @@ el.replaceWith(d);
executor = Nodes(node_data, config=self.config) executor = Nodes(node_data, config=self.config)
self.last_executor = executor self.last_executor = executor
results = executor.run( run_kwargs: dict[str, Any] = {
commands=commands, &#34;commands&#34;: commands,
vars=variables, &#34;vars&#34;: variables,
parallel=parallel, &#34;parallel&#34;: parallel,
timeout=timeout, &#34;timeout&#34;: timeout,
folder=folder, &#34;folder&#34;: folder,
prompt=prompt, &#34;prompt&#34;: prompt,
on_complete=on_node_complete, &#34;on_complete&#34;: on_node_complete,
logger=logger &#34;logger&#34;: logger,
) &#34;connect_timeout&#34;: connect_timeout,
&#34;auth_timeout&#34;: auth_timeout,
&#34;_default_cmd_timeout&#34;: 20,
}
run_params = inspect.signature(executor.run).parameters
if &#34;unattended&#34; in run_params:
run_kwargs[&#34;unattended&#34;] = True
if &#34;attempt_tracker&#34; in run_params:
run_kwargs[&#34;attempt_tracker&#34;] = self.attempt_tracker
# Combine output and status for the caller results = executor.run(**run_kwargs)
fb_map = getattr(executor, &#34;fallback_used&#34;, {})
fb_dict = fb_map if isinstance(fb_map, dict) else {}
# Combine output, status, and fallback metadata for the caller
full_results = {} full_results = {}
self.last_fallback_used = {}
for unique in results: for unique in results:
fb_used = bool(fb_dict.get(unique, False))
full_results[unique] = { full_results[unique] = {
&#34;output&#34;: results[unique], &#34;output&#34;: results[unique],
&#34;status&#34;: executor.status.get(unique, 1) &#34;status&#34;: executor.status.get(unique, 1),
&#34;fallback_used&#34;: fb_used,
} }
self.last_fallback_used[unique] = fb_used
return full_results return full_results
except Exception as e: except ConnpyError:
raise ConnpyError(f&#34;Execution failed: {e}&#34;)</code></pre> raise
except Exception as e: # noqa: BLE001
raise_sanitized_auth_error(e, code=&#34;AUTH_FAILED&#34;, context_label=&#34;execution failed&#34;)</code></pre>
</details> </details>
<div class="desc"><p>Execute commands on a set of nodes.</p></div> <div class="desc"><p>Execute commands on a set of nodes.</p></div>
</dd> </dd>
<dt id="connpy.services.execution_service.ExecutionService.test_commands"><code class="name flex"> <dt id="connpy.services.execution_service.ExecutionService.test_commands"><code class="name flex">
<span>def <span class="ident">test_commands</span></span>(<span>self,<br>nodes_filter: str,<br>commands: List[str],<br>expected: List[str],<br>variables: Dict[str, Any] | None = None,<br>parallel: int = 10,<br>timeout: int = 20,<br>folder: str | None = None,<br>prompt: str | None = None,<br>on_node_complete: Callable | None = None,<br>logger: Callable | None = None,<br>name: str | None = None) ‑> Dict[str, Dict[str, bool]]</span> <span>def <span class="ident">test_commands</span></span>(<span>self,<br>nodes_filter: str,<br>commands: list[str],<br>expected: list[str],<br>variables: dict[str, typing.Any] | None = None,<br>parallel: int = 10,<br>timeout: int | None = None,<br>folder: str | None = None,<br>prompt: str | None = None,<br>on_node_complete: collections.abc.Callable | None = None,<br>logger: collections.abc.Callable | None = None,<br>name: str | None = None,<br>connect_timeout: int | None = None,<br>auth_timeout: int | None = None) ‑> dict[str, dict[str, bool]]</span>
</code></dt> </code></dt>
<dd> <dd>
<details class="source"> <details class="source">
@@ -263,21 +551,51 @@ el.replaceWith(d);
<pre><code class="python">def test_commands( <pre><code class="python">def test_commands(
self, self,
nodes_filter: str, nodes_filter: str,
commands: List[str], commands: list[str],
expected: List[str], expected: list[str],
variables: Optional[Dict[str, Any]] = None, variables: dict[str, Any] | None = None,
parallel: int = 10, parallel: int = 10,
timeout: int = 20, timeout: int | None = None,
folder: Optional[str] = None, folder: str | None = None,
prompt: Optional[str] = None, prompt: str | None = None,
on_node_complete: Optional[Callable] = None, on_node_complete: Callable | None = None,
logger: Optional[Callable] = None, logger: Callable | None = None,
name: Optional[str] = None name: str | None = None,
) -&gt; Dict[str, Dict[str, bool]]: connect_timeout: int | None = None,
auth_timeout: int | None = None,
) -&gt; dict[str, dict[str, bool]]:
&#34;&#34;&#34;Run commands and verify expected output on a set of nodes.&#34;&#34;&#34; &#34;&#34;&#34;Run commands and verify expected output on a set of nodes.&#34;&#34;&#34;
try: try:
self._ensure_backend_supports_stage_timeouts(
connect_timeout=connect_timeout,
auth_timeout=auth_timeout,
)
if self._is_remote_backend():
batches = self._build_remote_filter_batches(nodes_filter)
stub = self._get_remote_stub()
batch_results = [
stub.test_commands(
nodes_filter=batch,
commands=commands,
expected=expected,
variables=variables,
parallel=parallel,
timeout=timeout if timeout is not None else 20,
folder=folder or &#34;&#34;,
prompt=prompt,
on_node_complete=on_node_complete,
logger=logger,
name=name or &#34;&#34;,
)
for batch in batches
]
return self._aggregate_remote_results(batch_results)
matched_names = self.config._getallnodes(nodes_filter) matched_names = self.config._getallnodes(nodes_filter)
ctx_svc = self._get_context_service()
if ctx_svc is not None and not ctx_svc.is_default_all_context():
matched_names = ctx_svc.filter_node_list(result=matched_names)
if not matched_names: if not matched_names:
raise ConnpyError(f&#34;No nodes found matching filter: {nodes_filter}&#34;) raise ConnpyError(f&#34;No nodes found matching filter: {nodes_filter}&#34;)
@@ -285,20 +603,38 @@ el.replaceWith(d);
executor = Nodes(node_data, config=self.config) executor = Nodes(node_data, config=self.config)
self.last_executor = executor self.last_executor = executor
results = executor.test( test_kwargs: dict[str, Any] = {
commands=commands, &#34;commands&#34;: commands,
expected=expected, &#34;expected&#34;: expected,
vars=variables, &#34;vars&#34;: variables,
parallel=parallel, &#34;parallel&#34;: parallel,
timeout=timeout, &#34;timeout&#34;: timeout,
folder=folder, &#34;folder&#34;: folder,
prompt=prompt, &#34;prompt&#34;: prompt,
on_complete=on_node_complete, &#34;on_complete&#34;: on_node_complete,
logger=logger &#34;logger&#34;: logger,
) &#34;connect_timeout&#34;: connect_timeout,
return results &#34;auth_timeout&#34;: auth_timeout,
except Exception as e: &#34;_default_cmd_timeout&#34;: 20,
raise ConnpyError(f&#34;Testing failed: {e}&#34;)</code></pre> }
test_params = inspect.signature(executor.test).parameters
if &#34;unattended&#34; in test_params:
test_kwargs[&#34;unattended&#34;] = True
if &#34;attempt_tracker&#34; in test_params:
test_kwargs[&#34;attempt_tracker&#34;] = self.attempt_tracker
res = executor.test(**test_kwargs)
fb_map = getattr(executor, &#34;fallback_used&#34;, {})
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=&#34;AUTH_FAILED&#34;, context_label=&#34;testing failed&#34;)</code></pre>
</details> </details>
<div class="desc"><p>Run commands and verify expected output on a set of nodes.</p></div> <div class="desc"><p>Run commands and verify expected output on a set of nodes.</p></div>
</dd> </dd>
+244 -50
View File
@@ -48,7 +48,7 @@ el.replaceWith(d);
<dl> <dl>
<dt id="connpy.services.import_export_service.ImportExportService"><code class="flex name class"> <dt id="connpy.services.import_export_service.ImportExportService"><code class="flex name class">
<span>class <span class="ident">ImportExportService</span></span> <span>class <span class="ident">ImportExportService</span></span>
<span>(</span><span>config=None)</span> <span>(</span><span>config=None, enforce_context: bool = True)</span>
</code></dt> </code></dt>
<dd> <dd>
<details class="source"> <details class="source">
@@ -58,6 +58,44 @@ el.replaceWith(d);
<pre><code class="python">class ImportExportService(BaseService): <pre><code class="python">class ImportExportService(BaseService):
&#34;&#34;&#34;Business logic for YAML/JSON inventory import and export.&#34;&#34;&#34; &#34;&#34;&#34;Business logic for YAML/JSON inventory import and export.&#34;&#34;&#34;
def __init__(self, config=None, enforce_context: bool = True):
super().__init__(config)
self.enforce_context = bool(enforce_context)
def _get_context_service(self, cfg=None):
target_cfg = cfg or self.config
if (
not getattr(self, &#34;enforce_context&#34;, True)
or not getattr(target_cfg, &#34;_enforce_context&#34;, True)
or (len(sys.argv) &gt; 1 and sys.argv[1] == &#34;api&#34;)
or not isinstance(getattr(target_cfg, &#34;config&#34;, None), dict)
):
return None
from .context_service import ContextService
return ContextService(target_cfg)
def _get_raw_folders(self, cfg=None) -&gt; list[str]:
target_cfg = cfg or self.config
if target_cfg is None:
return []
if (
not hasattr(target_cfg, &#34;assert_called&#34;)
and callable(getattr(target_cfg, &#34;_getallfolders_unlocked&#34;, None))
and hasattr(target_cfg, &#34;_mutation_lock&#34;)
):
with target_cfg._mutation_lock:
return target_cfg._getallfolders_unlocked()
return target_cfg._getallfolders()
def _folder_exists(self, folder_id: str, raw_folders: list[str], cfg=None) -&gt; bool:
target_cfg = cfg or self.config
cfg_dict = getattr(target_cfg, &#34;config&#34;, None)
case = cfg_dict.get(&#34;case&#34;, False) if isinstance(cfg_dict, dict) else False
if case:
return folder_id in raw_folders
return folder_id.lower() in {f.lower() for f in raw_folders}
def export_to_file(self, file_path, folders=None): def export_to_file(self, file_path, folders=None):
&#34;&#34;&#34;Export nodes/folders to a YAML file.&#34;&#34;&#34; &#34;&#34;&#34;Export nodes/folders to a YAML file.&#34;&#34;&#34;
if os.path.exists(file_path): if os.path.exists(file_path):
@@ -72,16 +110,42 @@ el.replaceWith(d);
def export_to_dict(self, folders=None): def export_to_dict(self, folders=None):
&#34;&#34;&#34;Export nodes/folders to a dictionary.&#34;&#34;&#34; &#34;&#34;&#34;Export nodes/folders to a dictionary.&#34;&#34;&#34;
if not folders: ctx_svc = self._get_context_service()
return deepcopy(self.config.connections) ctx_active = ctx_svc is not None and not ctx_svc.is_default_all_context()
else:
# Validate folders exist
for f in folders:
if f != &#34;@&#34; and f not in self.config._getallfolders():
raise NodeNotFoundError(f&#34;Folder &#39;{f}&#39; not found.&#34;)
if not folders and not ctx_active:
return deepcopy(self.config.connections)
raw_folders = self._get_raw_folders()
if folders:
for f in folders:
if f != &#34;@&#34; and (
not self._folder_exists(f, raw_folders)
or (ctx_active and not ctx_svc.matches_active_context(f))
):
raise NodeNotFoundError(f&#34;Folder &#39;{f}&#39; not found.&#34;)
flat = self.config._getallnodesfull(folders, extract=False) flat = self.config._getallnodesfull(folders, extract=False)
allowed_folders = []
else:
allowed_folders = ctx_svc.filter_node_list(result=raw_folders)
flat = self.config._getallnodesfull(extract=False)
if ctx_active:
flat = ctx_svc.filter_node_dict(result=flat)
nested = {} nested = {}
for folder_id in allowed_folders:
uniques = self.config._explode_unique(folder_id)
if not uniques or &#34;folder&#34; not in uniques:
continue
f_name = uniques[&#34;folder&#34;]
if f_name not in nested:
nested[f_name] = {&#34;type&#34;: &#34;folder&#34;}
if &#34;subfolder&#34; in uniques:
s_name = uniques[&#34;subfolder&#34;]
if s_name not in nested[f_name]:
nested[f_name][s_name] = {&#34;type&#34;: &#34;subfolder&#34;}
for k, v in flat.items(): for k, v in flat.items():
uniques = self.config._explode_unique(k) uniques = self.config._explode_unique(k)
if not uniques: if not uniques:
@@ -126,22 +190,52 @@ el.replaceWith(d);
raise InvalidConfigurationError(f&#34;Failed to read/parse import file: {e}&#34;) raise InvalidConfigurationError(f&#34;Failed to read/parse import file: {e}&#34;)
def import_from_dict(self, data): def import_from_dict(self, data):
&#34;&#34;&#34;Import nodes/folders from a dictionary.&#34;&#34;&#34; &#34;&#34;&#34;Import nodes/folders from a dictionary atomically with context pre-validation.&#34;&#34;&#34;
if not isinstance(data, dict): if not isinstance(data, dict):
raise InvalidConfigurationError(&#34;Invalid import data format: expected a dictionary of nodes.&#34;) raise InvalidConfigurationError(
&#34;Invalid import data format: expected a dictionary of nodes.&#34;
)
def _traverse_import(node_data, current_folder=&#39;&#39;, current_subfolder=&#39;&#39;): ctx_svc = self._get_context_service()
ctx_active = ctx_svc is not None and not ctx_svc.is_default_all_context()
raw_folders = self._get_raw_folders() if ctx_active else []
planned_ops = []
def _plan_import(node_data, current_folder=&#34;&#34;, current_subfolder=&#34;&#34;):
for k, v in node_data.items(): for k, v in node_data.items():
if k == &#34;type&#34;: if k == &#34;type&#34; or not isinstance(v, dict):
continue continue
if isinstance(v, dict):
node_type = v.get(&#34;type&#34;, &#34;connection&#34;) node_type = v.get(&#34;type&#34;, &#34;connection&#34;)
if node_type == &#34;folder&#34;: if node_type == &#34;folder&#34;:
self.config._folder_add(folder=k) folder_id = f&#34;@{k}&#34;
_traverse_import(v, current_folder=k, current_subfolder=&#39;&#39;) has_children = any(
isinstance(cv, dict) for ck, cv in v.items() if ck != &#34;type&#34;
)
if ctx_active and (
not has_children
or (
not ctx_svc.matches_active_context(folder_id)
and not self._folder_exists(folder_id, raw_folders)
)
):
ctx_svc.validate_in_active_context(folder_id, is_folder=True)
planned_ops.append((&#34;folder&#34;, k, &#34;&#34;))
_plan_import(v, current_folder=k, current_subfolder=&#34;&#34;)
elif node_type == &#34;subfolder&#34;: elif node_type == &#34;subfolder&#34;:
self.config._folder_add(folder=current_folder, subfolder=k) folder_id = f&#34;@{k}@{current_folder}&#34;
_traverse_import(v, current_folder=current_folder, current_subfolder=k) has_children = any(
isinstance(cv, dict) for ck, cv in v.items() if ck != &#34;type&#34;
)
if ctx_active and (
not has_children
or (
not ctx_svc.matches_active_context(folder_id)
and not self._folder_exists(folder_id, raw_folders)
)
):
ctx_svc.validate_in_active_context(folder_id, is_folder=True)
planned_ops.append((&#34;folder&#34;, current_folder, k))
_plan_import(v, current_folder=current_folder, current_subfolder=k)
elif node_type == &#34;connection&#34;: elif node_type == &#34;connection&#34;:
unique_id = k unique_id = k
if current_subfolder: if current_subfolder:
@@ -149,19 +243,41 @@ el.replaceWith(d);
elif current_folder: elif current_folder:
unique_id = f&#34;{k}@{current_folder}&#34; unique_id = f&#34;{k}@{current_folder}&#34;
self._validate_node_name(unique_id) self._validate_node_name(unique_id)
NodeService._validate_node_payload(v)
if ctx_svc is not None:
ctx_svc.validate_in_active_context(unique_id, is_folder=False)
kwargs = deepcopy(v) kwargs = deepcopy(v)
kwargs[&#39;id&#39;] = k kwargs[&#34;id&#34;] = k
kwargs[&#39;folder&#39;] = current_folder kwargs[&#34;folder&#34;] = current_folder
kwargs[&#39;subfolder&#39;] = current_subfolder kwargs[&#34;subfolder&#34;] = current_subfolder
planned_ops.append((&#34;connection&#34;, kwargs))
self.config._connections_add(**kwargs) _plan_import(data)
if _supports_transactional_update(self.config):
def _mut(cfg):
for op in planned_ops:
if op[0] == &#34;folder&#34;:
cfg._folder_add_unlocked(folder=op[1], subfolder=op[2])
else: else:
# Invalid format skip cfg._connections_add_unlocked(**op[1])
pass
_traverse_import(data) self.config.transactional_update(_mut)
self.config._saveconfig(self.config.file)</code></pre> return
snap_connections = deepcopy(self.config.connections)
try:
for op in planned_ops:
if op[0] == &#34;folder&#34;:
self.config._folder_add(folder=op[1], subfolder=op[2])
else:
self.config._connections_add(**op[1])
self.config._saveconfig(self.config.file)
except BaseException:
self.config.connections.clear()
self.config.connections.update(snap_connections)
raise</code></pre>
</details> </details>
<div class="desc"><p>Business logic for YAML/JSON inventory import and export.</p> <div class="desc"><p>Business logic for YAML/JSON inventory import and export.</p>
<p>Initialize the service.</p> <p>Initialize the service.</p>
@@ -187,16 +303,42 @@ el.replaceWith(d);
</summary> </summary>
<pre><code class="python">def export_to_dict(self, folders=None): <pre><code class="python">def export_to_dict(self, folders=None):
&#34;&#34;&#34;Export nodes/folders to a dictionary.&#34;&#34;&#34; &#34;&#34;&#34;Export nodes/folders to a dictionary.&#34;&#34;&#34;
if not folders: ctx_svc = self._get_context_service()
return deepcopy(self.config.connections) ctx_active = ctx_svc is not None and not ctx_svc.is_default_all_context()
else:
# Validate folders exist
for f in folders:
if f != &#34;@&#34; and f not in self.config._getallfolders():
raise NodeNotFoundError(f&#34;Folder &#39;{f}&#39; not found.&#34;)
if not folders and not ctx_active:
return deepcopy(self.config.connections)
raw_folders = self._get_raw_folders()
if folders:
for f in folders:
if f != &#34;@&#34; and (
not self._folder_exists(f, raw_folders)
or (ctx_active and not ctx_svc.matches_active_context(f))
):
raise NodeNotFoundError(f&#34;Folder &#39;{f}&#39; not found.&#34;)
flat = self.config._getallnodesfull(folders, extract=False) flat = self.config._getallnodesfull(folders, extract=False)
allowed_folders = []
else:
allowed_folders = ctx_svc.filter_node_list(result=raw_folders)
flat = self.config._getallnodesfull(extract=False)
if ctx_active:
flat = ctx_svc.filter_node_dict(result=flat)
nested = {} nested = {}
for folder_id in allowed_folders:
uniques = self.config._explode_unique(folder_id)
if not uniques or &#34;folder&#34; not in uniques:
continue
f_name = uniques[&#34;folder&#34;]
if f_name not in nested:
nested[f_name] = {&#34;type&#34;: &#34;folder&#34;}
if &#34;subfolder&#34; in uniques:
s_name = uniques[&#34;subfolder&#34;]
if s_name not in nested[f_name]:
nested[f_name][s_name] = {&#34;type&#34;: &#34;subfolder&#34;}
for k, v in flat.items(): for k, v in flat.items():
uniques = self.config._explode_unique(k) uniques = self.config._explode_unique(k)
if not uniques: if not uniques:
@@ -261,22 +403,52 @@ el.replaceWith(d);
<span>Expand source code</span> <span>Expand source code</span>
</summary> </summary>
<pre><code class="python">def import_from_dict(self, data): <pre><code class="python">def import_from_dict(self, data):
&#34;&#34;&#34;Import nodes/folders from a dictionary.&#34;&#34;&#34; &#34;&#34;&#34;Import nodes/folders from a dictionary atomically with context pre-validation.&#34;&#34;&#34;
if not isinstance(data, dict): if not isinstance(data, dict):
raise InvalidConfigurationError(&#34;Invalid import data format: expected a dictionary of nodes.&#34;) raise InvalidConfigurationError(
&#34;Invalid import data format: expected a dictionary of nodes.&#34;
)
def _traverse_import(node_data, current_folder=&#39;&#39;, current_subfolder=&#39;&#39;): ctx_svc = self._get_context_service()
ctx_active = ctx_svc is not None and not ctx_svc.is_default_all_context()
raw_folders = self._get_raw_folders() if ctx_active else []
planned_ops = []
def _plan_import(node_data, current_folder=&#34;&#34;, current_subfolder=&#34;&#34;):
for k, v in node_data.items(): for k, v in node_data.items():
if k == &#34;type&#34;: if k == &#34;type&#34; or not isinstance(v, dict):
continue continue
if isinstance(v, dict):
node_type = v.get(&#34;type&#34;, &#34;connection&#34;) node_type = v.get(&#34;type&#34;, &#34;connection&#34;)
if node_type == &#34;folder&#34;: if node_type == &#34;folder&#34;:
self.config._folder_add(folder=k) folder_id = f&#34;@{k}&#34;
_traverse_import(v, current_folder=k, current_subfolder=&#39;&#39;) has_children = any(
isinstance(cv, dict) for ck, cv in v.items() if ck != &#34;type&#34;
)
if ctx_active and (
not has_children
or (
not ctx_svc.matches_active_context(folder_id)
and not self._folder_exists(folder_id, raw_folders)
)
):
ctx_svc.validate_in_active_context(folder_id, is_folder=True)
planned_ops.append((&#34;folder&#34;, k, &#34;&#34;))
_plan_import(v, current_folder=k, current_subfolder=&#34;&#34;)
elif node_type == &#34;subfolder&#34;: elif node_type == &#34;subfolder&#34;:
self.config._folder_add(folder=current_folder, subfolder=k) folder_id = f&#34;@{k}@{current_folder}&#34;
_traverse_import(v, current_folder=current_folder, current_subfolder=k) has_children = any(
isinstance(cv, dict) for ck, cv in v.items() if ck != &#34;type&#34;
)
if ctx_active and (
not has_children
or (
not ctx_svc.matches_active_context(folder_id)
and not self._folder_exists(folder_id, raw_folders)
)
):
ctx_svc.validate_in_active_context(folder_id, is_folder=True)
planned_ops.append((&#34;folder&#34;, current_folder, k))
_plan_import(v, current_folder=current_folder, current_subfolder=k)
elif node_type == &#34;connection&#34;: elif node_type == &#34;connection&#34;:
unique_id = k unique_id = k
if current_subfolder: if current_subfolder:
@@ -284,21 +456,43 @@ el.replaceWith(d);
elif current_folder: elif current_folder:
unique_id = f&#34;{k}@{current_folder}&#34; unique_id = f&#34;{k}@{current_folder}&#34;
self._validate_node_name(unique_id) self._validate_node_name(unique_id)
NodeService._validate_node_payload(v)
if ctx_svc is not None:
ctx_svc.validate_in_active_context(unique_id, is_folder=False)
kwargs = deepcopy(v) kwargs = deepcopy(v)
kwargs[&#39;id&#39;] = k kwargs[&#34;id&#34;] = k
kwargs[&#39;folder&#39;] = current_folder kwargs[&#34;folder&#34;] = current_folder
kwargs[&#39;subfolder&#39;] = current_subfolder kwargs[&#34;subfolder&#34;] = current_subfolder
planned_ops.append((&#34;connection&#34;, kwargs))
self.config._connections_add(**kwargs) _plan_import(data)
if _supports_transactional_update(self.config):
def _mut(cfg):
for op in planned_ops:
if op[0] == &#34;folder&#34;:
cfg._folder_add_unlocked(folder=op[1], subfolder=op[2])
else: else:
# Invalid format skip cfg._connections_add_unlocked(**op[1])
pass
_traverse_import(data) self.config.transactional_update(_mut)
self.config._saveconfig(self.config.file)</code></pre> return
snap_connections = deepcopy(self.config.connections)
try:
for op in planned_ops:
if op[0] == &#34;folder&#34;:
self.config._folder_add(folder=op[1], subfolder=op[2])
else:
self.config._connections_add(**op[1])
self.config._saveconfig(self.config.file)
except BaseException:
self.config.connections.clear()
self.config.connections.update(snap_connections)
raise</code></pre>
</details> </details>
<div class="desc"><p>Import nodes/folders from a dictionary.</p></div> <div class="desc"><p>Import nodes/folders from a dictionary atomically with context pre-validation.</p></div>
</dd> </dd>
<dt id="connpy.services.import_export_service.ImportExportService.import_from_file"><code class="name flex"> <dt id="connpy.services.import_export_service.ImportExportService.import_from_file"><code class="name flex">
<span>def <span class="ident">import_from_file</span></span>(<span>self, file_path)</span> <span>def <span class="ident">import_from_file</span></span>(<span>self, file_path)</span>
File diff suppressed because it is too large. Load diff
+579 -62
View File
@@ -48,7 +48,7 @@ el.replaceWith(d);
<dl> <dl>
<dt id="connpy.services.node_service.NodeService"><code class="flex name class"> <dt id="connpy.services.node_service.NodeService"><code class="flex name class">
<span>class <span class="ident">NodeService</span></span> <span>class <span class="ident">NodeService</span></span>
<span>(</span><span>config=None)</span> <span>(</span><span>config=None, enforce_context: bool = True)</span>
</code></dt> </code></dt>
<dd> <dd>
<details class="source"> <details class="source">
@@ -56,8 +56,148 @@ el.replaceWith(d);
<span>Expand source code</span> <span>Expand source code</span>
</summary> </summary>
<pre><code class="python">class NodeService(BaseService): <pre><code class="python">class NodeService(BaseService):
def __init__(self, config=None): _CONNECTION_FIELDS: ClassVar[frozenset[str]] = frozenset({
&#34;id&#34;,
&#34;host&#34;,
&#34;folder&#34;,
&#34;subfolder&#34;,
&#34;options&#34;,
&#34;logs&#34;,
&#34;password&#34;,
&#34;port&#34;,
&#34;protocol&#34;,
&#34;user&#34;,
&#34;tags&#34;,
&#34;jumphost&#34;,
&#34;fallback&#34;,
&#34;type&#34;,
})
def __init__(self, config=None, enforce_context: bool = True):
super().__init__(config) super().__init__(config)
self.enforce_context = bool(enforce_context)
def _get_context_service(self, cfg=None):
target_cfg = cfg or self.config
if (
not getattr(self, &#34;enforce_context&#34;, True)
or not getattr(target_cfg, &#34;_enforce_context&#34;, True)
or (len(sys.argv) &gt; 1 and sys.argv[1] == &#34;api&#34;)
or not isinstance(getattr(target_cfg, &#34;config&#34;, None), dict)
):
return None
from .context_service import ContextService
return ContextService(target_cfg)
def _get_raw_folders(self, cfg=None) -&gt; list[str]:
target_cfg = cfg or self.config
if target_cfg is None:
return []
if (
not hasattr(target_cfg, &#34;assert_called&#34;)
and callable(getattr(target_cfg, &#34;_getallfolders_unlocked&#34;, None))
and hasattr(target_cfg, &#34;_mutation_lock&#34;)
):
with target_cfg._mutation_lock:
return target_cfg._getallfolders_unlocked()
return target_cfg._getallfolders()
def _get_raw_nodes(self, cfg=None) -&gt; list[str]:
target_cfg = cfg or self.config
if target_cfg is None:
return []
if (
not hasattr(target_cfg, &#34;assert_called&#34;)
and callable(getattr(target_cfg, &#34;_getallnodes_unlocked&#34;, None))
and hasattr(target_cfg, &#34;_mutation_lock&#34;)
):
with target_cfg._mutation_lock:
return target_cfg._getallnodes_unlocked()
return target_cfg._getallnodes()
def _derive_parent_folder(self, cfg, unique_id: str, is_folder: bool) -&gt; str | None:
explode_fn = (
cfg._explode_unique_unlocked
if (
not hasattr(cfg, &#34;assert_called&#34;)
and callable(getattr(cfg, &#34;_explode_unique_unlocked&#34;, None))
)
else cfg._explode_unique
)
uniques = explode_fn(unique_id)
if is_folder:
if (
not isinstance(unique_id, str)
or not unique_id.startswith(&#34;@&#34;)
or not isinstance(uniques, dict)
or &#34;folder&#34; not in uniques
):
raise InvalidConfigurationError(f&#34;Invalid folder name &#39;{unique_id}&#39;.&#34;)
if &#34;subfolder&#34; in uniques:
return f&#34;@{uniques[&#39;folder&#39;]}&#34;
return None
if (
not isinstance(unique_id, str)
or unique_id.startswith(&#34;@&#34;)
or not isinstance(uniques, dict)
or not uniques.get(&#34;id&#34;)
):
raise InvalidConfigurationError(f&#34;Invalid node name &#39;{unique_id}&#39;.&#34;)
if &#34;subfolder&#34; in uniques and &#34;folder&#34; in uniques:
return f&#34;@{uniques[&#39;subfolder&#39;]}@{uniques[&#39;folder&#39;]}&#34;
if &#34;folder&#34; in uniques:
return f&#34;@{uniques[&#39;folder&#39;]}&#34;
return None
def _folder_exists(self, parent_folder: str, raw_folders: list[str], cfg=None) -&gt; bool:
target_cfg = cfg or self.config
cfg_dict = getattr(target_cfg, &#34;config&#34;, None)
case = cfg_dict.get(&#34;case&#34;, False) if isinstance(cfg_dict, dict) else False
if case:
return parent_folder in raw_folders
return parent_folder.lower() in {f.lower() for f in raw_folders}
def _ensure_in_active_context(
self, unique_id: str, *, is_folder: bool = False, not_found_msg: str | None = None
) -&gt; None:
ctx_svc = self._get_context_service()
if (
ctx_svc is not None
and not ctx_svc.is_default_all_context()
and not ctx_svc.matches_active_context(unique_id)
):
if not_found_msg is not None:
raise NodeNotFoundError(not_found_msg)
label = &#34;Folder&#34; if is_folder else &#34;Node&#34;
raise NodeNotFoundError(f&#34;{label} &#39;{unique_id}&#39; not found.&#34;)
@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 &#34;tags&#34; in data and data[&#34;tags&#34;] not in (&#34;&#34;, None):
validate_auth_budget_tags(data[&#34;tags&#34;])
if &#34;fallback&#34; in data and data[&#34;fallback&#34;] not in (None, &#34;&#34;, []):
validate_fallback_entries(data[&#34;fallback&#34;])
pw = data.get(&#34;password&#34;)
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): def list_nodes(self, filter_str=None, format_str=None):
@@ -69,6 +209,10 @@ el.replaceWith(d);
flags = re.IGNORECASE if not case_sensitive else 0 flags = re.IGNORECASE if not case_sensitive else 0
nodes = [n for n in nodes if re.search(filter_str, n, flags)] nodes = [n for n in nodes if re.search(filter_str, n, flags)]
ctx_svc = self._get_context_service()
if ctx_svc is not None and not ctx_svc.is_default_all_context():
nodes = ctx_svc.filter_node_list(result=nodes)
if not format_str: if not format_str:
return nodes return nodes
@@ -125,17 +269,21 @@ el.replaceWith(d);
else: else:
flags = re.IGNORECASE if not case_sensitive else 0 flags = re.IGNORECASE if not case_sensitive else 0
folders = [f for f in folders if re.search(filter_str, f, flags)] folders = [f for f in folders if re.search(filter_str, f, flags)]
ctx_svc = self._get_context_service()
if ctx_svc is not None and not ctx_svc.is_default_all_context():
folders = ctx_svc.filter_node_list(result=folders)
return folders return folders
def get_node_details(self, unique_id): def get_node_details(self, unique_id):
&#34;&#34;&#34;Return full configuration dictionary for a specific node.&#34;&#34;&#34; &#34;&#34;&#34;Return full configuration dictionary for a specific node.&#34;&#34;&#34;
self._ensure_in_active_context(unique_id, is_folder=False)
try: try:
details = self.config.getitem(unique_id) details = self.config.getitem(unique_id)
if not details: if not details:
raise NodeNotFoundError(f&#34;Node &#39;{unique_id}&#39; not found.&#34;) raise NodeNotFoundError(f&#34;Node &#39;{unique_id}&#39; not found.&#34;)
return details return details
except (KeyError, TypeError): except (KeyError, TypeError):
raise NodeNotFoundError(f&#34;Node &#39;{unique_id}&#39; not found.&#34;) raise NodeNotFoundError(f&#34;Node &#39;{unique_id}&#39; not found.&#34;) from None
def explode_unique(self, unique_id): def explode_unique(self, unique_id):
&#34;&#34;&#34;Explode a unique ID into a dictionary of its parts.&#34;&#34;&#34; &#34;&#34;&#34;Explode a unique ID into a dictionary of its parts.&#34;&#34;&#34;
@@ -146,25 +294,54 @@ el.replaceWith(d);
self.config._generate_nodes_cache(nodes=nodes, folders=folders, profiles=profiles) self.config._generate_nodes_cache(nodes=nodes, folders=folders, profiles=profiles)
def validate_parent_folder(self, unique_id, is_folder=False): def validate_parent_folder(self, unique_id, is_folder=False):
&#34;&#34;&#34;Check if parent folder exists for a given node unique ID.&#34;&#34;&#34; &#34;&#34;&#34;Check if unique_id is in active context and its parent folder exists.&#34;&#34;&#34;
if is_folder: parent_folder = self._derive_parent_folder(self.config, unique_id, is_folder=is_folder)
uniques = self.config._explode_unique(unique_id) ctx_svc = self._get_context_service()
if uniques and &#34;subfolder&#34; in uniques and &#34;folder&#34; in uniques: if ctx_svc is not None:
parent_folder = f&#34;@{uniques[&#39;folder&#39;]}&#34; ctx_svc.validate_in_active_context(unique_id, is_folder=is_folder)
if parent_folder not in self.config._getallfolders(): if parent_folder is not None and not self._folder_exists(
parent_folder, self._get_raw_folders(), self.config
):
raise NodeNotFoundError(f&#34;Folder &#39;{parent_folder}&#39; not found.&#34;) raise NodeNotFoundError(f&#34;Folder &#39;{parent_folder}&#39; not found.&#34;)
else:
node_folder = unique_id.partition(&#34;@&#34;)[2]
if node_folder:
parent_folder = f&#34;@{node_folder}&#34;
if parent_folder not in self.config._getallfolders():
raise NodeNotFoundError(f&#34;Folder &#39;{parent_folder}&#39; not found.&#34;)
def add_node(self, unique_id, data, is_folder=False): def add_node(self, unique_id, data, is_folder=False):
&#34;&#34;&#34;Logic for adding a new node or folder to configuration.&#34;&#34;&#34; &#34;&#34;&#34;Logic for adding a new node or folder to configuration.&#34;&#34;&#34;
if not is_folder: if not is_folder:
self._validate_node_name(unique_id) self._validate_node_name(unique_id)
self._validate_node_payload(data)
self.validate_parent_folder(unique_id, is_folder=is_folder)
if _supports_transactional_update(self.config):
def _mut(cfg):
all_nodes = cfg._getallnodes_unlocked()
all_folders = cfg._getallfolders_unlocked()
parent_folder = self._derive_parent_folder(cfg, unique_id, is_folder=is_folder)
ctx_svc = self._get_context_service(cfg)
if ctx_svc is not None:
ctx_svc.validate_in_active_context(unique_id, is_folder=is_folder)
if parent_folder is not None and not self._folder_exists(
parent_folder, all_folders, cfg
):
raise NodeNotFoundError(f&#34;Folder &#39;{parent_folder}&#39; not found.&#34;)
if is_folder:
if unique_id in all_folders:
raise NodeAlreadyExistsError(f&#34;Folder &#39;{unique_id}&#39; already exists.&#34;)
uniques = cfg._explode_unique_unlocked(unique_id)
if not uniques:
raise InvalidConfigurationError(f&#34;Invalid folder name &#39;{unique_id}&#39;.&#34;)
cfg._folder_add_unlocked(**uniques)
else:
if unique_id in all_nodes:
raise NodeAlreadyExistsError(f&#34;Node &#39;{unique_id}&#39; already exists.&#34;)
payload = deepcopy(data)
if &#34;id&#34; not in payload:
uniques = cfg._explode_unique_unlocked(unique_id)
if uniques and &#34;id&#34; in uniques:
payload[&#34;id&#34;] = uniques[&#34;id&#34;]
cfg._connections_add_unlocked(**payload)
self.config.transactional_update(_mut)
return
all_nodes = self.config._getallnodes() all_nodes = self.config._getallnodes()
all_folders = self.config._getallfolders() all_folders = self.config._getallfolders()
@@ -176,7 +353,6 @@ el.replaceWith(d);
if not uniques: if not uniques:
raise InvalidConfigurationError(f&#34;Invalid folder name &#39;{unique_id}&#39;.&#34;) raise InvalidConfigurationError(f&#34;Invalid folder name &#39;{unique_id}&#39;.&#34;)
# Check if parent folder exists when creating a subfolder
if &#34;subfolder&#34; in uniques: if &#34;subfolder&#34; in uniques:
self.validate_parent_folder(unique_id, is_folder=True) self.validate_parent_folder(unique_id, is_folder=True)
@@ -186,37 +362,75 @@ el.replaceWith(d);
if unique_id in all_nodes: if unique_id in all_nodes:
raise NodeAlreadyExistsError(f&#34;Node &#39;{unique_id}&#39; already exists.&#34;) raise NodeAlreadyExistsError(f&#34;Node &#39;{unique_id}&#39; already exists.&#34;)
# Check if parent folder exists when creating a node in a folder
self.validate_parent_folder(unique_id) self.validate_parent_folder(unique_id)
# Ensure &#39;id&#39; is in data for config._connections_add payload = deepcopy(data)
if &#34;id&#34; not in data: if &#34;id&#34; not in payload:
uniques = self.config._explode_unique(unique_id) uniques = self.config._explode_unique(unique_id)
if uniques and &#34;id&#34; in uniques: if uniques and &#34;id&#34; in uniques:
data[&#34;id&#34;] = uniques[&#34;id&#34;] payload[&#34;id&#34;] = uniques[&#34;id&#34;]
self.config._connections_add(**data) self.config._connections_add(**payload)
self.config._saveconfig(self.config.file) 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 &#34;id&#34; not in filtered and isinstance(uniques, dict) and &#34;id&#34; in uniques:
filtered[&#34;id&#34;] = uniques[&#34;id&#34;]
self._validate_node_payload(filtered)
return filtered
def update_node(self, unique_id, data, save=True): def update_node(self, unique_id, data, save=True):
&#34;&#34;&#34;Explicitly update an existing node.&#34;&#34;&#34; &#34;&#34;&#34;Explicitly update an existing node.&#34;&#34;&#34;
self._ensure_in_active_context(unique_id, is_folder=False)
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&#34;Node &#39;{unique_id}&#39; not found.&#34;)
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() all_nodes = self.config._getallnodes()
if unique_id not in all_nodes: if unique_id not in all_nodes:
raise NodeNotFoundError(f&#34;Node &#39;{unique_id}&#39; not found.&#34;) raise NodeNotFoundError(f&#34;Node &#39;{unique_id}&#39; not found.&#34;)
# Ensure &#39;id&#39; is in data for config._connections_add
if &#34;id&#34; not in data:
uniques = self.config._explode_unique(unique_id) uniques = self.config._explode_unique(unique_id)
if uniques: existing = self.config.getitem(unique_id, extract=False)
data[&#34;id&#34;] = uniques[&#34;id&#34;] payload = self._build_update_payload(unique_id, data, existing, uniques)
self.config._connections_add(**payload)
# config._connections_add actually handles updates if ID exists correctly
self.config._connections_add(**data)
if save: if save:
self.config._saveconfig(self.config.file) self.config._saveconfig(self.config.file)
def delete_node(self, unique_id, is_folder=False, save=True): def delete_node(self, unique_id, is_folder=False, save=True):
&#34;&#34;&#34;Logic for deleting a node or folder.&#34;&#34;&#34; &#34;&#34;&#34;Logic for deleting a node or folder.&#34;&#34;&#34;
self._ensure_in_active_context(unique_id, is_folder=is_folder)
if _supports_transactional_update(self.config) and save:
def _mut(cfg):
uniques = cfg._explode_unique_unlocked(unique_id)
if not uniques:
label = &#34;Folder&#34; if is_folder else &#34;Node&#34;
raise NodeNotFoundError(f&#34;{label} &#39;{unique_id}&#39; not found or invalid.&#34;)
if is_folder:
cfg._folder_del_unlocked(**uniques)
else:
cfg._connections_del_unlocked(**uniques)
self.config.transactional_update(_mut)
return
if is_folder: if is_folder:
uniques = self.config._explode_unique(unique_id) uniques = self.config._explode_unique(unique_id)
if not uniques: if not uniques:
@@ -233,17 +447,35 @@ el.replaceWith(d);
def connect_node(self, unique_id, sftp=False, debug=False, logger=None): def connect_node(self, unique_id, sftp=False, debug=False, logger=None):
&#34;&#34;&#34;Interact with a node directly.&#34;&#34;&#34; &#34;&#34;&#34;Interact with a node directly.&#34;&#34;&#34;
self._ensure_in_active_context(unique_id, is_folder=False)
from connpy.core import node from connpy.core import node
from ..auth_utils import resolve_fallback_entries
from .profile_service import ProfileService from .profile_service import ProfileService
node_data = self.config.getitem(unique_id, extract=False) node_data = self.config.getitem(unique_id, extract=False)
if not node_data: if not node_data:
raise NodeNotFoundError(f&#34;Node &#39;{unique_id}&#39; not found.&#34;) raise NodeNotFoundError(f&#34;Node &#39;{unique_id}&#39; not found.&#34;)
# Resolve profiles
profile_service = ProfileService(self.config) profile_service = ProfileService(self.config)
resolved_data = profile_service.resolve_node_data(node_data) resolved_data = profile_service.resolve_node_data(node_data)
node_params = inspect.signature(node.__init__).parameters
if &#34;fallback&#34; not in node_params and &#34;fallback&#34; in resolved_data:
raw_fb = resolved_data.pop(&#34;fallback&#34;, None)
if raw_fb not in (None, &#34;&#34;, []):
resolved_fb = resolve_fallback_entries(raw_fb, self.config.profiles)
if resolved_fb:
tags_val = resolved_data.get(&#34;tags&#34;, &#34;&#34;)
if isinstance(tags_val, dict):
tags_copy = deepcopy(tags_val)
tags_copy[&#34;_fallback&#34;] = resolved_fb
resolved_data[&#34;tags&#34;] = tags_copy
elif tags_val in (&#34;&#34;, None):
resolved_data[&#34;tags&#34;] = {&#34;_fallback&#34;: resolved_fb}
else:
resolved_data[&#34;tags&#34;] = {&#34;_raw&#34;: tags_val, &#34;_fallback&#34;: resolved_fb}
n = node(unique_id, **resolved_data, config=self.config) n = node(unique_id, **resolved_data, config=self.config)
if sftp: if sftp:
n.protocol = &#34;sftp&#34; n.protocol = &#34;sftp&#34;
@@ -252,7 +484,48 @@ el.replaceWith(d);
def move_node(self, src_id, dst_id, copy=False): def move_node(self, src_id, dst_id, copy=False):
&#34;&#34;&#34;Move or copy a node.&#34;&#34;&#34; &#34;&#34;&#34;Move or copy a node.&#34;&#34;&#34;
self._ensure_in_active_context(
src_id, is_folder=False, not_found_msg=f&#34;Source node &#39;{src_id}&#39; not found.&#34;
)
self._validate_node_name(dst_id) self._validate_node_name(dst_id)
self.validate_parent_folder(dst_id, is_folder=False)
if _supports_transactional_update(self.config):
def _mut(cfg):
ctx_svc = self._get_context_service(cfg)
if (
ctx_svc is not None
and not ctx_svc.is_default_all_context()
and not ctx_svc.matches_active_context(src_id)
):
raise NodeNotFoundError(f&#34;Source node &#39;{src_id}&#39; not found.&#34;)
parent_folder = self._derive_parent_folder(cfg, dst_id, is_folder=False)
if ctx_svc is not None:
ctx_svc.validate_in_active_context(dst_id, is_folder=False)
if parent_folder is not None and not self._folder_exists(
parent_folder, cfg._getallfolders_unlocked(), cfg
):
raise NodeNotFoundError(f&#34;Folder &#39;{parent_folder}&#39; not found.&#34;)
try:
node_data = cfg.getitem_unlocked(src_id)
except (KeyError, TypeError):
node_data = None
if not node_data:
raise NodeNotFoundError(f&#34;Source node &#39;{src_id}&#39; not found.&#34;)
if dst_id in cfg._getallnodes_unlocked():
raise NodeAlreadyExistsError(f&#34;Destination node &#39;{dst_id}&#39; already exists.&#34;)
new_uniques = cfg._explode_unique_unlocked(dst_id)
if not new_uniques:
raise InvalidConfigurationError(f&#34;Invalid destination format &#39;{dst_id}&#39;.&#34;)
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) node_data = self.config.getitem(src_id)
if not node_data: if not node_data:
@@ -278,6 +551,55 @@ el.replaceWith(d);
def bulk_add(self, ids, hosts, common_data): def bulk_add(self, ids, hosts, common_data):
&#34;&#34;&#34;Add multiple nodes with shared common configuration.&#34;&#34;&#34; &#34;&#34;&#34;Add multiple nodes with shared common configuration.&#34;&#34;&#34;
self._validate_node_payload(common_data)
for uid in ids:
self.validate_parent_folder(uid, is_folder=False)
if _supports_transactional_update(self.config):
def _build_entries(cfg):
all_nodes = cfg._getallnodes_unlocked()
all_folders = cfg._getallfolders_unlocked()
ctx_svc = self._get_context_service(cfg)
planned = []
for i, uid in enumerate(ids):
parent_folder = self._derive_parent_folder(cfg, uid, is_folder=False)
if ctx_svc is not None:
ctx_svc.validate_in_active_context(uid, is_folder=False)
if parent_folder is not None and not self._folder_exists(
parent_folder, all_folders, cfg
):
raise NodeNotFoundError(f&#34;Folder &#39;{parent_folder}&#39; not found.&#34;)
if uid in all_nodes:
continue
try:
self._validate_node_name(uid)
except ReservedNameError:
continue
host = hosts[i] if i &lt; len(hosts) else hosts[0]
uniques = cfg._explode_unique_unlocked(uid)
if not uniques:
continue
node_data = deepcopy(common_data)
node_data.pop(&#34;ids&#34;, None)
node_data.pop(&#34;location&#34;, None)
node_data.update(uniques)
node_data[&#34;host&#34;] = host
node_data[&#34;type&#34;] = &#34;connection&#34;
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 count = 0
all_nodes = self.config._getallnodes() all_nodes = self.config._getallnodes()
@@ -288,8 +610,6 @@ el.replaceWith(d);
try: try:
self._validate_node_name(uid) self._validate_node_name(uid)
except ReservedNameError: except ReservedNameError:
# For bulk, we might want to just skip or log.
# CLI caller will handle if it wants to be strict.
continue continue
host = hosts[i] if i &lt; len(hosts) else hosts[0] host = hosts[i] if i &lt; len(hosts) else hosts[0]
@@ -313,6 +633,21 @@ el.replaceWith(d);
def full_replace(self, connections, profiles): def full_replace(self, connections, profiles):
&#34;&#34;&#34;Replace all connections and profiles with new data.&#34;&#34;&#34; &#34;&#34;&#34;Replace all connections and profiles with new data.&#34;&#34;&#34;
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.connections = connections
self.config.profiles = profiles self.config.profiles = profiles
self.config._saveconfig(self.config.file) self.config._saveconfig(self.config.file)
@@ -350,6 +685,40 @@ el.replaceWith(d);
&#34;&#34;&#34;Logic for adding a new node or folder to configuration.&#34;&#34;&#34; &#34;&#34;&#34;Logic for adding a new node or folder to configuration.&#34;&#34;&#34;
if not is_folder: if not is_folder:
self._validate_node_name(unique_id) self._validate_node_name(unique_id)
self._validate_node_payload(data)
self.validate_parent_folder(unique_id, is_folder=is_folder)
if _supports_transactional_update(self.config):
def _mut(cfg):
all_nodes = cfg._getallnodes_unlocked()
all_folders = cfg._getallfolders_unlocked()
parent_folder = self._derive_parent_folder(cfg, unique_id, is_folder=is_folder)
ctx_svc = self._get_context_service(cfg)
if ctx_svc is not None:
ctx_svc.validate_in_active_context(unique_id, is_folder=is_folder)
if parent_folder is not None and not self._folder_exists(
parent_folder, all_folders, cfg
):
raise NodeNotFoundError(f&#34;Folder &#39;{parent_folder}&#39; not found.&#34;)
if is_folder:
if unique_id in all_folders:
raise NodeAlreadyExistsError(f&#34;Folder &#39;{unique_id}&#39; already exists.&#34;)
uniques = cfg._explode_unique_unlocked(unique_id)
if not uniques:
raise InvalidConfigurationError(f&#34;Invalid folder name &#39;{unique_id}&#39;.&#34;)
cfg._folder_add_unlocked(**uniques)
else:
if unique_id in all_nodes:
raise NodeAlreadyExistsError(f&#34;Node &#39;{unique_id}&#39; already exists.&#34;)
payload = deepcopy(data)
if &#34;id&#34; not in payload:
uniques = cfg._explode_unique_unlocked(unique_id)
if uniques and &#34;id&#34; in uniques:
payload[&#34;id&#34;] = uniques[&#34;id&#34;]
cfg._connections_add_unlocked(**payload)
self.config.transactional_update(_mut)
return
all_nodes = self.config._getallnodes() all_nodes = self.config._getallnodes()
all_folders = self.config._getallfolders() all_folders = self.config._getallfolders()
@@ -361,7 +730,6 @@ el.replaceWith(d);
if not uniques: if not uniques:
raise InvalidConfigurationError(f&#34;Invalid folder name &#39;{unique_id}&#39;.&#34;) raise InvalidConfigurationError(f&#34;Invalid folder name &#39;{unique_id}&#39;.&#34;)
# Check if parent folder exists when creating a subfolder
if &#34;subfolder&#34; in uniques: if &#34;subfolder&#34; in uniques:
self.validate_parent_folder(unique_id, is_folder=True) self.validate_parent_folder(unique_id, is_folder=True)
@@ -371,16 +739,15 @@ el.replaceWith(d);
if unique_id in all_nodes: if unique_id in all_nodes:
raise NodeAlreadyExistsError(f&#34;Node &#39;{unique_id}&#39; already exists.&#34;) raise NodeAlreadyExistsError(f&#34;Node &#39;{unique_id}&#39; already exists.&#34;)
# Check if parent folder exists when creating a node in a folder
self.validate_parent_folder(unique_id) self.validate_parent_folder(unique_id)
# Ensure &#39;id&#39; is in data for config._connections_add payload = deepcopy(data)
if &#34;id&#34; not in data: if &#34;id&#34; not in payload:
uniques = self.config._explode_unique(unique_id) uniques = self.config._explode_unique(unique_id)
if uniques and &#34;id&#34; in uniques: if uniques and &#34;id&#34; in uniques:
data[&#34;id&#34;] = uniques[&#34;id&#34;] payload[&#34;id&#34;] = uniques[&#34;id&#34;]
self.config._connections_add(**data) self.config._connections_add(**payload)
self.config._saveconfig(self.config.file)</code></pre> self.config._saveconfig(self.config.file)</code></pre>
</details> </details>
<div class="desc"><p>Logic for adding a new node or folder to configuration.</p></div> <div class="desc"><p>Logic for adding a new node or folder to configuration.</p></div>
@@ -395,6 +762,55 @@ el.replaceWith(d);
</summary> </summary>
<pre><code class="python">def bulk_add(self, ids, hosts, common_data): <pre><code class="python">def bulk_add(self, ids, hosts, common_data):
&#34;&#34;&#34;Add multiple nodes with shared common configuration.&#34;&#34;&#34; &#34;&#34;&#34;Add multiple nodes with shared common configuration.&#34;&#34;&#34;
self._validate_node_payload(common_data)
for uid in ids:
self.validate_parent_folder(uid, is_folder=False)
if _supports_transactional_update(self.config):
def _build_entries(cfg):
all_nodes = cfg._getallnodes_unlocked()
all_folders = cfg._getallfolders_unlocked()
ctx_svc = self._get_context_service(cfg)
planned = []
for i, uid in enumerate(ids):
parent_folder = self._derive_parent_folder(cfg, uid, is_folder=False)
if ctx_svc is not None:
ctx_svc.validate_in_active_context(uid, is_folder=False)
if parent_folder is not None and not self._folder_exists(
parent_folder, all_folders, cfg
):
raise NodeNotFoundError(f&#34;Folder &#39;{parent_folder}&#39; not found.&#34;)
if uid in all_nodes:
continue
try:
self._validate_node_name(uid)
except ReservedNameError:
continue
host = hosts[i] if i &lt; len(hosts) else hosts[0]
uniques = cfg._explode_unique_unlocked(uid)
if not uniques:
continue
node_data = deepcopy(common_data)
node_data.pop(&#34;ids&#34;, None)
node_data.pop(&#34;location&#34;, None)
node_data.update(uniques)
node_data[&#34;host&#34;] = host
node_data[&#34;type&#34;] = &#34;connection&#34;
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 count = 0
all_nodes = self.config._getallnodes() all_nodes = self.config._getallnodes()
@@ -405,8 +821,6 @@ el.replaceWith(d);
try: try:
self._validate_node_name(uid) self._validate_node_name(uid)
except ReservedNameError: except ReservedNameError:
# For bulk, we might want to just skip or log.
# CLI caller will handle if it wants to be strict.
continue continue
host = hosts[i] if i &lt; len(hosts) else hosts[0] host = hosts[i] if i &lt; len(hosts) else hosts[0]
@@ -440,17 +854,35 @@ el.replaceWith(d);
</summary> </summary>
<pre><code class="python">def connect_node(self, unique_id, sftp=False, debug=False, logger=None): <pre><code class="python">def connect_node(self, unique_id, sftp=False, debug=False, logger=None):
&#34;&#34;&#34;Interact with a node directly.&#34;&#34;&#34; &#34;&#34;&#34;Interact with a node directly.&#34;&#34;&#34;
self._ensure_in_active_context(unique_id, is_folder=False)
from connpy.core import node from connpy.core import node
from ..auth_utils import resolve_fallback_entries
from .profile_service import ProfileService from .profile_service import ProfileService
node_data = self.config.getitem(unique_id, extract=False) node_data = self.config.getitem(unique_id, extract=False)
if not node_data: if not node_data:
raise NodeNotFoundError(f&#34;Node &#39;{unique_id}&#39; not found.&#34;) raise NodeNotFoundError(f&#34;Node &#39;{unique_id}&#39; not found.&#34;)
# Resolve profiles
profile_service = ProfileService(self.config) profile_service = ProfileService(self.config)
resolved_data = profile_service.resolve_node_data(node_data) resolved_data = profile_service.resolve_node_data(node_data)
node_params = inspect.signature(node.__init__).parameters
if &#34;fallback&#34; not in node_params and &#34;fallback&#34; in resolved_data:
raw_fb = resolved_data.pop(&#34;fallback&#34;, None)
if raw_fb not in (None, &#34;&#34;, []):
resolved_fb = resolve_fallback_entries(raw_fb, self.config.profiles)
if resolved_fb:
tags_val = resolved_data.get(&#34;tags&#34;, &#34;&#34;)
if isinstance(tags_val, dict):
tags_copy = deepcopy(tags_val)
tags_copy[&#34;_fallback&#34;] = resolved_fb
resolved_data[&#34;tags&#34;] = tags_copy
elif tags_val in (&#34;&#34;, None):
resolved_data[&#34;tags&#34;] = {&#34;_fallback&#34;: resolved_fb}
else:
resolved_data[&#34;tags&#34;] = {&#34;_raw&#34;: tags_val, &#34;_fallback&#34;: resolved_fb}
n = node(unique_id, **resolved_data, config=self.config) n = node(unique_id, **resolved_data, config=self.config)
if sftp: if sftp:
n.protocol = &#34;sftp&#34; n.protocol = &#34;sftp&#34;
@@ -469,6 +901,21 @@ el.replaceWith(d);
</summary> </summary>
<pre><code class="python">def delete_node(self, unique_id, is_folder=False, save=True): <pre><code class="python">def delete_node(self, unique_id, is_folder=False, save=True):
&#34;&#34;&#34;Logic for deleting a node or folder.&#34;&#34;&#34; &#34;&#34;&#34;Logic for deleting a node or folder.&#34;&#34;&#34;
self._ensure_in_active_context(unique_id, is_folder=is_folder)
if _supports_transactional_update(self.config) and save:
def _mut(cfg):
uniques = cfg._explode_unique_unlocked(unique_id)
if not uniques:
label = &#34;Folder&#34; if is_folder else &#34;Node&#34;
raise NodeNotFoundError(f&#34;{label} &#39;{unique_id}&#39; not found or invalid.&#34;)
if is_folder:
cfg._folder_del_unlocked(**uniques)
else:
cfg._connections_del_unlocked(**uniques)
self.config.transactional_update(_mut)
return
if is_folder: if is_folder:
uniques = self.config._explode_unique(unique_id) uniques = self.config._explode_unique(unique_id)
if not uniques: if not uniques:
@@ -509,6 +956,21 @@ el.replaceWith(d);
</summary> </summary>
<pre><code class="python">def full_replace(self, connections, profiles): <pre><code class="python">def full_replace(self, connections, profiles):
&#34;&#34;&#34;Replace all connections and profiles with new data.&#34;&#34;&#34; &#34;&#34;&#34;Replace all connections and profiles with new data.&#34;&#34;&#34;
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.connections = connections
self.config.profiles = profiles self.config.profiles = profiles
self.config._saveconfig(self.config.file)</code></pre> self.config._saveconfig(self.config.file)</code></pre>
@@ -556,13 +1018,14 @@ el.replaceWith(d);
</summary> </summary>
<pre><code class="python">def get_node_details(self, unique_id): <pre><code class="python">def get_node_details(self, unique_id):
&#34;&#34;&#34;Return full configuration dictionary for a specific node.&#34;&#34;&#34; &#34;&#34;&#34;Return full configuration dictionary for a specific node.&#34;&#34;&#34;
self._ensure_in_active_context(unique_id, is_folder=False)
try: try:
details = self.config.getitem(unique_id) details = self.config.getitem(unique_id)
if not details: if not details:
raise NodeNotFoundError(f&#34;Node &#39;{unique_id}&#39; not found.&#34;) raise NodeNotFoundError(f&#34;Node &#39;{unique_id}&#39; not found.&#34;)
return details return details
except (KeyError, TypeError): except (KeyError, TypeError):
raise NodeNotFoundError(f&#34;Node &#39;{unique_id}&#39; not found.&#34;)</code></pre> raise NodeNotFoundError(f&#34;Node &#39;{unique_id}&#39; not found.&#34;) from None</code></pre>
</details> </details>
<div class="desc"><p>Return full configuration dictionary for a specific node.</p></div> <div class="desc"><p>Return full configuration dictionary for a specific node.</p></div>
</dd> </dd>
@@ -588,6 +1051,9 @@ el.replaceWith(d);
else: else:
flags = re.IGNORECASE if not case_sensitive else 0 flags = re.IGNORECASE if not case_sensitive else 0
folders = [f for f in folders if re.search(filter_str, f, flags)] folders = [f for f in folders if re.search(filter_str, f, flags)]
ctx_svc = self._get_context_service()
if ctx_svc is not None and not ctx_svc.is_default_all_context():
folders = ctx_svc.filter_node_list(result=folders)
return folders</code></pre> return folders</code></pre>
</details> </details>
<div class="desc"><p>Return all unique folders, optionally filtered by regex.</p></div> <div class="desc"><p>Return all unique folders, optionally filtered by regex.</p></div>
@@ -609,6 +1075,10 @@ el.replaceWith(d);
flags = re.IGNORECASE if not case_sensitive else 0 flags = re.IGNORECASE if not case_sensitive else 0
nodes = [n for n in nodes if re.search(filter_str, n, flags)] nodes = [n for n in nodes if re.search(filter_str, n, flags)]
ctx_svc = self._get_context_service()
if ctx_svc is not None and not ctx_svc.is_default_all_context():
nodes = ctx_svc.filter_node_list(result=nodes)
if not format_str: if not format_str:
return nodes return nodes
@@ -663,7 +1133,48 @@ el.replaceWith(d);
</summary> </summary>
<pre><code class="python">def move_node(self, src_id, dst_id, copy=False): <pre><code class="python">def move_node(self, src_id, dst_id, copy=False):
&#34;&#34;&#34;Move or copy a node.&#34;&#34;&#34; &#34;&#34;&#34;Move or copy a node.&#34;&#34;&#34;
self._ensure_in_active_context(
src_id, is_folder=False, not_found_msg=f&#34;Source node &#39;{src_id}&#39; not found.&#34;
)
self._validate_node_name(dst_id) self._validate_node_name(dst_id)
self.validate_parent_folder(dst_id, is_folder=False)
if _supports_transactional_update(self.config):
def _mut(cfg):
ctx_svc = self._get_context_service(cfg)
if (
ctx_svc is not None
and not ctx_svc.is_default_all_context()
and not ctx_svc.matches_active_context(src_id)
):
raise NodeNotFoundError(f&#34;Source node &#39;{src_id}&#39; not found.&#34;)
parent_folder = self._derive_parent_folder(cfg, dst_id, is_folder=False)
if ctx_svc is not None:
ctx_svc.validate_in_active_context(dst_id, is_folder=False)
if parent_folder is not None and not self._folder_exists(
parent_folder, cfg._getallfolders_unlocked(), cfg
):
raise NodeNotFoundError(f&#34;Folder &#39;{parent_folder}&#39; not found.&#34;)
try:
node_data = cfg.getitem_unlocked(src_id)
except (KeyError, TypeError):
node_data = None
if not node_data:
raise NodeNotFoundError(f&#34;Source node &#39;{src_id}&#39; not found.&#34;)
if dst_id in cfg._getallnodes_unlocked():
raise NodeAlreadyExistsError(f&#34;Destination node &#39;{dst_id}&#39; already exists.&#34;)
new_uniques = cfg._explode_unique_unlocked(dst_id)
if not new_uniques:
raise InvalidConfigurationError(f&#34;Invalid destination format &#39;{dst_id}&#39;.&#34;)
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) node_data = self.config.getitem(src_id)
if not node_data: if not node_data:
@@ -699,18 +1210,28 @@ el.replaceWith(d);
</summary> </summary>
<pre><code class="python">def update_node(self, unique_id, data, save=True): <pre><code class="python">def update_node(self, unique_id, data, save=True):
&#34;&#34;&#34;Explicitly update an existing node.&#34;&#34;&#34; &#34;&#34;&#34;Explicitly update an existing node.&#34;&#34;&#34;
self._ensure_in_active_context(unique_id, is_folder=False)
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&#34;Node &#39;{unique_id}&#39; not found.&#34;)
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() all_nodes = self.config._getallnodes()
if unique_id not in all_nodes: if unique_id not in all_nodes:
raise NodeNotFoundError(f&#34;Node &#39;{unique_id}&#39; not found.&#34;) raise NodeNotFoundError(f&#34;Node &#39;{unique_id}&#39; not found.&#34;)
# Ensure &#39;id&#39; is in data for config._connections_add
if &#34;id&#34; not in data:
uniques = self.config._explode_unique(unique_id) uniques = self.config._explode_unique(unique_id)
if uniques: existing = self.config.getitem(unique_id, extract=False)
data[&#34;id&#34;] = uniques[&#34;id&#34;] payload = self._build_update_payload(unique_id, data, existing, uniques)
self.config._connections_add(**payload)
# config._connections_add actually handles updates if ID exists correctly
self.config._connections_add(**data)
if save: if save:
self.config._saveconfig(self.config.file)</code></pre> self.config._saveconfig(self.config.file)</code></pre>
</details> </details>
@@ -725,21 +1246,17 @@ el.replaceWith(d);
<span>Expand source code</span> <span>Expand source code</span>
</summary> </summary>
<pre><code class="python">def validate_parent_folder(self, unique_id, is_folder=False): <pre><code class="python">def validate_parent_folder(self, unique_id, is_folder=False):
&#34;&#34;&#34;Check if parent folder exists for a given node unique ID.&#34;&#34;&#34; &#34;&#34;&#34;Check if unique_id is in active context and its parent folder exists.&#34;&#34;&#34;
if is_folder: parent_folder = self._derive_parent_folder(self.config, unique_id, is_folder=is_folder)
uniques = self.config._explode_unique(unique_id) ctx_svc = self._get_context_service()
if uniques and &#34;subfolder&#34; in uniques and &#34;folder&#34; in uniques: if ctx_svc is not None:
parent_folder = f&#34;@{uniques[&#39;folder&#39;]}&#34; ctx_svc.validate_in_active_context(unique_id, is_folder=is_folder)
if parent_folder not in self.config._getallfolders(): if parent_folder is not None and not self._folder_exists(
raise NodeNotFoundError(f&#34;Folder &#39;{parent_folder}&#39; not found.&#34;) parent_folder, self._get_raw_folders(), self.config
else: ):
node_folder = unique_id.partition(&#34;@&#34;)[2]
if node_folder:
parent_folder = f&#34;@{node_folder}&#34;
if parent_folder not in self.config._getallfolders():
raise NodeNotFoundError(f&#34;Folder &#39;{parent_folder}&#39; not found.&#34;)</code></pre> raise NodeNotFoundError(f&#34;Folder &#39;{parent_folder}&#39; not found.&#34;)</code></pre>
</details> </details>
<div class="desc"><p>Check if parent folder exists for a given node unique ID.</p></div> <div class="desc"><p>Check if unique_id is in active context and its parent folder exists.</p></div>
</dd> </dd>
</dl> </dl>
<h3>Inherited members</h3> <h3>Inherited members</h3>
+364 -113
View File
@@ -58,61 +58,189 @@ el.replaceWith(d);
<pre><code class="python">class ProfileService(BaseService): <pre><code class="python">class ProfileService(BaseService):
&#34;&#34;&#34;Business logic for node profiles management.&#34;&#34;&#34; &#34;&#34;&#34;Business logic for node profiles management.&#34;&#34;&#34;
_ALLOWED_KEYS: ClassVar[frozenset[str]] = frozenset({
&#34;host&#34;,
&#34;options&#34;,
&#34;logs&#34;,
&#34;password&#34;,
&#34;port&#34;,
&#34;protocol&#34;,
&#34;user&#34;,
&#34;tags&#34;,
&#34;jumphost&#34;,
&#34;fallback&#34;,
})
@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 &#34;tags&#34; in data and data[&#34;tags&#34;] not in (&#34;&#34;, None):
validate_auth_budget_tags(data[&#34;tags&#34;])
if &#34;fallback&#34; in data and data[&#34;fallback&#34;] not in (None, &#34;&#34;, []):
validate_fallback_entries(data[&#34;fallback&#34;])
pw = data.get(&#34;password&#34;)
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, &#34;_mutation_lock&#34;, None)
if lock is not None and not hasattr(self.config, &#34;assert_called&#34;):
with lock:
return deepcopy(self.config.profiles)
return deepcopy(self.config.profiles)
def list_profiles(self, filter_str=None): def list_profiles(self, filter_str=None):
&#34;&#34;&#34;List all profile names, optionally filtered.&#34;&#34;&#34; &#34;&#34;&#34;List all profile names, optionally filtered.&#34;&#34;&#34;
profiles = list(self.config.profiles.keys()) profiles = list(self._get_profiles_snapshot().keys())
case_sensitive = self.config.config.get(&#34;case&#34;, False) case_sensitive = self.config.config.get(&#34;case&#34;, False)
if filter_str: if filter_str:
if not case_sensitive: if not case_sensitive:
f_str = filter_str.lower() f_str = filter_str.lower()
return [p for p in profiles if f_str in p.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 return profiles
def get_profile(self, name, resolve=True): def get_profile(self, name, resolve=True, *, _visited=frozenset(), _depth=0):
&#34;&#34;&#34;Get the profile dictionary, optionally resolved.&#34;&#34;&#34; &#34;&#34;&#34;Get the profile dictionary, optionally resolved.&#34;&#34;&#34;
profile = self.config.profiles.get(name) if _depth &gt;= MAX_PROFILE_DEPTH or name in _visited:
raise InvalidConfigurationError(
f&#34;Circular or overly deep profile reference involving &#39;{name}&#39;.&#34;
)
profiles = self._get_profiles_snapshot()
profile = profiles.get(name)
if not profile: if not profile:
raise ProfileNotFoundError(f&#34;Profile &#39;{name}&#39; not found.&#34;) raise ProfileNotFoundError(f&#34;Profile &#39;{name}&#39; not found.&#34;)
if resolve: 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 return profile
def add_profile(self, name, data): def add_profile(self, name, data):
&#34;&#34;&#34;Add a new profile.&#34;&#34;&#34; &#34;&#34;&#34;Add a new profile.&#34;&#34;&#34;
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&#34;Profile &#39;{name}&#39; already exists.&#34;)
cfg._profiles_add_unlocked(id=name, **filtered_data)
self.config.transactional_update(_mut)
return
if name in self.config.profiles: if name in self.config.profiles:
raise ProfileAlreadyExistsError(f&#34;Profile &#39;{name}&#39; already exists.&#34;) raise ProfileAlreadyExistsError(f&#34;Profile &#39;{name}&#39; already exists.&#34;)
# Filter data to match _profiles_add signature and ensure id is passed
allowed_keys = {&#34;host&#34;, &#34;options&#34;, &#34;logs&#34;, &#34;password&#34;, &#34;port&#34;, &#34;protocol&#34;, &#34;user&#34;, &#34;tags&#34;, &#34;jumphost&#34;}
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._profiles_add(id=name, **filtered_data)
self.config._saveconfig(self.config.file) self.config._saveconfig(self.config.file)
def resolve_node_data(self, node_data): def resolve_node_data(
&#34;&#34;&#34;Resolve profile references (@profile) in node data and handle inheritance.&#34;&#34;&#34; self,
resolved = node_data.copy() node_data,
*,
_visited=frozenset(),
_depth=0,
_profiles_snapshot=None,
):
&#34;&#34;&#34;Resolve profile references (@profile) in node data while preserving compose and scalar secrets.&#34;&#34;&#34;
from ..auth_utils import is_compose_expression
# 1. Identify all referenced profiles to support inheritance if _depth &gt; MAX_PROFILE_DEPTH:
referenced_profiles = [] raise InvalidConfigurationError(&#34;Profile inheritance exceeds maximum depth.&#34;)
for value in resolved.values():
if isinstance(value, str) and value.startswith(&#34;@&#34;): profiles = (
referenced_profiles.append(value[1:]) _profiles_snapshot
elif isinstance(value, list): if _profiles_snapshot is not None
else self._get_profiles_snapshot()
)
resolved = deepcopy(node_data)
auth_referenced_profiles = []
for key in (&#34;user&#34;, &#34;password&#34;):
value = resolved.get(key)
if isinstance(value, str):
if not is_compose_expression(value) and value.startswith(&#34;@&#34;):
ref = value[1:]
if key != &#34;password&#34; or ref in profiles:
auth_referenced_profiles.append(ref)
elif isinstance(value, (list, tuple)):
for item in value: for item in value:
if isinstance(item, str) and item.startswith(&#34;@&#34;): if (
referenced_profiles.append(item[1:]) isinstance(item, str)
and not is_compose_expression(item)
and item.startswith(&#34;@&#34;)
):
ref = item[1:]
if key != &#34;password&#34; or ref in profiles:
auth_referenced_profiles.append(ref)
def _get_resolved_ref(prof_name):
if prof_name in _visited or _depth &gt;= MAX_PROFILE_DEPTH:
raise InvalidConfigurationError(
f&#34;Circular or overly deep profile reference involving &#39;{prof_name}&#39;.&#34;
)
prof_raw = profiles.get(prof_name)
if not prof_raw:
raise ProfileNotFoundError(f&#34;Profile &#39;{prof_name}&#39; not found.&#34;)
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 == &#34;fallback&#34;:
continue
if key == &#34;password&#34;:
if isinstance(value, str):
if is_compose_expression(value):
continue
if value.startswith(&#34;@&#34;) and value[1:] in profiles:
prof_obj = _get_resolved_ref(value[1:])
resolved[&#34;password&#34;] = prof_obj.get(&#34;password&#34;, &#34;&#34;)
elif isinstance(value, list):
resolved_list = []
for item in value:
if (
isinstance(item, str)
and not is_compose_expression(item)
and item.startswith(&#34;@&#34;)
and item[1:] in profiles
):
with suppress(ProfileNotFoundError):
prof_obj = _get_resolved_ref(item[1:])
if &#34;password&#34; in prof_obj:
resolved_list.append(prof_obj[&#34;password&#34;])
else:
resolved_list.append(item)
resolved[&#34;password&#34;] = resolved_list
continue
# 2. Resolve explicit references
for key, value in resolved.items():
if isinstance(value, str) and value.startswith(&#34;@&#34;): if isinstance(value, str) and value.startswith(&#34;@&#34;):
profile_name = value[1:] profile_name = value[1:]
try: try:
profile = self.get_profile(profile_name, resolve=True) profile = _get_resolved_ref(profile_name)
resolved[key] = profile.get(key, &#34;&#34;) resolved[key] = profile.get(key, &#34;&#34;)
except ProfileNotFoundError: except ProfileNotFoundError:
resolved[key] = &#34;&#34; resolved[key] = &#34;&#34;
@@ -121,33 +249,29 @@ el.replaceWith(d);
for item in value: for item in value:
if isinstance(item, str) and item.startswith(&#34;@&#34;): if isinstance(item, str) and item.startswith(&#34;@&#34;):
profile_name = item[1:] profile_name = item[1:]
try: with suppress(ProfileNotFoundError):
profile = self.get_profile(profile_name, resolve=True) profile = _get_resolved_ref(profile_name)
if &#34;password&#34; in profile: if &#34;password&#34; in profile:
resolved_list.append(profile[&#34;password&#34;]) resolved_list.append(profile[&#34;password&#34;])
except ProfileNotFoundError:
pass
else: else:
resolved_list.append(item) resolved_list.append(item)
resolved[key] = resolved_list resolved[key] = resolved_list
# 3. Inheritance: Fill empty keys from the first referenced profile dedup_auth_profiles = list(dict.fromkeys(auth_referenced_profiles))
if referenced_profiles: for base_profile_name in dedup_auth_profiles:
base_profile_name = referenced_profiles[0] with suppress(ProfileNotFoundError):
try: base_profile = _get_resolved_ref(base_profile_name)
base_profile = self.get_profile(base_profile_name, resolve=True) for key in (&#34;user&#34;, &#34;password&#34;, &#34;fallback&#34;):
for key, value in base_profile.items(): val = base_profile.get(key)
# Fill if key is missing or empty if (
if key not in resolved or resolved[key] == &#34;&#34; or resolved[key] == [] or resolved[key] is None: key not in resolved or resolved[key] in (&#34;&#34;, [], None)
resolved[key] = value ) and val not in (&#34;&#34;, [], None):
except ProfileNotFoundError: resolved[key] = deepcopy(val)
pass
# 4. Handle default protocol if resolved.get(&#34;protocol&#34;) in (&#34;&#34;, None):
if resolved.get(&#34;protocol&#34;) == &#34;&#34; or resolved.get(&#34;protocol&#34;) is None:
try: try:
default_profile = self.get_profile(&#34;default&#34;, resolve=True) default_profile = _get_resolved_ref(&#34;default&#34;)
resolved[&#34;protocol&#34;] = default_profile.get(&#34;protocol&#34;, &#34;ssh&#34;) resolved[&#34;protocol&#34;] = default_profile.get(&#34;protocol&#34;, &#34;ssh&#34;) or &#34;ssh&#34;
except ProfileNotFoundError: except ProfileNotFoundError:
resolved[&#34;protocol&#34;] = &#34;ssh&#34; resolved[&#34;protocol&#34;] = &#34;ssh&#34;
@@ -155,34 +279,58 @@ el.replaceWith(d);
def delete_profile(self, name): def delete_profile(self, name):
&#34;&#34;&#34;Delete an existing profile, with safety checks.&#34;&#34;&#34; &#34;&#34;&#34;Delete an existing profile, with safety checks.&#34;&#34;&#34;
if name not in self.config.profiles: if _supports_transactional_update(self.config):
def _mut(cfg):
if name not in cfg.profiles:
raise ProfileNotFoundError(f&#34;Profile &#39;{name}&#39; not found.&#34;) raise ProfileNotFoundError(f&#34;Profile &#39;{name}&#39; not found.&#34;)
if name == &#34;default&#34;: if name == &#34;default&#34;:
raise InvalidConfigurationError(&#34;Cannot delete the &#39;default&#39; profile.&#34;) raise InvalidConfigurationError(&#34;Cannot delete the &#39;default&#39; profile.&#34;)
used_by = cfg._profileused_unlocked(name)
if used_by:
raise InvalidConfigurationError(
f&#34;Profile &#39;{name}&#39; is used by nodes: {&#39;, &#39;.join(used_by)}&#34;
)
cfg._profiles_del_unlocked(id=name)
self.config.transactional_update(_mut)
return
if name not in self.config.profiles:
raise ProfileNotFoundError(f&#34;Profile &#39;{name}&#39; not found.&#34;)
if name == &#34;default&#34;:
raise InvalidConfigurationError(&#34;Cannot delete the &#39;default&#39; profile.&#34;)
used_by = self.config._profileused(name) used_by = self.config._profileused(name)
if used_by: if used_by:
# We return the list of nodes using it so the UI can inform the user raise InvalidConfigurationError(
raise InvalidConfigurationError(f&#34;Profile &#39;{name}&#39; is used by nodes: {&#39;, &#39;.join(used_by)}&#34;) f&#34;Profile &#39;{name}&#39; is used by nodes: {&#39;, &#39;.join(used_by)}&#34;
)
self.config._profiles_del(id=name) self.config._profiles_del(id=name)
self.config._saveconfig(self.config.file) self.config._saveconfig(self.config.file)
def update_profile(self, name, data): def update_profile(self, name, data):
&#34;&#34;&#34;Update an existing profile.&#34;&#34;&#34; &#34;&#34;&#34;Update an existing profile.&#34;&#34;&#34;
if _supports_transactional_update(self.config):
def _mut(cfg):
if name not in cfg.profiles:
raise ProfileNotFoundError(f&#34;Profile &#39;{name}&#39; not found.&#34;)
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: if name not in self.config.profiles:
raise ProfileNotFoundError(f&#34;Profile &#39;{name}&#39; not found.&#34;) raise ProfileNotFoundError(f&#34;Profile &#39;{name}&#39; not found.&#34;)
# Merge with existing data
existing = self.get_profile(name, resolve=False) existing = self.get_profile(name, resolve=False)
updated_data = existing.copy() updated_data = deepcopy(existing)
updated_data.update(data) updated_data.update(data)
filtered_data = {k: v for k, v in updated_data.items() if k in self._ALLOWED_KEYS}
# Filter data to match _profiles_add signature self._validate_profile_payload(filtered_data)
allowed_keys = {&#34;host&#34;, &#34;options&#34;, &#34;logs&#34;, &#34;password&#34;, &#34;port&#34;, &#34;protocol&#34;, &#34;user&#34;, &#34;tags&#34;, &#34;jumphost&#34;}
filtered_data = {k: v for k, v in updated_data.items() if k in allowed_keys}
self.config._profiles_add(id=name, **filtered_data) self.config._profiles_add(id=name, **filtered_data)
self.config._saveconfig(self.config.file)</code></pre> self.config._saveconfig(self.config.file)</code></pre>
</details> </details>
@@ -210,13 +358,20 @@ el.replaceWith(d);
</summary> </summary>
<pre><code class="python">def add_profile(self, name, data): <pre><code class="python">def add_profile(self, name, data):
&#34;&#34;&#34;Add a new profile.&#34;&#34;&#34; &#34;&#34;&#34;Add a new profile.&#34;&#34;&#34;
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&#34;Profile &#39;{name}&#39; already exists.&#34;)
cfg._profiles_add_unlocked(id=name, **filtered_data)
self.config.transactional_update(_mut)
return
if name in self.config.profiles: if name in self.config.profiles:
raise ProfileAlreadyExistsError(f&#34;Profile &#39;{name}&#39; already exists.&#34;) raise ProfileAlreadyExistsError(f&#34;Profile &#39;{name}&#39; already exists.&#34;)
# Filter data to match _profiles_add signature and ensure id is passed
allowed_keys = {&#34;host&#34;, &#34;options&#34;, &#34;logs&#34;, &#34;password&#34;, &#34;port&#34;, &#34;protocol&#34;, &#34;user&#34;, &#34;tags&#34;, &#34;jumphost&#34;}
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._profiles_add(id=name, **filtered_data)
self.config._saveconfig(self.config.file)</code></pre> self.config._saveconfig(self.config.file)</code></pre>
</details> </details>
@@ -232,17 +387,31 @@ el.replaceWith(d);
</summary> </summary>
<pre><code class="python">def delete_profile(self, name): <pre><code class="python">def delete_profile(self, name):
&#34;&#34;&#34;Delete an existing profile, with safety checks.&#34;&#34;&#34; &#34;&#34;&#34;Delete an existing profile, with safety checks.&#34;&#34;&#34;
if name not in self.config.profiles: if _supports_transactional_update(self.config):
def _mut(cfg):
if name not in cfg.profiles:
raise ProfileNotFoundError(f&#34;Profile &#39;{name}&#39; not found.&#34;) raise ProfileNotFoundError(f&#34;Profile &#39;{name}&#39; not found.&#34;)
if name == &#34;default&#34;: if name == &#34;default&#34;:
raise InvalidConfigurationError(&#34;Cannot delete the &#39;default&#39; profile.&#34;) raise InvalidConfigurationError(&#34;Cannot delete the &#39;default&#39; profile.&#34;)
used_by = cfg._profileused_unlocked(name)
if used_by:
raise InvalidConfigurationError(
f&#34;Profile &#39;{name}&#39; is used by nodes: {&#39;, &#39;.join(used_by)}&#34;
)
cfg._profiles_del_unlocked(id=name)
self.config.transactional_update(_mut)
return
if name not in self.config.profiles:
raise ProfileNotFoundError(f&#34;Profile &#39;{name}&#39; not found.&#34;)
if name == &#34;default&#34;:
raise InvalidConfigurationError(&#34;Cannot delete the &#39;default&#39; profile.&#34;)
used_by = self.config._profileused(name) used_by = self.config._profileused(name)
if used_by: if used_by:
# We return the list of nodes using it so the UI can inform the user raise InvalidConfigurationError(
raise InvalidConfigurationError(f&#34;Profile &#39;{name}&#39; is used by nodes: {&#39;, &#39;.join(used_by)}&#34;) f&#34;Profile &#39;{name}&#39; is used by nodes: {&#39;, &#39;.join(used_by)}&#34;
)
self.config._profiles_del(id=name) self.config._profiles_del(id=name)
self.config._saveconfig(self.config.file)</code></pre> self.config._saveconfig(self.config.file)</code></pre>
</details> </details>
@@ -256,14 +425,24 @@ el.replaceWith(d);
<summary> <summary>
<span>Expand source code</span> <span>Expand source code</span>
</summary> </summary>
<pre><code class="python">def get_profile(self, name, resolve=True): <pre><code class="python">def get_profile(self, name, resolve=True, *, _visited=frozenset(), _depth=0):
&#34;&#34;&#34;Get the profile dictionary, optionally resolved.&#34;&#34;&#34; &#34;&#34;&#34;Get the profile dictionary, optionally resolved.&#34;&#34;&#34;
profile = self.config.profiles.get(name) if _depth &gt;= MAX_PROFILE_DEPTH or name in _visited:
raise InvalidConfigurationError(
f&#34;Circular or overly deep profile reference involving &#39;{name}&#39;.&#34;
)
profiles = self._get_profiles_snapshot()
profile = profiles.get(name)
if not profile: if not profile:
raise ProfileNotFoundError(f&#34;Profile &#39;{name}&#39; not found.&#34;) raise ProfileNotFoundError(f&#34;Profile &#39;{name}&#39; not found.&#34;)
if resolve: 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</code></pre> return profile</code></pre>
</details> </details>
<div class="desc"><p>Get the profile dictionary, optionally resolved.</p></div> <div class="desc"><p>Get the profile dictionary, optionally resolved.</p></div>
@@ -278,14 +457,13 @@ el.replaceWith(d);
</summary> </summary>
<pre><code class="python">def list_profiles(self, filter_str=None): <pre><code class="python">def list_profiles(self, filter_str=None):
&#34;&#34;&#34;List all profile names, optionally filtered.&#34;&#34;&#34; &#34;&#34;&#34;List all profile names, optionally filtered.&#34;&#34;&#34;
profiles = list(self.config.profiles.keys()) profiles = list(self._get_profiles_snapshot().keys())
case_sensitive = self.config.config.get(&#34;case&#34;, False) case_sensitive = self.config.config.get(&#34;case&#34;, False)
if filter_str: if filter_str:
if not case_sensitive: if not case_sensitive:
f_str = filter_str.lower() f_str = filter_str.lower()
return [p for p in profiles if f_str in p.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</code></pre> return profiles</code></pre>
</details> </details>
@@ -299,26 +477,93 @@ el.replaceWith(d);
<summary> <summary>
<span>Expand source code</span> <span>Expand source code</span>
</summary> </summary>
<pre><code class="python">def resolve_node_data(self, node_data): <pre><code class="python">def resolve_node_data(
&#34;&#34;&#34;Resolve profile references (@profile) in node data and handle inheritance.&#34;&#34;&#34; self,
resolved = node_data.copy() node_data,
*,
_visited=frozenset(),
_depth=0,
_profiles_snapshot=None,
):
&#34;&#34;&#34;Resolve profile references (@profile) in node data while preserving compose and scalar secrets.&#34;&#34;&#34;
from ..auth_utils import is_compose_expression
# 1. Identify all referenced profiles to support inheritance if _depth &gt; MAX_PROFILE_DEPTH:
referenced_profiles = [] raise InvalidConfigurationError(&#34;Profile inheritance exceeds maximum depth.&#34;)
for value in resolved.values():
if isinstance(value, str) and value.startswith(&#34;@&#34;): profiles = (
referenced_profiles.append(value[1:]) _profiles_snapshot
elif isinstance(value, list): if _profiles_snapshot is not None
else self._get_profiles_snapshot()
)
resolved = deepcopy(node_data)
auth_referenced_profiles = []
for key in (&#34;user&#34;, &#34;password&#34;):
value = resolved.get(key)
if isinstance(value, str):
if not is_compose_expression(value) and value.startswith(&#34;@&#34;):
ref = value[1:]
if key != &#34;password&#34; or ref in profiles:
auth_referenced_profiles.append(ref)
elif isinstance(value, (list, tuple)):
for item in value: for item in value:
if isinstance(item, str) and item.startswith(&#34;@&#34;): if (
referenced_profiles.append(item[1:]) isinstance(item, str)
and not is_compose_expression(item)
and item.startswith(&#34;@&#34;)
):
ref = item[1:]
if key != &#34;password&#34; or ref in profiles:
auth_referenced_profiles.append(ref)
def _get_resolved_ref(prof_name):
if prof_name in _visited or _depth &gt;= MAX_PROFILE_DEPTH:
raise InvalidConfigurationError(
f&#34;Circular or overly deep profile reference involving &#39;{prof_name}&#39;.&#34;
)
prof_raw = profiles.get(prof_name)
if not prof_raw:
raise ProfileNotFoundError(f&#34;Profile &#39;{prof_name}&#39; not found.&#34;)
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 == &#34;fallback&#34;:
continue
if key == &#34;password&#34;:
if isinstance(value, str):
if is_compose_expression(value):
continue
if value.startswith(&#34;@&#34;) and value[1:] in profiles:
prof_obj = _get_resolved_ref(value[1:])
resolved[&#34;password&#34;] = prof_obj.get(&#34;password&#34;, &#34;&#34;)
elif isinstance(value, list):
resolved_list = []
for item in value:
if (
isinstance(item, str)
and not is_compose_expression(item)
and item.startswith(&#34;@&#34;)
and item[1:] in profiles
):
with suppress(ProfileNotFoundError):
prof_obj = _get_resolved_ref(item[1:])
if &#34;password&#34; in prof_obj:
resolved_list.append(prof_obj[&#34;password&#34;])
else:
resolved_list.append(item)
resolved[&#34;password&#34;] = resolved_list
continue
# 2. Resolve explicit references
for key, value in resolved.items():
if isinstance(value, str) and value.startswith(&#34;@&#34;): if isinstance(value, str) and value.startswith(&#34;@&#34;):
profile_name = value[1:] profile_name = value[1:]
try: try:
profile = self.get_profile(profile_name, resolve=True) profile = _get_resolved_ref(profile_name)
resolved[key] = profile.get(key, &#34;&#34;) resolved[key] = profile.get(key, &#34;&#34;)
except ProfileNotFoundError: except ProfileNotFoundError:
resolved[key] = &#34;&#34; resolved[key] = &#34;&#34;
@@ -327,39 +572,35 @@ el.replaceWith(d);
for item in value: for item in value:
if isinstance(item, str) and item.startswith(&#34;@&#34;): if isinstance(item, str) and item.startswith(&#34;@&#34;):
profile_name = item[1:] profile_name = item[1:]
try: with suppress(ProfileNotFoundError):
profile = self.get_profile(profile_name, resolve=True) profile = _get_resolved_ref(profile_name)
if &#34;password&#34; in profile: if &#34;password&#34; in profile:
resolved_list.append(profile[&#34;password&#34;]) resolved_list.append(profile[&#34;password&#34;])
except ProfileNotFoundError:
pass
else: else:
resolved_list.append(item) resolved_list.append(item)
resolved[key] = resolved_list resolved[key] = resolved_list
# 3. Inheritance: Fill empty keys from the first referenced profile dedup_auth_profiles = list(dict.fromkeys(auth_referenced_profiles))
if referenced_profiles: for base_profile_name in dedup_auth_profiles:
base_profile_name = referenced_profiles[0] with suppress(ProfileNotFoundError):
try: base_profile = _get_resolved_ref(base_profile_name)
base_profile = self.get_profile(base_profile_name, resolve=True) for key in (&#34;user&#34;, &#34;password&#34;, &#34;fallback&#34;):
for key, value in base_profile.items(): val = base_profile.get(key)
# Fill if key is missing or empty if (
if key not in resolved or resolved[key] == &#34;&#34; or resolved[key] == [] or resolved[key] is None: key not in resolved or resolved[key] in (&#34;&#34;, [], None)
resolved[key] = value ) and val not in (&#34;&#34;, [], None):
except ProfileNotFoundError: resolved[key] = deepcopy(val)
pass
# 4. Handle default protocol if resolved.get(&#34;protocol&#34;) in (&#34;&#34;, None):
if resolved.get(&#34;protocol&#34;) == &#34;&#34; or resolved.get(&#34;protocol&#34;) is None:
try: try:
default_profile = self.get_profile(&#34;default&#34;, resolve=True) default_profile = _get_resolved_ref(&#34;default&#34;)
resolved[&#34;protocol&#34;] = default_profile.get(&#34;protocol&#34;, &#34;ssh&#34;) resolved[&#34;protocol&#34;] = default_profile.get(&#34;protocol&#34;, &#34;ssh&#34;) or &#34;ssh&#34;
except ProfileNotFoundError: except ProfileNotFoundError:
resolved[&#34;protocol&#34;] = &#34;ssh&#34; resolved[&#34;protocol&#34;] = &#34;ssh&#34;
return resolved</code></pre> return resolved</code></pre>
</details> </details>
<div class="desc"><p>Resolve profile references (@profile) in node data and handle inheritance.</p></div> <div class="desc"><p>Resolve profile references (@profile) in node data while preserving compose and scalar secrets.</p></div>
</dd> </dd>
<dt id="connpy.services.profile_service.ProfileService.update_profile"><code class="name flex"> <dt id="connpy.services.profile_service.ProfileService.update_profile"><code class="name flex">
<span>def <span class="ident">update_profile</span></span>(<span>self, name, data)</span> <span>def <span class="ident">update_profile</span></span>(<span>self, name, data)</span>
@@ -371,18 +612,28 @@ el.replaceWith(d);
</summary> </summary>
<pre><code class="python">def update_profile(self, name, data): <pre><code class="python">def update_profile(self, name, data):
&#34;&#34;&#34;Update an existing profile.&#34;&#34;&#34; &#34;&#34;&#34;Update an existing profile.&#34;&#34;&#34;
if _supports_transactional_update(self.config):
def _mut(cfg):
if name not in cfg.profiles:
raise ProfileNotFoundError(f&#34;Profile &#39;{name}&#39; not found.&#34;)
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: if name not in self.config.profiles:
raise ProfileNotFoundError(f&#34;Profile &#39;{name}&#39; not found.&#34;) raise ProfileNotFoundError(f&#34;Profile &#39;{name}&#39; not found.&#34;)
# Merge with existing data
existing = self.get_profile(name, resolve=False) existing = self.get_profile(name, resolve=False)
updated_data = existing.copy() updated_data = deepcopy(existing)
updated_data.update(data) updated_data.update(data)
filtered_data = {k: v for k, v in updated_data.items() if k in self._ALLOWED_KEYS}
# Filter data to match _profiles_add signature self._validate_profile_payload(filtered_data)
allowed_keys = {&#34;host&#34;, &#34;options&#34;, &#34;logs&#34;, &#34;password&#34;, &#34;port&#34;, &#34;protocol&#34;, &#34;user&#34;, &#34;tags&#34;, &#34;jumphost&#34;}
filtered_data = {k: v for k, v in updated_data.items() if k in allowed_keys}
self.config._profiles_add(id=name, **filtered_data) self.config._profiles_add(id=name, **filtered_data)
self.config._saveconfig(self.config.file)</code></pre> self.config._saveconfig(self.config.file)</code></pre>
</details> </details>
+100 -20
View File
@@ -65,7 +65,7 @@ el.replaceWith(d);
</dd> </dd>
<dt id="connpy.services.provider.ServiceProvider"><code class="flex name class"> <dt id="connpy.services.provider.ServiceProvider"><code class="flex name class">
<span>class <span class="ident">ServiceProvider</span></span> <span>class <span class="ident">ServiceProvider</span></span>
<span>(</span><span>config, mode='local', remote_host=None)</span> <span>(</span><span>config, mode='local', remote_host=None, enforce_context: bool = True)</span>
</code></dt> </code></dt>
<dd> <dd>
<details class="source"> <details class="source">
@@ -75,16 +75,22 @@ el.replaceWith(d);
<pre><code class="python">class ServiceProvider: <pre><code class="python">class ServiceProvider:
&#34;&#34;&#34;Dynamic service backend. Transparently provides local or remote services.&#34;&#34;&#34; &#34;&#34;&#34;Dynamic service backend. Transparently provides local or remote services.&#34;&#34;&#34;
def __init__(self, config, mode=&#34;local&#34;, remote_host=None): def __init__(self, config, mode=&#34;local&#34;, remote_host=None, enforce_context: bool = True):
from ..auth_utils import AuthAttemptTracker
self.mode = mode self.mode = mode
self.config = config self.config = config
self.remote_host = remote_host self.remote_host = remote_host
self.enforce_context = bool(enforce_context)
self.auth_tracker = AuthAttemptTracker()
self.nodes = None
self._system = None self._system = None
self._execution = None self._execution = None
self._import_export = None self._import_export = None
self._sync = None self._sync = None
self._users = None self._users = None
self._ai = None self._ai = None
self.set_enforce_context(self.enforce_context)
if mode == &#34;local&#34;: if mode == &#34;local&#34;:
self._init_local() self._init_local()
@@ -93,14 +99,26 @@ el.replaceWith(d);
else: else:
raise ValueError(f&#34;Unknown service mode: {mode}&#34;) raise ValueError(f&#34;Unknown service mode: {mode}&#34;)
def _init_local(self): def set_enforce_context(self, enforce_context: bool) -&gt; None:
from .node_service import NodeService self.enforce_context = bool(enforce_context)
from .profile_service import ProfileService if self.config is not None and not hasattr(self.config, &#34;assert_called&#34;):
from .config_service import ConfigService self.config._enforce_context = self.enforce_context
from .plugin_service import PluginService for svc in (
from .context_service import ContextService getattr(self, &#34;nodes&#34;, None),
self._execution,
self._import_export,
):
if svc is not None and hasattr(svc, &#34;enforce_context&#34;):
svc.enforce_context = self.enforce_context
self.nodes = NodeService(self.config) def _init_local(self):
from .config_service import ConfigService
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, enforce_context=self.enforce_context)
self.profiles = ProfileService(self.config) self.profiles = ProfileService(self.config)
self.config_svc = ConfigService(self.config) self.config_svc = ConfigService(self.config)
self.plugins = PluginService(self.config) self.plugins = PluginService(self.config)
@@ -110,18 +128,28 @@ el.replaceWith(d);
# Allow ConfigService to work locally so the user can revert the mode # Allow ConfigService to work locally so the user can revert the mode
from .config_service import ConfigService from .config_service import ConfigService
from .context_service import ContextService from .context_service import ContextService
self.config_svc = ConfigService(self.config) self.config_svc = ConfigService(self.config)
self.context = ContextService(self.config) self.context = ContextService(self.config)
if not self.remote_host: if not self.remote_host:
raise InvalidConfigurationError(&#34;Remote host must be specified in remote mode&#34;) raise InvalidConfigurationError(&#34;Remote host must be specified in remote mode&#34;)
import grpc
import os import os
import grpc
from ..grpc_layer.stubs import ( from ..grpc_layer.stubs import (
NodeStub, ProfileStub, PluginStub, AIStub, AIStub,
ExecutionStub, ImportExportStub, SystemStub, AuthClientInterceptor,
ConfigStub, AuthClientInterceptor, AuthStub AuthStub,
ConfigStub,
ExecutionStub,
ImportExportStub,
NodeStub,
PluginStub,
ProfileStub,
SystemStub,
) )
def get_token(): def get_token():
@@ -130,11 +158,8 @@ el.replaceWith(d);
return env_token return env_token
token_path = os.path.join(self.config.defaultdir, &#34;.token&#34;) token_path = os.path.join(self.config.defaultdir, &#34;.token&#34;)
if os.path.exists(token_path): if os.path.exists(token_path):
try: with suppress(OSError, UnicodeDecodeError), open(token_path) as f:
with open(token_path, &#34;r&#34;) as f:
return f.read().strip() return f.read().strip()
except Exception:
pass
return None return None
channel = grpc.insecure_channel(self.remote_host) channel = grpc.insecure_channel(self.remote_host)
@@ -151,7 +176,12 @@ el.replaceWith(d);
self.plugins = PluginStub(channel, remote_host=self.remote_host) self.plugins = PluginStub(channel, remote_host=self.remote_host)
self.ai = AIStub(channel, remote_host=self.remote_host) self.ai = AIStub(channel, remote_host=self.remote_host)
self.system = SystemStub(channel, remote_host=self.remote_host) self.system = SystemStub(channel, remote_host=self.remote_host)
self.execution = ExecutionStub(channel, remote_host=self.remote_host) self.execution = ExecutionStub(
channel,
remote_host=self.remote_host,
config=self.config,
node_stub=self.nodes,
)
self.import_export = ImportExportStub(channel, remote_host=self.remote_host) self.import_export = ImportExportStub(channel, remote_host=self.remote_host)
self.auth = AuthStub(channel, remote_host=self.remote_host) self.auth = AuthStub(channel, remote_host=self.remote_host)
@@ -170,7 +200,13 @@ el.replaceWith(d);
def execution(self): def execution(self):
if self._execution is None and self.mode == &#34;local&#34;: if self._execution is None and self.mode == &#34;local&#34;:
from .execution_service import ExecutionService from .execution_service import ExecutionService
self._execution = ExecutionService(self.config) params = inspect.signature(ExecutionService.__init__).parameters
kwargs = {}
if &#34;attempt_tracker&#34; in params:
kwargs[&#34;attempt_tracker&#34;] = self.auth_tracker
if &#34;enforce_context&#34; in params:
kwargs[&#34;enforce_context&#34;] = self.enforce_context
self._execution = ExecutionService(self.config, **kwargs)
return self._execution return self._execution
@execution.setter @execution.setter
@@ -181,6 +217,12 @@ el.replaceWith(d);
def import_export(self): def import_export(self):
if self._import_export is None and self.mode == &#34;local&#34;: if self._import_export is None and self.mode == &#34;local&#34;:
from .import_export_service import ImportExportService from .import_export_service import ImportExportService
params = inspect.signature(ImportExportService.__init__).parameters
if &#34;enforce_context&#34; in params:
self._import_export = ImportExportService(
self.config, enforce_context=self.enforce_context
)
else:
self._import_export = ImportExportService(self.config) self._import_export = ImportExportService(self.config)
return self._import_export return self._import_export
@@ -249,7 +291,13 @@ def ai(self):
def execution(self): def execution(self):
if self._execution is None and self.mode == &#34;local&#34;: if self._execution is None and self.mode == &#34;local&#34;:
from .execution_service import ExecutionService from .execution_service import ExecutionService
self._execution = ExecutionService(self.config) params = inspect.signature(ExecutionService.__init__).parameters
kwargs = {}
if &#34;attempt_tracker&#34; in params:
kwargs[&#34;attempt_tracker&#34;] = self.auth_tracker
if &#34;enforce_context&#34; in params:
kwargs[&#34;enforce_context&#34;] = self.enforce_context
self._execution = ExecutionService(self.config, **kwargs)
return self._execution</code></pre> return self._execution</code></pre>
</details> </details>
<div class="desc"></div> <div class="desc"></div>
@@ -264,6 +312,12 @@ def execution(self):
def import_export(self): def import_export(self):
if self._import_export is None and self.mode == &#34;local&#34;: if self._import_export is None and self.mode == &#34;local&#34;:
from .import_export_service import ImportExportService from .import_export_service import ImportExportService
params = inspect.signature(ImportExportService.__init__).parameters
if &#34;enforce_context&#34; in params:
self._import_export = ImportExportService(
self.config, enforce_context=self.enforce_context
)
else:
self._import_export = ImportExportService(self.config) self._import_export = ImportExportService(self.config)
return self._import_export</code></pre> return self._import_export</code></pre>
</details> </details>
@@ -315,6 +369,31 @@ def users(self):
<div class="desc"></div> <div class="desc"></div>
</dd> </dd>
</dl> </dl>
<h3>Methods</h3>
<dl>
<dt id="connpy.services.provider.ServiceProvider.set_enforce_context"><code class="name flex">
<span>def <span class="ident">set_enforce_context</span></span>(<span>self, enforce_context: bool) ‑> None</span>
</code></dt>
<dd>
<details class="source">
<summary>
<span>Expand source code</span>
</summary>
<pre><code class="python">def set_enforce_context(self, enforce_context: bool) -&gt; None:
self.enforce_context = bool(enforce_context)
if self.config is not None and not hasattr(self.config, &#34;assert_called&#34;):
self.config._enforce_context = self.enforce_context
for svc in (
getattr(self, &#34;nodes&#34;, None),
self._execution,
self._import_export,
):
if svc is not None and hasattr(svc, &#34;enforce_context&#34;):
svc.enforce_context = self.enforce_context</code></pre>
</details>
<div class="desc"></div>
</dd>
</dl>
</dd> </dd>
</dl> </dl>
</section> </section>
@@ -340,6 +419,7 @@ def users(self):
<li><code><a title="connpy.services.provider.ServiceProvider.ai" href="#connpy.services.provider.ServiceProvider.ai">ai</a></code></li> <li><code><a title="connpy.services.provider.ServiceProvider.ai" href="#connpy.services.provider.ServiceProvider.ai">ai</a></code></li>
<li><code><a title="connpy.services.provider.ServiceProvider.execution" href="#connpy.services.provider.ServiceProvider.execution">execution</a></code></li> <li><code><a title="connpy.services.provider.ServiceProvider.execution" href="#connpy.services.provider.ServiceProvider.execution">execution</a></code></li>
<li><code><a title="connpy.services.provider.ServiceProvider.import_export" href="#connpy.services.provider.ServiceProvider.import_export">import_export</a></code></li> <li><code><a title="connpy.services.provider.ServiceProvider.import_export" href="#connpy.services.provider.ServiceProvider.import_export">import_export</a></code></li>
<li><code><a title="connpy.services.provider.ServiceProvider.set_enforce_context" href="#connpy.services.provider.ServiceProvider.set_enforce_context">set_enforce_context</a></code></li>
<li><code><a title="connpy.services.provider.ServiceProvider.sync" href="#connpy.services.provider.ServiceProvider.sync">sync</a></code></li> <li><code><a title="connpy.services.provider.ServiceProvider.sync" href="#connpy.services.provider.ServiceProvider.sync">sync</a></code></li>
<li><code><a title="connpy.services.provider.ServiceProvider.system" href="#connpy.services.provider.ServiceProvider.system">system</a></code></li> <li><code><a title="connpy.services.provider.ServiceProvider.system" href="#connpy.services.provider.ServiceProvider.system">system</a></code></li>
<li><code><a title="connpy.services.provider.ServiceProvider.users" href="#connpy.services.provider.ServiceProvider.users">users</a></code></li> <li><code><a title="connpy.services.provider.ServiceProvider.users" href="#connpy.services.provider.ServiceProvider.users">users</a></code></li>