feat(spec-05-auth-fallback-chain): worker (eng_flash_medium) implementation
This commit is contained in:
1 parent
ecaa836b98
commit
175e623d99
13 files changed
+1333
-141
No files matched your search
@@ -61,6 +61,9 @@ MIN_HOP_HOST_ATTEMPTS = 1
|
||||
MAX_HOP_HOST_ATTEMPTS_CAP = 10
|
||||
MAX_JSON_INPUT_BYTES = 8192
|
||||
MAX_JSON_INPUT_DEPTH = 4
|
||||
MAX_CONTEXT_REGEXES = 64
|
||||
MAX_CONTEXT_PATTERN_LEN = 512
|
||||
MAX_REMOTE_FILTER_NODES = 500
|
||||
|
||||
SAFE_ERROR_TEMPLATES: dict[str, str] = {
|
||||
"AUTH_FAILED": "[AUTH_FAILED] Authentication failed",
|
||||
@@ -1609,3 +1612,77 @@ class AuthAttemptTracker:
|
||||
with self._lock:
|
||||
self._unblocked.clear()
|
||||
self._blocked.clear()
|
||||
|
||||
|
||||
def is_default_all_context_cfg(cfg_dict: Any) -> bool:
|
||||
"""Return True when config uses the default unrestricted 'all' context."""
|
||||
if not isinstance(cfg_dict, dict):
|
||||
return True
|
||||
current_ctx = cfg_dict.get("current_context", "all")
|
||||
if current_ctx != "all":
|
||||
return False
|
||||
contexts = cfg_dict.get("contexts", {"all": [".*"]})
|
||||
if not isinstance(contexts, dict):
|
||||
return True
|
||||
return contexts.get("all", [".*"]) == [".*"]
|
||||
|
||||
|
||||
def compile_active_context_patterns_with_status(
|
||||
cfg_dict: Any,
|
||||
*,
|
||||
strict: bool = False,
|
||||
) -> tuple[list[re.Pattern[str]], bool]:
|
||||
"""Compile active context regexes purely, returning (patterns, had_error)."""
|
||||
if is_default_all_context_cfg(cfg_dict):
|
||||
return ([re.compile(".*")], False)
|
||||
|
||||
current_ctx = cfg_dict.get("current_context", "all")
|
||||
contexts = cfg_dict.get("contexts", {"all": [".*"]})
|
||||
raw_patterns = contexts.get(current_ctx, [".*"]) if isinstance(contexts, dict) else [".*"]
|
||||
flags = 0 if cfg_dict.get("case", False) else re.IGNORECASE
|
||||
|
||||
if not isinstance(raw_patterns, list):
|
||||
if strict:
|
||||
raise ValueError("Context regexes must be a list")
|
||||
return ([], True)
|
||||
|
||||
had_error = False
|
||||
if len(raw_patterns) > MAX_CONTEXT_REGEXES:
|
||||
if strict:
|
||||
raise ValueError(f"Context exceeds maximum of {MAX_CONTEXT_REGEXES} regexes")
|
||||
had_error = True
|
||||
raw_patterns = raw_patterns[:MAX_CONTEXT_REGEXES]
|
||||
|
||||
patterns: list[re.Pattern[str]] = []
|
||||
for pat in raw_patterns:
|
||||
if not isinstance(pat, str) or not pat or len(pat) > MAX_CONTEXT_PATTERN_LEN:
|
||||
if strict:
|
||||
raise ValueError(
|
||||
f"Context pattern must be a non-empty string up to {MAX_CONTEXT_PATTERN_LEN} chars"
|
||||
)
|
||||
had_error = True
|
||||
continue
|
||||
try:
|
||||
patterns.append(re.compile(pat, flags))
|
||||
except re.error as exc:
|
||||
if strict:
|
||||
raise ValueError(f"Invalid context regex {pat!r}: {exc}") from None
|
||||
had_error = True
|
||||
return (patterns, had_error)
|
||||
|
||||
|
||||
def compile_active_context_patterns(
|
||||
cfg_dict: Any,
|
||||
*,
|
||||
strict: bool = False,
|
||||
) -> list[re.Pattern[str]]:
|
||||
"""Compile active context regexes purely respecting cfg_dict['case']."""
|
||||
patterns, _ = compile_active_context_patterns_with_status(cfg_dict, strict=strict)
|
||||
return patterns
|
||||
|
||||
|
||||
def match_active_context_patterns(name: Any, patterns: Iterable[re.Pattern[str]]) -> bool:
|
||||
"""Return True if non-empty string name matches at least one compiled context regex."""
|
||||
if not isinstance(name, str) or not name:
|
||||
return False
|
||||
return any(p.match(name) for p in patterns)
|
||||
@@ -144,6 +144,7 @@ class NodeHandler:
|
||||
uniques = self.app.services.nodes.explode_unique(args.data)
|
||||
if not uniques:
|
||||
raise InvalidConfigurationError(f"Invalid folder {args.data}")
|
||||
self.app.services.nodes.validate_parent_folder(args.data, is_folder=True)
|
||||
self.app.services.nodes.add_node(args.data, {}, is_folder=True)
|
||||
printer.success(f"{args.data} added successfully")
|
||||
else:
|
||||
|
||||
+59
-11
@@ -156,19 +156,33 @@ class Validators:
|
||||
_raise_val_err(str(exc))
|
||||
return True
|
||||
|
||||
def _get_all_valid_nodes(self) -> list[str]:
|
||||
valid_nodes = list(getattr(self.app, "nodes_list", []) or [])
|
||||
if hasattr(self.app, "services") and hasattr(self.app.services, "nodes"):
|
||||
raw_fn = getattr(self.app.services.nodes, "_get_raw_nodes", None)
|
||||
if callable(raw_fn) and not hasattr(raw_fn, "assert_called"):
|
||||
raw_res = raw_fn()
|
||||
if isinstance(raw_res, (list, tuple, set)):
|
||||
valid_nodes = list(dict.fromkeys([*raw_res, *valid_nodes]))
|
||||
return valid_nodes
|
||||
|
||||
def _node_exists(self, target: str) -> bool:
|
||||
valid_nodes = self._get_all_valid_nodes()
|
||||
if getattr(self.app, "case", True):
|
||||
return target in valid_nodes
|
||||
return target.lower() in {n.lower() for n in valid_nodes}
|
||||
|
||||
def jumphost_validation(self, answers, current):
|
||||
if current.startswith("@"):
|
||||
if current[1:] not in self.app.profiles:
|
||||
_raise_val_err("Profile {} don't exist".format(current))
|
||||
elif current != "":
|
||||
if current not in self.app.nodes_list:
|
||||
_raise_val_err("Node {} don't exist.".format(current))
|
||||
_raise_val_err(f"Profile {current} don't exist")
|
||||
elif current != "" and not self._node_exists(current):
|
||||
_raise_val_err(f"Node {current} don't exist.")
|
||||
return True
|
||||
|
||||
def profile_jumphost_validation(self, answers, current):
|
||||
if current != "":
|
||||
if current not in self.app.nodes_list:
|
||||
_raise_val_err("Node {} don't exist.".format(current))
|
||||
if current != "" and not self._node_exists(current):
|
||||
_raise_val_err(f"Node {current} don't exist.")
|
||||
return True
|
||||
|
||||
def default_validation(self, answers, current):
|
||||
@@ -186,12 +200,46 @@ class Validators:
|
||||
return True
|
||||
|
||||
def bulk_folder_validation(self, answers, current):
|
||||
if not self.app.case:
|
||||
case = getattr(self.app, "case", False)
|
||||
if not case:
|
||||
current = current.lower()
|
||||
|
||||
candidate = current
|
||||
if "/" in current:
|
||||
candidate = current.split("/")[0]
|
||||
candidate = current.split("/")[0] if "/" in current else current
|
||||
|
||||
if hasattr(self.app, "services") and hasattr(self.app.services, "context"):
|
||||
raw_folders = list(getattr(self.app, "folders", []) or [])
|
||||
if hasattr(self.app.services, "nodes"):
|
||||
raw_fn = getattr(self.app.services.nodes, "_get_raw_folders", None)
|
||||
if callable(raw_fn) and not hasattr(raw_fn, "assert_called"):
|
||||
raw_res = raw_fn()
|
||||
if isinstance(raw_res, (list, tuple, set)):
|
||||
raw_folders = list(raw_res)
|
||||
|
||||
def _folder_exists_norm(folder_name: str) -> bool:
|
||||
if case:
|
||||
return folder_name in raw_folders
|
||||
return folder_name.lower() in {f.lower() for f in raw_folders}
|
||||
|
||||
if current != "" and not _folder_exists_norm(candidate):
|
||||
_raise_val_err(f"Location {current} don't exist")
|
||||
|
||||
ctx_svc = self.app.services.context
|
||||
if ctx_svc is not None and not ctx_svc.is_default_all_context():
|
||||
raw_ids = answers.get("ids", "") if isinstance(answers, dict) else ""
|
||||
for nid in str(raw_ids).split(","):
|
||||
nid_stripped = nid.strip()
|
||||
if not nid_stripped:
|
||||
continue
|
||||
nid_norm = nid_stripped if case else nid_stripped.lower()
|
||||
full_id = f"{nid_norm}{candidate}" if candidate else nid_norm
|
||||
if not ctx_svc.matches_active_context(full_id):
|
||||
_raise_val_err(
|
||||
f"Node '{full_id}' does not match active context '{ctx_svc.current_context}'."
|
||||
)
|
||||
parent = full_id.partition("@")[2]
|
||||
if parent and not _folder_exists_norm(f"@{parent}"):
|
||||
_raise_val_err(f"Location @{parent} don't exist")
|
||||
return True
|
||||
|
||||
matches = list(filter(lambda k: k == candidate, self.app.folders))
|
||||
if current != "" and len(matches) == 0:
|
||||
|
||||
+25
-5
@@ -16,8 +16,11 @@ from . import printer
|
||||
from .auth_utils import (
|
||||
MAX_PROFILE_DEPTH,
|
||||
SanitizedAuthError,
|
||||
compile_active_context_patterns_with_status,
|
||||
extract_profile_refs_from_value,
|
||||
is_compose_expression,
|
||||
is_default_all_context_cfg,
|
||||
match_active_context_patterns,
|
||||
resolve_fallback_entries,
|
||||
validate_auth_budget_tags,
|
||||
validate_compose_syntax,
|
||||
@@ -60,6 +63,7 @@ class configfile:
|
||||
def __init__(self, conf = None, key = None, shared_config = None):
|
||||
self._mutation_lock = threading.RLock()
|
||||
self._cache_dirty = False
|
||||
self._enforce_context = True
|
||||
self._shared_config = shared_config
|
||||
'''
|
||||
|
||||
@@ -406,8 +410,9 @@ class configfile:
|
||||
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
|
||||
if self._is_context_filtering_enabled_unlocked():
|
||||
self._generate_nodes_cache_unlocked(_raise_on_error=True)
|
||||
except (OSError, TypeError, ValueError):
|
||||
self._cache_dirty = True
|
||||
return 0, None
|
||||
@@ -491,18 +496,33 @@ class configfile:
|
||||
self._cache_dirty = True
|
||||
return False
|
||||
|
||||
def _is_context_filtering_enabled_unlocked(self) -> bool:
|
||||
if not getattr(self, "_enforce_context", True):
|
||||
return False
|
||||
return not (len(sys.argv) > 1 and sys.argv[1] == "api")
|
||||
|
||||
def _filter_by_active_context_unlocked(self, items: list[str]) -> tuple[list[str], bool]:
|
||||
if not self._is_context_filtering_enabled_unlocked() or is_default_all_context_cfg(
|
||||
self.config
|
||||
):
|
||||
return (list(items), False)
|
||||
patterns, had_err = compile_active_context_patterns_with_status(self.config)
|
||||
return ([i for i in items if match_active_context_patterns(i, patterns)], had_err)
|
||||
|
||||
def _generate_nodes_cache_unlocked(self, nodes=None, folders=None, profiles=None, *, _raise_on_error=False):
|
||||
try:
|
||||
if nodes is None:
|
||||
nodes = self._getallnodes_unlocked()
|
||||
if folders is None:
|
||||
folders = self._getallfolders_unlocked()
|
||||
raw_nodes = self._getallnodes_unlocked() if nodes is None else list(nodes)
|
||||
raw_folders = self._getallfolders_unlocked() if folders is None else list(folders)
|
||||
nodes, err_n = self._filter_by_active_context_unlocked(raw_nodes)
|
||||
folders, err_f = self._filter_by_active_context_unlocked(raw_folders)
|
||||
if profiles is None:
|
||||
profiles = list(self.profiles.keys())
|
||||
|
||||
self._atomic_write_text_unlocked(self.fzf_cachefile, lambda f: f.write("\n".join(nodes)))
|
||||
self._atomic_write_text_unlocked(self.folders_cachefile, lambda f: f.write("\n".join(folders)))
|
||||
self._atomic_write_text_unlocked(self.profiles_cachefile, lambda f: f.write("\n".join(profiles)))
|
||||
if err_n or err_f:
|
||||
self._cache_dirty = True
|
||||
except (OSError, TypeError, ValueError):
|
||||
if _raise_on_error:
|
||||
raise
|
||||
|
||||
+19
-25
@@ -72,11 +72,19 @@ def handle_errors(func):
|
||||
printer.clear_thread_state()
|
||||
return wrapper
|
||||
|
||||
class NodeServicer(connpy_pb2_grpc.NodeServiceServicer):
|
||||
def __init__(self, provider, registry=None, debug=False):
|
||||
def _ensure_server_provider(provider):
|
||||
if not hasattr(provider, "mode"):
|
||||
from connpy.services.provider import ServiceProvider
|
||||
provider = ServiceProvider(provider, mode="local")
|
||||
|
||||
return ServiceProvider(provider, mode="local", enforce_context=False)
|
||||
if hasattr(provider, "set_enforce_context") and callable(provider.set_enforce_context):
|
||||
provider.set_enforce_context(False)
|
||||
return provider
|
||||
|
||||
|
||||
class NodeServicer(connpy_pb2_grpc.NodeServiceServicer):
|
||||
def __init__(self, provider, registry=None, debug=False):
|
||||
provider = _ensure_server_provider(provider)
|
||||
self._fallback_provider = provider
|
||||
self._registry = registry
|
||||
self.server_debug = debug
|
||||
@@ -689,9 +697,7 @@ class NodeServicer(connpy_pb2_grpc.NodeServiceServicer):
|
||||
|
||||
class ProfileServicer(connpy_pb2_grpc.ProfileServiceServicer):
|
||||
def __init__(self, provider, registry=None):
|
||||
if not hasattr(provider, "mode"):
|
||||
from connpy.services.provider import ServiceProvider
|
||||
provider = ServiceProvider(provider, mode="local")
|
||||
provider = _ensure_server_provider(provider)
|
||||
self._fallback_provider = provider
|
||||
self._registry = registry
|
||||
|
||||
@@ -743,9 +749,7 @@ class ProfileServicer(connpy_pb2_grpc.ProfileServiceServicer):
|
||||
|
||||
class ConfigServicer(connpy_pb2_grpc.ConfigServiceServicer):
|
||||
def __init__(self, provider, registry=None):
|
||||
if not hasattr(provider, "mode"):
|
||||
from connpy.services.provider import ServiceProvider
|
||||
provider = ServiceProvider(provider, mode="local")
|
||||
provider = _ensure_server_provider(provider)
|
||||
self._fallback_provider = provider
|
||||
self._registry = registry
|
||||
|
||||
@@ -788,9 +792,7 @@ class ConfigServicer(connpy_pb2_grpc.ConfigServiceServicer):
|
||||
|
||||
class PluginServicer(connpy_pb2_grpc.PluginServiceServicer, remote_plugin_pb2_grpc.RemotePluginServiceServicer):
|
||||
def __init__(self, provider, registry=None):
|
||||
if not hasattr(provider, "mode"):
|
||||
from connpy.services.provider import ServiceProvider
|
||||
provider = ServiceProvider(provider, mode="local")
|
||||
provider = _ensure_server_provider(provider)
|
||||
self._fallback_provider = provider
|
||||
self._registry = registry
|
||||
|
||||
@@ -846,9 +848,7 @@ class PluginServicer(connpy_pb2_grpc.PluginServiceServicer, remote_plugin_pb2_gr
|
||||
|
||||
class ExecutionServicer(connpy_pb2_grpc.ExecutionServiceServicer):
|
||||
def __init__(self, provider, registry=None):
|
||||
if not hasattr(provider, "mode"):
|
||||
from connpy.services.provider import ServiceProvider
|
||||
provider = ServiceProvider(provider, mode="local")
|
||||
provider = _ensure_server_provider(provider)
|
||||
self._fallback_provider = provider
|
||||
self._registry = registry
|
||||
|
||||
@@ -977,9 +977,7 @@ class ExecutionServicer(connpy_pb2_grpc.ExecutionServiceServicer):
|
||||
|
||||
class ImportExportServicer(connpy_pb2_grpc.ImportExportServiceServicer):
|
||||
def __init__(self, provider, registry=None):
|
||||
if not hasattr(provider, "mode"):
|
||||
from connpy.services.provider import ServiceProvider
|
||||
provider = ServiceProvider(provider, mode="local")
|
||||
provider = _ensure_server_provider(provider)
|
||||
self._fallback_provider = provider
|
||||
self._registry = registry
|
||||
|
||||
@@ -1112,9 +1110,7 @@ class StatusBridge:
|
||||
|
||||
class AIServicer(connpy_pb2_grpc.AIServiceServicer):
|
||||
def __init__(self, provider, registry=None, debug=False):
|
||||
if not hasattr(provider, "mode"):
|
||||
from connpy.services.provider import ServiceProvider
|
||||
provider = ServiceProvider(provider, mode="local")
|
||||
provider = _ensure_server_provider(provider)
|
||||
self._fallback_provider = provider
|
||||
self._registry = registry
|
||||
self.server_debug = debug
|
||||
@@ -1399,9 +1395,7 @@ class AIServicer(connpy_pb2_grpc.AIServiceServicer):
|
||||
|
||||
class SystemServicer(connpy_pb2_grpc.SystemServiceServicer):
|
||||
def __init__(self, provider, registry=None):
|
||||
if not hasattr(provider, "mode"):
|
||||
from connpy.services.provider import ServiceProvider
|
||||
provider = ServiceProvider(provider, mode="local")
|
||||
provider = _ensure_server_provider(provider)
|
||||
self._fallback_provider = provider
|
||||
self._registry = registry
|
||||
|
||||
@@ -1784,7 +1778,7 @@ def serve(config, port=8048, debug=False):
|
||||
from connpy.grpc_layer.user_registry import UserRegistry
|
||||
from connpy.services.provider import ServiceProvider
|
||||
|
||||
fallback_provider = ServiceProvider(config, mode="local")
|
||||
fallback_provider = ServiceProvider(config, mode="local", enforce_context=False)
|
||||
registry = UserRegistry(config.defaultdir)
|
||||
|
||||
# Check if trusted_gateway provider is configured if SSO Gateway Secret is present in env
|
||||
|
||||
+128
-18
@@ -1,16 +1,19 @@
|
||||
import grpc
|
||||
import queue
|
||||
import re
|
||||
import threading
|
||||
from functools import wraps
|
||||
|
||||
import grpc
|
||||
from google.protobuf.empty_pb2 import Empty
|
||||
|
||||
from . import connpy_pb2, connpy_pb2_grpc, remote_plugin_pb2, remote_plugin_pb2_grpc
|
||||
from .utils import to_value, from_value, to_struct, from_struct
|
||||
from ..services.exceptions import ConnpyError
|
||||
from ..hooks import MethodHook
|
||||
from .. import printer
|
||||
from ..cli.terminal_ui import CopilotInterface
|
||||
from ..auth_utils import MAX_REMOTE_FILTER_NODES
|
||||
from ..hooks import MethodHook
|
||||
from ..services.exceptions import ConnpyError, NodeNotFoundError
|
||||
from ..utils import log_cleaner
|
||||
from . import connpy_pb2, connpy_pb2_grpc, remote_plugin_pb2, remote_plugin_pb2_grpc
|
||||
from .utils import from_struct, from_value, to_struct, to_value
|
||||
|
||||
|
||||
def handle_errors(func):
|
||||
@wraps(func)
|
||||
@@ -43,8 +46,48 @@ class NodeStub:
|
||||
self.remote_host = remote_host
|
||||
self.config = config
|
||||
|
||||
def _get_context_service(self):
|
||||
if not getattr(self.config, "_enforce_context", True):
|
||||
return None
|
||||
if not isinstance(getattr(self.config, "config", None), dict):
|
||||
return None
|
||||
from ..services.context_service import ContextService
|
||||
|
||||
return ContextService(self.config)
|
||||
|
||||
def _ensure_in_active_context(
|
||||
self, unique_id: str, *, is_folder: bool = False, not_found_msg: str | None = None
|
||||
) -> None:
|
||||
ctx_svc = self._get_context_service()
|
||||
if (
|
||||
ctx_svc is not None
|
||||
and not ctx_svc.is_default_all_context()
|
||||
and not ctx_svc.matches_active_context(unique_id)
|
||||
):
|
||||
if not_found_msg is not None:
|
||||
raise NodeNotFoundError(not_found_msg)
|
||||
label = "Folder" if is_folder else "Node"
|
||||
raise NodeNotFoundError(f"{label} '{unique_id}' not found.")
|
||||
|
||||
@handle_errors
|
||||
def _get_raw_nodes(self) -> list[str]:
|
||||
req = connpy_pb2.FilterRequest(filter_str="", format_str="")
|
||||
return from_value(self.stub.list_nodes(req).data) or []
|
||||
|
||||
@handle_errors
|
||||
def _get_raw_folders(self) -> list[str]:
|
||||
req = connpy_pb2.FilterRequest(filter_str="")
|
||||
return from_value(self.stub.list_folders(req).data) or []
|
||||
|
||||
def _handle_remote_copilot(self, res, request_queue, response_queue, client_buffer_bytes, pause_generator, resume_generator, old_tty):
|
||||
import json, asyncio, termios, sys, tty, queue, os
|
||||
import asyncio
|
||||
import json
|
||||
import os
|
||||
import sys
|
||||
import termios
|
||||
import tty
|
||||
|
||||
from ..cli.terminal_ui import CopilotInterface
|
||||
from ..core import copilot_terminal_mode
|
||||
from . import connpy_pb2
|
||||
|
||||
@@ -138,6 +181,7 @@ class NodeStub:
|
||||
|
||||
@handle_errors
|
||||
def connect_node(self, unique_id, sftp=False, debug=False, logger=None):
|
||||
self._ensure_in_active_context(unique_id, is_folder=False)
|
||||
import sys
|
||||
import select
|
||||
import tty
|
||||
@@ -439,6 +483,7 @@ class NodeStub:
|
||||
|
||||
@handle_errors
|
||||
def get_node_details(self, unique_id):
|
||||
self._ensure_in_active_context(unique_id, is_folder=False)
|
||||
return from_struct(self.stub.get_node_details(connpy_pb2.IdRequest(id=unique_id)).data)
|
||||
|
||||
@handle_errors
|
||||
@@ -446,7 +491,11 @@ class NodeStub:
|
||||
return from_value(self.stub.explode_unique(connpy_pb2.IdRequest(id=unique_id)).data)
|
||||
|
||||
@handle_errors
|
||||
def validate_parent_folder(self, unique_id):
|
||||
def validate_parent_folder(self, unique_id, is_folder=False):
|
||||
ctx_svc = self._get_context_service()
|
||||
if ctx_svc is not None:
|
||||
ctx_svc.validate_in_active_context(unique_id, is_folder=is_folder)
|
||||
if not is_folder:
|
||||
self.stub.validate_parent_folder(connpy_pb2.IdRequest(id=unique_id))
|
||||
|
||||
@handle_errors
|
||||
@@ -478,12 +527,16 @@ class NodeStub:
|
||||
|
||||
@handle_errors
|
||||
def add_node(self, unique_id, data, is_folder=False):
|
||||
ctx_svc = self._get_context_service()
|
||||
if ctx_svc is not None:
|
||||
ctx_svc.validate_in_active_context(unique_id, is_folder=is_folder)
|
||||
req = connpy_pb2.NodeRequest(id=unique_id, data=to_struct(data), is_folder=is_folder)
|
||||
self.stub.add_node(req)
|
||||
self._trigger_local_cache_sync()
|
||||
|
||||
@handle_errors
|
||||
def update_node(self, unique_id, data, save=True):
|
||||
self._ensure_in_active_context(unique_id, is_folder=False)
|
||||
req = connpy_pb2.NodeRequest(id=unique_id, data=to_struct(data), is_folder=False)
|
||||
self.stub.update_node(req)
|
||||
if save:
|
||||
@@ -491,6 +544,7 @@ class NodeStub:
|
||||
|
||||
@handle_errors
|
||||
def delete_node(self, unique_id, is_folder=False, save=True):
|
||||
self._ensure_in_active_context(unique_id, is_folder=is_folder)
|
||||
req = connpy_pb2.DeleteRequest(id=unique_id, is_folder=is_folder)
|
||||
self.stub.delete_node(req)
|
||||
if save:
|
||||
@@ -498,12 +552,22 @@ class NodeStub:
|
||||
|
||||
@handle_errors
|
||||
def move_node(self, src_id, dst_id, copy=False):
|
||||
self._ensure_in_active_context(
|
||||
src_id, is_folder=False, not_found_msg=f"Source node '{src_id}' not found."
|
||||
)
|
||||
ctx_svc = self._get_context_service()
|
||||
if ctx_svc is not None:
|
||||
ctx_svc.validate_in_active_context(dst_id, is_folder=False)
|
||||
req = connpy_pb2.MoveRequest(src_id=src_id, dst_id=dst_id, copy=copy)
|
||||
self.stub.move_node(req)
|
||||
self._trigger_local_cache_sync()
|
||||
|
||||
@handle_errors
|
||||
def bulk_add(self, ids, hosts, common_data):
|
||||
ctx_svc = self._get_context_service()
|
||||
if ctx_svc is not None:
|
||||
for uid in ids:
|
||||
ctx_svc.validate_in_active_context(uid, is_folder=False)
|
||||
req = connpy_pb2.BulkRequest(ids=ids, hosts=hosts, common_data=to_struct(common_data))
|
||||
self.stub.bulk_add(req)
|
||||
self._trigger_local_cache_sync()
|
||||
@@ -649,13 +713,49 @@ class PluginStub:
|
||||
yield chunk.text
|
||||
|
||||
class ExecutionStub:
|
||||
def __init__(self, channel, remote_host):
|
||||
def __init__(self, channel, remote_host, config=None, node_stub=None):
|
||||
self.stub = connpy_pb2_grpc.ExecutionServiceStub(channel)
|
||||
self.remote_host = remote_host
|
||||
self.config = config
|
||||
self.node_stub = node_stub
|
||||
|
||||
def _get_context_service(self):
|
||||
if not getattr(self.config, "_enforce_context", True):
|
||||
return None
|
||||
if not isinstance(getattr(self.config, "config", None), dict):
|
||||
return None
|
||||
from ..services.context_service import ContextService
|
||||
|
||||
return ContextService(self.config)
|
||||
|
||||
def _resolve_remote_batches(self, nodes_filter):
|
||||
ctx_svc = self._get_context_service()
|
||||
if ctx_svc is not None and not ctx_svc.is_default_all_context():
|
||||
if isinstance(nodes_filter, (list, tuple)):
|
||||
permitted = ctx_svc.filter_node_list(result=list(nodes_filter))
|
||||
else:
|
||||
candidates = (
|
||||
self.node_stub.list_nodes(filter_str=nodes_filter)
|
||||
if self.node_stub is not None
|
||||
else []
|
||||
)
|
||||
permitted = ctx_svc.filter_node_list(result=candidates)
|
||||
if not permitted:
|
||||
raise ConnpyError(f"No nodes found matching filter: {nodes_filter}")
|
||||
patterns = [f"^{re.escape(n)}$" for n in permitted]
|
||||
return [
|
||||
patterns[i : i + MAX_REMOTE_FILTER_NODES]
|
||||
for i in range(0, len(patterns), MAX_REMOTE_FILTER_NODES)
|
||||
]
|
||||
return [[nodes_filter] if isinstance(nodes_filter, str) else list(nodes_filter)]
|
||||
|
||||
@handle_errors
|
||||
def run_commands(self, nodes_filter, commands, variables=None, parallel=10, timeout=10, folder=None, prompt=None, **kwargs):
|
||||
nodes_list = [nodes_filter] if isinstance(nodes_filter, str) else list(nodes_filter)
|
||||
batches = self._resolve_remote_batches(nodes_filter)
|
||||
final_results = {}
|
||||
on_complete = kwargs.get("on_node_complete")
|
||||
|
||||
for nodes_list in batches:
|
||||
req = connpy_pb2.RunRequest(
|
||||
nodes=nodes_list,
|
||||
commands=commands,
|
||||
@@ -668,9 +768,6 @@ class ExecutionStub:
|
||||
if variables is not None:
|
||||
req.vars.CopyFrom(to_struct(variables))
|
||||
|
||||
final_results = {}
|
||||
on_complete = kwargs.get("on_node_complete")
|
||||
|
||||
for response in self.stub.run_commands(req):
|
||||
if on_complete:
|
||||
on_complete(response.unique_id, response.output, response.status)
|
||||
@@ -683,7 +780,11 @@ class ExecutionStub:
|
||||
|
||||
@handle_errors
|
||||
def test_commands(self, nodes_filter, commands, expected, variables=None, parallel=10, timeout=10, prompt=None, **kwargs):
|
||||
nodes_list = [nodes_filter] if isinstance(nodes_filter, str) else list(nodes_filter)
|
||||
batches = self._resolve_remote_batches(nodes_filter)
|
||||
final_results = {}
|
||||
on_complete = kwargs.get("on_node_complete")
|
||||
|
||||
for nodes_list in batches:
|
||||
req = connpy_pb2.TestRequest(
|
||||
nodes=nodes_list,
|
||||
commands=commands,
|
||||
@@ -697,9 +798,6 @@ class ExecutionStub:
|
||||
if variables is not None:
|
||||
req.vars.CopyFrom(to_struct(variables))
|
||||
|
||||
final_results = {}
|
||||
on_complete = kwargs.get("on_node_complete")
|
||||
|
||||
for response in self.stub.test_commands(req):
|
||||
result_dict = from_struct(response.test_result) if response.HasField("test_result") else {}
|
||||
if on_complete:
|
||||
@@ -710,7 +808,19 @@ class ExecutionStub:
|
||||
|
||||
@handle_errors
|
||||
def run_cli_script(self, nodes_filter, script_path, parallel=10):
|
||||
req = connpy_pb2.ScriptRequest(param1=nodes_filter, param2=script_path, parallel=parallel)
|
||||
ctx_svc = self._get_context_service()
|
||||
effective_filter = nodes_filter
|
||||
if ctx_svc is not None and not ctx_svc.is_default_all_context():
|
||||
candidates = (
|
||||
self.node_stub.list_nodes(filter_str=nodes_filter)
|
||||
if self.node_stub is not None
|
||||
else []
|
||||
)
|
||||
permitted = ctx_svc.filter_node_list(result=candidates)
|
||||
if not permitted:
|
||||
raise ConnpyError(f"No nodes found matching filter: {nodes_filter}")
|
||||
effective_filter = "|".join(f"^{re.escape(n)}$" for n in permitted)
|
||||
req = connpy_pb2.ScriptRequest(param1=effective_filter, param2=script_path, parallel=parallel)
|
||||
return from_struct(self.stub.run_cli_script(req).data)
|
||||
|
||||
class ImportExportStub:
|
||||
|
||||
@@ -62,7 +62,7 @@ class UserRegistry:
|
||||
try:
|
||||
# Attempt a fresh configuration load
|
||||
config = configfile(conf=conf_file, shared_config=self._shared_config)
|
||||
new_provider = ServiceProvider(config, mode="local")
|
||||
new_provider = ServiceProvider(config, mode="local", enforce_context=False)
|
||||
|
||||
# Successfully loaded, clean up the old provider
|
||||
if old_provider:
|
||||
|
||||
@@ -1,21 +1,45 @@
|
||||
import re
|
||||
from typing import List, Dict, Any
|
||||
from typing import Any
|
||||
|
||||
from ..auth_utils import (
|
||||
MAX_CONTEXT_PATTERN_LEN,
|
||||
MAX_CONTEXT_REGEXES,
|
||||
compile_active_context_patterns,
|
||||
is_default_all_context_cfg,
|
||||
match_active_context_patterns,
|
||||
)
|
||||
from .base import BaseService
|
||||
from ..hooks import MethodHook
|
||||
from .. import printer
|
||||
from .exceptions import InvalidConfigurationError
|
||||
|
||||
|
||||
class ContextService(BaseService):
|
||||
"""Business logic for managing and applying regex-based contexts locally."""
|
||||
|
||||
@staticmethod
|
||||
def _validate_regexes(regexes: Any) -> None:
|
||||
if not isinstance(regexes, list) or not (1 <= len(regexes) <= MAX_CONTEXT_REGEXES):
|
||||
raise ValueError(
|
||||
f"Context regexes must be a list of 1 to {MAX_CONTEXT_REGEXES} patterns"
|
||||
)
|
||||
for pat in regexes:
|
||||
if not isinstance(pat, str) or not (1 <= len(pat) <= MAX_CONTEXT_PATTERN_LEN):
|
||||
raise ValueError(
|
||||
f"Each context regex must be a non-empty string up to {MAX_CONTEXT_PATTERN_LEN} characters"
|
||||
)
|
||||
try:
|
||||
re.compile(pat)
|
||||
except re.error as exc:
|
||||
raise ValueError(f"Invalid context regex {pat!r}: {exc}") from None
|
||||
|
||||
@property
|
||||
def contexts(self) -> Dict[str, List[str]]:
|
||||
def contexts(self) -> dict[str, list[str]]:
|
||||
return self.config.config.get("contexts", {"all": [".*"]})
|
||||
|
||||
@property
|
||||
def current_context(self) -> str:
|
||||
return self.config.config.get("current_context", "all")
|
||||
|
||||
def list_contexts(self) -> List[Dict[str, Any]]:
|
||||
def list_contexts(self) -> list[dict[str, Any]]:
|
||||
result = []
|
||||
for name in self.contexts.keys():
|
||||
result.append({
|
||||
@@ -25,27 +49,29 @@ class ContextService(BaseService):
|
||||
})
|
||||
return result
|
||||
|
||||
def add_context(self, name: str, regexes: List[str]):
|
||||
def add_context(self, name: str, regexes: list[str]):
|
||||
if not name.isalnum():
|
||||
raise ValueError("Context name must be alphanumeric")
|
||||
self._validate_regexes(regexes)
|
||||
|
||||
ctxs = self.contexts
|
||||
if name in ctxs:
|
||||
raise ValueError(f"Context '{name}' already exists")
|
||||
|
||||
ctxs[name] = regexes
|
||||
ctxs[name] = list(regexes)
|
||||
self.config.config["contexts"] = ctxs
|
||||
self.config._saveconfig(self.config.file)
|
||||
|
||||
def update_context(self, name: str, regexes: List[str]):
|
||||
def update_context(self, name: str, regexes: list[str]):
|
||||
if name == "all":
|
||||
raise ValueError("Cannot modify default context 'all'")
|
||||
self._validate_regexes(regexes)
|
||||
|
||||
ctxs = self.contexts
|
||||
if name not in ctxs:
|
||||
raise ValueError(f"Context '{name}' does not exist")
|
||||
|
||||
ctxs[name] = regexes
|
||||
ctxs[name] = list(regexes)
|
||||
self.config.config["contexts"] = ctxs
|
||||
self.config._saveconfig(self.config.file)
|
||||
|
||||
@@ -70,12 +96,28 @@ class ContextService(BaseService):
|
||||
self.config.config["current_context"] = name
|
||||
self.config._saveconfig(self.config.file)
|
||||
|
||||
def get_active_regexes(self) -> List[re.Pattern]:
|
||||
patterns = self.contexts.get(self.current_context, [".*"])
|
||||
return [re.compile(p) for p in patterns]
|
||||
def get_active_regexes(self) -> list[re.Pattern]:
|
||||
return compile_active_context_patterns(getattr(self.config, "config", None))
|
||||
|
||||
def _match_any(self, node_name: str, patterns: List[re.Pattern]) -> bool:
|
||||
return any(p.match(node_name) for p in patterns)
|
||||
def _match_any(self, node_name: str, patterns: list[re.Pattern]) -> bool:
|
||||
return match_active_context_patterns(node_name, patterns)
|
||||
|
||||
def is_default_all_context(self) -> bool:
|
||||
return is_default_all_context_cfg(getattr(self.config, "config", None))
|
||||
|
||||
def matches_active_context(self, name: str) -> bool:
|
||||
if self.is_default_all_context():
|
||||
return isinstance(name, str) and bool(name)
|
||||
if not isinstance(name, str) or not name:
|
||||
return False
|
||||
return match_active_context_patterns(name, self.get_active_regexes())
|
||||
|
||||
def validate_in_active_context(self, unique_id: str, is_folder: bool = False) -> None:
|
||||
if not self.is_default_all_context() and not self.matches_active_context(unique_id):
|
||||
label = "Folder" if is_folder else "Node"
|
||||
raise InvalidConfigurationError(
|
||||
f"{label} '{unique_id}' does not match active context '{self.current_context}'."
|
||||
)
|
||||
|
||||
# Hook handlers for filtering
|
||||
def filter_node_list(self, *args, **kwargs):
|
||||
|
||||
@@ -1,10 +1,16 @@
|
||||
import inspect
|
||||
import os
|
||||
import re
|
||||
import sys
|
||||
from collections.abc import Callable
|
||||
from contextlib import suppress
|
||||
from typing import Any
|
||||
|
||||
from connpy.auth_utils import AuthAttemptTracker, raise_sanitized_auth_error
|
||||
from connpy.auth_utils import (
|
||||
MAX_REMOTE_FILTER_NODES,
|
||||
AuthAttemptTracker,
|
||||
raise_sanitized_auth_error,
|
||||
)
|
||||
from connpy.core import nodes as Nodes
|
||||
|
||||
from .base import BaseService
|
||||
@@ -14,14 +20,33 @@ from .exceptions import ConnpyError
|
||||
class ExecutionService(BaseService):
|
||||
"""Business logic for executing commands on nodes and running automation scripts."""
|
||||
|
||||
def __init__(self, config=None, remote_stub=None, attempt_tracker=None):
|
||||
def __init__(
|
||||
self,
|
||||
config=None,
|
||||
remote_stub=None,
|
||||
attempt_tracker=None,
|
||||
enforce_context: bool = True,
|
||||
):
|
||||
super().__init__(config)
|
||||
self.remote_stub = remote_stub
|
||||
self.attempt_tracker = (
|
||||
attempt_tracker if attempt_tracker is not None else AuthAttemptTracker()
|
||||
)
|
||||
self.enforce_context = bool(enforce_context)
|
||||
self.last_fallback_used: dict[str, bool] = {}
|
||||
|
||||
def _get_context_service(self):
|
||||
if (
|
||||
not getattr(self, "enforce_context", True)
|
||||
or not getattr(self.config, "_enforce_context", True)
|
||||
or (len(sys.argv) > 1 and sys.argv[1] == "api")
|
||||
or not isinstance(getattr(self.config, "config", None), dict)
|
||||
):
|
||||
return None
|
||||
from .context_service import ContextService
|
||||
|
||||
return ContextService(self.config)
|
||||
|
||||
def _is_remote_backend(self) -> bool:
|
||||
"""Return True when execution is configured to run against a remote gRPC backend."""
|
||||
if getattr(self, "remote_stub", None) is not None:
|
||||
@@ -40,10 +65,7 @@ class ExecutionService(BaseService):
|
||||
"connect_timeout and auth_timeout overrides are not supported in remote gRPC mode"
|
||||
)
|
||||
|
||||
def _get_remote_stub(self):
|
||||
"""Return the configured or lazily constructed ExecutionStub for remote gRPC mode."""
|
||||
if getattr(self, "remote_stub", None) is not None:
|
||||
return self.remote_stub
|
||||
def _get_remote_channel(self):
|
||||
cfg = getattr(self.config, "config", None)
|
||||
remote_host = cfg.get("remote_host") if isinstance(cfg, dict) else None
|
||||
if not remote_host:
|
||||
@@ -51,7 +73,7 @@ class ExecutionService(BaseService):
|
||||
|
||||
import grpc
|
||||
|
||||
from connpy.grpc_layer.stubs import AuthClientInterceptor, ExecutionStub
|
||||
from connpy.grpc_layer.stubs import AuthClientInterceptor
|
||||
|
||||
def get_token():
|
||||
env_token = os.environ.get("CONNPY_TOKEN")
|
||||
@@ -67,8 +89,76 @@ class ExecutionService(BaseService):
|
||||
|
||||
channel = grpc.insecure_channel(remote_host)
|
||||
channel = grpc.intercept_channel(channel, AuthClientInterceptor(get_token))
|
||||
return channel, remote_host
|
||||
|
||||
def _get_remote_stub(self):
|
||||
"""Return the configured or lazily constructed ExecutionStub for remote gRPC mode."""
|
||||
if getattr(self, "remote_stub", None) is not None:
|
||||
return self.remote_stub
|
||||
from connpy.grpc_layer.stubs import ExecutionStub
|
||||
|
||||
channel, remote_host = self._get_remote_channel()
|
||||
return ExecutionStub(channel, remote_host=remote_host)
|
||||
|
||||
def _filter_remote_nodes_filter(self, nodes_filter):
|
||||
ctx_svc = self._get_context_service()
|
||||
if ctx_svc is None or ctx_svc.is_default_all_context():
|
||||
return nodes_filter
|
||||
|
||||
if isinstance(nodes_filter, (list, tuple)):
|
||||
permitted = ctx_svc.filter_node_list(result=list(nodes_filter))
|
||||
else:
|
||||
if self.remote_stub is not None and callable(
|
||||
getattr(self.remote_stub, "list_nodes", None)
|
||||
):
|
||||
candidates = self.remote_stub.list_nodes(filter_str=nodes_filter)
|
||||
elif self.remote_stub is not None and callable(
|
||||
getattr(getattr(self.remote_stub, "node_stub", None), "list_nodes", None)
|
||||
):
|
||||
candidates = self.remote_stub.node_stub.list_nodes(filter_str=nodes_filter)
|
||||
elif self.remote_stub is None:
|
||||
from connpy.grpc_layer.stubs import NodeStub
|
||||
|
||||
channel, remote_host = self._get_remote_channel()
|
||||
node_stub = NodeStub(channel, remote_host=remote_host, config=self.config)
|
||||
candidates = node_stub.list_nodes(filter_str=nodes_filter)
|
||||
else:
|
||||
candidates = (
|
||||
self.config._getallnodes(nodes_filter)
|
||||
if hasattr(self.config, "_getallnodes")
|
||||
else []
|
||||
)
|
||||
permitted = ctx_svc.filter_node_list(result=candidates)
|
||||
|
||||
if not permitted:
|
||||
raise ConnpyError(f"No nodes found matching filter: {nodes_filter}")
|
||||
return [f"^{re.escape(n)}$" for n in permitted]
|
||||
|
||||
def _build_remote_filter_batches(self, nodes_filter):
|
||||
filtered = self._filter_remote_nodes_filter(nodes_filter)
|
||||
if isinstance(filtered, list):
|
||||
return [
|
||||
filtered[i : i + MAX_REMOTE_FILTER_NODES]
|
||||
for i in range(0, len(filtered), MAX_REMOTE_FILTER_NODES)
|
||||
]
|
||||
return [filtered]
|
||||
|
||||
@staticmethod
|
||||
def _aggregate_remote_results(batch_results):
|
||||
if len(batch_results) == 1:
|
||||
return batch_results[0]
|
||||
if all(isinstance(r, dict) for r in batch_results):
|
||||
merged: dict[str, Any] = {}
|
||||
for r in batch_results:
|
||||
merged.update(r)
|
||||
return merged
|
||||
if all(isinstance(r, list) for r in batch_results):
|
||||
merged_list: list[Any] = []
|
||||
for r in batch_results:
|
||||
merged_list.extend(r)
|
||||
return merged_list
|
||||
return batch_results[-1]
|
||||
|
||||
def run_commands(
|
||||
self,
|
||||
nodes_filter: str,
|
||||
@@ -93,8 +183,10 @@ class ExecutionService(BaseService):
|
||||
|
||||
if self._is_remote_backend():
|
||||
stub = self._get_remote_stub()
|
||||
return stub.run_commands(
|
||||
nodes_filter=nodes_filter,
|
||||
batches = self._build_remote_filter_batches(nodes_filter)
|
||||
batch_results = [
|
||||
stub.run_commands(
|
||||
nodes_filter=batch,
|
||||
commands=commands,
|
||||
variables=variables,
|
||||
parallel=parallel,
|
||||
@@ -105,8 +197,14 @@ class ExecutionService(BaseService):
|
||||
logger=logger,
|
||||
name=name or "",
|
||||
)
|
||||
for batch in batches
|
||||
]
|
||||
return self._aggregate_remote_results(batch_results)
|
||||
|
||||
matched_names = self.config._getallnodes(nodes_filter)
|
||||
ctx_svc = self._get_context_service()
|
||||
if ctx_svc is not None and not ctx_svc.is_default_all_context():
|
||||
matched_names = ctx_svc.filter_node_list(result=matched_names)
|
||||
if not matched_names:
|
||||
raise ConnpyError(f"No nodes found matching filter: {nodes_filter}")
|
||||
|
||||
@@ -181,8 +279,10 @@ class ExecutionService(BaseService):
|
||||
|
||||
if self._is_remote_backend():
|
||||
stub = self._get_remote_stub()
|
||||
return stub.test_commands(
|
||||
nodes_filter=nodes_filter,
|
||||
batches = self._build_remote_filter_batches(nodes_filter)
|
||||
batch_results = [
|
||||
stub.test_commands(
|
||||
nodes_filter=batch,
|
||||
commands=commands,
|
||||
expected=expected,
|
||||
variables=variables,
|
||||
@@ -194,8 +294,14 @@ class ExecutionService(BaseService):
|
||||
logger=logger,
|
||||
name=name or "",
|
||||
)
|
||||
for batch in batches
|
||||
]
|
||||
return self._aggregate_remote_results(batch_results)
|
||||
|
||||
matched_names = self.config._getallnodes(nodes_filter)
|
||||
ctx_svc = self._get_context_service()
|
||||
if ctx_svc is not None and not ctx_svc.is_default_all_context():
|
||||
matched_names = ctx_svc.filter_node_list(result=matched_names)
|
||||
if not matched_names:
|
||||
raise ConnpyError(f"No nodes found matching filter: {nodes_filter}")
|
||||
|
||||
|
||||
@@ -1,14 +1,56 @@
|
||||
from .base import BaseService
|
||||
import yaml
|
||||
import os
|
||||
import sys
|
||||
from copy import deepcopy
|
||||
from .exceptions import InvalidConfigurationError, NodeNotFoundError, ReservedNameError
|
||||
|
||||
import yaml
|
||||
|
||||
from ..configfile import NoAliasDumper
|
||||
from .base import BaseService
|
||||
from .exceptions import InvalidConfigurationError, NodeNotFoundError
|
||||
from .node_service import NodeService, _supports_transactional_update
|
||||
|
||||
|
||||
class ImportExportService(BaseService):
|
||||
"""Business logic for YAML/JSON inventory import and export."""
|
||||
|
||||
def __init__(self, config=None, enforce_context: bool = True):
|
||||
super().__init__(config)
|
||||
self.enforce_context = bool(enforce_context)
|
||||
|
||||
def _get_context_service(self, cfg=None):
|
||||
target_cfg = cfg or self.config
|
||||
if (
|
||||
not getattr(self, "enforce_context", True)
|
||||
or not getattr(target_cfg, "_enforce_context", True)
|
||||
or (len(sys.argv) > 1 and sys.argv[1] == "api")
|
||||
or not isinstance(getattr(target_cfg, "config", None), dict)
|
||||
):
|
||||
return None
|
||||
from .context_service import ContextService
|
||||
|
||||
return ContextService(target_cfg)
|
||||
|
||||
def _get_raw_folders(self, cfg=None) -> list[str]:
|
||||
target_cfg = cfg or self.config
|
||||
if target_cfg is None:
|
||||
return []
|
||||
if (
|
||||
not hasattr(target_cfg, "assert_called")
|
||||
and callable(getattr(target_cfg, "_getallfolders_unlocked", None))
|
||||
and hasattr(target_cfg, "_mutation_lock")
|
||||
):
|
||||
with target_cfg._mutation_lock:
|
||||
return target_cfg._getallfolders_unlocked()
|
||||
return target_cfg._getallfolders()
|
||||
|
||||
def _folder_exists(self, folder_id: str, raw_folders: list[str], cfg=None) -> bool:
|
||||
target_cfg = cfg or self.config
|
||||
cfg_dict = getattr(target_cfg, "config", None)
|
||||
case = cfg_dict.get("case", False) if isinstance(cfg_dict, dict) else False
|
||||
if case:
|
||||
return folder_id in raw_folders
|
||||
return folder_id.lower() in {f.lower() for f in raw_folders}
|
||||
|
||||
def export_to_file(self, file_path, folders=None):
|
||||
"""Export nodes/folders to a YAML file."""
|
||||
if os.path.exists(file_path):
|
||||
@@ -23,16 +65,42 @@ class ImportExportService(BaseService):
|
||||
|
||||
def export_to_dict(self, folders=None):
|
||||
"""Export nodes/folders to a dictionary."""
|
||||
if not folders:
|
||||
return deepcopy(self.config.connections)
|
||||
else:
|
||||
# Validate folders exist
|
||||
for f in folders:
|
||||
if f != "@" and f not in self.config._getallfolders():
|
||||
raise NodeNotFoundError(f"Folder '{f}' not found.")
|
||||
ctx_svc = self._get_context_service()
|
||||
ctx_active = ctx_svc is not None and not ctx_svc.is_default_all_context()
|
||||
|
||||
if not folders and not ctx_active:
|
||||
return deepcopy(self.config.connections)
|
||||
|
||||
raw_folders = self._get_raw_folders()
|
||||
if folders:
|
||||
for f in folders:
|
||||
if f != "@" and (
|
||||
not self._folder_exists(f, raw_folders)
|
||||
or (ctx_active and not ctx_svc.matches_active_context(f))
|
||||
):
|
||||
raise NodeNotFoundError(f"Folder '{f}' not found.")
|
||||
flat = self.config._getallnodesfull(folders, extract=False)
|
||||
allowed_folders = []
|
||||
else:
|
||||
allowed_folders = ctx_svc.filter_node_list(result=raw_folders)
|
||||
flat = self.config._getallnodesfull(extract=False)
|
||||
|
||||
if ctx_active:
|
||||
flat = ctx_svc.filter_node_dict(result=flat)
|
||||
|
||||
nested = {}
|
||||
for folder_id in allowed_folders:
|
||||
uniques = self.config._explode_unique(folder_id)
|
||||
if not uniques or "folder" not in uniques:
|
||||
continue
|
||||
f_name = uniques["folder"]
|
||||
if f_name not in nested:
|
||||
nested[f_name] = {"type": "folder"}
|
||||
if "subfolder" in uniques:
|
||||
s_name = uniques["subfolder"]
|
||||
if s_name not in nested[f_name]:
|
||||
nested[f_name][s_name] = {"type": "subfolder"}
|
||||
|
||||
for k, v in flat.items():
|
||||
uniques = self.config._explode_unique(k)
|
||||
if not uniques:
|
||||
@@ -77,22 +145,52 @@ class ImportExportService(BaseService):
|
||||
raise InvalidConfigurationError(f"Failed to read/parse import file: {e}")
|
||||
|
||||
def import_from_dict(self, data):
|
||||
"""Import nodes/folders from a dictionary."""
|
||||
"""Import nodes/folders from a dictionary atomically with context pre-validation."""
|
||||
if not isinstance(data, dict):
|
||||
raise InvalidConfigurationError("Invalid import data format: expected a dictionary of nodes.")
|
||||
raise InvalidConfigurationError(
|
||||
"Invalid import data format: expected a dictionary of nodes."
|
||||
)
|
||||
|
||||
def _traverse_import(node_data, current_folder='', current_subfolder=''):
|
||||
ctx_svc = self._get_context_service()
|
||||
ctx_active = ctx_svc is not None and not ctx_svc.is_default_all_context()
|
||||
raw_folders = self._get_raw_folders() if ctx_active else []
|
||||
planned_ops = []
|
||||
|
||||
def _plan_import(node_data, current_folder="", current_subfolder=""):
|
||||
for k, v in node_data.items():
|
||||
if k == "type":
|
||||
if k == "type" or not isinstance(v, dict):
|
||||
continue
|
||||
if isinstance(v, dict):
|
||||
node_type = v.get("type", "connection")
|
||||
if node_type == "folder":
|
||||
self.config._folder_add(folder=k)
|
||||
_traverse_import(v, current_folder=k, current_subfolder='')
|
||||
folder_id = f"@{k}"
|
||||
has_children = any(
|
||||
isinstance(cv, dict) for ck, cv in v.items() if ck != "type"
|
||||
)
|
||||
if ctx_active and (
|
||||
not has_children
|
||||
or (
|
||||
not ctx_svc.matches_active_context(folder_id)
|
||||
and not self._folder_exists(folder_id, raw_folders)
|
||||
)
|
||||
):
|
||||
ctx_svc.validate_in_active_context(folder_id, is_folder=True)
|
||||
planned_ops.append(("folder", k, ""))
|
||||
_plan_import(v, current_folder=k, current_subfolder="")
|
||||
elif node_type == "subfolder":
|
||||
self.config._folder_add(folder=current_folder, subfolder=k)
|
||||
_traverse_import(v, current_folder=current_folder, current_subfolder=k)
|
||||
folder_id = f"@{k}@{current_folder}"
|
||||
has_children = any(
|
||||
isinstance(cv, dict) for ck, cv in v.items() if ck != "type"
|
||||
)
|
||||
if ctx_active and (
|
||||
not has_children
|
||||
or (
|
||||
not ctx_svc.matches_active_context(folder_id)
|
||||
and not self._folder_exists(folder_id, raw_folders)
|
||||
)
|
||||
):
|
||||
ctx_svc.validate_in_active_context(folder_id, is_folder=True)
|
||||
planned_ops.append(("folder", current_folder, k))
|
||||
_plan_import(v, current_folder=current_folder, current_subfolder=k)
|
||||
elif node_type == "connection":
|
||||
unique_id = k
|
||||
if current_subfolder:
|
||||
@@ -100,16 +198,38 @@ class ImportExportService(BaseService):
|
||||
elif current_folder:
|
||||
unique_id = f"{k}@{current_folder}"
|
||||
self._validate_node_name(unique_id)
|
||||
NodeService._validate_node_payload(v)
|
||||
if ctx_svc is not None:
|
||||
ctx_svc.validate_in_active_context(unique_id, is_folder=False)
|
||||
|
||||
kwargs = deepcopy(v)
|
||||
kwargs['id'] = k
|
||||
kwargs['folder'] = current_folder
|
||||
kwargs['subfolder'] = current_subfolder
|
||||
kwargs["id"] = k
|
||||
kwargs["folder"] = current_folder
|
||||
kwargs["subfolder"] = current_subfolder
|
||||
planned_ops.append(("connection", kwargs))
|
||||
|
||||
self.config._connections_add(**kwargs)
|
||||
_plan_import(data)
|
||||
|
||||
if _supports_transactional_update(self.config):
|
||||
def _mut(cfg):
|
||||
for op in planned_ops:
|
||||
if op[0] == "folder":
|
||||
cfg._folder_add_unlocked(folder=op[1], subfolder=op[2])
|
||||
else:
|
||||
# Invalid format skip
|
||||
pass
|
||||
cfg._connections_add_unlocked(**op[1])
|
||||
|
||||
_traverse_import(data)
|
||||
self.config.transactional_update(_mut)
|
||||
return
|
||||
|
||||
snap_connections = deepcopy(self.config.connections)
|
||||
try:
|
||||
for op in planned_ops:
|
||||
if op[0] == "folder":
|
||||
self.config._folder_add(folder=op[1], subfolder=op[2])
|
||||
else:
|
||||
self.config._connections_add(**op[1])
|
||||
self.config._saveconfig(self.config.file)
|
||||
except BaseException:
|
||||
self.config.connections.clear()
|
||||
self.config.connections.update(snap_connections)
|
||||
raise
|
||||
+155
-23
@@ -1,5 +1,6 @@
|
||||
import inspect
|
||||
import re
|
||||
import sys
|
||||
from copy import deepcopy
|
||||
from typing import ClassVar
|
||||
|
||||
@@ -38,8 +39,104 @@ class NodeService(BaseService):
|
||||
"type",
|
||||
})
|
||||
|
||||
def __init__(self, config=None):
|
||||
def __init__(self, config=None, enforce_context: bool = True):
|
||||
super().__init__(config)
|
||||
self.enforce_context = bool(enforce_context)
|
||||
|
||||
def _get_context_service(self, cfg=None):
|
||||
target_cfg = cfg or self.config
|
||||
if (
|
||||
not getattr(self, "enforce_context", True)
|
||||
or not getattr(target_cfg, "_enforce_context", True)
|
||||
or (len(sys.argv) > 1 and sys.argv[1] == "api")
|
||||
or not isinstance(getattr(target_cfg, "config", None), dict)
|
||||
):
|
||||
return None
|
||||
from .context_service import ContextService
|
||||
|
||||
return ContextService(target_cfg)
|
||||
|
||||
def _get_raw_folders(self, cfg=None) -> list[str]:
|
||||
target_cfg = cfg or self.config
|
||||
if target_cfg is None:
|
||||
return []
|
||||
if (
|
||||
not hasattr(target_cfg, "assert_called")
|
||||
and callable(getattr(target_cfg, "_getallfolders_unlocked", None))
|
||||
and hasattr(target_cfg, "_mutation_lock")
|
||||
):
|
||||
with target_cfg._mutation_lock:
|
||||
return target_cfg._getallfolders_unlocked()
|
||||
return target_cfg._getallfolders()
|
||||
|
||||
def _get_raw_nodes(self, cfg=None) -> list[str]:
|
||||
target_cfg = cfg or self.config
|
||||
if target_cfg is None:
|
||||
return []
|
||||
if (
|
||||
not hasattr(target_cfg, "assert_called")
|
||||
and callable(getattr(target_cfg, "_getallnodes_unlocked", None))
|
||||
and hasattr(target_cfg, "_mutation_lock")
|
||||
):
|
||||
with target_cfg._mutation_lock:
|
||||
return target_cfg._getallnodes_unlocked()
|
||||
return target_cfg._getallnodes()
|
||||
|
||||
def _derive_parent_folder(self, cfg, unique_id: str, is_folder: bool) -> str | None:
|
||||
explode_fn = (
|
||||
cfg._explode_unique_unlocked
|
||||
if (
|
||||
not hasattr(cfg, "assert_called")
|
||||
and callable(getattr(cfg, "_explode_unique_unlocked", None))
|
||||
)
|
||||
else cfg._explode_unique
|
||||
)
|
||||
uniques = explode_fn(unique_id)
|
||||
if is_folder:
|
||||
if (
|
||||
not isinstance(unique_id, str)
|
||||
or not unique_id.startswith("@")
|
||||
or not isinstance(uniques, dict)
|
||||
or "folder" not in uniques
|
||||
):
|
||||
raise InvalidConfigurationError(f"Invalid folder name '{unique_id}'.")
|
||||
if "subfolder" in uniques:
|
||||
return f"@{uniques['folder']}"
|
||||
return None
|
||||
if (
|
||||
not isinstance(unique_id, str)
|
||||
or unique_id.startswith("@")
|
||||
or not isinstance(uniques, dict)
|
||||
or not uniques.get("id")
|
||||
):
|
||||
raise InvalidConfigurationError(f"Invalid node name '{unique_id}'.")
|
||||
if "subfolder" in uniques and "folder" in uniques:
|
||||
return f"@{uniques['subfolder']}@{uniques['folder']}"
|
||||
if "folder" in uniques:
|
||||
return f"@{uniques['folder']}"
|
||||
return None
|
||||
|
||||
def _folder_exists(self, parent_folder: str, raw_folders: list[str], cfg=None) -> bool:
|
||||
target_cfg = cfg or self.config
|
||||
cfg_dict = getattr(target_cfg, "config", None)
|
||||
case = cfg_dict.get("case", False) if isinstance(cfg_dict, dict) else False
|
||||
if case:
|
||||
return parent_folder in raw_folders
|
||||
return parent_folder.lower() in {f.lower() for f in raw_folders}
|
||||
|
||||
def _ensure_in_active_context(
|
||||
self, unique_id: str, *, is_folder: bool = False, not_found_msg: str | None = None
|
||||
) -> None:
|
||||
ctx_svc = self._get_context_service()
|
||||
if (
|
||||
ctx_svc is not None
|
||||
and not ctx_svc.is_default_all_context()
|
||||
and not ctx_svc.matches_active_context(unique_id)
|
||||
):
|
||||
if not_found_msg is not None:
|
||||
raise NodeNotFoundError(not_found_msg)
|
||||
label = "Folder" if is_folder else "Node"
|
||||
raise NodeNotFoundError(f"{label} '{unique_id}' not found.")
|
||||
|
||||
@staticmethod
|
||||
def _validate_node_payload(data):
|
||||
@@ -78,6 +175,10 @@ class NodeService(BaseService):
|
||||
flags = re.IGNORECASE if not case_sensitive else 0
|
||||
nodes = [n for n in nodes if re.search(filter_str, n, flags)]
|
||||
|
||||
ctx_svc = self._get_context_service()
|
||||
if ctx_svc is not None and not ctx_svc.is_default_all_context():
|
||||
nodes = ctx_svc.filter_node_list(result=nodes)
|
||||
|
||||
if not format_str:
|
||||
return nodes
|
||||
|
||||
@@ -134,10 +235,14 @@ class NodeService(BaseService):
|
||||
else:
|
||||
flags = re.IGNORECASE if not case_sensitive else 0
|
||||
folders = [f for f in folders if re.search(filter_str, f, flags)]
|
||||
ctx_svc = self._get_context_service()
|
||||
if ctx_svc is not None and not ctx_svc.is_default_all_context():
|
||||
folders = ctx_svc.filter_node_list(result=folders)
|
||||
return folders
|
||||
|
||||
def get_node_details(self, unique_id):
|
||||
"""Return full configuration dictionary for a specific node."""
|
||||
self._ensure_in_active_context(unique_id, is_folder=False)
|
||||
try:
|
||||
details = self.config.getitem(unique_id)
|
||||
if not details:
|
||||
@@ -155,50 +260,45 @@ class NodeService(BaseService):
|
||||
self.config._generate_nodes_cache(nodes=nodes, folders=folders, profiles=profiles)
|
||||
|
||||
def validate_parent_folder(self, unique_id, is_folder=False):
|
||||
"""Check if parent folder exists for a given node unique ID."""
|
||||
if is_folder:
|
||||
uniques = self.config._explode_unique(unique_id)
|
||||
if uniques and "subfolder" in uniques and "folder" in uniques:
|
||||
parent_folder = f"@{uniques['folder']}"
|
||||
if parent_folder not in self.config._getallfolders():
|
||||
"""Check if unique_id is in active context and its parent folder exists."""
|
||||
parent_folder = self._derive_parent_folder(self.config, unique_id, is_folder=is_folder)
|
||||
ctx_svc = self._get_context_service()
|
||||
if ctx_svc is not None:
|
||||
ctx_svc.validate_in_active_context(unique_id, is_folder=is_folder)
|
||||
if parent_folder is not None and not self._folder_exists(
|
||||
parent_folder, self._get_raw_folders(), self.config
|
||||
):
|
||||
raise NodeNotFoundError(f"Folder '{parent_folder}' not found.")
|
||||
else:
|
||||
node_folder = unique_id.partition("@")[2]
|
||||
if node_folder:
|
||||
parent_folder = f"@{node_folder}"
|
||||
if parent_folder not in self.config._getallfolders():
|
||||
raise NodeNotFoundError(f"Folder '{parent_folder}' not found.")
|
||||
|
||||
|
||||
def add_node(self, unique_id, data, is_folder=False):
|
||||
"""Logic for adding a new node or folder to configuration."""
|
||||
if not is_folder:
|
||||
self._validate_node_name(unique_id)
|
||||
self._validate_node_payload(data)
|
||||
self.validate_parent_folder(unique_id, is_folder=is_folder)
|
||||
|
||||
if _supports_transactional_update(self.config):
|
||||
def _mut(cfg):
|
||||
all_nodes = cfg._getallnodes_unlocked()
|
||||
all_folders = cfg._getallfolders_unlocked()
|
||||
parent_folder = self._derive_parent_folder(cfg, unique_id, is_folder=is_folder)
|
||||
ctx_svc = self._get_context_service(cfg)
|
||||
if ctx_svc is not None:
|
||||
ctx_svc.validate_in_active_context(unique_id, is_folder=is_folder)
|
||||
if parent_folder is not None and not self._folder_exists(
|
||||
parent_folder, all_folders, cfg
|
||||
):
|
||||
raise NodeNotFoundError(f"Folder '{parent_folder}' not found.")
|
||||
if is_folder:
|
||||
if unique_id in all_folders:
|
||||
raise NodeAlreadyExistsError(f"Folder '{unique_id}' already exists.")
|
||||
uniques = cfg._explode_unique_unlocked(unique_id)
|
||||
if not uniques:
|
||||
raise InvalidConfigurationError(f"Invalid folder name '{unique_id}'.")
|
||||
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)
|
||||
@@ -255,6 +355,7 @@ class NodeService(BaseService):
|
||||
|
||||
def update_node(self, unique_id, data, save=True):
|
||||
"""Explicitly update an existing node."""
|
||||
self._ensure_in_active_context(unique_id, is_folder=False)
|
||||
if _supports_transactional_update(self.config) and save:
|
||||
def _mut(cfg):
|
||||
all_nodes = cfg._getallnodes_unlocked()
|
||||
@@ -281,6 +382,7 @@ class NodeService(BaseService):
|
||||
|
||||
def delete_node(self, unique_id, is_folder=False, save=True):
|
||||
"""Logic for deleting a node or folder."""
|
||||
self._ensure_in_active_context(unique_id, is_folder=is_folder)
|
||||
if _supports_transactional_update(self.config) and save:
|
||||
def _mut(cfg):
|
||||
uniques = cfg._explode_unique_unlocked(unique_id)
|
||||
@@ -311,6 +413,7 @@ class NodeService(BaseService):
|
||||
|
||||
def connect_node(self, unique_id, sftp=False, debug=False, logger=None):
|
||||
"""Interact with a node directly."""
|
||||
self._ensure_in_active_context(unique_id, is_folder=False)
|
||||
from connpy.core import node
|
||||
|
||||
from ..auth_utils import resolve_fallback_entries
|
||||
@@ -347,10 +450,28 @@ class NodeService(BaseService):
|
||||
|
||||
def move_node(self, src_id, dst_id, copy=False):
|
||||
"""Move or copy a node."""
|
||||
self._ensure_in_active_context(
|
||||
src_id, is_folder=False, not_found_msg=f"Source node '{src_id}' not found."
|
||||
)
|
||||
self._validate_node_name(dst_id)
|
||||
self.validate_parent_folder(dst_id, is_folder=False)
|
||||
|
||||
if _supports_transactional_update(self.config):
|
||||
def _mut(cfg):
|
||||
ctx_svc = self._get_context_service(cfg)
|
||||
if (
|
||||
ctx_svc is not None
|
||||
and not ctx_svc.is_default_all_context()
|
||||
and not ctx_svc.matches_active_context(src_id)
|
||||
):
|
||||
raise NodeNotFoundError(f"Source node '{src_id}' not found.")
|
||||
parent_folder = self._derive_parent_folder(cfg, dst_id, is_folder=False)
|
||||
if ctx_svc is not None:
|
||||
ctx_svc.validate_in_active_context(dst_id, is_folder=False)
|
||||
if parent_folder is not None and not self._folder_exists(
|
||||
parent_folder, cfg._getallfolders_unlocked(), cfg
|
||||
):
|
||||
raise NodeNotFoundError(f"Folder '{parent_folder}' not found.")
|
||||
try:
|
||||
node_data = cfg.getitem_unlocked(src_id)
|
||||
except (KeyError, TypeError):
|
||||
@@ -397,12 +518,23 @@ class NodeService(BaseService):
|
||||
def bulk_add(self, ids, hosts, common_data):
|
||||
"""Add multiple nodes with shared common configuration."""
|
||||
self._validate_node_payload(common_data)
|
||||
for uid in ids:
|
||||
self.validate_parent_folder(uid, is_folder=False)
|
||||
|
||||
if _supports_transactional_update(self.config):
|
||||
def _build_entries(cfg):
|
||||
all_nodes = cfg._getallnodes_unlocked()
|
||||
all_folders = cfg._getallfolders_unlocked()
|
||||
ctx_svc = self._get_context_service(cfg)
|
||||
planned = []
|
||||
for i, uid in enumerate(ids):
|
||||
parent_folder = self._derive_parent_folder(cfg, uid, is_folder=False)
|
||||
if ctx_svc is not None:
|
||||
ctx_svc.validate_in_active_context(uid, is_folder=False)
|
||||
if parent_folder is not None and not self._folder_exists(
|
||||
parent_folder, all_folders, cfg
|
||||
):
|
||||
raise NodeNotFoundError(f"Folder '{parent_folder}' not found.")
|
||||
if uid in all_nodes:
|
||||
continue
|
||||
try:
|
||||
|
||||
@@ -15,19 +15,22 @@ class RemoteStub:
|
||||
class ServiceProvider:
|
||||
"""Dynamic service backend. Transparently provides local or remote services."""
|
||||
|
||||
def __init__(self, config, mode="local", remote_host=None):
|
||||
def __init__(self, config, mode="local", remote_host=None, enforce_context: bool = True):
|
||||
from ..auth_utils import AuthAttemptTracker
|
||||
|
||||
self.mode = mode
|
||||
self.config = config
|
||||
self.remote_host = remote_host
|
||||
self.enforce_context = bool(enforce_context)
|
||||
self.auth_tracker = AuthAttemptTracker()
|
||||
self.nodes = None
|
||||
self._system = None
|
||||
self._execution = None
|
||||
self._import_export = None
|
||||
self._sync = None
|
||||
self._users = None
|
||||
self._ai = None
|
||||
self.set_enforce_context(self.enforce_context)
|
||||
|
||||
if mode == "local":
|
||||
self._init_local()
|
||||
@@ -36,6 +39,18 @@ class ServiceProvider:
|
||||
else:
|
||||
raise ValueError(f"Unknown service mode: {mode}")
|
||||
|
||||
def set_enforce_context(self, enforce_context: bool) -> None:
|
||||
self.enforce_context = bool(enforce_context)
|
||||
if self.config is not None and not hasattr(self.config, "assert_called"):
|
||||
self.config._enforce_context = self.enforce_context
|
||||
for svc in (
|
||||
getattr(self, "nodes", None),
|
||||
self._execution,
|
||||
self._import_export,
|
||||
):
|
||||
if svc is not None and hasattr(svc, "enforce_context"):
|
||||
svc.enforce_context = self.enforce_context
|
||||
|
||||
def _init_local(self):
|
||||
from .config_service import ConfigService
|
||||
from .context_service import ContextService
|
||||
@@ -43,7 +58,7 @@ class ServiceProvider:
|
||||
from .plugin_service import PluginService
|
||||
from .profile_service import ProfileService
|
||||
|
||||
self.nodes = NodeService(self.config)
|
||||
self.nodes = NodeService(self.config, enforce_context=self.enforce_context)
|
||||
self.profiles = ProfileService(self.config)
|
||||
self.config_svc = ConfigService(self.config)
|
||||
self.plugins = PluginService(self.config)
|
||||
@@ -101,7 +116,12 @@ class ServiceProvider:
|
||||
self.plugins = PluginStub(channel, remote_host=self.remote_host)
|
||||
self.ai = AIStub(channel, remote_host=self.remote_host)
|
||||
self.system = SystemStub(channel, remote_host=self.remote_host)
|
||||
self.execution = ExecutionStub(channel, remote_host=self.remote_host)
|
||||
self.execution = ExecutionStub(
|
||||
channel,
|
||||
remote_host=self.remote_host,
|
||||
config=self.config,
|
||||
node_stub=self.nodes,
|
||||
)
|
||||
self.import_export = ImportExportStub(channel, remote_host=self.remote_host)
|
||||
self.auth = AuthStub(channel, remote_host=self.remote_host)
|
||||
|
||||
@@ -120,10 +140,13 @@ class ServiceProvider:
|
||||
def execution(self):
|
||||
if self._execution is None and self.mode == "local":
|
||||
from .execution_service import ExecutionService
|
||||
if "attempt_tracker" in inspect.signature(ExecutionService.__init__).parameters:
|
||||
self._execution = ExecutionService(self.config, attempt_tracker=self.auth_tracker)
|
||||
else:
|
||||
self._execution = ExecutionService(self.config)
|
||||
params = inspect.signature(ExecutionService.__init__).parameters
|
||||
kwargs = {}
|
||||
if "attempt_tracker" in params:
|
||||
kwargs["attempt_tracker"] = self.auth_tracker
|
||||
if "enforce_context" in params:
|
||||
kwargs["enforce_context"] = self.enforce_context
|
||||
self._execution = ExecutionService(self.config, **kwargs)
|
||||
return self._execution
|
||||
|
||||
@execution.setter
|
||||
@@ -134,6 +157,12 @@ class ServiceProvider:
|
||||
def import_export(self):
|
||||
if self._import_export is None and self.mode == "local":
|
||||
from .import_export_service import ImportExportService
|
||||
params = inspect.signature(ImportExportService.__init__).parameters
|
||||
if "enforce_context" in params:
|
||||
self._import_export = ImportExportService(
|
||||
self.config, enforce_context=self.enforce_context
|
||||
)
|
||||
else:
|
||||
self._import_export = ImportExportService(self.config)
|
||||
return self._import_export
|
||||
|
||||
|
||||
@@ -2430,3 +2430,516 @@ def test_fallback_comma_separated_profiles_validation_parsing_resolution_and_pro
|
||||
assert seen_fb_msgs == [
|
||||
"Add fallback credentials (@profile, @p1,@p2, JSON list/dict, or compose:)"
|
||||
]
|
||||
|
||||
|
||||
def test_context_derive_parent_folder_golden_cases(tmp_path: Path) -> None:
|
||||
cfg = _make_config(tmp_path, {})
|
||||
svc = NodeService(cfg)
|
||||
|
||||
assert svc._derive_parent_folder(cfg, "@dish", is_folder=True) is None
|
||||
assert svc._derive_parent_folder(cfg, "@int@dish", is_folder=True) == "@dish"
|
||||
with pytest.raises(InvalidConfigurationError):
|
||||
svc._derive_parent_folder(cfg, "@a@b@c", is_folder=True)
|
||||
with pytest.raises(InvalidConfigurationError):
|
||||
svc._derive_parent_folder(cfg, "@", is_folder=True)
|
||||
with pytest.raises(InvalidConfigurationError):
|
||||
svc._derive_parent_folder(cfg, "@@dish", is_folder=True)
|
||||
|
||||
assert svc._derive_parent_folder(cfg, "router1", is_folder=False) is None
|
||||
assert svc._derive_parent_folder(cfg, "router1@dish", is_folder=False) == "@dish"
|
||||
assert svc._derive_parent_folder(cfg, "router1@int@dish", is_folder=False) == "@int@dish"
|
||||
with pytest.raises(InvalidConfigurationError):
|
||||
svc._derive_parent_folder(cfg, "@dish", is_folder=False)
|
||||
|
||||
|
||||
def test_context_enforcement_crud_move_bulk_and_jumphost_resolution(tmp_path: Path) -> None:
|
||||
from types import SimpleNamespace
|
||||
|
||||
import inquirer
|
||||
|
||||
from connpy.services.context_service import ContextService
|
||||
from connpy.services.exceptions import ConnpyError, NodeNotFoundError
|
||||
|
||||
cfg = _make_config(
|
||||
tmp_path,
|
||||
{
|
||||
"config": {
|
||||
"case": False,
|
||||
"current_context": "awsdish",
|
||||
"contexts": {
|
||||
"all": [".*"],
|
||||
"awsdish": [".*@aws@dish$"],
|
||||
"pedish": ["^pe-.*@dish$"],
|
||||
},
|
||||
},
|
||||
"connections": {
|
||||
"bastion": {
|
||||
"type": "connection",
|
||||
"host": "10.0.0.1",
|
||||
"protocol": "ssh",
|
||||
"port": "",
|
||||
"user": "b_u",
|
||||
"password": "",
|
||||
"options": "",
|
||||
"logs": "",
|
||||
"tags": "",
|
||||
"jumphost": "",
|
||||
},
|
||||
"dish": {
|
||||
"type": "folder",
|
||||
"jumpconnect": {
|
||||
"type": "connection",
|
||||
"host": "10.0.0.2",
|
||||
"protocol": "ssh",
|
||||
"port": "",
|
||||
"user": "j_u",
|
||||
"password": "",
|
||||
"options": "",
|
||||
"logs": "",
|
||||
"tags": "",
|
||||
"jumphost": "",
|
||||
},
|
||||
"pe-1": {
|
||||
"type": "connection",
|
||||
"host": "10.0.0.3",
|
||||
"protocol": "ssh",
|
||||
"port": "",
|
||||
"user": "u1",
|
||||
"password": "",
|
||||
"options": "",
|
||||
"logs": "",
|
||||
"tags": "",
|
||||
"jumphost": "",
|
||||
},
|
||||
"aws": {
|
||||
"type": "subfolder",
|
||||
"r1": {
|
||||
"type": "connection",
|
||||
"host": "10.1.0.1",
|
||||
"protocol": "ssh",
|
||||
"port": "",
|
||||
"user": "u_aws",
|
||||
"password": "",
|
||||
"options": "",
|
||||
"logs": "",
|
||||
"tags": "",
|
||||
"jumphost": "jumpconnect@dish",
|
||||
},
|
||||
},
|
||||
},
|
||||
"other": {
|
||||
"type": "folder",
|
||||
"r_out": {
|
||||
"type": "connection",
|
||||
"host": "10.2.0.1",
|
||||
"protocol": "ssh",
|
||||
"port": "",
|
||||
"user": "u_out",
|
||||
"password": "",
|
||||
"options": "",
|
||||
"logs": "",
|
||||
"tags": "",
|
||||
"jumphost": "",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
)
|
||||
provider = ServiceProvider(cfg)
|
||||
nodes_svc = provider.nodes
|
||||
|
||||
# list_nodes & list_folders filter by active context
|
||||
assert nodes_svc.list_nodes() == ["r1@aws@dish"]
|
||||
assert nodes_svc.list_folders() == ["@aws@dish"]
|
||||
|
||||
# Out-of-context CRUD is blocked
|
||||
with pytest.raises(NodeNotFoundError):
|
||||
nodes_svc.get_node_details("r_out@other")
|
||||
with pytest.raises(NodeNotFoundError):
|
||||
nodes_svc.update_node("r_out@other", {"host": "10.2.0.9"})
|
||||
with pytest.raises(NodeNotFoundError):
|
||||
nodes_svc.delete_node("r_out@other")
|
||||
with pytest.raises(NodeNotFoundError):
|
||||
nodes_svc.connect_node("r_out@other")
|
||||
with pytest.raises(InvalidConfigurationError):
|
||||
nodes_svc.add_node("root_bad", {"host": "1.1.1.1"})
|
||||
with pytest.raises(InvalidConfigurationError):
|
||||
nodes_svc.add_node("@badfolder", {}, is_folder=True)
|
||||
|
||||
# move_node blocks out-of-context src and dst
|
||||
with pytest.raises(NodeNotFoundError):
|
||||
nodes_svc.move_node("r_out@other", "r2@aws@dish")
|
||||
with pytest.raises(InvalidConfigurationError):
|
||||
nodes_svc.move_node("r1@aws@dish", "r2@other")
|
||||
|
||||
# bulk_add is atomic: if any node is outside context, zero nodes are inserted
|
||||
with pytest.raises(InvalidConfigurationError):
|
||||
nodes_svc.bulk_add(["ok@aws@dish", "bad_root"], ["10.1.0.2", "10.1.0.3"], {"protocol": "ssh"})
|
||||
assert "ok@aws@dish" not in cfg._getallnodes()
|
||||
|
||||
# Prefix context allows creating pe-new@dish inside existing @dish
|
||||
ctx_svc = ContextService(cfg)
|
||||
ctx_svc.set_active_context("pedish")
|
||||
nodes_svc.add_node(
|
||||
"pe-new@dish", {"host": "10.0.0.9", "jumphost": "bastion", "folder": "dish"}
|
||||
)
|
||||
assert "pe-new@dish" in nodes_svc.list_nodes()
|
||||
# Jumphost resolution via getitem remains unfiltered
|
||||
assert cfg.getitem("r1@aws@dish", extract=True)["jumphost"] == "jumpconnect@dish"
|
||||
|
||||
# Validators.jumphost_validation & profile_jumphost_validation allow global/parent bastions
|
||||
ctx_svc.set_active_context("awsdish")
|
||||
app_val = SimpleNamespace(
|
||||
case=False,
|
||||
profiles=list(cfg.profiles.keys()),
|
||||
nodes_list=nodes_svc.list_nodes(),
|
||||
folders=nodes_svc.list_folders(),
|
||||
services=provider,
|
||||
)
|
||||
vals = Validators(app_val)
|
||||
assert vals.jumphost_validation({}, "bastion") is True
|
||||
assert vals.jumphost_validation({}, "JUMPCONNECT@DISH") is True
|
||||
assert vals.profile_jumphost_validation({}, "bastion") is True
|
||||
with pytest.raises(inquirer.errors.ValidationError):
|
||||
vals.jumphost_validation({}, "does_not_exist")
|
||||
with pytest.raises(inquirer.errors.ValidationError):
|
||||
vals.profile_jumphost_validation({}, "does_not_exist")
|
||||
|
||||
# Validators.bulk_folder_validation checks active context & case
|
||||
assert vals.bulk_folder_validation({"ids": "r2"}, "@aws@dish") is True
|
||||
with pytest.raises(inquirer.errors.ValidationError):
|
||||
vals.bulk_folder_validation({"ids": "r2"}, "@other")
|
||||
with pytest.raises(ConnpyError):
|
||||
nodes_svc.validate_parent_folder("r2@other", is_folder=False)
|
||||
|
||||
|
||||
def test_context_import_export_roundtrip_and_atomicity(tmp_path: Path) -> None:
|
||||
cfg = _make_config(
|
||||
tmp_path,
|
||||
{
|
||||
"config": {
|
||||
"case": False,
|
||||
"current_context": "awsdish",
|
||||
"contexts": {
|
||||
"all": [".*"],
|
||||
"awsdish": [".*@aws@dish$"],
|
||||
"peonly": [".*pe.*"],
|
||||
},
|
||||
},
|
||||
"connections": {
|
||||
"dish": {
|
||||
"type": "folder",
|
||||
"aws": {
|
||||
"type": "subfolder",
|
||||
"r1": {
|
||||
"type": "connection",
|
||||
"host": "10.1.0.1",
|
||||
"protocol": "ssh",
|
||||
"port": "",
|
||||
"user": "u1",
|
||||
"password": "",
|
||||
"options": "",
|
||||
"logs": "",
|
||||
"tags": "",
|
||||
"jumphost": "",
|
||||
},
|
||||
},
|
||||
},
|
||||
"other": {
|
||||
"type": "folder",
|
||||
"x1": {
|
||||
"type": "connection",
|
||||
"host": "10.2.0.1",
|
||||
"protocol": "ssh",
|
||||
"port": "",
|
||||
"user": "u2",
|
||||
"password": "",
|
||||
"options": "",
|
||||
"logs": "",
|
||||
"tags": "",
|
||||
"jumphost": "",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
)
|
||||
provider = ServiceProvider(cfg)
|
||||
ie = provider.import_export
|
||||
|
||||
exported = ie.export_to_dict(folders=None)
|
||||
assert "dish" in exported
|
||||
assert "aws" in exported["dish"]
|
||||
assert "other" not in exported
|
||||
|
||||
# Round-trip export/import of @aws@dish succeeds when @dish exists
|
||||
rt = ie.export_to_dict(folders=["@aws@dish"])
|
||||
ie.import_from_dict(rt)
|
||||
|
||||
# Atomic rejection when container @office is new and outside context .*pe.*
|
||||
provider.context.set_active_context("peonly")
|
||||
with pytest.raises(InvalidConfigurationError):
|
||||
ie.import_from_dict(
|
||||
{
|
||||
"office": {
|
||||
"type": "folder",
|
||||
"pe1": {
|
||||
"type": "connection",
|
||||
"host": "10.3.0.1",
|
||||
"protocol": "ssh",
|
||||
},
|
||||
}
|
||||
}
|
||||
)
|
||||
assert "office" not in cfg.connections
|
||||
|
||||
# Atomic rejection when any leaf is outside context (no partial mutation)
|
||||
with pytest.raises(InvalidConfigurationError):
|
||||
ie.import_from_dict(
|
||||
{
|
||||
"pe-good": {"type": "connection", "host": "10.3.0.2", "protocol": "ssh"},
|
||||
"bad-node": {"type": "connection", "host": "10.3.0.3", "protocol": "ssh"},
|
||||
}
|
||||
)
|
||||
assert "pe-good" not in cfg.connections
|
||||
|
||||
|
||||
def test_context_execution_remote_chunking_mcp_and_grpc_server_bypass(
|
||||
tmp_path: Path, monkeypatch: pytest.MonkeyPatch
|
||||
) -> None:
|
||||
from connpy.grpc_layer import connpy_pb2
|
||||
from connpy.grpc_layer.server import (
|
||||
ExecutionServicer,
|
||||
ImportExportServicer,
|
||||
NodeServicer,
|
||||
serve,
|
||||
)
|
||||
from connpy.grpc_layer.stubs import ExecutionStub, NodeStub
|
||||
from connpy.grpc_layer.user_registry import UserRegistry
|
||||
from connpy.mcp_server import ConnpyMCPServer
|
||||
from connpy.services.exceptions import ConnpyError, NodeNotFoundError
|
||||
|
||||
cfg = _make_config(
|
||||
tmp_path,
|
||||
{
|
||||
"config": {
|
||||
"case": True,
|
||||
"current_context": "prod",
|
||||
"contexts": {
|
||||
"all": [".*"],
|
||||
"prod": ["^prod-.*"],
|
||||
},
|
||||
},
|
||||
"connections": {
|
||||
"prod-1": {
|
||||
"type": "connection",
|
||||
"host": "10.0.0.1",
|
||||
"protocol": "ssh",
|
||||
"port": "",
|
||||
"user": "u1",
|
||||
"password": "",
|
||||
"options": "",
|
||||
"logs": "",
|
||||
"tags": "",
|
||||
"jumphost": "",
|
||||
},
|
||||
"dev-1": {
|
||||
"type": "connection",
|
||||
"host": "10.0.0.2",
|
||||
"protocol": "ssh",
|
||||
"port": "",
|
||||
"user": "u2",
|
||||
"password": "",
|
||||
"options": "",
|
||||
"logs": "",
|
||||
"tags": "",
|
||||
"jumphost": "",
|
||||
},
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
# Local ExecutionService & run_cli_script enforce context
|
||||
exec_svc = ExecutionService(cfg)
|
||||
script_file = tmp_path / "cmds.txt"
|
||||
script_file.write_text("show version\n", encoding="utf-8")
|
||||
with pytest.raises(ConnpyError):
|
||||
exec_svc.run_commands("dev-1", ["show version"])
|
||||
with pytest.raises(ConnpyError):
|
||||
exec_svc.test_commands("dev-1", ["show version"], ["ok"])
|
||||
with pytest.raises(ConnpyError):
|
||||
exec_svc.run_cli_script("dev-1", str(script_file))
|
||||
|
||||
# Remote ExecutionService with injected remote_stub + chunking > MAX_REMOTE_FILTER_NODES (500)
|
||||
many_nodes = [f"prod-{i}" for i in range(505)] + ["dev-1"]
|
||||
seen_batches: list[list[str]] = []
|
||||
|
||||
class _InjectedRemoteStub:
|
||||
def list_nodes(self, filter_str=""):
|
||||
import re as _re
|
||||
|
||||
if not filter_str:
|
||||
return list(many_nodes)
|
||||
return [n for n in many_nodes if _re.search(filter_str, n)]
|
||||
|
||||
def run_commands(self, nodes_filter, **_kw):
|
||||
seen_batches.append(list(nodes_filter))
|
||||
return {pat.strip("^$").replace("\\", ""): {"output": "ok", "status": 0} for pat in nodes_filter}
|
||||
|
||||
def test_commands(self, nodes_filter, **_kw):
|
||||
seen_batches.append(list(nodes_filter))
|
||||
return {pat.strip("^$").replace("\\", ""): {"passed": True} for pat in nodes_filter}
|
||||
|
||||
remote_dir = tmp_path / "remote"
|
||||
remote_dir.mkdir()
|
||||
remote_cfg = configfile(conf=str(remote_dir / "config.yaml"), key=cfg.key)
|
||||
remote_cfg.transactional_update(
|
||||
lambda c: c.config.update(
|
||||
{
|
||||
"service_mode": "remote",
|
||||
"remote_host": "localhost:50051",
|
||||
"current_context": "prod",
|
||||
"contexts": {"all": [".*"], "prod": ["^prod-.*"]},
|
||||
}
|
||||
)
|
||||
)
|
||||
remote_exec = ExecutionService(remote_cfg, remote_stub=_InjectedRemoteStub())
|
||||
res_chunk = remote_exec.run_commands(".*", ["show ver"])
|
||||
assert len(seen_batches) == 2
|
||||
assert len(seen_batches[0]) == 500
|
||||
assert len(seen_batches[1]) == 5
|
||||
assert len(res_chunk) == 505
|
||||
assert "dev-1" not in res_chunk
|
||||
with pytest.raises(ConnpyError):
|
||||
remote_exec.run_cli_script("dev-1", str(script_file))
|
||||
|
||||
# NodeStub & ExecutionStub client-side guards + _get_raw_nodes
|
||||
fake_grpc_stub = MagicMock()
|
||||
fake_grpc_stub.list_nodes.return_value = MagicMock(data=None)
|
||||
with patch("connpy.grpc_layer.stubs.from_value", return_value=["prod-1", "dev-1"]):
|
||||
n_stub = NodeStub(MagicMock(), "localhost:50051", config=remote_cfg)
|
||||
n_stub.stub = fake_grpc_stub
|
||||
assert n_stub._get_raw_nodes() == ["prod-1", "dev-1"]
|
||||
with pytest.raises(NodeNotFoundError):
|
||||
n_stub.get_node_details("dev-1")
|
||||
with pytest.raises(InvalidConfigurationError):
|
||||
n_stub.add_node("dev-2", {"host": "1.2.3.4"})
|
||||
|
||||
e_stub = ExecutionStub(MagicMock(), "localhost:50051", config=remote_cfg, node_stub=n_stub)
|
||||
e_stub.stub = MagicMock()
|
||||
e_stub.stub.run_commands.return_value = [
|
||||
connpy_pb2.NodeRunResult(unique_id="prod-1", output="ok", status=0)
|
||||
]
|
||||
n_stub.list_nodes = MagicMock(return_value=["prod-1", "dev-1"])
|
||||
out_stub = e_stub.run_commands(".*", ["show ver"])
|
||||
assert list(out_stub.keys()) == ["prod-1"]
|
||||
|
||||
# ConnpyMCPServer inherits enforcement via ServiceProvider
|
||||
mcp = ConnpyMCPServer(config=cfg)
|
||||
assert mcp.list_nodes() == ["prod-1"]
|
||||
mcp_res = mcp.run_commands("dev-1", ["show ver"])
|
||||
assert mcp_res["success"] is False
|
||||
|
||||
# gRPC Server servicers & UserRegistry force enforce_context=False even when sys.argv != ['api']
|
||||
monkeypatch.setattr(sys, "argv", ["pytest"])
|
||||
cfg.fzf_cachefile = str(tmp_path / ".fzf_nodes_cache.txt")
|
||||
cfg.folders_cachefile = str(tmp_path / ".folders_cache.txt")
|
||||
Path(cfg.fzf_cachefile).write_text("client-cache-preserved", encoding="utf-8")
|
||||
|
||||
srv_provider = ServiceProvider(cfg, mode="local", enforce_context=True)
|
||||
node_servicer = NodeServicer(srv_provider)
|
||||
assert srv_provider.enforce_context is False
|
||||
assert cfg._enforce_context is False
|
||||
assert "dev-1" in node_servicer.service.list_nodes()
|
||||
cfg._saveconfig(cfg.file)
|
||||
assert Path(cfg.fzf_cachefile).read_text(encoding="utf-8") == "client-cache-preserved"
|
||||
|
||||
_ = ExecutionServicer(ServiceProvider(cfg, mode="local", enforce_context=True))
|
||||
_ = ImportExportServicer(ServiceProvider(cfg, mode="local", enforce_context=True))
|
||||
|
||||
reg = UserRegistry(str(tmp_path))
|
||||
reg.user_service.create_user("alice", "SecretPass123!")
|
||||
alice_prov = reg.get_provider("alice")
|
||||
assert alice_prov.enforce_context is False
|
||||
assert alice_prov.config._enforce_context is False
|
||||
|
||||
# serve() sets enforce_context=False on fallback_provider
|
||||
mock_grpc_srv = MagicMock()
|
||||
with patch("grpc.server", return_value=mock_grpc_srv):
|
||||
srv_dir = tmp_path / "srv"
|
||||
srv_dir.mkdir()
|
||||
serve_cfg = configfile(conf=str(srv_dir / "config.yaml"), key=cfg.key)
|
||||
serve(serve_cfg, port=0)
|
||||
assert serve_cfg._enforce_context is False
|
||||
|
||||
|
||||
def test_context_regex_parity_case_cache_and_dirty_recovery(tmp_path: Path) -> None:
|
||||
from connpy.services.context_service import ContextService
|
||||
|
||||
cfg = _make_config(
|
||||
tmp_path,
|
||||
{
|
||||
"config": {
|
||||
"case": False,
|
||||
"current_context": "upper",
|
||||
"contexts": {
|
||||
"all": [".*"],
|
||||
"upper": [".*PROD.*"],
|
||||
},
|
||||
},
|
||||
"connections": {
|
||||
"prod-1": {
|
||||
"type": "connection",
|
||||
"host": "10.0.0.1",
|
||||
"protocol": "ssh",
|
||||
"port": "",
|
||||
"user": "u1",
|
||||
"password": "",
|
||||
"options": "",
|
||||
"logs": "",
|
||||
"tags": "",
|
||||
"jumphost": "",
|
||||
},
|
||||
"dev-1": {
|
||||
"type": "connection",
|
||||
"host": "10.0.0.2",
|
||||
"protocol": "ssh",
|
||||
"port": "",
|
||||
"user": "u2",
|
||||
"password": "",
|
||||
"options": "",
|
||||
"logs": "",
|
||||
"tags": "",
|
||||
"jumphost": "",
|
||||
},
|
||||
"ProdFolder": {"type": "folder"},
|
||||
},
|
||||
},
|
||||
)
|
||||
ctx_svc = ContextService(cfg)
|
||||
|
||||
# Validation limits in add_context / update_context
|
||||
with pytest.raises(ValueError):
|
||||
ctx_svc.add_context("bad", ["[unclosed"])
|
||||
with pytest.raises(ValueError):
|
||||
ctx_svc.add_context("empty", [])
|
||||
with pytest.raises(ValueError):
|
||||
ctx_svc.update_context("all", [".*"])
|
||||
|
||||
# case=False parity between ContextService and configfile cache filter
|
||||
assert ctx_svc.matches_active_context("prod-1") is True
|
||||
cfg._generate_nodes_cache(nodes=["prod-1", "dev-1"], folders=["@ProdFolder", "@Other"])
|
||||
assert Path(cfg.fzf_cachefile).read_text(encoding="utf-8").splitlines() == ["prod-1"]
|
||||
assert Path(cfg.folders_cachefile).read_text(encoding="utf-8").splitlines() == ["@ProdFolder"]
|
||||
|
||||
# case=True parity
|
||||
cfg.config["case"] = True
|
||||
assert ctx_svc.matches_active_context("prod-1") is False
|
||||
cfg._generate_nodes_cache(nodes=["prod-1", "PROD-2"], folders=["@ProdFolder"])
|
||||
assert Path(cfg.fzf_cachefile).read_text(encoding="utf-8").splitlines() == ["PROD-2"]
|
||||
|
||||
# Corrupt regex in config marks _cache_dirty=True safely without raising
|
||||
cfg.config["contexts"]["broken"] = ["[invalid"]
|
||||
cfg.config["current_context"] = "broken"
|
||||
cfg._cache_dirty = False
|
||||
cfg._generate_nodes_cache()
|
||||
assert cfg._cache_dirty is True
|
||||
Reference in new issue
Block a user