Compare commits
20
Commits
1caa286e65
..
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
693531ed57 | ||
|
|
b3925fee07 | ||
|
|
1b50c7d3e7 | ||
|
|
fc27747c19 | ||
|
|
175e623d99 | ||
|
|
ecaa836b98 | ||
|
|
f86ce21ee0 | ||
|
|
dddeaf4b03 | ||
|
|
441bf54ac1 | ||
|
|
c6f260efc9 | ||
|
|
d2da49221f | ||
|
|
59640d20a4 | ||
|
|
ad9dd27316 | ||
|
|
89ca1ec621 | ||
|
|
d96d8fb1fa | ||
|
|
79d28ee6c3 | ||
|
|
ef6d1da5e5 | ||
|
|
8e388cc6b6 | ||
|
|
c3995ea6a9 | ||
|
|
951961a5d3 |
No files matched your search
@@ -3,7 +3,7 @@
|
||||
</p>
|
||||
|
||||
|
||||
# Connpy (v6.4.0b3)
|
||||
# Connpy (v6.5.0b1)
|
||||
[](https://pypi.org/pypi/connpy/)
|
||||
[](https://pypi.org/pypi/connpy/)
|
||||
[](https://pypi.org/pypi/connpy/)
|
||||
@@ -137,6 +137,12 @@ Customize connection settings dynamically using tags. Configure per-node setting
|
||||
tags: { "os": "cisco_ios", "prompt": ".*#", "screen_length_command": "terminal length 0" }
|
||||
```
|
||||
|
||||
### 3f. Credential Fallback Chain, `compose:` & Auth Budgets
|
||||
Configure automatic backup credentials (`fallback`) on nodes or `@profile` templates so both interactive (`conn <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
|
||||
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
@@ -5,7 +5,7 @@
|
||||
</p>
|
||||
|
||||
|
||||
# Connpy (v6.4.0b3)
|
||||
# Connpy (v6.5.0b1)
|
||||
[](https://pypi.org/pypi/connpy/)
|
||||
[](https://pypi.org/pypi/connpy/)
|
||||
[](https://pypi.org/pypi/connpy/)
|
||||
@@ -139,6 +139,12 @@ Customize connection settings dynamically using tags. Configure per-node setting
|
||||
tags: { "os": "cisco_ios", "prompt": ".*#", "screen_length_command": "terminal length 0" }
|
||||
```
|
||||
|
||||
### 3f. Credential Fallback Chain, `compose:` & Auth Budgets
|
||||
Configure automatic backup credentials (`fallback`) on nodes or `@profile` templates so both interactive (`conn <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
|
||||
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
@@ -1 +1 @@
|
||||
__version__ = "6.4.0b3"
|
||||
__version__ = "6.5.0b1"
|
||||
File diff suppressed because it is too large.
Load diff
@@ -1,9 +1,22 @@
|
||||
import re
|
||||
import sys
|
||||
|
||||
import yaml
|
||||
|
||||
from .. import printer
|
||||
from ..auth_utils import (
|
||||
MAX_AUTH_PROMPTS_CAP,
|
||||
MAX_HOP_HOST_ATTEMPTS_CAP,
|
||||
MAX_TOTAL_AUTH_ATTEMPTS_CAP,
|
||||
MIN_AUTH_PROMPTS,
|
||||
MIN_HOP_HOST_ATTEMPTS,
|
||||
MIN_TOTAL_AUTH_ATTEMPTS,
|
||||
validate_strict_int_range,
|
||||
)
|
||||
from ..services.exceptions import ConnpyError, InvalidConfigurationError
|
||||
from .help_text import get_instructions
|
||||
|
||||
|
||||
class ConfigHandler:
|
||||
def __init__(self, app):
|
||||
self.app = app
|
||||
@@ -29,7 +42,12 @@ class ConfigHandler:
|
||||
"sync_remote": self.set_sync_remote,
|
||||
"shell_command": self.set_shell_config,
|
||||
"shell_prompt": self.set_shell_config,
|
||||
"shell_os": self.set_shell_config
|
||||
"shell_os": self.set_shell_config,
|
||||
"max_auth_prompts": self.set_auth_budget,
|
||||
"max_total_auth_attempts": self.set_auth_budget,
|
||||
"max_hop_host_attempts": self.set_auth_budget,
|
||||
"max_global_user_attempts": self.set_auth_budget,
|
||||
"disable_fallback_on_insecure_hostkey": self.set_disable_fallback_on_insecure_hostkey,
|
||||
}
|
||||
handler = actions.get(getattr(args, "command", None))
|
||||
if handler:
|
||||
@@ -202,3 +220,67 @@ class ConfigHandler:
|
||||
except (ConnpyError, InvalidConfigurationError) as e:
|
||||
printer.error(str(e))
|
||||
|
||||
def set_auth_budget(self, args):
|
||||
key = args.command
|
||||
raw_val = args.data[0] if isinstance(args.data, list) else args.data
|
||||
bounds_map = {
|
||||
"max_auth_prompts": (MIN_AUTH_PROMPTS, MAX_AUTH_PROMPTS_CAP),
|
||||
"max_total_auth_attempts": (MIN_TOTAL_AUTH_ATTEMPTS, MAX_TOTAL_AUTH_ATTEMPTS_CAP),
|
||||
"max_hop_host_attempts": (MIN_HOP_HOST_ATTEMPTS, MAX_HOP_HOST_ATTEMPTS_CAP),
|
||||
"max_global_user_attempts": (1, 1000),
|
||||
}
|
||||
if key not in bounds_map:
|
||||
raise InvalidConfigurationError(f"Unknown auth budget setting: {key}")
|
||||
|
||||
if isinstance(raw_val, bool):
|
||||
msg = f"Invalid {key}: boolean is not permitted"
|
||||
printer.error(msg)
|
||||
raise InvalidConfigurationError(msg)
|
||||
|
||||
if isinstance(raw_val, int):
|
||||
val = raw_val
|
||||
elif isinstance(raw_val, str) and re.fullmatch(r"[+-]?\d+", raw_val.strip()):
|
||||
val = int(raw_val.strip())
|
||||
else:
|
||||
msg = f"Invalid {key}: expected integer, got {raw_val!r}"
|
||||
printer.error(msg)
|
||||
raise InvalidConfigurationError(msg)
|
||||
|
||||
if val == 0:
|
||||
cfg_obj = self.app.config
|
||||
if not hasattr(cfg_obj, "assert_called") and callable(
|
||||
getattr(cfg_obj, "transactional_update", None)
|
||||
):
|
||||
cfg_obj.transactional_update(lambda c: c.config.pop(key, None))
|
||||
else:
|
||||
cfg_obj.config.pop(key, None)
|
||||
cfg_obj._saveconfig(cfg_obj.file)
|
||||
printer.success("Config saved")
|
||||
return
|
||||
|
||||
min_v, max_v = bounds_map[key]
|
||||
try:
|
||||
validate_strict_int_range(val, key, min_v, max_v)
|
||||
except ValueError as exc:
|
||||
printer.error(str(exc))
|
||||
raise InvalidConfigurationError(str(exc)) from None
|
||||
|
||||
self.app.services.config_svc.update_setting(key, val)
|
||||
printer.success("Config saved")
|
||||
|
||||
def set_disable_fallback_on_insecure_hostkey(self, args):
|
||||
raw_val = args.data[0] if isinstance(args.data, list) else args.data
|
||||
if isinstance(raw_val, bool):
|
||||
val = raw_val
|
||||
elif isinstance(raw_val, str) and raw_val.strip().lower() in ("true", "false"):
|
||||
val = raw_val.strip().lower() == "true"
|
||||
else:
|
||||
msg = "Invalid disable_fallback_on_insecure_hostkey: expected 'true' or 'false'"
|
||||
printer.error(msg)
|
||||
raise InvalidConfigurationError(msg)
|
||||
self.app.services.config_svc.update_setting(
|
||||
"disable_fallback_on_insecure_hostkey",
|
||||
val,
|
||||
)
|
||||
printer.success("Config saved")
|
||||
|
||||
+132
-15
@@ -1,6 +1,77 @@
|
||||
import ast
|
||||
import json
|
||||
|
||||
from ..auth_utils import (
|
||||
MAX_FALLBACK_ENTRIES,
|
||||
MAX_JSON_INPUT_BYTES,
|
||||
MAX_JSON_INPUT_DEPTH,
|
||||
validate_auth_budget_tags,
|
||||
validate_fallback_entries,
|
||||
validate_json_depth_and_size,
|
||||
)
|
||||
from .validators import Validators
|
||||
|
||||
|
||||
def _parse_tags_input(raw_tags):
|
||||
if raw_tags in ("", None):
|
||||
return ""
|
||||
if isinstance(raw_tags, dict):
|
||||
validate_json_depth_and_size(
|
||||
raw_tags,
|
||||
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||
)
|
||||
validate_auth_budget_tags(raw_tags)
|
||||
return raw_tags
|
||||
s = str(raw_tags)
|
||||
if s.startswith("@"):
|
||||
return s
|
||||
if len(s.encode("utf-8", errors="replace")) > MAX_JSON_INPUT_BYTES:
|
||||
raise ValueError(f"Tags exceed maximum size of {MAX_JSON_INPUT_BYTES} bytes")
|
||||
try:
|
||||
parsed = json.loads(s)
|
||||
except (ValueError, TypeError):
|
||||
parsed = ast.literal_eval(s)
|
||||
validate_json_depth_and_size(
|
||||
parsed,
|
||||
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||
)
|
||||
validate_auth_budget_tags(parsed)
|
||||
return parsed
|
||||
|
||||
|
||||
def _parse_fallback_input(raw_fb):
|
||||
if raw_fb in (None, "", []):
|
||||
return ""
|
||||
if isinstance(raw_fb, str):
|
||||
stripped = raw_fb.strip()
|
||||
if not stripped:
|
||||
return ""
|
||||
if stripped.startswith(("[", "{")):
|
||||
parsed = validate_json_depth_and_size(
|
||||
stripped,
|
||||
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||
)
|
||||
return validate_fallback_entries(parsed, max_entries=MAX_FALLBACK_ENTRIES)
|
||||
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:
|
||||
def __init__(self, app):
|
||||
self.app = app
|
||||
@@ -18,6 +89,7 @@ class Forms:
|
||||
questions.append(inquirer.Confirm("jumphost", message="Edit jumphost?"))
|
||||
questions.append(inquirer.Confirm("user", message="Edit User?"))
|
||||
questions.append(inquirer.Confirm("password", message="Edit password?"))
|
||||
questions.append(inquirer.Confirm("fallback", message="Edit fallback credentials?"))
|
||||
return inquirer.prompt(questions)
|
||||
|
||||
def questions_nodes(self, unique, uniques=None, edit=None):
|
||||
@@ -28,11 +100,13 @@ class Forms:
|
||||
defaults["tags"] = ""
|
||||
if "jumphost" not in defaults:
|
||||
defaults["jumphost"] = ""
|
||||
except Exception:
|
||||
defaults = {"host": "", "protocol": "", "port": "", "user": "", "options": "", "logs": "", "tags": "", "password": "", "jumphost": ""}
|
||||
if "fallback" not in defaults:
|
||||
defaults["fallback"] = ""
|
||||
except Exception: # noqa: BLE001
|
||||
defaults = {"host": "", "protocol": "", "port": "", "user": "", "options": "", "logs": "", "tags": "", "password": "", "jumphost": "", "fallback": ""}
|
||||
node = {}
|
||||
if edit is None:
|
||||
edit = {"host": True, "protocol": True, "port": True, "user": True, "password": True, "options": True, "logs": True, "tags": True, "jumphost": True}
|
||||
edit = {"host": True, "protocol": True, "port": True, "user": True, "password": True, "options": True, "logs": True, "tags": True, "jumphost": True, "fallback": True}
|
||||
questions = []
|
||||
if edit["host"]:
|
||||
questions.append(inquirer.Text("host", message="Add Hostname or IP", validate=self.validators.host_validation, default=defaults["host"]))
|
||||
@@ -70,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"]))
|
||||
else:
|
||||
node["password"] = defaults["password"]
|
||||
if not edit.get("fallback"):
|
||||
node["fallback"] = defaults.get("fallback", "")
|
||||
|
||||
answer = inquirer.prompt(questions)
|
||||
if answer is None:
|
||||
@@ -87,12 +163,28 @@ class Forms:
|
||||
passa = inquirer.prompt(passq)
|
||||
if passa is None:
|
||||
return False
|
||||
answer["password"] = passa["password"].split(",")
|
||||
answer["password"] = [p.strip() for p in passa["password"].split(",") if p.strip()]
|
||||
elif answer["password"] == "No Password":
|
||||
answer["password"] = ""
|
||||
|
||||
if "tags" in answer and not answer["tags"].startswith("@") and answer["tags"]:
|
||||
answer["tags"] = ast.literal_eval(answer["tags"])
|
||||
if edit.get("fallback"):
|
||||
raw_def_fb = defaults.get("fallback", "")
|
||||
def_fb_str = json.dumps(raw_def_fb) if isinstance(raw_def_fb, (list, dict)) else str(raw_def_fb or "")
|
||||
fbq = [
|
||||
inquirer.Text(
|
||||
"fallback",
|
||||
message="Add fallback credentials (@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["type"] = "connection"
|
||||
@@ -106,11 +198,13 @@ class Forms:
|
||||
defaults["tags"] = ""
|
||||
if "jumphost" not in defaults:
|
||||
defaults["jumphost"] = ""
|
||||
except Exception:
|
||||
defaults = {"host": "", "protocol": "", "port": "", "user": "", "options": "", "logs": "", "tags": "", "jumphost": ""}
|
||||
if "fallback" not in defaults:
|
||||
defaults["fallback"] = ""
|
||||
except Exception: # noqa: BLE001
|
||||
defaults = {"host": "", "protocol": "", "port": "", "user": "", "options": "", "logs": "", "tags": "", "jumphost": "", "fallback": ""}
|
||||
profile = {}
|
||||
if edit is None:
|
||||
edit = {"host": True, "protocol": True, "port": True, "user": True, "password": True, "options": True, "logs": True, "tags": True, "jumphost": True}
|
||||
edit = {"host": True, "protocol": True, "port": True, "user": True, "password": True, "options": True, "logs": True, "tags": True, "jumphost": True, "fallback": True}
|
||||
questions = []
|
||||
if edit["host"]:
|
||||
questions.append(inquirer.Text("host", message="Add Hostname or IP", default=defaults["host"]))
|
||||
@@ -148,17 +242,24 @@ class Forms:
|
||||
questions.append(inquirer.Password("password", message="Set Password"))
|
||||
else:
|
||||
profile["password"] = defaults["password"]
|
||||
if edit.get("fallback"):
|
||||
raw_def_fb = defaults.get("fallback", "")
|
||||
def_fb_str = json.dumps(raw_def_fb) if isinstance(raw_def_fb, (list, dict)) else str(raw_def_fb or "")
|
||||
questions.append(inquirer.Text("fallback", message="Add fallback credentials (@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)
|
||||
if answer is None:
|
||||
return False
|
||||
|
||||
if "password" in answer:
|
||||
if answer["password"] != "":
|
||||
if "password" in answer and answer["password"] != "":
|
||||
answer["password"] = self.app.services.config_svc.encrypt_password(answer["password"])
|
||||
|
||||
if "tags" in answer and answer["tags"]:
|
||||
answer["tags"] = ast.literal_eval(answer["tags"])
|
||||
answer["tags"] = _parse_tags_input(answer["tags"])
|
||||
if "fallback" in answer:
|
||||
answer["fallback"] = _parse_fallback_input(answer["fallback"])
|
||||
|
||||
result = {**answer, **profile}
|
||||
result["id"] = unique
|
||||
@@ -187,17 +288,33 @@ class Forms:
|
||||
if answer["password"] == "Local Password":
|
||||
passq = [inquirer.Password("password", message="Set Password")]
|
||||
passa = inquirer.prompt(passq)
|
||||
if passa is None:
|
||||
return False
|
||||
answer["password"] = self.app.services.config_svc.encrypt_password(passa["password"])
|
||||
elif answer["password"] == "Profiles":
|
||||
passq = [(inquirer.Text("password", message="Set a @profile or a comma separated list of @profiles", validate=self.validators.pass_validation))]
|
||||
passa = inquirer.prompt(passq)
|
||||
answer["password"] = passa["password"].split(",")
|
||||
if passa is None:
|
||||
return False
|
||||
answer["password"] = [p.strip() for p in passa["password"].split(",") if p.strip()]
|
||||
elif answer["password"] == "No Password":
|
||||
answer["password"] = ""
|
||||
|
||||
fbq = [
|
||||
inquirer.Text(
|
||||
"fallback",
|
||||
message="Add fallback credentials (@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"
|
||||
if "tags" in answer and not answer["tags"].startswith("@") and answer["tags"]:
|
||||
answer["tags"] = ast.literal_eval(answer["tags"])
|
||||
if "tags" in answer and answer["tags"] and not str(answer["tags"]).startswith("@"):
|
||||
answer["tags"] = _parse_tags_input(answer["tags"])
|
||||
|
||||
return answer
|
||||
|
||||
|
||||
@@ -144,6 +144,7 @@ class NodeHandler:
|
||||
uniques = self.app.services.nodes.explode_unique(args.data)
|
||||
if not uniques:
|
||||
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)
|
||||
printer.success(f"{args.data} added successfully")
|
||||
else:
|
||||
|
||||
+143
-26
@@ -1,5 +1,19 @@
|
||||
import re
|
||||
import ast
|
||||
import json
|
||||
import re
|
||||
|
||||
from ..auth_utils import (
|
||||
MAX_FALLBACK_ENTRIES,
|
||||
MAX_JSON_INPUT_BYTES,
|
||||
MAX_JSON_INPUT_DEPTH,
|
||||
SanitizedAuthError,
|
||||
is_compose_expression,
|
||||
validate_auth_budget_tags,
|
||||
validate_compose_syntax,
|
||||
validate_fallback_entries,
|
||||
validate_json_depth_and_size,
|
||||
)
|
||||
|
||||
|
||||
def _raise_val_err(reason):
|
||||
import inquirer
|
||||
@@ -56,50 +70,119 @@ class Validators:
|
||||
return True
|
||||
|
||||
def pass_validation(self, answers, current, regex = "(^@.+$)"):
|
||||
if is_compose_expression(current):
|
||||
try:
|
||||
validate_compose_syntax(current)
|
||||
except SanitizedAuthError as exc:
|
||||
_raise_val_err(str(exc))
|
||||
return True
|
||||
profiles = current.split(",")
|
||||
for i in profiles:
|
||||
if not re.match(regex, i) or i[1:] not in self.app.profiles:
|
||||
_raise_val_err("Profile {} don't exist".format(i))
|
||||
return True
|
||||
|
||||
def _validate_tags_dict(self, current: str):
|
||||
if len(current.encode("utf-8", errors="replace")) > MAX_JSON_INPUT_BYTES:
|
||||
_raise_val_err(f"Tags exceed maximum size of {MAX_JSON_INPUT_BYTES} bytes")
|
||||
parsed = None
|
||||
try:
|
||||
parsed = json.loads(current)
|
||||
except (ValueError, TypeError):
|
||||
try:
|
||||
parsed = ast.literal_eval(current)
|
||||
except (ValueError, TypeError, SyntaxError, MemoryError, RecursionError):
|
||||
parsed = None
|
||||
if not isinstance(parsed, dict):
|
||||
_raise_val_err("Tags should be a python dictionary.")
|
||||
try:
|
||||
validate_json_depth_and_size(
|
||||
parsed,
|
||||
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||
)
|
||||
validate_auth_budget_tags(parsed)
|
||||
except (ValueError, TypeError, SanitizedAuthError) as exc:
|
||||
_raise_val_err(str(exc))
|
||||
return True
|
||||
|
||||
def tags_validation(self, answers, current):
|
||||
if current.startswith("@"):
|
||||
if current[1:] not in self.app.profiles:
|
||||
_raise_val_err("Profile {} don't exist".format(current))
|
||||
elif current != "":
|
||||
isdict = False
|
||||
try:
|
||||
isdict = ast.literal_eval(current)
|
||||
except Exception:
|
||||
pass
|
||||
if not isinstance (isdict, dict):
|
||||
_raise_val_err("Tags should be a python dictionary.".format(current))
|
||||
return self._validate_tags_dict(current)
|
||||
return True
|
||||
|
||||
def profile_tags_validation(self, answers, current):
|
||||
if current != "":
|
||||
isdict = False
|
||||
try:
|
||||
isdict = ast.literal_eval(current)
|
||||
except Exception:
|
||||
pass
|
||||
if not isinstance (isdict, dict):
|
||||
_raise_val_err("Tags should be a python dictionary.".format(current))
|
||||
return self._validate_tags_dict(current)
|
||||
return True
|
||||
|
||||
def fallback_validation(self, answers, current):
|
||||
if current in (None, ""):
|
||||
return True
|
||||
stripped = str(current).strip()
|
||||
if not stripped:
|
||||
return True
|
||||
if len(stripped.encode("utf-8", errors="replace")) > MAX_JSON_INPUT_BYTES:
|
||||
_raise_val_err(f"Fallback exceeds maximum size of {MAX_JSON_INPUT_BYTES} bytes")
|
||||
if stripped.startswith("@") and not 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):
|
||||
if current.startswith("@"):
|
||||
if current[1:] not in self.app.profiles:
|
||||
_raise_val_err("Profile {} don't exist".format(current))
|
||||
elif current != "":
|
||||
if current not in self.app.nodes_list:
|
||||
_raise_val_err("Node {} don't exist.".format(current))
|
||||
_raise_val_err(f"Profile {current} don't exist")
|
||||
elif current != "" and not self._node_exists(current):
|
||||
_raise_val_err(f"Node {current} don't exist.")
|
||||
return True
|
||||
|
||||
def profile_jumphost_validation(self, answers, current):
|
||||
if current != "":
|
||||
if current not in self.app.nodes_list:
|
||||
_raise_val_err("Node {} don't exist.".format(current))
|
||||
if current != "" and not self._node_exists(current):
|
||||
_raise_val_err(f"Node {current} don't exist.")
|
||||
return True
|
||||
|
||||
def default_validation(self, answers, current):
|
||||
@@ -117,12 +200,46 @@ class Validators:
|
||||
return True
|
||||
|
||||
def bulk_folder_validation(self, answers, current):
|
||||
if not self.app.case:
|
||||
case = getattr(self.app, "case", False)
|
||||
if not case:
|
||||
current = current.lower()
|
||||
|
||||
candidate = current
|
||||
if "/" in current:
|
||||
candidate = current.split("/")[0]
|
||||
candidate = current.split("/")[0] if "/" in current else current
|
||||
|
||||
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))
|
||||
if current != "" and len(matches) == 0:
|
||||
|
||||
+18
-1
@@ -248,9 +248,26 @@ def _build_tree(nodes, folders, profiles, plugins, configdir):
|
||||
"--fzf-wrapper": ["bash", "zsh"],
|
||||
"--service-mode": ["local", "remote"],
|
||||
"--sync-remote": ["true", "false"],
|
||||
"--disable-fallback-on-insecure-hostkey": ["true", "false"],
|
||||
"--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["--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}
|
||||
|
||||
+698
-213
File diff suppressed because it is too large.
Load diff
+15
-18
@@ -1,20 +1,20 @@
|
||||
#!/usr/bin/env python3
|
||||
#Imports
|
||||
import argparse
|
||||
import os
|
||||
import re
|
||||
import argparse
|
||||
import sys
|
||||
import yaml
|
||||
import sys
|
||||
from .core import node,nodes
|
||||
from ._version import __version__
|
||||
from . import printer
|
||||
from .api import start_api,stop_api,debug_api
|
||||
|
||||
import yaml
|
||||
from rich_argparse import RichHelpFormatter
|
||||
|
||||
from . import printer
|
||||
from .api import debug_api, start_api, stop_api
|
||||
from .cli.help_text import get_help
|
||||
from .cli.helpers import folders_completer, nodes_completer
|
||||
from .core import node, nodes
|
||||
from .plugins import Plugins
|
||||
from .services.exceptions import ConnpyError, ProfileNotFoundError, ReservedNameError
|
||||
|
||||
from rich_argparse import RichHelpFormatter
|
||||
# Bridge rich-argparse with our design system
|
||||
RichHelpFormatter.console = printer.console
|
||||
RichHelpFormatter.styles.update({
|
||||
@@ -28,14 +28,6 @@ RichHelpFormatter.styles.update({
|
||||
})
|
||||
RichHelpFormatter.group_name_formatter = str.upper
|
||||
|
||||
from .cli import (
|
||||
NodeHandler, ProfileHandler, ConfigHandler, RunHandler,
|
||||
AIHandler, APIHandler, PluginHandler, ImportExportHandler,
|
||||
ContextHandler, SSOHandler
|
||||
)
|
||||
from .cli.helpers import nodes_completer, folders_completer, profiles_completer
|
||||
from .cli.help_text import get_help
|
||||
|
||||
console = printer.console
|
||||
|
||||
#functions and classes
|
||||
@@ -319,7 +311,7 @@ class connapp:
|
||||
aiparser.add_argument("--session", nargs=1, help="Resume a specific AI session by ID")
|
||||
aiparser.add_argument("--resume", action="store_true", help="Resume the most recent AI session")
|
||||
aiparser.add_argument("--delete", "--delete-session", dest="delete_session", nargs=1, help="Delete an AI session by ID")
|
||||
aiparser.add_argument("--mcp", nargs='*', metavar=('ACTION', 'NAME'), help="Manage MCP servers. Actions: list, add, remove, enable, disable. Leave empty for interactive wizard.")
|
||||
aiparser.add_argument("--mcp", nargs=argparse.REMAINDER, metavar=('ACTION', 'NAME'), help="Manage MCP servers. Actions: list, add, remove, enable, disable, serve. Leave empty for interactive wizard.")
|
||||
aiparser.set_defaults(func=self._ai.dispatch)
|
||||
#RUNPARSER
|
||||
runparser = subparsers.add_parser("run", help="Run scripts or commands on nodes", description="Run scripts or commands on nodes", formatter_class=RichHelpFormatter)
|
||||
@@ -389,6 +381,11 @@ class connapp:
|
||||
configcrud.add_argument("--shell-command", dest="shell_command", nargs=1, action=self._store_type, help="Set default shell command", metavar="COMMAND")
|
||||
configcrud.add_argument("--shell-prompt", dest="shell_prompt", nargs=1, action=self._store_type, help="Set shell prompt regex for AI", metavar="REGEX")
|
||||
configcrud.add_argument("--shell-os", dest="shell_os", nargs=1, action=self._store_type, help="Set shell OS hint for AI", metavar="OS")
|
||||
configcrud.add_argument("--max-auth-prompts", dest="max_auth_prompts", nargs=1, action=self._store_type, help="Set max auth prompts per connection (1-5, 0 to reset)", type=int, metavar="INT")
|
||||
configcrud.add_argument("--max-total-auth-attempts", dest="max_total_auth_attempts", nargs=1, action=self._store_type, help="Set max total auth attempts per host (1-10, 0 to reset)", type=int, metavar="INT")
|
||||
configcrud.add_argument("--max-hop-host-attempts", dest="max_hop_host_attempts", nargs=1, action=self._store_type, help="Set max auth attempts per jumphost (1-10, 0 to reset)", type=int, metavar="INT")
|
||||
configcrud.add_argument("--max-global-user-attempts", dest="max_global_user_attempts", nargs=1, action=self._store_type, help="Set max global auth attempts per user (1-1000, 0 to reset)", type=int, metavar="INT")
|
||||
configcrud.add_argument("--disable-fallback-on-insecure-hostkey", dest="disable_fallback_on_insecure_hostkey", nargs=1, action=self._store_type, help="Disable AAA fallback when StrictHostKeyChecking=no", choices=["true", "false"])
|
||||
configparser.add_argument("--trusted-commands", dest="trusted_commands", nargs=1, action=self._store_type, help="Set custom trusted commands regexes (comma separated)", metavar="REGEX,REGEX")
|
||||
configparser.set_defaults(func=self._config.dispatch)
|
||||
|
||||
|
||||
+2178
-330
File diff suppressed because it is too large.
Load diff
+95
-53
@@ -1,26 +1,37 @@
|
||||
import grpc
|
||||
from concurrent import futures
|
||||
from google.protobuf.empty_pb2 import Empty
|
||||
import os
|
||||
import ctypes
|
||||
import threading
|
||||
import contextvars
|
||||
import datetime
|
||||
import functools
|
||||
import json
|
||||
import os
|
||||
from concurrent import futures
|
||||
|
||||
import grpc
|
||||
from google.protobuf.empty_pb2 import Empty
|
||||
|
||||
# Suppress harmless but noisy gRPC fork() warnings from pexpect child processes
|
||||
os.environ["GRPC_VERBOSITY"] = "NONE"
|
||||
os.environ["GRPC_ENABLE_FORK_SUPPORT"] = "0"
|
||||
from . import connpy_pb2, connpy_pb2_grpc, remote_plugin_pb2, remote_plugin_pb2_grpc
|
||||
import json
|
||||
from .utils import to_value, from_value, to_struct, from_struct
|
||||
from ..services.exceptions import ConnpyError
|
||||
from .. import printer
|
||||
from ..auth_utils import (
|
||||
MAX_FALLBACK_ENTRIES,
|
||||
MAX_JSON_INPUT_BYTES,
|
||||
MAX_JSON_INPUT_DEPTH,
|
||||
SanitizedAuthError,
|
||||
sanitize_exception,
|
||||
validate_auth_budget_tags,
|
||||
validate_fallback_entries,
|
||||
validate_json_depth_and_size,
|
||||
)
|
||||
from ..services.exceptions import ConnpyError
|
||||
from . import connpy_pb2, connpy_pb2_grpc, remote_plugin_pb2, remote_plugin_pb2_grpc
|
||||
from .utils import from_struct, from_value, to_struct, to_value
|
||||
|
||||
_current_user = contextvars.ContextVar("current_user", default=None)
|
||||
|
||||
def handle_errors(func):
|
||||
import inspect
|
||||
if inspect.isgeneratorfunction(func):
|
||||
@functools.wraps(func)
|
||||
def wrapper(*args, **kwargs):
|
||||
try:
|
||||
for item in func(*args, **kwargs):
|
||||
@@ -41,6 +52,7 @@ def handle_errors(func):
|
||||
printer.clear_thread_state()
|
||||
return wrapper
|
||||
else:
|
||||
@functools.wraps(func)
|
||||
def wrapper(*args, **kwargs):
|
||||
try:
|
||||
return func(*args, **kwargs)
|
||||
@@ -60,11 +72,19 @@ def handle_errors(func):
|
||||
printer.clear_thread_state()
|
||||
return wrapper
|
||||
|
||||
class NodeServicer(connpy_pb2_grpc.NodeServiceServicer):
|
||||
def __init__(self, provider, registry=None, debug=False):
|
||||
def _ensure_server_provider(provider):
|
||||
if not hasattr(provider, "mode"):
|
||||
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._registry = registry
|
||||
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]")
|
||||
|
||||
if first_req.connection_params_json:
|
||||
import json
|
||||
params = json.loads(first_req.connection_params_json)
|
||||
try:
|
||||
params = validate_json_depth_and_size(
|
||||
first_req.connection_params_json,
|
||||
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||
)
|
||||
if not isinstance(params, dict):
|
||||
raise TypeError("connection_params_json must be a JSON object")
|
||||
if "fallback" in params and params["fallback"] not in (None, "", []):
|
||||
validate_fallback_entries(
|
||||
params["fallback"],
|
||||
max_entries=MAX_FALLBACK_ENTRIES,
|
||||
)
|
||||
if "tags" in params and params["tags"] not in ("", None):
|
||||
validate_auth_budget_tags(params["tags"])
|
||||
except (ValueError, TypeError, SanitizedAuthError) as exc:
|
||||
context.abort(grpc.StatusCode.INVALID_ARGUMENT, str(exc))
|
||||
|
||||
base_node_id = params.get("base_node")
|
||||
# Valid attributes that a node object accepts
|
||||
valid_attrs = ['host', 'options', 'logs', 'password', 'port', 'protocol', 'user', 'jumphost']
|
||||
@@ -134,6 +170,8 @@ class NodeServicer(connpy_pb2_grpc.NodeServiceServicer):
|
||||
for attr in valid_attrs:
|
||||
if attr in params:
|
||||
device[attr] = params[attr]
|
||||
if "fallback" in params:
|
||||
device["fallback"] = params["fallback"]
|
||||
|
||||
if "tags" in params:
|
||||
device_tags = device.get("tags", {})
|
||||
@@ -147,20 +185,30 @@ class NodeServicer(connpy_pb2_grpc.NodeServiceServicer):
|
||||
else:
|
||||
# base_node not found, fall back to dynamic
|
||||
node_name = params.get("name", fallback_id)
|
||||
n = node(node_name, host=params.get("host", ""), config=user_config)
|
||||
for attr in valid_attrs:
|
||||
if attr in params:
|
||||
setattr(n, attr, params[attr])
|
||||
dyn_kwargs = {k: params[k] for k in valid_attrs if k in params and k != "host"}
|
||||
if "fallback" in params:
|
||||
dyn_kwargs["fallback"] = params["fallback"]
|
||||
if "tags" in params:
|
||||
n.tags = params["tags"]
|
||||
dyn_kwargs["tags"] = params["tags"]
|
||||
n = node(
|
||||
node_name,
|
||||
host=params.get("host", ""),
|
||||
config=user_config,
|
||||
**dyn_kwargs,
|
||||
)
|
||||
else:
|
||||
node_name = params.get("name", fallback_id)
|
||||
n = node(node_name, host=params.get("host", ""), config=user_config)
|
||||
for attr in valid_attrs:
|
||||
if attr in params:
|
||||
setattr(n, attr, params[attr])
|
||||
dyn_kwargs = {k: params[k] for k in valid_attrs if k in params and k != "host"}
|
||||
if "fallback" in params:
|
||||
dyn_kwargs["fallback"] = params["fallback"]
|
||||
if "tags" in params:
|
||||
n.tags = params["tags"]
|
||||
dyn_kwargs["tags"] = params["tags"]
|
||||
n = node(
|
||||
node_name,
|
||||
host=params.get("host", ""),
|
||||
config=user_config,
|
||||
**dyn_kwargs,
|
||||
)
|
||||
n._orig_protocol = n.protocol
|
||||
if sftp:
|
||||
n.protocol = "sftp"
|
||||
@@ -181,9 +229,11 @@ class NodeServicer(connpy_pb2_grpc.NodeServiceServicer):
|
||||
debug_chunks = []
|
||||
if debug:
|
||||
from io import StringIO
|
||||
|
||||
from rich.console import Console as RichConsole
|
||||
from ..printer import connpy_theme
|
||||
|
||||
from .. import printer as _printer
|
||||
from ..printer import connpy_theme
|
||||
|
||||
def remote_logger(msg_type, message):
|
||||
buf = StringIO()
|
||||
@@ -204,7 +254,14 @@ class NodeServicer(connpy_pb2_grpc.NodeServiceServicer):
|
||||
else:
|
||||
remote_logger = None
|
||||
|
||||
connect = n._connect(debug=debug, logger=remote_logger)
|
||||
try:
|
||||
connect = n._connect(
|
||||
debug=debug,
|
||||
logger=remote_logger,
|
||||
attempt_tracker=getattr(provider, "auth_tracker", None),
|
||||
)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
connect = str(sanitize_exception(exc, code="AUTH_FAILED"))
|
||||
|
||||
# Send debug output to client before checking result (always show the command)
|
||||
for chunk in debug_chunks:
|
||||
@@ -535,10 +592,9 @@ class NodeServicer(connpy_pb2_grpc.NodeServiceServicer):
|
||||
continue
|
||||
|
||||
asyncio.run(n._async_interact_loop(remote_stream, resize_callback, copilot_handler=remote_copilot_handler))
|
||||
except Exception as e:
|
||||
import traceback
|
||||
print(f"[ERROR in run_async_loop] {e}")
|
||||
traceback.print_exc()
|
||||
except Exception as e: # noqa: BLE001
|
||||
sanitized_err = sanitize_exception(e, code="TRANSPORT_ERROR", context_label="interact stream")
|
||||
print(f"[ERROR in run_async_loop] {sanitized_err}")
|
||||
finally:
|
||||
n._teardown_interact_environment()
|
||||
response_queue.put(None) # Signal EOF
|
||||
@@ -641,9 +697,7 @@ class NodeServicer(connpy_pb2_grpc.NodeServiceServicer):
|
||||
|
||||
class ProfileServicer(connpy_pb2_grpc.ProfileServiceServicer):
|
||||
def __init__(self, provider, registry=None):
|
||||
if not hasattr(provider, "mode"):
|
||||
from connpy.services.provider import ServiceProvider
|
||||
provider = ServiceProvider(provider, mode="local")
|
||||
provider = _ensure_server_provider(provider)
|
||||
self._fallback_provider = provider
|
||||
self._registry = registry
|
||||
|
||||
@@ -695,9 +749,7 @@ class ProfileServicer(connpy_pb2_grpc.ProfileServiceServicer):
|
||||
|
||||
class ConfigServicer(connpy_pb2_grpc.ConfigServiceServicer):
|
||||
def __init__(self, provider, registry=None):
|
||||
if not hasattr(provider, "mode"):
|
||||
from connpy.services.provider import ServiceProvider
|
||||
provider = ServiceProvider(provider, mode="local")
|
||||
provider = _ensure_server_provider(provider)
|
||||
self._fallback_provider = provider
|
||||
self._registry = registry
|
||||
|
||||
@@ -740,9 +792,7 @@ class ConfigServicer(connpy_pb2_grpc.ConfigServiceServicer):
|
||||
|
||||
class PluginServicer(connpy_pb2_grpc.PluginServiceServicer, remote_plugin_pb2_grpc.RemotePluginServiceServicer):
|
||||
def __init__(self, provider, registry=None):
|
||||
if not hasattr(provider, "mode"):
|
||||
from connpy.services.provider import ServiceProvider
|
||||
provider = ServiceProvider(provider, mode="local")
|
||||
provider = _ensure_server_provider(provider)
|
||||
self._fallback_provider = provider
|
||||
self._registry = registry
|
||||
|
||||
@@ -798,9 +848,7 @@ class PluginServicer(connpy_pb2_grpc.PluginServiceServicer, remote_plugin_pb2_gr
|
||||
|
||||
class ExecutionServicer(connpy_pb2_grpc.ExecutionServiceServicer):
|
||||
def __init__(self, provider, registry=None):
|
||||
if not hasattr(provider, "mode"):
|
||||
from connpy.services.provider import ServiceProvider
|
||||
provider = ServiceProvider(provider, mode="local")
|
||||
provider = _ensure_server_provider(provider)
|
||||
self._fallback_provider = provider
|
||||
self._registry = registry
|
||||
|
||||
@@ -929,9 +977,7 @@ class ExecutionServicer(connpy_pb2_grpc.ExecutionServiceServicer):
|
||||
|
||||
class ImportExportServicer(connpy_pb2_grpc.ImportExportServiceServicer):
|
||||
def __init__(self, provider, registry=None):
|
||||
if not hasattr(provider, "mode"):
|
||||
from connpy.services.provider import ServiceProvider
|
||||
provider = ServiceProvider(provider, mode="local")
|
||||
provider = _ensure_server_provider(provider)
|
||||
self._fallback_provider = provider
|
||||
self._registry = registry
|
||||
|
||||
@@ -1064,9 +1110,7 @@ class StatusBridge:
|
||||
|
||||
class AIServicer(connpy_pb2_grpc.AIServiceServicer):
|
||||
def __init__(self, provider, registry=None, debug=False):
|
||||
if not hasattr(provider, "mode"):
|
||||
from connpy.services.provider import ServiceProvider
|
||||
provider = ServiceProvider(provider, mode="local")
|
||||
provider = _ensure_server_provider(provider)
|
||||
self._fallback_provider = provider
|
||||
self._registry = registry
|
||||
self.server_debug = debug
|
||||
@@ -1351,9 +1395,7 @@ class AIServicer(connpy_pb2_grpc.AIServiceServicer):
|
||||
|
||||
class SystemServicer(connpy_pb2_grpc.SystemServiceServicer):
|
||||
def __init__(self, provider, registry=None):
|
||||
if not hasattr(provider, "mode"):
|
||||
from connpy.services.provider import ServiceProvider
|
||||
provider = ServiceProvider(provider, mode="local")
|
||||
provider = _ensure_server_provider(provider)
|
||||
self._fallback_provider = provider
|
||||
self._registry = registry
|
||||
|
||||
@@ -1736,7 +1778,7 @@ def serve(config, port=8048, debug=False):
|
||||
from connpy.grpc_layer.user_registry import UserRegistry
|
||||
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)
|
||||
|
||||
# Check if trusted_gateway provider is configured if SSO Gateway Secret is present in env
|
||||
|
||||
+151
-21
@@ -1,16 +1,19 @@
|
||||
import grpc
|
||||
import queue
|
||||
import re
|
||||
import threading
|
||||
from functools import wraps
|
||||
|
||||
import grpc
|
||||
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 ..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 . 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):
|
||||
@wraps(func)
|
||||
@@ -43,8 +46,48 @@ class NodeStub:
|
||||
self.remote_host = remote_host
|
||||
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):
|
||||
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 . import connpy_pb2
|
||||
|
||||
@@ -138,6 +181,7 @@ class NodeStub:
|
||||
|
||||
@handle_errors
|
||||
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 select
|
||||
import tty
|
||||
@@ -429,16 +473,26 @@ class NodeStub:
|
||||
@handle_errors
|
||||
def list_nodes(self, filter_str=None, format_str=None):
|
||||
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
|
||||
@handle_errors
|
||||
def list_folders(self, filter_str=None):
|
||||
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
|
||||
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)
|
||||
|
||||
@handle_errors
|
||||
@@ -446,7 +500,11 @@ class NodeStub:
|
||||
return from_value(self.stub.explode_unique(connpy_pb2.IdRequest(id=unique_id)).data)
|
||||
|
||||
@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))
|
||||
|
||||
@handle_errors
|
||||
@@ -463,7 +521,7 @@ class NodeStub:
|
||||
# node cache is what matters for fzf. We'll fetch profiles if we can.
|
||||
# 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)
|
||||
|
||||
def _trigger_local_cache_sync(self):
|
||||
@@ -478,12 +536,16 @@ class NodeStub:
|
||||
|
||||
@handle_errors
|
||||
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)
|
||||
self.stub.add_node(req)
|
||||
self._trigger_local_cache_sync()
|
||||
|
||||
@handle_errors
|
||||
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)
|
||||
self.stub.update_node(req)
|
||||
if save:
|
||||
@@ -491,6 +553,7 @@ class NodeStub:
|
||||
|
||||
@handle_errors
|
||||
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)
|
||||
self.stub.delete_node(req)
|
||||
if save:
|
||||
@@ -498,12 +561,22 @@ class NodeStub:
|
||||
|
||||
@handle_errors
|
||||
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)
|
||||
self.stub.move_node(req)
|
||||
self._trigger_local_cache_sync()
|
||||
|
||||
@handle_errors
|
||||
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))
|
||||
self.stub.bulk_add(req)
|
||||
self._trigger_local_cache_sync()
|
||||
@@ -649,13 +722,58 @@ class PluginStub:
|
||||
yield chunk.text
|
||||
|
||||
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.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
|
||||
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(
|
||||
nodes=nodes_list,
|
||||
commands=commands,
|
||||
@@ -668,9 +786,6 @@ class ExecutionStub:
|
||||
if variables is not None:
|
||||
req.vars.CopyFrom(to_struct(variables))
|
||||
|
||||
final_results = {}
|
||||
on_complete = kwargs.get("on_node_complete")
|
||||
|
||||
for response in self.stub.run_commands(req):
|
||||
if on_complete:
|
||||
on_complete(response.unique_id, response.output, response.status)
|
||||
@@ -683,7 +798,11 @@ class ExecutionStub:
|
||||
|
||||
@handle_errors
|
||||
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(
|
||||
nodes=nodes_list,
|
||||
commands=commands,
|
||||
@@ -697,9 +816,6 @@ class ExecutionStub:
|
||||
if variables is not None:
|
||||
req.vars.CopyFrom(to_struct(variables))
|
||||
|
||||
final_results = {}
|
||||
on_complete = kwargs.get("on_node_complete")
|
||||
|
||||
for response in self.stub.test_commands(req):
|
||||
result_dict = from_struct(response.test_result) if response.HasField("test_result") else {}
|
||||
if on_complete:
|
||||
@@ -710,7 +826,21 @@ class ExecutionStub:
|
||||
|
||||
@handle_errors
|
||||
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)
|
||||
|
||||
class ImportExportStub:
|
||||
|
||||
@@ -62,7 +62,7 @@ class UserRegistry:
|
||||
try:
|
||||
# Attempt a fresh configuration load
|
||||
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
|
||||
if old_provider:
|
||||
|
||||
+128
-50
@@ -6,6 +6,20 @@ from typing import Any, ClassVar
|
||||
|
||||
import yaml
|
||||
|
||||
from .auth_utils import (
|
||||
MAX_FALLBACK_ENTRIES,
|
||||
MAX_JSON_INPUT_BYTES,
|
||||
MAX_JSON_INPUT_DEPTH,
|
||||
SanitizedAuthError,
|
||||
is_compose_expression,
|
||||
mask_node_secrets,
|
||||
sanitize_exception,
|
||||
validate_auth_budget_tags,
|
||||
validate_compose_syntax,
|
||||
validate_fallback_entries,
|
||||
validate_json_depth_and_size,
|
||||
)
|
||||
|
||||
try:
|
||||
from mcp.server.mcpserver import MCPServer
|
||||
except ImportError:
|
||||
@@ -124,20 +138,7 @@ class ConnpyMCPServer:
|
||||
|
||||
def _mask_secrets(self, val: Any) -> Any:
|
||||
"""Recursively mask passwords, passphrases, secrets, and auth tokens with '***'."""
|
||||
if isinstance(val, dict):
|
||||
masked = {}
|
||||
for k, v in val.items():
|
||||
k_lower = str(k).lower()
|
||||
if any(sec in k_lower for sec in ["password", "passphrase", "secret", "token"]):
|
||||
masked[k] = "***"
|
||||
elif isinstance(v, (dict, list)):
|
||||
masked[k] = self._mask_secrets(v)
|
||||
else:
|
||||
masked[k] = v
|
||||
return masked
|
||||
if isinstance(val, list):
|
||||
return [self._mask_secrets(item) for item in val]
|
||||
return val
|
||||
return mask_node_secrets(val)
|
||||
|
||||
# -------------------------------------------------------------------------
|
||||
# Level 1: Read-Only Inventory and Context Tools
|
||||
@@ -196,6 +197,31 @@ class ConnpyMCPServer:
|
||||
# Level 2: Execution, Testing, and Playbooks Tools
|
||||
# -------------------------------------------------------------------------
|
||||
|
||||
@staticmethod
|
||||
def _parse_json_list_or_lines(raw_val: list[str] | str, *, allow_empty_str: bool = False) -> list[str]:
|
||||
if isinstance(raw_val, str):
|
||||
if len(raw_val.encode("utf-8", errors="replace")) > MAX_JSON_INPUT_BYTES:
|
||||
raise ValueError(f"Input exceeds maximum size of {MAX_JSON_INPUT_BYTES} bytes")
|
||||
stripped = raw_val.strip()
|
||||
if stripped.startswith(("[", "{")):
|
||||
parsed = validate_json_depth_and_size(
|
||||
stripped,
|
||||
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||
)
|
||||
return parsed if isinstance(parsed, list) else [raw_val]
|
||||
return ([raw_val] if raw_val else []) if allow_empty_str else [
|
||||
c.strip() for c in raw_val.splitlines() if c.strip()
|
||||
]
|
||||
if isinstance(raw_val, list):
|
||||
validate_json_depth_and_size(
|
||||
raw_val,
|
||||
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||
)
|
||||
return raw_val
|
||||
return [str(raw_val)] if (raw_val or not allow_empty_str) else []
|
||||
|
||||
def run_commands(
|
||||
self,
|
||||
nodes_filter: str,
|
||||
@@ -210,17 +236,10 @@ class ConnpyMCPServer:
|
||||
if not self.services or not hasattr(self.services, "execution"):
|
||||
return {"success": False, "error": "Execution service unavailable."}
|
||||
|
||||
# Normalize commands list
|
||||
if isinstance(commands, str):
|
||||
try:
|
||||
import json
|
||||
|
||||
parsed = json.loads(commands)
|
||||
commands = parsed if isinstance(parsed, list) else [commands]
|
||||
except Exception: # noqa: BLE001
|
||||
commands = [c.strip() for c in commands.splitlines() if c.strip()]
|
||||
elif not isinstance(commands, list):
|
||||
commands = [str(commands)]
|
||||
commands = self._parse_json_list_or_lines(commands, allow_empty_str=False)
|
||||
except ValueError as exc:
|
||||
return {"success": False, "error": str(exc)}
|
||||
|
||||
# Check safety
|
||||
if not self.trust:
|
||||
@@ -239,9 +258,24 @@ class ConnpyMCPServer:
|
||||
parallel=par_count,
|
||||
timeout=timeout,
|
||||
)
|
||||
return {"success": True, "results": results}
|
||||
fb_used = False
|
||||
if isinstance(results, dict):
|
||||
fb_used = any(
|
||||
isinstance(v, dict) and bool(v.get("fallback_used", False))
|
||||
for v in results.values()
|
||||
)
|
||||
if not fb_used:
|
||||
fb_map = getattr(self.services.execution, "last_fallback_used", {})
|
||||
if isinstance(fb_map, dict):
|
||||
fb_used = any(bool(v) for v in fb_map.values())
|
||||
return {"success": True, "results": results, "fallback_used": fb_used}
|
||||
except Exception as e: # noqa: BLE001
|
||||
return {"success": False, "error": str(e)}
|
||||
san = sanitize_exception(
|
||||
e,
|
||||
code="AUTH_FAILED",
|
||||
context_label="command execution failed",
|
||||
)
|
||||
return {"success": False, "error": str(san)}
|
||||
|
||||
def test_commands(
|
||||
self,
|
||||
@@ -255,29 +289,11 @@ class ConnpyMCPServer:
|
||||
if not self.services or not hasattr(self.services, "execution"):
|
||||
return {"success": False, "error": "Execution service unavailable."}
|
||||
|
||||
# Normalize commands
|
||||
if isinstance(commands, str):
|
||||
try:
|
||||
import json
|
||||
|
||||
parsed = json.loads(commands)
|
||||
commands = parsed if isinstance(parsed, list) else [commands]
|
||||
except Exception: # noqa: BLE001
|
||||
commands = [c.strip() for c in commands.splitlines() if c.strip()]
|
||||
elif not isinstance(commands, list):
|
||||
commands = [str(commands)]
|
||||
|
||||
# Normalize expected
|
||||
if isinstance(expected, str):
|
||||
try:
|
||||
import json
|
||||
|
||||
parsed = json.loads(expected)
|
||||
expected = parsed if isinstance(parsed, list) else ([expected] if expected else [])
|
||||
except Exception: # noqa: BLE001
|
||||
expected = [expected] if expected else []
|
||||
elif not isinstance(expected, list):
|
||||
expected = [str(expected)] if expected else []
|
||||
commands = self._parse_json_list_or_lines(commands, allow_empty_str=False)
|
||||
expected = self._parse_json_list_or_lines(expected, allow_empty_str=True)
|
||||
except ValueError as exc:
|
||||
return {"success": False, "error": str(exc)}
|
||||
|
||||
# Check safety
|
||||
if not self.trust:
|
||||
@@ -297,9 +313,16 @@ class ConnpyMCPServer:
|
||||
parallel=par_count,
|
||||
timeout=timeout,
|
||||
)
|
||||
return {"success": True, "results": results}
|
||||
fb_map = getattr(self.services.execution, "last_fallback_used", {})
|
||||
fb_used = any(bool(v) for v in fb_map.values()) if isinstance(fb_map, dict) else False
|
||||
return {"success": True, "results": results, "fallback_used": fb_used}
|
||||
except Exception as e: # noqa: BLE001
|
||||
return {"success": False, "error": str(e)}
|
||||
san = sanitize_exception(
|
||||
e,
|
||||
code="AUTH_FAILED",
|
||||
context_label="test execution failed",
|
||||
)
|
||||
return {"success": False, "error": str(san)}
|
||||
|
||||
def validate_playbook(self, playbook_yaml: str) -> dict:
|
||||
"""Validate the syntax, schema, and tasks of a Connpy YAML playbook."""
|
||||
@@ -427,6 +450,47 @@ class ConnpyMCPServer:
|
||||
# Level 3: Inventory Mutation Tools (CRUD - Requires trust=True)
|
||||
# -------------------------------------------------------------------------
|
||||
|
||||
@staticmethod
|
||||
def _validate_and_normalize_node_payload(node_data: dict[str, Any]) -> dict[str, Any]:
|
||||
validate_json_depth_and_size(
|
||||
node_data,
|
||||
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||
)
|
||||
normalized = node_data.copy()
|
||||
if "tags" in normalized and normalized["tags"] not in ("", None):
|
||||
tags_val = normalized["tags"]
|
||||
if isinstance(tags_val, str) and tags_val.strip().startswith("{"):
|
||||
tags_val = validate_json_depth_and_size(
|
||||
tags_val,
|
||||
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||
)
|
||||
normalized["tags"] = tags_val
|
||||
validate_auth_budget_tags(tags_val)
|
||||
|
||||
if "fallback" in normalized and normalized["fallback"] not in (None, "", []):
|
||||
fb_val = normalized["fallback"]
|
||||
if isinstance(fb_val, str) and fb_val.strip().startswith(("[", "{")):
|
||||
fb_val = validate_json_depth_and_size(
|
||||
fb_val,
|
||||
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||
)
|
||||
normalized["fallback"] = validate_fallback_entries(
|
||||
fb_val,
|
||||
max_entries=MAX_FALLBACK_ENTRIES,
|
||||
)
|
||||
|
||||
pw = normalized.get("password")
|
||||
if is_compose_expression(pw):
|
||||
validate_compose_syntax(pw)
|
||||
elif isinstance(pw, (list, tuple)):
|
||||
for item in pw:
|
||||
if is_compose_expression(item):
|
||||
validate_compose_syntax(item)
|
||||
return normalized
|
||||
|
||||
def add_node(
|
||||
self, unique_id: str, data: dict | None = None, is_folder: bool = False, **kwargs
|
||||
) -> dict:
|
||||
@@ -444,11 +508,14 @@ class ConnpyMCPServer:
|
||||
node_data.update(kwargs)
|
||||
|
||||
try:
|
||||
node_data = self._validate_and_normalize_node_payload(node_data)
|
||||
self.services.nodes.add_node(unique_id, node_data, is_folder=is_folder)
|
||||
return {
|
||||
"success": True,
|
||||
"message": f"{'Folder' if is_folder else 'Node'} '{unique_id}' created successfully.",
|
||||
}
|
||||
except (ValueError, TypeError, SanitizedAuthError) as exc:
|
||||
return {"success": False, "error": str(exc)}
|
||||
except Exception as e: # noqa: BLE001
|
||||
return {"success": False, "error": str(e)}
|
||||
|
||||
@@ -467,8 +534,11 @@ class ConnpyMCPServer:
|
||||
node_updates.update(kwargs)
|
||||
|
||||
try:
|
||||
node_updates = self._validate_and_normalize_node_payload(node_updates)
|
||||
self.services.nodes.update_node(unique_id, node_updates)
|
||||
return {"success": True, "message": f"Node '{unique_id}' updated successfully."}
|
||||
except (ValueError, TypeError, SanitizedAuthError) as exc:
|
||||
return {"success": False, "error": str(exc)}
|
||||
except Exception as e: # noqa: BLE001
|
||||
return {"success": False, "error": str(e)}
|
||||
|
||||
@@ -522,7 +592,15 @@ class ConnpyMCPServer:
|
||||
return self.server.run(transport="stdio", **kwargs)
|
||||
|
||||
if transport == "sse":
|
||||
if hasattr(self.server, "settings"):
|
||||
if hasattr(self.server.settings, "host"):
|
||||
self.server.settings.host = host
|
||||
if hasattr(self.server.settings, "port"):
|
||||
self.server.settings.port = port
|
||||
try:
|
||||
return self.server.run(transport="sse", host=host, port=port, **kwargs)
|
||||
except TypeError:
|
||||
return self.server.run(transport="sse", **kwargs)
|
||||
|
||||
msg = f"Unknown transport: {transport}. Supported transports: 'stdio', 'sse'"
|
||||
raise ValueError(msg)
|
||||
@@ -1,21 +1,45 @@
|
||||
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 ..hooks import MethodHook
|
||||
from .. import printer
|
||||
from .exceptions import InvalidConfigurationError
|
||||
|
||||
|
||||
class ContextService(BaseService):
|
||||
"""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
|
||||
def contexts(self) -> Dict[str, List[str]]:
|
||||
def contexts(self) -> dict[str, list[str]]:
|
||||
return self.config.config.get("contexts", {"all": [".*"]})
|
||||
|
||||
@property
|
||||
def current_context(self) -> str:
|
||||
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 = []
|
||||
for name in self.contexts.keys():
|
||||
result.append({
|
||||
@@ -25,27 +49,29 @@ class ContextService(BaseService):
|
||||
})
|
||||
return result
|
||||
|
||||
def add_context(self, name: str, regexes: List[str]):
|
||||
def add_context(self, name: str, regexes: list[str]):
|
||||
if not name.isalnum():
|
||||
raise ValueError("Context name must be alphanumeric")
|
||||
self._validate_regexes(regexes)
|
||||
|
||||
ctxs = self.contexts
|
||||
if name in ctxs:
|
||||
raise ValueError(f"Context '{name}' already exists")
|
||||
|
||||
ctxs[name] = regexes
|
||||
ctxs[name] = list(regexes)
|
||||
self.config.config["contexts"] = ctxs
|
||||
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":
|
||||
raise ValueError("Cannot modify default context 'all'")
|
||||
self._validate_regexes(regexes)
|
||||
|
||||
ctxs = self.contexts
|
||||
if name not in ctxs:
|
||||
raise ValueError(f"Context '{name}' does not exist")
|
||||
|
||||
ctxs[name] = regexes
|
||||
ctxs[name] = list(regexes)
|
||||
self.config.config["contexts"] = ctxs
|
||||
self.config._saveconfig(self.config.file)
|
||||
|
||||
@@ -67,15 +93,47 @@ class ContextService(BaseService):
|
||||
if name not in self.contexts:
|
||||
raise ValueError(f"Context '{name}' does not exist")
|
||||
|
||||
prev_context = self.current_context
|
||||
self.config.config["current_context"] = name
|
||||
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]:
|
||||
patterns = self.contexts.get(self.current_context, [".*"])
|
||||
return [re.compile(p) for p in patterns]
|
||||
def get_active_regexes(self) -> list[re.Pattern]:
|
||||
return compile_active_context_patterns(getattr(self.config, "config", None))
|
||||
|
||||
def _match_any(self, node_name: str, patterns: List[re.Pattern]) -> bool:
|
||||
return any(p.match(node_name) for p in patterns)
|
||||
def _match_any(self, node_name: str, patterns: list[re.Pattern]) -> bool:
|
||||
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
|
||||
def filter_node_list(self, *args, **kwargs):
|
||||
|
||||
@@ -1,29 +1,215 @@
|
||||
from typing import List, Dict, Any, Callable, Optional
|
||||
import inspect
|
||||
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 .base import BaseService
|
||||
from .exceptions import ConnpyError
|
||||
|
||||
|
||||
class ExecutionService(BaseService):
|
||||
"""Business logic for executing commands on nodes and running automation scripts."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
config=None,
|
||||
remote_stub=None,
|
||||
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(
|
||||
self,
|
||||
nodes_filter: str,
|
||||
commands: List[str],
|
||||
variables: Optional[Dict[str, Any]] = None,
|
||||
commands: list[str],
|
||||
variables: dict[str, Any] | None = None,
|
||||
parallel: int = 10,
|
||||
timeout: int = 20,
|
||||
folder: Optional[str] = None,
|
||||
prompt: Optional[str] = None,
|
||||
on_node_complete: Optional[Callable] = None,
|
||||
logger: Optional[Callable] = None,
|
||||
name: Optional[str] = None
|
||||
) -> Dict[str, str]:
|
||||
|
||||
timeout: int | None = None,
|
||||
folder: str | None = None,
|
||||
prompt: str | None = None,
|
||||
on_node_complete: Callable | None = None,
|
||||
logger: Callable | None = None,
|
||||
name: str | None = None,
|
||||
connect_timeout: int | None = None,
|
||||
auth_timeout: int | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""Execute commands on a set of nodes."""
|
||||
try:
|
||||
self._ensure_backend_supports_stage_timeouts(
|
||||
connect_timeout=connect_timeout,
|
||||
auth_timeout=auth_timeout,
|
||||
)
|
||||
|
||||
if self._is_remote_backend():
|
||||
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)
|
||||
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:
|
||||
raise ConnpyError(f"No nodes found matching filter: {nodes_filter}")
|
||||
|
||||
@@ -31,47 +217,96 @@ class ExecutionService(BaseService):
|
||||
executor = Nodes(node_data, config=self.config)
|
||||
self.last_executor = executor
|
||||
|
||||
results = executor.run(
|
||||
commands=commands,
|
||||
vars=variables,
|
||||
parallel=parallel,
|
||||
timeout=timeout,
|
||||
folder=folder,
|
||||
prompt=prompt,
|
||||
on_complete=on_node_complete,
|
||||
logger=logger
|
||||
)
|
||||
run_kwargs: dict[str, Any] = {
|
||||
"commands": commands,
|
||||
"vars": variables,
|
||||
"parallel": parallel,
|
||||
"timeout": timeout,
|
||||
"folder": folder,
|
||||
"prompt": prompt,
|
||||
"on_complete": on_node_complete,
|
||||
"logger": logger,
|
||||
"connect_timeout": connect_timeout,
|
||||
"auth_timeout": auth_timeout,
|
||||
"_default_cmd_timeout": 20,
|
||||
}
|
||||
run_params = inspect.signature(executor.run).parameters
|
||||
if "unattended" in run_params:
|
||||
run_kwargs["unattended"] = True
|
||||
if "attempt_tracker" in run_params:
|
||||
run_kwargs["attempt_tracker"] = self.attempt_tracker
|
||||
|
||||
# Combine output and status for the caller
|
||||
results = executor.run(**run_kwargs)
|
||||
|
||||
fb_map = getattr(executor, "fallback_used", {})
|
||||
fb_dict = fb_map if isinstance(fb_map, dict) else {}
|
||||
|
||||
# Combine output, status, and fallback metadata for the caller
|
||||
full_results = {}
|
||||
self.last_fallback_used = {}
|
||||
for unique in results:
|
||||
fb_used = bool(fb_dict.get(unique, False))
|
||||
full_results[unique] = {
|
||||
"output": results[unique],
|
||||
"status": executor.status.get(unique, 1)
|
||||
"status": executor.status.get(unique, 1),
|
||||
"fallback_used": fb_used,
|
||||
}
|
||||
self.last_fallback_used[unique] = fb_used
|
||||
|
||||
return full_results
|
||||
except Exception as e:
|
||||
raise ConnpyError(f"Execution failed: {e}")
|
||||
except ConnpyError:
|
||||
raise
|
||||
except Exception as e: # noqa: BLE001
|
||||
raise_sanitized_auth_error(e, code="AUTH_FAILED", context_label="execution failed")
|
||||
|
||||
def test_commands(
|
||||
self,
|
||||
nodes_filter: str,
|
||||
commands: List[str],
|
||||
expected: List[str],
|
||||
variables: Optional[Dict[str, Any]] = None,
|
||||
commands: list[str],
|
||||
expected: list[str],
|
||||
variables: dict[str, Any] | None = None,
|
||||
parallel: int = 10,
|
||||
timeout: int = 20,
|
||||
folder: Optional[str] = None,
|
||||
prompt: Optional[str] = None,
|
||||
on_node_complete: Optional[Callable] = None,
|
||||
logger: Optional[Callable] = None,
|
||||
name: Optional[str] = None
|
||||
) -> Dict[str, Dict[str, bool]]:
|
||||
|
||||
timeout: int | None = None,
|
||||
folder: str | None = None,
|
||||
prompt: str | None = None,
|
||||
on_node_complete: Callable | None = None,
|
||||
logger: Callable | None = None,
|
||||
name: str | None = None,
|
||||
connect_timeout: int | None = None,
|
||||
auth_timeout: int | None = None,
|
||||
) -> dict[str, dict[str, bool]]:
|
||||
"""Run commands and verify expected output on a set of nodes."""
|
||||
try:
|
||||
self._ensure_backend_supports_stage_timeouts(
|
||||
connect_timeout=connect_timeout,
|
||||
auth_timeout=auth_timeout,
|
||||
)
|
||||
|
||||
if self._is_remote_backend():
|
||||
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)
|
||||
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:
|
||||
raise ConnpyError(f"No nodes found matching filter: {nodes_filter}")
|
||||
|
||||
@@ -79,31 +314,49 @@ class ExecutionService(BaseService):
|
||||
executor = Nodes(node_data, config=self.config)
|
||||
self.last_executor = executor
|
||||
|
||||
results = executor.test(
|
||||
commands=commands,
|
||||
expected=expected,
|
||||
vars=variables,
|
||||
parallel=parallel,
|
||||
timeout=timeout,
|
||||
folder=folder,
|
||||
prompt=prompt,
|
||||
on_complete=on_node_complete,
|
||||
logger=logger
|
||||
)
|
||||
return results
|
||||
except Exception as e:
|
||||
raise ConnpyError(f"Testing failed: {e}")
|
||||
test_kwargs: dict[str, Any] = {
|
||||
"commands": commands,
|
||||
"expected": expected,
|
||||
"vars": variables,
|
||||
"parallel": parallel,
|
||||
"timeout": timeout,
|
||||
"folder": folder,
|
||||
"prompt": prompt,
|
||||
"on_complete": on_node_complete,
|
||||
"logger": logger,
|
||||
"connect_timeout": connect_timeout,
|
||||
"auth_timeout": auth_timeout,
|
||||
"_default_cmd_timeout": 20,
|
||||
}
|
||||
test_params = inspect.signature(executor.test).parameters
|
||||
if "unattended" in test_params:
|
||||
test_kwargs["unattended"] = True
|
||||
if "attempt_tracker" in test_params:
|
||||
test_kwargs["attempt_tracker"] = self.attempt_tracker
|
||||
|
||||
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."""
|
||||
if not os.path.exists(script_path):
|
||||
raise ConnpyError(f"Script file not found: {script_path}")
|
||||
|
||||
try:
|
||||
with open(script_path, "r") as f:
|
||||
with open(script_path) as f:
|
||||
commands = [line.strip() for line in f if line.strip()]
|
||||
except Exception as e:
|
||||
raise ConnpyError(f"Failed to read script {script_path}: {e}")
|
||||
except OSError:
|
||||
raise ConnpyError("Failed to read script file") from None
|
||||
|
||||
return self.run_commands(nodes_filter, commands, parallel=parallel)
|
||||
|
||||
@@ -1,14 +1,56 @@
|
||||
from .base import BaseService
|
||||
import yaml
|
||||
import os
|
||||
import sys
|
||||
from copy import deepcopy
|
||||
from .exceptions import InvalidConfigurationError, NodeNotFoundError, ReservedNameError
|
||||
|
||||
import yaml
|
||||
|
||||
from ..configfile import NoAliasDumper
|
||||
from .base import BaseService
|
||||
from .exceptions import InvalidConfigurationError, NodeNotFoundError
|
||||
from .node_service import NodeService, _supports_transactional_update
|
||||
|
||||
|
||||
class ImportExportService(BaseService):
|
||||
"""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):
|
||||
"""Export nodes/folders to a YAML file."""
|
||||
if os.path.exists(file_path):
|
||||
@@ -23,16 +65,42 @@ class ImportExportService(BaseService):
|
||||
|
||||
def export_to_dict(self, folders=None):
|
||||
"""Export nodes/folders to a dictionary."""
|
||||
if not folders:
|
||||
return deepcopy(self.config.connections)
|
||||
else:
|
||||
# Validate folders exist
|
||||
for f in folders:
|
||||
if f != "@" and f not in self.config._getallfolders():
|
||||
raise NodeNotFoundError(f"Folder '{f}' not found.")
|
||||
ctx_svc = self._get_context_service()
|
||||
ctx_active = ctx_svc is not None and not ctx_svc.is_default_all_context()
|
||||
|
||||
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)
|
||||
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 = {}
|
||||
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():
|
||||
uniques = self.config._explode_unique(k)
|
||||
if not uniques:
|
||||
@@ -77,22 +145,52 @@ class ImportExportService(BaseService):
|
||||
raise InvalidConfigurationError(f"Failed to read/parse import file: {e}")
|
||||
|
||||
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):
|
||||
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():
|
||||
if k == "type":
|
||||
if k == "type" or not isinstance(v, dict):
|
||||
continue
|
||||
if isinstance(v, dict):
|
||||
node_type = v.get("type", "connection")
|
||||
if node_type == "folder":
|
||||
self.config._folder_add(folder=k)
|
||||
_traverse_import(v, current_folder=k, current_subfolder='')
|
||||
folder_id = f"@{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", k, ""))
|
||||
_plan_import(v, current_folder=k, current_subfolder="")
|
||||
elif node_type == "subfolder":
|
||||
self.config._folder_add(folder=current_folder, subfolder=k)
|
||||
_traverse_import(v, current_folder=current_folder, current_subfolder=k)
|
||||
folder_id = f"@{k}@{current_folder}"
|
||||
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":
|
||||
unique_id = k
|
||||
if current_subfolder:
|
||||
@@ -100,16 +198,38 @@ class ImportExportService(BaseService):
|
||||
elif current_folder:
|
||||
unique_id = f"{k}@{current_folder}"
|
||||
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['id'] = k
|
||||
kwargs['folder'] = current_folder
|
||||
kwargs['subfolder'] = current_subfolder
|
||||
kwargs["id"] = k
|
||||
kwargs["folder"] = current_folder
|
||||
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:
|
||||
# Invalid format skip
|
||||
pass
|
||||
cfg._connections_add_unlocked(**op[1])
|
||||
|
||||
_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)
|
||||
except BaseException:
|
||||
self.config.connections.clear()
|
||||
self.config.connections.update(snap_connections)
|
||||
raise
|
||||
+384
-33
@@ -1,13 +1,169 @@
|
||||
import inspect
|
||||
import re
|
||||
import sys
|
||||
from copy import deepcopy
|
||||
from typing import ClassVar
|
||||
|
||||
from .base import BaseService
|
||||
from .exceptions import (
|
||||
NodeNotFoundError, NodeAlreadyExistsError,
|
||||
InvalidConfigurationError, ReservedNameError
|
||||
InvalidConfigurationError,
|
||||
NodeAlreadyExistsError,
|
||||
NodeNotFoundError,
|
||||
ReservedNameError,
|
||||
)
|
||||
|
||||
|
||||
def _supports_transactional_update(cfg):
|
||||
return (
|
||||
cfg is not None
|
||||
and not hasattr(cfg, "assert_called")
|
||||
and callable(getattr(cfg, "transactional_update", None))
|
||||
)
|
||||
|
||||
|
||||
class NodeService(BaseService):
|
||||
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)
|
||||
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):
|
||||
@@ -19,6 +175,10 @@ class NodeService(BaseService):
|
||||
flags = re.IGNORECASE if not case_sensitive else 0
|
||||
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:
|
||||
return nodes
|
||||
|
||||
@@ -75,17 +235,21 @@ class NodeService(BaseService):
|
||||
else:
|
||||
flags = re.IGNORECASE if not case_sensitive else 0
|
||||
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
|
||||
|
||||
def get_node_details(self, unique_id):
|
||||
"""Return full configuration dictionary for a specific node."""
|
||||
self._ensure_in_active_context(unique_id, is_folder=False)
|
||||
try:
|
||||
details = self.config.getitem(unique_id)
|
||||
if not details:
|
||||
raise NodeNotFoundError(f"Node '{unique_id}' not found.")
|
||||
return details
|
||||
except (KeyError, TypeError):
|
||||
raise NodeNotFoundError(f"Node '{unique_id}' not found.")
|
||||
raise NodeNotFoundError(f"Node '{unique_id}' not found.") from None
|
||||
|
||||
def explode_unique(self, unique_id):
|
||||
"""Explode a unique ID into a dictionary of its parts."""
|
||||
@@ -96,25 +260,54 @@ class NodeService(BaseService):
|
||||
self.config._generate_nodes_cache(nodes=nodes, folders=folders, profiles=profiles)
|
||||
|
||||
def validate_parent_folder(self, unique_id, is_folder=False):
|
||||
"""Check if parent folder exists for a given node unique ID."""
|
||||
if is_folder:
|
||||
uniques = self.config._explode_unique(unique_id)
|
||||
if uniques and "subfolder" in uniques and "folder" in uniques:
|
||||
parent_folder = f"@{uniques['folder']}"
|
||||
if parent_folder not in self.config._getallfolders():
|
||||
"""Check if unique_id is in active context and its parent folder exists."""
|
||||
parent_folder = self._derive_parent_folder(self.config, unique_id, is_folder=is_folder)
|
||||
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 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.")
|
||||
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):
|
||||
"""Logic for adding a new node or folder to configuration."""
|
||||
if not is_folder:
|
||||
self._validate_node_name(unique_id)
|
||||
self._validate_node_payload(data)
|
||||
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_folders = self.config._getallfolders()
|
||||
@@ -126,7 +319,6 @@ class NodeService(BaseService):
|
||||
if not uniques:
|
||||
raise InvalidConfigurationError(f"Invalid folder name '{unique_id}'.")
|
||||
|
||||
# Check if parent folder exists when creating a subfolder
|
||||
if "subfolder" in uniques:
|
||||
self.validate_parent_folder(unique_id, is_folder=True)
|
||||
|
||||
@@ -136,37 +328,75 @@ class NodeService(BaseService):
|
||||
if unique_id in all_nodes:
|
||||
raise NodeAlreadyExistsError(f"Node '{unique_id}' already exists.")
|
||||
|
||||
# Check if parent folder exists when creating a node in a folder
|
||||
self.validate_parent_folder(unique_id)
|
||||
|
||||
# Ensure 'id' is in data for config._connections_add
|
||||
if "id" not in data:
|
||||
payload = deepcopy(data)
|
||||
if "id" not in payload:
|
||||
uniques = self.config._explode_unique(unique_id)
|
||||
if uniques and "id" in uniques:
|
||||
data["id"] = uniques["id"]
|
||||
payload["id"] = uniques["id"]
|
||||
|
||||
self.config._connections_add(**data)
|
||||
self.config._connections_add(**payload)
|
||||
self.config._saveconfig(self.config.file)
|
||||
|
||||
def _build_update_payload(self, unique_id, data, existing, uniques):
|
||||
if isinstance(existing, dict):
|
||||
merged = deepcopy(existing)
|
||||
if isinstance(uniques, dict):
|
||||
merged.update({k: v for k, v in uniques.items() if v})
|
||||
merged.update(deepcopy(data))
|
||||
filtered = {k: v for k, v in merged.items() if k in self._CONNECTION_FIELDS}
|
||||
else:
|
||||
filtered = deepcopy(data)
|
||||
if "id" not in filtered and isinstance(uniques, dict) and "id" in uniques:
|
||||
filtered["id"] = uniques["id"]
|
||||
self._validate_node_payload(filtered)
|
||||
return filtered
|
||||
|
||||
def update_node(self, unique_id, data, save=True):
|
||||
"""Explicitly update an existing node."""
|
||||
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()
|
||||
if unique_id not in all_nodes:
|
||||
raise NodeNotFoundError(f"Node '{unique_id}' not found.")
|
||||
|
||||
# Ensure 'id' is in data for config._connections_add
|
||||
if "id" not in data:
|
||||
uniques = self.config._explode_unique(unique_id)
|
||||
if uniques:
|
||||
data["id"] = uniques["id"]
|
||||
|
||||
# config._connections_add actually handles updates if ID exists correctly
|
||||
self.config._connections_add(**data)
|
||||
existing = self.config.getitem(unique_id, extract=False)
|
||||
payload = self._build_update_payload(unique_id, data, existing, uniques)
|
||||
self.config._connections_add(**payload)
|
||||
if save:
|
||||
self.config._saveconfig(self.config.file)
|
||||
|
||||
def delete_node(self, unique_id, is_folder=False, save=True):
|
||||
"""Logic for deleting a node or folder."""
|
||||
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:
|
||||
uniques = self.config._explode_unique(unique_id)
|
||||
if not uniques:
|
||||
@@ -183,17 +413,35 @@ class NodeService(BaseService):
|
||||
|
||||
def connect_node(self, unique_id, sftp=False, debug=False, logger=None):
|
||||
"""Interact with a node directly."""
|
||||
self._ensure_in_active_context(unique_id, is_folder=False)
|
||||
from connpy.core import node
|
||||
|
||||
from ..auth_utils import resolve_fallback_entries
|
||||
from .profile_service import ProfileService
|
||||
|
||||
node_data = self.config.getitem(unique_id, extract=False)
|
||||
if not node_data:
|
||||
raise NodeNotFoundError(f"Node '{unique_id}' not found.")
|
||||
|
||||
# Resolve profiles
|
||||
profile_service = ProfileService(self.config)
|
||||
resolved_data = profile_service.resolve_node_data(node_data)
|
||||
|
||||
node_params = inspect.signature(node.__init__).parameters
|
||||
if "fallback" not in node_params and "fallback" in resolved_data:
|
||||
raw_fb = resolved_data.pop("fallback", None)
|
||||
if raw_fb not in (None, "", []):
|
||||
resolved_fb = resolve_fallback_entries(raw_fb, self.config.profiles)
|
||||
if resolved_fb:
|
||||
tags_val = resolved_data.get("tags", "")
|
||||
if isinstance(tags_val, dict):
|
||||
tags_copy = deepcopy(tags_val)
|
||||
tags_copy["_fallback"] = resolved_fb
|
||||
resolved_data["tags"] = tags_copy
|
||||
elif tags_val in ("", None):
|
||||
resolved_data["tags"] = {"_fallback": resolved_fb}
|
||||
else:
|
||||
resolved_data["tags"] = {"_raw": tags_val, "_fallback": resolved_fb}
|
||||
|
||||
n = node(unique_id, **resolved_data, config=self.config)
|
||||
if sftp:
|
||||
n.protocol = "sftp"
|
||||
@@ -202,7 +450,48 @@ class NodeService(BaseService):
|
||||
|
||||
def move_node(self, src_id, dst_id, copy=False):
|
||||
"""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_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)
|
||||
if not node_data:
|
||||
@@ -228,6 +517,55 @@ class NodeService(BaseService):
|
||||
|
||||
def bulk_add(self, ids, hosts, common_data):
|
||||
"""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
|
||||
all_nodes = self.config._getallnodes()
|
||||
|
||||
@@ -238,8 +576,6 @@ class NodeService(BaseService):
|
||||
try:
|
||||
self._validate_node_name(uid)
|
||||
except ReservedNameError:
|
||||
# For bulk, we might want to just skip or log.
|
||||
# CLI caller will handle if it wants to be strict.
|
||||
continue
|
||||
|
||||
host = hosts[i] if i < len(hosts) else hosts[0]
|
||||
@@ -263,6 +599,21 @@ class NodeService(BaseService):
|
||||
|
||||
def full_replace(self, connections, profiles):
|
||||
"""Replace all connections and profiles with new data."""
|
||||
from ..auth_utils import SanitizedAuthError
|
||||
|
||||
if _supports_transactional_update(self.config):
|
||||
def _mut(cfg):
|
||||
cfg.connections.clear()
|
||||
cfg.connections.update(deepcopy(connections))
|
||||
cfg.profiles.clear()
|
||||
cfg.profiles.update(deepcopy(profiles))
|
||||
|
||||
try:
|
||||
self.config.transactional_update(_mut)
|
||||
except (ValueError, TypeError, SanitizedAuthError) as exc:
|
||||
raise InvalidConfigurationError(str(exc)) from None
|
||||
return
|
||||
|
||||
self.config.connections = connections
|
||||
self.config.profiles = profiles
|
||||
self.config._saveconfig(self.config.file)
|
||||
|
||||
@@ -1,64 +1,211 @@
|
||||
from contextlib import suppress
|
||||
from copy import deepcopy
|
||||
from typing import ClassVar
|
||||
|
||||
from .base import BaseService
|
||||
from .exceptions import ProfileNotFoundError, ProfileAlreadyExistsError, InvalidConfigurationError
|
||||
from .exceptions import (
|
||||
InvalidConfigurationError,
|
||||
ProfileAlreadyExistsError,
|
||||
ProfileNotFoundError,
|
||||
)
|
||||
|
||||
MAX_PROFILE_DEPTH = 5
|
||||
|
||||
|
||||
def _supports_transactional_update(cfg):
|
||||
return (
|
||||
cfg is not None
|
||||
and not hasattr(cfg, "assert_called")
|
||||
and callable(getattr(cfg, "transactional_update", None))
|
||||
)
|
||||
|
||||
|
||||
class ProfileService(BaseService):
|
||||
"""Business logic for node profiles management."""
|
||||
|
||||
_ALLOWED_KEYS: ClassVar[frozenset[str]] = frozenset({
|
||||
"host",
|
||||
"options",
|
||||
"logs",
|
||||
"password",
|
||||
"port",
|
||||
"protocol",
|
||||
"user",
|
||||
"tags",
|
||||
"jumphost",
|
||||
"fallback",
|
||||
})
|
||||
|
||||
@staticmethod
|
||||
def _validate_profile_payload(data):
|
||||
from ..auth_utils import (
|
||||
SanitizedAuthError,
|
||||
is_compose_expression,
|
||||
validate_auth_budget_tags,
|
||||
validate_compose_syntax,
|
||||
validate_fallback_entries,
|
||||
)
|
||||
|
||||
try:
|
||||
if "tags" in data and data["tags"] not in ("", None):
|
||||
validate_auth_budget_tags(data["tags"])
|
||||
if "fallback" in data and data["fallback"] not in (None, "", []):
|
||||
validate_fallback_entries(data["fallback"])
|
||||
pw = data.get("password")
|
||||
if is_compose_expression(pw):
|
||||
validate_compose_syntax(pw)
|
||||
elif isinstance(pw, (list, tuple)):
|
||||
for item in pw:
|
||||
if is_compose_expression(item):
|
||||
validate_compose_syntax(item)
|
||||
except (ValueError, TypeError, SanitizedAuthError) as exc:
|
||||
raise InvalidConfigurationError(str(exc)) from None
|
||||
|
||||
def _get_profiles_snapshot(self):
|
||||
lock = getattr(self.config, "_mutation_lock", None)
|
||||
if lock is not None and not hasattr(self.config, "assert_called"):
|
||||
with lock:
|
||||
return deepcopy(self.config.profiles)
|
||||
return deepcopy(self.config.profiles)
|
||||
|
||||
def list_profiles(self, filter_str=None):
|
||||
"""List all profile names, optionally filtered."""
|
||||
profiles = list(self.config.profiles.keys())
|
||||
profiles = list(self._get_profiles_snapshot().keys())
|
||||
case_sensitive = self.config.config.get("case", False)
|
||||
|
||||
if filter_str:
|
||||
if not case_sensitive:
|
||||
f_str = filter_str.lower()
|
||||
return [p for p in profiles if f_str in p.lower()]
|
||||
else:
|
||||
return [p for p in profiles if filter_str in p]
|
||||
return profiles
|
||||
|
||||
def get_profile(self, name, resolve=True):
|
||||
def get_profile(self, name, resolve=True, *, _visited=frozenset(), _depth=0):
|
||||
"""Get the profile dictionary, optionally resolved."""
|
||||
profile = self.config.profiles.get(name)
|
||||
if _depth >= MAX_PROFILE_DEPTH or name in _visited:
|
||||
raise InvalidConfigurationError(
|
||||
f"Circular or overly deep profile reference involving '{name}'."
|
||||
)
|
||||
profiles = self._get_profiles_snapshot()
|
||||
profile = profiles.get(name)
|
||||
if not profile:
|
||||
raise ProfileNotFoundError(f"Profile '{name}' not found.")
|
||||
|
||||
if resolve:
|
||||
return self.resolve_node_data(profile)
|
||||
return self.resolve_node_data(
|
||||
profile,
|
||||
_visited=_visited | {name},
|
||||
_depth=_depth + 1,
|
||||
_profiles_snapshot=profiles,
|
||||
)
|
||||
return profile
|
||||
|
||||
def add_profile(self, name, data):
|
||||
"""Add a new profile."""
|
||||
filtered_data = {k: deepcopy(v) for k, v in data.items() if k in self._ALLOWED_KEYS}
|
||||
self._validate_profile_payload(filtered_data)
|
||||
|
||||
if _supports_transactional_update(self.config):
|
||||
def _mut(cfg):
|
||||
if name in cfg.profiles:
|
||||
raise ProfileAlreadyExistsError(f"Profile '{name}' already exists.")
|
||||
cfg._profiles_add_unlocked(id=name, **filtered_data)
|
||||
|
||||
self.config.transactional_update(_mut)
|
||||
return
|
||||
|
||||
if name in self.config.profiles:
|
||||
raise ProfileAlreadyExistsError(f"Profile '{name}' already exists.")
|
||||
|
||||
# Filter data to match _profiles_add signature and ensure id is passed
|
||||
allowed_keys = {"host", "options", "logs", "password", "port", "protocol", "user", "tags", "jumphost"}
|
||||
filtered_data = {k: v for k, v in data.items() if k in allowed_keys}
|
||||
|
||||
self.config._profiles_add(id=name, **filtered_data)
|
||||
self.config._saveconfig(self.config.file)
|
||||
|
||||
def resolve_node_data(self, node_data):
|
||||
"""Resolve profile references (@profile) in node data and handle inheritance."""
|
||||
resolved = node_data.copy()
|
||||
def resolve_node_data(
|
||||
self,
|
||||
node_data,
|
||||
*,
|
||||
_visited=frozenset(),
|
||||
_depth=0,
|
||||
_profiles_snapshot=None,
|
||||
):
|
||||
"""Resolve profile references (@profile) in node data while preserving compose and scalar secrets."""
|
||||
from ..auth_utils import is_compose_expression
|
||||
|
||||
# 1. Identify all referenced profiles to support inheritance
|
||||
referenced_profiles = []
|
||||
for value in resolved.values():
|
||||
if isinstance(value, str) and value.startswith("@"):
|
||||
referenced_profiles.append(value[1:])
|
||||
elif isinstance(value, list):
|
||||
if _depth > MAX_PROFILE_DEPTH:
|
||||
raise InvalidConfigurationError("Profile inheritance exceeds maximum depth.")
|
||||
|
||||
profiles = (
|
||||
_profiles_snapshot
|
||||
if _profiles_snapshot is not None
|
||||
else self._get_profiles_snapshot()
|
||||
)
|
||||
resolved = deepcopy(node_data)
|
||||
|
||||
auth_referenced_profiles = []
|
||||
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:
|
||||
if isinstance(item, str) and item.startswith("@"):
|
||||
referenced_profiles.append(item[1:])
|
||||
if (
|
||||
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("@"):
|
||||
profile_name = value[1:]
|
||||
try:
|
||||
profile = self.get_profile(profile_name, resolve=True)
|
||||
profile = _get_resolved_ref(profile_name)
|
||||
resolved[key] = profile.get(key, "")
|
||||
except ProfileNotFoundError:
|
||||
resolved[key] = ""
|
||||
@@ -67,33 +214,29 @@ class ProfileService(BaseService):
|
||||
for item in value:
|
||||
if isinstance(item, str) and item.startswith("@"):
|
||||
profile_name = item[1:]
|
||||
try:
|
||||
profile = self.get_profile(profile_name, resolve=True)
|
||||
with suppress(ProfileNotFoundError):
|
||||
profile = _get_resolved_ref(profile_name)
|
||||
if "password" in profile:
|
||||
resolved_list.append(profile["password"])
|
||||
except ProfileNotFoundError:
|
||||
pass
|
||||
else:
|
||||
resolved_list.append(item)
|
||||
resolved[key] = resolved_list
|
||||
|
||||
# 3. Inheritance: Fill empty keys from the first referenced profile
|
||||
if referenced_profiles:
|
||||
base_profile_name = referenced_profiles[0]
|
||||
try:
|
||||
base_profile = self.get_profile(base_profile_name, resolve=True)
|
||||
for key, value in base_profile.items():
|
||||
# Fill if key is missing or empty
|
||||
if key not in resolved or resolved[key] == "" or resolved[key] == [] or resolved[key] is None:
|
||||
resolved[key] = value
|
||||
except ProfileNotFoundError:
|
||||
pass
|
||||
dedup_auth_profiles = list(dict.fromkeys(auth_referenced_profiles))
|
||||
for base_profile_name in dedup_auth_profiles:
|
||||
with suppress(ProfileNotFoundError):
|
||||
base_profile = _get_resolved_ref(base_profile_name)
|
||||
for key in ("user", "password", "fallback"):
|
||||
val = base_profile.get(key)
|
||||
if (
|
||||
key not in resolved or resolved[key] in ("", [], None)
|
||||
) and val not in ("", [], None):
|
||||
resolved[key] = deepcopy(val)
|
||||
|
||||
# 4. Handle default protocol
|
||||
if resolved.get("protocol") == "" or resolved.get("protocol") is None:
|
||||
if resolved.get("protocol") in ("", None):
|
||||
try:
|
||||
default_profile = self.get_profile("default", resolve=True)
|
||||
resolved["protocol"] = default_profile.get("protocol", "ssh")
|
||||
default_profile = _get_resolved_ref("default")
|
||||
resolved["protocol"] = default_profile.get("protocol", "ssh") or "ssh"
|
||||
except ProfileNotFoundError:
|
||||
resolved["protocol"] = "ssh"
|
||||
|
||||
@@ -101,34 +244,58 @@ class ProfileService(BaseService):
|
||||
|
||||
def delete_profile(self, name):
|
||||
"""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.")
|
||||
|
||||
if name == "default":
|
||||
raise InvalidConfigurationError("Cannot delete the 'default' profile.")
|
||||
used_by = cfg._profileused_unlocked(name)
|
||||
if used_by:
|
||||
raise InvalidConfigurationError(
|
||||
f"Profile '{name}' is used by nodes: {', '.join(used_by)}"
|
||||
)
|
||||
cfg._profiles_del_unlocked(id=name)
|
||||
|
||||
self.config.transactional_update(_mut)
|
||||
return
|
||||
|
||||
if name not in self.config.profiles:
|
||||
raise ProfileNotFoundError(f"Profile '{name}' not found.")
|
||||
if name == "default":
|
||||
raise InvalidConfigurationError("Cannot delete the 'default' profile.")
|
||||
used_by = self.config._profileused(name)
|
||||
if used_by:
|
||||
# We return the list of nodes using it so the UI can inform the user
|
||||
raise InvalidConfigurationError(f"Profile '{name}' is used by nodes: {', '.join(used_by)}")
|
||||
|
||||
raise InvalidConfigurationError(
|
||||
f"Profile '{name}' is used by nodes: {', '.join(used_by)}"
|
||||
)
|
||||
self.config._profiles_del(id=name)
|
||||
self.config._saveconfig(self.config.file)
|
||||
|
||||
def update_profile(self, name, data):
|
||||
"""Update an existing profile."""
|
||||
if _supports_transactional_update(self.config):
|
||||
def _mut(cfg):
|
||||
if name not in cfg.profiles:
|
||||
raise ProfileNotFoundError(f"Profile '{name}' not found.")
|
||||
updated_data = deepcopy(cfg.profiles[name])
|
||||
updated_data.update(deepcopy(data))
|
||||
filtered_data = {
|
||||
k: v for k, v in updated_data.items() if k in self._ALLOWED_KEYS
|
||||
}
|
||||
self._validate_profile_payload(filtered_data)
|
||||
cfg._profiles_add_unlocked(id=name, **filtered_data)
|
||||
|
||||
self.config.transactional_update(_mut)
|
||||
return
|
||||
|
||||
if name not in self.config.profiles:
|
||||
raise ProfileNotFoundError(f"Profile '{name}' not found.")
|
||||
|
||||
# Merge with existing data
|
||||
existing = self.get_profile(name, resolve=False)
|
||||
updated_data = existing.copy()
|
||||
updated_data = deepcopy(existing)
|
||||
updated_data.update(data)
|
||||
|
||||
# Filter data to match _profiles_add signature
|
||||
allowed_keys = {"host", "options", "logs", "password", "port", "protocol", "user", "tags", "jumphost"}
|
||||
filtered_data = {k: v for k, v in updated_data.items() if k in allowed_keys}
|
||||
|
||||
filtered_data = {k: v for k, v in updated_data.items() if k in self._ALLOWED_KEYS}
|
||||
self._validate_profile_payload(filtered_data)
|
||||
self.config._profiles_add(id=name, **filtered_data)
|
||||
self.config._saveconfig(self.config.file)
|
||||
|
||||
+65
-18
@@ -1,5 +1,9 @@
|
||||
import inspect
|
||||
from contextlib import suppress
|
||||
|
||||
from .exceptions import InvalidConfigurationError
|
||||
|
||||
|
||||
class RemoteStub:
|
||||
def __getattr__(self, name):
|
||||
raise NotImplementedError(
|
||||
@@ -7,19 +11,26 @@ class RemoteStub:
|
||||
"Use local mode or wait for the gRPC implementation."
|
||||
)
|
||||
|
||||
|
||||
class ServiceProvider:
|
||||
"""Dynamic service backend. Transparently provides local or remote services."""
|
||||
|
||||
def __init__(self, config, mode="local", remote_host=None):
|
||||
def __init__(self, config, mode="local", remote_host=None, enforce_context: bool = True):
|
||||
from ..auth_utils import AuthAttemptTracker
|
||||
|
||||
self.mode = mode
|
||||
self.config = config
|
||||
self.remote_host = remote_host
|
||||
self.enforce_context = bool(enforce_context)
|
||||
self.auth_tracker = AuthAttemptTracker()
|
||||
self.nodes = None
|
||||
self._system = None
|
||||
self._execution = None
|
||||
self._import_export = None
|
||||
self._sync = None
|
||||
self._users = None
|
||||
self._ai = None
|
||||
self.set_enforce_context(self.enforce_context)
|
||||
|
||||
if mode == "local":
|
||||
self._init_local()
|
||||
@@ -28,14 +39,26 @@ class ServiceProvider:
|
||||
else:
|
||||
raise ValueError(f"Unknown service mode: {mode}")
|
||||
|
||||
def _init_local(self):
|
||||
from .node_service import NodeService
|
||||
from .profile_service import ProfileService
|
||||
from .config_service import ConfigService
|
||||
from .plugin_service import PluginService
|
||||
from .context_service import ContextService
|
||||
def set_enforce_context(self, enforce_context: bool) -> None:
|
||||
self.enforce_context = bool(enforce_context)
|
||||
if self.config is not None and not hasattr(self.config, "assert_called"):
|
||||
self.config._enforce_context = self.enforce_context
|
||||
for svc in (
|
||||
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.config_svc = ConfigService(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
|
||||
from .config_service import ConfigService
|
||||
from .context_service import ContextService
|
||||
|
||||
self.config_svc = ConfigService(self.config)
|
||||
self.context = ContextService(self.config)
|
||||
|
||||
if not self.remote_host:
|
||||
raise InvalidConfigurationError("Remote host must be specified in remote mode")
|
||||
|
||||
import grpc
|
||||
import os
|
||||
|
||||
import grpc
|
||||
|
||||
from ..grpc_layer.stubs import (
|
||||
NodeStub, ProfileStub, PluginStub, AIStub,
|
||||
ExecutionStub, ImportExportStub, SystemStub,
|
||||
ConfigStub, AuthClientInterceptor, AuthStub
|
||||
AIStub,
|
||||
AuthClientInterceptor,
|
||||
AuthStub,
|
||||
ConfigStub,
|
||||
ExecutionStub,
|
||||
ImportExportStub,
|
||||
NodeStub,
|
||||
PluginStub,
|
||||
ProfileStub,
|
||||
SystemStub,
|
||||
)
|
||||
|
||||
def get_token():
|
||||
@@ -65,11 +98,8 @@ class ServiceProvider:
|
||||
return env_token
|
||||
token_path = os.path.join(self.config.defaultdir, ".token")
|
||||
if os.path.exists(token_path):
|
||||
try:
|
||||
with open(token_path, "r") as f:
|
||||
with suppress(OSError, UnicodeDecodeError), open(token_path) as f:
|
||||
return f.read().strip()
|
||||
except Exception:
|
||||
pass
|
||||
return None
|
||||
|
||||
channel = grpc.insecure_channel(self.remote_host)
|
||||
@@ -86,7 +116,12 @@ class ServiceProvider:
|
||||
self.plugins = PluginStub(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.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.auth = AuthStub(channel, remote_host=self.remote_host)
|
||||
|
||||
@@ -105,7 +140,13 @@ class ServiceProvider:
|
||||
def execution(self):
|
||||
if self._execution is None and self.mode == "local":
|
||||
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
|
||||
|
||||
@execution.setter
|
||||
@@ -116,6 +157,12 @@ class ServiceProvider:
|
||||
def import_export(self):
|
||||
if self._import_export is None and self.mode == "local":
|
||||
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)
|
||||
return self._import_export
|
||||
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
@@ -80,11 +80,22 @@ class TestTreeCompletions:
|
||||
assert "--shell-command" in config_completions
|
||||
assert "--shell-prompt" 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
|
||||
auth_comp = resolve_completion(["config", "--engineer-auth", ""], tree)
|
||||
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:
|
||||
# e.g., connpy config --engineer-auth some_val
|
||||
# should loop back and resolve to config options
|
||||
@@ -93,6 +104,10 @@ class TestTreeCompletions:
|
||||
assert "--engineer-auth" 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):
|
||||
from connpy.completion import _build_tree, resolve_completion
|
||||
tree = _build_tree([], [], [], {}, "/tmp")
|
||||
|
||||
+27
-11
@@ -150,7 +150,7 @@ el.replaceWith(d);
|
||||
import yaml
|
||||
try:
|
||||
# 1. Parse YAML
|
||||
data = yaml.load(playbook_yaml, Loader=yaml.FullLoader)
|
||||
data = yaml.safe_load(playbook_yaml)
|
||||
except Exception as e:
|
||||
return {"valid": False, "error": f"YAML Syntax Error: {e}"}
|
||||
|
||||
@@ -473,7 +473,7 @@ el.replaceWith(d);
|
||||
import yaml
|
||||
try:
|
||||
# 1. Parse YAML
|
||||
data = yaml.load(playbook_yaml, Loader=yaml.FullLoader)
|
||||
data = yaml.safe_load(playbook_yaml)
|
||||
except Exception as e:
|
||||
return {"valid": False, "error": f"YAML Syntax Error: {e}"}
|
||||
|
||||
@@ -638,6 +638,9 @@ class ai:
|
||||
self.session_path = os.path.join(self.sessions_dir, f"{self.session_id}.json") if self.session_id else None
|
||||
|
||||
# Agnostic base prompts
|
||||
self._update_base_prompts()
|
||||
|
||||
def _update_base_prompts(self):
|
||||
architect_instructions = ""
|
||||
if self.has_architect:
|
||||
architect_instructions = """
|
||||
@@ -678,7 +681,7 @@ class ai:
|
||||
- NO WANDERING: Do not speculate. If stuck, report attempts.
|
||||
- SAFETY: When you use 'run_commands' with configuration commands, the system automatically prompts the user for confirmation. Just execute - don't ask permission first.
|
||||
{architect_instructions}
|
||||
Network Context: {{self.long_term_memory if self.long_term_memory else "Empty."}}
|
||||
Network Context: {self.long_term_memory if self.long_term_memory else "Empty."}
|
||||
""").strip()
|
||||
|
||||
self._architect_base_prompt = dedent(f"""
|
||||
@@ -718,6 +721,7 @@ class ai:
|
||||
@property
|
||||
def engineer_system_prompt(self):
|
||||
"""Build engineer system prompt with plugin extensions."""
|
||||
self._update_base_prompts()
|
||||
if self.engineer_prompt_extensions:
|
||||
extensions = "\n".join(self.engineer_prompt_extensions)
|
||||
return self._engineer_base_prompt + f"\n\nPlugin Capabilities:\n{extensions}"
|
||||
@@ -726,6 +730,7 @@ class ai:
|
||||
@property
|
||||
def architect_system_prompt(self):
|
||||
"""Build architect system prompt with plugin extensions."""
|
||||
self._update_base_prompts()
|
||||
prompt = self._architect_base_prompt
|
||||
if getattr(self, "one_shot", False):
|
||||
prompt += "\n\nCRITICAL 1-SHOT DIAGNOSTICS DIRECTIVE:\nYou are running in a 1-shot offline diagnostics mode. There is no active conversation loop, and you are NOT conversing with a Network Engineer. You MUST deliver your complete strategic analysis immediately and directly to the user. Do not suggest or attempt to delegate/return control to the engineer."
|
||||
@@ -1065,6 +1070,7 @@ class ai:
|
||||
# Reload memory after update
|
||||
with open(self.memory_path, "r") as f:
|
||||
self.long_term_memory = f.read()
|
||||
self._update_base_prompts()
|
||||
|
||||
return "Memory updated successfully."
|
||||
except PermissionError as e:
|
||||
@@ -1300,7 +1306,7 @@ class ai:
|
||||
# Silently fail for LLM tools
|
||||
pass
|
||||
|
||||
if self.architect_key:
|
||||
if self.has_architect:
|
||||
base_tools.extend([
|
||||
{"type": "function", "function": {"name": "consult_architect", "description": "Ask the Strategic Reasoning Engine for advice on complex design, architecture, or troubleshooting decisions. You remain in control and will present the response to the user. Use this for: configuration planning, design validation, complex troubleshooting.", "parameters": {"type": "object", "properties": {"question": {"type": "string", "description": "Strategic question or decision needed."}, "technical_summary": {"type": "string", "description": "Technical findings and context gathered so far."}}, "required": ["question", "technical_summary"]}}},
|
||||
{"type": "function", "function": {"name": "escalate_to_architect", "description": "Transfer full control to the Strategic Reasoning Engine. Use ONLY when the user explicitly requests the Architect or when the problem requires strategic oversight beyond consultation. After escalation, the Architect takes over the conversation.", "parameters": {"type": "object", "properties": {"reason": {"type": "string", "description": "Why you're escalating (e.g. 'User requested Architect', 'Complex multi-site design needed')."}, "context": {"type": "string", "description": "Full context and findings to hand over."}}, "required": ["reason", "context"]}}}
|
||||
@@ -1657,14 +1663,17 @@ class ai:
|
||||
if status: update_status("[architect]Engineer consulting Architect...")
|
||||
try:
|
||||
# Consultation only - Engineer stays in control
|
||||
auth_kwargs = dict(self.architect_auth or {})
|
||||
if self.architect_key and "api_key" not in auth_kwargs:
|
||||
auth_kwargs["api_key"] = self.architect_key
|
||||
claude_resp = completion(
|
||||
model=self.architect_model,
|
||||
messages=[
|
||||
{"role": "system", "content": self.architect_system_prompt},
|
||||
{"role": "user", "content": f"The Engineer needs your strategic advice.\n\nTECHNICAL SUMMARY: {args['technical_summary']}\n\nQUESTION: {args['question']}\n\nProvide strategic guidance. The Engineer will continue handling the user."}
|
||||
],
|
||||
api_key=self.architect_key,
|
||||
num_retries=3
|
||||
num_retries=3,
|
||||
**auth_kwargs
|
||||
)
|
||||
obs = claude_resp.choices[0].message.content
|
||||
if debug:
|
||||
@@ -2101,11 +2110,11 @@ Node: {node_name}"""
|
||||
<dl>
|
||||
<dt id="connpy.ai.ai.SAFE_COMMANDS"><code class="name">var <span class="ident">SAFE_COMMANDS</span></code></dt>
|
||||
<dd>
|
||||
<div class="desc"><p>The type of the None singleton.</p></div>
|
||||
<div class="desc"></div>
|
||||
</dd>
|
||||
<dt id="connpy.ai.ai.deferred_class_hooks"><code class="name">var <span class="ident">deferred_class_hooks</span></code></dt>
|
||||
<dd>
|
||||
<div class="desc"><p>The type of the None singleton.</p></div>
|
||||
<div class="desc"></div>
|
||||
</dd>
|
||||
</dl>
|
||||
<h3>Instance variables</h3>
|
||||
@@ -2119,6 +2128,7 @@ Node: {node_name}"""
|
||||
<pre><code class="python">@property
|
||||
def architect_system_prompt(self):
|
||||
"""Build architect system prompt with plugin extensions."""
|
||||
self._update_base_prompts()
|
||||
prompt = self._architect_base_prompt
|
||||
if getattr(self, "one_shot", False):
|
||||
prompt += "\n\nCRITICAL 1-SHOT DIAGNOSTICS DIRECTIVE:\nYou are running in a 1-shot offline diagnostics mode. There is no active conversation loop, and you are NOT conversing with a Network Engineer. You MUST deliver your complete strategic analysis immediately and directly to the user. Do not suggest or attempt to delegate/return control to the engineer."
|
||||
@@ -2138,6 +2148,7 @@ def architect_system_prompt(self):
|
||||
<pre><code class="python">@property
|
||||
def engineer_system_prompt(self):
|
||||
"""Build engineer system prompt with plugin extensions."""
|
||||
self._update_base_prompts()
|
||||
if self.engineer_prompt_extensions:
|
||||
extensions = "\n".join(self.engineer_prompt_extensions)
|
||||
return self._engineer_base_prompt + f"\n\nPlugin Capabilities:\n{extensions}"
|
||||
@@ -2661,14 +2672,17 @@ def ask(self, user_input, dryrun=False, chat_history=None, status=None, debug=Fa
|
||||
if status: update_status("[architect]Engineer consulting Architect...")
|
||||
try:
|
||||
# Consultation only - Engineer stays in control
|
||||
auth_kwargs = dict(self.architect_auth or {})
|
||||
if self.architect_key and "api_key" not in auth_kwargs:
|
||||
auth_kwargs["api_key"] = self.architect_key
|
||||
claude_resp = completion(
|
||||
model=self.architect_model,
|
||||
messages=[
|
||||
{"role": "system", "content": self.architect_system_prompt},
|
||||
{"role": "user", "content": f"The Engineer needs your strategic advice.\n\nTECHNICAL SUMMARY: {args['technical_summary']}\n\nQUESTION: {args['question']}\n\nProvide strategic guidance. The Engineer will continue handling the user."}
|
||||
],
|
||||
api_key=self.architect_key,
|
||||
num_retries=3
|
||||
num_retries=3,
|
||||
**auth_kwargs
|
||||
)
|
||||
obs = claude_resp.choices[0].message.content
|
||||
if debug:
|
||||
@@ -2976,6 +2990,7 @@ def confirm(self, user_input): return True</code></pre>
|
||||
# Reload memory after update
|
||||
with open(self.memory_path, "r") as f:
|
||||
self.long_term_memory = f.read()
|
||||
self._update_base_prompts()
|
||||
|
||||
return "Memory updated successfully."
|
||||
except PermissionError as e:
|
||||
@@ -3024,8 +3039,9 @@ def confirm(self, user_input): return True</code></pre>
|
||||
self.tool_status_formatters[name] = status_formatter</code></pre>
|
||||
</details>
|
||||
<div class="desc"><p>Register an external tool for the AI system.</p>
|
||||
<h2 id="args">Args</h2>
|
||||
<dl>
|
||||
<dt>Args</dt>
|
||||
<dt>-----=</dt>
|
||||
<dt><strong><code>tool_definition</code></strong> : <code>dict</code></dt>
|
||||
<dd>OpenAI-compatible tool definition.</dd>
|
||||
<dt><strong><code>handler</code></strong> : <code>callable</code></dt>
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -83,7 +83,7 @@ el.replaceWith(d);
|
||||
try:
|
||||
self.app.services.ai.delete_session(args.delete_session[0])
|
||||
printer.success(f"Session {args.delete_session[0]} deleted.")
|
||||
except Exception as e:
|
||||
except Exception as e: # noqa: BLE001
|
||||
printer.error(str(e))
|
||||
return
|
||||
|
||||
@@ -236,7 +236,7 @@ el.replaceWith(d);
|
||||
try:
|
||||
self.app.services.ai.configure_mcp(name, url=url, auto_load_on_os=os_filter)
|
||||
printer.success(f"MCP server '{name}' added/updated.")
|
||||
except Exception as e:
|
||||
except Exception as e: # noqa: BLE001
|
||||
printer.error(str(e))
|
||||
return
|
||||
|
||||
@@ -248,7 +248,7 @@ el.replaceWith(d);
|
||||
try:
|
||||
self.app.services.ai.configure_mcp(name, remove=True)
|
||||
printer.success(f"MCP server '{name}' removed.")
|
||||
except Exception as e:
|
||||
except Exception as e: # noqa: BLE001
|
||||
printer.error(str(e))
|
||||
return
|
||||
|
||||
@@ -261,13 +261,54 @@ el.replaceWith(d);
|
||||
try:
|
||||
self.app.services.ai.configure_mcp(name, enabled=enabled)
|
||||
printer.success(f"MCP server '{name}' {'enabled' if enabled else 'disabled'}.")
|
||||
except Exception as e:
|
||||
except Exception as e: # noqa: BLE001
|
||||
printer.error(str(e))
|
||||
return
|
||||
|
||||
elif action == "serve":
|
||||
transport = "stdio"
|
||||
host = "127.0.0.1"
|
||||
port = 8000
|
||||
trust = getattr(args, "trust", False)
|
||||
|
||||
idx = 1
|
||||
while idx < len(mcp_args):
|
||||
tok = mcp_args[idx]
|
||||
if tok in ["--sse", "sse"]:
|
||||
transport = "sse"
|
||||
elif tok in ["--stdio", "stdio"]:
|
||||
transport = "stdio"
|
||||
elif tok in ["--trust", "trust"]:
|
||||
trust = True
|
||||
elif tok in ["--port", "-p"]:
|
||||
idx += 1
|
||||
if idx < len(mcp_args):
|
||||
try:
|
||||
port = int(mcp_args[idx])
|
||||
except ValueError:
|
||||
printer.error(f"Invalid port: {mcp_args[idx]}")
|
||||
return
|
||||
elif tok.startswith("--port="):
|
||||
try:
|
||||
port = int(tok.split("=", 1)[1])
|
||||
except ValueError:
|
||||
printer.error(f"Invalid port: {tok}")
|
||||
return
|
||||
elif tok in ["--host", "-h"]:
|
||||
idx += 1
|
||||
if idx < len(mcp_args):
|
||||
host = mcp_args[idx]
|
||||
elif tok.startswith("--host="):
|
||||
host = tok.split("=", 1)[1]
|
||||
idx += 1
|
||||
|
||||
from ..mcp_server import ConnpyMCPServer
|
||||
server = ConnpyMCPServer(config=self.app.config, services=self.app.services, trust=trust)
|
||||
return server.run(transport=transport, host=host, port=port)
|
||||
|
||||
else:
|
||||
printer.error(f"Unknown MCP action: {action}")
|
||||
printer.info("Available actions: list, add, remove, enable, disable")
|
||||
printer.info("Available actions: list, add, remove, enable, disable, serve")
|
||||
return
|
||||
|
||||
# 2. Interactive Wizard Mode (if no arguments provided)
|
||||
@@ -309,15 +350,16 @@ el.replaceWith(d);
|
||||
self.app.services.ai.configure_mcp(result["name"], remove=True)
|
||||
printer.success(f"MCP server '{result['name']}' removed.")
|
||||
|
||||
except Exception as e:
|
||||
except Exception as e: # noqa: BLE001
|
||||
printer.error(str(e))
|
||||
|
||||
def _parse_auth_value(self, value):
|
||||
if not value or value.lower() in ["none", "clear"]:
|
||||
return None
|
||||
import os
|
||||
import yaml
|
||||
import json
|
||||
import os
|
||||
|
||||
import yaml
|
||||
if os.path.exists(value):
|
||||
try:
|
||||
with open(value, "r") as f:
|
||||
@@ -326,7 +368,7 @@ el.replaceWith(d);
|
||||
return json.loads(content)
|
||||
except ValueError:
|
||||
return yaml.safe_load(content)
|
||||
except Exception as e:
|
||||
except Exception as e: # noqa: BLE001
|
||||
printer.error(f"Failed to read/parse auth file '{value}': {e}")
|
||||
sys.exit(1)
|
||||
|
||||
@@ -338,7 +380,7 @@ el.replaceWith(d);
|
||||
if isinstance(parsed, dict):
|
||||
return parsed
|
||||
raise ValueError()
|
||||
except Exception:
|
||||
except Exception: # noqa: BLE001
|
||||
printer.error("Auth parameter must be a valid JSON/YAML string, or a path to a JSON/YAML file.")
|
||||
sys.exit(1)</code></pre>
|
||||
</details>
|
||||
@@ -387,7 +429,7 @@ el.replaceWith(d);
|
||||
try:
|
||||
self.app.services.ai.configure_mcp(name, url=url, auto_load_on_os=os_filter)
|
||||
printer.success(f"MCP server '{name}' added/updated.")
|
||||
except Exception as e:
|
||||
except Exception as e: # noqa: BLE001
|
||||
printer.error(str(e))
|
||||
return
|
||||
|
||||
@@ -399,7 +441,7 @@ el.replaceWith(d);
|
||||
try:
|
||||
self.app.services.ai.configure_mcp(name, remove=True)
|
||||
printer.success(f"MCP server '{name}' removed.")
|
||||
except Exception as e:
|
||||
except Exception as e: # noqa: BLE001
|
||||
printer.error(str(e))
|
||||
return
|
||||
|
||||
@@ -412,13 +454,54 @@ el.replaceWith(d);
|
||||
try:
|
||||
self.app.services.ai.configure_mcp(name, enabled=enabled)
|
||||
printer.success(f"MCP server '{name}' {'enabled' if enabled else 'disabled'}.")
|
||||
except Exception as e:
|
||||
except Exception as e: # noqa: BLE001
|
||||
printer.error(str(e))
|
||||
return
|
||||
|
||||
elif action == "serve":
|
||||
transport = "stdio"
|
||||
host = "127.0.0.1"
|
||||
port = 8000
|
||||
trust = getattr(args, "trust", False)
|
||||
|
||||
idx = 1
|
||||
while idx < len(mcp_args):
|
||||
tok = mcp_args[idx]
|
||||
if tok in ["--sse", "sse"]:
|
||||
transport = "sse"
|
||||
elif tok in ["--stdio", "stdio"]:
|
||||
transport = "stdio"
|
||||
elif tok in ["--trust", "trust"]:
|
||||
trust = True
|
||||
elif tok in ["--port", "-p"]:
|
||||
idx += 1
|
||||
if idx < len(mcp_args):
|
||||
try:
|
||||
port = int(mcp_args[idx])
|
||||
except ValueError:
|
||||
printer.error(f"Invalid port: {mcp_args[idx]}")
|
||||
return
|
||||
elif tok.startswith("--port="):
|
||||
try:
|
||||
port = int(tok.split("=", 1)[1])
|
||||
except ValueError:
|
||||
printer.error(f"Invalid port: {tok}")
|
||||
return
|
||||
elif tok in ["--host", "-h"]:
|
||||
idx += 1
|
||||
if idx < len(mcp_args):
|
||||
host = mcp_args[idx]
|
||||
elif tok.startswith("--host="):
|
||||
host = tok.split("=", 1)[1]
|
||||
idx += 1
|
||||
|
||||
from ..mcp_server import ConnpyMCPServer
|
||||
server = ConnpyMCPServer(config=self.app.config, services=self.app.services, trust=trust)
|
||||
return server.run(transport=transport, host=host, port=port)
|
||||
|
||||
else:
|
||||
printer.error(f"Unknown MCP action: {action}")
|
||||
printer.info("Available actions: list, add, remove, enable, disable")
|
||||
printer.info("Available actions: list, add, remove, enable, disable, serve")
|
||||
return
|
||||
|
||||
# 2. Interactive Wizard Mode (if no arguments provided)
|
||||
@@ -460,7 +543,7 @@ el.replaceWith(d);
|
||||
self.app.services.ai.configure_mcp(result["name"], remove=True)
|
||||
printer.success(f"MCP server '{result['name']}' removed.")
|
||||
|
||||
except Exception as e:
|
||||
except Exception as e: # noqa: BLE001
|
||||
printer.error(str(e))</code></pre>
|
||||
</details>
|
||||
<div class="desc"><p>Handle MCP server configuration via CLI tokens or interactive wizard.</p></div>
|
||||
@@ -497,7 +580,7 @@ el.replaceWith(d);
|
||||
try:
|
||||
self.app.services.ai.delete_session(args.delete_session[0])
|
||||
printer.success(f"Session {args.delete_session[0]} deleted.")
|
||||
except Exception as e:
|
||||
except Exception as e: # noqa: BLE001
|
||||
printer.error(str(e))
|
||||
return
|
||||
|
||||
|
||||
@@ -80,7 +80,12 @@ el.replaceWith(d);
|
||||
"sync_remote": self.set_sync_remote,
|
||||
"shell_command": self.set_shell_config,
|
||||
"shell_prompt": self.set_shell_config,
|
||||
"shell_os": self.set_shell_config
|
||||
"shell_os": self.set_shell_config,
|
||||
"max_auth_prompts": self.set_auth_budget,
|
||||
"max_total_auth_attempts": self.set_auth_budget,
|
||||
"max_hop_host_attempts": self.set_auth_budget,
|
||||
"max_global_user_attempts": self.set_auth_budget,
|
||||
"disable_fallback_on_insecure_hostkey": self.set_disable_fallback_on_insecure_hostkey,
|
||||
}
|
||||
handler = actions.get(getattr(args, "command", None))
|
||||
if handler:
|
||||
@@ -251,7 +256,71 @@ el.replaceWith(d);
|
||||
self.app.services.config_svc.update_setting("shell", shell_cfg)
|
||||
printer.success("Config saved")
|
||||
except (ConnpyError, InvalidConfigurationError) as e:
|
||||
printer.error(str(e))</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 = {
|
||||
"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")</code></pre>
|
||||
</details>
|
||||
<div class="desc"></div>
|
||||
<h3>Methods</h3>
|
||||
@@ -285,7 +354,12 @@ el.replaceWith(d);
|
||||
"sync_remote": self.set_sync_remote,
|
||||
"shell_command": self.set_shell_config,
|
||||
"shell_prompt": self.set_shell_config,
|
||||
"shell_os": self.set_shell_config
|
||||
"shell_os": self.set_shell_config,
|
||||
"max_auth_prompts": self.set_auth_budget,
|
||||
"max_total_auth_attempts": self.set_auth_budget,
|
||||
"max_hop_host_attempts": self.set_auth_budget,
|
||||
"max_global_user_attempts": self.set_auth_budget,
|
||||
"disable_fallback_on_insecure_hostkey": self.set_disable_fallback_on_insecure_hostkey,
|
||||
}
|
||||
handler = actions.get(getattr(args, "command", None))
|
||||
if handler:
|
||||
@@ -333,6 +407,64 @@ el.replaceWith(d);
|
||||
</details>
|
||||
<div class="desc"></div>
|
||||
</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 = {
|
||||
"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")</code></pre>
|
||||
</details>
|
||||
<div class="desc"></div>
|
||||
</dd>
|
||||
<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>
|
||||
</code></dt>
|
||||
@@ -367,6 +499,32 @@ el.replaceWith(d);
|
||||
</details>
|
||||
<div class="desc"></div>
|
||||
</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 ("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")</code></pre>
|
||||
</details>
|
||||
<div class="desc"></div>
|
||||
</dd>
|
||||
<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>
|
||||
</code></dt>
|
||||
@@ -577,11 +735,13 @@ el.replaceWith(d);
|
||||
<ul>
|
||||
<li>
|
||||
<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.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_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_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>
|
||||
|
||||
+122
-30
@@ -72,6 +72,7 @@ el.replaceWith(d);
|
||||
questions.append(inquirer.Confirm("jumphost", message="Edit jumphost?"))
|
||||
questions.append(inquirer.Confirm("user", message="Edit User?"))
|
||||
questions.append(inquirer.Confirm("password", message="Edit password?"))
|
||||
questions.append(inquirer.Confirm("fallback", message="Edit fallback credentials?"))
|
||||
return inquirer.prompt(questions)
|
||||
|
||||
def questions_nodes(self, unique, uniques=None, edit=None):
|
||||
@@ -82,11 +83,13 @@ el.replaceWith(d);
|
||||
defaults["tags"] = ""
|
||||
if "jumphost" not in defaults:
|
||||
defaults["jumphost"] = ""
|
||||
except Exception:
|
||||
defaults = {"host": "", "protocol": "", "port": "", "user": "", "options": "", "logs": "", "tags": "", "password": "", "jumphost": ""}
|
||||
if "fallback" not in defaults:
|
||||
defaults["fallback"] = ""
|
||||
except Exception: # noqa: BLE001
|
||||
defaults = {"host": "", "protocol": "", "port": "", "user": "", "options": "", "logs": "", "tags": "", "password": "", "jumphost": "", "fallback": ""}
|
||||
node = {}
|
||||
if edit is None:
|
||||
edit = {"host": True, "protocol": True, "port": True, "user": True, "password": True, "options": True, "logs": True, "tags": True, "jumphost": True}
|
||||
edit = {"host": True, "protocol": True, "port": True, "user": True, "password": True, "options": True, "logs": True, "tags": True, "jumphost": True, "fallback": True}
|
||||
questions = []
|
||||
if edit["host"]:
|
||||
questions.append(inquirer.Text("host", message="Add Hostname or IP", validate=self.validators.host_validation, default=defaults["host"]))
|
||||
@@ -124,6 +127,8 @@ el.replaceWith(d);
|
||||
questions.append(inquirer.List("password", message="Password: Use a local password, no password or a list of profiles to reference?", choices=["Local Password", "Profiles", "No Password"]))
|
||||
else:
|
||||
node["password"] = defaults["password"]
|
||||
if not edit.get("fallback"):
|
||||
node["fallback"] = defaults.get("fallback", "")
|
||||
|
||||
answer = inquirer.prompt(questions)
|
||||
if answer is None:
|
||||
@@ -141,12 +146,28 @@ el.replaceWith(d);
|
||||
passa = inquirer.prompt(passq)
|
||||
if passa is None:
|
||||
return False
|
||||
answer["password"] = passa["password"].split(",")
|
||||
answer["password"] = [p.strip() for p in passa["password"].split(",") if p.strip()]
|
||||
elif answer["password"] == "No Password":
|
||||
answer["password"] = ""
|
||||
|
||||
if "tags" in answer and not answer["tags"].startswith("@") and answer["tags"]:
|
||||
answer["tags"] = ast.literal_eval(answer["tags"])
|
||||
if edit.get("fallback"):
|
||||
raw_def_fb = defaults.get("fallback", "")
|
||||
def_fb_str = json.dumps(raw_def_fb) if isinstance(raw_def_fb, (list, dict)) else str(raw_def_fb or "")
|
||||
fbq = [
|
||||
inquirer.Text(
|
||||
"fallback",
|
||||
message="Add fallback credentials (@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["type"] = "connection"
|
||||
@@ -160,11 +181,13 @@ el.replaceWith(d);
|
||||
defaults["tags"] = ""
|
||||
if "jumphost" not in defaults:
|
||||
defaults["jumphost"] = ""
|
||||
except Exception:
|
||||
defaults = {"host": "", "protocol": "", "port": "", "user": "", "options": "", "logs": "", "tags": "", "jumphost": ""}
|
||||
if "fallback" not in defaults:
|
||||
defaults["fallback"] = ""
|
||||
except Exception: # noqa: BLE001
|
||||
defaults = {"host": "", "protocol": "", "port": "", "user": "", "options": "", "logs": "", "tags": "", "jumphost": "", "fallback": ""}
|
||||
profile = {}
|
||||
if edit is None:
|
||||
edit = {"host": True, "protocol": True, "port": True, "user": True, "password": True, "options": True, "logs": True, "tags": True, "jumphost": True}
|
||||
edit = {"host": True, "protocol": True, "port": True, "user": True, "password": True, "options": True, "logs": True, "tags": True, "jumphost": True, "fallback": True}
|
||||
questions = []
|
||||
if edit["host"]:
|
||||
questions.append(inquirer.Text("host", message="Add Hostname or IP", default=defaults["host"]))
|
||||
@@ -202,17 +225,24 @@ el.replaceWith(d);
|
||||
questions.append(inquirer.Password("password", message="Set Password"))
|
||||
else:
|
||||
profile["password"] = defaults["password"]
|
||||
if edit.get("fallback"):
|
||||
raw_def_fb = defaults.get("fallback", "")
|
||||
def_fb_str = json.dumps(raw_def_fb) if isinstance(raw_def_fb, (list, dict)) else str(raw_def_fb or "")
|
||||
questions.append(inquirer.Text("fallback", message="Add fallback credentials (@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)
|
||||
if answer is None:
|
||||
return False
|
||||
|
||||
if "password" in answer:
|
||||
if answer["password"] != "":
|
||||
if "password" in answer and answer["password"] != "":
|
||||
answer["password"] = self.app.services.config_svc.encrypt_password(answer["password"])
|
||||
|
||||
if "tags" in answer and answer["tags"]:
|
||||
answer["tags"] = ast.literal_eval(answer["tags"])
|
||||
answer["tags"] = _parse_tags_input(answer["tags"])
|
||||
if "fallback" in answer:
|
||||
answer["fallback"] = _parse_fallback_input(answer["fallback"])
|
||||
|
||||
result = {**answer, **profile}
|
||||
result["id"] = unique
|
||||
@@ -241,17 +271,33 @@ el.replaceWith(d);
|
||||
if answer["password"] == "Local Password":
|
||||
passq = [inquirer.Password("password", message="Set Password")]
|
||||
passa = inquirer.prompt(passq)
|
||||
if passa is None:
|
||||
return False
|
||||
answer["password"] = self.app.services.config_svc.encrypt_password(passa["password"])
|
||||
elif answer["password"] == "Profiles":
|
||||
passq = [(inquirer.Text("password", message="Set a @profile or a comma separated list of @profiles", validate=self.validators.pass_validation))]
|
||||
passa = inquirer.prompt(passq)
|
||||
answer["password"] = passa["password"].split(",")
|
||||
if passa is None:
|
||||
return False
|
||||
answer["password"] = [p.strip() for p in passa["password"].split(",") if p.strip()]
|
||||
elif answer["password"] == "No Password":
|
||||
answer["password"] = ""
|
||||
|
||||
fbq = [
|
||||
inquirer.Text(
|
||||
"fallback",
|
||||
message="Add fallback credentials (@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"
|
||||
if "tags" in answer and not answer["tags"].startswith("@") and answer["tags"]:
|
||||
answer["tags"] = ast.literal_eval(answer["tags"])
|
||||
if "tags" in answer and answer["tags"] and not str(answer["tags"]).startswith("@"):
|
||||
answer["tags"] = _parse_tags_input(answer["tags"])
|
||||
|
||||
return answer
|
||||
|
||||
@@ -463,17 +509,33 @@ el.replaceWith(d);
|
||||
if answer["password"] == "Local Password":
|
||||
passq = [inquirer.Password("password", message="Set Password")]
|
||||
passa = inquirer.prompt(passq)
|
||||
if passa is None:
|
||||
return False
|
||||
answer["password"] = self.app.services.config_svc.encrypt_password(passa["password"])
|
||||
elif answer["password"] == "Profiles":
|
||||
passq = [(inquirer.Text("password", message="Set a @profile or a comma separated list of @profiles", validate=self.validators.pass_validation))]
|
||||
passa = inquirer.prompt(passq)
|
||||
answer["password"] = passa["password"].split(",")
|
||||
if passa is None:
|
||||
return False
|
||||
answer["password"] = [p.strip() for p in passa["password"].split(",") if p.strip()]
|
||||
elif answer["password"] == "No Password":
|
||||
answer["password"] = ""
|
||||
|
||||
fbq = [
|
||||
inquirer.Text(
|
||||
"fallback",
|
||||
message="Add fallback credentials (@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"
|
||||
if "tags" in answer and not answer["tags"].startswith("@") and answer["tags"]:
|
||||
answer["tags"] = ast.literal_eval(answer["tags"])
|
||||
if "tags" in answer and answer["tags"] and not str(answer["tags"]).startswith("@"):
|
||||
answer["tags"] = _parse_tags_input(answer["tags"])
|
||||
|
||||
return answer</code></pre>
|
||||
</details>
|
||||
@@ -499,6 +561,7 @@ el.replaceWith(d);
|
||||
questions.append(inquirer.Confirm("jumphost", message="Edit jumphost?"))
|
||||
questions.append(inquirer.Confirm("user", message="Edit User?"))
|
||||
questions.append(inquirer.Confirm("password", message="Edit password?"))
|
||||
questions.append(inquirer.Confirm("fallback", message="Edit fallback credentials?"))
|
||||
return inquirer.prompt(questions)</code></pre>
|
||||
</details>
|
||||
<div class="desc"></div>
|
||||
@@ -519,11 +582,13 @@ el.replaceWith(d);
|
||||
defaults["tags"] = ""
|
||||
if "jumphost" not in defaults:
|
||||
defaults["jumphost"] = ""
|
||||
except Exception:
|
||||
defaults = {"host": "", "protocol": "", "port": "", "user": "", "options": "", "logs": "", "tags": "", "password": "", "jumphost": ""}
|
||||
if "fallback" not in defaults:
|
||||
defaults["fallback"] = ""
|
||||
except Exception: # noqa: BLE001
|
||||
defaults = {"host": "", "protocol": "", "port": "", "user": "", "options": "", "logs": "", "tags": "", "password": "", "jumphost": "", "fallback": ""}
|
||||
node = {}
|
||||
if edit is None:
|
||||
edit = {"host": True, "protocol": True, "port": True, "user": True, "password": True, "options": True, "logs": True, "tags": True, "jumphost": True}
|
||||
edit = {"host": True, "protocol": True, "port": True, "user": True, "password": True, "options": True, "logs": True, "tags": True, "jumphost": True, "fallback": True}
|
||||
questions = []
|
||||
if edit["host"]:
|
||||
questions.append(inquirer.Text("host", message="Add Hostname or IP", validate=self.validators.host_validation, default=defaults["host"]))
|
||||
@@ -561,6 +626,8 @@ el.replaceWith(d);
|
||||
questions.append(inquirer.List("password", message="Password: Use a local password, no password or a list of profiles to reference?", choices=["Local Password", "Profiles", "No Password"]))
|
||||
else:
|
||||
node["password"] = defaults["password"]
|
||||
if not edit.get("fallback"):
|
||||
node["fallback"] = defaults.get("fallback", "")
|
||||
|
||||
answer = inquirer.prompt(questions)
|
||||
if answer is None:
|
||||
@@ -578,12 +645,28 @@ el.replaceWith(d);
|
||||
passa = inquirer.prompt(passq)
|
||||
if passa is None:
|
||||
return False
|
||||
answer["password"] = passa["password"].split(",")
|
||||
answer["password"] = [p.strip() for p in passa["password"].split(",") if p.strip()]
|
||||
elif answer["password"] == "No Password":
|
||||
answer["password"] = ""
|
||||
|
||||
if "tags" in answer and not answer["tags"].startswith("@") and answer["tags"]:
|
||||
answer["tags"] = ast.literal_eval(answer["tags"])
|
||||
if edit.get("fallback"):
|
||||
raw_def_fb = defaults.get("fallback", "")
|
||||
def_fb_str = json.dumps(raw_def_fb) if isinstance(raw_def_fb, (list, dict)) else str(raw_def_fb or "")
|
||||
fbq = [
|
||||
inquirer.Text(
|
||||
"fallback",
|
||||
message="Add fallback credentials (@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["type"] = "connection"
|
||||
@@ -607,11 +690,13 @@ el.replaceWith(d);
|
||||
defaults["tags"] = ""
|
||||
if "jumphost" not in defaults:
|
||||
defaults["jumphost"] = ""
|
||||
except Exception:
|
||||
defaults = {"host": "", "protocol": "", "port": "", "user": "", "options": "", "logs": "", "tags": "", "jumphost": ""}
|
||||
if "fallback" not in defaults:
|
||||
defaults["fallback"] = ""
|
||||
except Exception: # noqa: BLE001
|
||||
defaults = {"host": "", "protocol": "", "port": "", "user": "", "options": "", "logs": "", "tags": "", "jumphost": "", "fallback": ""}
|
||||
profile = {}
|
||||
if edit is None:
|
||||
edit = {"host": True, "protocol": True, "port": True, "user": True, "password": True, "options": True, "logs": True, "tags": True, "jumphost": True}
|
||||
edit = {"host": True, "protocol": True, "port": True, "user": True, "password": True, "options": True, "logs": True, "tags": True, "jumphost": True, "fallback": True}
|
||||
questions = []
|
||||
if edit["host"]:
|
||||
questions.append(inquirer.Text("host", message="Add Hostname or IP", default=defaults["host"]))
|
||||
@@ -649,17 +734,24 @@ el.replaceWith(d);
|
||||
questions.append(inquirer.Password("password", message="Set Password"))
|
||||
else:
|
||||
profile["password"] = defaults["password"]
|
||||
if edit.get("fallback"):
|
||||
raw_def_fb = defaults.get("fallback", "")
|
||||
def_fb_str = json.dumps(raw_def_fb) if isinstance(raw_def_fb, (list, dict)) else str(raw_def_fb or "")
|
||||
questions.append(inquirer.Text("fallback", message="Add fallback credentials (@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)
|
||||
if answer is None:
|
||||
return False
|
||||
|
||||
if "password" in answer:
|
||||
if answer["password"] != "":
|
||||
if "password" in answer and answer["password"] != "":
|
||||
answer["password"] = self.app.services.config_svc.encrypt_password(answer["password"])
|
||||
|
||||
if "tags" in answer and answer["tags"]:
|
||||
answer["tags"] = ast.literal_eval(answer["tags"])
|
||||
answer["tags"] = _parse_tags_input(answer["tags"])
|
||||
if "fallback" in answer:
|
||||
answer["fallback"] = _parse_fallback_input(answer["fallback"])
|
||||
|
||||
result = {**answer, **profile}
|
||||
result["id"] = unique
|
||||
|
||||
@@ -193,6 +193,7 @@ el.replaceWith(d);
|
||||
uniques = self.app.services.nodes.explode_unique(args.data)
|
||||
if not uniques:
|
||||
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)
|
||||
printer.success(f"{args.data} added successfully")
|
||||
else:
|
||||
@@ -364,6 +365,7 @@ def forms(self):
|
||||
uniques = self.app.services.nodes.explode_unique(args.data)
|
||||
if not uniques:
|
||||
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)
|
||||
printer.success(f"{args.data} added successfully")
|
||||
else:
|
||||
|
||||
@@ -210,7 +210,10 @@ el.replaceWith(d);
|
||||
path = args.data[0]
|
||||
try:
|
||||
with open(path, "r") as f:
|
||||
playbook = yaml.load(f, Loader=yaml.FullLoader)
|
||||
playbook = yaml.safe_load(f)
|
||||
|
||||
if not isinstance(playbook, dict):
|
||||
raise TypeError("Invalid playbook format: expected a dictionary")
|
||||
|
||||
# Check preflight first before any task runs
|
||||
if getattr(args, "preflight_ai", False):
|
||||
@@ -1021,7 +1024,10 @@ el.replaceWith(d);
|
||||
path = args.data[0]
|
||||
try:
|
||||
with open(path, "r") as f:
|
||||
playbook = yaml.load(f, Loader=yaml.FullLoader)
|
||||
playbook = yaml.safe_load(f)
|
||||
|
||||
if not isinstance(playbook, dict):
|
||||
raise TypeError("Invalid playbook format: expected a dictionary")
|
||||
|
||||
# Check preflight first before any task runs
|
||||
if getattr(args, "preflight_ai", False):
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
+227
-50
@@ -106,50 +106,119 @@ el.replaceWith(d);
|
||||
return True
|
||||
|
||||
def pass_validation(self, answers, current, regex = "(^@.+$)"):
|
||||
if is_compose_expression(current):
|
||||
try:
|
||||
validate_compose_syntax(current)
|
||||
except SanitizedAuthError as exc:
|
||||
_raise_val_err(str(exc))
|
||||
return True
|
||||
profiles = current.split(",")
|
||||
for i in profiles:
|
||||
if not re.match(regex, i) or i[1:] not in self.app.profiles:
|
||||
_raise_val_err("Profile {} don't exist".format(i))
|
||||
return True
|
||||
|
||||
def _validate_tags_dict(self, current: str):
|
||||
if len(current.encode("utf-8", errors="replace")) > MAX_JSON_INPUT_BYTES:
|
||||
_raise_val_err(f"Tags exceed maximum size of {MAX_JSON_INPUT_BYTES} bytes")
|
||||
parsed = None
|
||||
try:
|
||||
parsed = json.loads(current)
|
||||
except (ValueError, TypeError):
|
||||
try:
|
||||
parsed = ast.literal_eval(current)
|
||||
except (ValueError, TypeError, SyntaxError, MemoryError, RecursionError):
|
||||
parsed = None
|
||||
if not isinstance(parsed, dict):
|
||||
_raise_val_err("Tags should be a python dictionary.")
|
||||
try:
|
||||
validate_json_depth_and_size(
|
||||
parsed,
|
||||
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||
)
|
||||
validate_auth_budget_tags(parsed)
|
||||
except (ValueError, TypeError, SanitizedAuthError) as exc:
|
||||
_raise_val_err(str(exc))
|
||||
return True
|
||||
|
||||
def tags_validation(self, answers, current):
|
||||
if current.startswith("@"):
|
||||
if current[1:] not in self.app.profiles:
|
||||
_raise_val_err("Profile {} don't exist".format(current))
|
||||
elif current != "":
|
||||
isdict = False
|
||||
try:
|
||||
isdict = ast.literal_eval(current)
|
||||
except Exception:
|
||||
pass
|
||||
if not isinstance (isdict, dict):
|
||||
_raise_val_err("Tags should be a python dictionary.".format(current))
|
||||
return self._validate_tags_dict(current)
|
||||
return True
|
||||
|
||||
def profile_tags_validation(self, answers, current):
|
||||
if current != "":
|
||||
isdict = False
|
||||
try:
|
||||
isdict = ast.literal_eval(current)
|
||||
except Exception:
|
||||
pass
|
||||
if not isinstance (isdict, dict):
|
||||
_raise_val_err("Tags should be a python dictionary.".format(current))
|
||||
return self._validate_tags_dict(current)
|
||||
return True
|
||||
|
||||
def fallback_validation(self, answers, current):
|
||||
if current in (None, ""):
|
||||
return True
|
||||
stripped = str(current).strip()
|
||||
if not stripped:
|
||||
return True
|
||||
if len(stripped.encode("utf-8", errors="replace")) > MAX_JSON_INPUT_BYTES:
|
||||
_raise_val_err(f"Fallback exceeds maximum size of {MAX_JSON_INPUT_BYTES} bytes")
|
||||
if stripped.startswith("@") and not 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):
|
||||
if current.startswith("@"):
|
||||
if current[1:] not in self.app.profiles:
|
||||
_raise_val_err("Profile {} don't exist".format(current))
|
||||
elif current != "":
|
||||
if current not in self.app.nodes_list:
|
||||
_raise_val_err("Node {} don't exist.".format(current))
|
||||
_raise_val_err(f"Profile {current} don't exist")
|
||||
elif current != "" and not self._node_exists(current):
|
||||
_raise_val_err(f"Node {current} don't exist.")
|
||||
return True
|
||||
|
||||
def profile_jumphost_validation(self, answers, current):
|
||||
if current != "":
|
||||
if current not in self.app.nodes_list:
|
||||
_raise_val_err("Node {} don't exist.".format(current))
|
||||
if current != "" and not self._node_exists(current):
|
||||
_raise_val_err(f"Node {current} don't exist.")
|
||||
return True
|
||||
|
||||
def default_validation(self, answers, current):
|
||||
@@ -167,12 +236,46 @@ el.replaceWith(d);
|
||||
return True
|
||||
|
||||
def bulk_folder_validation(self, answers, current):
|
||||
if not self.app.case:
|
||||
case = getattr(self.app, "case", False)
|
||||
if not case:
|
||||
current = current.lower()
|
||||
|
||||
candidate = current
|
||||
if "/" in current:
|
||||
candidate = current.split("/")[0]
|
||||
candidate = current.split("/")[0] if "/" in current else current
|
||||
|
||||
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))
|
||||
if current != "" and len(matches) == 0:
|
||||
@@ -203,12 +306,46 @@ el.replaceWith(d);
|
||||
<span>Expand source code</span>
|
||||
</summary>
|
||||
<pre><code class="python">def bulk_folder_validation(self, answers, current):
|
||||
if not self.app.case:
|
||||
case = getattr(self.app, "case", False)
|
||||
if not case:
|
||||
current = current.lower()
|
||||
|
||||
candidate = current
|
||||
if "/" in current:
|
||||
candidate = current.split("/")[0]
|
||||
candidate = current.split("/")[0] if "/" in current else current
|
||||
|
||||
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))
|
||||
if current != "" and len(matches) == 0:
|
||||
@@ -273,6 +410,53 @@ el.replaceWith(d);
|
||||
</details>
|
||||
<div class="desc"></div>
|
||||
</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, ""):
|
||||
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</code></pre>
|
||||
</details>
|
||||
<div class="desc"></div>
|
||||
</dd>
|
||||
<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>
|
||||
</code></dt>
|
||||
@@ -302,10 +486,9 @@ el.replaceWith(d);
|
||||
<pre><code class="python">def jumphost_validation(self, answers, current):
|
||||
if current.startswith("@"):
|
||||
if current[1:] not in self.app.profiles:
|
||||
_raise_val_err("Profile {} don't exist".format(current))
|
||||
elif current != "":
|
||||
if current not in self.app.nodes_list:
|
||||
_raise_val_err("Node {} don't exist.".format(current))
|
||||
_raise_val_err(f"Profile {current} don't exist")
|
||||
elif current != "" and not self._node_exists(current):
|
||||
_raise_val_err(f"Node {current} don't exist.")
|
||||
return True</code></pre>
|
||||
</details>
|
||||
<div class="desc"></div>
|
||||
@@ -319,6 +502,12 @@ el.replaceWith(d);
|
||||
<span>Expand source code</span>
|
||||
</summary>
|
||||
<pre><code class="python">def pass_validation(self, answers, current, regex = "(^@.+$)"):
|
||||
if is_compose_expression(current):
|
||||
try:
|
||||
validate_compose_syntax(current)
|
||||
except SanitizedAuthError as exc:
|
||||
_raise_val_err(str(exc))
|
||||
return True
|
||||
profiles = current.split(",")
|
||||
for i in profiles:
|
||||
if not re.match(regex, i) or i[1:] not in self.app.profiles:
|
||||
@@ -360,9 +549,8 @@ el.replaceWith(d);
|
||||
<span>Expand source code</span>
|
||||
</summary>
|
||||
<pre><code class="python">def profile_jumphost_validation(self, answers, current):
|
||||
if current != "":
|
||||
if current not in self.app.nodes_list:
|
||||
_raise_val_err("Node {} don't exist.".format(current))
|
||||
if current != "" and not self._node_exists(current):
|
||||
_raise_val_err(f"Node {current} don't exist.")
|
||||
return True</code></pre>
|
||||
</details>
|
||||
<div class="desc"></div>
|
||||
@@ -413,13 +601,7 @@ el.replaceWith(d);
|
||||
</summary>
|
||||
<pre><code class="python">def profile_tags_validation(self, answers, current):
|
||||
if current != "":
|
||||
isdict = False
|
||||
try:
|
||||
isdict = ast.literal_eval(current)
|
||||
except Exception:
|
||||
pass
|
||||
if not isinstance (isdict, dict):
|
||||
_raise_val_err("Tags should be a python dictionary.".format(current))
|
||||
return self._validate_tags_dict(current)
|
||||
return True</code></pre>
|
||||
</details>
|
||||
<div class="desc"></div>
|
||||
@@ -455,13 +637,7 @@ el.replaceWith(d);
|
||||
if current[1:] not in self.app.profiles:
|
||||
_raise_val_err("Profile {} don't exist".format(current))
|
||||
elif current != "":
|
||||
isdict = False
|
||||
try:
|
||||
isdict = ast.literal_eval(current)
|
||||
except Exception:
|
||||
pass
|
||||
if not isinstance (isdict, dict):
|
||||
_raise_val_err("Tags should be a python dictionary.".format(current))
|
||||
return self._validate_tags_dict(current)
|
||||
return True</code></pre>
|
||||
</details>
|
||||
<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_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.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.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>
|
||||
|
||||
@@ -1671,8 +1671,9 @@ def predict_execution_results(request,
|
||||
</details>
|
||||
<div class="desc"><p>Missing associated documentation comment in .proto file.</p>
|
||||
<p>Constructor.</p>
|
||||
<h2 id="args">Args</h2>
|
||||
<dl>
|
||||
<dt>Args</dt>
|
||||
<dt>-----=</dt>
|
||||
<dt><strong><code>channel</code></strong></dt>
|
||||
<dd>A grpc.Channel.</dd>
|
||||
</dl></div>
|
||||
@@ -2371,8 +2372,9 @@ def revoke_api_token(request,
|
||||
</details>
|
||||
<div class="desc"><p>Missing associated documentation comment in .proto file.</p>
|
||||
<p>Constructor.</p>
|
||||
<h2 id="args">Args</h2>
|
||||
<dl>
|
||||
<dt>Args</dt>
|
||||
<dt>-----=</dt>
|
||||
<dt><strong><code>channel</code></strong></dt>
|
||||
<dd>A grpc.Channel.</dd>
|
||||
</dl></div>
|
||||
@@ -2980,8 +2982,9 @@ def update_setting(request,
|
||||
</details>
|
||||
<div class="desc"><p>Missing associated documentation comment in .proto file.</p>
|
||||
<p>Constructor.</p>
|
||||
<h2 id="args">Args</h2>
|
||||
<dl>
|
||||
<dt>Args</dt>
|
||||
<dt>-----=</dt>
|
||||
<dt><strong><code>channel</code></strong></dt>
|
||||
<dd>A grpc.Channel.</dd>
|
||||
</dl></div>
|
||||
@@ -3316,8 +3319,9 @@ def test_commands(request,
|
||||
</details>
|
||||
<div class="desc"><p>Missing associated documentation comment in .proto file.</p>
|
||||
<p>Constructor.</p>
|
||||
<h2 id="args">Args</h2>
|
||||
<dl>
|
||||
<dt>Args</dt>
|
||||
<dt>-----=</dt>
|
||||
<dt><strong><code>channel</code></strong></dt>
|
||||
<dd>A grpc.Channel.</dd>
|
||||
</dl></div>
|
||||
@@ -3652,8 +3656,9 @@ def set_reserved_names(request,
|
||||
</details>
|
||||
<div class="desc"><p>Missing associated documentation comment in .proto file.</p>
|
||||
<p>Constructor.</p>
|
||||
<h2 id="args">Args</h2>
|
||||
<dl>
|
||||
<dt>Args</dt>
|
||||
<dt>-----=</dt>
|
||||
<dt><strong><code>channel</code></strong></dt>
|
||||
<dd>A grpc.Channel.</dd>
|
||||
</dl></div>
|
||||
@@ -5080,8 +5085,9 @@ def validate_parent_folder(request,
|
||||
</details>
|
||||
<div class="desc"><p>Missing associated documentation comment in .proto file.</p>
|
||||
<p>Constructor.</p>
|
||||
<h2 id="args">Args</h2>
|
||||
<dl>
|
||||
<dt>Args</dt>
|
||||
<dt>-----=</dt>
|
||||
<dt><strong><code>channel</code></strong></dt>
|
||||
<dd>A grpc.Channel.</dd>
|
||||
</dl></div>
|
||||
@@ -5598,8 +5604,9 @@ def list_plugins(request,
|
||||
</details>
|
||||
<div class="desc"><p>Missing associated documentation comment in .proto file.</p>
|
||||
<p>Constructor.</p>
|
||||
<h2 id="args">Args</h2>
|
||||
<dl>
|
||||
<dt>Args</dt>
|
||||
<dt>-----=</dt>
|
||||
<dt><strong><code>channel</code></strong></dt>
|
||||
<dd>A grpc.Channel.</dd>
|
||||
</dl></div>
|
||||
@@ -6207,8 +6214,9 @@ def update_profile(request,
|
||||
</details>
|
||||
<div class="desc"><p>Missing associated documentation comment in .proto file.</p>
|
||||
<p>Constructor.</p>
|
||||
<h2 id="args">Args</h2>
|
||||
<dl>
|
||||
<dt>Args</dt>
|
||||
<dt>-----=</dt>
|
||||
<dt><strong><code>channel</code></strong></dt>
|
||||
<dd>A grpc.Channel.</dd>
|
||||
</dl></div>
|
||||
@@ -6725,8 +6733,9 @@ def stop_api(request,
|
||||
</details>
|
||||
<div class="desc"><p>Missing associated documentation comment in .proto file.</p>
|
||||
<p>Constructor.</p>
|
||||
<h2 id="args">Args</h2>
|
||||
<dl>
|
||||
<dt>Args</dt>
|
||||
<dt>-----=</dt>
|
||||
<dt><strong><code>channel</code></strong></dt>
|
||||
<dd>A grpc.Channel.</dd>
|
||||
</dl></div>
|
||||
|
||||
@@ -62,7 +62,7 @@ el.replaceWith(d);
|
||||
<dl>
|
||||
<dt id="connpy.grpc_layer.remote_plugin_pb2.IdRequest.DESCRIPTOR"><code class="name">var <span class="ident">DESCRIPTOR</span></code></dt>
|
||||
<dd>
|
||||
<div class="desc"><p>The type of the None singleton.</p></div>
|
||||
<div class="desc"></div>
|
||||
</dd>
|
||||
</dl>
|
||||
</dd>
|
||||
@@ -81,7 +81,7 @@ el.replaceWith(d);
|
||||
<dl>
|
||||
<dt id="connpy.grpc_layer.remote_plugin_pb2.OutputChunk.DESCRIPTOR"><code class="name">var <span class="ident">DESCRIPTOR</span></code></dt>
|
||||
<dd>
|
||||
<div class="desc"><p>The type of the None singleton.</p></div>
|
||||
<div class="desc"></div>
|
||||
</dd>
|
||||
</dl>
|
||||
</dd>
|
||||
@@ -100,7 +100,7 @@ el.replaceWith(d);
|
||||
<dl>
|
||||
<dt id="connpy.grpc_layer.remote_plugin_pb2.PluginInvokeRequest.DESCRIPTOR"><code class="name">var <span class="ident">DESCRIPTOR</span></code></dt>
|
||||
<dd>
|
||||
<div class="desc"><p>The type of the None singleton.</p></div>
|
||||
<div class="desc"></div>
|
||||
</dd>
|
||||
</dl>
|
||||
</dd>
|
||||
@@ -119,7 +119,7 @@ el.replaceWith(d);
|
||||
<dl>
|
||||
<dt id="connpy.grpc_layer.remote_plugin_pb2.StringResponse.DESCRIPTOR"><code class="name">var <span class="ident">DESCRIPTOR</span></code></dt>
|
||||
<dd>
|
||||
<div class="desc"><p>The type of the None singleton.</p></div>
|
||||
<div class="desc"></div>
|
||||
</dd>
|
||||
</dl>
|
||||
</dd>
|
||||
|
||||
@@ -317,8 +317,9 @@ def invoke_plugin(request,
|
||||
</details>
|
||||
<div class="desc"><p>Missing associated documentation comment in .proto file.</p>
|
||||
<p>Constructor.</p>
|
||||
<h2 id="args">Args</h2>
|
||||
<dl>
|
||||
<dt>Args</dt>
|
||||
<dt>-----=</dt>
|
||||
<dt><strong><code>channel</code></strong></dt>
|
||||
<dd>A grpc.Channel.</dd>
|
||||
</dl></div>
|
||||
|
||||
@@ -55,6 +55,7 @@ el.replaceWith(d);
|
||||
<pre><code class="python">def handle_errors(func):
|
||||
import inspect
|
||||
if inspect.isgeneratorfunction(func):
|
||||
@functools.wraps(func)
|
||||
def wrapper(*args, **kwargs):
|
||||
try:
|
||||
for item in func(*args, **kwargs):
|
||||
@@ -75,6 +76,7 @@ el.replaceWith(d);
|
||||
printer.clear_thread_state()
|
||||
return wrapper
|
||||
else:
|
||||
@functools.wraps(func)
|
||||
def wrapper(*args, **kwargs):
|
||||
try:
|
||||
return func(*args, **kwargs)
|
||||
@@ -108,7 +110,7 @@ el.replaceWith(d);
|
||||
from connpy.grpc_layer.user_registry import UserRegistry
|
||||
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)
|
||||
|
||||
# Check if trusted_gateway provider is configured if SSO Gateway Secret is present in env
|
||||
@@ -161,9 +163,7 @@ el.replaceWith(d);
|
||||
</summary>
|
||||
<pre><code class="python">class AIServicer(connpy_pb2_grpc.AIServiceServicer):
|
||||
def __init__(self, provider, registry=None, debug=False):
|
||||
if not hasattr(provider, "mode"):
|
||||
from connpy.services.provider import ServiceProvider
|
||||
provider = ServiceProvider(provider, mode="local")
|
||||
provider = _ensure_server_provider(provider)
|
||||
self._fallback_provider = provider
|
||||
self._registry = registry
|
||||
self.server_debug = debug
|
||||
@@ -604,7 +604,7 @@ def service(self):
|
||||
<dl>
|
||||
<dt id="connpy.grpc_layer.server.AuthInterceptor.OPEN_METHODS"><code class="name">var <span class="ident">OPEN_METHODS</span></code></dt>
|
||||
<dd>
|
||||
<div class="desc"><p>The type of the None singleton.</p></div>
|
||||
<div class="desc"></div>
|
||||
</dd>
|
||||
</dl>
|
||||
<h3>Methods</h3>
|
||||
@@ -648,8 +648,9 @@ contextvars.Context, and the same Context is used for downstream
|
||||
interceptors and for the final handler call. Note that there are no
|
||||
guarantees that interceptors and handlers will be called from the
|
||||
same thread.</p>
|
||||
<h2 id="args">Args</h2>
|
||||
<dl>
|
||||
<dt>Args</dt>
|
||||
<dt>-----=</dt>
|
||||
<dt><strong><code>continuation</code></strong></dt>
|
||||
<dd>A function that takes a HandlerCallDetails and
|
||||
proceeds to invoke the next interceptor in the chain, if any,
|
||||
@@ -659,8 +660,9 @@ the RPC is considered serviced, or None otherwise.</dd>
|
||||
<dt><strong><code>handler_call_details</code></strong></dt>
|
||||
<dd>A HandlerCallDetails describing the RPC.</dd>
|
||||
</dl>
|
||||
<h2 id="returns">Returns</h2>
|
||||
<p>An RpcMethodHandler with which the RPC may be serviced if the
|
||||
<p>Returns
|
||||
-----=
|
||||
An RpcMethodHandler with which the RPC may be serviced if the
|
||||
interceptor chooses to service this RPC, or None otherwise.</p></div>
|
||||
</dd>
|
||||
</dl>
|
||||
@@ -923,9 +925,7 @@ interceptor chooses to service this RPC, or None otherwise.</p></div>
|
||||
</summary>
|
||||
<pre><code class="python">class ConfigServicer(connpy_pb2_grpc.ConfigServiceServicer):
|
||||
def __init__(self, provider, registry=None):
|
||||
if not hasattr(provider, "mode"):
|
||||
from connpy.services.provider import ServiceProvider
|
||||
provider = ServiceProvider(provider, mode="local")
|
||||
provider = _ensure_server_provider(provider)
|
||||
self._fallback_provider = provider
|
||||
self._registry = registry
|
||||
|
||||
@@ -1011,9 +1011,7 @@ def service(self):
|
||||
</summary>
|
||||
<pre><code class="python">class ExecutionServicer(connpy_pb2_grpc.ExecutionServiceServicer):
|
||||
def __init__(self, provider, registry=None):
|
||||
if not hasattr(provider, "mode"):
|
||||
from connpy.services.provider import ServiceProvider
|
||||
provider = ServiceProvider(provider, mode="local")
|
||||
provider = _ensure_server_provider(provider)
|
||||
self._fallback_provider = provider
|
||||
self._registry = registry
|
||||
|
||||
@@ -1182,9 +1180,7 @@ def service(self):
|
||||
</summary>
|
||||
<pre><code class="python">class ImportExportServicer(connpy_pb2_grpc.ImportExportServiceServicer):
|
||||
def __init__(self, provider, registry=None):
|
||||
if not hasattr(provider, "mode"):
|
||||
from connpy.services.provider import ServiceProvider
|
||||
provider = ServiceProvider(provider, mode="local")
|
||||
provider = _ensure_server_provider(provider)
|
||||
self._fallback_provider = provider
|
||||
self._registry = registry
|
||||
|
||||
@@ -1213,7 +1209,7 @@ def service(self):
|
||||
if request.value.startswith("---YAML---\n"):
|
||||
import yaml
|
||||
content = request.value[len("---YAML---\n"):]
|
||||
data = yaml.load(content, Loader=yaml.FullLoader)
|
||||
data = yaml.safe_load(content)
|
||||
self.service.import_from_dict(data)
|
||||
else:
|
||||
self.service.import_from_file(request.value)
|
||||
@@ -1340,8 +1336,9 @@ contextvars.Context, and the same Context is used for downstream
|
||||
interceptors and for the final handler call. Note that there are no
|
||||
guarantees that interceptors and handlers will be called from the
|
||||
same thread.</p>
|
||||
<h2 id="args">Args</h2>
|
||||
<dl>
|
||||
<dt>Args</dt>
|
||||
<dt>-----=</dt>
|
||||
<dt><strong><code>continuation</code></strong></dt>
|
||||
<dd>A function that takes a HandlerCallDetails and
|
||||
proceeds to invoke the next interceptor in the chain, if any,
|
||||
@@ -1351,8 +1348,9 @@ the RPC is considered serviced, or None otherwise.</dd>
|
||||
<dt><strong><code>handler_call_details</code></strong></dt>
|
||||
<dd>A HandlerCallDetails describing the RPC.</dd>
|
||||
</dl>
|
||||
<h2 id="returns">Returns</h2>
|
||||
<p>An RpcMethodHandler with which the RPC may be serviced if the
|
||||
<p>Returns
|
||||
-----=
|
||||
An RpcMethodHandler with which the RPC may be serviced if the
|
||||
interceptor chooses to service this RPC, or None otherwise.</p></div>
|
||||
</dd>
|
||||
</dl>
|
||||
@@ -1368,9 +1366,7 @@ interceptor chooses to service this RPC, or None otherwise.</p></div>
|
||||
</summary>
|
||||
<pre><code class="python">class NodeServicer(connpy_pb2_grpc.NodeServiceServicer):
|
||||
def __init__(self, provider, registry=None, debug=False):
|
||||
if not hasattr(provider, "mode"):
|
||||
from connpy.services.provider import ServiceProvider
|
||||
provider = ServiceProvider(provider, mode="local")
|
||||
provider = _ensure_server_provider(provider)
|
||||
self._fallback_provider = provider
|
||||
self._registry = registry
|
||||
self.server_debug = debug
|
||||
@@ -1421,8 +1417,24 @@ interceptor chooses to service this RPC, or None otherwise.</p></div>
|
||||
self.server_console.print(f"[debug][DEBUG][/debug] gRPC interact_node request for: [bold cyan]{unique_id}[/bold cyan]")
|
||||
|
||||
if first_req.connection_params_json:
|
||||
import json
|
||||
params = json.loads(first_req.connection_params_json)
|
||||
try:
|
||||
params = validate_json_depth_and_size(
|
||||
first_req.connection_params_json,
|
||||
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||
)
|
||||
if not isinstance(params, dict):
|
||||
raise TypeError("connection_params_json must be a JSON object")
|
||||
if "fallback" in params and params["fallback"] not in (None, "", []):
|
||||
validate_fallback_entries(
|
||||
params["fallback"],
|
||||
max_entries=MAX_FALLBACK_ENTRIES,
|
||||
)
|
||||
if "tags" in params and params["tags"] not in ("", None):
|
||||
validate_auth_budget_tags(params["tags"])
|
||||
except (ValueError, TypeError, SanitizedAuthError) as exc:
|
||||
context.abort(grpc.StatusCode.INVALID_ARGUMENT, str(exc))
|
||||
|
||||
base_node_id = params.get("base_node")
|
||||
# Valid attributes that a node object accepts
|
||||
valid_attrs = ['host', 'options', 'logs', 'password', 'port', 'protocol', 'user', 'jumphost']
|
||||
@@ -1440,6 +1452,8 @@ interceptor chooses to service this RPC, or None otherwise.</p></div>
|
||||
for attr in valid_attrs:
|
||||
if attr in params:
|
||||
device[attr] = params[attr]
|
||||
if "fallback" in params:
|
||||
device["fallback"] = params["fallback"]
|
||||
|
||||
if "tags" in params:
|
||||
device_tags = device.get("tags", {})
|
||||
@@ -1453,20 +1467,30 @@ interceptor chooses to service this RPC, or None otherwise.</p></div>
|
||||
else:
|
||||
# base_node not found, fall back to dynamic
|
||||
node_name = params.get("name", fallback_id)
|
||||
n = node(node_name, host=params.get("host", ""), config=user_config)
|
||||
for attr in valid_attrs:
|
||||
if attr in params:
|
||||
setattr(n, attr, params[attr])
|
||||
dyn_kwargs = {k: params[k] for k in valid_attrs if k in params and k != "host"}
|
||||
if "fallback" in params:
|
||||
dyn_kwargs["fallback"] = params["fallback"]
|
||||
if "tags" in params:
|
||||
n.tags = params["tags"]
|
||||
dyn_kwargs["tags"] = params["tags"]
|
||||
n = node(
|
||||
node_name,
|
||||
host=params.get("host", ""),
|
||||
config=user_config,
|
||||
**dyn_kwargs,
|
||||
)
|
||||
else:
|
||||
node_name = params.get("name", fallback_id)
|
||||
n = node(node_name, host=params.get("host", ""), config=user_config)
|
||||
for attr in valid_attrs:
|
||||
if attr in params:
|
||||
setattr(n, attr, params[attr])
|
||||
dyn_kwargs = {k: params[k] for k in valid_attrs if k in params and k != "host"}
|
||||
if "fallback" in params:
|
||||
dyn_kwargs["fallback"] = params["fallback"]
|
||||
if "tags" in params:
|
||||
n.tags = params["tags"]
|
||||
dyn_kwargs["tags"] = params["tags"]
|
||||
n = node(
|
||||
node_name,
|
||||
host=params.get("host", ""),
|
||||
config=user_config,
|
||||
**dyn_kwargs,
|
||||
)
|
||||
n._orig_protocol = n.protocol
|
||||
if sftp:
|
||||
n.protocol = "sftp"
|
||||
@@ -1487,9 +1511,11 @@ interceptor chooses to service this RPC, or None otherwise.</p></div>
|
||||
debug_chunks = []
|
||||
if debug:
|
||||
from io import StringIO
|
||||
|
||||
from rich.console import Console as RichConsole
|
||||
from ..printer import connpy_theme
|
||||
|
||||
from .. import printer as _printer
|
||||
from ..printer import connpy_theme
|
||||
|
||||
def remote_logger(msg_type, message):
|
||||
buf = StringIO()
|
||||
@@ -1510,7 +1536,14 @@ interceptor chooses to service this RPC, or None otherwise.</p></div>
|
||||
else:
|
||||
remote_logger = None
|
||||
|
||||
connect = n._connect(debug=debug, logger=remote_logger)
|
||||
try:
|
||||
connect = n._connect(
|
||||
debug=debug,
|
||||
logger=remote_logger,
|
||||
attempt_tracker=getattr(provider, "auth_tracker", None),
|
||||
)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
connect = str(sanitize_exception(exc, code="AUTH_FAILED"))
|
||||
|
||||
# Send debug output to client before checking result (always show the command)
|
||||
for chunk in debug_chunks:
|
||||
@@ -1841,10 +1874,9 @@ interceptor chooses to service this RPC, or None otherwise.</p></div>
|
||||
continue
|
||||
|
||||
asyncio.run(n._async_interact_loop(remote_stream, resize_callback, copilot_handler=remote_copilot_handler))
|
||||
except Exception as e:
|
||||
import traceback
|
||||
print(f"[ERROR in run_async_loop] {e}")
|
||||
traceback.print_exc()
|
||||
except Exception as e: # noqa: BLE001
|
||||
sanitized_err = sanitize_exception(e, code="TRANSPORT_ERROR", context_label="interact stream")
|
||||
print(f"[ERROR in run_async_loop] {sanitized_err}")
|
||||
finally:
|
||||
n._teardown_interact_environment()
|
||||
response_queue.put(None) # Signal EOF
|
||||
@@ -1999,9 +2031,7 @@ def service(self):
|
||||
</summary>
|
||||
<pre><code class="python">class PluginServicer(connpy_pb2_grpc.PluginServiceServicer, remote_plugin_pb2_grpc.RemotePluginServiceServicer):
|
||||
def __init__(self, provider, registry=None):
|
||||
if not hasattr(provider, "mode"):
|
||||
from connpy.services.provider import ServiceProvider
|
||||
provider = ServiceProvider(provider, mode="local")
|
||||
provider = _ensure_server_provider(provider)
|
||||
self._fallback_provider = provider
|
||||
self._registry = registry
|
||||
|
||||
@@ -2106,9 +2136,7 @@ def service(self):
|
||||
</summary>
|
||||
<pre><code class="python">class ProfileServicer(connpy_pb2_grpc.ProfileServiceServicer):
|
||||
def __init__(self, provider, registry=None):
|
||||
if not hasattr(provider, "mode"):
|
||||
from connpy.services.provider import ServiceProvider
|
||||
provider = ServiceProvider(provider, mode="local")
|
||||
provider = _ensure_server_provider(provider)
|
||||
self._fallback_provider = provider
|
||||
self._registry = registry
|
||||
|
||||
@@ -2409,9 +2437,7 @@ def service(self):
|
||||
</summary>
|
||||
<pre><code class="python">class SystemServicer(connpy_pb2_grpc.SystemServiceServicer):
|
||||
def __init__(self, provider, registry=None):
|
||||
if not hasattr(provider, "mode"):
|
||||
from connpy.services.provider import ServiceProvider
|
||||
provider = ServiceProvider(provider, mode="local")
|
||||
provider = _ensure_server_provider(provider)
|
||||
self._fallback_provider = provider
|
||||
self._registry = registry
|
||||
|
||||
|
||||
@@ -687,8 +687,9 @@ def predict_execution_results(self, target_nodes, commands, status=None, chunk_c
|
||||
return continuation(new_details, request_iterator)</code></pre>
|
||||
</details>
|
||||
<div class="desc"><p>Intercepts a stream-stream invocation.</p>
|
||||
<h2 id="args">Args</h2>
|
||||
<dl>
|
||||
<dt>Args</dt>
|
||||
<dt>-----=</dt>
|
||||
<dt><strong><code>continuation</code></strong></dt>
|
||||
<dd>A function that proceeds with the invocation by
|
||||
executing the next interceptor in chain or invoking the
|
||||
@@ -707,8 +708,9 @@ outgoing RPC.</dd>
|
||||
<dt><strong><code>request_iterator</code></strong></dt>
|
||||
<dd>An iterator that yields request values for the RPC.</dd>
|
||||
</dl>
|
||||
<h2 id="returns">Returns</h2>
|
||||
<p>An object that is both a Call for the RPC and an iterator of
|
||||
<p>Returns
|
||||
-----=
|
||||
An object that is both a Call for the RPC and an iterator of
|
||||
response values. Drawing response values from the returned
|
||||
Call-iterator may raise RpcError indicating termination of
|
||||
the RPC with non-OK status. This object <em>should</em> also fulfill the
|
||||
@@ -727,8 +729,9 @@ Future interface, though it may not.</p></div>
|
||||
return continuation(new_details, request_iterator)</code></pre>
|
||||
</details>
|
||||
<div class="desc"><p>Intercepts a stream-unary invocation asynchronously.</p>
|
||||
<h2 id="args">Args</h2>
|
||||
<dl>
|
||||
<dt>Args</dt>
|
||||
<dt>-----=</dt>
|
||||
<dt><strong><code>continuation</code></strong></dt>
|
||||
<dd>A function that proceeds with the invocation by
|
||||
executing the next interceptor in chain or invoking the
|
||||
@@ -747,8 +750,9 @@ outgoing RPC.</dd>
|
||||
<dt><strong><code>request_iterator</code></strong></dt>
|
||||
<dd>An iterator that yields request values for the RPC.</dd>
|
||||
</dl>
|
||||
<h2 id="returns">Returns</h2>
|
||||
<p>An object that is both a Call for the RPC and a Future.
|
||||
<p>Returns
|
||||
-----=
|
||||
An object that is both a Call for the RPC and a Future.
|
||||
In the event of RPC completion, the return Call-Future's
|
||||
result value will be the response message of the RPC.
|
||||
Should the event terminate with non-OK status, the returned
|
||||
@@ -767,8 +771,9 @@ Call-Future's exception value will be an RpcError.</p></div>
|
||||
return continuation(new_details, request)</code></pre>
|
||||
</details>
|
||||
<div class="desc"><p>Intercepts a unary-stream invocation.</p>
|
||||
<h2 id="args">Args</h2>
|
||||
<dl>
|
||||
<dt>Args</dt>
|
||||
<dt>-----=</dt>
|
||||
<dt><strong><code>continuation</code></strong></dt>
|
||||
<dd>A function that proceeds with the invocation by
|
||||
executing the next interceptor in chain or invoking the
|
||||
@@ -787,8 +792,9 @@ outgoing RPC.</dd>
|
||||
<dt><strong><code>request</code></strong></dt>
|
||||
<dd>The request value for the RPC.</dd>
|
||||
</dl>
|
||||
<h2 id="returns">Returns</h2>
|
||||
<p>An object that is both a Call for the RPC and an iterator of
|
||||
<p>Returns
|
||||
-----=
|
||||
An object that is both a Call for the RPC and an iterator of
|
||||
response values. Drawing response values from the returned
|
||||
Call-iterator may raise RpcError indicating termination of
|
||||
the RPC with non-OK status. This object <em>should</em> also fulfill the
|
||||
@@ -807,8 +813,9 @@ Future interface, though it may not.</p></div>
|
||||
return continuation(new_details, request)</code></pre>
|
||||
</details>
|
||||
<div class="desc"><p>Intercepts a unary-unary invocation asynchronously.</p>
|
||||
<h2 id="args">Args</h2>
|
||||
<dl>
|
||||
<dt>Args</dt>
|
||||
<dt>-----=</dt>
|
||||
<dt><strong><code>continuation</code></strong></dt>
|
||||
<dd>A function that proceeds with the invocation by
|
||||
executing the next interceptor in chain or invoking the
|
||||
@@ -828,8 +835,9 @@ outgoing RPC.</dd>
|
||||
<dt><strong><code>request</code></strong></dt>
|
||||
<dd>The request value for the RPC.</dd>
|
||||
</dl>
|
||||
<h2 id="returns">Returns</h2>
|
||||
<p>An object that is both a Call for the RPC and a Future.
|
||||
<p>Returns
|
||||
-----=
|
||||
An object that is both a Call for the RPC and a Future.
|
||||
In the event of RPC completion, the return Call-Future's
|
||||
result value will be the response message of the RPC.
|
||||
Should the event terminate with non-OK status, the returned
|
||||
@@ -1107,7 +1115,7 @@ def update_setting(self, key, value):
|
||||
</dd>
|
||||
<dt id="connpy.grpc_layer.stubs.ExecutionStub"><code class="flex name class">
|
||||
<span>class <span class="ident">ExecutionStub</span></span>
|
||||
<span>(</span><span>channel, remote_host)</span>
|
||||
<span>(</span><span>channel, remote_host, config=None, node_stub=None)</span>
|
||||
</code></dt>
|
||||
<dd>
|
||||
<details class="source">
|
||||
@@ -1115,13 +1123,58 @@ def update_setting(self, key, value):
|
||||
<span>Expand source code</span>
|
||||
</summary>
|
||||
<pre><code class="python">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.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
|
||||
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(
|
||||
nodes=nodes_list,
|
||||
commands=commands,
|
||||
@@ -1134,9 +1187,6 @@ def update_setting(self, key, value):
|
||||
if variables is not None:
|
||||
req.vars.CopyFrom(to_struct(variables))
|
||||
|
||||
final_results = {}
|
||||
on_complete = kwargs.get("on_node_complete")
|
||||
|
||||
for response in self.stub.run_commands(req):
|
||||
if on_complete:
|
||||
on_complete(response.unique_id, response.output, response.status)
|
||||
@@ -1149,7 +1199,11 @@ def update_setting(self, key, value):
|
||||
|
||||
@handle_errors
|
||||
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(
|
||||
nodes=nodes_list,
|
||||
commands=commands,
|
||||
@@ -1163,9 +1217,6 @@ def update_setting(self, key, value):
|
||||
if variables is not None:
|
||||
req.vars.CopyFrom(to_struct(variables))
|
||||
|
||||
final_results = {}
|
||||
on_complete = kwargs.get("on_node_complete")
|
||||
|
||||
for response in self.stub.test_commands(req):
|
||||
result_dict = from_struct(response.test_result) if response.HasField("test_result") else {}
|
||||
if on_complete:
|
||||
@@ -1176,7 +1227,21 @@ def update_setting(self, key, value):
|
||||
|
||||
@handle_errors
|
||||
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)</code></pre>
|
||||
</details>
|
||||
<div class="desc"></div>
|
||||
@@ -1192,7 +1257,21 @@ def update_setting(self, key, value):
|
||||
</summary>
|
||||
<pre><code class="python">@handle_errors
|
||||
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)</code></pre>
|
||||
</details>
|
||||
<div class="desc"></div>
|
||||
@@ -1207,7 +1286,11 @@ def run_cli_script(self, nodes_filter, script_path, parallel=10):
|
||||
</summary>
|
||||
<pre><code class="python">@handle_errors
|
||||
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(
|
||||
nodes=nodes_list,
|
||||
commands=commands,
|
||||
@@ -1220,9 +1303,6 @@ def run_commands(self, nodes_filter, commands, variables=None, parallel=10, time
|
||||
if variables is not None:
|
||||
req.vars.CopyFrom(to_struct(variables))
|
||||
|
||||
final_results = {}
|
||||
on_complete = kwargs.get("on_node_complete")
|
||||
|
||||
for response in self.stub.run_commands(req):
|
||||
if on_complete:
|
||||
on_complete(response.unique_id, response.output, response.status)
|
||||
@@ -1245,7 +1325,11 @@ def run_commands(self, nodes_filter, commands, variables=None, parallel=10, time
|
||||
</summary>
|
||||
<pre><code class="python">@handle_errors
|
||||
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(
|
||||
nodes=nodes_list,
|
||||
commands=commands,
|
||||
@@ -1259,9 +1343,6 @@ def test_commands(self, nodes_filter, commands, expected, variables=None, parall
|
||||
if variables is not None:
|
||||
req.vars.CopyFrom(to_struct(variables))
|
||||
|
||||
final_results = {}
|
||||
on_complete = kwargs.get("on_node_complete")
|
||||
|
||||
for response in self.stub.test_commands(req):
|
||||
result_dict = from_struct(response.test_result) if response.HasField("test_result") else {}
|
||||
if on_complete:
|
||||
@@ -1372,8 +1453,48 @@ def set_reserved_names(self, names):
|
||||
self.remote_host = remote_host
|
||||
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):
|
||||
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 . import connpy_pb2
|
||||
|
||||
@@ -1467,6 +1588,7 @@ def set_reserved_names(self, names):
|
||||
|
||||
@handle_errors
|
||||
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 select
|
||||
import tty
|
||||
@@ -1758,16 +1880,26 @@ def set_reserved_names(self, names):
|
||||
@handle_errors
|
||||
def list_nodes(self, filter_str=None, format_str=None):
|
||||
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
|
||||
@handle_errors
|
||||
def list_folders(self, filter_str=None):
|
||||
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
|
||||
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)
|
||||
|
||||
@handle_errors
|
||||
@@ -1775,7 +1907,11 @@ def set_reserved_names(self, names):
|
||||
return from_value(self.stub.explode_unique(connpy_pb2.IdRequest(id=unique_id)).data)
|
||||
|
||||
@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))
|
||||
|
||||
@handle_errors
|
||||
@@ -1792,7 +1928,7 @@ def set_reserved_names(self, names):
|
||||
# node cache is what matters for fzf. We'll fetch profiles if we can.
|
||||
# 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)
|
||||
|
||||
def _trigger_local_cache_sync(self):
|
||||
@@ -1807,12 +1943,16 @@ def set_reserved_names(self, names):
|
||||
|
||||
@handle_errors
|
||||
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)
|
||||
self.stub.add_node(req)
|
||||
self._trigger_local_cache_sync()
|
||||
|
||||
@handle_errors
|
||||
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)
|
||||
self.stub.update_node(req)
|
||||
if save:
|
||||
@@ -1820,6 +1960,7 @@ def set_reserved_names(self, names):
|
||||
|
||||
@handle_errors
|
||||
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)
|
||||
self.stub.delete_node(req)
|
||||
if save:
|
||||
@@ -1827,12 +1968,22 @@ def set_reserved_names(self, names):
|
||||
|
||||
@handle_errors
|
||||
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)
|
||||
self.stub.move_node(req)
|
||||
self._trigger_local_cache_sync()
|
||||
|
||||
@handle_errors
|
||||
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))
|
||||
self.stub.bulk_add(req)
|
||||
self._trigger_local_cache_sync()
|
||||
@@ -1872,6 +2023,9 @@ def set_reserved_names(self, names):
|
||||
</summary>
|
||||
<pre><code class="python">@handle_errors
|
||||
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)
|
||||
self.stub.add_node(req)
|
||||
self._trigger_local_cache_sync()</code></pre>
|
||||
@@ -1888,6 +2042,10 @@ def add_node(self, unique_id, data, is_folder=False):
|
||||
</summary>
|
||||
<pre><code class="python">@handle_errors
|
||||
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))
|
||||
self.stub.bulk_add(req)
|
||||
self._trigger_local_cache_sync()</code></pre>
|
||||
@@ -2059,6 +2217,7 @@ def connect_dynamic(self, connection_params, debug=False):
|
||||
</summary>
|
||||
<pre><code class="python">@handle_errors
|
||||
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 select
|
||||
import tty
|
||||
@@ -2213,6 +2372,7 @@ def connect_node(self, unique_id, sftp=False, debug=False, logger=None):
|
||||
</summary>
|
||||
<pre><code class="python">@handle_errors
|
||||
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)
|
||||
self.stub.delete_node(req)
|
||||
if save:
|
||||
@@ -2275,7 +2435,7 @@ def generate_cache(self, nodes=None, folders=None, profiles=None):
|
||||
# node cache is what matters for fzf. We'll fetch profiles if we can.
|
||||
# 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)</code></pre>
|
||||
</details>
|
||||
<div class="desc"></div>
|
||||
@@ -2308,6 +2468,7 @@ def get_inventory(self):
|
||||
</summary>
|
||||
<pre><code class="python">@handle_errors
|
||||
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)</code></pre>
|
||||
</details>
|
||||
<div class="desc"></div>
|
||||
@@ -2324,7 +2485,11 @@ def get_node_details(self, unique_id):
|
||||
@handle_errors
|
||||
def list_folders(self, filter_str=None):
|
||||
req = connpy_pb2.FilterRequest(filter_str=filter_str or "")
|
||||
return from_value(self.stub.list_folders(req).data) or []</code></pre>
|
||||
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</code></pre>
|
||||
</details>
|
||||
<div class="desc"></div>
|
||||
</dd>
|
||||
@@ -2340,7 +2505,12 @@ def list_folders(self, filter_str=None):
|
||||
@handle_errors
|
||||
def list_nodes(self, filter_str=None, format_str=None):
|
||||
req = connpy_pb2.FilterRequest(filter_str=filter_str or "", format_str=format_str or "")
|
||||
return from_value(self.stub.list_nodes(req).data) or []</code></pre>
|
||||
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</code></pre>
|
||||
</details>
|
||||
<div class="desc"></div>
|
||||
</dd>
|
||||
@@ -2354,6 +2524,12 @@ def list_nodes(self, filter_str=None, format_str=None):
|
||||
</summary>
|
||||
<pre><code class="python">@handle_errors
|
||||
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)
|
||||
self.stub.move_node(req)
|
||||
self._trigger_local_cache_sync()</code></pre>
|
||||
@@ -2385,6 +2561,7 @@ def set_reserved_names(self, names):
|
||||
</summary>
|
||||
<pre><code class="python">@handle_errors
|
||||
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)
|
||||
self.stub.update_node(req)
|
||||
if save:
|
||||
@@ -2393,7 +2570,7 @@ def update_node(self, unique_id, data, save=True):
|
||||
<div class="desc"></div>
|
||||
</dd>
|
||||
<dt id="connpy.grpc_layer.stubs.NodeStub.validate_parent_folder"><code class="name flex">
|
||||
<span>def <span class="ident">validate_parent_folder</span></span>(<span>self, unique_id)</span>
|
||||
<span>def <span class="ident">validate_parent_folder</span></span>(<span>self, unique_id, is_folder=False)</span>
|
||||
</code></dt>
|
||||
<dd>
|
||||
<details class="source">
|
||||
@@ -2401,7 +2578,11 @@ def update_node(self, unique_id, data, save=True):
|
||||
<span>Expand source code</span>
|
||||
</summary>
|
||||
<pre><code class="python">@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))</code></pre>
|
||||
</details>
|
||||
<div class="desc"></div>
|
||||
|
||||
@@ -113,7 +113,7 @@ el.replaceWith(d);
|
||||
try:
|
||||
# Attempt a fresh configuration load
|
||||
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
|
||||
if old_provider:
|
||||
@@ -221,7 +221,7 @@ el.replaceWith(d);
|
||||
try:
|
||||
# Attempt a fresh configuration load
|
||||
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
|
||||
if old_provider:
|
||||
|
||||
+3063
-675
File diff suppressed because it is too large.
Load diff
+148
-59
@@ -58,29 +58,50 @@ el.replaceWith(d);
|
||||
<pre><code class="python">class MCPClientManager:
|
||||
"""Manages MCP SSE client connections for connpy."""
|
||||
|
||||
_instance = None
|
||||
_instances: ClassVar[dict[Any, "MCPClientManager"]] = {}
|
||||
_instance: ClassVar["MCPClientManager | None"] = None
|
||||
_lock = threading.Lock()
|
||||
|
||||
def __new__(cls, *args, **kwargs):
|
||||
@classmethod
|
||||
def _resolve_scope_key(cls, config=None, *args, **kwargs) -> Any:
|
||||
cfg = config
|
||||
if cfg is None and "config" in kwargs:
|
||||
cfg = kwargs["config"]
|
||||
if cfg is not None:
|
||||
defaultdir = getattr(cfg, "defaultdir", None)
|
||||
if defaultdir:
|
||||
return os.path.abspath(str(defaultdir))
|
||||
return id(cfg)
|
||||
return "default"
|
||||
|
||||
def __new__(cls, config=None, *args, **kwargs):
|
||||
key = cls._resolve_scope_key(config, *args, **kwargs)
|
||||
with cls._lock:
|
||||
if cls._instance is None:
|
||||
cls._instance = super(MCPClientManager, cls).__new__(cls)
|
||||
cls._instance._initialized = False
|
||||
return cls._instance
|
||||
if cls._instance is None and cls._instances:
|
||||
cls._instances.clear()
|
||||
if key not in cls._instances:
|
||||
instance = super().__new__(cls)
|
||||
instance._initialized = False
|
||||
instance._scope_key = key
|
||||
cls._instances[key] = instance
|
||||
cls._instance = cls._instances[key]
|
||||
return cls._instances[key]
|
||||
|
||||
def __init__(self, config=None):
|
||||
if self._initialized:
|
||||
return
|
||||
if config is not None:
|
||||
self.config = config
|
||||
self.sessions: Dict[str, Dict[str, Any]] = {} # name -> {session, stack}
|
||||
self.tool_cache: Dict[str, List[Dict[str, Any]]] = {}
|
||||
self._connecting: Dict[str, asyncio.Future] = {}
|
||||
if getattr(self, "_initialized", False):
|
||||
return
|
||||
if not hasattr(self, "config"):
|
||||
self.config = config
|
||||
self.sessions: dict[str, dict[str, Any]] = {} # name -> {session, stack}
|
||||
self.tool_cache: dict[str, list[dict[str, Any]]] = {}
|
||||
self._server_configs: dict[str, dict[str, Any]] = {}
|
||||
self._connecting: dict[str, asyncio.Future] = {}
|
||||
self._initialized = True
|
||||
|
||||
async def get_tools_for_llm(self, os_filter: Optional[str] = None) -> List[Dict[str, Any]]:
|
||||
"""
|
||||
Fetches tools from enabled MCP servers that match the OS filter.
|
||||
"""
|
||||
async def get_tools_for_llm(self, os_filter: str | None = None) -> list[dict[str, Any]]:
|
||||
"""Fetches tools from enabled MCP servers that match the OS filter."""
|
||||
if not MCP_AVAILABLE:
|
||||
return []
|
||||
|
||||
@@ -90,29 +111,53 @@ el.replaceWith(d);
|
||||
mcp_config = self.config.get_effective_setting("ai", {}).get("mcp_servers", {})
|
||||
else:
|
||||
mcp_config = self.config.config.get("ai", {}).get("mcp_servers", {}) if hasattr(self.config, "config") else {}
|
||||
except Exception:
|
||||
except Exception: # noqa: BLE001
|
||||
return []
|
||||
|
||||
if not isinstance(mcp_config, dict):
|
||||
mcp_config = {}
|
||||
|
||||
# Invalidate cache and reconnect if server configuration changed or server was removed
|
||||
current_names = set(mcp_config.keys())
|
||||
for old_name in list(self._server_configs.keys()):
|
||||
if old_name not in current_names:
|
||||
self.tool_cache.pop(old_name, None)
|
||||
old_data = self.sessions.pop(old_name, None)
|
||||
if old_data and old_data.get("stack"):
|
||||
with suppress(Exception):
|
||||
await old_data["stack"].aclose()
|
||||
self._server_configs.pop(old_name, None)
|
||||
|
||||
for name, cfg in mcp_config.items():
|
||||
cached_cfg = self._server_configs.get(name)
|
||||
if cached_cfg is not None and cached_cfg != cfg:
|
||||
self.tool_cache.pop(name, None)
|
||||
old_data = self.sessions.pop(name, None)
|
||||
if old_data and old_data.get("stack"):
|
||||
with suppress(Exception):
|
||||
await old_data["stack"].aclose()
|
||||
self._server_configs[name] = dict(cfg) if isinstance(cfg, dict) else cfg
|
||||
|
||||
async def _fetch(name, cfg):
|
||||
if not cfg.get("enabled", True): return []
|
||||
if not isinstance(cfg, dict) or not cfg.get("enabled", True):
|
||||
return []
|
||||
|
||||
# Filter by OS if specified in config (primarily used for copilot strict matching)
|
||||
auto_os = cfg.get("auto_load_on_os")
|
||||
if os_filter is not None and auto_os and os_filter.lower() != auto_os.lower():
|
||||
return []
|
||||
|
||||
try:
|
||||
with suppress(Exception):
|
||||
session = await self._ensure_connected(name, cfg)
|
||||
if session:
|
||||
if name in self.tool_cache: return self.tool_cache[name]
|
||||
if name in self.tool_cache:
|
||||
return self.tool_cache[name]
|
||||
llm_tools = await self._fetch_tools_as_openai(name, session)
|
||||
self.tool_cache[name] = llm_tools
|
||||
return llm_tools
|
||||
except Exception:
|
||||
pass
|
||||
return []
|
||||
|
||||
tasks = [ _fetch(name, cfg) for name, cfg in mcp_config.items() ]
|
||||
tasks = [_fetch(name, cfg) for name, cfg in mcp_config.items()]
|
||||
|
||||
if tasks:
|
||||
results = await asyncio.gather(*tasks)
|
||||
@@ -121,8 +166,9 @@ el.replaceWith(d);
|
||||
|
||||
return all_llm_tools
|
||||
|
||||
async def _ensure_connected(self, name: str, cfg: Dict[str, Any]) -> Optional[Any]:
|
||||
if not MCP_AVAILABLE: return None
|
||||
async def _ensure_connected(self, name: str, cfg: dict[str, Any]) -> Any | None:
|
||||
if not MCP_AVAILABLE:
|
||||
return None
|
||||
|
||||
if name in self.sessions and self.sessions[name].get("session"):
|
||||
return self.sessions[name]["session"]
|
||||
@@ -134,7 +180,7 @@ el.replaceWith(d);
|
||||
if name in self._connecting:
|
||||
try:
|
||||
return await asyncio.wait_for(asyncio.shield(self._connecting[name]), timeout=10.0)
|
||||
except Exception:
|
||||
except Exception: # noqa: BLE001
|
||||
return None
|
||||
|
||||
loop = asyncio.get_running_loop()
|
||||
@@ -155,33 +201,32 @@ el.replaceWith(d);
|
||||
self.sessions[name] = {"session": session, "stack": stack}
|
||||
fut.set_result(session)
|
||||
return session
|
||||
except Exception:
|
||||
except Exception: # noqa: BLE001
|
||||
fut.set_result(None)
|
||||
return None
|
||||
finally:
|
||||
if name in self._connecting:
|
||||
del self._connecting[name]
|
||||
|
||||
async def _fetch_tools_as_openai(self, server_name: str, session: Any) -> List[Dict[str, Any]]:
|
||||
async def _fetch_tools_as_openai(self, server_name: str, session: Any) -> list[dict[str, Any]]:
|
||||
try:
|
||||
result = await asyncio.wait_for(session.list_tools(), timeout=5.0)
|
||||
openai_tools = []
|
||||
for tool in result.tools:
|
||||
# Use mcp_ prefix to ensure valid function name for LiteLLM/Gemini
|
||||
prefixed_name = f"mcp_{server_name}__{tool.name}"
|
||||
openai_tools.append({
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": prefixed_name,
|
||||
"description": f"[{server_name}] {tool.description}",
|
||||
"parameters": tool.inputSchema
|
||||
}
|
||||
"parameters": tool.inputSchema,
|
||||
},
|
||||
})
|
||||
return openai_tools
|
||||
except Exception:
|
||||
except Exception: # noqa: BLE001
|
||||
return []
|
||||
|
||||
async def call_tool(self, full_tool_name: str, arguments: Dict[str, Any]) -> Any:
|
||||
async def call_tool(self, full_tool_name: str, arguments: dict[str, Any]) -> Any:
|
||||
"""Calls an MCP tool and returns text result."""
|
||||
if not MCP_AVAILABLE:
|
||||
return "Error: MCP SDK is not installed."
|
||||
@@ -189,7 +234,7 @@ el.replaceWith(d);
|
||||
if "__" not in full_tool_name:
|
||||
return f"Error: Tool {full_tool_name} is not a valid MCP tool."
|
||||
|
||||
clean_name = full_tool_name[4:] if full_tool_name.startswith("mcp_") else full_tool_name
|
||||
clean_name = full_tool_name.removeprefix("mcp_")
|
||||
server_name, tool_name = clean_name.split("__", 1)
|
||||
|
||||
if server_name not in self.sessions:
|
||||
@@ -200,29 +245,40 @@ el.replaceWith(d);
|
||||
result = await asyncio.wait_for(session.call_tool(tool_name, arguments), timeout=60.0)
|
||||
text_outputs = [content.text for content in result.content if hasattr(content, "text")]
|
||||
return "\n".join(text_outputs) if text_outputs else str(result)
|
||||
except Exception as e:
|
||||
return f"Error calling tool {tool_name} on {server_name}: {str(e)}"
|
||||
except Exception as e: # noqa: BLE001
|
||||
return f"Error calling tool {tool_name} on {server_name}: {e!s}"
|
||||
|
||||
async def shutdown(self):
|
||||
"""Close all SSE connections."""
|
||||
for name, data in self.sessions.items():
|
||||
"""Close all SSE connections and clean up resources."""
|
||||
for data in list(self.sessions.values()):
|
||||
stack = data.get("stack")
|
||||
if stack:
|
||||
with suppress(Exception):
|
||||
await stack.aclose()
|
||||
self.sessions = {}</code></pre>
|
||||
self.sessions.clear()
|
||||
self.tool_cache.clear()
|
||||
self._server_configs.clear()
|
||||
for fut in list(self._connecting.values()):
|
||||
if not fut.done():
|
||||
fut.cancel()
|
||||
self._connecting.clear()
|
||||
with self._lock:
|
||||
if hasattr(self, "_scope_key") and self._scope_key in self._instances:
|
||||
del self._instances[self._scope_key]
|
||||
self._initialized = False</code></pre>
|
||||
</details>
|
||||
<div class="desc"><p>Manages MCP SSE client connections for connpy.</p></div>
|
||||
<h3>Methods</h3>
|
||||
<dl>
|
||||
<dt id="connpy.mcp_client.MCPClientManager.call_tool"><code class="name flex">
|
||||
<span>async def <span class="ident">call_tool</span></span>(<span>self, full_tool_name: str, arguments: Dict[str, Any]) ‑> Any</span>
|
||||
<span>async def <span class="ident">call_tool</span></span>(<span>self, full_tool_name: str, arguments: dict[str, typing.Any]) ‑> Any</span>
|
||||
</code></dt>
|
||||
<dd>
|
||||
<details class="source">
|
||||
<summary>
|
||||
<span>Expand source code</span>
|
||||
</summary>
|
||||
<pre><code class="python">async def call_tool(self, full_tool_name: str, arguments: Dict[str, Any]) -> Any:
|
||||
<pre><code class="python">async def call_tool(self, full_tool_name: str, arguments: dict[str, Any]) -> Any:
|
||||
"""Calls an MCP tool and returns text result."""
|
||||
if not MCP_AVAILABLE:
|
||||
return "Error: MCP SDK is not installed."
|
||||
@@ -230,7 +286,7 @@ el.replaceWith(d);
|
||||
if "__" not in full_tool_name:
|
||||
return f"Error: Tool {full_tool_name} is not a valid MCP tool."
|
||||
|
||||
clean_name = full_tool_name[4:] if full_tool_name.startswith("mcp_") else full_tool_name
|
||||
clean_name = full_tool_name.removeprefix("mcp_")
|
||||
server_name, tool_name = clean_name.split("__", 1)
|
||||
|
||||
if server_name not in self.sessions:
|
||||
@@ -241,23 +297,21 @@ el.replaceWith(d);
|
||||
result = await asyncio.wait_for(session.call_tool(tool_name, arguments), timeout=60.0)
|
||||
text_outputs = [content.text for content in result.content if hasattr(content, "text")]
|
||||
return "\n".join(text_outputs) if text_outputs else str(result)
|
||||
except Exception as e:
|
||||
return f"Error calling tool {tool_name} on {server_name}: {str(e)}"</code></pre>
|
||||
except Exception as e: # noqa: BLE001
|
||||
return f"Error calling tool {tool_name} on {server_name}: {e!s}"</code></pre>
|
||||
</details>
|
||||
<div class="desc"><p>Calls an MCP tool and returns text result.</p></div>
|
||||
</dd>
|
||||
<dt id="connpy.mcp_client.MCPClientManager.get_tools_for_llm"><code class="name flex">
|
||||
<span>async def <span class="ident">get_tools_for_llm</span></span>(<span>self, os_filter: str | None = None) ‑> List[Dict[str, Any]]</span>
|
||||
<span>async def <span class="ident">get_tools_for_llm</span></span>(<span>self, os_filter: str | None = None) ‑> list[dict[str, typing.Any]]</span>
|
||||
</code></dt>
|
||||
<dd>
|
||||
<details class="source">
|
||||
<summary>
|
||||
<span>Expand source code</span>
|
||||
</summary>
|
||||
<pre><code class="python">async def get_tools_for_llm(self, os_filter: Optional[str] = None) -> List[Dict[str, Any]]:
|
||||
"""
|
||||
Fetches tools from enabled MCP servers that match the OS filter.
|
||||
"""
|
||||
<pre><code class="python">async def get_tools_for_llm(self, os_filter: str | None = None) -> list[dict[str, Any]]:
|
||||
"""Fetches tools from enabled MCP servers that match the OS filter."""
|
||||
if not MCP_AVAILABLE:
|
||||
return []
|
||||
|
||||
@@ -267,29 +321,53 @@ el.replaceWith(d);
|
||||
mcp_config = self.config.get_effective_setting("ai", {}).get("mcp_servers", {})
|
||||
else:
|
||||
mcp_config = self.config.config.get("ai", {}).get("mcp_servers", {}) if hasattr(self.config, "config") else {}
|
||||
except Exception:
|
||||
except Exception: # noqa: BLE001
|
||||
return []
|
||||
|
||||
if not isinstance(mcp_config, dict):
|
||||
mcp_config = {}
|
||||
|
||||
# Invalidate cache and reconnect if server configuration changed or server was removed
|
||||
current_names = set(mcp_config.keys())
|
||||
for old_name in list(self._server_configs.keys()):
|
||||
if old_name not in current_names:
|
||||
self.tool_cache.pop(old_name, None)
|
||||
old_data = self.sessions.pop(old_name, None)
|
||||
if old_data and old_data.get("stack"):
|
||||
with suppress(Exception):
|
||||
await old_data["stack"].aclose()
|
||||
self._server_configs.pop(old_name, None)
|
||||
|
||||
for name, cfg in mcp_config.items():
|
||||
cached_cfg = self._server_configs.get(name)
|
||||
if cached_cfg is not None and cached_cfg != cfg:
|
||||
self.tool_cache.pop(name, None)
|
||||
old_data = self.sessions.pop(name, None)
|
||||
if old_data and old_data.get("stack"):
|
||||
with suppress(Exception):
|
||||
await old_data["stack"].aclose()
|
||||
self._server_configs[name] = dict(cfg) if isinstance(cfg, dict) else cfg
|
||||
|
||||
async def _fetch(name, cfg):
|
||||
if not cfg.get("enabled", True): return []
|
||||
if not isinstance(cfg, dict) or not cfg.get("enabled", True):
|
||||
return []
|
||||
|
||||
# Filter by OS if specified in config (primarily used for copilot strict matching)
|
||||
auto_os = cfg.get("auto_load_on_os")
|
||||
if os_filter is not None and auto_os and os_filter.lower() != auto_os.lower():
|
||||
return []
|
||||
|
||||
try:
|
||||
with suppress(Exception):
|
||||
session = await self._ensure_connected(name, cfg)
|
||||
if session:
|
||||
if name in self.tool_cache: return self.tool_cache[name]
|
||||
if name in self.tool_cache:
|
||||
return self.tool_cache[name]
|
||||
llm_tools = await self._fetch_tools_as_openai(name, session)
|
||||
self.tool_cache[name] = llm_tools
|
||||
return llm_tools
|
||||
except Exception:
|
||||
pass
|
||||
return []
|
||||
|
||||
tasks = [ _fetch(name, cfg) for name, cfg in mcp_config.items() ]
|
||||
tasks = [_fetch(name, cfg) for name, cfg in mcp_config.items()]
|
||||
|
||||
if tasks:
|
||||
results = await asyncio.gather(*tasks)
|
||||
@@ -309,14 +387,25 @@ el.replaceWith(d);
|
||||
<span>Expand source code</span>
|
||||
</summary>
|
||||
<pre><code class="python">async def shutdown(self):
|
||||
"""Close all SSE connections."""
|
||||
for name, data in self.sessions.items():
|
||||
"""Close all SSE connections and clean up resources."""
|
||||
for data in list(self.sessions.values()):
|
||||
stack = data.get("stack")
|
||||
if stack:
|
||||
with suppress(Exception):
|
||||
await stack.aclose()
|
||||
self.sessions = {}</code></pre>
|
||||
self.sessions.clear()
|
||||
self.tool_cache.clear()
|
||||
self._server_configs.clear()
|
||||
for fut in list(self._connecting.values()):
|
||||
if not fut.done():
|
||||
fut.cancel()
|
||||
self._connecting.clear()
|
||||
with self._lock:
|
||||
if hasattr(self, "_scope_key") and self._scope_key in self._instances:
|
||||
del self._instances[self._scope_key]
|
||||
self._initialized = False</code></pre>
|
||||
</details>
|
||||
<div class="desc"><p>Close all SSE connections.</p></div>
|
||||
<div class="desc"><p>Close all SSE connections and clean up resources.</p></div>
|
||||
</dd>
|
||||
</dl>
|
||||
</dd>
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -427,8 +427,9 @@ el.replaceWith(d);
|
||||
</details>
|
||||
<div class="desc"><p>Business logic for interacting with AI agents and LLM configurations.</p>
|
||||
<p>Initialize the service.</p>
|
||||
<h2 id="args">Args</h2>
|
||||
<dl>
|
||||
<dt>Args</dt>
|
||||
<dt>-----=</dt>
|
||||
<dt><strong><code>config</code></strong></dt>
|
||||
<dd>An instance of configfile (or None to instantiate a new one/use global context).</dd>
|
||||
</dl></div>
|
||||
|
||||
@@ -89,8 +89,9 @@ el.replaceWith(d);
|
||||
</details>
|
||||
<div class="desc"><p>Base class for all connpy services, providing common configuration access.</p>
|
||||
<p>Initialize the service.</p>
|
||||
<h2 id="args">Args</h2>
|
||||
<dl>
|
||||
<dt>Args</dt>
|
||||
<dt>-----=</dt>
|
||||
<dt><strong><code>config</code></strong></dt>
|
||||
<dd>An instance of configfile (or None to instantiate a new one/use global context).</dd>
|
||||
</dl></div>
|
||||
|
||||
@@ -133,8 +133,9 @@ el.replaceWith(d);
|
||||
</details>
|
||||
<div class="desc"><p>Business logic for general application settings and state configuration.</p>
|
||||
<p>Initialize the service.</p>
|
||||
<h2 id="args">Args</h2>
|
||||
<dl>
|
||||
<dt>Args</dt>
|
||||
<dt>-----=</dt>
|
||||
<dt><strong><code>config</code></strong></dt>
|
||||
<dd>An instance of configfile (or None to instantiate a new one/use global context).</dd>
|
||||
</dl></div>
|
||||
|
||||
@@ -58,15 +58,31 @@ el.replaceWith(d);
|
||||
<pre><code class="python">class ContextService(BaseService):
|
||||
"""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
|
||||
def contexts(self) -> Dict[str, List[str]]:
|
||||
def contexts(self) -> dict[str, list[str]]:
|
||||
return self.config.config.get("contexts", {"all": [".*"]})
|
||||
|
||||
@property
|
||||
def current_context(self) -> str:
|
||||
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 = []
|
||||
for name in self.contexts.keys():
|
||||
result.append({
|
||||
@@ -76,27 +92,29 @@ el.replaceWith(d);
|
||||
})
|
||||
return result
|
||||
|
||||
def add_context(self, name: str, regexes: List[str]):
|
||||
def add_context(self, name: str, regexes: list[str]):
|
||||
if not name.isalnum():
|
||||
raise ValueError("Context name must be alphanumeric")
|
||||
self._validate_regexes(regexes)
|
||||
|
||||
ctxs = self.contexts
|
||||
if name in ctxs:
|
||||
raise ValueError(f"Context '{name}' already exists")
|
||||
|
||||
ctxs[name] = regexes
|
||||
ctxs[name] = list(regexes)
|
||||
self.config.config["contexts"] = ctxs
|
||||
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":
|
||||
raise ValueError("Cannot modify default context 'all'")
|
||||
self._validate_regexes(regexes)
|
||||
|
||||
ctxs = self.contexts
|
||||
if name not in ctxs:
|
||||
raise ValueError(f"Context '{name}' does not exist")
|
||||
|
||||
ctxs[name] = regexes
|
||||
ctxs[name] = list(regexes)
|
||||
self.config.config["contexts"] = ctxs
|
||||
self.config._saveconfig(self.config.file)
|
||||
|
||||
@@ -118,15 +136,47 @@ el.replaceWith(d);
|
||||
if name not in self.contexts:
|
||||
raise ValueError(f"Context '{name}' does not exist")
|
||||
|
||||
prev_context = self.current_context
|
||||
self.config.config["current_context"] = name
|
||||
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]:
|
||||
patterns = self.contexts.get(self.current_context, [".*"])
|
||||
return [re.compile(p) for p in patterns]
|
||||
def get_active_regexes(self) -> list[re.Pattern]:
|
||||
return compile_active_context_patterns(getattr(self.config, "config", None))
|
||||
|
||||
def _match_any(self, node_name: str, patterns: List[re.Pattern]) -> bool:
|
||||
return any(p.match(node_name) for p in patterns)
|
||||
def _match_any(self, node_name: str, patterns: list[re.Pattern]) -> bool:
|
||||
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
|
||||
def filter_node_list(self, *args, **kwargs):
|
||||
@@ -139,8 +189,9 @@ el.replaceWith(d);
|
||||
</details>
|
||||
<div class="desc"><p>Business logic for managing and applying regex-based contexts locally.</p>
|
||||
<p>Initialize the service.</p>
|
||||
<h2 id="args">Args</h2>
|
||||
<dl>
|
||||
<dt>Args</dt>
|
||||
<dt>-----=</dt>
|
||||
<dt><strong><code>config</code></strong></dt>
|
||||
<dd>An instance of configfile (or None to instantiate a new one/use global context).</dd>
|
||||
</dl></div>
|
||||
@@ -150,14 +201,14 @@ el.replaceWith(d);
|
||||
</ul>
|
||||
<h3>Instance variables</h3>
|
||||
<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>
|
||||
<details class="source">
|
||||
<summary>
|
||||
<span>Expand source code</span>
|
||||
</summary>
|
||||
<pre><code class="python">@property
|
||||
def contexts(self) -> Dict[str, List[str]]:
|
||||
def contexts(self) -> dict[str, list[str]]:
|
||||
return self.config.config.get("contexts", {"all": [".*"]})</code></pre>
|
||||
</details>
|
||||
<div class="desc"></div>
|
||||
@@ -178,22 +229,23 @@ def current_context(self) -> str:
|
||||
<h3>Methods</h3>
|
||||
<dl>
|
||||
<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>
|
||||
<dd>
|
||||
<details class="source">
|
||||
<summary>
|
||||
<span>Expand source code</span>
|
||||
</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():
|
||||
raise ValueError("Context name must be alphanumeric")
|
||||
self._validate_regexes(regexes)
|
||||
|
||||
ctxs = self.contexts
|
||||
if name in ctxs:
|
||||
raise ValueError(f"Context '{name}' already exists")
|
||||
|
||||
ctxs[name] = regexes
|
||||
ctxs[name] = list(regexes)
|
||||
self.config.config["contexts"] = ctxs
|
||||
self.config._saveconfig(self.config.file)</code></pre>
|
||||
</details>
|
||||
@@ -252,28 +304,40 @@ def current_context(self) -> str:
|
||||
<div class="desc"></div>
|
||||
</dd>
|
||||
<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>
|
||||
<dd>
|
||||
<details class="source">
|
||||
<summary>
|
||||
<span>Expand source code</span>
|
||||
</summary>
|
||||
<pre><code class="python">def get_active_regexes(self) -> List[re.Pattern]:
|
||||
patterns = self.contexts.get(self.current_context, [".*"])
|
||||
return [re.compile(p) for p in patterns]</code></pre>
|
||||
<pre><code class="python">def get_active_regexes(self) -> list[re.Pattern]:
|
||||
return compile_active_context_patterns(getattr(self.config, "config", None))</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) -> bool:
|
||||
return is_default_all_context_cfg(getattr(self.config, "config", None))</code></pre>
|
||||
</details>
|
||||
<div class="desc"></div>
|
||||
</dd>
|
||||
<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>
|
||||
<dd>
|
||||
<details class="source">
|
||||
<summary>
|
||||
<span>Expand source code</span>
|
||||
</summary>
|
||||
<pre><code class="python">def list_contexts(self) -> List[Dict[str, Any]]:
|
||||
<pre><code class="python">def list_contexts(self) -> list[dict[str, Any]]:
|
||||
result = []
|
||||
for name in self.contexts.keys():
|
||||
result.append({
|
||||
@@ -285,6 +349,23 @@ def current_context(self) -> str:
|
||||
</details>
|
||||
<div class="desc"></div>
|
||||
</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) -> 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">
|
||||
<span>def <span class="ident">set_active_context</span></span>(<span>self, name: str)</span>
|
||||
</code></dt>
|
||||
@@ -297,33 +378,67 @@ def current_context(self) -> str:
|
||||
if name not in self.contexts:
|
||||
raise ValueError(f"Context '{name}' does not exist")
|
||||
|
||||
prev_context = self.current_context
|
||||
self.config.config["current_context"] = 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, "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()</code></pre>
|
||||
</details>
|
||||
<div class="desc"></div>
|
||||
</dd>
|
||||
<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>
|
||||
<dd>
|
||||
<details class="source">
|
||||
<summary>
|
||||
<span>Expand source code</span>
|
||||
</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 == "all":
|
||||
raise ValueError("Cannot modify default context 'all'")
|
||||
self._validate_regexes(regexes)
|
||||
|
||||
ctxs = self.contexts
|
||||
if name not in ctxs:
|
||||
raise ValueError(f"Context '{name}' does not exist")
|
||||
|
||||
ctxs[name] = regexes
|
||||
ctxs[name] = list(regexes)
|
||||
self.config.config["contexts"] = ctxs
|
||||
self.config._saveconfig(self.config.file)</code></pre>
|
||||
</details>
|
||||
<div class="desc"></div>
|
||||
</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) -> 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}'."
|
||||
)</code></pre>
|
||||
</details>
|
||||
<div class="desc"></div>
|
||||
</dd>
|
||||
</dl>
|
||||
<h3>Inherited members</h3>
|
||||
<ul class="hlist">
|
||||
@@ -351,7 +466,7 @@ def current_context(self) -> str:
|
||||
<ul>
|
||||
<li>
|
||||
<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.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>
|
||||
@@ -359,9 +474,12 @@ def current_context(self) -> 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_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.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.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.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>
|
||||
</li>
|
||||
</ul>
|
||||
|
||||
@@ -67,6 +67,7 @@ el.replaceWith(d);
|
||||
</ul>
|
||||
<h3>Subclasses</h3>
|
||||
<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.InvalidConfigurationError" href="#connpy.services.exceptions.InvalidConfigurationError">InvalidConfigurationError</a></li>
|
||||
<li><a title="connpy.services.exceptions.NodeAlreadyExistsError" href="#connpy.services.exceptions.NodeAlreadyExistsError">NodeAlreadyExistsError</a></li>
|
||||
|
||||
@@ -48,7 +48,7 @@ el.replaceWith(d);
|
||||
<dl>
|
||||
<dt id="connpy.services.execution_service.ExecutionService"><code class="flex name class">
|
||||
<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>
|
||||
<dd>
|
||||
<details class="source">
|
||||
@@ -58,23 +58,196 @@ el.replaceWith(d);
|
||||
<pre><code class="python">class ExecutionService(BaseService):
|
||||
"""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(
|
||||
self,
|
||||
nodes_filter: str,
|
||||
commands: List[str],
|
||||
variables: Optional[Dict[str, Any]] = None,
|
||||
commands: list[str],
|
||||
variables: dict[str, Any] | None = None,
|
||||
parallel: int = 10,
|
||||
timeout: int = 20,
|
||||
folder: Optional[str] = None,
|
||||
prompt: Optional[str] = None,
|
||||
on_node_complete: Optional[Callable] = None,
|
||||
logger: Optional[Callable] = None,
|
||||
name: Optional[str] = None
|
||||
) -> Dict[str, str]:
|
||||
|
||||
timeout: int | None = None,
|
||||
folder: str | None = None,
|
||||
prompt: str | None = None,
|
||||
on_node_complete: Callable | None = None,
|
||||
logger: Callable | None = None,
|
||||
name: str | None = None,
|
||||
connect_timeout: int | None = None,
|
||||
auth_timeout: int | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""Execute commands on a set of nodes."""
|
||||
try:
|
||||
self._ensure_backend_supports_stage_timeouts(
|
||||
connect_timeout=connect_timeout,
|
||||
auth_timeout=auth_timeout,
|
||||
)
|
||||
|
||||
if self._is_remote_backend():
|
||||
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)
|
||||
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:
|
||||
raise ConnpyError(f"No nodes found matching filter: {nodes_filter}")
|
||||
|
||||
@@ -82,47 +255,96 @@ el.replaceWith(d);
|
||||
executor = Nodes(node_data, config=self.config)
|
||||
self.last_executor = executor
|
||||
|
||||
results = executor.run(
|
||||
commands=commands,
|
||||
vars=variables,
|
||||
parallel=parallel,
|
||||
timeout=timeout,
|
||||
folder=folder,
|
||||
prompt=prompt,
|
||||
on_complete=on_node_complete,
|
||||
logger=logger
|
||||
)
|
||||
run_kwargs: dict[str, Any] = {
|
||||
"commands": commands,
|
||||
"vars": variables,
|
||||
"parallel": parallel,
|
||||
"timeout": timeout,
|
||||
"folder": folder,
|
||||
"prompt": prompt,
|
||||
"on_complete": on_node_complete,
|
||||
"logger": logger,
|
||||
"connect_timeout": connect_timeout,
|
||||
"auth_timeout": auth_timeout,
|
||||
"_default_cmd_timeout": 20,
|
||||
}
|
||||
run_params = inspect.signature(executor.run).parameters
|
||||
if "unattended" in run_params:
|
||||
run_kwargs["unattended"] = True
|
||||
if "attempt_tracker" in run_params:
|
||||
run_kwargs["attempt_tracker"] = self.attempt_tracker
|
||||
|
||||
# Combine output and status for the caller
|
||||
results = executor.run(**run_kwargs)
|
||||
|
||||
fb_map = getattr(executor, "fallback_used", {})
|
||||
fb_dict = fb_map if isinstance(fb_map, dict) else {}
|
||||
|
||||
# Combine output, status, and fallback metadata for the caller
|
||||
full_results = {}
|
||||
self.last_fallback_used = {}
|
||||
for unique in results:
|
||||
fb_used = bool(fb_dict.get(unique, False))
|
||||
full_results[unique] = {
|
||||
"output": results[unique],
|
||||
"status": executor.status.get(unique, 1)
|
||||
"status": executor.status.get(unique, 1),
|
||||
"fallback_used": fb_used,
|
||||
}
|
||||
self.last_fallback_used[unique] = fb_used
|
||||
|
||||
return full_results
|
||||
except Exception as e:
|
||||
raise ConnpyError(f"Execution failed: {e}")
|
||||
except ConnpyError:
|
||||
raise
|
||||
except Exception as e: # noqa: BLE001
|
||||
raise_sanitized_auth_error(e, code="AUTH_FAILED", context_label="execution failed")
|
||||
|
||||
def test_commands(
|
||||
self,
|
||||
nodes_filter: str,
|
||||
commands: List[str],
|
||||
expected: List[str],
|
||||
variables: Optional[Dict[str, Any]] = None,
|
||||
commands: list[str],
|
||||
expected: list[str],
|
||||
variables: dict[str, Any] | None = None,
|
||||
parallel: int = 10,
|
||||
timeout: int = 20,
|
||||
folder: Optional[str] = None,
|
||||
prompt: Optional[str] = None,
|
||||
on_node_complete: Optional[Callable] = None,
|
||||
logger: Optional[Callable] = None,
|
||||
name: Optional[str] = None
|
||||
) -> Dict[str, Dict[str, bool]]:
|
||||
|
||||
timeout: int | None = None,
|
||||
folder: str | None = None,
|
||||
prompt: str | None = None,
|
||||
on_node_complete: Callable | None = None,
|
||||
logger: Callable | None = None,
|
||||
name: str | None = None,
|
||||
connect_timeout: int | None = None,
|
||||
auth_timeout: int | None = None,
|
||||
) -> dict[str, dict[str, bool]]:
|
||||
"""Run commands and verify expected output on a set of nodes."""
|
||||
try:
|
||||
self._ensure_backend_supports_stage_timeouts(
|
||||
connect_timeout=connect_timeout,
|
||||
auth_timeout=auth_timeout,
|
||||
)
|
||||
|
||||
if self._is_remote_backend():
|
||||
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)
|
||||
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:
|
||||
raise ConnpyError(f"No nodes found matching filter: {nodes_filter}")
|
||||
|
||||
@@ -130,38 +352,57 @@ el.replaceWith(d);
|
||||
executor = Nodes(node_data, config=self.config)
|
||||
self.last_executor = executor
|
||||
|
||||
results = executor.test(
|
||||
commands=commands,
|
||||
expected=expected,
|
||||
vars=variables,
|
||||
parallel=parallel,
|
||||
timeout=timeout,
|
||||
folder=folder,
|
||||
prompt=prompt,
|
||||
on_complete=on_node_complete,
|
||||
logger=logger
|
||||
)
|
||||
return results
|
||||
except Exception as e:
|
||||
raise ConnpyError(f"Testing failed: {e}")
|
||||
test_kwargs: dict[str, Any] = {
|
||||
"commands": commands,
|
||||
"expected": expected,
|
||||
"vars": variables,
|
||||
"parallel": parallel,
|
||||
"timeout": timeout,
|
||||
"folder": folder,
|
||||
"prompt": prompt,
|
||||
"on_complete": on_node_complete,
|
||||
"logger": logger,
|
||||
"connect_timeout": connect_timeout,
|
||||
"auth_timeout": auth_timeout,
|
||||
"_default_cmd_timeout": 20,
|
||||
}
|
||||
test_params = inspect.signature(executor.test).parameters
|
||||
if "unattended" in test_params:
|
||||
test_kwargs["unattended"] = True
|
||||
if "attempt_tracker" in test_params:
|
||||
test_kwargs["attempt_tracker"] = self.attempt_tracker
|
||||
|
||||
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."""
|
||||
if not os.path.exists(script_path):
|
||||
raise ConnpyError(f"Script file not found: {script_path}")
|
||||
|
||||
try:
|
||||
with open(script_path, "r") as f:
|
||||
with open(script_path) as f:
|
||||
commands = [line.strip() for line in f if line.strip()]
|
||||
except Exception as e:
|
||||
raise ConnpyError(f"Failed to read script {script_path}: {e}")
|
||||
except OSError:
|
||||
raise ConnpyError("Failed to read script file") from None
|
||||
|
||||
return self.run_commands(nodes_filter, commands, parallel=parallel)</code></pre>
|
||||
</details>
|
||||
<div class="desc"><p>Business logic for executing commands on nodes and running automation scripts.</p>
|
||||
<p>Initialize the service.</p>
|
||||
<h2 id="args">Args</h2>
|
||||
<dl>
|
||||
<dt>Args</dt>
|
||||
<dt>-----=</dt>
|
||||
<dt><strong><code>config</code></strong></dt>
|
||||
<dd>An instance of configfile (or None to instantiate a new one/use global context).</dd>
|
||||
</dl></div>
|
||||
@@ -172,30 +413,30 @@ el.replaceWith(d);
|
||||
<h3>Methods</h3>
|
||||
<dl>
|
||||
<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>
|
||||
<dd>
|
||||
<details class="source">
|
||||
<summary>
|
||||
<span>Expand source code</span>
|
||||
</summary>
|
||||
<pre><code class="python">def run_cli_script(self, nodes_filter: str, script_path: str, parallel: int = 10) -> Dict[str, str]:
|
||||
<pre><code class="python">def run_cli_script(self, nodes_filter: str, script_path: str, parallel: int = 10) -> dict[str, str]:
|
||||
"""Run a plain-text script containing one command per line."""
|
||||
if not os.path.exists(script_path):
|
||||
raise ConnpyError(f"Script file not found: {script_path}")
|
||||
|
||||
try:
|
||||
with open(script_path, "r") as f:
|
||||
with open(script_path) as f:
|
||||
commands = [line.strip() for line in f if line.strip()]
|
||||
except Exception as e:
|
||||
raise ConnpyError(f"Failed to read script {script_path}: {e}")
|
||||
except OSError:
|
||||
raise ConnpyError("Failed to read script file") from None
|
||||
|
||||
return self.run_commands(nodes_filter, commands, parallel=parallel)</code></pre>
|
||||
</details>
|
||||
<div class="desc"><p>Run a plain-text script containing one command per line.</p></div>
|
||||
</dd>
|
||||
<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>
|
||||
<dd>
|
||||
<details class="source">
|
||||
@@ -205,20 +446,49 @@ el.replaceWith(d);
|
||||
<pre><code class="python">def run_commands(
|
||||
self,
|
||||
nodes_filter: str,
|
||||
commands: List[str],
|
||||
variables: Optional[Dict[str, Any]] = None,
|
||||
commands: list[str],
|
||||
variables: dict[str, Any] | None = None,
|
||||
parallel: int = 10,
|
||||
timeout: int = 20,
|
||||
folder: Optional[str] = None,
|
||||
prompt: Optional[str] = None,
|
||||
on_node_complete: Optional[Callable] = None,
|
||||
logger: Optional[Callable] = None,
|
||||
name: Optional[str] = None
|
||||
) -> Dict[str, str]:
|
||||
|
||||
timeout: int | None = None,
|
||||
folder: str | None = None,
|
||||
prompt: str | None = None,
|
||||
on_node_complete: Callable | None = None,
|
||||
logger: Callable | None = None,
|
||||
name: str | None = None,
|
||||
connect_timeout: int | None = None,
|
||||
auth_timeout: int | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""Execute commands on a set of nodes."""
|
||||
try:
|
||||
self._ensure_backend_supports_stage_timeouts(
|
||||
connect_timeout=connect_timeout,
|
||||
auth_timeout=auth_timeout,
|
||||
)
|
||||
|
||||
if self._is_remote_backend():
|
||||
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)
|
||||
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:
|
||||
raise ConnpyError(f"No nodes found matching filter: {nodes_filter}")
|
||||
|
||||
@@ -226,33 +496,52 @@ el.replaceWith(d);
|
||||
executor = Nodes(node_data, config=self.config)
|
||||
self.last_executor = executor
|
||||
|
||||
results = executor.run(
|
||||
commands=commands,
|
||||
vars=variables,
|
||||
parallel=parallel,
|
||||
timeout=timeout,
|
||||
folder=folder,
|
||||
prompt=prompt,
|
||||
on_complete=on_node_complete,
|
||||
logger=logger
|
||||
)
|
||||
run_kwargs: dict[str, Any] = {
|
||||
"commands": commands,
|
||||
"vars": variables,
|
||||
"parallel": parallel,
|
||||
"timeout": timeout,
|
||||
"folder": folder,
|
||||
"prompt": prompt,
|
||||
"on_complete": on_node_complete,
|
||||
"logger": logger,
|
||||
"connect_timeout": connect_timeout,
|
||||
"auth_timeout": auth_timeout,
|
||||
"_default_cmd_timeout": 20,
|
||||
}
|
||||
run_params = inspect.signature(executor.run).parameters
|
||||
if "unattended" in run_params:
|
||||
run_kwargs["unattended"] = True
|
||||
if "attempt_tracker" in run_params:
|
||||
run_kwargs["attempt_tracker"] = self.attempt_tracker
|
||||
|
||||
# Combine output and status for the caller
|
||||
results = executor.run(**run_kwargs)
|
||||
|
||||
fb_map = getattr(executor, "fallback_used", {})
|
||||
fb_dict = fb_map if isinstance(fb_map, dict) else {}
|
||||
|
||||
# Combine output, status, and fallback metadata for the caller
|
||||
full_results = {}
|
||||
self.last_fallback_used = {}
|
||||
for unique in results:
|
||||
fb_used = bool(fb_dict.get(unique, False))
|
||||
full_results[unique] = {
|
||||
"output": results[unique],
|
||||
"status": executor.status.get(unique, 1)
|
||||
"status": executor.status.get(unique, 1),
|
||||
"fallback_used": fb_used,
|
||||
}
|
||||
self.last_fallback_used[unique] = fb_used
|
||||
|
||||
return full_results
|
||||
except Exception as e:
|
||||
raise ConnpyError(f"Execution failed: {e}")</code></pre>
|
||||
except ConnpyError:
|
||||
raise
|
||||
except Exception as e: # noqa: BLE001
|
||||
raise_sanitized_auth_error(e, code="AUTH_FAILED", context_label="execution failed")</code></pre>
|
||||
</details>
|
||||
<div class="desc"><p>Execute commands on a set of nodes.</p></div>
|
||||
</dd>
|
||||
<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>
|
||||
<dd>
|
||||
<details class="source">
|
||||
@@ -262,21 +551,51 @@ el.replaceWith(d);
|
||||
<pre><code class="python">def test_commands(
|
||||
self,
|
||||
nodes_filter: str,
|
||||
commands: List[str],
|
||||
expected: List[str],
|
||||
variables: Optional[Dict[str, Any]] = None,
|
||||
commands: list[str],
|
||||
expected: list[str],
|
||||
variables: dict[str, Any] | None = None,
|
||||
parallel: int = 10,
|
||||
timeout: int = 20,
|
||||
folder: Optional[str] = None,
|
||||
prompt: Optional[str] = None,
|
||||
on_node_complete: Optional[Callable] = None,
|
||||
logger: Optional[Callable] = None,
|
||||
name: Optional[str] = None
|
||||
) -> Dict[str, Dict[str, bool]]:
|
||||
|
||||
timeout: int | None = None,
|
||||
folder: str | None = None,
|
||||
prompt: str | None = None,
|
||||
on_node_complete: Callable | None = None,
|
||||
logger: Callable | None = None,
|
||||
name: str | None = None,
|
||||
connect_timeout: int | None = None,
|
||||
auth_timeout: int | None = None,
|
||||
) -> dict[str, dict[str, bool]]:
|
||||
"""Run commands and verify expected output on a set of nodes."""
|
||||
try:
|
||||
self._ensure_backend_supports_stage_timeouts(
|
||||
connect_timeout=connect_timeout,
|
||||
auth_timeout=auth_timeout,
|
||||
)
|
||||
|
||||
if self._is_remote_backend():
|
||||
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)
|
||||
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:
|
||||
raise ConnpyError(f"No nodes found matching filter: {nodes_filter}")
|
||||
|
||||
@@ -284,20 +603,38 @@ el.replaceWith(d);
|
||||
executor = Nodes(node_data, config=self.config)
|
||||
self.last_executor = executor
|
||||
|
||||
results = executor.test(
|
||||
commands=commands,
|
||||
expected=expected,
|
||||
vars=variables,
|
||||
parallel=parallel,
|
||||
timeout=timeout,
|
||||
folder=folder,
|
||||
prompt=prompt,
|
||||
on_complete=on_node_complete,
|
||||
logger=logger
|
||||
)
|
||||
return results
|
||||
except Exception as e:
|
||||
raise ConnpyError(f"Testing failed: {e}")</code></pre>
|
||||
test_kwargs: dict[str, Any] = {
|
||||
"commands": commands,
|
||||
"expected": expected,
|
||||
"vars": variables,
|
||||
"parallel": parallel,
|
||||
"timeout": timeout,
|
||||
"folder": folder,
|
||||
"prompt": prompt,
|
||||
"on_complete": on_node_complete,
|
||||
"logger": logger,
|
||||
"connect_timeout": connect_timeout,
|
||||
"auth_timeout": auth_timeout,
|
||||
"_default_cmd_timeout": 20,
|
||||
}
|
||||
test_params = inspect.signature(executor.test).parameters
|
||||
if "unattended" in test_params:
|
||||
test_kwargs["unattended"] = True
|
||||
if "attempt_tracker" in test_params:
|
||||
test_kwargs["attempt_tracker"] = self.attempt_tracker
|
||||
|
||||
res = executor.test(**test_kwargs)
|
||||
fb_map = getattr(executor, "fallback_used", {})
|
||||
fb_dict = fb_map if isinstance(fb_map, dict) else {}
|
||||
if isinstance(res, dict):
|
||||
self.last_fallback_used = {u: bool(fb_dict.get(u, False)) for u in res}
|
||||
else:
|
||||
self.last_fallback_used = {}
|
||||
return res
|
||||
except ConnpyError:
|
||||
raise
|
||||
except Exception as e: # noqa: BLE001
|
||||
raise_sanitized_auth_error(e, code="AUTH_FAILED", context_label="testing failed")</code></pre>
|
||||
</details>
|
||||
<div class="desc"><p>Run commands and verify expected output on a set of nodes.</p></div>
|
||||
</dd>
|
||||
|
||||
@@ -48,7 +48,7 @@ el.replaceWith(d);
|
||||
<dl>
|
||||
<dt id="connpy.services.import_export_service.ImportExportService"><code class="flex name class">
|
||||
<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>
|
||||
<dd>
|
||||
<details class="source">
|
||||
@@ -58,6 +58,44 @@ el.replaceWith(d);
|
||||
<pre><code class="python">class ImportExportService(BaseService):
|
||||
"""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):
|
||||
"""Export nodes/folders to a YAML file."""
|
||||
if os.path.exists(file_path):
|
||||
@@ -72,16 +110,42 @@ el.replaceWith(d);
|
||||
|
||||
def export_to_dict(self, folders=None):
|
||||
"""Export nodes/folders to a dictionary."""
|
||||
if not folders:
|
||||
return deepcopy(self.config.connections)
|
||||
else:
|
||||
# Validate folders exist
|
||||
for f in folders:
|
||||
if f != "@" and f not in self.config._getallfolders():
|
||||
raise NodeNotFoundError(f"Folder '{f}' not found.")
|
||||
ctx_svc = self._get_context_service()
|
||||
ctx_active = ctx_svc is not None and not ctx_svc.is_default_all_context()
|
||||
|
||||
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)
|
||||
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 = {}
|
||||
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():
|
||||
uniques = self.config._explode_unique(k)
|
||||
if not uniques:
|
||||
@@ -120,28 +184,58 @@ el.replaceWith(d);
|
||||
|
||||
try:
|
||||
with open(file_path, "r") as f:
|
||||
data = yaml.load(f, Loader=yaml.FullLoader)
|
||||
data = yaml.safe_load(f)
|
||||
self.import_from_dict(data)
|
||||
except Exception as e:
|
||||
raise InvalidConfigurationError(f"Failed to read/parse import file: {e}")
|
||||
|
||||
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):
|
||||
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():
|
||||
if k == "type":
|
||||
if k == "type" or not isinstance(v, dict):
|
||||
continue
|
||||
if isinstance(v, dict):
|
||||
node_type = v.get("type", "connection")
|
||||
if node_type == "folder":
|
||||
self.config._folder_add(folder=k)
|
||||
_traverse_import(v, current_folder=k, current_subfolder='')
|
||||
folder_id = f"@{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", k, ""))
|
||||
_plan_import(v, current_folder=k, current_subfolder="")
|
||||
elif node_type == "subfolder":
|
||||
self.config._folder_add(folder=current_folder, subfolder=k)
|
||||
_traverse_import(v, current_folder=current_folder, current_subfolder=k)
|
||||
folder_id = f"@{k}@{current_folder}"
|
||||
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":
|
||||
unique_id = k
|
||||
if current_subfolder:
|
||||
@@ -149,24 +243,47 @@ el.replaceWith(d);
|
||||
elif current_folder:
|
||||
unique_id = f"{k}@{current_folder}"
|
||||
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['id'] = k
|
||||
kwargs['folder'] = current_folder
|
||||
kwargs['subfolder'] = current_subfolder
|
||||
kwargs["id"] = k
|
||||
kwargs["folder"] = current_folder
|
||||
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:
|
||||
# Invalid format skip
|
||||
pass
|
||||
cfg._connections_add_unlocked(**op[1])
|
||||
|
||||
_traverse_import(data)
|
||||
self.config._saveconfig(self.config.file)</code></pre>
|
||||
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)
|
||||
except BaseException:
|
||||
self.config.connections.clear()
|
||||
self.config.connections.update(snap_connections)
|
||||
raise</code></pre>
|
||||
</details>
|
||||
<div class="desc"><p>Business logic for YAML/JSON inventory import and export.</p>
|
||||
<p>Initialize the service.</p>
|
||||
<h2 id="args">Args</h2>
|
||||
<dl>
|
||||
<dt>Args</dt>
|
||||
<dt>-----=</dt>
|
||||
<dt><strong><code>config</code></strong></dt>
|
||||
<dd>An instance of configfile (or None to instantiate a new one/use global context).</dd>
|
||||
</dl></div>
|
||||
@@ -186,16 +303,42 @@ el.replaceWith(d);
|
||||
</summary>
|
||||
<pre><code class="python">def export_to_dict(self, folders=None):
|
||||
"""Export nodes/folders to a dictionary."""
|
||||
if not folders:
|
||||
return deepcopy(self.config.connections)
|
||||
else:
|
||||
# Validate folders exist
|
||||
for f in folders:
|
||||
if f != "@" and f not in self.config._getallfolders():
|
||||
raise NodeNotFoundError(f"Folder '{f}' not found.")
|
||||
ctx_svc = self._get_context_service()
|
||||
ctx_active = ctx_svc is not None and not ctx_svc.is_default_all_context()
|
||||
|
||||
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)
|
||||
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 = {}
|
||||
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():
|
||||
uniques = self.config._explode_unique(k)
|
||||
if not uniques:
|
||||
@@ -260,22 +403,52 @@ el.replaceWith(d);
|
||||
<span>Expand source code</span>
|
||||
</summary>
|
||||
<pre><code class="python">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):
|
||||
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():
|
||||
if k == "type":
|
||||
if k == "type" or not isinstance(v, dict):
|
||||
continue
|
||||
if isinstance(v, dict):
|
||||
node_type = v.get("type", "connection")
|
||||
if node_type == "folder":
|
||||
self.config._folder_add(folder=k)
|
||||
_traverse_import(v, current_folder=k, current_subfolder='')
|
||||
folder_id = f"@{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", k, ""))
|
||||
_plan_import(v, current_folder=k, current_subfolder="")
|
||||
elif node_type == "subfolder":
|
||||
self.config._folder_add(folder=current_folder, subfolder=k)
|
||||
_traverse_import(v, current_folder=current_folder, current_subfolder=k)
|
||||
folder_id = f"@{k}@{current_folder}"
|
||||
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":
|
||||
unique_id = k
|
||||
if current_subfolder:
|
||||
@@ -283,21 +456,43 @@ el.replaceWith(d);
|
||||
elif current_folder:
|
||||
unique_id = f"{k}@{current_folder}"
|
||||
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['id'] = k
|
||||
kwargs['folder'] = current_folder
|
||||
kwargs['subfolder'] = current_subfolder
|
||||
kwargs["id"] = k
|
||||
kwargs["folder"] = current_folder
|
||||
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:
|
||||
# Invalid format skip
|
||||
pass
|
||||
cfg._connections_add_unlocked(**op[1])
|
||||
|
||||
_traverse_import(data)
|
||||
self.config._saveconfig(self.config.file)</code></pre>
|
||||
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)
|
||||
except BaseException:
|
||||
self.config.connections.clear()
|
||||
self.config.connections.update(snap_connections)
|
||||
raise</code></pre>
|
||||
</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>
|
||||
<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>
|
||||
@@ -314,7 +509,7 @@ el.replaceWith(d);
|
||||
|
||||
try:
|
||||
with open(file_path, "r") as f:
|
||||
data = yaml.load(f, Loader=yaml.FullLoader)
|
||||
data = yaml.safe_load(f)
|
||||
self.import_from_dict(data)
|
||||
except Exception as e:
|
||||
raise InvalidConfigurationError(f"Failed to read/parse import file: {e}")</code></pre>
|
||||
|
||||
+1654
-346
File diff suppressed because it is too large.
Load diff
@@ -48,7 +48,7 @@ el.replaceWith(d);
|
||||
<dl>
|
||||
<dt id="connpy.services.node_service.NodeService"><code class="flex name class">
|
||||
<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>
|
||||
<dd>
|
||||
<details class="source">
|
||||
@@ -56,8 +56,148 @@ el.replaceWith(d);
|
||||
<span>Expand source code</span>
|
||||
</summary>
|
||||
<pre><code class="python">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)
|
||||
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):
|
||||
@@ -69,6 +209,10 @@ el.replaceWith(d);
|
||||
flags = re.IGNORECASE if not case_sensitive else 0
|
||||
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:
|
||||
return nodes
|
||||
|
||||
@@ -125,17 +269,21 @@ el.replaceWith(d);
|
||||
else:
|
||||
flags = re.IGNORECASE if not case_sensitive else 0
|
||||
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
|
||||
|
||||
def get_node_details(self, unique_id):
|
||||
"""Return full configuration dictionary for a specific node."""
|
||||
self._ensure_in_active_context(unique_id, is_folder=False)
|
||||
try:
|
||||
details = self.config.getitem(unique_id)
|
||||
if not details:
|
||||
raise NodeNotFoundError(f"Node '{unique_id}' not found.")
|
||||
return details
|
||||
except (KeyError, TypeError):
|
||||
raise NodeNotFoundError(f"Node '{unique_id}' not found.")
|
||||
raise NodeNotFoundError(f"Node '{unique_id}' not found.") from None
|
||||
|
||||
def explode_unique(self, unique_id):
|
||||
"""Explode a unique ID into a dictionary of its parts."""
|
||||
@@ -146,25 +294,54 @@ el.replaceWith(d);
|
||||
self.config._generate_nodes_cache(nodes=nodes, folders=folders, profiles=profiles)
|
||||
|
||||
def validate_parent_folder(self, unique_id, is_folder=False):
|
||||
"""Check if parent folder exists for a given node unique ID."""
|
||||
if is_folder:
|
||||
uniques = self.config._explode_unique(unique_id)
|
||||
if uniques and "subfolder" in uniques and "folder" in uniques:
|
||||
parent_folder = f"@{uniques['folder']}"
|
||||
if parent_folder not in self.config._getallfolders():
|
||||
"""Check if unique_id is in active context and its parent folder exists."""
|
||||
parent_folder = self._derive_parent_folder(self.config, unique_id, is_folder=is_folder)
|
||||
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 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.")
|
||||
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):
|
||||
"""Logic for adding a new node or folder to configuration."""
|
||||
if not is_folder:
|
||||
self._validate_node_name(unique_id)
|
||||
self._validate_node_payload(data)
|
||||
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_folders = self.config._getallfolders()
|
||||
@@ -176,7 +353,6 @@ el.replaceWith(d);
|
||||
if not uniques:
|
||||
raise InvalidConfigurationError(f"Invalid folder name '{unique_id}'.")
|
||||
|
||||
# Check if parent folder exists when creating a subfolder
|
||||
if "subfolder" in uniques:
|
||||
self.validate_parent_folder(unique_id, is_folder=True)
|
||||
|
||||
@@ -186,37 +362,75 @@ el.replaceWith(d);
|
||||
if unique_id in all_nodes:
|
||||
raise NodeAlreadyExistsError(f"Node '{unique_id}' already exists.")
|
||||
|
||||
# Check if parent folder exists when creating a node in a folder
|
||||
self.validate_parent_folder(unique_id)
|
||||
|
||||
# Ensure 'id' is in data for config._connections_add
|
||||
if "id" not in data:
|
||||
payload = deepcopy(data)
|
||||
if "id" not in payload:
|
||||
uniques = self.config._explode_unique(unique_id)
|
||||
if uniques and "id" in uniques:
|
||||
data["id"] = uniques["id"]
|
||||
payload["id"] = uniques["id"]
|
||||
|
||||
self.config._connections_add(**data)
|
||||
self.config._connections_add(**payload)
|
||||
self.config._saveconfig(self.config.file)
|
||||
|
||||
def _build_update_payload(self, unique_id, data, existing, uniques):
|
||||
if isinstance(existing, dict):
|
||||
merged = deepcopy(existing)
|
||||
if isinstance(uniques, dict):
|
||||
merged.update({k: v for k, v in uniques.items() if v})
|
||||
merged.update(deepcopy(data))
|
||||
filtered = {k: v for k, v in merged.items() if k in self._CONNECTION_FIELDS}
|
||||
else:
|
||||
filtered = deepcopy(data)
|
||||
if "id" not in filtered and isinstance(uniques, dict) and "id" in uniques:
|
||||
filtered["id"] = uniques["id"]
|
||||
self._validate_node_payload(filtered)
|
||||
return filtered
|
||||
|
||||
def update_node(self, unique_id, data, save=True):
|
||||
"""Explicitly update an existing node."""
|
||||
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()
|
||||
if unique_id not in all_nodes:
|
||||
raise NodeNotFoundError(f"Node '{unique_id}' not found.")
|
||||
|
||||
# Ensure 'id' is in data for config._connections_add
|
||||
if "id" not in data:
|
||||
uniques = self.config._explode_unique(unique_id)
|
||||
if uniques:
|
||||
data["id"] = uniques["id"]
|
||||
|
||||
# config._connections_add actually handles updates if ID exists correctly
|
||||
self.config._connections_add(**data)
|
||||
existing = self.config.getitem(unique_id, extract=False)
|
||||
payload = self._build_update_payload(unique_id, data, existing, uniques)
|
||||
self.config._connections_add(**payload)
|
||||
if save:
|
||||
self.config._saveconfig(self.config.file)
|
||||
|
||||
def delete_node(self, unique_id, is_folder=False, save=True):
|
||||
"""Logic for deleting a node or folder."""
|
||||
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:
|
||||
uniques = self.config._explode_unique(unique_id)
|
||||
if not uniques:
|
||||
@@ -233,17 +447,35 @@ el.replaceWith(d);
|
||||
|
||||
def connect_node(self, unique_id, sftp=False, debug=False, logger=None):
|
||||
"""Interact with a node directly."""
|
||||
self._ensure_in_active_context(unique_id, is_folder=False)
|
||||
from connpy.core import node
|
||||
|
||||
from ..auth_utils import resolve_fallback_entries
|
||||
from .profile_service import ProfileService
|
||||
|
||||
node_data = self.config.getitem(unique_id, extract=False)
|
||||
if not node_data:
|
||||
raise NodeNotFoundError(f"Node '{unique_id}' not found.")
|
||||
|
||||
# Resolve profiles
|
||||
profile_service = ProfileService(self.config)
|
||||
resolved_data = profile_service.resolve_node_data(node_data)
|
||||
|
||||
node_params = inspect.signature(node.__init__).parameters
|
||||
if "fallback" not in node_params and "fallback" in resolved_data:
|
||||
raw_fb = resolved_data.pop("fallback", None)
|
||||
if raw_fb not in (None, "", []):
|
||||
resolved_fb = resolve_fallback_entries(raw_fb, self.config.profiles)
|
||||
if resolved_fb:
|
||||
tags_val = resolved_data.get("tags", "")
|
||||
if isinstance(tags_val, dict):
|
||||
tags_copy = deepcopy(tags_val)
|
||||
tags_copy["_fallback"] = resolved_fb
|
||||
resolved_data["tags"] = tags_copy
|
||||
elif tags_val in ("", None):
|
||||
resolved_data["tags"] = {"_fallback": resolved_fb}
|
||||
else:
|
||||
resolved_data["tags"] = {"_raw": tags_val, "_fallback": resolved_fb}
|
||||
|
||||
n = node(unique_id, **resolved_data, config=self.config)
|
||||
if sftp:
|
||||
n.protocol = "sftp"
|
||||
@@ -252,7 +484,48 @@ el.replaceWith(d);
|
||||
|
||||
def move_node(self, src_id, dst_id, copy=False):
|
||||
"""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_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)
|
||||
if not node_data:
|
||||
@@ -278,6 +551,55 @@ el.replaceWith(d);
|
||||
|
||||
def bulk_add(self, ids, hosts, common_data):
|
||||
"""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
|
||||
all_nodes = self.config._getallnodes()
|
||||
|
||||
@@ -288,8 +610,6 @@ el.replaceWith(d);
|
||||
try:
|
||||
self._validate_node_name(uid)
|
||||
except ReservedNameError:
|
||||
# For bulk, we might want to just skip or log.
|
||||
# CLI caller will handle if it wants to be strict.
|
||||
continue
|
||||
|
||||
host = hosts[i] if i < len(hosts) else hosts[0]
|
||||
@@ -313,6 +633,21 @@ el.replaceWith(d);
|
||||
|
||||
def full_replace(self, connections, profiles):
|
||||
"""Replace all connections and profiles with new data."""
|
||||
from ..auth_utils import SanitizedAuthError
|
||||
|
||||
if _supports_transactional_update(self.config):
|
||||
def _mut(cfg):
|
||||
cfg.connections.clear()
|
||||
cfg.connections.update(deepcopy(connections))
|
||||
cfg.profiles.clear()
|
||||
cfg.profiles.update(deepcopy(profiles))
|
||||
|
||||
try:
|
||||
self.config.transactional_update(_mut)
|
||||
except (ValueError, TypeError, SanitizedAuthError) as exc:
|
||||
raise InvalidConfigurationError(str(exc)) from None
|
||||
return
|
||||
|
||||
self.config.connections = connections
|
||||
self.config.profiles = profiles
|
||||
self.config._saveconfig(self.config.file)
|
||||
@@ -326,8 +661,9 @@ el.replaceWith(d);
|
||||
</details>
|
||||
<div class="desc"><p>Base class for all connpy services, providing common configuration access.</p>
|
||||
<p>Initialize the service.</p>
|
||||
<h2 id="args">Args</h2>
|
||||
<dl>
|
||||
<dt>Args</dt>
|
||||
<dt>-----=</dt>
|
||||
<dt><strong><code>config</code></strong></dt>
|
||||
<dd>An instance of configfile (or None to instantiate a new one/use global context).</dd>
|
||||
</dl></div>
|
||||
@@ -349,6 +685,40 @@ el.replaceWith(d);
|
||||
"""Logic for adding a new node or folder to configuration."""
|
||||
if not is_folder:
|
||||
self._validate_node_name(unique_id)
|
||||
self._validate_node_payload(data)
|
||||
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_folders = self.config._getallfolders()
|
||||
@@ -360,7 +730,6 @@ el.replaceWith(d);
|
||||
if not uniques:
|
||||
raise InvalidConfigurationError(f"Invalid folder name '{unique_id}'.")
|
||||
|
||||
# Check if parent folder exists when creating a subfolder
|
||||
if "subfolder" in uniques:
|
||||
self.validate_parent_folder(unique_id, is_folder=True)
|
||||
|
||||
@@ -370,16 +739,15 @@ el.replaceWith(d);
|
||||
if unique_id in all_nodes:
|
||||
raise NodeAlreadyExistsError(f"Node '{unique_id}' already exists.")
|
||||
|
||||
# Check if parent folder exists when creating a node in a folder
|
||||
self.validate_parent_folder(unique_id)
|
||||
|
||||
# Ensure 'id' is in data for config._connections_add
|
||||
if "id" not in data:
|
||||
payload = deepcopy(data)
|
||||
if "id" not in payload:
|
||||
uniques = self.config._explode_unique(unique_id)
|
||||
if uniques and "id" in uniques:
|
||||
data["id"] = uniques["id"]
|
||||
payload["id"] = uniques["id"]
|
||||
|
||||
self.config._connections_add(**data)
|
||||
self.config._connections_add(**payload)
|
||||
self.config._saveconfig(self.config.file)</code></pre>
|
||||
</details>
|
||||
<div class="desc"><p>Logic for adding a new node or folder to configuration.</p></div>
|
||||
@@ -394,6 +762,55 @@ el.replaceWith(d);
|
||||
</summary>
|
||||
<pre><code class="python">def bulk_add(self, ids, hosts, common_data):
|
||||
"""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
|
||||
all_nodes = self.config._getallnodes()
|
||||
|
||||
@@ -404,8 +821,6 @@ el.replaceWith(d);
|
||||
try:
|
||||
self._validate_node_name(uid)
|
||||
except ReservedNameError:
|
||||
# For bulk, we might want to just skip or log.
|
||||
# CLI caller will handle if it wants to be strict.
|
||||
continue
|
||||
|
||||
host = hosts[i] if i < len(hosts) else hosts[0]
|
||||
@@ -439,17 +854,35 @@ el.replaceWith(d);
|
||||
</summary>
|
||||
<pre><code class="python">def connect_node(self, unique_id, sftp=False, debug=False, logger=None):
|
||||
"""Interact with a node directly."""
|
||||
self._ensure_in_active_context(unique_id, is_folder=False)
|
||||
from connpy.core import node
|
||||
|
||||
from ..auth_utils import resolve_fallback_entries
|
||||
from .profile_service import ProfileService
|
||||
|
||||
node_data = self.config.getitem(unique_id, extract=False)
|
||||
if not node_data:
|
||||
raise NodeNotFoundError(f"Node '{unique_id}' not found.")
|
||||
|
||||
# Resolve profiles
|
||||
profile_service = ProfileService(self.config)
|
||||
resolved_data = profile_service.resolve_node_data(node_data)
|
||||
|
||||
node_params = inspect.signature(node.__init__).parameters
|
||||
if "fallback" not in node_params and "fallback" in resolved_data:
|
||||
raw_fb = resolved_data.pop("fallback", None)
|
||||
if raw_fb not in (None, "", []):
|
||||
resolved_fb = resolve_fallback_entries(raw_fb, self.config.profiles)
|
||||
if resolved_fb:
|
||||
tags_val = resolved_data.get("tags", "")
|
||||
if isinstance(tags_val, dict):
|
||||
tags_copy = deepcopy(tags_val)
|
||||
tags_copy["_fallback"] = resolved_fb
|
||||
resolved_data["tags"] = tags_copy
|
||||
elif tags_val in ("", None):
|
||||
resolved_data["tags"] = {"_fallback": resolved_fb}
|
||||
else:
|
||||
resolved_data["tags"] = {"_raw": tags_val, "_fallback": resolved_fb}
|
||||
|
||||
n = node(unique_id, **resolved_data, config=self.config)
|
||||
if sftp:
|
||||
n.protocol = "sftp"
|
||||
@@ -468,6 +901,21 @@ el.replaceWith(d);
|
||||
</summary>
|
||||
<pre><code class="python">def delete_node(self, unique_id, is_folder=False, save=True):
|
||||
"""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:
|
||||
uniques = self.config._explode_unique(unique_id)
|
||||
if not uniques:
|
||||
@@ -508,6 +956,21 @@ el.replaceWith(d);
|
||||
</summary>
|
||||
<pre><code class="python">def full_replace(self, connections, profiles):
|
||||
"""Replace all connections and profiles with new data."""
|
||||
from ..auth_utils import SanitizedAuthError
|
||||
|
||||
if _supports_transactional_update(self.config):
|
||||
def _mut(cfg):
|
||||
cfg.connections.clear()
|
||||
cfg.connections.update(deepcopy(connections))
|
||||
cfg.profiles.clear()
|
||||
cfg.profiles.update(deepcopy(profiles))
|
||||
|
||||
try:
|
||||
self.config.transactional_update(_mut)
|
||||
except (ValueError, TypeError, SanitizedAuthError) as exc:
|
||||
raise InvalidConfigurationError(str(exc)) from None
|
||||
return
|
||||
|
||||
self.config.connections = connections
|
||||
self.config.profiles = profiles
|
||||
self.config._saveconfig(self.config.file)</code></pre>
|
||||
@@ -555,13 +1018,14 @@ el.replaceWith(d);
|
||||
</summary>
|
||||
<pre><code class="python">def get_node_details(self, unique_id):
|
||||
"""Return full configuration dictionary for a specific node."""
|
||||
self._ensure_in_active_context(unique_id, is_folder=False)
|
||||
try:
|
||||
details = self.config.getitem(unique_id)
|
||||
if not details:
|
||||
raise NodeNotFoundError(f"Node '{unique_id}' not found.")
|
||||
return details
|
||||
except (KeyError, TypeError):
|
||||
raise NodeNotFoundError(f"Node '{unique_id}' not found.")</code></pre>
|
||||
raise NodeNotFoundError(f"Node '{unique_id}' not found.") from None</code></pre>
|
||||
</details>
|
||||
<div class="desc"><p>Return full configuration dictionary for a specific node.</p></div>
|
||||
</dd>
|
||||
@@ -587,6 +1051,9 @@ el.replaceWith(d);
|
||||
else:
|
||||
flags = re.IGNORECASE if not case_sensitive else 0
|
||||
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>
|
||||
</details>
|
||||
<div class="desc"><p>Return all unique folders, optionally filtered by regex.</p></div>
|
||||
@@ -608,6 +1075,10 @@ el.replaceWith(d);
|
||||
flags = re.IGNORECASE if not case_sensitive else 0
|
||||
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:
|
||||
return nodes
|
||||
|
||||
@@ -662,7 +1133,48 @@ el.replaceWith(d);
|
||||
</summary>
|
||||
<pre><code class="python">def move_node(self, src_id, dst_id, copy=False):
|
||||
"""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_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)
|
||||
if not node_data:
|
||||
@@ -698,18 +1210,28 @@ el.replaceWith(d);
|
||||
</summary>
|
||||
<pre><code class="python">def update_node(self, unique_id, data, save=True):
|
||||
"""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()
|
||||
if unique_id not in all_nodes:
|
||||
raise NodeNotFoundError(f"Node '{unique_id}' not found.")
|
||||
|
||||
# Ensure 'id' is in data for config._connections_add
|
||||
if "id" not in data:
|
||||
uniques = self.config._explode_unique(unique_id)
|
||||
if uniques:
|
||||
data["id"] = uniques["id"]
|
||||
|
||||
# config._connections_add actually handles updates if ID exists correctly
|
||||
self.config._connections_add(**data)
|
||||
existing = self.config.getitem(unique_id, extract=False)
|
||||
payload = self._build_update_payload(unique_id, data, existing, uniques)
|
||||
self.config._connections_add(**payload)
|
||||
if save:
|
||||
self.config._saveconfig(self.config.file)</code></pre>
|
||||
</details>
|
||||
@@ -724,21 +1246,17 @@ el.replaceWith(d);
|
||||
<span>Expand source code</span>
|
||||
</summary>
|
||||
<pre><code class="python">def validate_parent_folder(self, unique_id, is_folder=False):
|
||||
"""Check if parent folder exists for a given node unique ID."""
|
||||
if is_folder:
|
||||
uniques = self.config._explode_unique(unique_id)
|
||||
if uniques and "subfolder" in uniques and "folder" in uniques:
|
||||
parent_folder = f"@{uniques['folder']}"
|
||||
if parent_folder not in self.config._getallfolders():
|
||||
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():
|
||||
"""Check if unique_id is in active context and its parent folder exists."""
|
||||
parent_folder = self._derive_parent_folder(self.config, unique_id, is_folder=is_folder)
|
||||
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 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.")</code></pre>
|
||||
</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>
|
||||
</dl>
|
||||
<h3>Inherited members</h3>
|
||||
|
||||
@@ -411,8 +411,9 @@ el.replaceWith(d);
|
||||
</details>
|
||||
<div class="desc"><p>Business logic for enabling, disabling, and listing plugins.</p>
|
||||
<p>Initialize the service.</p>
|
||||
<h2 id="args">Args</h2>
|
||||
<dl>
|
||||
<dt>Args</dt>
|
||||
<dt>-----=</dt>
|
||||
<dt><strong><code>config</code></strong></dt>
|
||||
<dd>An instance of configfile (or None to instantiate a new one/use global context).</dd>
|
||||
</dl></div>
|
||||
|
||||
@@ -58,61 +58,189 @@ el.replaceWith(d);
|
||||
<pre><code class="python">class ProfileService(BaseService):
|
||||
"""Business logic for node profiles management."""
|
||||
|
||||
_ALLOWED_KEYS: ClassVar[frozenset[str]] = frozenset({
|
||||
"host",
|
||||
"options",
|
||||
"logs",
|
||||
"password",
|
||||
"port",
|
||||
"protocol",
|
||||
"user",
|
||||
"tags",
|
||||
"jumphost",
|
||||
"fallback",
|
||||
})
|
||||
|
||||
@staticmethod
|
||||
def _validate_profile_payload(data):
|
||||
from ..auth_utils import (
|
||||
SanitizedAuthError,
|
||||
is_compose_expression,
|
||||
validate_auth_budget_tags,
|
||||
validate_compose_syntax,
|
||||
validate_fallback_entries,
|
||||
)
|
||||
|
||||
try:
|
||||
if "tags" in data and data["tags"] not in ("", None):
|
||||
validate_auth_budget_tags(data["tags"])
|
||||
if "fallback" in data and data["fallback"] not in (None, "", []):
|
||||
validate_fallback_entries(data["fallback"])
|
||||
pw = data.get("password")
|
||||
if is_compose_expression(pw):
|
||||
validate_compose_syntax(pw)
|
||||
elif isinstance(pw, (list, tuple)):
|
||||
for item in pw:
|
||||
if is_compose_expression(item):
|
||||
validate_compose_syntax(item)
|
||||
except (ValueError, TypeError, SanitizedAuthError) as exc:
|
||||
raise InvalidConfigurationError(str(exc)) from None
|
||||
|
||||
def _get_profiles_snapshot(self):
|
||||
lock = getattr(self.config, "_mutation_lock", None)
|
||||
if lock is not None and not hasattr(self.config, "assert_called"):
|
||||
with lock:
|
||||
return deepcopy(self.config.profiles)
|
||||
return deepcopy(self.config.profiles)
|
||||
|
||||
def list_profiles(self, filter_str=None):
|
||||
"""List all profile names, optionally filtered."""
|
||||
profiles = list(self.config.profiles.keys())
|
||||
profiles = list(self._get_profiles_snapshot().keys())
|
||||
case_sensitive = self.config.config.get("case", False)
|
||||
|
||||
if filter_str:
|
||||
if not case_sensitive:
|
||||
f_str = filter_str.lower()
|
||||
return [p for p in profiles if f_str in p.lower()]
|
||||
else:
|
||||
return [p for p in profiles if filter_str in p]
|
||||
return profiles
|
||||
|
||||
def get_profile(self, name, resolve=True):
|
||||
def get_profile(self, name, resolve=True, *, _visited=frozenset(), _depth=0):
|
||||
"""Get the profile dictionary, optionally resolved."""
|
||||
profile = self.config.profiles.get(name)
|
||||
if _depth >= MAX_PROFILE_DEPTH or name in _visited:
|
||||
raise InvalidConfigurationError(
|
||||
f"Circular or overly deep profile reference involving '{name}'."
|
||||
)
|
||||
profiles = self._get_profiles_snapshot()
|
||||
profile = profiles.get(name)
|
||||
if not profile:
|
||||
raise ProfileNotFoundError(f"Profile '{name}' not found.")
|
||||
|
||||
if resolve:
|
||||
return self.resolve_node_data(profile)
|
||||
return self.resolve_node_data(
|
||||
profile,
|
||||
_visited=_visited | {name},
|
||||
_depth=_depth + 1,
|
||||
_profiles_snapshot=profiles,
|
||||
)
|
||||
return profile
|
||||
|
||||
def add_profile(self, name, data):
|
||||
"""Add a new profile."""
|
||||
filtered_data = {k: deepcopy(v) for k, v in data.items() if k in self._ALLOWED_KEYS}
|
||||
self._validate_profile_payload(filtered_data)
|
||||
|
||||
if _supports_transactional_update(self.config):
|
||||
def _mut(cfg):
|
||||
if name in cfg.profiles:
|
||||
raise ProfileAlreadyExistsError(f"Profile '{name}' already exists.")
|
||||
cfg._profiles_add_unlocked(id=name, **filtered_data)
|
||||
|
||||
self.config.transactional_update(_mut)
|
||||
return
|
||||
|
||||
if name in self.config.profiles:
|
||||
raise ProfileAlreadyExistsError(f"Profile '{name}' already exists.")
|
||||
|
||||
# Filter data to match _profiles_add signature and ensure id is passed
|
||||
allowed_keys = {"host", "options", "logs", "password", "port", "protocol", "user", "tags", "jumphost"}
|
||||
filtered_data = {k: v for k, v in data.items() if k in allowed_keys}
|
||||
|
||||
self.config._profiles_add(id=name, **filtered_data)
|
||||
self.config._saveconfig(self.config.file)
|
||||
|
||||
def resolve_node_data(self, node_data):
|
||||
"""Resolve profile references (@profile) in node data and handle inheritance."""
|
||||
resolved = node_data.copy()
|
||||
def resolve_node_data(
|
||||
self,
|
||||
node_data,
|
||||
*,
|
||||
_visited=frozenset(),
|
||||
_depth=0,
|
||||
_profiles_snapshot=None,
|
||||
):
|
||||
"""Resolve profile references (@profile) in node data while preserving compose and scalar secrets."""
|
||||
from ..auth_utils import is_compose_expression
|
||||
|
||||
# 1. Identify all referenced profiles to support inheritance
|
||||
referenced_profiles = []
|
||||
for value in resolved.values():
|
||||
if isinstance(value, str) and value.startswith("@"):
|
||||
referenced_profiles.append(value[1:])
|
||||
elif isinstance(value, list):
|
||||
if _depth > MAX_PROFILE_DEPTH:
|
||||
raise InvalidConfigurationError("Profile inheritance exceeds maximum depth.")
|
||||
|
||||
profiles = (
|
||||
_profiles_snapshot
|
||||
if _profiles_snapshot is not None
|
||||
else self._get_profiles_snapshot()
|
||||
)
|
||||
resolved = deepcopy(node_data)
|
||||
|
||||
auth_referenced_profiles = []
|
||||
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:
|
||||
if isinstance(item, str) and item.startswith("@"):
|
||||
referenced_profiles.append(item[1:])
|
||||
if (
|
||||
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("@"):
|
||||
profile_name = value[1:]
|
||||
try:
|
||||
profile = self.get_profile(profile_name, resolve=True)
|
||||
profile = _get_resolved_ref(profile_name)
|
||||
resolved[key] = profile.get(key, "")
|
||||
except ProfileNotFoundError:
|
||||
resolved[key] = ""
|
||||
@@ -121,33 +249,29 @@ el.replaceWith(d);
|
||||
for item in value:
|
||||
if isinstance(item, str) and item.startswith("@"):
|
||||
profile_name = item[1:]
|
||||
try:
|
||||
profile = self.get_profile(profile_name, resolve=True)
|
||||
with suppress(ProfileNotFoundError):
|
||||
profile = _get_resolved_ref(profile_name)
|
||||
if "password" in profile:
|
||||
resolved_list.append(profile["password"])
|
||||
except ProfileNotFoundError:
|
||||
pass
|
||||
else:
|
||||
resolved_list.append(item)
|
||||
resolved[key] = resolved_list
|
||||
|
||||
# 3. Inheritance: Fill empty keys from the first referenced profile
|
||||
if referenced_profiles:
|
||||
base_profile_name = referenced_profiles[0]
|
||||
try:
|
||||
base_profile = self.get_profile(base_profile_name, resolve=True)
|
||||
for key, value in base_profile.items():
|
||||
# Fill if key is missing or empty
|
||||
if key not in resolved or resolved[key] == "" or resolved[key] == [] or resolved[key] is None:
|
||||
resolved[key] = value
|
||||
except ProfileNotFoundError:
|
||||
pass
|
||||
dedup_auth_profiles = list(dict.fromkeys(auth_referenced_profiles))
|
||||
for base_profile_name in dedup_auth_profiles:
|
||||
with suppress(ProfileNotFoundError):
|
||||
base_profile = _get_resolved_ref(base_profile_name)
|
||||
for key in ("user", "password", "fallback"):
|
||||
val = base_profile.get(key)
|
||||
if (
|
||||
key not in resolved or resolved[key] in ("", [], None)
|
||||
) and val not in ("", [], None):
|
||||
resolved[key] = deepcopy(val)
|
||||
|
||||
# 4. Handle default protocol
|
||||
if resolved.get("protocol") == "" or resolved.get("protocol") is None:
|
||||
if resolved.get("protocol") in ("", None):
|
||||
try:
|
||||
default_profile = self.get_profile("default", resolve=True)
|
||||
resolved["protocol"] = default_profile.get("protocol", "ssh")
|
||||
default_profile = _get_resolved_ref("default")
|
||||
resolved["protocol"] = default_profile.get("protocol", "ssh") or "ssh"
|
||||
except ProfileNotFoundError:
|
||||
resolved["protocol"] = "ssh"
|
||||
|
||||
@@ -155,41 +279,66 @@ el.replaceWith(d);
|
||||
|
||||
def delete_profile(self, name):
|
||||
"""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.")
|
||||
|
||||
if name == "default":
|
||||
raise InvalidConfigurationError("Cannot delete the 'default' profile.")
|
||||
used_by = cfg._profileused_unlocked(name)
|
||||
if used_by:
|
||||
raise InvalidConfigurationError(
|
||||
f"Profile '{name}' is used by nodes: {', '.join(used_by)}"
|
||||
)
|
||||
cfg._profiles_del_unlocked(id=name)
|
||||
|
||||
self.config.transactional_update(_mut)
|
||||
return
|
||||
|
||||
if name not in self.config.profiles:
|
||||
raise ProfileNotFoundError(f"Profile '{name}' not found.")
|
||||
if name == "default":
|
||||
raise InvalidConfigurationError("Cannot delete the 'default' profile.")
|
||||
used_by = self.config._profileused(name)
|
||||
if used_by:
|
||||
# We return the list of nodes using it so the UI can inform the user
|
||||
raise InvalidConfigurationError(f"Profile '{name}' is used by nodes: {', '.join(used_by)}")
|
||||
|
||||
raise InvalidConfigurationError(
|
||||
f"Profile '{name}' is used by nodes: {', '.join(used_by)}"
|
||||
)
|
||||
self.config._profiles_del(id=name)
|
||||
self.config._saveconfig(self.config.file)
|
||||
|
||||
def update_profile(self, name, data):
|
||||
"""Update an existing profile."""
|
||||
if _supports_transactional_update(self.config):
|
||||
def _mut(cfg):
|
||||
if name not in cfg.profiles:
|
||||
raise ProfileNotFoundError(f"Profile '{name}' not found.")
|
||||
updated_data = deepcopy(cfg.profiles[name])
|
||||
updated_data.update(deepcopy(data))
|
||||
filtered_data = {
|
||||
k: v for k, v in updated_data.items() if k in self._ALLOWED_KEYS
|
||||
}
|
||||
self._validate_profile_payload(filtered_data)
|
||||
cfg._profiles_add_unlocked(id=name, **filtered_data)
|
||||
|
||||
self.config.transactional_update(_mut)
|
||||
return
|
||||
|
||||
if name not in self.config.profiles:
|
||||
raise ProfileNotFoundError(f"Profile '{name}' not found.")
|
||||
|
||||
# Merge with existing data
|
||||
existing = self.get_profile(name, resolve=False)
|
||||
updated_data = existing.copy()
|
||||
updated_data = deepcopy(existing)
|
||||
updated_data.update(data)
|
||||
|
||||
# Filter data to match _profiles_add signature
|
||||
allowed_keys = {"host", "options", "logs", "password", "port", "protocol", "user", "tags", "jumphost"}
|
||||
filtered_data = {k: v for k, v in updated_data.items() if k in allowed_keys}
|
||||
|
||||
filtered_data = {k: v for k, v in updated_data.items() if k in self._ALLOWED_KEYS}
|
||||
self._validate_profile_payload(filtered_data)
|
||||
self.config._profiles_add(id=name, **filtered_data)
|
||||
self.config._saveconfig(self.config.file)</code></pre>
|
||||
</details>
|
||||
<div class="desc"><p>Business logic for node profiles management.</p>
|
||||
<p>Initialize the service.</p>
|
||||
<h2 id="args">Args</h2>
|
||||
<dl>
|
||||
<dt>Args</dt>
|
||||
<dt>-----=</dt>
|
||||
<dt><strong><code>config</code></strong></dt>
|
||||
<dd>An instance of configfile (or None to instantiate a new one/use global context).</dd>
|
||||
</dl></div>
|
||||
@@ -209,13 +358,20 @@ el.replaceWith(d);
|
||||
</summary>
|
||||
<pre><code class="python">def add_profile(self, name, data):
|
||||
"""Add a new profile."""
|
||||
filtered_data = {k: deepcopy(v) for k, v in data.items() if k in self._ALLOWED_KEYS}
|
||||
self._validate_profile_payload(filtered_data)
|
||||
|
||||
if _supports_transactional_update(self.config):
|
||||
def _mut(cfg):
|
||||
if name in cfg.profiles:
|
||||
raise ProfileAlreadyExistsError(f"Profile '{name}' already exists.")
|
||||
cfg._profiles_add_unlocked(id=name, **filtered_data)
|
||||
|
||||
self.config.transactional_update(_mut)
|
||||
return
|
||||
|
||||
if name in self.config.profiles:
|
||||
raise ProfileAlreadyExistsError(f"Profile '{name}' already exists.")
|
||||
|
||||
# Filter data to match _profiles_add signature and ensure id is passed
|
||||
allowed_keys = {"host", "options", "logs", "password", "port", "protocol", "user", "tags", "jumphost"}
|
||||
filtered_data = {k: v for k, v in data.items() if k in allowed_keys}
|
||||
|
||||
self.config._profiles_add(id=name, **filtered_data)
|
||||
self.config._saveconfig(self.config.file)</code></pre>
|
||||
</details>
|
||||
@@ -231,17 +387,31 @@ el.replaceWith(d);
|
||||
</summary>
|
||||
<pre><code class="python">def delete_profile(self, name):
|
||||
"""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.")
|
||||
|
||||
if name == "default":
|
||||
raise InvalidConfigurationError("Cannot delete the 'default' profile.")
|
||||
used_by = cfg._profileused_unlocked(name)
|
||||
if used_by:
|
||||
raise InvalidConfigurationError(
|
||||
f"Profile '{name}' is used by nodes: {', '.join(used_by)}"
|
||||
)
|
||||
cfg._profiles_del_unlocked(id=name)
|
||||
|
||||
self.config.transactional_update(_mut)
|
||||
return
|
||||
|
||||
if name not in self.config.profiles:
|
||||
raise ProfileNotFoundError(f"Profile '{name}' not found.")
|
||||
if name == "default":
|
||||
raise InvalidConfigurationError("Cannot delete the 'default' profile.")
|
||||
used_by = self.config._profileused(name)
|
||||
if used_by:
|
||||
# We return the list of nodes using it so the UI can inform the user
|
||||
raise InvalidConfigurationError(f"Profile '{name}' is used by nodes: {', '.join(used_by)}")
|
||||
|
||||
raise InvalidConfigurationError(
|
||||
f"Profile '{name}' is used by nodes: {', '.join(used_by)}"
|
||||
)
|
||||
self.config._profiles_del(id=name)
|
||||
self.config._saveconfig(self.config.file)</code></pre>
|
||||
</details>
|
||||
@@ -255,14 +425,24 @@ el.replaceWith(d);
|
||||
<summary>
|
||||
<span>Expand source code</span>
|
||||
</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):
|
||||
"""Get the profile dictionary, optionally resolved."""
|
||||
profile = self.config.profiles.get(name)
|
||||
if _depth >= MAX_PROFILE_DEPTH or name in _visited:
|
||||
raise InvalidConfigurationError(
|
||||
f"Circular or overly deep profile reference involving '{name}'."
|
||||
)
|
||||
profiles = self._get_profiles_snapshot()
|
||||
profile = profiles.get(name)
|
||||
if not profile:
|
||||
raise ProfileNotFoundError(f"Profile '{name}' not found.")
|
||||
|
||||
if resolve:
|
||||
return self.resolve_node_data(profile)
|
||||
return self.resolve_node_data(
|
||||
profile,
|
||||
_visited=_visited | {name},
|
||||
_depth=_depth + 1,
|
||||
_profiles_snapshot=profiles,
|
||||
)
|
||||
return profile</code></pre>
|
||||
</details>
|
||||
<div class="desc"><p>Get the profile dictionary, optionally resolved.</p></div>
|
||||
@@ -277,14 +457,13 @@ el.replaceWith(d);
|
||||
</summary>
|
||||
<pre><code class="python">def list_profiles(self, filter_str=None):
|
||||
"""List all profile names, optionally filtered."""
|
||||
profiles = list(self.config.profiles.keys())
|
||||
profiles = list(self._get_profiles_snapshot().keys())
|
||||
case_sensitive = self.config.config.get("case", False)
|
||||
|
||||
if filter_str:
|
||||
if not case_sensitive:
|
||||
f_str = filter_str.lower()
|
||||
return [p for p in profiles if f_str in p.lower()]
|
||||
else:
|
||||
return [p for p in profiles if filter_str in p]
|
||||
return profiles</code></pre>
|
||||
</details>
|
||||
@@ -298,26 +477,93 @@ el.replaceWith(d);
|
||||
<summary>
|
||||
<span>Expand source code</span>
|
||||
</summary>
|
||||
<pre><code class="python">def resolve_node_data(self, node_data):
|
||||
"""Resolve profile references (@profile) in node data and handle inheritance."""
|
||||
resolved = node_data.copy()
|
||||
<pre><code class="python">def resolve_node_data(
|
||||
self,
|
||||
node_data,
|
||||
*,
|
||||
_visited=frozenset(),
|
||||
_depth=0,
|
||||
_profiles_snapshot=None,
|
||||
):
|
||||
"""Resolve profile references (@profile) in node data while preserving compose and scalar secrets."""
|
||||
from ..auth_utils import is_compose_expression
|
||||
|
||||
# 1. Identify all referenced profiles to support inheritance
|
||||
referenced_profiles = []
|
||||
for value in resolved.values():
|
||||
if isinstance(value, str) and value.startswith("@"):
|
||||
referenced_profiles.append(value[1:])
|
||||
elif isinstance(value, list):
|
||||
if _depth > MAX_PROFILE_DEPTH:
|
||||
raise InvalidConfigurationError("Profile inheritance exceeds maximum depth.")
|
||||
|
||||
profiles = (
|
||||
_profiles_snapshot
|
||||
if _profiles_snapshot is not None
|
||||
else self._get_profiles_snapshot()
|
||||
)
|
||||
resolved = deepcopy(node_data)
|
||||
|
||||
auth_referenced_profiles = []
|
||||
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:
|
||||
if isinstance(item, str) and item.startswith("@"):
|
||||
referenced_profiles.append(item[1:])
|
||||
if (
|
||||
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("@"):
|
||||
profile_name = value[1:]
|
||||
try:
|
||||
profile = self.get_profile(profile_name, resolve=True)
|
||||
profile = _get_resolved_ref(profile_name)
|
||||
resolved[key] = profile.get(key, "")
|
||||
except ProfileNotFoundError:
|
||||
resolved[key] = ""
|
||||
@@ -326,39 +572,35 @@ el.replaceWith(d);
|
||||
for item in value:
|
||||
if isinstance(item, str) and item.startswith("@"):
|
||||
profile_name = item[1:]
|
||||
try:
|
||||
profile = self.get_profile(profile_name, resolve=True)
|
||||
with suppress(ProfileNotFoundError):
|
||||
profile = _get_resolved_ref(profile_name)
|
||||
if "password" in profile:
|
||||
resolved_list.append(profile["password"])
|
||||
except ProfileNotFoundError:
|
||||
pass
|
||||
else:
|
||||
resolved_list.append(item)
|
||||
resolved[key] = resolved_list
|
||||
|
||||
# 3. Inheritance: Fill empty keys from the first referenced profile
|
||||
if referenced_profiles:
|
||||
base_profile_name = referenced_profiles[0]
|
||||
try:
|
||||
base_profile = self.get_profile(base_profile_name, resolve=True)
|
||||
for key, value in base_profile.items():
|
||||
# Fill if key is missing or empty
|
||||
if key not in resolved or resolved[key] == "" or resolved[key] == [] or resolved[key] is None:
|
||||
resolved[key] = value
|
||||
except ProfileNotFoundError:
|
||||
pass
|
||||
dedup_auth_profiles = list(dict.fromkeys(auth_referenced_profiles))
|
||||
for base_profile_name in dedup_auth_profiles:
|
||||
with suppress(ProfileNotFoundError):
|
||||
base_profile = _get_resolved_ref(base_profile_name)
|
||||
for key in ("user", "password", "fallback"):
|
||||
val = base_profile.get(key)
|
||||
if (
|
||||
key not in resolved or resolved[key] in ("", [], None)
|
||||
) and val not in ("", [], None):
|
||||
resolved[key] = deepcopy(val)
|
||||
|
||||
# 4. Handle default protocol
|
||||
if resolved.get("protocol") == "" or resolved.get("protocol") is None:
|
||||
if resolved.get("protocol") in ("", None):
|
||||
try:
|
||||
default_profile = self.get_profile("default", resolve=True)
|
||||
resolved["protocol"] = default_profile.get("protocol", "ssh")
|
||||
default_profile = _get_resolved_ref("default")
|
||||
resolved["protocol"] = default_profile.get("protocol", "ssh") or "ssh"
|
||||
except ProfileNotFoundError:
|
||||
resolved["protocol"] = "ssh"
|
||||
|
||||
return resolved</code></pre>
|
||||
</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>
|
||||
<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>
|
||||
@@ -370,18 +612,28 @@ el.replaceWith(d);
|
||||
</summary>
|
||||
<pre><code class="python">def update_profile(self, name, data):
|
||||
"""Update an existing profile."""
|
||||
if _supports_transactional_update(self.config):
|
||||
def _mut(cfg):
|
||||
if name not in cfg.profiles:
|
||||
raise ProfileNotFoundError(f"Profile '{name}' not found.")
|
||||
updated_data = deepcopy(cfg.profiles[name])
|
||||
updated_data.update(deepcopy(data))
|
||||
filtered_data = {
|
||||
k: v for k, v in updated_data.items() if k in self._ALLOWED_KEYS
|
||||
}
|
||||
self._validate_profile_payload(filtered_data)
|
||||
cfg._profiles_add_unlocked(id=name, **filtered_data)
|
||||
|
||||
self.config.transactional_update(_mut)
|
||||
return
|
||||
|
||||
if name not in self.config.profiles:
|
||||
raise ProfileNotFoundError(f"Profile '{name}' not found.")
|
||||
|
||||
# Merge with existing data
|
||||
existing = self.get_profile(name, resolve=False)
|
||||
updated_data = existing.copy()
|
||||
updated_data = deepcopy(existing)
|
||||
updated_data.update(data)
|
||||
|
||||
# Filter data to match _profiles_add signature
|
||||
allowed_keys = {"host", "options", "logs", "password", "port", "protocol", "user", "tags", "jumphost"}
|
||||
filtered_data = {k: v for k, v in updated_data.items() if k in allowed_keys}
|
||||
|
||||
filtered_data = {k: v for k, v in updated_data.items() if k in self._ALLOWED_KEYS}
|
||||
self._validate_profile_payload(filtered_data)
|
||||
self.config._profiles_add(id=name, **filtered_data)
|
||||
self.config._saveconfig(self.config.file)</code></pre>
|
||||
</details>
|
||||
|
||||
@@ -65,7 +65,7 @@ el.replaceWith(d);
|
||||
</dd>
|
||||
<dt id="connpy.services.provider.ServiceProvider"><code class="flex name class">
|
||||
<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>
|
||||
<dd>
|
||||
<details class="source">
|
||||
@@ -75,16 +75,22 @@ el.replaceWith(d);
|
||||
<pre><code class="python">class ServiceProvider:
|
||||
"""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.config = config
|
||||
self.remote_host = remote_host
|
||||
self.enforce_context = bool(enforce_context)
|
||||
self.auth_tracker = AuthAttemptTracker()
|
||||
self.nodes = None
|
||||
self._system = None
|
||||
self._execution = None
|
||||
self._import_export = None
|
||||
self._sync = None
|
||||
self._users = None
|
||||
self._ai = None
|
||||
self.set_enforce_context(self.enforce_context)
|
||||
|
||||
if mode == "local":
|
||||
self._init_local()
|
||||
@@ -93,14 +99,26 @@ el.replaceWith(d);
|
||||
else:
|
||||
raise ValueError(f"Unknown service mode: {mode}")
|
||||
|
||||
def _init_local(self):
|
||||
from .node_service import NodeService
|
||||
from .profile_service import ProfileService
|
||||
from .config_service import ConfigService
|
||||
from .plugin_service import PluginService
|
||||
from .context_service import ContextService
|
||||
def set_enforce_context(self, enforce_context: bool) -> None:
|
||||
self.enforce_context = bool(enforce_context)
|
||||
if self.config is not None and not hasattr(self.config, "assert_called"):
|
||||
self.config._enforce_context = self.enforce_context
|
||||
for svc in (
|
||||
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.config_svc = ConfigService(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
|
||||
from .config_service import ConfigService
|
||||
from .context_service import ContextService
|
||||
|
||||
self.config_svc = ConfigService(self.config)
|
||||
self.context = ContextService(self.config)
|
||||
|
||||
if not self.remote_host:
|
||||
raise InvalidConfigurationError("Remote host must be specified in remote mode")
|
||||
|
||||
import grpc
|
||||
import os
|
||||
|
||||
import grpc
|
||||
|
||||
from ..grpc_layer.stubs import (
|
||||
NodeStub, ProfileStub, PluginStub, AIStub,
|
||||
ExecutionStub, ImportExportStub, SystemStub,
|
||||
ConfigStub, AuthClientInterceptor, AuthStub
|
||||
AIStub,
|
||||
AuthClientInterceptor,
|
||||
AuthStub,
|
||||
ConfigStub,
|
||||
ExecutionStub,
|
||||
ImportExportStub,
|
||||
NodeStub,
|
||||
PluginStub,
|
||||
ProfileStub,
|
||||
SystemStub,
|
||||
)
|
||||
|
||||
def get_token():
|
||||
@@ -130,11 +158,8 @@ el.replaceWith(d);
|
||||
return env_token
|
||||
token_path = os.path.join(self.config.defaultdir, ".token")
|
||||
if os.path.exists(token_path):
|
||||
try:
|
||||
with open(token_path, "r") as f:
|
||||
with suppress(OSError, UnicodeDecodeError), open(token_path) as f:
|
||||
return f.read().strip()
|
||||
except Exception:
|
||||
pass
|
||||
return None
|
||||
|
||||
channel = grpc.insecure_channel(self.remote_host)
|
||||
@@ -151,7 +176,12 @@ el.replaceWith(d);
|
||||
self.plugins = PluginStub(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.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.auth = AuthStub(channel, remote_host=self.remote_host)
|
||||
|
||||
@@ -170,7 +200,13 @@ el.replaceWith(d);
|
||||
def execution(self):
|
||||
if self._execution is None and self.mode == "local":
|
||||
from .execution_service import ExecutionService
|
||||
self._execution = ExecutionService(self.config)
|
||||
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
|
||||
|
||||
@execution.setter
|
||||
@@ -181,6 +217,12 @@ el.replaceWith(d);
|
||||
def import_export(self):
|
||||
if self._import_export is None and self.mode == "local":
|
||||
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)
|
||||
return self._import_export
|
||||
|
||||
@@ -249,7 +291,13 @@ def ai(self):
|
||||
def execution(self):
|
||||
if self._execution is None and self.mode == "local":
|
||||
from .execution_service import ExecutionService
|
||||
self._execution = ExecutionService(self.config)
|
||||
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</code></pre>
|
||||
</details>
|
||||
<div class="desc"></div>
|
||||
@@ -264,6 +312,12 @@ def execution(self):
|
||||
def import_export(self):
|
||||
if self._import_export is None and self.mode == "local":
|
||||
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)
|
||||
return self._import_export</code></pre>
|
||||
</details>
|
||||
@@ -315,6 +369,31 @@ def users(self):
|
||||
<div class="desc"></div>
|
||||
</dd>
|
||||
</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) -> None:
|
||||
self.enforce_context = bool(enforce_context)
|
||||
if self.config is not None and not hasattr(self.config, "assert_called"):
|
||||
self.config._enforce_context = self.enforce_context
|
||||
for svc in (
|
||||
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</code></pre>
|
||||
</details>
|
||||
<div class="desc"></div>
|
||||
</dd>
|
||||
</dl>
|
||||
</dd>
|
||||
</dl>
|
||||
</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.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.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.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>
|
||||
|
||||
@@ -435,8 +435,9 @@ el.replaceWith(d);
|
||||
</details>
|
||||
<div class="desc"><p>Business logic for Google Drive synchronization.</p>
|
||||
<p>Initialize the service.</p>
|
||||
<h2 id="args">Args</h2>
|
||||
<dl>
|
||||
<dt>Args</dt>
|
||||
<dt>-----=</dt>
|
||||
<dt><strong><code>config</code></strong></dt>
|
||||
<dd>An instance of configfile (or None to instantiate a new one/use global context).</dd>
|
||||
</dl></div>
|
||||
|
||||
@@ -142,8 +142,9 @@ el.replaceWith(d);
|
||||
</details>
|
||||
<div class="desc"><p>Business logic for application lifecycle (API, processes).</p>
|
||||
<p>Initialize the service.</p>
|
||||
<h2 id="args">Args</h2>
|
||||
<dl>
|
||||
<dt>Args</dt>
|
||||
<dt>-----=</dt>
|
||||
<dt><strong><code>config</code></strong></dt>
|
||||
<dd>An instance of configfile (or None to instantiate a new one/use global context).</dd>
|
||||
</dl></div>
|
||||
|
||||
Reference in new issue
Block a user