chore(release): bump version to v6.5.0b1 and update documentation

This commit is contained in:
fluzzi32 committed 2026-11-17 13:44:52 -03:00
1 parent c6f260efc9
commit 441bf54ac1
16 files changed
+7507 -1637

No files matched your search

File diff suppressed because it is too large. Load diff
+164 -4
View File
@@ -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 = {
&#34;max_auth_prompts&#34;: (MIN_AUTH_PROMPTS, MAX_AUTH_PROMPTS_CAP),
&#34;max_total_auth_attempts&#34;: (MIN_TOTAL_AUTH_ATTEMPTS, MAX_TOTAL_AUTH_ATTEMPTS_CAP),
&#34;max_hop_host_attempts&#34;: (MIN_HOP_HOST_ATTEMPTS, MAX_HOP_HOST_ATTEMPTS_CAP),
&#34;max_global_user_attempts&#34;: (1, 1000),
}
if key not in bounds_map:
raise InvalidConfigurationError(f&#34;Unknown auth budget setting: {key}&#34;)
if isinstance(raw_val, bool):
msg = f&#34;Invalid {key}: boolean is not permitted&#34;
printer.error(msg)
raise InvalidConfigurationError(msg)
if isinstance(raw_val, int):
val = raw_val
elif isinstance(raw_val, str) and re.fullmatch(r&#34;[+-]?\d+&#34;, raw_val.strip()):
val = int(raw_val.strip())
else:
msg = f&#34;Invalid {key}: expected integer, got {raw_val!r}&#34;
printer.error(msg)
raise InvalidConfigurationError(msg)
if val == 0:
cfg_obj = self.app.config
if not hasattr(cfg_obj, &#34;assert_called&#34;) and callable(
getattr(cfg_obj, &#34;transactional_update&#34;, None)
):
cfg_obj.transactional_update(lambda c: c.config.pop(key, None))
else:
cfg_obj.config.pop(key, None)
cfg_obj._saveconfig(cfg_obj.file)
printer.success(&#34;Config saved&#34;)
return
min_v, max_v = bounds_map[key]
try:
validate_strict_int_range(val, key, min_v, max_v)
except ValueError as exc:
printer.error(str(exc))
raise InvalidConfigurationError(str(exc)) from None
self.app.services.config_svc.update_setting(key, val)
printer.success(&#34;Config saved&#34;)
def set_disable_fallback_on_insecure_hostkey(self, args):
raw_val = args.data[0] if isinstance(args.data, list) else args.data
if isinstance(raw_val, bool):
val = raw_val
elif isinstance(raw_val, str) and raw_val.strip().lower() in (&#34;true&#34;, &#34;false&#34;):
val = raw_val.strip().lower() == &#34;true&#34;
else:
msg = &#34;Invalid disable_fallback_on_insecure_hostkey: expected &#39;true&#39; or &#39;false&#39;&#34;
printer.error(msg)
raise InvalidConfigurationError(msg)
self.app.services.config_svc.update_setting(
&#34;disable_fallback_on_insecure_hostkey&#34;,
val,
)
printer.success(&#34;Config saved&#34;)</code></pre>
</details>
<div class="desc"></div>
<h3>Methods</h3>
@@ -285,7 +354,12 @@ el.replaceWith(d);
&#34;sync_remote&#34;: self.set_sync_remote,
&#34;shell_command&#34;: self.set_shell_config,
&#34;shell_prompt&#34;: self.set_shell_config,
&#34;shell_os&#34;: self.set_shell_config
&#34;shell_os&#34;: self.set_shell_config,
&#34;max_auth_prompts&#34;: self.set_auth_budget,
&#34;max_total_auth_attempts&#34;: self.set_auth_budget,
&#34;max_hop_host_attempts&#34;: self.set_auth_budget,
&#34;max_global_user_attempts&#34;: self.set_auth_budget,
&#34;disable_fallback_on_insecure_hostkey&#34;: self.set_disable_fallback_on_insecure_hostkey,
}
handler = actions.get(getattr(args, &#34;command&#34;, None))
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 = {
&#34;max_auth_prompts&#34;: (MIN_AUTH_PROMPTS, MAX_AUTH_PROMPTS_CAP),
&#34;max_total_auth_attempts&#34;: (MIN_TOTAL_AUTH_ATTEMPTS, MAX_TOTAL_AUTH_ATTEMPTS_CAP),
&#34;max_hop_host_attempts&#34;: (MIN_HOP_HOST_ATTEMPTS, MAX_HOP_HOST_ATTEMPTS_CAP),
&#34;max_global_user_attempts&#34;: (1, 1000),
}
if key not in bounds_map:
raise InvalidConfigurationError(f&#34;Unknown auth budget setting: {key}&#34;)
if isinstance(raw_val, bool):
msg = f&#34;Invalid {key}: boolean is not permitted&#34;
printer.error(msg)
raise InvalidConfigurationError(msg)
if isinstance(raw_val, int):
val = raw_val
elif isinstance(raw_val, str) and re.fullmatch(r&#34;[+-]?\d+&#34;, raw_val.strip()):
val = int(raw_val.strip())
else:
msg = f&#34;Invalid {key}: expected integer, got {raw_val!r}&#34;
printer.error(msg)
raise InvalidConfigurationError(msg)
if val == 0:
cfg_obj = self.app.config
if not hasattr(cfg_obj, &#34;assert_called&#34;) and callable(
getattr(cfg_obj, &#34;transactional_update&#34;, None)
):
cfg_obj.transactional_update(lambda c: c.config.pop(key, None))
else:
cfg_obj.config.pop(key, None)
cfg_obj._saveconfig(cfg_obj.file)
printer.success(&#34;Config saved&#34;)
return
min_v, max_v = bounds_map[key]
try:
validate_strict_int_range(val, key, min_v, max_v)
except ValueError as exc:
printer.error(str(exc))
raise InvalidConfigurationError(str(exc)) from None
self.app.services.config_svc.update_setting(key, val)
printer.success(&#34;Config saved&#34;)</code></pre>
</details>
<div class="desc"></div>
</dd>
<dt id="connpy.cli.config_handler.ConfigHandler.set_case"><code class="name flex">
<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 (&#34;true&#34;, &#34;false&#34;):
val = raw_val.strip().lower() == &#34;true&#34;
else:
msg = &#34;Invalid disable_fallback_on_insecure_hostkey: expected &#39;true&#39; or &#39;false&#39;&#34;
printer.error(msg)
raise InvalidConfigurationError(msg)
self.app.services.config_svc.update_setting(
&#34;disable_fallback_on_insecure_hostkey&#34;,
val,
)
printer.success(&#34;Config saved&#34;)</code></pre>
</details>
<div class="desc"></div>
</dd>
<dt id="connpy.cli.config_handler.ConfigHandler.set_fzf"><code class="name flex">
<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>
+148 -56
View File
@@ -72,6 +72,7 @@ el.replaceWith(d);
questions.append(inquirer.Confirm(&#34;jumphost&#34;, message=&#34;Edit jumphost?&#34;))
questions.append(inquirer.Confirm(&#34;user&#34;, message=&#34;Edit User?&#34;))
questions.append(inquirer.Confirm(&#34;password&#34;, message=&#34;Edit password?&#34;))
questions.append(inquirer.Confirm(&#34;fallback&#34;, message=&#34;Edit fallback credentials?&#34;))
return inquirer.prompt(questions)
def questions_nodes(self, unique, uniques=None, edit=None):
@@ -82,11 +83,13 @@ el.replaceWith(d);
defaults[&#34;tags&#34;] = &#34;&#34;
if &#34;jumphost&#34; not in defaults:
defaults[&#34;jumphost&#34;] = &#34;&#34;
except Exception:
defaults = {&#34;host&#34;: &#34;&#34;, &#34;protocol&#34;: &#34;&#34;, &#34;port&#34;: &#34;&#34;, &#34;user&#34;: &#34;&#34;, &#34;options&#34;: &#34;&#34;, &#34;logs&#34;: &#34;&#34;, &#34;tags&#34;: &#34;&#34;, &#34;password&#34;: &#34;&#34;, &#34;jumphost&#34;: &#34;&#34;}
if &#34;fallback&#34; not in defaults:
defaults[&#34;fallback&#34;] = &#34;&#34;
except Exception: # noqa: BLE001
defaults = {&#34;host&#34;: &#34;&#34;, &#34;protocol&#34;: &#34;&#34;, &#34;port&#34;: &#34;&#34;, &#34;user&#34;: &#34;&#34;, &#34;options&#34;: &#34;&#34;, &#34;logs&#34;: &#34;&#34;, &#34;tags&#34;: &#34;&#34;, &#34;password&#34;: &#34;&#34;, &#34;jumphost&#34;: &#34;&#34;, &#34;fallback&#34;: &#34;&#34;}
node = {}
if edit is None:
edit = {&#34;host&#34;: True, &#34;protocol&#34;: True, &#34;port&#34;: True, &#34;user&#34;: True, &#34;password&#34;: True, &#34;options&#34;: True, &#34;logs&#34;: True, &#34;tags&#34;: True, &#34;jumphost&#34;: True}
edit = {&#34;host&#34;: True, &#34;protocol&#34;: True, &#34;port&#34;: True, &#34;user&#34;: True, &#34;password&#34;: True, &#34;options&#34;: True, &#34;logs&#34;: True, &#34;tags&#34;: True, &#34;jumphost&#34;: True, &#34;fallback&#34;: True}
questions = []
if edit[&#34;host&#34;]:
questions.append(inquirer.Text(&#34;host&#34;, message=&#34;Add Hostname or IP&#34;, validate=self.validators.host_validation, default=defaults[&#34;host&#34;]))
@@ -124,11 +127,13 @@ el.replaceWith(d);
questions.append(inquirer.List(&#34;password&#34;, message=&#34;Password: Use a local password, no password or a list of profiles to reference?&#34;, choices=[&#34;Local Password&#34;, &#34;Profiles&#34;, &#34;No Password&#34;]))
else:
node[&#34;password&#34;] = defaults[&#34;password&#34;]
if not edit.get(&#34;fallback&#34;):
node[&#34;fallback&#34;] = defaults.get(&#34;fallback&#34;, &#34;&#34;)
answer = inquirer.prompt(questions)
if answer is None:
return False
if &#34;password&#34; in answer:
if answer[&#34;password&#34;] == &#34;Local Password&#34;:
passq = [inquirer.Password(&#34;password&#34;, message=&#34;Set Password&#34;)]
@@ -141,13 +146,29 @@ el.replaceWith(d);
passa = inquirer.prompt(passq)
if passa is None:
return False
answer[&#34;password&#34;] = passa[&#34;password&#34;].split(&#34;,&#34;)
answer[&#34;password&#34;] = [p.strip() for p in passa[&#34;password&#34;].split(&#34;,&#34;) if p.strip()]
elif answer[&#34;password&#34;] == &#34;No Password&#34;:
answer[&#34;password&#34;] = &#34;&#34;
if &#34;tags&#34; in answer and not answer[&#34;tags&#34;].startswith(&#34;@&#34;) and answer[&#34;tags&#34;]:
answer[&#34;tags&#34;] = ast.literal_eval(answer[&#34;tags&#34;])
if edit.get(&#34;fallback&#34;):
raw_def_fb = defaults.get(&#34;fallback&#34;, &#34;&#34;)
def_fb_str = json.dumps(raw_def_fb) if isinstance(raw_def_fb, (list, dict)) else str(raw_def_fb or &#34;&#34;)
fbq = [
inquirer.Text(
&#34;fallback&#34;,
message=&#34;Add fallback credentials (JSON list/dict, @profile, or compose:)&#34;,
validate=self.validators.fallback_validation,
default=def_fb_str.replace(&#34;{&#34;, &#34;{{&#34;).replace(&#34;}&#34;, &#34;}}&#34;),
)
]
fba = inquirer.prompt(fbq)
if fba is None:
return False
answer[&#34;fallback&#34;] = _parse_fallback_input(fba[&#34;fallback&#34;])
if &#34;tags&#34; in answer and answer[&#34;tags&#34;] and not str(answer[&#34;tags&#34;]).startswith(&#34;@&#34;):
answer[&#34;tags&#34;] = _parse_tags_input(answer[&#34;tags&#34;])
result = {**uniques, **answer, **node}
result[&#34;type&#34;] = &#34;connection&#34;
return result
@@ -160,11 +181,13 @@ el.replaceWith(d);
defaults[&#34;tags&#34;] = &#34;&#34;
if &#34;jumphost&#34; not in defaults:
defaults[&#34;jumphost&#34;] = &#34;&#34;
except Exception:
defaults = {&#34;host&#34;: &#34;&#34;, &#34;protocol&#34;: &#34;&#34;, &#34;port&#34;: &#34;&#34;, &#34;user&#34;: &#34;&#34;, &#34;options&#34;: &#34;&#34;, &#34;logs&#34;: &#34;&#34;, &#34;tags&#34;: &#34;&#34;, &#34;jumphost&#34;: &#34;&#34;}
if &#34;fallback&#34; not in defaults:
defaults[&#34;fallback&#34;] = &#34;&#34;
except Exception: # noqa: BLE001
defaults = {&#34;host&#34;: &#34;&#34;, &#34;protocol&#34;: &#34;&#34;, &#34;port&#34;: &#34;&#34;, &#34;user&#34;: &#34;&#34;, &#34;options&#34;: &#34;&#34;, &#34;logs&#34;: &#34;&#34;, &#34;tags&#34;: &#34;&#34;, &#34;jumphost&#34;: &#34;&#34;, &#34;fallback&#34;: &#34;&#34;}
profile = {}
if edit is None:
edit = {&#34;host&#34;: True, &#34;protocol&#34;: True, &#34;port&#34;: True, &#34;user&#34;: True, &#34;password&#34;: True, &#34;options&#34;: True, &#34;logs&#34;: True, &#34;tags&#34;: True, &#34;jumphost&#34;: True}
edit = {&#34;host&#34;: True, &#34;protocol&#34;: True, &#34;port&#34;: True, &#34;user&#34;: True, &#34;password&#34;: True, &#34;options&#34;: True, &#34;logs&#34;: True, &#34;tags&#34;: True, &#34;jumphost&#34;: True, &#34;fallback&#34;: True}
questions = []
if edit[&#34;host&#34;]:
questions.append(inquirer.Text(&#34;host&#34;, message=&#34;Add Hostname or IP&#34;, default=defaults[&#34;host&#34;]))
@@ -202,18 +225,25 @@ el.replaceWith(d);
questions.append(inquirer.Password(&#34;password&#34;, message=&#34;Set Password&#34;))
else:
profile[&#34;password&#34;] = defaults[&#34;password&#34;]
if edit.get(&#34;fallback&#34;):
raw_def_fb = defaults.get(&#34;fallback&#34;, &#34;&#34;)
def_fb_str = json.dumps(raw_def_fb) if isinstance(raw_def_fb, (list, dict)) else str(raw_def_fb or &#34;&#34;)
questions.append(inquirer.Text(&#34;fallback&#34;, message=&#34;Add fallback credentials (JSON list/dict, @profile, or compose:)&#34;, validate=self.validators.fallback_validation, default=def_fb_str.replace(&#34;{&#34;, &#34;{{&#34;).replace(&#34;}&#34;, &#34;}}&#34;)))
else:
profile[&#34;fallback&#34;] = defaults.get(&#34;fallback&#34;, &#34;&#34;)
answer = inquirer.prompt(questions)
if answer is None:
return False
if &#34;password&#34; in answer:
if answer[&#34;password&#34;] != &#34;&#34;:
answer[&#34;password&#34;] = self.app.services.config_svc.encrypt_password(answer[&#34;password&#34;])
if &#34;password&#34; in answer and answer[&#34;password&#34;] != &#34;&#34;:
answer[&#34;password&#34;] = self.app.services.config_svc.encrypt_password(answer[&#34;password&#34;])
if &#34;tags&#34; in answer and answer[&#34;tags&#34;]:
answer[&#34;tags&#34;] = ast.literal_eval(answer[&#34;tags&#34;])
answer[&#34;tags&#34;] = _parse_tags_input(answer[&#34;tags&#34;])
if &#34;fallback&#34; in answer:
answer[&#34;fallback&#34;] = _parse_fallback_input(answer[&#34;fallback&#34;])
result = {**answer, **profile}
result[&#34;id&#34;] = unique
return result
@@ -232,27 +262,43 @@ el.replaceWith(d);
questions.append(inquirer.Text(&#34;jumphost&#34;, message=&#34;Add Jumphost node&#34;, validate=self.validators.jumphost_validation))
questions.append(inquirer.Text(&#34;user&#34;, message=&#34;Pick username&#34;, validate=self.validators.default_validation))
questions.append(inquirer.List(&#34;password&#34;, message=&#34;Password: Use a local password, no password or a list of profiles to reference?&#34;, choices=[&#34;Local Password&#34;, &#34;Profiles&#34;, &#34;No Password&#34;]))
answer = inquirer.prompt(questions)
if answer is None:
return False
if &#34;password&#34; in answer:
if answer[&#34;password&#34;] == &#34;Local Password&#34;:
passq = [inquirer.Password(&#34;password&#34;, message=&#34;Set Password&#34;)]
passa = inquirer.prompt(passq)
if passa is None:
return False
answer[&#34;password&#34;] = self.app.services.config_svc.encrypt_password(passa[&#34;password&#34;])
elif answer[&#34;password&#34;] == &#34;Profiles&#34;:
passq = [(inquirer.Text(&#34;password&#34;, message=&#34;Set a @profile or a comma separated list of @profiles&#34;, validate=self.validators.pass_validation))]
passa = inquirer.prompt(passq)
answer[&#34;password&#34;] = passa[&#34;password&#34;].split(&#34;,&#34;)
if passa is None:
return False
answer[&#34;password&#34;] = [p.strip() for p in passa[&#34;password&#34;].split(&#34;,&#34;) if p.strip()]
elif answer[&#34;password&#34;] == &#34;No Password&#34;:
answer[&#34;password&#34;] = &#34;&#34;
fbq = [
inquirer.Text(
&#34;fallback&#34;,
message=&#34;Add fallback credentials (JSON list/dict, @profile, or compose:)&#34;,
validate=self.validators.fallback_validation,
)
]
fba = inquirer.prompt(fbq)
if fba is None:
return False
answer[&#34;fallback&#34;] = _parse_fallback_input(fba[&#34;fallback&#34;])
answer[&#34;type&#34;] = &#34;connection&#34;
if &#34;tags&#34; in answer and not answer[&#34;tags&#34;].startswith(&#34;@&#34;) and answer[&#34;tags&#34;]:
answer[&#34;tags&#34;] = ast.literal_eval(answer[&#34;tags&#34;])
if &#34;tags&#34; in answer and answer[&#34;tags&#34;] and not str(answer[&#34;tags&#34;]).startswith(&#34;@&#34;):
answer[&#34;tags&#34;] = _parse_tags_input(answer[&#34;tags&#34;])
return answer
def mcp_wizard(self, mcp_servers):
@@ -454,27 +500,43 @@ el.replaceWith(d);
questions.append(inquirer.Text(&#34;jumphost&#34;, message=&#34;Add Jumphost node&#34;, validate=self.validators.jumphost_validation))
questions.append(inquirer.Text(&#34;user&#34;, message=&#34;Pick username&#34;, validate=self.validators.default_validation))
questions.append(inquirer.List(&#34;password&#34;, message=&#34;Password: Use a local password, no password or a list of profiles to reference?&#34;, choices=[&#34;Local Password&#34;, &#34;Profiles&#34;, &#34;No Password&#34;]))
answer = inquirer.prompt(questions)
if answer is None:
return False
if &#34;password&#34; in answer:
if answer[&#34;password&#34;] == &#34;Local Password&#34;:
passq = [inquirer.Password(&#34;password&#34;, message=&#34;Set Password&#34;)]
passa = inquirer.prompt(passq)
if passa is None:
return False
answer[&#34;password&#34;] = self.app.services.config_svc.encrypt_password(passa[&#34;password&#34;])
elif answer[&#34;password&#34;] == &#34;Profiles&#34;:
passq = [(inquirer.Text(&#34;password&#34;, message=&#34;Set a @profile or a comma separated list of @profiles&#34;, validate=self.validators.pass_validation))]
passa = inquirer.prompt(passq)
answer[&#34;password&#34;] = passa[&#34;password&#34;].split(&#34;,&#34;)
if passa is None:
return False
answer[&#34;password&#34;] = [p.strip() for p in passa[&#34;password&#34;].split(&#34;,&#34;) if p.strip()]
elif answer[&#34;password&#34;] == &#34;No Password&#34;:
answer[&#34;password&#34;] = &#34;&#34;
fbq = [
inquirer.Text(
&#34;fallback&#34;,
message=&#34;Add fallback credentials (JSON list/dict, @profile, or compose:)&#34;,
validate=self.validators.fallback_validation,
)
]
fba = inquirer.prompt(fbq)
if fba is None:
return False
answer[&#34;fallback&#34;] = _parse_fallback_input(fba[&#34;fallback&#34;])
answer[&#34;type&#34;] = &#34;connection&#34;
if &#34;tags&#34; in answer and not answer[&#34;tags&#34;].startswith(&#34;@&#34;) and answer[&#34;tags&#34;]:
answer[&#34;tags&#34;] = ast.literal_eval(answer[&#34;tags&#34;])
if &#34;tags&#34; in answer and answer[&#34;tags&#34;] and not str(answer[&#34;tags&#34;]).startswith(&#34;@&#34;):
answer[&#34;tags&#34;] = _parse_tags_input(answer[&#34;tags&#34;])
return answer</code></pre>
</details>
<div class="desc"></div>
@@ -499,6 +561,7 @@ el.replaceWith(d);
questions.append(inquirer.Confirm(&#34;jumphost&#34;, message=&#34;Edit jumphost?&#34;))
questions.append(inquirer.Confirm(&#34;user&#34;, message=&#34;Edit User?&#34;))
questions.append(inquirer.Confirm(&#34;password&#34;, message=&#34;Edit password?&#34;))
questions.append(inquirer.Confirm(&#34;fallback&#34;, message=&#34;Edit fallback credentials?&#34;))
return inquirer.prompt(questions)</code></pre>
</details>
<div class="desc"></div>
@@ -519,11 +582,13 @@ el.replaceWith(d);
defaults[&#34;tags&#34;] = &#34;&#34;
if &#34;jumphost&#34; not in defaults:
defaults[&#34;jumphost&#34;] = &#34;&#34;
except Exception:
defaults = {&#34;host&#34;: &#34;&#34;, &#34;protocol&#34;: &#34;&#34;, &#34;port&#34;: &#34;&#34;, &#34;user&#34;: &#34;&#34;, &#34;options&#34;: &#34;&#34;, &#34;logs&#34;: &#34;&#34;, &#34;tags&#34;: &#34;&#34;, &#34;password&#34;: &#34;&#34;, &#34;jumphost&#34;: &#34;&#34;}
if &#34;fallback&#34; not in defaults:
defaults[&#34;fallback&#34;] = &#34;&#34;
except Exception: # noqa: BLE001
defaults = {&#34;host&#34;: &#34;&#34;, &#34;protocol&#34;: &#34;&#34;, &#34;port&#34;: &#34;&#34;, &#34;user&#34;: &#34;&#34;, &#34;options&#34;: &#34;&#34;, &#34;logs&#34;: &#34;&#34;, &#34;tags&#34;: &#34;&#34;, &#34;password&#34;: &#34;&#34;, &#34;jumphost&#34;: &#34;&#34;, &#34;fallback&#34;: &#34;&#34;}
node = {}
if edit is None:
edit = {&#34;host&#34;: True, &#34;protocol&#34;: True, &#34;port&#34;: True, &#34;user&#34;: True, &#34;password&#34;: True, &#34;options&#34;: True, &#34;logs&#34;: True, &#34;tags&#34;: True, &#34;jumphost&#34;: True}
edit = {&#34;host&#34;: True, &#34;protocol&#34;: True, &#34;port&#34;: True, &#34;user&#34;: True, &#34;password&#34;: True, &#34;options&#34;: True, &#34;logs&#34;: True, &#34;tags&#34;: True, &#34;jumphost&#34;: True, &#34;fallback&#34;: True}
questions = []
if edit[&#34;host&#34;]:
questions.append(inquirer.Text(&#34;host&#34;, message=&#34;Add Hostname or IP&#34;, validate=self.validators.host_validation, default=defaults[&#34;host&#34;]))
@@ -561,11 +626,13 @@ el.replaceWith(d);
questions.append(inquirer.List(&#34;password&#34;, message=&#34;Password: Use a local password, no password or a list of profiles to reference?&#34;, choices=[&#34;Local Password&#34;, &#34;Profiles&#34;, &#34;No Password&#34;]))
else:
node[&#34;password&#34;] = defaults[&#34;password&#34;]
if not edit.get(&#34;fallback&#34;):
node[&#34;fallback&#34;] = defaults.get(&#34;fallback&#34;, &#34;&#34;)
answer = inquirer.prompt(questions)
if answer is None:
return False
if &#34;password&#34; in answer:
if answer[&#34;password&#34;] == &#34;Local Password&#34;:
passq = [inquirer.Password(&#34;password&#34;, message=&#34;Set Password&#34;)]
@@ -578,13 +645,29 @@ el.replaceWith(d);
passa = inquirer.prompt(passq)
if passa is None:
return False
answer[&#34;password&#34;] = passa[&#34;password&#34;].split(&#34;,&#34;)
answer[&#34;password&#34;] = [p.strip() for p in passa[&#34;password&#34;].split(&#34;,&#34;) if p.strip()]
elif answer[&#34;password&#34;] == &#34;No Password&#34;:
answer[&#34;password&#34;] = &#34;&#34;
if &#34;tags&#34; in answer and not answer[&#34;tags&#34;].startswith(&#34;@&#34;) and answer[&#34;tags&#34;]:
answer[&#34;tags&#34;] = ast.literal_eval(answer[&#34;tags&#34;])
if edit.get(&#34;fallback&#34;):
raw_def_fb = defaults.get(&#34;fallback&#34;, &#34;&#34;)
def_fb_str = json.dumps(raw_def_fb) if isinstance(raw_def_fb, (list, dict)) else str(raw_def_fb or &#34;&#34;)
fbq = [
inquirer.Text(
&#34;fallback&#34;,
message=&#34;Add fallback credentials (JSON list/dict, @profile, or compose:)&#34;,
validate=self.validators.fallback_validation,
default=def_fb_str.replace(&#34;{&#34;, &#34;{{&#34;).replace(&#34;}&#34;, &#34;}}&#34;),
)
]
fba = inquirer.prompt(fbq)
if fba is None:
return False
answer[&#34;fallback&#34;] = _parse_fallback_input(fba[&#34;fallback&#34;])
if &#34;tags&#34; in answer and answer[&#34;tags&#34;] and not str(answer[&#34;tags&#34;]).startswith(&#34;@&#34;):
answer[&#34;tags&#34;] = _parse_tags_input(answer[&#34;tags&#34;])
result = {**uniques, **answer, **node}
result[&#34;type&#34;] = &#34;connection&#34;
return result</code></pre>
@@ -607,11 +690,13 @@ el.replaceWith(d);
defaults[&#34;tags&#34;] = &#34;&#34;
if &#34;jumphost&#34; not in defaults:
defaults[&#34;jumphost&#34;] = &#34;&#34;
except Exception:
defaults = {&#34;host&#34;: &#34;&#34;, &#34;protocol&#34;: &#34;&#34;, &#34;port&#34;: &#34;&#34;, &#34;user&#34;: &#34;&#34;, &#34;options&#34;: &#34;&#34;, &#34;logs&#34;: &#34;&#34;, &#34;tags&#34;: &#34;&#34;, &#34;jumphost&#34;: &#34;&#34;}
if &#34;fallback&#34; not in defaults:
defaults[&#34;fallback&#34;] = &#34;&#34;
except Exception: # noqa: BLE001
defaults = {&#34;host&#34;: &#34;&#34;, &#34;protocol&#34;: &#34;&#34;, &#34;port&#34;: &#34;&#34;, &#34;user&#34;: &#34;&#34;, &#34;options&#34;: &#34;&#34;, &#34;logs&#34;: &#34;&#34;, &#34;tags&#34;: &#34;&#34;, &#34;jumphost&#34;: &#34;&#34;, &#34;fallback&#34;: &#34;&#34;}
profile = {}
if edit is None:
edit = {&#34;host&#34;: True, &#34;protocol&#34;: True, &#34;port&#34;: True, &#34;user&#34;: True, &#34;password&#34;: True, &#34;options&#34;: True, &#34;logs&#34;: True, &#34;tags&#34;: True, &#34;jumphost&#34;: True}
edit = {&#34;host&#34;: True, &#34;protocol&#34;: True, &#34;port&#34;: True, &#34;user&#34;: True, &#34;password&#34;: True, &#34;options&#34;: True, &#34;logs&#34;: True, &#34;tags&#34;: True, &#34;jumphost&#34;: True, &#34;fallback&#34;: True}
questions = []
if edit[&#34;host&#34;]:
questions.append(inquirer.Text(&#34;host&#34;, message=&#34;Add Hostname or IP&#34;, default=defaults[&#34;host&#34;]))
@@ -649,18 +734,25 @@ el.replaceWith(d);
questions.append(inquirer.Password(&#34;password&#34;, message=&#34;Set Password&#34;))
else:
profile[&#34;password&#34;] = defaults[&#34;password&#34;]
if edit.get(&#34;fallback&#34;):
raw_def_fb = defaults.get(&#34;fallback&#34;, &#34;&#34;)
def_fb_str = json.dumps(raw_def_fb) if isinstance(raw_def_fb, (list, dict)) else str(raw_def_fb or &#34;&#34;)
questions.append(inquirer.Text(&#34;fallback&#34;, message=&#34;Add fallback credentials (JSON list/dict, @profile, or compose:)&#34;, validate=self.validators.fallback_validation, default=def_fb_str.replace(&#34;{&#34;, &#34;{{&#34;).replace(&#34;}&#34;, &#34;}}&#34;)))
else:
profile[&#34;fallback&#34;] = defaults.get(&#34;fallback&#34;, &#34;&#34;)
answer = inquirer.prompt(questions)
if answer is None:
return False
if &#34;password&#34; in answer:
if answer[&#34;password&#34;] != &#34;&#34;:
answer[&#34;password&#34;] = self.app.services.config_svc.encrypt_password(answer[&#34;password&#34;])
if &#34;password&#34; in answer and answer[&#34;password&#34;] != &#34;&#34;:
answer[&#34;password&#34;] = self.app.services.config_svc.encrypt_password(answer[&#34;password&#34;])
if &#34;tags&#34; in answer and answer[&#34;tags&#34;]:
answer[&#34;tags&#34;] = ast.literal_eval(answer[&#34;tags&#34;])
answer[&#34;tags&#34;] = _parse_tags_input(answer[&#34;tags&#34;])
if &#34;fallback&#34; in answer:
answer[&#34;fallback&#34;] = _parse_fallback_input(answer[&#34;fallback&#34;])
result = {**answer, **profile}
result[&#34;id&#34;] = unique
return result</code></pre>
+105 -28
View File
@@ -106,35 +106,80 @@ el.replaceWith(d);
return True
def pass_validation(self, answers, current, regex = &#34;(^@.+$)&#34;):
if is_compose_expression(current):
try:
validate_compose_syntax(current)
except SanitizedAuthError as exc:
_raise_val_err(str(exc))
return True
profiles = current.split(&#34;,&#34;)
for i in profiles:
if not re.match(regex, i) or i[1:] not in self.app.profiles:
_raise_val_err(&#34;Profile {} don&#39;t exist&#34;.format(i))
return True
def _validate_tags_dict(self, current: str):
if len(current.encode(&#34;utf-8&#34;, errors=&#34;replace&#34;)) &gt; MAX_JSON_INPUT_BYTES:
_raise_val_err(f&#34;Tags exceed maximum size of {MAX_JSON_INPUT_BYTES} bytes&#34;)
parsed = None
try:
parsed = json.loads(current)
except (ValueError, TypeError):
try:
parsed = ast.literal_eval(current)
except (ValueError, TypeError, SyntaxError, MemoryError, RecursionError):
parsed = None
if not isinstance(parsed, dict):
_raise_val_err(&#34;Tags should be a python dictionary.&#34;)
try:
validate_json_depth_and_size(
parsed,
max_bytes=MAX_JSON_INPUT_BYTES,
max_depth=MAX_JSON_INPUT_DEPTH,
)
validate_auth_budget_tags(parsed)
except (ValueError, TypeError, SanitizedAuthError) as exc:
_raise_val_err(str(exc))
return True
def tags_validation(self, answers, current):
if current.startswith(&#34;@&#34;):
if current[1:] not in self.app.profiles:
_raise_val_err(&#34;Profile {} don&#39;t exist&#34;.format(current))
elif current != &#34;&#34;:
isdict = False
try:
isdict = ast.literal_eval(current)
except Exception:
pass
if not isinstance (isdict, dict):
_raise_val_err(&#34;Tags should be a python dictionary.&#34;.format(current))
return self._validate_tags_dict(current)
return True
def profile_tags_validation(self, answers, current):
if current != &#34;&#34;:
isdict = False
try:
isdict = ast.literal_eval(current)
except Exception:
pass
if not isinstance (isdict, dict):
_raise_val_err(&#34;Tags should be a python dictionary.&#34;.format(current))
return self._validate_tags_dict(current)
return True
def fallback_validation(self, answers, current):
if current in (None, &#34;&#34;):
return True
stripped = str(current).strip()
if not stripped:
return True
if len(stripped.encode(&#34;utf-8&#34;, errors=&#34;replace&#34;)) &gt; MAX_JSON_INPUT_BYTES:
_raise_val_err(f&#34;Fallback exceeds maximum size of {MAX_JSON_INPUT_BYTES} bytes&#34;)
if stripped.startswith(&#34;@&#34;) and &#34;+&#34; not in stripped:
if stripped[1:] not in self.app.profiles:
_raise_val_err(f&#34;Profile {stripped} don&#39;t exist&#34;)
return True
try:
if is_compose_expression(stripped):
validate_compose_syntax(stripped)
return True
if stripped.startswith((&#34;[&#34;, &#34;{&#34;)):
validate_json_depth_and_size(
stripped,
max_bytes=MAX_JSON_INPUT_BYTES,
max_depth=MAX_JSON_INPUT_DEPTH,
)
validate_fallback_entries(stripped, max_entries=MAX_FALLBACK_ENTRIES)
except (ValueError, TypeError, SanitizedAuthError) as exc:
_raise_val_err(str(exc))
return True
def 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, &#34;&#34;):
return True
stripped = str(current).strip()
if not stripped:
return True
if len(stripped.encode(&#34;utf-8&#34;, errors=&#34;replace&#34;)) &gt; MAX_JSON_INPUT_BYTES:
_raise_val_err(f&#34;Fallback exceeds maximum size of {MAX_JSON_INPUT_BYTES} bytes&#34;)
if stripped.startswith(&#34;@&#34;) and &#34;+&#34; not in stripped:
if stripped[1:] not in self.app.profiles:
_raise_val_err(f&#34;Profile {stripped} don&#39;t exist&#34;)
return True
try:
if is_compose_expression(stripped):
validate_compose_syntax(stripped)
return True
if stripped.startswith((&#34;[&#34;, &#34;{&#34;)):
validate_json_depth_and_size(
stripped,
max_bytes=MAX_JSON_INPUT_BYTES,
max_depth=MAX_JSON_INPUT_DEPTH,
)
validate_fallback_entries(stripped, max_entries=MAX_FALLBACK_ENTRIES)
except (ValueError, TypeError, SanitizedAuthError) as exc:
_raise_val_err(str(exc))
return True</code></pre>
</details>
<div class="desc"></div>
</dd>
<dt id="connpy.cli.validators.Validators.host_validation"><code class="name flex">
<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 = &#34;(^@.+$)&#34;):
if is_compose_expression(current):
try:
validate_compose_syntax(current)
except SanitizedAuthError as exc:
_raise_val_err(str(exc))
return True
profiles = current.split(&#34;,&#34;)
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 != &#34;&#34;:
isdict = False
try:
isdict = ast.literal_eval(current)
except Exception:
pass
if not isinstance (isdict, dict):
_raise_val_err(&#34;Tags should be a python dictionary.&#34;.format(current))
return 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(&#34;Profile {} don&#39;t exist&#34;.format(current))
elif current != &#34;&#34;:
isdict = False
try:
isdict = ast.literal_eval(current)
except Exception:
pass
if not isinstance (isdict, dict):
_raise_val_err(&#34;Tags should be a python dictionary.&#34;.format(current))
return 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>
+56 -18
View File
@@ -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&#34;[debug][DEBUG][/debug] gRPC interact_node request for: [bold cyan]{unique_id}[/bold cyan]&#34;)
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(&#34;connection_params_json must be a JSON object&#34;)
if &#34;fallback&#34; in params and params[&#34;fallback&#34;] not in (None, &#34;&#34;, []):
validate_fallback_entries(
params[&#34;fallback&#34;],
max_entries=MAX_FALLBACK_ENTRIES,
)
if &#34;tags&#34; in params and params[&#34;tags&#34;] not in (&#34;&#34;, None):
validate_auth_budget_tags(params[&#34;tags&#34;])
except (ValueError, TypeError, SanitizedAuthError) as exc:
context.abort(grpc.StatusCode.INVALID_ARGUMENT, str(exc))
base_node_id = params.get(&#34;base_node&#34;)
# Valid attributes that a node object accepts
valid_attrs = [&#39;host&#39;, &#39;options&#39;, &#39;logs&#39;, &#39;password&#39;, &#39;port&#39;, &#39;protocol&#39;, &#39;user&#39;, &#39;jumphost&#39;]
@@ -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 &#34;fallback&#34; in params:
device[&#34;fallback&#34;] = params[&#34;fallback&#34;]
if &#34;tags&#34; in params:
device_tags = device.get(&#34;tags&#34;, {})
@@ -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(&#34;name&#34;, fallback_id)
n = node(node_name, host=params.get(&#34;host&#34;, &#34;&#34;), 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 != &#34;host&#34;}
if &#34;fallback&#34; in params:
dyn_kwargs[&#34;fallback&#34;] = params[&#34;fallback&#34;]
if &#34;tags&#34; in params:
n.tags = params[&#34;tags&#34;]
dyn_kwargs[&#34;tags&#34;] = params[&#34;tags&#34;]
n = node(
node_name,
host=params.get(&#34;host&#34;, &#34;&#34;),
config=user_config,
**dyn_kwargs,
)
else:
node_name = params.get(&#34;name&#34;, fallback_id)
n = node(node_name, host=params.get(&#34;host&#34;, &#34;&#34;), 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 != &#34;host&#34;}
if &#34;fallback&#34; in params:
dyn_kwargs[&#34;fallback&#34;] = params[&#34;fallback&#34;]
if &#34;tags&#34; in params:
n.tags = params[&#34;tags&#34;]
dyn_kwargs[&#34;tags&#34;] = params[&#34;tags&#34;]
n = node(
node_name,
host=params.get(&#34;host&#34;, &#34;&#34;),
config=user_config,
**dyn_kwargs,
)
n._orig_protocol = n.protocol
if sftp:
n.protocol = &#34;sftp&#34;
@@ -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, &#34;auth_tracker&#34;, None),
)
except Exception as exc: # noqa: BLE001
connect = str(sanitize_exception(exc, code=&#34;AUTH_FAILED&#34;))
# Send debug output to client before checking result (always show the command)
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&#34;[ERROR in run_async_loop] {e}&#34;)
traceback.print_exc()
except Exception as e: # noqa: BLE001
sanitized_err = sanitize_exception(e, code=&#34;TRANSPORT_ERROR&#34;, context_label=&#34;interact stream&#34;)
print(f&#34;[ERROR in run_async_loop] {sanitized_err}&#34;)
finally:
n._teardown_interact_environment()
response_queue.put(None) # Signal EOF
+2989 -743
View File
File diff suppressed because it is too large. Load diff
+149 -90
View File
@@ -162,20 +162,7 @@ el.replaceWith(d);
def _mask_secrets(self, val: Any) -&gt; Any:
&#34;&#34;&#34;Recursively mask passwords, passphrases, secrets, and auth tokens with &#39;***&#39;.&#34;&#34;&#34;
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 [&#34;password&#34;, &#34;passphrase&#34;, &#34;secret&#34;, &#34;token&#34;]):
masked[k] = &#34;***&#34;
elif isinstance(v, (dict, list)):
masked[k] = self._mask_secrets(v)
else:
masked[k] = v
return masked
if isinstance(val, list):
return [self._mask_secrets(item) for item in val]
return val
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) -&gt; list[str]:
if isinstance(raw_val, str):
if len(raw_val.encode(&#34;utf-8&#34;, errors=&#34;replace&#34;)) &gt; MAX_JSON_INPUT_BYTES:
raise ValueError(f&#34;Input exceeds maximum size of {MAX_JSON_INPUT_BYTES} bytes&#34;)
stripped = raw_val.strip()
if stripped.startswith((&#34;[&#34;, &#34;{&#34;)):
parsed = validate_json_depth_and_size(
stripped,
max_bytes=MAX_JSON_INPUT_BYTES,
max_depth=MAX_JSON_INPUT_DEPTH,
)
return parsed if isinstance(parsed, list) else [raw_val]
return ([raw_val] if raw_val else []) if allow_empty_str else [
c.strip() for c in raw_val.splitlines() if c.strip()
]
if isinstance(raw_val, list):
validate_json_depth_and_size(
raw_val,
max_bytes=MAX_JSON_INPUT_BYTES,
max_depth=MAX_JSON_INPUT_DEPTH,
)
return raw_val
return [str(raw_val)] if (raw_val or not allow_empty_str) else []
def run_commands(
self,
nodes_filter: str,
@@ -248,17 +260,10 @@ el.replaceWith(d);
if not self.services or not hasattr(self.services, &#34;execution&#34;):
return {&#34;success&#34;: False, &#34;error&#34;: &#34;Execution service unavailable.&#34;}
# 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)]
try:
commands = self._parse_json_list_or_lines(commands, allow_empty_str=False)
except ValueError as exc:
return {&#34;success&#34;: False, &#34;error&#34;: str(exc)}
# Check safety
if not self.trust:
@@ -277,9 +282,24 @@ el.replaceWith(d);
parallel=par_count,
timeout=timeout,
)
return {&#34;success&#34;: True, &#34;results&#34;: results}
fb_used = False
if isinstance(results, dict):
fb_used = any(
isinstance(v, dict) and bool(v.get(&#34;fallback_used&#34;, False))
for v in results.values()
)
if not fb_used:
fb_map = getattr(self.services.execution, &#34;last_fallback_used&#34;, {})
if isinstance(fb_map, dict):
fb_used = any(bool(v) for v in fb_map.values())
return {&#34;success&#34;: True, &#34;results&#34;: results, &#34;fallback_used&#34;: fb_used}
except Exception as e: # noqa: BLE001
return {&#34;success&#34;: False, &#34;error&#34;: str(e)}
san = sanitize_exception(
e,
code=&#34;AUTH_FAILED&#34;,
context_label=&#34;command execution failed&#34;,
)
return {&#34;success&#34;: False, &#34;error&#34;: str(san)}
def test_commands(
self,
@@ -293,29 +313,11 @@ el.replaceWith(d);
if not self.services or not hasattr(self.services, &#34;execution&#34;):
return {&#34;success&#34;: False, &#34;error&#34;: &#34;Execution service unavailable.&#34;}
# 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 []
try:
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 {&#34;success&#34;: False, &#34;error&#34;: str(exc)}
# Check safety
if not self.trust:
@@ -335,9 +337,16 @@ el.replaceWith(d);
parallel=par_count,
timeout=timeout,
)
return {&#34;success&#34;: True, &#34;results&#34;: results}
fb_map = getattr(self.services.execution, &#34;last_fallback_used&#34;, {})
fb_used = any(bool(v) for v in fb_map.values()) if isinstance(fb_map, dict) else False
return {&#34;success&#34;: True, &#34;results&#34;: results, &#34;fallback_used&#34;: fb_used}
except Exception as e: # noqa: BLE001
return {&#34;success&#34;: False, &#34;error&#34;: str(e)}
san = sanitize_exception(
e,
code=&#34;AUTH_FAILED&#34;,
context_label=&#34;test execution failed&#34;,
)
return {&#34;success&#34;: False, &#34;error&#34;: str(san)}
def validate_playbook(self, playbook_yaml: str) -&gt; dict:
&#34;&#34;&#34;Validate the syntax, schema, and tasks of a Connpy YAML playbook.&#34;&#34;&#34;
@@ -465,6 +474,47 @@ el.replaceWith(d);
# Level 3: Inventory Mutation Tools (CRUD - Requires trust=True)
# -------------------------------------------------------------------------
@staticmethod
def _validate_and_normalize_node_payload(node_data: dict[str, Any]) -&gt; dict[str, Any]:
validate_json_depth_and_size(
node_data,
max_bytes=MAX_JSON_INPUT_BYTES,
max_depth=MAX_JSON_INPUT_DEPTH,
)
normalized = node_data.copy()
if &#34;tags&#34; in normalized and normalized[&#34;tags&#34;] not in (&#34;&#34;, None):
tags_val = normalized[&#34;tags&#34;]
if isinstance(tags_val, str) and tags_val.strip().startswith(&#34;{&#34;):
tags_val = validate_json_depth_and_size(
tags_val,
max_bytes=MAX_JSON_INPUT_BYTES,
max_depth=MAX_JSON_INPUT_DEPTH,
)
normalized[&#34;tags&#34;] = tags_val
validate_auth_budget_tags(tags_val)
if &#34;fallback&#34; in normalized and normalized[&#34;fallback&#34;] not in (None, &#34;&#34;, []):
fb_val = normalized[&#34;fallback&#34;]
if isinstance(fb_val, str) and fb_val.strip().startswith((&#34;[&#34;, &#34;{&#34;)):
fb_val = validate_json_depth_and_size(
fb_val,
max_bytes=MAX_JSON_INPUT_BYTES,
max_depth=MAX_JSON_INPUT_DEPTH,
)
normalized[&#34;fallback&#34;] = validate_fallback_entries(
fb_val,
max_entries=MAX_FALLBACK_ENTRIES,
)
pw = normalized.get(&#34;password&#34;)
if is_compose_expression(pw):
validate_compose_syntax(pw)
elif isinstance(pw, (list, tuple)):
for item in pw:
if is_compose_expression(item):
validate_compose_syntax(item)
return normalized
def add_node(
self, unique_id: str, data: dict | None = None, is_folder: bool = False, **kwargs
) -&gt; 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 {
&#34;success&#34;: True,
&#34;message&#34;: f&#34;{&#39;Folder&#39; if is_folder else &#39;Node&#39;} &#39;{unique_id}&#39; created successfully.&#34;,
}
except (ValueError, TypeError, SanitizedAuthError) as exc:
return {&#34;success&#34;: False, &#34;error&#34;: str(exc)}
except Exception as e: # noqa: BLE001
return {&#34;success&#34;: False, &#34;error&#34;: 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 {&#34;success&#34;: True, &#34;message&#34;: f&#34;Node &#39;{unique_id}&#39; updated successfully.&#34;}
except (ValueError, TypeError, SanitizedAuthError) as exc:
return {&#34;success&#34;: False, &#34;error&#34;: str(exc)}
except Exception as e: # noqa: BLE001
return {&#34;success&#34;: False, &#34;error&#34;: 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 {
&#34;success&#34;: True,
&#34;message&#34;: f&#34;{&#39;Folder&#39; if is_folder else &#39;Node&#39;} &#39;{unique_id}&#39; created successfully.&#34;,
}
except (ValueError, TypeError, SanitizedAuthError) as exc:
return {&#34;success&#34;: False, &#34;error&#34;: str(exc)}
except Exception as e: # noqa: BLE001
return {&#34;success&#34;: False, &#34;error&#34;: str(e)}</code></pre>
</details>
@@ -865,17 +924,10 @@ el.replaceWith(d);
if not self.services or not hasattr(self.services, &#34;execution&#34;):
return {&#34;success&#34;: False, &#34;error&#34;: &#34;Execution service unavailable.&#34;}
# 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)]
try:
commands = self._parse_json_list_or_lines(commands, allow_empty_str=False)
except ValueError as exc:
return {&#34;success&#34;: False, &#34;error&#34;: str(exc)}
# Check safety
if not self.trust:
@@ -894,9 +946,24 @@ el.replaceWith(d);
parallel=par_count,
timeout=timeout,
)
return {&#34;success&#34;: True, &#34;results&#34;: results}
fb_used = False
if isinstance(results, dict):
fb_used = any(
isinstance(v, dict) and bool(v.get(&#34;fallback_used&#34;, False))
for v in results.values()
)
if not fb_used:
fb_map = getattr(self.services.execution, &#34;last_fallback_used&#34;, {})
if isinstance(fb_map, dict):
fb_used = any(bool(v) for v in fb_map.values())
return {&#34;success&#34;: True, &#34;results&#34;: results, &#34;fallback_used&#34;: fb_used}
except Exception as e: # noqa: BLE001
return {&#34;success&#34;: False, &#34;error&#34;: str(e)}</code></pre>
san = sanitize_exception(
e,
code=&#34;AUTH_FAILED&#34;,
context_label=&#34;command execution failed&#34;,
)
return {&#34;success&#34;: False, &#34;error&#34;: str(san)}</code></pre>
</details>
<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, &#34;execution&#34;):
return {&#34;success&#34;: False, &#34;error&#34;: &#34;Execution service unavailable.&#34;}
# 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 []
try:
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 {&#34;success&#34;: False, &#34;error&#34;: str(exc)}
# Check safety
if not self.trust:
@@ -1028,9 +1077,16 @@ el.replaceWith(d);
parallel=par_count,
timeout=timeout,
)
return {&#34;success&#34;: True, &#34;results&#34;: results}
fb_map = getattr(self.services.execution, &#34;last_fallback_used&#34;, {})
fb_used = any(bool(v) for v in fb_map.values()) if isinstance(fb_map, dict) else False
return {&#34;success&#34;: True, &#34;results&#34;: results, &#34;fallback_used&#34;: fb_used}
except Exception as e: # noqa: BLE001
return {&#34;success&#34;: False, &#34;error&#34;: str(e)}</code></pre>
san = sanitize_exception(
e,
code=&#34;AUTH_FAILED&#34;,
context_label=&#34;test execution failed&#34;,
)
return {&#34;success&#34;: False, &#34;error&#34;: str(san)}</code></pre>
</details>
<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 {&#34;success&#34;: True, &#34;message&#34;: f&#34;Node &#39;{unique_id}&#39; updated successfully.&#34;}
except (ValueError, TypeError, SanitizedAuthError) as exc:
return {&#34;success&#34;: False, &#34;error&#34;: str(exc)}
except Exception as e: # noqa: BLE001
return {&#34;success&#34;: False, &#34;error&#34;: str(e)}</code></pre>
</details>
+1
View File
@@ -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>
+342 -127
View File
@@ -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,104 +58,239 @@ el.replaceWith(d);
<pre><code class="python">class ExecutionService(BaseService):
&#34;&#34;&#34;Business logic for executing commands on nodes and running automation scripts.&#34;&#34;&#34;
def run_commands(
self,
nodes_filter: str,
commands: List[str],
variables: Optional[Dict[str, Any]] = None,
parallel: int = 10,
timeout: int = 20,
folder: Optional[str] = None,
prompt: Optional[str] = None,
on_node_complete: Optional[Callable] = None,
logger: Optional[Callable] = None,
name: Optional[str] = None
) -&gt; Dict[str, str]:
def __init__(self, config=None, remote_stub=None, attempt_tracker=None):
super().__init__(config)
self.remote_stub = remote_stub
self.attempt_tracker = (
attempt_tracker if attempt_tracker is not None else AuthAttemptTracker()
)
self.last_fallback_used: dict[str, bool] = {}
def _is_remote_backend(self) -&gt; bool:
&#34;&#34;&#34;Return True when execution is configured to run against a remote gRPC backend.&#34;&#34;&#34;
if getattr(self, &#34;remote_stub&#34;, None) is not None:
return True
cfg = getattr(self.config, &#34;config&#34;, None)
return isinstance(cfg, dict) and cfg.get(&#34;service_mode&#34;) == &#34;remote&#34;
def _ensure_backend_supports_stage_timeouts(
self,
connect_timeout: int | None = None,
auth_timeout: int | None = None,
) -&gt; None:
&#34;&#34;&#34;Raise ConnpyError if per-stage timeout overrides are used on gRPC remote backend.&#34;&#34;&#34;
if self._is_remote_backend() and (connect_timeout is not None or auth_timeout is not None):
raise ConnpyError(
&#34;connect_timeout and auth_timeout overrides are not supported in remote gRPC mode&#34;
)
def _get_remote_stub(self):
&#34;&#34;&#34;Return the configured or lazily constructed ExecutionStub for remote gRPC mode.&#34;&#34;&#34;
if getattr(self, &#34;remote_stub&#34;, None) is not None:
return self.remote_stub
cfg = getattr(self.config, &#34;config&#34;, None)
remote_host = cfg.get(&#34;remote_host&#34;) if isinstance(cfg, dict) else None
if not remote_host:
raise ConnpyError(&#34;Remote host must be specified in remote mode&#34;)
import grpc
from connpy.grpc_layer.stubs import AuthClientInterceptor, ExecutionStub
def get_token():
env_token = os.environ.get(&#34;CONNPY_TOKEN&#34;)
if env_token:
return env_token
defaultdir = getattr(self.config, &#34;defaultdir&#34;, None)
if defaultdir:
token_path = os.path.join(defaultdir, &#34;.token&#34;)
if os.path.exists(token_path):
with suppress(OSError), open(token_path) as f:
return f.read().strip()
return None
channel = grpc.insecure_channel(remote_host)
channel = grpc.intercept_channel(channel, AuthClientInterceptor(get_token))
return ExecutionStub(channel, remote_host=remote_host)
def run_commands(
self,
nodes_filter: str,
commands: list[str],
variables: dict[str, Any] | None = None,
parallel: int = 10,
timeout: int | None = None,
folder: str | None = None,
prompt: str | None = None,
on_node_complete: Callable | None = None,
logger: Callable | None = None,
name: str | None = None,
connect_timeout: int | None = None,
auth_timeout: int | None = None,
) -&gt; dict[str, Any]:
&#34;&#34;&#34;Execute commands on a set of nodes.&#34;&#34;&#34;
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 &#34;&#34;,
)
matched_names = self.config._getallnodes(nodes_filter)
if not matched_names:
raise ConnpyError(f&#34;No nodes found matching filter: {nodes_filter}&#34;)
node_data = self.config.getitems(matched_names, extract=True)
executor = Nodes(node_data, config=self.config)
self.last_executor = executor
results = executor.run(
commands=commands,
vars=variables,
parallel=parallel,
timeout=timeout,
folder=folder,
prompt=prompt,
on_complete=on_node_complete,
logger=logger
)
# Combine output and status for the caller
run_kwargs: dict[str, Any] = {
&#34;commands&#34;: commands,
&#34;vars&#34;: variables,
&#34;parallel&#34;: parallel,
&#34;timeout&#34;: timeout,
&#34;folder&#34;: folder,
&#34;prompt&#34;: prompt,
&#34;on_complete&#34;: on_node_complete,
&#34;logger&#34;: logger,
&#34;connect_timeout&#34;: connect_timeout,
&#34;auth_timeout&#34;: auth_timeout,
&#34;_default_cmd_timeout&#34;: 20,
}
run_params = inspect.signature(executor.run).parameters
if &#34;unattended&#34; in run_params:
run_kwargs[&#34;unattended&#34;] = True
if &#34;attempt_tracker&#34; in run_params:
run_kwargs[&#34;attempt_tracker&#34;] = self.attempt_tracker
results = executor.run(**run_kwargs)
fb_map = getattr(executor, &#34;fallback_used&#34;, {})
fb_dict = fb_map if isinstance(fb_map, dict) else {}
# Combine output, status, and fallback metadata for the caller
full_results = {}
self.last_fallback_used = {}
for unique in results:
fb_used = bool(fb_dict.get(unique, False))
full_results[unique] = {
&#34;output&#34;: results[unique],
&#34;status&#34;: executor.status.get(unique, 1)
&#34;status&#34;: executor.status.get(unique, 1),
&#34;fallback_used&#34;: fb_used,
}
self.last_fallback_used[unique] = fb_used
return full_results
except Exception as e:
raise ConnpyError(f&#34;Execution failed: {e}&#34;)
except ConnpyError:
raise
except Exception as e: # noqa: BLE001
raise_sanitized_auth_error(e, code=&#34;AUTH_FAILED&#34;, context_label=&#34;execution failed&#34;)
def test_commands(
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
) -&gt; 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,
) -&gt; dict[str, dict[str, bool]]:
&#34;&#34;&#34;Run commands and verify expected output on a set of nodes.&#34;&#34;&#34;
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 &#34;&#34;,
prompt=prompt,
on_node_complete=on_node_complete,
logger=logger,
name=name or &#34;&#34;,
)
matched_names = self.config._getallnodes(nodes_filter)
if not matched_names:
raise ConnpyError(f&#34;No nodes found matching filter: {nodes_filter}&#34;)
node_data = self.config.getitems(matched_names, extract=True)
executor = Nodes(node_data, config=self.config)
self.last_executor = executor
results = executor.test(
commands=commands,
expected=expected,
vars=variables,
parallel=parallel,
timeout=timeout,
folder=folder,
prompt=prompt,
on_complete=on_node_complete,
logger=logger
)
return results
except Exception as e:
raise ConnpyError(f&#34;Testing failed: {e}&#34;)
def run_cli_script(self, nodes_filter: str, script_path: str, parallel: int = 10) -&gt; Dict[str, str]:
test_kwargs: dict[str, Any] = {
&#34;commands&#34;: commands,
&#34;expected&#34;: expected,
&#34;vars&#34;: variables,
&#34;parallel&#34;: parallel,
&#34;timeout&#34;: timeout,
&#34;folder&#34;: folder,
&#34;prompt&#34;: prompt,
&#34;on_complete&#34;: on_node_complete,
&#34;logger&#34;: logger,
&#34;connect_timeout&#34;: connect_timeout,
&#34;auth_timeout&#34;: auth_timeout,
&#34;_default_cmd_timeout&#34;: 20,
}
test_params = inspect.signature(executor.test).parameters
if &#34;unattended&#34; in test_params:
test_kwargs[&#34;unattended&#34;] = True
if &#34;attempt_tracker&#34; in test_params:
test_kwargs[&#34;attempt_tracker&#34;] = self.attempt_tracker
res = executor.test(**test_kwargs)
fb_map = getattr(executor, &#34;fallback_used&#34;, {})
fb_dict = fb_map if isinstance(fb_map, dict) else {}
if isinstance(res, dict):
self.last_fallback_used = {u: bool(fb_dict.get(u, False)) for u in res}
else:
self.last_fallback_used = {}
return res
except ConnpyError:
raise
except Exception as e: # noqa: BLE001
raise_sanitized_auth_error(e, code=&#34;AUTH_FAILED&#34;, context_label=&#34;testing failed&#34;)
def run_cli_script(self, nodes_filter: str, script_path: str, parallel: int = 10) -&gt; dict[str, str]:
&#34;&#34;&#34;Run a plain-text script containing one command per line.&#34;&#34;&#34;
if not os.path.exists(script_path):
raise ConnpyError(f&#34;Script file not found: {script_path}&#34;)
try:
with open(script_path, &#34;r&#34;) as f:
with open(script_path) as f:
commands = [line.strip() for line in f if line.strip()]
except Exception as e:
raise ConnpyError(f&#34;Failed to read script {script_path}: {e}&#34;)
except OSError:
raise ConnpyError(&#34;Failed to read script file&#34;) from None
return self.run_commands(nodes_filter, commands, parallel=parallel)</code></pre>
</details>
<div class="desc"><p>Business logic for executing commands on nodes and running automation scripts.</p>
@@ -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) -&gt; Dict[str, str]:
<pre><code class="python">def run_cli_script(self, nodes_filter: str, script_path: str, parallel: int = 10) -&gt; dict[str, str]:
&#34;&#34;&#34;Run a plain-text script containing one command per line.&#34;&#34;&#34;
if not os.path.exists(script_path):
raise ConnpyError(f&#34;Script file not found: {script_path}&#34;)
try:
with open(script_path, &#34;r&#34;) as f:
with open(script_path) as f:
commands = [line.strip() for line in f if line.strip()]
except Exception as e:
raise ConnpyError(f&#34;Failed to read script {script_path}: {e}&#34;)
except OSError:
raise ConnpyError(&#34;Failed to read script file&#34;) 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">
@@ -204,56 +339,96 @@ el.replaceWith(d);
<span>Expand source code</span>
</summary>
<pre><code class="python">def run_commands(
self,
nodes_filter: str,
commands: List[str],
variables: Optional[Dict[str, Any]] = None,
self,
nodes_filter: str,
commands: list[str],
variables: dict[str, Any] | None = None,
parallel: int = 10,
timeout: int = 20,
folder: Optional[str] = None,
prompt: Optional[str] = None,
on_node_complete: Optional[Callable] = None,
logger: Optional[Callable] = None,
name: Optional[str] = None
) -&gt; 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,
) -&gt; dict[str, Any]:
&#34;&#34;&#34;Execute commands on a set of nodes.&#34;&#34;&#34;
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 &#34;&#34;,
)
matched_names = self.config._getallnodes(nodes_filter)
if not matched_names:
raise ConnpyError(f&#34;No nodes found matching filter: {nodes_filter}&#34;)
node_data = self.config.getitems(matched_names, extract=True)
executor = Nodes(node_data, config=self.config)
self.last_executor = executor
results = executor.run(
commands=commands,
vars=variables,
parallel=parallel,
timeout=timeout,
folder=folder,
prompt=prompt,
on_complete=on_node_complete,
logger=logger
)
# Combine output and status for the caller
run_kwargs: dict[str, Any] = {
&#34;commands&#34;: commands,
&#34;vars&#34;: variables,
&#34;parallel&#34;: parallel,
&#34;timeout&#34;: timeout,
&#34;folder&#34;: folder,
&#34;prompt&#34;: prompt,
&#34;on_complete&#34;: on_node_complete,
&#34;logger&#34;: logger,
&#34;connect_timeout&#34;: connect_timeout,
&#34;auth_timeout&#34;: auth_timeout,
&#34;_default_cmd_timeout&#34;: 20,
}
run_params = inspect.signature(executor.run).parameters
if &#34;unattended&#34; in run_params:
run_kwargs[&#34;unattended&#34;] = True
if &#34;attempt_tracker&#34; in run_params:
run_kwargs[&#34;attempt_tracker&#34;] = self.attempt_tracker
results = executor.run(**run_kwargs)
fb_map = getattr(executor, &#34;fallback_used&#34;, {})
fb_dict = fb_map if isinstance(fb_map, dict) else {}
# Combine output, status, and fallback metadata for the caller
full_results = {}
self.last_fallback_used = {}
for unique in results:
fb_used = bool(fb_dict.get(unique, False))
full_results[unique] = {
&#34;output&#34;: results[unique],
&#34;status&#34;: executor.status.get(unique, 1)
&#34;status&#34;: executor.status.get(unique, 1),
&#34;fallback_used&#34;: fb_used,
}
self.last_fallback_used[unique] = fb_used
return full_results
except Exception as e:
raise ConnpyError(f&#34;Execution failed: {e}&#34;)</code></pre>
except ConnpyError:
raise
except Exception as e: # noqa: BLE001
raise_sanitized_auth_error(e, code=&#34;AUTH_FAILED&#34;, context_label=&#34;execution failed&#34;)</code></pre>
</details>
<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,42 +438,82 @@ 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
) -&gt; 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,
) -&gt; dict[str, dict[str, bool]]:
&#34;&#34;&#34;Run commands and verify expected output on a set of nodes.&#34;&#34;&#34;
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 &#34;&#34;,
prompt=prompt,
on_node_complete=on_node_complete,
logger=logger,
name=name or &#34;&#34;,
)
matched_names = self.config._getallnodes(nodes_filter)
if not matched_names:
raise ConnpyError(f&#34;No nodes found matching filter: {nodes_filter}&#34;)
node_data = self.config.getitems(matched_names, extract=True)
executor = Nodes(node_data, config=self.config)
self.last_executor = executor
results = executor.test(
commands=commands,
expected=expected,
vars=variables,
parallel=parallel,
timeout=timeout,
folder=folder,
prompt=prompt,
on_complete=on_node_complete,
logger=logger
)
return results
except Exception as e:
raise ConnpyError(f&#34;Testing failed: {e}&#34;)</code></pre>
test_kwargs: dict[str, Any] = {
&#34;commands&#34;: commands,
&#34;expected&#34;: expected,
&#34;vars&#34;: variables,
&#34;parallel&#34;: parallel,
&#34;timeout&#34;: timeout,
&#34;folder&#34;: folder,
&#34;prompt&#34;: prompt,
&#34;on_complete&#34;: on_node_complete,
&#34;logger&#34;: logger,
&#34;connect_timeout&#34;: connect_timeout,
&#34;auth_timeout&#34;: auth_timeout,
&#34;_default_cmd_timeout&#34;: 20,
}
test_params = inspect.signature(executor.test).parameters
if &#34;unattended&#34; in test_params:
test_kwargs[&#34;unattended&#34;] = True
if &#34;attempt_tracker&#34; in test_params:
test_kwargs[&#34;attempt_tracker&#34;] = self.attempt_tracker
res = executor.test(**test_kwargs)
fb_map = getattr(executor, &#34;fallback_used&#34;, {})
fb_dict = fb_map if isinstance(fb_map, dict) else {}
if isinstance(res, dict):
self.last_fallback_used = {u: bool(fb_dict.get(u, False)) for u in res}
else:
self.last_fallback_used = {}
return res
except ConnpyError:
raise
except Exception as e: # noqa: BLE001
raise_sanitized_auth_error(e, code=&#34;AUTH_FAILED&#34;, context_label=&#34;testing failed&#34;)</code></pre>
</details>
<div class="desc"><p>Run commands and verify expected output on a set of nodes.</p></div>
</dd>
File diff suppressed because it is too large. Load diff
+434 -84
View File
@@ -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({
&#34;id&#34;,
&#34;host&#34;,
&#34;folder&#34;,
&#34;subfolder&#34;,
&#34;options&#34;,
&#34;logs&#34;,
&#34;password&#34;,
&#34;port&#34;,
&#34;protocol&#34;,
&#34;user&#34;,
&#34;tags&#34;,
&#34;jumphost&#34;,
&#34;fallback&#34;,
&#34;type&#34;,
})
def __init__(self, config=None):
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 &#34;tags&#34; in data and data[&#34;tags&#34;] not in (&#34;&#34;, None):
validate_auth_budget_tags(data[&#34;tags&#34;])
if &#34;fallback&#34; in data and data[&#34;fallback&#34;] not in (None, &#34;&#34;, []):
validate_fallback_entries(data[&#34;fallback&#34;])
pw = data.get(&#34;password&#34;)
if is_compose_expression(pw):
validate_compose_syntax(pw)
elif isinstance(pw, (list, tuple)):
for item in pw:
if is_compose_expression(item):
validate_compose_syntax(item)
except (ValueError, TypeError, SanitizedAuthError) as exc:
raise InvalidConfigurationError(str(exc)) from None
def list_nodes(self, filter_str=None, format_str=None):
&#34;&#34;&#34;Return a listed filtered by regex match and formatted if needed.&#34;&#34;&#34;
@@ -135,7 +179,7 @@ el.replaceWith(d);
raise NodeNotFoundError(f&#34;Node &#39;{unique_id}&#39; not found.&#34;)
return details
except (KeyError, TypeError):
raise NodeNotFoundError(f&#34;Node &#39;{unique_id}&#39; not found.&#34;)
raise NodeNotFoundError(f&#34;Node &#39;{unique_id}&#39; not found.&#34;) from None
def explode_unique(self, unique_id):
&#34;&#34;&#34;Explode a unique ID into a dictionary of its parts.&#34;&#34;&#34;
@@ -165,58 +209,127 @@ el.replaceWith(d);
&#34;&#34;&#34;Logic for adding a new node or folder to configuration.&#34;&#34;&#34;
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&#34;Folder &#39;{unique_id}&#39; already exists.&#34;)
uniques = cfg._explode_unique_unlocked(unique_id)
if not uniques:
raise InvalidConfigurationError(f&#34;Invalid folder name &#39;{unique_id}&#39;.&#34;)
if &#34;subfolder&#34; in uniques:
parent_folder = f&#34;@{uniques[&#39;folder&#39;]}&#34;
if parent_folder not in all_folders:
raise NodeNotFoundError(f&#34;Folder &#39;{parent_folder}&#39; not found.&#34;)
cfg._folder_add_unlocked(**uniques)
else:
if unique_id in all_nodes:
raise NodeAlreadyExistsError(f&#34;Node &#39;{unique_id}&#39; already exists.&#34;)
node_folder = unique_id.partition(&#34;@&#34;)[2]
if node_folder:
parent_folder = f&#34;@{node_folder}&#34;
if parent_folder not in all_folders:
raise NodeNotFoundError(f&#34;Folder &#39;{parent_folder}&#39; not found.&#34;)
payload = deepcopy(data)
if &#34;id&#34; not in payload:
uniques = cfg._explode_unique_unlocked(unique_id)
if uniques and &#34;id&#34; in uniques:
payload[&#34;id&#34;] = uniques[&#34;id&#34;]
cfg._connections_add_unlocked(**payload)
self.config.transactional_update(_mut)
return
all_nodes = self.config._getallnodes()
all_folders = self.config._getallfolders()
if is_folder:
if unique_id in all_folders:
raise NodeAlreadyExistsError(f&#34;Folder &#39;{unique_id}&#39; already exists.&#34;)
uniques = self.config._explode_unique(unique_id)
if not uniques:
raise InvalidConfigurationError(f&#34;Invalid folder name &#39;{unique_id}&#39;.&#34;)
# Check if parent folder exists when creating a subfolder
if &#34;subfolder&#34; in uniques:
self.validate_parent_folder(unique_id, is_folder=True)
self.config._folder_add(**uniques)
self.config._saveconfig(self.config.file)
else:
if unique_id in all_nodes:
raise NodeAlreadyExistsError(f&#34;Node &#39;{unique_id}&#39; already exists.&#34;)
# Check if parent folder exists when creating a node in a folder
self.validate_parent_folder(unique_id)
# Ensure &#39;id&#39; is in data for config._connections_add
if &#34;id&#34; not in data:
payload = deepcopy(data)
if &#34;id&#34; not in payload:
uniques = self.config._explode_unique(unique_id)
if uniques and &#34;id&#34; in uniques:
data[&#34;id&#34;] = uniques[&#34;id&#34;]
self.config._connections_add(**data)
payload[&#34;id&#34;] = uniques[&#34;id&#34;]
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 &#34;id&#34; not in filtered and isinstance(uniques, dict) and &#34;id&#34; in uniques:
filtered[&#34;id&#34;] = uniques[&#34;id&#34;]
self._validate_node_payload(filtered)
return filtered
def update_node(self, unique_id, data, save=True):
&#34;&#34;&#34;Explicitly update an existing node.&#34;&#34;&#34;
if _supports_transactional_update(self.config) and save:
def _mut(cfg):
all_nodes = cfg._getallnodes_unlocked()
if unique_id not in all_nodes:
raise NodeNotFoundError(f&#34;Node &#39;{unique_id}&#39; not found.&#34;)
uniques = cfg._explode_unique_unlocked(unique_id)
existing = cfg.getitem_unlocked(unique_id, extract=False)
payload = self._build_update_payload(unique_id, data, existing, uniques)
cfg._connections_add_unlocked(**payload)
self.config.transactional_update(_mut)
return
all_nodes = self.config._getallnodes()
if unique_id not in all_nodes:
raise NodeNotFoundError(f&#34;Node &#39;{unique_id}&#39; not found.&#34;)
# Ensure &#39;id&#39; is in data for config._connections_add
if &#34;id&#34; not in data:
uniques = self.config._explode_unique(unique_id)
if uniques:
data[&#34;id&#34;] = uniques[&#34;id&#34;]
# config._connections_add actually handles updates if ID exists correctly
self.config._connections_add(**data)
uniques = self.config._explode_unique(unique_id)
existing = self.config.getitem(unique_id, extract=False)
payload = self._build_update_payload(unique_id, data, existing, uniques)
self.config._connections_add(**payload)
if save:
self.config._saveconfig(self.config.file)
def delete_node(self, unique_id, is_folder=False, save=True):
&#34;&#34;&#34;Logic for deleting a node or folder.&#34;&#34;&#34;
if _supports_transactional_update(self.config) and save:
def _mut(cfg):
uniques = cfg._explode_unique_unlocked(unique_id)
if not uniques:
label = &#34;Folder&#34; if is_folder else &#34;Node&#34;
raise NodeNotFoundError(f&#34;{label} &#39;{unique_id}&#39; not found or invalid.&#34;)
if is_folder:
cfg._folder_del_unlocked(**uniques)
else:
cfg._connections_del_unlocked(**uniques)
self.config.transactional_update(_mut)
return
if is_folder:
uniques = self.config._explode_unique(unique_id)
if not uniques:
@@ -227,23 +340,40 @@ el.replaceWith(d);
if not uniques:
raise NodeNotFoundError(f&#34;Node &#39;{unique_id}&#39; not found or invalid.&#34;)
self.config._connections_del(**uniques)
if save:
self.config._saveconfig(self.config.file)
def connect_node(self, unique_id, sftp=False, debug=False, logger=None):
&#34;&#34;&#34;Interact with a node directly.&#34;&#34;&#34;
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&#34;Node &#39;{unique_id}&#39; not found.&#34;)
# Resolve profiles
profile_service = ProfileService(self.config)
resolved_data = profile_service.resolve_node_data(node_data)
node_params = inspect.signature(node.__init__).parameters
if &#34;fallback&#34; not in node_params and &#34;fallback&#34; in resolved_data:
raw_fb = resolved_data.pop(&#34;fallback&#34;, None)
if raw_fb not in (None, &#34;&#34;, []):
resolved_fb = resolve_fallback_entries(raw_fb, self.config.profiles)
if resolved_fb:
tags_val = resolved_data.get(&#34;tags&#34;, &#34;&#34;)
if isinstance(tags_val, dict):
tags_copy = deepcopy(tags_val)
tags_copy[&#34;_fallback&#34;] = resolved_fb
resolved_data[&#34;tags&#34;] = tags_copy
elif tags_val in (&#34;&#34;, None):
resolved_data[&#34;tags&#34;] = {&#34;_fallback&#34;: resolved_fb}
else:
resolved_data[&#34;tags&#34;] = {&#34;_raw&#34;: tags_val, &#34;_fallback&#34;: resolved_fb}
n = node(unique_id, **resolved_data, config=self.config)
if sftp:
n.protocol = &#34;sftp&#34;
@@ -253,50 +383,109 @@ el.replaceWith(d);
def move_node(self, src_id, dst_id, copy=False):
&#34;&#34;&#34;Move or copy a node.&#34;&#34;&#34;
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&#34;Source node &#39;{src_id}&#39; not found.&#34;)
if dst_id in cfg._getallnodes_unlocked():
raise NodeAlreadyExistsError(f&#34;Destination node &#39;{dst_id}&#39; already exists.&#34;)
new_uniques = cfg._explode_unique_unlocked(dst_id)
if not new_uniques:
raise InvalidConfigurationError(f&#34;Invalid destination format &#39;{dst_id}&#39;.&#34;)
new_node_data = deepcopy(node_data)
new_node_data.update(new_uniques)
cfg._connections_add_unlocked(**new_node_data)
if not copy:
src_uniques = cfg._explode_unique_unlocked(src_id)
cfg._connections_del_unlocked(**src_uniques)
self.config.transactional_update(_mut)
return
node_data = self.config.getitem(src_id)
if not node_data:
raise NodeNotFoundError(f&#34;Source node &#39;{src_id}&#39; not found.&#34;)
if dst_id in self.config._getallnodes():
raise NodeAlreadyExistsError(f&#34;Destination node &#39;{dst_id}&#39; already exists.&#34;)
new_uniques = self.config._explode_unique(dst_id)
if not new_uniques:
raise InvalidConfigurationError(f&#34;Invalid destination format &#39;{dst_id}&#39;.&#34;)
new_node_data = node_data.copy()
new_node_data.update(new_uniques)
self.config._connections_add(**new_node_data)
if not copy:
src_uniques = self.config._explode_unique(src_id)
self.config._connections_del(**src_uniques)
self.config._saveconfig(self.config.file)
def bulk_add(self, ids, hosts, common_data):
&#34;&#34;&#34;Add multiple nodes with shared common configuration.&#34;&#34;&#34;
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 &lt; len(hosts) else hosts[0]
uniques = cfg._explode_unique_unlocked(uid)
if not uniques:
continue
node_data = deepcopy(common_data)
node_data.pop(&#34;ids&#34;, None)
node_data.pop(&#34;location&#34;, None)
node_data.update(uniques)
node_data[&#34;host&#34;] = host
node_data[&#34;type&#34;] = &#34;connection&#34;
planned.append(node_data)
return planned
with self.config._mutation_lock:
if not _build_entries(self.config):
return 0
def _mut(cfg):
entries = _build_entries(cfg)
for node_data in entries:
cfg._connections_add_unlocked(**node_data)
return len(entries)
return self.config.transactional_update(_mut)
count = 0
all_nodes = self.config._getallnodes()
for i, uid in enumerate(ids):
if uid in all_nodes:
continue
try:
self._validate_node_name(uid)
except ReservedNameError:
# For bulk, we might want to just skip or log.
# CLI caller will handle if it wants to be strict.
continue
host = hosts[i] if i &lt; len(hosts) else hosts[0]
uniques = self.config._explode_unique(uid)
if not uniques:
continue
node_data = common_data.copy()
node_data.pop(&#34;ids&#34;, None)
node_data.pop(&#34;location&#34;, None)
@@ -306,13 +495,28 @@ el.replaceWith(d);
self.config._connections_add(**node_data)
count += 1
if count &gt; 0:
self.config._saveconfig(self.config.file)
return count
def full_replace(self, connections, profiles):
&#34;&#34;&#34;Replace all connections and profiles with new data.&#34;&#34;&#34;
from ..auth_utils import SanitizedAuthError
if _supports_transactional_update(self.config):
def _mut(cfg):
cfg.connections.clear()
cfg.connections.update(deepcopy(connections))
cfg.profiles.clear()
cfg.profiles.update(deepcopy(profiles))
try:
self.config.transactional_update(_mut)
except (ValueError, TypeError, SanitizedAuthError) as exc:
raise InvalidConfigurationError(str(exc)) from None
return
self.config.connections = connections
self.config.profiles = profiles
self.config._saveconfig(self.config.file)
@@ -350,37 +554,69 @@ el.replaceWith(d);
&#34;&#34;&#34;Logic for adding a new node or folder to configuration.&#34;&#34;&#34;
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&#34;Folder &#39;{unique_id}&#39; already exists.&#34;)
uniques = cfg._explode_unique_unlocked(unique_id)
if not uniques:
raise InvalidConfigurationError(f&#34;Invalid folder name &#39;{unique_id}&#39;.&#34;)
if &#34;subfolder&#34; in uniques:
parent_folder = f&#34;@{uniques[&#39;folder&#39;]}&#34;
if parent_folder not in all_folders:
raise NodeNotFoundError(f&#34;Folder &#39;{parent_folder}&#39; not found.&#34;)
cfg._folder_add_unlocked(**uniques)
else:
if unique_id in all_nodes:
raise NodeAlreadyExistsError(f&#34;Node &#39;{unique_id}&#39; already exists.&#34;)
node_folder = unique_id.partition(&#34;@&#34;)[2]
if node_folder:
parent_folder = f&#34;@{node_folder}&#34;
if parent_folder not in all_folders:
raise NodeNotFoundError(f&#34;Folder &#39;{parent_folder}&#39; not found.&#34;)
payload = deepcopy(data)
if &#34;id&#34; not in payload:
uniques = cfg._explode_unique_unlocked(unique_id)
if uniques and &#34;id&#34; in uniques:
payload[&#34;id&#34;] = uniques[&#34;id&#34;]
cfg._connections_add_unlocked(**payload)
self.config.transactional_update(_mut)
return
all_nodes = self.config._getallnodes()
all_folders = self.config._getallfolders()
if is_folder:
if unique_id in all_folders:
raise NodeAlreadyExistsError(f&#34;Folder &#39;{unique_id}&#39; already exists.&#34;)
uniques = self.config._explode_unique(unique_id)
if not uniques:
raise InvalidConfigurationError(f&#34;Invalid folder name &#39;{unique_id}&#39;.&#34;)
# Check if parent folder exists when creating a subfolder
if &#34;subfolder&#34; in uniques:
self.validate_parent_folder(unique_id, is_folder=True)
self.config._folder_add(**uniques)
self.config._saveconfig(self.config.file)
else:
if unique_id in all_nodes:
raise NodeAlreadyExistsError(f&#34;Node &#39;{unique_id}&#39; already exists.&#34;)
# Check if parent folder exists when creating a node in a folder
self.validate_parent_folder(unique_id)
# Ensure &#39;id&#39; is in data for config._connections_add
if &#34;id&#34; not in data:
payload = deepcopy(data)
if &#34;id&#34; not in payload:
uniques = self.config._explode_unique(unique_id)
if uniques and &#34;id&#34; in uniques:
data[&#34;id&#34;] = uniques[&#34;id&#34;]
self.config._connections_add(**data)
payload[&#34;id&#34;] = uniques[&#34;id&#34;]
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,25 +631,61 @@ el.replaceWith(d);
</summary>
<pre><code class="python">def bulk_add(self, ids, hosts, common_data):
&#34;&#34;&#34;Add multiple nodes with shared common configuration.&#34;&#34;&#34;
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 &lt; len(hosts) else hosts[0]
uniques = cfg._explode_unique_unlocked(uid)
if not uniques:
continue
node_data = deepcopy(common_data)
node_data.pop(&#34;ids&#34;, None)
node_data.pop(&#34;location&#34;, None)
node_data.update(uniques)
node_data[&#34;host&#34;] = host
node_data[&#34;type&#34;] = &#34;connection&#34;
planned.append(node_data)
return planned
with self.config._mutation_lock:
if not _build_entries(self.config):
return 0
def _mut(cfg):
entries = _build_entries(cfg)
for node_data in entries:
cfg._connections_add_unlocked(**node_data)
return len(entries)
return self.config.transactional_update(_mut)
count = 0
all_nodes = self.config._getallnodes()
for i, uid in enumerate(ids):
if uid in all_nodes:
continue
try:
self._validate_node_name(uid)
except ReservedNameError:
# For bulk, we might want to just skip or log.
# CLI caller will handle if it wants to be strict.
continue
host = hosts[i] if i &lt; len(hosts) else hosts[0]
uniques = self.config._explode_unique(uid)
if not uniques:
continue
node_data = common_data.copy()
node_data.pop(&#34;ids&#34;, None)
node_data.pop(&#34;location&#34;, None)
@@ -423,7 +695,7 @@ el.replaceWith(d);
self.config._connections_add(**node_data)
count += 1
if count &gt; 0:
self.config._saveconfig(self.config.file)
return count</code></pre>
@@ -441,16 +713,33 @@ el.replaceWith(d);
<pre><code class="python">def connect_node(self, unique_id, sftp=False, debug=False, logger=None):
&#34;&#34;&#34;Interact with a node directly.&#34;&#34;&#34;
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&#34;Node &#39;{unique_id}&#39; not found.&#34;)
# Resolve profiles
profile_service = ProfileService(self.config)
resolved_data = profile_service.resolve_node_data(node_data)
node_params = inspect.signature(node.__init__).parameters
if &#34;fallback&#34; not in node_params and &#34;fallback&#34; in resolved_data:
raw_fb = resolved_data.pop(&#34;fallback&#34;, None)
if raw_fb not in (None, &#34;&#34;, []):
resolved_fb = resolve_fallback_entries(raw_fb, self.config.profiles)
if resolved_fb:
tags_val = resolved_data.get(&#34;tags&#34;, &#34;&#34;)
if isinstance(tags_val, dict):
tags_copy = deepcopy(tags_val)
tags_copy[&#34;_fallback&#34;] = resolved_fb
resolved_data[&#34;tags&#34;] = tags_copy
elif tags_val in (&#34;&#34;, None):
resolved_data[&#34;tags&#34;] = {&#34;_fallback&#34;: resolved_fb}
else:
resolved_data[&#34;tags&#34;] = {&#34;_raw&#34;: tags_val, &#34;_fallback&#34;: resolved_fb}
n = node(unique_id, **resolved_data, config=self.config)
if sftp:
n.protocol = &#34;sftp&#34;
@@ -469,6 +758,20 @@ el.replaceWith(d);
</summary>
<pre><code class="python">def delete_node(self, unique_id, is_folder=False, save=True):
&#34;&#34;&#34;Logic for deleting a node or folder.&#34;&#34;&#34;
if _supports_transactional_update(self.config) and save:
def _mut(cfg):
uniques = cfg._explode_unique_unlocked(unique_id)
if not uniques:
label = &#34;Folder&#34; if is_folder else &#34;Node&#34;
raise NodeNotFoundError(f&#34;{label} &#39;{unique_id}&#39; not found or invalid.&#34;)
if is_folder:
cfg._folder_del_unlocked(**uniques)
else:
cfg._connections_del_unlocked(**uniques)
self.config.transactional_update(_mut)
return
if is_folder:
uniques = self.config._explode_unique(unique_id)
if not uniques:
@@ -479,7 +782,7 @@ el.replaceWith(d);
if not uniques:
raise NodeNotFoundError(f&#34;Node &#39;{unique_id}&#39; not found or invalid.&#34;)
self.config._connections_del(**uniques)
if save:
self.config._saveconfig(self.config.file)</code></pre>
</details>
@@ -509,6 +812,21 @@ el.replaceWith(d);
</summary>
<pre><code class="python">def full_replace(self, connections, profiles):
&#34;&#34;&#34;Replace all connections and profiles with new data.&#34;&#34;&#34;
from ..auth_utils import SanitizedAuthError
if _supports_transactional_update(self.config):
def _mut(cfg):
cfg.connections.clear()
cfg.connections.update(deepcopy(connections))
cfg.profiles.clear()
cfg.profiles.update(deepcopy(profiles))
try:
self.config.transactional_update(_mut)
except (ValueError, TypeError, SanitizedAuthError) as exc:
raise InvalidConfigurationError(str(exc)) from None
return
self.config.connections = connections
self.config.profiles = profiles
self.config._saveconfig(self.config.file)</code></pre>
@@ -562,7 +880,7 @@ el.replaceWith(d);
raise NodeNotFoundError(f&#34;Node &#39;{unique_id}&#39; not found.&#34;)
return details
except (KeyError, TypeError):
raise NodeNotFoundError(f&#34;Node &#39;{unique_id}&#39; not found.&#34;)</code></pre>
raise NodeNotFoundError(f&#34;Node &#39;{unique_id}&#39; not found.&#34;) from None</code></pre>
</details>
<div class="desc"><p>Return full configuration dictionary for a specific node.</p></div>
</dd>
@@ -664,27 +982,50 @@ el.replaceWith(d);
<pre><code class="python">def move_node(self, src_id, dst_id, copy=False):
&#34;&#34;&#34;Move or copy a node.&#34;&#34;&#34;
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&#34;Source node &#39;{src_id}&#39; not found.&#34;)
if dst_id in cfg._getallnodes_unlocked():
raise NodeAlreadyExistsError(f&#34;Destination node &#39;{dst_id}&#39; already exists.&#34;)
new_uniques = cfg._explode_unique_unlocked(dst_id)
if not new_uniques:
raise InvalidConfigurationError(f&#34;Invalid destination format &#39;{dst_id}&#39;.&#34;)
new_node_data = deepcopy(node_data)
new_node_data.update(new_uniques)
cfg._connections_add_unlocked(**new_node_data)
if not copy:
src_uniques = cfg._explode_unique_unlocked(src_id)
cfg._connections_del_unlocked(**src_uniques)
self.config.transactional_update(_mut)
return
node_data = self.config.getitem(src_id)
if not node_data:
raise NodeNotFoundError(f&#34;Source node &#39;{src_id}&#39; not found.&#34;)
if dst_id in self.config._getallnodes():
raise NodeAlreadyExistsError(f&#34;Destination node &#39;{dst_id}&#39; already exists.&#34;)
new_uniques = self.config._explode_unique(dst_id)
if not new_uniques:
raise InvalidConfigurationError(f&#34;Invalid destination format &#39;{dst_id}&#39;.&#34;)
new_node_data = node_data.copy()
new_node_data.update(new_uniques)
self.config._connections_add(**new_node_data)
if not copy:
src_uniques = self.config._explode_unique(src_id)
self.config._connections_del(**src_uniques)
self.config._saveconfig(self.config.file)</code></pre>
</details>
<div class="desc"><p>Move or copy a node.</p></div>
@@ -699,18 +1040,27 @@ el.replaceWith(d);
</summary>
<pre><code class="python">def update_node(self, unique_id, data, save=True):
&#34;&#34;&#34;Explicitly update an existing node.&#34;&#34;&#34;
if _supports_transactional_update(self.config) and save:
def _mut(cfg):
all_nodes = cfg._getallnodes_unlocked()
if unique_id not in all_nodes:
raise NodeNotFoundError(f&#34;Node &#39;{unique_id}&#39; not found.&#34;)
uniques = cfg._explode_unique_unlocked(unique_id)
existing = cfg.getitem_unlocked(unique_id, extract=False)
payload = self._build_update_payload(unique_id, data, existing, uniques)
cfg._connections_add_unlocked(**payload)
self.config.transactional_update(_mut)
return
all_nodes = self.config._getallnodes()
if unique_id not in all_nodes:
raise NodeNotFoundError(f&#34;Node &#39;{unique_id}&#39; not found.&#34;)
# Ensure &#39;id&#39; is in data for config._connections_add
if &#34;id&#34; not in data:
uniques = self.config._explode_unique(unique_id)
if uniques:
data[&#34;id&#34;] = uniques[&#34;id&#34;]
# config._connections_add actually handles updates if ID exists correctly
self.config._connections_add(**data)
uniques = self.config._explode_unique(unique_id)
existing = self.config.getitem(unique_id, extract=False)
payload = self._build_update_payload(unique_id, data, existing, uniques)
self.config._connections_add(**payload)
if save:
self.config._saveconfig(self.config.file)</code></pre>
</details>
+386 -127
View File
@@ -58,61 +58,194 @@ el.replaceWith(d);
<pre><code class="python">class ProfileService(BaseService):
&#34;&#34;&#34;Business logic for node profiles management.&#34;&#34;&#34;
_ALLOWED_KEYS: ClassVar[frozenset[str]] = frozenset({
&#34;host&#34;,
&#34;options&#34;,
&#34;logs&#34;,
&#34;password&#34;,
&#34;port&#34;,
&#34;protocol&#34;,
&#34;user&#34;,
&#34;tags&#34;,
&#34;jumphost&#34;,
&#34;fallback&#34;,
})
@staticmethod
def _validate_profile_payload(data):
from ..auth_utils import (
SanitizedAuthError,
is_compose_expression,
validate_auth_budget_tags,
validate_compose_syntax,
validate_fallback_entries,
)
try:
if &#34;tags&#34; in data and data[&#34;tags&#34;] not in (&#34;&#34;, None):
validate_auth_budget_tags(data[&#34;tags&#34;])
if &#34;fallback&#34; in data and data[&#34;fallback&#34;] not in (None, &#34;&#34;, []):
validate_fallback_entries(data[&#34;fallback&#34;])
pw = data.get(&#34;password&#34;)
if is_compose_expression(pw):
validate_compose_syntax(pw)
elif isinstance(pw, (list, tuple)):
for item in pw:
if is_compose_expression(item):
validate_compose_syntax(item)
except (ValueError, TypeError, SanitizedAuthError) as exc:
raise InvalidConfigurationError(str(exc)) from None
def _get_profiles_snapshot(self):
lock = getattr(self.config, &#34;_mutation_lock&#34;, None)
if lock is not None and not hasattr(self.config, &#34;assert_called&#34;):
with lock:
return deepcopy(self.config.profiles)
return deepcopy(self.config.profiles)
def list_profiles(self, filter_str=None):
&#34;&#34;&#34;List all profile names, optionally filtered.&#34;&#34;&#34;
profiles = list(self.config.profiles.keys())
profiles = list(self._get_profiles_snapshot().keys())
case_sensitive = self.config.config.get(&#34;case&#34;, False)
if filter_str:
if not case_sensitive:
f_str = filter_str.lower()
return [p for p in profiles if f_str in p.lower()]
else:
return [p for p in profiles if filter_str in p]
return [p for p in profiles if filter_str in p]
return profiles
def get_profile(self, name, resolve=True):
def get_profile(self, name, resolve=True, *, _visited=frozenset(), _depth=0):
&#34;&#34;&#34;Get the profile dictionary, optionally resolved.&#34;&#34;&#34;
profile = self.config.profiles.get(name)
if _depth &gt;= MAX_PROFILE_DEPTH or name in _visited:
raise InvalidConfigurationError(
f&#34;Circular or overly deep profile reference involving &#39;{name}&#39;.&#34;
)
profiles = self._get_profiles_snapshot()
profile = profiles.get(name)
if not profile:
raise ProfileNotFoundError(f&#34;Profile &#39;{name}&#39; not found.&#34;)
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):
&#34;&#34;&#34;Add a new profile.&#34;&#34;&#34;
filtered_data = {k: deepcopy(v) for k, v in data.items() if k in self._ALLOWED_KEYS}
self._validate_profile_payload(filtered_data)
if _supports_transactional_update(self.config):
def _mut(cfg):
if name in cfg.profiles:
raise ProfileAlreadyExistsError(f&#34;Profile &#39;{name}&#39; already exists.&#34;)
cfg._profiles_add_unlocked(id=name, **filtered_data)
self.config.transactional_update(_mut)
return
if name in self.config.profiles:
raise ProfileAlreadyExistsError(f&#34;Profile &#39;{name}&#39; already exists.&#34;)
# Filter data to match _profiles_add signature and ensure id is passed
allowed_keys = {&#34;host&#34;, &#34;options&#34;, &#34;logs&#34;, &#34;password&#34;, &#34;port&#34;, &#34;protocol&#34;, &#34;user&#34;, &#34;tags&#34;, &#34;jumphost&#34;}
filtered_data = {k: v for k, v in data.items() if k in allowed_keys}
self.config._profiles_add(id=name, **filtered_data)
self.config._saveconfig(self.config.file)
def resolve_node_data(self, node_data):
&#34;&#34;&#34;Resolve profile references (@profile) in node data and handle inheritance.&#34;&#34;&#34;
resolved = node_data.copy()
# 1. Identify all referenced profiles to support inheritance
referenced_profiles = []
for value in resolved.values():
if isinstance(value, str) and value.startswith(&#34;@&#34;):
referenced_profiles.append(value[1:])
elif isinstance(value, list):
for item in value:
if isinstance(item, str) and item.startswith(&#34;@&#34;):
referenced_profiles.append(item[1:])
# 2. Resolve explicit references
def resolve_node_data(
self,
node_data,
*,
_visited=frozenset(),
_depth=0,
_profiles_snapshot=None,
):
&#34;&#34;&#34;Resolve profile references (@profile) in node data while preserving compose and scalar secrets.&#34;&#34;&#34;
from ..auth_utils import is_compose_expression
if _depth &gt; MAX_PROFILE_DEPTH:
raise InvalidConfigurationError(&#34;Profile inheritance exceeds maximum depth.&#34;)
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 == &#34;fallback&#34;:
continue
target_refs = (
auth_referenced_profiles if key in (&#34;user&#34;, &#34;password&#34;) else other_referenced_profiles
)
if isinstance(value, str):
if not is_compose_expression(value) and value.startswith(&#34;@&#34;):
ref = value[1:]
if key != &#34;password&#34; or ref in profiles:
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(&#34;@&#34;)
):
ref = item[1:]
if key != &#34;password&#34; or ref in profiles:
target_refs.append(ref)
def _get_resolved_ref(prof_name):
if prof_name in _visited or _depth &gt;= MAX_PROFILE_DEPTH:
raise InvalidConfigurationError(
f&#34;Circular or overly deep profile reference involving &#39;{prof_name}&#39;.&#34;
)
prof_raw = profiles.get(prof_name)
if not prof_raw:
raise ProfileNotFoundError(f&#34;Profile &#39;{prof_name}&#39; not found.&#34;)
return self.resolve_node_data(
prof_raw,
_visited=_visited | {prof_name},
_depth=_depth + 1,
_profiles_snapshot=profiles,
)
for key, value in list(resolved.items()):
if key == &#34;fallback&#34;:
continue
if key == &#34;password&#34;:
if isinstance(value, str):
if is_compose_expression(value):
continue
if value.startswith(&#34;@&#34;) and value[1:] in profiles:
prof_obj = _get_resolved_ref(value[1:])
resolved[&#34;password&#34;] = prof_obj.get(&#34;password&#34;, &#34;&#34;)
elif isinstance(value, list):
resolved_list = []
for item in value:
if (
isinstance(item, str)
and not is_compose_expression(item)
and item.startswith(&#34;@&#34;)
and item[1:] in profiles
):
with suppress(ProfileNotFoundError):
prof_obj = _get_resolved_ref(item[1:])
if &#34;password&#34; in prof_obj:
resolved_list.append(prof_obj[&#34;password&#34;])
else:
resolved_list.append(item)
resolved[&#34;password&#34;] = resolved_list
continue
if isinstance(value, str) and value.startswith(&#34;@&#34;):
profile_name = value[1:]
try:
profile = self.get_profile(profile_name, resolve=True)
profile = _get_resolved_ref(profile_name)
resolved[key] = profile.get(key, &#34;&#34;)
except ProfileNotFoundError:
resolved[key] = &#34;&#34;
@@ -121,68 +254,87 @@ el.replaceWith(d);
for item in value:
if isinstance(item, str) and item.startswith(&#34;@&#34;):
profile_name = item[1:]
try:
profile = self.get_profile(profile_name, resolve=True)
with suppress(ProfileNotFoundError):
profile = _get_resolved_ref(profile_name)
if &#34;password&#34; in profile:
resolved_list.append(profile[&#34;password&#34;])
except ProfileNotFoundError:
pass
else:
resolved_list.append(item)
resolved[key] = resolved_list
# 3. Inheritance: Fill empty keys from the first referenced profile
if referenced_profiles:
base_profile_name = referenced_profiles[0]
try:
base_profile = self.get_profile(base_profile_name, resolve=True)
for key, value in base_profile.items():
# Fill if key is missing or empty
if key not in resolved or resolved[key] == &#34;&#34; or resolved[key] == [] or resolved[key] is None:
resolved[key] = value
except ProfileNotFoundError:
pass
# 4. Handle default protocol
if resolved.get(&#34;protocol&#34;) == &#34;&#34; or resolved.get(&#34;protocol&#34;) is None:
ordered_profiles = list(dict.fromkeys(auth_referenced_profiles + other_referenced_profiles))
for base_profile_name in ordered_profiles:
with suppress(ProfileNotFoundError):
base_profile = _get_resolved_ref(base_profile_name)
for key, value in base_profile.items():
if (key not in resolved or resolved[key] in (&#34;&#34;, [], None)) and (
value not in (&#34;&#34;, [], None) or key not in resolved
):
resolved[key] = deepcopy(value)
if resolved.get(&#34;protocol&#34;) in (&#34;&#34;, None):
try:
default_profile = self.get_profile(&#34;default&#34;, resolve=True)
resolved[&#34;protocol&#34;] = default_profile.get(&#34;protocol&#34;, &#34;ssh&#34;)
default_profile = _get_resolved_ref(&#34;default&#34;)
resolved[&#34;protocol&#34;] = default_profile.get(&#34;protocol&#34;, &#34;ssh&#34;) or &#34;ssh&#34;
except ProfileNotFoundError:
resolved[&#34;protocol&#34;] = &#34;ssh&#34;
return resolved
def delete_profile(self, name):
&#34;&#34;&#34;Delete an existing profile, with safety checks.&#34;&#34;&#34;
if _supports_transactional_update(self.config):
def _mut(cfg):
if name not in cfg.profiles:
raise ProfileNotFoundError(f&#34;Profile &#39;{name}&#39; not found.&#34;)
if name == &#34;default&#34;:
raise InvalidConfigurationError(&#34;Cannot delete the &#39;default&#39; profile.&#34;)
used_by = cfg._profileused_unlocked(name)
if used_by:
raise InvalidConfigurationError(
f&#34;Profile &#39;{name}&#39; is used by nodes: {&#39;, &#39;.join(used_by)}&#34;
)
cfg._profiles_del_unlocked(id=name)
self.config.transactional_update(_mut)
return
if name not in self.config.profiles:
raise ProfileNotFoundError(f&#34;Profile &#39;{name}&#39; not found.&#34;)
if name == &#34;default&#34;:
raise InvalidConfigurationError(&#34;Cannot delete the &#39;default&#39; profile.&#34;)
used_by = self.config._profileused(name)
if used_by:
# We return the list of nodes using it so the UI can inform the user
raise InvalidConfigurationError(f&#34;Profile &#39;{name}&#39; is used by nodes: {&#39;, &#39;.join(used_by)}&#34;)
raise InvalidConfigurationError(
f&#34;Profile &#39;{name}&#39; is used by nodes: {&#39;, &#39;.join(used_by)}&#34;
)
self.config._profiles_del(id=name)
self.config._saveconfig(self.config.file)
def update_profile(self, name, data):
&#34;&#34;&#34;Update an existing profile.&#34;&#34;&#34;
if _supports_transactional_update(self.config):
def _mut(cfg):
if name not in cfg.profiles:
raise ProfileNotFoundError(f&#34;Profile &#39;{name}&#39; not found.&#34;)
updated_data = deepcopy(cfg.profiles[name])
updated_data.update(deepcopy(data))
filtered_data = {
k: v for k, v in updated_data.items() if k in self._ALLOWED_KEYS
}
self._validate_profile_payload(filtered_data)
cfg._profiles_add_unlocked(id=name, **filtered_data)
self.config.transactional_update(_mut)
return
if name not in self.config.profiles:
raise ProfileNotFoundError(f&#34;Profile &#39;{name}&#39; not found.&#34;)
# 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 = {&#34;host&#34;, &#34;options&#34;, &#34;logs&#34;, &#34;password&#34;, &#34;port&#34;, &#34;protocol&#34;, &#34;user&#34;, &#34;tags&#34;, &#34;jumphost&#34;}
filtered_data = {k: v for k, v in updated_data.items() if k in allowed_keys}
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):
&#34;&#34;&#34;Add a new profile.&#34;&#34;&#34;
filtered_data = {k: deepcopy(v) for k, v in data.items() if k in self._ALLOWED_KEYS}
self._validate_profile_payload(filtered_data)
if _supports_transactional_update(self.config):
def _mut(cfg):
if name in cfg.profiles:
raise ProfileAlreadyExistsError(f&#34;Profile &#39;{name}&#39; already exists.&#34;)
cfg._profiles_add_unlocked(id=name, **filtered_data)
self.config.transactional_update(_mut)
return
if name in self.config.profiles:
raise ProfileAlreadyExistsError(f&#34;Profile &#39;{name}&#39; already exists.&#34;)
# Filter data to match _profiles_add signature and ensure id is passed
allowed_keys = {&#34;host&#34;, &#34;options&#34;, &#34;logs&#34;, &#34;password&#34;, &#34;port&#34;, &#34;protocol&#34;, &#34;user&#34;, &#34;tags&#34;, &#34;jumphost&#34;}
filtered_data = {k: v for k, v in data.items() if k in allowed_keys}
self.config._profiles_add(id=name, **filtered_data)
self.config._saveconfig(self.config.file)</code></pre>
</details>
@@ -232,17 +391,31 @@ el.replaceWith(d);
</summary>
<pre><code class="python">def delete_profile(self, name):
&#34;&#34;&#34;Delete an existing profile, with safety checks.&#34;&#34;&#34;
if _supports_transactional_update(self.config):
def _mut(cfg):
if name not in cfg.profiles:
raise ProfileNotFoundError(f&#34;Profile &#39;{name}&#39; not found.&#34;)
if name == &#34;default&#34;:
raise InvalidConfigurationError(&#34;Cannot delete the &#39;default&#39; profile.&#34;)
used_by = cfg._profileused_unlocked(name)
if used_by:
raise InvalidConfigurationError(
f&#34;Profile &#39;{name}&#39; is used by nodes: {&#39;, &#39;.join(used_by)}&#34;
)
cfg._profiles_del_unlocked(id=name)
self.config.transactional_update(_mut)
return
if name not in self.config.profiles:
raise ProfileNotFoundError(f&#34;Profile &#39;{name}&#39; not found.&#34;)
if name == &#34;default&#34;:
raise InvalidConfigurationError(&#34;Cannot delete the &#39;default&#39; profile.&#34;)
used_by = self.config._profileused(name)
if used_by:
# We return the list of nodes using it so the UI can inform the user
raise InvalidConfigurationError(f&#34;Profile &#39;{name}&#39; is used by nodes: {&#39;, &#39;.join(used_by)}&#34;)
raise InvalidConfigurationError(
f&#34;Profile &#39;{name}&#39; is used by nodes: {&#39;, &#39;.join(used_by)}&#34;
)
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):
&#34;&#34;&#34;Get the profile dictionary, optionally resolved.&#34;&#34;&#34;
profile = self.config.profiles.get(name)
if _depth &gt;= MAX_PROFILE_DEPTH or name in _visited:
raise InvalidConfigurationError(
f&#34;Circular or overly deep profile reference involving &#39;{name}&#39;.&#34;
)
profiles = self._get_profiles_snapshot()
profile = profiles.get(name)
if not profile:
raise ProfileNotFoundError(f&#34;Profile &#39;{name}&#39; not found.&#34;)
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,15 +461,14 @@ el.replaceWith(d);
</summary>
<pre><code class="python">def list_profiles(self, filter_str=None):
&#34;&#34;&#34;List all profile names, optionally filtered.&#34;&#34;&#34;
profiles = list(self.config.profiles.keys())
profiles = list(self._get_profiles_snapshot().keys())
case_sensitive = self.config.config.get(&#34;case&#34;, False)
if filter_str:
if not case_sensitive:
f_str = filter_str.lower()
return [p for p in profiles if f_str in p.lower()]
else:
return [p for p in profiles if filter_str in p]
return [p for p in profiles if filter_str in p]
return profiles</code></pre>
</details>
<div class="desc"><p>List all profile names, optionally filtered.</p></div>
@@ -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):
&#34;&#34;&#34;Resolve profile references (@profile) in node data and handle inheritance.&#34;&#34;&#34;
resolved = node_data.copy()
# 1. Identify all referenced profiles to support inheritance
referenced_profiles = []
for value in resolved.values():
if isinstance(value, str) and value.startswith(&#34;@&#34;):
referenced_profiles.append(value[1:])
elif isinstance(value, list):
for item in value:
if isinstance(item, str) and item.startswith(&#34;@&#34;):
referenced_profiles.append(item[1:])
# 2. Resolve explicit references
<pre><code class="python">def resolve_node_data(
self,
node_data,
*,
_visited=frozenset(),
_depth=0,
_profiles_snapshot=None,
):
&#34;&#34;&#34;Resolve profile references (@profile) in node data while preserving compose and scalar secrets.&#34;&#34;&#34;
from ..auth_utils import is_compose_expression
if _depth &gt; MAX_PROFILE_DEPTH:
raise InvalidConfigurationError(&#34;Profile inheritance exceeds maximum depth.&#34;)
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 == &#34;fallback&#34;:
continue
target_refs = (
auth_referenced_profiles if key in (&#34;user&#34;, &#34;password&#34;) else other_referenced_profiles
)
if isinstance(value, str):
if not is_compose_expression(value) and value.startswith(&#34;@&#34;):
ref = value[1:]
if key != &#34;password&#34; or ref in profiles:
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(&#34;@&#34;)
):
ref = item[1:]
if key != &#34;password&#34; or ref in profiles:
target_refs.append(ref)
def _get_resolved_ref(prof_name):
if prof_name in _visited or _depth &gt;= MAX_PROFILE_DEPTH:
raise InvalidConfigurationError(
f&#34;Circular or overly deep profile reference involving &#39;{prof_name}&#39;.&#34;
)
prof_raw = profiles.get(prof_name)
if not prof_raw:
raise ProfileNotFoundError(f&#34;Profile &#39;{prof_name}&#39; not found.&#34;)
return self.resolve_node_data(
prof_raw,
_visited=_visited | {prof_name},
_depth=_depth + 1,
_profiles_snapshot=profiles,
)
for key, value in list(resolved.items()):
if key == &#34;fallback&#34;:
continue
if key == &#34;password&#34;:
if isinstance(value, str):
if is_compose_expression(value):
continue
if value.startswith(&#34;@&#34;) and value[1:] in profiles:
prof_obj = _get_resolved_ref(value[1:])
resolved[&#34;password&#34;] = prof_obj.get(&#34;password&#34;, &#34;&#34;)
elif isinstance(value, list):
resolved_list = []
for item in value:
if (
isinstance(item, str)
and not is_compose_expression(item)
and item.startswith(&#34;@&#34;)
and item[1:] in profiles
):
with suppress(ProfileNotFoundError):
prof_obj = _get_resolved_ref(item[1:])
if &#34;password&#34; in prof_obj:
resolved_list.append(prof_obj[&#34;password&#34;])
else:
resolved_list.append(item)
resolved[&#34;password&#34;] = resolved_list
continue
if isinstance(value, str) and value.startswith(&#34;@&#34;):
profile_name = value[1:]
try:
profile = self.get_profile(profile_name, resolve=True)
profile = _get_resolved_ref(profile_name)
resolved[key] = profile.get(key, &#34;&#34;)
except ProfileNotFoundError:
resolved[key] = &#34;&#34;
@@ -327,39 +581,34 @@ el.replaceWith(d);
for item in value:
if isinstance(item, str) and item.startswith(&#34;@&#34;):
profile_name = item[1:]
try:
profile = self.get_profile(profile_name, resolve=True)
with suppress(ProfileNotFoundError):
profile = _get_resolved_ref(profile_name)
if &#34;password&#34; in profile:
resolved_list.append(profile[&#34;password&#34;])
except ProfileNotFoundError:
pass
else:
resolved_list.append(item)
resolved[key] = resolved_list
# 3. Inheritance: Fill empty keys from the first referenced profile
if referenced_profiles:
base_profile_name = referenced_profiles[0]
try:
base_profile = self.get_profile(base_profile_name, resolve=True)
for key, value in base_profile.items():
# Fill if key is missing or empty
if key not in resolved or resolved[key] == &#34;&#34; or resolved[key] == [] or resolved[key] is None:
resolved[key] = value
except ProfileNotFoundError:
pass
# 4. Handle default protocol
if resolved.get(&#34;protocol&#34;) == &#34;&#34; or resolved.get(&#34;protocol&#34;) is None:
ordered_profiles = list(dict.fromkeys(auth_referenced_profiles + other_referenced_profiles))
for base_profile_name in ordered_profiles:
with suppress(ProfileNotFoundError):
base_profile = _get_resolved_ref(base_profile_name)
for key, value in base_profile.items():
if (key not in resolved or resolved[key] in (&#34;&#34;, [], None)) and (
value not in (&#34;&#34;, [], None) or key not in resolved
):
resolved[key] = deepcopy(value)
if resolved.get(&#34;protocol&#34;) in (&#34;&#34;, None):
try:
default_profile = self.get_profile(&#34;default&#34;, resolve=True)
resolved[&#34;protocol&#34;] = default_profile.get(&#34;protocol&#34;, &#34;ssh&#34;)
default_profile = _get_resolved_ref(&#34;default&#34;)
resolved[&#34;protocol&#34;] = default_profile.get(&#34;protocol&#34;, &#34;ssh&#34;) or &#34;ssh&#34;
except ProfileNotFoundError:
resolved[&#34;protocol&#34;] = &#34;ssh&#34;
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):
&#34;&#34;&#34;Update an existing profile.&#34;&#34;&#34;
if _supports_transactional_update(self.config):
def _mut(cfg):
if name not in cfg.profiles:
raise ProfileNotFoundError(f&#34;Profile &#39;{name}&#39; not found.&#34;)
updated_data = deepcopy(cfg.profiles[name])
updated_data.update(deepcopy(data))
filtered_data = {
k: v for k, v in updated_data.items() if k in self._ALLOWED_KEYS
}
self._validate_profile_payload(filtered_data)
cfg._profiles_add_unlocked(id=name, **filtered_data)
self.config.transactional_update(_mut)
return
if name not in self.config.profiles:
raise ProfileNotFoundError(f&#34;Profile &#39;{name}&#39; not found.&#34;)
# 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 = {&#34;host&#34;, &#34;options&#34;, &#34;logs&#34;, &#34;password&#34;, &#34;port&#34;, &#34;protocol&#34;, &#34;user&#34;, &#34;tags&#34;, &#34;jumphost&#34;}
filtered_data = {k: v for k, v in updated_data.items() if k in allowed_keys}
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>
+35 -19
View File
@@ -74,11 +74,14 @@ el.replaceWith(d);
</summary>
<pre><code class="python">class ServiceProvider:
&#34;&#34;&#34;Dynamic service backend. Transparently provides local or remote services.&#34;&#34;&#34;
def __init__(self, config, mode=&#34;local&#34;, remote_host=None):
from ..auth_utils import AuthAttemptTracker
self.mode = mode
self.config = config
self.remote_host = remote_host
self.auth_tracker = AuthAttemptTracker()
self._system = None
self._execution = None
self._import_export = None
@@ -94,47 +97,54 @@ el.replaceWith(d);
raise ValueError(f&#34;Unknown service mode: {mode}&#34;)
def _init_local(self):
from .node_service import NodeService
from .profile_service import ProfileService
from .config_service import ConfigService
from .plugin_service import PluginService
from .context_service import ContextService
from .node_service import NodeService
from .plugin_service import PluginService
from .profile_service import ProfileService
self.nodes = NodeService(self.config)
self.profiles = ProfileService(self.config)
self.config_svc = ConfigService(self.config)
self.plugins = PluginService(self.config)
self.context = ContextService(self.config)
def _init_remote(self):
# Allow ConfigService to work locally so the user can revert the mode
from .config_service import ConfigService
from .context_service import ContextService
self.config_svc = ConfigService(self.config)
self.context = ContextService(self.config)
if not self.remote_host:
raise InvalidConfigurationError(&#34;Remote host must be specified in remote mode&#34;)
import grpc
import os
import grpc
from ..grpc_layer.stubs import (
NodeStub, ProfileStub, PluginStub, AIStub,
ExecutionStub, ImportExportStub, SystemStub,
ConfigStub, AuthClientInterceptor, AuthStub
AIStub,
AuthClientInterceptor,
AuthStub,
ConfigStub,
ExecutionStub,
ImportExportStub,
NodeStub,
PluginStub,
ProfileStub,
SystemStub,
)
def get_token():
env_token = os.environ.get(&#34;CONNPY_TOKEN&#34;)
if env_token:
return env_token
token_path = os.path.join(self.config.defaultdir, &#34;.token&#34;)
if os.path.exists(token_path):
try:
with open(token_path, &#34;r&#34;) as f:
return f.read().strip()
except Exception:
pass
with suppress(OSError, UnicodeDecodeError), open(token_path) as f:
return f.read().strip()
return None
channel = grpc.insecure_channel(self.remote_host)
@@ -170,7 +180,10 @@ el.replaceWith(d);
def execution(self):
if self._execution is None and self.mode == &#34;local&#34;:
from .execution_service import ExecutionService
self._execution = ExecutionService(self.config)
if &#34;attempt_tracker&#34; in inspect.signature(ExecutionService.__init__).parameters:
self._execution = ExecutionService(self.config, attempt_tracker=self.auth_tracker)
else:
self._execution = ExecutionService(self.config)
return self._execution
@execution.setter
@@ -249,7 +262,10 @@ def ai(self):
def execution(self):
if self._execution is None and self.mode == &#34;local&#34;:
from .execution_service import ExecutionService
self._execution = ExecutionService(self.config)
if &#34;attempt_tracker&#34; 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>
<div class="desc"></div>