Compare commits
19
Commits
951961a5d3
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
693531ed57 | ||
|
|
b3925fee07 | ||
|
|
1b50c7d3e7 | ||
|
|
fc27747c19 | ||
|
|
175e623d99 | ||
|
|
ecaa836b98 | ||
|
|
f86ce21ee0 | ||
|
|
dddeaf4b03 | ||
|
|
441bf54ac1 | ||
|
|
c6f260efc9 | ||
|
|
d2da49221f | ||
|
|
59640d20a4 | ||
|
|
ad9dd27316 | ||
|
|
89ca1ec621 | ||
|
|
d96d8fb1fa | ||
|
|
79d28ee6c3 | ||
|
|
ef6d1da5e5 | ||
|
|
8e388cc6b6 | ||
|
|
c3995ea6a9 |
No files matched your search
@@ -3,7 +3,7 @@
|
|||||||
</p>
|
</p>
|
||||||
|
|
||||||
|
|
||||||
# Connpy (v6.4.0b4)
|
# Connpy (v6.5.0b1)
|
||||||
[](https://pypi.org/pypi/connpy/)
|
[](https://pypi.org/pypi/connpy/)
|
||||||
[](https://pypi.org/pypi/connpy/)
|
[](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" }
|
tags: { "os": "cisco_ios", "prompt": ".*#", "screen_length_command": "terminal length 0" }
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### 3f. Credential Fallback Chain, `compose:` & Auth Budgets
|
||||||
|
Configure automatic backup credentials (`fallback`) on nodes or `@profile` templates so both interactive (`conn <node>`) and unattended (`conn run` / MCP) sessions switch seamlessly when primary TACACS+/AAA authentication fails (opt-out via `tags.allow_fallback: false`).
|
||||||
|
* **Fallback Formats**: Accepts `@profile`, explicit JSON list/dict entries (`[{"user": "backup", "password": "..."}]`), or `compose:` expressions. Nodes referencing multiple profiles inherit `fallback` with priority from `user`/`password` `@profile` references.
|
||||||
|
* **Password Composition (`compose:`)**: Concatenate secrets and literals explicitly using `compose:@prof1+@prof2+"suffix"` (use `@@` to escape leading literal `@`).
|
||||||
|
* **Anti-Lockout Guardrails (`conn config`)**: Enforce strict attempt bounds via `--max-auth-prompts`, `--max-total-auth-attempts`, `--max-hop-host-attempts`, `--max-global-user-attempts`, and `--disable-fallback-on-insecure-hostkey`.
|
||||||
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -157,6 +163,9 @@ Support for single or chained intermediate gateway nodes (SSH, Mosh, SSM, kubect
|
|||||||
### 4c. Debug Mode, Keepalive, Logging
|
### 4c. Debug Mode, Keepalive, Logging
|
||||||
Track connection steps (`conn --debug node`), set idle keepalive intervals (`conn config --keepalive <seconds>`), or define dynamic output log files using variables like `${unique}`, `${host}`, `${port}`, `${user}`, `${protocol}`, or `${date 'format'}`.
|
Track connection steps (`conn --debug node`), set idle keepalive intervals (`conn config --keepalive <seconds>`), or define dynamic output log files using variables like `${unique}`, `${host}`, `${port}`, `${user}`, `${protocol}`, or `${date 'format'}`.
|
||||||
|
|
||||||
|
### 4d. Adaptive Multi-Hop Timeouts
|
||||||
|
Differentiate `--connect-timeout` and `--auth-timeout` stages (`conn run` / `conn config`), automatically computing topological per-hop latency budgets for chained `SSM` and `Jumphost` tunnels with bounded progress extensions on slow handshakes.
|
||||||
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|||||||
+10
-1
@@ -5,7 +5,7 @@
|
|||||||
</p>
|
</p>
|
||||||
|
|
||||||
|
|
||||||
# Connpy (v6.4.0b4)
|
# Connpy (v6.5.0b1)
|
||||||
[](https://pypi.org/pypi/connpy/)
|
[](https://pypi.org/pypi/connpy/)
|
||||||
[](https://pypi.org/pypi/connpy/)
|
[](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" }
|
tags: { "os": "cisco_ios", "prompt": ".*#", "screen_length_command": "terminal length 0" }
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### 3f. Credential Fallback Chain, `compose:` & Auth Budgets
|
||||||
|
Configure automatic backup credentials (`fallback`) on nodes or `@profile` templates so both interactive (`conn <node>`) and unattended (`conn run` / MCP) sessions switch seamlessly when primary TACACS+/AAA authentication fails (opt-out via `tags.allow_fallback: false`).
|
||||||
|
* **Fallback Formats**: Accepts `@profile`, explicit JSON list/dict entries (`[{"user": "backup", "password": "..."}]`), or `compose:` expressions. Nodes referencing multiple profiles inherit `fallback` with priority from `user`/`password` `@profile` references.
|
||||||
|
* **Password Composition (`compose:`)**: Concatenate secrets and literals explicitly using `compose:@prof1+@prof2+"suffix"` (use `@@` to escape leading literal `@`).
|
||||||
|
* **Anti-Lockout Guardrails (`conn config`)**: Enforce strict attempt bounds via `--max-auth-prompts`, `--max-total-auth-attempts`, `--max-hop-host-attempts`, `--max-global-user-attempts`, and `--disable-fallback-on-insecure-hostkey`.
|
||||||
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -159,6 +165,9 @@ Support for single or chained intermediate gateway nodes (SSH, Mosh, SSM, kubect
|
|||||||
### 4c. Debug Mode, Keepalive, Logging
|
### 4c. Debug Mode, Keepalive, Logging
|
||||||
Track connection steps (`conn --debug node`), set idle keepalive intervals (`conn config --keepalive <seconds>`), or define dynamic output log files using variables like `${unique}`, `${host}`, `${port}`, `${user}`, `${protocol}`, or `${date 'format'}`.
|
Track connection steps (`conn --debug node`), set idle keepalive intervals (`conn config --keepalive <seconds>`), or define dynamic output log files using variables like `${unique}`, `${host}`, `${port}`, `${user}`, `${protocol}`, or `${date 'format'}`.
|
||||||
|
|
||||||
|
### 4d. Adaptive Multi-Hop Timeouts
|
||||||
|
Differentiate `--connect-timeout` and `--auth-timeout` stages (`conn run` / `conn config`), automatically computing topological per-hop latency budgets for chained `SSM` and `Jumphost` tunnels with bounded progress extensions on slow handshakes.
|
||||||
|
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -1 +1 @@
|
|||||||
__version__ = "6.4.0b4"
|
__version__ = "6.5.0b1"
|
||||||
File diff suppressed because it is too large.
Load diff
@@ -1,9 +1,22 @@
|
|||||||
|
import re
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
from .. import printer
|
from .. import printer
|
||||||
|
from ..auth_utils import (
|
||||||
|
MAX_AUTH_PROMPTS_CAP,
|
||||||
|
MAX_HOP_HOST_ATTEMPTS_CAP,
|
||||||
|
MAX_TOTAL_AUTH_ATTEMPTS_CAP,
|
||||||
|
MIN_AUTH_PROMPTS,
|
||||||
|
MIN_HOP_HOST_ATTEMPTS,
|
||||||
|
MIN_TOTAL_AUTH_ATTEMPTS,
|
||||||
|
validate_strict_int_range,
|
||||||
|
)
|
||||||
from ..services.exceptions import ConnpyError, InvalidConfigurationError
|
from ..services.exceptions import ConnpyError, InvalidConfigurationError
|
||||||
from .help_text import get_instructions
|
from .help_text import get_instructions
|
||||||
|
|
||||||
|
|
||||||
class ConfigHandler:
|
class ConfigHandler:
|
||||||
def __init__(self, app):
|
def __init__(self, app):
|
||||||
self.app = app
|
self.app = app
|
||||||
@@ -29,7 +42,12 @@ class ConfigHandler:
|
|||||||
"sync_remote": self.set_sync_remote,
|
"sync_remote": self.set_sync_remote,
|
||||||
"shell_command": self.set_shell_config,
|
"shell_command": self.set_shell_config,
|
||||||
"shell_prompt": self.set_shell_config,
|
"shell_prompt": self.set_shell_config,
|
||||||
"shell_os": self.set_shell_config
|
"shell_os": self.set_shell_config,
|
||||||
|
"max_auth_prompts": self.set_auth_budget,
|
||||||
|
"max_total_auth_attempts": self.set_auth_budget,
|
||||||
|
"max_hop_host_attempts": self.set_auth_budget,
|
||||||
|
"max_global_user_attempts": self.set_auth_budget,
|
||||||
|
"disable_fallback_on_insecure_hostkey": self.set_disable_fallback_on_insecure_hostkey,
|
||||||
}
|
}
|
||||||
handler = actions.get(getattr(args, "command", None))
|
handler = actions.get(getattr(args, "command", None))
|
||||||
if handler:
|
if handler:
|
||||||
@@ -202,3 +220,67 @@ class ConfigHandler:
|
|||||||
except (ConnpyError, InvalidConfigurationError) as e:
|
except (ConnpyError, InvalidConfigurationError) as e:
|
||||||
printer.error(str(e))
|
printer.error(str(e))
|
||||||
|
|
||||||
|
def set_auth_budget(self, args):
|
||||||
|
key = args.command
|
||||||
|
raw_val = args.data[0] if isinstance(args.data, list) else args.data
|
||||||
|
bounds_map = {
|
||||||
|
"max_auth_prompts": (MIN_AUTH_PROMPTS, MAX_AUTH_PROMPTS_CAP),
|
||||||
|
"max_total_auth_attempts": (MIN_TOTAL_AUTH_ATTEMPTS, MAX_TOTAL_AUTH_ATTEMPTS_CAP),
|
||||||
|
"max_hop_host_attempts": (MIN_HOP_HOST_ATTEMPTS, MAX_HOP_HOST_ATTEMPTS_CAP),
|
||||||
|
"max_global_user_attempts": (1, 1000),
|
||||||
|
}
|
||||||
|
if key not in bounds_map:
|
||||||
|
raise InvalidConfigurationError(f"Unknown auth budget setting: {key}")
|
||||||
|
|
||||||
|
if isinstance(raw_val, bool):
|
||||||
|
msg = f"Invalid {key}: boolean is not permitted"
|
||||||
|
printer.error(msg)
|
||||||
|
raise InvalidConfigurationError(msg)
|
||||||
|
|
||||||
|
if isinstance(raw_val, int):
|
||||||
|
val = raw_val
|
||||||
|
elif isinstance(raw_val, str) and re.fullmatch(r"[+-]?\d+", raw_val.strip()):
|
||||||
|
val = int(raw_val.strip())
|
||||||
|
else:
|
||||||
|
msg = f"Invalid {key}: expected integer, got {raw_val!r}"
|
||||||
|
printer.error(msg)
|
||||||
|
raise InvalidConfigurationError(msg)
|
||||||
|
|
||||||
|
if val == 0:
|
||||||
|
cfg_obj = self.app.config
|
||||||
|
if not hasattr(cfg_obj, "assert_called") and callable(
|
||||||
|
getattr(cfg_obj, "transactional_update", None)
|
||||||
|
):
|
||||||
|
cfg_obj.transactional_update(lambda c: c.config.pop(key, None))
|
||||||
|
else:
|
||||||
|
cfg_obj.config.pop(key, None)
|
||||||
|
cfg_obj._saveconfig(cfg_obj.file)
|
||||||
|
printer.success("Config saved")
|
||||||
|
return
|
||||||
|
|
||||||
|
min_v, max_v = bounds_map[key]
|
||||||
|
try:
|
||||||
|
validate_strict_int_range(val, key, min_v, max_v)
|
||||||
|
except ValueError as exc:
|
||||||
|
printer.error(str(exc))
|
||||||
|
raise InvalidConfigurationError(str(exc)) from None
|
||||||
|
|
||||||
|
self.app.services.config_svc.update_setting(key, val)
|
||||||
|
printer.success("Config saved")
|
||||||
|
|
||||||
|
def set_disable_fallback_on_insecure_hostkey(self, args):
|
||||||
|
raw_val = args.data[0] if isinstance(args.data, list) else args.data
|
||||||
|
if isinstance(raw_val, bool):
|
||||||
|
val = raw_val
|
||||||
|
elif isinstance(raw_val, str) and raw_val.strip().lower() in ("true", "false"):
|
||||||
|
val = raw_val.strip().lower() == "true"
|
||||||
|
else:
|
||||||
|
msg = "Invalid disable_fallback_on_insecure_hostkey: expected 'true' or 'false'"
|
||||||
|
printer.error(msg)
|
||||||
|
raise InvalidConfigurationError(msg)
|
||||||
|
self.app.services.config_svc.update_setting(
|
||||||
|
"disable_fallback_on_insecure_hostkey",
|
||||||
|
val,
|
||||||
|
)
|
||||||
|
printer.success("Config saved")
|
||||||
|
|
||||||
+145
-28
@@ -1,6 +1,77 @@
|
|||||||
import ast
|
import ast
|
||||||
|
import json
|
||||||
|
|
||||||
|
from ..auth_utils import (
|
||||||
|
MAX_FALLBACK_ENTRIES,
|
||||||
|
MAX_JSON_INPUT_BYTES,
|
||||||
|
MAX_JSON_INPUT_DEPTH,
|
||||||
|
validate_auth_budget_tags,
|
||||||
|
validate_fallback_entries,
|
||||||
|
validate_json_depth_and_size,
|
||||||
|
)
|
||||||
from .validators import Validators
|
from .validators import Validators
|
||||||
|
|
||||||
|
|
||||||
|
def _parse_tags_input(raw_tags):
|
||||||
|
if raw_tags in ("", None):
|
||||||
|
return ""
|
||||||
|
if isinstance(raw_tags, dict):
|
||||||
|
validate_json_depth_and_size(
|
||||||
|
raw_tags,
|
||||||
|
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||||
|
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||||
|
)
|
||||||
|
validate_auth_budget_tags(raw_tags)
|
||||||
|
return raw_tags
|
||||||
|
s = str(raw_tags)
|
||||||
|
if s.startswith("@"):
|
||||||
|
return s
|
||||||
|
if len(s.encode("utf-8", errors="replace")) > MAX_JSON_INPUT_BYTES:
|
||||||
|
raise ValueError(f"Tags exceed maximum size of {MAX_JSON_INPUT_BYTES} bytes")
|
||||||
|
try:
|
||||||
|
parsed = json.loads(s)
|
||||||
|
except (ValueError, TypeError):
|
||||||
|
parsed = ast.literal_eval(s)
|
||||||
|
validate_json_depth_and_size(
|
||||||
|
parsed,
|
||||||
|
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||||
|
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||||
|
)
|
||||||
|
validate_auth_budget_tags(parsed)
|
||||||
|
return parsed
|
||||||
|
|
||||||
|
|
||||||
|
def _parse_fallback_input(raw_fb):
|
||||||
|
if raw_fb in (None, "", []):
|
||||||
|
return ""
|
||||||
|
if isinstance(raw_fb, str):
|
||||||
|
stripped = raw_fb.strip()
|
||||||
|
if not stripped:
|
||||||
|
return ""
|
||||||
|
if stripped.startswith(("[", "{")):
|
||||||
|
parsed = validate_json_depth_and_size(
|
||||||
|
stripped,
|
||||||
|
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||||
|
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||||
|
)
|
||||||
|
return validate_fallback_entries(parsed, max_entries=MAX_FALLBACK_ENTRIES)
|
||||||
|
validated = validate_fallback_entries(stripped, max_entries=MAX_FALLBACK_ENTRIES)
|
||||||
|
if (
|
||||||
|
stripped.startswith("@")
|
||||||
|
and not stripped.startswith("@@")
|
||||||
|
and "," in stripped
|
||||||
|
and not stripped.startswith("compose:")
|
||||||
|
):
|
||||||
|
return validated
|
||||||
|
return stripped
|
||||||
|
validate_json_depth_and_size(
|
||||||
|
raw_fb,
|
||||||
|
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||||
|
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||||
|
)
|
||||||
|
return validate_fallback_entries(raw_fb, max_entries=MAX_FALLBACK_ENTRIES)
|
||||||
|
|
||||||
|
|
||||||
class Forms:
|
class Forms:
|
||||||
def __init__(self, app):
|
def __init__(self, app):
|
||||||
self.app = app
|
self.app = app
|
||||||
@@ -18,6 +89,7 @@ class Forms:
|
|||||||
questions.append(inquirer.Confirm("jumphost", message="Edit jumphost?"))
|
questions.append(inquirer.Confirm("jumphost", message="Edit jumphost?"))
|
||||||
questions.append(inquirer.Confirm("user", message="Edit User?"))
|
questions.append(inquirer.Confirm("user", message="Edit User?"))
|
||||||
questions.append(inquirer.Confirm("password", message="Edit password?"))
|
questions.append(inquirer.Confirm("password", message="Edit password?"))
|
||||||
|
questions.append(inquirer.Confirm("fallback", message="Edit fallback credentials?"))
|
||||||
return inquirer.prompt(questions)
|
return inquirer.prompt(questions)
|
||||||
|
|
||||||
def questions_nodes(self, unique, uniques=None, edit=None):
|
def questions_nodes(self, unique, uniques=None, edit=None):
|
||||||
@@ -28,11 +100,13 @@ class Forms:
|
|||||||
defaults["tags"] = ""
|
defaults["tags"] = ""
|
||||||
if "jumphost" not in defaults:
|
if "jumphost" not in defaults:
|
||||||
defaults["jumphost"] = ""
|
defaults["jumphost"] = ""
|
||||||
except Exception:
|
if "fallback" not in defaults:
|
||||||
defaults = {"host": "", "protocol": "", "port": "", "user": "", "options": "", "logs": "", "tags": "", "password": "", "jumphost": ""}
|
defaults["fallback"] = ""
|
||||||
|
except Exception: # noqa: BLE001
|
||||||
|
defaults = {"host": "", "protocol": "", "port": "", "user": "", "options": "", "logs": "", "tags": "", "password": "", "jumphost": "", "fallback": ""}
|
||||||
node = {}
|
node = {}
|
||||||
if edit is None:
|
if edit is None:
|
||||||
edit = {"host": True, "protocol": True, "port": True, "user": True, "password": True, "options": True, "logs": True, "tags": True, "jumphost": True}
|
edit = {"host": True, "protocol": True, "port": True, "user": True, "password": True, "options": True, "logs": True, "tags": True, "jumphost": True, "fallback": True}
|
||||||
questions = []
|
questions = []
|
||||||
if edit["host"]:
|
if edit["host"]:
|
||||||
questions.append(inquirer.Text("host", message="Add Hostname or IP", validate=self.validators.host_validation, default=defaults["host"]))
|
questions.append(inquirer.Text("host", message="Add Hostname or IP", validate=self.validators.host_validation, default=defaults["host"]))
|
||||||
@@ -70,11 +144,13 @@ class Forms:
|
|||||||
questions.append(inquirer.List("password", message="Password: Use a local password, no password or a list of profiles to reference?", choices=["Local Password", "Profiles", "No Password"]))
|
questions.append(inquirer.List("password", message="Password: Use a local password, no password or a list of profiles to reference?", choices=["Local Password", "Profiles", "No Password"]))
|
||||||
else:
|
else:
|
||||||
node["password"] = defaults["password"]
|
node["password"] = defaults["password"]
|
||||||
|
if not edit.get("fallback"):
|
||||||
|
node["fallback"] = defaults.get("fallback", "")
|
||||||
|
|
||||||
answer = inquirer.prompt(questions)
|
answer = inquirer.prompt(questions)
|
||||||
if answer is None:
|
if answer is None:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if "password" in answer:
|
if "password" in answer:
|
||||||
if answer["password"] == "Local Password":
|
if answer["password"] == "Local Password":
|
||||||
passq = [inquirer.Password("password", message="Set Password")]
|
passq = [inquirer.Password("password", message="Set Password")]
|
||||||
@@ -87,13 +163,29 @@ class Forms:
|
|||||||
passa = inquirer.prompt(passq)
|
passa = inquirer.prompt(passq)
|
||||||
if passa is None:
|
if passa is None:
|
||||||
return False
|
return False
|
||||||
answer["password"] = passa["password"].split(",")
|
answer["password"] = [p.strip() for p in passa["password"].split(",") if p.strip()]
|
||||||
elif answer["password"] == "No Password":
|
elif answer["password"] == "No Password":
|
||||||
answer["password"] = ""
|
answer["password"] = ""
|
||||||
|
|
||||||
if "tags" in answer and not answer["tags"].startswith("@") and answer["tags"]:
|
if edit.get("fallback"):
|
||||||
answer["tags"] = ast.literal_eval(answer["tags"])
|
raw_def_fb = defaults.get("fallback", "")
|
||||||
|
def_fb_str = json.dumps(raw_def_fb) if isinstance(raw_def_fb, (list, dict)) else str(raw_def_fb or "")
|
||||||
|
fbq = [
|
||||||
|
inquirer.Text(
|
||||||
|
"fallback",
|
||||||
|
message="Add fallback credentials (@profile, @p1,@p2, JSON list/dict, or compose:)",
|
||||||
|
validate=self.validators.fallback_validation,
|
||||||
|
default=def_fb_str.replace("{", "{{").replace("}", "}}"),
|
||||||
|
)
|
||||||
|
]
|
||||||
|
fba = inquirer.prompt(fbq)
|
||||||
|
if fba is None:
|
||||||
|
return False
|
||||||
|
answer["fallback"] = _parse_fallback_input(fba["fallback"])
|
||||||
|
|
||||||
|
if "tags" in answer and answer["tags"] and not str(answer["tags"]).startswith("@"):
|
||||||
|
answer["tags"] = _parse_tags_input(answer["tags"])
|
||||||
|
|
||||||
result = {**uniques, **answer, **node}
|
result = {**uniques, **answer, **node}
|
||||||
result["type"] = "connection"
|
result["type"] = "connection"
|
||||||
return result
|
return result
|
||||||
@@ -106,11 +198,13 @@ class Forms:
|
|||||||
defaults["tags"] = ""
|
defaults["tags"] = ""
|
||||||
if "jumphost" not in defaults:
|
if "jumphost" not in defaults:
|
||||||
defaults["jumphost"] = ""
|
defaults["jumphost"] = ""
|
||||||
except Exception:
|
if "fallback" not in defaults:
|
||||||
defaults = {"host": "", "protocol": "", "port": "", "user": "", "options": "", "logs": "", "tags": "", "jumphost": ""}
|
defaults["fallback"] = ""
|
||||||
|
except Exception: # noqa: BLE001
|
||||||
|
defaults = {"host": "", "protocol": "", "port": "", "user": "", "options": "", "logs": "", "tags": "", "jumphost": "", "fallback": ""}
|
||||||
profile = {}
|
profile = {}
|
||||||
if edit is None:
|
if edit is None:
|
||||||
edit = {"host": True, "protocol": True, "port": True, "user": True, "password": True, "options": True, "logs": True, "tags": True, "jumphost": True}
|
edit = {"host": True, "protocol": True, "port": True, "user": True, "password": True, "options": True, "logs": True, "tags": True, "jumphost": True, "fallback": True}
|
||||||
questions = []
|
questions = []
|
||||||
if edit["host"]:
|
if edit["host"]:
|
||||||
questions.append(inquirer.Text("host", message="Add Hostname or IP", default=defaults["host"]))
|
questions.append(inquirer.Text("host", message="Add Hostname or IP", default=defaults["host"]))
|
||||||
@@ -148,18 +242,25 @@ class Forms:
|
|||||||
questions.append(inquirer.Password("password", message="Set Password"))
|
questions.append(inquirer.Password("password", message="Set Password"))
|
||||||
else:
|
else:
|
||||||
profile["password"] = defaults["password"]
|
profile["password"] = defaults["password"]
|
||||||
|
if edit.get("fallback"):
|
||||||
|
raw_def_fb = defaults.get("fallback", "")
|
||||||
|
def_fb_str = json.dumps(raw_def_fb) if isinstance(raw_def_fb, (list, dict)) else str(raw_def_fb or "")
|
||||||
|
questions.append(inquirer.Text("fallback", message="Add fallback credentials (@profile, @p1,@p2, JSON list/dict, or compose:)", validate=self.validators.fallback_validation, default=def_fb_str.replace("{", "{{").replace("}", "}}")))
|
||||||
|
else:
|
||||||
|
profile["fallback"] = defaults.get("fallback", "")
|
||||||
|
|
||||||
answer = inquirer.prompt(questions)
|
answer = inquirer.prompt(questions)
|
||||||
if answer is None:
|
if answer is None:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if "password" in answer:
|
if "password" in answer and answer["password"] != "":
|
||||||
if answer["password"] != "":
|
answer["password"] = self.app.services.config_svc.encrypt_password(answer["password"])
|
||||||
answer["password"] = self.app.services.config_svc.encrypt_password(answer["password"])
|
|
||||||
|
|
||||||
if "tags" in answer and answer["tags"]:
|
if "tags" in answer and answer["tags"]:
|
||||||
answer["tags"] = ast.literal_eval(answer["tags"])
|
answer["tags"] = _parse_tags_input(answer["tags"])
|
||||||
|
if "fallback" in answer:
|
||||||
|
answer["fallback"] = _parse_fallback_input(answer["fallback"])
|
||||||
|
|
||||||
result = {**answer, **profile}
|
result = {**answer, **profile}
|
||||||
result["id"] = unique
|
result["id"] = unique
|
||||||
return result
|
return result
|
||||||
@@ -178,27 +279,43 @@ class Forms:
|
|||||||
questions.append(inquirer.Text("jumphost", message="Add Jumphost node", validate=self.validators.jumphost_validation))
|
questions.append(inquirer.Text("jumphost", message="Add Jumphost node", validate=self.validators.jumphost_validation))
|
||||||
questions.append(inquirer.Text("user", message="Pick username", validate=self.validators.default_validation))
|
questions.append(inquirer.Text("user", message="Pick username", validate=self.validators.default_validation))
|
||||||
questions.append(inquirer.List("password", message="Password: Use a local password, no password or a list of profiles to reference?", choices=["Local Password", "Profiles", "No Password"]))
|
questions.append(inquirer.List("password", message="Password: Use a local password, no password or a list of profiles to reference?", choices=["Local Password", "Profiles", "No Password"]))
|
||||||
|
|
||||||
answer = inquirer.prompt(questions)
|
answer = inquirer.prompt(questions)
|
||||||
if answer is None:
|
if answer is None:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if "password" in answer:
|
if "password" in answer:
|
||||||
if answer["password"] == "Local Password":
|
if answer["password"] == "Local Password":
|
||||||
passq = [inquirer.Password("password", message="Set Password")]
|
passq = [inquirer.Password("password", message="Set Password")]
|
||||||
passa = inquirer.prompt(passq)
|
passa = inquirer.prompt(passq)
|
||||||
|
if passa is None:
|
||||||
|
return False
|
||||||
answer["password"] = self.app.services.config_svc.encrypt_password(passa["password"])
|
answer["password"] = self.app.services.config_svc.encrypt_password(passa["password"])
|
||||||
elif answer["password"] == "Profiles":
|
elif answer["password"] == "Profiles":
|
||||||
passq = [(inquirer.Text("password", message="Set a @profile or a comma separated list of @profiles", validate=self.validators.pass_validation))]
|
passq = [(inquirer.Text("password", message="Set a @profile or a comma separated list of @profiles", validate=self.validators.pass_validation))]
|
||||||
passa = inquirer.prompt(passq)
|
passa = inquirer.prompt(passq)
|
||||||
answer["password"] = passa["password"].split(",")
|
if passa is None:
|
||||||
|
return False
|
||||||
|
answer["password"] = [p.strip() for p in passa["password"].split(",") if p.strip()]
|
||||||
elif answer["password"] == "No Password":
|
elif answer["password"] == "No Password":
|
||||||
answer["password"] = ""
|
answer["password"] = ""
|
||||||
|
|
||||||
|
fbq = [
|
||||||
|
inquirer.Text(
|
||||||
|
"fallback",
|
||||||
|
message="Add fallback credentials (@profile, @p1,@p2, JSON list/dict, or compose:)",
|
||||||
|
validate=self.validators.fallback_validation,
|
||||||
|
)
|
||||||
|
]
|
||||||
|
fba = inquirer.prompt(fbq)
|
||||||
|
if fba is None:
|
||||||
|
return False
|
||||||
|
answer["fallback"] = _parse_fallback_input(fba["fallback"])
|
||||||
|
|
||||||
answer["type"] = "connection"
|
answer["type"] = "connection"
|
||||||
if "tags" in answer and not answer["tags"].startswith("@") and answer["tags"]:
|
if "tags" in answer and answer["tags"] and not str(answer["tags"]).startswith("@"):
|
||||||
answer["tags"] = ast.literal_eval(answer["tags"])
|
answer["tags"] = _parse_tags_input(answer["tags"])
|
||||||
|
|
||||||
return answer
|
return answer
|
||||||
|
|
||||||
def mcp_wizard(self, mcp_servers):
|
def mcp_wizard(self, mcp_servers):
|
||||||
|
|||||||
@@ -144,6 +144,7 @@ class NodeHandler:
|
|||||||
uniques = self.app.services.nodes.explode_unique(args.data)
|
uniques = self.app.services.nodes.explode_unique(args.data)
|
||||||
if not uniques:
|
if not uniques:
|
||||||
raise InvalidConfigurationError(f"Invalid folder {args.data}")
|
raise InvalidConfigurationError(f"Invalid folder {args.data}")
|
||||||
|
self.app.services.nodes.validate_parent_folder(args.data, is_folder=True)
|
||||||
self.app.services.nodes.add_node(args.data, {}, is_folder=True)
|
self.app.services.nodes.add_node(args.data, {}, is_folder=True)
|
||||||
printer.success(f"{args.data} added successfully")
|
printer.success(f"{args.data} added successfully")
|
||||||
else:
|
else:
|
||||||
|
|||||||
+145
-28
@@ -1,5 +1,19 @@
|
|||||||
import re
|
|
||||||
import ast
|
import ast
|
||||||
|
import json
|
||||||
|
import re
|
||||||
|
|
||||||
|
from ..auth_utils import (
|
||||||
|
MAX_FALLBACK_ENTRIES,
|
||||||
|
MAX_JSON_INPUT_BYTES,
|
||||||
|
MAX_JSON_INPUT_DEPTH,
|
||||||
|
SanitizedAuthError,
|
||||||
|
is_compose_expression,
|
||||||
|
validate_auth_budget_tags,
|
||||||
|
validate_compose_syntax,
|
||||||
|
validate_fallback_entries,
|
||||||
|
validate_json_depth_and_size,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def _raise_val_err(reason):
|
def _raise_val_err(reason):
|
||||||
import inquirer
|
import inquirer
|
||||||
@@ -56,50 +70,119 @@ class Validators:
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
def pass_validation(self, answers, current, regex = "(^@.+$)"):
|
def pass_validation(self, answers, current, regex = "(^@.+$)"):
|
||||||
|
if is_compose_expression(current):
|
||||||
|
try:
|
||||||
|
validate_compose_syntax(current)
|
||||||
|
except SanitizedAuthError as exc:
|
||||||
|
_raise_val_err(str(exc))
|
||||||
|
return True
|
||||||
profiles = current.split(",")
|
profiles = current.split(",")
|
||||||
for i in profiles:
|
for i in profiles:
|
||||||
if not re.match(regex, i) or i[1:] not in self.app.profiles:
|
if not re.match(regex, i) or i[1:] not in self.app.profiles:
|
||||||
_raise_val_err("Profile {} don't exist".format(i))
|
_raise_val_err("Profile {} don't exist".format(i))
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
def _validate_tags_dict(self, current: str):
|
||||||
|
if len(current.encode("utf-8", errors="replace")) > MAX_JSON_INPUT_BYTES:
|
||||||
|
_raise_val_err(f"Tags exceed maximum size of {MAX_JSON_INPUT_BYTES} bytes")
|
||||||
|
parsed = None
|
||||||
|
try:
|
||||||
|
parsed = json.loads(current)
|
||||||
|
except (ValueError, TypeError):
|
||||||
|
try:
|
||||||
|
parsed = ast.literal_eval(current)
|
||||||
|
except (ValueError, TypeError, SyntaxError, MemoryError, RecursionError):
|
||||||
|
parsed = None
|
||||||
|
if not isinstance(parsed, dict):
|
||||||
|
_raise_val_err("Tags should be a python dictionary.")
|
||||||
|
try:
|
||||||
|
validate_json_depth_and_size(
|
||||||
|
parsed,
|
||||||
|
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||||
|
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||||
|
)
|
||||||
|
validate_auth_budget_tags(parsed)
|
||||||
|
except (ValueError, TypeError, SanitizedAuthError) as exc:
|
||||||
|
_raise_val_err(str(exc))
|
||||||
|
return True
|
||||||
|
|
||||||
def tags_validation(self, answers, current):
|
def tags_validation(self, answers, current):
|
||||||
if current.startswith("@"):
|
if current.startswith("@"):
|
||||||
if current[1:] not in self.app.profiles:
|
if current[1:] not in self.app.profiles:
|
||||||
_raise_val_err("Profile {} don't exist".format(current))
|
_raise_val_err("Profile {} don't exist".format(current))
|
||||||
elif current != "":
|
elif current != "":
|
||||||
isdict = False
|
return self._validate_tags_dict(current)
|
||||||
try:
|
|
||||||
isdict = ast.literal_eval(current)
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
if not isinstance (isdict, dict):
|
|
||||||
_raise_val_err("Tags should be a python dictionary.".format(current))
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def profile_tags_validation(self, answers, current):
|
def profile_tags_validation(self, answers, current):
|
||||||
if current != "":
|
if current != "":
|
||||||
isdict = False
|
return self._validate_tags_dict(current)
|
||||||
try:
|
|
||||||
isdict = ast.literal_eval(current)
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
if not isinstance (isdict, dict):
|
|
||||||
_raise_val_err("Tags should be a python dictionary.".format(current))
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
def fallback_validation(self, answers, current):
|
||||||
|
if current in (None, ""):
|
||||||
|
return True
|
||||||
|
stripped = str(current).strip()
|
||||||
|
if not stripped:
|
||||||
|
return True
|
||||||
|
if len(stripped.encode("utf-8", errors="replace")) > MAX_JSON_INPUT_BYTES:
|
||||||
|
_raise_val_err(f"Fallback exceeds maximum size of {MAX_JSON_INPUT_BYTES} bytes")
|
||||||
|
if stripped.startswith("@") and not stripped.startswith("@@") and "+" not in stripped:
|
||||||
|
parts = [p.strip() for p in stripped.split(",")]
|
||||||
|
if any(not p for p in parts):
|
||||||
|
_raise_val_err("Fallback profile entry cannot be empty")
|
||||||
|
if len(parts) > MAX_FALLBACK_ENTRIES:
|
||||||
|
_raise_val_err(f"Fallback exceeds maximum of {MAX_FALLBACK_ENTRIES} entries")
|
||||||
|
for p in parts:
|
||||||
|
if not p.startswith("@") or not re.fullmatch(r"[A-Za-z0-9_.$:/-]+", p[1:]):
|
||||||
|
_raise_val_err(
|
||||||
|
"Comma-separated fallback entries must be valid @profile references"
|
||||||
|
)
|
||||||
|
if p[1:] not in self.app.profiles:
|
||||||
|
_raise_val_err(f"Profile {p} don't exist")
|
||||||
|
return True
|
||||||
|
try:
|
||||||
|
if is_compose_expression(stripped):
|
||||||
|
validate_compose_syntax(stripped)
|
||||||
|
return True
|
||||||
|
if stripped.startswith(("[", "{")):
|
||||||
|
validate_json_depth_and_size(
|
||||||
|
stripped,
|
||||||
|
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||||
|
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||||
|
)
|
||||||
|
validate_fallback_entries(stripped, max_entries=MAX_FALLBACK_ENTRIES)
|
||||||
|
except (ValueError, TypeError, SanitizedAuthError) as exc:
|
||||||
|
_raise_val_err(str(exc))
|
||||||
|
return True
|
||||||
|
|
||||||
|
def _get_all_valid_nodes(self) -> list[str]:
|
||||||
|
valid_nodes = list(getattr(self.app, "nodes_list", []) or [])
|
||||||
|
if hasattr(self.app, "services") and hasattr(self.app.services, "nodes"):
|
||||||
|
raw_fn = getattr(self.app.services.nodes, "_get_raw_nodes", None)
|
||||||
|
if callable(raw_fn) and not hasattr(raw_fn, "assert_called"):
|
||||||
|
raw_res = raw_fn()
|
||||||
|
if isinstance(raw_res, (list, tuple, set)):
|
||||||
|
valid_nodes = list(dict.fromkeys([*raw_res, *valid_nodes]))
|
||||||
|
return valid_nodes
|
||||||
|
|
||||||
|
def _node_exists(self, target: str) -> bool:
|
||||||
|
valid_nodes = self._get_all_valid_nodes()
|
||||||
|
if getattr(self.app, "case", True):
|
||||||
|
return target in valid_nodes
|
||||||
|
return target.lower() in {n.lower() for n in valid_nodes}
|
||||||
|
|
||||||
def jumphost_validation(self, answers, current):
|
def jumphost_validation(self, answers, current):
|
||||||
if current.startswith("@"):
|
if current.startswith("@"):
|
||||||
if current[1:] not in self.app.profiles:
|
if current[1:] not in self.app.profiles:
|
||||||
_raise_val_err("Profile {} don't exist".format(current))
|
_raise_val_err(f"Profile {current} don't exist")
|
||||||
elif current != "":
|
elif current != "" and not self._node_exists(current):
|
||||||
if current not in self.app.nodes_list:
|
_raise_val_err(f"Node {current} don't exist.")
|
||||||
_raise_val_err("Node {} don't exist.".format(current))
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def profile_jumphost_validation(self, answers, current):
|
def profile_jumphost_validation(self, answers, current):
|
||||||
if current != "":
|
if current != "" and not self._node_exists(current):
|
||||||
if current not in self.app.nodes_list:
|
_raise_val_err(f"Node {current} don't exist.")
|
||||||
_raise_val_err("Node {} don't exist.".format(current))
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def default_validation(self, answers, current):
|
def default_validation(self, answers, current):
|
||||||
@@ -117,13 +200,47 @@ class Validators:
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
def bulk_folder_validation(self, answers, current):
|
def bulk_folder_validation(self, answers, current):
|
||||||
if not self.app.case:
|
case = getattr(self.app, "case", False)
|
||||||
|
if not case:
|
||||||
current = current.lower()
|
current = current.lower()
|
||||||
|
|
||||||
candidate = current
|
candidate = current.split("/")[0] if "/" in current else current
|
||||||
if "/" in current:
|
|
||||||
candidate = current.split("/")[0]
|
if hasattr(self.app, "services") and hasattr(self.app.services, "context"):
|
||||||
|
raw_folders = list(getattr(self.app, "folders", []) or [])
|
||||||
|
if hasattr(self.app.services, "nodes"):
|
||||||
|
raw_fn = getattr(self.app.services.nodes, "_get_raw_folders", None)
|
||||||
|
if callable(raw_fn) and not hasattr(raw_fn, "assert_called"):
|
||||||
|
raw_res = raw_fn()
|
||||||
|
if isinstance(raw_res, (list, tuple, set)):
|
||||||
|
raw_folders = list(raw_res)
|
||||||
|
|
||||||
|
def _folder_exists_norm(folder_name: str) -> bool:
|
||||||
|
if case:
|
||||||
|
return folder_name in raw_folders
|
||||||
|
return folder_name.lower() in {f.lower() for f in raw_folders}
|
||||||
|
|
||||||
|
if current != "" and not _folder_exists_norm(candidate):
|
||||||
|
_raise_val_err(f"Location {current} don't exist")
|
||||||
|
|
||||||
|
ctx_svc = self.app.services.context
|
||||||
|
if ctx_svc is not None and not ctx_svc.is_default_all_context():
|
||||||
|
raw_ids = answers.get("ids", "") if isinstance(answers, dict) else ""
|
||||||
|
for nid in str(raw_ids).split(","):
|
||||||
|
nid_stripped = nid.strip()
|
||||||
|
if not nid_stripped:
|
||||||
|
continue
|
||||||
|
nid_norm = nid_stripped if case else nid_stripped.lower()
|
||||||
|
full_id = f"{nid_norm}{candidate}" if candidate else nid_norm
|
||||||
|
if not ctx_svc.matches_active_context(full_id):
|
||||||
|
_raise_val_err(
|
||||||
|
f"Node '{full_id}' does not match active context '{ctx_svc.current_context}'."
|
||||||
|
)
|
||||||
|
parent = full_id.partition("@")[2]
|
||||||
|
if parent and not _folder_exists_norm(f"@{parent}"):
|
||||||
|
_raise_val_err(f"Location @{parent} don't exist")
|
||||||
|
return True
|
||||||
|
|
||||||
matches = list(filter(lambda k: k == candidate, self.app.folders))
|
matches = list(filter(lambda k: k == candidate, self.app.folders))
|
||||||
if current != "" and len(matches) == 0:
|
if current != "" and len(matches) == 0:
|
||||||
_raise_val_err("Location {} don't exist".format(current))
|
_raise_val_err("Location {} don't exist".format(current))
|
||||||
|
|||||||
+18
-1
@@ -248,9 +248,26 @@ def _build_tree(nodes, folders, profiles, plugins, configdir):
|
|||||||
"--fzf-wrapper": ["bash", "zsh"],
|
"--fzf-wrapper": ["bash", "zsh"],
|
||||||
"--service-mode": ["local", "remote"],
|
"--service-mode": ["local", "remote"],
|
||||||
"--sync-remote": ["true", "false"],
|
"--sync-remote": ["true", "false"],
|
||||||
|
"--disable-fallback-on-insecure-hostkey": ["true", "false"],
|
||||||
"--help": None, "-h": None,
|
"--help": None, "-h": None,
|
||||||
}
|
}
|
||||||
for opt in ["--keepalive", "--engineer-model", "--engineer-api-key", "--architect-model", "--architect-api-key", "--theme", "--remote", "--trusted-commands", "--shell-command", "--shell-prompt", "--shell-os"]:
|
for opt in [
|
||||||
|
"--keepalive",
|
||||||
|
"--engineer-model",
|
||||||
|
"--engineer-api-key",
|
||||||
|
"--architect-model",
|
||||||
|
"--architect-api-key",
|
||||||
|
"--theme",
|
||||||
|
"--remote",
|
||||||
|
"--trusted-commands",
|
||||||
|
"--shell-command",
|
||||||
|
"--shell-prompt",
|
||||||
|
"--shell-os",
|
||||||
|
"--max-auth-prompts",
|
||||||
|
"--max-total-auth-attempts",
|
||||||
|
"--max-hop-host-attempts",
|
||||||
|
"--max-global-user-attempts",
|
||||||
|
]:
|
||||||
config_dict[opt] = {"*": config_dict}
|
config_dict[opt] = {"*": config_dict}
|
||||||
config_dict["--configfolder"] = {"__extra__": lambda w: get_cwd(w, "--configfolder", True), "*": config_dict}
|
config_dict["--configfolder"] = {"__extra__": lambda w: get_cwd(w, "--configfolder", True), "*": config_dict}
|
||||||
config_dict["--engineer-auth"] = {"__extra__": lambda w: get_cwd(w, "--engineer-auth"), "*": config_dict}
|
config_dict["--engineer-auth"] = {"__extra__": lambda w: get_cwd(w, "--engineer-auth"), "*": config_dict}
|
||||||
|
|||||||
+709
-224
File diff suppressed because it is too large.
Load diff
+14
-17
@@ -1,20 +1,20 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
#Imports
|
import argparse
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import argparse
|
|
||||||
import sys
|
import sys
|
||||||
import yaml
|
|
||||||
import sys
|
|
||||||
from .core import node,nodes
|
|
||||||
from ._version import __version__
|
|
||||||
from . import printer
|
|
||||||
from .api import start_api,stop_api,debug_api
|
|
||||||
|
|
||||||
|
import yaml
|
||||||
|
from rich_argparse import RichHelpFormatter
|
||||||
|
|
||||||
|
from . import printer
|
||||||
|
from .api import debug_api, start_api, stop_api
|
||||||
|
from .cli.help_text import get_help
|
||||||
|
from .cli.helpers import folders_completer, nodes_completer
|
||||||
|
from .core import node, nodes
|
||||||
from .plugins import Plugins
|
from .plugins import Plugins
|
||||||
from .services.exceptions import ConnpyError, ProfileNotFoundError, ReservedNameError
|
from .services.exceptions import ConnpyError, ProfileNotFoundError, ReservedNameError
|
||||||
|
|
||||||
from rich_argparse import RichHelpFormatter
|
|
||||||
# Bridge rich-argparse with our design system
|
# Bridge rich-argparse with our design system
|
||||||
RichHelpFormatter.console = printer.console
|
RichHelpFormatter.console = printer.console
|
||||||
RichHelpFormatter.styles.update({
|
RichHelpFormatter.styles.update({
|
||||||
@@ -28,14 +28,6 @@ RichHelpFormatter.styles.update({
|
|||||||
})
|
})
|
||||||
RichHelpFormatter.group_name_formatter = str.upper
|
RichHelpFormatter.group_name_formatter = str.upper
|
||||||
|
|
||||||
from .cli import (
|
|
||||||
NodeHandler, ProfileHandler, ConfigHandler, RunHandler,
|
|
||||||
AIHandler, APIHandler, PluginHandler, ImportExportHandler,
|
|
||||||
ContextHandler, SSOHandler
|
|
||||||
)
|
|
||||||
from .cli.helpers import nodes_completer, folders_completer, profiles_completer
|
|
||||||
from .cli.help_text import get_help
|
|
||||||
|
|
||||||
console = printer.console
|
console = printer.console
|
||||||
|
|
||||||
#functions and classes
|
#functions and classes
|
||||||
@@ -389,6 +381,11 @@ class connapp:
|
|||||||
configcrud.add_argument("--shell-command", dest="shell_command", nargs=1, action=self._store_type, help="Set default shell command", metavar="COMMAND")
|
configcrud.add_argument("--shell-command", dest="shell_command", nargs=1, action=self._store_type, help="Set default shell command", metavar="COMMAND")
|
||||||
configcrud.add_argument("--shell-prompt", dest="shell_prompt", nargs=1, action=self._store_type, help="Set shell prompt regex for AI", metavar="REGEX")
|
configcrud.add_argument("--shell-prompt", dest="shell_prompt", nargs=1, action=self._store_type, help="Set shell prompt regex for AI", metavar="REGEX")
|
||||||
configcrud.add_argument("--shell-os", dest="shell_os", nargs=1, action=self._store_type, help="Set shell OS hint for AI", metavar="OS")
|
configcrud.add_argument("--shell-os", dest="shell_os", nargs=1, action=self._store_type, help="Set shell OS hint for AI", metavar="OS")
|
||||||
|
configcrud.add_argument("--max-auth-prompts", dest="max_auth_prompts", nargs=1, action=self._store_type, help="Set max auth prompts per connection (1-5, 0 to reset)", type=int, metavar="INT")
|
||||||
|
configcrud.add_argument("--max-total-auth-attempts", dest="max_total_auth_attempts", nargs=1, action=self._store_type, help="Set max total auth attempts per host (1-10, 0 to reset)", type=int, metavar="INT")
|
||||||
|
configcrud.add_argument("--max-hop-host-attempts", dest="max_hop_host_attempts", nargs=1, action=self._store_type, help="Set max auth attempts per jumphost (1-10, 0 to reset)", type=int, metavar="INT")
|
||||||
|
configcrud.add_argument("--max-global-user-attempts", dest="max_global_user_attempts", nargs=1, action=self._store_type, help="Set max global auth attempts per user (1-1000, 0 to reset)", type=int, metavar="INT")
|
||||||
|
configcrud.add_argument("--disable-fallback-on-insecure-hostkey", dest="disable_fallback_on_insecure_hostkey", nargs=1, action=self._store_type, help="Disable AAA fallback when StrictHostKeyChecking=no", choices=["true", "false"])
|
||||||
configparser.add_argument("--trusted-commands", dest="trusted_commands", nargs=1, action=self._store_type, help="Set custom trusted commands regexes (comma separated)", metavar="REGEX,REGEX")
|
configparser.add_argument("--trusted-commands", dest="trusted_commands", nargs=1, action=self._store_type, help="Set custom trusted commands regexes (comma separated)", metavar="REGEX,REGEX")
|
||||||
configparser.set_defaults(func=self._config.dispatch)
|
configparser.set_defaults(func=self._config.dispatch)
|
||||||
|
|
||||||
|
|||||||
+2225
-377
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 contextvars
|
||||||
import datetime
|
import datetime
|
||||||
|
import functools
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
from concurrent import futures
|
||||||
|
|
||||||
|
import grpc
|
||||||
|
from google.protobuf.empty_pb2 import Empty
|
||||||
|
|
||||||
# Suppress harmless but noisy gRPC fork() warnings from pexpect child processes
|
# Suppress harmless but noisy gRPC fork() warnings from pexpect child processes
|
||||||
os.environ["GRPC_VERBOSITY"] = "NONE"
|
os.environ["GRPC_VERBOSITY"] = "NONE"
|
||||||
os.environ["GRPC_ENABLE_FORK_SUPPORT"] = "0"
|
os.environ["GRPC_ENABLE_FORK_SUPPORT"] = "0"
|
||||||
from . import connpy_pb2, connpy_pb2_grpc, remote_plugin_pb2, remote_plugin_pb2_grpc
|
|
||||||
import json
|
|
||||||
from .utils import to_value, from_value, to_struct, from_struct
|
|
||||||
from ..services.exceptions import ConnpyError
|
|
||||||
from .. import printer
|
from .. import printer
|
||||||
|
from ..auth_utils import (
|
||||||
|
MAX_FALLBACK_ENTRIES,
|
||||||
|
MAX_JSON_INPUT_BYTES,
|
||||||
|
MAX_JSON_INPUT_DEPTH,
|
||||||
|
SanitizedAuthError,
|
||||||
|
sanitize_exception,
|
||||||
|
validate_auth_budget_tags,
|
||||||
|
validate_fallback_entries,
|
||||||
|
validate_json_depth_and_size,
|
||||||
|
)
|
||||||
|
from ..services.exceptions import ConnpyError
|
||||||
|
from . import connpy_pb2, connpy_pb2_grpc, remote_plugin_pb2, remote_plugin_pb2_grpc
|
||||||
|
from .utils import from_struct, from_value, to_struct, to_value
|
||||||
|
|
||||||
_current_user = contextvars.ContextVar("current_user", default=None)
|
_current_user = contextvars.ContextVar("current_user", default=None)
|
||||||
|
|
||||||
def handle_errors(func):
|
def handle_errors(func):
|
||||||
import inspect
|
import inspect
|
||||||
if inspect.isgeneratorfunction(func):
|
if inspect.isgeneratorfunction(func):
|
||||||
|
@functools.wraps(func)
|
||||||
def wrapper(*args, **kwargs):
|
def wrapper(*args, **kwargs):
|
||||||
try:
|
try:
|
||||||
for item in func(*args, **kwargs):
|
for item in func(*args, **kwargs):
|
||||||
@@ -41,6 +52,7 @@ def handle_errors(func):
|
|||||||
printer.clear_thread_state()
|
printer.clear_thread_state()
|
||||||
return wrapper
|
return wrapper
|
||||||
else:
|
else:
|
||||||
|
@functools.wraps(func)
|
||||||
def wrapper(*args, **kwargs):
|
def wrapper(*args, **kwargs):
|
||||||
try:
|
try:
|
||||||
return func(*args, **kwargs)
|
return func(*args, **kwargs)
|
||||||
@@ -60,11 +72,19 @@ def handle_errors(func):
|
|||||||
printer.clear_thread_state()
|
printer.clear_thread_state()
|
||||||
return wrapper
|
return wrapper
|
||||||
|
|
||||||
|
def _ensure_server_provider(provider):
|
||||||
|
if not hasattr(provider, "mode"):
|
||||||
|
from connpy.services.provider import ServiceProvider
|
||||||
|
|
||||||
|
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):
|
class NodeServicer(connpy_pb2_grpc.NodeServiceServicer):
|
||||||
def __init__(self, provider, registry=None, debug=False):
|
def __init__(self, provider, registry=None, debug=False):
|
||||||
if not hasattr(provider, "mode"):
|
provider = _ensure_server_provider(provider)
|
||||||
from connpy.services.provider import ServiceProvider
|
|
||||||
provider = ServiceProvider(provider, mode="local")
|
|
||||||
self._fallback_provider = provider
|
self._fallback_provider = provider
|
||||||
self._registry = registry
|
self._registry = registry
|
||||||
self.server_debug = debug
|
self.server_debug = debug
|
||||||
@@ -115,8 +135,24 @@ class NodeServicer(connpy_pb2_grpc.NodeServiceServicer):
|
|||||||
self.server_console.print(f"[debug][DEBUG][/debug] gRPC interact_node request for: [bold cyan]{unique_id}[/bold cyan]")
|
self.server_console.print(f"[debug][DEBUG][/debug] gRPC interact_node request for: [bold cyan]{unique_id}[/bold cyan]")
|
||||||
|
|
||||||
if first_req.connection_params_json:
|
if first_req.connection_params_json:
|
||||||
import json
|
try:
|
||||||
params = json.loads(first_req.connection_params_json)
|
params = validate_json_depth_and_size(
|
||||||
|
first_req.connection_params_json,
|
||||||
|
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||||
|
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||||
|
)
|
||||||
|
if not isinstance(params, dict):
|
||||||
|
raise TypeError("connection_params_json must be a JSON object")
|
||||||
|
if "fallback" in params and params["fallback"] not in (None, "", []):
|
||||||
|
validate_fallback_entries(
|
||||||
|
params["fallback"],
|
||||||
|
max_entries=MAX_FALLBACK_ENTRIES,
|
||||||
|
)
|
||||||
|
if "tags" in params and params["tags"] not in ("", None):
|
||||||
|
validate_auth_budget_tags(params["tags"])
|
||||||
|
except (ValueError, TypeError, SanitizedAuthError) as exc:
|
||||||
|
context.abort(grpc.StatusCode.INVALID_ARGUMENT, str(exc))
|
||||||
|
|
||||||
base_node_id = params.get("base_node")
|
base_node_id = params.get("base_node")
|
||||||
# Valid attributes that a node object accepts
|
# Valid attributes that a node object accepts
|
||||||
valid_attrs = ['host', 'options', 'logs', 'password', 'port', 'protocol', 'user', 'jumphost']
|
valid_attrs = ['host', 'options', 'logs', 'password', 'port', 'protocol', 'user', 'jumphost']
|
||||||
@@ -134,6 +170,8 @@ class NodeServicer(connpy_pb2_grpc.NodeServiceServicer):
|
|||||||
for attr in valid_attrs:
|
for attr in valid_attrs:
|
||||||
if attr in params:
|
if attr in params:
|
||||||
device[attr] = params[attr]
|
device[attr] = params[attr]
|
||||||
|
if "fallback" in params:
|
||||||
|
device["fallback"] = params["fallback"]
|
||||||
|
|
||||||
if "tags" in params:
|
if "tags" in params:
|
||||||
device_tags = device.get("tags", {})
|
device_tags = device.get("tags", {})
|
||||||
@@ -147,20 +185,30 @@ class NodeServicer(connpy_pb2_grpc.NodeServiceServicer):
|
|||||||
else:
|
else:
|
||||||
# base_node not found, fall back to dynamic
|
# base_node not found, fall back to dynamic
|
||||||
node_name = params.get("name", fallback_id)
|
node_name = params.get("name", fallback_id)
|
||||||
n = node(node_name, host=params.get("host", ""), config=user_config)
|
dyn_kwargs = {k: params[k] for k in valid_attrs if k in params and k != "host"}
|
||||||
for attr in valid_attrs:
|
if "fallback" in params:
|
||||||
if attr in params:
|
dyn_kwargs["fallback"] = params["fallback"]
|
||||||
setattr(n, attr, params[attr])
|
|
||||||
if "tags" in params:
|
if "tags" in params:
|
||||||
n.tags = params["tags"]
|
dyn_kwargs["tags"] = params["tags"]
|
||||||
|
n = node(
|
||||||
|
node_name,
|
||||||
|
host=params.get("host", ""),
|
||||||
|
config=user_config,
|
||||||
|
**dyn_kwargs,
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
node_name = params.get("name", fallback_id)
|
node_name = params.get("name", fallback_id)
|
||||||
n = node(node_name, host=params.get("host", ""), config=user_config)
|
dyn_kwargs = {k: params[k] for k in valid_attrs if k in params and k != "host"}
|
||||||
for attr in valid_attrs:
|
if "fallback" in params:
|
||||||
if attr in params:
|
dyn_kwargs["fallback"] = params["fallback"]
|
||||||
setattr(n, attr, params[attr])
|
|
||||||
if "tags" in params:
|
if "tags" in params:
|
||||||
n.tags = params["tags"]
|
dyn_kwargs["tags"] = params["tags"]
|
||||||
|
n = node(
|
||||||
|
node_name,
|
||||||
|
host=params.get("host", ""),
|
||||||
|
config=user_config,
|
||||||
|
**dyn_kwargs,
|
||||||
|
)
|
||||||
n._orig_protocol = n.protocol
|
n._orig_protocol = n.protocol
|
||||||
if sftp:
|
if sftp:
|
||||||
n.protocol = "sftp"
|
n.protocol = "sftp"
|
||||||
@@ -181,9 +229,11 @@ class NodeServicer(connpy_pb2_grpc.NodeServiceServicer):
|
|||||||
debug_chunks = []
|
debug_chunks = []
|
||||||
if debug:
|
if debug:
|
||||||
from io import StringIO
|
from io import StringIO
|
||||||
|
|
||||||
from rich.console import Console as RichConsole
|
from rich.console import Console as RichConsole
|
||||||
from ..printer import connpy_theme
|
|
||||||
from .. import printer as _printer
|
from .. import printer as _printer
|
||||||
|
from ..printer import connpy_theme
|
||||||
|
|
||||||
def remote_logger(msg_type, message):
|
def remote_logger(msg_type, message):
|
||||||
buf = StringIO()
|
buf = StringIO()
|
||||||
@@ -204,7 +254,14 @@ class NodeServicer(connpy_pb2_grpc.NodeServiceServicer):
|
|||||||
else:
|
else:
|
||||||
remote_logger = None
|
remote_logger = None
|
||||||
|
|
||||||
connect = n._connect(debug=debug, logger=remote_logger)
|
try:
|
||||||
|
connect = n._connect(
|
||||||
|
debug=debug,
|
||||||
|
logger=remote_logger,
|
||||||
|
attempt_tracker=getattr(provider, "auth_tracker", None),
|
||||||
|
)
|
||||||
|
except Exception as exc: # noqa: BLE001
|
||||||
|
connect = str(sanitize_exception(exc, code="AUTH_FAILED"))
|
||||||
|
|
||||||
# Send debug output to client before checking result (always show the command)
|
# Send debug output to client before checking result (always show the command)
|
||||||
for chunk in debug_chunks:
|
for chunk in debug_chunks:
|
||||||
@@ -535,10 +592,9 @@ class NodeServicer(connpy_pb2_grpc.NodeServiceServicer):
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
asyncio.run(n._async_interact_loop(remote_stream, resize_callback, copilot_handler=remote_copilot_handler))
|
asyncio.run(n._async_interact_loop(remote_stream, resize_callback, copilot_handler=remote_copilot_handler))
|
||||||
except Exception as e:
|
except Exception as e: # noqa: BLE001
|
||||||
import traceback
|
sanitized_err = sanitize_exception(e, code="TRANSPORT_ERROR", context_label="interact stream")
|
||||||
print(f"[ERROR in run_async_loop] {e}")
|
print(f"[ERROR in run_async_loop] {sanitized_err}")
|
||||||
traceback.print_exc()
|
|
||||||
finally:
|
finally:
|
||||||
n._teardown_interact_environment()
|
n._teardown_interact_environment()
|
||||||
response_queue.put(None) # Signal EOF
|
response_queue.put(None) # Signal EOF
|
||||||
@@ -641,9 +697,7 @@ class NodeServicer(connpy_pb2_grpc.NodeServiceServicer):
|
|||||||
|
|
||||||
class ProfileServicer(connpy_pb2_grpc.ProfileServiceServicer):
|
class ProfileServicer(connpy_pb2_grpc.ProfileServiceServicer):
|
||||||
def __init__(self, provider, registry=None):
|
def __init__(self, provider, registry=None):
|
||||||
if not hasattr(provider, "mode"):
|
provider = _ensure_server_provider(provider)
|
||||||
from connpy.services.provider import ServiceProvider
|
|
||||||
provider = ServiceProvider(provider, mode="local")
|
|
||||||
self._fallback_provider = provider
|
self._fallback_provider = provider
|
||||||
self._registry = registry
|
self._registry = registry
|
||||||
|
|
||||||
@@ -695,9 +749,7 @@ class ProfileServicer(connpy_pb2_grpc.ProfileServiceServicer):
|
|||||||
|
|
||||||
class ConfigServicer(connpy_pb2_grpc.ConfigServiceServicer):
|
class ConfigServicer(connpy_pb2_grpc.ConfigServiceServicer):
|
||||||
def __init__(self, provider, registry=None):
|
def __init__(self, provider, registry=None):
|
||||||
if not hasattr(provider, "mode"):
|
provider = _ensure_server_provider(provider)
|
||||||
from connpy.services.provider import ServiceProvider
|
|
||||||
provider = ServiceProvider(provider, mode="local")
|
|
||||||
self._fallback_provider = provider
|
self._fallback_provider = provider
|
||||||
self._registry = registry
|
self._registry = registry
|
||||||
|
|
||||||
@@ -740,9 +792,7 @@ class ConfigServicer(connpy_pb2_grpc.ConfigServiceServicer):
|
|||||||
|
|
||||||
class PluginServicer(connpy_pb2_grpc.PluginServiceServicer, remote_plugin_pb2_grpc.RemotePluginServiceServicer):
|
class PluginServicer(connpy_pb2_grpc.PluginServiceServicer, remote_plugin_pb2_grpc.RemotePluginServiceServicer):
|
||||||
def __init__(self, provider, registry=None):
|
def __init__(self, provider, registry=None):
|
||||||
if not hasattr(provider, "mode"):
|
provider = _ensure_server_provider(provider)
|
||||||
from connpy.services.provider import ServiceProvider
|
|
||||||
provider = ServiceProvider(provider, mode="local")
|
|
||||||
self._fallback_provider = provider
|
self._fallback_provider = provider
|
||||||
self._registry = registry
|
self._registry = registry
|
||||||
|
|
||||||
@@ -798,9 +848,7 @@ class PluginServicer(connpy_pb2_grpc.PluginServiceServicer, remote_plugin_pb2_gr
|
|||||||
|
|
||||||
class ExecutionServicer(connpy_pb2_grpc.ExecutionServiceServicer):
|
class ExecutionServicer(connpy_pb2_grpc.ExecutionServiceServicer):
|
||||||
def __init__(self, provider, registry=None):
|
def __init__(self, provider, registry=None):
|
||||||
if not hasattr(provider, "mode"):
|
provider = _ensure_server_provider(provider)
|
||||||
from connpy.services.provider import ServiceProvider
|
|
||||||
provider = ServiceProvider(provider, mode="local")
|
|
||||||
self._fallback_provider = provider
|
self._fallback_provider = provider
|
||||||
self._registry = registry
|
self._registry = registry
|
||||||
|
|
||||||
@@ -929,9 +977,7 @@ class ExecutionServicer(connpy_pb2_grpc.ExecutionServiceServicer):
|
|||||||
|
|
||||||
class ImportExportServicer(connpy_pb2_grpc.ImportExportServiceServicer):
|
class ImportExportServicer(connpy_pb2_grpc.ImportExportServiceServicer):
|
||||||
def __init__(self, provider, registry=None):
|
def __init__(self, provider, registry=None):
|
||||||
if not hasattr(provider, "mode"):
|
provider = _ensure_server_provider(provider)
|
||||||
from connpy.services.provider import ServiceProvider
|
|
||||||
provider = ServiceProvider(provider, mode="local")
|
|
||||||
self._fallback_provider = provider
|
self._fallback_provider = provider
|
||||||
self._registry = registry
|
self._registry = registry
|
||||||
|
|
||||||
@@ -1064,9 +1110,7 @@ class StatusBridge:
|
|||||||
|
|
||||||
class AIServicer(connpy_pb2_grpc.AIServiceServicer):
|
class AIServicer(connpy_pb2_grpc.AIServiceServicer):
|
||||||
def __init__(self, provider, registry=None, debug=False):
|
def __init__(self, provider, registry=None, debug=False):
|
||||||
if not hasattr(provider, "mode"):
|
provider = _ensure_server_provider(provider)
|
||||||
from connpy.services.provider import ServiceProvider
|
|
||||||
provider = ServiceProvider(provider, mode="local")
|
|
||||||
self._fallback_provider = provider
|
self._fallback_provider = provider
|
||||||
self._registry = registry
|
self._registry = registry
|
||||||
self.server_debug = debug
|
self.server_debug = debug
|
||||||
@@ -1351,9 +1395,7 @@ class AIServicer(connpy_pb2_grpc.AIServiceServicer):
|
|||||||
|
|
||||||
class SystemServicer(connpy_pb2_grpc.SystemServiceServicer):
|
class SystemServicer(connpy_pb2_grpc.SystemServiceServicer):
|
||||||
def __init__(self, provider, registry=None):
|
def __init__(self, provider, registry=None):
|
||||||
if not hasattr(provider, "mode"):
|
provider = _ensure_server_provider(provider)
|
||||||
from connpy.services.provider import ServiceProvider
|
|
||||||
provider = ServiceProvider(provider, mode="local")
|
|
||||||
self._fallback_provider = provider
|
self._fallback_provider = provider
|
||||||
self._registry = registry
|
self._registry = registry
|
||||||
|
|
||||||
@@ -1736,7 +1778,7 @@ def serve(config, port=8048, debug=False):
|
|||||||
from connpy.grpc_layer.user_registry import UserRegistry
|
from connpy.grpc_layer.user_registry import UserRegistry
|
||||||
from connpy.services.provider import ServiceProvider
|
from connpy.services.provider import ServiceProvider
|
||||||
|
|
||||||
fallback_provider = ServiceProvider(config, mode="local")
|
fallback_provider = ServiceProvider(config, mode="local", enforce_context=False)
|
||||||
registry = UserRegistry(config.defaultdir)
|
registry = UserRegistry(config.defaultdir)
|
||||||
|
|
||||||
# Check if trusted_gateway provider is configured if SSO Gateway Secret is present in env
|
# Check if trusted_gateway provider is configured if SSO Gateway Secret is present in env
|
||||||
|
|||||||
+187
-57
@@ -1,16 +1,19 @@
|
|||||||
import grpc
|
|
||||||
import queue
|
import queue
|
||||||
|
import re
|
||||||
import threading
|
import threading
|
||||||
from functools import wraps
|
from functools import wraps
|
||||||
|
|
||||||
|
import grpc
|
||||||
from google.protobuf.empty_pb2 import Empty
|
from google.protobuf.empty_pb2 import Empty
|
||||||
|
|
||||||
from . import connpy_pb2, connpy_pb2_grpc, remote_plugin_pb2, remote_plugin_pb2_grpc
|
|
||||||
from .utils import to_value, from_value, to_struct, from_struct
|
|
||||||
from ..services.exceptions import ConnpyError
|
|
||||||
from ..hooks import MethodHook
|
|
||||||
from .. import printer
|
from .. import printer
|
||||||
from ..cli.terminal_ui import CopilotInterface
|
from ..auth_utils import MAX_REMOTE_FILTER_NODES
|
||||||
|
from ..hooks import MethodHook
|
||||||
|
from ..services.exceptions import ConnpyError, NodeNotFoundError
|
||||||
from ..utils import log_cleaner
|
from ..utils import log_cleaner
|
||||||
|
from . import connpy_pb2, connpy_pb2_grpc, remote_plugin_pb2, remote_plugin_pb2_grpc
|
||||||
|
from .utils import from_struct, from_value, to_struct, to_value
|
||||||
|
|
||||||
|
|
||||||
def handle_errors(func):
|
def handle_errors(func):
|
||||||
@wraps(func)
|
@wraps(func)
|
||||||
@@ -43,8 +46,48 @@ class NodeStub:
|
|||||||
self.remote_host = remote_host
|
self.remote_host = remote_host
|
||||||
self.config = config
|
self.config = config
|
||||||
|
|
||||||
|
def _get_context_service(self):
|
||||||
|
if not getattr(self.config, "_enforce_context", True):
|
||||||
|
return None
|
||||||
|
if not isinstance(getattr(self.config, "config", None), dict):
|
||||||
|
return None
|
||||||
|
from ..services.context_service import ContextService
|
||||||
|
|
||||||
|
return ContextService(self.config)
|
||||||
|
|
||||||
|
def _ensure_in_active_context(
|
||||||
|
self, unique_id: str, *, is_folder: bool = False, not_found_msg: str | None = None
|
||||||
|
) -> None:
|
||||||
|
ctx_svc = self._get_context_service()
|
||||||
|
if (
|
||||||
|
ctx_svc is not None
|
||||||
|
and not ctx_svc.is_default_all_context()
|
||||||
|
and not ctx_svc.matches_active_context(unique_id)
|
||||||
|
):
|
||||||
|
if not_found_msg is not None:
|
||||||
|
raise NodeNotFoundError(not_found_msg)
|
||||||
|
label = "Folder" if is_folder else "Node"
|
||||||
|
raise NodeNotFoundError(f"{label} '{unique_id}' not found.")
|
||||||
|
|
||||||
|
@handle_errors
|
||||||
|
def _get_raw_nodes(self) -> list[str]:
|
||||||
|
req = connpy_pb2.FilterRequest(filter_str="", format_str="")
|
||||||
|
return from_value(self.stub.list_nodes(req).data) or []
|
||||||
|
|
||||||
|
@handle_errors
|
||||||
|
def _get_raw_folders(self) -> list[str]:
|
||||||
|
req = connpy_pb2.FilterRequest(filter_str="")
|
||||||
|
return from_value(self.stub.list_folders(req).data) or []
|
||||||
|
|
||||||
def _handle_remote_copilot(self, res, request_queue, response_queue, client_buffer_bytes, pause_generator, resume_generator, old_tty):
|
def _handle_remote_copilot(self, res, request_queue, response_queue, client_buffer_bytes, pause_generator, resume_generator, old_tty):
|
||||||
import json, asyncio, termios, sys, tty, queue, os
|
import asyncio
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import termios
|
||||||
|
import tty
|
||||||
|
|
||||||
|
from ..cli.terminal_ui import CopilotInterface
|
||||||
from ..core import copilot_terminal_mode
|
from ..core import copilot_terminal_mode
|
||||||
from . import connpy_pb2
|
from . import connpy_pb2
|
||||||
|
|
||||||
@@ -138,6 +181,7 @@ class NodeStub:
|
|||||||
|
|
||||||
@handle_errors
|
@handle_errors
|
||||||
def connect_node(self, unique_id, sftp=False, debug=False, logger=None):
|
def connect_node(self, unique_id, sftp=False, debug=False, logger=None):
|
||||||
|
self._ensure_in_active_context(unique_id, is_folder=False)
|
||||||
import sys
|
import sys
|
||||||
import select
|
import select
|
||||||
import tty
|
import tty
|
||||||
@@ -429,16 +473,26 @@ class NodeStub:
|
|||||||
@handle_errors
|
@handle_errors
|
||||||
def list_nodes(self, filter_str=None, format_str=None):
|
def list_nodes(self, filter_str=None, format_str=None):
|
||||||
req = connpy_pb2.FilterRequest(filter_str=filter_str or "", format_str=format_str or "")
|
req = connpy_pb2.FilterRequest(filter_str=filter_str or "", format_str=format_str or "")
|
||||||
return from_value(self.stub.list_nodes(req).data) or []
|
nodes = from_value(self.stub.list_nodes(req).data) or []
|
||||||
|
if not format_str:
|
||||||
|
ctx_svc = self._get_context_service()
|
||||||
|
if ctx_svc is not None and not ctx_svc.is_default_all_context():
|
||||||
|
nodes = ctx_svc.filter_node_list(result=nodes)
|
||||||
|
return nodes
|
||||||
|
|
||||||
@MethodHook
|
@MethodHook
|
||||||
@handle_errors
|
@handle_errors
|
||||||
def list_folders(self, filter_str=None):
|
def list_folders(self, filter_str=None):
|
||||||
req = connpy_pb2.FilterRequest(filter_str=filter_str or "")
|
req = connpy_pb2.FilterRequest(filter_str=filter_str or "")
|
||||||
return from_value(self.stub.list_folders(req).data) or []
|
folders = from_value(self.stub.list_folders(req).data) or []
|
||||||
|
ctx_svc = self._get_context_service()
|
||||||
|
if ctx_svc is not None and not ctx_svc.is_default_all_context():
|
||||||
|
folders = ctx_svc.filter_node_list(result=folders)
|
||||||
|
return folders
|
||||||
|
|
||||||
@handle_errors
|
@handle_errors
|
||||||
def get_node_details(self, unique_id):
|
def get_node_details(self, unique_id):
|
||||||
|
self._ensure_in_active_context(unique_id, is_folder=False)
|
||||||
return from_struct(self.stub.get_node_details(connpy_pb2.IdRequest(id=unique_id)).data)
|
return from_struct(self.stub.get_node_details(connpy_pb2.IdRequest(id=unique_id)).data)
|
||||||
|
|
||||||
@handle_errors
|
@handle_errors
|
||||||
@@ -446,8 +500,12 @@ class NodeStub:
|
|||||||
return from_value(self.stub.explode_unique(connpy_pb2.IdRequest(id=unique_id)).data)
|
return from_value(self.stub.explode_unique(connpy_pb2.IdRequest(id=unique_id)).data)
|
||||||
|
|
||||||
@handle_errors
|
@handle_errors
|
||||||
def validate_parent_folder(self, unique_id):
|
def validate_parent_folder(self, unique_id, is_folder=False):
|
||||||
self.stub.validate_parent_folder(connpy_pb2.IdRequest(id=unique_id))
|
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
|
@handle_errors
|
||||||
def generate_cache(self, nodes=None, folders=None, profiles=None):
|
def generate_cache(self, nodes=None, folders=None, profiles=None):
|
||||||
@@ -463,7 +521,7 @@ class NodeStub:
|
|||||||
# node cache is what matters for fzf. We'll fetch profiles if we can.
|
# node cache is what matters for fzf. We'll fetch profiles if we can.
|
||||||
# For now, let's sync what we have.
|
# For now, let's sync what we have.
|
||||||
|
|
||||||
if nodes is not None or folders is not None or profiles is not None:
|
if self.config is not None and (nodes is not None or folders is not None or profiles is not None):
|
||||||
self.config._generate_nodes_cache(nodes=nodes, folders=folders, profiles=profiles)
|
self.config._generate_nodes_cache(nodes=nodes, folders=folders, profiles=profiles)
|
||||||
|
|
||||||
def _trigger_local_cache_sync(self):
|
def _trigger_local_cache_sync(self):
|
||||||
@@ -478,12 +536,16 @@ class NodeStub:
|
|||||||
|
|
||||||
@handle_errors
|
@handle_errors
|
||||||
def add_node(self, unique_id, data, is_folder=False):
|
def add_node(self, unique_id, data, is_folder=False):
|
||||||
|
ctx_svc = self._get_context_service()
|
||||||
|
if ctx_svc is not None:
|
||||||
|
ctx_svc.validate_in_active_context(unique_id, is_folder=is_folder)
|
||||||
req = connpy_pb2.NodeRequest(id=unique_id, data=to_struct(data), is_folder=is_folder)
|
req = connpy_pb2.NodeRequest(id=unique_id, data=to_struct(data), is_folder=is_folder)
|
||||||
self.stub.add_node(req)
|
self.stub.add_node(req)
|
||||||
self._trigger_local_cache_sync()
|
self._trigger_local_cache_sync()
|
||||||
|
|
||||||
@handle_errors
|
@handle_errors
|
||||||
def update_node(self, unique_id, data, save=True):
|
def update_node(self, unique_id, data, save=True):
|
||||||
|
self._ensure_in_active_context(unique_id, is_folder=False)
|
||||||
req = connpy_pb2.NodeRequest(id=unique_id, data=to_struct(data), is_folder=False)
|
req = connpy_pb2.NodeRequest(id=unique_id, data=to_struct(data), is_folder=False)
|
||||||
self.stub.update_node(req)
|
self.stub.update_node(req)
|
||||||
if save:
|
if save:
|
||||||
@@ -491,6 +553,7 @@ class NodeStub:
|
|||||||
|
|
||||||
@handle_errors
|
@handle_errors
|
||||||
def delete_node(self, unique_id, is_folder=False, save=True):
|
def delete_node(self, unique_id, is_folder=False, save=True):
|
||||||
|
self._ensure_in_active_context(unique_id, is_folder=is_folder)
|
||||||
req = connpy_pb2.DeleteRequest(id=unique_id, is_folder=is_folder)
|
req = connpy_pb2.DeleteRequest(id=unique_id, is_folder=is_folder)
|
||||||
self.stub.delete_node(req)
|
self.stub.delete_node(req)
|
||||||
if save:
|
if save:
|
||||||
@@ -498,12 +561,22 @@ class NodeStub:
|
|||||||
|
|
||||||
@handle_errors
|
@handle_errors
|
||||||
def move_node(self, src_id, dst_id, copy=False):
|
def move_node(self, src_id, dst_id, copy=False):
|
||||||
|
self._ensure_in_active_context(
|
||||||
|
src_id, is_folder=False, not_found_msg=f"Source node '{src_id}' not found."
|
||||||
|
)
|
||||||
|
ctx_svc = self._get_context_service()
|
||||||
|
if ctx_svc is not None:
|
||||||
|
ctx_svc.validate_in_active_context(dst_id, is_folder=False)
|
||||||
req = connpy_pb2.MoveRequest(src_id=src_id, dst_id=dst_id, copy=copy)
|
req = connpy_pb2.MoveRequest(src_id=src_id, dst_id=dst_id, copy=copy)
|
||||||
self.stub.move_node(req)
|
self.stub.move_node(req)
|
||||||
self._trigger_local_cache_sync()
|
self._trigger_local_cache_sync()
|
||||||
|
|
||||||
@handle_errors
|
@handle_errors
|
||||||
def bulk_add(self, ids, hosts, common_data):
|
def bulk_add(self, ids, hosts, common_data):
|
||||||
|
ctx_svc = self._get_context_service()
|
||||||
|
if ctx_svc is not None:
|
||||||
|
for uid in ids:
|
||||||
|
ctx_svc.validate_in_active_context(uid, is_folder=False)
|
||||||
req = connpy_pb2.BulkRequest(ids=ids, hosts=hosts, common_data=to_struct(common_data))
|
req = connpy_pb2.BulkRequest(ids=ids, hosts=hosts, common_data=to_struct(common_data))
|
||||||
self.stub.bulk_add(req)
|
self.stub.bulk_add(req)
|
||||||
self._trigger_local_cache_sync()
|
self._trigger_local_cache_sync()
|
||||||
@@ -649,68 +722,125 @@ class PluginStub:
|
|||||||
yield chunk.text
|
yield chunk.text
|
||||||
|
|
||||||
class ExecutionStub:
|
class ExecutionStub:
|
||||||
def __init__(self, channel, remote_host):
|
def __init__(self, channel, remote_host, config=None, node_stub=None):
|
||||||
self.stub = connpy_pb2_grpc.ExecutionServiceStub(channel)
|
self.stub = connpy_pb2_grpc.ExecutionServiceStub(channel)
|
||||||
self.remote_host = remote_host
|
self.remote_host = remote_host
|
||||||
|
self.config = config
|
||||||
|
self.node_stub = node_stub
|
||||||
|
|
||||||
|
def _get_context_service(self):
|
||||||
|
if not getattr(self.config, "_enforce_context", True):
|
||||||
|
return None
|
||||||
|
if not isinstance(getattr(self.config, "config", None), dict):
|
||||||
|
return None
|
||||||
|
from ..services.context_service import ContextService
|
||||||
|
|
||||||
|
return ContextService(self.config)
|
||||||
|
|
||||||
|
def _filter_candidates_by_filter_str(self, candidates: list[str], nodes_filter: str) -> list[str]:
|
||||||
|
if not nodes_filter:
|
||||||
|
return list(candidates)
|
||||||
|
cfg_dict = getattr(self.config, "config", None)
|
||||||
|
case = cfg_dict.get("case", False) if isinstance(cfg_dict, dict) else False
|
||||||
|
flags = 0 if case else re.IGNORECASE
|
||||||
|
return [c for c in candidates if re.search(nodes_filter, c, flags)]
|
||||||
|
|
||||||
|
def _resolve_remote_batches(self, nodes_filter):
|
||||||
|
ctx_svc = self._get_context_service()
|
||||||
|
if ctx_svc is not None and not ctx_svc.is_default_all_context():
|
||||||
|
if isinstance(nodes_filter, (list, tuple)):
|
||||||
|
permitted = ctx_svc.filter_node_list(result=list(nodes_filter))
|
||||||
|
else:
|
||||||
|
candidates = (
|
||||||
|
self.node_stub.list_nodes(filter_str=nodes_filter)
|
||||||
|
if self.node_stub is not None
|
||||||
|
else []
|
||||||
|
)
|
||||||
|
candidates = self._filter_candidates_by_filter_str(candidates, nodes_filter)
|
||||||
|
permitted = ctx_svc.filter_node_list(result=candidates)
|
||||||
|
if not permitted:
|
||||||
|
raise ConnpyError(f"No nodes found matching filter: {nodes_filter}")
|
||||||
|
patterns = [f"^{re.escape(n)}$" for n in permitted]
|
||||||
|
return [
|
||||||
|
patterns[i : i + MAX_REMOTE_FILTER_NODES]
|
||||||
|
for i in range(0, len(patterns), MAX_REMOTE_FILTER_NODES)
|
||||||
|
]
|
||||||
|
return [[nodes_filter] if isinstance(nodes_filter, str) else list(nodes_filter)]
|
||||||
|
|
||||||
@handle_errors
|
@handle_errors
|
||||||
def run_commands(self, nodes_filter, commands, variables=None, parallel=10, timeout=10, folder=None, prompt=None, **kwargs):
|
def run_commands(self, nodes_filter, commands, variables=None, parallel=10, timeout=10, folder=None, prompt=None, **kwargs):
|
||||||
nodes_list = [nodes_filter] if isinstance(nodes_filter, str) else list(nodes_filter)
|
batches = self._resolve_remote_batches(nodes_filter)
|
||||||
req = connpy_pb2.RunRequest(
|
|
||||||
nodes=nodes_list,
|
|
||||||
commands=commands,
|
|
||||||
folder=folder or "",
|
|
||||||
prompt=prompt or "",
|
|
||||||
parallel=parallel,
|
|
||||||
timeout=timeout,
|
|
||||||
name=kwargs.get("name", "")
|
|
||||||
)
|
|
||||||
if variables is not None:
|
|
||||||
req.vars.CopyFrom(to_struct(variables))
|
|
||||||
|
|
||||||
final_results = {}
|
final_results = {}
|
||||||
on_complete = kwargs.get("on_node_complete")
|
on_complete = kwargs.get("on_node_complete")
|
||||||
|
|
||||||
for response in self.stub.run_commands(req):
|
for nodes_list in batches:
|
||||||
if on_complete:
|
req = connpy_pb2.RunRequest(
|
||||||
on_complete(response.unique_id, response.output, response.status)
|
nodes=nodes_list,
|
||||||
final_results[response.unique_id] = {
|
commands=commands,
|
||||||
"output": response.output,
|
folder=folder or "",
|
||||||
"status": response.status
|
prompt=prompt or "",
|
||||||
}
|
parallel=parallel,
|
||||||
|
timeout=timeout,
|
||||||
|
name=kwargs.get("name", "")
|
||||||
|
)
|
||||||
|
if variables is not None:
|
||||||
|
req.vars.CopyFrom(to_struct(variables))
|
||||||
|
|
||||||
|
for response in self.stub.run_commands(req):
|
||||||
|
if on_complete:
|
||||||
|
on_complete(response.unique_id, response.output, response.status)
|
||||||
|
final_results[response.unique_id] = {
|
||||||
|
"output": response.output,
|
||||||
|
"status": response.status
|
||||||
|
}
|
||||||
|
|
||||||
return final_results
|
return final_results
|
||||||
|
|
||||||
@handle_errors
|
@handle_errors
|
||||||
def test_commands(self, nodes_filter, commands, expected, variables=None, parallel=10, timeout=10, prompt=None, **kwargs):
|
def test_commands(self, nodes_filter, commands, expected, variables=None, parallel=10, timeout=10, prompt=None, **kwargs):
|
||||||
nodes_list = [nodes_filter] if isinstance(nodes_filter, str) else list(nodes_filter)
|
batches = self._resolve_remote_batches(nodes_filter)
|
||||||
req = connpy_pb2.TestRequest(
|
|
||||||
nodes=nodes_list,
|
|
||||||
commands=commands,
|
|
||||||
expected=expected if isinstance(expected, list) else [expected],
|
|
||||||
folder=kwargs.get("folder", ""),
|
|
||||||
prompt=prompt or "",
|
|
||||||
parallel=parallel,
|
|
||||||
timeout=timeout,
|
|
||||||
name=kwargs.get("name", "")
|
|
||||||
)
|
|
||||||
if variables is not None:
|
|
||||||
req.vars.CopyFrom(to_struct(variables))
|
|
||||||
|
|
||||||
final_results = {}
|
final_results = {}
|
||||||
on_complete = kwargs.get("on_node_complete")
|
on_complete = kwargs.get("on_node_complete")
|
||||||
|
|
||||||
for response in self.stub.test_commands(req):
|
for nodes_list in batches:
|
||||||
result_dict = from_struct(response.test_result) if response.HasField("test_result") else {}
|
req = connpy_pb2.TestRequest(
|
||||||
if on_complete:
|
nodes=nodes_list,
|
||||||
on_complete(response.unique_id, response.output, response.status, result_dict)
|
commands=commands,
|
||||||
final_results[response.unique_id] = result_dict
|
expected=expected if isinstance(expected, list) else [expected],
|
||||||
|
folder=kwargs.get("folder", ""),
|
||||||
|
prompt=prompt or "",
|
||||||
|
parallel=parallel,
|
||||||
|
timeout=timeout,
|
||||||
|
name=kwargs.get("name", "")
|
||||||
|
)
|
||||||
|
if variables is not None:
|
||||||
|
req.vars.CopyFrom(to_struct(variables))
|
||||||
|
|
||||||
|
for response in self.stub.test_commands(req):
|
||||||
|
result_dict = from_struct(response.test_result) if response.HasField("test_result") else {}
|
||||||
|
if on_complete:
|
||||||
|
on_complete(response.unique_id, response.output, response.status, result_dict)
|
||||||
|
final_results[response.unique_id] = result_dict
|
||||||
|
|
||||||
return final_results
|
return final_results
|
||||||
|
|
||||||
@handle_errors
|
@handle_errors
|
||||||
def run_cli_script(self, nodes_filter, script_path, parallel=10):
|
def run_cli_script(self, nodes_filter, script_path, parallel=10):
|
||||||
req = connpy_pb2.ScriptRequest(param1=nodes_filter, param2=script_path, parallel=parallel)
|
ctx_svc = self._get_context_service()
|
||||||
|
effective_filter = nodes_filter
|
||||||
|
if ctx_svc is not None and not ctx_svc.is_default_all_context():
|
||||||
|
candidates = (
|
||||||
|
self.node_stub.list_nodes(filter_str=nodes_filter)
|
||||||
|
if self.node_stub is not None
|
||||||
|
else []
|
||||||
|
)
|
||||||
|
if isinstance(nodes_filter, str):
|
||||||
|
candidates = self._filter_candidates_by_filter_str(candidates, nodes_filter)
|
||||||
|
permitted = ctx_svc.filter_node_list(result=candidates)
|
||||||
|
if not permitted:
|
||||||
|
raise ConnpyError(f"No nodes found matching filter: {nodes_filter}")
|
||||||
|
effective_filter = "|".join(f"^{re.escape(n)}$" for n in permitted)
|
||||||
|
req = connpy_pb2.ScriptRequest(param1=effective_filter, param2=script_path, parallel=parallel)
|
||||||
return from_struct(self.stub.run_cli_script(req).data)
|
return from_struct(self.stub.run_cli_script(req).data)
|
||||||
|
|
||||||
class ImportExportStub:
|
class ImportExportStub:
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ class UserRegistry:
|
|||||||
try:
|
try:
|
||||||
# Attempt a fresh configuration load
|
# Attempt a fresh configuration load
|
||||||
config = configfile(conf=conf_file, shared_config=self._shared_config)
|
config = configfile(conf=conf_file, shared_config=self._shared_config)
|
||||||
new_provider = ServiceProvider(config, mode="local")
|
new_provider = ServiceProvider(config, mode="local", enforce_context=False)
|
||||||
|
|
||||||
# Successfully loaded, clean up the old provider
|
# Successfully loaded, clean up the old provider
|
||||||
if old_provider:
|
if old_provider:
|
||||||
|
|||||||
+122
-52
@@ -6,6 +6,20 @@ from typing import Any, ClassVar
|
|||||||
|
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
|
from .auth_utils import (
|
||||||
|
MAX_FALLBACK_ENTRIES,
|
||||||
|
MAX_JSON_INPUT_BYTES,
|
||||||
|
MAX_JSON_INPUT_DEPTH,
|
||||||
|
SanitizedAuthError,
|
||||||
|
is_compose_expression,
|
||||||
|
mask_node_secrets,
|
||||||
|
sanitize_exception,
|
||||||
|
validate_auth_budget_tags,
|
||||||
|
validate_compose_syntax,
|
||||||
|
validate_fallback_entries,
|
||||||
|
validate_json_depth_and_size,
|
||||||
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from mcp.server.mcpserver import MCPServer
|
from mcp.server.mcpserver import MCPServer
|
||||||
except ImportError:
|
except ImportError:
|
||||||
@@ -124,20 +138,7 @@ class ConnpyMCPServer:
|
|||||||
|
|
||||||
def _mask_secrets(self, val: Any) -> Any:
|
def _mask_secrets(self, val: Any) -> Any:
|
||||||
"""Recursively mask passwords, passphrases, secrets, and auth tokens with '***'."""
|
"""Recursively mask passwords, passphrases, secrets, and auth tokens with '***'."""
|
||||||
if isinstance(val, dict):
|
return mask_node_secrets(val)
|
||||||
masked = {}
|
|
||||||
for k, v in val.items():
|
|
||||||
k_lower = str(k).lower()
|
|
||||||
if any(sec in k_lower for sec in ["password", "passphrase", "secret", "token"]):
|
|
||||||
masked[k] = "***"
|
|
||||||
elif isinstance(v, (dict, list)):
|
|
||||||
masked[k] = self._mask_secrets(v)
|
|
||||||
else:
|
|
||||||
masked[k] = v
|
|
||||||
return masked
|
|
||||||
if isinstance(val, list):
|
|
||||||
return [self._mask_secrets(item) for item in val]
|
|
||||||
return val
|
|
||||||
|
|
||||||
# -------------------------------------------------------------------------
|
# -------------------------------------------------------------------------
|
||||||
# Level 1: Read-Only Inventory and Context Tools
|
# Level 1: Read-Only Inventory and Context Tools
|
||||||
@@ -196,6 +197,31 @@ class ConnpyMCPServer:
|
|||||||
# Level 2: Execution, Testing, and Playbooks Tools
|
# Level 2: Execution, Testing, and Playbooks Tools
|
||||||
# -------------------------------------------------------------------------
|
# -------------------------------------------------------------------------
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _parse_json_list_or_lines(raw_val: list[str] | str, *, allow_empty_str: bool = False) -> list[str]:
|
||||||
|
if isinstance(raw_val, str):
|
||||||
|
if len(raw_val.encode("utf-8", errors="replace")) > MAX_JSON_INPUT_BYTES:
|
||||||
|
raise ValueError(f"Input exceeds maximum size of {MAX_JSON_INPUT_BYTES} bytes")
|
||||||
|
stripped = raw_val.strip()
|
||||||
|
if stripped.startswith(("[", "{")):
|
||||||
|
parsed = validate_json_depth_and_size(
|
||||||
|
stripped,
|
||||||
|
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||||
|
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||||
|
)
|
||||||
|
return parsed if isinstance(parsed, list) else [raw_val]
|
||||||
|
return ([raw_val] if raw_val else []) if allow_empty_str else [
|
||||||
|
c.strip() for c in raw_val.splitlines() if c.strip()
|
||||||
|
]
|
||||||
|
if isinstance(raw_val, list):
|
||||||
|
validate_json_depth_and_size(
|
||||||
|
raw_val,
|
||||||
|
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||||
|
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||||
|
)
|
||||||
|
return raw_val
|
||||||
|
return [str(raw_val)] if (raw_val or not allow_empty_str) else []
|
||||||
|
|
||||||
def run_commands(
|
def run_commands(
|
||||||
self,
|
self,
|
||||||
nodes_filter: str,
|
nodes_filter: str,
|
||||||
@@ -210,17 +236,10 @@ class ConnpyMCPServer:
|
|||||||
if not self.services or not hasattr(self.services, "execution"):
|
if not self.services or not hasattr(self.services, "execution"):
|
||||||
return {"success": False, "error": "Execution service unavailable."}
|
return {"success": False, "error": "Execution service unavailable."}
|
||||||
|
|
||||||
# Normalize commands list
|
try:
|
||||||
if isinstance(commands, str):
|
commands = self._parse_json_list_or_lines(commands, allow_empty_str=False)
|
||||||
try:
|
except ValueError as exc:
|
||||||
import json
|
return {"success": False, "error": str(exc)}
|
||||||
|
|
||||||
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)]
|
|
||||||
|
|
||||||
# Check safety
|
# Check safety
|
||||||
if not self.trust:
|
if not self.trust:
|
||||||
@@ -239,9 +258,24 @@ class ConnpyMCPServer:
|
|||||||
parallel=par_count,
|
parallel=par_count,
|
||||||
timeout=timeout,
|
timeout=timeout,
|
||||||
)
|
)
|
||||||
return {"success": True, "results": results}
|
fb_used = False
|
||||||
|
if isinstance(results, dict):
|
||||||
|
fb_used = any(
|
||||||
|
isinstance(v, dict) and bool(v.get("fallback_used", False))
|
||||||
|
for v in results.values()
|
||||||
|
)
|
||||||
|
if not fb_used:
|
||||||
|
fb_map = getattr(self.services.execution, "last_fallback_used", {})
|
||||||
|
if isinstance(fb_map, dict):
|
||||||
|
fb_used = any(bool(v) for v in fb_map.values())
|
||||||
|
return {"success": True, "results": results, "fallback_used": fb_used}
|
||||||
except Exception as e: # noqa: BLE001
|
except Exception as e: # noqa: BLE001
|
||||||
return {"success": False, "error": str(e)}
|
san = sanitize_exception(
|
||||||
|
e,
|
||||||
|
code="AUTH_FAILED",
|
||||||
|
context_label="command execution failed",
|
||||||
|
)
|
||||||
|
return {"success": False, "error": str(san)}
|
||||||
|
|
||||||
def test_commands(
|
def test_commands(
|
||||||
self,
|
self,
|
||||||
@@ -255,29 +289,11 @@ class ConnpyMCPServer:
|
|||||||
if not self.services or not hasattr(self.services, "execution"):
|
if not self.services or not hasattr(self.services, "execution"):
|
||||||
return {"success": False, "error": "Execution service unavailable."}
|
return {"success": False, "error": "Execution service unavailable."}
|
||||||
|
|
||||||
# Normalize commands
|
try:
|
||||||
if isinstance(commands, str):
|
commands = self._parse_json_list_or_lines(commands, allow_empty_str=False)
|
||||||
try:
|
expected = self._parse_json_list_or_lines(expected, allow_empty_str=True)
|
||||||
import json
|
except ValueError as exc:
|
||||||
|
return {"success": False, "error": str(exc)}
|
||||||
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 []
|
|
||||||
|
|
||||||
# Check safety
|
# Check safety
|
||||||
if not self.trust:
|
if not self.trust:
|
||||||
@@ -297,9 +313,16 @@ class ConnpyMCPServer:
|
|||||||
parallel=par_count,
|
parallel=par_count,
|
||||||
timeout=timeout,
|
timeout=timeout,
|
||||||
)
|
)
|
||||||
return {"success": True, "results": results}
|
fb_map = getattr(self.services.execution, "last_fallback_used", {})
|
||||||
|
fb_used = any(bool(v) for v in fb_map.values()) if isinstance(fb_map, dict) else False
|
||||||
|
return {"success": True, "results": results, "fallback_used": fb_used}
|
||||||
except Exception as e: # noqa: BLE001
|
except Exception as e: # noqa: BLE001
|
||||||
return {"success": False, "error": str(e)}
|
san = sanitize_exception(
|
||||||
|
e,
|
||||||
|
code="AUTH_FAILED",
|
||||||
|
context_label="test execution failed",
|
||||||
|
)
|
||||||
|
return {"success": False, "error": str(san)}
|
||||||
|
|
||||||
def validate_playbook(self, playbook_yaml: str) -> dict:
|
def validate_playbook(self, playbook_yaml: str) -> dict:
|
||||||
"""Validate the syntax, schema, and tasks of a Connpy YAML playbook."""
|
"""Validate the syntax, schema, and tasks of a Connpy YAML playbook."""
|
||||||
@@ -427,6 +450,47 @@ class ConnpyMCPServer:
|
|||||||
# Level 3: Inventory Mutation Tools (CRUD - Requires trust=True)
|
# Level 3: Inventory Mutation Tools (CRUD - Requires trust=True)
|
||||||
# -------------------------------------------------------------------------
|
# -------------------------------------------------------------------------
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _validate_and_normalize_node_payload(node_data: dict[str, Any]) -> dict[str, Any]:
|
||||||
|
validate_json_depth_and_size(
|
||||||
|
node_data,
|
||||||
|
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||||
|
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||||
|
)
|
||||||
|
normalized = node_data.copy()
|
||||||
|
if "tags" in normalized and normalized["tags"] not in ("", None):
|
||||||
|
tags_val = normalized["tags"]
|
||||||
|
if isinstance(tags_val, str) and tags_val.strip().startswith("{"):
|
||||||
|
tags_val = validate_json_depth_and_size(
|
||||||
|
tags_val,
|
||||||
|
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||||
|
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||||
|
)
|
||||||
|
normalized["tags"] = tags_val
|
||||||
|
validate_auth_budget_tags(tags_val)
|
||||||
|
|
||||||
|
if "fallback" in normalized and normalized["fallback"] not in (None, "", []):
|
||||||
|
fb_val = normalized["fallback"]
|
||||||
|
if isinstance(fb_val, str) and fb_val.strip().startswith(("[", "{")):
|
||||||
|
fb_val = validate_json_depth_and_size(
|
||||||
|
fb_val,
|
||||||
|
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||||
|
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||||
|
)
|
||||||
|
normalized["fallback"] = validate_fallback_entries(
|
||||||
|
fb_val,
|
||||||
|
max_entries=MAX_FALLBACK_ENTRIES,
|
||||||
|
)
|
||||||
|
|
||||||
|
pw = normalized.get("password")
|
||||||
|
if is_compose_expression(pw):
|
||||||
|
validate_compose_syntax(pw)
|
||||||
|
elif isinstance(pw, (list, tuple)):
|
||||||
|
for item in pw:
|
||||||
|
if is_compose_expression(item):
|
||||||
|
validate_compose_syntax(item)
|
||||||
|
return normalized
|
||||||
|
|
||||||
def add_node(
|
def add_node(
|
||||||
self, unique_id: str, data: dict | None = None, is_folder: bool = False, **kwargs
|
self, unique_id: str, data: dict | None = None, is_folder: bool = False, **kwargs
|
||||||
) -> dict:
|
) -> dict:
|
||||||
@@ -444,11 +508,14 @@ class ConnpyMCPServer:
|
|||||||
node_data.update(kwargs)
|
node_data.update(kwargs)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
node_data = self._validate_and_normalize_node_payload(node_data)
|
||||||
self.services.nodes.add_node(unique_id, node_data, is_folder=is_folder)
|
self.services.nodes.add_node(unique_id, node_data, is_folder=is_folder)
|
||||||
return {
|
return {
|
||||||
"success": True,
|
"success": True,
|
||||||
"message": f"{'Folder' if is_folder else 'Node'} '{unique_id}' created successfully.",
|
"message": f"{'Folder' if is_folder else 'Node'} '{unique_id}' created successfully.",
|
||||||
}
|
}
|
||||||
|
except (ValueError, TypeError, SanitizedAuthError) as exc:
|
||||||
|
return {"success": False, "error": str(exc)}
|
||||||
except Exception as e: # noqa: BLE001
|
except Exception as e: # noqa: BLE001
|
||||||
return {"success": False, "error": str(e)}
|
return {"success": False, "error": str(e)}
|
||||||
|
|
||||||
@@ -467,8 +534,11 @@ class ConnpyMCPServer:
|
|||||||
node_updates.update(kwargs)
|
node_updates.update(kwargs)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
node_updates = self._validate_and_normalize_node_payload(node_updates)
|
||||||
self.services.nodes.update_node(unique_id, node_updates)
|
self.services.nodes.update_node(unique_id, node_updates)
|
||||||
return {"success": True, "message": f"Node '{unique_id}' updated successfully."}
|
return {"success": True, "message": f"Node '{unique_id}' updated successfully."}
|
||||||
|
except (ValueError, TypeError, SanitizedAuthError) as exc:
|
||||||
|
return {"success": False, "error": str(exc)}
|
||||||
except Exception as e: # noqa: BLE001
|
except Exception as e: # noqa: BLE001
|
||||||
return {"success": False, "error": str(e)}
|
return {"success": False, "error": str(e)}
|
||||||
|
|
||||||
|
|||||||
@@ -1,21 +1,45 @@
|
|||||||
import re
|
import re
|
||||||
from typing import List, Dict, Any
|
from typing import Any
|
||||||
|
|
||||||
|
from ..auth_utils import (
|
||||||
|
MAX_CONTEXT_PATTERN_LEN,
|
||||||
|
MAX_CONTEXT_REGEXES,
|
||||||
|
compile_active_context_patterns,
|
||||||
|
is_default_all_context_cfg,
|
||||||
|
match_active_context_patterns,
|
||||||
|
)
|
||||||
from .base import BaseService
|
from .base import BaseService
|
||||||
from ..hooks import MethodHook
|
from .exceptions import InvalidConfigurationError
|
||||||
from .. import printer
|
|
||||||
|
|
||||||
class ContextService(BaseService):
|
class ContextService(BaseService):
|
||||||
"""Business logic for managing and applying regex-based contexts locally."""
|
"""Business logic for managing and applying regex-based contexts locally."""
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _validate_regexes(regexes: Any) -> None:
|
||||||
|
if not isinstance(regexes, list) or not (1 <= len(regexes) <= MAX_CONTEXT_REGEXES):
|
||||||
|
raise ValueError(
|
||||||
|
f"Context regexes must be a list of 1 to {MAX_CONTEXT_REGEXES} patterns"
|
||||||
|
)
|
||||||
|
for pat in regexes:
|
||||||
|
if not isinstance(pat, str) or not (1 <= len(pat) <= MAX_CONTEXT_PATTERN_LEN):
|
||||||
|
raise ValueError(
|
||||||
|
f"Each context regex must be a non-empty string up to {MAX_CONTEXT_PATTERN_LEN} characters"
|
||||||
|
)
|
||||||
|
try:
|
||||||
|
re.compile(pat)
|
||||||
|
except re.error as exc:
|
||||||
|
raise ValueError(f"Invalid context regex {pat!r}: {exc}") from None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def contexts(self) -> Dict[str, List[str]]:
|
def contexts(self) -> dict[str, list[str]]:
|
||||||
return self.config.config.get("contexts", {"all": [".*"]})
|
return self.config.config.get("contexts", {"all": [".*"]})
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def current_context(self) -> str:
|
def current_context(self) -> str:
|
||||||
return self.config.config.get("current_context", "all")
|
return self.config.config.get("current_context", "all")
|
||||||
|
|
||||||
def list_contexts(self) -> List[Dict[str, Any]]:
|
def list_contexts(self) -> list[dict[str, Any]]:
|
||||||
result = []
|
result = []
|
||||||
for name in self.contexts.keys():
|
for name in self.contexts.keys():
|
||||||
result.append({
|
result.append({
|
||||||
@@ -25,27 +49,29 @@ class ContextService(BaseService):
|
|||||||
})
|
})
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def add_context(self, name: str, regexes: List[str]):
|
def add_context(self, name: str, regexes: list[str]):
|
||||||
if not name.isalnum():
|
if not name.isalnum():
|
||||||
raise ValueError("Context name must be alphanumeric")
|
raise ValueError("Context name must be alphanumeric")
|
||||||
|
self._validate_regexes(regexes)
|
||||||
|
|
||||||
ctxs = self.contexts
|
ctxs = self.contexts
|
||||||
if name in ctxs:
|
if name in ctxs:
|
||||||
raise ValueError(f"Context '{name}' already exists")
|
raise ValueError(f"Context '{name}' already exists")
|
||||||
|
|
||||||
ctxs[name] = regexes
|
ctxs[name] = list(regexes)
|
||||||
self.config.config["contexts"] = ctxs
|
self.config.config["contexts"] = ctxs
|
||||||
self.config._saveconfig(self.config.file)
|
self.config._saveconfig(self.config.file)
|
||||||
|
|
||||||
def update_context(self, name: str, regexes: List[str]):
|
def update_context(self, name: str, regexes: list[str]):
|
||||||
if name == "all":
|
if name == "all":
|
||||||
raise ValueError("Cannot modify default context 'all'")
|
raise ValueError("Cannot modify default context 'all'")
|
||||||
|
self._validate_regexes(regexes)
|
||||||
|
|
||||||
ctxs = self.contexts
|
ctxs = self.contexts
|
||||||
if name not in ctxs:
|
if name not in ctxs:
|
||||||
raise ValueError(f"Context '{name}' does not exist")
|
raise ValueError(f"Context '{name}' does not exist")
|
||||||
|
|
||||||
ctxs[name] = regexes
|
ctxs[name] = list(regexes)
|
||||||
self.config.config["contexts"] = ctxs
|
self.config.config["contexts"] = ctxs
|
||||||
self.config._saveconfig(self.config.file)
|
self.config._saveconfig(self.config.file)
|
||||||
|
|
||||||
@@ -66,16 +92,48 @@ class ContextService(BaseService):
|
|||||||
def set_active_context(self, name: str):
|
def set_active_context(self, name: str):
|
||||||
if name not in self.contexts:
|
if name not in self.contexts:
|
||||||
raise ValueError(f"Context '{name}' does not exist")
|
raise ValueError(f"Context '{name}' does not exist")
|
||||||
|
|
||||||
|
prev_context = self.current_context
|
||||||
self.config.config["current_context"] = name
|
self.config.config["current_context"] = name
|
||||||
self.config._saveconfig(self.config.file)
|
self.config._saveconfig(self.config.file)
|
||||||
|
can_refresh = (
|
||||||
|
self.config._is_context_filtering_enabled_unlocked()
|
||||||
|
if (
|
||||||
|
not hasattr(self.config, "assert_called")
|
||||||
|
and callable(getattr(self.config, "_is_context_filtering_enabled_unlocked", None))
|
||||||
|
)
|
||||||
|
else getattr(self.config, "_enforce_context", True)
|
||||||
|
)
|
||||||
|
if (
|
||||||
|
name == "all"
|
||||||
|
and prev_context != "all"
|
||||||
|
and can_refresh
|
||||||
|
and hasattr(self.config, "_generate_nodes_cache")
|
||||||
|
):
|
||||||
|
self.config._generate_nodes_cache()
|
||||||
|
|
||||||
def get_active_regexes(self) -> List[re.Pattern]:
|
def get_active_regexes(self) -> list[re.Pattern]:
|
||||||
patterns = self.contexts.get(self.current_context, [".*"])
|
return compile_active_context_patterns(getattr(self.config, "config", None))
|
||||||
return [re.compile(p) for p in patterns]
|
|
||||||
|
|
||||||
def _match_any(self, node_name: str, patterns: List[re.Pattern]) -> bool:
|
def _match_any(self, node_name: str, patterns: list[re.Pattern]) -> bool:
|
||||||
return any(p.match(node_name) for p in patterns)
|
return match_active_context_patterns(node_name, patterns)
|
||||||
|
|
||||||
|
def is_default_all_context(self) -> bool:
|
||||||
|
return is_default_all_context_cfg(getattr(self.config, "config", None))
|
||||||
|
|
||||||
|
def matches_active_context(self, name: str) -> bool:
|
||||||
|
if self.is_default_all_context():
|
||||||
|
return isinstance(name, str) and bool(name)
|
||||||
|
if not isinstance(name, str) or not name:
|
||||||
|
return False
|
||||||
|
return match_active_context_patterns(name, self.get_active_regexes())
|
||||||
|
|
||||||
|
def validate_in_active_context(self, unique_id: str, is_folder: bool = False) -> None:
|
||||||
|
if not self.is_default_all_context() and not self.matches_active_context(unique_id):
|
||||||
|
label = "Folder" if is_folder else "Node"
|
||||||
|
raise InvalidConfigurationError(
|
||||||
|
f"{label} '{unique_id}' does not match active context '{self.current_context}'."
|
||||||
|
)
|
||||||
|
|
||||||
# Hook handlers for filtering
|
# Hook handlers for filtering
|
||||||
def filter_node_list(self, *args, **kwargs):
|
def filter_node_list(self, *args, **kwargs):
|
||||||
|
|||||||
@@ -1,109 +1,362 @@
|
|||||||
from typing import List, Dict, Any, Callable, Optional
|
import inspect
|
||||||
import os
|
import os
|
||||||
from .base import BaseService
|
import re
|
||||||
|
import sys
|
||||||
|
from collections.abc import Callable
|
||||||
|
from contextlib import suppress
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
|
from connpy.auth_utils import (
|
||||||
|
MAX_REMOTE_FILTER_NODES,
|
||||||
|
AuthAttemptTracker,
|
||||||
|
raise_sanitized_auth_error,
|
||||||
|
)
|
||||||
from connpy.core import nodes as Nodes
|
from connpy.core import nodes as Nodes
|
||||||
|
|
||||||
|
from .base import BaseService
|
||||||
from .exceptions import ConnpyError
|
from .exceptions import ConnpyError
|
||||||
|
|
||||||
|
|
||||||
class ExecutionService(BaseService):
|
class ExecutionService(BaseService):
|
||||||
"""Business logic for executing commands on nodes and running automation scripts."""
|
"""Business logic for executing commands on nodes and running automation scripts."""
|
||||||
|
|
||||||
def run_commands(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
nodes_filter: str,
|
config=None,
|
||||||
commands: List[str],
|
remote_stub=None,
|
||||||
variables: Optional[Dict[str, Any]] = None,
|
attempt_tracker=None,
|
||||||
parallel: int = 10,
|
enforce_context: bool = True,
|
||||||
timeout: int = 20,
|
):
|
||||||
folder: Optional[str] = None,
|
super().__init__(config)
|
||||||
prompt: Optional[str] = None,
|
self.remote_stub = remote_stub
|
||||||
on_node_complete: Optional[Callable] = None,
|
self.attempt_tracker = (
|
||||||
logger: Optional[Callable] = None,
|
attempt_tracker if attempt_tracker is not None else AuthAttemptTracker()
|
||||||
name: Optional[str] = None
|
)
|
||||||
) -> Dict[str, str]:
|
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: dict[str, Any] | None = None,
|
||||||
|
parallel: int = 10,
|
||||||
|
timeout: int | None = None,
|
||||||
|
folder: str | None = None,
|
||||||
|
prompt: str | None = None,
|
||||||
|
on_node_complete: Callable | None = None,
|
||||||
|
logger: Callable | None = None,
|
||||||
|
name: str | None = None,
|
||||||
|
connect_timeout: int | None = None,
|
||||||
|
auth_timeout: int | None = None,
|
||||||
|
) -> dict[str, Any]:
|
||||||
"""Execute commands on a set of nodes."""
|
"""Execute commands on a set of nodes."""
|
||||||
try:
|
try:
|
||||||
|
self._ensure_backend_supports_stage_timeouts(
|
||||||
|
connect_timeout=connect_timeout,
|
||||||
|
auth_timeout=auth_timeout,
|
||||||
|
)
|
||||||
|
|
||||||
|
if self._is_remote_backend():
|
||||||
|
batches = self._build_remote_filter_batches(nodes_filter)
|
||||||
|
stub = self._get_remote_stub()
|
||||||
|
batch_results = [
|
||||||
|
stub.run_commands(
|
||||||
|
nodes_filter=batch,
|
||||||
|
commands=commands,
|
||||||
|
variables=variables,
|
||||||
|
parallel=parallel,
|
||||||
|
timeout=timeout if timeout is not None else 20,
|
||||||
|
folder=folder,
|
||||||
|
prompt=prompt,
|
||||||
|
on_node_complete=on_node_complete,
|
||||||
|
logger=logger,
|
||||||
|
name=name or "",
|
||||||
|
)
|
||||||
|
for batch in batches
|
||||||
|
]
|
||||||
|
return self._aggregate_remote_results(batch_results)
|
||||||
|
|
||||||
matched_names = self.config._getallnodes(nodes_filter)
|
matched_names = self.config._getallnodes(nodes_filter)
|
||||||
|
ctx_svc = self._get_context_service()
|
||||||
|
if ctx_svc is not None and not ctx_svc.is_default_all_context():
|
||||||
|
matched_names = ctx_svc.filter_node_list(result=matched_names)
|
||||||
if not matched_names:
|
if not matched_names:
|
||||||
raise ConnpyError(f"No nodes found matching filter: {nodes_filter}")
|
raise ConnpyError(f"No nodes found matching filter: {nodes_filter}")
|
||||||
|
|
||||||
node_data = self.config.getitems(matched_names, extract=True)
|
node_data = self.config.getitems(matched_names, extract=True)
|
||||||
executor = Nodes(node_data, config=self.config)
|
executor = Nodes(node_data, config=self.config)
|
||||||
self.last_executor = executor
|
self.last_executor = executor
|
||||||
|
|
||||||
results = executor.run(
|
|
||||||
commands=commands,
|
|
||||||
vars=variables,
|
|
||||||
parallel=parallel,
|
|
||||||
timeout=timeout,
|
|
||||||
folder=folder,
|
|
||||||
prompt=prompt,
|
|
||||||
on_complete=on_node_complete,
|
|
||||||
logger=logger
|
|
||||||
)
|
|
||||||
|
|
||||||
# Combine output and status for the caller
|
run_kwargs: dict[str, Any] = {
|
||||||
|
"commands": commands,
|
||||||
|
"vars": variables,
|
||||||
|
"parallel": parallel,
|
||||||
|
"timeout": timeout,
|
||||||
|
"folder": folder,
|
||||||
|
"prompt": prompt,
|
||||||
|
"on_complete": on_node_complete,
|
||||||
|
"logger": logger,
|
||||||
|
"connect_timeout": connect_timeout,
|
||||||
|
"auth_timeout": auth_timeout,
|
||||||
|
"_default_cmd_timeout": 20,
|
||||||
|
}
|
||||||
|
run_params = inspect.signature(executor.run).parameters
|
||||||
|
if "unattended" in run_params:
|
||||||
|
run_kwargs["unattended"] = True
|
||||||
|
if "attempt_tracker" in run_params:
|
||||||
|
run_kwargs["attempt_tracker"] = self.attempt_tracker
|
||||||
|
|
||||||
|
results = executor.run(**run_kwargs)
|
||||||
|
|
||||||
|
fb_map = getattr(executor, "fallback_used", {})
|
||||||
|
fb_dict = fb_map if isinstance(fb_map, dict) else {}
|
||||||
|
|
||||||
|
# Combine output, status, and fallback metadata for the caller
|
||||||
full_results = {}
|
full_results = {}
|
||||||
|
self.last_fallback_used = {}
|
||||||
for unique in results:
|
for unique in results:
|
||||||
|
fb_used = bool(fb_dict.get(unique, False))
|
||||||
full_results[unique] = {
|
full_results[unique] = {
|
||||||
"output": results[unique],
|
"output": results[unique],
|
||||||
"status": executor.status.get(unique, 1)
|
"status": executor.status.get(unique, 1),
|
||||||
|
"fallback_used": fb_used,
|
||||||
}
|
}
|
||||||
|
self.last_fallback_used[unique] = fb_used
|
||||||
|
|
||||||
return full_results
|
return full_results
|
||||||
except Exception as e:
|
except ConnpyError:
|
||||||
raise ConnpyError(f"Execution failed: {e}")
|
raise
|
||||||
|
except Exception as e: # noqa: BLE001
|
||||||
|
raise_sanitized_auth_error(e, code="AUTH_FAILED", context_label="execution failed")
|
||||||
|
|
||||||
def test_commands(
|
def test_commands(
|
||||||
self,
|
self,
|
||||||
nodes_filter: str,
|
nodes_filter: str,
|
||||||
commands: List[str],
|
commands: list[str],
|
||||||
expected: List[str],
|
expected: list[str],
|
||||||
variables: Optional[Dict[str, Any]] = None,
|
variables: dict[str, Any] | None = None,
|
||||||
parallel: int = 10,
|
parallel: int = 10,
|
||||||
timeout: int = 20,
|
timeout: int | None = None,
|
||||||
folder: Optional[str] = None,
|
folder: str | None = None,
|
||||||
prompt: Optional[str] = None,
|
prompt: str | None = None,
|
||||||
on_node_complete: Optional[Callable] = None,
|
on_node_complete: Callable | None = None,
|
||||||
logger: Optional[Callable] = None,
|
logger: Callable | None = None,
|
||||||
name: Optional[str] = None
|
name: str | None = None,
|
||||||
) -> Dict[str, Dict[str, bool]]:
|
connect_timeout: int | None = None,
|
||||||
|
auth_timeout: int | None = None,
|
||||||
|
) -> dict[str, dict[str, bool]]:
|
||||||
"""Run commands and verify expected output on a set of nodes."""
|
"""Run commands and verify expected output on a set of nodes."""
|
||||||
try:
|
try:
|
||||||
|
self._ensure_backend_supports_stage_timeouts(
|
||||||
|
connect_timeout=connect_timeout,
|
||||||
|
auth_timeout=auth_timeout,
|
||||||
|
)
|
||||||
|
|
||||||
|
if self._is_remote_backend():
|
||||||
|
batches = self._build_remote_filter_batches(nodes_filter)
|
||||||
|
stub = self._get_remote_stub()
|
||||||
|
batch_results = [
|
||||||
|
stub.test_commands(
|
||||||
|
nodes_filter=batch,
|
||||||
|
commands=commands,
|
||||||
|
expected=expected,
|
||||||
|
variables=variables,
|
||||||
|
parallel=parallel,
|
||||||
|
timeout=timeout if timeout is not None else 20,
|
||||||
|
folder=folder or "",
|
||||||
|
prompt=prompt,
|
||||||
|
on_node_complete=on_node_complete,
|
||||||
|
logger=logger,
|
||||||
|
name=name or "",
|
||||||
|
)
|
||||||
|
for batch in batches
|
||||||
|
]
|
||||||
|
return self._aggregate_remote_results(batch_results)
|
||||||
|
|
||||||
matched_names = self.config._getallnodes(nodes_filter)
|
matched_names = self.config._getallnodes(nodes_filter)
|
||||||
|
ctx_svc = self._get_context_service()
|
||||||
|
if ctx_svc is not None and not ctx_svc.is_default_all_context():
|
||||||
|
matched_names = ctx_svc.filter_node_list(result=matched_names)
|
||||||
if not matched_names:
|
if not matched_names:
|
||||||
raise ConnpyError(f"No nodes found matching filter: {nodes_filter}")
|
raise ConnpyError(f"No nodes found matching filter: {nodes_filter}")
|
||||||
|
|
||||||
node_data = self.config.getitems(matched_names, extract=True)
|
node_data = self.config.getitems(matched_names, extract=True)
|
||||||
executor = Nodes(node_data, config=self.config)
|
executor = Nodes(node_data, config=self.config)
|
||||||
self.last_executor = executor
|
self.last_executor = executor
|
||||||
|
|
||||||
results = executor.test(
|
|
||||||
commands=commands,
|
|
||||||
expected=expected,
|
|
||||||
vars=variables,
|
|
||||||
parallel=parallel,
|
|
||||||
timeout=timeout,
|
|
||||||
folder=folder,
|
|
||||||
prompt=prompt,
|
|
||||||
on_complete=on_node_complete,
|
|
||||||
logger=logger
|
|
||||||
)
|
|
||||||
return results
|
|
||||||
except Exception as e:
|
|
||||||
raise ConnpyError(f"Testing failed: {e}")
|
|
||||||
|
|
||||||
def run_cli_script(self, nodes_filter: str, script_path: str, parallel: int = 10) -> Dict[str, str]:
|
test_kwargs: dict[str, Any] = {
|
||||||
|
"commands": commands,
|
||||||
|
"expected": expected,
|
||||||
|
"vars": variables,
|
||||||
|
"parallel": parallel,
|
||||||
|
"timeout": timeout,
|
||||||
|
"folder": folder,
|
||||||
|
"prompt": prompt,
|
||||||
|
"on_complete": on_node_complete,
|
||||||
|
"logger": logger,
|
||||||
|
"connect_timeout": connect_timeout,
|
||||||
|
"auth_timeout": auth_timeout,
|
||||||
|
"_default_cmd_timeout": 20,
|
||||||
|
}
|
||||||
|
test_params = inspect.signature(executor.test).parameters
|
||||||
|
if "unattended" in test_params:
|
||||||
|
test_kwargs["unattended"] = True
|
||||||
|
if "attempt_tracker" in test_params:
|
||||||
|
test_kwargs["attempt_tracker"] = self.attempt_tracker
|
||||||
|
|
||||||
|
res = executor.test(**test_kwargs)
|
||||||
|
fb_map = getattr(executor, "fallback_used", {})
|
||||||
|
fb_dict = fb_map if isinstance(fb_map, dict) else {}
|
||||||
|
if isinstance(res, dict):
|
||||||
|
self.last_fallback_used = {u: bool(fb_dict.get(u, False)) for u in res}
|
||||||
|
else:
|
||||||
|
self.last_fallback_used = {}
|
||||||
|
return res
|
||||||
|
except ConnpyError:
|
||||||
|
raise
|
||||||
|
except Exception as e: # noqa: BLE001
|
||||||
|
raise_sanitized_auth_error(e, code="AUTH_FAILED", context_label="testing failed")
|
||||||
|
|
||||||
|
def run_cli_script(self, nodes_filter: str, script_path: str, parallel: int = 10) -> dict[str, str]:
|
||||||
"""Run a plain-text script containing one command per line."""
|
"""Run a plain-text script containing one command per line."""
|
||||||
if not os.path.exists(script_path):
|
if not os.path.exists(script_path):
|
||||||
raise ConnpyError(f"Script file not found: {script_path}")
|
raise ConnpyError(f"Script file not found: {script_path}")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with open(script_path, "r") as f:
|
with open(script_path) as f:
|
||||||
commands = [line.strip() for line in f if line.strip()]
|
commands = [line.strip() for line in f if line.strip()]
|
||||||
except Exception as e:
|
except OSError:
|
||||||
raise ConnpyError(f"Failed to read script {script_path}: {e}")
|
raise ConnpyError("Failed to read script file") from None
|
||||||
|
|
||||||
return self.run_commands(nodes_filter, commands, parallel=parallel)
|
return self.run_commands(nodes_filter, commands, parallel=parallel)
|
||||||
|
|
||||||
@@ -1,14 +1,56 @@
|
|||||||
from .base import BaseService
|
|
||||||
import yaml
|
|
||||||
import os
|
import os
|
||||||
|
import sys
|
||||||
from copy import deepcopy
|
from copy import deepcopy
|
||||||
from .exceptions import InvalidConfigurationError, NodeNotFoundError, ReservedNameError
|
|
||||||
|
import yaml
|
||||||
|
|
||||||
from ..configfile import NoAliasDumper
|
from ..configfile import NoAliasDumper
|
||||||
|
from .base import BaseService
|
||||||
|
from .exceptions import InvalidConfigurationError, NodeNotFoundError
|
||||||
|
from .node_service import NodeService, _supports_transactional_update
|
||||||
|
|
||||||
|
|
||||||
class ImportExportService(BaseService):
|
class ImportExportService(BaseService):
|
||||||
"""Business logic for YAML/JSON inventory import and export."""
|
"""Business logic for YAML/JSON inventory import and export."""
|
||||||
|
|
||||||
|
def __init__(self, config=None, enforce_context: bool = True):
|
||||||
|
super().__init__(config)
|
||||||
|
self.enforce_context = bool(enforce_context)
|
||||||
|
|
||||||
|
def _get_context_service(self, cfg=None):
|
||||||
|
target_cfg = cfg or self.config
|
||||||
|
if (
|
||||||
|
not getattr(self, "enforce_context", True)
|
||||||
|
or not getattr(target_cfg, "_enforce_context", True)
|
||||||
|
or (len(sys.argv) > 1 and sys.argv[1] == "api")
|
||||||
|
or not isinstance(getattr(target_cfg, "config", None), dict)
|
||||||
|
):
|
||||||
|
return None
|
||||||
|
from .context_service import ContextService
|
||||||
|
|
||||||
|
return ContextService(target_cfg)
|
||||||
|
|
||||||
|
def _get_raw_folders(self, cfg=None) -> list[str]:
|
||||||
|
target_cfg = cfg or self.config
|
||||||
|
if target_cfg is None:
|
||||||
|
return []
|
||||||
|
if (
|
||||||
|
not hasattr(target_cfg, "assert_called")
|
||||||
|
and callable(getattr(target_cfg, "_getallfolders_unlocked", None))
|
||||||
|
and hasattr(target_cfg, "_mutation_lock")
|
||||||
|
):
|
||||||
|
with target_cfg._mutation_lock:
|
||||||
|
return target_cfg._getallfolders_unlocked()
|
||||||
|
return target_cfg._getallfolders()
|
||||||
|
|
||||||
|
def _folder_exists(self, folder_id: str, raw_folders: list[str], cfg=None) -> bool:
|
||||||
|
target_cfg = cfg or self.config
|
||||||
|
cfg_dict = getattr(target_cfg, "config", None)
|
||||||
|
case = cfg_dict.get("case", False) if isinstance(cfg_dict, dict) else False
|
||||||
|
if case:
|
||||||
|
return folder_id in raw_folders
|
||||||
|
return folder_id.lower() in {f.lower() for f in raw_folders}
|
||||||
|
|
||||||
def export_to_file(self, file_path, folders=None):
|
def export_to_file(self, file_path, folders=None):
|
||||||
"""Export nodes/folders to a YAML file."""
|
"""Export nodes/folders to a YAML file."""
|
||||||
if os.path.exists(file_path):
|
if os.path.exists(file_path):
|
||||||
@@ -23,46 +65,72 @@ class ImportExportService(BaseService):
|
|||||||
|
|
||||||
def export_to_dict(self, folders=None):
|
def export_to_dict(self, folders=None):
|
||||||
"""Export nodes/folders to a dictionary."""
|
"""Export nodes/folders to a dictionary."""
|
||||||
if not folders:
|
ctx_svc = self._get_context_service()
|
||||||
|
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)
|
return deepcopy(self.config.connections)
|
||||||
else:
|
|
||||||
# Validate folders exist
|
raw_folders = self._get_raw_folders()
|
||||||
|
if folders:
|
||||||
for f in folders:
|
for f in folders:
|
||||||
if f != "@" and f not in self.config._getallfolders():
|
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.")
|
raise NodeNotFoundError(f"Folder '{f}' not found.")
|
||||||
|
|
||||||
flat = self.config._getallnodesfull(folders, extract=False)
|
flat = self.config._getallnodesfull(folders, extract=False)
|
||||||
nested = {}
|
allowed_folders = []
|
||||||
for k, v in flat.items():
|
else:
|
||||||
uniques = self.config._explode_unique(k)
|
allowed_folders = ctx_svc.filter_node_list(result=raw_folders)
|
||||||
if not uniques:
|
flat = self.config._getallnodesfull(extract=False)
|
||||||
continue
|
|
||||||
|
if ctx_active:
|
||||||
if "folder" in uniques and "subfolder" in uniques:
|
flat = ctx_svc.filter_node_dict(result=flat)
|
||||||
f_name = uniques["folder"]
|
|
||||||
s_name = uniques["subfolder"]
|
nested = {}
|
||||||
i_name = uniques["id"]
|
for folder_id in allowed_folders:
|
||||||
|
uniques = self.config._explode_unique(folder_id)
|
||||||
if f_name not in nested:
|
if not uniques or "folder" not in uniques:
|
||||||
nested[f_name] = {"type": "folder"}
|
continue
|
||||||
if s_name not in nested[f_name]:
|
f_name = uniques["folder"]
|
||||||
nested[f_name][s_name] = {"type": "subfolder"}
|
if f_name not in nested:
|
||||||
|
nested[f_name] = {"type": "folder"}
|
||||||
nested[f_name][s_name][i_name] = v
|
if "subfolder" in uniques:
|
||||||
|
s_name = uniques["subfolder"]
|
||||||
elif "folder" in uniques:
|
if s_name not in nested[f_name]:
|
||||||
f_name = uniques["folder"]
|
nested[f_name][s_name] = {"type": "subfolder"}
|
||||||
i_name = uniques["id"]
|
|
||||||
|
for k, v in flat.items():
|
||||||
if f_name not in nested:
|
uniques = self.config._explode_unique(k)
|
||||||
nested[f_name] = {"type": "folder"}
|
if not uniques:
|
||||||
|
continue
|
||||||
nested[f_name][i_name] = v
|
|
||||||
else:
|
if "folder" in uniques and "subfolder" in uniques:
|
||||||
i_name = uniques["id"]
|
f_name = uniques["folder"]
|
||||||
nested[i_name] = v
|
s_name = uniques["subfolder"]
|
||||||
|
i_name = uniques["id"]
|
||||||
return nested
|
|
||||||
|
if f_name not in nested:
|
||||||
|
nested[f_name] = {"type": "folder"}
|
||||||
|
if s_name not in nested[f_name]:
|
||||||
|
nested[f_name][s_name] = {"type": "subfolder"}
|
||||||
|
|
||||||
|
nested[f_name][s_name][i_name] = v
|
||||||
|
|
||||||
|
elif "folder" in uniques:
|
||||||
|
f_name = uniques["folder"]
|
||||||
|
i_name = uniques["id"]
|
||||||
|
|
||||||
|
if f_name not in nested:
|
||||||
|
nested[f_name] = {"type": "folder"}
|
||||||
|
|
||||||
|
nested[f_name][i_name] = v
|
||||||
|
else:
|
||||||
|
i_name = uniques["id"]
|
||||||
|
nested[i_name] = v
|
||||||
|
|
||||||
|
return nested
|
||||||
|
|
||||||
def import_from_file(self, file_path):
|
def import_from_file(self, file_path):
|
||||||
"""Import nodes/folders from a YAML file."""
|
"""Import nodes/folders from a YAML file."""
|
||||||
@@ -77,39 +145,91 @@ class ImportExportService(BaseService):
|
|||||||
raise InvalidConfigurationError(f"Failed to read/parse import file: {e}")
|
raise InvalidConfigurationError(f"Failed to read/parse import file: {e}")
|
||||||
|
|
||||||
def import_from_dict(self, data):
|
def import_from_dict(self, data):
|
||||||
"""Import nodes/folders from a dictionary."""
|
"""Import nodes/folders from a dictionary atomically with context pre-validation."""
|
||||||
if not isinstance(data, dict):
|
if not isinstance(data, dict):
|
||||||
raise InvalidConfigurationError("Invalid import data format: expected a dictionary of nodes.")
|
raise InvalidConfigurationError(
|
||||||
|
"Invalid import data format: expected a dictionary of nodes."
|
||||||
|
)
|
||||||
|
|
||||||
def _traverse_import(node_data, current_folder='', current_subfolder=''):
|
ctx_svc = self._get_context_service()
|
||||||
|
ctx_active = ctx_svc is not None and not ctx_svc.is_default_all_context()
|
||||||
|
raw_folders = self._get_raw_folders() if ctx_active else []
|
||||||
|
planned_ops = []
|
||||||
|
|
||||||
|
def _plan_import(node_data, current_folder="", current_subfolder=""):
|
||||||
for k, v in node_data.items():
|
for k, v in node_data.items():
|
||||||
if k == "type":
|
if k == "type" or not isinstance(v, dict):
|
||||||
continue
|
continue
|
||||||
if isinstance(v, dict):
|
node_type = v.get("type", "connection")
|
||||||
node_type = v.get("type", "connection")
|
if node_type == "folder":
|
||||||
if node_type == "folder":
|
folder_id = f"@{k}"
|
||||||
self.config._folder_add(folder=k)
|
has_children = any(
|
||||||
_traverse_import(v, current_folder=k, current_subfolder='')
|
isinstance(cv, dict) for ck, cv in v.items() if ck != "type"
|
||||||
elif node_type == "subfolder":
|
)
|
||||||
self.config._folder_add(folder=current_folder, subfolder=k)
|
if ctx_active and (
|
||||||
_traverse_import(v, current_folder=current_folder, current_subfolder=k)
|
not has_children
|
||||||
elif node_type == "connection":
|
or (
|
||||||
unique_id = k
|
not ctx_svc.matches_active_context(folder_id)
|
||||||
if current_subfolder:
|
and not self._folder_exists(folder_id, raw_folders)
|
||||||
unique_id = f"{k}@{current_subfolder}@{current_folder}"
|
)
|
||||||
elif current_folder:
|
):
|
||||||
unique_id = f"{k}@{current_folder}"
|
ctx_svc.validate_in_active_context(folder_id, is_folder=True)
|
||||||
self._validate_node_name(unique_id)
|
planned_ops.append(("folder", k, ""))
|
||||||
|
_plan_import(v, current_folder=k, current_subfolder="")
|
||||||
kwargs = deepcopy(v)
|
elif node_type == "subfolder":
|
||||||
kwargs['id'] = k
|
folder_id = f"@{k}@{current_folder}"
|
||||||
kwargs['folder'] = current_folder
|
has_children = any(
|
||||||
kwargs['subfolder'] = current_subfolder
|
isinstance(cv, dict) for ck, cv in v.items() if ck != "type"
|
||||||
|
)
|
||||||
self.config._connections_add(**kwargs)
|
if ctx_active and (
|
||||||
else:
|
not has_children
|
||||||
# Invalid format skip
|
or (
|
||||||
pass
|
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:
|
||||||
|
unique_id = f"{k}@{current_subfolder}@{current_folder}"
|
||||||
|
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)
|
||||||
|
|
||||||
_traverse_import(data)
|
kwargs = deepcopy(v)
|
||||||
self.config._saveconfig(self.config.file)
|
kwargs["id"] = k
|
||||||
|
kwargs["folder"] = current_folder
|
||||||
|
kwargs["subfolder"] = current_subfolder
|
||||||
|
planned_ops.append(("connection", 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:
|
||||||
|
cfg._connections_add_unlocked(**op[1])
|
||||||
|
|
||||||
|
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
|
||||||
+411
-60
@@ -1,13 +1,169 @@
|
|||||||
|
import inspect
|
||||||
import re
|
import re
|
||||||
|
import sys
|
||||||
|
from copy import deepcopy
|
||||||
|
from typing import ClassVar
|
||||||
|
|
||||||
from .base import BaseService
|
from .base import BaseService
|
||||||
from .exceptions import (
|
from .exceptions import (
|
||||||
NodeNotFoundError, NodeAlreadyExistsError,
|
InvalidConfigurationError,
|
||||||
InvalidConfigurationError, ReservedNameError
|
NodeAlreadyExistsError,
|
||||||
|
NodeNotFoundError,
|
||||||
|
ReservedNameError,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _supports_transactional_update(cfg):
|
||||||
|
return (
|
||||||
|
cfg is not None
|
||||||
|
and not hasattr(cfg, "assert_called")
|
||||||
|
and callable(getattr(cfg, "transactional_update", None))
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class NodeService(BaseService):
|
class NodeService(BaseService):
|
||||||
def __init__(self, config=None):
|
_CONNECTION_FIELDS: ClassVar[frozenset[str]] = frozenset({
|
||||||
|
"id",
|
||||||
|
"host",
|
||||||
|
"folder",
|
||||||
|
"subfolder",
|
||||||
|
"options",
|
||||||
|
"logs",
|
||||||
|
"password",
|
||||||
|
"port",
|
||||||
|
"protocol",
|
||||||
|
"user",
|
||||||
|
"tags",
|
||||||
|
"jumphost",
|
||||||
|
"fallback",
|
||||||
|
"type",
|
||||||
|
})
|
||||||
|
|
||||||
|
def __init__(self, config=None, enforce_context: bool = True):
|
||||||
super().__init__(config)
|
super().__init__(config)
|
||||||
|
self.enforce_context = bool(enforce_context)
|
||||||
|
|
||||||
|
def _get_context_service(self, cfg=None):
|
||||||
|
target_cfg = cfg or self.config
|
||||||
|
if (
|
||||||
|
not getattr(self, "enforce_context", True)
|
||||||
|
or not getattr(target_cfg, "_enforce_context", True)
|
||||||
|
or (len(sys.argv) > 1 and sys.argv[1] == "api")
|
||||||
|
or not isinstance(getattr(target_cfg, "config", None), dict)
|
||||||
|
):
|
||||||
|
return None
|
||||||
|
from .context_service import ContextService
|
||||||
|
|
||||||
|
return ContextService(target_cfg)
|
||||||
|
|
||||||
|
def _get_raw_folders(self, cfg=None) -> list[str]:
|
||||||
|
target_cfg = cfg or self.config
|
||||||
|
if target_cfg is None:
|
||||||
|
return []
|
||||||
|
if (
|
||||||
|
not hasattr(target_cfg, "assert_called")
|
||||||
|
and callable(getattr(target_cfg, "_getallfolders_unlocked", None))
|
||||||
|
and hasattr(target_cfg, "_mutation_lock")
|
||||||
|
):
|
||||||
|
with target_cfg._mutation_lock:
|
||||||
|
return target_cfg._getallfolders_unlocked()
|
||||||
|
return target_cfg._getallfolders()
|
||||||
|
|
||||||
|
def _get_raw_nodes(self, cfg=None) -> list[str]:
|
||||||
|
target_cfg = cfg or self.config
|
||||||
|
if target_cfg is None:
|
||||||
|
return []
|
||||||
|
if (
|
||||||
|
not hasattr(target_cfg, "assert_called")
|
||||||
|
and callable(getattr(target_cfg, "_getallnodes_unlocked", None))
|
||||||
|
and hasattr(target_cfg, "_mutation_lock")
|
||||||
|
):
|
||||||
|
with target_cfg._mutation_lock:
|
||||||
|
return target_cfg._getallnodes_unlocked()
|
||||||
|
return target_cfg._getallnodes()
|
||||||
|
|
||||||
|
def _derive_parent_folder(self, cfg, unique_id: str, is_folder: bool) -> str | None:
|
||||||
|
explode_fn = (
|
||||||
|
cfg._explode_unique_unlocked
|
||||||
|
if (
|
||||||
|
not hasattr(cfg, "assert_called")
|
||||||
|
and callable(getattr(cfg, "_explode_unique_unlocked", None))
|
||||||
|
)
|
||||||
|
else cfg._explode_unique
|
||||||
|
)
|
||||||
|
uniques = explode_fn(unique_id)
|
||||||
|
if is_folder:
|
||||||
|
if (
|
||||||
|
not isinstance(unique_id, str)
|
||||||
|
or not unique_id.startswith("@")
|
||||||
|
or not isinstance(uniques, dict)
|
||||||
|
or "folder" not in uniques
|
||||||
|
):
|
||||||
|
raise InvalidConfigurationError(f"Invalid folder name '{unique_id}'.")
|
||||||
|
if "subfolder" in uniques:
|
||||||
|
return f"@{uniques['folder']}"
|
||||||
|
return None
|
||||||
|
if (
|
||||||
|
not isinstance(unique_id, str)
|
||||||
|
or unique_id.startswith("@")
|
||||||
|
or not isinstance(uniques, dict)
|
||||||
|
or not uniques.get("id")
|
||||||
|
):
|
||||||
|
raise InvalidConfigurationError(f"Invalid node name '{unique_id}'.")
|
||||||
|
if "subfolder" in uniques and "folder" in uniques:
|
||||||
|
return f"@{uniques['subfolder']}@{uniques['folder']}"
|
||||||
|
if "folder" in uniques:
|
||||||
|
return f"@{uniques['folder']}"
|
||||||
|
return None
|
||||||
|
|
||||||
|
def _folder_exists(self, parent_folder: str, raw_folders: list[str], cfg=None) -> bool:
|
||||||
|
target_cfg = cfg or self.config
|
||||||
|
cfg_dict = getattr(target_cfg, "config", None)
|
||||||
|
case = cfg_dict.get("case", False) if isinstance(cfg_dict, dict) else False
|
||||||
|
if case:
|
||||||
|
return parent_folder in raw_folders
|
||||||
|
return parent_folder.lower() in {f.lower() for f in raw_folders}
|
||||||
|
|
||||||
|
def _ensure_in_active_context(
|
||||||
|
self, unique_id: str, *, is_folder: bool = False, not_found_msg: str | None = None
|
||||||
|
) -> None:
|
||||||
|
ctx_svc = self._get_context_service()
|
||||||
|
if (
|
||||||
|
ctx_svc is not None
|
||||||
|
and not ctx_svc.is_default_all_context()
|
||||||
|
and not ctx_svc.matches_active_context(unique_id)
|
||||||
|
):
|
||||||
|
if not_found_msg is not None:
|
||||||
|
raise NodeNotFoundError(not_found_msg)
|
||||||
|
label = "Folder" if is_folder else "Node"
|
||||||
|
raise NodeNotFoundError(f"{label} '{unique_id}' not found.")
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _validate_node_payload(data):
|
||||||
|
if not isinstance(data, dict):
|
||||||
|
return
|
||||||
|
from ..auth_utils import (
|
||||||
|
SanitizedAuthError,
|
||||||
|
is_compose_expression,
|
||||||
|
validate_auth_budget_tags,
|
||||||
|
validate_compose_syntax,
|
||||||
|
validate_fallback_entries,
|
||||||
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
|
if "tags" in data and data["tags"] not in ("", None):
|
||||||
|
validate_auth_budget_tags(data["tags"])
|
||||||
|
if "fallback" in data and data["fallback"] not in (None, "", []):
|
||||||
|
validate_fallback_entries(data["fallback"])
|
||||||
|
pw = data.get("password")
|
||||||
|
if is_compose_expression(pw):
|
||||||
|
validate_compose_syntax(pw)
|
||||||
|
elif isinstance(pw, (list, tuple)):
|
||||||
|
for item in pw:
|
||||||
|
if is_compose_expression(item):
|
||||||
|
validate_compose_syntax(item)
|
||||||
|
except (ValueError, TypeError, SanitizedAuthError) as exc:
|
||||||
|
raise InvalidConfigurationError(str(exc)) from None
|
||||||
|
|
||||||
|
|
||||||
def list_nodes(self, filter_str=None, format_str=None):
|
def list_nodes(self, filter_str=None, format_str=None):
|
||||||
@@ -18,7 +174,11 @@ class NodeService(BaseService):
|
|||||||
if filter_str:
|
if filter_str:
|
||||||
flags = re.IGNORECASE if not case_sensitive else 0
|
flags = re.IGNORECASE if not case_sensitive else 0
|
||||||
nodes = [n for n in nodes if re.search(filter_str, n, flags)]
|
nodes = [n for n in nodes if re.search(filter_str, n, flags)]
|
||||||
|
|
||||||
|
ctx_svc = self._get_context_service()
|
||||||
|
if ctx_svc is not None and not ctx_svc.is_default_all_context():
|
||||||
|
nodes = ctx_svc.filter_node_list(result=nodes)
|
||||||
|
|
||||||
if not format_str:
|
if not format_str:
|
||||||
return nodes
|
return nodes
|
||||||
|
|
||||||
@@ -75,17 +235,21 @@ class NodeService(BaseService):
|
|||||||
else:
|
else:
|
||||||
flags = re.IGNORECASE if not case_sensitive else 0
|
flags = re.IGNORECASE if not case_sensitive else 0
|
||||||
folders = [f for f in folders if re.search(filter_str, f, flags)]
|
folders = [f for f in folders if re.search(filter_str, f, flags)]
|
||||||
|
ctx_svc = self._get_context_service()
|
||||||
|
if ctx_svc is not None and not ctx_svc.is_default_all_context():
|
||||||
|
folders = ctx_svc.filter_node_list(result=folders)
|
||||||
return folders
|
return folders
|
||||||
|
|
||||||
def get_node_details(self, unique_id):
|
def get_node_details(self, unique_id):
|
||||||
"""Return full configuration dictionary for a specific node."""
|
"""Return full configuration dictionary for a specific node."""
|
||||||
|
self._ensure_in_active_context(unique_id, is_folder=False)
|
||||||
try:
|
try:
|
||||||
details = self.config.getitem(unique_id)
|
details = self.config.getitem(unique_id)
|
||||||
if not details:
|
if not details:
|
||||||
raise NodeNotFoundError(f"Node '{unique_id}' not found.")
|
raise NodeNotFoundError(f"Node '{unique_id}' not found.")
|
||||||
return details
|
return details
|
||||||
except (KeyError, TypeError):
|
except (KeyError, TypeError):
|
||||||
raise NodeNotFoundError(f"Node '{unique_id}' not found.")
|
raise NodeNotFoundError(f"Node '{unique_id}' not found.") from None
|
||||||
|
|
||||||
def explode_unique(self, unique_id):
|
def explode_unique(self, unique_id):
|
||||||
"""Explode a unique ID into a dictionary of its parts."""
|
"""Explode a unique ID into a dictionary of its parts."""
|
||||||
@@ -96,77 +260,143 @@ class NodeService(BaseService):
|
|||||||
self.config._generate_nodes_cache(nodes=nodes, folders=folders, profiles=profiles)
|
self.config._generate_nodes_cache(nodes=nodes, folders=folders, profiles=profiles)
|
||||||
|
|
||||||
def validate_parent_folder(self, unique_id, is_folder=False):
|
def validate_parent_folder(self, unique_id, is_folder=False):
|
||||||
"""Check if parent folder exists for a given node unique ID."""
|
"""Check if unique_id is in active context and its parent folder exists."""
|
||||||
if is_folder:
|
parent_folder = self._derive_parent_folder(self.config, unique_id, is_folder=is_folder)
|
||||||
uniques = self.config._explode_unique(unique_id)
|
ctx_svc = self._get_context_service()
|
||||||
if uniques and "subfolder" in uniques and "folder" in uniques:
|
if ctx_svc is not None:
|
||||||
parent_folder = f"@{uniques['folder']}"
|
ctx_svc.validate_in_active_context(unique_id, is_folder=is_folder)
|
||||||
if parent_folder not in self.config._getallfolders():
|
if parent_folder is not None and not self._folder_exists(
|
||||||
raise NodeNotFoundError(f"Folder '{parent_folder}' not found.")
|
parent_folder, self._get_raw_folders(), self.config
|
||||||
else:
|
):
|
||||||
node_folder = unique_id.partition("@")[2]
|
raise NodeNotFoundError(f"Folder '{parent_folder}' not found.")
|
||||||
if node_folder:
|
|
||||||
parent_folder = f"@{node_folder}"
|
|
||||||
if parent_folder not in self.config._getallfolders():
|
|
||||||
raise NodeNotFoundError(f"Folder '{parent_folder}' not found.")
|
|
||||||
|
|
||||||
|
|
||||||
def add_node(self, unique_id, data, is_folder=False):
|
def add_node(self, unique_id, data, is_folder=False):
|
||||||
"""Logic for adding a new node or folder to configuration."""
|
"""Logic for adding a new node or folder to configuration."""
|
||||||
if not is_folder:
|
if not is_folder:
|
||||||
self._validate_node_name(unique_id)
|
self._validate_node_name(unique_id)
|
||||||
|
self._validate_node_payload(data)
|
||||||
|
self.validate_parent_folder(unique_id, is_folder=is_folder)
|
||||||
|
|
||||||
|
if _supports_transactional_update(self.config):
|
||||||
|
def _mut(cfg):
|
||||||
|
all_nodes = cfg._getallnodes_unlocked()
|
||||||
|
all_folders = cfg._getallfolders_unlocked()
|
||||||
|
parent_folder = self._derive_parent_folder(cfg, unique_id, is_folder=is_folder)
|
||||||
|
ctx_svc = self._get_context_service(cfg)
|
||||||
|
if ctx_svc is not None:
|
||||||
|
ctx_svc.validate_in_active_context(unique_id, is_folder=is_folder)
|
||||||
|
if parent_folder is not None and not self._folder_exists(
|
||||||
|
parent_folder, all_folders, cfg
|
||||||
|
):
|
||||||
|
raise NodeNotFoundError(f"Folder '{parent_folder}' not found.")
|
||||||
|
if is_folder:
|
||||||
|
if unique_id in all_folders:
|
||||||
|
raise NodeAlreadyExistsError(f"Folder '{unique_id}' already exists.")
|
||||||
|
uniques = cfg._explode_unique_unlocked(unique_id)
|
||||||
|
if not uniques:
|
||||||
|
raise InvalidConfigurationError(f"Invalid folder name '{unique_id}'.")
|
||||||
|
cfg._folder_add_unlocked(**uniques)
|
||||||
|
else:
|
||||||
|
if unique_id in all_nodes:
|
||||||
|
raise NodeAlreadyExistsError(f"Node '{unique_id}' already exists.")
|
||||||
|
payload = deepcopy(data)
|
||||||
|
if "id" not in payload:
|
||||||
|
uniques = cfg._explode_unique_unlocked(unique_id)
|
||||||
|
if uniques and "id" in uniques:
|
||||||
|
payload["id"] = uniques["id"]
|
||||||
|
cfg._connections_add_unlocked(**payload)
|
||||||
|
|
||||||
|
self.config.transactional_update(_mut)
|
||||||
|
return
|
||||||
|
|
||||||
all_nodes = self.config._getallnodes()
|
all_nodes = self.config._getallnodes()
|
||||||
all_folders = self.config._getallfolders()
|
all_folders = self.config._getallfolders()
|
||||||
|
|
||||||
if is_folder:
|
if is_folder:
|
||||||
if unique_id in all_folders:
|
if unique_id in all_folders:
|
||||||
raise NodeAlreadyExistsError(f"Folder '{unique_id}' already exists.")
|
raise NodeAlreadyExistsError(f"Folder '{unique_id}' already exists.")
|
||||||
uniques = self.config._explode_unique(unique_id)
|
uniques = self.config._explode_unique(unique_id)
|
||||||
if not uniques:
|
if not uniques:
|
||||||
raise InvalidConfigurationError(f"Invalid folder name '{unique_id}'.")
|
raise InvalidConfigurationError(f"Invalid folder name '{unique_id}'.")
|
||||||
|
|
||||||
# Check if parent folder exists when creating a subfolder
|
|
||||||
if "subfolder" in uniques:
|
if "subfolder" in uniques:
|
||||||
self.validate_parent_folder(unique_id, is_folder=True)
|
self.validate_parent_folder(unique_id, is_folder=True)
|
||||||
|
|
||||||
self.config._folder_add(**uniques)
|
self.config._folder_add(**uniques)
|
||||||
self.config._saveconfig(self.config.file)
|
self.config._saveconfig(self.config.file)
|
||||||
else:
|
else:
|
||||||
if unique_id in all_nodes:
|
if unique_id in all_nodes:
|
||||||
raise NodeAlreadyExistsError(f"Node '{unique_id}' already exists.")
|
raise NodeAlreadyExistsError(f"Node '{unique_id}' already exists.")
|
||||||
|
|
||||||
# Check if parent folder exists when creating a node in a folder
|
|
||||||
self.validate_parent_folder(unique_id)
|
self.validate_parent_folder(unique_id)
|
||||||
|
|
||||||
# Ensure 'id' is in data for config._connections_add
|
payload = deepcopy(data)
|
||||||
if "id" not in data:
|
if "id" not in payload:
|
||||||
uniques = self.config._explode_unique(unique_id)
|
uniques = self.config._explode_unique(unique_id)
|
||||||
if uniques and "id" in uniques:
|
if uniques and "id" in uniques:
|
||||||
data["id"] = uniques["id"]
|
payload["id"] = uniques["id"]
|
||||||
|
|
||||||
self.config._connections_add(**data)
|
self.config._connections_add(**payload)
|
||||||
self.config._saveconfig(self.config.file)
|
self.config._saveconfig(self.config.file)
|
||||||
|
|
||||||
|
def _build_update_payload(self, unique_id, data, existing, uniques):
|
||||||
|
if isinstance(existing, dict):
|
||||||
|
merged = deepcopy(existing)
|
||||||
|
if isinstance(uniques, dict):
|
||||||
|
merged.update({k: v for k, v in uniques.items() if v})
|
||||||
|
merged.update(deepcopy(data))
|
||||||
|
filtered = {k: v for k, v in merged.items() if k in self._CONNECTION_FIELDS}
|
||||||
|
else:
|
||||||
|
filtered = deepcopy(data)
|
||||||
|
if "id" not in filtered and isinstance(uniques, dict) and "id" in uniques:
|
||||||
|
filtered["id"] = uniques["id"]
|
||||||
|
self._validate_node_payload(filtered)
|
||||||
|
return filtered
|
||||||
|
|
||||||
def update_node(self, unique_id, data, save=True):
|
def update_node(self, unique_id, data, save=True):
|
||||||
"""Explicitly update an existing node."""
|
"""Explicitly update an existing node."""
|
||||||
|
self._ensure_in_active_context(unique_id, is_folder=False)
|
||||||
|
if _supports_transactional_update(self.config) and save:
|
||||||
|
def _mut(cfg):
|
||||||
|
all_nodes = cfg._getallnodes_unlocked()
|
||||||
|
if unique_id not in all_nodes:
|
||||||
|
raise NodeNotFoundError(f"Node '{unique_id}' not found.")
|
||||||
|
uniques = cfg._explode_unique_unlocked(unique_id)
|
||||||
|
existing = cfg.getitem_unlocked(unique_id, extract=False)
|
||||||
|
payload = self._build_update_payload(unique_id, data, existing, uniques)
|
||||||
|
cfg._connections_add_unlocked(**payload)
|
||||||
|
|
||||||
|
self.config.transactional_update(_mut)
|
||||||
|
return
|
||||||
|
|
||||||
all_nodes = self.config._getallnodes()
|
all_nodes = self.config._getallnodes()
|
||||||
if unique_id not in all_nodes:
|
if unique_id not in all_nodes:
|
||||||
raise NodeNotFoundError(f"Node '{unique_id}' not found.")
|
raise NodeNotFoundError(f"Node '{unique_id}' not found.")
|
||||||
|
|
||||||
# Ensure 'id' is in data for config._connections_add
|
uniques = self.config._explode_unique(unique_id)
|
||||||
if "id" not in data:
|
existing = self.config.getitem(unique_id, extract=False)
|
||||||
uniques = self.config._explode_unique(unique_id)
|
payload = self._build_update_payload(unique_id, data, existing, uniques)
|
||||||
if uniques:
|
self.config._connections_add(**payload)
|
||||||
data["id"] = uniques["id"]
|
|
||||||
|
|
||||||
# config._connections_add actually handles updates if ID exists correctly
|
|
||||||
self.config._connections_add(**data)
|
|
||||||
if save:
|
if save:
|
||||||
self.config._saveconfig(self.config.file)
|
self.config._saveconfig(self.config.file)
|
||||||
|
|
||||||
def delete_node(self, unique_id, is_folder=False, save=True):
|
def delete_node(self, unique_id, is_folder=False, save=True):
|
||||||
"""Logic for deleting a node or folder."""
|
"""Logic for deleting a node or folder."""
|
||||||
|
self._ensure_in_active_context(unique_id, is_folder=is_folder)
|
||||||
|
if _supports_transactional_update(self.config) and save:
|
||||||
|
def _mut(cfg):
|
||||||
|
uniques = cfg._explode_unique_unlocked(unique_id)
|
||||||
|
if not uniques:
|
||||||
|
label = "Folder" if is_folder else "Node"
|
||||||
|
raise NodeNotFoundError(f"{label} '{unique_id}' not found or invalid.")
|
||||||
|
if is_folder:
|
||||||
|
cfg._folder_del_unlocked(**uniques)
|
||||||
|
else:
|
||||||
|
cfg._connections_del_unlocked(**uniques)
|
||||||
|
|
||||||
|
self.config.transactional_update(_mut)
|
||||||
|
return
|
||||||
|
|
||||||
if is_folder:
|
if is_folder:
|
||||||
uniques = self.config._explode_unique(unique_id)
|
uniques = self.config._explode_unique(unique_id)
|
||||||
if not uniques:
|
if not uniques:
|
||||||
@@ -177,23 +407,41 @@ class NodeService(BaseService):
|
|||||||
if not uniques:
|
if not uniques:
|
||||||
raise NodeNotFoundError(f"Node '{unique_id}' not found or invalid.")
|
raise NodeNotFoundError(f"Node '{unique_id}' not found or invalid.")
|
||||||
self.config._connections_del(**uniques)
|
self.config._connections_del(**uniques)
|
||||||
|
|
||||||
if save:
|
if save:
|
||||||
self.config._saveconfig(self.config.file)
|
self.config._saveconfig(self.config.file)
|
||||||
|
|
||||||
def connect_node(self, unique_id, sftp=False, debug=False, logger=None):
|
def connect_node(self, unique_id, sftp=False, debug=False, logger=None):
|
||||||
"""Interact with a node directly."""
|
"""Interact with a node directly."""
|
||||||
|
self._ensure_in_active_context(unique_id, is_folder=False)
|
||||||
from connpy.core import node
|
from connpy.core import node
|
||||||
|
|
||||||
|
from ..auth_utils import resolve_fallback_entries
|
||||||
from .profile_service import ProfileService
|
from .profile_service import ProfileService
|
||||||
|
|
||||||
node_data = self.config.getitem(unique_id, extract=False)
|
node_data = self.config.getitem(unique_id, extract=False)
|
||||||
if not node_data:
|
if not node_data:
|
||||||
raise NodeNotFoundError(f"Node '{unique_id}' not found.")
|
raise NodeNotFoundError(f"Node '{unique_id}' not found.")
|
||||||
|
|
||||||
# Resolve profiles
|
|
||||||
profile_service = ProfileService(self.config)
|
profile_service = ProfileService(self.config)
|
||||||
resolved_data = profile_service.resolve_node_data(node_data)
|
resolved_data = profile_service.resolve_node_data(node_data)
|
||||||
|
|
||||||
|
node_params = inspect.signature(node.__init__).parameters
|
||||||
|
if "fallback" not in node_params and "fallback" in resolved_data:
|
||||||
|
raw_fb = resolved_data.pop("fallback", None)
|
||||||
|
if raw_fb not in (None, "", []):
|
||||||
|
resolved_fb = resolve_fallback_entries(raw_fb, self.config.profiles)
|
||||||
|
if resolved_fb:
|
||||||
|
tags_val = resolved_data.get("tags", "")
|
||||||
|
if isinstance(tags_val, dict):
|
||||||
|
tags_copy = deepcopy(tags_val)
|
||||||
|
tags_copy["_fallback"] = resolved_fb
|
||||||
|
resolved_data["tags"] = tags_copy
|
||||||
|
elif tags_val in ("", None):
|
||||||
|
resolved_data["tags"] = {"_fallback": resolved_fb}
|
||||||
|
else:
|
||||||
|
resolved_data["tags"] = {"_raw": tags_val, "_fallback": resolved_fb}
|
||||||
|
|
||||||
n = node(unique_id, **resolved_data, config=self.config)
|
n = node(unique_id, **resolved_data, config=self.config)
|
||||||
if sftp:
|
if sftp:
|
||||||
n.protocol = "sftp"
|
n.protocol = "sftp"
|
||||||
@@ -202,51 +450,139 @@ class NodeService(BaseService):
|
|||||||
|
|
||||||
def move_node(self, src_id, dst_id, copy=False):
|
def move_node(self, src_id, dst_id, copy=False):
|
||||||
"""Move or copy a node."""
|
"""Move or copy a node."""
|
||||||
|
self._ensure_in_active_context(
|
||||||
|
src_id, is_folder=False, not_found_msg=f"Source node '{src_id}' not found."
|
||||||
|
)
|
||||||
self._validate_node_name(dst_id)
|
self._validate_node_name(dst_id)
|
||||||
|
self.validate_parent_folder(dst_id, is_folder=False)
|
||||||
|
|
||||||
|
if _supports_transactional_update(self.config):
|
||||||
|
def _mut(cfg):
|
||||||
|
ctx_svc = self._get_context_service(cfg)
|
||||||
|
if (
|
||||||
|
ctx_svc is not None
|
||||||
|
and not ctx_svc.is_default_all_context()
|
||||||
|
and not ctx_svc.matches_active_context(src_id)
|
||||||
|
):
|
||||||
|
raise NodeNotFoundError(f"Source node '{src_id}' not found.")
|
||||||
|
parent_folder = self._derive_parent_folder(cfg, dst_id, is_folder=False)
|
||||||
|
if ctx_svc is not None:
|
||||||
|
ctx_svc.validate_in_active_context(dst_id, is_folder=False)
|
||||||
|
if parent_folder is not None and not self._folder_exists(
|
||||||
|
parent_folder, cfg._getallfolders_unlocked(), cfg
|
||||||
|
):
|
||||||
|
raise NodeNotFoundError(f"Folder '{parent_folder}' not found.")
|
||||||
|
try:
|
||||||
|
node_data = cfg.getitem_unlocked(src_id)
|
||||||
|
except (KeyError, TypeError):
|
||||||
|
node_data = None
|
||||||
|
if not node_data:
|
||||||
|
raise NodeNotFoundError(f"Source node '{src_id}' not found.")
|
||||||
|
if dst_id in cfg._getallnodes_unlocked():
|
||||||
|
raise NodeAlreadyExistsError(f"Destination node '{dst_id}' already exists.")
|
||||||
|
new_uniques = cfg._explode_unique_unlocked(dst_id)
|
||||||
|
if not new_uniques:
|
||||||
|
raise InvalidConfigurationError(f"Invalid destination format '{dst_id}'.")
|
||||||
|
new_node_data = deepcopy(node_data)
|
||||||
|
new_node_data.update(new_uniques)
|
||||||
|
cfg._connections_add_unlocked(**new_node_data)
|
||||||
|
if not copy:
|
||||||
|
src_uniques = cfg._explode_unique_unlocked(src_id)
|
||||||
|
cfg._connections_del_unlocked(**src_uniques)
|
||||||
|
|
||||||
|
self.config.transactional_update(_mut)
|
||||||
|
return
|
||||||
|
|
||||||
node_data = self.config.getitem(src_id)
|
node_data = self.config.getitem(src_id)
|
||||||
if not node_data:
|
if not node_data:
|
||||||
raise NodeNotFoundError(f"Source node '{src_id}' not found.")
|
raise NodeNotFoundError(f"Source node '{src_id}' not found.")
|
||||||
|
|
||||||
if dst_id in self.config._getallnodes():
|
if dst_id in self.config._getallnodes():
|
||||||
raise NodeAlreadyExistsError(f"Destination node '{dst_id}' already exists.")
|
raise NodeAlreadyExistsError(f"Destination node '{dst_id}' already exists.")
|
||||||
|
|
||||||
new_uniques = self.config._explode_unique(dst_id)
|
new_uniques = self.config._explode_unique(dst_id)
|
||||||
if not new_uniques:
|
if not new_uniques:
|
||||||
raise InvalidConfigurationError(f"Invalid destination format '{dst_id}'.")
|
raise InvalidConfigurationError(f"Invalid destination format '{dst_id}'.")
|
||||||
|
|
||||||
new_node_data = node_data.copy()
|
new_node_data = node_data.copy()
|
||||||
new_node_data.update(new_uniques)
|
new_node_data.update(new_uniques)
|
||||||
|
|
||||||
self.config._connections_add(**new_node_data)
|
self.config._connections_add(**new_node_data)
|
||||||
|
|
||||||
if not copy:
|
if not copy:
|
||||||
src_uniques = self.config._explode_unique(src_id)
|
src_uniques = self.config._explode_unique(src_id)
|
||||||
self.config._connections_del(**src_uniques)
|
self.config._connections_del(**src_uniques)
|
||||||
|
|
||||||
self.config._saveconfig(self.config.file)
|
self.config._saveconfig(self.config.file)
|
||||||
|
|
||||||
def bulk_add(self, ids, hosts, common_data):
|
def bulk_add(self, ids, hosts, common_data):
|
||||||
"""Add multiple nodes with shared common configuration."""
|
"""Add multiple nodes with shared common configuration."""
|
||||||
|
self._validate_node_payload(common_data)
|
||||||
|
for uid in ids:
|
||||||
|
self.validate_parent_folder(uid, is_folder=False)
|
||||||
|
|
||||||
|
if _supports_transactional_update(self.config):
|
||||||
|
def _build_entries(cfg):
|
||||||
|
all_nodes = cfg._getallnodes_unlocked()
|
||||||
|
all_folders = cfg._getallfolders_unlocked()
|
||||||
|
ctx_svc = self._get_context_service(cfg)
|
||||||
|
planned = []
|
||||||
|
for i, uid in enumerate(ids):
|
||||||
|
parent_folder = self._derive_parent_folder(cfg, uid, is_folder=False)
|
||||||
|
if ctx_svc is not None:
|
||||||
|
ctx_svc.validate_in_active_context(uid, is_folder=False)
|
||||||
|
if parent_folder is not None and not self._folder_exists(
|
||||||
|
parent_folder, all_folders, cfg
|
||||||
|
):
|
||||||
|
raise NodeNotFoundError(f"Folder '{parent_folder}' not found.")
|
||||||
|
if uid in all_nodes:
|
||||||
|
continue
|
||||||
|
try:
|
||||||
|
self._validate_node_name(uid)
|
||||||
|
except ReservedNameError:
|
||||||
|
continue
|
||||||
|
host = hosts[i] if i < len(hosts) else hosts[0]
|
||||||
|
uniques = cfg._explode_unique_unlocked(uid)
|
||||||
|
if not uniques:
|
||||||
|
continue
|
||||||
|
node_data = deepcopy(common_data)
|
||||||
|
node_data.pop("ids", None)
|
||||||
|
node_data.pop("location", None)
|
||||||
|
node_data.update(uniques)
|
||||||
|
node_data["host"] = host
|
||||||
|
node_data["type"] = "connection"
|
||||||
|
planned.append(node_data)
|
||||||
|
return planned
|
||||||
|
|
||||||
|
with self.config._mutation_lock:
|
||||||
|
if not _build_entries(self.config):
|
||||||
|
return 0
|
||||||
|
|
||||||
|
def _mut(cfg):
|
||||||
|
entries = _build_entries(cfg)
|
||||||
|
for node_data in entries:
|
||||||
|
cfg._connections_add_unlocked(**node_data)
|
||||||
|
return len(entries)
|
||||||
|
|
||||||
|
return self.config.transactional_update(_mut)
|
||||||
|
|
||||||
count = 0
|
count = 0
|
||||||
all_nodes = self.config._getallnodes()
|
all_nodes = self.config._getallnodes()
|
||||||
|
|
||||||
for i, uid in enumerate(ids):
|
for i, uid in enumerate(ids):
|
||||||
if uid in all_nodes:
|
if uid in all_nodes:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self._validate_node_name(uid)
|
self._validate_node_name(uid)
|
||||||
except ReservedNameError:
|
except ReservedNameError:
|
||||||
# For bulk, we might want to just skip or log.
|
|
||||||
# CLI caller will handle if it wants to be strict.
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
host = hosts[i] if i < len(hosts) else hosts[0]
|
host = hosts[i] if i < len(hosts) else hosts[0]
|
||||||
uniques = self.config._explode_unique(uid)
|
uniques = self.config._explode_unique(uid)
|
||||||
if not uniques:
|
if not uniques:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
node_data = common_data.copy()
|
node_data = common_data.copy()
|
||||||
node_data.pop("ids", None)
|
node_data.pop("ids", None)
|
||||||
node_data.pop("location", None)
|
node_data.pop("location", None)
|
||||||
@@ -256,13 +592,28 @@ class NodeService(BaseService):
|
|||||||
|
|
||||||
self.config._connections_add(**node_data)
|
self.config._connections_add(**node_data)
|
||||||
count += 1
|
count += 1
|
||||||
|
|
||||||
if count > 0:
|
if count > 0:
|
||||||
self.config._saveconfig(self.config.file)
|
self.config._saveconfig(self.config.file)
|
||||||
return count
|
return count
|
||||||
|
|
||||||
def full_replace(self, connections, profiles):
|
def full_replace(self, connections, profiles):
|
||||||
"""Replace all connections and profiles with new data."""
|
"""Replace all connections and profiles with new data."""
|
||||||
|
from ..auth_utils import SanitizedAuthError
|
||||||
|
|
||||||
|
if _supports_transactional_update(self.config):
|
||||||
|
def _mut(cfg):
|
||||||
|
cfg.connections.clear()
|
||||||
|
cfg.connections.update(deepcopy(connections))
|
||||||
|
cfg.profiles.clear()
|
||||||
|
cfg.profiles.update(deepcopy(profiles))
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.config.transactional_update(_mut)
|
||||||
|
except (ValueError, TypeError, SanitizedAuthError) as exc:
|
||||||
|
raise InvalidConfigurationError(str(exc)) from None
|
||||||
|
return
|
||||||
|
|
||||||
self.config.connections = connections
|
self.config.connections = connections
|
||||||
self.config.profiles = profiles
|
self.config.profiles = profiles
|
||||||
self.config._saveconfig(self.config.file)
|
self.config._saveconfig(self.config.file)
|
||||||
|
|||||||
@@ -1,64 +1,211 @@
|
|||||||
|
from contextlib import suppress
|
||||||
|
from copy import deepcopy
|
||||||
|
from typing import ClassVar
|
||||||
|
|
||||||
from .base import BaseService
|
from .base import BaseService
|
||||||
from .exceptions import ProfileNotFoundError, ProfileAlreadyExistsError, InvalidConfigurationError
|
from .exceptions import (
|
||||||
|
InvalidConfigurationError,
|
||||||
|
ProfileAlreadyExistsError,
|
||||||
|
ProfileNotFoundError,
|
||||||
|
)
|
||||||
|
|
||||||
|
MAX_PROFILE_DEPTH = 5
|
||||||
|
|
||||||
|
|
||||||
|
def _supports_transactional_update(cfg):
|
||||||
|
return (
|
||||||
|
cfg is not None
|
||||||
|
and not hasattr(cfg, "assert_called")
|
||||||
|
and callable(getattr(cfg, "transactional_update", None))
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class ProfileService(BaseService):
|
class ProfileService(BaseService):
|
||||||
"""Business logic for node profiles management."""
|
"""Business logic for node profiles management."""
|
||||||
|
|
||||||
|
_ALLOWED_KEYS: ClassVar[frozenset[str]] = frozenset({
|
||||||
|
"host",
|
||||||
|
"options",
|
||||||
|
"logs",
|
||||||
|
"password",
|
||||||
|
"port",
|
||||||
|
"protocol",
|
||||||
|
"user",
|
||||||
|
"tags",
|
||||||
|
"jumphost",
|
||||||
|
"fallback",
|
||||||
|
})
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _validate_profile_payload(data):
|
||||||
|
from ..auth_utils import (
|
||||||
|
SanitizedAuthError,
|
||||||
|
is_compose_expression,
|
||||||
|
validate_auth_budget_tags,
|
||||||
|
validate_compose_syntax,
|
||||||
|
validate_fallback_entries,
|
||||||
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
|
if "tags" in data and data["tags"] not in ("", None):
|
||||||
|
validate_auth_budget_tags(data["tags"])
|
||||||
|
if "fallback" in data and data["fallback"] not in (None, "", []):
|
||||||
|
validate_fallback_entries(data["fallback"])
|
||||||
|
pw = data.get("password")
|
||||||
|
if is_compose_expression(pw):
|
||||||
|
validate_compose_syntax(pw)
|
||||||
|
elif isinstance(pw, (list, tuple)):
|
||||||
|
for item in pw:
|
||||||
|
if is_compose_expression(item):
|
||||||
|
validate_compose_syntax(item)
|
||||||
|
except (ValueError, TypeError, SanitizedAuthError) as exc:
|
||||||
|
raise InvalidConfigurationError(str(exc)) from None
|
||||||
|
|
||||||
|
def _get_profiles_snapshot(self):
|
||||||
|
lock = getattr(self.config, "_mutation_lock", None)
|
||||||
|
if lock is not None and not hasattr(self.config, "assert_called"):
|
||||||
|
with lock:
|
||||||
|
return deepcopy(self.config.profiles)
|
||||||
|
return deepcopy(self.config.profiles)
|
||||||
|
|
||||||
def list_profiles(self, filter_str=None):
|
def list_profiles(self, filter_str=None):
|
||||||
"""List all profile names, optionally filtered."""
|
"""List all profile names, optionally filtered."""
|
||||||
profiles = list(self.config.profiles.keys())
|
profiles = list(self._get_profiles_snapshot().keys())
|
||||||
case_sensitive = self.config.config.get("case", False)
|
case_sensitive = self.config.config.get("case", False)
|
||||||
|
|
||||||
if filter_str:
|
if filter_str:
|
||||||
if not case_sensitive:
|
if not case_sensitive:
|
||||||
f_str = filter_str.lower()
|
f_str = filter_str.lower()
|
||||||
return [p for p in profiles if f_str in p.lower()]
|
return [p for p in profiles if f_str in p.lower()]
|
||||||
else:
|
return [p for p in profiles if filter_str in p]
|
||||||
return [p for p in profiles if filter_str in p]
|
|
||||||
return profiles
|
return profiles
|
||||||
|
|
||||||
def get_profile(self, name, resolve=True):
|
def get_profile(self, name, resolve=True, *, _visited=frozenset(), _depth=0):
|
||||||
"""Get the profile dictionary, optionally resolved."""
|
"""Get the profile dictionary, optionally resolved."""
|
||||||
profile = self.config.profiles.get(name)
|
if _depth >= MAX_PROFILE_DEPTH or name in _visited:
|
||||||
|
raise InvalidConfigurationError(
|
||||||
|
f"Circular or overly deep profile reference involving '{name}'."
|
||||||
|
)
|
||||||
|
profiles = self._get_profiles_snapshot()
|
||||||
|
profile = profiles.get(name)
|
||||||
if not profile:
|
if not profile:
|
||||||
raise ProfileNotFoundError(f"Profile '{name}' not found.")
|
raise ProfileNotFoundError(f"Profile '{name}' not found.")
|
||||||
|
|
||||||
if resolve:
|
if resolve:
|
||||||
return self.resolve_node_data(profile)
|
return self.resolve_node_data(
|
||||||
|
profile,
|
||||||
|
_visited=_visited | {name},
|
||||||
|
_depth=_depth + 1,
|
||||||
|
_profiles_snapshot=profiles,
|
||||||
|
)
|
||||||
return profile
|
return profile
|
||||||
|
|
||||||
def add_profile(self, name, data):
|
def add_profile(self, name, data):
|
||||||
"""Add a new profile."""
|
"""Add a new profile."""
|
||||||
|
filtered_data = {k: deepcopy(v) for k, v in data.items() if k in self._ALLOWED_KEYS}
|
||||||
|
self._validate_profile_payload(filtered_data)
|
||||||
|
|
||||||
|
if _supports_transactional_update(self.config):
|
||||||
|
def _mut(cfg):
|
||||||
|
if name in cfg.profiles:
|
||||||
|
raise ProfileAlreadyExistsError(f"Profile '{name}' already exists.")
|
||||||
|
cfg._profiles_add_unlocked(id=name, **filtered_data)
|
||||||
|
|
||||||
|
self.config.transactional_update(_mut)
|
||||||
|
return
|
||||||
|
|
||||||
if name in self.config.profiles:
|
if name in self.config.profiles:
|
||||||
raise ProfileAlreadyExistsError(f"Profile '{name}' already exists.")
|
raise ProfileAlreadyExistsError(f"Profile '{name}' already exists.")
|
||||||
|
|
||||||
# Filter data to match _profiles_add signature and ensure id is passed
|
|
||||||
allowed_keys = {"host", "options", "logs", "password", "port", "protocol", "user", "tags", "jumphost"}
|
|
||||||
filtered_data = {k: v for k, v in data.items() if k in allowed_keys}
|
|
||||||
|
|
||||||
self.config._profiles_add(id=name, **filtered_data)
|
self.config._profiles_add(id=name, **filtered_data)
|
||||||
self.config._saveconfig(self.config.file)
|
self.config._saveconfig(self.config.file)
|
||||||
|
|
||||||
def resolve_node_data(self, node_data):
|
def resolve_node_data(
|
||||||
"""Resolve profile references (@profile) in node data and handle inheritance."""
|
self,
|
||||||
resolved = node_data.copy()
|
node_data,
|
||||||
|
*,
|
||||||
# 1. Identify all referenced profiles to support inheritance
|
_visited=frozenset(),
|
||||||
referenced_profiles = []
|
_depth=0,
|
||||||
for value in resolved.values():
|
_profiles_snapshot=None,
|
||||||
if isinstance(value, str) and value.startswith("@"):
|
):
|
||||||
referenced_profiles.append(value[1:])
|
"""Resolve profile references (@profile) in node data while preserving compose and scalar secrets."""
|
||||||
elif isinstance(value, list):
|
from ..auth_utils import is_compose_expression
|
||||||
|
|
||||||
|
if _depth > MAX_PROFILE_DEPTH:
|
||||||
|
raise InvalidConfigurationError("Profile inheritance exceeds maximum depth.")
|
||||||
|
|
||||||
|
profiles = (
|
||||||
|
_profiles_snapshot
|
||||||
|
if _profiles_snapshot is not None
|
||||||
|
else self._get_profiles_snapshot()
|
||||||
|
)
|
||||||
|
resolved = deepcopy(node_data)
|
||||||
|
|
||||||
|
auth_referenced_profiles = []
|
||||||
|
for key in ("user", "password"):
|
||||||
|
value = resolved.get(key)
|
||||||
|
if isinstance(value, str):
|
||||||
|
if not is_compose_expression(value) and value.startswith("@"):
|
||||||
|
ref = value[1:]
|
||||||
|
if key != "password" or ref in profiles:
|
||||||
|
auth_referenced_profiles.append(ref)
|
||||||
|
elif isinstance(value, (list, tuple)):
|
||||||
for item in value:
|
for item in value:
|
||||||
if isinstance(item, str) and item.startswith("@"):
|
if (
|
||||||
referenced_profiles.append(item[1:])
|
isinstance(item, str)
|
||||||
|
and not is_compose_expression(item)
|
||||||
# 2. Resolve explicit references
|
and item.startswith("@")
|
||||||
for key, value in resolved.items():
|
):
|
||||||
|
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
|
||||||
|
|
||||||
if isinstance(value, str) and value.startswith("@"):
|
if isinstance(value, str) and value.startswith("@"):
|
||||||
profile_name = value[1:]
|
profile_name = value[1:]
|
||||||
try:
|
try:
|
||||||
profile = self.get_profile(profile_name, resolve=True)
|
profile = _get_resolved_ref(profile_name)
|
||||||
resolved[key] = profile.get(key, "")
|
resolved[key] = profile.get(key, "")
|
||||||
except ProfileNotFoundError:
|
except ProfileNotFoundError:
|
||||||
resolved[key] = ""
|
resolved[key] = ""
|
||||||
@@ -67,68 +214,88 @@ class ProfileService(BaseService):
|
|||||||
for item in value:
|
for item in value:
|
||||||
if isinstance(item, str) and item.startswith("@"):
|
if isinstance(item, str) and item.startswith("@"):
|
||||||
profile_name = item[1:]
|
profile_name = item[1:]
|
||||||
try:
|
with suppress(ProfileNotFoundError):
|
||||||
profile = self.get_profile(profile_name, resolve=True)
|
profile = _get_resolved_ref(profile_name)
|
||||||
if "password" in profile:
|
if "password" in profile:
|
||||||
resolved_list.append(profile["password"])
|
resolved_list.append(profile["password"])
|
||||||
except ProfileNotFoundError:
|
|
||||||
pass
|
|
||||||
else:
|
else:
|
||||||
resolved_list.append(item)
|
resolved_list.append(item)
|
||||||
resolved[key] = resolved_list
|
resolved[key] = resolved_list
|
||||||
|
|
||||||
# 3. Inheritance: Fill empty keys from the first referenced profile
|
|
||||||
if referenced_profiles:
|
|
||||||
base_profile_name = referenced_profiles[0]
|
|
||||||
try:
|
|
||||||
base_profile = self.get_profile(base_profile_name, resolve=True)
|
|
||||||
for key, value in base_profile.items():
|
|
||||||
# Fill if key is missing or empty
|
|
||||||
if key not in resolved or resolved[key] == "" or resolved[key] == [] or resolved[key] is None:
|
|
||||||
resolved[key] = value
|
|
||||||
except ProfileNotFoundError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
# 4. Handle default protocol
|
dedup_auth_profiles = list(dict.fromkeys(auth_referenced_profiles))
|
||||||
if resolved.get("protocol") == "" or resolved.get("protocol") is None:
|
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)
|
||||||
|
|
||||||
|
if resolved.get("protocol") in ("", None):
|
||||||
try:
|
try:
|
||||||
default_profile = self.get_profile("default", resolve=True)
|
default_profile = _get_resolved_ref("default")
|
||||||
resolved["protocol"] = default_profile.get("protocol", "ssh")
|
resolved["protocol"] = default_profile.get("protocol", "ssh") or "ssh"
|
||||||
except ProfileNotFoundError:
|
except ProfileNotFoundError:
|
||||||
resolved["protocol"] = "ssh"
|
resolved["protocol"] = "ssh"
|
||||||
|
|
||||||
return resolved
|
return resolved
|
||||||
|
|
||||||
def delete_profile(self, name):
|
def delete_profile(self, name):
|
||||||
"""Delete an existing profile, with safety checks."""
|
"""Delete an existing profile, with safety checks."""
|
||||||
|
if _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:
|
if name not in self.config.profiles:
|
||||||
raise ProfileNotFoundError(f"Profile '{name}' not found.")
|
raise ProfileNotFoundError(f"Profile '{name}' not found.")
|
||||||
|
|
||||||
if name == "default":
|
if name == "default":
|
||||||
raise InvalidConfigurationError("Cannot delete the 'default' profile.")
|
raise InvalidConfigurationError("Cannot delete the 'default' profile.")
|
||||||
|
|
||||||
used_by = self.config._profileused(name)
|
used_by = self.config._profileused(name)
|
||||||
if used_by:
|
if used_by:
|
||||||
# We return the list of nodes using it so the UI can inform the user
|
raise InvalidConfigurationError(
|
||||||
raise InvalidConfigurationError(f"Profile '{name}' is used by nodes: {', '.join(used_by)}")
|
f"Profile '{name}' is used by nodes: {', '.join(used_by)}"
|
||||||
|
)
|
||||||
self.config._profiles_del(id=name)
|
self.config._profiles_del(id=name)
|
||||||
self.config._saveconfig(self.config.file)
|
self.config._saveconfig(self.config.file)
|
||||||
|
|
||||||
def update_profile(self, name, data):
|
def update_profile(self, name, data):
|
||||||
"""Update an existing profile."""
|
"""Update an existing profile."""
|
||||||
|
if _supports_transactional_update(self.config):
|
||||||
|
def _mut(cfg):
|
||||||
|
if name not in cfg.profiles:
|
||||||
|
raise ProfileNotFoundError(f"Profile '{name}' not found.")
|
||||||
|
updated_data = deepcopy(cfg.profiles[name])
|
||||||
|
updated_data.update(deepcopy(data))
|
||||||
|
filtered_data = {
|
||||||
|
k: v for k, v in updated_data.items() if k in self._ALLOWED_KEYS
|
||||||
|
}
|
||||||
|
self._validate_profile_payload(filtered_data)
|
||||||
|
cfg._profiles_add_unlocked(id=name, **filtered_data)
|
||||||
|
|
||||||
|
self.config.transactional_update(_mut)
|
||||||
|
return
|
||||||
|
|
||||||
if name not in self.config.profiles:
|
if name not in self.config.profiles:
|
||||||
raise ProfileNotFoundError(f"Profile '{name}' not found.")
|
raise ProfileNotFoundError(f"Profile '{name}' not found.")
|
||||||
|
|
||||||
# Merge with existing data
|
|
||||||
existing = self.get_profile(name, resolve=False)
|
existing = self.get_profile(name, resolve=False)
|
||||||
updated_data = existing.copy()
|
updated_data = deepcopy(existing)
|
||||||
updated_data.update(data)
|
updated_data.update(data)
|
||||||
|
filtered_data = {k: v for k, v in updated_data.items() if k in self._ALLOWED_KEYS}
|
||||||
# Filter data to match _profiles_add signature
|
self._validate_profile_payload(filtered_data)
|
||||||
allowed_keys = {"host", "options", "logs", "password", "port", "protocol", "user", "tags", "jumphost"}
|
|
||||||
filtered_data = {k: v for k, v in updated_data.items() if k in allowed_keys}
|
|
||||||
|
|
||||||
self.config._profiles_add(id=name, **filtered_data)
|
self.config._profiles_add(id=name, **filtered_data)
|
||||||
self.config._saveconfig(self.config.file)
|
self.config._saveconfig(self.config.file)
|
||||||
|
|
||||||
+70
-23
@@ -1,5 +1,9 @@
|
|||||||
|
import inspect
|
||||||
|
from contextlib import suppress
|
||||||
|
|
||||||
from .exceptions import InvalidConfigurationError
|
from .exceptions import InvalidConfigurationError
|
||||||
|
|
||||||
|
|
||||||
class RemoteStub:
|
class RemoteStub:
|
||||||
def __getattr__(self, name):
|
def __getattr__(self, name):
|
||||||
raise NotImplementedError(
|
raise NotImplementedError(
|
||||||
@@ -7,69 +11,95 @@ class RemoteStub:
|
|||||||
"Use local mode or wait for the gRPC implementation."
|
"Use local mode or wait for the gRPC implementation."
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class ServiceProvider:
|
class ServiceProvider:
|
||||||
"""Dynamic service backend. Transparently provides local or remote services."""
|
"""Dynamic service backend. Transparently provides local or remote services."""
|
||||||
|
|
||||||
def __init__(self, config, mode="local", remote_host=None):
|
def __init__(self, config, mode="local", remote_host=None, enforce_context: bool = True):
|
||||||
|
from ..auth_utils import AuthAttemptTracker
|
||||||
|
|
||||||
self.mode = mode
|
self.mode = mode
|
||||||
self.config = config
|
self.config = config
|
||||||
self.remote_host = remote_host
|
self.remote_host = remote_host
|
||||||
|
self.enforce_context = bool(enforce_context)
|
||||||
|
self.auth_tracker = AuthAttemptTracker()
|
||||||
|
self.nodes = None
|
||||||
self._system = None
|
self._system = None
|
||||||
self._execution = None
|
self._execution = None
|
||||||
self._import_export = None
|
self._import_export = None
|
||||||
self._sync = None
|
self._sync = None
|
||||||
self._users = None
|
self._users = None
|
||||||
self._ai = None
|
self._ai = None
|
||||||
|
self.set_enforce_context(self.enforce_context)
|
||||||
|
|
||||||
if mode == "local":
|
if mode == "local":
|
||||||
self._init_local()
|
self._init_local()
|
||||||
elif mode == "remote":
|
elif mode == "remote":
|
||||||
self._init_remote()
|
self._init_remote()
|
||||||
else:
|
else:
|
||||||
raise ValueError(f"Unknown service mode: {mode}")
|
raise ValueError(f"Unknown service mode: {mode}")
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
def _init_local(self):
|
def _init_local(self):
|
||||||
from .node_service import NodeService
|
|
||||||
from .profile_service import ProfileService
|
|
||||||
from .config_service import ConfigService
|
from .config_service import ConfigService
|
||||||
from .plugin_service import PluginService
|
|
||||||
from .context_service import ContextService
|
from .context_service import ContextService
|
||||||
|
from .node_service import NodeService
|
||||||
self.nodes = NodeService(self.config)
|
from .plugin_service import PluginService
|
||||||
|
from .profile_service import ProfileService
|
||||||
|
|
||||||
|
self.nodes = NodeService(self.config, enforce_context=self.enforce_context)
|
||||||
self.profiles = ProfileService(self.config)
|
self.profiles = ProfileService(self.config)
|
||||||
self.config_svc = ConfigService(self.config)
|
self.config_svc = ConfigService(self.config)
|
||||||
self.plugins = PluginService(self.config)
|
self.plugins = PluginService(self.config)
|
||||||
self.context = ContextService(self.config)
|
self.context = ContextService(self.config)
|
||||||
|
|
||||||
def _init_remote(self):
|
def _init_remote(self):
|
||||||
# Allow ConfigService to work locally so the user can revert the mode
|
# Allow ConfigService to work locally so the user can revert the mode
|
||||||
from .config_service import ConfigService
|
from .config_service import ConfigService
|
||||||
from .context_service import ContextService
|
from .context_service import ContextService
|
||||||
|
|
||||||
self.config_svc = ConfigService(self.config)
|
self.config_svc = ConfigService(self.config)
|
||||||
self.context = ContextService(self.config)
|
self.context = ContextService(self.config)
|
||||||
|
|
||||||
if not self.remote_host:
|
if not self.remote_host:
|
||||||
raise InvalidConfigurationError("Remote host must be specified in remote mode")
|
raise InvalidConfigurationError("Remote host must be specified in remote mode")
|
||||||
|
|
||||||
import grpc
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
import grpc
|
||||||
|
|
||||||
from ..grpc_layer.stubs import (
|
from ..grpc_layer.stubs import (
|
||||||
NodeStub, ProfileStub, PluginStub, AIStub,
|
AIStub,
|
||||||
ExecutionStub, ImportExportStub, SystemStub,
|
AuthClientInterceptor,
|
||||||
ConfigStub, AuthClientInterceptor, AuthStub
|
AuthStub,
|
||||||
|
ConfigStub,
|
||||||
|
ExecutionStub,
|
||||||
|
ImportExportStub,
|
||||||
|
NodeStub,
|
||||||
|
PluginStub,
|
||||||
|
ProfileStub,
|
||||||
|
SystemStub,
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_token():
|
def get_token():
|
||||||
env_token = os.environ.get("CONNPY_TOKEN")
|
env_token = os.environ.get("CONNPY_TOKEN")
|
||||||
if env_token:
|
if env_token:
|
||||||
return env_token
|
return env_token
|
||||||
token_path = os.path.join(self.config.defaultdir, ".token")
|
token_path = os.path.join(self.config.defaultdir, ".token")
|
||||||
if os.path.exists(token_path):
|
if os.path.exists(token_path):
|
||||||
try:
|
with suppress(OSError, UnicodeDecodeError), open(token_path) as f:
|
||||||
with open(token_path, "r") as f:
|
return f.read().strip()
|
||||||
return f.read().strip()
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
channel = grpc.insecure_channel(self.remote_host)
|
channel = grpc.insecure_channel(self.remote_host)
|
||||||
@@ -86,7 +116,12 @@ class ServiceProvider:
|
|||||||
self.plugins = PluginStub(channel, remote_host=self.remote_host)
|
self.plugins = PluginStub(channel, remote_host=self.remote_host)
|
||||||
self.ai = AIStub(channel, remote_host=self.remote_host)
|
self.ai = AIStub(channel, remote_host=self.remote_host)
|
||||||
self.system = SystemStub(channel, remote_host=self.remote_host)
|
self.system = SystemStub(channel, remote_host=self.remote_host)
|
||||||
self.execution = ExecutionStub(channel, remote_host=self.remote_host)
|
self.execution = ExecutionStub(
|
||||||
|
channel,
|
||||||
|
remote_host=self.remote_host,
|
||||||
|
config=self.config,
|
||||||
|
node_stub=self.nodes,
|
||||||
|
)
|
||||||
self.import_export = ImportExportStub(channel, remote_host=self.remote_host)
|
self.import_export = ImportExportStub(channel, remote_host=self.remote_host)
|
||||||
self.auth = AuthStub(channel, remote_host=self.remote_host)
|
self.auth = AuthStub(channel, remote_host=self.remote_host)
|
||||||
|
|
||||||
@@ -105,7 +140,13 @@ class ServiceProvider:
|
|||||||
def execution(self):
|
def execution(self):
|
||||||
if self._execution is None and self.mode == "local":
|
if self._execution is None and self.mode == "local":
|
||||||
from .execution_service import ExecutionService
|
from .execution_service import ExecutionService
|
||||||
self._execution = ExecutionService(self.config)
|
params = inspect.signature(ExecutionService.__init__).parameters
|
||||||
|
kwargs = {}
|
||||||
|
if "attempt_tracker" in params:
|
||||||
|
kwargs["attempt_tracker"] = self.auth_tracker
|
||||||
|
if "enforce_context" in params:
|
||||||
|
kwargs["enforce_context"] = self.enforce_context
|
||||||
|
self._execution = ExecutionService(self.config, **kwargs)
|
||||||
return self._execution
|
return self._execution
|
||||||
|
|
||||||
@execution.setter
|
@execution.setter
|
||||||
@@ -116,7 +157,13 @@ class ServiceProvider:
|
|||||||
def import_export(self):
|
def import_export(self):
|
||||||
if self._import_export is None and self.mode == "local":
|
if self._import_export is None and self.mode == "local":
|
||||||
from .import_export_service import ImportExportService
|
from .import_export_service import ImportExportService
|
||||||
self._import_export = ImportExportService(self.config)
|
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
|
return self._import_export
|
||||||
|
|
||||||
@import_export.setter
|
@import_export.setter
|
||||||
|
|||||||
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-command" in config_completions
|
||||||
assert "--shell-prompt" in config_completions
|
assert "--shell-prompt" in config_completions
|
||||||
assert "--shell-os" in config_completions
|
assert "--shell-os" in config_completions
|
||||||
|
assert "--max-auth-prompts" in config_completions
|
||||||
|
assert "--max-total-auth-attempts" in config_completions
|
||||||
|
assert "--max-hop-host-attempts" in config_completions
|
||||||
|
assert "--max-global-user-attempts" in config_completions
|
||||||
|
assert "--disable-fallback-on-insecure-hostkey" in config_completions
|
||||||
|
|
||||||
# Resolve when --engineer-auth is chosen in config
|
# Resolve when --engineer-auth is chosen in config
|
||||||
auth_comp = resolve_completion(["config", "--engineer-auth", ""], tree)
|
auth_comp = resolve_completion(["config", "--engineer-auth", ""], tree)
|
||||||
assert isinstance(auth_comp, list)
|
assert isinstance(auth_comp, list)
|
||||||
|
|
||||||
|
# Resolve boolean values for --disable-fallback-on-insecure-hostkey
|
||||||
|
assert resolve_completion(
|
||||||
|
["config", "--disable-fallback-on-insecure-hostkey", ""],
|
||||||
|
tree,
|
||||||
|
) == ["true", "false"]
|
||||||
|
|
||||||
# Loop back check:
|
# Loop back check:
|
||||||
# e.g., connpy config --engineer-auth some_val
|
# e.g., connpy config --engineer-auth some_val
|
||||||
# should loop back and resolve to config options
|
# should loop back and resolve to config options
|
||||||
@@ -93,6 +104,10 @@ class TestTreeCompletions:
|
|||||||
assert "--engineer-auth" in loop_back_comp
|
assert "--engineer-auth" in loop_back_comp
|
||||||
assert "--shell-command" in loop_back_comp
|
assert "--shell-command" in loop_back_comp
|
||||||
|
|
||||||
|
auth_budget_loop_back = resolve_completion(["config", "--max-auth-prompts", "3", ""], tree)
|
||||||
|
assert "--max-total-auth-attempts" in auth_budget_loop_back
|
||||||
|
assert "--disable-fallback-on-insecure-hostkey" in auth_budget_loop_back
|
||||||
|
|
||||||
def test_shell_completions(self):
|
def test_shell_completions(self):
|
||||||
from connpy.completion import _build_tree, resolve_completion
|
from connpy.completion import _build_tree, resolve_completion
|
||||||
tree = _build_tree([], [], [], {}, "/tmp")
|
tree = _build_tree([], [], [], {}, "/tmp")
|
||||||
|
|||||||
File diff suppressed because it is too large.
Load diff
@@ -80,7 +80,12 @@ el.replaceWith(d);
|
|||||||
"sync_remote": self.set_sync_remote,
|
"sync_remote": self.set_sync_remote,
|
||||||
"shell_command": self.set_shell_config,
|
"shell_command": self.set_shell_config,
|
||||||
"shell_prompt": self.set_shell_config,
|
"shell_prompt": self.set_shell_config,
|
||||||
"shell_os": self.set_shell_config
|
"shell_os": self.set_shell_config,
|
||||||
|
"max_auth_prompts": self.set_auth_budget,
|
||||||
|
"max_total_auth_attempts": self.set_auth_budget,
|
||||||
|
"max_hop_host_attempts": self.set_auth_budget,
|
||||||
|
"max_global_user_attempts": self.set_auth_budget,
|
||||||
|
"disable_fallback_on_insecure_hostkey": self.set_disable_fallback_on_insecure_hostkey,
|
||||||
}
|
}
|
||||||
handler = actions.get(getattr(args, "command", None))
|
handler = actions.get(getattr(args, "command", None))
|
||||||
if handler:
|
if handler:
|
||||||
@@ -251,7 +256,71 @@ el.replaceWith(d);
|
|||||||
self.app.services.config_svc.update_setting("shell", shell_cfg)
|
self.app.services.config_svc.update_setting("shell", shell_cfg)
|
||||||
printer.success("Config saved")
|
printer.success("Config saved")
|
||||||
except (ConnpyError, InvalidConfigurationError) as e:
|
except (ConnpyError, InvalidConfigurationError) as e:
|
||||||
printer.error(str(e))</code></pre>
|
printer.error(str(e))
|
||||||
|
|
||||||
|
def set_auth_budget(self, args):
|
||||||
|
key = args.command
|
||||||
|
raw_val = args.data[0] if isinstance(args.data, list) else args.data
|
||||||
|
bounds_map = {
|
||||||
|
"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>
|
</details>
|
||||||
<div class="desc"></div>
|
<div class="desc"></div>
|
||||||
<h3>Methods</h3>
|
<h3>Methods</h3>
|
||||||
@@ -285,7 +354,12 @@ el.replaceWith(d);
|
|||||||
"sync_remote": self.set_sync_remote,
|
"sync_remote": self.set_sync_remote,
|
||||||
"shell_command": self.set_shell_config,
|
"shell_command": self.set_shell_config,
|
||||||
"shell_prompt": self.set_shell_config,
|
"shell_prompt": self.set_shell_config,
|
||||||
"shell_os": self.set_shell_config
|
"shell_os": self.set_shell_config,
|
||||||
|
"max_auth_prompts": self.set_auth_budget,
|
||||||
|
"max_total_auth_attempts": self.set_auth_budget,
|
||||||
|
"max_hop_host_attempts": self.set_auth_budget,
|
||||||
|
"max_global_user_attempts": self.set_auth_budget,
|
||||||
|
"disable_fallback_on_insecure_hostkey": self.set_disable_fallback_on_insecure_hostkey,
|
||||||
}
|
}
|
||||||
handler = actions.get(getattr(args, "command", None))
|
handler = actions.get(getattr(args, "command", None))
|
||||||
if handler:
|
if handler:
|
||||||
@@ -333,6 +407,64 @@ el.replaceWith(d);
|
|||||||
</details>
|
</details>
|
||||||
<div class="desc"></div>
|
<div class="desc"></div>
|
||||||
</dd>
|
</dd>
|
||||||
|
<dt id="connpy.cli.config_handler.ConfigHandler.set_auth_budget"><code class="name flex">
|
||||||
|
<span>def <span class="ident">set_auth_budget</span></span>(<span>self, args)</span>
|
||||||
|
</code></dt>
|
||||||
|
<dd>
|
||||||
|
<details class="source">
|
||||||
|
<summary>
|
||||||
|
<span>Expand source code</span>
|
||||||
|
</summary>
|
||||||
|
<pre><code class="python">def set_auth_budget(self, args):
|
||||||
|
key = args.command
|
||||||
|
raw_val = args.data[0] if isinstance(args.data, list) else args.data
|
||||||
|
bounds_map = {
|
||||||
|
"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">
|
<dt id="connpy.cli.config_handler.ConfigHandler.set_case"><code class="name flex">
|
||||||
<span>def <span class="ident">set_case</span></span>(<span>self, args)</span>
|
<span>def <span class="ident">set_case</span></span>(<span>self, args)</span>
|
||||||
</code></dt>
|
</code></dt>
|
||||||
@@ -367,6 +499,32 @@ el.replaceWith(d);
|
|||||||
</details>
|
</details>
|
||||||
<div class="desc"></div>
|
<div class="desc"></div>
|
||||||
</dd>
|
</dd>
|
||||||
|
<dt id="connpy.cli.config_handler.ConfigHandler.set_disable_fallback_on_insecure_hostkey"><code class="name flex">
|
||||||
|
<span>def <span class="ident">set_disable_fallback_on_insecure_hostkey</span></span>(<span>self, args)</span>
|
||||||
|
</code></dt>
|
||||||
|
<dd>
|
||||||
|
<details class="source">
|
||||||
|
<summary>
|
||||||
|
<span>Expand source code</span>
|
||||||
|
</summary>
|
||||||
|
<pre><code class="python">def set_disable_fallback_on_insecure_hostkey(self, args):
|
||||||
|
raw_val = args.data[0] if isinstance(args.data, list) else args.data
|
||||||
|
if isinstance(raw_val, bool):
|
||||||
|
val = raw_val
|
||||||
|
elif isinstance(raw_val, str) and raw_val.strip().lower() in ("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">
|
<dt id="connpy.cli.config_handler.ConfigHandler.set_fzf"><code class="name flex">
|
||||||
<span>def <span class="ident">set_fzf</span></span>(<span>self, args)</span>
|
<span>def <span class="ident">set_fzf</span></span>(<span>self, args)</span>
|
||||||
</code></dt>
|
</code></dt>
|
||||||
@@ -577,11 +735,13 @@ el.replaceWith(d);
|
|||||||
<ul>
|
<ul>
|
||||||
<li>
|
<li>
|
||||||
<h4><code><a title="connpy.cli.config_handler.ConfigHandler" href="#connpy.cli.config_handler.ConfigHandler">ConfigHandler</a></code></h4>
|
<h4><code><a title="connpy.cli.config_handler.ConfigHandler" href="#connpy.cli.config_handler.ConfigHandler">ConfigHandler</a></code></h4>
|
||||||
<ul class="two-column">
|
<ul class="">
|
||||||
<li><code><a title="connpy.cli.config_handler.ConfigHandler.dispatch" href="#connpy.cli.config_handler.ConfigHandler.dispatch">dispatch</a></code></li>
|
<li><code><a title="connpy.cli.config_handler.ConfigHandler.dispatch" href="#connpy.cli.config_handler.ConfigHandler.dispatch">dispatch</a></code></li>
|
||||||
<li><code><a title="connpy.cli.config_handler.ConfigHandler.set_ai_config" href="#connpy.cli.config_handler.ConfigHandler.set_ai_config">set_ai_config</a></code></li>
|
<li><code><a title="connpy.cli.config_handler.ConfigHandler.set_ai_config" href="#connpy.cli.config_handler.ConfigHandler.set_ai_config">set_ai_config</a></code></li>
|
||||||
|
<li><code><a title="connpy.cli.config_handler.ConfigHandler.set_auth_budget" href="#connpy.cli.config_handler.ConfigHandler.set_auth_budget">set_auth_budget</a></code></li>
|
||||||
<li><code><a title="connpy.cli.config_handler.ConfigHandler.set_case" href="#connpy.cli.config_handler.ConfigHandler.set_case">set_case</a></code></li>
|
<li><code><a title="connpy.cli.config_handler.ConfigHandler.set_case" href="#connpy.cli.config_handler.ConfigHandler.set_case">set_case</a></code></li>
|
||||||
<li><code><a title="connpy.cli.config_handler.ConfigHandler.set_configfolder" href="#connpy.cli.config_handler.ConfigHandler.set_configfolder">set_configfolder</a></code></li>
|
<li><code><a title="connpy.cli.config_handler.ConfigHandler.set_configfolder" href="#connpy.cli.config_handler.ConfigHandler.set_configfolder">set_configfolder</a></code></li>
|
||||||
|
<li><code><a title="connpy.cli.config_handler.ConfigHandler.set_disable_fallback_on_insecure_hostkey" href="#connpy.cli.config_handler.ConfigHandler.set_disable_fallback_on_insecure_hostkey">set_disable_fallback_on_insecure_hostkey</a></code></li>
|
||||||
<li><code><a title="connpy.cli.config_handler.ConfigHandler.set_fzf" href="#connpy.cli.config_handler.ConfigHandler.set_fzf">set_fzf</a></code></li>
|
<li><code><a title="connpy.cli.config_handler.ConfigHandler.set_fzf" href="#connpy.cli.config_handler.ConfigHandler.set_fzf">set_fzf</a></code></li>
|
||||||
<li><code><a title="connpy.cli.config_handler.ConfigHandler.set_idletime" href="#connpy.cli.config_handler.ConfigHandler.set_idletime">set_idletime</a></code></li>
|
<li><code><a title="connpy.cli.config_handler.ConfigHandler.set_idletime" href="#connpy.cli.config_handler.ConfigHandler.set_idletime">set_idletime</a></code></li>
|
||||||
<li><code><a title="connpy.cli.config_handler.ConfigHandler.set_remote_host" href="#connpy.cli.config_handler.ConfigHandler.set_remote_host">set_remote_host</a></code></li>
|
<li><code><a title="connpy.cli.config_handler.ConfigHandler.set_remote_host" href="#connpy.cli.config_handler.ConfigHandler.set_remote_host">set_remote_host</a></code></li>
|
||||||
|
|||||||
+148
-56
@@ -72,6 +72,7 @@ el.replaceWith(d);
|
|||||||
questions.append(inquirer.Confirm("jumphost", message="Edit jumphost?"))
|
questions.append(inquirer.Confirm("jumphost", message="Edit jumphost?"))
|
||||||
questions.append(inquirer.Confirm("user", message="Edit User?"))
|
questions.append(inquirer.Confirm("user", message="Edit User?"))
|
||||||
questions.append(inquirer.Confirm("password", message="Edit password?"))
|
questions.append(inquirer.Confirm("password", message="Edit password?"))
|
||||||
|
questions.append(inquirer.Confirm("fallback", message="Edit fallback credentials?"))
|
||||||
return inquirer.prompt(questions)
|
return inquirer.prompt(questions)
|
||||||
|
|
||||||
def questions_nodes(self, unique, uniques=None, edit=None):
|
def questions_nodes(self, unique, uniques=None, edit=None):
|
||||||
@@ -82,11 +83,13 @@ el.replaceWith(d);
|
|||||||
defaults["tags"] = ""
|
defaults["tags"] = ""
|
||||||
if "jumphost" not in defaults:
|
if "jumphost" not in defaults:
|
||||||
defaults["jumphost"] = ""
|
defaults["jumphost"] = ""
|
||||||
except Exception:
|
if "fallback" not in defaults:
|
||||||
defaults = {"host": "", "protocol": "", "port": "", "user": "", "options": "", "logs": "", "tags": "", "password": "", "jumphost": ""}
|
defaults["fallback"] = ""
|
||||||
|
except Exception: # noqa: BLE001
|
||||||
|
defaults = {"host": "", "protocol": "", "port": "", "user": "", "options": "", "logs": "", "tags": "", "password": "", "jumphost": "", "fallback": ""}
|
||||||
node = {}
|
node = {}
|
||||||
if edit is None:
|
if edit is None:
|
||||||
edit = {"host": True, "protocol": True, "port": True, "user": True, "password": True, "options": True, "logs": True, "tags": True, "jumphost": True}
|
edit = {"host": True, "protocol": True, "port": True, "user": True, "password": True, "options": True, "logs": True, "tags": True, "jumphost": True, "fallback": True}
|
||||||
questions = []
|
questions = []
|
||||||
if edit["host"]:
|
if edit["host"]:
|
||||||
questions.append(inquirer.Text("host", message="Add Hostname or IP", validate=self.validators.host_validation, default=defaults["host"]))
|
questions.append(inquirer.Text("host", message="Add Hostname or IP", validate=self.validators.host_validation, default=defaults["host"]))
|
||||||
@@ -124,11 +127,13 @@ el.replaceWith(d);
|
|||||||
questions.append(inquirer.List("password", message="Password: Use a local password, no password or a list of profiles to reference?", choices=["Local Password", "Profiles", "No Password"]))
|
questions.append(inquirer.List("password", message="Password: Use a local password, no password or a list of profiles to reference?", choices=["Local Password", "Profiles", "No Password"]))
|
||||||
else:
|
else:
|
||||||
node["password"] = defaults["password"]
|
node["password"] = defaults["password"]
|
||||||
|
if not edit.get("fallback"):
|
||||||
|
node["fallback"] = defaults.get("fallback", "")
|
||||||
|
|
||||||
answer = inquirer.prompt(questions)
|
answer = inquirer.prompt(questions)
|
||||||
if answer is None:
|
if answer is None:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if "password" in answer:
|
if "password" in answer:
|
||||||
if answer["password"] == "Local Password":
|
if answer["password"] == "Local Password":
|
||||||
passq = [inquirer.Password("password", message="Set Password")]
|
passq = [inquirer.Password("password", message="Set Password")]
|
||||||
@@ -141,13 +146,29 @@ el.replaceWith(d);
|
|||||||
passa = inquirer.prompt(passq)
|
passa = inquirer.prompt(passq)
|
||||||
if passa is None:
|
if passa is None:
|
||||||
return False
|
return False
|
||||||
answer["password"] = passa["password"].split(",")
|
answer["password"] = [p.strip() for p in passa["password"].split(",") if p.strip()]
|
||||||
elif answer["password"] == "No Password":
|
elif answer["password"] == "No Password":
|
||||||
answer["password"] = ""
|
answer["password"] = ""
|
||||||
|
|
||||||
if "tags" in answer and not answer["tags"].startswith("@") and answer["tags"]:
|
if edit.get("fallback"):
|
||||||
answer["tags"] = ast.literal_eval(answer["tags"])
|
raw_def_fb = defaults.get("fallback", "")
|
||||||
|
def_fb_str = json.dumps(raw_def_fb) if isinstance(raw_def_fb, (list, dict)) else str(raw_def_fb or "")
|
||||||
|
fbq = [
|
||||||
|
inquirer.Text(
|
||||||
|
"fallback",
|
||||||
|
message="Add fallback credentials (@profile, @p1,@p2, JSON list/dict, or compose:)",
|
||||||
|
validate=self.validators.fallback_validation,
|
||||||
|
default=def_fb_str.replace("{", "{{").replace("}", "}}"),
|
||||||
|
)
|
||||||
|
]
|
||||||
|
fba = inquirer.prompt(fbq)
|
||||||
|
if fba is None:
|
||||||
|
return False
|
||||||
|
answer["fallback"] = _parse_fallback_input(fba["fallback"])
|
||||||
|
|
||||||
|
if "tags" in answer and answer["tags"] and not str(answer["tags"]).startswith("@"):
|
||||||
|
answer["tags"] = _parse_tags_input(answer["tags"])
|
||||||
|
|
||||||
result = {**uniques, **answer, **node}
|
result = {**uniques, **answer, **node}
|
||||||
result["type"] = "connection"
|
result["type"] = "connection"
|
||||||
return result
|
return result
|
||||||
@@ -160,11 +181,13 @@ el.replaceWith(d);
|
|||||||
defaults["tags"] = ""
|
defaults["tags"] = ""
|
||||||
if "jumphost" not in defaults:
|
if "jumphost" not in defaults:
|
||||||
defaults["jumphost"] = ""
|
defaults["jumphost"] = ""
|
||||||
except Exception:
|
if "fallback" not in defaults:
|
||||||
defaults = {"host": "", "protocol": "", "port": "", "user": "", "options": "", "logs": "", "tags": "", "jumphost": ""}
|
defaults["fallback"] = ""
|
||||||
|
except Exception: # noqa: BLE001
|
||||||
|
defaults = {"host": "", "protocol": "", "port": "", "user": "", "options": "", "logs": "", "tags": "", "jumphost": "", "fallback": ""}
|
||||||
profile = {}
|
profile = {}
|
||||||
if edit is None:
|
if edit is None:
|
||||||
edit = {"host": True, "protocol": True, "port": True, "user": True, "password": True, "options": True, "logs": True, "tags": True, "jumphost": True}
|
edit = {"host": True, "protocol": True, "port": True, "user": True, "password": True, "options": True, "logs": True, "tags": True, "jumphost": True, "fallback": True}
|
||||||
questions = []
|
questions = []
|
||||||
if edit["host"]:
|
if edit["host"]:
|
||||||
questions.append(inquirer.Text("host", message="Add Hostname or IP", default=defaults["host"]))
|
questions.append(inquirer.Text("host", message="Add Hostname or IP", default=defaults["host"]))
|
||||||
@@ -202,18 +225,25 @@ el.replaceWith(d);
|
|||||||
questions.append(inquirer.Password("password", message="Set Password"))
|
questions.append(inquirer.Password("password", message="Set Password"))
|
||||||
else:
|
else:
|
||||||
profile["password"] = defaults["password"]
|
profile["password"] = defaults["password"]
|
||||||
|
if edit.get("fallback"):
|
||||||
|
raw_def_fb = defaults.get("fallback", "")
|
||||||
|
def_fb_str = json.dumps(raw_def_fb) if isinstance(raw_def_fb, (list, dict)) else str(raw_def_fb or "")
|
||||||
|
questions.append(inquirer.Text("fallback", message="Add fallback credentials (@profile, @p1,@p2, JSON list/dict, or compose:)", validate=self.validators.fallback_validation, default=def_fb_str.replace("{", "{{").replace("}", "}}")))
|
||||||
|
else:
|
||||||
|
profile["fallback"] = defaults.get("fallback", "")
|
||||||
|
|
||||||
answer = inquirer.prompt(questions)
|
answer = inquirer.prompt(questions)
|
||||||
if answer is None:
|
if answer is None:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if "password" in answer:
|
if "password" in answer and answer["password"] != "":
|
||||||
if answer["password"] != "":
|
answer["password"] = self.app.services.config_svc.encrypt_password(answer["password"])
|
||||||
answer["password"] = self.app.services.config_svc.encrypt_password(answer["password"])
|
|
||||||
|
|
||||||
if "tags" in answer and answer["tags"]:
|
if "tags" in answer and answer["tags"]:
|
||||||
answer["tags"] = ast.literal_eval(answer["tags"])
|
answer["tags"] = _parse_tags_input(answer["tags"])
|
||||||
|
if "fallback" in answer:
|
||||||
|
answer["fallback"] = _parse_fallback_input(answer["fallback"])
|
||||||
|
|
||||||
result = {**answer, **profile}
|
result = {**answer, **profile}
|
||||||
result["id"] = unique
|
result["id"] = unique
|
||||||
return result
|
return result
|
||||||
@@ -232,27 +262,43 @@ el.replaceWith(d);
|
|||||||
questions.append(inquirer.Text("jumphost", message="Add Jumphost node", validate=self.validators.jumphost_validation))
|
questions.append(inquirer.Text("jumphost", message="Add Jumphost node", validate=self.validators.jumphost_validation))
|
||||||
questions.append(inquirer.Text("user", message="Pick username", validate=self.validators.default_validation))
|
questions.append(inquirer.Text("user", message="Pick username", validate=self.validators.default_validation))
|
||||||
questions.append(inquirer.List("password", message="Password: Use a local password, no password or a list of profiles to reference?", choices=["Local Password", "Profiles", "No Password"]))
|
questions.append(inquirer.List("password", message="Password: Use a local password, no password or a list of profiles to reference?", choices=["Local Password", "Profiles", "No Password"]))
|
||||||
|
|
||||||
answer = inquirer.prompt(questions)
|
answer = inquirer.prompt(questions)
|
||||||
if answer is None:
|
if answer is None:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if "password" in answer:
|
if "password" in answer:
|
||||||
if answer["password"] == "Local Password":
|
if answer["password"] == "Local Password":
|
||||||
passq = [inquirer.Password("password", message="Set Password")]
|
passq = [inquirer.Password("password", message="Set Password")]
|
||||||
passa = inquirer.prompt(passq)
|
passa = inquirer.prompt(passq)
|
||||||
|
if passa is None:
|
||||||
|
return False
|
||||||
answer["password"] = self.app.services.config_svc.encrypt_password(passa["password"])
|
answer["password"] = self.app.services.config_svc.encrypt_password(passa["password"])
|
||||||
elif answer["password"] == "Profiles":
|
elif answer["password"] == "Profiles":
|
||||||
passq = [(inquirer.Text("password", message="Set a @profile or a comma separated list of @profiles", validate=self.validators.pass_validation))]
|
passq = [(inquirer.Text("password", message="Set a @profile or a comma separated list of @profiles", validate=self.validators.pass_validation))]
|
||||||
passa = inquirer.prompt(passq)
|
passa = inquirer.prompt(passq)
|
||||||
answer["password"] = passa["password"].split(",")
|
if passa is None:
|
||||||
|
return False
|
||||||
|
answer["password"] = [p.strip() for p in passa["password"].split(",") if p.strip()]
|
||||||
elif answer["password"] == "No Password":
|
elif answer["password"] == "No Password":
|
||||||
answer["password"] = ""
|
answer["password"] = ""
|
||||||
|
|
||||||
|
fbq = [
|
||||||
|
inquirer.Text(
|
||||||
|
"fallback",
|
||||||
|
message="Add fallback credentials (@profile, @p1,@p2, JSON list/dict, or compose:)",
|
||||||
|
validate=self.validators.fallback_validation,
|
||||||
|
)
|
||||||
|
]
|
||||||
|
fba = inquirer.prompt(fbq)
|
||||||
|
if fba is None:
|
||||||
|
return False
|
||||||
|
answer["fallback"] = _parse_fallback_input(fba["fallback"])
|
||||||
|
|
||||||
answer["type"] = "connection"
|
answer["type"] = "connection"
|
||||||
if "tags" in answer and not answer["tags"].startswith("@") and answer["tags"]:
|
if "tags" in answer and answer["tags"] and not str(answer["tags"]).startswith("@"):
|
||||||
answer["tags"] = ast.literal_eval(answer["tags"])
|
answer["tags"] = _parse_tags_input(answer["tags"])
|
||||||
|
|
||||||
return answer
|
return answer
|
||||||
|
|
||||||
def mcp_wizard(self, mcp_servers):
|
def mcp_wizard(self, mcp_servers):
|
||||||
@@ -454,27 +500,43 @@ el.replaceWith(d);
|
|||||||
questions.append(inquirer.Text("jumphost", message="Add Jumphost node", validate=self.validators.jumphost_validation))
|
questions.append(inquirer.Text("jumphost", message="Add Jumphost node", validate=self.validators.jumphost_validation))
|
||||||
questions.append(inquirer.Text("user", message="Pick username", validate=self.validators.default_validation))
|
questions.append(inquirer.Text("user", message="Pick username", validate=self.validators.default_validation))
|
||||||
questions.append(inquirer.List("password", message="Password: Use a local password, no password or a list of profiles to reference?", choices=["Local Password", "Profiles", "No Password"]))
|
questions.append(inquirer.List("password", message="Password: Use a local password, no password or a list of profiles to reference?", choices=["Local Password", "Profiles", "No Password"]))
|
||||||
|
|
||||||
answer = inquirer.prompt(questions)
|
answer = inquirer.prompt(questions)
|
||||||
if answer is None:
|
if answer is None:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if "password" in answer:
|
if "password" in answer:
|
||||||
if answer["password"] == "Local Password":
|
if answer["password"] == "Local Password":
|
||||||
passq = [inquirer.Password("password", message="Set Password")]
|
passq = [inquirer.Password("password", message="Set Password")]
|
||||||
passa = inquirer.prompt(passq)
|
passa = inquirer.prompt(passq)
|
||||||
|
if passa is None:
|
||||||
|
return False
|
||||||
answer["password"] = self.app.services.config_svc.encrypt_password(passa["password"])
|
answer["password"] = self.app.services.config_svc.encrypt_password(passa["password"])
|
||||||
elif answer["password"] == "Profiles":
|
elif answer["password"] == "Profiles":
|
||||||
passq = [(inquirer.Text("password", message="Set a @profile or a comma separated list of @profiles", validate=self.validators.pass_validation))]
|
passq = [(inquirer.Text("password", message="Set a @profile or a comma separated list of @profiles", validate=self.validators.pass_validation))]
|
||||||
passa = inquirer.prompt(passq)
|
passa = inquirer.prompt(passq)
|
||||||
answer["password"] = passa["password"].split(",")
|
if passa is None:
|
||||||
|
return False
|
||||||
|
answer["password"] = [p.strip() for p in passa["password"].split(",") if p.strip()]
|
||||||
elif answer["password"] == "No Password":
|
elif answer["password"] == "No Password":
|
||||||
answer["password"] = ""
|
answer["password"] = ""
|
||||||
|
|
||||||
|
fbq = [
|
||||||
|
inquirer.Text(
|
||||||
|
"fallback",
|
||||||
|
message="Add fallback credentials (@profile, @p1,@p2, JSON list/dict, or compose:)",
|
||||||
|
validate=self.validators.fallback_validation,
|
||||||
|
)
|
||||||
|
]
|
||||||
|
fba = inquirer.prompt(fbq)
|
||||||
|
if fba is None:
|
||||||
|
return False
|
||||||
|
answer["fallback"] = _parse_fallback_input(fba["fallback"])
|
||||||
|
|
||||||
answer["type"] = "connection"
|
answer["type"] = "connection"
|
||||||
if "tags" in answer and not answer["tags"].startswith("@") and answer["tags"]:
|
if "tags" in answer and answer["tags"] and not str(answer["tags"]).startswith("@"):
|
||||||
answer["tags"] = ast.literal_eval(answer["tags"])
|
answer["tags"] = _parse_tags_input(answer["tags"])
|
||||||
|
|
||||||
return answer</code></pre>
|
return answer</code></pre>
|
||||||
</details>
|
</details>
|
||||||
<div class="desc"></div>
|
<div class="desc"></div>
|
||||||
@@ -499,6 +561,7 @@ el.replaceWith(d);
|
|||||||
questions.append(inquirer.Confirm("jumphost", message="Edit jumphost?"))
|
questions.append(inquirer.Confirm("jumphost", message="Edit jumphost?"))
|
||||||
questions.append(inquirer.Confirm("user", message="Edit User?"))
|
questions.append(inquirer.Confirm("user", message="Edit User?"))
|
||||||
questions.append(inquirer.Confirm("password", message="Edit password?"))
|
questions.append(inquirer.Confirm("password", message="Edit password?"))
|
||||||
|
questions.append(inquirer.Confirm("fallback", message="Edit fallback credentials?"))
|
||||||
return inquirer.prompt(questions)</code></pre>
|
return inquirer.prompt(questions)</code></pre>
|
||||||
</details>
|
</details>
|
||||||
<div class="desc"></div>
|
<div class="desc"></div>
|
||||||
@@ -519,11 +582,13 @@ el.replaceWith(d);
|
|||||||
defaults["tags"] = ""
|
defaults["tags"] = ""
|
||||||
if "jumphost" not in defaults:
|
if "jumphost" not in defaults:
|
||||||
defaults["jumphost"] = ""
|
defaults["jumphost"] = ""
|
||||||
except Exception:
|
if "fallback" not in defaults:
|
||||||
defaults = {"host": "", "protocol": "", "port": "", "user": "", "options": "", "logs": "", "tags": "", "password": "", "jumphost": ""}
|
defaults["fallback"] = ""
|
||||||
|
except Exception: # noqa: BLE001
|
||||||
|
defaults = {"host": "", "protocol": "", "port": "", "user": "", "options": "", "logs": "", "tags": "", "password": "", "jumphost": "", "fallback": ""}
|
||||||
node = {}
|
node = {}
|
||||||
if edit is None:
|
if edit is None:
|
||||||
edit = {"host": True, "protocol": True, "port": True, "user": True, "password": True, "options": True, "logs": True, "tags": True, "jumphost": True}
|
edit = {"host": True, "protocol": True, "port": True, "user": True, "password": True, "options": True, "logs": True, "tags": True, "jumphost": True, "fallback": True}
|
||||||
questions = []
|
questions = []
|
||||||
if edit["host"]:
|
if edit["host"]:
|
||||||
questions.append(inquirer.Text("host", message="Add Hostname or IP", validate=self.validators.host_validation, default=defaults["host"]))
|
questions.append(inquirer.Text("host", message="Add Hostname or IP", validate=self.validators.host_validation, default=defaults["host"]))
|
||||||
@@ -561,11 +626,13 @@ el.replaceWith(d);
|
|||||||
questions.append(inquirer.List("password", message="Password: Use a local password, no password or a list of profiles to reference?", choices=["Local Password", "Profiles", "No Password"]))
|
questions.append(inquirer.List("password", message="Password: Use a local password, no password or a list of profiles to reference?", choices=["Local Password", "Profiles", "No Password"]))
|
||||||
else:
|
else:
|
||||||
node["password"] = defaults["password"]
|
node["password"] = defaults["password"]
|
||||||
|
if not edit.get("fallback"):
|
||||||
|
node["fallback"] = defaults.get("fallback", "")
|
||||||
|
|
||||||
answer = inquirer.prompt(questions)
|
answer = inquirer.prompt(questions)
|
||||||
if answer is None:
|
if answer is None:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if "password" in answer:
|
if "password" in answer:
|
||||||
if answer["password"] == "Local Password":
|
if answer["password"] == "Local Password":
|
||||||
passq = [inquirer.Password("password", message="Set Password")]
|
passq = [inquirer.Password("password", message="Set Password")]
|
||||||
@@ -578,13 +645,29 @@ el.replaceWith(d);
|
|||||||
passa = inquirer.prompt(passq)
|
passa = inquirer.prompt(passq)
|
||||||
if passa is None:
|
if passa is None:
|
||||||
return False
|
return False
|
||||||
answer["password"] = passa["password"].split(",")
|
answer["password"] = [p.strip() for p in passa["password"].split(",") if p.strip()]
|
||||||
elif answer["password"] == "No Password":
|
elif answer["password"] == "No Password":
|
||||||
answer["password"] = ""
|
answer["password"] = ""
|
||||||
|
|
||||||
if "tags" in answer and not answer["tags"].startswith("@") and answer["tags"]:
|
if edit.get("fallback"):
|
||||||
answer["tags"] = ast.literal_eval(answer["tags"])
|
raw_def_fb = defaults.get("fallback", "")
|
||||||
|
def_fb_str = json.dumps(raw_def_fb) if isinstance(raw_def_fb, (list, dict)) else str(raw_def_fb or "")
|
||||||
|
fbq = [
|
||||||
|
inquirer.Text(
|
||||||
|
"fallback",
|
||||||
|
message="Add fallback credentials (@profile, @p1,@p2, JSON list/dict, or compose:)",
|
||||||
|
validate=self.validators.fallback_validation,
|
||||||
|
default=def_fb_str.replace("{", "{{").replace("}", "}}"),
|
||||||
|
)
|
||||||
|
]
|
||||||
|
fba = inquirer.prompt(fbq)
|
||||||
|
if fba is None:
|
||||||
|
return False
|
||||||
|
answer["fallback"] = _parse_fallback_input(fba["fallback"])
|
||||||
|
|
||||||
|
if "tags" in answer and answer["tags"] and not str(answer["tags"]).startswith("@"):
|
||||||
|
answer["tags"] = _parse_tags_input(answer["tags"])
|
||||||
|
|
||||||
result = {**uniques, **answer, **node}
|
result = {**uniques, **answer, **node}
|
||||||
result["type"] = "connection"
|
result["type"] = "connection"
|
||||||
return result</code></pre>
|
return result</code></pre>
|
||||||
@@ -607,11 +690,13 @@ el.replaceWith(d);
|
|||||||
defaults["tags"] = ""
|
defaults["tags"] = ""
|
||||||
if "jumphost" not in defaults:
|
if "jumphost" not in defaults:
|
||||||
defaults["jumphost"] = ""
|
defaults["jumphost"] = ""
|
||||||
except Exception:
|
if "fallback" not in defaults:
|
||||||
defaults = {"host": "", "protocol": "", "port": "", "user": "", "options": "", "logs": "", "tags": "", "jumphost": ""}
|
defaults["fallback"] = ""
|
||||||
|
except Exception: # noqa: BLE001
|
||||||
|
defaults = {"host": "", "protocol": "", "port": "", "user": "", "options": "", "logs": "", "tags": "", "jumphost": "", "fallback": ""}
|
||||||
profile = {}
|
profile = {}
|
||||||
if edit is None:
|
if edit is None:
|
||||||
edit = {"host": True, "protocol": True, "port": True, "user": True, "password": True, "options": True, "logs": True, "tags": True, "jumphost": True}
|
edit = {"host": True, "protocol": True, "port": True, "user": True, "password": True, "options": True, "logs": True, "tags": True, "jumphost": True, "fallback": True}
|
||||||
questions = []
|
questions = []
|
||||||
if edit["host"]:
|
if edit["host"]:
|
||||||
questions.append(inquirer.Text("host", message="Add Hostname or IP", default=defaults["host"]))
|
questions.append(inquirer.Text("host", message="Add Hostname or IP", default=defaults["host"]))
|
||||||
@@ -649,18 +734,25 @@ el.replaceWith(d);
|
|||||||
questions.append(inquirer.Password("password", message="Set Password"))
|
questions.append(inquirer.Password("password", message="Set Password"))
|
||||||
else:
|
else:
|
||||||
profile["password"] = defaults["password"]
|
profile["password"] = defaults["password"]
|
||||||
|
if edit.get("fallback"):
|
||||||
|
raw_def_fb = defaults.get("fallback", "")
|
||||||
|
def_fb_str = json.dumps(raw_def_fb) if isinstance(raw_def_fb, (list, dict)) else str(raw_def_fb or "")
|
||||||
|
questions.append(inquirer.Text("fallback", message="Add fallback credentials (@profile, @p1,@p2, JSON list/dict, or compose:)", validate=self.validators.fallback_validation, default=def_fb_str.replace("{", "{{").replace("}", "}}")))
|
||||||
|
else:
|
||||||
|
profile["fallback"] = defaults.get("fallback", "")
|
||||||
|
|
||||||
answer = inquirer.prompt(questions)
|
answer = inquirer.prompt(questions)
|
||||||
if answer is None:
|
if answer is None:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if "password" in answer:
|
if "password" in answer and answer["password"] != "":
|
||||||
if answer["password"] != "":
|
answer["password"] = self.app.services.config_svc.encrypt_password(answer["password"])
|
||||||
answer["password"] = self.app.services.config_svc.encrypt_password(answer["password"])
|
|
||||||
|
|
||||||
if "tags" in answer and answer["tags"]:
|
if "tags" in answer and answer["tags"]:
|
||||||
answer["tags"] = ast.literal_eval(answer["tags"])
|
answer["tags"] = _parse_tags_input(answer["tags"])
|
||||||
|
if "fallback" in answer:
|
||||||
|
answer["fallback"] = _parse_fallback_input(answer["fallback"])
|
||||||
|
|
||||||
result = {**answer, **profile}
|
result = {**answer, **profile}
|
||||||
result["id"] = unique
|
result["id"] = unique
|
||||||
return result</code></pre>
|
return result</code></pre>
|
||||||
|
|||||||
@@ -193,6 +193,7 @@ el.replaceWith(d);
|
|||||||
uniques = self.app.services.nodes.explode_unique(args.data)
|
uniques = self.app.services.nodes.explode_unique(args.data)
|
||||||
if not uniques:
|
if not uniques:
|
||||||
raise InvalidConfigurationError(f"Invalid folder {args.data}")
|
raise InvalidConfigurationError(f"Invalid folder {args.data}")
|
||||||
|
self.app.services.nodes.validate_parent_folder(args.data, is_folder=True)
|
||||||
self.app.services.nodes.add_node(args.data, {}, is_folder=True)
|
self.app.services.nodes.add_node(args.data, {}, is_folder=True)
|
||||||
printer.success(f"{args.data} added successfully")
|
printer.success(f"{args.data} added successfully")
|
||||||
else:
|
else:
|
||||||
@@ -364,6 +365,7 @@ def forms(self):
|
|||||||
uniques = self.app.services.nodes.explode_unique(args.data)
|
uniques = self.app.services.nodes.explode_unique(args.data)
|
||||||
if not uniques:
|
if not uniques:
|
||||||
raise InvalidConfigurationError(f"Invalid folder {args.data}")
|
raise InvalidConfigurationError(f"Invalid folder {args.data}")
|
||||||
|
self.app.services.nodes.validate_parent_folder(args.data, is_folder=True)
|
||||||
self.app.services.nodes.add_node(args.data, {}, is_folder=True)
|
self.app.services.nodes.add_node(args.data, {}, is_folder=True)
|
||||||
printer.success(f"{args.data} added successfully")
|
printer.success(f"{args.data} added successfully")
|
||||||
else:
|
else:
|
||||||
|
|||||||
+231
-54
@@ -106,50 +106,119 @@ el.replaceWith(d);
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
def pass_validation(self, answers, current, regex = "(^@.+$)"):
|
def pass_validation(self, answers, current, regex = "(^@.+$)"):
|
||||||
|
if is_compose_expression(current):
|
||||||
|
try:
|
||||||
|
validate_compose_syntax(current)
|
||||||
|
except SanitizedAuthError as exc:
|
||||||
|
_raise_val_err(str(exc))
|
||||||
|
return True
|
||||||
profiles = current.split(",")
|
profiles = current.split(",")
|
||||||
for i in profiles:
|
for i in profiles:
|
||||||
if not re.match(regex, i) or i[1:] not in self.app.profiles:
|
if not re.match(regex, i) or i[1:] not in self.app.profiles:
|
||||||
_raise_val_err("Profile {} don't exist".format(i))
|
_raise_val_err("Profile {} don't exist".format(i))
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
def _validate_tags_dict(self, current: str):
|
||||||
|
if len(current.encode("utf-8", errors="replace")) > MAX_JSON_INPUT_BYTES:
|
||||||
|
_raise_val_err(f"Tags exceed maximum size of {MAX_JSON_INPUT_BYTES} bytes")
|
||||||
|
parsed = None
|
||||||
|
try:
|
||||||
|
parsed = json.loads(current)
|
||||||
|
except (ValueError, TypeError):
|
||||||
|
try:
|
||||||
|
parsed = ast.literal_eval(current)
|
||||||
|
except (ValueError, TypeError, SyntaxError, MemoryError, RecursionError):
|
||||||
|
parsed = None
|
||||||
|
if not isinstance(parsed, dict):
|
||||||
|
_raise_val_err("Tags should be a python dictionary.")
|
||||||
|
try:
|
||||||
|
validate_json_depth_and_size(
|
||||||
|
parsed,
|
||||||
|
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||||
|
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||||
|
)
|
||||||
|
validate_auth_budget_tags(parsed)
|
||||||
|
except (ValueError, TypeError, SanitizedAuthError) as exc:
|
||||||
|
_raise_val_err(str(exc))
|
||||||
|
return True
|
||||||
|
|
||||||
def tags_validation(self, answers, current):
|
def tags_validation(self, answers, current):
|
||||||
if current.startswith("@"):
|
if current.startswith("@"):
|
||||||
if current[1:] not in self.app.profiles:
|
if current[1:] not in self.app.profiles:
|
||||||
_raise_val_err("Profile {} don't exist".format(current))
|
_raise_val_err("Profile {} don't exist".format(current))
|
||||||
elif current != "":
|
elif current != "":
|
||||||
isdict = False
|
return self._validate_tags_dict(current)
|
||||||
try:
|
|
||||||
isdict = ast.literal_eval(current)
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
if not isinstance (isdict, dict):
|
|
||||||
_raise_val_err("Tags should be a python dictionary.".format(current))
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def profile_tags_validation(self, answers, current):
|
def profile_tags_validation(self, answers, current):
|
||||||
if current != "":
|
if current != "":
|
||||||
isdict = False
|
return self._validate_tags_dict(current)
|
||||||
try:
|
|
||||||
isdict = ast.literal_eval(current)
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
if not isinstance (isdict, dict):
|
|
||||||
_raise_val_err("Tags should be a python dictionary.".format(current))
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
def fallback_validation(self, answers, current):
|
||||||
|
if current in (None, ""):
|
||||||
|
return True
|
||||||
|
stripped = str(current).strip()
|
||||||
|
if not stripped:
|
||||||
|
return True
|
||||||
|
if len(stripped.encode("utf-8", errors="replace")) > MAX_JSON_INPUT_BYTES:
|
||||||
|
_raise_val_err(f"Fallback exceeds maximum size of {MAX_JSON_INPUT_BYTES} bytes")
|
||||||
|
if stripped.startswith("@") and not stripped.startswith("@@") and "+" not in stripped:
|
||||||
|
parts = [p.strip() for p in stripped.split(",")]
|
||||||
|
if any(not p for p in parts):
|
||||||
|
_raise_val_err("Fallback profile entry cannot be empty")
|
||||||
|
if len(parts) > MAX_FALLBACK_ENTRIES:
|
||||||
|
_raise_val_err(f"Fallback exceeds maximum of {MAX_FALLBACK_ENTRIES} entries")
|
||||||
|
for p in parts:
|
||||||
|
if not p.startswith("@") or not re.fullmatch(r"[A-Za-z0-9_.$:/-]+", p[1:]):
|
||||||
|
_raise_val_err(
|
||||||
|
"Comma-separated fallback entries must be valid @profile references"
|
||||||
|
)
|
||||||
|
if p[1:] not in self.app.profiles:
|
||||||
|
_raise_val_err(f"Profile {p} don't exist")
|
||||||
|
return True
|
||||||
|
try:
|
||||||
|
if is_compose_expression(stripped):
|
||||||
|
validate_compose_syntax(stripped)
|
||||||
|
return True
|
||||||
|
if stripped.startswith(("[", "{")):
|
||||||
|
validate_json_depth_and_size(
|
||||||
|
stripped,
|
||||||
|
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||||
|
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||||
|
)
|
||||||
|
validate_fallback_entries(stripped, max_entries=MAX_FALLBACK_ENTRIES)
|
||||||
|
except (ValueError, TypeError, SanitizedAuthError) as exc:
|
||||||
|
_raise_val_err(str(exc))
|
||||||
|
return True
|
||||||
|
|
||||||
|
def _get_all_valid_nodes(self) -> list[str]:
|
||||||
|
valid_nodes = list(getattr(self.app, "nodes_list", []) or [])
|
||||||
|
if hasattr(self.app, "services") and hasattr(self.app.services, "nodes"):
|
||||||
|
raw_fn = getattr(self.app.services.nodes, "_get_raw_nodes", None)
|
||||||
|
if callable(raw_fn) and not hasattr(raw_fn, "assert_called"):
|
||||||
|
raw_res = raw_fn()
|
||||||
|
if isinstance(raw_res, (list, tuple, set)):
|
||||||
|
valid_nodes = list(dict.fromkeys([*raw_res, *valid_nodes]))
|
||||||
|
return valid_nodes
|
||||||
|
|
||||||
|
def _node_exists(self, target: str) -> bool:
|
||||||
|
valid_nodes = self._get_all_valid_nodes()
|
||||||
|
if getattr(self.app, "case", True):
|
||||||
|
return target in valid_nodes
|
||||||
|
return target.lower() in {n.lower() for n in valid_nodes}
|
||||||
|
|
||||||
def jumphost_validation(self, answers, current):
|
def jumphost_validation(self, answers, current):
|
||||||
if current.startswith("@"):
|
if current.startswith("@"):
|
||||||
if current[1:] not in self.app.profiles:
|
if current[1:] not in self.app.profiles:
|
||||||
_raise_val_err("Profile {} don't exist".format(current))
|
_raise_val_err(f"Profile {current} don't exist")
|
||||||
elif current != "":
|
elif current != "" and not self._node_exists(current):
|
||||||
if current not in self.app.nodes_list:
|
_raise_val_err(f"Node {current} don't exist.")
|
||||||
_raise_val_err("Node {} don't exist.".format(current))
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def profile_jumphost_validation(self, answers, current):
|
def profile_jumphost_validation(self, answers, current):
|
||||||
if current != "":
|
if current != "" and not self._node_exists(current):
|
||||||
if current not in self.app.nodes_list:
|
_raise_val_err(f"Node {current} don't exist.")
|
||||||
_raise_val_err("Node {} don't exist.".format(current))
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def default_validation(self, answers, current):
|
def default_validation(self, answers, current):
|
||||||
@@ -167,13 +236,47 @@ el.replaceWith(d);
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
def bulk_folder_validation(self, answers, current):
|
def bulk_folder_validation(self, answers, current):
|
||||||
if not self.app.case:
|
case = getattr(self.app, "case", False)
|
||||||
|
if not case:
|
||||||
current = current.lower()
|
current = current.lower()
|
||||||
|
|
||||||
candidate = current
|
candidate = current.split("/")[0] if "/" in current else current
|
||||||
if "/" in current:
|
|
||||||
candidate = current.split("/")[0]
|
if hasattr(self.app, "services") and hasattr(self.app.services, "context"):
|
||||||
|
raw_folders = list(getattr(self.app, "folders", []) or [])
|
||||||
|
if hasattr(self.app.services, "nodes"):
|
||||||
|
raw_fn = getattr(self.app.services.nodes, "_get_raw_folders", None)
|
||||||
|
if callable(raw_fn) and not hasattr(raw_fn, "assert_called"):
|
||||||
|
raw_res = raw_fn()
|
||||||
|
if isinstance(raw_res, (list, tuple, set)):
|
||||||
|
raw_folders = list(raw_res)
|
||||||
|
|
||||||
|
def _folder_exists_norm(folder_name: str) -> bool:
|
||||||
|
if case:
|
||||||
|
return folder_name in raw_folders
|
||||||
|
return folder_name.lower() in {f.lower() for f in raw_folders}
|
||||||
|
|
||||||
|
if current != "" and not _folder_exists_norm(candidate):
|
||||||
|
_raise_val_err(f"Location {current} don't exist")
|
||||||
|
|
||||||
|
ctx_svc = self.app.services.context
|
||||||
|
if ctx_svc is not None and not ctx_svc.is_default_all_context():
|
||||||
|
raw_ids = answers.get("ids", "") if isinstance(answers, dict) else ""
|
||||||
|
for nid in str(raw_ids).split(","):
|
||||||
|
nid_stripped = nid.strip()
|
||||||
|
if not nid_stripped:
|
||||||
|
continue
|
||||||
|
nid_norm = nid_stripped if case else nid_stripped.lower()
|
||||||
|
full_id = f"{nid_norm}{candidate}" if candidate else nid_norm
|
||||||
|
if not ctx_svc.matches_active_context(full_id):
|
||||||
|
_raise_val_err(
|
||||||
|
f"Node '{full_id}' does not match active context '{ctx_svc.current_context}'."
|
||||||
|
)
|
||||||
|
parent = full_id.partition("@")[2]
|
||||||
|
if parent and not _folder_exists_norm(f"@{parent}"):
|
||||||
|
_raise_val_err(f"Location @{parent} don't exist")
|
||||||
|
return True
|
||||||
|
|
||||||
matches = list(filter(lambda k: k == candidate, self.app.folders))
|
matches = list(filter(lambda k: k == candidate, self.app.folders))
|
||||||
if current != "" and len(matches) == 0:
|
if current != "" and len(matches) == 0:
|
||||||
_raise_val_err("Location {} don't exist".format(current))
|
_raise_val_err("Location {} don't exist".format(current))
|
||||||
@@ -203,13 +306,47 @@ el.replaceWith(d);
|
|||||||
<span>Expand source code</span>
|
<span>Expand source code</span>
|
||||||
</summary>
|
</summary>
|
||||||
<pre><code class="python">def bulk_folder_validation(self, answers, current):
|
<pre><code class="python">def bulk_folder_validation(self, answers, current):
|
||||||
if not self.app.case:
|
case = getattr(self.app, "case", False)
|
||||||
|
if not case:
|
||||||
current = current.lower()
|
current = current.lower()
|
||||||
|
|
||||||
candidate = current
|
candidate = current.split("/")[0] if "/" in current else current
|
||||||
if "/" in current:
|
|
||||||
candidate = current.split("/")[0]
|
if hasattr(self.app, "services") and hasattr(self.app.services, "context"):
|
||||||
|
raw_folders = list(getattr(self.app, "folders", []) or [])
|
||||||
|
if hasattr(self.app.services, "nodes"):
|
||||||
|
raw_fn = getattr(self.app.services.nodes, "_get_raw_folders", None)
|
||||||
|
if callable(raw_fn) and not hasattr(raw_fn, "assert_called"):
|
||||||
|
raw_res = raw_fn()
|
||||||
|
if isinstance(raw_res, (list, tuple, set)):
|
||||||
|
raw_folders = list(raw_res)
|
||||||
|
|
||||||
|
def _folder_exists_norm(folder_name: str) -> bool:
|
||||||
|
if case:
|
||||||
|
return folder_name in raw_folders
|
||||||
|
return folder_name.lower() in {f.lower() for f in raw_folders}
|
||||||
|
|
||||||
|
if current != "" and not _folder_exists_norm(candidate):
|
||||||
|
_raise_val_err(f"Location {current} don't exist")
|
||||||
|
|
||||||
|
ctx_svc = self.app.services.context
|
||||||
|
if ctx_svc is not None and not ctx_svc.is_default_all_context():
|
||||||
|
raw_ids = answers.get("ids", "") if isinstance(answers, dict) else ""
|
||||||
|
for nid in str(raw_ids).split(","):
|
||||||
|
nid_stripped = nid.strip()
|
||||||
|
if not nid_stripped:
|
||||||
|
continue
|
||||||
|
nid_norm = nid_stripped if case else nid_stripped.lower()
|
||||||
|
full_id = f"{nid_norm}{candidate}" if candidate else nid_norm
|
||||||
|
if not ctx_svc.matches_active_context(full_id):
|
||||||
|
_raise_val_err(
|
||||||
|
f"Node '{full_id}' does not match active context '{ctx_svc.current_context}'."
|
||||||
|
)
|
||||||
|
parent = full_id.partition("@")[2]
|
||||||
|
if parent and not _folder_exists_norm(f"@{parent}"):
|
||||||
|
_raise_val_err(f"Location @{parent} don't exist")
|
||||||
|
return True
|
||||||
|
|
||||||
matches = list(filter(lambda k: k == candidate, self.app.folders))
|
matches = list(filter(lambda k: k == candidate, self.app.folders))
|
||||||
if current != "" and len(matches) == 0:
|
if current != "" and len(matches) == 0:
|
||||||
_raise_val_err("Location {} don't exist".format(current))
|
_raise_val_err("Location {} don't exist".format(current))
|
||||||
@@ -273,6 +410,53 @@ el.replaceWith(d);
|
|||||||
</details>
|
</details>
|
||||||
<div class="desc"></div>
|
<div class="desc"></div>
|
||||||
</dd>
|
</dd>
|
||||||
|
<dt id="connpy.cli.validators.Validators.fallback_validation"><code class="name flex">
|
||||||
|
<span>def <span class="ident">fallback_validation</span></span>(<span>self, answers, current)</span>
|
||||||
|
</code></dt>
|
||||||
|
<dd>
|
||||||
|
<details class="source">
|
||||||
|
<summary>
|
||||||
|
<span>Expand source code</span>
|
||||||
|
</summary>
|
||||||
|
<pre><code class="python">def fallback_validation(self, answers, current):
|
||||||
|
if current in (None, ""):
|
||||||
|
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">
|
<dt id="connpy.cli.validators.Validators.host_validation"><code class="name flex">
|
||||||
<span>def <span class="ident">host_validation</span></span>(<span>self, answers, current, regex='^.+$')</span>
|
<span>def <span class="ident">host_validation</span></span>(<span>self, answers, current, regex='^.+$')</span>
|
||||||
</code></dt>
|
</code></dt>
|
||||||
@@ -302,10 +486,9 @@ el.replaceWith(d);
|
|||||||
<pre><code class="python">def jumphost_validation(self, answers, current):
|
<pre><code class="python">def jumphost_validation(self, answers, current):
|
||||||
if current.startswith("@"):
|
if current.startswith("@"):
|
||||||
if current[1:] not in self.app.profiles:
|
if current[1:] not in self.app.profiles:
|
||||||
_raise_val_err("Profile {} don't exist".format(current))
|
_raise_val_err(f"Profile {current} don't exist")
|
||||||
elif current != "":
|
elif current != "" and not self._node_exists(current):
|
||||||
if current not in self.app.nodes_list:
|
_raise_val_err(f"Node {current} don't exist.")
|
||||||
_raise_val_err("Node {} don't exist.".format(current))
|
|
||||||
return True</code></pre>
|
return True</code></pre>
|
||||||
</details>
|
</details>
|
||||||
<div class="desc"></div>
|
<div class="desc"></div>
|
||||||
@@ -319,6 +502,12 @@ el.replaceWith(d);
|
|||||||
<span>Expand source code</span>
|
<span>Expand source code</span>
|
||||||
</summary>
|
</summary>
|
||||||
<pre><code class="python">def pass_validation(self, answers, current, regex = "(^@.+$)"):
|
<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(",")
|
profiles = current.split(",")
|
||||||
for i in profiles:
|
for i in profiles:
|
||||||
if not re.match(regex, i) or i[1:] not in self.app.profiles:
|
if not re.match(regex, i) or i[1:] not in self.app.profiles:
|
||||||
@@ -360,9 +549,8 @@ el.replaceWith(d);
|
|||||||
<span>Expand source code</span>
|
<span>Expand source code</span>
|
||||||
</summary>
|
</summary>
|
||||||
<pre><code class="python">def profile_jumphost_validation(self, answers, current):
|
<pre><code class="python">def profile_jumphost_validation(self, answers, current):
|
||||||
if current != "":
|
if current != "" and not self._node_exists(current):
|
||||||
if current not in self.app.nodes_list:
|
_raise_val_err(f"Node {current} don't exist.")
|
||||||
_raise_val_err("Node {} don't exist.".format(current))
|
|
||||||
return True</code></pre>
|
return True</code></pre>
|
||||||
</details>
|
</details>
|
||||||
<div class="desc"></div>
|
<div class="desc"></div>
|
||||||
@@ -413,13 +601,7 @@ el.replaceWith(d);
|
|||||||
</summary>
|
</summary>
|
||||||
<pre><code class="python">def profile_tags_validation(self, answers, current):
|
<pre><code class="python">def profile_tags_validation(self, answers, current):
|
||||||
if current != "":
|
if current != "":
|
||||||
isdict = False
|
return self._validate_tags_dict(current)
|
||||||
try:
|
|
||||||
isdict = ast.literal_eval(current)
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
if not isinstance (isdict, dict):
|
|
||||||
_raise_val_err("Tags should be a python dictionary.".format(current))
|
|
||||||
return True</code></pre>
|
return True</code></pre>
|
||||||
</details>
|
</details>
|
||||||
<div class="desc"></div>
|
<div class="desc"></div>
|
||||||
@@ -455,13 +637,7 @@ el.replaceWith(d);
|
|||||||
if current[1:] not in self.app.profiles:
|
if current[1:] not in self.app.profiles:
|
||||||
_raise_val_err("Profile {} don't exist".format(current))
|
_raise_val_err("Profile {} don't exist".format(current))
|
||||||
elif current != "":
|
elif current != "":
|
||||||
isdict = False
|
return self._validate_tags_dict(current)
|
||||||
try:
|
|
||||||
isdict = ast.literal_eval(current)
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
if not isinstance (isdict, dict):
|
|
||||||
_raise_val_err("Tags should be a python dictionary.".format(current))
|
|
||||||
return True</code></pre>
|
return True</code></pre>
|
||||||
</details>
|
</details>
|
||||||
<div class="desc"></div>
|
<div class="desc"></div>
|
||||||
@@ -490,6 +666,7 @@ el.replaceWith(d);
|
|||||||
<li><code><a title="connpy.cli.validators.Validators.bulk_host_validation" href="#connpy.cli.validators.Validators.bulk_host_validation">bulk_host_validation</a></code></li>
|
<li><code><a title="connpy.cli.validators.Validators.bulk_host_validation" href="#connpy.cli.validators.Validators.bulk_host_validation">bulk_host_validation</a></code></li>
|
||||||
<li><code><a title="connpy.cli.validators.Validators.bulk_node_validation" href="#connpy.cli.validators.Validators.bulk_node_validation">bulk_node_validation</a></code></li>
|
<li><code><a title="connpy.cli.validators.Validators.bulk_node_validation" href="#connpy.cli.validators.Validators.bulk_node_validation">bulk_node_validation</a></code></li>
|
||||||
<li><code><a title="connpy.cli.validators.Validators.default_validation" href="#connpy.cli.validators.Validators.default_validation">default_validation</a></code></li>
|
<li><code><a title="connpy.cli.validators.Validators.default_validation" href="#connpy.cli.validators.Validators.default_validation">default_validation</a></code></li>
|
||||||
|
<li><code><a title="connpy.cli.validators.Validators.fallback_validation" href="#connpy.cli.validators.Validators.fallback_validation">fallback_validation</a></code></li>
|
||||||
<li><code><a title="connpy.cli.validators.Validators.host_validation" href="#connpy.cli.validators.Validators.host_validation">host_validation</a></code></li>
|
<li><code><a title="connpy.cli.validators.Validators.host_validation" href="#connpy.cli.validators.Validators.host_validation">host_validation</a></code></li>
|
||||||
<li><code><a title="connpy.cli.validators.Validators.jumphost_validation" href="#connpy.cli.validators.Validators.jumphost_validation">jumphost_validation</a></code></li>
|
<li><code><a title="connpy.cli.validators.Validators.jumphost_validation" href="#connpy.cli.validators.Validators.jumphost_validation">jumphost_validation</a></code></li>
|
||||||
<li><code><a title="connpy.cli.validators.Validators.pass_validation" href="#connpy.cli.validators.Validators.pass_validation">pass_validation</a></code></li>
|
<li><code><a title="connpy.cli.validators.Validators.pass_validation" href="#connpy.cli.validators.Validators.pass_validation">pass_validation</a></code></li>
|
||||||
|
|||||||
@@ -55,6 +55,7 @@ el.replaceWith(d);
|
|||||||
<pre><code class="python">def handle_errors(func):
|
<pre><code class="python">def handle_errors(func):
|
||||||
import inspect
|
import inspect
|
||||||
if inspect.isgeneratorfunction(func):
|
if inspect.isgeneratorfunction(func):
|
||||||
|
@functools.wraps(func)
|
||||||
def wrapper(*args, **kwargs):
|
def wrapper(*args, **kwargs):
|
||||||
try:
|
try:
|
||||||
for item in func(*args, **kwargs):
|
for item in func(*args, **kwargs):
|
||||||
@@ -75,6 +76,7 @@ el.replaceWith(d);
|
|||||||
printer.clear_thread_state()
|
printer.clear_thread_state()
|
||||||
return wrapper
|
return wrapper
|
||||||
else:
|
else:
|
||||||
|
@functools.wraps(func)
|
||||||
def wrapper(*args, **kwargs):
|
def wrapper(*args, **kwargs):
|
||||||
try:
|
try:
|
||||||
return func(*args, **kwargs)
|
return func(*args, **kwargs)
|
||||||
@@ -108,7 +110,7 @@ el.replaceWith(d);
|
|||||||
from connpy.grpc_layer.user_registry import UserRegistry
|
from connpy.grpc_layer.user_registry import UserRegistry
|
||||||
from connpy.services.provider import ServiceProvider
|
from connpy.services.provider import ServiceProvider
|
||||||
|
|
||||||
fallback_provider = ServiceProvider(config, mode="local")
|
fallback_provider = ServiceProvider(config, mode="local", enforce_context=False)
|
||||||
registry = UserRegistry(config.defaultdir)
|
registry = UserRegistry(config.defaultdir)
|
||||||
|
|
||||||
# Check if trusted_gateway provider is configured if SSO Gateway Secret is present in env
|
# Check if trusted_gateway provider is configured if SSO Gateway Secret is present in env
|
||||||
@@ -161,9 +163,7 @@ el.replaceWith(d);
|
|||||||
</summary>
|
</summary>
|
||||||
<pre><code class="python">class AIServicer(connpy_pb2_grpc.AIServiceServicer):
|
<pre><code class="python">class AIServicer(connpy_pb2_grpc.AIServiceServicer):
|
||||||
def __init__(self, provider, registry=None, debug=False):
|
def __init__(self, provider, registry=None, debug=False):
|
||||||
if not hasattr(provider, "mode"):
|
provider = _ensure_server_provider(provider)
|
||||||
from connpy.services.provider import ServiceProvider
|
|
||||||
provider = ServiceProvider(provider, mode="local")
|
|
||||||
self._fallback_provider = provider
|
self._fallback_provider = provider
|
||||||
self._registry = registry
|
self._registry = registry
|
||||||
self.server_debug = debug
|
self.server_debug = debug
|
||||||
@@ -925,9 +925,7 @@ interceptor chooses to service this RPC, or None otherwise.</p></div>
|
|||||||
</summary>
|
</summary>
|
||||||
<pre><code class="python">class ConfigServicer(connpy_pb2_grpc.ConfigServiceServicer):
|
<pre><code class="python">class ConfigServicer(connpy_pb2_grpc.ConfigServiceServicer):
|
||||||
def __init__(self, provider, registry=None):
|
def __init__(self, provider, registry=None):
|
||||||
if not hasattr(provider, "mode"):
|
provider = _ensure_server_provider(provider)
|
||||||
from connpy.services.provider import ServiceProvider
|
|
||||||
provider = ServiceProvider(provider, mode="local")
|
|
||||||
self._fallback_provider = provider
|
self._fallback_provider = provider
|
||||||
self._registry = registry
|
self._registry = registry
|
||||||
|
|
||||||
@@ -1013,9 +1011,7 @@ def service(self):
|
|||||||
</summary>
|
</summary>
|
||||||
<pre><code class="python">class ExecutionServicer(connpy_pb2_grpc.ExecutionServiceServicer):
|
<pre><code class="python">class ExecutionServicer(connpy_pb2_grpc.ExecutionServiceServicer):
|
||||||
def __init__(self, provider, registry=None):
|
def __init__(self, provider, registry=None):
|
||||||
if not hasattr(provider, "mode"):
|
provider = _ensure_server_provider(provider)
|
||||||
from connpy.services.provider import ServiceProvider
|
|
||||||
provider = ServiceProvider(provider, mode="local")
|
|
||||||
self._fallback_provider = provider
|
self._fallback_provider = provider
|
||||||
self._registry = registry
|
self._registry = registry
|
||||||
|
|
||||||
@@ -1184,9 +1180,7 @@ def service(self):
|
|||||||
</summary>
|
</summary>
|
||||||
<pre><code class="python">class ImportExportServicer(connpy_pb2_grpc.ImportExportServiceServicer):
|
<pre><code class="python">class ImportExportServicer(connpy_pb2_grpc.ImportExportServiceServicer):
|
||||||
def __init__(self, provider, registry=None):
|
def __init__(self, provider, registry=None):
|
||||||
if not hasattr(provider, "mode"):
|
provider = _ensure_server_provider(provider)
|
||||||
from connpy.services.provider import ServiceProvider
|
|
||||||
provider = ServiceProvider(provider, mode="local")
|
|
||||||
self._fallback_provider = provider
|
self._fallback_provider = provider
|
||||||
self._registry = registry
|
self._registry = registry
|
||||||
|
|
||||||
@@ -1372,9 +1366,7 @@ interceptor chooses to service this RPC, or None otherwise.</p></div>
|
|||||||
</summary>
|
</summary>
|
||||||
<pre><code class="python">class NodeServicer(connpy_pb2_grpc.NodeServiceServicer):
|
<pre><code class="python">class NodeServicer(connpy_pb2_grpc.NodeServiceServicer):
|
||||||
def __init__(self, provider, registry=None, debug=False):
|
def __init__(self, provider, registry=None, debug=False):
|
||||||
if not hasattr(provider, "mode"):
|
provider = _ensure_server_provider(provider)
|
||||||
from connpy.services.provider import ServiceProvider
|
|
||||||
provider = ServiceProvider(provider, mode="local")
|
|
||||||
self._fallback_provider = provider
|
self._fallback_provider = provider
|
||||||
self._registry = registry
|
self._registry = registry
|
||||||
self.server_debug = debug
|
self.server_debug = debug
|
||||||
@@ -1425,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]")
|
self.server_console.print(f"[debug][DEBUG][/debug] gRPC interact_node request for: [bold cyan]{unique_id}[/bold cyan]")
|
||||||
|
|
||||||
if first_req.connection_params_json:
|
if first_req.connection_params_json:
|
||||||
import json
|
try:
|
||||||
params = json.loads(first_req.connection_params_json)
|
params = validate_json_depth_and_size(
|
||||||
|
first_req.connection_params_json,
|
||||||
|
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||||
|
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||||
|
)
|
||||||
|
if not isinstance(params, dict):
|
||||||
|
raise TypeError("connection_params_json must be a JSON object")
|
||||||
|
if "fallback" in params and params["fallback"] not in (None, "", []):
|
||||||
|
validate_fallback_entries(
|
||||||
|
params["fallback"],
|
||||||
|
max_entries=MAX_FALLBACK_ENTRIES,
|
||||||
|
)
|
||||||
|
if "tags" in params and params["tags"] not in ("", None):
|
||||||
|
validate_auth_budget_tags(params["tags"])
|
||||||
|
except (ValueError, TypeError, SanitizedAuthError) as exc:
|
||||||
|
context.abort(grpc.StatusCode.INVALID_ARGUMENT, str(exc))
|
||||||
|
|
||||||
base_node_id = params.get("base_node")
|
base_node_id = params.get("base_node")
|
||||||
# Valid attributes that a node object accepts
|
# Valid attributes that a node object accepts
|
||||||
valid_attrs = ['host', 'options', 'logs', 'password', 'port', 'protocol', 'user', 'jumphost']
|
valid_attrs = ['host', 'options', 'logs', 'password', 'port', 'protocol', 'user', 'jumphost']
|
||||||
@@ -1444,6 +1452,8 @@ interceptor chooses to service this RPC, or None otherwise.</p></div>
|
|||||||
for attr in valid_attrs:
|
for attr in valid_attrs:
|
||||||
if attr in params:
|
if attr in params:
|
||||||
device[attr] = params[attr]
|
device[attr] = params[attr]
|
||||||
|
if "fallback" in params:
|
||||||
|
device["fallback"] = params["fallback"]
|
||||||
|
|
||||||
if "tags" in params:
|
if "tags" in params:
|
||||||
device_tags = device.get("tags", {})
|
device_tags = device.get("tags", {})
|
||||||
@@ -1457,20 +1467,30 @@ interceptor chooses to service this RPC, or None otherwise.</p></div>
|
|||||||
else:
|
else:
|
||||||
# base_node not found, fall back to dynamic
|
# base_node not found, fall back to dynamic
|
||||||
node_name = params.get("name", fallback_id)
|
node_name = params.get("name", fallback_id)
|
||||||
n = node(node_name, host=params.get("host", ""), config=user_config)
|
dyn_kwargs = {k: params[k] for k in valid_attrs if k in params and k != "host"}
|
||||||
for attr in valid_attrs:
|
if "fallback" in params:
|
||||||
if attr in params:
|
dyn_kwargs["fallback"] = params["fallback"]
|
||||||
setattr(n, attr, params[attr])
|
|
||||||
if "tags" in params:
|
if "tags" in params:
|
||||||
n.tags = params["tags"]
|
dyn_kwargs["tags"] = params["tags"]
|
||||||
|
n = node(
|
||||||
|
node_name,
|
||||||
|
host=params.get("host", ""),
|
||||||
|
config=user_config,
|
||||||
|
**dyn_kwargs,
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
node_name = params.get("name", fallback_id)
|
node_name = params.get("name", fallback_id)
|
||||||
n = node(node_name, host=params.get("host", ""), config=user_config)
|
dyn_kwargs = {k: params[k] for k in valid_attrs if k in params and k != "host"}
|
||||||
for attr in valid_attrs:
|
if "fallback" in params:
|
||||||
if attr in params:
|
dyn_kwargs["fallback"] = params["fallback"]
|
||||||
setattr(n, attr, params[attr])
|
|
||||||
if "tags" in params:
|
if "tags" in params:
|
||||||
n.tags = params["tags"]
|
dyn_kwargs["tags"] = params["tags"]
|
||||||
|
n = node(
|
||||||
|
node_name,
|
||||||
|
host=params.get("host", ""),
|
||||||
|
config=user_config,
|
||||||
|
**dyn_kwargs,
|
||||||
|
)
|
||||||
n._orig_protocol = n.protocol
|
n._orig_protocol = n.protocol
|
||||||
if sftp:
|
if sftp:
|
||||||
n.protocol = "sftp"
|
n.protocol = "sftp"
|
||||||
@@ -1491,9 +1511,11 @@ interceptor chooses to service this RPC, or None otherwise.</p></div>
|
|||||||
debug_chunks = []
|
debug_chunks = []
|
||||||
if debug:
|
if debug:
|
||||||
from io import StringIO
|
from io import StringIO
|
||||||
|
|
||||||
from rich.console import Console as RichConsole
|
from rich.console import Console as RichConsole
|
||||||
from ..printer import connpy_theme
|
|
||||||
from .. import printer as _printer
|
from .. import printer as _printer
|
||||||
|
from ..printer import connpy_theme
|
||||||
|
|
||||||
def remote_logger(msg_type, message):
|
def remote_logger(msg_type, message):
|
||||||
buf = StringIO()
|
buf = StringIO()
|
||||||
@@ -1514,7 +1536,14 @@ interceptor chooses to service this RPC, or None otherwise.</p></div>
|
|||||||
else:
|
else:
|
||||||
remote_logger = None
|
remote_logger = None
|
||||||
|
|
||||||
connect = n._connect(debug=debug, logger=remote_logger)
|
try:
|
||||||
|
connect = n._connect(
|
||||||
|
debug=debug,
|
||||||
|
logger=remote_logger,
|
||||||
|
attempt_tracker=getattr(provider, "auth_tracker", None),
|
||||||
|
)
|
||||||
|
except Exception as exc: # noqa: BLE001
|
||||||
|
connect = str(sanitize_exception(exc, code="AUTH_FAILED"))
|
||||||
|
|
||||||
# Send debug output to client before checking result (always show the command)
|
# Send debug output to client before checking result (always show the command)
|
||||||
for chunk in debug_chunks:
|
for chunk in debug_chunks:
|
||||||
@@ -1845,10 +1874,9 @@ interceptor chooses to service this RPC, or None otherwise.</p></div>
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
asyncio.run(n._async_interact_loop(remote_stream, resize_callback, copilot_handler=remote_copilot_handler))
|
asyncio.run(n._async_interact_loop(remote_stream, resize_callback, copilot_handler=remote_copilot_handler))
|
||||||
except Exception as e:
|
except Exception as e: # noqa: BLE001
|
||||||
import traceback
|
sanitized_err = sanitize_exception(e, code="TRANSPORT_ERROR", context_label="interact stream")
|
||||||
print(f"[ERROR in run_async_loop] {e}")
|
print(f"[ERROR in run_async_loop] {sanitized_err}")
|
||||||
traceback.print_exc()
|
|
||||||
finally:
|
finally:
|
||||||
n._teardown_interact_environment()
|
n._teardown_interact_environment()
|
||||||
response_queue.put(None) # Signal EOF
|
response_queue.put(None) # Signal EOF
|
||||||
@@ -2003,9 +2031,7 @@ def service(self):
|
|||||||
</summary>
|
</summary>
|
||||||
<pre><code class="python">class PluginServicer(connpy_pb2_grpc.PluginServiceServicer, remote_plugin_pb2_grpc.RemotePluginServiceServicer):
|
<pre><code class="python">class PluginServicer(connpy_pb2_grpc.PluginServiceServicer, remote_plugin_pb2_grpc.RemotePluginServiceServicer):
|
||||||
def __init__(self, provider, registry=None):
|
def __init__(self, provider, registry=None):
|
||||||
if not hasattr(provider, "mode"):
|
provider = _ensure_server_provider(provider)
|
||||||
from connpy.services.provider import ServiceProvider
|
|
||||||
provider = ServiceProvider(provider, mode="local")
|
|
||||||
self._fallback_provider = provider
|
self._fallback_provider = provider
|
||||||
self._registry = registry
|
self._registry = registry
|
||||||
|
|
||||||
@@ -2110,9 +2136,7 @@ def service(self):
|
|||||||
</summary>
|
</summary>
|
||||||
<pre><code class="python">class ProfileServicer(connpy_pb2_grpc.ProfileServiceServicer):
|
<pre><code class="python">class ProfileServicer(connpy_pb2_grpc.ProfileServiceServicer):
|
||||||
def __init__(self, provider, registry=None):
|
def __init__(self, provider, registry=None):
|
||||||
if not hasattr(provider, "mode"):
|
provider = _ensure_server_provider(provider)
|
||||||
from connpy.services.provider import ServiceProvider
|
|
||||||
provider = ServiceProvider(provider, mode="local")
|
|
||||||
self._fallback_provider = provider
|
self._fallback_provider = provider
|
||||||
self._registry = registry
|
self._registry = registry
|
||||||
|
|
||||||
@@ -2413,9 +2437,7 @@ def service(self):
|
|||||||
</summary>
|
</summary>
|
||||||
<pre><code class="python">class SystemServicer(connpy_pb2_grpc.SystemServiceServicer):
|
<pre><code class="python">class SystemServicer(connpy_pb2_grpc.SystemServiceServicer):
|
||||||
def __init__(self, provider, registry=None):
|
def __init__(self, provider, registry=None):
|
||||||
if not hasattr(provider, "mode"):
|
provider = _ensure_server_provider(provider)
|
||||||
from connpy.services.provider import ServiceProvider
|
|
||||||
provider = ServiceProvider(provider, mode="local")
|
|
||||||
self._fallback_provider = provider
|
self._fallback_provider = provider
|
||||||
self._registry = registry
|
self._registry = registry
|
||||||
|
|
||||||
|
|||||||
File diff suppressed because it is too large.
Load diff
@@ -113,7 +113,7 @@ el.replaceWith(d);
|
|||||||
try:
|
try:
|
||||||
# Attempt a fresh configuration load
|
# Attempt a fresh configuration load
|
||||||
config = configfile(conf=conf_file, shared_config=self._shared_config)
|
config = configfile(conf=conf_file, shared_config=self._shared_config)
|
||||||
new_provider = ServiceProvider(config, mode="local")
|
new_provider = ServiceProvider(config, mode="local", enforce_context=False)
|
||||||
|
|
||||||
# Successfully loaded, clean up the old provider
|
# Successfully loaded, clean up the old provider
|
||||||
if old_provider:
|
if old_provider:
|
||||||
@@ -221,7 +221,7 @@ el.replaceWith(d);
|
|||||||
try:
|
try:
|
||||||
# Attempt a fresh configuration load
|
# Attempt a fresh configuration load
|
||||||
config = configfile(conf=conf_file, shared_config=self._shared_config)
|
config = configfile(conf=conf_file, shared_config=self._shared_config)
|
||||||
new_provider = ServiceProvider(config, mode="local")
|
new_provider = ServiceProvider(config, mode="local", enforce_context=False)
|
||||||
|
|
||||||
# Successfully loaded, clean up the old provider
|
# Successfully loaded, clean up the old provider
|
||||||
if old_provider:
|
if old_provider:
|
||||||
|
|||||||
+3008
-745
File diff suppressed because it is too large.
Load diff
+149
-90
@@ -162,20 +162,7 @@ el.replaceWith(d);
|
|||||||
|
|
||||||
def _mask_secrets(self, val: Any) -> Any:
|
def _mask_secrets(self, val: Any) -> Any:
|
||||||
"""Recursively mask passwords, passphrases, secrets, and auth tokens with '***'."""
|
"""Recursively mask passwords, passphrases, secrets, and auth tokens with '***'."""
|
||||||
if isinstance(val, dict):
|
return mask_node_secrets(val)
|
||||||
masked = {}
|
|
||||||
for k, v in val.items():
|
|
||||||
k_lower = str(k).lower()
|
|
||||||
if any(sec in k_lower for sec in ["password", "passphrase", "secret", "token"]):
|
|
||||||
masked[k] = "***"
|
|
||||||
elif isinstance(v, (dict, list)):
|
|
||||||
masked[k] = self._mask_secrets(v)
|
|
||||||
else:
|
|
||||||
masked[k] = v
|
|
||||||
return masked
|
|
||||||
if isinstance(val, list):
|
|
||||||
return [self._mask_secrets(item) for item in val]
|
|
||||||
return val
|
|
||||||
|
|
||||||
# -------------------------------------------------------------------------
|
# -------------------------------------------------------------------------
|
||||||
# Level 1: Read-Only Inventory and Context Tools
|
# Level 1: Read-Only Inventory and Context Tools
|
||||||
@@ -234,6 +221,31 @@ el.replaceWith(d);
|
|||||||
# Level 2: Execution, Testing, and Playbooks Tools
|
# Level 2: Execution, Testing, and Playbooks Tools
|
||||||
# -------------------------------------------------------------------------
|
# -------------------------------------------------------------------------
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _parse_json_list_or_lines(raw_val: list[str] | str, *, allow_empty_str: bool = False) -> list[str]:
|
||||||
|
if isinstance(raw_val, str):
|
||||||
|
if len(raw_val.encode("utf-8", errors="replace")) > MAX_JSON_INPUT_BYTES:
|
||||||
|
raise ValueError(f"Input exceeds maximum size of {MAX_JSON_INPUT_BYTES} bytes")
|
||||||
|
stripped = raw_val.strip()
|
||||||
|
if stripped.startswith(("[", "{")):
|
||||||
|
parsed = validate_json_depth_and_size(
|
||||||
|
stripped,
|
||||||
|
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||||
|
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||||
|
)
|
||||||
|
return parsed if isinstance(parsed, list) else [raw_val]
|
||||||
|
return ([raw_val] if raw_val else []) if allow_empty_str else [
|
||||||
|
c.strip() for c in raw_val.splitlines() if c.strip()
|
||||||
|
]
|
||||||
|
if isinstance(raw_val, list):
|
||||||
|
validate_json_depth_and_size(
|
||||||
|
raw_val,
|
||||||
|
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||||
|
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||||
|
)
|
||||||
|
return raw_val
|
||||||
|
return [str(raw_val)] if (raw_val or not allow_empty_str) else []
|
||||||
|
|
||||||
def run_commands(
|
def run_commands(
|
||||||
self,
|
self,
|
||||||
nodes_filter: str,
|
nodes_filter: str,
|
||||||
@@ -248,17 +260,10 @@ el.replaceWith(d);
|
|||||||
if not self.services or not hasattr(self.services, "execution"):
|
if not self.services or not hasattr(self.services, "execution"):
|
||||||
return {"success": False, "error": "Execution service unavailable."}
|
return {"success": False, "error": "Execution service unavailable."}
|
||||||
|
|
||||||
# Normalize commands list
|
try:
|
||||||
if isinstance(commands, str):
|
commands = self._parse_json_list_or_lines(commands, allow_empty_str=False)
|
||||||
try:
|
except ValueError as exc:
|
||||||
import json
|
return {"success": False, "error": str(exc)}
|
||||||
|
|
||||||
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)]
|
|
||||||
|
|
||||||
# Check safety
|
# Check safety
|
||||||
if not self.trust:
|
if not self.trust:
|
||||||
@@ -277,9 +282,24 @@ el.replaceWith(d);
|
|||||||
parallel=par_count,
|
parallel=par_count,
|
||||||
timeout=timeout,
|
timeout=timeout,
|
||||||
)
|
)
|
||||||
return {"success": True, "results": results}
|
fb_used = False
|
||||||
|
if isinstance(results, dict):
|
||||||
|
fb_used = any(
|
||||||
|
isinstance(v, dict) and bool(v.get("fallback_used", False))
|
||||||
|
for v in results.values()
|
||||||
|
)
|
||||||
|
if not fb_used:
|
||||||
|
fb_map = getattr(self.services.execution, "last_fallback_used", {})
|
||||||
|
if isinstance(fb_map, dict):
|
||||||
|
fb_used = any(bool(v) for v in fb_map.values())
|
||||||
|
return {"success": True, "results": results, "fallback_used": fb_used}
|
||||||
except Exception as e: # noqa: BLE001
|
except Exception as e: # noqa: BLE001
|
||||||
return {"success": False, "error": str(e)}
|
san = sanitize_exception(
|
||||||
|
e,
|
||||||
|
code="AUTH_FAILED",
|
||||||
|
context_label="command execution failed",
|
||||||
|
)
|
||||||
|
return {"success": False, "error": str(san)}
|
||||||
|
|
||||||
def test_commands(
|
def test_commands(
|
||||||
self,
|
self,
|
||||||
@@ -293,29 +313,11 @@ el.replaceWith(d);
|
|||||||
if not self.services or not hasattr(self.services, "execution"):
|
if not self.services or not hasattr(self.services, "execution"):
|
||||||
return {"success": False, "error": "Execution service unavailable."}
|
return {"success": False, "error": "Execution service unavailable."}
|
||||||
|
|
||||||
# Normalize commands
|
try:
|
||||||
if isinstance(commands, str):
|
commands = self._parse_json_list_or_lines(commands, allow_empty_str=False)
|
||||||
try:
|
expected = self._parse_json_list_or_lines(expected, allow_empty_str=True)
|
||||||
import json
|
except ValueError as exc:
|
||||||
|
return {"success": False, "error": str(exc)}
|
||||||
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 []
|
|
||||||
|
|
||||||
# Check safety
|
# Check safety
|
||||||
if not self.trust:
|
if not self.trust:
|
||||||
@@ -335,9 +337,16 @@ el.replaceWith(d);
|
|||||||
parallel=par_count,
|
parallel=par_count,
|
||||||
timeout=timeout,
|
timeout=timeout,
|
||||||
)
|
)
|
||||||
return {"success": True, "results": results}
|
fb_map = getattr(self.services.execution, "last_fallback_used", {})
|
||||||
|
fb_used = any(bool(v) for v in fb_map.values()) if isinstance(fb_map, dict) else False
|
||||||
|
return {"success": True, "results": results, "fallback_used": fb_used}
|
||||||
except Exception as e: # noqa: BLE001
|
except Exception as e: # noqa: BLE001
|
||||||
return {"success": False, "error": str(e)}
|
san = sanitize_exception(
|
||||||
|
e,
|
||||||
|
code="AUTH_FAILED",
|
||||||
|
context_label="test execution failed",
|
||||||
|
)
|
||||||
|
return {"success": False, "error": str(san)}
|
||||||
|
|
||||||
def validate_playbook(self, playbook_yaml: str) -> dict:
|
def validate_playbook(self, playbook_yaml: str) -> dict:
|
||||||
"""Validate the syntax, schema, and tasks of a Connpy YAML playbook."""
|
"""Validate the syntax, schema, and tasks of a Connpy YAML playbook."""
|
||||||
@@ -465,6 +474,47 @@ el.replaceWith(d);
|
|||||||
# Level 3: Inventory Mutation Tools (CRUD - Requires trust=True)
|
# Level 3: Inventory Mutation Tools (CRUD - Requires trust=True)
|
||||||
# -------------------------------------------------------------------------
|
# -------------------------------------------------------------------------
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _validate_and_normalize_node_payload(node_data: dict[str, Any]) -> dict[str, Any]:
|
||||||
|
validate_json_depth_and_size(
|
||||||
|
node_data,
|
||||||
|
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||||
|
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||||
|
)
|
||||||
|
normalized = node_data.copy()
|
||||||
|
if "tags" in normalized and normalized["tags"] not in ("", None):
|
||||||
|
tags_val = normalized["tags"]
|
||||||
|
if isinstance(tags_val, str) and tags_val.strip().startswith("{"):
|
||||||
|
tags_val = validate_json_depth_and_size(
|
||||||
|
tags_val,
|
||||||
|
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||||
|
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||||
|
)
|
||||||
|
normalized["tags"] = tags_val
|
||||||
|
validate_auth_budget_tags(tags_val)
|
||||||
|
|
||||||
|
if "fallback" in normalized and normalized["fallback"] not in (None, "", []):
|
||||||
|
fb_val = normalized["fallback"]
|
||||||
|
if isinstance(fb_val, str) and fb_val.strip().startswith(("[", "{")):
|
||||||
|
fb_val = validate_json_depth_and_size(
|
||||||
|
fb_val,
|
||||||
|
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||||
|
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||||
|
)
|
||||||
|
normalized["fallback"] = validate_fallback_entries(
|
||||||
|
fb_val,
|
||||||
|
max_entries=MAX_FALLBACK_ENTRIES,
|
||||||
|
)
|
||||||
|
|
||||||
|
pw = normalized.get("password")
|
||||||
|
if is_compose_expression(pw):
|
||||||
|
validate_compose_syntax(pw)
|
||||||
|
elif isinstance(pw, (list, tuple)):
|
||||||
|
for item in pw:
|
||||||
|
if is_compose_expression(item):
|
||||||
|
validate_compose_syntax(item)
|
||||||
|
return normalized
|
||||||
|
|
||||||
def add_node(
|
def add_node(
|
||||||
self, unique_id: str, data: dict | None = None, is_folder: bool = False, **kwargs
|
self, unique_id: str, data: dict | None = None, is_folder: bool = False, **kwargs
|
||||||
) -> dict:
|
) -> dict:
|
||||||
@@ -482,11 +532,14 @@ el.replaceWith(d);
|
|||||||
node_data.update(kwargs)
|
node_data.update(kwargs)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
node_data = self._validate_and_normalize_node_payload(node_data)
|
||||||
self.services.nodes.add_node(unique_id, node_data, is_folder=is_folder)
|
self.services.nodes.add_node(unique_id, node_data, is_folder=is_folder)
|
||||||
return {
|
return {
|
||||||
"success": True,
|
"success": True,
|
||||||
"message": f"{'Folder' if is_folder else 'Node'} '{unique_id}' created successfully.",
|
"message": f"{'Folder' if is_folder else 'Node'} '{unique_id}' created successfully.",
|
||||||
}
|
}
|
||||||
|
except (ValueError, TypeError, SanitizedAuthError) as exc:
|
||||||
|
return {"success": False, "error": str(exc)}
|
||||||
except Exception as e: # noqa: BLE001
|
except Exception as e: # noqa: BLE001
|
||||||
return {"success": False, "error": str(e)}
|
return {"success": False, "error": str(e)}
|
||||||
|
|
||||||
@@ -505,8 +558,11 @@ el.replaceWith(d);
|
|||||||
node_updates.update(kwargs)
|
node_updates.update(kwargs)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
node_updates = self._validate_and_normalize_node_payload(node_updates)
|
||||||
self.services.nodes.update_node(unique_id, node_updates)
|
self.services.nodes.update_node(unique_id, node_updates)
|
||||||
return {"success": True, "message": f"Node '{unique_id}' updated successfully."}
|
return {"success": True, "message": f"Node '{unique_id}' updated successfully."}
|
||||||
|
except (ValueError, TypeError, SanitizedAuthError) as exc:
|
||||||
|
return {"success": False, "error": str(exc)}
|
||||||
except Exception as e: # noqa: BLE001
|
except Exception as e: # noqa: BLE001
|
||||||
return {"success": False, "error": str(e)}
|
return {"success": False, "error": str(e)}
|
||||||
|
|
||||||
@@ -608,11 +664,14 @@ el.replaceWith(d);
|
|||||||
node_data.update(kwargs)
|
node_data.update(kwargs)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
node_data = self._validate_and_normalize_node_payload(node_data)
|
||||||
self.services.nodes.add_node(unique_id, node_data, is_folder=is_folder)
|
self.services.nodes.add_node(unique_id, node_data, is_folder=is_folder)
|
||||||
return {
|
return {
|
||||||
"success": True,
|
"success": True,
|
||||||
"message": f"{'Folder' if is_folder else 'Node'} '{unique_id}' created successfully.",
|
"message": f"{'Folder' if is_folder else 'Node'} '{unique_id}' created successfully.",
|
||||||
}
|
}
|
||||||
|
except (ValueError, TypeError, SanitizedAuthError) as exc:
|
||||||
|
return {"success": False, "error": str(exc)}
|
||||||
except Exception as e: # noqa: BLE001
|
except Exception as e: # noqa: BLE001
|
||||||
return {"success": False, "error": str(e)}</code></pre>
|
return {"success": False, "error": str(e)}</code></pre>
|
||||||
</details>
|
</details>
|
||||||
@@ -865,17 +924,10 @@ el.replaceWith(d);
|
|||||||
if not self.services or not hasattr(self.services, "execution"):
|
if not self.services or not hasattr(self.services, "execution"):
|
||||||
return {"success": False, "error": "Execution service unavailable."}
|
return {"success": False, "error": "Execution service unavailable."}
|
||||||
|
|
||||||
# Normalize commands list
|
try:
|
||||||
if isinstance(commands, str):
|
commands = self._parse_json_list_or_lines(commands, allow_empty_str=False)
|
||||||
try:
|
except ValueError as exc:
|
||||||
import json
|
return {"success": False, "error": str(exc)}
|
||||||
|
|
||||||
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)]
|
|
||||||
|
|
||||||
# Check safety
|
# Check safety
|
||||||
if not self.trust:
|
if not self.trust:
|
||||||
@@ -894,9 +946,24 @@ el.replaceWith(d);
|
|||||||
parallel=par_count,
|
parallel=par_count,
|
||||||
timeout=timeout,
|
timeout=timeout,
|
||||||
)
|
)
|
||||||
return {"success": True, "results": results}
|
fb_used = False
|
||||||
|
if isinstance(results, dict):
|
||||||
|
fb_used = any(
|
||||||
|
isinstance(v, dict) and bool(v.get("fallback_used", False))
|
||||||
|
for v in results.values()
|
||||||
|
)
|
||||||
|
if not fb_used:
|
||||||
|
fb_map = getattr(self.services.execution, "last_fallback_used", {})
|
||||||
|
if isinstance(fb_map, dict):
|
||||||
|
fb_used = any(bool(v) for v in fb_map.values())
|
||||||
|
return {"success": True, "results": results, "fallback_used": fb_used}
|
||||||
except Exception as e: # noqa: BLE001
|
except Exception as e: # noqa: BLE001
|
||||||
return {"success": False, "error": str(e)}</code></pre>
|
san = sanitize_exception(
|
||||||
|
e,
|
||||||
|
code="AUTH_FAILED",
|
||||||
|
context_label="command execution failed",
|
||||||
|
)
|
||||||
|
return {"success": False, "error": str(san)}</code></pre>
|
||||||
</details>
|
</details>
|
||||||
<div class="desc"><p>Execute commands on nodes matching the filter pattern.</p>
|
<div class="desc"><p>Execute commands on nodes matching the filter pattern.</p>
|
||||||
<p>Unsafe commands are blocked unless trust mode is enabled.</p></div>
|
<p>Unsafe commands are blocked unless trust mode is enabled.</p></div>
|
||||||
@@ -986,29 +1053,11 @@ el.replaceWith(d);
|
|||||||
if not self.services or not hasattr(self.services, "execution"):
|
if not self.services or not hasattr(self.services, "execution"):
|
||||||
return {"success": False, "error": "Execution service unavailable."}
|
return {"success": False, "error": "Execution service unavailable."}
|
||||||
|
|
||||||
# Normalize commands
|
try:
|
||||||
if isinstance(commands, str):
|
commands = self._parse_json_list_or_lines(commands, allow_empty_str=False)
|
||||||
try:
|
expected = self._parse_json_list_or_lines(expected, allow_empty_str=True)
|
||||||
import json
|
except ValueError as exc:
|
||||||
|
return {"success": False, "error": str(exc)}
|
||||||
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 []
|
|
||||||
|
|
||||||
# Check safety
|
# Check safety
|
||||||
if not self.trust:
|
if not self.trust:
|
||||||
@@ -1028,9 +1077,16 @@ el.replaceWith(d);
|
|||||||
parallel=par_count,
|
parallel=par_count,
|
||||||
timeout=timeout,
|
timeout=timeout,
|
||||||
)
|
)
|
||||||
return {"success": True, "results": results}
|
fb_map = getattr(self.services.execution, "last_fallback_used", {})
|
||||||
|
fb_used = any(bool(v) for v in fb_map.values()) if isinstance(fb_map, dict) else False
|
||||||
|
return {"success": True, "results": results, "fallback_used": fb_used}
|
||||||
except Exception as e: # noqa: BLE001
|
except Exception as e: # noqa: BLE001
|
||||||
return {"success": False, "error": str(e)}</code></pre>
|
san = sanitize_exception(
|
||||||
|
e,
|
||||||
|
code="AUTH_FAILED",
|
||||||
|
context_label="test execution failed",
|
||||||
|
)
|
||||||
|
return {"success": False, "error": str(san)}</code></pre>
|
||||||
</details>
|
</details>
|
||||||
<div class="desc"><p>Execute commands and assert expected patterns on nodes matching the filter.</p></div>
|
<div class="desc"><p>Execute commands and assert expected patterns on nodes matching the filter.</p></div>
|
||||||
</dd>
|
</dd>
|
||||||
@@ -1057,8 +1113,11 @@ el.replaceWith(d);
|
|||||||
node_updates.update(kwargs)
|
node_updates.update(kwargs)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
node_updates = self._validate_and_normalize_node_payload(node_updates)
|
||||||
self.services.nodes.update_node(unique_id, node_updates)
|
self.services.nodes.update_node(unique_id, node_updates)
|
||||||
return {"success": True, "message": f"Node '{unique_id}' updated successfully."}
|
return {"success": True, "message": f"Node '{unique_id}' updated successfully."}
|
||||||
|
except (ValueError, TypeError, SanitizedAuthError) as exc:
|
||||||
|
return {"success": False, "error": str(exc)}
|
||||||
except Exception as e: # noqa: BLE001
|
except Exception as e: # noqa: BLE001
|
||||||
return {"success": False, "error": str(e)}</code></pre>
|
return {"success": False, "error": str(e)}</code></pre>
|
||||||
</details>
|
</details>
|
||||||
|
|||||||
@@ -58,15 +58,31 @@ el.replaceWith(d);
|
|||||||
<pre><code class="python">class ContextService(BaseService):
|
<pre><code class="python">class ContextService(BaseService):
|
||||||
"""Business logic for managing and applying regex-based contexts locally."""
|
"""Business logic for managing and applying regex-based contexts locally."""
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _validate_regexes(regexes: Any) -> None:
|
||||||
|
if not isinstance(regexes, list) or not (1 <= len(regexes) <= MAX_CONTEXT_REGEXES):
|
||||||
|
raise ValueError(
|
||||||
|
f"Context regexes must be a list of 1 to {MAX_CONTEXT_REGEXES} patterns"
|
||||||
|
)
|
||||||
|
for pat in regexes:
|
||||||
|
if not isinstance(pat, str) or not (1 <= len(pat) <= MAX_CONTEXT_PATTERN_LEN):
|
||||||
|
raise ValueError(
|
||||||
|
f"Each context regex must be a non-empty string up to {MAX_CONTEXT_PATTERN_LEN} characters"
|
||||||
|
)
|
||||||
|
try:
|
||||||
|
re.compile(pat)
|
||||||
|
except re.error as exc:
|
||||||
|
raise ValueError(f"Invalid context regex {pat!r}: {exc}") from None
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def contexts(self) -> Dict[str, List[str]]:
|
def contexts(self) -> dict[str, list[str]]:
|
||||||
return self.config.config.get("contexts", {"all": [".*"]})
|
return self.config.config.get("contexts", {"all": [".*"]})
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def current_context(self) -> str:
|
def current_context(self) -> str:
|
||||||
return self.config.config.get("current_context", "all")
|
return self.config.config.get("current_context", "all")
|
||||||
|
|
||||||
def list_contexts(self) -> List[Dict[str, Any]]:
|
def list_contexts(self) -> list[dict[str, Any]]:
|
||||||
result = []
|
result = []
|
||||||
for name in self.contexts.keys():
|
for name in self.contexts.keys():
|
||||||
result.append({
|
result.append({
|
||||||
@@ -76,27 +92,29 @@ el.replaceWith(d);
|
|||||||
})
|
})
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def add_context(self, name: str, regexes: List[str]):
|
def add_context(self, name: str, regexes: list[str]):
|
||||||
if not name.isalnum():
|
if not name.isalnum():
|
||||||
raise ValueError("Context name must be alphanumeric")
|
raise ValueError("Context name must be alphanumeric")
|
||||||
|
self._validate_regexes(regexes)
|
||||||
|
|
||||||
ctxs = self.contexts
|
ctxs = self.contexts
|
||||||
if name in ctxs:
|
if name in ctxs:
|
||||||
raise ValueError(f"Context '{name}' already exists")
|
raise ValueError(f"Context '{name}' already exists")
|
||||||
|
|
||||||
ctxs[name] = regexes
|
ctxs[name] = list(regexes)
|
||||||
self.config.config["contexts"] = ctxs
|
self.config.config["contexts"] = ctxs
|
||||||
self.config._saveconfig(self.config.file)
|
self.config._saveconfig(self.config.file)
|
||||||
|
|
||||||
def update_context(self, name: str, regexes: List[str]):
|
def update_context(self, name: str, regexes: list[str]):
|
||||||
if name == "all":
|
if name == "all":
|
||||||
raise ValueError("Cannot modify default context 'all'")
|
raise ValueError("Cannot modify default context 'all'")
|
||||||
|
self._validate_regexes(regexes)
|
||||||
|
|
||||||
ctxs = self.contexts
|
ctxs = self.contexts
|
||||||
if name not in ctxs:
|
if name not in ctxs:
|
||||||
raise ValueError(f"Context '{name}' does not exist")
|
raise ValueError(f"Context '{name}' does not exist")
|
||||||
|
|
||||||
ctxs[name] = regexes
|
ctxs[name] = list(regexes)
|
||||||
self.config.config["contexts"] = ctxs
|
self.config.config["contexts"] = ctxs
|
||||||
self.config._saveconfig(self.config.file)
|
self.config._saveconfig(self.config.file)
|
||||||
|
|
||||||
@@ -117,16 +135,48 @@ el.replaceWith(d);
|
|||||||
def set_active_context(self, name: str):
|
def set_active_context(self, name: str):
|
||||||
if name not in self.contexts:
|
if name not in self.contexts:
|
||||||
raise ValueError(f"Context '{name}' does not exist")
|
raise ValueError(f"Context '{name}' does not exist")
|
||||||
|
|
||||||
|
prev_context = self.current_context
|
||||||
self.config.config["current_context"] = name
|
self.config.config["current_context"] = name
|
||||||
self.config._saveconfig(self.config.file)
|
self.config._saveconfig(self.config.file)
|
||||||
|
can_refresh = (
|
||||||
|
self.config._is_context_filtering_enabled_unlocked()
|
||||||
|
if (
|
||||||
|
not hasattr(self.config, "assert_called")
|
||||||
|
and callable(getattr(self.config, "_is_context_filtering_enabled_unlocked", None))
|
||||||
|
)
|
||||||
|
else getattr(self.config, "_enforce_context", True)
|
||||||
|
)
|
||||||
|
if (
|
||||||
|
name == "all"
|
||||||
|
and prev_context != "all"
|
||||||
|
and can_refresh
|
||||||
|
and hasattr(self.config, "_generate_nodes_cache")
|
||||||
|
):
|
||||||
|
self.config._generate_nodes_cache()
|
||||||
|
|
||||||
def get_active_regexes(self) -> List[re.Pattern]:
|
def get_active_regexes(self) -> list[re.Pattern]:
|
||||||
patterns = self.contexts.get(self.current_context, [".*"])
|
return compile_active_context_patterns(getattr(self.config, "config", None))
|
||||||
return [re.compile(p) for p in patterns]
|
|
||||||
|
|
||||||
def _match_any(self, node_name: str, patterns: List[re.Pattern]) -> bool:
|
def _match_any(self, node_name: str, patterns: list[re.Pattern]) -> bool:
|
||||||
return any(p.match(node_name) for p in patterns)
|
return match_active_context_patterns(node_name, patterns)
|
||||||
|
|
||||||
|
def is_default_all_context(self) -> bool:
|
||||||
|
return is_default_all_context_cfg(getattr(self.config, "config", None))
|
||||||
|
|
||||||
|
def matches_active_context(self, name: str) -> bool:
|
||||||
|
if self.is_default_all_context():
|
||||||
|
return isinstance(name, str) and bool(name)
|
||||||
|
if not isinstance(name, str) or not name:
|
||||||
|
return False
|
||||||
|
return match_active_context_patterns(name, self.get_active_regexes())
|
||||||
|
|
||||||
|
def validate_in_active_context(self, unique_id: str, is_folder: bool = False) -> None:
|
||||||
|
if not self.is_default_all_context() and not self.matches_active_context(unique_id):
|
||||||
|
label = "Folder" if is_folder else "Node"
|
||||||
|
raise InvalidConfigurationError(
|
||||||
|
f"{label} '{unique_id}' does not match active context '{self.current_context}'."
|
||||||
|
)
|
||||||
|
|
||||||
# Hook handlers for filtering
|
# Hook handlers for filtering
|
||||||
def filter_node_list(self, *args, **kwargs):
|
def filter_node_list(self, *args, **kwargs):
|
||||||
@@ -151,14 +201,14 @@ el.replaceWith(d);
|
|||||||
</ul>
|
</ul>
|
||||||
<h3>Instance variables</h3>
|
<h3>Instance variables</h3>
|
||||||
<dl>
|
<dl>
|
||||||
<dt id="connpy.services.context_service.ContextService.contexts"><code class="name">prop <span class="ident">contexts</span> : Dict[str, List[str]]</code></dt>
|
<dt id="connpy.services.context_service.ContextService.contexts"><code class="name">prop <span class="ident">contexts</span> : dict[str, list[str]]</code></dt>
|
||||||
<dd>
|
<dd>
|
||||||
<details class="source">
|
<details class="source">
|
||||||
<summary>
|
<summary>
|
||||||
<span>Expand source code</span>
|
<span>Expand source code</span>
|
||||||
</summary>
|
</summary>
|
||||||
<pre><code class="python">@property
|
<pre><code class="python">@property
|
||||||
def contexts(self) -> Dict[str, List[str]]:
|
def contexts(self) -> dict[str, list[str]]:
|
||||||
return self.config.config.get("contexts", {"all": [".*"]})</code></pre>
|
return self.config.config.get("contexts", {"all": [".*"]})</code></pre>
|
||||||
</details>
|
</details>
|
||||||
<div class="desc"></div>
|
<div class="desc"></div>
|
||||||
@@ -179,22 +229,23 @@ def current_context(self) -> str:
|
|||||||
<h3>Methods</h3>
|
<h3>Methods</h3>
|
||||||
<dl>
|
<dl>
|
||||||
<dt id="connpy.services.context_service.ContextService.add_context"><code class="name flex">
|
<dt id="connpy.services.context_service.ContextService.add_context"><code class="name flex">
|
||||||
<span>def <span class="ident">add_context</span></span>(<span>self, name: str, regexes: List[str])</span>
|
<span>def <span class="ident">add_context</span></span>(<span>self, name: str, regexes: list[str])</span>
|
||||||
</code></dt>
|
</code></dt>
|
||||||
<dd>
|
<dd>
|
||||||
<details class="source">
|
<details class="source">
|
||||||
<summary>
|
<summary>
|
||||||
<span>Expand source code</span>
|
<span>Expand source code</span>
|
||||||
</summary>
|
</summary>
|
||||||
<pre><code class="python">def add_context(self, name: str, regexes: List[str]):
|
<pre><code class="python">def add_context(self, name: str, regexes: list[str]):
|
||||||
if not name.isalnum():
|
if not name.isalnum():
|
||||||
raise ValueError("Context name must be alphanumeric")
|
raise ValueError("Context name must be alphanumeric")
|
||||||
|
self._validate_regexes(regexes)
|
||||||
|
|
||||||
ctxs = self.contexts
|
ctxs = self.contexts
|
||||||
if name in ctxs:
|
if name in ctxs:
|
||||||
raise ValueError(f"Context '{name}' already exists")
|
raise ValueError(f"Context '{name}' already exists")
|
||||||
|
|
||||||
ctxs[name] = regexes
|
ctxs[name] = list(regexes)
|
||||||
self.config.config["contexts"] = ctxs
|
self.config.config["contexts"] = ctxs
|
||||||
self.config._saveconfig(self.config.file)</code></pre>
|
self.config._saveconfig(self.config.file)</code></pre>
|
||||||
</details>
|
</details>
|
||||||
@@ -253,28 +304,40 @@ def current_context(self) -> str:
|
|||||||
<div class="desc"></div>
|
<div class="desc"></div>
|
||||||
</dd>
|
</dd>
|
||||||
<dt id="connpy.services.context_service.ContextService.get_active_regexes"><code class="name flex">
|
<dt id="connpy.services.context_service.ContextService.get_active_regexes"><code class="name flex">
|
||||||
<span>def <span class="ident">get_active_regexes</span></span>(<span>self) ‑> List[re.Pattern]</span>
|
<span>def <span class="ident">get_active_regexes</span></span>(<span>self) ‑> list[re.Pattern]</span>
|
||||||
</code></dt>
|
</code></dt>
|
||||||
<dd>
|
<dd>
|
||||||
<details class="source">
|
<details class="source">
|
||||||
<summary>
|
<summary>
|
||||||
<span>Expand source code</span>
|
<span>Expand source code</span>
|
||||||
</summary>
|
</summary>
|
||||||
<pre><code class="python">def get_active_regexes(self) -> List[re.Pattern]:
|
<pre><code class="python">def get_active_regexes(self) -> list[re.Pattern]:
|
||||||
patterns = self.contexts.get(self.current_context, [".*"])
|
return compile_active_context_patterns(getattr(self.config, "config", None))</code></pre>
|
||||||
return [re.compile(p) for p in patterns]</code></pre>
|
</details>
|
||||||
|
<div class="desc"></div>
|
||||||
|
</dd>
|
||||||
|
<dt id="connpy.services.context_service.ContextService.is_default_all_context"><code class="name flex">
|
||||||
|
<span>def <span class="ident">is_default_all_context</span></span>(<span>self) ‑> bool</span>
|
||||||
|
</code></dt>
|
||||||
|
<dd>
|
||||||
|
<details class="source">
|
||||||
|
<summary>
|
||||||
|
<span>Expand source code</span>
|
||||||
|
</summary>
|
||||||
|
<pre><code class="python">def is_default_all_context(self) -> bool:
|
||||||
|
return is_default_all_context_cfg(getattr(self.config, "config", None))</code></pre>
|
||||||
</details>
|
</details>
|
||||||
<div class="desc"></div>
|
<div class="desc"></div>
|
||||||
</dd>
|
</dd>
|
||||||
<dt id="connpy.services.context_service.ContextService.list_contexts"><code class="name flex">
|
<dt id="connpy.services.context_service.ContextService.list_contexts"><code class="name flex">
|
||||||
<span>def <span class="ident">list_contexts</span></span>(<span>self) ‑> List[Dict[str, Any]]</span>
|
<span>def <span class="ident">list_contexts</span></span>(<span>self) ‑> list[dict[str, typing.Any]]</span>
|
||||||
</code></dt>
|
</code></dt>
|
||||||
<dd>
|
<dd>
|
||||||
<details class="source">
|
<details class="source">
|
||||||
<summary>
|
<summary>
|
||||||
<span>Expand source code</span>
|
<span>Expand source code</span>
|
||||||
</summary>
|
</summary>
|
||||||
<pre><code class="python">def list_contexts(self) -> List[Dict[str, Any]]:
|
<pre><code class="python">def list_contexts(self) -> list[dict[str, Any]]:
|
||||||
result = []
|
result = []
|
||||||
for name in self.contexts.keys():
|
for name in self.contexts.keys():
|
||||||
result.append({
|
result.append({
|
||||||
@@ -286,6 +349,23 @@ def current_context(self) -> str:
|
|||||||
</details>
|
</details>
|
||||||
<div class="desc"></div>
|
<div class="desc"></div>
|
||||||
</dd>
|
</dd>
|
||||||
|
<dt id="connpy.services.context_service.ContextService.matches_active_context"><code class="name flex">
|
||||||
|
<span>def <span class="ident">matches_active_context</span></span>(<span>self, name: str) ‑> bool</span>
|
||||||
|
</code></dt>
|
||||||
|
<dd>
|
||||||
|
<details class="source">
|
||||||
|
<summary>
|
||||||
|
<span>Expand source code</span>
|
||||||
|
</summary>
|
||||||
|
<pre><code class="python">def matches_active_context(self, name: str) -> bool:
|
||||||
|
if self.is_default_all_context():
|
||||||
|
return isinstance(name, str) and bool(name)
|
||||||
|
if not isinstance(name, str) or not name:
|
||||||
|
return False
|
||||||
|
return match_active_context_patterns(name, self.get_active_regexes())</code></pre>
|
||||||
|
</details>
|
||||||
|
<div class="desc"></div>
|
||||||
|
</dd>
|
||||||
<dt id="connpy.services.context_service.ContextService.set_active_context"><code class="name flex">
|
<dt id="connpy.services.context_service.ContextService.set_active_context"><code class="name flex">
|
||||||
<span>def <span class="ident">set_active_context</span></span>(<span>self, name: str)</span>
|
<span>def <span class="ident">set_active_context</span></span>(<span>self, name: str)</span>
|
||||||
</code></dt>
|
</code></dt>
|
||||||
@@ -297,34 +377,68 @@ def current_context(self) -> str:
|
|||||||
<pre><code class="python">def set_active_context(self, name: str):
|
<pre><code class="python">def set_active_context(self, name: str):
|
||||||
if name not in self.contexts:
|
if name not in self.contexts:
|
||||||
raise ValueError(f"Context '{name}' does not exist")
|
raise ValueError(f"Context '{name}' does not exist")
|
||||||
|
|
||||||
|
prev_context = self.current_context
|
||||||
self.config.config["current_context"] = name
|
self.config.config["current_context"] = name
|
||||||
self.config._saveconfig(self.config.file)</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>
|
</details>
|
||||||
<div class="desc"></div>
|
<div class="desc"></div>
|
||||||
</dd>
|
</dd>
|
||||||
<dt id="connpy.services.context_service.ContextService.update_context"><code class="name flex">
|
<dt id="connpy.services.context_service.ContextService.update_context"><code class="name flex">
|
||||||
<span>def <span class="ident">update_context</span></span>(<span>self, name: str, regexes: List[str])</span>
|
<span>def <span class="ident">update_context</span></span>(<span>self, name: str, regexes: list[str])</span>
|
||||||
</code></dt>
|
</code></dt>
|
||||||
<dd>
|
<dd>
|
||||||
<details class="source">
|
<details class="source">
|
||||||
<summary>
|
<summary>
|
||||||
<span>Expand source code</span>
|
<span>Expand source code</span>
|
||||||
</summary>
|
</summary>
|
||||||
<pre><code class="python">def update_context(self, name: str, regexes: List[str]):
|
<pre><code class="python">def update_context(self, name: str, regexes: list[str]):
|
||||||
if name == "all":
|
if name == "all":
|
||||||
raise ValueError("Cannot modify default context 'all'")
|
raise ValueError("Cannot modify default context 'all'")
|
||||||
|
self._validate_regexes(regexes)
|
||||||
|
|
||||||
ctxs = self.contexts
|
ctxs = self.contexts
|
||||||
if name not in ctxs:
|
if name not in ctxs:
|
||||||
raise ValueError(f"Context '{name}' does not exist")
|
raise ValueError(f"Context '{name}' does not exist")
|
||||||
|
|
||||||
ctxs[name] = regexes
|
ctxs[name] = list(regexes)
|
||||||
self.config.config["contexts"] = ctxs
|
self.config.config["contexts"] = ctxs
|
||||||
self.config._saveconfig(self.config.file)</code></pre>
|
self.config._saveconfig(self.config.file)</code></pre>
|
||||||
</details>
|
</details>
|
||||||
<div class="desc"></div>
|
<div class="desc"></div>
|
||||||
</dd>
|
</dd>
|
||||||
|
<dt id="connpy.services.context_service.ContextService.validate_in_active_context"><code class="name flex">
|
||||||
|
<span>def <span class="ident">validate_in_active_context</span></span>(<span>self, unique_id: str, is_folder: bool = False) ‑> None</span>
|
||||||
|
</code></dt>
|
||||||
|
<dd>
|
||||||
|
<details class="source">
|
||||||
|
<summary>
|
||||||
|
<span>Expand source code</span>
|
||||||
|
</summary>
|
||||||
|
<pre><code class="python">def validate_in_active_context(self, unique_id: str, is_folder: bool = False) -> 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>
|
</dl>
|
||||||
<h3>Inherited members</h3>
|
<h3>Inherited members</h3>
|
||||||
<ul class="hlist">
|
<ul class="hlist">
|
||||||
@@ -352,7 +466,7 @@ def current_context(self) -> str:
|
|||||||
<ul>
|
<ul>
|
||||||
<li>
|
<li>
|
||||||
<h4><code><a title="connpy.services.context_service.ContextService" href="#connpy.services.context_service.ContextService">ContextService</a></code></h4>
|
<h4><code><a title="connpy.services.context_service.ContextService" href="#connpy.services.context_service.ContextService">ContextService</a></code></h4>
|
||||||
<ul class="two-column">
|
<ul class="">
|
||||||
<li><code><a title="connpy.services.context_service.ContextService.add_context" href="#connpy.services.context_service.ContextService.add_context">add_context</a></code></li>
|
<li><code><a title="connpy.services.context_service.ContextService.add_context" href="#connpy.services.context_service.ContextService.add_context">add_context</a></code></li>
|
||||||
<li><code><a title="connpy.services.context_service.ContextService.contexts" href="#connpy.services.context_service.ContextService.contexts">contexts</a></code></li>
|
<li><code><a title="connpy.services.context_service.ContextService.contexts" href="#connpy.services.context_service.ContextService.contexts">contexts</a></code></li>
|
||||||
<li><code><a title="connpy.services.context_service.ContextService.current_context" href="#connpy.services.context_service.ContextService.current_context">current_context</a></code></li>
|
<li><code><a title="connpy.services.context_service.ContextService.current_context" href="#connpy.services.context_service.ContextService.current_context">current_context</a></code></li>
|
||||||
@@ -360,9 +474,12 @@ def current_context(self) -> str:
|
|||||||
<li><code><a title="connpy.services.context_service.ContextService.filter_node_dict" href="#connpy.services.context_service.ContextService.filter_node_dict">filter_node_dict</a></code></li>
|
<li><code><a title="connpy.services.context_service.ContextService.filter_node_dict" href="#connpy.services.context_service.ContextService.filter_node_dict">filter_node_dict</a></code></li>
|
||||||
<li><code><a title="connpy.services.context_service.ContextService.filter_node_list" href="#connpy.services.context_service.ContextService.filter_node_list">filter_node_list</a></code></li>
|
<li><code><a title="connpy.services.context_service.ContextService.filter_node_list" href="#connpy.services.context_service.ContextService.filter_node_list">filter_node_list</a></code></li>
|
||||||
<li><code><a title="connpy.services.context_service.ContextService.get_active_regexes" href="#connpy.services.context_service.ContextService.get_active_regexes">get_active_regexes</a></code></li>
|
<li><code><a title="connpy.services.context_service.ContextService.get_active_regexes" href="#connpy.services.context_service.ContextService.get_active_regexes">get_active_regexes</a></code></li>
|
||||||
|
<li><code><a title="connpy.services.context_service.ContextService.is_default_all_context" href="#connpy.services.context_service.ContextService.is_default_all_context">is_default_all_context</a></code></li>
|
||||||
<li><code><a title="connpy.services.context_service.ContextService.list_contexts" href="#connpy.services.context_service.ContextService.list_contexts">list_contexts</a></code></li>
|
<li><code><a title="connpy.services.context_service.ContextService.list_contexts" href="#connpy.services.context_service.ContextService.list_contexts">list_contexts</a></code></li>
|
||||||
|
<li><code><a title="connpy.services.context_service.ContextService.matches_active_context" href="#connpy.services.context_service.ContextService.matches_active_context">matches_active_context</a></code></li>
|
||||||
<li><code><a title="connpy.services.context_service.ContextService.set_active_context" href="#connpy.services.context_service.ContextService.set_active_context">set_active_context</a></code></li>
|
<li><code><a title="connpy.services.context_service.ContextService.set_active_context" href="#connpy.services.context_service.ContextService.set_active_context">set_active_context</a></code></li>
|
||||||
<li><code><a title="connpy.services.context_service.ContextService.update_context" href="#connpy.services.context_service.ContextService.update_context">update_context</a></code></li>
|
<li><code><a title="connpy.services.context_service.ContextService.update_context" href="#connpy.services.context_service.ContextService.update_context">update_context</a></code></li>
|
||||||
|
<li><code><a title="connpy.services.context_service.ContextService.validate_in_active_context" href="#connpy.services.context_service.ContextService.validate_in_active_context">validate_in_active_context</a></code></li>
|
||||||
</ul>
|
</ul>
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
|
|||||||
@@ -67,6 +67,7 @@ el.replaceWith(d);
|
|||||||
</ul>
|
</ul>
|
||||||
<h3>Subclasses</h3>
|
<h3>Subclasses</h3>
|
||||||
<ul class="hlist">
|
<ul class="hlist">
|
||||||
|
<li><a title="connpy.auth_utils.SanitizedAuthError" href="../auth_utils.html#connpy.auth_utils.SanitizedAuthError">SanitizedAuthError</a></li>
|
||||||
<li><a title="connpy.services.exceptions.ExecutionError" href="#connpy.services.exceptions.ExecutionError">ExecutionError</a></li>
|
<li><a title="connpy.services.exceptions.ExecutionError" href="#connpy.services.exceptions.ExecutionError">ExecutionError</a></li>
|
||||||
<li><a title="connpy.services.exceptions.InvalidConfigurationError" href="#connpy.services.exceptions.InvalidConfigurationError">InvalidConfigurationError</a></li>
|
<li><a title="connpy.services.exceptions.InvalidConfigurationError" href="#connpy.services.exceptions.InvalidConfigurationError">InvalidConfigurationError</a></li>
|
||||||
<li><a title="connpy.services.exceptions.NodeAlreadyExistsError" href="#connpy.services.exceptions.NodeAlreadyExistsError">NodeAlreadyExistsError</a></li>
|
<li><a title="connpy.services.exceptions.NodeAlreadyExistsError" href="#connpy.services.exceptions.NodeAlreadyExistsError">NodeAlreadyExistsError</a></li>
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ el.replaceWith(d);
|
|||||||
<dl>
|
<dl>
|
||||||
<dt id="connpy.services.execution_service.ExecutionService"><code class="flex name class">
|
<dt id="connpy.services.execution_service.ExecutionService"><code class="flex name class">
|
||||||
<span>class <span class="ident">ExecutionService</span></span>
|
<span>class <span class="ident">ExecutionService</span></span>
|
||||||
<span>(</span><span>config=None)</span>
|
<span>(</span><span>config=None, remote_stub=None, attempt_tracker=None, enforce_context: bool = True)</span>
|
||||||
</code></dt>
|
</code></dt>
|
||||||
<dd>
|
<dd>
|
||||||
<details class="source">
|
<details class="source">
|
||||||
@@ -58,104 +58,344 @@ el.replaceWith(d);
|
|||||||
<pre><code class="python">class ExecutionService(BaseService):
|
<pre><code class="python">class ExecutionService(BaseService):
|
||||||
"""Business logic for executing commands on nodes and running automation scripts."""
|
"""Business logic for executing commands on nodes and running automation scripts."""
|
||||||
|
|
||||||
def run_commands(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
nodes_filter: str,
|
config=None,
|
||||||
commands: List[str],
|
remote_stub=None,
|
||||||
variables: Optional[Dict[str, Any]] = None,
|
attempt_tracker=None,
|
||||||
parallel: int = 10,
|
enforce_context: bool = True,
|
||||||
timeout: int = 20,
|
):
|
||||||
folder: Optional[str] = None,
|
super().__init__(config)
|
||||||
prompt: Optional[str] = None,
|
self.remote_stub = remote_stub
|
||||||
on_node_complete: Optional[Callable] = None,
|
self.attempt_tracker = (
|
||||||
logger: Optional[Callable] = None,
|
attempt_tracker if attempt_tracker is not None else AuthAttemptTracker()
|
||||||
name: Optional[str] = None
|
)
|
||||||
) -> Dict[str, str]:
|
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: dict[str, Any] | None = None,
|
||||||
|
parallel: int = 10,
|
||||||
|
timeout: int | None = None,
|
||||||
|
folder: str | None = None,
|
||||||
|
prompt: str | None = None,
|
||||||
|
on_node_complete: Callable | None = None,
|
||||||
|
logger: Callable | None = None,
|
||||||
|
name: str | None = None,
|
||||||
|
connect_timeout: int | None = None,
|
||||||
|
auth_timeout: int | None = None,
|
||||||
|
) -> dict[str, Any]:
|
||||||
"""Execute commands on a set of nodes."""
|
"""Execute commands on a set of nodes."""
|
||||||
try:
|
try:
|
||||||
|
self._ensure_backend_supports_stage_timeouts(
|
||||||
|
connect_timeout=connect_timeout,
|
||||||
|
auth_timeout=auth_timeout,
|
||||||
|
)
|
||||||
|
|
||||||
|
if self._is_remote_backend():
|
||||||
|
batches = self._build_remote_filter_batches(nodes_filter)
|
||||||
|
stub = self._get_remote_stub()
|
||||||
|
batch_results = [
|
||||||
|
stub.run_commands(
|
||||||
|
nodes_filter=batch,
|
||||||
|
commands=commands,
|
||||||
|
variables=variables,
|
||||||
|
parallel=parallel,
|
||||||
|
timeout=timeout if timeout is not None else 20,
|
||||||
|
folder=folder,
|
||||||
|
prompt=prompt,
|
||||||
|
on_node_complete=on_node_complete,
|
||||||
|
logger=logger,
|
||||||
|
name=name or "",
|
||||||
|
)
|
||||||
|
for batch in batches
|
||||||
|
]
|
||||||
|
return self._aggregate_remote_results(batch_results)
|
||||||
|
|
||||||
matched_names = self.config._getallnodes(nodes_filter)
|
matched_names = self.config._getallnodes(nodes_filter)
|
||||||
|
ctx_svc = self._get_context_service()
|
||||||
|
if ctx_svc is not None and not ctx_svc.is_default_all_context():
|
||||||
|
matched_names = ctx_svc.filter_node_list(result=matched_names)
|
||||||
if not matched_names:
|
if not matched_names:
|
||||||
raise ConnpyError(f"No nodes found matching filter: {nodes_filter}")
|
raise ConnpyError(f"No nodes found matching filter: {nodes_filter}")
|
||||||
|
|
||||||
node_data = self.config.getitems(matched_names, extract=True)
|
node_data = self.config.getitems(matched_names, extract=True)
|
||||||
executor = Nodes(node_data, config=self.config)
|
executor = Nodes(node_data, config=self.config)
|
||||||
self.last_executor = executor
|
self.last_executor = executor
|
||||||
|
|
||||||
results = executor.run(
|
|
||||||
commands=commands,
|
|
||||||
vars=variables,
|
|
||||||
parallel=parallel,
|
|
||||||
timeout=timeout,
|
|
||||||
folder=folder,
|
|
||||||
prompt=prompt,
|
|
||||||
on_complete=on_node_complete,
|
|
||||||
logger=logger
|
|
||||||
)
|
|
||||||
|
|
||||||
# Combine output and status for the caller
|
run_kwargs: dict[str, Any] = {
|
||||||
|
"commands": commands,
|
||||||
|
"vars": variables,
|
||||||
|
"parallel": parallel,
|
||||||
|
"timeout": timeout,
|
||||||
|
"folder": folder,
|
||||||
|
"prompt": prompt,
|
||||||
|
"on_complete": on_node_complete,
|
||||||
|
"logger": logger,
|
||||||
|
"connect_timeout": connect_timeout,
|
||||||
|
"auth_timeout": auth_timeout,
|
||||||
|
"_default_cmd_timeout": 20,
|
||||||
|
}
|
||||||
|
run_params = inspect.signature(executor.run).parameters
|
||||||
|
if "unattended" in run_params:
|
||||||
|
run_kwargs["unattended"] = True
|
||||||
|
if "attempt_tracker" in run_params:
|
||||||
|
run_kwargs["attempt_tracker"] = self.attempt_tracker
|
||||||
|
|
||||||
|
results = executor.run(**run_kwargs)
|
||||||
|
|
||||||
|
fb_map = getattr(executor, "fallback_used", {})
|
||||||
|
fb_dict = fb_map if isinstance(fb_map, dict) else {}
|
||||||
|
|
||||||
|
# Combine output, status, and fallback metadata for the caller
|
||||||
full_results = {}
|
full_results = {}
|
||||||
|
self.last_fallback_used = {}
|
||||||
for unique in results:
|
for unique in results:
|
||||||
|
fb_used = bool(fb_dict.get(unique, False))
|
||||||
full_results[unique] = {
|
full_results[unique] = {
|
||||||
"output": results[unique],
|
"output": results[unique],
|
||||||
"status": executor.status.get(unique, 1)
|
"status": executor.status.get(unique, 1),
|
||||||
|
"fallback_used": fb_used,
|
||||||
}
|
}
|
||||||
|
self.last_fallback_used[unique] = fb_used
|
||||||
|
|
||||||
return full_results
|
return full_results
|
||||||
except Exception as e:
|
except ConnpyError:
|
||||||
raise ConnpyError(f"Execution failed: {e}")
|
raise
|
||||||
|
except Exception as e: # noqa: BLE001
|
||||||
|
raise_sanitized_auth_error(e, code="AUTH_FAILED", context_label="execution failed")
|
||||||
|
|
||||||
def test_commands(
|
def test_commands(
|
||||||
self,
|
self,
|
||||||
nodes_filter: str,
|
nodes_filter: str,
|
||||||
commands: List[str],
|
commands: list[str],
|
||||||
expected: List[str],
|
expected: list[str],
|
||||||
variables: Optional[Dict[str, Any]] = None,
|
variables: dict[str, Any] | None = None,
|
||||||
parallel: int = 10,
|
parallel: int = 10,
|
||||||
timeout: int = 20,
|
timeout: int | None = None,
|
||||||
folder: Optional[str] = None,
|
folder: str | None = None,
|
||||||
prompt: Optional[str] = None,
|
prompt: str | None = None,
|
||||||
on_node_complete: Optional[Callable] = None,
|
on_node_complete: Callable | None = None,
|
||||||
logger: Optional[Callable] = None,
|
logger: Callable | None = None,
|
||||||
name: Optional[str] = None
|
name: str | None = None,
|
||||||
) -> Dict[str, Dict[str, bool]]:
|
connect_timeout: int | None = None,
|
||||||
|
auth_timeout: int | None = None,
|
||||||
|
) -> dict[str, dict[str, bool]]:
|
||||||
"""Run commands and verify expected output on a set of nodes."""
|
"""Run commands and verify expected output on a set of nodes."""
|
||||||
try:
|
try:
|
||||||
|
self._ensure_backend_supports_stage_timeouts(
|
||||||
|
connect_timeout=connect_timeout,
|
||||||
|
auth_timeout=auth_timeout,
|
||||||
|
)
|
||||||
|
|
||||||
|
if self._is_remote_backend():
|
||||||
|
batches = self._build_remote_filter_batches(nodes_filter)
|
||||||
|
stub = self._get_remote_stub()
|
||||||
|
batch_results = [
|
||||||
|
stub.test_commands(
|
||||||
|
nodes_filter=batch,
|
||||||
|
commands=commands,
|
||||||
|
expected=expected,
|
||||||
|
variables=variables,
|
||||||
|
parallel=parallel,
|
||||||
|
timeout=timeout if timeout is not None else 20,
|
||||||
|
folder=folder or "",
|
||||||
|
prompt=prompt,
|
||||||
|
on_node_complete=on_node_complete,
|
||||||
|
logger=logger,
|
||||||
|
name=name or "",
|
||||||
|
)
|
||||||
|
for batch in batches
|
||||||
|
]
|
||||||
|
return self._aggregate_remote_results(batch_results)
|
||||||
|
|
||||||
matched_names = self.config._getallnodes(nodes_filter)
|
matched_names = self.config._getallnodes(nodes_filter)
|
||||||
|
ctx_svc = self._get_context_service()
|
||||||
|
if ctx_svc is not None and not ctx_svc.is_default_all_context():
|
||||||
|
matched_names = ctx_svc.filter_node_list(result=matched_names)
|
||||||
if not matched_names:
|
if not matched_names:
|
||||||
raise ConnpyError(f"No nodes found matching filter: {nodes_filter}")
|
raise ConnpyError(f"No nodes found matching filter: {nodes_filter}")
|
||||||
|
|
||||||
node_data = self.config.getitems(matched_names, extract=True)
|
node_data = self.config.getitems(matched_names, extract=True)
|
||||||
executor = Nodes(node_data, config=self.config)
|
executor = Nodes(node_data, config=self.config)
|
||||||
self.last_executor = executor
|
self.last_executor = executor
|
||||||
|
|
||||||
results = executor.test(
|
|
||||||
commands=commands,
|
|
||||||
expected=expected,
|
|
||||||
vars=variables,
|
|
||||||
parallel=parallel,
|
|
||||||
timeout=timeout,
|
|
||||||
folder=folder,
|
|
||||||
prompt=prompt,
|
|
||||||
on_complete=on_node_complete,
|
|
||||||
logger=logger
|
|
||||||
)
|
|
||||||
return results
|
|
||||||
except Exception as e:
|
|
||||||
raise ConnpyError(f"Testing failed: {e}")
|
|
||||||
|
|
||||||
def run_cli_script(self, nodes_filter: str, script_path: str, parallel: int = 10) -> Dict[str, str]:
|
test_kwargs: dict[str, Any] = {
|
||||||
|
"commands": commands,
|
||||||
|
"expected": expected,
|
||||||
|
"vars": variables,
|
||||||
|
"parallel": parallel,
|
||||||
|
"timeout": timeout,
|
||||||
|
"folder": folder,
|
||||||
|
"prompt": prompt,
|
||||||
|
"on_complete": on_node_complete,
|
||||||
|
"logger": logger,
|
||||||
|
"connect_timeout": connect_timeout,
|
||||||
|
"auth_timeout": auth_timeout,
|
||||||
|
"_default_cmd_timeout": 20,
|
||||||
|
}
|
||||||
|
test_params = inspect.signature(executor.test).parameters
|
||||||
|
if "unattended" in test_params:
|
||||||
|
test_kwargs["unattended"] = True
|
||||||
|
if "attempt_tracker" in test_params:
|
||||||
|
test_kwargs["attempt_tracker"] = self.attempt_tracker
|
||||||
|
|
||||||
|
res = executor.test(**test_kwargs)
|
||||||
|
fb_map = getattr(executor, "fallback_used", {})
|
||||||
|
fb_dict = fb_map if isinstance(fb_map, dict) else {}
|
||||||
|
if isinstance(res, dict):
|
||||||
|
self.last_fallback_used = {u: bool(fb_dict.get(u, False)) for u in res}
|
||||||
|
else:
|
||||||
|
self.last_fallback_used = {}
|
||||||
|
return res
|
||||||
|
except ConnpyError:
|
||||||
|
raise
|
||||||
|
except Exception as e: # noqa: BLE001
|
||||||
|
raise_sanitized_auth_error(e, code="AUTH_FAILED", context_label="testing failed")
|
||||||
|
|
||||||
|
def run_cli_script(self, nodes_filter: str, script_path: str, parallel: int = 10) -> dict[str, str]:
|
||||||
"""Run a plain-text script containing one command per line."""
|
"""Run a plain-text script containing one command per line."""
|
||||||
if not os.path.exists(script_path):
|
if not os.path.exists(script_path):
|
||||||
raise ConnpyError(f"Script file not found: {script_path}")
|
raise ConnpyError(f"Script file not found: {script_path}")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with open(script_path, "r") as f:
|
with open(script_path) as f:
|
||||||
commands = [line.strip() for line in f if line.strip()]
|
commands = [line.strip() for line in f if line.strip()]
|
||||||
except Exception as e:
|
except OSError:
|
||||||
raise ConnpyError(f"Failed to read script {script_path}: {e}")
|
raise ConnpyError("Failed to read script file") from None
|
||||||
|
|
||||||
return self.run_commands(nodes_filter, commands, parallel=parallel)</code></pre>
|
return self.run_commands(nodes_filter, commands, parallel=parallel)</code></pre>
|
||||||
</details>
|
</details>
|
||||||
<div class="desc"><p>Business logic for executing commands on nodes and running automation scripts.</p>
|
<div class="desc"><p>Business logic for executing commands on nodes and running automation scripts.</p>
|
||||||
@@ -173,30 +413,30 @@ el.replaceWith(d);
|
|||||||
<h3>Methods</h3>
|
<h3>Methods</h3>
|
||||||
<dl>
|
<dl>
|
||||||
<dt id="connpy.services.execution_service.ExecutionService.run_cli_script"><code class="name flex">
|
<dt id="connpy.services.execution_service.ExecutionService.run_cli_script"><code class="name flex">
|
||||||
<span>def <span class="ident">run_cli_script</span></span>(<span>self, nodes_filter: str, script_path: str, parallel: int = 10) ‑> Dict[str, str]</span>
|
<span>def <span class="ident">run_cli_script</span></span>(<span>self, nodes_filter: str, script_path: str, parallel: int = 10) ‑> dict[str, str]</span>
|
||||||
</code></dt>
|
</code></dt>
|
||||||
<dd>
|
<dd>
|
||||||
<details class="source">
|
<details class="source">
|
||||||
<summary>
|
<summary>
|
||||||
<span>Expand source code</span>
|
<span>Expand source code</span>
|
||||||
</summary>
|
</summary>
|
||||||
<pre><code class="python">def run_cli_script(self, nodes_filter: str, script_path: str, parallel: int = 10) -> 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."""
|
"""Run a plain-text script containing one command per line."""
|
||||||
if not os.path.exists(script_path):
|
if not os.path.exists(script_path):
|
||||||
raise ConnpyError(f"Script file not found: {script_path}")
|
raise ConnpyError(f"Script file not found: {script_path}")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with open(script_path, "r") as f:
|
with open(script_path) as f:
|
||||||
commands = [line.strip() for line in f if line.strip()]
|
commands = [line.strip() for line in f if line.strip()]
|
||||||
except Exception as e:
|
except OSError:
|
||||||
raise ConnpyError(f"Failed to read script {script_path}: {e}")
|
raise ConnpyError("Failed to read script file") from None
|
||||||
|
|
||||||
return self.run_commands(nodes_filter, commands, parallel=parallel)</code></pre>
|
return self.run_commands(nodes_filter, commands, parallel=parallel)</code></pre>
|
||||||
</details>
|
</details>
|
||||||
<div class="desc"><p>Run a plain-text script containing one command per line.</p></div>
|
<div class="desc"><p>Run a plain-text script containing one command per line.</p></div>
|
||||||
</dd>
|
</dd>
|
||||||
<dt id="connpy.services.execution_service.ExecutionService.run_commands"><code class="name flex">
|
<dt id="connpy.services.execution_service.ExecutionService.run_commands"><code class="name flex">
|
||||||
<span>def <span class="ident">run_commands</span></span>(<span>self,<br>nodes_filter: str,<br>commands: List[str],<br>variables: Dict[str, Any] | None = None,<br>parallel: int = 10,<br>timeout: int = 20,<br>folder: str | None = None,<br>prompt: str | None = None,<br>on_node_complete: Callable | None = None,<br>logger: Callable | None = None,<br>name: str | None = None) ‑> Dict[str, str]</span>
|
<span>def <span class="ident">run_commands</span></span>(<span>self,<br>nodes_filter: str,<br>commands: list[str],<br>variables: dict[str, typing.Any] | None = None,<br>parallel: int = 10,<br>timeout: int | None = None,<br>folder: str | None = None,<br>prompt: str | None = None,<br>on_node_complete: collections.abc.Callable | None = None,<br>logger: collections.abc.Callable | None = None,<br>name: str | None = None,<br>connect_timeout: int | None = None,<br>auth_timeout: int | None = None) ‑> dict[str, typing.Any]</span>
|
||||||
</code></dt>
|
</code></dt>
|
||||||
<dd>
|
<dd>
|
||||||
<details class="source">
|
<details class="source">
|
||||||
@@ -204,56 +444,104 @@ el.replaceWith(d);
|
|||||||
<span>Expand source code</span>
|
<span>Expand source code</span>
|
||||||
</summary>
|
</summary>
|
||||||
<pre><code class="python">def run_commands(
|
<pre><code class="python">def run_commands(
|
||||||
self,
|
self,
|
||||||
nodes_filter: str,
|
nodes_filter: str,
|
||||||
commands: List[str],
|
commands: list[str],
|
||||||
variables: Optional[Dict[str, Any]] = None,
|
variables: dict[str, Any] | None = None,
|
||||||
parallel: int = 10,
|
parallel: int = 10,
|
||||||
timeout: int = 20,
|
timeout: int | None = None,
|
||||||
folder: Optional[str] = None,
|
folder: str | None = None,
|
||||||
prompt: Optional[str] = None,
|
prompt: str | None = None,
|
||||||
on_node_complete: Optional[Callable] = None,
|
on_node_complete: Callable | None = None,
|
||||||
logger: Optional[Callable] = None,
|
logger: Callable | None = None,
|
||||||
name: Optional[str] = None
|
name: str | None = None,
|
||||||
) -> Dict[str, str]:
|
connect_timeout: int | None = None,
|
||||||
|
auth_timeout: int | None = None,
|
||||||
|
) -> dict[str, Any]:
|
||||||
"""Execute commands on a set of nodes."""
|
"""Execute commands on a set of nodes."""
|
||||||
try:
|
try:
|
||||||
|
self._ensure_backend_supports_stage_timeouts(
|
||||||
|
connect_timeout=connect_timeout,
|
||||||
|
auth_timeout=auth_timeout,
|
||||||
|
)
|
||||||
|
|
||||||
|
if self._is_remote_backend():
|
||||||
|
batches = self._build_remote_filter_batches(nodes_filter)
|
||||||
|
stub = self._get_remote_stub()
|
||||||
|
batch_results = [
|
||||||
|
stub.run_commands(
|
||||||
|
nodes_filter=batch,
|
||||||
|
commands=commands,
|
||||||
|
variables=variables,
|
||||||
|
parallel=parallel,
|
||||||
|
timeout=timeout if timeout is not None else 20,
|
||||||
|
folder=folder,
|
||||||
|
prompt=prompt,
|
||||||
|
on_node_complete=on_node_complete,
|
||||||
|
logger=logger,
|
||||||
|
name=name or "",
|
||||||
|
)
|
||||||
|
for batch in batches
|
||||||
|
]
|
||||||
|
return self._aggregate_remote_results(batch_results)
|
||||||
|
|
||||||
matched_names = self.config._getallnodes(nodes_filter)
|
matched_names = self.config._getallnodes(nodes_filter)
|
||||||
|
ctx_svc = self._get_context_service()
|
||||||
|
if ctx_svc is not None and not ctx_svc.is_default_all_context():
|
||||||
|
matched_names = ctx_svc.filter_node_list(result=matched_names)
|
||||||
if not matched_names:
|
if not matched_names:
|
||||||
raise ConnpyError(f"No nodes found matching filter: {nodes_filter}")
|
raise ConnpyError(f"No nodes found matching filter: {nodes_filter}")
|
||||||
|
|
||||||
node_data = self.config.getitems(matched_names, extract=True)
|
node_data = self.config.getitems(matched_names, extract=True)
|
||||||
executor = Nodes(node_data, config=self.config)
|
executor = Nodes(node_data, config=self.config)
|
||||||
self.last_executor = executor
|
self.last_executor = executor
|
||||||
|
|
||||||
results = executor.run(
|
|
||||||
commands=commands,
|
|
||||||
vars=variables,
|
|
||||||
parallel=parallel,
|
|
||||||
timeout=timeout,
|
|
||||||
folder=folder,
|
|
||||||
prompt=prompt,
|
|
||||||
on_complete=on_node_complete,
|
|
||||||
logger=logger
|
|
||||||
)
|
|
||||||
|
|
||||||
# Combine output and status for the caller
|
run_kwargs: dict[str, Any] = {
|
||||||
|
"commands": commands,
|
||||||
|
"vars": variables,
|
||||||
|
"parallel": parallel,
|
||||||
|
"timeout": timeout,
|
||||||
|
"folder": folder,
|
||||||
|
"prompt": prompt,
|
||||||
|
"on_complete": on_node_complete,
|
||||||
|
"logger": logger,
|
||||||
|
"connect_timeout": connect_timeout,
|
||||||
|
"auth_timeout": auth_timeout,
|
||||||
|
"_default_cmd_timeout": 20,
|
||||||
|
}
|
||||||
|
run_params = inspect.signature(executor.run).parameters
|
||||||
|
if "unattended" in run_params:
|
||||||
|
run_kwargs["unattended"] = True
|
||||||
|
if "attempt_tracker" in run_params:
|
||||||
|
run_kwargs["attempt_tracker"] = self.attempt_tracker
|
||||||
|
|
||||||
|
results = executor.run(**run_kwargs)
|
||||||
|
|
||||||
|
fb_map = getattr(executor, "fallback_used", {})
|
||||||
|
fb_dict = fb_map if isinstance(fb_map, dict) else {}
|
||||||
|
|
||||||
|
# Combine output, status, and fallback metadata for the caller
|
||||||
full_results = {}
|
full_results = {}
|
||||||
|
self.last_fallback_used = {}
|
||||||
for unique in results:
|
for unique in results:
|
||||||
|
fb_used = bool(fb_dict.get(unique, False))
|
||||||
full_results[unique] = {
|
full_results[unique] = {
|
||||||
"output": results[unique],
|
"output": results[unique],
|
||||||
"status": executor.status.get(unique, 1)
|
"status": executor.status.get(unique, 1),
|
||||||
|
"fallback_used": fb_used,
|
||||||
}
|
}
|
||||||
|
self.last_fallback_used[unique] = fb_used
|
||||||
|
|
||||||
return full_results
|
return full_results
|
||||||
except Exception as e:
|
except ConnpyError:
|
||||||
raise ConnpyError(f"Execution failed: {e}")</code></pre>
|
raise
|
||||||
|
except Exception as e: # noqa: BLE001
|
||||||
|
raise_sanitized_auth_error(e, code="AUTH_FAILED", context_label="execution failed")</code></pre>
|
||||||
</details>
|
</details>
|
||||||
<div class="desc"><p>Execute commands on a set of nodes.</p></div>
|
<div class="desc"><p>Execute commands on a set of nodes.</p></div>
|
||||||
</dd>
|
</dd>
|
||||||
<dt id="connpy.services.execution_service.ExecutionService.test_commands"><code class="name flex">
|
<dt id="connpy.services.execution_service.ExecutionService.test_commands"><code class="name flex">
|
||||||
<span>def <span class="ident">test_commands</span></span>(<span>self,<br>nodes_filter: str,<br>commands: List[str],<br>expected: List[str],<br>variables: Dict[str, Any] | None = None,<br>parallel: int = 10,<br>timeout: int = 20,<br>folder: str | None = None,<br>prompt: str | None = None,<br>on_node_complete: Callable | None = None,<br>logger: Callable | None = None,<br>name: str | None = None) ‑> Dict[str, Dict[str, bool]]</span>
|
<span>def <span class="ident">test_commands</span></span>(<span>self,<br>nodes_filter: str,<br>commands: list[str],<br>expected: list[str],<br>variables: dict[str, typing.Any] | None = None,<br>parallel: int = 10,<br>timeout: int | None = None,<br>folder: str | None = None,<br>prompt: str | None = None,<br>on_node_complete: collections.abc.Callable | None = None,<br>logger: collections.abc.Callable | None = None,<br>name: str | None = None,<br>connect_timeout: int | None = None,<br>auth_timeout: int | None = None) ‑> dict[str, dict[str, bool]]</span>
|
||||||
</code></dt>
|
</code></dt>
|
||||||
<dd>
|
<dd>
|
||||||
<details class="source">
|
<details class="source">
|
||||||
@@ -263,42 +551,90 @@ el.replaceWith(d);
|
|||||||
<pre><code class="python">def test_commands(
|
<pre><code class="python">def test_commands(
|
||||||
self,
|
self,
|
||||||
nodes_filter: str,
|
nodes_filter: str,
|
||||||
commands: List[str],
|
commands: list[str],
|
||||||
expected: List[str],
|
expected: list[str],
|
||||||
variables: Optional[Dict[str, Any]] = None,
|
variables: dict[str, Any] | None = None,
|
||||||
parallel: int = 10,
|
parallel: int = 10,
|
||||||
timeout: int = 20,
|
timeout: int | None = None,
|
||||||
folder: Optional[str] = None,
|
folder: str | None = None,
|
||||||
prompt: Optional[str] = None,
|
prompt: str | None = None,
|
||||||
on_node_complete: Optional[Callable] = None,
|
on_node_complete: Callable | None = None,
|
||||||
logger: Optional[Callable] = None,
|
logger: Callable | None = None,
|
||||||
name: Optional[str] = None
|
name: str | None = None,
|
||||||
) -> Dict[str, Dict[str, bool]]:
|
connect_timeout: int | None = None,
|
||||||
|
auth_timeout: int | None = None,
|
||||||
|
) -> dict[str, dict[str, bool]]:
|
||||||
"""Run commands and verify expected output on a set of nodes."""
|
"""Run commands and verify expected output on a set of nodes."""
|
||||||
try:
|
try:
|
||||||
|
self._ensure_backend_supports_stage_timeouts(
|
||||||
|
connect_timeout=connect_timeout,
|
||||||
|
auth_timeout=auth_timeout,
|
||||||
|
)
|
||||||
|
|
||||||
|
if self._is_remote_backend():
|
||||||
|
batches = self._build_remote_filter_batches(nodes_filter)
|
||||||
|
stub = self._get_remote_stub()
|
||||||
|
batch_results = [
|
||||||
|
stub.test_commands(
|
||||||
|
nodes_filter=batch,
|
||||||
|
commands=commands,
|
||||||
|
expected=expected,
|
||||||
|
variables=variables,
|
||||||
|
parallel=parallel,
|
||||||
|
timeout=timeout if timeout is not None else 20,
|
||||||
|
folder=folder or "",
|
||||||
|
prompt=prompt,
|
||||||
|
on_node_complete=on_node_complete,
|
||||||
|
logger=logger,
|
||||||
|
name=name or "",
|
||||||
|
)
|
||||||
|
for batch in batches
|
||||||
|
]
|
||||||
|
return self._aggregate_remote_results(batch_results)
|
||||||
|
|
||||||
matched_names = self.config._getallnodes(nodes_filter)
|
matched_names = self.config._getallnodes(nodes_filter)
|
||||||
|
ctx_svc = self._get_context_service()
|
||||||
|
if ctx_svc is not None and not ctx_svc.is_default_all_context():
|
||||||
|
matched_names = ctx_svc.filter_node_list(result=matched_names)
|
||||||
if not matched_names:
|
if not matched_names:
|
||||||
raise ConnpyError(f"No nodes found matching filter: {nodes_filter}")
|
raise ConnpyError(f"No nodes found matching filter: {nodes_filter}")
|
||||||
|
|
||||||
node_data = self.config.getitems(matched_names, extract=True)
|
node_data = self.config.getitems(matched_names, extract=True)
|
||||||
executor = Nodes(node_data, config=self.config)
|
executor = Nodes(node_data, config=self.config)
|
||||||
self.last_executor = executor
|
self.last_executor = executor
|
||||||
|
|
||||||
results = executor.test(
|
test_kwargs: dict[str, Any] = {
|
||||||
commands=commands,
|
"commands": commands,
|
||||||
expected=expected,
|
"expected": expected,
|
||||||
vars=variables,
|
"vars": variables,
|
||||||
parallel=parallel,
|
"parallel": parallel,
|
||||||
timeout=timeout,
|
"timeout": timeout,
|
||||||
folder=folder,
|
"folder": folder,
|
||||||
prompt=prompt,
|
"prompt": prompt,
|
||||||
on_complete=on_node_complete,
|
"on_complete": on_node_complete,
|
||||||
logger=logger
|
"logger": logger,
|
||||||
)
|
"connect_timeout": connect_timeout,
|
||||||
return results
|
"auth_timeout": auth_timeout,
|
||||||
except Exception as e:
|
"_default_cmd_timeout": 20,
|
||||||
raise ConnpyError(f"Testing failed: {e}")</code></pre>
|
}
|
||||||
|
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>
|
</details>
|
||||||
<div class="desc"><p>Run commands and verify expected output on a set of nodes.</p></div>
|
<div class="desc"><p>Run commands and verify expected output on a set of nodes.</p></div>
|
||||||
</dd>
|
</dd>
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ el.replaceWith(d);
|
|||||||
<dl>
|
<dl>
|
||||||
<dt id="connpy.services.import_export_service.ImportExportService"><code class="flex name class">
|
<dt id="connpy.services.import_export_service.ImportExportService"><code class="flex name class">
|
||||||
<span>class <span class="ident">ImportExportService</span></span>
|
<span>class <span class="ident">ImportExportService</span></span>
|
||||||
<span>(</span><span>config=None)</span>
|
<span>(</span><span>config=None, enforce_context: bool = True)</span>
|
||||||
</code></dt>
|
</code></dt>
|
||||||
<dd>
|
<dd>
|
||||||
<details class="source">
|
<details class="source">
|
||||||
@@ -58,6 +58,44 @@ el.replaceWith(d);
|
|||||||
<pre><code class="python">class ImportExportService(BaseService):
|
<pre><code class="python">class ImportExportService(BaseService):
|
||||||
"""Business logic for YAML/JSON inventory import and export."""
|
"""Business logic for YAML/JSON inventory import and export."""
|
||||||
|
|
||||||
|
def __init__(self, config=None, enforce_context: bool = True):
|
||||||
|
super().__init__(config)
|
||||||
|
self.enforce_context = bool(enforce_context)
|
||||||
|
|
||||||
|
def _get_context_service(self, cfg=None):
|
||||||
|
target_cfg = cfg or self.config
|
||||||
|
if (
|
||||||
|
not getattr(self, "enforce_context", True)
|
||||||
|
or not getattr(target_cfg, "_enforce_context", True)
|
||||||
|
or (len(sys.argv) > 1 and sys.argv[1] == "api")
|
||||||
|
or not isinstance(getattr(target_cfg, "config", None), dict)
|
||||||
|
):
|
||||||
|
return None
|
||||||
|
from .context_service import ContextService
|
||||||
|
|
||||||
|
return ContextService(target_cfg)
|
||||||
|
|
||||||
|
def _get_raw_folders(self, cfg=None) -> list[str]:
|
||||||
|
target_cfg = cfg or self.config
|
||||||
|
if target_cfg is None:
|
||||||
|
return []
|
||||||
|
if (
|
||||||
|
not hasattr(target_cfg, "assert_called")
|
||||||
|
and callable(getattr(target_cfg, "_getallfolders_unlocked", None))
|
||||||
|
and hasattr(target_cfg, "_mutation_lock")
|
||||||
|
):
|
||||||
|
with target_cfg._mutation_lock:
|
||||||
|
return target_cfg._getallfolders_unlocked()
|
||||||
|
return target_cfg._getallfolders()
|
||||||
|
|
||||||
|
def _folder_exists(self, folder_id: str, raw_folders: list[str], cfg=None) -> bool:
|
||||||
|
target_cfg = cfg or self.config
|
||||||
|
cfg_dict = getattr(target_cfg, "config", None)
|
||||||
|
case = cfg_dict.get("case", False) if isinstance(cfg_dict, dict) else False
|
||||||
|
if case:
|
||||||
|
return folder_id in raw_folders
|
||||||
|
return folder_id.lower() in {f.lower() for f in raw_folders}
|
||||||
|
|
||||||
def export_to_file(self, file_path, folders=None):
|
def export_to_file(self, file_path, folders=None):
|
||||||
"""Export nodes/folders to a YAML file."""
|
"""Export nodes/folders to a YAML file."""
|
||||||
if os.path.exists(file_path):
|
if os.path.exists(file_path):
|
||||||
@@ -72,46 +110,72 @@ el.replaceWith(d);
|
|||||||
|
|
||||||
def export_to_dict(self, folders=None):
|
def export_to_dict(self, folders=None):
|
||||||
"""Export nodes/folders to a dictionary."""
|
"""Export nodes/folders to a dictionary."""
|
||||||
if not folders:
|
ctx_svc = self._get_context_service()
|
||||||
|
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)
|
return deepcopy(self.config.connections)
|
||||||
else:
|
|
||||||
# Validate folders exist
|
raw_folders = self._get_raw_folders()
|
||||||
|
if folders:
|
||||||
for f in folders:
|
for f in folders:
|
||||||
if f != "@" and f not in self.config._getallfolders():
|
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.")
|
raise NodeNotFoundError(f"Folder '{f}' not found.")
|
||||||
|
|
||||||
flat = self.config._getallnodesfull(folders, extract=False)
|
flat = self.config._getallnodesfull(folders, extract=False)
|
||||||
nested = {}
|
allowed_folders = []
|
||||||
for k, v in flat.items():
|
else:
|
||||||
uniques = self.config._explode_unique(k)
|
allowed_folders = ctx_svc.filter_node_list(result=raw_folders)
|
||||||
if not uniques:
|
flat = self.config._getallnodesfull(extract=False)
|
||||||
continue
|
|
||||||
|
if ctx_active:
|
||||||
if "folder" in uniques and "subfolder" in uniques:
|
flat = ctx_svc.filter_node_dict(result=flat)
|
||||||
f_name = uniques["folder"]
|
|
||||||
s_name = uniques["subfolder"]
|
nested = {}
|
||||||
i_name = uniques["id"]
|
for folder_id in allowed_folders:
|
||||||
|
uniques = self.config._explode_unique(folder_id)
|
||||||
if f_name not in nested:
|
if not uniques or "folder" not in uniques:
|
||||||
nested[f_name] = {"type": "folder"}
|
continue
|
||||||
if s_name not in nested[f_name]:
|
f_name = uniques["folder"]
|
||||||
nested[f_name][s_name] = {"type": "subfolder"}
|
if f_name not in nested:
|
||||||
|
nested[f_name] = {"type": "folder"}
|
||||||
nested[f_name][s_name][i_name] = v
|
if "subfolder" in uniques:
|
||||||
|
s_name = uniques["subfolder"]
|
||||||
elif "folder" in uniques:
|
if s_name not in nested[f_name]:
|
||||||
f_name = uniques["folder"]
|
nested[f_name][s_name] = {"type": "subfolder"}
|
||||||
i_name = uniques["id"]
|
|
||||||
|
for k, v in flat.items():
|
||||||
if f_name not in nested:
|
uniques = self.config._explode_unique(k)
|
||||||
nested[f_name] = {"type": "folder"}
|
if not uniques:
|
||||||
|
continue
|
||||||
nested[f_name][i_name] = v
|
|
||||||
else:
|
if "folder" in uniques and "subfolder" in uniques:
|
||||||
i_name = uniques["id"]
|
f_name = uniques["folder"]
|
||||||
nested[i_name] = v
|
s_name = uniques["subfolder"]
|
||||||
|
i_name = uniques["id"]
|
||||||
return nested
|
|
||||||
|
if f_name not in nested:
|
||||||
|
nested[f_name] = {"type": "folder"}
|
||||||
|
if s_name not in nested[f_name]:
|
||||||
|
nested[f_name][s_name] = {"type": "subfolder"}
|
||||||
|
|
||||||
|
nested[f_name][s_name][i_name] = v
|
||||||
|
|
||||||
|
elif "folder" in uniques:
|
||||||
|
f_name = uniques["folder"]
|
||||||
|
i_name = uniques["id"]
|
||||||
|
|
||||||
|
if f_name not in nested:
|
||||||
|
nested[f_name] = {"type": "folder"}
|
||||||
|
|
||||||
|
nested[f_name][i_name] = v
|
||||||
|
else:
|
||||||
|
i_name = uniques["id"]
|
||||||
|
nested[i_name] = v
|
||||||
|
|
||||||
|
return nested
|
||||||
|
|
||||||
def import_from_file(self, file_path):
|
def import_from_file(self, file_path):
|
||||||
"""Import nodes/folders from a YAML file."""
|
"""Import nodes/folders from a YAML file."""
|
||||||
@@ -126,42 +190,94 @@ el.replaceWith(d);
|
|||||||
raise InvalidConfigurationError(f"Failed to read/parse import file: {e}")
|
raise InvalidConfigurationError(f"Failed to read/parse import file: {e}")
|
||||||
|
|
||||||
def import_from_dict(self, data):
|
def import_from_dict(self, data):
|
||||||
"""Import nodes/folders from a dictionary."""
|
"""Import nodes/folders from a dictionary atomically with context pre-validation."""
|
||||||
if not isinstance(data, dict):
|
if not isinstance(data, dict):
|
||||||
raise InvalidConfigurationError("Invalid import data format: expected a dictionary of nodes.")
|
raise InvalidConfigurationError(
|
||||||
|
"Invalid import data format: expected a dictionary of nodes."
|
||||||
|
)
|
||||||
|
|
||||||
def _traverse_import(node_data, current_folder='', current_subfolder=''):
|
ctx_svc = self._get_context_service()
|
||||||
|
ctx_active = ctx_svc is not None and not ctx_svc.is_default_all_context()
|
||||||
|
raw_folders = self._get_raw_folders() if ctx_active else []
|
||||||
|
planned_ops = []
|
||||||
|
|
||||||
|
def _plan_import(node_data, current_folder="", current_subfolder=""):
|
||||||
for k, v in node_data.items():
|
for k, v in node_data.items():
|
||||||
if k == "type":
|
if k == "type" or not isinstance(v, dict):
|
||||||
continue
|
continue
|
||||||
if isinstance(v, dict):
|
node_type = v.get("type", "connection")
|
||||||
node_type = v.get("type", "connection")
|
if node_type == "folder":
|
||||||
if node_type == "folder":
|
folder_id = f"@{k}"
|
||||||
self.config._folder_add(folder=k)
|
has_children = any(
|
||||||
_traverse_import(v, current_folder=k, current_subfolder='')
|
isinstance(cv, dict) for ck, cv in v.items() if ck != "type"
|
||||||
elif node_type == "subfolder":
|
)
|
||||||
self.config._folder_add(folder=current_folder, subfolder=k)
|
if ctx_active and (
|
||||||
_traverse_import(v, current_folder=current_folder, current_subfolder=k)
|
not has_children
|
||||||
elif node_type == "connection":
|
or (
|
||||||
unique_id = k
|
not ctx_svc.matches_active_context(folder_id)
|
||||||
if current_subfolder:
|
and not self._folder_exists(folder_id, raw_folders)
|
||||||
unique_id = f"{k}@{current_subfolder}@{current_folder}"
|
)
|
||||||
elif current_folder:
|
):
|
||||||
unique_id = f"{k}@{current_folder}"
|
ctx_svc.validate_in_active_context(folder_id, is_folder=True)
|
||||||
self._validate_node_name(unique_id)
|
planned_ops.append(("folder", k, ""))
|
||||||
|
_plan_import(v, current_folder=k, current_subfolder="")
|
||||||
kwargs = deepcopy(v)
|
elif node_type == "subfolder":
|
||||||
kwargs['id'] = k
|
folder_id = f"@{k}@{current_folder}"
|
||||||
kwargs['folder'] = current_folder
|
has_children = any(
|
||||||
kwargs['subfolder'] = current_subfolder
|
isinstance(cv, dict) for ck, cv in v.items() if ck != "type"
|
||||||
|
)
|
||||||
self.config._connections_add(**kwargs)
|
if ctx_active and (
|
||||||
else:
|
not has_children
|
||||||
# Invalid format skip
|
or (
|
||||||
pass
|
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:
|
||||||
|
unique_id = f"{k}@{current_subfolder}@{current_folder}"
|
||||||
|
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)
|
||||||
|
|
||||||
_traverse_import(data)
|
kwargs = deepcopy(v)
|
||||||
self.config._saveconfig(self.config.file)</code></pre>
|
kwargs["id"] = k
|
||||||
|
kwargs["folder"] = current_folder
|
||||||
|
kwargs["subfolder"] = current_subfolder
|
||||||
|
planned_ops.append(("connection", 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:
|
||||||
|
cfg._connections_add_unlocked(**op[1])
|
||||||
|
|
||||||
|
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>
|
</details>
|
||||||
<div class="desc"><p>Business logic for YAML/JSON inventory import and export.</p>
|
<div class="desc"><p>Business logic for YAML/JSON inventory import and export.</p>
|
||||||
<p>Initialize the service.</p>
|
<p>Initialize the service.</p>
|
||||||
@@ -187,46 +303,72 @@ el.replaceWith(d);
|
|||||||
</summary>
|
</summary>
|
||||||
<pre><code class="python">def export_to_dict(self, folders=None):
|
<pre><code class="python">def export_to_dict(self, folders=None):
|
||||||
"""Export nodes/folders to a dictionary."""
|
"""Export nodes/folders to a dictionary."""
|
||||||
if not folders:
|
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)
|
return deepcopy(self.config.connections)
|
||||||
else:
|
|
||||||
# Validate folders exist
|
raw_folders = self._get_raw_folders()
|
||||||
|
if folders:
|
||||||
for f in folders:
|
for f in folders:
|
||||||
if f != "@" and f not in self.config._getallfolders():
|
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.")
|
raise NodeNotFoundError(f"Folder '{f}' not found.")
|
||||||
|
|
||||||
flat = self.config._getallnodesfull(folders, extract=False)
|
flat = self.config._getallnodesfull(folders, extract=False)
|
||||||
nested = {}
|
allowed_folders = []
|
||||||
for k, v in flat.items():
|
else:
|
||||||
uniques = self.config._explode_unique(k)
|
allowed_folders = ctx_svc.filter_node_list(result=raw_folders)
|
||||||
if not uniques:
|
flat = self.config._getallnodesfull(extract=False)
|
||||||
continue
|
|
||||||
|
if ctx_active:
|
||||||
if "folder" in uniques and "subfolder" in uniques:
|
flat = ctx_svc.filter_node_dict(result=flat)
|
||||||
f_name = uniques["folder"]
|
|
||||||
s_name = uniques["subfolder"]
|
nested = {}
|
||||||
i_name = uniques["id"]
|
for folder_id in allowed_folders:
|
||||||
|
uniques = self.config._explode_unique(folder_id)
|
||||||
if f_name not in nested:
|
if not uniques or "folder" not in uniques:
|
||||||
nested[f_name] = {"type": "folder"}
|
continue
|
||||||
if s_name not in nested[f_name]:
|
f_name = uniques["folder"]
|
||||||
nested[f_name][s_name] = {"type": "subfolder"}
|
if f_name not in nested:
|
||||||
|
nested[f_name] = {"type": "folder"}
|
||||||
nested[f_name][s_name][i_name] = v
|
if "subfolder" in uniques:
|
||||||
|
s_name = uniques["subfolder"]
|
||||||
elif "folder" in uniques:
|
if s_name not in nested[f_name]:
|
||||||
f_name = uniques["folder"]
|
nested[f_name][s_name] = {"type": "subfolder"}
|
||||||
i_name = uniques["id"]
|
|
||||||
|
for k, v in flat.items():
|
||||||
if f_name not in nested:
|
uniques = self.config._explode_unique(k)
|
||||||
nested[f_name] = {"type": "folder"}
|
if not uniques:
|
||||||
|
continue
|
||||||
nested[f_name][i_name] = v
|
|
||||||
else:
|
if "folder" in uniques and "subfolder" in uniques:
|
||||||
i_name = uniques["id"]
|
f_name = uniques["folder"]
|
||||||
nested[i_name] = v
|
s_name = uniques["subfolder"]
|
||||||
|
i_name = uniques["id"]
|
||||||
return nested</code></pre>
|
|
||||||
|
if f_name not in nested:
|
||||||
|
nested[f_name] = {"type": "folder"}
|
||||||
|
if s_name not in nested[f_name]:
|
||||||
|
nested[f_name][s_name] = {"type": "subfolder"}
|
||||||
|
|
||||||
|
nested[f_name][s_name][i_name] = v
|
||||||
|
|
||||||
|
elif "folder" in uniques:
|
||||||
|
f_name = uniques["folder"]
|
||||||
|
i_name = uniques["id"]
|
||||||
|
|
||||||
|
if f_name not in nested:
|
||||||
|
nested[f_name] = {"type": "folder"}
|
||||||
|
|
||||||
|
nested[f_name][i_name] = v
|
||||||
|
else:
|
||||||
|
i_name = uniques["id"]
|
||||||
|
nested[i_name] = v
|
||||||
|
|
||||||
|
return nested</code></pre>
|
||||||
</details>
|
</details>
|
||||||
<div class="desc"><p>Export nodes/folders to a dictionary.</p></div>
|
<div class="desc"><p>Export nodes/folders to a dictionary.</p></div>
|
||||||
</dd>
|
</dd>
|
||||||
@@ -261,44 +403,96 @@ el.replaceWith(d);
|
|||||||
<span>Expand source code</span>
|
<span>Expand source code</span>
|
||||||
</summary>
|
</summary>
|
||||||
<pre><code class="python">def import_from_dict(self, data):
|
<pre><code class="python">def import_from_dict(self, data):
|
||||||
"""Import nodes/folders from a dictionary."""
|
"""Import nodes/folders from a dictionary atomically with context pre-validation."""
|
||||||
if not isinstance(data, dict):
|
if not isinstance(data, dict):
|
||||||
raise InvalidConfigurationError("Invalid import data format: expected a dictionary of nodes.")
|
raise InvalidConfigurationError(
|
||||||
|
"Invalid import data format: expected a dictionary of nodes."
|
||||||
|
)
|
||||||
|
|
||||||
def _traverse_import(node_data, current_folder='', current_subfolder=''):
|
ctx_svc = self._get_context_service()
|
||||||
|
ctx_active = ctx_svc is not None and not ctx_svc.is_default_all_context()
|
||||||
|
raw_folders = self._get_raw_folders() if ctx_active else []
|
||||||
|
planned_ops = []
|
||||||
|
|
||||||
|
def _plan_import(node_data, current_folder="", current_subfolder=""):
|
||||||
for k, v in node_data.items():
|
for k, v in node_data.items():
|
||||||
if k == "type":
|
if k == "type" or not isinstance(v, dict):
|
||||||
continue
|
continue
|
||||||
if isinstance(v, dict):
|
node_type = v.get("type", "connection")
|
||||||
node_type = v.get("type", "connection")
|
if node_type == "folder":
|
||||||
if node_type == "folder":
|
folder_id = f"@{k}"
|
||||||
self.config._folder_add(folder=k)
|
has_children = any(
|
||||||
_traverse_import(v, current_folder=k, current_subfolder='')
|
isinstance(cv, dict) for ck, cv in v.items() if ck != "type"
|
||||||
elif node_type == "subfolder":
|
)
|
||||||
self.config._folder_add(folder=current_folder, subfolder=k)
|
if ctx_active and (
|
||||||
_traverse_import(v, current_folder=current_folder, current_subfolder=k)
|
not has_children
|
||||||
elif node_type == "connection":
|
or (
|
||||||
unique_id = k
|
not ctx_svc.matches_active_context(folder_id)
|
||||||
if current_subfolder:
|
and not self._folder_exists(folder_id, raw_folders)
|
||||||
unique_id = f"{k}@{current_subfolder}@{current_folder}"
|
)
|
||||||
elif current_folder:
|
):
|
||||||
unique_id = f"{k}@{current_folder}"
|
ctx_svc.validate_in_active_context(folder_id, is_folder=True)
|
||||||
self._validate_node_name(unique_id)
|
planned_ops.append(("folder", k, ""))
|
||||||
|
_plan_import(v, current_folder=k, current_subfolder="")
|
||||||
kwargs = deepcopy(v)
|
elif node_type == "subfolder":
|
||||||
kwargs['id'] = k
|
folder_id = f"@{k}@{current_folder}"
|
||||||
kwargs['folder'] = current_folder
|
has_children = any(
|
||||||
kwargs['subfolder'] = current_subfolder
|
isinstance(cv, dict) for ck, cv in v.items() if ck != "type"
|
||||||
|
)
|
||||||
self.config._connections_add(**kwargs)
|
if ctx_active and (
|
||||||
else:
|
not has_children
|
||||||
# Invalid format skip
|
or (
|
||||||
pass
|
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:
|
||||||
|
unique_id = f"{k}@{current_subfolder}@{current_folder}"
|
||||||
|
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)
|
||||||
|
|
||||||
_traverse_import(data)
|
kwargs = deepcopy(v)
|
||||||
self.config._saveconfig(self.config.file)</code></pre>
|
kwargs["id"] = k
|
||||||
|
kwargs["folder"] = current_folder
|
||||||
|
kwargs["subfolder"] = current_subfolder
|
||||||
|
planned_ops.append(("connection", 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:
|
||||||
|
cfg._connections_add_unlocked(**op[1])
|
||||||
|
|
||||||
|
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>
|
</details>
|
||||||
<div class="desc"><p>Import nodes/folders from a dictionary.</p></div>
|
<div class="desc"><p>Import nodes/folders from a dictionary atomically with context pre-validation.</p></div>
|
||||||
</dd>
|
</dd>
|
||||||
<dt id="connpy.services.import_export_service.ImportExportService.import_from_file"><code class="name flex">
|
<dt id="connpy.services.import_export_service.ImportExportService.import_from_file"><code class="name flex">
|
||||||
<span>def <span class="ident">import_from_file</span></span>(<span>self, file_path)</span>
|
<span>def <span class="ident">import_from_file</span></span>(<span>self, file_path)</span>
|
||||||
|
|||||||
+1805
-506
File diff suppressed because it is too large.
Load diff
File diff suppressed because it is too large.
Load diff
@@ -58,61 +58,189 @@ el.replaceWith(d);
|
|||||||
<pre><code class="python">class ProfileService(BaseService):
|
<pre><code class="python">class ProfileService(BaseService):
|
||||||
"""Business logic for node profiles management."""
|
"""Business logic for node profiles management."""
|
||||||
|
|
||||||
|
_ALLOWED_KEYS: ClassVar[frozenset[str]] = frozenset({
|
||||||
|
"host",
|
||||||
|
"options",
|
||||||
|
"logs",
|
||||||
|
"password",
|
||||||
|
"port",
|
||||||
|
"protocol",
|
||||||
|
"user",
|
||||||
|
"tags",
|
||||||
|
"jumphost",
|
||||||
|
"fallback",
|
||||||
|
})
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _validate_profile_payload(data):
|
||||||
|
from ..auth_utils import (
|
||||||
|
SanitizedAuthError,
|
||||||
|
is_compose_expression,
|
||||||
|
validate_auth_budget_tags,
|
||||||
|
validate_compose_syntax,
|
||||||
|
validate_fallback_entries,
|
||||||
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
|
if "tags" in data and data["tags"] not in ("", None):
|
||||||
|
validate_auth_budget_tags(data["tags"])
|
||||||
|
if "fallback" in data and data["fallback"] not in (None, "", []):
|
||||||
|
validate_fallback_entries(data["fallback"])
|
||||||
|
pw = data.get("password")
|
||||||
|
if is_compose_expression(pw):
|
||||||
|
validate_compose_syntax(pw)
|
||||||
|
elif isinstance(pw, (list, tuple)):
|
||||||
|
for item in pw:
|
||||||
|
if is_compose_expression(item):
|
||||||
|
validate_compose_syntax(item)
|
||||||
|
except (ValueError, TypeError, SanitizedAuthError) as exc:
|
||||||
|
raise InvalidConfigurationError(str(exc)) from None
|
||||||
|
|
||||||
|
def _get_profiles_snapshot(self):
|
||||||
|
lock = getattr(self.config, "_mutation_lock", None)
|
||||||
|
if lock is not None and not hasattr(self.config, "assert_called"):
|
||||||
|
with lock:
|
||||||
|
return deepcopy(self.config.profiles)
|
||||||
|
return deepcopy(self.config.profiles)
|
||||||
|
|
||||||
def list_profiles(self, filter_str=None):
|
def list_profiles(self, filter_str=None):
|
||||||
"""List all profile names, optionally filtered."""
|
"""List all profile names, optionally filtered."""
|
||||||
profiles = list(self.config.profiles.keys())
|
profiles = list(self._get_profiles_snapshot().keys())
|
||||||
case_sensitive = self.config.config.get("case", False)
|
case_sensitive = self.config.config.get("case", False)
|
||||||
|
|
||||||
if filter_str:
|
if filter_str:
|
||||||
if not case_sensitive:
|
if not case_sensitive:
|
||||||
f_str = filter_str.lower()
|
f_str = filter_str.lower()
|
||||||
return [p for p in profiles if f_str in p.lower()]
|
return [p for p in profiles if f_str in p.lower()]
|
||||||
else:
|
return [p for p in profiles if filter_str in p]
|
||||||
return [p for p in profiles if filter_str in p]
|
|
||||||
return profiles
|
return profiles
|
||||||
|
|
||||||
def get_profile(self, name, resolve=True):
|
def get_profile(self, name, resolve=True, *, _visited=frozenset(), _depth=0):
|
||||||
"""Get the profile dictionary, optionally resolved."""
|
"""Get the profile dictionary, optionally resolved."""
|
||||||
profile = self.config.profiles.get(name)
|
if _depth >= MAX_PROFILE_DEPTH or name in _visited:
|
||||||
|
raise InvalidConfigurationError(
|
||||||
|
f"Circular or overly deep profile reference involving '{name}'."
|
||||||
|
)
|
||||||
|
profiles = self._get_profiles_snapshot()
|
||||||
|
profile = profiles.get(name)
|
||||||
if not profile:
|
if not profile:
|
||||||
raise ProfileNotFoundError(f"Profile '{name}' not found.")
|
raise ProfileNotFoundError(f"Profile '{name}' not found.")
|
||||||
|
|
||||||
if resolve:
|
if resolve:
|
||||||
return self.resolve_node_data(profile)
|
return self.resolve_node_data(
|
||||||
|
profile,
|
||||||
|
_visited=_visited | {name},
|
||||||
|
_depth=_depth + 1,
|
||||||
|
_profiles_snapshot=profiles,
|
||||||
|
)
|
||||||
return profile
|
return profile
|
||||||
|
|
||||||
def add_profile(self, name, data):
|
def add_profile(self, name, data):
|
||||||
"""Add a new profile."""
|
"""Add a new profile."""
|
||||||
|
filtered_data = {k: deepcopy(v) for k, v in data.items() if k in self._ALLOWED_KEYS}
|
||||||
|
self._validate_profile_payload(filtered_data)
|
||||||
|
|
||||||
|
if _supports_transactional_update(self.config):
|
||||||
|
def _mut(cfg):
|
||||||
|
if name in cfg.profiles:
|
||||||
|
raise ProfileAlreadyExistsError(f"Profile '{name}' already exists.")
|
||||||
|
cfg._profiles_add_unlocked(id=name, **filtered_data)
|
||||||
|
|
||||||
|
self.config.transactional_update(_mut)
|
||||||
|
return
|
||||||
|
|
||||||
if name in self.config.profiles:
|
if name in self.config.profiles:
|
||||||
raise ProfileAlreadyExistsError(f"Profile '{name}' already exists.")
|
raise ProfileAlreadyExistsError(f"Profile '{name}' already exists.")
|
||||||
|
|
||||||
# Filter data to match _profiles_add signature and ensure id is passed
|
|
||||||
allowed_keys = {"host", "options", "logs", "password", "port", "protocol", "user", "tags", "jumphost"}
|
|
||||||
filtered_data = {k: v for k, v in data.items() if k in allowed_keys}
|
|
||||||
|
|
||||||
self.config._profiles_add(id=name, **filtered_data)
|
self.config._profiles_add(id=name, **filtered_data)
|
||||||
self.config._saveconfig(self.config.file)
|
self.config._saveconfig(self.config.file)
|
||||||
|
|
||||||
def resolve_node_data(self, node_data):
|
def resolve_node_data(
|
||||||
"""Resolve profile references (@profile) in node data and handle inheritance."""
|
self,
|
||||||
resolved = node_data.copy()
|
node_data,
|
||||||
|
*,
|
||||||
# 1. Identify all referenced profiles to support inheritance
|
_visited=frozenset(),
|
||||||
referenced_profiles = []
|
_depth=0,
|
||||||
for value in resolved.values():
|
_profiles_snapshot=None,
|
||||||
if isinstance(value, str) and value.startswith("@"):
|
):
|
||||||
referenced_profiles.append(value[1:])
|
"""Resolve profile references (@profile) in node data while preserving compose and scalar secrets."""
|
||||||
elif isinstance(value, list):
|
from ..auth_utils import is_compose_expression
|
||||||
|
|
||||||
|
if _depth > MAX_PROFILE_DEPTH:
|
||||||
|
raise InvalidConfigurationError("Profile inheritance exceeds maximum depth.")
|
||||||
|
|
||||||
|
profiles = (
|
||||||
|
_profiles_snapshot
|
||||||
|
if _profiles_snapshot is not None
|
||||||
|
else self._get_profiles_snapshot()
|
||||||
|
)
|
||||||
|
resolved = deepcopy(node_data)
|
||||||
|
|
||||||
|
auth_referenced_profiles = []
|
||||||
|
for key in ("user", "password"):
|
||||||
|
value = resolved.get(key)
|
||||||
|
if isinstance(value, str):
|
||||||
|
if not is_compose_expression(value) and value.startswith("@"):
|
||||||
|
ref = value[1:]
|
||||||
|
if key != "password" or ref in profiles:
|
||||||
|
auth_referenced_profiles.append(ref)
|
||||||
|
elif isinstance(value, (list, tuple)):
|
||||||
for item in value:
|
for item in value:
|
||||||
if isinstance(item, str) and item.startswith("@"):
|
if (
|
||||||
referenced_profiles.append(item[1:])
|
isinstance(item, str)
|
||||||
|
and not is_compose_expression(item)
|
||||||
# 2. Resolve explicit references
|
and item.startswith("@")
|
||||||
for key, value in resolved.items():
|
):
|
||||||
|
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
|
||||||
|
|
||||||
if isinstance(value, str) and value.startswith("@"):
|
if isinstance(value, str) and value.startswith("@"):
|
||||||
profile_name = value[1:]
|
profile_name = value[1:]
|
||||||
try:
|
try:
|
||||||
profile = self.get_profile(profile_name, resolve=True)
|
profile = _get_resolved_ref(profile_name)
|
||||||
resolved[key] = profile.get(key, "")
|
resolved[key] = profile.get(key, "")
|
||||||
except ProfileNotFoundError:
|
except ProfileNotFoundError:
|
||||||
resolved[key] = ""
|
resolved[key] = ""
|
||||||
@@ -121,68 +249,88 @@ el.replaceWith(d);
|
|||||||
for item in value:
|
for item in value:
|
||||||
if isinstance(item, str) and item.startswith("@"):
|
if isinstance(item, str) and item.startswith("@"):
|
||||||
profile_name = item[1:]
|
profile_name = item[1:]
|
||||||
try:
|
with suppress(ProfileNotFoundError):
|
||||||
profile = self.get_profile(profile_name, resolve=True)
|
profile = _get_resolved_ref(profile_name)
|
||||||
if "password" in profile:
|
if "password" in profile:
|
||||||
resolved_list.append(profile["password"])
|
resolved_list.append(profile["password"])
|
||||||
except ProfileNotFoundError:
|
|
||||||
pass
|
|
||||||
else:
|
else:
|
||||||
resolved_list.append(item)
|
resolved_list.append(item)
|
||||||
resolved[key] = resolved_list
|
resolved[key] = resolved_list
|
||||||
|
|
||||||
# 3. Inheritance: Fill empty keys from the first referenced profile
|
|
||||||
if referenced_profiles:
|
|
||||||
base_profile_name = referenced_profiles[0]
|
|
||||||
try:
|
|
||||||
base_profile = self.get_profile(base_profile_name, resolve=True)
|
|
||||||
for key, value in base_profile.items():
|
|
||||||
# Fill if key is missing or empty
|
|
||||||
if key not in resolved or resolved[key] == "" or resolved[key] == [] or resolved[key] is None:
|
|
||||||
resolved[key] = value
|
|
||||||
except ProfileNotFoundError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
# 4. Handle default protocol
|
dedup_auth_profiles = list(dict.fromkeys(auth_referenced_profiles))
|
||||||
if resolved.get("protocol") == "" or resolved.get("protocol") is None:
|
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)
|
||||||
|
|
||||||
|
if resolved.get("protocol") in ("", None):
|
||||||
try:
|
try:
|
||||||
default_profile = self.get_profile("default", resolve=True)
|
default_profile = _get_resolved_ref("default")
|
||||||
resolved["protocol"] = default_profile.get("protocol", "ssh")
|
resolved["protocol"] = default_profile.get("protocol", "ssh") or "ssh"
|
||||||
except ProfileNotFoundError:
|
except ProfileNotFoundError:
|
||||||
resolved["protocol"] = "ssh"
|
resolved["protocol"] = "ssh"
|
||||||
|
|
||||||
return resolved
|
return resolved
|
||||||
|
|
||||||
def delete_profile(self, name):
|
def delete_profile(self, name):
|
||||||
"""Delete an existing profile, with safety checks."""
|
"""Delete an existing profile, with safety checks."""
|
||||||
|
if _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:
|
if name not in self.config.profiles:
|
||||||
raise ProfileNotFoundError(f"Profile '{name}' not found.")
|
raise ProfileNotFoundError(f"Profile '{name}' not found.")
|
||||||
|
|
||||||
if name == "default":
|
if name == "default":
|
||||||
raise InvalidConfigurationError("Cannot delete the 'default' profile.")
|
raise InvalidConfigurationError("Cannot delete the 'default' profile.")
|
||||||
|
|
||||||
used_by = self.config._profileused(name)
|
used_by = self.config._profileused(name)
|
||||||
if used_by:
|
if used_by:
|
||||||
# We return the list of nodes using it so the UI can inform the user
|
raise InvalidConfigurationError(
|
||||||
raise InvalidConfigurationError(f"Profile '{name}' is used by nodes: {', '.join(used_by)}")
|
f"Profile '{name}' is used by nodes: {', '.join(used_by)}"
|
||||||
|
)
|
||||||
self.config._profiles_del(id=name)
|
self.config._profiles_del(id=name)
|
||||||
self.config._saveconfig(self.config.file)
|
self.config._saveconfig(self.config.file)
|
||||||
|
|
||||||
def update_profile(self, name, data):
|
def update_profile(self, name, data):
|
||||||
"""Update an existing profile."""
|
"""Update an existing profile."""
|
||||||
|
if _supports_transactional_update(self.config):
|
||||||
|
def _mut(cfg):
|
||||||
|
if name not in cfg.profiles:
|
||||||
|
raise ProfileNotFoundError(f"Profile '{name}' not found.")
|
||||||
|
updated_data = deepcopy(cfg.profiles[name])
|
||||||
|
updated_data.update(deepcopy(data))
|
||||||
|
filtered_data = {
|
||||||
|
k: v for k, v in updated_data.items() if k in self._ALLOWED_KEYS
|
||||||
|
}
|
||||||
|
self._validate_profile_payload(filtered_data)
|
||||||
|
cfg._profiles_add_unlocked(id=name, **filtered_data)
|
||||||
|
|
||||||
|
self.config.transactional_update(_mut)
|
||||||
|
return
|
||||||
|
|
||||||
if name not in self.config.profiles:
|
if name not in self.config.profiles:
|
||||||
raise ProfileNotFoundError(f"Profile '{name}' not found.")
|
raise ProfileNotFoundError(f"Profile '{name}' not found.")
|
||||||
|
|
||||||
# Merge with existing data
|
|
||||||
existing = self.get_profile(name, resolve=False)
|
existing = self.get_profile(name, resolve=False)
|
||||||
updated_data = existing.copy()
|
updated_data = deepcopy(existing)
|
||||||
updated_data.update(data)
|
updated_data.update(data)
|
||||||
|
filtered_data = {k: v for k, v in updated_data.items() if k in self._ALLOWED_KEYS}
|
||||||
# Filter data to match _profiles_add signature
|
self._validate_profile_payload(filtered_data)
|
||||||
allowed_keys = {"host", "options", "logs", "password", "port", "protocol", "user", "tags", "jumphost"}
|
|
||||||
filtered_data = {k: v for k, v in updated_data.items() if k in allowed_keys}
|
|
||||||
|
|
||||||
self.config._profiles_add(id=name, **filtered_data)
|
self.config._profiles_add(id=name, **filtered_data)
|
||||||
self.config._saveconfig(self.config.file)</code></pre>
|
self.config._saveconfig(self.config.file)</code></pre>
|
||||||
</details>
|
</details>
|
||||||
@@ -210,13 +358,20 @@ el.replaceWith(d);
|
|||||||
</summary>
|
</summary>
|
||||||
<pre><code class="python">def add_profile(self, name, data):
|
<pre><code class="python">def add_profile(self, name, data):
|
||||||
"""Add a new profile."""
|
"""Add a new profile."""
|
||||||
|
filtered_data = {k: deepcopy(v) for k, v in data.items() if k in self._ALLOWED_KEYS}
|
||||||
|
self._validate_profile_payload(filtered_data)
|
||||||
|
|
||||||
|
if _supports_transactional_update(self.config):
|
||||||
|
def _mut(cfg):
|
||||||
|
if name in cfg.profiles:
|
||||||
|
raise ProfileAlreadyExistsError(f"Profile '{name}' already exists.")
|
||||||
|
cfg._profiles_add_unlocked(id=name, **filtered_data)
|
||||||
|
|
||||||
|
self.config.transactional_update(_mut)
|
||||||
|
return
|
||||||
|
|
||||||
if name in self.config.profiles:
|
if name in self.config.profiles:
|
||||||
raise ProfileAlreadyExistsError(f"Profile '{name}' already exists.")
|
raise ProfileAlreadyExistsError(f"Profile '{name}' already exists.")
|
||||||
|
|
||||||
# Filter data to match _profiles_add signature and ensure id is passed
|
|
||||||
allowed_keys = {"host", "options", "logs", "password", "port", "protocol", "user", "tags", "jumphost"}
|
|
||||||
filtered_data = {k: v for k, v in data.items() if k in allowed_keys}
|
|
||||||
|
|
||||||
self.config._profiles_add(id=name, **filtered_data)
|
self.config._profiles_add(id=name, **filtered_data)
|
||||||
self.config._saveconfig(self.config.file)</code></pre>
|
self.config._saveconfig(self.config.file)</code></pre>
|
||||||
</details>
|
</details>
|
||||||
@@ -232,17 +387,31 @@ el.replaceWith(d);
|
|||||||
</summary>
|
</summary>
|
||||||
<pre><code class="python">def delete_profile(self, name):
|
<pre><code class="python">def delete_profile(self, name):
|
||||||
"""Delete an existing profile, with safety checks."""
|
"""Delete an existing profile, with safety checks."""
|
||||||
|
if _supports_transactional_update(self.config):
|
||||||
|
def _mut(cfg):
|
||||||
|
if name not in cfg.profiles:
|
||||||
|
raise ProfileNotFoundError(f"Profile '{name}' not found.")
|
||||||
|
if name == "default":
|
||||||
|
raise InvalidConfigurationError("Cannot delete the 'default' profile.")
|
||||||
|
used_by = cfg._profileused_unlocked(name)
|
||||||
|
if used_by:
|
||||||
|
raise InvalidConfigurationError(
|
||||||
|
f"Profile '{name}' is used by nodes: {', '.join(used_by)}"
|
||||||
|
)
|
||||||
|
cfg._profiles_del_unlocked(id=name)
|
||||||
|
|
||||||
|
self.config.transactional_update(_mut)
|
||||||
|
return
|
||||||
|
|
||||||
if name not in self.config.profiles:
|
if name not in self.config.profiles:
|
||||||
raise ProfileNotFoundError(f"Profile '{name}' not found.")
|
raise ProfileNotFoundError(f"Profile '{name}' not found.")
|
||||||
|
|
||||||
if name == "default":
|
if name == "default":
|
||||||
raise InvalidConfigurationError("Cannot delete the 'default' profile.")
|
raise InvalidConfigurationError("Cannot delete the 'default' profile.")
|
||||||
|
|
||||||
used_by = self.config._profileused(name)
|
used_by = self.config._profileused(name)
|
||||||
if used_by:
|
if used_by:
|
||||||
# We return the list of nodes using it so the UI can inform the user
|
raise InvalidConfigurationError(
|
||||||
raise InvalidConfigurationError(f"Profile '{name}' is used by nodes: {', '.join(used_by)}")
|
f"Profile '{name}' is used by nodes: {', '.join(used_by)}"
|
||||||
|
)
|
||||||
self.config._profiles_del(id=name)
|
self.config._profiles_del(id=name)
|
||||||
self.config._saveconfig(self.config.file)</code></pre>
|
self.config._saveconfig(self.config.file)</code></pre>
|
||||||
</details>
|
</details>
|
||||||
@@ -256,14 +425,24 @@ el.replaceWith(d);
|
|||||||
<summary>
|
<summary>
|
||||||
<span>Expand source code</span>
|
<span>Expand source code</span>
|
||||||
</summary>
|
</summary>
|
||||||
<pre><code class="python">def get_profile(self, name, resolve=True):
|
<pre><code class="python">def get_profile(self, name, resolve=True, *, _visited=frozenset(), _depth=0):
|
||||||
"""Get the profile dictionary, optionally resolved."""
|
"""Get the profile dictionary, optionally resolved."""
|
||||||
profile = self.config.profiles.get(name)
|
if _depth >= MAX_PROFILE_DEPTH or name in _visited:
|
||||||
|
raise InvalidConfigurationError(
|
||||||
|
f"Circular or overly deep profile reference involving '{name}'."
|
||||||
|
)
|
||||||
|
profiles = self._get_profiles_snapshot()
|
||||||
|
profile = profiles.get(name)
|
||||||
if not profile:
|
if not profile:
|
||||||
raise ProfileNotFoundError(f"Profile '{name}' not found.")
|
raise ProfileNotFoundError(f"Profile '{name}' not found.")
|
||||||
|
|
||||||
if resolve:
|
if resolve:
|
||||||
return self.resolve_node_data(profile)
|
return self.resolve_node_data(
|
||||||
|
profile,
|
||||||
|
_visited=_visited | {name},
|
||||||
|
_depth=_depth + 1,
|
||||||
|
_profiles_snapshot=profiles,
|
||||||
|
)
|
||||||
return profile</code></pre>
|
return profile</code></pre>
|
||||||
</details>
|
</details>
|
||||||
<div class="desc"><p>Get the profile dictionary, optionally resolved.</p></div>
|
<div class="desc"><p>Get the profile dictionary, optionally resolved.</p></div>
|
||||||
@@ -278,15 +457,14 @@ el.replaceWith(d);
|
|||||||
</summary>
|
</summary>
|
||||||
<pre><code class="python">def list_profiles(self, filter_str=None):
|
<pre><code class="python">def list_profiles(self, filter_str=None):
|
||||||
"""List all profile names, optionally filtered."""
|
"""List all profile names, optionally filtered."""
|
||||||
profiles = list(self.config.profiles.keys())
|
profiles = list(self._get_profiles_snapshot().keys())
|
||||||
case_sensitive = self.config.config.get("case", False)
|
case_sensitive = self.config.config.get("case", False)
|
||||||
|
|
||||||
if filter_str:
|
if filter_str:
|
||||||
if not case_sensitive:
|
if not case_sensitive:
|
||||||
f_str = filter_str.lower()
|
f_str = filter_str.lower()
|
||||||
return [p for p in profiles if f_str in p.lower()]
|
return [p for p in profiles if f_str in p.lower()]
|
||||||
else:
|
return [p for p in profiles if filter_str in p]
|
||||||
return [p for p in profiles if filter_str in p]
|
|
||||||
return profiles</code></pre>
|
return profiles</code></pre>
|
||||||
</details>
|
</details>
|
||||||
<div class="desc"><p>List all profile names, optionally filtered.</p></div>
|
<div class="desc"><p>List all profile names, optionally filtered.</p></div>
|
||||||
@@ -299,26 +477,93 @@ el.replaceWith(d);
|
|||||||
<summary>
|
<summary>
|
||||||
<span>Expand source code</span>
|
<span>Expand source code</span>
|
||||||
</summary>
|
</summary>
|
||||||
<pre><code class="python">def resolve_node_data(self, node_data):
|
<pre><code class="python">def resolve_node_data(
|
||||||
"""Resolve profile references (@profile) in node data and handle inheritance."""
|
self,
|
||||||
resolved = node_data.copy()
|
node_data,
|
||||||
|
*,
|
||||||
# 1. Identify all referenced profiles to support inheritance
|
_visited=frozenset(),
|
||||||
referenced_profiles = []
|
_depth=0,
|
||||||
for value in resolved.values():
|
_profiles_snapshot=None,
|
||||||
if isinstance(value, str) and value.startswith("@"):
|
):
|
||||||
referenced_profiles.append(value[1:])
|
"""Resolve profile references (@profile) in node data while preserving compose and scalar secrets."""
|
||||||
elif isinstance(value, list):
|
from ..auth_utils import is_compose_expression
|
||||||
|
|
||||||
|
if _depth > MAX_PROFILE_DEPTH:
|
||||||
|
raise InvalidConfigurationError("Profile inheritance exceeds maximum depth.")
|
||||||
|
|
||||||
|
profiles = (
|
||||||
|
_profiles_snapshot
|
||||||
|
if _profiles_snapshot is not None
|
||||||
|
else self._get_profiles_snapshot()
|
||||||
|
)
|
||||||
|
resolved = deepcopy(node_data)
|
||||||
|
|
||||||
|
auth_referenced_profiles = []
|
||||||
|
for key in ("user", "password"):
|
||||||
|
value = resolved.get(key)
|
||||||
|
if isinstance(value, str):
|
||||||
|
if not is_compose_expression(value) and value.startswith("@"):
|
||||||
|
ref = value[1:]
|
||||||
|
if key != "password" or ref in profiles:
|
||||||
|
auth_referenced_profiles.append(ref)
|
||||||
|
elif isinstance(value, (list, tuple)):
|
||||||
for item in value:
|
for item in value:
|
||||||
if isinstance(item, str) and item.startswith("@"):
|
if (
|
||||||
referenced_profiles.append(item[1:])
|
isinstance(item, str)
|
||||||
|
and not is_compose_expression(item)
|
||||||
# 2. Resolve explicit references
|
and item.startswith("@")
|
||||||
for key, value in resolved.items():
|
):
|
||||||
|
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
|
||||||
|
|
||||||
if isinstance(value, str) and value.startswith("@"):
|
if isinstance(value, str) and value.startswith("@"):
|
||||||
profile_name = value[1:]
|
profile_name = value[1:]
|
||||||
try:
|
try:
|
||||||
profile = self.get_profile(profile_name, resolve=True)
|
profile = _get_resolved_ref(profile_name)
|
||||||
resolved[key] = profile.get(key, "")
|
resolved[key] = profile.get(key, "")
|
||||||
except ProfileNotFoundError:
|
except ProfileNotFoundError:
|
||||||
resolved[key] = ""
|
resolved[key] = ""
|
||||||
@@ -327,39 +572,35 @@ el.replaceWith(d);
|
|||||||
for item in value:
|
for item in value:
|
||||||
if isinstance(item, str) and item.startswith("@"):
|
if isinstance(item, str) and item.startswith("@"):
|
||||||
profile_name = item[1:]
|
profile_name = item[1:]
|
||||||
try:
|
with suppress(ProfileNotFoundError):
|
||||||
profile = self.get_profile(profile_name, resolve=True)
|
profile = _get_resolved_ref(profile_name)
|
||||||
if "password" in profile:
|
if "password" in profile:
|
||||||
resolved_list.append(profile["password"])
|
resolved_list.append(profile["password"])
|
||||||
except ProfileNotFoundError:
|
|
||||||
pass
|
|
||||||
else:
|
else:
|
||||||
resolved_list.append(item)
|
resolved_list.append(item)
|
||||||
resolved[key] = resolved_list
|
resolved[key] = resolved_list
|
||||||
|
|
||||||
# 3. Inheritance: Fill empty keys from the first referenced profile
|
|
||||||
if referenced_profiles:
|
|
||||||
base_profile_name = referenced_profiles[0]
|
|
||||||
try:
|
|
||||||
base_profile = self.get_profile(base_profile_name, resolve=True)
|
|
||||||
for key, value in base_profile.items():
|
|
||||||
# Fill if key is missing or empty
|
|
||||||
if key not in resolved or resolved[key] == "" or resolved[key] == [] or resolved[key] is None:
|
|
||||||
resolved[key] = value
|
|
||||||
except ProfileNotFoundError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
# 4. Handle default protocol
|
dedup_auth_profiles = list(dict.fromkeys(auth_referenced_profiles))
|
||||||
if resolved.get("protocol") == "" or resolved.get("protocol") is None:
|
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)
|
||||||
|
|
||||||
|
if resolved.get("protocol") in ("", None):
|
||||||
try:
|
try:
|
||||||
default_profile = self.get_profile("default", resolve=True)
|
default_profile = _get_resolved_ref("default")
|
||||||
resolved["protocol"] = default_profile.get("protocol", "ssh")
|
resolved["protocol"] = default_profile.get("protocol", "ssh") or "ssh"
|
||||||
except ProfileNotFoundError:
|
except ProfileNotFoundError:
|
||||||
resolved["protocol"] = "ssh"
|
resolved["protocol"] = "ssh"
|
||||||
|
|
||||||
return resolved</code></pre>
|
return resolved</code></pre>
|
||||||
</details>
|
</details>
|
||||||
<div class="desc"><p>Resolve profile references (@profile) in node data and handle inheritance.</p></div>
|
<div class="desc"><p>Resolve profile references (@profile) in node data while preserving compose and scalar secrets.</p></div>
|
||||||
</dd>
|
</dd>
|
||||||
<dt id="connpy.services.profile_service.ProfileService.update_profile"><code class="name flex">
|
<dt id="connpy.services.profile_service.ProfileService.update_profile"><code class="name flex">
|
||||||
<span>def <span class="ident">update_profile</span></span>(<span>self, name, data)</span>
|
<span>def <span class="ident">update_profile</span></span>(<span>self, name, data)</span>
|
||||||
@@ -371,18 +612,28 @@ el.replaceWith(d);
|
|||||||
</summary>
|
</summary>
|
||||||
<pre><code class="python">def update_profile(self, name, data):
|
<pre><code class="python">def update_profile(self, name, data):
|
||||||
"""Update an existing profile."""
|
"""Update an existing profile."""
|
||||||
|
if _supports_transactional_update(self.config):
|
||||||
|
def _mut(cfg):
|
||||||
|
if name not in cfg.profiles:
|
||||||
|
raise ProfileNotFoundError(f"Profile '{name}' not found.")
|
||||||
|
updated_data = deepcopy(cfg.profiles[name])
|
||||||
|
updated_data.update(deepcopy(data))
|
||||||
|
filtered_data = {
|
||||||
|
k: v for k, v in updated_data.items() if k in self._ALLOWED_KEYS
|
||||||
|
}
|
||||||
|
self._validate_profile_payload(filtered_data)
|
||||||
|
cfg._profiles_add_unlocked(id=name, **filtered_data)
|
||||||
|
|
||||||
|
self.config.transactional_update(_mut)
|
||||||
|
return
|
||||||
|
|
||||||
if name not in self.config.profiles:
|
if name not in self.config.profiles:
|
||||||
raise ProfileNotFoundError(f"Profile '{name}' not found.")
|
raise ProfileNotFoundError(f"Profile '{name}' not found.")
|
||||||
|
|
||||||
# Merge with existing data
|
|
||||||
existing = self.get_profile(name, resolve=False)
|
existing = self.get_profile(name, resolve=False)
|
||||||
updated_data = existing.copy()
|
updated_data = deepcopy(existing)
|
||||||
updated_data.update(data)
|
updated_data.update(data)
|
||||||
|
filtered_data = {k: v for k, v in updated_data.items() if k in self._ALLOWED_KEYS}
|
||||||
# Filter data to match _profiles_add signature
|
self._validate_profile_payload(filtered_data)
|
||||||
allowed_keys = {"host", "options", "logs", "password", "port", "protocol", "user", "tags", "jumphost"}
|
|
||||||
filtered_data = {k: v for k, v in updated_data.items() if k in allowed_keys}
|
|
||||||
|
|
||||||
self.config._profiles_add(id=name, **filtered_data)
|
self.config._profiles_add(id=name, **filtered_data)
|
||||||
self.config._saveconfig(self.config.file)</code></pre>
|
self.config._saveconfig(self.config.file)</code></pre>
|
||||||
</details>
|
</details>
|
||||||
|
|||||||
@@ -65,7 +65,7 @@ el.replaceWith(d);
|
|||||||
</dd>
|
</dd>
|
||||||
<dt id="connpy.services.provider.ServiceProvider"><code class="flex name class">
|
<dt id="connpy.services.provider.ServiceProvider"><code class="flex name class">
|
||||||
<span>class <span class="ident">ServiceProvider</span></span>
|
<span>class <span class="ident">ServiceProvider</span></span>
|
||||||
<span>(</span><span>config, mode='local', remote_host=None)</span>
|
<span>(</span><span>config, mode='local', remote_host=None, enforce_context: bool = True)</span>
|
||||||
</code></dt>
|
</code></dt>
|
||||||
<dd>
|
<dd>
|
||||||
<details class="source">
|
<details class="source">
|
||||||
@@ -74,67 +74,92 @@ el.replaceWith(d);
|
|||||||
</summary>
|
</summary>
|
||||||
<pre><code class="python">class ServiceProvider:
|
<pre><code class="python">class ServiceProvider:
|
||||||
"""Dynamic service backend. Transparently provides local or remote services."""
|
"""Dynamic service backend. Transparently provides local or remote services."""
|
||||||
|
|
||||||
def __init__(self, config, mode="local", remote_host=None):
|
def __init__(self, config, mode="local", remote_host=None, enforce_context: bool = True):
|
||||||
|
from ..auth_utils import AuthAttemptTracker
|
||||||
|
|
||||||
self.mode = mode
|
self.mode = mode
|
||||||
self.config = config
|
self.config = config
|
||||||
self.remote_host = remote_host
|
self.remote_host = remote_host
|
||||||
|
self.enforce_context = bool(enforce_context)
|
||||||
|
self.auth_tracker = AuthAttemptTracker()
|
||||||
|
self.nodes = None
|
||||||
self._system = None
|
self._system = None
|
||||||
self._execution = None
|
self._execution = None
|
||||||
self._import_export = None
|
self._import_export = None
|
||||||
self._sync = None
|
self._sync = None
|
||||||
self._users = None
|
self._users = None
|
||||||
self._ai = None
|
self._ai = None
|
||||||
|
self.set_enforce_context(self.enforce_context)
|
||||||
|
|
||||||
if mode == "local":
|
if mode == "local":
|
||||||
self._init_local()
|
self._init_local()
|
||||||
elif mode == "remote":
|
elif mode == "remote":
|
||||||
self._init_remote()
|
self._init_remote()
|
||||||
else:
|
else:
|
||||||
raise ValueError(f"Unknown service mode: {mode}")
|
raise ValueError(f"Unknown service mode: {mode}")
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
def _init_local(self):
|
def _init_local(self):
|
||||||
from .node_service import NodeService
|
|
||||||
from .profile_service import ProfileService
|
|
||||||
from .config_service import ConfigService
|
from .config_service import ConfigService
|
||||||
from .plugin_service import PluginService
|
|
||||||
from .context_service import ContextService
|
from .context_service import ContextService
|
||||||
|
from .node_service import NodeService
|
||||||
self.nodes = NodeService(self.config)
|
from .plugin_service import PluginService
|
||||||
|
from .profile_service import ProfileService
|
||||||
|
|
||||||
|
self.nodes = NodeService(self.config, enforce_context=self.enforce_context)
|
||||||
self.profiles = ProfileService(self.config)
|
self.profiles = ProfileService(self.config)
|
||||||
self.config_svc = ConfigService(self.config)
|
self.config_svc = ConfigService(self.config)
|
||||||
self.plugins = PluginService(self.config)
|
self.plugins = PluginService(self.config)
|
||||||
self.context = ContextService(self.config)
|
self.context = ContextService(self.config)
|
||||||
|
|
||||||
def _init_remote(self):
|
def _init_remote(self):
|
||||||
# Allow ConfigService to work locally so the user can revert the mode
|
# Allow ConfigService to work locally so the user can revert the mode
|
||||||
from .config_service import ConfigService
|
from .config_service import ConfigService
|
||||||
from .context_service import ContextService
|
from .context_service import ContextService
|
||||||
|
|
||||||
self.config_svc = ConfigService(self.config)
|
self.config_svc = ConfigService(self.config)
|
||||||
self.context = ContextService(self.config)
|
self.context = ContextService(self.config)
|
||||||
|
|
||||||
if not self.remote_host:
|
if not self.remote_host:
|
||||||
raise InvalidConfigurationError("Remote host must be specified in remote mode")
|
raise InvalidConfigurationError("Remote host must be specified in remote mode")
|
||||||
|
|
||||||
import grpc
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
import grpc
|
||||||
|
|
||||||
from ..grpc_layer.stubs import (
|
from ..grpc_layer.stubs import (
|
||||||
NodeStub, ProfileStub, PluginStub, AIStub,
|
AIStub,
|
||||||
ExecutionStub, ImportExportStub, SystemStub,
|
AuthClientInterceptor,
|
||||||
ConfigStub, AuthClientInterceptor, AuthStub
|
AuthStub,
|
||||||
|
ConfigStub,
|
||||||
|
ExecutionStub,
|
||||||
|
ImportExportStub,
|
||||||
|
NodeStub,
|
||||||
|
PluginStub,
|
||||||
|
ProfileStub,
|
||||||
|
SystemStub,
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_token():
|
def get_token():
|
||||||
env_token = os.environ.get("CONNPY_TOKEN")
|
env_token = os.environ.get("CONNPY_TOKEN")
|
||||||
if env_token:
|
if env_token:
|
||||||
return env_token
|
return env_token
|
||||||
token_path = os.path.join(self.config.defaultdir, ".token")
|
token_path = os.path.join(self.config.defaultdir, ".token")
|
||||||
if os.path.exists(token_path):
|
if os.path.exists(token_path):
|
||||||
try:
|
with suppress(OSError, UnicodeDecodeError), open(token_path) as f:
|
||||||
with open(token_path, "r") as f:
|
return f.read().strip()
|
||||||
return f.read().strip()
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
channel = grpc.insecure_channel(self.remote_host)
|
channel = grpc.insecure_channel(self.remote_host)
|
||||||
@@ -151,7 +176,12 @@ el.replaceWith(d);
|
|||||||
self.plugins = PluginStub(channel, remote_host=self.remote_host)
|
self.plugins = PluginStub(channel, remote_host=self.remote_host)
|
||||||
self.ai = AIStub(channel, remote_host=self.remote_host)
|
self.ai = AIStub(channel, remote_host=self.remote_host)
|
||||||
self.system = SystemStub(channel, remote_host=self.remote_host)
|
self.system = SystemStub(channel, remote_host=self.remote_host)
|
||||||
self.execution = ExecutionStub(channel, remote_host=self.remote_host)
|
self.execution = ExecutionStub(
|
||||||
|
channel,
|
||||||
|
remote_host=self.remote_host,
|
||||||
|
config=self.config,
|
||||||
|
node_stub=self.nodes,
|
||||||
|
)
|
||||||
self.import_export = ImportExportStub(channel, remote_host=self.remote_host)
|
self.import_export = ImportExportStub(channel, remote_host=self.remote_host)
|
||||||
self.auth = AuthStub(channel, remote_host=self.remote_host)
|
self.auth = AuthStub(channel, remote_host=self.remote_host)
|
||||||
|
|
||||||
@@ -170,7 +200,13 @@ el.replaceWith(d);
|
|||||||
def execution(self):
|
def execution(self):
|
||||||
if self._execution is None and self.mode == "local":
|
if self._execution is None and self.mode == "local":
|
||||||
from .execution_service import ExecutionService
|
from .execution_service import ExecutionService
|
||||||
self._execution = ExecutionService(self.config)
|
params = inspect.signature(ExecutionService.__init__).parameters
|
||||||
|
kwargs = {}
|
||||||
|
if "attempt_tracker" in params:
|
||||||
|
kwargs["attempt_tracker"] = self.auth_tracker
|
||||||
|
if "enforce_context" in params:
|
||||||
|
kwargs["enforce_context"] = self.enforce_context
|
||||||
|
self._execution = ExecutionService(self.config, **kwargs)
|
||||||
return self._execution
|
return self._execution
|
||||||
|
|
||||||
@execution.setter
|
@execution.setter
|
||||||
@@ -181,7 +217,13 @@ el.replaceWith(d);
|
|||||||
def import_export(self):
|
def import_export(self):
|
||||||
if self._import_export is None and self.mode == "local":
|
if self._import_export is None and self.mode == "local":
|
||||||
from .import_export_service import ImportExportService
|
from .import_export_service import ImportExportService
|
||||||
self._import_export = ImportExportService(self.config)
|
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
|
return self._import_export
|
||||||
|
|
||||||
@import_export.setter
|
@import_export.setter
|
||||||
@@ -249,7 +291,13 @@ def ai(self):
|
|||||||
def execution(self):
|
def execution(self):
|
||||||
if self._execution is None and self.mode == "local":
|
if self._execution is None and self.mode == "local":
|
||||||
from .execution_service import ExecutionService
|
from .execution_service import ExecutionService
|
||||||
self._execution = ExecutionService(self.config)
|
params = inspect.signature(ExecutionService.__init__).parameters
|
||||||
|
kwargs = {}
|
||||||
|
if "attempt_tracker" in params:
|
||||||
|
kwargs["attempt_tracker"] = self.auth_tracker
|
||||||
|
if "enforce_context" in params:
|
||||||
|
kwargs["enforce_context"] = self.enforce_context
|
||||||
|
self._execution = ExecutionService(self.config, **kwargs)
|
||||||
return self._execution</code></pre>
|
return self._execution</code></pre>
|
||||||
</details>
|
</details>
|
||||||
<div class="desc"></div>
|
<div class="desc"></div>
|
||||||
@@ -264,7 +312,13 @@ def execution(self):
|
|||||||
def import_export(self):
|
def import_export(self):
|
||||||
if self._import_export is None and self.mode == "local":
|
if self._import_export is None and self.mode == "local":
|
||||||
from .import_export_service import ImportExportService
|
from .import_export_service import ImportExportService
|
||||||
self._import_export = ImportExportService(self.config)
|
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>
|
return self._import_export</code></pre>
|
||||||
</details>
|
</details>
|
||||||
<div class="desc"></div>
|
<div class="desc"></div>
|
||||||
@@ -315,6 +369,31 @@ def users(self):
|
|||||||
<div class="desc"></div>
|
<div class="desc"></div>
|
||||||
</dd>
|
</dd>
|
||||||
</dl>
|
</dl>
|
||||||
|
<h3>Methods</h3>
|
||||||
|
<dl>
|
||||||
|
<dt id="connpy.services.provider.ServiceProvider.set_enforce_context"><code class="name flex">
|
||||||
|
<span>def <span class="ident">set_enforce_context</span></span>(<span>self, enforce_context: bool) ‑> None</span>
|
||||||
|
</code></dt>
|
||||||
|
<dd>
|
||||||
|
<details class="source">
|
||||||
|
<summary>
|
||||||
|
<span>Expand source code</span>
|
||||||
|
</summary>
|
||||||
|
<pre><code class="python">def set_enforce_context(self, enforce_context: bool) -> 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>
|
</dd>
|
||||||
</dl>
|
</dl>
|
||||||
</section>
|
</section>
|
||||||
@@ -340,6 +419,7 @@ def users(self):
|
|||||||
<li><code><a title="connpy.services.provider.ServiceProvider.ai" href="#connpy.services.provider.ServiceProvider.ai">ai</a></code></li>
|
<li><code><a title="connpy.services.provider.ServiceProvider.ai" href="#connpy.services.provider.ServiceProvider.ai">ai</a></code></li>
|
||||||
<li><code><a title="connpy.services.provider.ServiceProvider.execution" href="#connpy.services.provider.ServiceProvider.execution">execution</a></code></li>
|
<li><code><a title="connpy.services.provider.ServiceProvider.execution" href="#connpy.services.provider.ServiceProvider.execution">execution</a></code></li>
|
||||||
<li><code><a title="connpy.services.provider.ServiceProvider.import_export" href="#connpy.services.provider.ServiceProvider.import_export">import_export</a></code></li>
|
<li><code><a title="connpy.services.provider.ServiceProvider.import_export" href="#connpy.services.provider.ServiceProvider.import_export">import_export</a></code></li>
|
||||||
|
<li><code><a title="connpy.services.provider.ServiceProvider.set_enforce_context" href="#connpy.services.provider.ServiceProvider.set_enforce_context">set_enforce_context</a></code></li>
|
||||||
<li><code><a title="connpy.services.provider.ServiceProvider.sync" href="#connpy.services.provider.ServiceProvider.sync">sync</a></code></li>
|
<li><code><a title="connpy.services.provider.ServiceProvider.sync" href="#connpy.services.provider.ServiceProvider.sync">sync</a></code></li>
|
||||||
<li><code><a title="connpy.services.provider.ServiceProvider.system" href="#connpy.services.provider.ServiceProvider.system">system</a></code></li>
|
<li><code><a title="connpy.services.provider.ServiceProvider.system" href="#connpy.services.provider.ServiceProvider.system">system</a></code></li>
|
||||||
<li><code><a title="connpy.services.provider.ServiceProvider.users" href="#connpy.services.provider.ServiceProvider.users">users</a></code></li>
|
<li><code><a title="connpy.services.provider.ServiceProvider.users" href="#connpy.services.provider.ServiceProvider.users">users</a></code></li>
|
||||||
|
|||||||
Reference in new issue
Block a user