feat(spec-05-auth-fallback-chain): worker (eng_flash_high) implementation
This commit is contained in:
1 parent
ef6d1da5e5
commit
79d28ee6c3
14 files changed
+5447
-564
No files matched your search
File diff suppressed because it is too large.
Load diff
@@ -1,9 +1,22 @@
|
|||||||
|
import re
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
from .. import printer
|
from .. import printer
|
||||||
|
from ..auth_utils import (
|
||||||
|
MAX_AUTH_PROMPTS_CAP,
|
||||||
|
MAX_HOP_HOST_ATTEMPTS_CAP,
|
||||||
|
MAX_TOTAL_AUTH_ATTEMPTS_CAP,
|
||||||
|
MIN_AUTH_PROMPTS,
|
||||||
|
MIN_HOP_HOST_ATTEMPTS,
|
||||||
|
MIN_TOTAL_AUTH_ATTEMPTS,
|
||||||
|
validate_strict_int_range,
|
||||||
|
)
|
||||||
from ..services.exceptions import ConnpyError, InvalidConfigurationError
|
from ..services.exceptions import ConnpyError, InvalidConfigurationError
|
||||||
from .help_text import get_instructions
|
from .help_text import get_instructions
|
||||||
|
|
||||||
|
|
||||||
class ConfigHandler:
|
class ConfigHandler:
|
||||||
def __init__(self, app):
|
def __init__(self, app):
|
||||||
self.app = app
|
self.app = app
|
||||||
@@ -29,7 +42,12 @@ class ConfigHandler:
|
|||||||
"sync_remote": self.set_sync_remote,
|
"sync_remote": self.set_sync_remote,
|
||||||
"shell_command": self.set_shell_config,
|
"shell_command": self.set_shell_config,
|
||||||
"shell_prompt": self.set_shell_config,
|
"shell_prompt": self.set_shell_config,
|
||||||
"shell_os": self.set_shell_config
|
"shell_os": self.set_shell_config,
|
||||||
|
"max_auth_prompts": self.set_auth_budget,
|
||||||
|
"max_total_auth_attempts": self.set_auth_budget,
|
||||||
|
"max_hop_host_attempts": self.set_auth_budget,
|
||||||
|
"max_global_user_attempts": self.set_auth_budget,
|
||||||
|
"disable_fallback_on_insecure_hostkey": self.set_disable_fallback_on_insecure_hostkey,
|
||||||
}
|
}
|
||||||
handler = actions.get(getattr(args, "command", None))
|
handler = actions.get(getattr(args, "command", None))
|
||||||
if handler:
|
if handler:
|
||||||
@@ -202,3 +220,67 @@ class ConfigHandler:
|
|||||||
except (ConnpyError, InvalidConfigurationError) as e:
|
except (ConnpyError, InvalidConfigurationError) as e:
|
||||||
printer.error(str(e))
|
printer.error(str(e))
|
||||||
|
|
||||||
|
def set_auth_budget(self, args):
|
||||||
|
key = args.command
|
||||||
|
raw_val = args.data[0] if isinstance(args.data, list) else args.data
|
||||||
|
bounds_map = {
|
||||||
|
"max_auth_prompts": (MIN_AUTH_PROMPTS, MAX_AUTH_PROMPTS_CAP),
|
||||||
|
"max_total_auth_attempts": (MIN_TOTAL_AUTH_ATTEMPTS, MAX_TOTAL_AUTH_ATTEMPTS_CAP),
|
||||||
|
"max_hop_host_attempts": (MIN_HOP_HOST_ATTEMPTS, MAX_HOP_HOST_ATTEMPTS_CAP),
|
||||||
|
"max_global_user_attempts": (1, 1000),
|
||||||
|
}
|
||||||
|
if key not in bounds_map:
|
||||||
|
raise InvalidConfigurationError(f"Unknown auth budget setting: {key}")
|
||||||
|
|
||||||
|
if isinstance(raw_val, bool):
|
||||||
|
msg = f"Invalid {key}: boolean is not permitted"
|
||||||
|
printer.error(msg)
|
||||||
|
raise InvalidConfigurationError(msg)
|
||||||
|
|
||||||
|
if isinstance(raw_val, int):
|
||||||
|
val = raw_val
|
||||||
|
elif isinstance(raw_val, str) and re.fullmatch(r"[+-]?\d+", raw_val.strip()):
|
||||||
|
val = int(raw_val.strip())
|
||||||
|
else:
|
||||||
|
msg = f"Invalid {key}: expected integer, got {raw_val!r}"
|
||||||
|
printer.error(msg)
|
||||||
|
raise InvalidConfigurationError(msg)
|
||||||
|
|
||||||
|
if val == 0:
|
||||||
|
cfg_obj = self.app.config
|
||||||
|
if not hasattr(cfg_obj, "assert_called") and callable(
|
||||||
|
getattr(cfg_obj, "transactional_update", None)
|
||||||
|
):
|
||||||
|
cfg_obj.transactional_update(lambda c: c.config.pop(key, None))
|
||||||
|
else:
|
||||||
|
cfg_obj.config.pop(key, None)
|
||||||
|
cfg_obj._saveconfig(cfg_obj.file)
|
||||||
|
printer.success("Config saved")
|
||||||
|
return
|
||||||
|
|
||||||
|
min_v, max_v = bounds_map[key]
|
||||||
|
try:
|
||||||
|
validate_strict_int_range(val, key, min_v, max_v)
|
||||||
|
except ValueError as exc:
|
||||||
|
printer.error(str(exc))
|
||||||
|
raise InvalidConfigurationError(str(exc)) from None
|
||||||
|
|
||||||
|
self.app.services.config_svc.update_setting(key, val)
|
||||||
|
printer.success("Config saved")
|
||||||
|
|
||||||
|
def set_disable_fallback_on_insecure_hostkey(self, args):
|
||||||
|
raw_val = args.data[0] if isinstance(args.data, list) else args.data
|
||||||
|
if isinstance(raw_val, bool):
|
||||||
|
val = raw_val
|
||||||
|
elif isinstance(raw_val, str) and raw_val.strip().lower() in ("true", "false"):
|
||||||
|
val = raw_val.strip().lower() == "true"
|
||||||
|
else:
|
||||||
|
msg = "Invalid disable_fallback_on_insecure_hostkey: expected 'true' or 'false'"
|
||||||
|
printer.error(msg)
|
||||||
|
raise InvalidConfigurationError(msg)
|
||||||
|
self.app.services.config_svc.update_setting(
|
||||||
|
"disable_fallback_on_insecure_hostkey",
|
||||||
|
val,
|
||||||
|
)
|
||||||
|
printer.success("Config saved")
|
||||||
|
|
||||||
+100
-13
@@ -1,6 +1,70 @@
|
|||||||
import ast
|
import ast
|
||||||
|
import json
|
||||||
|
|
||||||
|
from ..auth_utils import (
|
||||||
|
MAX_FALLBACK_ENTRIES,
|
||||||
|
MAX_JSON_INPUT_BYTES,
|
||||||
|
MAX_JSON_INPUT_DEPTH,
|
||||||
|
validate_auth_budget_tags,
|
||||||
|
validate_fallback_entries,
|
||||||
|
validate_json_depth_and_size,
|
||||||
|
)
|
||||||
from .validators import Validators
|
from .validators import Validators
|
||||||
|
|
||||||
|
|
||||||
|
def _parse_tags_input(raw_tags):
|
||||||
|
if raw_tags in ("", None):
|
||||||
|
return ""
|
||||||
|
if isinstance(raw_tags, dict):
|
||||||
|
validate_json_depth_and_size(
|
||||||
|
raw_tags,
|
||||||
|
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||||
|
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||||
|
)
|
||||||
|
validate_auth_budget_tags(raw_tags)
|
||||||
|
return raw_tags
|
||||||
|
s = str(raw_tags)
|
||||||
|
if s.startswith("@"):
|
||||||
|
return s
|
||||||
|
if len(s.encode("utf-8", errors="replace")) > MAX_JSON_INPUT_BYTES:
|
||||||
|
raise ValueError(f"Tags exceed maximum size of {MAX_JSON_INPUT_BYTES} bytes")
|
||||||
|
try:
|
||||||
|
parsed = json.loads(s)
|
||||||
|
except (ValueError, TypeError):
|
||||||
|
parsed = ast.literal_eval(s)
|
||||||
|
validate_json_depth_and_size(
|
||||||
|
parsed,
|
||||||
|
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||||
|
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||||
|
)
|
||||||
|
validate_auth_budget_tags(parsed)
|
||||||
|
return parsed
|
||||||
|
|
||||||
|
|
||||||
|
def _parse_fallback_input(raw_fb):
|
||||||
|
if raw_fb in (None, "", []):
|
||||||
|
return ""
|
||||||
|
if isinstance(raw_fb, str):
|
||||||
|
stripped = raw_fb.strip()
|
||||||
|
if not stripped:
|
||||||
|
return ""
|
||||||
|
if stripped.startswith(("[", "{")):
|
||||||
|
parsed = validate_json_depth_and_size(
|
||||||
|
stripped,
|
||||||
|
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||||
|
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||||
|
)
|
||||||
|
return validate_fallback_entries(parsed, max_entries=MAX_FALLBACK_ENTRIES)
|
||||||
|
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:
|
class Forms:
|
||||||
def __init__(self, app):
|
def __init__(self, app):
|
||||||
self.app = app
|
self.app = app
|
||||||
@@ -18,6 +82,7 @@ class Forms:
|
|||||||
questions.append(inquirer.Confirm("jumphost", message="Edit jumphost?"))
|
questions.append(inquirer.Confirm("jumphost", message="Edit jumphost?"))
|
||||||
questions.append(inquirer.Confirm("user", message="Edit User?"))
|
questions.append(inquirer.Confirm("user", message="Edit User?"))
|
||||||
questions.append(inquirer.Confirm("password", message="Edit password?"))
|
questions.append(inquirer.Confirm("password", message="Edit password?"))
|
||||||
|
questions.append(inquirer.Confirm("fallback", message="Edit fallback credentials?"))
|
||||||
return inquirer.prompt(questions)
|
return inquirer.prompt(questions)
|
||||||
|
|
||||||
def questions_nodes(self, unique, uniques=None, edit=None):
|
def questions_nodes(self, unique, uniques=None, edit=None):
|
||||||
@@ -28,11 +93,13 @@ class Forms:
|
|||||||
defaults["tags"] = ""
|
defaults["tags"] = ""
|
||||||
if "jumphost" not in defaults:
|
if "jumphost" not in defaults:
|
||||||
defaults["jumphost"] = ""
|
defaults["jumphost"] = ""
|
||||||
except Exception:
|
if "fallback" not in defaults:
|
||||||
defaults = {"host": "", "protocol": "", "port": "", "user": "", "options": "", "logs": "", "tags": "", "password": "", "jumphost": ""}
|
defaults["fallback"] = ""
|
||||||
|
except Exception: # noqa: BLE001
|
||||||
|
defaults = {"host": "", "protocol": "", "port": "", "user": "", "options": "", "logs": "", "tags": "", "password": "", "jumphost": "", "fallback": ""}
|
||||||
node = {}
|
node = {}
|
||||||
if edit is None:
|
if edit is None:
|
||||||
edit = {"host": True, "protocol": True, "port": True, "user": True, "password": True, "options": True, "logs": True, "tags": True, "jumphost": True}
|
edit = {"host": True, "protocol": True, "port": True, "user": True, "password": True, "options": True, "logs": True, "tags": True, "jumphost": True, "fallback": True}
|
||||||
questions = []
|
questions = []
|
||||||
if edit["host"]:
|
if edit["host"]:
|
||||||
questions.append(inquirer.Text("host", message="Add Hostname or IP", validate=self.validators.host_validation, default=defaults["host"]))
|
questions.append(inquirer.Text("host", message="Add Hostname or IP", validate=self.validators.host_validation, default=defaults["host"]))
|
||||||
@@ -70,6 +137,12 @@ class Forms:
|
|||||||
questions.append(inquirer.List("password", message="Password: Use a local password, no password or a list of profiles to reference?", choices=["Local Password", "Profiles", "No Password"]))
|
questions.append(inquirer.List("password", message="Password: Use a local password, no password or a list of profiles to reference?", choices=["Local Password", "Profiles", "No Password"]))
|
||||||
else:
|
else:
|
||||||
node["password"] = defaults["password"]
|
node["password"] = defaults["password"]
|
||||||
|
if 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)
|
answer = inquirer.prompt(questions)
|
||||||
if answer is None:
|
if answer is None:
|
||||||
@@ -91,8 +164,10 @@ class Forms:
|
|||||||
elif answer["password"] == "No Password":
|
elif answer["password"] == "No Password":
|
||||||
answer["password"] = ""
|
answer["password"] = ""
|
||||||
|
|
||||||
if "tags" in answer and not answer["tags"].startswith("@") and answer["tags"]:
|
if "tags" in answer and answer["tags"] and not str(answer["tags"]).startswith("@"):
|
||||||
answer["tags"] = ast.literal_eval(answer["tags"])
|
answer["tags"] = _parse_tags_input(answer["tags"])
|
||||||
|
if "fallback" in answer:
|
||||||
|
answer["fallback"] = _parse_fallback_input(answer["fallback"])
|
||||||
|
|
||||||
result = {**uniques, **answer, **node}
|
result = {**uniques, **answer, **node}
|
||||||
result["type"] = "connection"
|
result["type"] = "connection"
|
||||||
@@ -106,11 +181,13 @@ class Forms:
|
|||||||
defaults["tags"] = ""
|
defaults["tags"] = ""
|
||||||
if "jumphost" not in defaults:
|
if "jumphost" not in defaults:
|
||||||
defaults["jumphost"] = ""
|
defaults["jumphost"] = ""
|
||||||
except Exception:
|
if "fallback" not in defaults:
|
||||||
defaults = {"host": "", "protocol": "", "port": "", "user": "", "options": "", "logs": "", "tags": "", "jumphost": ""}
|
defaults["fallback"] = ""
|
||||||
|
except Exception: # noqa: BLE001
|
||||||
|
defaults = {"host": "", "protocol": "", "port": "", "user": "", "options": "", "logs": "", "tags": "", "jumphost": "", "fallback": ""}
|
||||||
profile = {}
|
profile = {}
|
||||||
if edit is None:
|
if edit is None:
|
||||||
edit = {"host": True, "protocol": True, "port": True, "user": True, "password": True, "options": True, "logs": True, "tags": True, "jumphost": True}
|
edit = {"host": True, "protocol": True, "port": True, "user": True, "password": True, "options": True, "logs": True, "tags": True, "jumphost": True, "fallback": True}
|
||||||
questions = []
|
questions = []
|
||||||
if edit["host"]:
|
if edit["host"]:
|
||||||
questions.append(inquirer.Text("host", message="Add Hostname or IP", default=defaults["host"]))
|
questions.append(inquirer.Text("host", message="Add Hostname or IP", default=defaults["host"]))
|
||||||
@@ -148,17 +225,24 @@ class Forms:
|
|||||||
questions.append(inquirer.Password("password", message="Set Password"))
|
questions.append(inquirer.Password("password", message="Set Password"))
|
||||||
else:
|
else:
|
||||||
profile["password"] = defaults["password"]
|
profile["password"] = defaults["password"]
|
||||||
|
if edit.get("fallback"):
|
||||||
|
raw_def_fb = defaults.get("fallback", "")
|
||||||
|
def_fb_str = json.dumps(raw_def_fb) if isinstance(raw_def_fb, (list, dict)) else str(raw_def_fb or "")
|
||||||
|
questions.append(inquirer.Text("fallback", message="Add fallback credentials (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)
|
answer = inquirer.prompt(questions)
|
||||||
if answer is None:
|
if answer is None:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
if "password" in answer:
|
if "password" in answer and answer["password"] != "":
|
||||||
if answer["password"] != "":
|
|
||||||
answer["password"] = self.app.services.config_svc.encrypt_password(answer["password"])
|
answer["password"] = self.app.services.config_svc.encrypt_password(answer["password"])
|
||||||
|
|
||||||
if "tags" in answer and answer["tags"]:
|
if "tags" in answer and answer["tags"]:
|
||||||
answer["tags"] = ast.literal_eval(answer["tags"])
|
answer["tags"] = _parse_tags_input(answer["tags"])
|
||||||
|
if "fallback" in answer:
|
||||||
|
answer["fallback"] = _parse_fallback_input(answer["fallback"])
|
||||||
|
|
||||||
result = {**answer, **profile}
|
result = {**answer, **profile}
|
||||||
result["id"] = unique
|
result["id"] = unique
|
||||||
@@ -178,6 +262,7 @@ class Forms:
|
|||||||
questions.append(inquirer.Text("jumphost", message="Add Jumphost node", validate=self.validators.jumphost_validation))
|
questions.append(inquirer.Text("jumphost", message="Add Jumphost node", validate=self.validators.jumphost_validation))
|
||||||
questions.append(inquirer.Text("user", message="Pick username", validate=self.validators.default_validation))
|
questions.append(inquirer.Text("user", message="Pick username", validate=self.validators.default_validation))
|
||||||
questions.append(inquirer.List("password", message="Password: Use a local password, no password or a list of profiles to reference?", choices=["Local Password", "Profiles", "No Password"]))
|
questions.append(inquirer.List("password", message="Password: Use a local password, no password or a list of profiles to reference?", choices=["Local Password", "Profiles", "No Password"]))
|
||||||
|
questions.append(inquirer.Text("fallback", message="Add fallback credentials (JSON list/dict, @profile, or compose:)", validate=self.validators.fallback_validation))
|
||||||
|
|
||||||
answer = inquirer.prompt(questions)
|
answer = inquirer.prompt(questions)
|
||||||
if answer is None:
|
if answer is None:
|
||||||
@@ -196,8 +281,10 @@ class Forms:
|
|||||||
answer["password"] = ""
|
answer["password"] = ""
|
||||||
|
|
||||||
answer["type"] = "connection"
|
answer["type"] = "connection"
|
||||||
if "tags" in answer and not answer["tags"].startswith("@") and answer["tags"]:
|
if "tags" in answer and answer["tags"] and not str(answer["tags"]).startswith("@"):
|
||||||
answer["tags"] = ast.literal_eval(answer["tags"])
|
answer["tags"] = _parse_tags_input(answer["tags"])
|
||||||
|
if "fallback" in answer:
|
||||||
|
answer["fallback"] = _parse_fallback_input(answer["fallback"])
|
||||||
|
|
||||||
return answer
|
return answer
|
||||||
|
|
||||||
|
|||||||
+73
-14
@@ -1,5 +1,19 @@
|
|||||||
import re
|
|
||||||
import ast
|
import ast
|
||||||
|
import json
|
||||||
|
import re
|
||||||
|
|
||||||
|
from ..auth_utils import (
|
||||||
|
MAX_FALLBACK_ENTRIES,
|
||||||
|
MAX_JSON_INPUT_BYTES,
|
||||||
|
MAX_JSON_INPUT_DEPTH,
|
||||||
|
SanitizedAuthError,
|
||||||
|
is_compose_expression,
|
||||||
|
validate_auth_budget_tags,
|
||||||
|
validate_compose_syntax,
|
||||||
|
validate_fallback_entries,
|
||||||
|
validate_json_depth_and_size,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def _raise_val_err(reason):
|
def _raise_val_err(reason):
|
||||||
import inquirer
|
import inquirer
|
||||||
@@ -56,35 +70,80 @@ class Validators:
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
def pass_validation(self, answers, current, regex = "(^@.+$)"):
|
def pass_validation(self, answers, current, regex = "(^@.+$)"):
|
||||||
|
if is_compose_expression(current):
|
||||||
|
try:
|
||||||
|
validate_compose_syntax(current)
|
||||||
|
except SanitizedAuthError as exc:
|
||||||
|
_raise_val_err(str(exc))
|
||||||
|
return True
|
||||||
profiles = current.split(",")
|
profiles = current.split(",")
|
||||||
for i in profiles:
|
for i in profiles:
|
||||||
if not re.match(regex, i) or i[1:] not in self.app.profiles:
|
if not re.match(regex, i) or i[1:] not in self.app.profiles:
|
||||||
_raise_val_err("Profile {} don't exist".format(i))
|
_raise_val_err("Profile {} don't exist".format(i))
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
def _validate_tags_dict(self, current: str):
|
||||||
|
if len(current.encode("utf-8", errors="replace")) > MAX_JSON_INPUT_BYTES:
|
||||||
|
_raise_val_err(f"Tags exceed maximum size of {MAX_JSON_INPUT_BYTES} bytes")
|
||||||
|
parsed = None
|
||||||
|
try:
|
||||||
|
parsed = json.loads(current)
|
||||||
|
except (ValueError, TypeError):
|
||||||
|
try:
|
||||||
|
parsed = ast.literal_eval(current)
|
||||||
|
except (ValueError, TypeError, SyntaxError, MemoryError, RecursionError):
|
||||||
|
parsed = None
|
||||||
|
if not isinstance(parsed, dict):
|
||||||
|
_raise_val_err("Tags should be a python dictionary.")
|
||||||
|
try:
|
||||||
|
validate_json_depth_and_size(
|
||||||
|
parsed,
|
||||||
|
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||||
|
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||||
|
)
|
||||||
|
validate_auth_budget_tags(parsed)
|
||||||
|
except (ValueError, TypeError, SanitizedAuthError) as exc:
|
||||||
|
_raise_val_err(str(exc))
|
||||||
|
return True
|
||||||
|
|
||||||
def tags_validation(self, answers, current):
|
def tags_validation(self, answers, current):
|
||||||
if current.startswith("@"):
|
if current.startswith("@"):
|
||||||
if current[1:] not in self.app.profiles:
|
if current[1:] not in self.app.profiles:
|
||||||
_raise_val_err("Profile {} don't exist".format(current))
|
_raise_val_err("Profile {} don't exist".format(current))
|
||||||
elif current != "":
|
elif current != "":
|
||||||
isdict = False
|
return self._validate_tags_dict(current)
|
||||||
try:
|
|
||||||
isdict = ast.literal_eval(current)
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
if not isinstance (isdict, dict):
|
|
||||||
_raise_val_err("Tags should be a python dictionary.".format(current))
|
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def profile_tags_validation(self, answers, current):
|
def profile_tags_validation(self, answers, current):
|
||||||
if current != "":
|
if current != "":
|
||||||
isdict = False
|
return self._validate_tags_dict(current)
|
||||||
|
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:
|
try:
|
||||||
isdict = ast.literal_eval(current)
|
if is_compose_expression(stripped):
|
||||||
except Exception:
|
validate_compose_syntax(stripped)
|
||||||
pass
|
return True
|
||||||
if not isinstance (isdict, dict):
|
if stripped.startswith(("[", "{")):
|
||||||
_raise_val_err("Tags should be a python dictionary.".format(current))
|
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
|
return True
|
||||||
|
|
||||||
def jumphost_validation(self, answers, current):
|
def jumphost_validation(self, answers, current):
|
||||||
|
|||||||
+596
-195
@@ -1,15 +1,30 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
#Imports
|
|
||||||
import json
|
import json
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import sys
|
|
||||||
import yaml
|
|
||||||
import shutil
|
import shutil
|
||||||
from pathlib import Path
|
import sys
|
||||||
|
import tempfile
|
||||||
|
import threading
|
||||||
|
from contextlib import suppress
|
||||||
from copy import deepcopy
|
from copy import deepcopy
|
||||||
from .hooks import MethodHook, ClassHook
|
from pathlib import Path
|
||||||
|
|
||||||
|
import yaml
|
||||||
|
|
||||||
from . import printer
|
from . import printer
|
||||||
|
from .auth_utils import (
|
||||||
|
SanitizedAuthError,
|
||||||
|
extract_profile_refs_from_value,
|
||||||
|
is_compose_expression,
|
||||||
|
resolve_fallback_entries,
|
||||||
|
validate_auth_budget_tags,
|
||||||
|
validate_compose_syntax,
|
||||||
|
validate_config_auth_settings,
|
||||||
|
validate_fallback_entries,
|
||||||
|
)
|
||||||
|
from .hooks import ClassHook, MethodHook
|
||||||
|
|
||||||
|
|
||||||
class NoAliasDumper(yaml.SafeDumper):
|
class NoAliasDumper(yaml.SafeDumper):
|
||||||
def ignore_aliases(self, data):
|
def ignore_aliases(self, data):
|
||||||
@@ -42,6 +57,8 @@ class configfile:
|
|||||||
'''
|
'''
|
||||||
|
|
||||||
def __init__(self, conf = None, key = None, shared_config = None):
|
def __init__(self, conf = None, key = None, shared_config = None):
|
||||||
|
self._mutation_lock = threading.RLock()
|
||||||
|
self._cache_dirty = False
|
||||||
self._shared_config = shared_config
|
self._shared_config = shared_config
|
||||||
'''
|
'''
|
||||||
|
|
||||||
@@ -109,10 +126,10 @@ class configfile:
|
|||||||
json.dump(old_data, f)
|
json.dump(old_data, f)
|
||||||
shutil.move(legacy_file, legacy_file + ".backup")
|
shutil.move(legacy_file, legacy_file + ".backup")
|
||||||
printer.success(f"Migrated legacy config ({len(old_data.get('connections',{}))} folders/nodes) into YAML and Cache successfully!")
|
printer.success(f"Migrated legacy config ({len(old_data.get('connections',{}))} folders/nodes) into YAML and Cache successfully!")
|
||||||
except Exception as e:
|
except Exception as e: # noqa: BLE001
|
||||||
if os.path.exists(self.file):
|
if os.path.exists(self.file):
|
||||||
try: os.remove(self.file)
|
with suppress(OSError):
|
||||||
except OSError: pass
|
os.remove(self.file)
|
||||||
printer.warning(f"Failed to migrate legacy config: {e}")
|
printer.warning(f"Failed to migrate legacy config: {e}")
|
||||||
else:
|
else:
|
||||||
# Custom path (common in tests): isolate everything to the conf parent directory
|
# Custom path (common in tests): isolate everything to the conf parent directory
|
||||||
@@ -207,12 +224,99 @@ class configfile:
|
|||||||
return val if val is not None else default
|
return val if val is not None else default
|
||||||
|
|
||||||
|
|
||||||
|
def _validate_config_entry_unlocked(self, entry):
|
||||||
|
"""Strictly validate tags, fallback, and password compose syntax on a node or profile entry."""
|
||||||
|
if not isinstance(entry, dict):
|
||||||
|
return
|
||||||
|
if "tags" in entry and entry["tags"] not in ("", None):
|
||||||
|
validate_auth_budget_tags(entry["tags"])
|
||||||
|
if "fallback" in entry and entry["fallback"] not in (None, "", []):
|
||||||
|
validate_fallback_entries(entry["fallback"])
|
||||||
|
pw = entry.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)
|
||||||
|
|
||||||
|
def _walk_connections_validate_unlocked(self, container):
|
||||||
|
"""Recursively validate all connection dicts inside nested folders/subfolders."""
|
||||||
|
if not isinstance(container, dict):
|
||||||
|
raise TypeError("Connections container must be a dict")
|
||||||
|
for key, val in container.items():
|
||||||
|
if key == "type" or not isinstance(val, dict):
|
||||||
|
continue
|
||||||
|
entry_type = val.get("type")
|
||||||
|
if entry_type in ("folder", "subfolder"):
|
||||||
|
self._walk_connections_validate_unlocked(val)
|
||||||
|
else:
|
||||||
|
self._validate_config_entry_unlocked(val)
|
||||||
|
|
||||||
|
def _validate_config_strict(self, data):
|
||||||
|
"""Strictly validate config structure, budgets, and fallback rules; raises on error."""
|
||||||
|
if not isinstance(data, dict):
|
||||||
|
raise TypeError("Config root must be a dict")
|
||||||
|
required = {"config", "connections", "profiles"}
|
||||||
|
if not required.issubset(data.keys()):
|
||||||
|
raise ValueError("Config missing required sections")
|
||||||
|
if not isinstance(data["config"], dict):
|
||||||
|
raise TypeError("'config' section must be a dict")
|
||||||
|
if not isinstance(data["connections"], dict):
|
||||||
|
raise TypeError("'connections' section must be a dict")
|
||||||
|
if not isinstance(data["profiles"], dict):
|
||||||
|
raise TypeError("'profiles' section must be a dict")
|
||||||
|
validate_config_auth_settings(data["config"])
|
||||||
|
self._walk_connections_validate_unlocked(data["connections"])
|
||||||
|
for prof_val in data["profiles"].values():
|
||||||
|
if not isinstance(prof_val, dict):
|
||||||
|
raise TypeError("Each profile entry must be a dict")
|
||||||
|
self._validate_config_entry_unlocked(prof_val)
|
||||||
|
|
||||||
def _validate_config(self, data):
|
def _validate_config(self, data):
|
||||||
"""Verify config data has the required structure."""
|
"""Verify config data has the required structure."""
|
||||||
if not isinstance(data, dict):
|
try:
|
||||||
|
self._validate_config_strict(data)
|
||||||
|
except (ValueError, TypeError, SanitizedAuthError):
|
||||||
return False
|
return False
|
||||||
required = {"config", "connections", "profiles"}
|
return True
|
||||||
return required.issubset(data.keys())
|
|
||||||
|
def _atomic_write_text_unlocked(self, target_path, writer):
|
||||||
|
"""Atomically write text file with symlink refusal, 0o600 mode, fsync, and os.replace."""
|
||||||
|
if os.path.islink(target_path):
|
||||||
|
raise OSError(f"Refusing to write through symlink: {target_path}")
|
||||||
|
target_dir = os.path.dirname(os.path.abspath(target_path)) or "."
|
||||||
|
os.makedirs(target_dir, exist_ok=True)
|
||||||
|
fd, tmp_path = tempfile.mkstemp(
|
||||||
|
dir=target_dir,
|
||||||
|
prefix=f".{os.path.basename(target_path)}.",
|
||||||
|
suffix=".tmp",
|
||||||
|
)
|
||||||
|
try:
|
||||||
|
with os.fdopen(fd, "w", encoding="utf-8") as f:
|
||||||
|
writer(f)
|
||||||
|
f.flush()
|
||||||
|
os.fsync(f.fileno())
|
||||||
|
os.chmod(tmp_path, 0o600)
|
||||||
|
os.replace(tmp_path, target_path)
|
||||||
|
except BaseException:
|
||||||
|
with suppress(OSError):
|
||||||
|
os.unlink(tmp_path)
|
||||||
|
raise
|
||||||
|
|
||||||
|
def _fsync_dir_nonfatal(self, file_path):
|
||||||
|
target_dir = os.path.dirname(os.path.abspath(file_path)) or "."
|
||||||
|
try:
|
||||||
|
dir_fd = os.open(target_dir, os.O_RDONLY)
|
||||||
|
except OSError:
|
||||||
|
return
|
||||||
|
try:
|
||||||
|
os.fsync(dir_fd)
|
||||||
|
except OSError:
|
||||||
|
return
|
||||||
|
finally:
|
||||||
|
with suppress(OSError):
|
||||||
|
os.close(dir_fd)
|
||||||
|
|
||||||
def _loadconfig(self, conf):
|
def _loadconfig(self, conf):
|
||||||
#Loads config file using dual cache
|
#Loads config file using dual cache
|
||||||
@@ -231,17 +335,23 @@ class configfile:
|
|||||||
data = json.load(f)
|
data = json.load(f)
|
||||||
if self._validate_config(data):
|
if self._validate_config(data):
|
||||||
# Re-write the YAML from good cache
|
# Re-write the YAML from good cache
|
||||||
with open(conf, 'w') as f:
|
with suppress(OSError):
|
||||||
yaml.dump(data, f, Dumper=NoAliasDumper, default_flow_style=False, sort_keys=False)
|
self._atomic_write_text_unlocked(
|
||||||
|
conf,
|
||||||
|
lambda fp: yaml.dump(
|
||||||
|
data,
|
||||||
|
fp,
|
||||||
|
Dumper=NoAliasDumper,
|
||||||
|
default_flow_style=False,
|
||||||
|
sort_keys=False,
|
||||||
|
),
|
||||||
|
)
|
||||||
return data
|
return data
|
||||||
# Both broken or no cache - create fresh
|
# Both broken or no cache - create fresh
|
||||||
printer.error("Config file is corrupt and no valid cache exists. Creating default config.")
|
printer.error("Config file is corrupt and no valid cache exists. Creating default config.")
|
||||||
return self._createconfig(conf)
|
return self._createconfig(conf)
|
||||||
try:
|
with suppress(OSError, TypeError, ValueError):
|
||||||
with open(self.cachefile, 'w') as f:
|
self._atomic_write_text_unlocked(self.cachefile, lambda fp: json.dump(data, fp))
|
||||||
json.dump(data, f)
|
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
return data
|
return data
|
||||||
else:
|
else:
|
||||||
with open(self.cachefile, 'r') as f:
|
with open(self.cachefile, 'r') as f:
|
||||||
@@ -259,61 +369,152 @@ class configfile:
|
|||||||
|
|
||||||
def _createconfig(self, conf):
|
def _createconfig(self, conf):
|
||||||
#Create config file (always writes defaults, safe for recovery)
|
#Create config file (always writes defaults, safe for recovery)
|
||||||
defaultconfig = {'config': {'case': False, 'idletime': 30, 'fzf': False}, 'connections': {}, 'profiles': { "default": { "host":"", "protocol":"ssh", "port":"", "user":"", "password":"", "options":"", "logs":"", "tags": "", "jumphost":""}}}
|
defaultconfig = {'config': {'case': False, 'idletime': 30, 'fzf': False}, 'connections': {}, 'profiles': { "default": { "host":"", "protocol":"ssh", "port":"", "user":"", "password":"", "options":"", "logs":"", "tags": "", "jumphost":"", "fallback": ""}}}
|
||||||
with open(conf, "w") as f:
|
self._atomic_write_text_unlocked(
|
||||||
yaml.dump(defaultconfig, f, Dumper=NoAliasDumper, default_flow_style=False, sort_keys=False)
|
conf,
|
||||||
os.chmod(conf, 0o600)
|
lambda f: yaml.dump(defaultconfig, f, Dumper=NoAliasDumper, default_flow_style=False, sort_keys=False),
|
||||||
try:
|
)
|
||||||
with open(self.cachefile, 'w') as f:
|
with suppress(OSError, TypeError, ValueError):
|
||||||
json.dump(defaultconfig, f)
|
self._atomic_write_text_unlocked(self.cachefile, lambda f: json.dump(defaultconfig, f))
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
return defaultconfig
|
return defaultconfig
|
||||||
|
|
||||||
|
def _saveconfig_unlocked(self, conf, *, _raise_on_error=False):
|
||||||
|
# Save config file atomically inside _mutation_lock without invoking hooks or printer
|
||||||
|
newconfig = {
|
||||||
|
"config": self.config,
|
||||||
|
"connections": self.connections,
|
||||||
|
"profiles": self.profiles,
|
||||||
|
}
|
||||||
|
try:
|
||||||
|
self._atomic_write_text_unlocked(
|
||||||
|
conf,
|
||||||
|
lambda f: yaml.dump(
|
||||||
|
newconfig,
|
||||||
|
f,
|
||||||
|
Dumper=NoAliasDumper,
|
||||||
|
default_flow_style=False,
|
||||||
|
sort_keys=False,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
except (OSError, TypeError, yaml.YAMLError) as e:
|
||||||
|
if _raise_on_error:
|
||||||
|
raise
|
||||||
|
return 1, f"Failed to save config: {e}"
|
||||||
|
|
||||||
|
# YAML is safely committed on disk; fsync dir and sidecar caches are non-fatal
|
||||||
|
self._fsync_dir_nonfatal(conf)
|
||||||
|
try:
|
||||||
|
self._atomic_write_text_unlocked(self.cachefile, lambda f: json.dump(newconfig, f))
|
||||||
|
self._generate_nodes_cache_unlocked(_raise_on_error=True)
|
||||||
|
self._cache_dirty = False
|
||||||
|
except (OSError, TypeError, ValueError):
|
||||||
|
self._cache_dirty = True
|
||||||
|
return 0, None
|
||||||
|
|
||||||
|
def _run_saveconfig_post_hooks(self, conf, result):
|
||||||
|
"""Run @MethodHook post-hooks registered for _saveconfig outside _mutation_lock."""
|
||||||
|
cls_desc = type(self).__dict__.get("_saveconfig")
|
||||||
|
cls_post = list(cls_desc.post_hooks) if isinstance(cls_desc, MethodHook) else []
|
||||||
|
inst_post = (
|
||||||
|
self.__dict__.get("_method_post_hooks", {}).get("_saveconfig", [])
|
||||||
|
if isinstance(self.__dict__.get("_method_post_hooks"), dict)
|
||||||
|
else []
|
||||||
|
)
|
||||||
|
all_post = cls_post + [h for h in inst_post if h not in cls_post]
|
||||||
|
for hook in all_post:
|
||||||
|
try:
|
||||||
|
result = hook(self, conf, result=result)
|
||||||
|
except Exception as e: # noqa: BLE001
|
||||||
|
hook_name = getattr(hook, "__name__", str(hook))
|
||||||
|
printer.error(f"_saveconfig Post-hook {hook_name} raised an exception: {e}")
|
||||||
|
return result
|
||||||
|
|
||||||
@MethodHook
|
@MethodHook
|
||||||
def _saveconfig(self, conf):
|
def _saveconfig(self, conf):
|
||||||
#Save config file atomically to prevent corruption
|
#Save config file atomically to prevent corruption
|
||||||
newconfig = {"config":{}, "connections": {}, "profiles": {}}
|
with self._mutation_lock:
|
||||||
newconfig["config"] = self.config
|
rc, err_msg = self._saveconfig_unlocked(conf, _raise_on_error=False)
|
||||||
newconfig["connections"] = self.connections
|
if err_msg:
|
||||||
newconfig["profiles"] = self.profiles
|
printer.error(err_msg)
|
||||||
tmpfile = conf + '.tmp'
|
return rc
|
||||||
try:
|
|
||||||
with open(tmpfile, "w") as f:
|
|
||||||
yaml.dump(newconfig, f, Dumper=NoAliasDumper, default_flow_style=False, sort_keys=False)
|
|
||||||
# Atomic replace: only overwrite original if write succeeded
|
|
||||||
shutil.move(tmpfile, conf)
|
|
||||||
with open(self.cachefile, "w") as f:
|
|
||||||
json.dump(newconfig, f)
|
|
||||||
self._generate_nodes_cache()
|
|
||||||
except (IOError, OSError) as e:
|
|
||||||
printer.error(f"Failed to save config: {e}")
|
|
||||||
# Clean up temp file if it exists
|
|
||||||
if os.path.exists(tmpfile):
|
|
||||||
try:
|
|
||||||
os.remove(tmpfile)
|
|
||||||
except OSError:
|
|
||||||
pass
|
|
||||||
return 1
|
|
||||||
return 0
|
|
||||||
|
|
||||||
def _generate_nodes_cache(self, nodes=None, folders=None, profiles=None):
|
def transactional_update(self, mutator):
|
||||||
|
"""Run mutator atomically inside _mutation_lock with in-place RAM rollback on error."""
|
||||||
|
save_desc = type(self).__dict__.get("_saveconfig")
|
||||||
|
mocked_save = "_saveconfig" in self.__dict__ or not isinstance(save_desc, MethodHook)
|
||||||
|
with self._mutation_lock:
|
||||||
|
snap_config = deepcopy(self.config)
|
||||||
|
snap_connections = deepcopy(self.connections)
|
||||||
|
snap_profiles = deepcopy(self.profiles)
|
||||||
|
try:
|
||||||
|
result = mutator(self)
|
||||||
|
self._validate_config_strict(
|
||||||
|
{
|
||||||
|
"config": self.config,
|
||||||
|
"connections": self.connections,
|
||||||
|
"profiles": self.profiles,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
if mocked_save:
|
||||||
|
rc = self._saveconfig(self.file)
|
||||||
|
if rc != 0:
|
||||||
|
raise OSError("Failed to save config")
|
||||||
|
else:
|
||||||
|
self._saveconfig_unlocked(self.file, _raise_on_error=True)
|
||||||
|
except BaseException:
|
||||||
|
self.config.clear()
|
||||||
|
self.config.update(snap_config)
|
||||||
|
self.connections.clear()
|
||||||
|
self.connections.update(snap_connections)
|
||||||
|
self.profiles.clear()
|
||||||
|
self.profiles.update(snap_profiles)
|
||||||
|
raise
|
||||||
|
if not mocked_save:
|
||||||
|
self._run_saveconfig_post_hooks(self.file, 0)
|
||||||
|
return result
|
||||||
|
|
||||||
|
def refresh_cache(self):
|
||||||
|
"""Rebuild JSON and fzf sidecar caches atomically; returns True on success."""
|
||||||
|
with self._mutation_lock:
|
||||||
|
snapshot = {
|
||||||
|
"config": self.config,
|
||||||
|
"connections": self.connections,
|
||||||
|
"profiles": self.profiles,
|
||||||
|
}
|
||||||
|
try:
|
||||||
|
self._atomic_write_text_unlocked(self.cachefile, lambda f: json.dump(snapshot, f))
|
||||||
|
self._generate_nodes_cache_unlocked(_raise_on_error=True)
|
||||||
|
self._cache_dirty = False
|
||||||
|
return True
|
||||||
|
except (OSError, TypeError, ValueError):
|
||||||
|
self._cache_dirty = True
|
||||||
|
return False
|
||||||
|
|
||||||
|
def _generate_nodes_cache_unlocked(self, nodes=None, folders=None, profiles=None, *, _raise_on_error=False):
|
||||||
try:
|
try:
|
||||||
if nodes is None:
|
if nodes is None:
|
||||||
nodes = self._getallnodes()
|
nodes = self._getallnodes_unlocked()
|
||||||
if folders is None:
|
if folders is None:
|
||||||
folders = self._getallfolders()
|
folders = self._getallfolders_unlocked()
|
||||||
if profiles is None:
|
if profiles is None:
|
||||||
profiles = list(self.profiles.keys())
|
profiles = list(self.profiles.keys())
|
||||||
|
|
||||||
with open(self.fzf_cachefile, "w") as f:
|
self._atomic_write_text_unlocked(self.fzf_cachefile, lambda f: f.write("\n".join(nodes)))
|
||||||
f.write("\n".join(nodes))
|
self._atomic_write_text_unlocked(self.folders_cachefile, lambda f: f.write("\n".join(folders)))
|
||||||
with open(self.folders_cachefile, "w") as f:
|
self._atomic_write_text_unlocked(self.profiles_cachefile, lambda f: f.write("\n".join(profiles)))
|
||||||
f.write("\n".join(folders))
|
except (OSError, TypeError, ValueError):
|
||||||
with open(self.profiles_cachefile, "w") as f:
|
if _raise_on_error:
|
||||||
f.write("\n".join(profiles))
|
raise
|
||||||
except Exception:
|
self._cache_dirty = True
|
||||||
pass
|
|
||||||
|
def _generate_nodes_cache(self, nodes=None, folders=None, profiles=None):
|
||||||
|
with self._mutation_lock:
|
||||||
|
self._generate_nodes_cache_unlocked(
|
||||||
|
nodes=nodes,
|
||||||
|
folders=folders,
|
||||||
|
profiles=profiles,
|
||||||
|
_raise_on_error=False,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def _createkey(self, keyfile):
|
def _createkey(self, keyfile):
|
||||||
@@ -326,8 +527,7 @@ class configfile:
|
|||||||
os.chmod(keyfile, 0o600)
|
os.chmod(keyfile, 0o600)
|
||||||
return key
|
return key
|
||||||
|
|
||||||
@MethodHook
|
def _explode_unique_unlocked(self, unique):
|
||||||
def _explode_unique(self, unique):
|
|
||||||
#Divide unique name into folder, subfolder and id
|
#Divide unique name into folder, subfolder and id
|
||||||
uniques = unique.split("@")
|
uniques = unique.split("@")
|
||||||
if not unique.startswith("@"):
|
if not unique.startswith("@"):
|
||||||
@@ -348,30 +548,130 @@ class configfile:
|
|||||||
return result
|
return result
|
||||||
|
|
||||||
@MethodHook
|
@MethodHook
|
||||||
def getitem(self, unique, keys = None, extract = False):
|
def _explode_unique(self, unique):
|
||||||
'''
|
return self._explode_unique_unlocked(unique)
|
||||||
Get an node or a group of nodes from configfile which can be passed to node/nodes class
|
|
||||||
|
|
||||||
### Parameters:
|
def _resolve_profile_attr_unlocked(self, prof_name, key, *, depth=0, visited=frozenset()):
|
||||||
|
if prof_name in visited or depth >= 5:
|
||||||
|
return None
|
||||||
|
prof = self.profiles.get(prof_name)
|
||||||
|
if not isinstance(prof, dict) or key not in prof:
|
||||||
|
return None
|
||||||
|
val = prof[key]
|
||||||
|
if key == "password":
|
||||||
|
if isinstance(val, str):
|
||||||
|
if is_compose_expression(val):
|
||||||
|
return val
|
||||||
|
if val.startswith("@") and val[1:] in self.profiles:
|
||||||
|
sub = self._resolve_profile_attr_unlocked(
|
||||||
|
val[1:],
|
||||||
|
key,
|
||||||
|
depth=depth + 1,
|
||||||
|
visited=visited | {prof_name},
|
||||||
|
)
|
||||||
|
return sub if sub is not None else val
|
||||||
|
return val
|
||||||
|
if isinstance(val, list):
|
||||||
|
resolved_list = []
|
||||||
|
for item in val:
|
||||||
|
if (
|
||||||
|
isinstance(item, str)
|
||||||
|
and not is_compose_expression(item)
|
||||||
|
and item.startswith("@")
|
||||||
|
and item[1:] in self.profiles
|
||||||
|
):
|
||||||
|
sub = self._resolve_profile_attr_unlocked(
|
||||||
|
item[1:],
|
||||||
|
"password",
|
||||||
|
depth=depth + 1,
|
||||||
|
visited=visited | {prof_name},
|
||||||
|
)
|
||||||
|
if isinstance(sub, list):
|
||||||
|
resolved_list.extend(deepcopy(sub))
|
||||||
|
elif sub is not None:
|
||||||
|
resolved_list.append(deepcopy(sub))
|
||||||
|
else:
|
||||||
|
resolved_list.append(item)
|
||||||
|
else:
|
||||||
|
resolved_list.append(deepcopy(item))
|
||||||
|
return resolved_list
|
||||||
|
return deepcopy(val)
|
||||||
|
|
||||||
- unique (str): Unique name of the node or folder in config using
|
if isinstance(val, str) and val.startswith("@") and val[1:] in self.profiles:
|
||||||
connection manager style: node[@subfolder][@folder]
|
sub = self._resolve_profile_attr_unlocked(
|
||||||
or [@subfolder]@folder
|
val[1:],
|
||||||
|
key,
|
||||||
|
depth=depth + 1,
|
||||||
|
visited=visited | {prof_name},
|
||||||
|
)
|
||||||
|
return sub if sub is not None else ""
|
||||||
|
return deepcopy(val)
|
||||||
|
|
||||||
### Optional Parameters:
|
def _extract_node_fields_unlocked(self, node_dict):
|
||||||
|
"""Resolve @profile references on a copied node dict, keeping legacy core.node(**cfg) contract."""
|
||||||
|
raw_fb = node_dict.pop("fallback", None)
|
||||||
|
|
||||||
- keys (list): In case you pass a folder as unique, you can filter
|
for key, value in list(node_dict.items()):
|
||||||
nodes inside the folder passing a list.
|
if key == "password":
|
||||||
- extract (bool): If True, extract information from profiles.
|
if isinstance(value, str):
|
||||||
Default False.
|
if is_compose_expression(value):
|
||||||
|
continue
|
||||||
|
if value.startswith("@") and value[1:] in self.profiles:
|
||||||
|
resolved = self._resolve_profile_attr_unlocked(value[1:], "password")
|
||||||
|
node_dict["password"] = resolved if resolved is not None else ""
|
||||||
|
elif isinstance(value, list):
|
||||||
|
resolved_pw = []
|
||||||
|
for item in value:
|
||||||
|
if (
|
||||||
|
isinstance(item, str)
|
||||||
|
and not is_compose_expression(item)
|
||||||
|
and item.startswith("@")
|
||||||
|
and item[1:] in self.profiles
|
||||||
|
):
|
||||||
|
sub = self._resolve_profile_attr_unlocked(item[1:], "password")
|
||||||
|
if isinstance(sub, list):
|
||||||
|
resolved_pw.extend(deepcopy(sub))
|
||||||
|
elif sub is not None:
|
||||||
|
resolved_pw.append(deepcopy(sub))
|
||||||
|
else:
|
||||||
|
resolved_pw.append("")
|
||||||
|
else:
|
||||||
|
resolved_pw.append(deepcopy(item))
|
||||||
|
node_dict["password"] = resolved_pw
|
||||||
|
continue
|
||||||
|
|
||||||
### Returns:
|
if isinstance(value, str) and value.startswith("@"):
|
||||||
|
ref_name = value[1:]
|
||||||
|
if ref_name in self.profiles:
|
||||||
|
res = self._resolve_profile_attr_unlocked(ref_name, key)
|
||||||
|
node_dict[key] = res if res is not None else ""
|
||||||
|
else:
|
||||||
|
node_dict[key] = ""
|
||||||
|
elif value == "" and key == "protocol":
|
||||||
|
default_proto = self.profiles.get("default", {}).get("protocol", "ssh")
|
||||||
|
node_dict[key] = default_proto if default_proto else "ssh"
|
||||||
|
|
||||||
dict: Dictionary containing information of node or multiple
|
resolved_fb = []
|
||||||
dictionaries of multiple nodes.
|
if raw_fb not in (None, "", []):
|
||||||
|
resolved_fb = resolve_fallback_entries(raw_fb, self.profiles)
|
||||||
|
|
||||||
'''
|
if resolved_fb:
|
||||||
uniques = self._explode_unique(unique)
|
tags_val = node_dict.get("tags", "")
|
||||||
|
if isinstance(tags_val, dict):
|
||||||
|
tags_copy = deepcopy(tags_val)
|
||||||
|
tags_copy["_fallback"] = resolved_fb
|
||||||
|
node_dict["tags"] = tags_copy
|
||||||
|
elif tags_val in ("", None):
|
||||||
|
node_dict["tags"] = {"_fallback": resolved_fb}
|
||||||
|
else:
|
||||||
|
node_dict["tags"] = {"_raw": tags_val, "_fallback": resolved_fb}
|
||||||
|
elif isinstance(node_dict.get("tags"), dict) and node_dict["tags"].get("fallback") not in (None, "", []):
|
||||||
|
tags_copy = deepcopy(node_dict["tags"])
|
||||||
|
tags_copy["_fallback"] = resolve_fallback_entries(tags_copy["fallback"], self.profiles)
|
||||||
|
node_dict["tags"] = tags_copy
|
||||||
|
|
||||||
|
def getitem_unlocked(self, unique, keys=None, extract=False):
|
||||||
|
uniques = self._explode_unique_unlocked(unique)
|
||||||
if unique.startswith("@"):
|
if unique.startswith("@"):
|
||||||
if uniques.keys() >= {"folder", "subfolder"}:
|
if uniques.keys() >= {"folder", "subfolder"}:
|
||||||
folder = self.connections[uniques["folder"]][uniques["subfolder"]]
|
folder = self.connections[uniques["folder"]][uniques["subfolder"]]
|
||||||
@@ -379,60 +679,42 @@ class configfile:
|
|||||||
folder = self.connections[uniques["folder"]]
|
folder = self.connections[uniques["folder"]]
|
||||||
newfolder = deepcopy(folder)
|
newfolder = deepcopy(folder)
|
||||||
newfolder.pop("type")
|
newfolder.pop("type")
|
||||||
for node_name in folder.keys():
|
for node_name in list(folder.keys()):
|
||||||
if node_name == "type":
|
if node_name == "type":
|
||||||
continue
|
continue
|
||||||
if "type" in newfolder[node_name].keys():
|
if "type" in newfolder[node_name]:
|
||||||
if newfolder[node_name]["type"] == "subfolder":
|
if newfolder[node_name]["type"] == "subfolder":
|
||||||
newfolder.pop(node_name)
|
newfolder.pop(node_name)
|
||||||
else:
|
else:
|
||||||
newfolder[node_name].pop("type")
|
newfolder[node_name].pop("type")
|
||||||
|
|
||||||
if keys != None:
|
if keys is not None:
|
||||||
newfolder = dict((k, newfolder[k]) for k in keys)
|
newfolder = {k: newfolder[k] for k in keys}
|
||||||
|
|
||||||
if extract:
|
if extract:
|
||||||
for node_name, node_keys in newfolder.items():
|
for node_keys in newfolder.values():
|
||||||
for key, value in node_keys.items():
|
self._extract_node_fields_unlocked(node_keys)
|
||||||
profile = re.search("^@(.*)", str(value))
|
|
||||||
if profile:
|
return {f"{k}{unique}": v for k, v in newfolder.items()}
|
||||||
try:
|
|
||||||
newfolder[node_name][key] = self.profiles[profile.group(1)][key]
|
|
||||||
except KeyError:
|
|
||||||
newfolder[node_name][key] = ""
|
|
||||||
elif value == '' and key == "protocol":
|
|
||||||
try:
|
|
||||||
newfolder[node_name][key] = self.profiles["default"][key]
|
|
||||||
except KeyError:
|
|
||||||
newfolder[node_name][key] = "ssh"
|
|
||||||
|
|
||||||
newfolder = {"{}{}".format(k,unique):v for k,v in newfolder.items()}
|
|
||||||
return newfolder
|
|
||||||
else:
|
|
||||||
if uniques.keys() >= {"folder", "subfolder"}:
|
if uniques.keys() >= {"folder", "subfolder"}:
|
||||||
node = self.connections[uniques["folder"]][uniques["subfolder"]][uniques["id"]]
|
node = self.connections[uniques["folder"]][uniques["subfolder"]][uniques["id"]]
|
||||||
elif "folder" in uniques.keys():
|
elif "folder" in uniques:
|
||||||
node = self.connections[uniques["folder"]][uniques["id"]]
|
node = self.connections[uniques["folder"]][uniques["id"]]
|
||||||
else:
|
else:
|
||||||
node = self.connections[uniques["id"]]
|
node = self.connections[uniques["id"]]
|
||||||
newnode = deepcopy(node)
|
newnode = deepcopy(node)
|
||||||
newnode.pop("type")
|
newnode.pop("type")
|
||||||
|
|
||||||
if extract:
|
if extract:
|
||||||
for key, value in newnode.items():
|
self._extract_node_fields_unlocked(newnode)
|
||||||
profile = re.search("^@(.*)", str(value))
|
|
||||||
if profile:
|
|
||||||
try:
|
|
||||||
newnode[key] = self.profiles[profile.group(1)][key]
|
|
||||||
except KeyError:
|
|
||||||
newnode[key] = ""
|
|
||||||
elif value == '' and key == "protocol":
|
|
||||||
try:
|
|
||||||
newnode[key] = self.profiles["default"][key]
|
|
||||||
except KeyError:
|
|
||||||
newnode[key] = "ssh"
|
|
||||||
return newnode
|
return newnode
|
||||||
|
|
||||||
|
@MethodHook
|
||||||
|
def getitem(self, unique, keys = None, extract = False):
|
||||||
|
"""Get a node or folder of nodes from configfile."""
|
||||||
|
with self._mutation_lock:
|
||||||
|
return self.getitem_unlocked(unique, keys=keys, extract=extract)
|
||||||
|
|
||||||
@MethodHook
|
@MethodHook
|
||||||
def getitems(self, uniques, extract = False):
|
def getitems(self, uniques, extract = False):
|
||||||
'''
|
'''
|
||||||
@@ -455,47 +737,94 @@ class configfile:
|
|||||||
dictionaries of multiple nodes.
|
dictionaries of multiple nodes.
|
||||||
|
|
||||||
'''
|
'''
|
||||||
nodes = {}
|
|
||||||
if isinstance(uniques, str):
|
if isinstance(uniques, str):
|
||||||
uniques = [uniques]
|
uniques = [uniques]
|
||||||
for i in uniques:
|
with self._mutation_lock:
|
||||||
if i.startswith("@"):
|
nodes = {}
|
||||||
if not self.config["case"]:
|
case_sensitive = self.config.get("case", False)
|
||||||
i = i.lower()
|
for item in uniques:
|
||||||
this = self.getitem(i, extract = extract)
|
target = item if case_sensitive else item.lower()
|
||||||
nodes.update(this)
|
if target.startswith("@"):
|
||||||
|
nodes.update(self.getitem_unlocked(target, extract=extract))
|
||||||
else:
|
else:
|
||||||
if not self.config["case"]:
|
nodes[target] = self.getitem_unlocked(target, extract=extract)
|
||||||
i = i.lower()
|
|
||||||
this = self.getitem(i, extract = extract)
|
|
||||||
nodes[i] = this
|
|
||||||
return nodes
|
return nodes
|
||||||
|
|
||||||
|
def _connections_add_unlocked(
|
||||||
|
self,
|
||||||
|
*,
|
||||||
|
id,
|
||||||
|
host,
|
||||||
|
folder='',
|
||||||
|
subfolder='',
|
||||||
|
options='',
|
||||||
|
logs='',
|
||||||
|
password='',
|
||||||
|
port='',
|
||||||
|
protocol='',
|
||||||
|
user='',
|
||||||
|
tags='',
|
||||||
|
jumphost='',
|
||||||
|
fallback='',
|
||||||
|
type="connection",
|
||||||
|
):
|
||||||
|
entry = {
|
||||||
|
"host": host,
|
||||||
|
"options": options,
|
||||||
|
"logs": logs,
|
||||||
|
"password": password,
|
||||||
|
"port": port,
|
||||||
|
"protocol": protocol,
|
||||||
|
"user": user,
|
||||||
|
"tags": tags,
|
||||||
|
"jumphost": jumphost,
|
||||||
|
"fallback": fallback,
|
||||||
|
"type": type,
|
||||||
|
}
|
||||||
|
if folder == '':
|
||||||
|
self.connections[id] = entry
|
||||||
|
elif subfolder == '':
|
||||||
|
self.connections[folder][id] = entry
|
||||||
|
else:
|
||||||
|
self.connections[folder][subfolder][id] = entry
|
||||||
|
|
||||||
@MethodHook
|
@MethodHook
|
||||||
def _connections_add(self,*, id, host, folder='', subfolder='', options='', logs='', password='', port='', protocol='', user='', tags='', jumphost='', type = "connection" ):
|
def _connections_add(self,*, id, host, folder='', subfolder='', options='', logs='', password='', port='', protocol='', user='', tags='', jumphost='', fallback='', type = "connection" ):
|
||||||
#Add connection from config
|
#Add connection from config
|
||||||
if folder == '':
|
with self._mutation_lock:
|
||||||
self.connections[id] = {"host": host, "options": options, "logs": logs, "password": password, "port": port, "protocol": protocol, "user": user, "tags": tags,"jumphost": jumphost,"type": type}
|
self._connections_add_unlocked(
|
||||||
elif folder != '' and subfolder == '':
|
id=id,
|
||||||
self.connections[folder][id] = {"host": host, "options": options, "logs": logs, "password": password, "port": port, "protocol": protocol, "user": user, "tags": tags, "jumphost": jumphost, "type": type}
|
host=host,
|
||||||
elif folder != '' and subfolder != '':
|
folder=folder,
|
||||||
self.connections[folder][subfolder][id] = {"host": host, "options": options, "logs": logs, "password": password, "port": port, "protocol": protocol, "user": user, "tags": tags, "jumphost": jumphost, "type": type}
|
subfolder=subfolder,
|
||||||
|
options=options,
|
||||||
|
logs=logs,
|
||||||
|
password=password,
|
||||||
|
port=port,
|
||||||
|
protocol=protocol,
|
||||||
|
user=user,
|
||||||
|
tags=tags,
|
||||||
|
jumphost=jumphost,
|
||||||
|
fallback=fallback,
|
||||||
|
type=type,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _connections_del_unlocked(self, *, id, folder='', subfolder=''):
|
||||||
|
if folder == '':
|
||||||
|
del self.connections[id]
|
||||||
|
elif subfolder == '':
|
||||||
|
del self.connections[folder][id]
|
||||||
|
else:
|
||||||
|
del self.connections[folder][subfolder][id]
|
||||||
|
|
||||||
@MethodHook
|
@MethodHook
|
||||||
def _connections_del(self,*, id, folder='', subfolder=''):
|
def _connections_del(self,*, id, folder='', subfolder=''):
|
||||||
#Delete connection from config
|
#Delete connection from config
|
||||||
if folder == '':
|
with self._mutation_lock:
|
||||||
del self.connections[id]
|
self._connections_del_unlocked(id=id, folder=folder, subfolder=subfolder)
|
||||||
elif folder != '' and subfolder == '':
|
|
||||||
del self.connections[folder][id]
|
|
||||||
elif folder != '' and subfolder != '':
|
|
||||||
del self.connections[folder][subfolder][id]
|
|
||||||
|
|
||||||
@MethodHook
|
def _folder_add_unlocked(self, *, folder, subfolder=''):
|
||||||
def _folder_add(self,*, folder, subfolder = ''):
|
|
||||||
#Add Folder from config
|
|
||||||
if subfolder == '':
|
if subfolder == '':
|
||||||
if folder not in self.connections:
|
if folder not in self.connections:
|
||||||
self.connections[folder] = {"type": "folder"}
|
self.connections[folder] = {"type": "folder"}
|
||||||
@@ -504,28 +833,75 @@ class configfile:
|
|||||||
self.connections[folder][subfolder] = {"type": "subfolder"}
|
self.connections[folder][subfolder] = {"type": "subfolder"}
|
||||||
|
|
||||||
@MethodHook
|
@MethodHook
|
||||||
def _folder_del(self,*, folder, subfolder=''):
|
def _folder_add(self,*, folder, subfolder = ''):
|
||||||
#Delete folder from config
|
#Add Folder from config
|
||||||
|
with self._mutation_lock:
|
||||||
|
self._folder_add_unlocked(folder=folder, subfolder=subfolder)
|
||||||
|
|
||||||
|
def _folder_del_unlocked(self, *, folder, subfolder=''):
|
||||||
if subfolder == '':
|
if subfolder == '':
|
||||||
del self.connections[folder]
|
del self.connections[folder]
|
||||||
else:
|
else:
|
||||||
del self.connections[folder][subfolder]
|
del self.connections[folder][subfolder]
|
||||||
|
|
||||||
|
@MethodHook
|
||||||
|
def _folder_del(self,*, folder, subfolder=''):
|
||||||
|
#Delete folder from config
|
||||||
|
with self._mutation_lock:
|
||||||
|
self._folder_del_unlocked(folder=folder, subfolder=subfolder)
|
||||||
|
|
||||||
|
def _profiles_add_unlocked(self, *, id, host='', options='', logs='', password='', port='', protocol='', user='', tags='', jumphost='', fallback=''):
|
||||||
|
self.profiles[id] = {
|
||||||
|
"host": host,
|
||||||
|
"options": options,
|
||||||
|
"logs": logs,
|
||||||
|
"password": password,
|
||||||
|
"port": port,
|
||||||
|
"protocol": protocol,
|
||||||
|
"user": user,
|
||||||
|
"tags": tags,
|
||||||
|
"jumphost": jumphost,
|
||||||
|
"fallback": fallback,
|
||||||
|
}
|
||||||
|
|
||||||
@MethodHook
|
@MethodHook
|
||||||
def _profiles_add(self,*, id, host = '', options='', logs='', password='', port='', protocol='', user='', tags='', jumphost='' ):
|
def _profiles_add(self,*, id, host = '', options='', logs='', password='', port='', protocol='', user='', tags='', jumphost='', fallback=''):
|
||||||
#Add profile from config
|
#Add profile from config
|
||||||
self.profiles[id] = {"host": host, "options": options, "logs": logs, "password": password, "port": port, "protocol": protocol, "user": user, "tags": tags, "jumphost": jumphost}
|
with self._mutation_lock:
|
||||||
|
self._profiles_add_unlocked(
|
||||||
|
id=id,
|
||||||
|
host=host,
|
||||||
|
options=options,
|
||||||
|
logs=logs,
|
||||||
|
password=password,
|
||||||
|
port=port,
|
||||||
|
protocol=protocol,
|
||||||
|
user=user,
|
||||||
|
tags=tags,
|
||||||
|
jumphost=jumphost,
|
||||||
|
fallback=fallback,
|
||||||
|
)
|
||||||
|
|
||||||
|
def _profiles_del_unlocked(self, *, id):
|
||||||
|
del self.profiles[id]
|
||||||
|
|
||||||
@MethodHook
|
@MethodHook
|
||||||
def _profiles_del(self,*, id ):
|
def _profiles_del(self,*, id ):
|
||||||
#Delete profile from config
|
#Delete profile from config
|
||||||
del self.profiles[id]
|
with self._mutation_lock:
|
||||||
|
self._profiles_del_unlocked(id=id)
|
||||||
|
|
||||||
@MethodHook
|
@staticmethod
|
||||||
def _getallnodes(self, filter = None):
|
def _normalize_filter_arg(filter_arg):
|
||||||
#get all nodes on configfile
|
if not filter_arg:
|
||||||
|
return []
|
||||||
|
if isinstance(filter_arg, str):
|
||||||
|
return [filter_arg]
|
||||||
|
if isinstance(filter_arg, list):
|
||||||
|
return [item for item in filter_arg if isinstance(item, str)]
|
||||||
|
return None
|
||||||
|
|
||||||
|
def _getallnodes_unlocked(self, filter=None):
|
||||||
nodes = []
|
nodes = []
|
||||||
layer1 = [k for k, v in self.connections.items() if isinstance(v, dict) and v.get("type") == "connection"]
|
layer1 = [k for k, v in self.connections.items() if isinstance(v, dict) and v.get("type") == "connection"]
|
||||||
folders = [k for k, v in self.connections.items() if isinstance(v, dict) and v.get("type") == "folder"]
|
folders = [k for k, v in self.connections.items() if isinstance(v, dict) and v.get("type") == "folder"]
|
||||||
@@ -538,67 +914,51 @@ class configfile:
|
|||||||
layer3 = [k + "@" + s + "@" + f for k, v in self.connections[f][s].items() if isinstance(v, dict) and v.get("type") == "connection"]
|
layer3 = [k + "@" + s + "@" + f for k, v in self.connections[f][s].items() if isinstance(v, dict) and v.get("type") == "connection"]
|
||||||
nodes.extend(layer3)
|
nodes.extend(layer3)
|
||||||
if filter:
|
if filter:
|
||||||
flat_filter = []
|
flat_filter = self._normalize_filter_arg(filter)
|
||||||
if isinstance(filter, str):
|
|
||||||
flat_filter = [filter]
|
|
||||||
elif isinstance(filter, list):
|
|
||||||
for item in filter:
|
|
||||||
if isinstance(item, str):
|
|
||||||
flat_filter.append(item)
|
|
||||||
else:
|
|
||||||
printer.error("Filter must be a string or a list of strings")
|
|
||||||
sys.exit(1)
|
|
||||||
flags = re.IGNORECASE if not self.config.get("case", False) else 0
|
flags = re.IGNORECASE if not self.config.get("case", False) else 0
|
||||||
nodes = [item for item in nodes if any(re.search(pattern, item, flags) for pattern in flat_filter)]
|
nodes = [item for item in nodes if any(re.search(pattern, item, flags) for pattern in flat_filter)]
|
||||||
return nodes
|
return nodes
|
||||||
|
|
||||||
@MethodHook
|
@MethodHook
|
||||||
def _getallnodesfull(self, filter = None, extract = True):
|
def _getallnodes(self, filter = None):
|
||||||
#get all nodes on configfile with all their attributes.
|
#get all nodes on configfile
|
||||||
|
if filter and self._normalize_filter_arg(filter) is None:
|
||||||
|
printer.error("Filter must be a string or a list of strings")
|
||||||
|
sys.exit(1)
|
||||||
|
with self._mutation_lock:
|
||||||
|
return self._getallnodes_unlocked(filter=filter)
|
||||||
|
|
||||||
|
def _getallnodesfull_unlocked(self, filter=None, extract=True):
|
||||||
nodes = {}
|
nodes = {}
|
||||||
layer1 = {k:v for k,v in self.connections.items() if isinstance(v, dict) and v.get("type") == "connection"}
|
layer1 = {k: deepcopy(v) for k, v in self.connections.items() if isinstance(v, dict) and v.get("type") == "connection"}
|
||||||
folders = [k for k, v in self.connections.items() if isinstance(v, dict) and v.get("type") == "folder"]
|
folders = [k for k, v in self.connections.items() if isinstance(v, dict) and v.get("type") == "folder"]
|
||||||
nodes.update(layer1)
|
nodes.update(layer1)
|
||||||
for f in folders:
|
for f in folders:
|
||||||
layer2 = {k + "@" + f:v for k,v in self.connections[f].items() if isinstance(v, dict) and v.get("type") == "connection"}
|
layer2 = {k + "@" + f: deepcopy(v) for k, v in self.connections[f].items() if isinstance(v, dict) and v.get("type") == "connection"}
|
||||||
nodes.update(layer2)
|
nodes.update(layer2)
|
||||||
subfolders = [k for k, v in self.connections[f].items() if isinstance(v, dict) and v.get("type") == "subfolder"]
|
subfolders = [k for k, v in self.connections[f].items() if isinstance(v, dict) and v.get("type") == "subfolder"]
|
||||||
for s in subfolders:
|
for s in subfolders:
|
||||||
layer3 = {k + "@" + s + "@" + f:v for k,v in self.connections[f][s].items() if isinstance(v, dict) and v.get("type") == "connection"}
|
layer3 = {k + "@" + s + "@" + f: deepcopy(v) for k, v in self.connections[f][s].items() if isinstance(v, dict) and v.get("type") == "connection"}
|
||||||
nodes.update(layer3)
|
nodes.update(layer3)
|
||||||
if filter:
|
if filter:
|
||||||
flat_filter = []
|
flat_filter = self._normalize_filter_arg(filter)
|
||||||
if isinstance(filter, str):
|
|
||||||
flat_filter = [filter]
|
|
||||||
elif isinstance(filter, list):
|
|
||||||
for item in filter:
|
|
||||||
if isinstance(item, str):
|
|
||||||
flat_filter.append(item)
|
|
||||||
else:
|
|
||||||
printer.error("Filter must be a string or a list of strings")
|
|
||||||
sys.exit(1)
|
|
||||||
flat_filter = ["^(?!.*@).+$" if item == "@" else item for item in flat_filter]
|
flat_filter = ["^(?!.*@).+$" if item == "@" else item for item in flat_filter]
|
||||||
nodes = {k: v for k, v in nodes.items() if any(re.search(pattern, k) for pattern in flat_filter)}
|
nodes = {k: v for k, v in nodes.items() if any(re.search(pattern, k) for pattern in flat_filter)}
|
||||||
if extract:
|
if extract:
|
||||||
for node, keys in nodes.items():
|
for node_dict in nodes.values():
|
||||||
for key, value in keys.items():
|
self._extract_node_fields_unlocked(node_dict)
|
||||||
profile = re.search("^@(.*)", str(value))
|
|
||||||
if profile:
|
|
||||||
try:
|
|
||||||
nodes[node][key] = self.profiles[profile.group(1)][key]
|
|
||||||
except KeyError:
|
|
||||||
nodes[node][key] = ""
|
|
||||||
elif value == '' and key == "protocol":
|
|
||||||
try:
|
|
||||||
nodes[node][key] = self.profiles["default"][key]
|
|
||||||
except KeyError:
|
|
||||||
nodes[node][key] = "ssh"
|
|
||||||
return nodes
|
return nodes
|
||||||
|
|
||||||
|
|
||||||
@MethodHook
|
@MethodHook
|
||||||
def _getallfolders(self):
|
def _getallnodesfull(self, filter = None, extract = True):
|
||||||
#get all folders on configfile
|
#get all nodes on configfile with all their attributes.
|
||||||
|
if filter and self._normalize_filter_arg(filter) is None:
|
||||||
|
printer.error("Filter must be a string or a list of strings")
|
||||||
|
sys.exit(1)
|
||||||
|
with self._mutation_lock:
|
||||||
|
return self._getallnodesfull_unlocked(filter=filter, extract=extract)
|
||||||
|
|
||||||
|
def _getallfolders_unlocked(self):
|
||||||
folders = ["@" + k for k, v in self.connections.items() if isinstance(v, dict) and v.get("type") == "folder"]
|
folders = ["@" + k for k, v in self.connections.items() if isinstance(v, dict) and v.get("type") == "folder"]
|
||||||
subfolders = []
|
subfolders = []
|
||||||
for f in folders:
|
for f in folders:
|
||||||
@@ -608,21 +968,62 @@ class configfile:
|
|||||||
return folders
|
return folders
|
||||||
|
|
||||||
@MethodHook
|
@MethodHook
|
||||||
def _profileused(self, profile):
|
def _getallfolders(self):
|
||||||
#Return all the nodes that uses this profile.
|
#get all folders on configfile
|
||||||
|
with self._mutation_lock:
|
||||||
|
return self._getallfolders_unlocked()
|
||||||
|
|
||||||
|
def _profileused_unlocked(self, profile):
|
||||||
|
target_profiles = {profile}
|
||||||
|
changed = True
|
||||||
|
while changed:
|
||||||
|
changed = False
|
||||||
|
for pname, pval in self.profiles.items():
|
||||||
|
if (
|
||||||
|
pname not in target_profiles
|
||||||
|
and isinstance(pval, dict)
|
||||||
|
and (extract_profile_refs_from_value(pval) & target_profiles)
|
||||||
|
):
|
||||||
|
target_profiles.add(pname)
|
||||||
|
changed = True
|
||||||
|
|
||||||
nodes = []
|
nodes = []
|
||||||
layer1 = [k for k,v in self.connections.items() if isinstance(v, dict) and v.get("type") == "connection" and ("@" + profile in v.values() or ( isinstance(v.get("password"),list) and "@" + profile in v.get("password")))]
|
layer1 = [
|
||||||
|
k
|
||||||
|
for k, v in self.connections.items()
|
||||||
|
if isinstance(v, dict)
|
||||||
|
and v.get("type") == "connection"
|
||||||
|
and bool(extract_profile_refs_from_value(v) & target_profiles)
|
||||||
|
]
|
||||||
folders = [k for k, v in self.connections.items() if isinstance(v, dict) and v.get("type") == "folder"]
|
folders = [k for k, v in self.connections.items() if isinstance(v, dict) and v.get("type") == "folder"]
|
||||||
nodes.extend(layer1)
|
nodes.extend(layer1)
|
||||||
for f in folders:
|
for f in folders:
|
||||||
layer2 = [k + "@" + f for k,v in self.connections[f].items() if isinstance(v, dict) and v.get("type") == "connection" and ("@" + profile in v.values() or ( isinstance(v.get("password"),list) and "@" + profile in v.get("password")))]
|
layer2 = [
|
||||||
|
k + "@" + f
|
||||||
|
for k, v in self.connections[f].items()
|
||||||
|
if isinstance(v, dict)
|
||||||
|
and v.get("type") == "connection"
|
||||||
|
and bool(extract_profile_refs_from_value(v) & target_profiles)
|
||||||
|
]
|
||||||
nodes.extend(layer2)
|
nodes.extend(layer2)
|
||||||
subfolders = [k for k, v in self.connections[f].items() if isinstance(v, dict) and v.get("type") == "subfolder"]
|
subfolders = [k for k, v in self.connections[f].items() if isinstance(v, dict) and v.get("type") == "subfolder"]
|
||||||
for s in subfolders:
|
for s in subfolders:
|
||||||
layer3 = [k + "@" + s + "@" + f for k,v in self.connections[f][s].items() if isinstance(v, dict) and v.get("type") == "connection" and ("@" + profile in v.values() or ( isinstance(v.get("password"),list) and "@" + profile in v.get("password")))]
|
layer3 = [
|
||||||
|
k + "@" + s + "@" + f
|
||||||
|
for k, v in self.connections[f][s].items()
|
||||||
|
if isinstance(v, dict)
|
||||||
|
and v.get("type") == "connection"
|
||||||
|
and bool(extract_profile_refs_from_value(v) & target_profiles)
|
||||||
|
]
|
||||||
nodes.extend(layer3)
|
nodes.extend(layer3)
|
||||||
return nodes
|
return nodes
|
||||||
|
|
||||||
|
@MethodHook
|
||||||
|
def _profileused(self, profile):
|
||||||
|
#Return all the nodes that uses this profile.
|
||||||
|
with self._mutation_lock:
|
||||||
|
return self._profileused_unlocked(profile)
|
||||||
|
|
||||||
@MethodHook
|
@MethodHook
|
||||||
def encrypt(self, password, keyfile=None):
|
def encrypt(self, password, keyfile=None):
|
||||||
'''
|
'''
|
||||||
|
|||||||
+14
-17
@@ -1,20 +1,20 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
#Imports
|
import argparse
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import argparse
|
|
||||||
import sys
|
import sys
|
||||||
import yaml
|
|
||||||
import sys
|
|
||||||
from .core import node,nodes
|
|
||||||
from ._version import __version__
|
|
||||||
from . import printer
|
|
||||||
from .api import start_api,stop_api,debug_api
|
|
||||||
|
|
||||||
|
import yaml
|
||||||
|
from rich_argparse import RichHelpFormatter
|
||||||
|
|
||||||
|
from . import printer
|
||||||
|
from .api import debug_api, start_api, stop_api
|
||||||
|
from .cli.help_text import get_help
|
||||||
|
from .cli.helpers import folders_completer, nodes_completer
|
||||||
|
from .core import node, nodes
|
||||||
from .plugins import Plugins
|
from .plugins import Plugins
|
||||||
from .services.exceptions import ConnpyError, ProfileNotFoundError, ReservedNameError
|
from .services.exceptions import ConnpyError, ProfileNotFoundError, ReservedNameError
|
||||||
|
|
||||||
from rich_argparse import RichHelpFormatter
|
|
||||||
# Bridge rich-argparse with our design system
|
# Bridge rich-argparse with our design system
|
||||||
RichHelpFormatter.console = printer.console
|
RichHelpFormatter.console = printer.console
|
||||||
RichHelpFormatter.styles.update({
|
RichHelpFormatter.styles.update({
|
||||||
@@ -28,14 +28,6 @@ RichHelpFormatter.styles.update({
|
|||||||
})
|
})
|
||||||
RichHelpFormatter.group_name_formatter = str.upper
|
RichHelpFormatter.group_name_formatter = str.upper
|
||||||
|
|
||||||
from .cli import (
|
|
||||||
NodeHandler, ProfileHandler, ConfigHandler, RunHandler,
|
|
||||||
AIHandler, APIHandler, PluginHandler, ImportExportHandler,
|
|
||||||
ContextHandler, SSOHandler
|
|
||||||
)
|
|
||||||
from .cli.helpers import nodes_completer, folders_completer, profiles_completer
|
|
||||||
from .cli.help_text import get_help
|
|
||||||
|
|
||||||
console = printer.console
|
console = printer.console
|
||||||
|
|
||||||
#functions and classes
|
#functions and classes
|
||||||
@@ -389,6 +381,11 @@ class connapp:
|
|||||||
configcrud.add_argument("--shell-command", dest="shell_command", nargs=1, action=self._store_type, help="Set default shell command", metavar="COMMAND")
|
configcrud.add_argument("--shell-command", dest="shell_command", nargs=1, action=self._store_type, help="Set default shell command", metavar="COMMAND")
|
||||||
configcrud.add_argument("--shell-prompt", dest="shell_prompt", nargs=1, action=self._store_type, help="Set shell prompt regex for AI", metavar="REGEX")
|
configcrud.add_argument("--shell-prompt", dest="shell_prompt", nargs=1, action=self._store_type, help="Set shell prompt regex for AI", metavar="REGEX")
|
||||||
configcrud.add_argument("--shell-os", dest="shell_os", nargs=1, action=self._store_type, help="Set shell OS hint for AI", metavar="OS")
|
configcrud.add_argument("--shell-os", dest="shell_os", nargs=1, action=self._store_type, help="Set shell OS hint for AI", metavar="OS")
|
||||||
|
configcrud.add_argument("--max-auth-prompts", dest="max_auth_prompts", nargs=1, action=self._store_type, help="Set max auth prompts per connection (1-5, 0 to reset)", type=int, metavar="INT")
|
||||||
|
configcrud.add_argument("--max-total-auth-attempts", dest="max_total_auth_attempts", nargs=1, action=self._store_type, help="Set max total auth attempts per host (1-10, 0 to reset)", type=int, metavar="INT")
|
||||||
|
configcrud.add_argument("--max-hop-host-attempts", dest="max_hop_host_attempts", nargs=1, action=self._store_type, help="Set max auth attempts per jumphost (1-10, 0 to reset)", type=int, metavar="INT")
|
||||||
|
configcrud.add_argument("--max-global-user-attempts", dest="max_global_user_attempts", nargs=1, action=self._store_type, help="Set max global auth attempts per user (1-1000, 0 to reset)", type=int, metavar="INT")
|
||||||
|
configcrud.add_argument("--disable-fallback-on-insecure-hostkey", dest="disable_fallback_on_insecure_hostkey", nargs=1, action=self._store_type, help="Disable AAA fallback when StrictHostKeyChecking=no", choices=["true", "false"])
|
||||||
configparser.add_argument("--trusted-commands", dest="trusted_commands", nargs=1, action=self._store_type, help="Set custom trusted commands regexes (comma separated)", metavar="REGEX,REGEX")
|
configparser.add_argument("--trusted-commands", dest="trusted_commands", nargs=1, action=self._store_type, help="Set custom trusted commands regexes (comma separated)", metavar="REGEX,REGEX")
|
||||||
configparser.set_defaults(func=self._config.dispatch)
|
configparser.set_defaults(func=self._config.dispatch)
|
||||||
|
|
||||||
|
|||||||
+1052
-125
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 contextvars
|
||||||
import datetime
|
import datetime
|
||||||
|
import functools
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
from concurrent import futures
|
||||||
|
|
||||||
|
import grpc
|
||||||
|
from google.protobuf.empty_pb2 import Empty
|
||||||
|
|
||||||
# Suppress harmless but noisy gRPC fork() warnings from pexpect child processes
|
# Suppress harmless but noisy gRPC fork() warnings from pexpect child processes
|
||||||
os.environ["GRPC_VERBOSITY"] = "NONE"
|
os.environ["GRPC_VERBOSITY"] = "NONE"
|
||||||
os.environ["GRPC_ENABLE_FORK_SUPPORT"] = "0"
|
os.environ["GRPC_ENABLE_FORK_SUPPORT"] = "0"
|
||||||
from . import connpy_pb2, connpy_pb2_grpc, remote_plugin_pb2, remote_plugin_pb2_grpc
|
|
||||||
import json
|
|
||||||
from .utils import to_value, from_value, to_struct, from_struct
|
|
||||||
from ..services.exceptions import ConnpyError
|
|
||||||
from .. import printer
|
from .. import printer
|
||||||
|
from ..auth_utils import (
|
||||||
|
MAX_FALLBACK_ENTRIES,
|
||||||
|
MAX_JSON_INPUT_BYTES,
|
||||||
|
MAX_JSON_INPUT_DEPTH,
|
||||||
|
SanitizedAuthError,
|
||||||
|
sanitize_exception,
|
||||||
|
validate_auth_budget_tags,
|
||||||
|
validate_fallback_entries,
|
||||||
|
validate_json_depth_and_size,
|
||||||
|
)
|
||||||
|
from ..services.exceptions import ConnpyError
|
||||||
|
from . import connpy_pb2, connpy_pb2_grpc, remote_plugin_pb2, remote_plugin_pb2_grpc
|
||||||
|
from .utils import from_struct, from_value, to_struct, to_value
|
||||||
|
|
||||||
_current_user = contextvars.ContextVar("current_user", default=None)
|
_current_user = contextvars.ContextVar("current_user", default=None)
|
||||||
|
|
||||||
def handle_errors(func):
|
def handle_errors(func):
|
||||||
import inspect
|
import inspect
|
||||||
if inspect.isgeneratorfunction(func):
|
if inspect.isgeneratorfunction(func):
|
||||||
|
@functools.wraps(func)
|
||||||
def wrapper(*args, **kwargs):
|
def wrapper(*args, **kwargs):
|
||||||
try:
|
try:
|
||||||
for item in func(*args, **kwargs):
|
for item in func(*args, **kwargs):
|
||||||
@@ -41,6 +52,7 @@ def handle_errors(func):
|
|||||||
printer.clear_thread_state()
|
printer.clear_thread_state()
|
||||||
return wrapper
|
return wrapper
|
||||||
else:
|
else:
|
||||||
|
@functools.wraps(func)
|
||||||
def wrapper(*args, **kwargs):
|
def wrapper(*args, **kwargs):
|
||||||
try:
|
try:
|
||||||
return func(*args, **kwargs)
|
return func(*args, **kwargs)
|
||||||
@@ -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]")
|
self.server_console.print(f"[debug][DEBUG][/debug] gRPC interact_node request for: [bold cyan]{unique_id}[/bold cyan]")
|
||||||
|
|
||||||
if first_req.connection_params_json:
|
if first_req.connection_params_json:
|
||||||
import json
|
try:
|
||||||
params = json.loads(first_req.connection_params_json)
|
params = validate_json_depth_and_size(
|
||||||
|
first_req.connection_params_json,
|
||||||
|
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||||
|
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||||
|
)
|
||||||
|
if not isinstance(params, dict):
|
||||||
|
raise TypeError("connection_params_json must be a JSON object")
|
||||||
|
if "fallback" in params and params["fallback"] not in (None, "", []):
|
||||||
|
validate_fallback_entries(
|
||||||
|
params["fallback"],
|
||||||
|
max_entries=MAX_FALLBACK_ENTRIES,
|
||||||
|
)
|
||||||
|
if "tags" in params and params["tags"] not in ("", None):
|
||||||
|
validate_auth_budget_tags(params["tags"])
|
||||||
|
except (ValueError, TypeError, SanitizedAuthError) as exc:
|
||||||
|
context.abort(grpc.StatusCode.INVALID_ARGUMENT, str(exc))
|
||||||
|
|
||||||
base_node_id = params.get("base_node")
|
base_node_id = params.get("base_node")
|
||||||
# Valid attributes that a node object accepts
|
# Valid attributes that a node object accepts
|
||||||
valid_attrs = ['host', 'options', 'logs', 'password', 'port', 'protocol', 'user', 'jumphost']
|
valid_attrs = ['host', 'options', 'logs', 'password', 'port', 'protocol', 'user', 'jumphost']
|
||||||
@@ -134,6 +162,8 @@ class NodeServicer(connpy_pb2_grpc.NodeServiceServicer):
|
|||||||
for attr in valid_attrs:
|
for attr in valid_attrs:
|
||||||
if attr in params:
|
if attr in params:
|
||||||
device[attr] = params[attr]
|
device[attr] = params[attr]
|
||||||
|
if "fallback" in params:
|
||||||
|
device["fallback"] = params["fallback"]
|
||||||
|
|
||||||
if "tags" in params:
|
if "tags" in params:
|
||||||
device_tags = device.get("tags", {})
|
device_tags = device.get("tags", {})
|
||||||
@@ -147,20 +177,30 @@ class NodeServicer(connpy_pb2_grpc.NodeServiceServicer):
|
|||||||
else:
|
else:
|
||||||
# base_node not found, fall back to dynamic
|
# base_node not found, fall back to dynamic
|
||||||
node_name = params.get("name", fallback_id)
|
node_name = params.get("name", fallback_id)
|
||||||
n = node(node_name, host=params.get("host", ""), config=user_config)
|
dyn_kwargs = {k: params[k] for k in valid_attrs if k in params and k != "host"}
|
||||||
for attr in valid_attrs:
|
if "fallback" in params:
|
||||||
if attr in params:
|
dyn_kwargs["fallback"] = params["fallback"]
|
||||||
setattr(n, attr, params[attr])
|
|
||||||
if "tags" in params:
|
if "tags" in params:
|
||||||
n.tags = params["tags"]
|
dyn_kwargs["tags"] = params["tags"]
|
||||||
|
n = node(
|
||||||
|
node_name,
|
||||||
|
host=params.get("host", ""),
|
||||||
|
config=user_config,
|
||||||
|
**dyn_kwargs,
|
||||||
|
)
|
||||||
else:
|
else:
|
||||||
node_name = params.get("name", fallback_id)
|
node_name = params.get("name", fallback_id)
|
||||||
n = node(node_name, host=params.get("host", ""), config=user_config)
|
dyn_kwargs = {k: params[k] for k in valid_attrs if k in params and k != "host"}
|
||||||
for attr in valid_attrs:
|
if "fallback" in params:
|
||||||
if attr in params:
|
dyn_kwargs["fallback"] = params["fallback"]
|
||||||
setattr(n, attr, params[attr])
|
|
||||||
if "tags" in params:
|
if "tags" in params:
|
||||||
n.tags = params["tags"]
|
dyn_kwargs["tags"] = params["tags"]
|
||||||
|
n = node(
|
||||||
|
node_name,
|
||||||
|
host=params.get("host", ""),
|
||||||
|
config=user_config,
|
||||||
|
**dyn_kwargs,
|
||||||
|
)
|
||||||
n._orig_protocol = n.protocol
|
n._orig_protocol = n.protocol
|
||||||
if sftp:
|
if sftp:
|
||||||
n.protocol = "sftp"
|
n.protocol = "sftp"
|
||||||
@@ -181,9 +221,11 @@ class NodeServicer(connpy_pb2_grpc.NodeServiceServicer):
|
|||||||
debug_chunks = []
|
debug_chunks = []
|
||||||
if debug:
|
if debug:
|
||||||
from io import StringIO
|
from io import StringIO
|
||||||
|
|
||||||
from rich.console import Console as RichConsole
|
from rich.console import Console as RichConsole
|
||||||
from ..printer import connpy_theme
|
|
||||||
from .. import printer as _printer
|
from .. import printer as _printer
|
||||||
|
from ..printer import connpy_theme
|
||||||
|
|
||||||
def remote_logger(msg_type, message):
|
def remote_logger(msg_type, message):
|
||||||
buf = StringIO()
|
buf = StringIO()
|
||||||
@@ -204,7 +246,14 @@ class NodeServicer(connpy_pb2_grpc.NodeServiceServicer):
|
|||||||
else:
|
else:
|
||||||
remote_logger = None
|
remote_logger = None
|
||||||
|
|
||||||
connect = n._connect(debug=debug, logger=remote_logger)
|
try:
|
||||||
|
connect = n._connect(
|
||||||
|
debug=debug,
|
||||||
|
logger=remote_logger,
|
||||||
|
attempt_tracker=getattr(provider, "auth_tracker", None),
|
||||||
|
)
|
||||||
|
except Exception as exc: # noqa: BLE001
|
||||||
|
connect = str(sanitize_exception(exc, code="AUTH_FAILED"))
|
||||||
|
|
||||||
# Send debug output to client before checking result (always show the command)
|
# Send debug output to client before checking result (always show the command)
|
||||||
for chunk in debug_chunks:
|
for chunk in debug_chunks:
|
||||||
@@ -535,10 +584,9 @@ class NodeServicer(connpy_pb2_grpc.NodeServiceServicer):
|
|||||||
continue
|
continue
|
||||||
|
|
||||||
asyncio.run(n._async_interact_loop(remote_stream, resize_callback, copilot_handler=remote_copilot_handler))
|
asyncio.run(n._async_interact_loop(remote_stream, resize_callback, copilot_handler=remote_copilot_handler))
|
||||||
except Exception as e:
|
except Exception as e: # noqa: BLE001
|
||||||
import traceback
|
sanitized_err = sanitize_exception(e, code="TRANSPORT_ERROR", context_label="interact stream")
|
||||||
print(f"[ERROR in run_async_loop] {e}")
|
print(f"[ERROR in run_async_loop] {sanitized_err}")
|
||||||
traceback.print_exc()
|
|
||||||
finally:
|
finally:
|
||||||
n._teardown_interact_environment()
|
n._teardown_interact_environment()
|
||||||
response_queue.put(None) # Signal EOF
|
response_queue.put(None) # Signal EOF
|
||||||
|
|||||||
+120
-50
@@ -6,6 +6,20 @@ from typing import Any, ClassVar
|
|||||||
|
|
||||||
import yaml
|
import yaml
|
||||||
|
|
||||||
|
from .auth_utils import (
|
||||||
|
MAX_FALLBACK_ENTRIES,
|
||||||
|
MAX_JSON_INPUT_BYTES,
|
||||||
|
MAX_JSON_INPUT_DEPTH,
|
||||||
|
SanitizedAuthError,
|
||||||
|
is_compose_expression,
|
||||||
|
mask_node_secrets,
|
||||||
|
sanitize_exception,
|
||||||
|
validate_auth_budget_tags,
|
||||||
|
validate_compose_syntax,
|
||||||
|
validate_fallback_entries,
|
||||||
|
validate_json_depth_and_size,
|
||||||
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from mcp.server.mcpserver import MCPServer
|
from mcp.server.mcpserver import MCPServer
|
||||||
except ImportError:
|
except ImportError:
|
||||||
@@ -124,20 +138,7 @@ class ConnpyMCPServer:
|
|||||||
|
|
||||||
def _mask_secrets(self, val: Any) -> Any:
|
def _mask_secrets(self, val: Any) -> Any:
|
||||||
"""Recursively mask passwords, passphrases, secrets, and auth tokens with '***'."""
|
"""Recursively mask passwords, passphrases, secrets, and auth tokens with '***'."""
|
||||||
if isinstance(val, dict):
|
return mask_node_secrets(val)
|
||||||
masked = {}
|
|
||||||
for k, v in val.items():
|
|
||||||
k_lower = str(k).lower()
|
|
||||||
if any(sec in k_lower for sec in ["password", "passphrase", "secret", "token"]):
|
|
||||||
masked[k] = "***"
|
|
||||||
elif isinstance(v, (dict, list)):
|
|
||||||
masked[k] = self._mask_secrets(v)
|
|
||||||
else:
|
|
||||||
masked[k] = v
|
|
||||||
return masked
|
|
||||||
if isinstance(val, list):
|
|
||||||
return [self._mask_secrets(item) for item in val]
|
|
||||||
return val
|
|
||||||
|
|
||||||
# -------------------------------------------------------------------------
|
# -------------------------------------------------------------------------
|
||||||
# Level 1: Read-Only Inventory and Context Tools
|
# Level 1: Read-Only Inventory and Context Tools
|
||||||
@@ -196,6 +197,31 @@ class ConnpyMCPServer:
|
|||||||
# Level 2: Execution, Testing, and Playbooks Tools
|
# Level 2: Execution, Testing, and Playbooks Tools
|
||||||
# -------------------------------------------------------------------------
|
# -------------------------------------------------------------------------
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _parse_json_list_or_lines(raw_val: list[str] | str, *, allow_empty_str: bool = False) -> list[str]:
|
||||||
|
if isinstance(raw_val, str):
|
||||||
|
if len(raw_val.encode("utf-8", errors="replace")) > MAX_JSON_INPUT_BYTES:
|
||||||
|
raise ValueError(f"Input exceeds maximum size of {MAX_JSON_INPUT_BYTES} bytes")
|
||||||
|
stripped = raw_val.strip()
|
||||||
|
if stripped.startswith(("[", "{")):
|
||||||
|
parsed = validate_json_depth_and_size(
|
||||||
|
stripped,
|
||||||
|
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||||
|
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||||
|
)
|
||||||
|
return parsed if isinstance(parsed, list) else [raw_val]
|
||||||
|
return ([raw_val] if raw_val else []) if allow_empty_str else [
|
||||||
|
c.strip() for c in raw_val.splitlines() if c.strip()
|
||||||
|
]
|
||||||
|
if isinstance(raw_val, list):
|
||||||
|
validate_json_depth_and_size(
|
||||||
|
raw_val,
|
||||||
|
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||||
|
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||||
|
)
|
||||||
|
return raw_val
|
||||||
|
return [str(raw_val)] if (raw_val or not allow_empty_str) else []
|
||||||
|
|
||||||
def run_commands(
|
def run_commands(
|
||||||
self,
|
self,
|
||||||
nodes_filter: str,
|
nodes_filter: str,
|
||||||
@@ -210,17 +236,10 @@ class ConnpyMCPServer:
|
|||||||
if not self.services or not hasattr(self.services, "execution"):
|
if not self.services or not hasattr(self.services, "execution"):
|
||||||
return {"success": False, "error": "Execution service unavailable."}
|
return {"success": False, "error": "Execution service unavailable."}
|
||||||
|
|
||||||
# Normalize commands list
|
|
||||||
if isinstance(commands, str):
|
|
||||||
try:
|
try:
|
||||||
import json
|
commands = self._parse_json_list_or_lines(commands, allow_empty_str=False)
|
||||||
|
except ValueError as exc:
|
||||||
parsed = json.loads(commands)
|
return {"success": False, "error": str(exc)}
|
||||||
commands = parsed if isinstance(parsed, list) else [commands]
|
|
||||||
except Exception: # noqa: BLE001
|
|
||||||
commands = [c.strip() for c in commands.splitlines() if c.strip()]
|
|
||||||
elif not isinstance(commands, list):
|
|
||||||
commands = [str(commands)]
|
|
||||||
|
|
||||||
# Check safety
|
# Check safety
|
||||||
if not self.trust:
|
if not self.trust:
|
||||||
@@ -239,9 +258,24 @@ class ConnpyMCPServer:
|
|||||||
parallel=par_count,
|
parallel=par_count,
|
||||||
timeout=timeout,
|
timeout=timeout,
|
||||||
)
|
)
|
||||||
return {"success": True, "results": results}
|
fb_used = False
|
||||||
|
if isinstance(results, dict):
|
||||||
|
fb_used = any(
|
||||||
|
isinstance(v, dict) and bool(v.get("fallback_used", False))
|
||||||
|
for v in results.values()
|
||||||
|
)
|
||||||
|
if not fb_used:
|
||||||
|
fb_map = getattr(self.services.execution, "last_fallback_used", {})
|
||||||
|
if isinstance(fb_map, dict):
|
||||||
|
fb_used = any(bool(v) for v in fb_map.values())
|
||||||
|
return {"success": True, "results": results, "fallback_used": fb_used}
|
||||||
except Exception as e: # noqa: BLE001
|
except Exception as e: # noqa: BLE001
|
||||||
return {"success": False, "error": str(e)}
|
san = sanitize_exception(
|
||||||
|
e,
|
||||||
|
code="AUTH_FAILED",
|
||||||
|
context_label="command execution failed",
|
||||||
|
)
|
||||||
|
return {"success": False, "error": str(san)}
|
||||||
|
|
||||||
def test_commands(
|
def test_commands(
|
||||||
self,
|
self,
|
||||||
@@ -255,29 +289,11 @@ class ConnpyMCPServer:
|
|||||||
if not self.services or not hasattr(self.services, "execution"):
|
if not self.services or not hasattr(self.services, "execution"):
|
||||||
return {"success": False, "error": "Execution service unavailable."}
|
return {"success": False, "error": "Execution service unavailable."}
|
||||||
|
|
||||||
# Normalize commands
|
|
||||||
if isinstance(commands, str):
|
|
||||||
try:
|
try:
|
||||||
import json
|
commands = self._parse_json_list_or_lines(commands, allow_empty_str=False)
|
||||||
|
expected = self._parse_json_list_or_lines(expected, allow_empty_str=True)
|
||||||
parsed = json.loads(commands)
|
except ValueError as exc:
|
||||||
commands = parsed if isinstance(parsed, list) else [commands]
|
return {"success": False, "error": str(exc)}
|
||||||
except Exception: # noqa: BLE001
|
|
||||||
commands = [c.strip() for c in commands.splitlines() if c.strip()]
|
|
||||||
elif not isinstance(commands, list):
|
|
||||||
commands = [str(commands)]
|
|
||||||
|
|
||||||
# Normalize expected
|
|
||||||
if isinstance(expected, str):
|
|
||||||
try:
|
|
||||||
import json
|
|
||||||
|
|
||||||
parsed = json.loads(expected)
|
|
||||||
expected = parsed if isinstance(parsed, list) else ([expected] if expected else [])
|
|
||||||
except Exception: # noqa: BLE001
|
|
||||||
expected = [expected] if expected else []
|
|
||||||
elif not isinstance(expected, list):
|
|
||||||
expected = [str(expected)] if expected else []
|
|
||||||
|
|
||||||
# Check safety
|
# Check safety
|
||||||
if not self.trust:
|
if not self.trust:
|
||||||
@@ -297,9 +313,16 @@ class ConnpyMCPServer:
|
|||||||
parallel=par_count,
|
parallel=par_count,
|
||||||
timeout=timeout,
|
timeout=timeout,
|
||||||
)
|
)
|
||||||
return {"success": True, "results": results}
|
fb_map = getattr(self.services.execution, "last_fallback_used", {})
|
||||||
|
fb_used = any(bool(v) for v in fb_map.values()) if isinstance(fb_map, dict) else False
|
||||||
|
return {"success": True, "results": results, "fallback_used": fb_used}
|
||||||
except Exception as e: # noqa: BLE001
|
except Exception as e: # noqa: BLE001
|
||||||
return {"success": False, "error": str(e)}
|
san = sanitize_exception(
|
||||||
|
e,
|
||||||
|
code="AUTH_FAILED",
|
||||||
|
context_label="test execution failed",
|
||||||
|
)
|
||||||
|
return {"success": False, "error": str(san)}
|
||||||
|
|
||||||
def validate_playbook(self, playbook_yaml: str) -> dict:
|
def validate_playbook(self, playbook_yaml: str) -> dict:
|
||||||
"""Validate the syntax, schema, and tasks of a Connpy YAML playbook."""
|
"""Validate the syntax, schema, and tasks of a Connpy YAML playbook."""
|
||||||
@@ -427,6 +450,47 @@ class ConnpyMCPServer:
|
|||||||
# Level 3: Inventory Mutation Tools (CRUD - Requires trust=True)
|
# Level 3: Inventory Mutation Tools (CRUD - Requires trust=True)
|
||||||
# -------------------------------------------------------------------------
|
# -------------------------------------------------------------------------
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _validate_and_normalize_node_payload(node_data: dict[str, Any]) -> dict[str, Any]:
|
||||||
|
validate_json_depth_and_size(
|
||||||
|
node_data,
|
||||||
|
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||||
|
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||||
|
)
|
||||||
|
normalized = node_data.copy()
|
||||||
|
if "tags" in normalized and normalized["tags"] not in ("", None):
|
||||||
|
tags_val = normalized["tags"]
|
||||||
|
if isinstance(tags_val, str) and tags_val.strip().startswith("{"):
|
||||||
|
tags_val = validate_json_depth_and_size(
|
||||||
|
tags_val,
|
||||||
|
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||||
|
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||||
|
)
|
||||||
|
normalized["tags"] = tags_val
|
||||||
|
validate_auth_budget_tags(tags_val)
|
||||||
|
|
||||||
|
if "fallback" in normalized and normalized["fallback"] not in (None, "", []):
|
||||||
|
fb_val = normalized["fallback"]
|
||||||
|
if isinstance(fb_val, str) and fb_val.strip().startswith(("[", "{")):
|
||||||
|
fb_val = validate_json_depth_and_size(
|
||||||
|
fb_val,
|
||||||
|
max_bytes=MAX_JSON_INPUT_BYTES,
|
||||||
|
max_depth=MAX_JSON_INPUT_DEPTH,
|
||||||
|
)
|
||||||
|
normalized["fallback"] = validate_fallback_entries(
|
||||||
|
fb_val,
|
||||||
|
max_entries=MAX_FALLBACK_ENTRIES,
|
||||||
|
)
|
||||||
|
|
||||||
|
pw = normalized.get("password")
|
||||||
|
if is_compose_expression(pw):
|
||||||
|
validate_compose_syntax(pw)
|
||||||
|
elif isinstance(pw, (list, tuple)):
|
||||||
|
for item in pw:
|
||||||
|
if is_compose_expression(item):
|
||||||
|
validate_compose_syntax(item)
|
||||||
|
return normalized
|
||||||
|
|
||||||
def add_node(
|
def add_node(
|
||||||
self, unique_id: str, data: dict | None = None, is_folder: bool = False, **kwargs
|
self, unique_id: str, data: dict | None = None, is_folder: bool = False, **kwargs
|
||||||
) -> dict:
|
) -> dict:
|
||||||
@@ -444,11 +508,14 @@ class ConnpyMCPServer:
|
|||||||
node_data.update(kwargs)
|
node_data.update(kwargs)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
node_data = self._validate_and_normalize_node_payload(node_data)
|
||||||
self.services.nodes.add_node(unique_id, node_data, is_folder=is_folder)
|
self.services.nodes.add_node(unique_id, node_data, is_folder=is_folder)
|
||||||
return {
|
return {
|
||||||
"success": True,
|
"success": True,
|
||||||
"message": f"{'Folder' if is_folder else 'Node'} '{unique_id}' created successfully.",
|
"message": f"{'Folder' if is_folder else 'Node'} '{unique_id}' created successfully.",
|
||||||
}
|
}
|
||||||
|
except (ValueError, TypeError, SanitizedAuthError) as exc:
|
||||||
|
return {"success": False, "error": str(exc)}
|
||||||
except Exception as e: # noqa: BLE001
|
except Exception as e: # noqa: BLE001
|
||||||
return {"success": False, "error": str(e)}
|
return {"success": False, "error": str(e)}
|
||||||
|
|
||||||
@@ -467,8 +534,11 @@ class ConnpyMCPServer:
|
|||||||
node_updates.update(kwargs)
|
node_updates.update(kwargs)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
node_updates = self._validate_and_normalize_node_payload(node_updates)
|
||||||
self.services.nodes.update_node(unique_id, node_updates)
|
self.services.nodes.update_node(unique_id, node_updates)
|
||||||
return {"success": True, "message": f"Node '{unique_id}' updated successfully."}
|
return {"success": True, "message": f"Node '{unique_id}' updated successfully."}
|
||||||
|
except (ValueError, TypeError, SanitizedAuthError) as exc:
|
||||||
|
return {"success": False, "error": str(exc)}
|
||||||
except Exception as e: # noqa: BLE001
|
except Exception as e: # noqa: BLE001
|
||||||
return {"success": False, "error": str(e)}
|
return {"success": False, "error": str(e)}
|
||||||
|
|
||||||
|
|||||||
@@ -1,7 +1,10 @@
|
|||||||
|
import inspect
|
||||||
import os
|
import os
|
||||||
from collections.abc import Callable
|
from collections.abc import Callable
|
||||||
|
from contextlib import suppress
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
|
from connpy.auth_utils import AuthAttemptTracker, raise_sanitized_auth_error
|
||||||
from connpy.core import nodes as Nodes
|
from connpy.core import nodes as Nodes
|
||||||
|
|
||||||
from .base import BaseService
|
from .base import BaseService
|
||||||
@@ -11,9 +14,13 @@ from .exceptions import ConnpyError
|
|||||||
class ExecutionService(BaseService):
|
class ExecutionService(BaseService):
|
||||||
"""Business logic for executing commands on nodes and running automation scripts."""
|
"""Business logic for executing commands on nodes and running automation scripts."""
|
||||||
|
|
||||||
def __init__(self, config=None, remote_stub=None):
|
def __init__(self, config=None, remote_stub=None, attempt_tracker=None):
|
||||||
super().__init__(config)
|
super().__init__(config)
|
||||||
self.remote_stub = remote_stub
|
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:
|
def _is_remote_backend(self) -> bool:
|
||||||
"""Return True when execution is configured to run against a remote gRPC backend."""
|
"""Return True when execution is configured to run against a remote gRPC backend."""
|
||||||
@@ -54,11 +61,8 @@ class ExecutionService(BaseService):
|
|||||||
if defaultdir:
|
if defaultdir:
|
||||||
token_path = os.path.join(defaultdir, ".token")
|
token_path = os.path.join(defaultdir, ".token")
|
||||||
if os.path.exists(token_path):
|
if os.path.exists(token_path):
|
||||||
try:
|
with suppress(OSError), open(token_path) as f:
|
||||||
with open(token_path) as f:
|
|
||||||
return f.read().strip()
|
return f.read().strip()
|
||||||
except OSError:
|
|
||||||
pass
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
channel = grpc.insecure_channel(remote_host)
|
channel = grpc.insecure_channel(remote_host)
|
||||||
@@ -110,33 +114,47 @@ class ExecutionService(BaseService):
|
|||||||
executor = Nodes(node_data, config=self.config)
|
executor = Nodes(node_data, config=self.config)
|
||||||
self.last_executor = executor
|
self.last_executor = executor
|
||||||
|
|
||||||
results = executor.run(
|
run_kwargs: dict[str, Any] = {
|
||||||
commands=commands,
|
"commands": commands,
|
||||||
vars=variables,
|
"vars": variables,
|
||||||
parallel=parallel,
|
"parallel": parallel,
|
||||||
timeout=timeout,
|
"timeout": timeout,
|
||||||
folder=folder,
|
"folder": folder,
|
||||||
prompt=prompt,
|
"prompt": prompt,
|
||||||
on_complete=on_node_complete,
|
"on_complete": on_node_complete,
|
||||||
logger=logger,
|
"logger": logger,
|
||||||
connect_timeout=connect_timeout,
|
"connect_timeout": connect_timeout,
|
||||||
auth_timeout=auth_timeout,
|
"auth_timeout": auth_timeout,
|
||||||
_default_cmd_timeout=20,
|
"_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 = {}
|
full_results = {}
|
||||||
|
self.last_fallback_used = {}
|
||||||
for unique in results:
|
for unique in results:
|
||||||
|
fb_used = bool(fb_dict.get(unique, False))
|
||||||
full_results[unique] = {
|
full_results[unique] = {
|
||||||
"output": results[unique],
|
"output": results[unique],
|
||||||
"status": executor.status.get(unique, 1),
|
"status": executor.status.get(unique, 1),
|
||||||
|
"fallback_used": fb_used,
|
||||||
}
|
}
|
||||||
|
self.last_fallback_used[unique] = fb_used
|
||||||
|
|
||||||
return full_results
|
return full_results
|
||||||
except ConnpyError:
|
except ConnpyError:
|
||||||
raise
|
raise
|
||||||
except Exception as e:
|
except Exception as e: # noqa: BLE001
|
||||||
raise ConnpyError(f"Execution failed: {e}") from e
|
raise_sanitized_auth_error(e, code="AUTH_FAILED", context_label="execution failed")
|
||||||
|
|
||||||
def test_commands(
|
def test_commands(
|
||||||
self,
|
self,
|
||||||
@@ -185,24 +203,38 @@ class ExecutionService(BaseService):
|
|||||||
executor = Nodes(node_data, config=self.config)
|
executor = Nodes(node_data, config=self.config)
|
||||||
self.last_executor = executor
|
self.last_executor = executor
|
||||||
|
|
||||||
return executor.test(
|
test_kwargs: dict[str, Any] = {
|
||||||
commands=commands,
|
"commands": commands,
|
||||||
expected=expected,
|
"expected": expected,
|
||||||
vars=variables,
|
"vars": variables,
|
||||||
parallel=parallel,
|
"parallel": parallel,
|
||||||
timeout=timeout,
|
"timeout": timeout,
|
||||||
folder=folder,
|
"folder": folder,
|
||||||
prompt=prompt,
|
"prompt": prompt,
|
||||||
on_complete=on_node_complete,
|
"on_complete": on_node_complete,
|
||||||
logger=logger,
|
"logger": logger,
|
||||||
connect_timeout=connect_timeout,
|
"connect_timeout": connect_timeout,
|
||||||
auth_timeout=auth_timeout,
|
"auth_timeout": auth_timeout,
|
||||||
_default_cmd_timeout=20,
|
"_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:
|
except ConnpyError:
|
||||||
raise
|
raise
|
||||||
except Exception as e:
|
except Exception as e: # noqa: BLE001
|
||||||
raise ConnpyError(f"Testing failed: {e}") from e
|
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]:
|
def run_cli_script(self, nodes_filter: str, script_path: str, parallel: int = 10) -> dict[str, str]:
|
||||||
"""Run a plain-text script containing one command per line."""
|
"""Run a plain-text script containing one command per line."""
|
||||||
@@ -212,8 +244,8 @@ class ExecutionService(BaseService):
|
|||||||
try:
|
try:
|
||||||
with open(script_path) as f:
|
with open(script_path) as f:
|
||||||
commands = [line.strip() for line in f if line.strip()]
|
commands = [line.strip() for line in f if line.strip()]
|
||||||
except OSError as e:
|
except OSError:
|
||||||
raise ConnpyError(f"Failed to read script {script_path}: {e}") from e
|
raise ConnpyError("Failed to read script file") from None
|
||||||
|
|
||||||
return self.run_commands(nodes_filter, commands, parallel=parallel)
|
return self.run_commands(nodes_filter, commands, parallel=parallel)
|
||||||
|
|
||||||
+238
-19
@@ -1,14 +1,73 @@
|
|||||||
|
import inspect
|
||||||
import re
|
import re
|
||||||
|
from copy import deepcopy
|
||||||
|
from typing import ClassVar
|
||||||
|
|
||||||
from .base import BaseService
|
from .base import BaseService
|
||||||
from .exceptions import (
|
from .exceptions import (
|
||||||
NodeNotFoundError, NodeAlreadyExistsError,
|
InvalidConfigurationError,
|
||||||
InvalidConfigurationError, ReservedNameError
|
NodeAlreadyExistsError,
|
||||||
|
NodeNotFoundError,
|
||||||
|
ReservedNameError,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _supports_transactional_update(cfg):
|
||||||
|
return (
|
||||||
|
cfg is not None
|
||||||
|
and not hasattr(cfg, "assert_called")
|
||||||
|
and callable(getattr(cfg, "transactional_update", None))
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class NodeService(BaseService):
|
class NodeService(BaseService):
|
||||||
|
_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):
|
def __init__(self, config=None):
|
||||||
super().__init__(config)
|
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):
|
def list_nodes(self, filter_str=None, format_str=None):
|
||||||
"""Return a listed filtered by regex match and formatted if needed."""
|
"""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.")
|
raise NodeNotFoundError(f"Node '{unique_id}' not found.")
|
||||||
return details
|
return details
|
||||||
except (KeyError, TypeError):
|
except (KeyError, TypeError):
|
||||||
raise NodeNotFoundError(f"Node '{unique_id}' not found.")
|
raise NodeNotFoundError(f"Node '{unique_id}' not found.") from None
|
||||||
|
|
||||||
def explode_unique(self, unique_id):
|
def explode_unique(self, unique_id):
|
||||||
"""Explode a unique ID into a dictionary of its parts."""
|
"""Explode a unique ID into a dictionary of its parts."""
|
||||||
@@ -115,6 +174,40 @@ class NodeService(BaseService):
|
|||||||
"""Logic for adding a new node or folder to configuration."""
|
"""Logic for adding a new node or folder to configuration."""
|
||||||
if not is_folder:
|
if not is_folder:
|
||||||
self._validate_node_name(unique_id)
|
self._validate_node_name(unique_id)
|
||||||
|
self._validate_node_payload(data)
|
||||||
|
|
||||||
|
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_nodes = self.config._getallnodes()
|
||||||
all_folders = self.config._getallfolders()
|
all_folders = self.config._getallfolders()
|
||||||
@@ -126,7 +219,6 @@ class NodeService(BaseService):
|
|||||||
if not uniques:
|
if not uniques:
|
||||||
raise InvalidConfigurationError(f"Invalid folder name '{unique_id}'.")
|
raise InvalidConfigurationError(f"Invalid folder name '{unique_id}'.")
|
||||||
|
|
||||||
# Check if parent folder exists when creating a subfolder
|
|
||||||
if "subfolder" in uniques:
|
if "subfolder" in uniques:
|
||||||
self.validate_parent_folder(unique_id, is_folder=True)
|
self.validate_parent_folder(unique_id, is_folder=True)
|
||||||
|
|
||||||
@@ -136,37 +228,73 @@ class NodeService(BaseService):
|
|||||||
if unique_id in all_nodes:
|
if unique_id in all_nodes:
|
||||||
raise NodeAlreadyExistsError(f"Node '{unique_id}' already exists.")
|
raise NodeAlreadyExistsError(f"Node '{unique_id}' already exists.")
|
||||||
|
|
||||||
# Check if parent folder exists when creating a node in a folder
|
|
||||||
self.validate_parent_folder(unique_id)
|
self.validate_parent_folder(unique_id)
|
||||||
|
|
||||||
# Ensure 'id' is in data for config._connections_add
|
payload = deepcopy(data)
|
||||||
if "id" not in data:
|
if "id" not in payload:
|
||||||
uniques = self.config._explode_unique(unique_id)
|
uniques = self.config._explode_unique(unique_id)
|
||||||
if uniques and "id" in uniques:
|
if uniques and "id" in uniques:
|
||||||
data["id"] = uniques["id"]
|
payload["id"] = uniques["id"]
|
||||||
|
|
||||||
self.config._connections_add(**data)
|
self.config._connections_add(**payload)
|
||||||
self.config._saveconfig(self.config.file)
|
self.config._saveconfig(self.config.file)
|
||||||
|
|
||||||
|
def _build_update_payload(self, unique_id, data, existing, uniques):
|
||||||
|
if isinstance(existing, dict):
|
||||||
|
merged = deepcopy(existing)
|
||||||
|
if isinstance(uniques, dict):
|
||||||
|
merged.update({k: v for k, v in uniques.items() if v})
|
||||||
|
merged.update(deepcopy(data))
|
||||||
|
filtered = {k: v for k, v in merged.items() if k in self._CONNECTION_FIELDS}
|
||||||
|
else:
|
||||||
|
filtered = deepcopy(data)
|
||||||
|
if "id" not in filtered and isinstance(uniques, dict) and "id" in uniques:
|
||||||
|
filtered["id"] = uniques["id"]
|
||||||
|
self._validate_node_payload(filtered)
|
||||||
|
return filtered
|
||||||
|
|
||||||
def update_node(self, unique_id, data, save=True):
|
def update_node(self, unique_id, data, save=True):
|
||||||
"""Explicitly update an existing node."""
|
"""Explicitly update an existing node."""
|
||||||
|
if _supports_transactional_update(self.config) and save:
|
||||||
|
def _mut(cfg):
|
||||||
|
all_nodes = cfg._getallnodes_unlocked()
|
||||||
|
if unique_id not in all_nodes:
|
||||||
|
raise NodeNotFoundError(f"Node '{unique_id}' not found.")
|
||||||
|
uniques = cfg._explode_unique_unlocked(unique_id)
|
||||||
|
existing = cfg.getitem_unlocked(unique_id, extract=False)
|
||||||
|
payload = self._build_update_payload(unique_id, data, existing, uniques)
|
||||||
|
cfg._connections_add_unlocked(**payload)
|
||||||
|
|
||||||
|
self.config.transactional_update(_mut)
|
||||||
|
return
|
||||||
|
|
||||||
all_nodes = self.config._getallnodes()
|
all_nodes = self.config._getallnodes()
|
||||||
if unique_id not in all_nodes:
|
if unique_id not in all_nodes:
|
||||||
raise NodeNotFoundError(f"Node '{unique_id}' not found.")
|
raise NodeNotFoundError(f"Node '{unique_id}' not found.")
|
||||||
|
|
||||||
# Ensure 'id' is in data for config._connections_add
|
|
||||||
if "id" not in data:
|
|
||||||
uniques = self.config._explode_unique(unique_id)
|
uniques = self.config._explode_unique(unique_id)
|
||||||
if uniques:
|
existing = self.config.getitem(unique_id, extract=False)
|
||||||
data["id"] = uniques["id"]
|
payload = self._build_update_payload(unique_id, data, existing, uniques)
|
||||||
|
self.config._connections_add(**payload)
|
||||||
# config._connections_add actually handles updates if ID exists correctly
|
|
||||||
self.config._connections_add(**data)
|
|
||||||
if save:
|
if save:
|
||||||
self.config._saveconfig(self.config.file)
|
self.config._saveconfig(self.config.file)
|
||||||
|
|
||||||
def delete_node(self, unique_id, is_folder=False, save=True):
|
def delete_node(self, unique_id, is_folder=False, save=True):
|
||||||
"""Logic for deleting a node or folder."""
|
"""Logic for deleting a node or folder."""
|
||||||
|
if _supports_transactional_update(self.config) and save:
|
||||||
|
def _mut(cfg):
|
||||||
|
uniques = cfg._explode_unique_unlocked(unique_id)
|
||||||
|
if not uniques:
|
||||||
|
label = "Folder" if is_folder else "Node"
|
||||||
|
raise NodeNotFoundError(f"{label} '{unique_id}' not found or invalid.")
|
||||||
|
if is_folder:
|
||||||
|
cfg._folder_del_unlocked(**uniques)
|
||||||
|
else:
|
||||||
|
cfg._connections_del_unlocked(**uniques)
|
||||||
|
|
||||||
|
self.config.transactional_update(_mut)
|
||||||
|
return
|
||||||
|
|
||||||
if is_folder:
|
if is_folder:
|
||||||
uniques = self.config._explode_unique(unique_id)
|
uniques = self.config._explode_unique(unique_id)
|
||||||
if not uniques:
|
if not uniques:
|
||||||
@@ -184,16 +312,33 @@ class NodeService(BaseService):
|
|||||||
def connect_node(self, unique_id, sftp=False, debug=False, logger=None):
|
def connect_node(self, unique_id, sftp=False, debug=False, logger=None):
|
||||||
"""Interact with a node directly."""
|
"""Interact with a node directly."""
|
||||||
from connpy.core import node
|
from connpy.core import node
|
||||||
|
|
||||||
|
from ..auth_utils import resolve_fallback_entries
|
||||||
from .profile_service import ProfileService
|
from .profile_service import ProfileService
|
||||||
|
|
||||||
node_data = self.config.getitem(unique_id, extract=False)
|
node_data = self.config.getitem(unique_id, extract=False)
|
||||||
if not node_data:
|
if not node_data:
|
||||||
raise NodeNotFoundError(f"Node '{unique_id}' not found.")
|
raise NodeNotFoundError(f"Node '{unique_id}' not found.")
|
||||||
|
|
||||||
# Resolve profiles
|
|
||||||
profile_service = ProfileService(self.config)
|
profile_service = ProfileService(self.config)
|
||||||
resolved_data = profile_service.resolve_node_data(node_data)
|
resolved_data = profile_service.resolve_node_data(node_data)
|
||||||
|
|
||||||
|
node_params = inspect.signature(node.__init__).parameters
|
||||||
|
if "fallback" not in node_params and "fallback" in resolved_data:
|
||||||
|
raw_fb = resolved_data.pop("fallback", None)
|
||||||
|
if raw_fb not in (None, "", []):
|
||||||
|
resolved_fb = resolve_fallback_entries(raw_fb, self.config.profiles)
|
||||||
|
if resolved_fb:
|
||||||
|
tags_val = resolved_data.get("tags", "")
|
||||||
|
if isinstance(tags_val, dict):
|
||||||
|
tags_copy = deepcopy(tags_val)
|
||||||
|
tags_copy["_fallback"] = resolved_fb
|
||||||
|
resolved_data["tags"] = tags_copy
|
||||||
|
elif tags_val in ("", None):
|
||||||
|
resolved_data["tags"] = {"_fallback": resolved_fb}
|
||||||
|
else:
|
||||||
|
resolved_data["tags"] = {"_raw": tags_val, "_fallback": resolved_fb}
|
||||||
|
|
||||||
n = node(unique_id, **resolved_data, config=self.config)
|
n = node(unique_id, **resolved_data, config=self.config)
|
||||||
if sftp:
|
if sftp:
|
||||||
n.protocol = "sftp"
|
n.protocol = "sftp"
|
||||||
@@ -204,6 +349,29 @@ class NodeService(BaseService):
|
|||||||
"""Move or copy a node."""
|
"""Move or copy a node."""
|
||||||
self._validate_node_name(dst_id)
|
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)
|
node_data = self.config.getitem(src_id)
|
||||||
if not node_data:
|
if not node_data:
|
||||||
raise NodeNotFoundError(f"Source node '{src_id}' not found.")
|
raise NodeNotFoundError(f"Source node '{src_id}' not found.")
|
||||||
@@ -228,6 +396,44 @@ class NodeService(BaseService):
|
|||||||
|
|
||||||
def bulk_add(self, ids, hosts, common_data):
|
def bulk_add(self, ids, hosts, common_data):
|
||||||
"""Add multiple nodes with shared common configuration."""
|
"""Add multiple nodes with shared common configuration."""
|
||||||
|
self._validate_node_payload(common_data)
|
||||||
|
|
||||||
|
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
|
count = 0
|
||||||
all_nodes = self.config._getallnodes()
|
all_nodes = self.config._getallnodes()
|
||||||
|
|
||||||
@@ -238,8 +444,6 @@ class NodeService(BaseService):
|
|||||||
try:
|
try:
|
||||||
self._validate_node_name(uid)
|
self._validate_node_name(uid)
|
||||||
except ReservedNameError:
|
except ReservedNameError:
|
||||||
# For bulk, we might want to just skip or log.
|
|
||||||
# CLI caller will handle if it wants to be strict.
|
|
||||||
continue
|
continue
|
||||||
|
|
||||||
host = hosts[i] if i < len(hosts) else hosts[0]
|
host = hosts[i] if i < len(hosts) else hosts[0]
|
||||||
@@ -263,6 +467,21 @@ class NodeService(BaseService):
|
|||||||
|
|
||||||
def full_replace(self, connections, profiles):
|
def full_replace(self, connections, profiles):
|
||||||
"""Replace all connections and profiles with new data."""
|
"""Replace all connections and profiles with new data."""
|
||||||
|
from ..auth_utils import SanitizedAuthError
|
||||||
|
|
||||||
|
if _supports_transactional_update(self.config):
|
||||||
|
def _mut(cfg):
|
||||||
|
cfg.connections.clear()
|
||||||
|
cfg.connections.update(deepcopy(connections))
|
||||||
|
cfg.profiles.clear()
|
||||||
|
cfg.profiles.update(deepcopy(profiles))
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.config.transactional_update(_mut)
|
||||||
|
except (ValueError, TypeError, SanitizedAuthError) as exc:
|
||||||
|
raise InvalidConfigurationError(str(exc)) from None
|
||||||
|
return
|
||||||
|
|
||||||
self.config.connections = connections
|
self.config.connections = connections
|
||||||
self.config.profiles = profiles
|
self.config.profiles = profiles
|
||||||
self.config._saveconfig(self.config.file)
|
self.config._saveconfig(self.config.file)
|
||||||
|
|||||||
@@ -1,64 +1,212 @@
|
|||||||
|
from contextlib import suppress
|
||||||
|
from copy import deepcopy
|
||||||
|
from typing import ClassVar
|
||||||
|
|
||||||
from .base import BaseService
|
from .base import BaseService
|
||||||
from .exceptions import ProfileNotFoundError, ProfileAlreadyExistsError, InvalidConfigurationError
|
from .exceptions import (
|
||||||
|
InvalidConfigurationError,
|
||||||
|
ProfileAlreadyExistsError,
|
||||||
|
ProfileNotFoundError,
|
||||||
|
)
|
||||||
|
|
||||||
|
MAX_PROFILE_DEPTH = 5
|
||||||
|
|
||||||
|
|
||||||
|
def _supports_transactional_update(cfg):
|
||||||
|
return (
|
||||||
|
cfg is not None
|
||||||
|
and not hasattr(cfg, "assert_called")
|
||||||
|
and callable(getattr(cfg, "transactional_update", None))
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class ProfileService(BaseService):
|
class ProfileService(BaseService):
|
||||||
"""Business logic for node profiles management."""
|
"""Business logic for node profiles management."""
|
||||||
|
|
||||||
|
_ALLOWED_KEYS: ClassVar[frozenset[str]] = frozenset({
|
||||||
|
"host",
|
||||||
|
"options",
|
||||||
|
"logs",
|
||||||
|
"password",
|
||||||
|
"port",
|
||||||
|
"protocol",
|
||||||
|
"user",
|
||||||
|
"tags",
|
||||||
|
"jumphost",
|
||||||
|
"fallback",
|
||||||
|
})
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
def _validate_profile_payload(data):
|
||||||
|
from ..auth_utils import (
|
||||||
|
SanitizedAuthError,
|
||||||
|
is_compose_expression,
|
||||||
|
validate_auth_budget_tags,
|
||||||
|
validate_compose_syntax,
|
||||||
|
validate_fallback_entries,
|
||||||
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
|
if "tags" in data and data["tags"] not in ("", None):
|
||||||
|
validate_auth_budget_tags(data["tags"])
|
||||||
|
if "fallback" in data and data["fallback"] not in (None, "", []):
|
||||||
|
validate_fallback_entries(data["fallback"])
|
||||||
|
pw = data.get("password")
|
||||||
|
if is_compose_expression(pw):
|
||||||
|
validate_compose_syntax(pw)
|
||||||
|
elif isinstance(pw, (list, tuple)):
|
||||||
|
for item in pw:
|
||||||
|
if is_compose_expression(item):
|
||||||
|
validate_compose_syntax(item)
|
||||||
|
except (ValueError, TypeError, SanitizedAuthError) as exc:
|
||||||
|
raise InvalidConfigurationError(str(exc)) from None
|
||||||
|
|
||||||
|
def _get_profiles_snapshot(self):
|
||||||
|
lock = getattr(self.config, "_mutation_lock", None)
|
||||||
|
if lock is not None and not hasattr(self.config, "assert_called"):
|
||||||
|
with lock:
|
||||||
|
return deepcopy(self.config.profiles)
|
||||||
|
return deepcopy(self.config.profiles)
|
||||||
|
|
||||||
def list_profiles(self, filter_str=None):
|
def list_profiles(self, filter_str=None):
|
||||||
"""List all profile names, optionally filtered."""
|
"""List all profile names, optionally filtered."""
|
||||||
profiles = list(self.config.profiles.keys())
|
profiles = list(self._get_profiles_snapshot().keys())
|
||||||
case_sensitive = self.config.config.get("case", False)
|
case_sensitive = self.config.config.get("case", False)
|
||||||
|
|
||||||
if filter_str:
|
if filter_str:
|
||||||
if not case_sensitive:
|
if not case_sensitive:
|
||||||
f_str = filter_str.lower()
|
f_str = filter_str.lower()
|
||||||
return [p for p in profiles if f_str in p.lower()]
|
return [p for p in profiles if f_str in p.lower()]
|
||||||
else:
|
|
||||||
return [p for p in profiles if filter_str in p]
|
return [p for p in profiles if filter_str in p]
|
||||||
return profiles
|
return profiles
|
||||||
|
|
||||||
def get_profile(self, name, resolve=True):
|
def get_profile(self, name, resolve=True, *, _visited=frozenset(), _depth=0):
|
||||||
"""Get the profile dictionary, optionally resolved."""
|
"""Get the profile dictionary, optionally resolved."""
|
||||||
profile = self.config.profiles.get(name)
|
if _depth >= MAX_PROFILE_DEPTH or name in _visited:
|
||||||
|
raise InvalidConfigurationError(
|
||||||
|
f"Circular or overly deep profile reference involving '{name}'."
|
||||||
|
)
|
||||||
|
profiles = self._get_profiles_snapshot()
|
||||||
|
profile = profiles.get(name)
|
||||||
if not profile:
|
if not profile:
|
||||||
raise ProfileNotFoundError(f"Profile '{name}' not found.")
|
raise ProfileNotFoundError(f"Profile '{name}' not found.")
|
||||||
|
|
||||||
if resolve:
|
if resolve:
|
||||||
return self.resolve_node_data(profile)
|
return self.resolve_node_data(
|
||||||
|
profile,
|
||||||
|
_visited=_visited | {name},
|
||||||
|
_depth=_depth + 1,
|
||||||
|
_profiles_snapshot=profiles,
|
||||||
|
)
|
||||||
return profile
|
return profile
|
||||||
|
|
||||||
def add_profile(self, name, data):
|
def add_profile(self, name, data):
|
||||||
"""Add a new profile."""
|
"""Add a new profile."""
|
||||||
|
filtered_data = {k: deepcopy(v) for k, v in data.items() if k in self._ALLOWED_KEYS}
|
||||||
|
self._validate_profile_payload(filtered_data)
|
||||||
|
|
||||||
|
if _supports_transactional_update(self.config):
|
||||||
|
def _mut(cfg):
|
||||||
|
if name in cfg.profiles:
|
||||||
|
raise ProfileAlreadyExistsError(f"Profile '{name}' already exists.")
|
||||||
|
cfg._profiles_add_unlocked(id=name, **filtered_data)
|
||||||
|
|
||||||
|
self.config.transactional_update(_mut)
|
||||||
|
return
|
||||||
|
|
||||||
if name in self.config.profiles:
|
if name in self.config.profiles:
|
||||||
raise ProfileAlreadyExistsError(f"Profile '{name}' already exists.")
|
raise ProfileAlreadyExistsError(f"Profile '{name}' already exists.")
|
||||||
|
|
||||||
# Filter data to match _profiles_add signature and ensure id is passed
|
|
||||||
allowed_keys = {"host", "options", "logs", "password", "port", "protocol", "user", "tags", "jumphost"}
|
|
||||||
filtered_data = {k: v for k, v in data.items() if k in allowed_keys}
|
|
||||||
|
|
||||||
self.config._profiles_add(id=name, **filtered_data)
|
self.config._profiles_add(id=name, **filtered_data)
|
||||||
self.config._saveconfig(self.config.file)
|
self.config._saveconfig(self.config.file)
|
||||||
|
|
||||||
def resolve_node_data(self, node_data):
|
def resolve_node_data(
|
||||||
"""Resolve profile references (@profile) in node data and handle inheritance."""
|
self,
|
||||||
resolved = node_data.copy()
|
node_data,
|
||||||
|
*,
|
||||||
|
_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)
|
||||||
|
|
||||||
# 1. Identify all referenced profiles to support inheritance
|
|
||||||
referenced_profiles = []
|
referenced_profiles = []
|
||||||
for value in resolved.values():
|
for key, value in resolved.items():
|
||||||
if isinstance(value, str) and value.startswith("@"):
|
if key == "fallback":
|
||||||
referenced_profiles.append(value[1:])
|
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):
|
elif isinstance(value, list):
|
||||||
for item in value:
|
for item in value:
|
||||||
if isinstance(item, str) and item.startswith("@"):
|
if (
|
||||||
referenced_profiles.append(item[1:])
|
isinstance(item, str)
|
||||||
|
and not is_compose_expression(item)
|
||||||
|
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
|
||||||
|
|
||||||
# 2. Resolve explicit references
|
|
||||||
for key, value in resolved.items():
|
|
||||||
if isinstance(value, str) and value.startswith("@"):
|
if isinstance(value, str) and value.startswith("@"):
|
||||||
profile_name = value[1:]
|
profile_name = value[1:]
|
||||||
try:
|
try:
|
||||||
profile = self.get_profile(profile_name, resolve=True)
|
profile = _get_resolved_ref(profile_name)
|
||||||
resolved[key] = profile.get(key, "")
|
resolved[key] = profile.get(key, "")
|
||||||
except ProfileNotFoundError:
|
except ProfileNotFoundError:
|
||||||
resolved[key] = ""
|
resolved[key] = ""
|
||||||
@@ -67,33 +215,26 @@ class ProfileService(BaseService):
|
|||||||
for item in value:
|
for item in value:
|
||||||
if isinstance(item, str) and item.startswith("@"):
|
if isinstance(item, str) and item.startswith("@"):
|
||||||
profile_name = item[1:]
|
profile_name = item[1:]
|
||||||
try:
|
with suppress(ProfileNotFoundError):
|
||||||
profile = self.get_profile(profile_name, resolve=True)
|
profile = _get_resolved_ref(profile_name)
|
||||||
if "password" in profile:
|
if "password" in profile:
|
||||||
resolved_list.append(profile["password"])
|
resolved_list.append(profile["password"])
|
||||||
except ProfileNotFoundError:
|
|
||||||
pass
|
|
||||||
else:
|
else:
|
||||||
resolved_list.append(item)
|
resolved_list.append(item)
|
||||||
resolved[key] = resolved_list
|
resolved[key] = resolved_list
|
||||||
|
|
||||||
# 3. Inheritance: Fill empty keys from the first referenced profile
|
|
||||||
if referenced_profiles:
|
if referenced_profiles:
|
||||||
base_profile_name = referenced_profiles[0]
|
base_profile_name = referenced_profiles[0]
|
||||||
try:
|
with suppress(ProfileNotFoundError):
|
||||||
base_profile = self.get_profile(base_profile_name, resolve=True)
|
base_profile = _get_resolved_ref(base_profile_name)
|
||||||
for key, value in base_profile.items():
|
for key, value in base_profile.items():
|
||||||
# Fill if key is missing or empty
|
if key not in resolved or resolved[key] in ("", [], None):
|
||||||
if key not in resolved or resolved[key] == "" or resolved[key] == [] or resolved[key] is None:
|
resolved[key] = deepcopy(value)
|
||||||
resolved[key] = value
|
|
||||||
except ProfileNotFoundError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
# 4. Handle default protocol
|
if resolved.get("protocol") in ("", None):
|
||||||
if resolved.get("protocol") == "" or resolved.get("protocol") is None:
|
|
||||||
try:
|
try:
|
||||||
default_profile = self.get_profile("default", resolve=True)
|
default_profile = _get_resolved_ref("default")
|
||||||
resolved["protocol"] = default_profile.get("protocol", "ssh")
|
resolved["protocol"] = default_profile.get("protocol", "ssh") or "ssh"
|
||||||
except ProfileNotFoundError:
|
except ProfileNotFoundError:
|
||||||
resolved["protocol"] = "ssh"
|
resolved["protocol"] = "ssh"
|
||||||
|
|
||||||
@@ -101,34 +242,58 @@ class ProfileService(BaseService):
|
|||||||
|
|
||||||
def delete_profile(self, name):
|
def delete_profile(self, name):
|
||||||
"""Delete an existing profile, with safety checks."""
|
"""Delete an existing profile, with safety checks."""
|
||||||
if name not in self.config.profiles:
|
if _supports_transactional_update(self.config):
|
||||||
|
def _mut(cfg):
|
||||||
|
if name not in cfg.profiles:
|
||||||
raise ProfileNotFoundError(f"Profile '{name}' not found.")
|
raise ProfileNotFoundError(f"Profile '{name}' not found.")
|
||||||
|
|
||||||
if name == "default":
|
if name == "default":
|
||||||
raise InvalidConfigurationError("Cannot delete the 'default' profile.")
|
raise InvalidConfigurationError("Cannot delete the 'default' profile.")
|
||||||
|
used_by = 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)
|
used_by = self.config._profileused(name)
|
||||||
if used_by:
|
if used_by:
|
||||||
# We return the list of nodes using it so the UI can inform the user
|
raise InvalidConfigurationError(
|
||||||
raise InvalidConfigurationError(f"Profile '{name}' is used by nodes: {', '.join(used_by)}")
|
f"Profile '{name}' is used by nodes: {', '.join(used_by)}"
|
||||||
|
)
|
||||||
self.config._profiles_del(id=name)
|
self.config._profiles_del(id=name)
|
||||||
self.config._saveconfig(self.config.file)
|
self.config._saveconfig(self.config.file)
|
||||||
|
|
||||||
def update_profile(self, name, data):
|
def update_profile(self, name, data):
|
||||||
"""Update an existing profile."""
|
"""Update an existing profile."""
|
||||||
|
if _supports_transactional_update(self.config):
|
||||||
|
def _mut(cfg):
|
||||||
|
if name not in cfg.profiles:
|
||||||
|
raise ProfileNotFoundError(f"Profile '{name}' not found.")
|
||||||
|
updated_data = deepcopy(cfg.profiles[name])
|
||||||
|
updated_data.update(deepcopy(data))
|
||||||
|
filtered_data = {
|
||||||
|
k: v for k, v in updated_data.items() if k in self._ALLOWED_KEYS
|
||||||
|
}
|
||||||
|
self._validate_profile_payload(filtered_data)
|
||||||
|
cfg._profiles_add_unlocked(id=name, **filtered_data)
|
||||||
|
|
||||||
|
self.config.transactional_update(_mut)
|
||||||
|
return
|
||||||
|
|
||||||
if name not in self.config.profiles:
|
if name not in self.config.profiles:
|
||||||
raise ProfileNotFoundError(f"Profile '{name}' not found.")
|
raise ProfileNotFoundError(f"Profile '{name}' not found.")
|
||||||
|
|
||||||
# Merge with existing data
|
|
||||||
existing = self.get_profile(name, resolve=False)
|
existing = self.get_profile(name, resolve=False)
|
||||||
updated_data = existing.copy()
|
updated_data = deepcopy(existing)
|
||||||
updated_data.update(data)
|
updated_data.update(data)
|
||||||
|
filtered_data = {k: v for k, v in updated_data.items() if k in self._ALLOWED_KEYS}
|
||||||
# Filter data to match _profiles_add signature
|
self._validate_profile_payload(filtered_data)
|
||||||
allowed_keys = {"host", "options", "logs", "password", "port", "protocol", "user", "tags", "jumphost"}
|
|
||||||
filtered_data = {k: v for k, v in updated_data.items() if k in allowed_keys}
|
|
||||||
|
|
||||||
self.config._profiles_add(id=name, **filtered_data)
|
self.config._profiles_add(id=name, **filtered_data)
|
||||||
self.config._saveconfig(self.config.file)
|
self.config._saveconfig(self.config.file)
|
||||||
|
|
||||||
+29
-11
@@ -1,5 +1,9 @@
|
|||||||
|
import inspect
|
||||||
|
from contextlib import suppress
|
||||||
|
|
||||||
from .exceptions import InvalidConfigurationError
|
from .exceptions import InvalidConfigurationError
|
||||||
|
|
||||||
|
|
||||||
class RemoteStub:
|
class RemoteStub:
|
||||||
def __getattr__(self, name):
|
def __getattr__(self, name):
|
||||||
raise NotImplementedError(
|
raise NotImplementedError(
|
||||||
@@ -7,13 +11,17 @@ class RemoteStub:
|
|||||||
"Use local mode or wait for the gRPC implementation."
|
"Use local mode or wait for the gRPC implementation."
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
class ServiceProvider:
|
class ServiceProvider:
|
||||||
"""Dynamic service backend. Transparently provides local or remote services."""
|
"""Dynamic service backend. Transparently provides local or remote services."""
|
||||||
|
|
||||||
def __init__(self, config, mode="local", remote_host=None):
|
def __init__(self, config, mode="local", remote_host=None):
|
||||||
|
from ..auth_utils import AuthAttemptTracker
|
||||||
|
|
||||||
self.mode = mode
|
self.mode = mode
|
||||||
self.config = config
|
self.config = config
|
||||||
self.remote_host = remote_host
|
self.remote_host = remote_host
|
||||||
|
self.auth_tracker = AuthAttemptTracker()
|
||||||
self._system = None
|
self._system = None
|
||||||
self._execution = None
|
self._execution = None
|
||||||
self._import_export = None
|
self._import_export = None
|
||||||
@@ -29,11 +37,11 @@ class ServiceProvider:
|
|||||||
raise ValueError(f"Unknown service mode: {mode}")
|
raise ValueError(f"Unknown service mode: {mode}")
|
||||||
|
|
||||||
def _init_local(self):
|
def _init_local(self):
|
||||||
from .node_service import NodeService
|
|
||||||
from .profile_service import ProfileService
|
|
||||||
from .config_service import ConfigService
|
from .config_service import ConfigService
|
||||||
from .plugin_service import PluginService
|
|
||||||
from .context_service import ContextService
|
from .context_service import ContextService
|
||||||
|
from .node_service import NodeService
|
||||||
|
from .plugin_service import PluginService
|
||||||
|
from .profile_service import ProfileService
|
||||||
|
|
||||||
self.nodes = NodeService(self.config)
|
self.nodes = NodeService(self.config)
|
||||||
self.profiles = ProfileService(self.config)
|
self.profiles = ProfileService(self.config)
|
||||||
@@ -45,18 +53,28 @@ class ServiceProvider:
|
|||||||
# Allow ConfigService to work locally so the user can revert the mode
|
# Allow ConfigService to work locally so the user can revert the mode
|
||||||
from .config_service import ConfigService
|
from .config_service import ConfigService
|
||||||
from .context_service import ContextService
|
from .context_service import ContextService
|
||||||
|
|
||||||
self.config_svc = ConfigService(self.config)
|
self.config_svc = ConfigService(self.config)
|
||||||
self.context = ContextService(self.config)
|
self.context = ContextService(self.config)
|
||||||
|
|
||||||
if not self.remote_host:
|
if not self.remote_host:
|
||||||
raise InvalidConfigurationError("Remote host must be specified in remote mode")
|
raise InvalidConfigurationError("Remote host must be specified in remote mode")
|
||||||
|
|
||||||
import grpc
|
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
import grpc
|
||||||
|
|
||||||
from ..grpc_layer.stubs import (
|
from ..grpc_layer.stubs import (
|
||||||
NodeStub, ProfileStub, PluginStub, AIStub,
|
AIStub,
|
||||||
ExecutionStub, ImportExportStub, SystemStub,
|
AuthClientInterceptor,
|
||||||
ConfigStub, AuthClientInterceptor, AuthStub
|
AuthStub,
|
||||||
|
ConfigStub,
|
||||||
|
ExecutionStub,
|
||||||
|
ImportExportStub,
|
||||||
|
NodeStub,
|
||||||
|
PluginStub,
|
||||||
|
ProfileStub,
|
||||||
|
SystemStub,
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_token():
|
def get_token():
|
||||||
@@ -65,11 +83,8 @@ class ServiceProvider:
|
|||||||
return env_token
|
return env_token
|
||||||
token_path = os.path.join(self.config.defaultdir, ".token")
|
token_path = os.path.join(self.config.defaultdir, ".token")
|
||||||
if os.path.exists(token_path):
|
if os.path.exists(token_path):
|
||||||
try:
|
with suppress(OSError, UnicodeDecodeError), open(token_path) as f:
|
||||||
with open(token_path, "r") as f:
|
|
||||||
return f.read().strip()
|
return f.read().strip()
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
return None
|
return None
|
||||||
|
|
||||||
channel = grpc.insecure_channel(self.remote_host)
|
channel = grpc.insecure_channel(self.remote_host)
|
||||||
@@ -105,6 +120,9 @@ class ServiceProvider:
|
|||||||
def execution(self):
|
def execution(self):
|
||||||
if self._execution is None and self.mode == "local":
|
if self._execution is None and self.mode == "local":
|
||||||
from .execution_service import ExecutionService
|
from .execution_service import ExecutionService
|
||||||
|
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)
|
self._execution = ExecutionService(self.config)
|
||||||
return self._execution
|
return self._execution
|
||||||
|
|
||||||
|
|||||||
File diff suppressed because it is too large.
Load diff
Reference in new issue
Block a user