chore(release): bump version to v6.5.0b1 and update documentation
This commit is contained in:
1 parent
c6f260efc9
commit
441bf54ac1
16 files changed
+7207
-1337
No files matched your search
@@ -3,7 +3,7 @@
|
||||
</p>
|
||||
|
||||
|
||||
# Connpy (v6.4.0b4)
|
||||
# Connpy (v6.5.0b1)
|
||||
[](https://pypi.org/pypi/connpy/)
|
||||
[](https://pypi.org/pypi/connpy/)
|
||||
[](https://pypi.org/pypi/connpy/)
|
||||
@@ -137,6 +137,12 @@ Customize connection settings dynamically using tags. Configure per-node setting
|
||||
tags: { "os": "cisco_ios", "prompt": ".*#", "screen_length_command": "terminal length 0" }
|
||||
```
|
||||
|
||||
### 3f. Credential Fallback Chain, `compose:` & Auth Budgets
|
||||
Configure automatic backup credentials (`fallback`) on nodes or `@profile` templates so both interactive (`conn <node>`) and unattended (`conn run` / MCP) sessions switch seamlessly when primary TACACS+/AAA authentication fails (opt-out via `tags.allow_fallback: false`).
|
||||
* **Fallback Formats**: Accepts `@profile`, explicit JSON list/dict entries (`[{"user": "backup", "password": "..."}]`), or `compose:` expressions. Nodes referencing multiple profiles inherit `fallback` with priority from `user`/`password` `@profile` references.
|
||||
* **Password Composition (`compose:`)**: Concatenate secrets and literals explicitly using `compose:@prof1+@prof2+"suffix"` (use `@@` to escape leading literal `@`).
|
||||
* **Anti-Lockout Guardrails (`conn config`)**: Enforce strict attempt bounds via `--max-auth-prompts`, `--max-total-auth-attempts`, `--max-hop-host-attempts`, `--max-global-user-attempts`, and `--disable-fallback-on-insecure-hostkey`.
|
||||
|
||||
|
||||
---
|
||||
|
||||
@@ -157,6 +163,9 @@ Support for single or chained intermediate gateway nodes (SSH, Mosh, SSM, kubect
|
||||
### 4c. Debug Mode, Keepalive, Logging
|
||||
Track connection steps (`conn --debug node`), set idle keepalive intervals (`conn config --keepalive <seconds>`), or define dynamic output log files using variables like `${unique}`, `${host}`, `${port}`, `${user}`, `${protocol}`, or `${date 'format'}`.
|
||||
|
||||
### 4d. Adaptive Multi-Hop Timeouts
|
||||
Differentiate `--connect-timeout` and `--auth-timeout` stages (`conn run` / `conn config`), automatically computing topological per-hop latency budgets for chained `SSM` and `Jumphost` tunnels with bounded progress extensions on slow handshakes.
|
||||
|
||||
|
||||
---
|
||||
|
||||
|
||||
+10
-1
@@ -5,7 +5,7 @@
|
||||
</p>
|
||||
|
||||
|
||||
# Connpy (v6.4.0b4)
|
||||
# Connpy (v6.5.0b1)
|
||||
[](https://pypi.org/pypi/connpy/)
|
||||
[](https://pypi.org/pypi/connpy/)
|
||||
[](https://pypi.org/pypi/connpy/)
|
||||
@@ -139,6 +139,12 @@ Customize connection settings dynamically using tags. Configure per-node setting
|
||||
tags: { "os": "cisco_ios", "prompt": ".*#", "screen_length_command": "terminal length 0" }
|
||||
```
|
||||
|
||||
### 3f. Credential Fallback Chain, `compose:` & Auth Budgets
|
||||
Configure automatic backup credentials (`fallback`) on nodes or `@profile` templates so both interactive (`conn <node>`) and unattended (`conn run` / MCP) sessions switch seamlessly when primary TACACS+/AAA authentication fails (opt-out via `tags.allow_fallback: false`).
|
||||
* **Fallback Formats**: Accepts `@profile`, explicit JSON list/dict entries (`[{"user": "backup", "password": "..."}]`), or `compose:` expressions. Nodes referencing multiple profiles inherit `fallback` with priority from `user`/`password` `@profile` references.
|
||||
* **Password Composition (`compose:`)**: Concatenate secrets and literals explicitly using `compose:@prof1+@prof2+"suffix"` (use `@@` to escape leading literal `@`).
|
||||
* **Anti-Lockout Guardrails (`conn config`)**: Enforce strict attempt bounds via `--max-auth-prompts`, `--max-total-auth-attempts`, `--max-hop-host-attempts`, `--max-global-user-attempts`, and `--disable-fallback-on-insecure-hostkey`.
|
||||
|
||||
|
||||
---
|
||||
|
||||
@@ -159,6 +165,9 @@ Support for single or chained intermediate gateway nodes (SSH, Mosh, SSM, kubect
|
||||
### 4c. Debug Mode, Keepalive, Logging
|
||||
Track connection steps (`conn --debug node`), set idle keepalive intervals (`conn config --keepalive <seconds>`), or define dynamic output log files using variables like `${unique}`, `${host}`, `${port}`, `${user}`, `${protocol}`, or `${date 'format'}`.
|
||||
|
||||
### 4d. Adaptive Multi-Hop Timeouts
|
||||
Differentiate `--connect-timeout` and `--auth-timeout` stages (`conn run` / `conn config`), automatically computing topological per-hop latency budgets for chained `SSM` and `Jumphost` tunnels with bounded progress extensions on slow handshakes.
|
||||
|
||||
|
||||
---
|
||||
|
||||
|
||||
+1
-1
@@ -1 +1 @@
|
||||
__version__ = "6.4.0b4"
|
||||
__version__ = "6.5.0b1"
|
||||
File diff suppressed because it is too large.
Load diff
@@ -80,7 +80,12 @@ el.replaceWith(d);
|
||||
"sync_remote": self.set_sync_remote,
|
||||
"shell_command": self.set_shell_config,
|
||||
"shell_prompt": self.set_shell_config,
|
||||
"shell_os": self.set_shell_config
|
||||
"shell_os": self.set_shell_config,
|
||||
"max_auth_prompts": self.set_auth_budget,
|
||||
"max_total_auth_attempts": self.set_auth_budget,
|
||||
"max_hop_host_attempts": self.set_auth_budget,
|
||||
"max_global_user_attempts": self.set_auth_budget,
|
||||
"disable_fallback_on_insecure_hostkey": self.set_disable_fallback_on_insecure_hostkey,
|
||||
}
|
||||
handler = actions.get(getattr(args, "command", None))
|
||||
if handler:
|
||||
@@ -251,7 +256,71 @@ el.replaceWith(d);
|
||||
self.app.services.config_svc.update_setting("shell", shell_cfg)
|
||||
printer.success("Config saved")
|
||||
except (ConnpyError, InvalidConfigurationError) as e:
|
||||
printer.error(str(e))</code></pre>
|
||||
printer.error(str(e))
|
||||
|
||||
def set_auth_budget(self, args):
|
||||
key = args.command
|
||||
raw_val = args.data[0] if isinstance(args.data, list) else args.data
|
||||
bounds_map = {
|
||||
"max_auth_prompts": (MIN_AUTH_PROMPTS, MAX_AUTH_PROMPTS_CAP),
|
||||
"max_total_auth_attempts": (MIN_TOTAL_AUTH_ATTEMPTS, MAX_TOTAL_AUTH_ATTEMPTS_CAP),
|
||||
"max_hop_host_attempts": (MIN_HOP_HOST_ATTEMPTS, MAX_HOP_HOST_ATTEMPTS_CAP),
|
||||
"max_global_user_attempts": (1, 1000),
|
||||
}
|
||||
if key not in bounds_map:
|
||||
raise InvalidConfigurationError(f"Unknown auth budget setting: {key}")
|
||||
|
||||
if isinstance(raw_val, bool):
|
||||
msg = f"Invalid {key}: boolean is not permitted"
|
||||
printer.error(msg)
|
||||
raise InvalidConfigurationError(msg)
|
||||
|
||||
if isinstance(raw_val, int):
|
||||
val = raw_val
|
||||
elif isinstance(raw_val, str) and re.fullmatch(r"[+-]?\d+", raw_val.strip()):
|
||||
val = int(raw_val.strip())
|
||||
else:
|
||||
msg = f"Invalid {key}: expected integer, got {raw_val!r}"
|
||||
printer.error(msg)
|
||||
raise InvalidConfigurationError(msg)
|
||||
|
||||
if val == 0:
|
||||
cfg_obj = self.app.config
|
||||
if not hasattr(cfg_obj, "assert_called") and callable(
|
||||
getattr(cfg_obj, "transactional_update", None)
|
||||
):
|
||||
cfg_obj.transactional_update(lambda c: c.config.pop(key, None))
|
||||
else:
|
||||
cfg_obj.config.pop(key, None)
|
||||
cfg_obj._saveconfig(cfg_obj.file)
|
||||
printer.success("Config saved")
|
||||
return
|
||||
|
||||
min_v, max_v = bounds_map[key]
|
||||
try:
|
||||
validate_strict_int_range(val, key, min_v, max_v)
|
||||
except ValueError as exc:
|
||||
printer.error(str(exc))
|
||||
raise InvalidConfigurationError(str(exc)) from None
|
||||
|
||||
self.app.services.config_svc.update_setting(key, val)
|
||||
printer.success("Config saved")
|
||||
|
||||
def set_disable_fallback_on_insecure_hostkey(self, args):
|
||||
raw_val = args.data[0] if isinstance(args.data, list) else args.data
|
||||
if isinstance(raw_val, bool):
|
||||
val = raw_val
|
||||
elif isinstance(raw_val, str) and raw_val.strip().lower() in ("true", "false"):
|
||||
val = raw_val.strip().lower() == "true"
|
||||
else:
|
||||
msg = "Invalid disable_fallback_on_insecure_hostkey: expected 'true' or 'false'"
|
||||
printer.error(msg)
|
||||
raise InvalidConfigurationError(msg)
|
||||
self.app.services.config_svc.update_setting(
|
||||
"disable_fallback_on_insecure_hostkey",
|
||||
val,
|
||||
)
|
||||
printer.success("Config saved")</code></pre>
|
||||
</details>
|
||||
<div class="desc"></div>
|
||||
<h3>Methods</h3>
|
||||
@@ -285,7 +354,12 @@ el.replaceWith(d);
|
||||
"sync_remote": self.set_sync_remote,
|
||||
"shell_command": self.set_shell_config,
|
||||
"shell_prompt": self.set_shell_config,
|
||||
"shell_os": self.set_shell_config
|
||||
"shell_os": self.set_shell_config,
|
||||
"max_auth_prompts": self.set_auth_budget,
|
||||
"max_total_auth_attempts": self.set_auth_budget,
|
||||
"max_hop_host_attempts": self.set_auth_budget,
|
||||
"max_global_user_attempts": self.set_auth_budget,
|
||||
"disable_fallback_on_insecure_hostkey": self.set_disable_fallback_on_insecure_hostkey,
|
||||
}
|
||||
handler = actions.get(getattr(args, "command", None))
|
||||
if handler:
|
||||
@@ -333,6 +407,64 @@ el.replaceWith(d);
|
||||
</details>
|
||||
<div class="desc"></div>
|
||||
</dd>
|
||||
<dt id="connpy.cli.config_handler.ConfigHandler.set_auth_budget"><code class="name flex">
|
||||
<span>def <span class="ident">set_auth_budget</span></span>(<span>self, args)</span>
|
||||
</code></dt>
|
||||
<dd>
|
||||
<details class="source">
|
||||
<summary>
|
||||
<span>Expand source code</span>
|
||||
</summary>
|
||||
<pre><code class="python">def set_auth_budget(self, args):
|
||||
key = args.command
|
||||
raw_val = args.data[0] if isinstance(args.data, list) else args.data
|
||||
bounds_map = {
|
||||
"max_auth_prompts": (MIN_AUTH_PROMPTS, MAX_AUTH_PROMPTS_CAP),
|
||||
"max_total_auth_attempts": (MIN_TOTAL_AUTH_ATTEMPTS, MAX_TOTAL_AUTH_ATTEMPTS_CAP),
|
||||
"max_hop_host_attempts": (MIN_HOP_HOST_ATTEMPTS, MAX_HOP_HOST_ATTEMPTS_CAP),
|
||||
"max_global_user_attempts": (1, 1000),
|
||||
}
|
||||
if key not in bounds_map:
|
||||
raise InvalidConfigurationError(f"Unknown auth budget setting: {key}")
|
||||
|
||||
if isinstance(raw_val, bool):
|
||||
msg = f"Invalid {key}: boolean is not permitted"
|
||||
printer.error(msg)
|
||||
raise InvalidConfigurationError(msg)
|
||||
|
||||
if isinstance(raw_val, int):
|
||||
val = raw_val
|
||||
elif isinstance(raw_val, str) and re.fullmatch(r"[+-]?\d+", raw_val.strip()):
|
||||
val = int(raw_val.strip())
|
||||
else:
|
||||
msg = f"Invalid {key}: expected integer, got {raw_val!r}"
|
||||
printer.error(msg)
|
||||
raise InvalidConfigurationError(msg)
|
||||
|
||||
if val == 0:
|
||||
cfg_obj = self.app.config
|
||||
if not hasattr(cfg_obj, "assert_called") and callable(
|
||||
getattr(cfg_obj, "transactional_update", None)
|
||||
):
|
||||
cfg_obj.transactional_update(lambda c: c.config.pop(key, None))
|
||||
else:
|
||||
cfg_obj.config.pop(key, None)
|
||||
cfg_obj._saveconfig(cfg_obj.file)
|
||||
printer.success("Config saved")
|
||||
return
|
||||
|
||||
min_v, max_v = bounds_map[key]
|
||||
try:
|
||||
validate_strict_int_range(val, key, min_v, max_v)
|
||||
except ValueError as exc:
|
||||
printer.error(str(exc))
|
||||
raise InvalidConfigurationError(str(exc)) from None
|
||||
|
||||
self.app.services.config_svc.update_setting(key, val)
|
||||
printer.success("Config saved")</code></pre>
|
||||
</details>
|
||||
<div class="desc"></div>
|
||||
</dd>
|
||||
<dt id="connpy.cli.config_handler.ConfigHandler.set_case"><code class="name flex">
|
||||
<span>def <span class="ident">set_case</span></span>(<span>self, args)</span>
|
||||
</code></dt>
|
||||
@@ -367,6 +499,32 @@ el.replaceWith(d);
|
||||
</details>
|
||||
<div class="desc"></div>
|
||||
</dd>
|
||||
<dt id="connpy.cli.config_handler.ConfigHandler.set_disable_fallback_on_insecure_hostkey"><code class="name flex">
|
||||
<span>def <span class="ident">set_disable_fallback_on_insecure_hostkey</span></span>(<span>self, args)</span>
|
||||
</code></dt>
|
||||
<dd>
|
||||
<details class="source">
|
||||
<summary>
|
||||
<span>Expand source code</span>
|
||||
</summary>
|
||||
<pre><code class="python">def set_disable_fallback_on_insecure_hostkey(self, args):
|
||||
raw_val = args.data[0] if isinstance(args.data, list) else args.data
|
||||
if isinstance(raw_val, bool):
|
||||
val = raw_val
|
||||
elif isinstance(raw_val, str) and raw_val.strip().lower() in ("true", "false"):
|
||||
val = raw_val.strip().lower() == "true"
|
||||
else:
|
||||
msg = "Invalid disable_fallback_on_insecure_hostkey: expected 'true' or 'false'"
|
||||
printer.error(msg)
|
||||
raise InvalidConfigurationError(msg)
|
||||
self.app.services.config_svc.update_setting(
|
||||
"disable_fallback_on_insecure_hostkey",
|
||||
val,
|
||||
)
|
||||
printer.success("Config saved")</code></pre>
|
||||
</details>
|
||||
<div class="desc"></div>
|
||||
</dd>
|
||||
<dt id="connpy.cli.config_handler.ConfigHandler.set_fzf"><code class="name flex">
|
||||
<span>def <span class="ident">set_fzf</span></span>(<span>self, args)</span>
|
||||
</code></dt>
|
||||
@@ -577,11 +735,13 @@ el.replaceWith(d);
|
||||
<ul>
|
||||
<li>
|
||||
<h4><code><a title="connpy.cli.config_handler.ConfigHandler" href="#connpy.cli.config_handler.ConfigHandler">ConfigHandler</a></code></h4>
|
||||
<ul class="two-column">
|
||||
<ul class="">
|
||||
<li><code><a title="connpy.cli.config_handler.ConfigHandler.dispatch" href="#connpy.cli.config_handler.ConfigHandler.dispatch">dispatch</a></code></li>
|
||||
<li><code><a title="connpy.cli.config_handler.ConfigHandler.set_ai_config" href="#connpy.cli.config_handler.ConfigHandler.set_ai_config">set_ai_config</a></code></li>
|
||||
<li><code><a title="connpy.cli.config_handler.ConfigHandler.set_auth_budget" href="#connpy.cli.config_handler.ConfigHandler.set_auth_budget">set_auth_budget</a></code></li>
|
||||
<li><code><a title="connpy.cli.config_handler.ConfigHandler.set_case" href="#connpy.cli.config_handler.ConfigHandler.set_case">set_case</a></code></li>
|
||||
<li><code><a title="connpy.cli.config_handler.ConfigHandler.set_configfolder" href="#connpy.cli.config_handler.ConfigHandler.set_configfolder">set_configfolder</a></code></li>
|
||||
<li><code><a title="connpy.cli.config_handler.ConfigHandler.set_disable_fallback_on_insecure_hostkey" href="#connpy.cli.config_handler.ConfigHandler.set_disable_fallback_on_insecure_hostkey">set_disable_fallback_on_insecure_hostkey</a></code></li>
|
||||
<li><code><a title="connpy.cli.config_handler.ConfigHandler.set_fzf" href="#connpy.cli.config_handler.ConfigHandler.set_fzf">set_fzf</a></code></li>
|
||||
<li><code><a title="connpy.cli.config_handler.ConfigHandler.set_idletime" href="#connpy.cli.config_handler.ConfigHandler.set_idletime">set_idletime</a></code></li>
|
||||
<li><code><a title="connpy.cli.config_handler.ConfigHandler.set_remote_host" href="#connpy.cli.config_handler.ConfigHandler.set_remote_host">set_remote_host</a></code></li>
|
||||
|
||||
+122
-30
@@ -72,6 +72,7 @@ el.replaceWith(d);
|
||||
questions.append(inquirer.Confirm("jumphost", message="Edit jumphost?"))
|
||||
questions.append(inquirer.Confirm("user", message="Edit User?"))
|
||||
questions.append(inquirer.Confirm("password", message="Edit password?"))
|
||||
questions.append(inquirer.Confirm("fallback", message="Edit fallback credentials?"))
|
||||
return inquirer.prompt(questions)
|
||||
|
||||
def questions_nodes(self, unique, uniques=None, edit=None):
|
||||
@@ -82,11 +83,13 @@ el.replaceWith(d);
|
||||
defaults["tags"] = ""
|
||||
if "jumphost" not in defaults:
|
||||
defaults["jumphost"] = ""
|
||||
except Exception:
|
||||
defaults = {"host": "", "protocol": "", "port": "", "user": "", "options": "", "logs": "", "tags": "", "password": "", "jumphost": ""}
|
||||
if "fallback" not in defaults:
|
||||
defaults["fallback"] = ""
|
||||
except Exception: # noqa: BLE001
|
||||
defaults = {"host": "", "protocol": "", "port": "", "user": "", "options": "", "logs": "", "tags": "", "password": "", "jumphost": "", "fallback": ""}
|
||||
node = {}
|
||||
if edit is None:
|
||||
edit = {"host": True, "protocol": True, "port": True, "user": True, "password": True, "options": True, "logs": True, "tags": True, "jumphost": True}
|
||||
edit = {"host": True, "protocol": True, "port": True, "user": True, "password": True, "options": True, "logs": True, "tags": True, "jumphost": True, "fallback": True}
|
||||
questions = []
|
||||
if edit["host"]:
|
||||
questions.append(inquirer.Text("host", message="Add Hostname or IP", validate=self.validators.host_validation, default=defaults["host"]))
|
||||
@@ -124,6 +127,8 @@ el.replaceWith(d);
|
||||
questions.append(inquirer.List("password", message="Password: Use a local password, no password or a list of profiles to reference?", choices=["Local Password", "Profiles", "No Password"]))
|
||||
else:
|
||||
node["password"] = defaults["password"]
|
||||
if not edit.get("fallback"):
|
||||
node["fallback"] = defaults.get("fallback", "")
|
||||
|
||||
answer = inquirer.prompt(questions)
|
||||
if answer is None:
|
||||
@@ -141,12 +146,28 @@ el.replaceWith(d);
|
||||
passa = inquirer.prompt(passq)
|
||||
if passa is None:
|
||||
return False
|
||||
answer["password"] = passa["password"].split(",")
|
||||
answer["password"] = [p.strip() for p in passa["password"].split(",") if p.strip()]
|
||||
elif answer["password"] == "No Password":
|
||||
answer["password"] = ""
|
||||
|
||||
if "tags" in answer and not answer["tags"].startswith("@") and answer["tags"]:
|
||||
answer["tags"] = ast.literal_eval(answer["tags"])
|
||||
if edit.get("fallback"):
|
||||
raw_def_fb = defaults.get("fallback", "")
|
||||
def_fb_str = json.dumps(raw_def_fb) if isinstance(raw_def_fb, (list, dict)) else str(raw_def_fb or "")
|
||||
fbq = [
|
||||
inquirer.Text(
|
||||
"fallback",
|
||||
message="Add fallback credentials (JSON list/dict, @profile, or compose:)",
|
||||
validate=self.validators.fallback_validation,
|
||||
default=def_fb_str.replace("{", "{{").replace("}", "}}"),
|
||||
)
|
||||
]
|
||||
fba = inquirer.prompt(fbq)
|
||||
if fba is None:
|
||||
return False
|
||||
answer["fallback"] = _parse_fallback_input(fba["fallback"])
|
||||
|
||||
if "tags" in answer and answer["tags"] and not str(answer["tags"]).startswith("@"):
|
||||
answer["tags"] = _parse_tags_input(answer["tags"])
|
||||
|
||||
result = {**uniques, **answer, **node}
|
||||
result["type"] = "connection"
|
||||
@@ -160,11 +181,13 @@ el.replaceWith(d);
|
||||
defaults["tags"] = ""
|
||||
if "jumphost" not in defaults:
|
||||
defaults["jumphost"] = ""
|
||||
except Exception:
|
||||
defaults = {"host": "", "protocol": "", "port": "", "user": "", "options": "", "logs": "", "tags": "", "jumphost": ""}
|
||||
if "fallback" not in defaults:
|
||||
defaults["fallback"] = ""
|
||||
except Exception: # noqa: BLE001
|
||||
defaults = {"host": "", "protocol": "", "port": "", "user": "", "options": "", "logs": "", "tags": "", "jumphost": "", "fallback": ""}
|
||||
profile = {}
|
||||
if edit is None:
|
||||
edit = {"host": True, "protocol": True, "port": True, "user": True, "password": True, "options": True, "logs": True, "tags": True, "jumphost": True}
|
||||
edit = {"host": True, "protocol": True, "port": True, "user": True, "password": True, "options": True, "logs": True, "tags": True, "jumphost": True, "fallback": True}
|
||||
questions = []
|
||||
if edit["host"]:
|
||||
questions.append(inquirer.Text("host", message="Add Hostname or IP", default=defaults["host"]))
|
||||
@@ -202,17 +225,24 @@ el.replaceWith(d);
|
||||
questions.append(inquirer.Password("password", message="Set Password"))
|
||||
else:
|
||||
profile["password"] = defaults["password"]
|
||||
if edit.get("fallback"):
|
||||
raw_def_fb = defaults.get("fallback", "")
|
||||
def_fb_str = json.dumps(raw_def_fb) if isinstance(raw_def_fb, (list, dict)) else str(raw_def_fb or "")
|
||||
questions.append(inquirer.Text("fallback", message="Add fallback credentials (JSON list/dict, @profile, or compose:)", validate=self.validators.fallback_validation, default=def_fb_str.replace("{", "{{").replace("}", "}}")))
|
||||
else:
|
||||
profile["fallback"] = defaults.get("fallback", "")
|
||||
|
||||
answer = inquirer.prompt(questions)
|
||||
if answer is None:
|
||||
return False
|
||||
|
||||
if "password" in answer:
|
||||
if answer["password"] != "":
|
||||
if "password" in answer and answer["password"] != "":
|
||||
answer["password"] = self.app.services.config_svc.encrypt_password(answer["password"])
|
||||
|
||||
if "tags" in answer and answer["tags"]:
|
||||
answer["tags"] = ast.literal_eval(answer["tags"])
|
||||
answer["tags"] = _parse_tags_input(answer["tags"])
|
||||
if "fallback" in answer:
|
||||
answer["fallback"] = _parse_fallback_input(answer["fallback"])
|
||||
|
||||
result = {**answer, **profile}
|
||||
result["id"] = unique
|
||||
@@ -241,17 +271,33 @@ el.replaceWith(d);
|
||||
if answer["password"] == "Local Password":
|
||||
passq = [inquirer.Password("password", message="Set Password")]
|
||||
passa = inquirer.prompt(passq)
|
||||
if passa is None:
|
||||
return False
|
||||
answer["password"] = self.app.services.config_svc.encrypt_password(passa["password"])
|
||||
elif answer["password"] == "Profiles":
|
||||
passq = [(inquirer.Text("password", message="Set a @profile or a comma separated list of @profiles", validate=self.validators.pass_validation))]
|
||||
passa = inquirer.prompt(passq)
|
||||
answer["password"] = passa["password"].split(",")
|
||||
if passa is None:
|
||||
return False
|
||||
answer["password"] = [p.strip() for p in passa["password"].split(",") if p.strip()]
|
||||
elif answer["password"] == "No Password":
|
||||
answer["password"] = ""
|
||||
|
||||
fbq = [
|
||||
inquirer.Text(
|
||||
"fallback",
|
||||
message="Add fallback credentials (JSON list/dict, @profile, or compose:)",
|
||||
validate=self.validators.fallback_validation,
|
||||
)
|
||||
]
|
||||
fba = inquirer.prompt(fbq)
|
||||
if fba is None:
|
||||
return False
|
||||
answer["fallback"] = _parse_fallback_input(fba["fallback"])
|
||||
|
||||
answer["type"] = "connection"
|
||||
if "tags" in answer and not answer["tags"].startswith("@") and answer["tags"]:
|
||||
answer["tags"] = ast.literal_eval(answer["tags"])
|
||||
if "tags" in answer and answer["tags"] and not str(answer["tags"]).startswith("@"):
|
||||
answer["tags"] = _parse_tags_input(answer["tags"])
|
||||
|
||||
return answer
|
||||
|
||||
@@ -463,17 +509,33 @@ el.replaceWith(d);
|
||||
if answer["password"] == "Local Password":
|
||||
passq = [inquirer.Password("password", message="Set Password")]
|
||||
passa = inquirer.prompt(passq)
|
||||
if passa is None:
|
||||
return False
|
||||
answer["password"] = self.app.services.config_svc.encrypt_password(passa["password"])
|
||||
elif answer["password"] == "Profiles":
|
||||
passq = [(inquirer.Text("password", message="Set a @profile or a comma separated list of @profiles", validate=self.validators.pass_validation))]
|
||||
passa = inquirer.prompt(passq)
|
||||
answer["password"] = passa["password"].split(",")
|
||||
if passa is None:
|
||||
return False
|
||||
answer["password"] = [p.strip() for p in passa["password"].split(",") if p.strip()]
|
||||
elif answer["password"] == "No Password":
|
||||
answer["password"] = ""
|
||||
|
||||
fbq = [
|
||||
inquirer.Text(
|
||||
"fallback",
|
||||
message="Add fallback credentials (JSON list/dict, @profile, or compose:)",
|
||||
validate=self.validators.fallback_validation,
|
||||
)
|
||||
]
|
||||
fba = inquirer.prompt(fbq)
|
||||
if fba is None:
|
||||
return False
|
||||
answer["fallback"] = _parse_fallback_input(fba["fallback"])
|
||||
|
||||
answer["type"] = "connection"
|
||||
if "tags" in answer and not answer["tags"].startswith("@") and answer["tags"]:
|
||||
answer["tags"] = ast.literal_eval(answer["tags"])
|
||||
if "tags" in answer and answer["tags"] and not str(answer["tags"]).startswith("@"):
|
||||
answer["tags"] = _parse_tags_input(answer["tags"])
|
||||
|
||||
return answer</code></pre>
|
||||
</details>
|
||||
@@ -499,6 +561,7 @@ el.replaceWith(d);
|
||||
questions.append(inquirer.Confirm("jumphost", message="Edit jumphost?"))
|
||||
questions.append(inquirer.Confirm("user", message="Edit User?"))
|
||||
questions.append(inquirer.Confirm("password", message="Edit password?"))
|
||||
questions.append(inquirer.Confirm("fallback", message="Edit fallback credentials?"))
|
||||
return inquirer.prompt(questions)</code></pre>
|
||||
</details>
|
||||
<div class="desc"></div>
|
||||
@@ -519,11 +582,13 @@ el.replaceWith(d);
|
||||
defaults["tags"] = ""
|
||||
if "jumphost" not in defaults:
|
||||
defaults["jumphost"] = ""
|
||||
except Exception:
|
||||
defaults = {"host": "", "protocol": "", "port": "", "user": "", "options": "", "logs": "", "tags": "", "password": "", "jumphost": ""}
|
||||
if "fallback" not in defaults:
|
||||
defaults["fallback"] = ""
|
||||
except Exception: # noqa: BLE001
|
||||
defaults = {"host": "", "protocol": "", "port": "", "user": "", "options": "", "logs": "", "tags": "", "password": "", "jumphost": "", "fallback": ""}
|
||||
node = {}
|
||||
if edit is None:
|
||||
edit = {"host": True, "protocol": True, "port": True, "user": True, "password": True, "options": True, "logs": True, "tags": True, "jumphost": True}
|
||||
edit = {"host": True, "protocol": True, "port": True, "user": True, "password": True, "options": True, "logs": True, "tags": True, "jumphost": True, "fallback": True}
|
||||
questions = []
|
||||
if edit["host"]:
|
||||
questions.append(inquirer.Text("host", message="Add Hostname or IP", validate=self.validators.host_validation, default=defaults["host"]))
|
||||
@@ -561,6 +626,8 @@ el.replaceWith(d);
|
||||
questions.append(inquirer.List("password", message="Password: Use a local password, no password or a list of profiles to reference?", choices=["Local Password", "Profiles", "No Password"]))
|
||||
else:
|
||||
node["password"] = defaults["password"]
|
||||
if not edit.get("fallback"):
|
||||
node["fallback"] = defaults.get("fallback", "")
|
||||
|
||||
answer = inquirer.prompt(questions)
|
||||
if answer is None:
|
||||
@@ -578,12 +645,28 @@ el.replaceWith(d);
|
||||
passa = inquirer.prompt(passq)
|
||||
if passa is None:
|
||||
return False
|
||||
answer["password"] = passa["password"].split(",")
|
||||
answer["password"] = [p.strip() for p in passa["password"].split(",") if p.strip()]
|
||||
elif answer["password"] == "No Password":
|
||||
answer["password"] = ""
|
||||
|
||||
if "tags" in answer and not answer["tags"].startswith("@") and answer["tags"]:
|
||||
answer["tags"] = ast.literal_eval(answer["tags"])
|
||||
if edit.get("fallback"):
|
||||
raw_def_fb = defaults.get("fallback", "")
|
||||
def_fb_str = json.dumps(raw_def_fb) if isinstance(raw_def_fb, (list, dict)) else str(raw_def_fb or "")
|
||||
fbq = [
|
||||
inquirer.Text(
|
||||
"fallback",
|
||||
message="Add fallback credentials (JSON list/dict, @profile, or compose:)",
|
||||
validate=self.validators.fallback_validation,
|
||||
default=def_fb_str.replace("{", "{{").replace("}", "}}"),
|
||||
)
|
||||
]
|
||||
fba = inquirer.prompt(fbq)
|
||||
if fba is None:
|
||||
return False
|
||||
answer["fallback"] = _parse_fallback_input(fba["fallback"])
|
||||
|
||||
if "tags" in answer and answer["tags"] and not str(answer["tags"]).startswith("@"):
|
||||
answer["tags"] = _parse_tags_input(answer["tags"])
|
||||
|
||||
result = {**uniques, **answer, **node}
|
||||
result["type"] = "connection"
|
||||
@@ -607,11 +690,13 @@ el.replaceWith(d);
|
||||
defaults["tags"] = ""
|
||||
if "jumphost" not in defaults:
|
||||
defaults["jumphost"] = ""
|
||||
except Exception:
|
||||
defaults = {"host": "", "protocol": "", "port": "", "user": "", "options": "", "logs": "", "tags": "", "jumphost": ""}
|
||||
if "fallback" not in defaults:
|
||||
defaults["fallback"] = ""
|
||||
except Exception: # noqa: BLE001
|
||||
defaults = {"host": "", "protocol": "", "port": "", "user": "", "options": "", "logs": "", "tags": "", "jumphost": "", "fallback": ""}
|
||||
profile = {}
|
||||
if edit is None:
|
||||
edit = {"host": True, "protocol": True, "port": True, "user": True, "password": True, "options": True, "logs": True, "tags": True, "jumphost": True}
|
||||
edit = {"host": True, "protocol": True, "port": True, "user": True, "password": True, "options": True, "logs": True, "tags": True, "jumphost": True, "fallback": True}
|
||||
questions = []
|
||||
if edit["host"]:
|
||||
questions.append(inquirer.Text("host", message="Add Hostname or IP", default=defaults["host"]))
|
||||
@@ -649,17 +734,24 @@ el.replaceWith(d);
|
||||
questions.append(inquirer.Password("password", message="Set Password"))
|
||||
else:
|
||||
profile["password"] = defaults["password"]
|
||||
if edit.get("fallback"):
|
||||
raw_def_fb = defaults.get("fallback", "")
|
||||
def_fb_str = json.dumps(raw_def_fb) if isinstance(raw_def_fb, (list, dict)) else str(raw_def_fb or "")
|
||||
questions.append(inquirer.Text("fallback", message="Add fallback credentials (JSON list/dict, @profile, or compose:)", validate=self.validators.fallback_validation, default=def_fb_str.replace("{", "{{").replace("}", "}}")))
|
||||
else:
|
||||
profile["fallback"] = defaults.get("fallback", "")
|
||||
|
||||
answer = inquirer.prompt(questions)
|
||||
if answer is None:
|
||||
return False
|
||||
|
||||
if "password" in answer:
|
||||
if answer["password"] != "":
|
||||
if "password" in answer and answer["password"] != "":
|
||||
answer["password"] = self.app.services.config_svc.encrypt_password(answer["password"])
|
||||
|
||||
if "tags" in answer and answer["tags"]:
|
||||
answer["tags"] = ast.literal_eval(answer["tags"])
|
||||
answer["tags"] = _parse_tags_input(answer["tags"])
|
||||
if "fallback" in answer:
|
||||
answer["fallback"] = _parse_fallback_input(answer["fallback"])
|
||||
|
||||
result = {**answer, **profile}
|
||||
result["id"] = unique
|
||||
|
||||
+104
-27
@@ -106,35 +106,80 @@ el.replaceWith(d);
|
||||
return True
|
||||
|
||||
def pass_validation(self, answers, current, regex = "(^@.+$)"):
|
||||
if is_compose_expression(current):
|
||||
try:
|
||||
validate_compose_syntax(current)
|
||||
except SanitizedAuthError as exc:
|
||||
_raise_val_err(str(exc))
|
||||
return True
|
||||
profiles = current.split(",")
|
||||
for i in profiles:
|
||||
if not re.match(regex, i) or i[1:] not in self.app.profiles:
|
||||
_raise_val_err("Profile {} don't exist".format(i))
|
||||
return True
|
||||
|
||||
def _validate_tags_dict(self, current: str):
|
||||
if len(current.encode("utf-8", errors="replace")) > MAX_JSON_INPUT_BYTES:
|
||||
_raise_val_err(f"Tags exceed maximum size of {MAX_JSON_INPUT_BYTES} bytes")
|
||||
parsed = None
|
||||
try:
|
||||
parsed = json.loads(current)
|
||||
except (ValueError, TypeError):
|
||||
try:
|
||||
parsed = ast.literal_eval(current)
|
||||
except (ValueError, TypeError, SyntaxError, MemoryError, RecursionError):
|
||||
parsed = None
|
||||
if not isinstance(parsed, dict):
|
||||
_raise_val_err("Tags should be a python dictionary.")
|
||||
try:
|
||||
validate_json_depth_and_size(
|
||||
parsed,
|
||||
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||
)
|
||||
validate_auth_budget_tags(parsed)
|
||||
except (ValueError, TypeError, SanitizedAuthError) as exc:
|
||||
_raise_val_err(str(exc))
|
||||
return True
|
||||
|
||||
def tags_validation(self, answers, current):
|
||||
if current.startswith("@"):
|
||||
if current[1:] not in self.app.profiles:
|
||||
_raise_val_err("Profile {} don't exist".format(current))
|
||||
elif current != "":
|
||||
isdict = False
|
||||
try:
|
||||
isdict = ast.literal_eval(current)
|
||||
except Exception:
|
||||
pass
|
||||
if not isinstance (isdict, dict):
|
||||
_raise_val_err("Tags should be a python dictionary.".format(current))
|
||||
return self._validate_tags_dict(current)
|
||||
return True
|
||||
|
||||
def profile_tags_validation(self, answers, current):
|
||||
if current != "":
|
||||
isdict = False
|
||||
return self._validate_tags_dict(current)
|
||||
return True
|
||||
|
||||
def fallback_validation(self, answers, current):
|
||||
if current in (None, ""):
|
||||
return True
|
||||
stripped = str(current).strip()
|
||||
if not stripped:
|
||||
return True
|
||||
if len(stripped.encode("utf-8", errors="replace")) > MAX_JSON_INPUT_BYTES:
|
||||
_raise_val_err(f"Fallback exceeds maximum size of {MAX_JSON_INPUT_BYTES} bytes")
|
||||
if stripped.startswith("@") and "+" not in stripped:
|
||||
if stripped[1:] not in self.app.profiles:
|
||||
_raise_val_err(f"Profile {stripped} don't exist")
|
||||
return True
|
||||
try:
|
||||
isdict = ast.literal_eval(current)
|
||||
except Exception:
|
||||
pass
|
||||
if not isinstance (isdict, dict):
|
||||
_raise_val_err("Tags should be a python dictionary.".format(current))
|
||||
if is_compose_expression(stripped):
|
||||
validate_compose_syntax(stripped)
|
||||
return True
|
||||
if stripped.startswith(("[", "{")):
|
||||
validate_json_depth_and_size(
|
||||
stripped,
|
||||
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||
)
|
||||
validate_fallback_entries(stripped, max_entries=MAX_FALLBACK_ENTRIES)
|
||||
except (ValueError, TypeError, SanitizedAuthError) as exc:
|
||||
_raise_val_err(str(exc))
|
||||
return True
|
||||
|
||||
def jumphost_validation(self, answers, current):
|
||||
@@ -273,6 +318,43 @@ el.replaceWith(d);
|
||||
</details>
|
||||
<div class="desc"></div>
|
||||
</dd>
|
||||
<dt id="connpy.cli.validators.Validators.fallback_validation"><code class="name flex">
|
||||
<span>def <span class="ident">fallback_validation</span></span>(<span>self, answers, current)</span>
|
||||
</code></dt>
|
||||
<dd>
|
||||
<details class="source">
|
||||
<summary>
|
||||
<span>Expand source code</span>
|
||||
</summary>
|
||||
<pre><code class="python">def fallback_validation(self, answers, current):
|
||||
if current in (None, ""):
|
||||
return True
|
||||
stripped = str(current).strip()
|
||||
if not stripped:
|
||||
return True
|
||||
if len(stripped.encode("utf-8", errors="replace")) > MAX_JSON_INPUT_BYTES:
|
||||
_raise_val_err(f"Fallback exceeds maximum size of {MAX_JSON_INPUT_BYTES} bytes")
|
||||
if stripped.startswith("@") and "+" not in stripped:
|
||||
if stripped[1:] not in self.app.profiles:
|
||||
_raise_val_err(f"Profile {stripped} don't exist")
|
||||
return True
|
||||
try:
|
||||
if is_compose_expression(stripped):
|
||||
validate_compose_syntax(stripped)
|
||||
return True
|
||||
if stripped.startswith(("[", "{")):
|
||||
validate_json_depth_and_size(
|
||||
stripped,
|
||||
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||
)
|
||||
validate_fallback_entries(stripped, max_entries=MAX_FALLBACK_ENTRIES)
|
||||
except (ValueError, TypeError, SanitizedAuthError) as exc:
|
||||
_raise_val_err(str(exc))
|
||||
return True</code></pre>
|
||||
</details>
|
||||
<div class="desc"></div>
|
||||
</dd>
|
||||
<dt id="connpy.cli.validators.Validators.host_validation"><code class="name flex">
|
||||
<span>def <span class="ident">host_validation</span></span>(<span>self, answers, current, regex='^.+$')</span>
|
||||
</code></dt>
|
||||
@@ -319,6 +401,12 @@ el.replaceWith(d);
|
||||
<span>Expand source code</span>
|
||||
</summary>
|
||||
<pre><code class="python">def pass_validation(self, answers, current, regex = "(^@.+$)"):
|
||||
if is_compose_expression(current):
|
||||
try:
|
||||
validate_compose_syntax(current)
|
||||
except SanitizedAuthError as exc:
|
||||
_raise_val_err(str(exc))
|
||||
return True
|
||||
profiles = current.split(",")
|
||||
for i in profiles:
|
||||
if not re.match(regex, i) or i[1:] not in self.app.profiles:
|
||||
@@ -413,13 +501,7 @@ el.replaceWith(d);
|
||||
</summary>
|
||||
<pre><code class="python">def profile_tags_validation(self, answers, current):
|
||||
if current != "":
|
||||
isdict = False
|
||||
try:
|
||||
isdict = ast.literal_eval(current)
|
||||
except Exception:
|
||||
pass
|
||||
if not isinstance (isdict, dict):
|
||||
_raise_val_err("Tags should be a python dictionary.".format(current))
|
||||
return self._validate_tags_dict(current)
|
||||
return True</code></pre>
|
||||
</details>
|
||||
<div class="desc"></div>
|
||||
@@ -455,13 +537,7 @@ el.replaceWith(d);
|
||||
if current[1:] not in self.app.profiles:
|
||||
_raise_val_err("Profile {} don't exist".format(current))
|
||||
elif current != "":
|
||||
isdict = False
|
||||
try:
|
||||
isdict = ast.literal_eval(current)
|
||||
except Exception:
|
||||
pass
|
||||
if not isinstance (isdict, dict):
|
||||
_raise_val_err("Tags should be a python dictionary.".format(current))
|
||||
return self._validate_tags_dict(current)
|
||||
return True</code></pre>
|
||||
</details>
|
||||
<div class="desc"></div>
|
||||
@@ -490,6 +566,7 @@ el.replaceWith(d);
|
||||
<li><code><a title="connpy.cli.validators.Validators.bulk_host_validation" href="#connpy.cli.validators.Validators.bulk_host_validation">bulk_host_validation</a></code></li>
|
||||
<li><code><a title="connpy.cli.validators.Validators.bulk_node_validation" href="#connpy.cli.validators.Validators.bulk_node_validation">bulk_node_validation</a></code></li>
|
||||
<li><code><a title="connpy.cli.validators.Validators.default_validation" href="#connpy.cli.validators.Validators.default_validation">default_validation</a></code></li>
|
||||
<li><code><a title="connpy.cli.validators.Validators.fallback_validation" href="#connpy.cli.validators.Validators.fallback_validation">fallback_validation</a></code></li>
|
||||
<li><code><a title="connpy.cli.validators.Validators.host_validation" href="#connpy.cli.validators.Validators.host_validation">host_validation</a></code></li>
|
||||
<li><code><a title="connpy.cli.validators.Validators.jumphost_validation" href="#connpy.cli.validators.Validators.jumphost_validation">jumphost_validation</a></code></li>
|
||||
<li><code><a title="connpy.cli.validators.Validators.pass_validation" href="#connpy.cli.validators.Validators.pass_validation">pass_validation</a></code></li>
|
||||
|
||||
@@ -55,6 +55,7 @@ el.replaceWith(d);
|
||||
<pre><code class="python">def handle_errors(func):
|
||||
import inspect
|
||||
if inspect.isgeneratorfunction(func):
|
||||
@functools.wraps(func)
|
||||
def wrapper(*args, **kwargs):
|
||||
try:
|
||||
for item in func(*args, **kwargs):
|
||||
@@ -75,6 +76,7 @@ el.replaceWith(d);
|
||||
printer.clear_thread_state()
|
||||
return wrapper
|
||||
else:
|
||||
@functools.wraps(func)
|
||||
def wrapper(*args, **kwargs):
|
||||
try:
|
||||
return func(*args, **kwargs)
|
||||
@@ -1425,8 +1427,24 @@ interceptor chooses to service this RPC, or None otherwise.</p></div>
|
||||
self.server_console.print(f"[debug][DEBUG][/debug] gRPC interact_node request for: [bold cyan]{unique_id}[/bold cyan]")
|
||||
|
||||
if first_req.connection_params_json:
|
||||
import json
|
||||
params = json.loads(first_req.connection_params_json)
|
||||
try:
|
||||
params = validate_json_depth_and_size(
|
||||
first_req.connection_params_json,
|
||||
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||
)
|
||||
if not isinstance(params, dict):
|
||||
raise TypeError("connection_params_json must be a JSON object")
|
||||
if "fallback" in params and params["fallback"] not in (None, "", []):
|
||||
validate_fallback_entries(
|
||||
params["fallback"],
|
||||
max_entries=MAX_FALLBACK_ENTRIES,
|
||||
)
|
||||
if "tags" in params and params["tags"] not in ("", None):
|
||||
validate_auth_budget_tags(params["tags"])
|
||||
except (ValueError, TypeError, SanitizedAuthError) as exc:
|
||||
context.abort(grpc.StatusCode.INVALID_ARGUMENT, str(exc))
|
||||
|
||||
base_node_id = params.get("base_node")
|
||||
# Valid attributes that a node object accepts
|
||||
valid_attrs = ['host', 'options', 'logs', 'password', 'port', 'protocol', 'user', 'jumphost']
|
||||
@@ -1444,6 +1462,8 @@ interceptor chooses to service this RPC, or None otherwise.</p></div>
|
||||
for attr in valid_attrs:
|
||||
if attr in params:
|
||||
device[attr] = params[attr]
|
||||
if "fallback" in params:
|
||||
device["fallback"] = params["fallback"]
|
||||
|
||||
if "tags" in params:
|
||||
device_tags = device.get("tags", {})
|
||||
@@ -1457,20 +1477,30 @@ interceptor chooses to service this RPC, or None otherwise.</p></div>
|
||||
else:
|
||||
# base_node not found, fall back to dynamic
|
||||
node_name = params.get("name", fallback_id)
|
||||
n = node(node_name, host=params.get("host", ""), config=user_config)
|
||||
for attr in valid_attrs:
|
||||
if attr in params:
|
||||
setattr(n, attr, params[attr])
|
||||
dyn_kwargs = {k: params[k] for k in valid_attrs if k in params and k != "host"}
|
||||
if "fallback" in params:
|
||||
dyn_kwargs["fallback"] = params["fallback"]
|
||||
if "tags" in params:
|
||||
n.tags = params["tags"]
|
||||
dyn_kwargs["tags"] = params["tags"]
|
||||
n = node(
|
||||
node_name,
|
||||
host=params.get("host", ""),
|
||||
config=user_config,
|
||||
**dyn_kwargs,
|
||||
)
|
||||
else:
|
||||
node_name = params.get("name", fallback_id)
|
||||
n = node(node_name, host=params.get("host", ""), config=user_config)
|
||||
for attr in valid_attrs:
|
||||
if attr in params:
|
||||
setattr(n, attr, params[attr])
|
||||
dyn_kwargs = {k: params[k] for k in valid_attrs if k in params and k != "host"}
|
||||
if "fallback" in params:
|
||||
dyn_kwargs["fallback"] = params["fallback"]
|
||||
if "tags" in params:
|
||||
n.tags = params["tags"]
|
||||
dyn_kwargs["tags"] = params["tags"]
|
||||
n = node(
|
||||
node_name,
|
||||
host=params.get("host", ""),
|
||||
config=user_config,
|
||||
**dyn_kwargs,
|
||||
)
|
||||
n._orig_protocol = n.protocol
|
||||
if sftp:
|
||||
n.protocol = "sftp"
|
||||
@@ -1491,9 +1521,11 @@ interceptor chooses to service this RPC, or None otherwise.</p></div>
|
||||
debug_chunks = []
|
||||
if debug:
|
||||
from io import StringIO
|
||||
|
||||
from rich.console import Console as RichConsole
|
||||
from ..printer import connpy_theme
|
||||
|
||||
from .. import printer as _printer
|
||||
from ..printer import connpy_theme
|
||||
|
||||
def remote_logger(msg_type, message):
|
||||
buf = StringIO()
|
||||
@@ -1514,7 +1546,14 @@ interceptor chooses to service this RPC, or None otherwise.</p></div>
|
||||
else:
|
||||
remote_logger = None
|
||||
|
||||
connect = n._connect(debug=debug, logger=remote_logger)
|
||||
try:
|
||||
connect = n._connect(
|
||||
debug=debug,
|
||||
logger=remote_logger,
|
||||
attempt_tracker=getattr(provider, "auth_tracker", None),
|
||||
)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
connect = str(sanitize_exception(exc, code="AUTH_FAILED"))
|
||||
|
||||
# Send debug output to client before checking result (always show the command)
|
||||
for chunk in debug_chunks:
|
||||
@@ -1845,10 +1884,9 @@ interceptor chooses to service this RPC, or None otherwise.</p></div>
|
||||
continue
|
||||
|
||||
asyncio.run(n._async_interact_loop(remote_stream, resize_callback, copilot_handler=remote_copilot_handler))
|
||||
except Exception as e:
|
||||
import traceback
|
||||
print(f"[ERROR in run_async_loop] {e}")
|
||||
traceback.print_exc()
|
||||
except Exception as e: # noqa: BLE001
|
||||
sanitized_err = sanitize_exception(e, code="TRANSPORT_ERROR", context_label="interact stream")
|
||||
print(f"[ERROR in run_async_loop] {sanitized_err}")
|
||||
finally:
|
||||
n._teardown_interact_environment()
|
||||
response_queue.put(None) # Signal EOF
|
||||
|
||||
+2894
-648
File diff suppressed because it is too large.
Load diff
+145
-86
@@ -162,20 +162,7 @@ el.replaceWith(d);
|
||||
|
||||
def _mask_secrets(self, val: Any) -> Any:
|
||||
"""Recursively mask passwords, passphrases, secrets, and auth tokens with '***'."""
|
||||
if isinstance(val, dict):
|
||||
masked = {}
|
||||
for k, v in val.items():
|
||||
k_lower = str(k).lower()
|
||||
if any(sec in k_lower for sec in ["password", "passphrase", "secret", "token"]):
|
||||
masked[k] = "***"
|
||||
elif isinstance(v, (dict, list)):
|
||||
masked[k] = self._mask_secrets(v)
|
||||
else:
|
||||
masked[k] = v
|
||||
return masked
|
||||
if isinstance(val, list):
|
||||
return [self._mask_secrets(item) for item in val]
|
||||
return val
|
||||
return mask_node_secrets(val)
|
||||
|
||||
# -------------------------------------------------------------------------
|
||||
# Level 1: Read-Only Inventory and Context Tools
|
||||
@@ -234,6 +221,31 @@ el.replaceWith(d);
|
||||
# Level 2: Execution, Testing, and Playbooks Tools
|
||||
# -------------------------------------------------------------------------
|
||||
|
||||
@staticmethod
|
||||
def _parse_json_list_or_lines(raw_val: list[str] | str, *, allow_empty_str: bool = False) -> list[str]:
|
||||
if isinstance(raw_val, str):
|
||||
if len(raw_val.encode("utf-8", errors="replace")) > MAX_JSON_INPUT_BYTES:
|
||||
raise ValueError(f"Input exceeds maximum size of {MAX_JSON_INPUT_BYTES} bytes")
|
||||
stripped = raw_val.strip()
|
||||
if stripped.startswith(("[", "{")):
|
||||
parsed = validate_json_depth_and_size(
|
||||
stripped,
|
||||
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||
)
|
||||
return parsed if isinstance(parsed, list) else [raw_val]
|
||||
return ([raw_val] if raw_val else []) if allow_empty_str else [
|
||||
c.strip() for c in raw_val.splitlines() if c.strip()
|
||||
]
|
||||
if isinstance(raw_val, list):
|
||||
validate_json_depth_and_size(
|
||||
raw_val,
|
||||
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||
)
|
||||
return raw_val
|
||||
return [str(raw_val)] if (raw_val or not allow_empty_str) else []
|
||||
|
||||
def run_commands(
|
||||
self,
|
||||
nodes_filter: str,
|
||||
@@ -248,17 +260,10 @@ el.replaceWith(d);
|
||||
if not self.services or not hasattr(self.services, "execution"):
|
||||
return {"success": False, "error": "Execution service unavailable."}
|
||||
|
||||
# Normalize commands list
|
||||
if isinstance(commands, str):
|
||||
try:
|
||||
import json
|
||||
|
||||
parsed = json.loads(commands)
|
||||
commands = parsed if isinstance(parsed, list) else [commands]
|
||||
except Exception: # noqa: BLE001
|
||||
commands = [c.strip() for c in commands.splitlines() if c.strip()]
|
||||
elif not isinstance(commands, list):
|
||||
commands = [str(commands)]
|
||||
commands = self._parse_json_list_or_lines(commands, allow_empty_str=False)
|
||||
except ValueError as exc:
|
||||
return {"success": False, "error": str(exc)}
|
||||
|
||||
# Check safety
|
||||
if not self.trust:
|
||||
@@ -277,9 +282,24 @@ el.replaceWith(d);
|
||||
parallel=par_count,
|
||||
timeout=timeout,
|
||||
)
|
||||
return {"success": True, "results": results}
|
||||
fb_used = False
|
||||
if isinstance(results, dict):
|
||||
fb_used = any(
|
||||
isinstance(v, dict) and bool(v.get("fallback_used", False))
|
||||
for v in results.values()
|
||||
)
|
||||
if not fb_used:
|
||||
fb_map = getattr(self.services.execution, "last_fallback_used", {})
|
||||
if isinstance(fb_map, dict):
|
||||
fb_used = any(bool(v) for v in fb_map.values())
|
||||
return {"success": True, "results": results, "fallback_used": fb_used}
|
||||
except Exception as e: # noqa: BLE001
|
||||
return {"success": False, "error": str(e)}
|
||||
san = sanitize_exception(
|
||||
e,
|
||||
code="AUTH_FAILED",
|
||||
context_label="command execution failed",
|
||||
)
|
||||
return {"success": False, "error": str(san)}
|
||||
|
||||
def test_commands(
|
||||
self,
|
||||
@@ -293,29 +313,11 @@ el.replaceWith(d);
|
||||
if not self.services or not hasattr(self.services, "execution"):
|
||||
return {"success": False, "error": "Execution service unavailable."}
|
||||
|
||||
# Normalize commands
|
||||
if isinstance(commands, str):
|
||||
try:
|
||||
import json
|
||||
|
||||
parsed = json.loads(commands)
|
||||
commands = parsed if isinstance(parsed, list) else [commands]
|
||||
except Exception: # noqa: BLE001
|
||||
commands = [c.strip() for c in commands.splitlines() if c.strip()]
|
||||
elif not isinstance(commands, list):
|
||||
commands = [str(commands)]
|
||||
|
||||
# Normalize expected
|
||||
if isinstance(expected, str):
|
||||
try:
|
||||
import json
|
||||
|
||||
parsed = json.loads(expected)
|
||||
expected = parsed if isinstance(parsed, list) else ([expected] if expected else [])
|
||||
except Exception: # noqa: BLE001
|
||||
expected = [expected] if expected else []
|
||||
elif not isinstance(expected, list):
|
||||
expected = [str(expected)] if expected else []
|
||||
commands = self._parse_json_list_or_lines(commands, allow_empty_str=False)
|
||||
expected = self._parse_json_list_or_lines(expected, allow_empty_str=True)
|
||||
except ValueError as exc:
|
||||
return {"success": False, "error": str(exc)}
|
||||
|
||||
# Check safety
|
||||
if not self.trust:
|
||||
@@ -335,9 +337,16 @@ el.replaceWith(d);
|
||||
parallel=par_count,
|
||||
timeout=timeout,
|
||||
)
|
||||
return {"success": True, "results": results}
|
||||
fb_map = getattr(self.services.execution, "last_fallback_used", {})
|
||||
fb_used = any(bool(v) for v in fb_map.values()) if isinstance(fb_map, dict) else False
|
||||
return {"success": True, "results": results, "fallback_used": fb_used}
|
||||
except Exception as e: # noqa: BLE001
|
||||
return {"success": False, "error": str(e)}
|
||||
san = sanitize_exception(
|
||||
e,
|
||||
code="AUTH_FAILED",
|
||||
context_label="test execution failed",
|
||||
)
|
||||
return {"success": False, "error": str(san)}
|
||||
|
||||
def validate_playbook(self, playbook_yaml: str) -> dict:
|
||||
"""Validate the syntax, schema, and tasks of a Connpy YAML playbook."""
|
||||
@@ -465,6 +474,47 @@ el.replaceWith(d);
|
||||
# Level 3: Inventory Mutation Tools (CRUD - Requires trust=True)
|
||||
# -------------------------------------------------------------------------
|
||||
|
||||
@staticmethod
|
||||
def _validate_and_normalize_node_payload(node_data: dict[str, Any]) -> dict[str, Any]:
|
||||
validate_json_depth_and_size(
|
||||
node_data,
|
||||
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||
)
|
||||
normalized = node_data.copy()
|
||||
if "tags" in normalized and normalized["tags"] not in ("", None):
|
||||
tags_val = normalized["tags"]
|
||||
if isinstance(tags_val, str) and tags_val.strip().startswith("{"):
|
||||
tags_val = validate_json_depth_and_size(
|
||||
tags_val,
|
||||
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||
)
|
||||
normalized["tags"] = tags_val
|
||||
validate_auth_budget_tags(tags_val)
|
||||
|
||||
if "fallback" in normalized and normalized["fallback"] not in (None, "", []):
|
||||
fb_val = normalized["fallback"]
|
||||
if isinstance(fb_val, str) and fb_val.strip().startswith(("[", "{")):
|
||||
fb_val = validate_json_depth_and_size(
|
||||
fb_val,
|
||||
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||
)
|
||||
normalized["fallback"] = validate_fallback_entries(
|
||||
fb_val,
|
||||
max_entries=MAX_FALLBACK_ENTRIES,
|
||||
)
|
||||
|
||||
pw = normalized.get("password")
|
||||
if is_compose_expression(pw):
|
||||
validate_compose_syntax(pw)
|
||||
elif isinstance(pw, (list, tuple)):
|
||||
for item in pw:
|
||||
if is_compose_expression(item):
|
||||
validate_compose_syntax(item)
|
||||
return normalized
|
||||
|
||||
def add_node(
|
||||
self, unique_id: str, data: dict | None = None, is_folder: bool = False, **kwargs
|
||||
) -> dict:
|
||||
@@ -482,11 +532,14 @@ el.replaceWith(d);
|
||||
node_data.update(kwargs)
|
||||
|
||||
try:
|
||||
node_data = self._validate_and_normalize_node_payload(node_data)
|
||||
self.services.nodes.add_node(unique_id, node_data, is_folder=is_folder)
|
||||
return {
|
||||
"success": True,
|
||||
"message": f"{'Folder' if is_folder else 'Node'} '{unique_id}' created successfully.",
|
||||
}
|
||||
except (ValueError, TypeError, SanitizedAuthError) as exc:
|
||||
return {"success": False, "error": str(exc)}
|
||||
except Exception as e: # noqa: BLE001
|
||||
return {"success": False, "error": str(e)}
|
||||
|
||||
@@ -505,8 +558,11 @@ el.replaceWith(d);
|
||||
node_updates.update(kwargs)
|
||||
|
||||
try:
|
||||
node_updates = self._validate_and_normalize_node_payload(node_updates)
|
||||
self.services.nodes.update_node(unique_id, node_updates)
|
||||
return {"success": True, "message": f"Node '{unique_id}' updated successfully."}
|
||||
except (ValueError, TypeError, SanitizedAuthError) as exc:
|
||||
return {"success": False, "error": str(exc)}
|
||||
except Exception as e: # noqa: BLE001
|
||||
return {"success": False, "error": str(e)}
|
||||
|
||||
@@ -608,11 +664,14 @@ el.replaceWith(d);
|
||||
node_data.update(kwargs)
|
||||
|
||||
try:
|
||||
node_data = self._validate_and_normalize_node_payload(node_data)
|
||||
self.services.nodes.add_node(unique_id, node_data, is_folder=is_folder)
|
||||
return {
|
||||
"success": True,
|
||||
"message": f"{'Folder' if is_folder else 'Node'} '{unique_id}' created successfully.",
|
||||
}
|
||||
except (ValueError, TypeError, SanitizedAuthError) as exc:
|
||||
return {"success": False, "error": str(exc)}
|
||||
except Exception as e: # noqa: BLE001
|
||||
return {"success": False, "error": str(e)}</code></pre>
|
||||
</details>
|
||||
@@ -865,17 +924,10 @@ el.replaceWith(d);
|
||||
if not self.services or not hasattr(self.services, "execution"):
|
||||
return {"success": False, "error": "Execution service unavailable."}
|
||||
|
||||
# Normalize commands list
|
||||
if isinstance(commands, str):
|
||||
try:
|
||||
import json
|
||||
|
||||
parsed = json.loads(commands)
|
||||
commands = parsed if isinstance(parsed, list) else [commands]
|
||||
except Exception: # noqa: BLE001
|
||||
commands = [c.strip() for c in commands.splitlines() if c.strip()]
|
||||
elif not isinstance(commands, list):
|
||||
commands = [str(commands)]
|
||||
commands = self._parse_json_list_or_lines(commands, allow_empty_str=False)
|
||||
except ValueError as exc:
|
||||
return {"success": False, "error": str(exc)}
|
||||
|
||||
# Check safety
|
||||
if not self.trust:
|
||||
@@ -894,9 +946,24 @@ el.replaceWith(d);
|
||||
parallel=par_count,
|
||||
timeout=timeout,
|
||||
)
|
||||
return {"success": True, "results": results}
|
||||
fb_used = False
|
||||
if isinstance(results, dict):
|
||||
fb_used = any(
|
||||
isinstance(v, dict) and bool(v.get("fallback_used", False))
|
||||
for v in results.values()
|
||||
)
|
||||
if not fb_used:
|
||||
fb_map = getattr(self.services.execution, "last_fallback_used", {})
|
||||
if isinstance(fb_map, dict):
|
||||
fb_used = any(bool(v) for v in fb_map.values())
|
||||
return {"success": True, "results": results, "fallback_used": fb_used}
|
||||
except Exception as e: # noqa: BLE001
|
||||
return {"success": False, "error": str(e)}</code></pre>
|
||||
san = sanitize_exception(
|
||||
e,
|
||||
code="AUTH_FAILED",
|
||||
context_label="command execution failed",
|
||||
)
|
||||
return {"success": False, "error": str(san)}</code></pre>
|
||||
</details>
|
||||
<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>
|
||||
@@ -986,29 +1053,11 @@ el.replaceWith(d);
|
||||
if not self.services or not hasattr(self.services, "execution"):
|
||||
return {"success": False, "error": "Execution service unavailable."}
|
||||
|
||||
# Normalize commands
|
||||
if isinstance(commands, str):
|
||||
try:
|
||||
import json
|
||||
|
||||
parsed = json.loads(commands)
|
||||
commands = parsed if isinstance(parsed, list) else [commands]
|
||||
except Exception: # noqa: BLE001
|
||||
commands = [c.strip() for c in commands.splitlines() if c.strip()]
|
||||
elif not isinstance(commands, list):
|
||||
commands = [str(commands)]
|
||||
|
||||
# Normalize expected
|
||||
if isinstance(expected, str):
|
||||
try:
|
||||
import json
|
||||
|
||||
parsed = json.loads(expected)
|
||||
expected = parsed if isinstance(parsed, list) else ([expected] if expected else [])
|
||||
except Exception: # noqa: BLE001
|
||||
expected = [expected] if expected else []
|
||||
elif not isinstance(expected, list):
|
||||
expected = [str(expected)] if expected else []
|
||||
commands = self._parse_json_list_or_lines(commands, allow_empty_str=False)
|
||||
expected = self._parse_json_list_or_lines(expected, allow_empty_str=True)
|
||||
except ValueError as exc:
|
||||
return {"success": False, "error": str(exc)}
|
||||
|
||||
# Check safety
|
||||
if not self.trust:
|
||||
@@ -1028,9 +1077,16 @@ el.replaceWith(d);
|
||||
parallel=par_count,
|
||||
timeout=timeout,
|
||||
)
|
||||
return {"success": True, "results": results}
|
||||
fb_map = getattr(self.services.execution, "last_fallback_used", {})
|
||||
fb_used = any(bool(v) for v in fb_map.values()) if isinstance(fb_map, dict) else False
|
||||
return {"success": True, "results": results, "fallback_used": fb_used}
|
||||
except Exception as e: # noqa: BLE001
|
||||
return {"success": False, "error": str(e)}</code></pre>
|
||||
san = sanitize_exception(
|
||||
e,
|
||||
code="AUTH_FAILED",
|
||||
context_label="test execution failed",
|
||||
)
|
||||
return {"success": False, "error": str(san)}</code></pre>
|
||||
</details>
|
||||
<div class="desc"><p>Execute commands and assert expected patterns on nodes matching the filter.</p></div>
|
||||
</dd>
|
||||
@@ -1057,8 +1113,11 @@ el.replaceWith(d);
|
||||
node_updates.update(kwargs)
|
||||
|
||||
try:
|
||||
node_updates = self._validate_and_normalize_node_payload(node_updates)
|
||||
self.services.nodes.update_node(unique_id, node_updates)
|
||||
return {"success": True, "message": f"Node '{unique_id}' updated successfully."}
|
||||
except (ValueError, TypeError, SanitizedAuthError) as exc:
|
||||
return {"success": False, "error": str(exc)}
|
||||
except Exception as e: # noqa: BLE001
|
||||
return {"success": False, "error": str(e)}</code></pre>
|
||||
</details>
|
||||
|
||||
@@ -67,6 +67,7 @@ el.replaceWith(d);
|
||||
</ul>
|
||||
<h3>Subclasses</h3>
|
||||
<ul class="hlist">
|
||||
<li><a title="connpy.auth_utils.SanitizedAuthError" href="../auth_utils.html#connpy.auth_utils.SanitizedAuthError">SanitizedAuthError</a></li>
|
||||
<li><a title="connpy.services.exceptions.ExecutionError" href="#connpy.services.exceptions.ExecutionError">ExecutionError</a></li>
|
||||
<li><a title="connpy.services.exceptions.InvalidConfigurationError" href="#connpy.services.exceptions.InvalidConfigurationError">InvalidConfigurationError</a></li>
|
||||
<li><a title="connpy.services.exceptions.NodeAlreadyExistsError" href="#connpy.services.exceptions.NodeAlreadyExistsError">NodeAlreadyExistsError</a></li>
|
||||
|
||||
@@ -48,7 +48,7 @@ el.replaceWith(d);
|
||||
<dl>
|
||||
<dt id="connpy.services.execution_service.ExecutionService"><code class="flex name class">
|
||||
<span>class <span class="ident">ExecutionService</span></span>
|
||||
<span>(</span><span>config=None)</span>
|
||||
<span>(</span><span>config=None, remote_stub=None, attempt_tracker=None)</span>
|
||||
</code></dt>
|
||||
<dd>
|
||||
<details class="source">
|
||||
@@ -58,22 +58,98 @@ el.replaceWith(d);
|
||||
<pre><code class="python">class ExecutionService(BaseService):
|
||||
"""Business logic for executing commands on nodes and running automation scripts."""
|
||||
|
||||
def __init__(self, config=None, remote_stub=None, attempt_tracker=None):
|
||||
super().__init__(config)
|
||||
self.remote_stub = remote_stub
|
||||
self.attempt_tracker = (
|
||||
attempt_tracker if attempt_tracker is not None else AuthAttemptTracker()
|
||||
)
|
||||
self.last_fallback_used: dict[str, bool] = {}
|
||||
|
||||
def _is_remote_backend(self) -> bool:
|
||||
"""Return True when execution is configured to run against a remote gRPC backend."""
|
||||
if getattr(self, "remote_stub", None) is not None:
|
||||
return True
|
||||
cfg = getattr(self.config, "config", None)
|
||||
return isinstance(cfg, dict) and cfg.get("service_mode") == "remote"
|
||||
|
||||
def _ensure_backend_supports_stage_timeouts(
|
||||
self,
|
||||
connect_timeout: int | None = None,
|
||||
auth_timeout: int | None = None,
|
||||
) -> None:
|
||||
"""Raise ConnpyError if per-stage timeout overrides are used on gRPC remote backend."""
|
||||
if self._is_remote_backend() and (connect_timeout is not None or auth_timeout is not None):
|
||||
raise ConnpyError(
|
||||
"connect_timeout and auth_timeout overrides are not supported in remote gRPC mode"
|
||||
)
|
||||
|
||||
def _get_remote_stub(self):
|
||||
"""Return the configured or lazily constructed ExecutionStub for remote gRPC mode."""
|
||||
if getattr(self, "remote_stub", None) is not None:
|
||||
return self.remote_stub
|
||||
cfg = getattr(self.config, "config", None)
|
||||
remote_host = cfg.get("remote_host") if isinstance(cfg, dict) else None
|
||||
if not remote_host:
|
||||
raise ConnpyError("Remote host must be specified in remote mode")
|
||||
|
||||
import grpc
|
||||
|
||||
from connpy.grpc_layer.stubs import AuthClientInterceptor, ExecutionStub
|
||||
|
||||
def get_token():
|
||||
env_token = os.environ.get("CONNPY_TOKEN")
|
||||
if env_token:
|
||||
return env_token
|
||||
defaultdir = getattr(self.config, "defaultdir", None)
|
||||
if defaultdir:
|
||||
token_path = os.path.join(defaultdir, ".token")
|
||||
if os.path.exists(token_path):
|
||||
with suppress(OSError), open(token_path) as f:
|
||||
return f.read().strip()
|
||||
return None
|
||||
|
||||
channel = grpc.insecure_channel(remote_host)
|
||||
channel = grpc.intercept_channel(channel, AuthClientInterceptor(get_token))
|
||||
return ExecutionStub(channel, remote_host=remote_host)
|
||||
|
||||
def run_commands(
|
||||
self,
|
||||
nodes_filter: str,
|
||||
commands: List[str],
|
||||
variables: Optional[Dict[str, Any]] = None,
|
||||
commands: list[str],
|
||||
variables: dict[str, Any] | None = None,
|
||||
parallel: int = 10,
|
||||
timeout: int = 20,
|
||||
folder: Optional[str] = None,
|
||||
prompt: Optional[str] = None,
|
||||
on_node_complete: Optional[Callable] = None,
|
||||
logger: Optional[Callable] = None,
|
||||
name: Optional[str] = None
|
||||
) -> Dict[str, str]:
|
||||
|
||||
timeout: int | None = None,
|
||||
folder: str | None = None,
|
||||
prompt: str | None = None,
|
||||
on_node_complete: Callable | None = None,
|
||||
logger: Callable | None = None,
|
||||
name: str | None = None,
|
||||
connect_timeout: int | None = None,
|
||||
auth_timeout: int | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""Execute commands on a set of nodes."""
|
||||
try:
|
||||
self._ensure_backend_supports_stage_timeouts(
|
||||
connect_timeout=connect_timeout,
|
||||
auth_timeout=auth_timeout,
|
||||
)
|
||||
|
||||
if self._is_remote_backend():
|
||||
stub = self._get_remote_stub()
|
||||
return stub.run_commands(
|
||||
nodes_filter=nodes_filter,
|
||||
commands=commands,
|
||||
variables=variables,
|
||||
parallel=parallel,
|
||||
timeout=timeout if timeout is not None else 20,
|
||||
folder=folder,
|
||||
prompt=prompt,
|
||||
on_node_complete=on_node_complete,
|
||||
logger=logger,
|
||||
name=name or "",
|
||||
)
|
||||
|
||||
matched_names = self.config._getallnodes(nodes_filter)
|
||||
if not matched_names:
|
||||
raise ConnpyError(f"No nodes found matching filter: {nodes_filter}")
|
||||
@@ -82,46 +158,87 @@ el.replaceWith(d);
|
||||
executor = Nodes(node_data, config=self.config)
|
||||
self.last_executor = executor
|
||||
|
||||
results = executor.run(
|
||||
commands=commands,
|
||||
vars=variables,
|
||||
parallel=parallel,
|
||||
timeout=timeout,
|
||||
folder=folder,
|
||||
prompt=prompt,
|
||||
on_complete=on_node_complete,
|
||||
logger=logger
|
||||
)
|
||||
run_kwargs: dict[str, Any] = {
|
||||
"commands": commands,
|
||||
"vars": variables,
|
||||
"parallel": parallel,
|
||||
"timeout": timeout,
|
||||
"folder": folder,
|
||||
"prompt": prompt,
|
||||
"on_complete": on_node_complete,
|
||||
"logger": logger,
|
||||
"connect_timeout": connect_timeout,
|
||||
"auth_timeout": auth_timeout,
|
||||
"_default_cmd_timeout": 20,
|
||||
}
|
||||
run_params = inspect.signature(executor.run).parameters
|
||||
if "unattended" in run_params:
|
||||
run_kwargs["unattended"] = True
|
||||
if "attempt_tracker" in run_params:
|
||||
run_kwargs["attempt_tracker"] = self.attempt_tracker
|
||||
|
||||
# Combine output and status for the caller
|
||||
results = executor.run(**run_kwargs)
|
||||
|
||||
fb_map = getattr(executor, "fallback_used", {})
|
||||
fb_dict = fb_map if isinstance(fb_map, dict) else {}
|
||||
|
||||
# Combine output, status, and fallback metadata for the caller
|
||||
full_results = {}
|
||||
self.last_fallback_used = {}
|
||||
for unique in results:
|
||||
fb_used = bool(fb_dict.get(unique, False))
|
||||
full_results[unique] = {
|
||||
"output": results[unique],
|
||||
"status": executor.status.get(unique, 1)
|
||||
"status": executor.status.get(unique, 1),
|
||||
"fallback_used": fb_used,
|
||||
}
|
||||
self.last_fallback_used[unique] = fb_used
|
||||
|
||||
return full_results
|
||||
except Exception as e:
|
||||
raise ConnpyError(f"Execution failed: {e}")
|
||||
except ConnpyError:
|
||||
raise
|
||||
except Exception as e: # noqa: BLE001
|
||||
raise_sanitized_auth_error(e, code="AUTH_FAILED", context_label="execution failed")
|
||||
|
||||
def test_commands(
|
||||
self,
|
||||
nodes_filter: str,
|
||||
commands: List[str],
|
||||
expected: List[str],
|
||||
variables: Optional[Dict[str, Any]] = None,
|
||||
commands: list[str],
|
||||
expected: list[str],
|
||||
variables: dict[str, Any] | None = None,
|
||||
parallel: int = 10,
|
||||
timeout: int = 20,
|
||||
folder: Optional[str] = None,
|
||||
prompt: Optional[str] = None,
|
||||
on_node_complete: Optional[Callable] = None,
|
||||
logger: Optional[Callable] = None,
|
||||
name: Optional[str] = None
|
||||
) -> Dict[str, Dict[str, bool]]:
|
||||
|
||||
timeout: int | None = None,
|
||||
folder: str | None = None,
|
||||
prompt: str | None = None,
|
||||
on_node_complete: Callable | None = None,
|
||||
logger: Callable | None = None,
|
||||
name: str | None = None,
|
||||
connect_timeout: int | None = None,
|
||||
auth_timeout: int | None = None,
|
||||
) -> dict[str, dict[str, bool]]:
|
||||
"""Run commands and verify expected output on a set of nodes."""
|
||||
try:
|
||||
self._ensure_backend_supports_stage_timeouts(
|
||||
connect_timeout=connect_timeout,
|
||||
auth_timeout=auth_timeout,
|
||||
)
|
||||
|
||||
if self._is_remote_backend():
|
||||
stub = self._get_remote_stub()
|
||||
return stub.test_commands(
|
||||
nodes_filter=nodes_filter,
|
||||
commands=commands,
|
||||
expected=expected,
|
||||
variables=variables,
|
||||
parallel=parallel,
|
||||
timeout=timeout if timeout is not None else 20,
|
||||
folder=folder or "",
|
||||
prompt=prompt,
|
||||
on_node_complete=on_node_complete,
|
||||
logger=logger,
|
||||
name=name or "",
|
||||
)
|
||||
|
||||
matched_names = self.config._getallnodes(nodes_filter)
|
||||
if not matched_names:
|
||||
raise ConnpyError(f"No nodes found matching filter: {nodes_filter}")
|
||||
@@ -130,31 +247,49 @@ el.replaceWith(d);
|
||||
executor = Nodes(node_data, config=self.config)
|
||||
self.last_executor = executor
|
||||
|
||||
results = executor.test(
|
||||
commands=commands,
|
||||
expected=expected,
|
||||
vars=variables,
|
||||
parallel=parallel,
|
||||
timeout=timeout,
|
||||
folder=folder,
|
||||
prompt=prompt,
|
||||
on_complete=on_node_complete,
|
||||
logger=logger
|
||||
)
|
||||
return results
|
||||
except Exception as e:
|
||||
raise ConnpyError(f"Testing failed: {e}")
|
||||
test_kwargs: dict[str, Any] = {
|
||||
"commands": commands,
|
||||
"expected": expected,
|
||||
"vars": variables,
|
||||
"parallel": parallel,
|
||||
"timeout": timeout,
|
||||
"folder": folder,
|
||||
"prompt": prompt,
|
||||
"on_complete": on_node_complete,
|
||||
"logger": logger,
|
||||
"connect_timeout": connect_timeout,
|
||||
"auth_timeout": auth_timeout,
|
||||
"_default_cmd_timeout": 20,
|
||||
}
|
||||
test_params = inspect.signature(executor.test).parameters
|
||||
if "unattended" in test_params:
|
||||
test_kwargs["unattended"] = True
|
||||
if "attempt_tracker" in test_params:
|
||||
test_kwargs["attempt_tracker"] = self.attempt_tracker
|
||||
|
||||
def run_cli_script(self, nodes_filter: str, script_path: str, parallel: int = 10) -> Dict[str, str]:
|
||||
res = executor.test(**test_kwargs)
|
||||
fb_map = getattr(executor, "fallback_used", {})
|
||||
fb_dict = fb_map if isinstance(fb_map, dict) else {}
|
||||
if isinstance(res, dict):
|
||||
self.last_fallback_used = {u: bool(fb_dict.get(u, False)) for u in res}
|
||||
else:
|
||||
self.last_fallback_used = {}
|
||||
return res
|
||||
except ConnpyError:
|
||||
raise
|
||||
except Exception as e: # noqa: BLE001
|
||||
raise_sanitized_auth_error(e, code="AUTH_FAILED", context_label="testing failed")
|
||||
|
||||
def run_cli_script(self, nodes_filter: str, script_path: str, parallel: int = 10) -> dict[str, str]:
|
||||
"""Run a plain-text script containing one command per line."""
|
||||
if not os.path.exists(script_path):
|
||||
raise ConnpyError(f"Script file not found: {script_path}")
|
||||
|
||||
try:
|
||||
with open(script_path, "r") as f:
|
||||
with open(script_path) as f:
|
||||
commands = [line.strip() for line in f if line.strip()]
|
||||
except Exception as e:
|
||||
raise ConnpyError(f"Failed to read script {script_path}: {e}")
|
||||
except OSError:
|
||||
raise ConnpyError("Failed to read script file") from None
|
||||
|
||||
return self.run_commands(nodes_filter, commands, parallel=parallel)</code></pre>
|
||||
</details>
|
||||
@@ -173,30 +308,30 @@ el.replaceWith(d);
|
||||
<h3>Methods</h3>
|
||||
<dl>
|
||||
<dt id="connpy.services.execution_service.ExecutionService.run_cli_script"><code class="name flex">
|
||||
<span>def <span class="ident">run_cli_script</span></span>(<span>self, nodes_filter: str, script_path: str, parallel: int = 10) ‑> Dict[str, str]</span>
|
||||
<span>def <span class="ident">run_cli_script</span></span>(<span>self, nodes_filter: str, script_path: str, parallel: int = 10) ‑> dict[str, str]</span>
|
||||
</code></dt>
|
||||
<dd>
|
||||
<details class="source">
|
||||
<summary>
|
||||
<span>Expand source code</span>
|
||||
</summary>
|
||||
<pre><code class="python">def run_cli_script(self, nodes_filter: str, script_path: str, parallel: int = 10) -> Dict[str, str]:
|
||||
<pre><code class="python">def run_cli_script(self, nodes_filter: str, script_path: str, parallel: int = 10) -> dict[str, str]:
|
||||
"""Run a plain-text script containing one command per line."""
|
||||
if not os.path.exists(script_path):
|
||||
raise ConnpyError(f"Script file not found: {script_path}")
|
||||
|
||||
try:
|
||||
with open(script_path, "r") as f:
|
||||
with open(script_path) as f:
|
||||
commands = [line.strip() for line in f if line.strip()]
|
||||
except Exception as e:
|
||||
raise ConnpyError(f"Failed to read script {script_path}: {e}")
|
||||
except OSError:
|
||||
raise ConnpyError("Failed to read script file") from None
|
||||
|
||||
return self.run_commands(nodes_filter, commands, parallel=parallel)</code></pre>
|
||||
</details>
|
||||
<div class="desc"><p>Run a plain-text script containing one command per line.</p></div>
|
||||
</dd>
|
||||
<dt id="connpy.services.execution_service.ExecutionService.run_commands"><code class="name flex">
|
||||
<span>def <span class="ident">run_commands</span></span>(<span>self,<br>nodes_filter: str,<br>commands: List[str],<br>variables: Dict[str, Any] | None = None,<br>parallel: int = 10,<br>timeout: int = 20,<br>folder: str | None = None,<br>prompt: str | None = None,<br>on_node_complete: Callable | None = None,<br>logger: Callable | None = None,<br>name: str | None = None) ‑> Dict[str, str]</span>
|
||||
<span>def <span class="ident">run_commands</span></span>(<span>self,<br>nodes_filter: str,<br>commands: list[str],<br>variables: dict[str, typing.Any] | None = None,<br>parallel: int = 10,<br>timeout: int | None = None,<br>folder: str | None = None,<br>prompt: str | None = None,<br>on_node_complete: collections.abc.Callable | None = None,<br>logger: collections.abc.Callable | None = None,<br>name: str | None = None,<br>connect_timeout: int | None = None,<br>auth_timeout: int | None = None) ‑> dict[str, typing.Any]</span>
|
||||
</code></dt>
|
||||
<dd>
|
||||
<details class="source">
|
||||
@@ -206,19 +341,40 @@ el.replaceWith(d);
|
||||
<pre><code class="python">def run_commands(
|
||||
self,
|
||||
nodes_filter: str,
|
||||
commands: List[str],
|
||||
variables: Optional[Dict[str, Any]] = None,
|
||||
commands: list[str],
|
||||
variables: dict[str, Any] | None = None,
|
||||
parallel: int = 10,
|
||||
timeout: int = 20,
|
||||
folder: Optional[str] = None,
|
||||
prompt: Optional[str] = None,
|
||||
on_node_complete: Optional[Callable] = None,
|
||||
logger: Optional[Callable] = None,
|
||||
name: Optional[str] = None
|
||||
) -> Dict[str, str]:
|
||||
|
||||
timeout: int | None = None,
|
||||
folder: str | None = None,
|
||||
prompt: str | None = None,
|
||||
on_node_complete: Callable | None = None,
|
||||
logger: Callable | None = None,
|
||||
name: str | None = None,
|
||||
connect_timeout: int | None = None,
|
||||
auth_timeout: int | None = None,
|
||||
) -> dict[str, Any]:
|
||||
"""Execute commands on a set of nodes."""
|
||||
try:
|
||||
self._ensure_backend_supports_stage_timeouts(
|
||||
connect_timeout=connect_timeout,
|
||||
auth_timeout=auth_timeout,
|
||||
)
|
||||
|
||||
if self._is_remote_backend():
|
||||
stub = self._get_remote_stub()
|
||||
return stub.run_commands(
|
||||
nodes_filter=nodes_filter,
|
||||
commands=commands,
|
||||
variables=variables,
|
||||
parallel=parallel,
|
||||
timeout=timeout if timeout is not None else 20,
|
||||
folder=folder,
|
||||
prompt=prompt,
|
||||
on_node_complete=on_node_complete,
|
||||
logger=logger,
|
||||
name=name or "",
|
||||
)
|
||||
|
||||
matched_names = self.config._getallnodes(nodes_filter)
|
||||
if not matched_names:
|
||||
raise ConnpyError(f"No nodes found matching filter: {nodes_filter}")
|
||||
@@ -227,33 +383,52 @@ el.replaceWith(d);
|
||||
executor = Nodes(node_data, config=self.config)
|
||||
self.last_executor = executor
|
||||
|
||||
results = executor.run(
|
||||
commands=commands,
|
||||
vars=variables,
|
||||
parallel=parallel,
|
||||
timeout=timeout,
|
||||
folder=folder,
|
||||
prompt=prompt,
|
||||
on_complete=on_node_complete,
|
||||
logger=logger
|
||||
)
|
||||
run_kwargs: dict[str, Any] = {
|
||||
"commands": commands,
|
||||
"vars": variables,
|
||||
"parallel": parallel,
|
||||
"timeout": timeout,
|
||||
"folder": folder,
|
||||
"prompt": prompt,
|
||||
"on_complete": on_node_complete,
|
||||
"logger": logger,
|
||||
"connect_timeout": connect_timeout,
|
||||
"auth_timeout": auth_timeout,
|
||||
"_default_cmd_timeout": 20,
|
||||
}
|
||||
run_params = inspect.signature(executor.run).parameters
|
||||
if "unattended" in run_params:
|
||||
run_kwargs["unattended"] = True
|
||||
if "attempt_tracker" in run_params:
|
||||
run_kwargs["attempt_tracker"] = self.attempt_tracker
|
||||
|
||||
# Combine output and status for the caller
|
||||
results = executor.run(**run_kwargs)
|
||||
|
||||
fb_map = getattr(executor, "fallback_used", {})
|
||||
fb_dict = fb_map if isinstance(fb_map, dict) else {}
|
||||
|
||||
# Combine output, status, and fallback metadata for the caller
|
||||
full_results = {}
|
||||
self.last_fallback_used = {}
|
||||
for unique in results:
|
||||
fb_used = bool(fb_dict.get(unique, False))
|
||||
full_results[unique] = {
|
||||
"output": results[unique],
|
||||
"status": executor.status.get(unique, 1)
|
||||
"status": executor.status.get(unique, 1),
|
||||
"fallback_used": fb_used,
|
||||
}
|
||||
self.last_fallback_used[unique] = fb_used
|
||||
|
||||
return full_results
|
||||
except Exception as e:
|
||||
raise ConnpyError(f"Execution failed: {e}")</code></pre>
|
||||
except ConnpyError:
|
||||
raise
|
||||
except Exception as e: # noqa: BLE001
|
||||
raise_sanitized_auth_error(e, code="AUTH_FAILED", context_label="execution failed")</code></pre>
|
||||
</details>
|
||||
<div class="desc"><p>Execute commands on a set of nodes.</p></div>
|
||||
</dd>
|
||||
<dt id="connpy.services.execution_service.ExecutionService.test_commands"><code class="name flex">
|
||||
<span>def <span class="ident">test_commands</span></span>(<span>self,<br>nodes_filter: str,<br>commands: List[str],<br>expected: List[str],<br>variables: Dict[str, Any] | None = None,<br>parallel: int = 10,<br>timeout: int = 20,<br>folder: str | None = None,<br>prompt: str | None = None,<br>on_node_complete: Callable | None = None,<br>logger: Callable | None = None,<br>name: str | None = None) ‑> Dict[str, Dict[str, bool]]</span>
|
||||
<span>def <span class="ident">test_commands</span></span>(<span>self,<br>nodes_filter: str,<br>commands: list[str],<br>expected: list[str],<br>variables: dict[str, typing.Any] | None = None,<br>parallel: int = 10,<br>timeout: int | None = None,<br>folder: str | None = None,<br>prompt: str | None = None,<br>on_node_complete: collections.abc.Callable | None = None,<br>logger: collections.abc.Callable | None = None,<br>name: str | None = None,<br>connect_timeout: int | None = None,<br>auth_timeout: int | None = None) ‑> dict[str, dict[str, bool]]</span>
|
||||
</code></dt>
|
||||
<dd>
|
||||
<details class="source">
|
||||
@@ -263,20 +438,42 @@ el.replaceWith(d);
|
||||
<pre><code class="python">def test_commands(
|
||||
self,
|
||||
nodes_filter: str,
|
||||
commands: List[str],
|
||||
expected: List[str],
|
||||
variables: Optional[Dict[str, Any]] = None,
|
||||
commands: list[str],
|
||||
expected: list[str],
|
||||
variables: dict[str, Any] | None = None,
|
||||
parallel: int = 10,
|
||||
timeout: int = 20,
|
||||
folder: Optional[str] = None,
|
||||
prompt: Optional[str] = None,
|
||||
on_node_complete: Optional[Callable] = None,
|
||||
logger: Optional[Callable] = None,
|
||||
name: Optional[str] = None
|
||||
) -> Dict[str, Dict[str, bool]]:
|
||||
|
||||
timeout: int | None = None,
|
||||
folder: str | None = None,
|
||||
prompt: str | None = None,
|
||||
on_node_complete: Callable | None = None,
|
||||
logger: Callable | None = None,
|
||||
name: str | None = None,
|
||||
connect_timeout: int | None = None,
|
||||
auth_timeout: int | None = None,
|
||||
) -> dict[str, dict[str, bool]]:
|
||||
"""Run commands and verify expected output on a set of nodes."""
|
||||
try:
|
||||
self._ensure_backend_supports_stage_timeouts(
|
||||
connect_timeout=connect_timeout,
|
||||
auth_timeout=auth_timeout,
|
||||
)
|
||||
|
||||
if self._is_remote_backend():
|
||||
stub = self._get_remote_stub()
|
||||
return stub.test_commands(
|
||||
nodes_filter=nodes_filter,
|
||||
commands=commands,
|
||||
expected=expected,
|
||||
variables=variables,
|
||||
parallel=parallel,
|
||||
timeout=timeout if timeout is not None else 20,
|
||||
folder=folder or "",
|
||||
prompt=prompt,
|
||||
on_node_complete=on_node_complete,
|
||||
logger=logger,
|
||||
name=name or "",
|
||||
)
|
||||
|
||||
matched_names = self.config._getallnodes(nodes_filter)
|
||||
if not matched_names:
|
||||
raise ConnpyError(f"No nodes found matching filter: {nodes_filter}")
|
||||
@@ -285,20 +482,38 @@ el.replaceWith(d);
|
||||
executor = Nodes(node_data, config=self.config)
|
||||
self.last_executor = executor
|
||||
|
||||
results = executor.test(
|
||||
commands=commands,
|
||||
expected=expected,
|
||||
vars=variables,
|
||||
parallel=parallel,
|
||||
timeout=timeout,
|
||||
folder=folder,
|
||||
prompt=prompt,
|
||||
on_complete=on_node_complete,
|
||||
logger=logger
|
||||
)
|
||||
return results
|
||||
except Exception as e:
|
||||
raise ConnpyError(f"Testing failed: {e}")</code></pre>
|
||||
test_kwargs: dict[str, Any] = {
|
||||
"commands": commands,
|
||||
"expected": expected,
|
||||
"vars": variables,
|
||||
"parallel": parallel,
|
||||
"timeout": timeout,
|
||||
"folder": folder,
|
||||
"prompt": prompt,
|
||||
"on_complete": on_node_complete,
|
||||
"logger": logger,
|
||||
"connect_timeout": connect_timeout,
|
||||
"auth_timeout": auth_timeout,
|
||||
"_default_cmd_timeout": 20,
|
||||
}
|
||||
test_params = inspect.signature(executor.test).parameters
|
||||
if "unattended" in test_params:
|
||||
test_kwargs["unattended"] = True
|
||||
if "attempt_tracker" in test_params:
|
||||
test_kwargs["attempt_tracker"] = self.attempt_tracker
|
||||
|
||||
res = executor.test(**test_kwargs)
|
||||
fb_map = getattr(executor, "fallback_used", {})
|
||||
fb_dict = fb_map if isinstance(fb_map, dict) else {}
|
||||
if isinstance(res, dict):
|
||||
self.last_fallback_used = {u: bool(fb_dict.get(u, False)) for u in res}
|
||||
else:
|
||||
self.last_fallback_used = {}
|
||||
return res
|
||||
except ConnpyError:
|
||||
raise
|
||||
except Exception as e: # noqa: BLE001
|
||||
raise_sanitized_auth_error(e, code="AUTH_FAILED", context_label="testing failed")</code></pre>
|
||||
</details>
|
||||
<div class="desc"><p>Run commands and verify expected output on a set of nodes.</p></div>
|
||||
</dd>
|
||||
|
||||
+1080
-255
File diff suppressed because it is too large.
Load diff
@@ -56,9 +56,53 @@ el.replaceWith(d);
|
||||
<span>Expand source code</span>
|
||||
</summary>
|
||||
<pre><code class="python">class NodeService(BaseService):
|
||||
_CONNECTION_FIELDS: ClassVar[frozenset[str]] = frozenset({
|
||||
"id",
|
||||
"host",
|
||||
"folder",
|
||||
"subfolder",
|
||||
"options",
|
||||
"logs",
|
||||
"password",
|
||||
"port",
|
||||
"protocol",
|
||||
"user",
|
||||
"tags",
|
||||
"jumphost",
|
||||
"fallback",
|
||||
"type",
|
||||
})
|
||||
|
||||
def __init__(self, config=None):
|
||||
super().__init__(config)
|
||||
|
||||
@staticmethod
|
||||
def _validate_node_payload(data):
|
||||
if not isinstance(data, dict):
|
||||
return
|
||||
from ..auth_utils import (
|
||||
SanitizedAuthError,
|
||||
is_compose_expression,
|
||||
validate_auth_budget_tags,
|
||||
validate_compose_syntax,
|
||||
validate_fallback_entries,
|
||||
)
|
||||
|
||||
try:
|
||||
if "tags" in data and data["tags"] not in ("", None):
|
||||
validate_auth_budget_tags(data["tags"])
|
||||
if "fallback" in data and data["fallback"] not in (None, "", []):
|
||||
validate_fallback_entries(data["fallback"])
|
||||
pw = data.get("password")
|
||||
if is_compose_expression(pw):
|
||||
validate_compose_syntax(pw)
|
||||
elif isinstance(pw, (list, tuple)):
|
||||
for item in pw:
|
||||
if is_compose_expression(item):
|
||||
validate_compose_syntax(item)
|
||||
except (ValueError, TypeError, SanitizedAuthError) as exc:
|
||||
raise InvalidConfigurationError(str(exc)) from None
|
||||
|
||||
|
||||
def list_nodes(self, filter_str=None, format_str=None):
|
||||
"""Return a listed filtered by regex match and formatted if needed."""
|
||||
@@ -135,7 +179,7 @@ el.replaceWith(d);
|
||||
raise NodeNotFoundError(f"Node '{unique_id}' not found.")
|
||||
return details
|
||||
except (KeyError, TypeError):
|
||||
raise NodeNotFoundError(f"Node '{unique_id}' not found.")
|
||||
raise NodeNotFoundError(f"Node '{unique_id}' not found.") from None
|
||||
|
||||
def explode_unique(self, unique_id):
|
||||
"""Explode a unique ID into a dictionary of its parts."""
|
||||
@@ -165,6 +209,40 @@ el.replaceWith(d);
|
||||
"""Logic for adding a new node or folder to configuration."""
|
||||
if not is_folder:
|
||||
self._validate_node_name(unique_id)
|
||||
self._validate_node_payload(data)
|
||||
|
||||
if _supports_transactional_update(self.config):
|
||||
def _mut(cfg):
|
||||
all_nodes = cfg._getallnodes_unlocked()
|
||||
all_folders = cfg._getallfolders_unlocked()
|
||||
if is_folder:
|
||||
if unique_id in all_folders:
|
||||
raise NodeAlreadyExistsError(f"Folder '{unique_id}' already exists.")
|
||||
uniques = cfg._explode_unique_unlocked(unique_id)
|
||||
if not uniques:
|
||||
raise InvalidConfigurationError(f"Invalid folder name '{unique_id}'.")
|
||||
if "subfolder" in uniques:
|
||||
parent_folder = f"@{uniques['folder']}"
|
||||
if parent_folder not in all_folders:
|
||||
raise NodeNotFoundError(f"Folder '{parent_folder}' not found.")
|
||||
cfg._folder_add_unlocked(**uniques)
|
||||
else:
|
||||
if unique_id in all_nodes:
|
||||
raise NodeAlreadyExistsError(f"Node '{unique_id}' already exists.")
|
||||
node_folder = unique_id.partition("@")[2]
|
||||
if node_folder:
|
||||
parent_folder = f"@{node_folder}"
|
||||
if parent_folder not in all_folders:
|
||||
raise NodeNotFoundError(f"Folder '{parent_folder}' not found.")
|
||||
payload = deepcopy(data)
|
||||
if "id" not in payload:
|
||||
uniques = cfg._explode_unique_unlocked(unique_id)
|
||||
if uniques and "id" in uniques:
|
||||
payload["id"] = uniques["id"]
|
||||
cfg._connections_add_unlocked(**payload)
|
||||
|
||||
self.config.transactional_update(_mut)
|
||||
return
|
||||
|
||||
all_nodes = self.config._getallnodes()
|
||||
all_folders = self.config._getallfolders()
|
||||
@@ -176,7 +254,6 @@ el.replaceWith(d);
|
||||
if not uniques:
|
||||
raise InvalidConfigurationError(f"Invalid folder name '{unique_id}'.")
|
||||
|
||||
# Check if parent folder exists when creating a subfolder
|
||||
if "subfolder" in uniques:
|
||||
self.validate_parent_folder(unique_id, is_folder=True)
|
||||
|
||||
@@ -186,37 +263,73 @@ el.replaceWith(d);
|
||||
if unique_id in all_nodes:
|
||||
raise NodeAlreadyExistsError(f"Node '{unique_id}' already exists.")
|
||||
|
||||
# Check if parent folder exists when creating a node in a folder
|
||||
self.validate_parent_folder(unique_id)
|
||||
|
||||
# Ensure 'id' is in data for config._connections_add
|
||||
if "id" not in data:
|
||||
payload = deepcopy(data)
|
||||
if "id" not in payload:
|
||||
uniques = self.config._explode_unique(unique_id)
|
||||
if uniques and "id" in uniques:
|
||||
data["id"] = uniques["id"]
|
||||
payload["id"] = uniques["id"]
|
||||
|
||||
self.config._connections_add(**data)
|
||||
self.config._connections_add(**payload)
|
||||
self.config._saveconfig(self.config.file)
|
||||
|
||||
def _build_update_payload(self, unique_id, data, existing, uniques):
|
||||
if isinstance(existing, dict):
|
||||
merged = deepcopy(existing)
|
||||
if isinstance(uniques, dict):
|
||||
merged.update({k: v for k, v in uniques.items() if v})
|
||||
merged.update(deepcopy(data))
|
||||
filtered = {k: v for k, v in merged.items() if k in self._CONNECTION_FIELDS}
|
||||
else:
|
||||
filtered = deepcopy(data)
|
||||
if "id" not in filtered and isinstance(uniques, dict) and "id" in uniques:
|
||||
filtered["id"] = uniques["id"]
|
||||
self._validate_node_payload(filtered)
|
||||
return filtered
|
||||
|
||||
def update_node(self, unique_id, data, save=True):
|
||||
"""Explicitly update an existing node."""
|
||||
if _supports_transactional_update(self.config) and save:
|
||||
def _mut(cfg):
|
||||
all_nodes = cfg._getallnodes_unlocked()
|
||||
if unique_id not in all_nodes:
|
||||
raise NodeNotFoundError(f"Node '{unique_id}' not found.")
|
||||
uniques = cfg._explode_unique_unlocked(unique_id)
|
||||
existing = cfg.getitem_unlocked(unique_id, extract=False)
|
||||
payload = self._build_update_payload(unique_id, data, existing, uniques)
|
||||
cfg._connections_add_unlocked(**payload)
|
||||
|
||||
self.config.transactional_update(_mut)
|
||||
return
|
||||
|
||||
all_nodes = self.config._getallnodes()
|
||||
if unique_id not in all_nodes:
|
||||
raise NodeNotFoundError(f"Node '{unique_id}' not found.")
|
||||
|
||||
# Ensure 'id' is in data for config._connections_add
|
||||
if "id" not in data:
|
||||
uniques = self.config._explode_unique(unique_id)
|
||||
if uniques:
|
||||
data["id"] = uniques["id"]
|
||||
|
||||
# config._connections_add actually handles updates if ID exists correctly
|
||||
self.config._connections_add(**data)
|
||||
existing = self.config.getitem(unique_id, extract=False)
|
||||
payload = self._build_update_payload(unique_id, data, existing, uniques)
|
||||
self.config._connections_add(**payload)
|
||||
if save:
|
||||
self.config._saveconfig(self.config.file)
|
||||
|
||||
def delete_node(self, unique_id, is_folder=False, save=True):
|
||||
"""Logic for deleting a node or folder."""
|
||||
if _supports_transactional_update(self.config) and save:
|
||||
def _mut(cfg):
|
||||
uniques = cfg._explode_unique_unlocked(unique_id)
|
||||
if not uniques:
|
||||
label = "Folder" if is_folder else "Node"
|
||||
raise NodeNotFoundError(f"{label} '{unique_id}' not found or invalid.")
|
||||
if is_folder:
|
||||
cfg._folder_del_unlocked(**uniques)
|
||||
else:
|
||||
cfg._connections_del_unlocked(**uniques)
|
||||
|
||||
self.config.transactional_update(_mut)
|
||||
return
|
||||
|
||||
if is_folder:
|
||||
uniques = self.config._explode_unique(unique_id)
|
||||
if not uniques:
|
||||
@@ -234,16 +347,33 @@ el.replaceWith(d);
|
||||
def connect_node(self, unique_id, sftp=False, debug=False, logger=None):
|
||||
"""Interact with a node directly."""
|
||||
from connpy.core import node
|
||||
|
||||
from ..auth_utils import resolve_fallback_entries
|
||||
from .profile_service import ProfileService
|
||||
|
||||
node_data = self.config.getitem(unique_id, extract=False)
|
||||
if not node_data:
|
||||
raise NodeNotFoundError(f"Node '{unique_id}' not found.")
|
||||
|
||||
# Resolve profiles
|
||||
profile_service = ProfileService(self.config)
|
||||
resolved_data = profile_service.resolve_node_data(node_data)
|
||||
|
||||
node_params = inspect.signature(node.__init__).parameters
|
||||
if "fallback" not in node_params and "fallback" in resolved_data:
|
||||
raw_fb = resolved_data.pop("fallback", None)
|
||||
if raw_fb not in (None, "", []):
|
||||
resolved_fb = resolve_fallback_entries(raw_fb, self.config.profiles)
|
||||
if resolved_fb:
|
||||
tags_val = resolved_data.get("tags", "")
|
||||
if isinstance(tags_val, dict):
|
||||
tags_copy = deepcopy(tags_val)
|
||||
tags_copy["_fallback"] = resolved_fb
|
||||
resolved_data["tags"] = tags_copy
|
||||
elif tags_val in ("", None):
|
||||
resolved_data["tags"] = {"_fallback": resolved_fb}
|
||||
else:
|
||||
resolved_data["tags"] = {"_raw": tags_val, "_fallback": resolved_fb}
|
||||
|
||||
n = node(unique_id, **resolved_data, config=self.config)
|
||||
if sftp:
|
||||
n.protocol = "sftp"
|
||||
@@ -254,6 +384,29 @@ el.replaceWith(d);
|
||||
"""Move or copy a node."""
|
||||
self._validate_node_name(dst_id)
|
||||
|
||||
if _supports_transactional_update(self.config):
|
||||
def _mut(cfg):
|
||||
try:
|
||||
node_data = cfg.getitem_unlocked(src_id)
|
||||
except (KeyError, TypeError):
|
||||
node_data = None
|
||||
if not node_data:
|
||||
raise NodeNotFoundError(f"Source node '{src_id}' not found.")
|
||||
if dst_id in cfg._getallnodes_unlocked():
|
||||
raise NodeAlreadyExistsError(f"Destination node '{dst_id}' already exists.")
|
||||
new_uniques = cfg._explode_unique_unlocked(dst_id)
|
||||
if not new_uniques:
|
||||
raise InvalidConfigurationError(f"Invalid destination format '{dst_id}'.")
|
||||
new_node_data = deepcopy(node_data)
|
||||
new_node_data.update(new_uniques)
|
||||
cfg._connections_add_unlocked(**new_node_data)
|
||||
if not copy:
|
||||
src_uniques = cfg._explode_unique_unlocked(src_id)
|
||||
cfg._connections_del_unlocked(**src_uniques)
|
||||
|
||||
self.config.transactional_update(_mut)
|
||||
return
|
||||
|
||||
node_data = self.config.getitem(src_id)
|
||||
if not node_data:
|
||||
raise NodeNotFoundError(f"Source node '{src_id}' not found.")
|
||||
@@ -278,6 +431,44 @@ el.replaceWith(d);
|
||||
|
||||
def bulk_add(self, ids, hosts, common_data):
|
||||
"""Add multiple nodes with shared common configuration."""
|
||||
self._validate_node_payload(common_data)
|
||||
|
||||
if _supports_transactional_update(self.config):
|
||||
def _build_entries(cfg):
|
||||
all_nodes = cfg._getallnodes_unlocked()
|
||||
planned = []
|
||||
for i, uid in enumerate(ids):
|
||||
if uid in all_nodes:
|
||||
continue
|
||||
try:
|
||||
self._validate_node_name(uid)
|
||||
except ReservedNameError:
|
||||
continue
|
||||
host = hosts[i] if i < len(hosts) else hosts[0]
|
||||
uniques = cfg._explode_unique_unlocked(uid)
|
||||
if not uniques:
|
||||
continue
|
||||
node_data = deepcopy(common_data)
|
||||
node_data.pop("ids", None)
|
||||
node_data.pop("location", None)
|
||||
node_data.update(uniques)
|
||||
node_data["host"] = host
|
||||
node_data["type"] = "connection"
|
||||
planned.append(node_data)
|
||||
return planned
|
||||
|
||||
with self.config._mutation_lock:
|
||||
if not _build_entries(self.config):
|
||||
return 0
|
||||
|
||||
def _mut(cfg):
|
||||
entries = _build_entries(cfg)
|
||||
for node_data in entries:
|
||||
cfg._connections_add_unlocked(**node_data)
|
||||
return len(entries)
|
||||
|
||||
return self.config.transactional_update(_mut)
|
||||
|
||||
count = 0
|
||||
all_nodes = self.config._getallnodes()
|
||||
|
||||
@@ -288,8 +479,6 @@ el.replaceWith(d);
|
||||
try:
|
||||
self._validate_node_name(uid)
|
||||
except ReservedNameError:
|
||||
# For bulk, we might want to just skip or log.
|
||||
# CLI caller will handle if it wants to be strict.
|
||||
continue
|
||||
|
||||
host = hosts[i] if i < len(hosts) else hosts[0]
|
||||
@@ -313,6 +502,21 @@ el.replaceWith(d);
|
||||
|
||||
def full_replace(self, connections, profiles):
|
||||
"""Replace all connections and profiles with new data."""
|
||||
from ..auth_utils import SanitizedAuthError
|
||||
|
||||
if _supports_transactional_update(self.config):
|
||||
def _mut(cfg):
|
||||
cfg.connections.clear()
|
||||
cfg.connections.update(deepcopy(connections))
|
||||
cfg.profiles.clear()
|
||||
cfg.profiles.update(deepcopy(profiles))
|
||||
|
||||
try:
|
||||
self.config.transactional_update(_mut)
|
||||
except (ValueError, TypeError, SanitizedAuthError) as exc:
|
||||
raise InvalidConfigurationError(str(exc)) from None
|
||||
return
|
||||
|
||||
self.config.connections = connections
|
||||
self.config.profiles = profiles
|
||||
self.config._saveconfig(self.config.file)
|
||||
@@ -350,6 +554,40 @@ el.replaceWith(d);
|
||||
"""Logic for adding a new node or folder to configuration."""
|
||||
if not is_folder:
|
||||
self._validate_node_name(unique_id)
|
||||
self._validate_node_payload(data)
|
||||
|
||||
if _supports_transactional_update(self.config):
|
||||
def _mut(cfg):
|
||||
all_nodes = cfg._getallnodes_unlocked()
|
||||
all_folders = cfg._getallfolders_unlocked()
|
||||
if is_folder:
|
||||
if unique_id in all_folders:
|
||||
raise NodeAlreadyExistsError(f"Folder '{unique_id}' already exists.")
|
||||
uniques = cfg._explode_unique_unlocked(unique_id)
|
||||
if not uniques:
|
||||
raise InvalidConfigurationError(f"Invalid folder name '{unique_id}'.")
|
||||
if "subfolder" in uniques:
|
||||
parent_folder = f"@{uniques['folder']}"
|
||||
if parent_folder not in all_folders:
|
||||
raise NodeNotFoundError(f"Folder '{parent_folder}' not found.")
|
||||
cfg._folder_add_unlocked(**uniques)
|
||||
else:
|
||||
if unique_id in all_nodes:
|
||||
raise NodeAlreadyExistsError(f"Node '{unique_id}' already exists.")
|
||||
node_folder = unique_id.partition("@")[2]
|
||||
if node_folder:
|
||||
parent_folder = f"@{node_folder}"
|
||||
if parent_folder not in all_folders:
|
||||
raise NodeNotFoundError(f"Folder '{parent_folder}' not found.")
|
||||
payload = deepcopy(data)
|
||||
if "id" not in payload:
|
||||
uniques = cfg._explode_unique_unlocked(unique_id)
|
||||
if uniques and "id" in uniques:
|
||||
payload["id"] = uniques["id"]
|
||||
cfg._connections_add_unlocked(**payload)
|
||||
|
||||
self.config.transactional_update(_mut)
|
||||
return
|
||||
|
||||
all_nodes = self.config._getallnodes()
|
||||
all_folders = self.config._getallfolders()
|
||||
@@ -361,7 +599,6 @@ el.replaceWith(d);
|
||||
if not uniques:
|
||||
raise InvalidConfigurationError(f"Invalid folder name '{unique_id}'.")
|
||||
|
||||
# Check if parent folder exists when creating a subfolder
|
||||
if "subfolder" in uniques:
|
||||
self.validate_parent_folder(unique_id, is_folder=True)
|
||||
|
||||
@@ -371,16 +608,15 @@ el.replaceWith(d);
|
||||
if unique_id in all_nodes:
|
||||
raise NodeAlreadyExistsError(f"Node '{unique_id}' already exists.")
|
||||
|
||||
# Check if parent folder exists when creating a node in a folder
|
||||
self.validate_parent_folder(unique_id)
|
||||
|
||||
# Ensure 'id' is in data for config._connections_add
|
||||
if "id" not in data:
|
||||
payload = deepcopy(data)
|
||||
if "id" not in payload:
|
||||
uniques = self.config._explode_unique(unique_id)
|
||||
if uniques and "id" in uniques:
|
||||
data["id"] = uniques["id"]
|
||||
payload["id"] = uniques["id"]
|
||||
|
||||
self.config._connections_add(**data)
|
||||
self.config._connections_add(**payload)
|
||||
self.config._saveconfig(self.config.file)</code></pre>
|
||||
</details>
|
||||
<div class="desc"><p>Logic for adding a new node or folder to configuration.</p></div>
|
||||
@@ -395,6 +631,44 @@ el.replaceWith(d);
|
||||
</summary>
|
||||
<pre><code class="python">def bulk_add(self, ids, hosts, common_data):
|
||||
"""Add multiple nodes with shared common configuration."""
|
||||
self._validate_node_payload(common_data)
|
||||
|
||||
if _supports_transactional_update(self.config):
|
||||
def _build_entries(cfg):
|
||||
all_nodes = cfg._getallnodes_unlocked()
|
||||
planned = []
|
||||
for i, uid in enumerate(ids):
|
||||
if uid in all_nodes:
|
||||
continue
|
||||
try:
|
||||
self._validate_node_name(uid)
|
||||
except ReservedNameError:
|
||||
continue
|
||||
host = hosts[i] if i < len(hosts) else hosts[0]
|
||||
uniques = cfg._explode_unique_unlocked(uid)
|
||||
if not uniques:
|
||||
continue
|
||||
node_data = deepcopy(common_data)
|
||||
node_data.pop("ids", None)
|
||||
node_data.pop("location", None)
|
||||
node_data.update(uniques)
|
||||
node_data["host"] = host
|
||||
node_data["type"] = "connection"
|
||||
planned.append(node_data)
|
||||
return planned
|
||||
|
||||
with self.config._mutation_lock:
|
||||
if not _build_entries(self.config):
|
||||
return 0
|
||||
|
||||
def _mut(cfg):
|
||||
entries = _build_entries(cfg)
|
||||
for node_data in entries:
|
||||
cfg._connections_add_unlocked(**node_data)
|
||||
return len(entries)
|
||||
|
||||
return self.config.transactional_update(_mut)
|
||||
|
||||
count = 0
|
||||
all_nodes = self.config._getallnodes()
|
||||
|
||||
@@ -405,8 +679,6 @@ el.replaceWith(d);
|
||||
try:
|
||||
self._validate_node_name(uid)
|
||||
except ReservedNameError:
|
||||
# For bulk, we might want to just skip or log.
|
||||
# CLI caller will handle if it wants to be strict.
|
||||
continue
|
||||
|
||||
host = hosts[i] if i < len(hosts) else hosts[0]
|
||||
@@ -441,16 +713,33 @@ el.replaceWith(d);
|
||||
<pre><code class="python">def connect_node(self, unique_id, sftp=False, debug=False, logger=None):
|
||||
"""Interact with a node directly."""
|
||||
from connpy.core import node
|
||||
|
||||
from ..auth_utils import resolve_fallback_entries
|
||||
from .profile_service import ProfileService
|
||||
|
||||
node_data = self.config.getitem(unique_id, extract=False)
|
||||
if not node_data:
|
||||
raise NodeNotFoundError(f"Node '{unique_id}' not found.")
|
||||
|
||||
# Resolve profiles
|
||||
profile_service = ProfileService(self.config)
|
||||
resolved_data = profile_service.resolve_node_data(node_data)
|
||||
|
||||
node_params = inspect.signature(node.__init__).parameters
|
||||
if "fallback" not in node_params and "fallback" in resolved_data:
|
||||
raw_fb = resolved_data.pop("fallback", None)
|
||||
if raw_fb not in (None, "", []):
|
||||
resolved_fb = resolve_fallback_entries(raw_fb, self.config.profiles)
|
||||
if resolved_fb:
|
||||
tags_val = resolved_data.get("tags", "")
|
||||
if isinstance(tags_val, dict):
|
||||
tags_copy = deepcopy(tags_val)
|
||||
tags_copy["_fallback"] = resolved_fb
|
||||
resolved_data["tags"] = tags_copy
|
||||
elif tags_val in ("", None):
|
||||
resolved_data["tags"] = {"_fallback": resolved_fb}
|
||||
else:
|
||||
resolved_data["tags"] = {"_raw": tags_val, "_fallback": resolved_fb}
|
||||
|
||||
n = node(unique_id, **resolved_data, config=self.config)
|
||||
if sftp:
|
||||
n.protocol = "sftp"
|
||||
@@ -469,6 +758,20 @@ el.replaceWith(d);
|
||||
</summary>
|
||||
<pre><code class="python">def delete_node(self, unique_id, is_folder=False, save=True):
|
||||
"""Logic for deleting a node or folder."""
|
||||
if _supports_transactional_update(self.config) and save:
|
||||
def _mut(cfg):
|
||||
uniques = cfg._explode_unique_unlocked(unique_id)
|
||||
if not uniques:
|
||||
label = "Folder" if is_folder else "Node"
|
||||
raise NodeNotFoundError(f"{label} '{unique_id}' not found or invalid.")
|
||||
if is_folder:
|
||||
cfg._folder_del_unlocked(**uniques)
|
||||
else:
|
||||
cfg._connections_del_unlocked(**uniques)
|
||||
|
||||
self.config.transactional_update(_mut)
|
||||
return
|
||||
|
||||
if is_folder:
|
||||
uniques = self.config._explode_unique(unique_id)
|
||||
if not uniques:
|
||||
@@ -509,6 +812,21 @@ el.replaceWith(d);
|
||||
</summary>
|
||||
<pre><code class="python">def full_replace(self, connections, profiles):
|
||||
"""Replace all connections and profiles with new data."""
|
||||
from ..auth_utils import SanitizedAuthError
|
||||
|
||||
if _supports_transactional_update(self.config):
|
||||
def _mut(cfg):
|
||||
cfg.connections.clear()
|
||||
cfg.connections.update(deepcopy(connections))
|
||||
cfg.profiles.clear()
|
||||
cfg.profiles.update(deepcopy(profiles))
|
||||
|
||||
try:
|
||||
self.config.transactional_update(_mut)
|
||||
except (ValueError, TypeError, SanitizedAuthError) as exc:
|
||||
raise InvalidConfigurationError(str(exc)) from None
|
||||
return
|
||||
|
||||
self.config.connections = connections
|
||||
self.config.profiles = profiles
|
||||
self.config._saveconfig(self.config.file)</code></pre>
|
||||
@@ -562,7 +880,7 @@ el.replaceWith(d);
|
||||
raise NodeNotFoundError(f"Node '{unique_id}' not found.")
|
||||
return details
|
||||
except (KeyError, TypeError):
|
||||
raise NodeNotFoundError(f"Node '{unique_id}' not found.")</code></pre>
|
||||
raise NodeNotFoundError(f"Node '{unique_id}' not found.") from None</code></pre>
|
||||
</details>
|
||||
<div class="desc"><p>Return full configuration dictionary for a specific node.</p></div>
|
||||
</dd>
|
||||
@@ -665,6 +983,29 @@ el.replaceWith(d);
|
||||
"""Move or copy a node."""
|
||||
self._validate_node_name(dst_id)
|
||||
|
||||
if _supports_transactional_update(self.config):
|
||||
def _mut(cfg):
|
||||
try:
|
||||
node_data = cfg.getitem_unlocked(src_id)
|
||||
except (KeyError, TypeError):
|
||||
node_data = None
|
||||
if not node_data:
|
||||
raise NodeNotFoundError(f"Source node '{src_id}' not found.")
|
||||
if dst_id in cfg._getallnodes_unlocked():
|
||||
raise NodeAlreadyExistsError(f"Destination node '{dst_id}' already exists.")
|
||||
new_uniques = cfg._explode_unique_unlocked(dst_id)
|
||||
if not new_uniques:
|
||||
raise InvalidConfigurationError(f"Invalid destination format '{dst_id}'.")
|
||||
new_node_data = deepcopy(node_data)
|
||||
new_node_data.update(new_uniques)
|
||||
cfg._connections_add_unlocked(**new_node_data)
|
||||
if not copy:
|
||||
src_uniques = cfg._explode_unique_unlocked(src_id)
|
||||
cfg._connections_del_unlocked(**src_uniques)
|
||||
|
||||
self.config.transactional_update(_mut)
|
||||
return
|
||||
|
||||
node_data = self.config.getitem(src_id)
|
||||
if not node_data:
|
||||
raise NodeNotFoundError(f"Source node '{src_id}' not found.")
|
||||
@@ -699,18 +1040,27 @@ el.replaceWith(d);
|
||||
</summary>
|
||||
<pre><code class="python">def update_node(self, unique_id, data, save=True):
|
||||
"""Explicitly update an existing node."""
|
||||
if _supports_transactional_update(self.config) and save:
|
||||
def _mut(cfg):
|
||||
all_nodes = cfg._getallnodes_unlocked()
|
||||
if unique_id not in all_nodes:
|
||||
raise NodeNotFoundError(f"Node '{unique_id}' not found.")
|
||||
uniques = cfg._explode_unique_unlocked(unique_id)
|
||||
existing = cfg.getitem_unlocked(unique_id, extract=False)
|
||||
payload = self._build_update_payload(unique_id, data, existing, uniques)
|
||||
cfg._connections_add_unlocked(**payload)
|
||||
|
||||
self.config.transactional_update(_mut)
|
||||
return
|
||||
|
||||
all_nodes = self.config._getallnodes()
|
||||
if unique_id not in all_nodes:
|
||||
raise NodeNotFoundError(f"Node '{unique_id}' not found.")
|
||||
|
||||
# Ensure 'id' is in data for config._connections_add
|
||||
if "id" not in data:
|
||||
uniques = self.config._explode_unique(unique_id)
|
||||
if uniques:
|
||||
data["id"] = uniques["id"]
|
||||
|
||||
# config._connections_add actually handles updates if ID exists correctly
|
||||
self.config._connections_add(**data)
|
||||
existing = self.config.getitem(unique_id, extract=False)
|
||||
payload = self._build_update_payload(unique_id, data, existing, uniques)
|
||||
self.config._connections_add(**payload)
|
||||
if save:
|
||||
self.config._saveconfig(self.config.file)</code></pre>
|
||||
</details>
|
||||
|
||||
@@ -58,61 +58,194 @@ el.replaceWith(d);
|
||||
<pre><code class="python">class ProfileService(BaseService):
|
||||
"""Business logic for node profiles management."""
|
||||
|
||||
_ALLOWED_KEYS: ClassVar[frozenset[str]] = frozenset({
|
||||
"host",
|
||||
"options",
|
||||
"logs",
|
||||
"password",
|
||||
"port",
|
||||
"protocol",
|
||||
"user",
|
||||
"tags",
|
||||
"jumphost",
|
||||
"fallback",
|
||||
})
|
||||
|
||||
@staticmethod
|
||||
def _validate_profile_payload(data):
|
||||
from ..auth_utils import (
|
||||
SanitizedAuthError,
|
||||
is_compose_expression,
|
||||
validate_auth_budget_tags,
|
||||
validate_compose_syntax,
|
||||
validate_fallback_entries,
|
||||
)
|
||||
|
||||
try:
|
||||
if "tags" in data and data["tags"] not in ("", None):
|
||||
validate_auth_budget_tags(data["tags"])
|
||||
if "fallback" in data and data["fallback"] not in (None, "", []):
|
||||
validate_fallback_entries(data["fallback"])
|
||||
pw = data.get("password")
|
||||
if is_compose_expression(pw):
|
||||
validate_compose_syntax(pw)
|
||||
elif isinstance(pw, (list, tuple)):
|
||||
for item in pw:
|
||||
if is_compose_expression(item):
|
||||
validate_compose_syntax(item)
|
||||
except (ValueError, TypeError, SanitizedAuthError) as exc:
|
||||
raise InvalidConfigurationError(str(exc)) from None
|
||||
|
||||
def _get_profiles_snapshot(self):
|
||||
lock = getattr(self.config, "_mutation_lock", None)
|
||||
if lock is not None and not hasattr(self.config, "assert_called"):
|
||||
with lock:
|
||||
return deepcopy(self.config.profiles)
|
||||
return deepcopy(self.config.profiles)
|
||||
|
||||
def list_profiles(self, filter_str=None):
|
||||
"""List all profile names, optionally filtered."""
|
||||
profiles = list(self.config.profiles.keys())
|
||||
profiles = list(self._get_profiles_snapshot().keys())
|
||||
case_sensitive = self.config.config.get("case", False)
|
||||
|
||||
if filter_str:
|
||||
if not case_sensitive:
|
||||
f_str = filter_str.lower()
|
||||
return [p for p in profiles if f_str in p.lower()]
|
||||
else:
|
||||
return [p for p in profiles if filter_str in p]
|
||||
return profiles
|
||||
|
||||
def get_profile(self, name, resolve=True):
|
||||
def get_profile(self, name, resolve=True, *, _visited=frozenset(), _depth=0):
|
||||
"""Get the profile dictionary, optionally resolved."""
|
||||
profile = self.config.profiles.get(name)
|
||||
if _depth >= MAX_PROFILE_DEPTH or name in _visited:
|
||||
raise InvalidConfigurationError(
|
||||
f"Circular or overly deep profile reference involving '{name}'."
|
||||
)
|
||||
profiles = self._get_profiles_snapshot()
|
||||
profile = profiles.get(name)
|
||||
if not profile:
|
||||
raise ProfileNotFoundError(f"Profile '{name}' not found.")
|
||||
|
||||
if resolve:
|
||||
return self.resolve_node_data(profile)
|
||||
return self.resolve_node_data(
|
||||
profile,
|
||||
_visited=_visited | {name},
|
||||
_depth=_depth + 1,
|
||||
_profiles_snapshot=profiles,
|
||||
)
|
||||
return profile
|
||||
|
||||
def add_profile(self, name, data):
|
||||
"""Add a new profile."""
|
||||
filtered_data = {k: deepcopy(v) for k, v in data.items() if k in self._ALLOWED_KEYS}
|
||||
self._validate_profile_payload(filtered_data)
|
||||
|
||||
if _supports_transactional_update(self.config):
|
||||
def _mut(cfg):
|
||||
if name in cfg.profiles:
|
||||
raise ProfileAlreadyExistsError(f"Profile '{name}' already exists.")
|
||||
cfg._profiles_add_unlocked(id=name, **filtered_data)
|
||||
|
||||
self.config.transactional_update(_mut)
|
||||
return
|
||||
|
||||
if name in self.config.profiles:
|
||||
raise ProfileAlreadyExistsError(f"Profile '{name}' already exists.")
|
||||
|
||||
# Filter data to match _profiles_add signature and ensure id is passed
|
||||
allowed_keys = {"host", "options", "logs", "password", "port", "protocol", "user", "tags", "jumphost"}
|
||||
filtered_data = {k: v for k, v in data.items() if k in allowed_keys}
|
||||
|
||||
self.config._profiles_add(id=name, **filtered_data)
|
||||
self.config._saveconfig(self.config.file)
|
||||
|
||||
def resolve_node_data(self, node_data):
|
||||
"""Resolve profile references (@profile) in node data and handle inheritance."""
|
||||
resolved = node_data.copy()
|
||||
def resolve_node_data(
|
||||
self,
|
||||
node_data,
|
||||
*,
|
||||
_visited=frozenset(),
|
||||
_depth=0,
|
||||
_profiles_snapshot=None,
|
||||
):
|
||||
"""Resolve profile references (@profile) in node data while preserving compose and scalar secrets."""
|
||||
from ..auth_utils import is_compose_expression
|
||||
|
||||
# 1. Identify all referenced profiles to support inheritance
|
||||
referenced_profiles = []
|
||||
for value in resolved.values():
|
||||
if isinstance(value, str) and value.startswith("@"):
|
||||
referenced_profiles.append(value[1:])
|
||||
elif isinstance(value, list):
|
||||
for item in value:
|
||||
if isinstance(item, str) and item.startswith("@"):
|
||||
referenced_profiles.append(item[1:])
|
||||
if _depth > MAX_PROFILE_DEPTH:
|
||||
raise InvalidConfigurationError("Profile inheritance exceeds maximum depth.")
|
||||
|
||||
# 2. Resolve explicit references
|
||||
profiles = (
|
||||
_profiles_snapshot
|
||||
if _profiles_snapshot is not None
|
||||
else self._get_profiles_snapshot()
|
||||
)
|
||||
resolved = deepcopy(node_data)
|
||||
|
||||
auth_referenced_profiles = []
|
||||
other_referenced_profiles = []
|
||||
for key, value in resolved.items():
|
||||
if key == "fallback":
|
||||
continue
|
||||
target_refs = (
|
||||
auth_referenced_profiles if key in ("user", "password") else other_referenced_profiles
|
||||
)
|
||||
if isinstance(value, str):
|
||||
if not is_compose_expression(value) and value.startswith("@"):
|
||||
ref = value[1:]
|
||||
if key != "password" or ref in profiles:
|
||||
target_refs.append(ref)
|
||||
elif isinstance(value, (list, tuple)):
|
||||
for item in value:
|
||||
if (
|
||||
isinstance(item, str)
|
||||
and not is_compose_expression(item)
|
||||
and item.startswith("@")
|
||||
):
|
||||
ref = item[1:]
|
||||
if key != "password" or ref in profiles:
|
||||
target_refs.append(ref)
|
||||
|
||||
def _get_resolved_ref(prof_name):
|
||||
if prof_name in _visited or _depth >= MAX_PROFILE_DEPTH:
|
||||
raise InvalidConfigurationError(
|
||||
f"Circular or overly deep profile reference involving '{prof_name}'."
|
||||
)
|
||||
prof_raw = profiles.get(prof_name)
|
||||
if not prof_raw:
|
||||
raise ProfileNotFoundError(f"Profile '{prof_name}' not found.")
|
||||
return self.resolve_node_data(
|
||||
prof_raw,
|
||||
_visited=_visited | {prof_name},
|
||||
_depth=_depth + 1,
|
||||
_profiles_snapshot=profiles,
|
||||
)
|
||||
|
||||
for key, value in list(resolved.items()):
|
||||
if key == "fallback":
|
||||
continue
|
||||
if key == "password":
|
||||
if isinstance(value, str):
|
||||
if is_compose_expression(value):
|
||||
continue
|
||||
if value.startswith("@") and value[1:] in profiles:
|
||||
prof_obj = _get_resolved_ref(value[1:])
|
||||
resolved["password"] = prof_obj.get("password", "")
|
||||
elif isinstance(value, list):
|
||||
resolved_list = []
|
||||
for item in value:
|
||||
if (
|
||||
isinstance(item, str)
|
||||
and not is_compose_expression(item)
|
||||
and item.startswith("@")
|
||||
and item[1:] in profiles
|
||||
):
|
||||
with suppress(ProfileNotFoundError):
|
||||
prof_obj = _get_resolved_ref(item[1:])
|
||||
if "password" in prof_obj:
|
||||
resolved_list.append(prof_obj["password"])
|
||||
else:
|
||||
resolved_list.append(item)
|
||||
resolved["password"] = resolved_list
|
||||
continue
|
||||
|
||||
if isinstance(value, str) and value.startswith("@"):
|
||||
profile_name = value[1:]
|
||||
try:
|
||||
profile = self.get_profile(profile_name, resolve=True)
|
||||
profile = _get_resolved_ref(profile_name)
|
||||
resolved[key] = profile.get(key, "")
|
||||
except ProfileNotFoundError:
|
||||
resolved[key] = ""
|
||||
@@ -121,33 +254,28 @@ el.replaceWith(d);
|
||||
for item in value:
|
||||
if isinstance(item, str) and item.startswith("@"):
|
||||
profile_name = item[1:]
|
||||
try:
|
||||
profile = self.get_profile(profile_name, resolve=True)
|
||||
with suppress(ProfileNotFoundError):
|
||||
profile = _get_resolved_ref(profile_name)
|
||||
if "password" in profile:
|
||||
resolved_list.append(profile["password"])
|
||||
except ProfileNotFoundError:
|
||||
pass
|
||||
else:
|
||||
resolved_list.append(item)
|
||||
resolved[key] = resolved_list
|
||||
|
||||
# 3. Inheritance: Fill empty keys from the first referenced profile
|
||||
if referenced_profiles:
|
||||
base_profile_name = referenced_profiles[0]
|
||||
try:
|
||||
base_profile = self.get_profile(base_profile_name, resolve=True)
|
||||
ordered_profiles = list(dict.fromkeys(auth_referenced_profiles + other_referenced_profiles))
|
||||
for base_profile_name in ordered_profiles:
|
||||
with suppress(ProfileNotFoundError):
|
||||
base_profile = _get_resolved_ref(base_profile_name)
|
||||
for key, value in base_profile.items():
|
||||
# Fill if key is missing or empty
|
||||
if key not in resolved or resolved[key] == "" or resolved[key] == [] or resolved[key] is None:
|
||||
resolved[key] = value
|
||||
except ProfileNotFoundError:
|
||||
pass
|
||||
if (key not in resolved or resolved[key] in ("", [], None)) and (
|
||||
value not in ("", [], None) or key not in resolved
|
||||
):
|
||||
resolved[key] = deepcopy(value)
|
||||
|
||||
# 4. Handle default protocol
|
||||
if resolved.get("protocol") == "" or resolved.get("protocol") is None:
|
||||
if resolved.get("protocol") in ("", None):
|
||||
try:
|
||||
default_profile = self.get_profile("default", resolve=True)
|
||||
resolved["protocol"] = default_profile.get("protocol", "ssh")
|
||||
default_profile = _get_resolved_ref("default")
|
||||
resolved["protocol"] = default_profile.get("protocol", "ssh") or "ssh"
|
||||
except ProfileNotFoundError:
|
||||
resolved["protocol"] = "ssh"
|
||||
|
||||
@@ -155,34 +283,58 @@ el.replaceWith(d);
|
||||
|
||||
def delete_profile(self, name):
|
||||
"""Delete an existing profile, with safety checks."""
|
||||
if name not in self.config.profiles:
|
||||
if _supports_transactional_update(self.config):
|
||||
def _mut(cfg):
|
||||
if name not in cfg.profiles:
|
||||
raise ProfileNotFoundError(f"Profile '{name}' not found.")
|
||||
|
||||
if name == "default":
|
||||
raise InvalidConfigurationError("Cannot delete the 'default' profile.")
|
||||
used_by = cfg._profileused_unlocked(name)
|
||||
if used_by:
|
||||
raise InvalidConfigurationError(
|
||||
f"Profile '{name}' is used by nodes: {', '.join(used_by)}"
|
||||
)
|
||||
cfg._profiles_del_unlocked(id=name)
|
||||
|
||||
self.config.transactional_update(_mut)
|
||||
return
|
||||
|
||||
if name not in self.config.profiles:
|
||||
raise ProfileNotFoundError(f"Profile '{name}' not found.")
|
||||
if name == "default":
|
||||
raise InvalidConfigurationError("Cannot delete the 'default' profile.")
|
||||
used_by = self.config._profileused(name)
|
||||
if used_by:
|
||||
# We return the list of nodes using it so the UI can inform the user
|
||||
raise InvalidConfigurationError(f"Profile '{name}' is used by nodes: {', '.join(used_by)}")
|
||||
|
||||
raise InvalidConfigurationError(
|
||||
f"Profile '{name}' is used by nodes: {', '.join(used_by)}"
|
||||
)
|
||||
self.config._profiles_del(id=name)
|
||||
self.config._saveconfig(self.config.file)
|
||||
|
||||
def update_profile(self, name, data):
|
||||
"""Update an existing profile."""
|
||||
if _supports_transactional_update(self.config):
|
||||
def _mut(cfg):
|
||||
if name not in cfg.profiles:
|
||||
raise ProfileNotFoundError(f"Profile '{name}' not found.")
|
||||
updated_data = deepcopy(cfg.profiles[name])
|
||||
updated_data.update(deepcopy(data))
|
||||
filtered_data = {
|
||||
k: v for k, v in updated_data.items() if k in self._ALLOWED_KEYS
|
||||
}
|
||||
self._validate_profile_payload(filtered_data)
|
||||
cfg._profiles_add_unlocked(id=name, **filtered_data)
|
||||
|
||||
self.config.transactional_update(_mut)
|
||||
return
|
||||
|
||||
if name not in self.config.profiles:
|
||||
raise ProfileNotFoundError(f"Profile '{name}' not found.")
|
||||
|
||||
# Merge with existing data
|
||||
existing = self.get_profile(name, resolve=False)
|
||||
updated_data = existing.copy()
|
||||
updated_data = deepcopy(existing)
|
||||
updated_data.update(data)
|
||||
|
||||
# Filter data to match _profiles_add signature
|
||||
allowed_keys = {"host", "options", "logs", "password", "port", "protocol", "user", "tags", "jumphost"}
|
||||
filtered_data = {k: v for k, v in updated_data.items() if k in allowed_keys}
|
||||
|
||||
filtered_data = {k: v for k, v in updated_data.items() if k in self._ALLOWED_KEYS}
|
||||
self._validate_profile_payload(filtered_data)
|
||||
self.config._profiles_add(id=name, **filtered_data)
|
||||
self.config._saveconfig(self.config.file)</code></pre>
|
||||
</details>
|
||||
@@ -210,13 +362,20 @@ el.replaceWith(d);
|
||||
</summary>
|
||||
<pre><code class="python">def add_profile(self, name, data):
|
||||
"""Add a new profile."""
|
||||
filtered_data = {k: deepcopy(v) for k, v in data.items() if k in self._ALLOWED_KEYS}
|
||||
self._validate_profile_payload(filtered_data)
|
||||
|
||||
if _supports_transactional_update(self.config):
|
||||
def _mut(cfg):
|
||||
if name in cfg.profiles:
|
||||
raise ProfileAlreadyExistsError(f"Profile '{name}' already exists.")
|
||||
cfg._profiles_add_unlocked(id=name, **filtered_data)
|
||||
|
||||
self.config.transactional_update(_mut)
|
||||
return
|
||||
|
||||
if name in self.config.profiles:
|
||||
raise ProfileAlreadyExistsError(f"Profile '{name}' already exists.")
|
||||
|
||||
# Filter data to match _profiles_add signature and ensure id is passed
|
||||
allowed_keys = {"host", "options", "logs", "password", "port", "protocol", "user", "tags", "jumphost"}
|
||||
filtered_data = {k: v for k, v in data.items() if k in allowed_keys}
|
||||
|
||||
self.config._profiles_add(id=name, **filtered_data)
|
||||
self.config._saveconfig(self.config.file)</code></pre>
|
||||
</details>
|
||||
@@ -232,17 +391,31 @@ el.replaceWith(d);
|
||||
</summary>
|
||||
<pre><code class="python">def delete_profile(self, name):
|
||||
"""Delete an existing profile, with safety checks."""
|
||||
if name not in self.config.profiles:
|
||||
if _supports_transactional_update(self.config):
|
||||
def _mut(cfg):
|
||||
if name not in cfg.profiles:
|
||||
raise ProfileNotFoundError(f"Profile '{name}' not found.")
|
||||
|
||||
if name == "default":
|
||||
raise InvalidConfigurationError("Cannot delete the 'default' profile.")
|
||||
used_by = cfg._profileused_unlocked(name)
|
||||
if used_by:
|
||||
raise InvalidConfigurationError(
|
||||
f"Profile '{name}' is used by nodes: {', '.join(used_by)}"
|
||||
)
|
||||
cfg._profiles_del_unlocked(id=name)
|
||||
|
||||
self.config.transactional_update(_mut)
|
||||
return
|
||||
|
||||
if name not in self.config.profiles:
|
||||
raise ProfileNotFoundError(f"Profile '{name}' not found.")
|
||||
if name == "default":
|
||||
raise InvalidConfigurationError("Cannot delete the 'default' profile.")
|
||||
used_by = self.config._profileused(name)
|
||||
if used_by:
|
||||
# We return the list of nodes using it so the UI can inform the user
|
||||
raise InvalidConfigurationError(f"Profile '{name}' is used by nodes: {', '.join(used_by)}")
|
||||
|
||||
raise InvalidConfigurationError(
|
||||
f"Profile '{name}' is used by nodes: {', '.join(used_by)}"
|
||||
)
|
||||
self.config._profiles_del(id=name)
|
||||
self.config._saveconfig(self.config.file)</code></pre>
|
||||
</details>
|
||||
@@ -256,14 +429,24 @@ el.replaceWith(d);
|
||||
<summary>
|
||||
<span>Expand source code</span>
|
||||
</summary>
|
||||
<pre><code class="python">def get_profile(self, name, resolve=True):
|
||||
<pre><code class="python">def get_profile(self, name, resolve=True, *, _visited=frozenset(), _depth=0):
|
||||
"""Get the profile dictionary, optionally resolved."""
|
||||
profile = self.config.profiles.get(name)
|
||||
if _depth >= MAX_PROFILE_DEPTH or name in _visited:
|
||||
raise InvalidConfigurationError(
|
||||
f"Circular or overly deep profile reference involving '{name}'."
|
||||
)
|
||||
profiles = self._get_profiles_snapshot()
|
||||
profile = profiles.get(name)
|
||||
if not profile:
|
||||
raise ProfileNotFoundError(f"Profile '{name}' not found.")
|
||||
|
||||
if resolve:
|
||||
return self.resolve_node_data(profile)
|
||||
return self.resolve_node_data(
|
||||
profile,
|
||||
_visited=_visited | {name},
|
||||
_depth=_depth + 1,
|
||||
_profiles_snapshot=profiles,
|
||||
)
|
||||
return profile</code></pre>
|
||||
</details>
|
||||
<div class="desc"><p>Get the profile dictionary, optionally resolved.</p></div>
|
||||
@@ -278,14 +461,13 @@ el.replaceWith(d);
|
||||
</summary>
|
||||
<pre><code class="python">def list_profiles(self, filter_str=None):
|
||||
"""List all profile names, optionally filtered."""
|
||||
profiles = list(self.config.profiles.keys())
|
||||
profiles = list(self._get_profiles_snapshot().keys())
|
||||
case_sensitive = self.config.config.get("case", False)
|
||||
|
||||
if filter_str:
|
||||
if not case_sensitive:
|
||||
f_str = filter_str.lower()
|
||||
return [p for p in profiles if f_str in p.lower()]
|
||||
else:
|
||||
return [p for p in profiles if filter_str in p]
|
||||
return profiles</code></pre>
|
||||
</details>
|
||||
@@ -299,26 +481,98 @@ el.replaceWith(d);
|
||||
<summary>
|
||||
<span>Expand source code</span>
|
||||
</summary>
|
||||
<pre><code class="python">def resolve_node_data(self, node_data):
|
||||
"""Resolve profile references (@profile) in node data and handle inheritance."""
|
||||
resolved = node_data.copy()
|
||||
<pre><code class="python">def resolve_node_data(
|
||||
self,
|
||||
node_data,
|
||||
*,
|
||||
_visited=frozenset(),
|
||||
_depth=0,
|
||||
_profiles_snapshot=None,
|
||||
):
|
||||
"""Resolve profile references (@profile) in node data while preserving compose and scalar secrets."""
|
||||
from ..auth_utils import is_compose_expression
|
||||
|
||||
# 1. Identify all referenced profiles to support inheritance
|
||||
referenced_profiles = []
|
||||
for value in resolved.values():
|
||||
if isinstance(value, str) and value.startswith("@"):
|
||||
referenced_profiles.append(value[1:])
|
||||
elif isinstance(value, list):
|
||||
for item in value:
|
||||
if isinstance(item, str) and item.startswith("@"):
|
||||
referenced_profiles.append(item[1:])
|
||||
if _depth > MAX_PROFILE_DEPTH:
|
||||
raise InvalidConfigurationError("Profile inheritance exceeds maximum depth.")
|
||||
|
||||
# 2. Resolve explicit references
|
||||
profiles = (
|
||||
_profiles_snapshot
|
||||
if _profiles_snapshot is not None
|
||||
else self._get_profiles_snapshot()
|
||||
)
|
||||
resolved = deepcopy(node_data)
|
||||
|
||||
auth_referenced_profiles = []
|
||||
other_referenced_profiles = []
|
||||
for key, value in resolved.items():
|
||||
if key == "fallback":
|
||||
continue
|
||||
target_refs = (
|
||||
auth_referenced_profiles if key in ("user", "password") else other_referenced_profiles
|
||||
)
|
||||
if isinstance(value, str):
|
||||
if not is_compose_expression(value) and value.startswith("@"):
|
||||
ref = value[1:]
|
||||
if key != "password" or ref in profiles:
|
||||
target_refs.append(ref)
|
||||
elif isinstance(value, (list, tuple)):
|
||||
for item in value:
|
||||
if (
|
||||
isinstance(item, str)
|
||||
and not is_compose_expression(item)
|
||||
and item.startswith("@")
|
||||
):
|
||||
ref = item[1:]
|
||||
if key != "password" or ref in profiles:
|
||||
target_refs.append(ref)
|
||||
|
||||
def _get_resolved_ref(prof_name):
|
||||
if prof_name in _visited or _depth >= MAX_PROFILE_DEPTH:
|
||||
raise InvalidConfigurationError(
|
||||
f"Circular or overly deep profile reference involving '{prof_name}'."
|
||||
)
|
||||
prof_raw = profiles.get(prof_name)
|
||||
if not prof_raw:
|
||||
raise ProfileNotFoundError(f"Profile '{prof_name}' not found.")
|
||||
return self.resolve_node_data(
|
||||
prof_raw,
|
||||
_visited=_visited | {prof_name},
|
||||
_depth=_depth + 1,
|
||||
_profiles_snapshot=profiles,
|
||||
)
|
||||
|
||||
for key, value in list(resolved.items()):
|
||||
if key == "fallback":
|
||||
continue
|
||||
if key == "password":
|
||||
if isinstance(value, str):
|
||||
if is_compose_expression(value):
|
||||
continue
|
||||
if value.startswith("@") and value[1:] in profiles:
|
||||
prof_obj = _get_resolved_ref(value[1:])
|
||||
resolved["password"] = prof_obj.get("password", "")
|
||||
elif isinstance(value, list):
|
||||
resolved_list = []
|
||||
for item in value:
|
||||
if (
|
||||
isinstance(item, str)
|
||||
and not is_compose_expression(item)
|
||||
and item.startswith("@")
|
||||
and item[1:] in profiles
|
||||
):
|
||||
with suppress(ProfileNotFoundError):
|
||||
prof_obj = _get_resolved_ref(item[1:])
|
||||
if "password" in prof_obj:
|
||||
resolved_list.append(prof_obj["password"])
|
||||
else:
|
||||
resolved_list.append(item)
|
||||
resolved["password"] = resolved_list
|
||||
continue
|
||||
|
||||
if isinstance(value, str) and value.startswith("@"):
|
||||
profile_name = value[1:]
|
||||
try:
|
||||
profile = self.get_profile(profile_name, resolve=True)
|
||||
profile = _get_resolved_ref(profile_name)
|
||||
resolved[key] = profile.get(key, "")
|
||||
except ProfileNotFoundError:
|
||||
resolved[key] = ""
|
||||
@@ -327,39 +581,34 @@ el.replaceWith(d);
|
||||
for item in value:
|
||||
if isinstance(item, str) and item.startswith("@"):
|
||||
profile_name = item[1:]
|
||||
try:
|
||||
profile = self.get_profile(profile_name, resolve=True)
|
||||
with suppress(ProfileNotFoundError):
|
||||
profile = _get_resolved_ref(profile_name)
|
||||
if "password" in profile:
|
||||
resolved_list.append(profile["password"])
|
||||
except ProfileNotFoundError:
|
||||
pass
|
||||
else:
|
||||
resolved_list.append(item)
|
||||
resolved[key] = resolved_list
|
||||
|
||||
# 3. Inheritance: Fill empty keys from the first referenced profile
|
||||
if referenced_profiles:
|
||||
base_profile_name = referenced_profiles[0]
|
||||
try:
|
||||
base_profile = self.get_profile(base_profile_name, resolve=True)
|
||||
ordered_profiles = list(dict.fromkeys(auth_referenced_profiles + other_referenced_profiles))
|
||||
for base_profile_name in ordered_profiles:
|
||||
with suppress(ProfileNotFoundError):
|
||||
base_profile = _get_resolved_ref(base_profile_name)
|
||||
for key, value in base_profile.items():
|
||||
# Fill if key is missing or empty
|
||||
if key not in resolved or resolved[key] == "" or resolved[key] == [] or resolved[key] is None:
|
||||
resolved[key] = value
|
||||
except ProfileNotFoundError:
|
||||
pass
|
||||
if (key not in resolved or resolved[key] in ("", [], None)) and (
|
||||
value not in ("", [], None) or key not in resolved
|
||||
):
|
||||
resolved[key] = deepcopy(value)
|
||||
|
||||
# 4. Handle default protocol
|
||||
if resolved.get("protocol") == "" or resolved.get("protocol") is None:
|
||||
if resolved.get("protocol") in ("", None):
|
||||
try:
|
||||
default_profile = self.get_profile("default", resolve=True)
|
||||
resolved["protocol"] = default_profile.get("protocol", "ssh")
|
||||
default_profile = _get_resolved_ref("default")
|
||||
resolved["protocol"] = default_profile.get("protocol", "ssh") or "ssh"
|
||||
except ProfileNotFoundError:
|
||||
resolved["protocol"] = "ssh"
|
||||
|
||||
return resolved</code></pre>
|
||||
</details>
|
||||
<div class="desc"><p>Resolve profile references (@profile) in node data and handle inheritance.</p></div>
|
||||
<div class="desc"><p>Resolve profile references (@profile) in node data while preserving compose and scalar secrets.</p></div>
|
||||
</dd>
|
||||
<dt id="connpy.services.profile_service.ProfileService.update_profile"><code class="name flex">
|
||||
<span>def <span class="ident">update_profile</span></span>(<span>self, name, data)</span>
|
||||
@@ -371,18 +620,28 @@ el.replaceWith(d);
|
||||
</summary>
|
||||
<pre><code class="python">def update_profile(self, name, data):
|
||||
"""Update an existing profile."""
|
||||
if _supports_transactional_update(self.config):
|
||||
def _mut(cfg):
|
||||
if name not in cfg.profiles:
|
||||
raise ProfileNotFoundError(f"Profile '{name}' not found.")
|
||||
updated_data = deepcopy(cfg.profiles[name])
|
||||
updated_data.update(deepcopy(data))
|
||||
filtered_data = {
|
||||
k: v for k, v in updated_data.items() if k in self._ALLOWED_KEYS
|
||||
}
|
||||
self._validate_profile_payload(filtered_data)
|
||||
cfg._profiles_add_unlocked(id=name, **filtered_data)
|
||||
|
||||
self.config.transactional_update(_mut)
|
||||
return
|
||||
|
||||
if name not in self.config.profiles:
|
||||
raise ProfileNotFoundError(f"Profile '{name}' not found.")
|
||||
|
||||
# Merge with existing data
|
||||
existing = self.get_profile(name, resolve=False)
|
||||
updated_data = existing.copy()
|
||||
updated_data = deepcopy(existing)
|
||||
updated_data.update(data)
|
||||
|
||||
# Filter data to match _profiles_add signature
|
||||
allowed_keys = {"host", "options", "logs", "password", "port", "protocol", "user", "tags", "jumphost"}
|
||||
filtered_data = {k: v for k, v in updated_data.items() if k in allowed_keys}
|
||||
|
||||
filtered_data = {k: v for k, v in updated_data.items() if k in self._ALLOWED_KEYS}
|
||||
self._validate_profile_payload(filtered_data)
|
||||
self.config._profiles_add(id=name, **filtered_data)
|
||||
self.config._saveconfig(self.config.file)</code></pre>
|
||||
</details>
|
||||
|
||||
@@ -76,9 +76,12 @@ el.replaceWith(d);
|
||||
"""Dynamic service backend. Transparently provides local or remote services."""
|
||||
|
||||
def __init__(self, config, mode="local", remote_host=None):
|
||||
from ..auth_utils import AuthAttemptTracker
|
||||
|
||||
self.mode = mode
|
||||
self.config = config
|
||||
self.remote_host = remote_host
|
||||
self.auth_tracker = AuthAttemptTracker()
|
||||
self._system = None
|
||||
self._execution = None
|
||||
self._import_export = None
|
||||
@@ -94,11 +97,11 @@ el.replaceWith(d);
|
||||
raise ValueError(f"Unknown service mode: {mode}")
|
||||
|
||||
def _init_local(self):
|
||||
from .node_service import NodeService
|
||||
from .profile_service import ProfileService
|
||||
from .config_service import ConfigService
|
||||
from .plugin_service import PluginService
|
||||
from .context_service import ContextService
|
||||
from .node_service import NodeService
|
||||
from .plugin_service import PluginService
|
||||
from .profile_service import ProfileService
|
||||
|
||||
self.nodes = NodeService(self.config)
|
||||
self.profiles = ProfileService(self.config)
|
||||
@@ -110,18 +113,28 @@ el.replaceWith(d);
|
||||
# Allow ConfigService to work locally so the user can revert the mode
|
||||
from .config_service import ConfigService
|
||||
from .context_service import ContextService
|
||||
|
||||
self.config_svc = ConfigService(self.config)
|
||||
self.context = ContextService(self.config)
|
||||
|
||||
if not self.remote_host:
|
||||
raise InvalidConfigurationError("Remote host must be specified in remote mode")
|
||||
|
||||
import grpc
|
||||
import os
|
||||
|
||||
import grpc
|
||||
|
||||
from ..grpc_layer.stubs import (
|
||||
NodeStub, ProfileStub, PluginStub, AIStub,
|
||||
ExecutionStub, ImportExportStub, SystemStub,
|
||||
ConfigStub, AuthClientInterceptor, AuthStub
|
||||
AIStub,
|
||||
AuthClientInterceptor,
|
||||
AuthStub,
|
||||
ConfigStub,
|
||||
ExecutionStub,
|
||||
ImportExportStub,
|
||||
NodeStub,
|
||||
PluginStub,
|
||||
ProfileStub,
|
||||
SystemStub,
|
||||
)
|
||||
|
||||
def get_token():
|
||||
@@ -130,11 +143,8 @@ el.replaceWith(d);
|
||||
return env_token
|
||||
token_path = os.path.join(self.config.defaultdir, ".token")
|
||||
if os.path.exists(token_path):
|
||||
try:
|
||||
with open(token_path, "r") as f:
|
||||
with suppress(OSError, UnicodeDecodeError), open(token_path) as f:
|
||||
return f.read().strip()
|
||||
except Exception:
|
||||
pass
|
||||
return None
|
||||
|
||||
channel = grpc.insecure_channel(self.remote_host)
|
||||
@@ -170,6 +180,9 @@ el.replaceWith(d);
|
||||
def execution(self):
|
||||
if self._execution is None and self.mode == "local":
|
||||
from .execution_service import ExecutionService
|
||||
if "attempt_tracker" in inspect.signature(ExecutionService.__init__).parameters:
|
||||
self._execution = ExecutionService(self.config, attempt_tracker=self.auth_tracker)
|
||||
else:
|
||||
self._execution = ExecutionService(self.config)
|
||||
return self._execution
|
||||
|
||||
@@ -249,6 +262,9 @@ def ai(self):
|
||||
def execution(self):
|
||||
if self._execution is None and self.mode == "local":
|
||||
from .execution_service import ExecutionService
|
||||
if "attempt_tracker" in inspect.signature(ExecutionService.__init__).parameters:
|
||||
self._execution = ExecutionService(self.config, attempt_tracker=self.auth_tracker)
|
||||
else:
|
||||
self._execution = ExecutionService(self.config)
|
||||
return self._execution</code></pre>
|
||||
</details>
|
||||
|
||||
Reference in new issue
Block a user