feat(spec-05-auth-fallback-chain): worker (eng_flash_high) implementation
This commit is contained in:
1 parent
ef6d1da5e5
commit
79d28ee6c3
14 files changed
+5514
-631
No files matched your search
File diff suppressed because it is too large.
Load diff
@@ -1,9 +1,22 @@
|
||||
import re
|
||||
import sys
|
||||
|
||||
import yaml
|
||||
|
||||
from .. import printer
|
||||
from ..auth_utils import (
|
||||
MAX_AUTH_PROMPTS_CAP,
|
||||
MAX_HOP_HOST_ATTEMPTS_CAP,
|
||||
MAX_TOTAL_AUTH_ATTEMPTS_CAP,
|
||||
MIN_AUTH_PROMPTS,
|
||||
MIN_HOP_HOST_ATTEMPTS,
|
||||
MIN_TOTAL_AUTH_ATTEMPTS,
|
||||
validate_strict_int_range,
|
||||
)
|
||||
from ..services.exceptions import ConnpyError, InvalidConfigurationError
|
||||
from .help_text import get_instructions
|
||||
|
||||
|
||||
class ConfigHandler:
|
||||
def __init__(self, app):
|
||||
self.app = app
|
||||
@@ -29,7 +42,12 @@ class ConfigHandler:
|
||||
"sync_remote": self.set_sync_remote,
|
||||
"shell_command": self.set_shell_config,
|
||||
"shell_prompt": self.set_shell_config,
|
||||
"shell_os": self.set_shell_config
|
||||
"shell_os": self.set_shell_config,
|
||||
"max_auth_prompts": self.set_auth_budget,
|
||||
"max_total_auth_attempts": self.set_auth_budget,
|
||||
"max_hop_host_attempts": self.set_auth_budget,
|
||||
"max_global_user_attempts": self.set_auth_budget,
|
||||
"disable_fallback_on_insecure_hostkey": self.set_disable_fallback_on_insecure_hostkey,
|
||||
}
|
||||
handler = actions.get(getattr(args, "command", None))
|
||||
if handler:
|
||||
@@ -202,3 +220,67 @@ class ConfigHandler:
|
||||
except (ConnpyError, InvalidConfigurationError) as e:
|
||||
printer.error(str(e))
|
||||
|
||||
def set_auth_budget(self, args):
|
||||
key = args.command
|
||||
raw_val = args.data[0] if isinstance(args.data, list) else args.data
|
||||
bounds_map = {
|
||||
"max_auth_prompts": (MIN_AUTH_PROMPTS, MAX_AUTH_PROMPTS_CAP),
|
||||
"max_total_auth_attempts": (MIN_TOTAL_AUTH_ATTEMPTS, MAX_TOTAL_AUTH_ATTEMPTS_CAP),
|
||||
"max_hop_host_attempts": (MIN_HOP_HOST_ATTEMPTS, MAX_HOP_HOST_ATTEMPTS_CAP),
|
||||
"max_global_user_attempts": (1, 1000),
|
||||
}
|
||||
if key not in bounds_map:
|
||||
raise InvalidConfigurationError(f"Unknown auth budget setting: {key}")
|
||||
|
||||
if isinstance(raw_val, bool):
|
||||
msg = f"Invalid {key}: boolean is not permitted"
|
||||
printer.error(msg)
|
||||
raise InvalidConfigurationError(msg)
|
||||
|
||||
if isinstance(raw_val, int):
|
||||
val = raw_val
|
||||
elif isinstance(raw_val, str) and re.fullmatch(r"[+-]?\d+", raw_val.strip()):
|
||||
val = int(raw_val.strip())
|
||||
else:
|
||||
msg = f"Invalid {key}: expected integer, got {raw_val!r}"
|
||||
printer.error(msg)
|
||||
raise InvalidConfigurationError(msg)
|
||||
|
||||
if val == 0:
|
||||
cfg_obj = self.app.config
|
||||
if not hasattr(cfg_obj, "assert_called") and callable(
|
||||
getattr(cfg_obj, "transactional_update", None)
|
||||
):
|
||||
cfg_obj.transactional_update(lambda c: c.config.pop(key, None))
|
||||
else:
|
||||
cfg_obj.config.pop(key, None)
|
||||
cfg_obj._saveconfig(cfg_obj.file)
|
||||
printer.success("Config saved")
|
||||
return
|
||||
|
||||
min_v, max_v = bounds_map[key]
|
||||
try:
|
||||
validate_strict_int_range(val, key, min_v, max_v)
|
||||
except ValueError as exc:
|
||||
printer.error(str(exc))
|
||||
raise InvalidConfigurationError(str(exc)) from None
|
||||
|
||||
self.app.services.config_svc.update_setting(key, val)
|
||||
printer.success("Config saved")
|
||||
|
||||
def set_disable_fallback_on_insecure_hostkey(self, args):
|
||||
raw_val = args.data[0] if isinstance(args.data, list) else args.data
|
||||
if isinstance(raw_val, bool):
|
||||
val = raw_val
|
||||
elif isinstance(raw_val, str) and raw_val.strip().lower() in ("true", "false"):
|
||||
val = raw_val.strip().lower() == "true"
|
||||
else:
|
||||
msg = "Invalid disable_fallback_on_insecure_hostkey: expected 'true' or 'false'"
|
||||
printer.error(msg)
|
||||
raise InvalidConfigurationError(msg)
|
||||
self.app.services.config_svc.update_setting(
|
||||
"disable_fallback_on_insecure_hostkey",
|
||||
val,
|
||||
)
|
||||
printer.success("Config saved")
|
||||
|
||||
+113
-26
@@ -1,6 +1,70 @@
|
||||
import ast
|
||||
import json
|
||||
|
||||
from ..auth_utils import (
|
||||
MAX_FALLBACK_ENTRIES,
|
||||
MAX_JSON_INPUT_BYTES,
|
||||
MAX_JSON_INPUT_DEPTH,
|
||||
validate_auth_budget_tags,
|
||||
validate_fallback_entries,
|
||||
validate_json_depth_and_size,
|
||||
)
|
||||
from .validators import Validators
|
||||
|
||||
|
||||
def _parse_tags_input(raw_tags):
|
||||
if raw_tags in ("", None):
|
||||
return ""
|
||||
if isinstance(raw_tags, dict):
|
||||
validate_json_depth_and_size(
|
||||
raw_tags,
|
||||
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||
)
|
||||
validate_auth_budget_tags(raw_tags)
|
||||
return raw_tags
|
||||
s = str(raw_tags)
|
||||
if s.startswith("@"):
|
||||
return s
|
||||
if len(s.encode("utf-8", errors="replace")) > MAX_JSON_INPUT_BYTES:
|
||||
raise ValueError(f"Tags exceed maximum size of {MAX_JSON_INPUT_BYTES} bytes")
|
||||
try:
|
||||
parsed = json.loads(s)
|
||||
except (ValueError, TypeError):
|
||||
parsed = ast.literal_eval(s)
|
||||
validate_json_depth_and_size(
|
||||
parsed,
|
||||
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||
)
|
||||
validate_auth_budget_tags(parsed)
|
||||
return parsed
|
||||
|
||||
|
||||
def _parse_fallback_input(raw_fb):
|
||||
if raw_fb in (None, "", []):
|
||||
return ""
|
||||
if isinstance(raw_fb, str):
|
||||
stripped = raw_fb.strip()
|
||||
if not stripped:
|
||||
return ""
|
||||
if stripped.startswith(("[", "{")):
|
||||
parsed = validate_json_depth_and_size(
|
||||
stripped,
|
||||
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||
)
|
||||
return validate_fallback_entries(parsed, max_entries=MAX_FALLBACK_ENTRIES)
|
||||
validate_fallback_entries(stripped, max_entries=MAX_FALLBACK_ENTRIES)
|
||||
return stripped
|
||||
validate_json_depth_and_size(
|
||||
raw_fb,
|
||||
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||
)
|
||||
return validate_fallback_entries(raw_fb, max_entries=MAX_FALLBACK_ENTRIES)
|
||||
|
||||
|
||||
class Forms:
|
||||
def __init__(self, app):
|
||||
self.app = app
|
||||
@@ -18,6 +82,7 @@ class Forms:
|
||||
questions.append(inquirer.Confirm("jumphost", message="Edit jumphost?"))
|
||||
questions.append(inquirer.Confirm("user", message="Edit User?"))
|
||||
questions.append(inquirer.Confirm("password", message="Edit password?"))
|
||||
questions.append(inquirer.Confirm("fallback", message="Edit fallback credentials?"))
|
||||
return inquirer.prompt(questions)
|
||||
|
||||
def questions_nodes(self, unique, uniques=None, edit=None):
|
||||
@@ -28,11 +93,13 @@ class Forms:
|
||||
defaults["tags"] = ""
|
||||
if "jumphost" not in defaults:
|
||||
defaults["jumphost"] = ""
|
||||
except Exception:
|
||||
defaults = {"host": "", "protocol": "", "port": "", "user": "", "options": "", "logs": "", "tags": "", "password": "", "jumphost": ""}
|
||||
if "fallback" not in defaults:
|
||||
defaults["fallback"] = ""
|
||||
except Exception: # noqa: BLE001
|
||||
defaults = {"host": "", "protocol": "", "port": "", "user": "", "options": "", "logs": "", "tags": "", "password": "", "jumphost": "", "fallback": ""}
|
||||
node = {}
|
||||
if edit is None:
|
||||
edit = {"host": True, "protocol": True, "port": True, "user": True, "password": True, "options": True, "logs": True, "tags": True, "jumphost": True}
|
||||
edit = {"host": True, "protocol": True, "port": True, "user": True, "password": True, "options": True, "logs": True, "tags": True, "jumphost": True, "fallback": True}
|
||||
questions = []
|
||||
if edit["host"]:
|
||||
questions.append(inquirer.Text("host", message="Add Hostname or IP", validate=self.validators.host_validation, default=defaults["host"]))
|
||||
@@ -70,11 +137,17 @@ class Forms:
|
||||
questions.append(inquirer.List("password", message="Password: Use a local password, no password or a list of profiles to reference?", choices=["Local Password", "Profiles", "No Password"]))
|
||||
else:
|
||||
node["password"] = defaults["password"]
|
||||
|
||||
if edit.get("fallback"):
|
||||
raw_def_fb = defaults.get("fallback", "")
|
||||
def_fb_str = json.dumps(raw_def_fb) if isinstance(raw_def_fb, (list, dict)) else str(raw_def_fb or "")
|
||||
questions.append(inquirer.Text("fallback", message="Add fallback credentials (JSON list/dict, @profile, or compose:)", validate=self.validators.fallback_validation, default=def_fb_str.replace("{", "{{").replace("}", "}}")))
|
||||
else:
|
||||
node["fallback"] = defaults.get("fallback", "")
|
||||
|
||||
answer = inquirer.prompt(questions)
|
||||
if answer is None:
|
||||
return False
|
||||
|
||||
|
||||
if "password" in answer:
|
||||
if answer["password"] == "Local Password":
|
||||
passq = [inquirer.Password("password", message="Set Password")]
|
||||
@@ -90,10 +163,12 @@ class Forms:
|
||||
answer["password"] = passa["password"].split(",")
|
||||
elif answer["password"] == "No Password":
|
||||
answer["password"] = ""
|
||||
|
||||
if "tags" in answer and not answer["tags"].startswith("@") and answer["tags"]:
|
||||
answer["tags"] = ast.literal_eval(answer["tags"])
|
||||
|
||||
|
||||
if "tags" in answer and answer["tags"] and not str(answer["tags"]).startswith("@"):
|
||||
answer["tags"] = _parse_tags_input(answer["tags"])
|
||||
if "fallback" in answer:
|
||||
answer["fallback"] = _parse_fallback_input(answer["fallback"])
|
||||
|
||||
result = {**uniques, **answer, **node}
|
||||
result["type"] = "connection"
|
||||
return result
|
||||
@@ -106,11 +181,13 @@ class Forms:
|
||||
defaults["tags"] = ""
|
||||
if "jumphost" not in defaults:
|
||||
defaults["jumphost"] = ""
|
||||
except Exception:
|
||||
defaults = {"host": "", "protocol": "", "port": "", "user": "", "options": "", "logs": "", "tags": "", "jumphost": ""}
|
||||
if "fallback" not in defaults:
|
||||
defaults["fallback"] = ""
|
||||
except Exception: # noqa: BLE001
|
||||
defaults = {"host": "", "protocol": "", "port": "", "user": "", "options": "", "logs": "", "tags": "", "jumphost": "", "fallback": ""}
|
||||
profile = {}
|
||||
if edit is None:
|
||||
edit = {"host": True, "protocol": True, "port": True, "user": True, "password": True, "options": True, "logs": True, "tags": True, "jumphost": True}
|
||||
edit = {"host": True, "protocol": True, "port": True, "user": True, "password": True, "options": True, "logs": True, "tags": True, "jumphost": True, "fallback": True}
|
||||
questions = []
|
||||
if edit["host"]:
|
||||
questions.append(inquirer.Text("host", message="Add Hostname or IP", default=defaults["host"]))
|
||||
@@ -148,18 +225,25 @@ class Forms:
|
||||
questions.append(inquirer.Password("password", message="Set Password"))
|
||||
else:
|
||||
profile["password"] = defaults["password"]
|
||||
|
||||
if edit.get("fallback"):
|
||||
raw_def_fb = defaults.get("fallback", "")
|
||||
def_fb_str = json.dumps(raw_def_fb) if isinstance(raw_def_fb, (list, dict)) else str(raw_def_fb or "")
|
||||
questions.append(inquirer.Text("fallback", message="Add fallback credentials (JSON list/dict, @profile, or compose:)", validate=self.validators.fallback_validation, default=def_fb_str.replace("{", "{{").replace("}", "}}")))
|
||||
else:
|
||||
profile["fallback"] = defaults.get("fallback", "")
|
||||
|
||||
answer = inquirer.prompt(questions)
|
||||
if answer is None:
|
||||
return False
|
||||
|
||||
if "password" in answer:
|
||||
if answer["password"] != "":
|
||||
answer["password"] = self.app.services.config_svc.encrypt_password(answer["password"])
|
||||
|
||||
|
||||
if "password" in answer and answer["password"] != "":
|
||||
answer["password"] = self.app.services.config_svc.encrypt_password(answer["password"])
|
||||
|
||||
if "tags" in answer and answer["tags"]:
|
||||
answer["tags"] = ast.literal_eval(answer["tags"])
|
||||
|
||||
answer["tags"] = _parse_tags_input(answer["tags"])
|
||||
if "fallback" in answer:
|
||||
answer["fallback"] = _parse_fallback_input(answer["fallback"])
|
||||
|
||||
result = {**answer, **profile}
|
||||
result["id"] = unique
|
||||
return result
|
||||
@@ -178,11 +262,12 @@ class Forms:
|
||||
questions.append(inquirer.Text("jumphost", message="Add Jumphost node", validate=self.validators.jumphost_validation))
|
||||
questions.append(inquirer.Text("user", message="Pick username", validate=self.validators.default_validation))
|
||||
questions.append(inquirer.List("password", message="Password: Use a local password, no password or a list of profiles to reference?", choices=["Local Password", "Profiles", "No Password"]))
|
||||
|
||||
questions.append(inquirer.Text("fallback", message="Add fallback credentials (JSON list/dict, @profile, or compose:)", validate=self.validators.fallback_validation))
|
||||
|
||||
answer = inquirer.prompt(questions)
|
||||
if answer is None:
|
||||
return False
|
||||
|
||||
|
||||
if "password" in answer:
|
||||
if answer["password"] == "Local Password":
|
||||
passq = [inquirer.Password("password", message="Set Password")]
|
||||
@@ -194,11 +279,13 @@ class Forms:
|
||||
answer["password"] = passa["password"].split(",")
|
||||
elif answer["password"] == "No Password":
|
||||
answer["password"] = ""
|
||||
|
||||
|
||||
answer["type"] = "connection"
|
||||
if "tags" in answer and not answer["tags"].startswith("@") and answer["tags"]:
|
||||
answer["tags"] = ast.literal_eval(answer["tags"])
|
||||
|
||||
if "tags" in answer and answer["tags"] and not str(answer["tags"]).startswith("@"):
|
||||
answer["tags"] = _parse_tags_input(answer["tags"])
|
||||
if "fallback" in answer:
|
||||
answer["fallback"] = _parse_fallback_input(answer["fallback"])
|
||||
|
||||
return answer
|
||||
|
||||
def mcp_wizard(self, mcp_servers):
|
||||
|
||||
+74
-15
@@ -1,5 +1,19 @@
|
||||
import re
|
||||
import ast
|
||||
import json
|
||||
import re
|
||||
|
||||
from ..auth_utils import (
|
||||
MAX_FALLBACK_ENTRIES,
|
||||
MAX_JSON_INPUT_BYTES,
|
||||
MAX_JSON_INPUT_DEPTH,
|
||||
SanitizedAuthError,
|
||||
is_compose_expression,
|
||||
validate_auth_budget_tags,
|
||||
validate_compose_syntax,
|
||||
validate_fallback_entries,
|
||||
validate_json_depth_and_size,
|
||||
)
|
||||
|
||||
|
||||
def _raise_val_err(reason):
|
||||
import inquirer
|
||||
@@ -56,35 +70,80 @@ class Validators:
|
||||
return True
|
||||
|
||||
def pass_validation(self, answers, current, regex = "(^@.+$)"):
|
||||
if is_compose_expression(current):
|
||||
try:
|
||||
validate_compose_syntax(current)
|
||||
except SanitizedAuthError as exc:
|
||||
_raise_val_err(str(exc))
|
||||
return True
|
||||
profiles = current.split(",")
|
||||
for i in profiles:
|
||||
if not re.match(regex, i) or i[1:] not in self.app.profiles:
|
||||
_raise_val_err("Profile {} don't exist".format(i))
|
||||
return True
|
||||
|
||||
def _validate_tags_dict(self, current: str):
|
||||
if len(current.encode("utf-8", errors="replace")) > MAX_JSON_INPUT_BYTES:
|
||||
_raise_val_err(f"Tags exceed maximum size of {MAX_JSON_INPUT_BYTES} bytes")
|
||||
parsed = None
|
||||
try:
|
||||
parsed = json.loads(current)
|
||||
except (ValueError, TypeError):
|
||||
try:
|
||||
parsed = ast.literal_eval(current)
|
||||
except (ValueError, TypeError, SyntaxError, MemoryError, RecursionError):
|
||||
parsed = None
|
||||
if not isinstance(parsed, dict):
|
||||
_raise_val_err("Tags should be a python dictionary.")
|
||||
try:
|
||||
validate_json_depth_and_size(
|
||||
parsed,
|
||||
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||
)
|
||||
validate_auth_budget_tags(parsed)
|
||||
except (ValueError, TypeError, SanitizedAuthError) as exc:
|
||||
_raise_val_err(str(exc))
|
||||
return True
|
||||
|
||||
def tags_validation(self, answers, current):
|
||||
if current.startswith("@"):
|
||||
if current[1:] not in self.app.profiles:
|
||||
_raise_val_err("Profile {} don't exist".format(current))
|
||||
elif current != "":
|
||||
isdict = False
|
||||
try:
|
||||
isdict = ast.literal_eval(current)
|
||||
except Exception:
|
||||
pass
|
||||
if not isinstance (isdict, dict):
|
||||
_raise_val_err("Tags should be a python dictionary.".format(current))
|
||||
return self._validate_tags_dict(current)
|
||||
return True
|
||||
|
||||
def profile_tags_validation(self, answers, current):
|
||||
if current != "":
|
||||
isdict = False
|
||||
try:
|
||||
isdict = ast.literal_eval(current)
|
||||
except Exception:
|
||||
pass
|
||||
if not isinstance (isdict, dict):
|
||||
_raise_val_err("Tags should be a python dictionary.".format(current))
|
||||
return self._validate_tags_dict(current)
|
||||
return True
|
||||
|
||||
def fallback_validation(self, answers, current):
|
||||
if current in (None, ""):
|
||||
return True
|
||||
stripped = str(current).strip()
|
||||
if not stripped:
|
||||
return True
|
||||
if len(stripped.encode("utf-8", errors="replace")) > MAX_JSON_INPUT_BYTES:
|
||||
_raise_val_err(f"Fallback exceeds maximum size of {MAX_JSON_INPUT_BYTES} bytes")
|
||||
if stripped.startswith("@") and "+" not in stripped:
|
||||
if stripped[1:] not in self.app.profiles:
|
||||
_raise_val_err(f"Profile {stripped} don't exist")
|
||||
return True
|
||||
try:
|
||||
if is_compose_expression(stripped):
|
||||
validate_compose_syntax(stripped)
|
||||
return True
|
||||
if stripped.startswith(("[", "{")):
|
||||
validate_json_depth_and_size(
|
||||
stripped,
|
||||
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||
)
|
||||
validate_fallback_entries(stripped, max_entries=MAX_FALLBACK_ENTRIES)
|
||||
except (ValueError, TypeError, SanitizedAuthError) as exc:
|
||||
_raise_val_err(str(exc))
|
||||
return True
|
||||
|
||||
def jumphost_validation(self, answers, current):
|
||||
|
||||
+623
-222
File diff suppressed because it is too large.
Load diff
+14
-17
@@ -1,20 +1,20 @@
|
||||
#!/usr/bin/env python3
|
||||
#Imports
|
||||
import argparse
|
||||
import os
|
||||
import re
|
||||
import argparse
|
||||
import sys
|
||||
import yaml
|
||||
import sys
|
||||
from .core import node,nodes
|
||||
from ._version import __version__
|
||||
from . import printer
|
||||
from .api import start_api,stop_api,debug_api
|
||||
|
||||
import yaml
|
||||
from rich_argparse import RichHelpFormatter
|
||||
|
||||
from . import printer
|
||||
from .api import debug_api, start_api, stop_api
|
||||
from .cli.help_text import get_help
|
||||
from .cli.helpers import folders_completer, nodes_completer
|
||||
from .core import node, nodes
|
||||
from .plugins import Plugins
|
||||
from .services.exceptions import ConnpyError, ProfileNotFoundError, ReservedNameError
|
||||
|
||||
from rich_argparse import RichHelpFormatter
|
||||
# Bridge rich-argparse with our design system
|
||||
RichHelpFormatter.console = printer.console
|
||||
RichHelpFormatter.styles.update({
|
||||
@@ -28,14 +28,6 @@ RichHelpFormatter.styles.update({
|
||||
})
|
||||
RichHelpFormatter.group_name_formatter = str.upper
|
||||
|
||||
from .cli import (
|
||||
NodeHandler, ProfileHandler, ConfigHandler, RunHandler,
|
||||
AIHandler, APIHandler, PluginHandler, ImportExportHandler,
|
||||
ContextHandler, SSOHandler
|
||||
)
|
||||
from .cli.helpers import nodes_completer, folders_completer, profiles_completer
|
||||
from .cli.help_text import get_help
|
||||
|
||||
console = printer.console
|
||||
|
||||
#functions and classes
|
||||
@@ -389,6 +381,11 @@ class connapp:
|
||||
configcrud.add_argument("--shell-command", dest="shell_command", nargs=1, action=self._store_type, help="Set default shell command", metavar="COMMAND")
|
||||
configcrud.add_argument("--shell-prompt", dest="shell_prompt", nargs=1, action=self._store_type, help="Set shell prompt regex for AI", metavar="REGEX")
|
||||
configcrud.add_argument("--shell-os", dest="shell_os", nargs=1, action=self._store_type, help="Set shell OS hint for AI", metavar="OS")
|
||||
configcrud.add_argument("--max-auth-prompts", dest="max_auth_prompts", nargs=1, action=self._store_type, help="Set max auth prompts per connection (1-5, 0 to reset)", type=int, metavar="INT")
|
||||
configcrud.add_argument("--max-total-auth-attempts", dest="max_total_auth_attempts", nargs=1, action=self._store_type, help="Set max total auth attempts per host (1-10, 0 to reset)", type=int, metavar="INT")
|
||||
configcrud.add_argument("--max-hop-host-attempts", dest="max_hop_host_attempts", nargs=1, action=self._store_type, help="Set max auth attempts per jumphost (1-10, 0 to reset)", type=int, metavar="INT")
|
||||
configcrud.add_argument("--max-global-user-attempts", dest="max_global_user_attempts", nargs=1, action=self._store_type, help="Set max global auth attempts per user (1-1000, 0 to reset)", type=int, metavar="INT")
|
||||
configcrud.add_argument("--disable-fallback-on-insecure-hostkey", dest="disable_fallback_on_insecure_hostkey", nargs=1, action=self._store_type, help="Disable AAA fallback when StrictHostKeyChecking=no", choices=["true", "false"])
|
||||
configparser.add_argument("--trusted-commands", dest="trusted_commands", nargs=1, action=self._store_type, help="Set custom trusted commands regexes (comma separated)", metavar="REGEX,REGEX")
|
||||
configparser.set_defaults(func=self._config.dispatch)
|
||||
|
||||
|
||||
+1036
-109
File diff suppressed because it is too large.
Load diff
+76
-28
@@ -1,26 +1,37 @@
|
||||
import grpc
|
||||
from concurrent import futures
|
||||
from google.protobuf.empty_pb2 import Empty
|
||||
import os
|
||||
import ctypes
|
||||
import threading
|
||||
import contextvars
|
||||
import datetime
|
||||
import functools
|
||||
import json
|
||||
import os
|
||||
from concurrent import futures
|
||||
|
||||
import grpc
|
||||
from google.protobuf.empty_pb2 import Empty
|
||||
|
||||
# Suppress harmless but noisy gRPC fork() warnings from pexpect child processes
|
||||
os.environ["GRPC_VERBOSITY"] = "NONE"
|
||||
os.environ["GRPC_ENABLE_FORK_SUPPORT"] = "0"
|
||||
from . import connpy_pb2, connpy_pb2_grpc, remote_plugin_pb2, remote_plugin_pb2_grpc
|
||||
import json
|
||||
from .utils import to_value, from_value, to_struct, from_struct
|
||||
from ..services.exceptions import ConnpyError
|
||||
from .. import printer
|
||||
from ..auth_utils import (
|
||||
MAX_FALLBACK_ENTRIES,
|
||||
MAX_JSON_INPUT_BYTES,
|
||||
MAX_JSON_INPUT_DEPTH,
|
||||
SanitizedAuthError,
|
||||
sanitize_exception,
|
||||
validate_auth_budget_tags,
|
||||
validate_fallback_entries,
|
||||
validate_json_depth_and_size,
|
||||
)
|
||||
from ..services.exceptions import ConnpyError
|
||||
from . import connpy_pb2, connpy_pb2_grpc, remote_plugin_pb2, remote_plugin_pb2_grpc
|
||||
from .utils import from_struct, from_value, to_struct, to_value
|
||||
|
||||
_current_user = contextvars.ContextVar("current_user", default=None)
|
||||
|
||||
def handle_errors(func):
|
||||
import inspect
|
||||
if inspect.isgeneratorfunction(func):
|
||||
@functools.wraps(func)
|
||||
def wrapper(*args, **kwargs):
|
||||
try:
|
||||
for item in func(*args, **kwargs):
|
||||
@@ -41,6 +52,7 @@ def handle_errors(func):
|
||||
printer.clear_thread_state()
|
||||
return wrapper
|
||||
else:
|
||||
@functools.wraps(func)
|
||||
def wrapper(*args, **kwargs):
|
||||
try:
|
||||
return func(*args, **kwargs)
|
||||
@@ -115,8 +127,24 @@ class NodeServicer(connpy_pb2_grpc.NodeServiceServicer):
|
||||
self.server_console.print(f"[debug][DEBUG][/debug] gRPC interact_node request for: [bold cyan]{unique_id}[/bold cyan]")
|
||||
|
||||
if first_req.connection_params_json:
|
||||
import json
|
||||
params = json.loads(first_req.connection_params_json)
|
||||
try:
|
||||
params = validate_json_depth_and_size(
|
||||
first_req.connection_params_json,
|
||||
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||
)
|
||||
if not isinstance(params, dict):
|
||||
raise TypeError("connection_params_json must be a JSON object")
|
||||
if "fallback" in params and params["fallback"] not in (None, "", []):
|
||||
validate_fallback_entries(
|
||||
params["fallback"],
|
||||
max_entries=MAX_FALLBACK_ENTRIES,
|
||||
)
|
||||
if "tags" in params and params["tags"] not in ("", None):
|
||||
validate_auth_budget_tags(params["tags"])
|
||||
except (ValueError, TypeError, SanitizedAuthError) as exc:
|
||||
context.abort(grpc.StatusCode.INVALID_ARGUMENT, str(exc))
|
||||
|
||||
base_node_id = params.get("base_node")
|
||||
# Valid attributes that a node object accepts
|
||||
valid_attrs = ['host', 'options', 'logs', 'password', 'port', 'protocol', 'user', 'jumphost']
|
||||
@@ -134,6 +162,8 @@ class NodeServicer(connpy_pb2_grpc.NodeServiceServicer):
|
||||
for attr in valid_attrs:
|
||||
if attr in params:
|
||||
device[attr] = params[attr]
|
||||
if "fallback" in params:
|
||||
device["fallback"] = params["fallback"]
|
||||
|
||||
if "tags" in params:
|
||||
device_tags = device.get("tags", {})
|
||||
@@ -147,20 +177,30 @@ class NodeServicer(connpy_pb2_grpc.NodeServiceServicer):
|
||||
else:
|
||||
# base_node not found, fall back to dynamic
|
||||
node_name = params.get("name", fallback_id)
|
||||
n = node(node_name, host=params.get("host", ""), config=user_config)
|
||||
for attr in valid_attrs:
|
||||
if attr in params:
|
||||
setattr(n, attr, params[attr])
|
||||
dyn_kwargs = {k: params[k] for k in valid_attrs if k in params and k != "host"}
|
||||
if "fallback" in params:
|
||||
dyn_kwargs["fallback"] = params["fallback"]
|
||||
if "tags" in params:
|
||||
n.tags = params["tags"]
|
||||
dyn_kwargs["tags"] = params["tags"]
|
||||
n = node(
|
||||
node_name,
|
||||
host=params.get("host", ""),
|
||||
config=user_config,
|
||||
**dyn_kwargs,
|
||||
)
|
||||
else:
|
||||
node_name = params.get("name", fallback_id)
|
||||
n = node(node_name, host=params.get("host", ""), config=user_config)
|
||||
for attr in valid_attrs:
|
||||
if attr in params:
|
||||
setattr(n, attr, params[attr])
|
||||
dyn_kwargs = {k: params[k] for k in valid_attrs if k in params and k != "host"}
|
||||
if "fallback" in params:
|
||||
dyn_kwargs["fallback"] = params["fallback"]
|
||||
if "tags" in params:
|
||||
n.tags = params["tags"]
|
||||
dyn_kwargs["tags"] = params["tags"]
|
||||
n = node(
|
||||
node_name,
|
||||
host=params.get("host", ""),
|
||||
config=user_config,
|
||||
**dyn_kwargs,
|
||||
)
|
||||
n._orig_protocol = n.protocol
|
||||
if sftp:
|
||||
n.protocol = "sftp"
|
||||
@@ -181,9 +221,11 @@ class NodeServicer(connpy_pb2_grpc.NodeServiceServicer):
|
||||
debug_chunks = []
|
||||
if debug:
|
||||
from io import StringIO
|
||||
|
||||
from rich.console import Console as RichConsole
|
||||
from ..printer import connpy_theme
|
||||
|
||||
from .. import printer as _printer
|
||||
from ..printer import connpy_theme
|
||||
|
||||
def remote_logger(msg_type, message):
|
||||
buf = StringIO()
|
||||
@@ -204,7 +246,14 @@ class NodeServicer(connpy_pb2_grpc.NodeServiceServicer):
|
||||
else:
|
||||
remote_logger = None
|
||||
|
||||
connect = n._connect(debug=debug, logger=remote_logger)
|
||||
try:
|
||||
connect = n._connect(
|
||||
debug=debug,
|
||||
logger=remote_logger,
|
||||
attempt_tracker=getattr(provider, "auth_tracker", None),
|
||||
)
|
||||
except Exception as exc: # noqa: BLE001
|
||||
connect = str(sanitize_exception(exc, code="AUTH_FAILED"))
|
||||
|
||||
# Send debug output to client before checking result (always show the command)
|
||||
for chunk in debug_chunks:
|
||||
@@ -535,10 +584,9 @@ class NodeServicer(connpy_pb2_grpc.NodeServiceServicer):
|
||||
continue
|
||||
|
||||
asyncio.run(n._async_interact_loop(remote_stream, resize_callback, copilot_handler=remote_copilot_handler))
|
||||
except Exception as e:
|
||||
import traceback
|
||||
print(f"[ERROR in run_async_loop] {e}")
|
||||
traceback.print_exc()
|
||||
except Exception as e: # noqa: BLE001
|
||||
sanitized_err = sanitize_exception(e, code="TRANSPORT_ERROR", context_label="interact stream")
|
||||
print(f"[ERROR in run_async_loop] {sanitized_err}")
|
||||
finally:
|
||||
n._teardown_interact_environment()
|
||||
response_queue.put(None) # Signal EOF
|
||||
|
||||
+122
-52
@@ -6,6 +6,20 @@ from typing import Any, ClassVar
|
||||
|
||||
import yaml
|
||||
|
||||
from .auth_utils import (
|
||||
MAX_FALLBACK_ENTRIES,
|
||||
MAX_JSON_INPUT_BYTES,
|
||||
MAX_JSON_INPUT_DEPTH,
|
||||
SanitizedAuthError,
|
||||
is_compose_expression,
|
||||
mask_node_secrets,
|
||||
sanitize_exception,
|
||||
validate_auth_budget_tags,
|
||||
validate_compose_syntax,
|
||||
validate_fallback_entries,
|
||||
validate_json_depth_and_size,
|
||||
)
|
||||
|
||||
try:
|
||||
from mcp.server.mcpserver import MCPServer
|
||||
except ImportError:
|
||||
@@ -124,20 +138,7 @@ class ConnpyMCPServer:
|
||||
|
||||
def _mask_secrets(self, val: Any) -> Any:
|
||||
"""Recursively mask passwords, passphrases, secrets, and auth tokens with '***'."""
|
||||
if isinstance(val, dict):
|
||||
masked = {}
|
||||
for k, v in val.items():
|
||||
k_lower = str(k).lower()
|
||||
if any(sec in k_lower for sec in ["password", "passphrase", "secret", "token"]):
|
||||
masked[k] = "***"
|
||||
elif isinstance(v, (dict, list)):
|
||||
masked[k] = self._mask_secrets(v)
|
||||
else:
|
||||
masked[k] = v
|
||||
return masked
|
||||
if isinstance(val, list):
|
||||
return [self._mask_secrets(item) for item in val]
|
||||
return val
|
||||
return mask_node_secrets(val)
|
||||
|
||||
# -------------------------------------------------------------------------
|
||||
# Level 1: Read-Only Inventory and Context Tools
|
||||
@@ -196,6 +197,31 @@ class ConnpyMCPServer:
|
||||
# Level 2: Execution, Testing, and Playbooks Tools
|
||||
# -------------------------------------------------------------------------
|
||||
|
||||
@staticmethod
|
||||
def _parse_json_list_or_lines(raw_val: list[str] | str, *, allow_empty_str: bool = False) -> list[str]:
|
||||
if isinstance(raw_val, str):
|
||||
if len(raw_val.encode("utf-8", errors="replace")) > MAX_JSON_INPUT_BYTES:
|
||||
raise ValueError(f"Input exceeds maximum size of {MAX_JSON_INPUT_BYTES} bytes")
|
||||
stripped = raw_val.strip()
|
||||
if stripped.startswith(("[", "{")):
|
||||
parsed = validate_json_depth_and_size(
|
||||
stripped,
|
||||
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||
)
|
||||
return parsed if isinstance(parsed, list) else [raw_val]
|
||||
return ([raw_val] if raw_val else []) if allow_empty_str else [
|
||||
c.strip() for c in raw_val.splitlines() if c.strip()
|
||||
]
|
||||
if isinstance(raw_val, list):
|
||||
validate_json_depth_and_size(
|
||||
raw_val,
|
||||
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||
)
|
||||
return raw_val
|
||||
return [str(raw_val)] if (raw_val or not allow_empty_str) else []
|
||||
|
||||
def run_commands(
|
||||
self,
|
||||
nodes_filter: str,
|
||||
@@ -210,17 +236,10 @@ class ConnpyMCPServer:
|
||||
if not self.services or not hasattr(self.services, "execution"):
|
||||
return {"success": False, "error": "Execution service unavailable."}
|
||||
|
||||
# Normalize commands list
|
||||
if isinstance(commands, str):
|
||||
try:
|
||||
import json
|
||||
|
||||
parsed = json.loads(commands)
|
||||
commands = parsed if isinstance(parsed, list) else [commands]
|
||||
except Exception: # noqa: BLE001
|
||||
commands = [c.strip() for c in commands.splitlines() if c.strip()]
|
||||
elif not isinstance(commands, list):
|
||||
commands = [str(commands)]
|
||||
try:
|
||||
commands = self._parse_json_list_or_lines(commands, allow_empty_str=False)
|
||||
except ValueError as exc:
|
||||
return {"success": False, "error": str(exc)}
|
||||
|
||||
# Check safety
|
||||
if not self.trust:
|
||||
@@ -239,9 +258,24 @@ class ConnpyMCPServer:
|
||||
parallel=par_count,
|
||||
timeout=timeout,
|
||||
)
|
||||
return {"success": True, "results": results}
|
||||
fb_used = False
|
||||
if isinstance(results, dict):
|
||||
fb_used = any(
|
||||
isinstance(v, dict) and bool(v.get("fallback_used", False))
|
||||
for v in results.values()
|
||||
)
|
||||
if not fb_used:
|
||||
fb_map = getattr(self.services.execution, "last_fallback_used", {})
|
||||
if isinstance(fb_map, dict):
|
||||
fb_used = any(bool(v) for v in fb_map.values())
|
||||
return {"success": True, "results": results, "fallback_used": fb_used}
|
||||
except Exception as e: # noqa: BLE001
|
||||
return {"success": False, "error": str(e)}
|
||||
san = sanitize_exception(
|
||||
e,
|
||||
code="AUTH_FAILED",
|
||||
context_label="command execution failed",
|
||||
)
|
||||
return {"success": False, "error": str(san)}
|
||||
|
||||
def test_commands(
|
||||
self,
|
||||
@@ -255,29 +289,11 @@ class ConnpyMCPServer:
|
||||
if not self.services or not hasattr(self.services, "execution"):
|
||||
return {"success": False, "error": "Execution service unavailable."}
|
||||
|
||||
# Normalize commands
|
||||
if isinstance(commands, str):
|
||||
try:
|
||||
import json
|
||||
|
||||
parsed = json.loads(commands)
|
||||
commands = parsed if isinstance(parsed, list) else [commands]
|
||||
except Exception: # noqa: BLE001
|
||||
commands = [c.strip() for c in commands.splitlines() if c.strip()]
|
||||
elif not isinstance(commands, list):
|
||||
commands = [str(commands)]
|
||||
|
||||
# Normalize expected
|
||||
if isinstance(expected, str):
|
||||
try:
|
||||
import json
|
||||
|
||||
parsed = json.loads(expected)
|
||||
expected = parsed if isinstance(parsed, list) else ([expected] if expected else [])
|
||||
except Exception: # noqa: BLE001
|
||||
expected = [expected] if expected else []
|
||||
elif not isinstance(expected, list):
|
||||
expected = [str(expected)] if expected else []
|
||||
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 {"success": False, "error": str(exc)}
|
||||
|
||||
# Check safety
|
||||
if not self.trust:
|
||||
@@ -297,9 +313,16 @@ class ConnpyMCPServer:
|
||||
parallel=par_count,
|
||||
timeout=timeout,
|
||||
)
|
||||
return {"success": True, "results": results}
|
||||
fb_map = getattr(self.services.execution, "last_fallback_used", {})
|
||||
fb_used = any(bool(v) for v in fb_map.values()) if isinstance(fb_map, dict) else False
|
||||
return {"success": True, "results": results, "fallback_used": fb_used}
|
||||
except Exception as e: # noqa: BLE001
|
||||
return {"success": False, "error": str(e)}
|
||||
san = sanitize_exception(
|
||||
e,
|
||||
code="AUTH_FAILED",
|
||||
context_label="test execution failed",
|
||||
)
|
||||
return {"success": False, "error": str(san)}
|
||||
|
||||
def validate_playbook(self, playbook_yaml: str) -> dict:
|
||||
"""Validate the syntax, schema, and tasks of a Connpy YAML playbook."""
|
||||
@@ -427,6 +450,47 @@ class ConnpyMCPServer:
|
||||
# Level 3: Inventory Mutation Tools (CRUD - Requires trust=True)
|
||||
# -------------------------------------------------------------------------
|
||||
|
||||
@staticmethod
|
||||
def _validate_and_normalize_node_payload(node_data: dict[str, Any]) -> dict[str, Any]:
|
||||
validate_json_depth_and_size(
|
||||
node_data,
|
||||
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||
)
|
||||
normalized = node_data.copy()
|
||||
if "tags" in normalized and normalized["tags"] not in ("", None):
|
||||
tags_val = normalized["tags"]
|
||||
if isinstance(tags_val, str) and tags_val.strip().startswith("{"):
|
||||
tags_val = validate_json_depth_and_size(
|
||||
tags_val,
|
||||
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||
)
|
||||
normalized["tags"] = tags_val
|
||||
validate_auth_budget_tags(tags_val)
|
||||
|
||||
if "fallback" in normalized and normalized["fallback"] not in (None, "", []):
|
||||
fb_val = normalized["fallback"]
|
||||
if isinstance(fb_val, str) and fb_val.strip().startswith(("[", "{")):
|
||||
fb_val = validate_json_depth_and_size(
|
||||
fb_val,
|
||||
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||
)
|
||||
normalized["fallback"] = validate_fallback_entries(
|
||||
fb_val,
|
||||
max_entries=MAX_FALLBACK_ENTRIES,
|
||||
)
|
||||
|
||||
pw = normalized.get("password")
|
||||
if is_compose_expression(pw):
|
||||
validate_compose_syntax(pw)
|
||||
elif isinstance(pw, (list, tuple)):
|
||||
for item in pw:
|
||||
if is_compose_expression(item):
|
||||
validate_compose_syntax(item)
|
||||
return normalized
|
||||
|
||||
def add_node(
|
||||
self, unique_id: str, data: dict | None = None, is_folder: bool = False, **kwargs
|
||||
) -> dict:
|
||||
@@ -444,11 +508,14 @@ class ConnpyMCPServer:
|
||||
node_data.update(kwargs)
|
||||
|
||||
try:
|
||||
node_data = self._validate_and_normalize_node_payload(node_data)
|
||||
self.services.nodes.add_node(unique_id, node_data, is_folder=is_folder)
|
||||
return {
|
||||
"success": True,
|
||||
"message": f"{'Folder' if is_folder else 'Node'} '{unique_id}' created successfully.",
|
||||
}
|
||||
except (ValueError, TypeError, SanitizedAuthError) as exc:
|
||||
return {"success": False, "error": str(exc)}
|
||||
except Exception as e: # noqa: BLE001
|
||||
return {"success": False, "error": str(e)}
|
||||
|
||||
@@ -467,8 +534,11 @@ class ConnpyMCPServer:
|
||||
node_updates.update(kwargs)
|
||||
|
||||
try:
|
||||
node_updates = self._validate_and_normalize_node_payload(node_updates)
|
||||
self.services.nodes.update_node(unique_id, node_updates)
|
||||
return {"success": True, "message": f"Node '{unique_id}' updated successfully."}
|
||||
except (ValueError, TypeError, SanitizedAuthError) as exc:
|
||||
return {"success": False, "error": str(exc)}
|
||||
except Exception as e: # noqa: BLE001
|
||||
return {"success": False, "error": str(e)}
|
||||
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
import inspect
|
||||
import os
|
||||
from collections.abc import Callable
|
||||
from contextlib import suppress
|
||||
from typing import Any
|
||||
|
||||
from connpy.auth_utils import AuthAttemptTracker, raise_sanitized_auth_error
|
||||
from connpy.core import nodes as Nodes
|
||||
|
||||
from .base import BaseService
|
||||
@@ -11,9 +14,13 @@ from .exceptions import ConnpyError
|
||||
class ExecutionService(BaseService):
|
||||
"""Business logic for executing commands on nodes and running automation scripts."""
|
||||
|
||||
def __init__(self, config=None, remote_stub=None):
|
||||
def __init__(self, config=None, remote_stub=None, attempt_tracker=None):
|
||||
super().__init__(config)
|
||||
self.remote_stub = remote_stub
|
||||
self.attempt_tracker = (
|
||||
attempt_tracker if attempt_tracker is not None else AuthAttemptTracker()
|
||||
)
|
||||
self.last_fallback_used: dict[str, bool] = {}
|
||||
|
||||
def _is_remote_backend(self) -> bool:
|
||||
"""Return True when execution is configured to run against a remote gRPC backend."""
|
||||
@@ -54,11 +61,8 @@ class ExecutionService(BaseService):
|
||||
if defaultdir:
|
||||
token_path = os.path.join(defaultdir, ".token")
|
||||
if os.path.exists(token_path):
|
||||
try:
|
||||
with open(token_path) as f:
|
||||
return f.read().strip()
|
||||
except OSError:
|
||||
pass
|
||||
with suppress(OSError), open(token_path) as f:
|
||||
return f.read().strip()
|
||||
return None
|
||||
|
||||
channel = grpc.insecure_channel(remote_host)
|
||||
@@ -110,33 +114,47 @@ class ExecutionService(BaseService):
|
||||
executor = Nodes(node_data, config=self.config)
|
||||
self.last_executor = executor
|
||||
|
||||
results = executor.run(
|
||||
commands=commands,
|
||||
vars=variables,
|
||||
parallel=parallel,
|
||||
timeout=timeout,
|
||||
folder=folder,
|
||||
prompt=prompt,
|
||||
on_complete=on_node_complete,
|
||||
logger=logger,
|
||||
connect_timeout=connect_timeout,
|
||||
auth_timeout=auth_timeout,
|
||||
_default_cmd_timeout=20,
|
||||
)
|
||||
run_kwargs: dict[str, Any] = {
|
||||
"commands": commands,
|
||||
"vars": variables,
|
||||
"parallel": parallel,
|
||||
"timeout": timeout,
|
||||
"folder": folder,
|
||||
"prompt": prompt,
|
||||
"on_complete": on_node_complete,
|
||||
"logger": logger,
|
||||
"connect_timeout": connect_timeout,
|
||||
"auth_timeout": auth_timeout,
|
||||
"_default_cmd_timeout": 20,
|
||||
}
|
||||
run_params = inspect.signature(executor.run).parameters
|
||||
if "unattended" in run_params:
|
||||
run_kwargs["unattended"] = True
|
||||
if "attempt_tracker" in run_params:
|
||||
run_kwargs["attempt_tracker"] = self.attempt_tracker
|
||||
|
||||
# Combine output and status for the caller
|
||||
results = executor.run(**run_kwargs)
|
||||
|
||||
fb_map = getattr(executor, "fallback_used", {})
|
||||
fb_dict = fb_map if isinstance(fb_map, dict) else {}
|
||||
|
||||
# Combine output, status, and fallback metadata for the caller
|
||||
full_results = {}
|
||||
self.last_fallback_used = {}
|
||||
for unique in results:
|
||||
fb_used = bool(fb_dict.get(unique, False))
|
||||
full_results[unique] = {
|
||||
"output": results[unique],
|
||||
"status": executor.status.get(unique, 1),
|
||||
"fallback_used": fb_used,
|
||||
}
|
||||
self.last_fallback_used[unique] = fb_used
|
||||
|
||||
return full_results
|
||||
except ConnpyError:
|
||||
raise
|
||||
except Exception as e:
|
||||
raise ConnpyError(f"Execution failed: {e}") from e
|
||||
except Exception as e: # noqa: BLE001
|
||||
raise_sanitized_auth_error(e, code="AUTH_FAILED", context_label="execution failed")
|
||||
|
||||
def test_commands(
|
||||
self,
|
||||
@@ -185,24 +203,38 @@ class ExecutionService(BaseService):
|
||||
executor = Nodes(node_data, config=self.config)
|
||||
self.last_executor = executor
|
||||
|
||||
return executor.test(
|
||||
commands=commands,
|
||||
expected=expected,
|
||||
vars=variables,
|
||||
parallel=parallel,
|
||||
timeout=timeout,
|
||||
folder=folder,
|
||||
prompt=prompt,
|
||||
on_complete=on_node_complete,
|
||||
logger=logger,
|
||||
connect_timeout=connect_timeout,
|
||||
auth_timeout=auth_timeout,
|
||||
_default_cmd_timeout=20,
|
||||
)
|
||||
test_kwargs: dict[str, Any] = {
|
||||
"commands": commands,
|
||||
"expected": expected,
|
||||
"vars": variables,
|
||||
"parallel": parallel,
|
||||
"timeout": timeout,
|
||||
"folder": folder,
|
||||
"prompt": prompt,
|
||||
"on_complete": on_node_complete,
|
||||
"logger": logger,
|
||||
"connect_timeout": connect_timeout,
|
||||
"auth_timeout": auth_timeout,
|
||||
"_default_cmd_timeout": 20,
|
||||
}
|
||||
test_params = inspect.signature(executor.test).parameters
|
||||
if "unattended" in test_params:
|
||||
test_kwargs["unattended"] = True
|
||||
if "attempt_tracker" in test_params:
|
||||
test_kwargs["attempt_tracker"] = self.attempt_tracker
|
||||
|
||||
res = executor.test(**test_kwargs)
|
||||
fb_map = getattr(executor, "fallback_used", {})
|
||||
fb_dict = fb_map if isinstance(fb_map, dict) else {}
|
||||
if isinstance(res, dict):
|
||||
self.last_fallback_used = {u: bool(fb_dict.get(u, False)) for u in res}
|
||||
else:
|
||||
self.last_fallback_used = {}
|
||||
return res
|
||||
except ConnpyError:
|
||||
raise
|
||||
except Exception as e:
|
||||
raise ConnpyError(f"Testing failed: {e}") from e
|
||||
except Exception as e: # noqa: BLE001
|
||||
raise_sanitized_auth_error(e, code="AUTH_FAILED", context_label="testing failed")
|
||||
|
||||
def run_cli_script(self, nodes_filter: str, script_path: str, parallel: int = 10) -> dict[str, str]:
|
||||
"""Run a plain-text script containing one command per line."""
|
||||
@@ -212,8 +244,8 @@ class ExecutionService(BaseService):
|
||||
try:
|
||||
with open(script_path) as f:
|
||||
commands = [line.strip() for line in f if line.strip()]
|
||||
except OSError as e:
|
||||
raise ConnpyError(f"Failed to read script {script_path}: {e}") from e
|
||||
except OSError:
|
||||
raise ConnpyError("Failed to read script file") from None
|
||||
|
||||
return self.run_commands(nodes_filter, commands, parallel=parallel)
|
||||
|
||||
+263
-44
@@ -1,14 +1,73 @@
|
||||
import inspect
|
||||
import re
|
||||
from copy import deepcopy
|
||||
from typing import ClassVar
|
||||
|
||||
from .base import BaseService
|
||||
from .exceptions import (
|
||||
NodeNotFoundError, NodeAlreadyExistsError,
|
||||
InvalidConfigurationError, ReservedNameError
|
||||
InvalidConfigurationError,
|
||||
NodeAlreadyExistsError,
|
||||
NodeNotFoundError,
|
||||
ReservedNameError,
|
||||
)
|
||||
|
||||
|
||||
def _supports_transactional_update(cfg):
|
||||
return (
|
||||
cfg is not None
|
||||
and not hasattr(cfg, "assert_called")
|
||||
and callable(getattr(cfg, "transactional_update", None))
|
||||
)
|
||||
|
||||
|
||||
class NodeService(BaseService):
|
||||
_CONNECTION_FIELDS: ClassVar[frozenset[str]] = frozenset({
|
||||
"id",
|
||||
"host",
|
||||
"folder",
|
||||
"subfolder",
|
||||
"options",
|
||||
"logs",
|
||||
"password",
|
||||
"port",
|
||||
"protocol",
|
||||
"user",
|
||||
"tags",
|
||||
"jumphost",
|
||||
"fallback",
|
||||
"type",
|
||||
})
|
||||
|
||||
def __init__(self, config=None):
|
||||
super().__init__(config)
|
||||
|
||||
@staticmethod
|
||||
def _validate_node_payload(data):
|
||||
if not isinstance(data, dict):
|
||||
return
|
||||
from ..auth_utils import (
|
||||
SanitizedAuthError,
|
||||
is_compose_expression,
|
||||
validate_auth_budget_tags,
|
||||
validate_compose_syntax,
|
||||
validate_fallback_entries,
|
||||
)
|
||||
|
||||
try:
|
||||
if "tags" in data and data["tags"] not in ("", None):
|
||||
validate_auth_budget_tags(data["tags"])
|
||||
if "fallback" in data and data["fallback"] not in (None, "", []):
|
||||
validate_fallback_entries(data["fallback"])
|
||||
pw = data.get("password")
|
||||
if is_compose_expression(pw):
|
||||
validate_compose_syntax(pw)
|
||||
elif isinstance(pw, (list, tuple)):
|
||||
for item in pw:
|
||||
if is_compose_expression(item):
|
||||
validate_compose_syntax(item)
|
||||
except (ValueError, TypeError, SanitizedAuthError) as exc:
|
||||
raise InvalidConfigurationError(str(exc)) from None
|
||||
|
||||
|
||||
def list_nodes(self, filter_str=None, format_str=None):
|
||||
"""Return a listed filtered by regex match and formatted if needed."""
|
||||
@@ -85,7 +144,7 @@ class NodeService(BaseService):
|
||||
raise NodeNotFoundError(f"Node '{unique_id}' not found.")
|
||||
return details
|
||||
except (KeyError, TypeError):
|
||||
raise NodeNotFoundError(f"Node '{unique_id}' not found.")
|
||||
raise NodeNotFoundError(f"Node '{unique_id}' not found.") from None
|
||||
|
||||
def explode_unique(self, unique_id):
|
||||
"""Explode a unique ID into a dictionary of its parts."""
|
||||
@@ -115,58 +174,127 @@ class NodeService(BaseService):
|
||||
"""Logic for adding a new node or folder to configuration."""
|
||||
if not is_folder:
|
||||
self._validate_node_name(unique_id)
|
||||
|
||||
self._validate_node_payload(data)
|
||||
|
||||
if _supports_transactional_update(self.config):
|
||||
def _mut(cfg):
|
||||
all_nodes = cfg._getallnodes_unlocked()
|
||||
all_folders = cfg._getallfolders_unlocked()
|
||||
if is_folder:
|
||||
if unique_id in all_folders:
|
||||
raise NodeAlreadyExistsError(f"Folder '{unique_id}' already exists.")
|
||||
uniques = cfg._explode_unique_unlocked(unique_id)
|
||||
if not uniques:
|
||||
raise InvalidConfigurationError(f"Invalid folder name '{unique_id}'.")
|
||||
if "subfolder" in uniques:
|
||||
parent_folder = f"@{uniques['folder']}"
|
||||
if parent_folder not in all_folders:
|
||||
raise NodeNotFoundError(f"Folder '{parent_folder}' not found.")
|
||||
cfg._folder_add_unlocked(**uniques)
|
||||
else:
|
||||
if unique_id in all_nodes:
|
||||
raise NodeAlreadyExistsError(f"Node '{unique_id}' already exists.")
|
||||
node_folder = unique_id.partition("@")[2]
|
||||
if node_folder:
|
||||
parent_folder = f"@{node_folder}"
|
||||
if parent_folder not in all_folders:
|
||||
raise NodeNotFoundError(f"Folder '{parent_folder}' not found.")
|
||||
payload = deepcopy(data)
|
||||
if "id" not in payload:
|
||||
uniques = cfg._explode_unique_unlocked(unique_id)
|
||||
if uniques and "id" in uniques:
|
||||
payload["id"] = uniques["id"]
|
||||
cfg._connections_add_unlocked(**payload)
|
||||
|
||||
self.config.transactional_update(_mut)
|
||||
return
|
||||
|
||||
all_nodes = self.config._getallnodes()
|
||||
all_folders = self.config._getallfolders()
|
||||
|
||||
|
||||
if is_folder:
|
||||
if unique_id in all_folders:
|
||||
raise NodeAlreadyExistsError(f"Folder '{unique_id}' already exists.")
|
||||
uniques = self.config._explode_unique(unique_id)
|
||||
if not uniques:
|
||||
raise InvalidConfigurationError(f"Invalid folder name '{unique_id}'.")
|
||||
|
||||
# Check if parent folder exists when creating a subfolder
|
||||
|
||||
if "subfolder" in uniques:
|
||||
self.validate_parent_folder(unique_id, is_folder=True)
|
||||
|
||||
|
||||
self.config._folder_add(**uniques)
|
||||
self.config._saveconfig(self.config.file)
|
||||
else:
|
||||
if unique_id in all_nodes:
|
||||
raise NodeAlreadyExistsError(f"Node '{unique_id}' already exists.")
|
||||
|
||||
# Check if parent folder exists when creating a node in a folder
|
||||
|
||||
self.validate_parent_folder(unique_id)
|
||||
|
||||
# Ensure 'id' is in data for config._connections_add
|
||||
if "id" not in data:
|
||||
|
||||
payload = deepcopy(data)
|
||||
if "id" not in payload:
|
||||
uniques = self.config._explode_unique(unique_id)
|
||||
if uniques and "id" in uniques:
|
||||
data["id"] = uniques["id"]
|
||||
|
||||
self.config._connections_add(**data)
|
||||
payload["id"] = uniques["id"]
|
||||
|
||||
self.config._connections_add(**payload)
|
||||
self.config._saveconfig(self.config.file)
|
||||
|
||||
def _build_update_payload(self, unique_id, data, existing, uniques):
|
||||
if isinstance(existing, dict):
|
||||
merged = deepcopy(existing)
|
||||
if isinstance(uniques, dict):
|
||||
merged.update({k: v for k, v in uniques.items() if v})
|
||||
merged.update(deepcopy(data))
|
||||
filtered = {k: v for k, v in merged.items() if k in self._CONNECTION_FIELDS}
|
||||
else:
|
||||
filtered = deepcopy(data)
|
||||
if "id" not in filtered and isinstance(uniques, dict) and "id" in uniques:
|
||||
filtered["id"] = uniques["id"]
|
||||
self._validate_node_payload(filtered)
|
||||
return filtered
|
||||
|
||||
def update_node(self, unique_id, data, save=True):
|
||||
"""Explicitly update an existing node."""
|
||||
if _supports_transactional_update(self.config) and save:
|
||||
def _mut(cfg):
|
||||
all_nodes = cfg._getallnodes_unlocked()
|
||||
if unique_id not in all_nodes:
|
||||
raise NodeNotFoundError(f"Node '{unique_id}' not found.")
|
||||
uniques = cfg._explode_unique_unlocked(unique_id)
|
||||
existing = cfg.getitem_unlocked(unique_id, extract=False)
|
||||
payload = self._build_update_payload(unique_id, data, existing, uniques)
|
||||
cfg._connections_add_unlocked(**payload)
|
||||
|
||||
self.config.transactional_update(_mut)
|
||||
return
|
||||
|
||||
all_nodes = self.config._getallnodes()
|
||||
if unique_id not in all_nodes:
|
||||
raise NodeNotFoundError(f"Node '{unique_id}' not found.")
|
||||
|
||||
# Ensure 'id' is in data for config._connections_add
|
||||
if "id" not in data:
|
||||
uniques = self.config._explode_unique(unique_id)
|
||||
if uniques:
|
||||
data["id"] = uniques["id"]
|
||||
|
||||
# config._connections_add actually handles updates if ID exists correctly
|
||||
self.config._connections_add(**data)
|
||||
|
||||
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):
|
||||
"""Logic for deleting a node or folder."""
|
||||
if _supports_transactional_update(self.config) and save:
|
||||
def _mut(cfg):
|
||||
uniques = cfg._explode_unique_unlocked(unique_id)
|
||||
if not uniques:
|
||||
label = "Folder" if is_folder else "Node"
|
||||
raise NodeNotFoundError(f"{label} '{unique_id}' not found or invalid.")
|
||||
if is_folder:
|
||||
cfg._folder_del_unlocked(**uniques)
|
||||
else:
|
||||
cfg._connections_del_unlocked(**uniques)
|
||||
|
||||
self.config.transactional_update(_mut)
|
||||
return
|
||||
|
||||
if is_folder:
|
||||
uniques = self.config._explode_unique(unique_id)
|
||||
if not uniques:
|
||||
@@ -177,23 +305,40 @@ class NodeService(BaseService):
|
||||
if not uniques:
|
||||
raise NodeNotFoundError(f"Node '{unique_id}' not found or invalid.")
|
||||
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):
|
||||
"""Interact with a node directly."""
|
||||
from connpy.core import node
|
||||
|
||||
from ..auth_utils import resolve_fallback_entries
|
||||
from .profile_service import ProfileService
|
||||
|
||||
|
||||
node_data = self.config.getitem(unique_id, extract=False)
|
||||
if not node_data:
|
||||
raise NodeNotFoundError(f"Node '{unique_id}' not found.")
|
||||
|
||||
# Resolve profiles
|
||||
|
||||
profile_service = ProfileService(self.config)
|
||||
resolved_data = profile_service.resolve_node_data(node_data)
|
||||
|
||||
|
||||
node_params = inspect.signature(node.__init__).parameters
|
||||
if "fallback" not in node_params and "fallback" in resolved_data:
|
||||
raw_fb = resolved_data.pop("fallback", None)
|
||||
if raw_fb not in (None, "", []):
|
||||
resolved_fb = resolve_fallback_entries(raw_fb, self.config.profiles)
|
||||
if resolved_fb:
|
||||
tags_val = resolved_data.get("tags", "")
|
||||
if isinstance(tags_val, dict):
|
||||
tags_copy = deepcopy(tags_val)
|
||||
tags_copy["_fallback"] = resolved_fb
|
||||
resolved_data["tags"] = tags_copy
|
||||
elif tags_val in ("", None):
|
||||
resolved_data["tags"] = {"_fallback": resolved_fb}
|
||||
else:
|
||||
resolved_data["tags"] = {"_raw": tags_val, "_fallback": resolved_fb}
|
||||
|
||||
n = node(unique_id, **resolved_data, config=self.config)
|
||||
if sftp:
|
||||
n.protocol = "sftp"
|
||||
@@ -203,50 +348,109 @@ class NodeService(BaseService):
|
||||
def move_node(self, src_id, dst_id, copy=False):
|
||||
"""Move or copy a node."""
|
||||
self._validate_node_name(dst_id)
|
||||
|
||||
|
||||
if _supports_transactional_update(self.config):
|
||||
def _mut(cfg):
|
||||
try:
|
||||
node_data = cfg.getitem_unlocked(src_id)
|
||||
except (KeyError, TypeError):
|
||||
node_data = None
|
||||
if not node_data:
|
||||
raise NodeNotFoundError(f"Source node '{src_id}' not found.")
|
||||
if dst_id in cfg._getallnodes_unlocked():
|
||||
raise NodeAlreadyExistsError(f"Destination node '{dst_id}' already exists.")
|
||||
new_uniques = cfg._explode_unique_unlocked(dst_id)
|
||||
if not new_uniques:
|
||||
raise InvalidConfigurationError(f"Invalid destination format '{dst_id}'.")
|
||||
new_node_data = deepcopy(node_data)
|
||||
new_node_data.update(new_uniques)
|
||||
cfg._connections_add_unlocked(**new_node_data)
|
||||
if not copy:
|
||||
src_uniques = cfg._explode_unique_unlocked(src_id)
|
||||
cfg._connections_del_unlocked(**src_uniques)
|
||||
|
||||
self.config.transactional_update(_mut)
|
||||
return
|
||||
|
||||
node_data = self.config.getitem(src_id)
|
||||
if not node_data:
|
||||
raise NodeNotFoundError(f"Source node '{src_id}' not found.")
|
||||
|
||||
|
||||
if dst_id in self.config._getallnodes():
|
||||
raise NodeAlreadyExistsError(f"Destination node '{dst_id}' already exists.")
|
||||
|
||||
|
||||
new_uniques = self.config._explode_unique(dst_id)
|
||||
if not new_uniques:
|
||||
raise InvalidConfigurationError(f"Invalid destination format '{dst_id}'.")
|
||||
|
||||
|
||||
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):
|
||||
"""Add multiple nodes with shared common configuration."""
|
||||
self._validate_node_payload(common_data)
|
||||
|
||||
if _supports_transactional_update(self.config):
|
||||
def _build_entries(cfg):
|
||||
all_nodes = cfg._getallnodes_unlocked()
|
||||
planned = []
|
||||
for i, uid in enumerate(ids):
|
||||
if uid in all_nodes:
|
||||
continue
|
||||
try:
|
||||
self._validate_node_name(uid)
|
||||
except ReservedNameError:
|
||||
continue
|
||||
host = hosts[i] if i < len(hosts) else hosts[0]
|
||||
uniques = cfg._explode_unique_unlocked(uid)
|
||||
if not uniques:
|
||||
continue
|
||||
node_data = deepcopy(common_data)
|
||||
node_data.pop("ids", None)
|
||||
node_data.pop("location", None)
|
||||
node_data.update(uniques)
|
||||
node_data["host"] = host
|
||||
node_data["type"] = "connection"
|
||||
planned.append(node_data)
|
||||
return planned
|
||||
|
||||
with self.config._mutation_lock:
|
||||
if not _build_entries(self.config):
|
||||
return 0
|
||||
|
||||
def _mut(cfg):
|
||||
entries = _build_entries(cfg)
|
||||
for node_data in entries:
|
||||
cfg._connections_add_unlocked(**node_data)
|
||||
return len(entries)
|
||||
|
||||
return self.config.transactional_update(_mut)
|
||||
|
||||
count = 0
|
||||
all_nodes = self.config._getallnodes()
|
||||
|
||||
|
||||
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 < len(hosts) else hosts[0]
|
||||
uniques = self.config._explode_unique(uid)
|
||||
if not uniques:
|
||||
continue
|
||||
|
||||
|
||||
node_data = common_data.copy()
|
||||
node_data.pop("ids", None)
|
||||
node_data.pop("location", None)
|
||||
@@ -256,13 +460,28 @@ class NodeService(BaseService):
|
||||
|
||||
self.config._connections_add(**node_data)
|
||||
count += 1
|
||||
|
||||
|
||||
if count > 0:
|
||||
self.config._saveconfig(self.config.file)
|
||||
return count
|
||||
|
||||
def full_replace(self, connections, profiles):
|
||||
"""Replace all connections and profiles with new data."""
|
||||
from ..auth_utils import SanitizedAuthError
|
||||
|
||||
if _supports_transactional_update(self.config):
|
||||
def _mut(cfg):
|
||||
cfg.connections.clear()
|
||||
cfg.connections.update(deepcopy(connections))
|
||||
cfg.profiles.clear()
|
||||
cfg.profiles.update(deepcopy(profiles))
|
||||
|
||||
try:
|
||||
self.config.transactional_update(_mut)
|
||||
except (ValueError, TypeError, SanitizedAuthError) as exc:
|
||||
raise InvalidConfigurationError(str(exc)) from None
|
||||
return
|
||||
|
||||
self.config.connections = connections
|
||||
self.config.profiles = profiles
|
||||
self.config._saveconfig(self.config.file)
|
||||
|
||||
@@ -1,64 +1,212 @@
|
||||
from contextlib import suppress
|
||||
from copy import deepcopy
|
||||
from typing import ClassVar
|
||||
|
||||
from .base import BaseService
|
||||
from .exceptions import ProfileNotFoundError, ProfileAlreadyExistsError, InvalidConfigurationError
|
||||
from .exceptions import (
|
||||
InvalidConfigurationError,
|
||||
ProfileAlreadyExistsError,
|
||||
ProfileNotFoundError,
|
||||
)
|
||||
|
||||
MAX_PROFILE_DEPTH = 5
|
||||
|
||||
|
||||
def _supports_transactional_update(cfg):
|
||||
return (
|
||||
cfg is not None
|
||||
and not hasattr(cfg, "assert_called")
|
||||
and callable(getattr(cfg, "transactional_update", None))
|
||||
)
|
||||
|
||||
|
||||
class ProfileService(BaseService):
|
||||
"""Business logic for node profiles management."""
|
||||
|
||||
_ALLOWED_KEYS: ClassVar[frozenset[str]] = frozenset({
|
||||
"host",
|
||||
"options",
|
||||
"logs",
|
||||
"password",
|
||||
"port",
|
||||
"protocol",
|
||||
"user",
|
||||
"tags",
|
||||
"jumphost",
|
||||
"fallback",
|
||||
})
|
||||
|
||||
@staticmethod
|
||||
def _validate_profile_payload(data):
|
||||
from ..auth_utils import (
|
||||
SanitizedAuthError,
|
||||
is_compose_expression,
|
||||
validate_auth_budget_tags,
|
||||
validate_compose_syntax,
|
||||
validate_fallback_entries,
|
||||
)
|
||||
|
||||
try:
|
||||
if "tags" in data and data["tags"] not in ("", None):
|
||||
validate_auth_budget_tags(data["tags"])
|
||||
if "fallback" in data and data["fallback"] not in (None, "", []):
|
||||
validate_fallback_entries(data["fallback"])
|
||||
pw = data.get("password")
|
||||
if is_compose_expression(pw):
|
||||
validate_compose_syntax(pw)
|
||||
elif isinstance(pw, (list, tuple)):
|
||||
for item in pw:
|
||||
if is_compose_expression(item):
|
||||
validate_compose_syntax(item)
|
||||
except (ValueError, TypeError, SanitizedAuthError) as exc:
|
||||
raise InvalidConfigurationError(str(exc)) from None
|
||||
|
||||
def _get_profiles_snapshot(self):
|
||||
lock = getattr(self.config, "_mutation_lock", None)
|
||||
if lock is not None and not hasattr(self.config, "assert_called"):
|
||||
with lock:
|
||||
return deepcopy(self.config.profiles)
|
||||
return deepcopy(self.config.profiles)
|
||||
|
||||
def list_profiles(self, filter_str=None):
|
||||
"""List all profile names, optionally filtered."""
|
||||
profiles = list(self.config.profiles.keys())
|
||||
profiles = list(self._get_profiles_snapshot().keys())
|
||||
case_sensitive = self.config.config.get("case", False)
|
||||
|
||||
|
||||
if filter_str:
|
||||
if not case_sensitive:
|
||||
f_str = filter_str.lower()
|
||||
return [p for p in profiles if f_str in p.lower()]
|
||||
else:
|
||||
return [p for p in profiles if filter_str in p]
|
||||
return [p for p in profiles if filter_str in p]
|
||||
return profiles
|
||||
|
||||
def get_profile(self, name, resolve=True):
|
||||
def get_profile(self, name, resolve=True, *, _visited=frozenset(), _depth=0):
|
||||
"""Get the profile dictionary, optionally resolved."""
|
||||
profile = self.config.profiles.get(name)
|
||||
if _depth >= MAX_PROFILE_DEPTH or name in _visited:
|
||||
raise InvalidConfigurationError(
|
||||
f"Circular or overly deep profile reference involving '{name}'."
|
||||
)
|
||||
profiles = self._get_profiles_snapshot()
|
||||
profile = profiles.get(name)
|
||||
if not profile:
|
||||
raise ProfileNotFoundError(f"Profile '{name}' not found.")
|
||||
|
||||
|
||||
if resolve:
|
||||
return self.resolve_node_data(profile)
|
||||
return self.resolve_node_data(
|
||||
profile,
|
||||
_visited=_visited | {name},
|
||||
_depth=_depth + 1,
|
||||
_profiles_snapshot=profiles,
|
||||
)
|
||||
return profile
|
||||
|
||||
def add_profile(self, name, data):
|
||||
"""Add a new profile."""
|
||||
filtered_data = {k: deepcopy(v) for k, v in data.items() if k in self._ALLOWED_KEYS}
|
||||
self._validate_profile_payload(filtered_data)
|
||||
|
||||
if _supports_transactional_update(self.config):
|
||||
def _mut(cfg):
|
||||
if name in cfg.profiles:
|
||||
raise ProfileAlreadyExistsError(f"Profile '{name}' already exists.")
|
||||
cfg._profiles_add_unlocked(id=name, **filtered_data)
|
||||
|
||||
self.config.transactional_update(_mut)
|
||||
return
|
||||
|
||||
if name in self.config.profiles:
|
||||
raise ProfileAlreadyExistsError(f"Profile '{name}' already exists.")
|
||||
|
||||
# Filter data to match _profiles_add signature and ensure id is passed
|
||||
allowed_keys = {"host", "options", "logs", "password", "port", "protocol", "user", "tags", "jumphost"}
|
||||
filtered_data = {k: v for k, v in data.items() if k in allowed_keys}
|
||||
|
||||
self.config._profiles_add(id=name, **filtered_data)
|
||||
self.config._saveconfig(self.config.file)
|
||||
|
||||
def resolve_node_data(self, node_data):
|
||||
"""Resolve profile references (@profile) in node data and handle inheritance."""
|
||||
resolved = node_data.copy()
|
||||
|
||||
# 1. Identify all referenced profiles to support inheritance
|
||||
def resolve_node_data(
|
||||
self,
|
||||
node_data,
|
||||
*,
|
||||
_visited=frozenset(),
|
||||
_depth=0,
|
||||
_profiles_snapshot=None,
|
||||
):
|
||||
"""Resolve profile references (@profile) in node data while preserving compose and scalar secrets."""
|
||||
from ..auth_utils import is_compose_expression
|
||||
|
||||
if _depth > MAX_PROFILE_DEPTH:
|
||||
raise InvalidConfigurationError("Profile inheritance exceeds maximum depth.")
|
||||
|
||||
profiles = (
|
||||
_profiles_snapshot
|
||||
if _profiles_snapshot is not None
|
||||
else self._get_profiles_snapshot()
|
||||
)
|
||||
resolved = deepcopy(node_data)
|
||||
|
||||
referenced_profiles = []
|
||||
for value in resolved.values():
|
||||
if isinstance(value, str) and value.startswith("@"):
|
||||
referenced_profiles.append(value[1:])
|
||||
for key, value in resolved.items():
|
||||
if key == "fallback":
|
||||
continue
|
||||
if isinstance(value, str):
|
||||
if not is_compose_expression(value) and value.startswith("@"):
|
||||
ref = value[1:]
|
||||
if key != "password" or ref in profiles:
|
||||
referenced_profiles.append(ref)
|
||||
elif isinstance(value, list):
|
||||
for item in value:
|
||||
if isinstance(item, str) and item.startswith("@"):
|
||||
referenced_profiles.append(item[1:])
|
||||
|
||||
# 2. Resolve explicit references
|
||||
for key, value in resolved.items():
|
||||
if (
|
||||
isinstance(item, str)
|
||||
and not is_compose_expression(item)
|
||||
and item.startswith("@")
|
||||
):
|
||||
ref = item[1:]
|
||||
if key != "password" or ref in profiles:
|
||||
referenced_profiles.append(ref)
|
||||
|
||||
def _get_resolved_ref(prof_name):
|
||||
if prof_name in _visited or _depth >= MAX_PROFILE_DEPTH:
|
||||
raise InvalidConfigurationError(
|
||||
f"Circular or overly deep profile reference involving '{prof_name}'."
|
||||
)
|
||||
prof_raw = profiles.get(prof_name)
|
||||
if not prof_raw:
|
||||
raise ProfileNotFoundError(f"Profile '{prof_name}' not found.")
|
||||
return self.resolve_node_data(
|
||||
prof_raw,
|
||||
_visited=_visited | {prof_name},
|
||||
_depth=_depth + 1,
|
||||
_profiles_snapshot=profiles,
|
||||
)
|
||||
|
||||
for key, value in list(resolved.items()):
|
||||
if key == "fallback":
|
||||
continue
|
||||
if key == "password":
|
||||
if isinstance(value, str):
|
||||
if is_compose_expression(value):
|
||||
continue
|
||||
if value.startswith("@") and value[1:] in profiles:
|
||||
prof_obj = _get_resolved_ref(value[1:])
|
||||
resolved["password"] = prof_obj.get("password", "")
|
||||
elif isinstance(value, list):
|
||||
resolved_list = []
|
||||
for item in value:
|
||||
if (
|
||||
isinstance(item, str)
|
||||
and not is_compose_expression(item)
|
||||
and item.startswith("@")
|
||||
and item[1:] in profiles
|
||||
):
|
||||
with suppress(ProfileNotFoundError):
|
||||
prof_obj = _get_resolved_ref(item[1:])
|
||||
if "password" in prof_obj:
|
||||
resolved_list.append(prof_obj["password"])
|
||||
else:
|
||||
resolved_list.append(item)
|
||||
resolved["password"] = resolved_list
|
||||
continue
|
||||
|
||||
if isinstance(value, str) and value.startswith("@"):
|
||||
profile_name = value[1:]
|
||||
try:
|
||||
profile = self.get_profile(profile_name, resolve=True)
|
||||
profile = _get_resolved_ref(profile_name)
|
||||
resolved[key] = profile.get(key, "")
|
||||
except ProfileNotFoundError:
|
||||
resolved[key] = ""
|
||||
@@ -67,68 +215,85 @@ class ProfileService(BaseService):
|
||||
for item in value:
|
||||
if isinstance(item, str) and item.startswith("@"):
|
||||
profile_name = item[1:]
|
||||
try:
|
||||
profile = self.get_profile(profile_name, resolve=True)
|
||||
with suppress(ProfileNotFoundError):
|
||||
profile = _get_resolved_ref(profile_name)
|
||||
if "password" in profile:
|
||||
resolved_list.append(profile["password"])
|
||||
except ProfileNotFoundError:
|
||||
pass
|
||||
else:
|
||||
resolved_list.append(item)
|
||||
resolved[key] = resolved_list
|
||||
|
||||
# 3. Inheritance: Fill empty keys from the first referenced profile
|
||||
|
||||
if referenced_profiles:
|
||||
base_profile_name = referenced_profiles[0]
|
||||
try:
|
||||
base_profile = self.get_profile(base_profile_name, resolve=True)
|
||||
with suppress(ProfileNotFoundError):
|
||||
base_profile = _get_resolved_ref(base_profile_name)
|
||||
for key, value in base_profile.items():
|
||||
# Fill if key is missing or empty
|
||||
if key not in resolved or resolved[key] == "" or resolved[key] == [] or resolved[key] is None:
|
||||
resolved[key] = value
|
||||
except ProfileNotFoundError:
|
||||
pass
|
||||
if key not in resolved or resolved[key] in ("", [], None):
|
||||
resolved[key] = deepcopy(value)
|
||||
|
||||
# 4. Handle default protocol
|
||||
if resolved.get("protocol") == "" or resolved.get("protocol") is None:
|
||||
if resolved.get("protocol") in ("", None):
|
||||
try:
|
||||
default_profile = self.get_profile("default", resolve=True)
|
||||
resolved["protocol"] = default_profile.get("protocol", "ssh")
|
||||
default_profile = _get_resolved_ref("default")
|
||||
resolved["protocol"] = default_profile.get("protocol", "ssh") or "ssh"
|
||||
except ProfileNotFoundError:
|
||||
resolved["protocol"] = "ssh"
|
||||
|
||||
|
||||
return resolved
|
||||
|
||||
def delete_profile(self, name):
|
||||
"""Delete an existing profile, with safety checks."""
|
||||
if _supports_transactional_update(self.config):
|
||||
def _mut(cfg):
|
||||
if name not in cfg.profiles:
|
||||
raise ProfileNotFoundError(f"Profile '{name}' not found.")
|
||||
if name == "default":
|
||||
raise InvalidConfigurationError("Cannot delete the 'default' profile.")
|
||||
used_by = cfg._profileused_unlocked(name)
|
||||
if used_by:
|
||||
raise InvalidConfigurationError(
|
||||
f"Profile '{name}' is used by nodes: {', '.join(used_by)}"
|
||||
)
|
||||
cfg._profiles_del_unlocked(id=name)
|
||||
|
||||
self.config.transactional_update(_mut)
|
||||
return
|
||||
|
||||
if name not in self.config.profiles:
|
||||
raise ProfileNotFoundError(f"Profile '{name}' not found.")
|
||||
|
||||
if name == "default":
|
||||
raise InvalidConfigurationError("Cannot delete the 'default' profile.")
|
||||
|
||||
used_by = self.config._profileused(name)
|
||||
if used_by:
|
||||
# We return the list of nodes using it so the UI can inform the user
|
||||
raise InvalidConfigurationError(f"Profile '{name}' is used by nodes: {', '.join(used_by)}")
|
||||
|
||||
raise InvalidConfigurationError(
|
||||
f"Profile '{name}' is used by nodes: {', '.join(used_by)}"
|
||||
)
|
||||
self.config._profiles_del(id=name)
|
||||
self.config._saveconfig(self.config.file)
|
||||
|
||||
def update_profile(self, name, data):
|
||||
"""Update an existing profile."""
|
||||
if _supports_transactional_update(self.config):
|
||||
def _mut(cfg):
|
||||
if name not in cfg.profiles:
|
||||
raise ProfileNotFoundError(f"Profile '{name}' not found.")
|
||||
updated_data = deepcopy(cfg.profiles[name])
|
||||
updated_data.update(deepcopy(data))
|
||||
filtered_data = {
|
||||
k: v for k, v in updated_data.items() if k in self._ALLOWED_KEYS
|
||||
}
|
||||
self._validate_profile_payload(filtered_data)
|
||||
cfg._profiles_add_unlocked(id=name, **filtered_data)
|
||||
|
||||
self.config.transactional_update(_mut)
|
||||
return
|
||||
|
||||
if name not in self.config.profiles:
|
||||
raise ProfileNotFoundError(f"Profile '{name}' not found.")
|
||||
|
||||
# Merge with existing data
|
||||
existing = self.get_profile(name, resolve=False)
|
||||
updated_data = existing.copy()
|
||||
updated_data = deepcopy(existing)
|
||||
updated_data.update(data)
|
||||
|
||||
# Filter data to match _profiles_add signature
|
||||
allowed_keys = {"host", "options", "logs", "password", "port", "protocol", "user", "tags", "jumphost"}
|
||||
filtered_data = {k: v for k, v in updated_data.items() if k in allowed_keys}
|
||||
|
||||
filtered_data = {k: v for k, v in updated_data.items() if k in self._ALLOWED_KEYS}
|
||||
self._validate_profile_payload(filtered_data)
|
||||
self.config._profiles_add(id=name, **filtered_data)
|
||||
self.config._saveconfig(self.config.file)
|
||||
|
||||
+36
-18
@@ -1,5 +1,9 @@
|
||||
import inspect
|
||||
from contextlib import suppress
|
||||
|
||||
from .exceptions import InvalidConfigurationError
|
||||
|
||||
|
||||
class RemoteStub:
|
||||
def __getattr__(self, name):
|
||||
raise NotImplementedError(
|
||||
@@ -7,13 +11,17 @@ class RemoteStub:
|
||||
"Use local mode or wait for the gRPC implementation."
|
||||
)
|
||||
|
||||
|
||||
class ServiceProvider:
|
||||
"""Dynamic service backend. Transparently provides local or remote services."""
|
||||
|
||||
|
||||
def __init__(self, config, mode="local", remote_host=None):
|
||||
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
|
||||
@@ -29,47 +37,54 @@ class ServiceProvider:
|
||||
raise ValueError(f"Unknown service mode: {mode}")
|
||||
|
||||
def _init_local(self):
|
||||
from .node_service import NodeService
|
||||
from .profile_service import ProfileService
|
||||
from .config_service import ConfigService
|
||||
from .plugin_service import PluginService
|
||||
from .context_service import ContextService
|
||||
|
||||
from .node_service import NodeService
|
||||
from .plugin_service import PluginService
|
||||
from .profile_service import ProfileService
|
||||
|
||||
self.nodes = NodeService(self.config)
|
||||
self.profiles = ProfileService(self.config)
|
||||
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("Remote host must be specified in remote mode")
|
||||
|
||||
import grpc
|
||||
import os
|
||||
|
||||
import grpc
|
||||
|
||||
from ..grpc_layer.stubs import (
|
||||
NodeStub, ProfileStub, PluginStub, AIStub,
|
||||
ExecutionStub, ImportExportStub, SystemStub,
|
||||
ConfigStub, AuthClientInterceptor, AuthStub
|
||||
AIStub,
|
||||
AuthClientInterceptor,
|
||||
AuthStub,
|
||||
ConfigStub,
|
||||
ExecutionStub,
|
||||
ImportExportStub,
|
||||
NodeStub,
|
||||
PluginStub,
|
||||
ProfileStub,
|
||||
SystemStub,
|
||||
)
|
||||
|
||||
|
||||
def get_token():
|
||||
env_token = os.environ.get("CONNPY_TOKEN")
|
||||
if env_token:
|
||||
return env_token
|
||||
token_path = os.path.join(self.config.defaultdir, ".token")
|
||||
if os.path.exists(token_path):
|
||||
try:
|
||||
with open(token_path, "r") as f:
|
||||
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)
|
||||
@@ -105,7 +120,10 @@ class ServiceProvider:
|
||||
def execution(self):
|
||||
if self._execution is None and self.mode == "local":
|
||||
from .execution_service import ExecutionService
|
||||
self._execution = ExecutionService(self.config)
|
||||
if "attempt_tracker" in inspect.signature(ExecutionService.__init__).parameters:
|
||||
self._execution = ExecutionService(self.config, attempt_tracker=self.auth_tracker)
|
||||
else:
|
||||
self._execution = ExecutionService(self.config)
|
||||
return self._execution
|
||||
|
||||
@execution.setter
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user