Compare commits
7
Commits
2da9b45548
...
1caa286e65
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1caa286e65 | ||
|
|
388d042eba | ||
|
|
91c6732719 | ||
|
|
a2ca91dfc9 | ||
|
|
2721e39e78 | ||
|
|
494a7dffe6 | ||
|
|
a90e8bf597 |
No files matched your search
@@ -3,7 +3,7 @@
|
|||||||
</p>
|
</p>
|
||||||
|
|
||||||
|
|
||||||
# Connpy (v6.4.0b2)
|
# Connpy (v6.4.0b3)
|
||||||
[](https://pypi.org/pypi/connpy/)
|
[](https://pypi.org/pypi/connpy/)
|
||||||
[](https://pypi.org/pypi/connpy/)
|
[](https://pypi.org/pypi/connpy/)
|
||||||
[](https://pypi.org/pypi/connpy/)
|
[](https://pypi.org/pypi/connpy/)
|
||||||
@@ -40,10 +40,18 @@ Start a standalone persistent session with the AI Copilot. Manage sessions using
|
|||||||
conn ai "how do i check bgp summary on cisco?"
|
conn ai "how do i check bgp summary on cisco?"
|
||||||
```
|
```
|
||||||
|
|
||||||
### 1c. MCP Integration
|
### 1c. MCP Integration (Client & Server)
|
||||||
Connect to external data sources and tools dynamically via the Model Context Protocol (MCP). Use the interactive wizard or command actions to configure MCP servers:
|
Connect to external data sources and tools dynamically, or expose `connpy` as a native MCP server for external agents (Claude Desktop, Cursor, IDEs):
|
||||||
```bash
|
```bash
|
||||||
conn ai --mcp
|
# MCP Client: Manage connected MCP servers
|
||||||
|
conn ai --mcp # Interactive configuration wizard
|
||||||
|
conn ai --mcp list # List configured MCP servers
|
||||||
|
conn ai --mcp add <name> <url> [os] # Add a remote SSE MCP server
|
||||||
|
|
||||||
|
# MCP Server: Serve connpy inventory, diagnostic tools, and playbooks
|
||||||
|
conn ai --mcp serve # Standard I/O mode (default, for local clients)
|
||||||
|
conn ai --mcp serve --sse --port 8080 # Remote SSE mode
|
||||||
|
conn ai --mcp serve --trust # Enable inventory mutations (CRUD) & configuration commands
|
||||||
```
|
```
|
||||||
|
|
||||||
### 1d. Local Interactive Shell (conn shell)
|
### 1d. Local Interactive Shell (conn shell)
|
||||||
|
|||||||
+12
-4
@@ -5,7 +5,7 @@
|
|||||||
</p>
|
</p>
|
||||||
|
|
||||||
|
|
||||||
# Connpy (v6.4.0b2)
|
# Connpy (v6.4.0b3)
|
||||||
[](https://pypi.org/pypi/connpy/)
|
[](https://pypi.org/pypi/connpy/)
|
||||||
[](https://pypi.org/pypi/connpy/)
|
[](https://pypi.org/pypi/connpy/)
|
||||||
[](https://pypi.org/pypi/connpy/)
|
[](https://pypi.org/pypi/connpy/)
|
||||||
@@ -42,10 +42,18 @@ Start a standalone persistent session with the AI Copilot. Manage sessions using
|
|||||||
conn ai "how do i check bgp summary on cisco?"
|
conn ai "how do i check bgp summary on cisco?"
|
||||||
```
|
```
|
||||||
|
|
||||||
### 1c. MCP Integration
|
### 1c. MCP Integration (Client & Server)
|
||||||
Connect to external data sources and tools dynamically via the Model Context Protocol (MCP). Use the interactive wizard or command actions to configure MCP servers:
|
Connect to external data sources and tools dynamically, or expose `connpy` as a native MCP server for external agents (Claude Desktop, Cursor, IDEs):
|
||||||
```bash
|
```bash
|
||||||
conn ai --mcp
|
# MCP Client: Manage connected MCP servers
|
||||||
|
conn ai --mcp # Interactive configuration wizard
|
||||||
|
conn ai --mcp list # List configured MCP servers
|
||||||
|
conn ai --mcp add <name> <url> [os] # Add a remote SSE MCP server
|
||||||
|
|
||||||
|
# MCP Server: Serve connpy inventory, diagnostic tools, and playbooks
|
||||||
|
conn ai --mcp serve # Standard I/O mode (default, for local clients)
|
||||||
|
conn ai --mcp serve --sse --port 8080 # Remote SSE mode
|
||||||
|
conn ai --mcp serve --trust # Enable inventory mutations (CRUD) & configuration commands
|
||||||
```
|
```
|
||||||
|
|
||||||
### 1d. Local Interactive Shell (conn shell)
|
### 1d. Local Interactive Shell (conn shell)
|
||||||
|
|||||||
+1
-1
@@ -1 +1 @@
|
|||||||
__version__ = "6.4.0b2"
|
__version__ = "6.4.0b3"
|
||||||
+16
-5
@@ -1,3 +1,5 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
# ruff: noqa
|
||||||
import os
|
import os
|
||||||
import secrets
|
import secrets
|
||||||
|
|
||||||
@@ -197,6 +199,9 @@ class ai:
|
|||||||
self.session_path = os.path.join(self.sessions_dir, f"{self.session_id}.json") if self.session_id else None
|
self.session_path = os.path.join(self.sessions_dir, f"{self.session_id}.json") if self.session_id else None
|
||||||
|
|
||||||
# Agnostic base prompts
|
# Agnostic base prompts
|
||||||
|
self._update_base_prompts()
|
||||||
|
|
||||||
|
def _update_base_prompts(self):
|
||||||
architect_instructions = ""
|
architect_instructions = ""
|
||||||
if self.has_architect:
|
if self.has_architect:
|
||||||
architect_instructions = """
|
architect_instructions = """
|
||||||
@@ -237,7 +242,7 @@ class ai:
|
|||||||
- NO WANDERING: Do not speculate. If stuck, report attempts.
|
- NO WANDERING: Do not speculate. If stuck, report attempts.
|
||||||
- SAFETY: When you use 'run_commands' with configuration commands, the system automatically prompts the user for confirmation. Just execute - don't ask permission first.
|
- SAFETY: When you use 'run_commands' with configuration commands, the system automatically prompts the user for confirmation. Just execute - don't ask permission first.
|
||||||
{architect_instructions}
|
{architect_instructions}
|
||||||
Network Context: {{self.long_term_memory if self.long_term_memory else "Empty."}}
|
Network Context: {self.long_term_memory if self.long_term_memory else "Empty."}
|
||||||
""").strip()
|
""").strip()
|
||||||
|
|
||||||
self._architect_base_prompt = dedent(f"""
|
self._architect_base_prompt = dedent(f"""
|
||||||
@@ -277,6 +282,7 @@ class ai:
|
|||||||
@property
|
@property
|
||||||
def engineer_system_prompt(self):
|
def engineer_system_prompt(self):
|
||||||
"""Build engineer system prompt with plugin extensions."""
|
"""Build engineer system prompt with plugin extensions."""
|
||||||
|
self._update_base_prompts()
|
||||||
if self.engineer_prompt_extensions:
|
if self.engineer_prompt_extensions:
|
||||||
extensions = "\n".join(self.engineer_prompt_extensions)
|
extensions = "\n".join(self.engineer_prompt_extensions)
|
||||||
return self._engineer_base_prompt + f"\n\nPlugin Capabilities:\n{extensions}"
|
return self._engineer_base_prompt + f"\n\nPlugin Capabilities:\n{extensions}"
|
||||||
@@ -285,6 +291,7 @@ class ai:
|
|||||||
@property
|
@property
|
||||||
def architect_system_prompt(self):
|
def architect_system_prompt(self):
|
||||||
"""Build architect system prompt with plugin extensions."""
|
"""Build architect system prompt with plugin extensions."""
|
||||||
|
self._update_base_prompts()
|
||||||
prompt = self._architect_base_prompt
|
prompt = self._architect_base_prompt
|
||||||
if getattr(self, "one_shot", False):
|
if getattr(self, "one_shot", False):
|
||||||
prompt += "\n\nCRITICAL 1-SHOT DIAGNOSTICS DIRECTIVE:\nYou are running in a 1-shot offline diagnostics mode. There is no active conversation loop, and you are NOT conversing with a Network Engineer. You MUST deliver your complete strategic analysis immediately and directly to the user. Do not suggest or attempt to delegate/return control to the engineer."
|
prompt += "\n\nCRITICAL 1-SHOT DIAGNOSTICS DIRECTIVE:\nYou are running in a 1-shot offline diagnostics mode. There is no active conversation loop, and you are NOT conversing with a Network Engineer. You MUST deliver your complete strategic analysis immediately and directly to the user. Do not suggest or attempt to delegate/return control to the engineer."
|
||||||
@@ -624,6 +631,7 @@ class ai:
|
|||||||
# Reload memory after update
|
# Reload memory after update
|
||||||
with open(self.memory_path, "r") as f:
|
with open(self.memory_path, "r") as f:
|
||||||
self.long_term_memory = f.read()
|
self.long_term_memory = f.read()
|
||||||
|
self._update_base_prompts()
|
||||||
|
|
||||||
return "Memory updated successfully."
|
return "Memory updated successfully."
|
||||||
except PermissionError as e:
|
except PermissionError as e:
|
||||||
@@ -859,7 +867,7 @@ class ai:
|
|||||||
# Silently fail for LLM tools
|
# Silently fail for LLM tools
|
||||||
pass
|
pass
|
||||||
|
|
||||||
if self.architect_key:
|
if self.has_architect:
|
||||||
base_tools.extend([
|
base_tools.extend([
|
||||||
{"type": "function", "function": {"name": "consult_architect", "description": "Ask the Strategic Reasoning Engine for advice on complex design, architecture, or troubleshooting decisions. You remain in control and will present the response to the user. Use this for: configuration planning, design validation, complex troubleshooting.", "parameters": {"type": "object", "properties": {"question": {"type": "string", "description": "Strategic question or decision needed."}, "technical_summary": {"type": "string", "description": "Technical findings and context gathered so far."}}, "required": ["question", "technical_summary"]}}},
|
{"type": "function", "function": {"name": "consult_architect", "description": "Ask the Strategic Reasoning Engine for advice on complex design, architecture, or troubleshooting decisions. You remain in control and will present the response to the user. Use this for: configuration planning, design validation, complex troubleshooting.", "parameters": {"type": "object", "properties": {"question": {"type": "string", "description": "Strategic question or decision needed."}, "technical_summary": {"type": "string", "description": "Technical findings and context gathered so far."}}, "required": ["question", "technical_summary"]}}},
|
||||||
{"type": "function", "function": {"name": "escalate_to_architect", "description": "Transfer full control to the Strategic Reasoning Engine. Use ONLY when the user explicitly requests the Architect or when the problem requires strategic oversight beyond consultation. After escalation, the Architect takes over the conversation.", "parameters": {"type": "object", "properties": {"reason": {"type": "string", "description": "Why you're escalating (e.g. 'User requested Architect', 'Complex multi-site design needed')."}, "context": {"type": "string", "description": "Full context and findings to hand over."}}, "required": ["reason", "context"]}}}
|
{"type": "function", "function": {"name": "escalate_to_architect", "description": "Transfer full control to the Strategic Reasoning Engine. Use ONLY when the user explicitly requests the Architect or when the problem requires strategic oversight beyond consultation. After escalation, the Architect takes over the conversation.", "parameters": {"type": "object", "properties": {"reason": {"type": "string", "description": "Why you're escalating (e.g. 'User requested Architect', 'Complex multi-site design needed')."}, "context": {"type": "string", "description": "Full context and findings to hand over."}}, "required": ["reason", "context"]}}}
|
||||||
@@ -1216,14 +1224,17 @@ class ai:
|
|||||||
if status: update_status("[architect]Engineer consulting Architect...")
|
if status: update_status("[architect]Engineer consulting Architect...")
|
||||||
try:
|
try:
|
||||||
# Consultation only - Engineer stays in control
|
# Consultation only - Engineer stays in control
|
||||||
|
auth_kwargs = dict(self.architect_auth or {})
|
||||||
|
if self.architect_key and "api_key" not in auth_kwargs:
|
||||||
|
auth_kwargs["api_key"] = self.architect_key
|
||||||
claude_resp = completion(
|
claude_resp = completion(
|
||||||
model=self.architect_model,
|
model=self.architect_model,
|
||||||
messages=[
|
messages=[
|
||||||
{"role": "system", "content": self.architect_system_prompt},
|
{"role": "system", "content": self.architect_system_prompt},
|
||||||
{"role": "user", "content": f"The Engineer needs your strategic advice.\n\nTECHNICAL SUMMARY: {args['technical_summary']}\n\nQUESTION: {args['question']}\n\nProvide strategic guidance. The Engineer will continue handling the user."}
|
{"role": "user", "content": f"The Engineer needs your strategic advice.\n\nTECHNICAL SUMMARY: {args['technical_summary']}\n\nQUESTION: {args['question']}\n\nProvide strategic guidance. The Engineer will continue handling the user."}
|
||||||
],
|
],
|
||||||
api_key=self.architect_key,
|
num_retries=3,
|
||||||
num_retries=3
|
**auth_kwargs
|
||||||
)
|
)
|
||||||
obs = claude_resp.choices[0].message.content
|
obs = claude_resp.choices[0].message.content
|
||||||
if debug:
|
if debug:
|
||||||
@@ -1790,7 +1801,7 @@ class PlaybookBuilderAgent:
|
|||||||
import yaml
|
import yaml
|
||||||
try:
|
try:
|
||||||
# 1. Parse YAML
|
# 1. Parse YAML
|
||||||
data = yaml.load(playbook_yaml, Loader=yaml.FullLoader)
|
data = yaml.safe_load(playbook_yaml)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return {"valid": False, "error": f"YAML Syntax Error: {e}"}
|
return {"valid": False, "error": f"YAML Syntax Error: {e}"}
|
||||||
|
|
||||||
|
|||||||
+54
-11
@@ -1,7 +1,8 @@
|
|||||||
import sys
|
import sys
|
||||||
|
|
||||||
from rich.panel import Panel
|
from rich.panel import Panel
|
||||||
from rich.rule import Rule
|
|
||||||
from rich.prompt import Prompt
|
from rich.prompt import Prompt
|
||||||
|
from rich.rule import Rule
|
||||||
|
|
||||||
from .. import printer
|
from .. import printer
|
||||||
|
|
||||||
@@ -36,7 +37,7 @@ class AIHandler:
|
|||||||
try:
|
try:
|
||||||
self.app.services.ai.delete_session(args.delete_session[0])
|
self.app.services.ai.delete_session(args.delete_session[0])
|
||||||
printer.success(f"Session {args.delete_session[0]} deleted.")
|
printer.success(f"Session {args.delete_session[0]} deleted.")
|
||||||
except Exception as e:
|
except Exception as e: # noqa: BLE001
|
||||||
printer.error(str(e))
|
printer.error(str(e))
|
||||||
return
|
return
|
||||||
|
|
||||||
@@ -189,7 +190,7 @@ class AIHandler:
|
|||||||
try:
|
try:
|
||||||
self.app.services.ai.configure_mcp(name, url=url, auto_load_on_os=os_filter)
|
self.app.services.ai.configure_mcp(name, url=url, auto_load_on_os=os_filter)
|
||||||
printer.success(f"MCP server '{name}' added/updated.")
|
printer.success(f"MCP server '{name}' added/updated.")
|
||||||
except Exception as e:
|
except Exception as e: # noqa: BLE001
|
||||||
printer.error(str(e))
|
printer.error(str(e))
|
||||||
return
|
return
|
||||||
|
|
||||||
@@ -201,7 +202,7 @@ class AIHandler:
|
|||||||
try:
|
try:
|
||||||
self.app.services.ai.configure_mcp(name, remove=True)
|
self.app.services.ai.configure_mcp(name, remove=True)
|
||||||
printer.success(f"MCP server '{name}' removed.")
|
printer.success(f"MCP server '{name}' removed.")
|
||||||
except Exception as e:
|
except Exception as e: # noqa: BLE001
|
||||||
printer.error(str(e))
|
printer.error(str(e))
|
||||||
return
|
return
|
||||||
|
|
||||||
@@ -214,13 +215,54 @@ class AIHandler:
|
|||||||
try:
|
try:
|
||||||
self.app.services.ai.configure_mcp(name, enabled=enabled)
|
self.app.services.ai.configure_mcp(name, enabled=enabled)
|
||||||
printer.success(f"MCP server '{name}' {'enabled' if enabled else 'disabled'}.")
|
printer.success(f"MCP server '{name}' {'enabled' if enabled else 'disabled'}.")
|
||||||
except Exception as e:
|
except Exception as e: # noqa: BLE001
|
||||||
printer.error(str(e))
|
printer.error(str(e))
|
||||||
return
|
return
|
||||||
|
|
||||||
|
elif action == "serve":
|
||||||
|
transport = "stdio"
|
||||||
|
host = "127.0.0.1"
|
||||||
|
port = 8000
|
||||||
|
trust = getattr(args, "trust", False)
|
||||||
|
|
||||||
|
idx = 1
|
||||||
|
while idx < len(mcp_args):
|
||||||
|
tok = mcp_args[idx]
|
||||||
|
if tok in ["--sse", "sse"]:
|
||||||
|
transport = "sse"
|
||||||
|
elif tok in ["--stdio", "stdio"]:
|
||||||
|
transport = "stdio"
|
||||||
|
elif tok in ["--trust", "trust"]:
|
||||||
|
trust = True
|
||||||
|
elif tok in ["--port", "-p"]:
|
||||||
|
idx += 1
|
||||||
|
if idx < len(mcp_args):
|
||||||
|
try:
|
||||||
|
port = int(mcp_args[idx])
|
||||||
|
except ValueError:
|
||||||
|
printer.error(f"Invalid port: {mcp_args[idx]}")
|
||||||
|
return
|
||||||
|
elif tok.startswith("--port="):
|
||||||
|
try:
|
||||||
|
port = int(tok.split("=", 1)[1])
|
||||||
|
except ValueError:
|
||||||
|
printer.error(f"Invalid port: {tok}")
|
||||||
|
return
|
||||||
|
elif tok in ["--host", "-h"]:
|
||||||
|
idx += 1
|
||||||
|
if idx < len(mcp_args):
|
||||||
|
host = mcp_args[idx]
|
||||||
|
elif tok.startswith("--host="):
|
||||||
|
host = tok.split("=", 1)[1]
|
||||||
|
idx += 1
|
||||||
|
|
||||||
|
from ..mcp_server import ConnpyMCPServer
|
||||||
|
server = ConnpyMCPServer(config=self.app.config, services=self.app.services, trust=trust)
|
||||||
|
return server.run(transport=transport, host=host, port=port)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
printer.error(f"Unknown MCP action: {action}")
|
printer.error(f"Unknown MCP action: {action}")
|
||||||
printer.info("Available actions: list, add, remove, enable, disable")
|
printer.info("Available actions: list, add, remove, enable, disable, serve")
|
||||||
return
|
return
|
||||||
|
|
||||||
# 2. Interactive Wizard Mode (if no arguments provided)
|
# 2. Interactive Wizard Mode (if no arguments provided)
|
||||||
@@ -262,15 +304,16 @@ class AIHandler:
|
|||||||
self.app.services.ai.configure_mcp(result["name"], remove=True)
|
self.app.services.ai.configure_mcp(result["name"], remove=True)
|
||||||
printer.success(f"MCP server '{result['name']}' removed.")
|
printer.success(f"MCP server '{result['name']}' removed.")
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e: # noqa: BLE001
|
||||||
printer.error(str(e))
|
printer.error(str(e))
|
||||||
|
|
||||||
def _parse_auth_value(self, value):
|
def _parse_auth_value(self, value):
|
||||||
if not value or value.lower() in ["none", "clear"]:
|
if not value or value.lower() in ["none", "clear"]:
|
||||||
return None
|
return None
|
||||||
import os
|
|
||||||
import yaml
|
|
||||||
import json
|
import json
|
||||||
|
import os
|
||||||
|
|
||||||
|
import yaml
|
||||||
if os.path.exists(value):
|
if os.path.exists(value):
|
||||||
try:
|
try:
|
||||||
with open(value, "r") as f:
|
with open(value, "r") as f:
|
||||||
@@ -279,7 +322,7 @@ class AIHandler:
|
|||||||
return json.loads(content)
|
return json.loads(content)
|
||||||
except ValueError:
|
except ValueError:
|
||||||
return yaml.safe_load(content)
|
return yaml.safe_load(content)
|
||||||
except Exception as e:
|
except Exception as e: # noqa: BLE001
|
||||||
printer.error(f"Failed to read/parse auth file '{value}': {e}")
|
printer.error(f"Failed to read/parse auth file '{value}': {e}")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
@@ -291,6 +334,6 @@ class AIHandler:
|
|||||||
if isinstance(parsed, dict):
|
if isinstance(parsed, dict):
|
||||||
return parsed
|
return parsed
|
||||||
raise ValueError()
|
raise ValueError()
|
||||||
except Exception:
|
except Exception: # noqa: BLE001
|
||||||
printer.error("Auth parameter must be a valid JSON/YAML string, or a path to a JSON/YAML file.")
|
printer.error("Auth parameter must be a valid JSON/YAML string, or a path to a JSON/YAML file.")
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
@@ -162,7 +162,10 @@ class RunHandler:
|
|||||||
path = args.data[0]
|
path = args.data[0]
|
||||||
try:
|
try:
|
||||||
with open(path, "r") as f:
|
with open(path, "r") as f:
|
||||||
playbook = yaml.load(f, Loader=yaml.FullLoader)
|
playbook = yaml.safe_load(f)
|
||||||
|
|
||||||
|
if not isinstance(playbook, dict):
|
||||||
|
raise TypeError("Invalid playbook format: expected a dictionary")
|
||||||
|
|
||||||
# Check preflight first before any task runs
|
# Check preflight first before any task runs
|
||||||
if getattr(args, "preflight_ai", False):
|
if getattr(args, "preflight_ai", False):
|
||||||
|
|||||||
+29
-17
@@ -1,5 +1,7 @@
|
|||||||
import sys
|
#!/usr/bin/env python3
|
||||||
import os
|
import os
|
||||||
|
import sys
|
||||||
|
|
||||||
|
|
||||||
def load_txt_cache(filepath):
|
def load_txt_cache(filepath):
|
||||||
try:
|
try:
|
||||||
@@ -27,7 +29,7 @@ def get_cwd(words, option=None, folderonly=False):
|
|||||||
for i in range(len(pathstrings)):
|
for i in range(len(pathstrings)):
|
||||||
if os.path.isdir(pathstrings[i]):
|
if os.path.isdir(pathstrings[i]):
|
||||||
pathstrings[i] += '/'
|
pathstrings[i] += '/'
|
||||||
pathstrings = [s[2:] if s.startswith('./') else s for s in pathstrings]
|
pathstrings = [s.removeprefix('./') for s in pathstrings]
|
||||||
if folderonly:
|
if folderonly:
|
||||||
pathstrings = [s for s in pathstrings if os.path.isdir(s)]
|
pathstrings = [s for s in pathstrings if os.path.isdir(s)]
|
||||||
return pathstrings
|
return pathstrings
|
||||||
@@ -43,7 +45,7 @@ def _get_plugins(which, defaultdir):
|
|||||||
try:
|
try:
|
||||||
with open(pref_path) as f:
|
with open(pref_path) as f:
|
||||||
preferences = json.load(f)
|
preferences = json.load(f)
|
||||||
except Exception:
|
except Exception: # noqa: BLE001
|
||||||
preferences = {}
|
preferences = {}
|
||||||
|
|
||||||
# Load service mode
|
# Load service mode
|
||||||
@@ -71,8 +73,8 @@ def _get_plugins(which, defaultdir):
|
|||||||
|
|
||||||
# Get plugins from all directories
|
# Get plugins from all directories
|
||||||
user_enabled, user_disabled, user_all_plugins = get_plugins_from_directory(defaultdir + "/plugins")
|
user_enabled, user_disabled, user_all_plugins = get_plugins_from_directory(defaultdir + "/plugins")
|
||||||
core_enabled, core_disabled, core_all_plugins = get_plugins_from_directory(core_path)
|
_core_enabled, _core_disabled, core_all_plugins = get_plugins_from_directory(core_path)
|
||||||
remote_enabled, remote_disabled, remote_all_plugins = get_plugins_from_directory(remote_path)
|
_remote_enabled, _remote_disabled, remote_all_plugins = get_plugins_from_directory(remote_path)
|
||||||
|
|
||||||
# Calculate final paths respecting priorities and preferences
|
# Calculate final paths respecting priorities and preferences
|
||||||
# Priority: User Local > Core Local > Remote (unless preferred)
|
# Priority: User Local > Core Local > Remote (unless preferred)
|
||||||
@@ -115,7 +117,7 @@ def _get_users(configdir):
|
|||||||
data = yaml.safe_load(f) or {}
|
data = yaml.safe_load(f) or {}
|
||||||
if isinstance(data, dict) and "users" in data:
|
if isinstance(data, dict) and "users" in data:
|
||||||
return list(data["users"].keys())
|
return list(data["users"].keys())
|
||||||
except Exception:
|
except Exception: # noqa: BLE001, S110
|
||||||
pass
|
pass
|
||||||
return []
|
return []
|
||||||
|
|
||||||
@@ -135,7 +137,7 @@ def _get_sso_providers(configdir):
|
|||||||
providers = sso.get("providers", {})
|
providers = sso.get("providers", {})
|
||||||
if isinstance(providers, dict):
|
if isinstance(providers, dict):
|
||||||
return list(providers.keys())
|
return list(providers.keys())
|
||||||
except Exception:
|
except Exception: # noqa: BLE001, S110
|
||||||
pass
|
pass
|
||||||
return []
|
return []
|
||||||
|
|
||||||
@@ -210,12 +212,22 @@ def _build_tree(nodes, folders, profiles, plugins, configdir):
|
|||||||
}
|
}
|
||||||
|
|
||||||
# State Machine Definitions
|
# State Machine Definitions
|
||||||
|
serve_dict = {"__exclude_used__": True}
|
||||||
|
serve_dict.update({
|
||||||
|
"--sse": serve_dict,
|
||||||
|
"--port": {"*": serve_dict},
|
||||||
|
"--trust": serve_dict,
|
||||||
|
"--help": None,
|
||||||
|
"-h": None
|
||||||
|
})
|
||||||
|
|
||||||
mcp_dict = {
|
mcp_dict = {
|
||||||
"list": None,
|
"list": None,
|
||||||
"add": {"*": {"*": {"*": None}}}, # name url [os]
|
"add": {"*": {"*": {"*": None}}}, # name url [os]
|
||||||
"remove": {"*": None},
|
"remove": {"*": None},
|
||||||
"enable": {"*": None},
|
"enable": {"*": None},
|
||||||
"disable": {"*": None},
|
"disable": {"*": None},
|
||||||
|
"serve": serve_dict,
|
||||||
"--help": None, "-h": None
|
"--help": None, "-h": None
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -291,7 +303,7 @@ def _build_tree(nodes, folders, profiles, plugins, configdir):
|
|||||||
# Long flags are offered; short forms (-d/-t) only used for navigation.
|
# Long flags are offered; short forms (-d/-t) only used for navigation.
|
||||||
# Two states: before node (offer nodes + remaining long flags)
|
# Two states: before node (offer nodes + remaining long flags)
|
||||||
# after node (offer only remaining long flags, no more nodes)
|
# after node (offer only remaining long flags, no more nodes)
|
||||||
connect_flags_long = ["--debug", "--sftp"]
|
_connect_flags_long = ["--debug", "--sftp"]
|
||||||
connect_flags_all = ["--debug", "-d", "--sftp", "-t"]
|
connect_flags_all = ["--debug", "-d", "--sftp", "-t"]
|
||||||
|
|
||||||
# Post-node: only offer remaining long flags
|
# Post-node: only offer remaining long flags
|
||||||
@@ -443,7 +455,7 @@ def main():
|
|||||||
try:
|
try:
|
||||||
with open(pathfile, "r") as f:
|
with open(pathfile, "r") as f:
|
||||||
configdir = f.read().strip()
|
configdir = f.read().strip()
|
||||||
except (FileNotFoundError, IOError):
|
except (OSError, FileNotFoundError):
|
||||||
configdir = defaultdir
|
configdir = defaultdir
|
||||||
cachefile = configdir + '/.config.cache.json'
|
cachefile = configdir + '/.config.cache.json'
|
||||||
|
|
||||||
@@ -477,8 +489,8 @@ def main():
|
|||||||
module = importlib.util.module_from_spec(spec)
|
module = importlib.util.module_from_spec(spec)
|
||||||
spec.loader.exec_module(module)
|
spec.loader.exec_module(module)
|
||||||
module.get_cwd = get_cwd
|
module.get_cwd = get_cwd
|
||||||
except Exception:
|
except Exception: # noqa: BLE001
|
||||||
exit()
|
sys.exit()
|
||||||
|
|
||||||
# New API: _connpy_tree → integrate into main tree and use resolver
|
# New API: _connpy_tree → integrate into main tree and use resolver
|
||||||
if hasattr(module, "_connpy_tree"):
|
if hasattr(module, "_connpy_tree"):
|
||||||
@@ -493,20 +505,20 @@ def main():
|
|||||||
try:
|
try:
|
||||||
with open(cachefile, "r") as jsonconf:
|
with open(cachefile, "r") as jsonconf:
|
||||||
info["config"] = json.load(jsonconf)
|
info["config"] = json.load(jsonconf)
|
||||||
except Exception:
|
except Exception: # noqa: BLE001
|
||||||
try:
|
try:
|
||||||
import yaml
|
import yaml
|
||||||
with open(configdir + '/config.yaml', "r") as yamlconf:
|
with open(configdir + '/config.yaml', "r") as yamlconf:
|
||||||
info["config"] = yaml.safe_load(yamlconf)
|
info["config"] = yaml.safe_load(yamlconf)
|
||||||
except Exception:
|
except Exception: # noqa: BLE001
|
||||||
info["config"] = {}
|
info["config"] = {}
|
||||||
try:
|
try:
|
||||||
plugin_completion = getattr(module, "_connpy_completion")
|
plugin_completion = module._connpy_completion
|
||||||
strings = plugin_completion(wordsnumber, words, info)
|
strings = plugin_completion(wordsnumber, words, info)
|
||||||
except Exception:
|
except Exception: # noqa: BLE001
|
||||||
exit()
|
sys.exit()
|
||||||
else:
|
else:
|
||||||
exit()
|
sys.exit()
|
||||||
|
|
||||||
# --- Tree-based completion ---
|
# --- Tree-based completion ---
|
||||||
else:
|
else:
|
||||||
|
|||||||
+125
-7
@@ -103,7 +103,7 @@ class node:
|
|||||||
|
|
||||||
'''
|
'''
|
||||||
|
|
||||||
def __init__(self, unique, host, options='', logs='', password='', port='', protocol='', user='', config='', tags='', jumphost=''):
|
def __init__(self, unique, host, options='', logs='', password='', port='', protocol='', user='', config='', tags='', jumphost='', allow_unsafe_options=None):
|
||||||
'''
|
'''
|
||||||
|
|
||||||
### Parameters:
|
### Parameters:
|
||||||
@@ -138,14 +138,22 @@ class node:
|
|||||||
like "os", "prompt" and "screenleght_command"
|
like "os", "prompt" and "screenleght_command"
|
||||||
|
|
||||||
- jumphost (str): Reference another node to be used as a jumphost
|
- jumphost (str): Reference another node to be used as a jumphost
|
||||||
|
|
||||||
|
- allow_unsafe_options (bool): Explicitly override safety restrictions.
|
||||||
'''
|
'''
|
||||||
self.config = config
|
self.config = config
|
||||||
if config == '':
|
if config == '':
|
||||||
self.idletime = 0
|
self.idletime = 0
|
||||||
self.key = None
|
self.key = None
|
||||||
else:
|
else:
|
||||||
self.idletime = config.config["idletime"]
|
self.idletime = config.config["idletime"] if (hasattr(config, "config") and isinstance(config.config, dict) and "idletime" in config.config) else 0
|
||||||
self.key = config.key
|
self.key = getattr(config, "key", None)
|
||||||
|
if allow_unsafe_options is not None:
|
||||||
|
self.allow_unsafe_options = bool(allow_unsafe_options)
|
||||||
|
elif config != '' and config is not None and hasattr(config, "config") and isinstance(config.config, dict):
|
||||||
|
self.allow_unsafe_options = bool(config.config.get("allow_unsafe_options", False))
|
||||||
|
else:
|
||||||
|
self.allow_unsafe_options = False
|
||||||
self.unique = unique
|
self.unique = unique
|
||||||
attr = {"host": host, "logs": logs, "options":options, "port": port, "protocol": protocol, "user": user, "tags": tags, "jumphost": jumphost}
|
attr = {"host": host, "logs": logs, "options":options, "port": port, "protocol": protocol, "user": user, "tags": tags, "jumphost": jumphost}
|
||||||
for key in attr:
|
for key in attr:
|
||||||
@@ -216,6 +224,79 @@ class node:
|
|||||||
jh_dict["password"] = [jh_dict["password"]]
|
jh_dict["password"] = [jh_dict["password"]]
|
||||||
return jh_dict
|
return jh_dict
|
||||||
|
|
||||||
|
def _is_unsafe_allowed(self) -> bool:
|
||||||
|
if getattr(self, "allow_unsafe_options", False):
|
||||||
|
return True
|
||||||
|
if hasattr(self, "config") and self.config and hasattr(self.config, "config") and isinstance(self.config.config, dict):
|
||||||
|
return bool(self.config.config.get("allow_unsafe_options", False))
|
||||||
|
return False
|
||||||
|
|
||||||
|
def _validate_base_fields(self, check_port=True):
|
||||||
|
"""Sanitize host, user, and port against argument injection and shell metacharacters."""
|
||||||
|
host_s = str(self.host) if self.host is not None else ""
|
||||||
|
if not host_s:
|
||||||
|
raise ValueError("Host cannot be empty")
|
||||||
|
if host_s.startswith("-"):
|
||||||
|
raise ValueError(f"Host cannot start with '-': '{self.host}'")
|
||||||
|
host_bare = (
|
||||||
|
host_s[1:-1]
|
||||||
|
if (host_s.startswith("[") and host_s.endswith("]"))
|
||||||
|
else host_s
|
||||||
|
)
|
||||||
|
if not host_bare or host_bare.startswith("-"):
|
||||||
|
raise ValueError(f"Invalid characters in host: '{self.host}'")
|
||||||
|
if _LEGACY_SSH_UNSAFE_RE.search(host_bare):
|
||||||
|
raise ValueError(f"Invalid characters in host: '{self.host}'")
|
||||||
|
|
||||||
|
user_s = str(self.user) if self.user is not None else ""
|
||||||
|
if user_s:
|
||||||
|
if user_s.startswith("-"):
|
||||||
|
raise ValueError(f"User cannot start with '-': '{self.user}'")
|
||||||
|
if _LEGACY_SSH_UNSAFE_RE.search(user_s):
|
||||||
|
raise ValueError(f"Invalid characters in user: '{self.user}'")
|
||||||
|
|
||||||
|
if check_port and self.port != "" and self.port is not None:
|
||||||
|
if isinstance(self.port, (bool, float)):
|
||||||
|
raise ValueError(f"Invalid TCP SSH port '{self.port}'")
|
||||||
|
port_s = str(self.port).strip()
|
||||||
|
if port_s:
|
||||||
|
if (
|
||||||
|
not port_s.isascii()
|
||||||
|
or not re.fullmatch(r"[0-9]+", port_s, re.ASCII)
|
||||||
|
or not (1 <= int(port_s) <= 65535)
|
||||||
|
):
|
||||||
|
raise ValueError(f"Invalid TCP SSH port '{self.port}'")
|
||||||
|
|
||||||
|
def _validate_options_and_safety(self):
|
||||||
|
"""Validate options string for unbalanced quotes and dangerous control characters."""
|
||||||
|
if not self.options:
|
||||||
|
return
|
||||||
|
opts_str = str(self.options).strip()
|
||||||
|
if not opts_str:
|
||||||
|
return
|
||||||
|
if not self._is_unsafe_allowed():
|
||||||
|
if any(c in opts_str for c in ("\n", "\r", "\x00")):
|
||||||
|
raise ValueError("Options cannot contain newlines or null bytes")
|
||||||
|
try:
|
||||||
|
shlex.split(opts_str)
|
||||||
|
except ValueError as e:
|
||||||
|
raise ValueError(f"Invalid options: unbalanced quotes ({e})") from e
|
||||||
|
|
||||||
|
def _validate_ssh_options(self):
|
||||||
|
"""Sanitize SSH options against local command execution directives."""
|
||||||
|
self._validate_options_and_safety()
|
||||||
|
if not self.options:
|
||||||
|
return
|
||||||
|
opts_str = str(self.options).strip()
|
||||||
|
if not opts_str:
|
||||||
|
return
|
||||||
|
if not self._is_unsafe_allowed():
|
||||||
|
dangerous_directives = ("ProxyCommand", "LocalCommand", "PermitLocalCommand", "KnownHostsCommand")
|
||||||
|
pattern = r'(?:' + '|'.join(dangerous_directives) + r')'
|
||||||
|
match = re.search(pattern, opts_str, re.IGNORECASE)
|
||||||
|
if match:
|
||||||
|
raise ValueError(f"Disallowed SSH directive in options: '{match.group(0)}'")
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _validate_legacy_ssh_hop_fields(host, user, port):
|
def _validate_legacy_ssh_hop_fields(host, user, port):
|
||||||
"""Minimal injection guard for legacy SSH jumphost fields (preserves DOMAIN\\user, %scope, etc.)."""
|
"""Minimal injection guard for legacy SSH jumphost fields (preserves DOMAIN\\user, %scope, etc.)."""
|
||||||
@@ -1442,10 +1523,13 @@ class node:
|
|||||||
|
|
||||||
@MethodHook
|
@MethodHook
|
||||||
def _generate_ssh_sftp_cmd(self, force_ssh=False, allow_reverse_tunnel_port=None):
|
def _generate_ssh_sftp_cmd(self, force_ssh=False, allow_reverse_tunnel_port=None):
|
||||||
|
self._validate_base_fields(check_port=True)
|
||||||
is_from_mosh = (
|
is_from_mosh = (
|
||||||
self.protocol == "mosh"
|
self.protocol == "mosh"
|
||||||
or getattr(self, "_orig_protocol", "") == "mosh"
|
or getattr(self, "_orig_protocol", "") == "mosh"
|
||||||
)
|
)
|
||||||
|
if not is_from_mosh:
|
||||||
|
self._validate_ssh_options()
|
||||||
proto = "ssh" if force_ssh else self.protocol
|
proto = "ssh" if force_ssh else self.protocol
|
||||||
cmd = proto
|
cmd = proto
|
||||||
if is_from_mosh:
|
if is_from_mosh:
|
||||||
@@ -1536,7 +1620,8 @@ class node:
|
|||||||
if isinstance(self.tags, dict) and self.tags.get("mosh_ssh_options"):
|
if isinstance(self.tags, dict) and self.tags.get("mosh_ssh_options"):
|
||||||
ssh_opts = str(self.tags["mosh_ssh_options"]).strip()
|
ssh_opts = str(self.tags["mosh_ssh_options"]).strip()
|
||||||
if ssh_opts:
|
if ssh_opts:
|
||||||
if re.search(r'(?:LocalCommand|ProxyCommand)', ssh_opts, re.IGNORECASE):
|
if not self._is_unsafe_allowed():
|
||||||
|
if re.search(r'(?:LocalCommand|ProxyCommand|PermitLocalCommand|KnownHostsCommand)', ssh_opts, re.IGNORECASE):
|
||||||
raise ValueError("mosh_ssh_options cannot contain LocalCommand or ProxyCommand")
|
raise ValueError("mosh_ssh_options cannot contain LocalCommand or ProxyCommand")
|
||||||
try:
|
try:
|
||||||
shlex.split(ssh_opts)
|
shlex.split(ssh_opts)
|
||||||
@@ -1555,7 +1640,8 @@ class node:
|
|||||||
if opts:
|
if opts:
|
||||||
if re.search(r'--(?:ssh|server|client)(?:=|\s|$)', opts):
|
if re.search(r'--(?:ssh|server|client)(?:=|\s|$)', opts):
|
||||||
raise ValueError("Options cannot override --ssh, --server or --client for mosh")
|
raise ValueError("Options cannot override --ssh, --server or --client for mosh")
|
||||||
if re.search(r'(?:LocalCommand|ProxyCommand)', opts, re.IGNORECASE):
|
if not self._is_unsafe_allowed():
|
||||||
|
if re.search(r'(?:LocalCommand|ProxyCommand|PermitLocalCommand|KnownHostsCommand)', opts, re.IGNORECASE):
|
||||||
raise ValueError("Options cannot contain LocalCommand or ProxyCommand")
|
raise ValueError("Options cannot contain LocalCommand or ProxyCommand")
|
||||||
try:
|
try:
|
||||||
shlex.split(opts)
|
shlex.split(opts)
|
||||||
@@ -1569,6 +1655,8 @@ class node:
|
|||||||
|
|
||||||
@MethodHook
|
@MethodHook
|
||||||
def _generate_telnet_cmd(self):
|
def _generate_telnet_cmd(self):
|
||||||
|
self._validate_base_fields(check_port=True)
|
||||||
|
self._validate_options_and_safety()
|
||||||
cmd = f"telnet {self.host}"
|
cmd = f"telnet {self.host}"
|
||||||
if self.port:
|
if self.port:
|
||||||
cmd += f" {self.port}"
|
cmd += f" {self.port}"
|
||||||
@@ -1578,22 +1666,52 @@ class node:
|
|||||||
|
|
||||||
@MethodHook
|
@MethodHook
|
||||||
def _generate_kube_cmd(self):
|
def _generate_kube_cmd(self):
|
||||||
cmd = f"kubectl exec {self.options} {self.host} -it --"
|
self._validate_base_fields(check_port=False)
|
||||||
|
self._validate_options_and_safety()
|
||||||
kube_command = self.tags.get("kube_command", "/bin/bash") if isinstance(self.tags, dict) else "/bin/bash"
|
kube_command = self.tags.get("kube_command", "/bin/bash") if isinstance(self.tags, dict) else "/bin/bash"
|
||||||
|
if not self._is_unsafe_allowed() and kube_command:
|
||||||
|
k_str = str(kube_command)
|
||||||
|
if any(c in k_str for c in ("\n", "\r", "\x00")):
|
||||||
|
raise ValueError("kube_command cannot contain newlines or null bytes")
|
||||||
|
try:
|
||||||
|
shlex.split(k_str)
|
||||||
|
except ValueError as e:
|
||||||
|
raise ValueError(f"Invalid kube_command: unbalanced quotes ({e})") from e
|
||||||
|
cmd = f"kubectl exec {self.options} {self.host} -it --"
|
||||||
cmd += f" {kube_command}"
|
cmd += f" {kube_command}"
|
||||||
return cmd
|
return cmd
|
||||||
|
|
||||||
@MethodHook
|
@MethodHook
|
||||||
def _generate_docker_cmd(self):
|
def _generate_docker_cmd(self):
|
||||||
cmd = f"docker {self.options} exec -it {self.host}"
|
self._validate_base_fields(check_port=False)
|
||||||
|
self._validate_options_and_safety()
|
||||||
docker_command = self.tags.get("docker_command", "/bin/bash") if isinstance(self.tags, dict) else "/bin/bash"
|
docker_command = self.tags.get("docker_command", "/bin/bash") if isinstance(self.tags, dict) else "/bin/bash"
|
||||||
|
if not self._is_unsafe_allowed() and docker_command:
|
||||||
|
d_str = str(docker_command)
|
||||||
|
if any(c in d_str for c in ("\n", "\r", "\x00")):
|
||||||
|
raise ValueError("docker_command cannot contain newlines or null bytes")
|
||||||
|
try:
|
||||||
|
shlex.split(d_str)
|
||||||
|
except ValueError as e:
|
||||||
|
raise ValueError(f"Invalid docker_command: unbalanced quotes ({e})") from e
|
||||||
|
cmd = f"docker {self.options} exec -it {self.host}"
|
||||||
cmd += f" {docker_command}"
|
cmd += f" {docker_command}"
|
||||||
return cmd
|
return cmd
|
||||||
|
|
||||||
@MethodHook
|
@MethodHook
|
||||||
def _generate_ssm_cmd(self):
|
def _generate_ssm_cmd(self):
|
||||||
|
self._validate_base_fields(check_port=False)
|
||||||
|
self._validate_options_and_safety()
|
||||||
region = self.tags.get("region", "") if isinstance(self.tags, dict) else ""
|
region = self.tags.get("region", "") if isinstance(self.tags, dict) else ""
|
||||||
profile = self.tags.get("profile", "") if isinstance(self.tags, dict) else ""
|
profile = self.tags.get("profile", "") if isinstance(self.tags, dict) else ""
|
||||||
|
if not self._is_unsafe_allowed():
|
||||||
|
for field_name, field_val in [("region", region), ("profile", profile)]:
|
||||||
|
if field_val:
|
||||||
|
f_str = str(field_val)
|
||||||
|
if f_str.startswith("-"):
|
||||||
|
raise ValueError(f"SSM tag '{field_name}' cannot start with '-': '{field_val}'")
|
||||||
|
if re.search(r'[\s\x00-\x1f\x7f;|&$`><"\']', f_str):
|
||||||
|
raise ValueError(f"Invalid characters in SSM tag '{field_name}': '{field_val}'")
|
||||||
cmd = f"aws ssm start-session --target {self.host}"
|
cmd = f"aws ssm start-session --target {self.host}"
|
||||||
if region:
|
if region:
|
||||||
cmd += f" --region {region}"
|
cmd += f" --region {region}"
|
||||||
|
|||||||
@@ -960,7 +960,7 @@ class ImportExportServicer(connpy_pb2_grpc.ImportExportServiceServicer):
|
|||||||
if request.value.startswith("---YAML---\n"):
|
if request.value.startswith("---YAML---\n"):
|
||||||
import yaml
|
import yaml
|
||||||
content = request.value[len("---YAML---\n"):]
|
content = request.value[len("---YAML---\n"):]
|
||||||
data = yaml.load(content, Loader=yaml.FullLoader)
|
data = yaml.safe_load(content)
|
||||||
self.service.import_from_dict(data)
|
self.service.import_from_dict(data)
|
||||||
else:
|
else:
|
||||||
self.service.import_from_file(request.value)
|
self.service.import_from_file(request.value)
|
||||||
|
|||||||
+91
-27
@@ -1,6 +1,7 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
#Imports
|
#Imports
|
||||||
from functools import wraps, partial, update_wrapper
|
from functools import update_wrapper, wraps
|
||||||
|
|
||||||
from . import printer
|
from . import printer
|
||||||
|
|
||||||
#functions and classes
|
#functions and classes
|
||||||
@@ -10,54 +11,118 @@ class MethodHook:
|
|||||||
|
|
||||||
def __init__(self, func):
|
def __init__(self, func):
|
||||||
self.func = func
|
self.func = func
|
||||||
self.pre_hooks = [] # List to store registered pre-hooks
|
self.pre_hooks = [] # List to store registered class-level pre-hooks
|
||||||
self.post_hooks = [] # List to store registered post-hooks
|
self.post_hooks = [] # List to store registered class-level post-hooks
|
||||||
wraps(func)(self)
|
wraps(func)(self)
|
||||||
|
|
||||||
|
def register_pre_hook(self, hook):
|
||||||
|
"""Register a function to be called before the original function"""
|
||||||
|
if hook not in self.pre_hooks:
|
||||||
|
self.pre_hooks.append(hook)
|
||||||
|
|
||||||
|
def register_post_hook(self, hook):
|
||||||
|
"""Register a function to be called after the original function"""
|
||||||
|
if hook not in self.post_hooks:
|
||||||
|
self.post_hooks.append(hook)
|
||||||
|
|
||||||
def __call__(self, *args, **kwargs):
|
def __call__(self, *args, **kwargs):
|
||||||
# Execute pre-hooks before the original function
|
# Fallback when called directly on descriptor or standalone function
|
||||||
for hook in self.pre_hooks:
|
inst_pre = []
|
||||||
|
inst_post = []
|
||||||
|
if args and hasattr(args[0], "__dict__"):
|
||||||
|
inst_dict = args[0].__dict__
|
||||||
|
inst_pre = inst_dict.get("_method_pre_hooks", {}).get(self.func.__name__, []) if isinstance(inst_dict.get("_method_pre_hooks"), dict) else []
|
||||||
|
inst_post = inst_dict.get("_method_post_hooks", {}).get(self.func.__name__, []) if isinstance(inst_dict.get("_method_post_hooks"), dict) else []
|
||||||
|
|
||||||
|
all_pre = list(self.pre_hooks) + [h for h in inst_pre if h not in self.pre_hooks]
|
||||||
|
for hook in all_pre:
|
||||||
try:
|
try:
|
||||||
args, kwargs = hook(*args, **kwargs)
|
args, kwargs = hook(*args, **kwargs)
|
||||||
except Exception as e:
|
except Exception as e: # noqa: BLE001
|
||||||
hook_name = getattr(hook, "__name__", str(hook))
|
hook_name = getattr(hook, "__name__", str(hook))
|
||||||
printer.error(f"{self.func.__name__} Pre-hook {hook_name} raised an exception: {e}")
|
printer.error(f"{self.func.__name__} Pre-hook {hook_name} raised an exception: {e}")
|
||||||
|
|
||||||
result = self.func(*args, **kwargs)
|
result = self.func(*args, **kwargs)
|
||||||
|
|
||||||
# Execute post-hooks after the original function
|
all_post = list(self.post_hooks) + [h for h in inst_post if h not in self.post_hooks]
|
||||||
if self.post_hooks:
|
for hook in all_post:
|
||||||
#printer.info(f"Executing {len(self.post_hooks)} post-hooks for {self.func.__name__}...")
|
|
||||||
pass
|
|
||||||
for hook in self.post_hooks:
|
|
||||||
try:
|
try:
|
||||||
result = hook(*args, **kwargs, result=result) # Pass result to hooks
|
result = hook(*args, **kwargs, result=result)
|
||||||
except Exception as e:
|
except Exception as e: # noqa: BLE001
|
||||||
hook_name = getattr(hook, "__name__", str(hook))
|
hook_name = getattr(hook, "__name__", str(hook))
|
||||||
printer.error(f"{self.func.__name__} Post-hook {hook_name} raised an exception: {e}")
|
printer.error(f"{self.func.__name__} Post-hook {hook_name} raised an exception: {e}")
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
def __get__(self, instance, owner):
|
def __get__(self, instance, owner):
|
||||||
if not instance:
|
if instance is None:
|
||||||
return self
|
return self
|
||||||
else:
|
|
||||||
return self.make_callable(instance)
|
return self.make_callable(instance)
|
||||||
|
|
||||||
def make_callable(self, instance):
|
def make_callable(self, instance):
|
||||||
# Returns a callable that also allows access to hook registration methods
|
# Returns a callable bound to instance with per-instance hook registration
|
||||||
callable_instance = partial(self.__call__, instance)
|
func_name = self.func.__name__
|
||||||
callable_instance.register_pre_hook = self.register_pre_hook
|
|
||||||
callable_instance.register_post_hook = self.register_post_hook
|
|
||||||
return callable_instance
|
|
||||||
|
|
||||||
def register_pre_hook(self, hook):
|
def bound_method(*args, **kwargs):
|
||||||
"""Register a function to be called before the original function"""
|
# Pre-hooks: class-level followed by instance-level
|
||||||
self.pre_hooks.append(hook)
|
if hasattr(instance, "__dict__"):
|
||||||
|
inst_pre = instance.__dict__.get("_method_pre_hooks", {}).get(func_name, [])
|
||||||
|
inst_post = instance.__dict__.get("_method_post_hooks", {}).get(func_name, [])
|
||||||
|
else:
|
||||||
|
inst_pre = instance._method_pre_hooks.get(func_name, []) if hasattr(instance, "_method_pre_hooks") else []
|
||||||
|
inst_post = instance._method_post_hooks.get(func_name, []) if hasattr(instance, "_method_post_hooks") else []
|
||||||
|
|
||||||
|
all_pre = list(self.pre_hooks) + [h for h in inst_pre if h not in self.pre_hooks]
|
||||||
|
call_args = (instance,) + args
|
||||||
|
|
||||||
|
for hook in all_pre:
|
||||||
|
try:
|
||||||
|
call_args, kwargs = hook(*call_args, **kwargs)
|
||||||
|
except Exception as e: # noqa: BLE001
|
||||||
|
hook_name = getattr(hook, "__name__", str(hook))
|
||||||
|
printer.error(f"{func_name} Pre-hook {hook_name} raised an exception: {e}")
|
||||||
|
|
||||||
|
result = self.func(*call_args, **kwargs)
|
||||||
|
|
||||||
|
all_post = list(self.post_hooks) + [h for h in inst_post if h not in self.post_hooks]
|
||||||
|
for hook in all_post:
|
||||||
|
try:
|
||||||
|
result = hook(*call_args, **kwargs, result=result)
|
||||||
|
except Exception as e: # noqa: BLE001
|
||||||
|
hook_name = getattr(hook, "__name__", str(hook))
|
||||||
|
printer.error(f"{func_name} Post-hook {hook_name} raised an exception: {e}")
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
wraps(self.func)(bound_method)
|
||||||
|
|
||||||
|
def register_pre_hook(hook):
|
||||||
|
if hasattr(instance, "__dict__"):
|
||||||
|
hooks = instance.__dict__.setdefault("_method_pre_hooks", {}).setdefault(func_name, [])
|
||||||
|
else:
|
||||||
|
if not hasattr(instance, "_method_pre_hooks"):
|
||||||
|
instance._method_pre_hooks = {}
|
||||||
|
hooks = instance._method_pre_hooks.setdefault(func_name, [])
|
||||||
|
if hook not in hooks:
|
||||||
|
hooks.append(hook)
|
||||||
|
|
||||||
|
def register_post_hook(hook):
|
||||||
|
if hasattr(instance, "__dict__"):
|
||||||
|
hooks = instance.__dict__.setdefault("_method_post_hooks", {}).setdefault(func_name, [])
|
||||||
|
else:
|
||||||
|
if not hasattr(instance, "_method_post_hooks"):
|
||||||
|
instance._method_post_hooks = {}
|
||||||
|
hooks = instance._method_post_hooks.setdefault(func_name, [])
|
||||||
|
if hook not in hooks:
|
||||||
|
hooks.append(hook)
|
||||||
|
|
||||||
|
bound_method.register_pre_hook = register_pre_hook
|
||||||
|
bound_method.register_post_hook = register_post_hook
|
||||||
|
bound_method.__self__ = instance
|
||||||
|
bound_method.__func__ = self.func
|
||||||
|
|
||||||
|
return bound_method
|
||||||
|
|
||||||
def register_post_hook(self, hook):
|
|
||||||
"""Register a function to be called after the original function"""
|
|
||||||
self.post_hooks.append(hook)
|
|
||||||
|
|
||||||
class ClassHook:
|
class ClassHook:
|
||||||
"""Decorator class to enable Class Modifying"""
|
"""Decorator class to enable Class Modifying"""
|
||||||
@@ -91,4 +156,3 @@ class ClassHook:
|
|||||||
"""Modify this specific instance."""
|
"""Modify this specific instance."""
|
||||||
modification_func(instance)
|
modification_func(instance)
|
||||||
return modify_instance
|
return modify_instance
|
||||||
|
|
||||||
+98
-41
@@ -1,9 +1,9 @@
|
|||||||
import asyncio
|
import asyncio
|
||||||
import json
|
import logging
|
||||||
import os
|
import os
|
||||||
import threading
|
import threading
|
||||||
from typing import Any, Dict, List, Optional
|
from contextlib import suppress
|
||||||
import logging
|
from typing import Any, ClassVar
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from mcp import ClientSession
|
from mcp import ClientSession
|
||||||
@@ -17,32 +17,54 @@ logging.getLogger("mcp").setLevel(logging.CRITICAL)
|
|||||||
logging.getLogger("httpx").setLevel(logging.CRITICAL)
|
logging.getLogger("httpx").setLevel(logging.CRITICAL)
|
||||||
logging.getLogger("httpcore").setLevel(logging.CRITICAL)
|
logging.getLogger("httpcore").setLevel(logging.CRITICAL)
|
||||||
|
|
||||||
|
|
||||||
class MCPClientManager:
|
class MCPClientManager:
|
||||||
"""Manages MCP SSE client connections for connpy."""
|
"""Manages MCP SSE client connections for connpy."""
|
||||||
|
|
||||||
_instance = None
|
_instances: ClassVar[dict[Any, "MCPClientManager"]] = {}
|
||||||
|
_instance: ClassVar["MCPClientManager | None"] = None
|
||||||
_lock = threading.Lock()
|
_lock = threading.Lock()
|
||||||
|
|
||||||
def __new__(cls, *args, **kwargs):
|
@classmethod
|
||||||
|
def _resolve_scope_key(cls, config=None, *args, **kwargs) -> Any:
|
||||||
|
cfg = config
|
||||||
|
if cfg is None and "config" in kwargs:
|
||||||
|
cfg = kwargs["config"]
|
||||||
|
if cfg is not None:
|
||||||
|
defaultdir = getattr(cfg, "defaultdir", None)
|
||||||
|
if defaultdir:
|
||||||
|
return os.path.abspath(str(defaultdir))
|
||||||
|
return id(cfg)
|
||||||
|
return "default"
|
||||||
|
|
||||||
|
def __new__(cls, config=None, *args, **kwargs):
|
||||||
|
key = cls._resolve_scope_key(config, *args, **kwargs)
|
||||||
with cls._lock:
|
with cls._lock:
|
||||||
if cls._instance is None:
|
if cls._instance is None and cls._instances:
|
||||||
cls._instance = super(MCPClientManager, cls).__new__(cls)
|
cls._instances.clear()
|
||||||
cls._instance._initialized = False
|
if key not in cls._instances:
|
||||||
return cls._instance
|
instance = super().__new__(cls)
|
||||||
|
instance._initialized = False
|
||||||
|
instance._scope_key = key
|
||||||
|
cls._instances[key] = instance
|
||||||
|
cls._instance = cls._instances[key]
|
||||||
|
return cls._instances[key]
|
||||||
|
|
||||||
def __init__(self, config=None):
|
def __init__(self, config=None):
|
||||||
if self._initialized:
|
if config is not None:
|
||||||
return
|
|
||||||
self.config = config
|
self.config = config
|
||||||
self.sessions: Dict[str, Dict[str, Any]] = {} # name -> {session, stack}
|
if getattr(self, "_initialized", False):
|
||||||
self.tool_cache: Dict[str, List[Dict[str, Any]]] = {}
|
return
|
||||||
self._connecting: Dict[str, asyncio.Future] = {}
|
if not hasattr(self, "config"):
|
||||||
|
self.config = config
|
||||||
|
self.sessions: dict[str, dict[str, Any]] = {} # name -> {session, stack}
|
||||||
|
self.tool_cache: dict[str, list[dict[str, Any]]] = {}
|
||||||
|
self._server_configs: dict[str, dict[str, Any]] = {}
|
||||||
|
self._connecting: dict[str, asyncio.Future] = {}
|
||||||
self._initialized = True
|
self._initialized = True
|
||||||
|
|
||||||
async def get_tools_for_llm(self, os_filter: Optional[str] = None) -> List[Dict[str, Any]]:
|
async def get_tools_for_llm(self, os_filter: str | None = None) -> list[dict[str, Any]]:
|
||||||
"""
|
"""Fetches tools from enabled MCP servers that match the OS filter."""
|
||||||
Fetches tools from enabled MCP servers that match the OS filter.
|
|
||||||
"""
|
|
||||||
if not MCP_AVAILABLE:
|
if not MCP_AVAILABLE:
|
||||||
return []
|
return []
|
||||||
|
|
||||||
@@ -52,29 +74,53 @@ class MCPClientManager:
|
|||||||
mcp_config = self.config.get_effective_setting("ai", {}).get("mcp_servers", {})
|
mcp_config = self.config.get_effective_setting("ai", {}).get("mcp_servers", {})
|
||||||
else:
|
else:
|
||||||
mcp_config = self.config.config.get("ai", {}).get("mcp_servers", {}) if hasattr(self.config, "config") else {}
|
mcp_config = self.config.config.get("ai", {}).get("mcp_servers", {}) if hasattr(self.config, "config") else {}
|
||||||
except Exception:
|
except Exception: # noqa: BLE001
|
||||||
return []
|
return []
|
||||||
|
|
||||||
|
if not isinstance(mcp_config, dict):
|
||||||
|
mcp_config = {}
|
||||||
|
|
||||||
|
# Invalidate cache and reconnect if server configuration changed or server was removed
|
||||||
|
current_names = set(mcp_config.keys())
|
||||||
|
for old_name in list(self._server_configs.keys()):
|
||||||
|
if old_name not in current_names:
|
||||||
|
self.tool_cache.pop(old_name, None)
|
||||||
|
old_data = self.sessions.pop(old_name, None)
|
||||||
|
if old_data and old_data.get("stack"):
|
||||||
|
with suppress(Exception):
|
||||||
|
await old_data["stack"].aclose()
|
||||||
|
self._server_configs.pop(old_name, None)
|
||||||
|
|
||||||
|
for name, cfg in mcp_config.items():
|
||||||
|
cached_cfg = self._server_configs.get(name)
|
||||||
|
if cached_cfg is not None and cached_cfg != cfg:
|
||||||
|
self.tool_cache.pop(name, None)
|
||||||
|
old_data = self.sessions.pop(name, None)
|
||||||
|
if old_data and old_data.get("stack"):
|
||||||
|
with suppress(Exception):
|
||||||
|
await old_data["stack"].aclose()
|
||||||
|
self._server_configs[name] = dict(cfg) if isinstance(cfg, dict) else cfg
|
||||||
|
|
||||||
async def _fetch(name, cfg):
|
async def _fetch(name, cfg):
|
||||||
if not cfg.get("enabled", True): return []
|
if not isinstance(cfg, dict) or not cfg.get("enabled", True):
|
||||||
|
return []
|
||||||
|
|
||||||
# Filter by OS if specified in config (primarily used for copilot strict matching)
|
# Filter by OS if specified in config (primarily used for copilot strict matching)
|
||||||
auto_os = cfg.get("auto_load_on_os")
|
auto_os = cfg.get("auto_load_on_os")
|
||||||
if os_filter is not None and auto_os and os_filter.lower() != auto_os.lower():
|
if os_filter is not None and auto_os and os_filter.lower() != auto_os.lower():
|
||||||
return []
|
return []
|
||||||
|
|
||||||
try:
|
with suppress(Exception):
|
||||||
session = await self._ensure_connected(name, cfg)
|
session = await self._ensure_connected(name, cfg)
|
||||||
if session:
|
if session:
|
||||||
if name in self.tool_cache: return self.tool_cache[name]
|
if name in self.tool_cache:
|
||||||
|
return self.tool_cache[name]
|
||||||
llm_tools = await self._fetch_tools_as_openai(name, session)
|
llm_tools = await self._fetch_tools_as_openai(name, session)
|
||||||
self.tool_cache[name] = llm_tools
|
self.tool_cache[name] = llm_tools
|
||||||
return llm_tools
|
return llm_tools
|
||||||
except Exception:
|
|
||||||
pass
|
|
||||||
return []
|
return []
|
||||||
|
|
||||||
tasks = [ _fetch(name, cfg) for name, cfg in mcp_config.items() ]
|
tasks = [_fetch(name, cfg) for name, cfg in mcp_config.items()]
|
||||||
|
|
||||||
if tasks:
|
if tasks:
|
||||||
results = await asyncio.gather(*tasks)
|
results = await asyncio.gather(*tasks)
|
||||||
@@ -83,8 +129,9 @@ class MCPClientManager:
|
|||||||
|
|
||||||
return all_llm_tools
|
return all_llm_tools
|
||||||
|
|
||||||
async def _ensure_connected(self, name: str, cfg: Dict[str, Any]) -> Optional[Any]:
|
async def _ensure_connected(self, name: str, cfg: dict[str, Any]) -> Any | None:
|
||||||
if not MCP_AVAILABLE: return None
|
if not MCP_AVAILABLE:
|
||||||
|
return None
|
||||||
|
|
||||||
if name in self.sessions and self.sessions[name].get("session"):
|
if name in self.sessions and self.sessions[name].get("session"):
|
||||||
return self.sessions[name]["session"]
|
return self.sessions[name]["session"]
|
||||||
@@ -96,7 +143,7 @@ class MCPClientManager:
|
|||||||
if name in self._connecting:
|
if name in self._connecting:
|
||||||
try:
|
try:
|
||||||
return await asyncio.wait_for(asyncio.shield(self._connecting[name]), timeout=10.0)
|
return await asyncio.wait_for(asyncio.shield(self._connecting[name]), timeout=10.0)
|
||||||
except Exception:
|
except Exception: # noqa: BLE001
|
||||||
return None
|
return None
|
||||||
|
|
||||||
loop = asyncio.get_running_loop()
|
loop = asyncio.get_running_loop()
|
||||||
@@ -117,33 +164,32 @@ class MCPClientManager:
|
|||||||
self.sessions[name] = {"session": session, "stack": stack}
|
self.sessions[name] = {"session": session, "stack": stack}
|
||||||
fut.set_result(session)
|
fut.set_result(session)
|
||||||
return session
|
return session
|
||||||
except Exception:
|
except Exception: # noqa: BLE001
|
||||||
fut.set_result(None)
|
fut.set_result(None)
|
||||||
return None
|
return None
|
||||||
finally:
|
finally:
|
||||||
if name in self._connecting:
|
if name in self._connecting:
|
||||||
del self._connecting[name]
|
del self._connecting[name]
|
||||||
|
|
||||||
async def _fetch_tools_as_openai(self, server_name: str, session: Any) -> List[Dict[str, Any]]:
|
async def _fetch_tools_as_openai(self, server_name: str, session: Any) -> list[dict[str, Any]]:
|
||||||
try:
|
try:
|
||||||
result = await asyncio.wait_for(session.list_tools(), timeout=5.0)
|
result = await asyncio.wait_for(session.list_tools(), timeout=5.0)
|
||||||
openai_tools = []
|
openai_tools = []
|
||||||
for tool in result.tools:
|
for tool in result.tools:
|
||||||
# Use mcp_ prefix to ensure valid function name for LiteLLM/Gemini
|
|
||||||
prefixed_name = f"mcp_{server_name}__{tool.name}"
|
prefixed_name = f"mcp_{server_name}__{tool.name}"
|
||||||
openai_tools.append({
|
openai_tools.append({
|
||||||
"type": "function",
|
"type": "function",
|
||||||
"function": {
|
"function": {
|
||||||
"name": prefixed_name,
|
"name": prefixed_name,
|
||||||
"description": f"[{server_name}] {tool.description}",
|
"description": f"[{server_name}] {tool.description}",
|
||||||
"parameters": tool.inputSchema
|
"parameters": tool.inputSchema,
|
||||||
}
|
},
|
||||||
})
|
})
|
||||||
return openai_tools
|
return openai_tools
|
||||||
except Exception:
|
except Exception: # noqa: BLE001
|
||||||
return []
|
return []
|
||||||
|
|
||||||
async def call_tool(self, full_tool_name: str, arguments: Dict[str, Any]) -> Any:
|
async def call_tool(self, full_tool_name: str, arguments: dict[str, Any]) -> Any:
|
||||||
"""Calls an MCP tool and returns text result."""
|
"""Calls an MCP tool and returns text result."""
|
||||||
if not MCP_AVAILABLE:
|
if not MCP_AVAILABLE:
|
||||||
return "Error: MCP SDK is not installed."
|
return "Error: MCP SDK is not installed."
|
||||||
@@ -151,7 +197,7 @@ class MCPClientManager:
|
|||||||
if "__" not in full_tool_name:
|
if "__" not in full_tool_name:
|
||||||
return f"Error: Tool {full_tool_name} is not a valid MCP tool."
|
return f"Error: Tool {full_tool_name} is not a valid MCP tool."
|
||||||
|
|
||||||
clean_name = full_tool_name[4:] if full_tool_name.startswith("mcp_") else full_tool_name
|
clean_name = full_tool_name.removeprefix("mcp_")
|
||||||
server_name, tool_name = clean_name.split("__", 1)
|
server_name, tool_name = clean_name.split("__", 1)
|
||||||
|
|
||||||
if server_name not in self.sessions:
|
if server_name not in self.sessions:
|
||||||
@@ -162,13 +208,24 @@ class MCPClientManager:
|
|||||||
result = await asyncio.wait_for(session.call_tool(tool_name, arguments), timeout=60.0)
|
result = await asyncio.wait_for(session.call_tool(tool_name, arguments), timeout=60.0)
|
||||||
text_outputs = [content.text for content in result.content if hasattr(content, "text")]
|
text_outputs = [content.text for content in result.content if hasattr(content, "text")]
|
||||||
return "\n".join(text_outputs) if text_outputs else str(result)
|
return "\n".join(text_outputs) if text_outputs else str(result)
|
||||||
except Exception as e:
|
except Exception as e: # noqa: BLE001
|
||||||
return f"Error calling tool {tool_name} on {server_name}: {str(e)}"
|
return f"Error calling tool {tool_name} on {server_name}: {e!s}"
|
||||||
|
|
||||||
async def shutdown(self):
|
async def shutdown(self):
|
||||||
"""Close all SSE connections."""
|
"""Close all SSE connections and clean up resources."""
|
||||||
for name, data in self.sessions.items():
|
for data in list(self.sessions.values()):
|
||||||
stack = data.get("stack")
|
stack = data.get("stack")
|
||||||
if stack:
|
if stack:
|
||||||
|
with suppress(Exception):
|
||||||
await stack.aclose()
|
await stack.aclose()
|
||||||
self.sessions = {}
|
self.sessions.clear()
|
||||||
|
self.tool_cache.clear()
|
||||||
|
self._server_configs.clear()
|
||||||
|
for fut in list(self._connecting.values()):
|
||||||
|
if not fut.done():
|
||||||
|
fut.cancel()
|
||||||
|
self._connecting.clear()
|
||||||
|
with self._lock:
|
||||||
|
if hasattr(self, "_scope_key") and self._scope_key in self._instances:
|
||||||
|
del self._instances[self._scope_key]
|
||||||
|
self._initialized = False
|
||||||
@@ -0,0 +1,528 @@
|
|||||||
|
import contextlib
|
||||||
|
import logging
|
||||||
|
import re
|
||||||
|
import sys
|
||||||
|
from typing import Any, ClassVar
|
||||||
|
|
||||||
|
import yaml
|
||||||
|
|
||||||
|
try:
|
||||||
|
from mcp.server.mcpserver import MCPServer
|
||||||
|
except ImportError:
|
||||||
|
try:
|
||||||
|
from mcp.server.fastmcp import FastMCP as MCPServer
|
||||||
|
except ImportError:
|
||||||
|
MCPServer = None
|
||||||
|
|
||||||
|
logger = logging.getLogger("connpy.mcp_server")
|
||||||
|
|
||||||
|
|
||||||
|
class ConnpyMCPServer:
|
||||||
|
"""Native Model Context Protocol (MCP) Server for connpy."""
|
||||||
|
|
||||||
|
SAFE_COMMANDS: ClassVar[list[str]] = [
|
||||||
|
r"^show\s+",
|
||||||
|
r"^ls\s*",
|
||||||
|
r"^cat\s+",
|
||||||
|
r"^ip\s+",
|
||||||
|
r"^pwd$",
|
||||||
|
r"^hostname$",
|
||||||
|
r"^uname",
|
||||||
|
r"^df\s*",
|
||||||
|
r"^free\s*",
|
||||||
|
r"^ps\s*",
|
||||||
|
r"^ping\s+",
|
||||||
|
r"^traceroute\s+",
|
||||||
|
r"^whois\s+",
|
||||||
|
r"^kubectl\s+(get|describe|version|logs|top|explain|cluster-info|api-resources|api-versions)\s+",
|
||||||
|
r"^systemctl\s+status\s+",
|
||||||
|
r"^journalctl\s+",
|
||||||
|
]
|
||||||
|
|
||||||
|
def __init__(self, config=None, services=None, trust: bool = False):
|
||||||
|
self.config = config
|
||||||
|
self.services = services
|
||||||
|
if self.services is None:
|
||||||
|
if self.config is not None:
|
||||||
|
from .services.provider import ServiceProvider
|
||||||
|
|
||||||
|
self.services = ServiceProvider(self.config)
|
||||||
|
elif self.config is None and hasattr(self.services, "config"):
|
||||||
|
self.config = self.services.config
|
||||||
|
|
||||||
|
self.trust = bool(trust)
|
||||||
|
|
||||||
|
# Build list of safe commands (SAFE_COMMANDS + trusted_commands from config)
|
||||||
|
aiconfig = {}
|
||||||
|
if self.config:
|
||||||
|
if hasattr(self.config, "get_effective_setting"):
|
||||||
|
aiconfig = self.config.get_effective_setting("ai", {})
|
||||||
|
elif hasattr(self.config, "config") and isinstance(self.config.config, dict):
|
||||||
|
aiconfig = self.config.config.get("ai", {})
|
||||||
|
|
||||||
|
custom_trusted = aiconfig.get("trusted_commands", []) if isinstance(aiconfig, dict) else []
|
||||||
|
if isinstance(custom_trusted, str):
|
||||||
|
custom_trusted = [c.strip() for c in custom_trusted.split(",") if c.strip()]
|
||||||
|
self.safe_commands = list(self.SAFE_COMMANDS) + (
|
||||||
|
custom_trusted if isinstance(custom_trusted, list) else []
|
||||||
|
)
|
||||||
|
|
||||||
|
# Initialize MCP Server
|
||||||
|
if MCPServer is not None:
|
||||||
|
self.server = MCPServer("connpy")
|
||||||
|
self._register_tools()
|
||||||
|
else:
|
||||||
|
self.server = None
|
||||||
|
|
||||||
|
def _register_tools(self):
|
||||||
|
"""Register the 3 tiers of MCP tools."""
|
||||||
|
if not self.server:
|
||||||
|
return
|
||||||
|
|
||||||
|
# Level 1 Tools (Read-Only)
|
||||||
|
self.server.tool()(self.list_nodes)
|
||||||
|
self.server.tool()(self.list_folders)
|
||||||
|
self.server.tool()(self.get_node_info)
|
||||||
|
self.server.tool()(self.list_profiles)
|
||||||
|
self.server.tool()(self.get_active_context)
|
||||||
|
|
||||||
|
# Level 2 Tools (Execution, Testing, Playbooks)
|
||||||
|
self.server.tool()(self.run_commands)
|
||||||
|
self.server.tool()(self.test_commands)
|
||||||
|
self.server.tool()(self.validate_playbook)
|
||||||
|
self.server.tool()(self.run_playbook)
|
||||||
|
|
||||||
|
# Level 3 Tools (CRUD - Requires trust=True)
|
||||||
|
self.server.tool()(self.add_node)
|
||||||
|
self.server.tool()(self.update_node)
|
||||||
|
self.server.tool()(self.delete_node)
|
||||||
|
|
||||||
|
# -------------------------------------------------------------------------
|
||||||
|
# Security Validation
|
||||||
|
# -------------------------------------------------------------------------
|
||||||
|
|
||||||
|
def is_safe_command(self, cmd: str) -> bool:
|
||||||
|
"""Validate if a command is considered safe under current trust settings.
|
||||||
|
|
||||||
|
- If trust is True, all commands are allowed.
|
||||||
|
- Block chaining/injection characters: ';', '&&', '||', '|', '`', '$(', '>', '<', '\\n', '\\r', '&'.
|
||||||
|
- Validates against self.safe_commands (SAFE_COMMANDS + trusted_commands).
|
||||||
|
"""
|
||||||
|
if self.trust:
|
||||||
|
return True
|
||||||
|
|
||||||
|
if not isinstance(cmd, str):
|
||||||
|
cmd = str(cmd)
|
||||||
|
|
||||||
|
# Check for shell chaining and redirection injection tokens
|
||||||
|
injection_tokens = [";", "&&", "||", "|", "`", "$(", ">", "<", "\n", "\r", "&"]
|
||||||
|
if any(tok in cmd for tok in injection_tokens):
|
||||||
|
return False
|
||||||
|
|
||||||
|
clean_cmd = cmd.strip()
|
||||||
|
return any(re.match(pattern, clean_cmd, re.IGNORECASE) for pattern in self.safe_commands)
|
||||||
|
|
||||||
|
def _mask_secrets(self, val: Any) -> Any:
|
||||||
|
"""Recursively mask passwords, passphrases, secrets, and auth tokens with '***'."""
|
||||||
|
if isinstance(val, dict):
|
||||||
|
masked = {}
|
||||||
|
for k, v in val.items():
|
||||||
|
k_lower = str(k).lower()
|
||||||
|
if any(sec in k_lower for sec in ["password", "passphrase", "secret", "token"]):
|
||||||
|
masked[k] = "***"
|
||||||
|
elif isinstance(v, (dict, list)):
|
||||||
|
masked[k] = self._mask_secrets(v)
|
||||||
|
else:
|
||||||
|
masked[k] = v
|
||||||
|
return masked
|
||||||
|
if isinstance(val, list):
|
||||||
|
return [self._mask_secrets(item) for item in val]
|
||||||
|
return val
|
||||||
|
|
||||||
|
# -------------------------------------------------------------------------
|
||||||
|
# Level 1: Read-Only Inventory and Context Tools
|
||||||
|
# -------------------------------------------------------------------------
|
||||||
|
|
||||||
|
def list_nodes(self, filter_pattern: str = "", format_str: str = "") -> list[str]:
|
||||||
|
"""List inventory nodes, optionally filtered by regex pattern and formatted."""
|
||||||
|
if not self.services or not hasattr(self.services, "nodes"):
|
||||||
|
return []
|
||||||
|
f = filter_pattern if filter_pattern else None
|
||||||
|
fmt = format_str if format_str else None
|
||||||
|
return self.services.nodes.list_nodes(filter_str=f, format_str=fmt)
|
||||||
|
|
||||||
|
def list_folders(self, filter_pattern: str = "") -> list[str]:
|
||||||
|
"""List inventory folders, optionally filtered by regex pattern."""
|
||||||
|
if not self.services or not hasattr(self.services, "nodes"):
|
||||||
|
return []
|
||||||
|
f = filter_pattern if filter_pattern else None
|
||||||
|
return self.services.nodes.list_folders(filter_str=f)
|
||||||
|
|
||||||
|
def get_node_info(self, node_name: str) -> dict:
|
||||||
|
"""Get full configuration metadata for a specific node.
|
||||||
|
|
||||||
|
Passwords, passphrases, and secrets are ALWAYS masked with '***'.
|
||||||
|
"""
|
||||||
|
if not self.services or not hasattr(self.services, "nodes"):
|
||||||
|
return {"error": "Node service unavailable."}
|
||||||
|
try:
|
||||||
|
details = self.services.nodes.get_node_details(node_name)
|
||||||
|
if hasattr(self.services, "profiles"):
|
||||||
|
with contextlib.suppress(Exception):
|
||||||
|
details = self.services.profiles.resolve_node_data(details)
|
||||||
|
return self._mask_secrets(details)
|
||||||
|
except Exception as e: # noqa: BLE001
|
||||||
|
return {"error": f"Node '{node_name}' not found: {e}"}
|
||||||
|
|
||||||
|
def list_profiles(self, filter_pattern: str = "") -> list[str]:
|
||||||
|
"""List all available node connection profiles."""
|
||||||
|
if not self.services or not hasattr(self.services, "profiles"):
|
||||||
|
return []
|
||||||
|
f = filter_pattern if filter_pattern else None
|
||||||
|
return self.services.profiles.list_profiles(filter_str=f)
|
||||||
|
|
||||||
|
def get_active_context(self) -> dict:
|
||||||
|
"""Get the currently active context name and its matching regex patterns."""
|
||||||
|
if not self.services or not hasattr(self.services, "context"):
|
||||||
|
return {"context": "all", "patterns": [".*"]}
|
||||||
|
try:
|
||||||
|
ctx_name = self.services.context.current_context
|
||||||
|
patterns = [p.pattern for p in self.services.context.get_active_regexes()]
|
||||||
|
return {"context": ctx_name, "patterns": patterns}
|
||||||
|
except Exception as e: # noqa: BLE001
|
||||||
|
return {"context": "unknown", "error": str(e)}
|
||||||
|
|
||||||
|
# -------------------------------------------------------------------------
|
||||||
|
# Level 2: Execution, Testing, and Playbooks Tools
|
||||||
|
# -------------------------------------------------------------------------
|
||||||
|
|
||||||
|
def run_commands(
|
||||||
|
self,
|
||||||
|
nodes_filter: str,
|
||||||
|
commands: list[str] | str,
|
||||||
|
parallel: bool = True,
|
||||||
|
timeout: int = 30,
|
||||||
|
) -> dict:
|
||||||
|
"""Execute commands on nodes matching the filter pattern.
|
||||||
|
|
||||||
|
Unsafe commands are blocked unless trust mode is enabled.
|
||||||
|
"""
|
||||||
|
if not self.services or not hasattr(self.services, "execution"):
|
||||||
|
return {"success": False, "error": "Execution service unavailable."}
|
||||||
|
|
||||||
|
# Normalize commands list
|
||||||
|
if isinstance(commands, str):
|
||||||
|
try:
|
||||||
|
import json
|
||||||
|
|
||||||
|
parsed = json.loads(commands)
|
||||||
|
commands = parsed if isinstance(parsed, list) else [commands]
|
||||||
|
except Exception: # noqa: BLE001
|
||||||
|
commands = [c.strip() for c in commands.splitlines() if c.strip()]
|
||||||
|
elif not isinstance(commands, list):
|
||||||
|
commands = [str(commands)]
|
||||||
|
|
||||||
|
# Check safety
|
||||||
|
if not self.trust:
|
||||||
|
unsafe = [cmd for cmd in commands if not self.is_safe_command(cmd)]
|
||||||
|
if unsafe:
|
||||||
|
return {
|
||||||
|
"success": False,
|
||||||
|
"error": f"Permission denied: unsafe commands detected: {unsafe}. Requires trust=True.",
|
||||||
|
}
|
||||||
|
|
||||||
|
par_count = 10 if parallel is True else (parallel if isinstance(parallel, int) else 1)
|
||||||
|
try:
|
||||||
|
results = self.services.execution.run_commands(
|
||||||
|
nodes_filter=nodes_filter,
|
||||||
|
commands=commands,
|
||||||
|
parallel=par_count,
|
||||||
|
timeout=timeout,
|
||||||
|
)
|
||||||
|
return {"success": True, "results": results}
|
||||||
|
except Exception as e: # noqa: BLE001
|
||||||
|
return {"success": False, "error": str(e)}
|
||||||
|
|
||||||
|
def test_commands(
|
||||||
|
self,
|
||||||
|
nodes_filter: str,
|
||||||
|
commands: list[str] | str,
|
||||||
|
expected: list[str] | str = "",
|
||||||
|
parallel: bool = True,
|
||||||
|
timeout: int = 30,
|
||||||
|
) -> dict:
|
||||||
|
"""Execute commands and assert expected patterns on nodes matching the filter."""
|
||||||
|
if not self.services or not hasattr(self.services, "execution"):
|
||||||
|
return {"success": False, "error": "Execution service unavailable."}
|
||||||
|
|
||||||
|
# Normalize commands
|
||||||
|
if isinstance(commands, str):
|
||||||
|
try:
|
||||||
|
import json
|
||||||
|
|
||||||
|
parsed = json.loads(commands)
|
||||||
|
commands = parsed if isinstance(parsed, list) else [commands]
|
||||||
|
except Exception: # noqa: BLE001
|
||||||
|
commands = [c.strip() for c in commands.splitlines() if c.strip()]
|
||||||
|
elif not isinstance(commands, list):
|
||||||
|
commands = [str(commands)]
|
||||||
|
|
||||||
|
# Normalize expected
|
||||||
|
if isinstance(expected, str):
|
||||||
|
try:
|
||||||
|
import json
|
||||||
|
|
||||||
|
parsed = json.loads(expected)
|
||||||
|
expected = parsed if isinstance(parsed, list) else ([expected] if expected else [])
|
||||||
|
except Exception: # noqa: BLE001
|
||||||
|
expected = [expected] if expected else []
|
||||||
|
elif not isinstance(expected, list):
|
||||||
|
expected = [str(expected)] if expected else []
|
||||||
|
|
||||||
|
# Check safety
|
||||||
|
if not self.trust:
|
||||||
|
unsafe = [cmd for cmd in commands if not self.is_safe_command(cmd)]
|
||||||
|
if unsafe:
|
||||||
|
return {
|
||||||
|
"success": False,
|
||||||
|
"error": f"Permission denied: unsafe commands detected: {unsafe}. Requires trust=True.",
|
||||||
|
}
|
||||||
|
|
||||||
|
par_count = 10 if parallel is True else (parallel if isinstance(parallel, int) else 1)
|
||||||
|
try:
|
||||||
|
results = self.services.execution.test_commands(
|
||||||
|
nodes_filter=nodes_filter,
|
||||||
|
commands=commands,
|
||||||
|
expected=expected,
|
||||||
|
parallel=par_count,
|
||||||
|
timeout=timeout,
|
||||||
|
)
|
||||||
|
return {"success": True, "results": results}
|
||||||
|
except Exception as e: # noqa: BLE001
|
||||||
|
return {"success": False, "error": str(e)}
|
||||||
|
|
||||||
|
def validate_playbook(self, playbook_yaml: str) -> dict:
|
||||||
|
"""Validate the syntax, schema, and tasks of a Connpy YAML playbook."""
|
||||||
|
try:
|
||||||
|
data = yaml.safe_load(playbook_yaml)
|
||||||
|
except Exception as e: # noqa: BLE001
|
||||||
|
return {"valid": False, "error": f"YAML Syntax Error: {e}"}
|
||||||
|
|
||||||
|
if not isinstance(data, dict):
|
||||||
|
return {"valid": False, "error": "Playbook must be a YAML dictionary."}
|
||||||
|
|
||||||
|
if "tasks" not in data:
|
||||||
|
return {"valid": False, "error": "Playbook missing mandatory root 'tasks' key."}
|
||||||
|
|
||||||
|
tasks = data["tasks"]
|
||||||
|
if not isinstance(tasks, list):
|
||||||
|
return {"valid": False, "error": "'tasks' must be a list of tasks."}
|
||||||
|
|
||||||
|
for idx, task in enumerate(tasks):
|
||||||
|
if not isinstance(task, dict):
|
||||||
|
return {"valid": False, "error": f"Task index {idx} must be a dictionary."}
|
||||||
|
|
||||||
|
name = task.get("name", f"Task {idx}")
|
||||||
|
mandatory = ["name", "action", "nodes", "commands", "output"]
|
||||||
|
missing = [f for f in mandatory if f not in task]
|
||||||
|
if missing:
|
||||||
|
return {
|
||||||
|
"valid": False,
|
||||||
|
"error": f"Task '{name}' (index {idx}) is missing mandatory fields: {missing}",
|
||||||
|
}
|
||||||
|
|
||||||
|
nodes = task["nodes"]
|
||||||
|
if not isinstance(nodes, (str, list)):
|
||||||
|
return {
|
||||||
|
"valid": False,
|
||||||
|
"error": f"Task '{name}' (index {idx}) 'nodes' must be a string or list of strings.",
|
||||||
|
}
|
||||||
|
|
||||||
|
if isinstance(nodes, list):
|
||||||
|
for n_idx, node_item in enumerate(nodes):
|
||||||
|
if not isinstance(node_item, str):
|
||||||
|
return {
|
||||||
|
"valid": False,
|
||||||
|
"error": f"Task '{name}' (index {idx}) 'nodes' contains non-string at index {n_idx}: {node_item}",
|
||||||
|
}
|
||||||
|
|
||||||
|
action = task["action"]
|
||||||
|
if action not in ["run", "test"]:
|
||||||
|
return {
|
||||||
|
"valid": False,
|
||||||
|
"error": f"Task '{name}' (index {idx}) invalid action '{action}'. Choices: 'run', 'test'.",
|
||||||
|
}
|
||||||
|
|
||||||
|
if action == "test" and "expected" not in task:
|
||||||
|
return {
|
||||||
|
"valid": False,
|
||||||
|
"error": f"Task '{name}' (index {idx}) action 'test' requires 'expected' key.",
|
||||||
|
}
|
||||||
|
|
||||||
|
output = task["output"]
|
||||||
|
if output not in [None, "stdout", "null"] and not str(output).startswith("/"):
|
||||||
|
return {
|
||||||
|
"valid": False,
|
||||||
|
"error": f"Task '{name}' (index {idx}) output '{output}' must be 'stdout', 'null' or an absolute path.",
|
||||||
|
}
|
||||||
|
|
||||||
|
return {"valid": True, "message": "Playbook schema and syntax is valid."}
|
||||||
|
|
||||||
|
def run_playbook(self, playbook_yaml: str) -> dict:
|
||||||
|
"""Validate and execute a playbook, verifying commands beforehand if trust=False."""
|
||||||
|
val = self.validate_playbook(playbook_yaml)
|
||||||
|
if not val.get("valid"):
|
||||||
|
return {"success": False, "error": f"Invalid playbook: {val.get('error')}"}
|
||||||
|
|
||||||
|
data = yaml.safe_load(playbook_yaml)
|
||||||
|
tasks = data.get("tasks", [])
|
||||||
|
|
||||||
|
# Pre-verify commands if not trusted
|
||||||
|
if not self.trust:
|
||||||
|
for idx, task in enumerate(tasks):
|
||||||
|
name = task.get("name", f"Task {idx}")
|
||||||
|
commands = task.get("commands", [])
|
||||||
|
if isinstance(commands, str):
|
||||||
|
commands = [commands]
|
||||||
|
unsafe = [cmd for cmd in commands if not self.is_safe_command(cmd)]
|
||||||
|
if unsafe:
|
||||||
|
return {
|
||||||
|
"success": False,
|
||||||
|
"error": f"Permission denied: unsafe commands detected in task '{name}': {unsafe}. Requires trust=True.",
|
||||||
|
}
|
||||||
|
|
||||||
|
# Execute tasks
|
||||||
|
results = {}
|
||||||
|
for idx, task in enumerate(tasks):
|
||||||
|
name = task.get("name", f"task_{idx}")
|
||||||
|
action = task.get("action")
|
||||||
|
nodes = task.get("nodes")
|
||||||
|
commands = task.get("commands")
|
||||||
|
if isinstance(commands, str):
|
||||||
|
commands = [commands]
|
||||||
|
|
||||||
|
try:
|
||||||
|
if action == "run":
|
||||||
|
res = self.services.execution.run_commands(nodes_filter=nodes, commands=commands)
|
||||||
|
elif action == "test":
|
||||||
|
expected = task.get("expected", [])
|
||||||
|
if isinstance(expected, str):
|
||||||
|
expected = [expected]
|
||||||
|
res = self.services.execution.test_commands(
|
||||||
|
nodes_filter=nodes, commands=commands, expected=expected
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
res = f"Unsupported action: {action}"
|
||||||
|
results[name] = res
|
||||||
|
except Exception as e: # noqa: BLE001
|
||||||
|
return {
|
||||||
|
"success": False,
|
||||||
|
"error": f"Execution failed at task '{name}': {e}",
|
||||||
|
"partial_results": results,
|
||||||
|
}
|
||||||
|
|
||||||
|
return {"success": True, "results": results}
|
||||||
|
|
||||||
|
# -------------------------------------------------------------------------
|
||||||
|
# Level 3: Inventory Mutation Tools (CRUD - Requires trust=True)
|
||||||
|
# -------------------------------------------------------------------------
|
||||||
|
|
||||||
|
def add_node(
|
||||||
|
self, unique_id: str, data: dict | None = None, is_folder: bool = False, **kwargs
|
||||||
|
) -> dict:
|
||||||
|
"""Add a new node or folder to the inventory (requires trust=True)."""
|
||||||
|
if not self.trust:
|
||||||
|
return {
|
||||||
|
"success": False,
|
||||||
|
"error": "Permission denied: CRUD operations require trust mode (--trust).",
|
||||||
|
}
|
||||||
|
|
||||||
|
if not self.services or not hasattr(self.services, "nodes"):
|
||||||
|
return {"success": False, "error": "Node service unavailable."}
|
||||||
|
|
||||||
|
node_data = (data or {}).copy()
|
||||||
|
node_data.update(kwargs)
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.services.nodes.add_node(unique_id, node_data, is_folder=is_folder)
|
||||||
|
return {
|
||||||
|
"success": True,
|
||||||
|
"message": f"{'Folder' if is_folder else 'Node'} '{unique_id}' created successfully.",
|
||||||
|
}
|
||||||
|
except Exception as e: # noqa: BLE001
|
||||||
|
return {"success": False, "error": str(e)}
|
||||||
|
|
||||||
|
def update_node(self, unique_id: str, updates: dict | None = None, **kwargs) -> dict:
|
||||||
|
"""Update an existing node configuration (requires trust=True)."""
|
||||||
|
if not self.trust:
|
||||||
|
return {
|
||||||
|
"success": False,
|
||||||
|
"error": "Permission denied: CRUD operations require trust mode (--trust).",
|
||||||
|
}
|
||||||
|
|
||||||
|
if not self.services or not hasattr(self.services, "nodes"):
|
||||||
|
return {"success": False, "error": "Node service unavailable."}
|
||||||
|
|
||||||
|
node_updates = (updates or {}).copy()
|
||||||
|
node_updates.update(kwargs)
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.services.nodes.update_node(unique_id, node_updates)
|
||||||
|
return {"success": True, "message": f"Node '{unique_id}' updated successfully."}
|
||||||
|
except Exception as e: # noqa: BLE001
|
||||||
|
return {"success": False, "error": str(e)}
|
||||||
|
|
||||||
|
def delete_node(self, unique_id: str, is_folder: bool = False) -> dict:
|
||||||
|
"""Delete a node or folder from the inventory (requires trust=True)."""
|
||||||
|
if not self.trust:
|
||||||
|
return {
|
||||||
|
"success": False,
|
||||||
|
"error": "Permission denied: CRUD operations require trust mode (--trust).",
|
||||||
|
}
|
||||||
|
|
||||||
|
if not self.services or not hasattr(self.services, "nodes"):
|
||||||
|
return {"success": False, "error": "Node service unavailable."}
|
||||||
|
|
||||||
|
try:
|
||||||
|
self.services.nodes.delete_node(unique_id, is_folder=is_folder)
|
||||||
|
return {
|
||||||
|
"success": True,
|
||||||
|
"message": f"{'Folder' if is_folder else 'Node'} '{unique_id}' deleted successfully.",
|
||||||
|
}
|
||||||
|
except Exception as e: # noqa: BLE001
|
||||||
|
return {"success": False, "error": str(e)}
|
||||||
|
|
||||||
|
# -------------------------------------------------------------------------
|
||||||
|
# Server Execution and Async Helper
|
||||||
|
# -------------------------------------------------------------------------
|
||||||
|
|
||||||
|
async def call_tool(self, name: str, arguments: dict | None = None) -> Any:
|
||||||
|
"""Call an MCP tool asynchronously via the underlying MCP server."""
|
||||||
|
if not self.server:
|
||||||
|
raise RuntimeError("MCP SDK is not initialized.")
|
||||||
|
return await self.server.call_tool(name, arguments or {})
|
||||||
|
|
||||||
|
def run(self, transport: str = "stdio", host: str = "127.0.0.1", port: int = 8000, **kwargs):
|
||||||
|
"""Run the MCP server using either stdio or sse transport."""
|
||||||
|
if not self.server:
|
||||||
|
raise RuntimeError("MCP server cannot start: 'mcp' package is not installed.")
|
||||||
|
|
||||||
|
if transport == "stdio":
|
||||||
|
# Redirect logs and printer outputs to sys.stderr for JSON-RPC
|
||||||
|
with contextlib.suppress(Exception):
|
||||||
|
from . import printer
|
||||||
|
|
||||||
|
printer.set_thread_stream(sys.stderr)
|
||||||
|
|
||||||
|
for handler in logging.root.handlers:
|
||||||
|
if hasattr(handler, "stream") and handler.stream == sys.stdout:
|
||||||
|
handler.stream = sys.stderr
|
||||||
|
logging.basicConfig(stream=sys.stderr, level=logging.INFO)
|
||||||
|
|
||||||
|
return self.server.run(transport="stdio", **kwargs)
|
||||||
|
|
||||||
|
if transport == "sse":
|
||||||
|
return self.server.run(transport="sse", host=host, port=port, **kwargs)
|
||||||
|
|
||||||
|
msg = f"Unknown transport: {transport}. Supported transports: 'stdio', 'sse'"
|
||||||
|
raise ValueError(msg)
|
||||||
@@ -71,7 +71,7 @@ class ImportExportService(BaseService):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
with open(file_path, "r") as f:
|
with open(file_path, "r") as f:
|
||||||
data = yaml.load(f, Loader=yaml.FullLoader)
|
data = yaml.safe_load(f)
|
||||||
self.import_from_dict(data)
|
self.import_from_dict(data)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise InvalidConfigurationError(f"Failed to read/parse import file: {e}")
|
raise InvalidConfigurationError(f"Failed to read/parse import file: {e}")
|
||||||
|
|||||||
@@ -0,0 +1,501 @@
|
|||||||
|
"""Unit tests for AI bugfixes, MethodHook instance isolation, and MCP multi-tenancy.
|
||||||
|
|
||||||
|
Covering specification: spec-02-ai-and-hooks-bugfixes.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import os
|
||||||
|
from unittest.mock import AsyncMock, MagicMock, patch
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from connpy.ai import PlaybookBuilderAgent, ai
|
||||||
|
from connpy.hooks import MethodHook
|
||||||
|
from connpy.mcp_client import MCPClientManager
|
||||||
|
|
||||||
|
# =========================================================================
|
||||||
|
# 1. AI Long-Term Memory Bugfix Tests
|
||||||
|
# =========================================================================
|
||||||
|
|
||||||
|
class TestAILongTermMemory:
|
||||||
|
"""Tests ensuring long_term_memory interpolation and updates work correctly."""
|
||||||
|
|
||||||
|
def test_long_term_memory_injected_without_template_literal(self, ai_config, mock_litellm):
|
||||||
|
"""Verify no double braces or unresolved '{self.long_term_memory' literals exist."""
|
||||||
|
myai = ai(ai_config)
|
||||||
|
assert "{self.long_term_memory" not in myai.engineer_system_prompt
|
||||||
|
assert "{{self.long_term_memory" not in myai.engineer_system_prompt
|
||||||
|
assert "{self.long_term_memory" not in myai.architect_system_prompt
|
||||||
|
assert "Network Context: Empty." in myai.engineer_system_prompt
|
||||||
|
assert "Network Context: Empty." in myai.architect_system_prompt
|
||||||
|
|
||||||
|
def test_manage_memory_tool_updates_prompts_immediately(self, ai_config, mock_litellm):
|
||||||
|
"""Verify calling manage_memory_tool immediately updates engineer and architect prompts."""
|
||||||
|
myai = ai(ai_config)
|
||||||
|
res = myai.manage_memory_tool("DC-East ASN is 65010", action="append")
|
||||||
|
assert "Memory updated successfully" in res
|
||||||
|
assert "DC-East ASN is 65010" in myai.long_term_memory
|
||||||
|
assert "DC-East ASN is 65010" in myai.engineer_system_prompt
|
||||||
|
assert "DC-East ASN is 65010" in myai.architect_system_prompt
|
||||||
|
assert "{self.long_term_memory" not in myai.engineer_system_prompt
|
||||||
|
|
||||||
|
def test_init_with_preexisting_memory_file(self, ai_config, mock_litellm):
|
||||||
|
"""Verify preexisting ai_memory.md content is loaded into prompts on startup."""
|
||||||
|
mem_file = os.path.join(ai_config.defaultdir, "ai_memory.md")
|
||||||
|
with open(mem_file, "w") as f:
|
||||||
|
f.write("Border routers running BGP EVPN")
|
||||||
|
|
||||||
|
myai = ai(ai_config)
|
||||||
|
assert "Border routers running BGP EVPN" in myai.long_term_memory
|
||||||
|
assert "Border routers running BGP EVPN" in myai.engineer_system_prompt
|
||||||
|
assert "Border routers running BGP EVPN" in myai.architect_system_prompt
|
||||||
|
|
||||||
|
|
||||||
|
# =========================================================================
|
||||||
|
# 2. AI has_architect and architect_auth Tests
|
||||||
|
# =========================================================================
|
||||||
|
|
||||||
|
class TestAIArchitectAuthAndTools:
|
||||||
|
"""Tests ensuring architect tools and authentication function with architect_auth."""
|
||||||
|
|
||||||
|
def test_engineer_tools_include_architect_when_auth_configured_without_key(self, ai_config, mock_litellm):
|
||||||
|
"""consult_architect & escalate_to_architect must be available with architect_auth even without key."""
|
||||||
|
ai_config.config["ai"]["architect_api_key"] = None
|
||||||
|
ai_config.config["ai"]["architect_auth"] = {"service_account": "sa@google.com"}
|
||||||
|
|
||||||
|
myai = ai(ai_config)
|
||||||
|
assert myai.architect_key is None
|
||||||
|
assert myai.has_architect is True
|
||||||
|
|
||||||
|
tools = myai._get_engineer_tools()
|
||||||
|
tool_names = [t["function"]["name"] for t in tools]
|
||||||
|
assert "consult_architect" in tool_names
|
||||||
|
assert "escalate_to_architect" in tool_names
|
||||||
|
|
||||||
|
def test_engineer_tools_include_architect_for_keyless_model(self, ai_config, mock_litellm):
|
||||||
|
"""Vertex AI / Ollama keyless models must have architect tools enabled."""
|
||||||
|
ai_config.config["ai"]["architect_api_key"] = None
|
||||||
|
ai_config.config["ai"]["architect_auth"] = {}
|
||||||
|
ai_config.config["ai"]["architect_model"] = "vertex_ai/gemini-1.5-pro"
|
||||||
|
|
||||||
|
myai = ai(ai_config)
|
||||||
|
assert myai.has_architect is True
|
||||||
|
tools = myai._get_engineer_tools()
|
||||||
|
tool_names = [t["function"]["name"] for t in tools]
|
||||||
|
assert "consult_architect" in tool_names
|
||||||
|
assert "escalate_to_architect" in tool_names
|
||||||
|
|
||||||
|
def test_consult_architect_passes_auth_kwargs_cleanly(self, ai_config):
|
||||||
|
"""consult_architect must pass dict(architect_auth) to litellm.completion."""
|
||||||
|
ai_config.config["ai"]["architect_api_key"] = None
|
||||||
|
ai_config.config["ai"]["architect_auth"] = {
|
||||||
|
"api_base": "https://custom-ai.internal/v1",
|
||||||
|
"custom_header": "test-val",
|
||||||
|
}
|
||||||
|
|
||||||
|
myai = ai(ai_config)
|
||||||
|
|
||||||
|
with patch("connpy.ai.completion") as mock_comp:
|
||||||
|
fake_resp = MagicMock()
|
||||||
|
fake_msg = MagicMock()
|
||||||
|
fake_msg.content = "Architect advice on spine-leaf"
|
||||||
|
fake_msg.tool_calls = None
|
||||||
|
fake_msg.role = "assistant"
|
||||||
|
fake_msg.model_dump.return_value = {"role": "assistant", "content": "Advice"}
|
||||||
|
fake_choice = MagicMock()
|
||||||
|
fake_choice.message = fake_msg
|
||||||
|
fake_resp.choices = [fake_choice]
|
||||||
|
fake_resp.usage = MagicMock()
|
||||||
|
fake_resp.usage.prompt_tokens = 10
|
||||||
|
fake_resp.usage.completion_tokens = 10
|
||||||
|
fake_resp.usage.total_tokens = 20
|
||||||
|
|
||||||
|
# Craft first completion call that instructs engineer to consult architect
|
||||||
|
tc = MagicMock()
|
||||||
|
tc.function.name = "consult_architect"
|
||||||
|
tc.function.arguments = '{"question": "How to design MTU?", "technical_summary": "9000 vs 1500"}'
|
||||||
|
eng_msg = MagicMock()
|
||||||
|
eng_msg.role = "assistant"
|
||||||
|
eng_msg.content = ""
|
||||||
|
eng_msg.tool_calls = [tc]
|
||||||
|
eng_msg.model_dump.return_value = {
|
||||||
|
"role": "assistant",
|
||||||
|
"content": None,
|
||||||
|
"tool_calls": [{
|
||||||
|
"id": "call_1",
|
||||||
|
"type": "function",
|
||||||
|
"function": {
|
||||||
|
"name": "consult_architect",
|
||||||
|
"arguments": '{"question": "How to design MTU?", "technical_summary": "9000 vs 1500"}',
|
||||||
|
},
|
||||||
|
}],
|
||||||
|
}
|
||||||
|
eng_choice = MagicMock()
|
||||||
|
eng_choice.message = eng_msg
|
||||||
|
eng_resp = MagicMock()
|
||||||
|
eng_resp.choices = [eng_choice]
|
||||||
|
eng_resp.usage = MagicMock()
|
||||||
|
eng_resp.usage.prompt_tokens = 5
|
||||||
|
eng_resp.usage.completion_tokens = 5
|
||||||
|
eng_resp.usage.total_tokens = 10
|
||||||
|
|
||||||
|
# Final response after consultation
|
||||||
|
final_msg = MagicMock()
|
||||||
|
final_msg.role = "assistant"
|
||||||
|
final_msg.content = "Here is the guidance."
|
||||||
|
final_msg.tool_calls = None
|
||||||
|
final_msg.model_dump.return_value = {"role": "assistant", "content": "Here is the guidance."}
|
||||||
|
final_choice = MagicMock()
|
||||||
|
final_choice.message = final_msg
|
||||||
|
final_resp = MagicMock()
|
||||||
|
final_resp.choices = [final_choice]
|
||||||
|
final_resp.usage = MagicMock()
|
||||||
|
final_resp.usage.prompt_tokens = 5
|
||||||
|
final_resp.usage.completion_tokens = 5
|
||||||
|
final_resp.usage.total_tokens = 10
|
||||||
|
|
||||||
|
mock_comp.side_effect = [eng_resp, fake_resp, final_resp]
|
||||||
|
|
||||||
|
res = myai.ask("Design MTU question", stream=False)
|
||||||
|
assert "guidance" in res["response"]
|
||||||
|
|
||||||
|
# Second call to completion was consult_architect
|
||||||
|
assert mock_comp.call_count >= 2
|
||||||
|
consult_call = mock_comp.call_args_list[1]
|
||||||
|
kwargs = consult_call.kwargs
|
||||||
|
assert kwargs.get("api_base") == "https://custom-ai.internal/v1"
|
||||||
|
assert kwargs.get("custom_header") == "test-val"
|
||||||
|
assert "api_key" not in kwargs
|
||||||
|
|
||||||
|
def test_consult_architect_injects_architect_key_if_missing_from_auth(self, ai_config):
|
||||||
|
"""If architect_key exists and 'api_key' is not in architect_auth, it must be added."""
|
||||||
|
ai_config.config["ai"]["architect_api_key"] = "arch-key-12345"
|
||||||
|
ai_config.config["ai"]["architect_auth"] = {"api_base": "https://custom-ai.internal/v1"}
|
||||||
|
|
||||||
|
myai = ai(ai_config)
|
||||||
|
|
||||||
|
with patch("connpy.ai.completion") as mock_comp:
|
||||||
|
fake_resp = MagicMock()
|
||||||
|
fake_msg = MagicMock()
|
||||||
|
fake_msg.content = "Advice"
|
||||||
|
fake_msg.tool_calls = None
|
||||||
|
fake_msg.role = "assistant"
|
||||||
|
fake_msg.model_dump.return_value = {"role": "assistant", "content": "Advice"}
|
||||||
|
fake_choice = MagicMock()
|
||||||
|
fake_choice.message = fake_msg
|
||||||
|
fake_resp.choices = [fake_choice]
|
||||||
|
fake_resp.usage = MagicMock()
|
||||||
|
fake_resp.usage.prompt_tokens = 10
|
||||||
|
fake_resp.usage.completion_tokens = 10
|
||||||
|
fake_resp.usage.total_tokens = 20
|
||||||
|
|
||||||
|
tc = MagicMock()
|
||||||
|
tc.function.name = "consult_architect"
|
||||||
|
tc.function.arguments = '{"question": "Q?", "technical_summary": "S"}'
|
||||||
|
eng_msg = MagicMock()
|
||||||
|
eng_msg.role = "assistant"
|
||||||
|
eng_msg.content = ""
|
||||||
|
eng_msg.tool_calls = [tc]
|
||||||
|
eng_msg.model_dump.return_value = {
|
||||||
|
"role": "assistant",
|
||||||
|
"content": None,
|
||||||
|
"tool_calls": [{
|
||||||
|
"id": "call_1",
|
||||||
|
"type": "function",
|
||||||
|
"function": {
|
||||||
|
"name": "consult_architect",
|
||||||
|
"arguments": '{"question": "Q?", "technical_summary": "S"}',
|
||||||
|
},
|
||||||
|
}],
|
||||||
|
}
|
||||||
|
eng_choice = MagicMock()
|
||||||
|
eng_choice.message = eng_msg
|
||||||
|
eng_resp = MagicMock()
|
||||||
|
eng_resp.choices = [eng_choice]
|
||||||
|
eng_resp.usage = MagicMock()
|
||||||
|
eng_resp.usage.prompt_tokens = 5
|
||||||
|
eng_resp.usage.completion_tokens = 5
|
||||||
|
eng_resp.usage.total_tokens = 10
|
||||||
|
|
||||||
|
final_msg = MagicMock()
|
||||||
|
final_msg.role = "assistant"
|
||||||
|
final_msg.content = "Done."
|
||||||
|
final_msg.tool_calls = None
|
||||||
|
final_msg.model_dump.return_value = {"role": "assistant", "content": "Done."}
|
||||||
|
final_choice = MagicMock()
|
||||||
|
final_choice.message = final_msg
|
||||||
|
final_resp = MagicMock()
|
||||||
|
final_resp.choices = [final_choice]
|
||||||
|
final_resp.usage = MagicMock()
|
||||||
|
final_resp.usage.prompt_tokens = 5
|
||||||
|
final_resp.usage.completion_tokens = 5
|
||||||
|
final_resp.usage.total_tokens = 10
|
||||||
|
|
||||||
|
mock_comp.side_effect = [eng_resp, fake_resp, final_resp]
|
||||||
|
|
||||||
|
myai.ask("Consult question", stream=False)
|
||||||
|
consult_call = mock_comp.call_args_list[1]
|
||||||
|
kwargs = consult_call.kwargs
|
||||||
|
assert kwargs.get("api_key") == "arch-key-12345"
|
||||||
|
assert kwargs.get("api_base") == "https://custom-ai.internal/v1"
|
||||||
|
|
||||||
|
|
||||||
|
# =========================================================================
|
||||||
|
# 3. PlaybookBuilderAgent Safe YAML Validation Tests
|
||||||
|
# =========================================================================
|
||||||
|
|
||||||
|
class TestPlaybookValidationSafeLoad:
|
||||||
|
"""Tests verifying PlaybookBuilderAgent.validate_playbook uses safe_load."""
|
||||||
|
|
||||||
|
def test_validate_playbook_rejects_malicious_yaml_tags(self, config):
|
||||||
|
"""Ensure arbitrary Python object deserialization payloads are safely rejected."""
|
||||||
|
agent = PlaybookBuilderAgent(config)
|
||||||
|
malicious_yaml = """
|
||||||
|
tasks:
|
||||||
|
- name: Exploit
|
||||||
|
command: !!python/object/apply:os.system ["touch /tmp/hacked_marker"]
|
||||||
|
"""
|
||||||
|
res = agent.validate_playbook(malicious_yaml)
|
||||||
|
assert res["valid"] is False
|
||||||
|
assert "YAML Syntax Error" in res["error"]
|
||||||
|
assert not os.path.exists("/tmp/hacked_marker")
|
||||||
|
|
||||||
|
def test_validate_playbook_accepts_valid_yaml(self, config):
|
||||||
|
"""Ensure safe YAML with tasks structure validates successfully."""
|
||||||
|
agent = PlaybookBuilderAgent(config)
|
||||||
|
valid_yaml = """
|
||||||
|
tasks:
|
||||||
|
- name: Check BGP
|
||||||
|
action: run
|
||||||
|
nodes: "router.*"
|
||||||
|
commands:
|
||||||
|
- show ip bgp summary
|
||||||
|
output: stdout
|
||||||
|
"""
|
||||||
|
res = agent.validate_playbook(valid_yaml)
|
||||||
|
assert res["valid"] is True
|
||||||
|
|
||||||
|
def test_validate_playbook_invalid_structures(self, config):
|
||||||
|
"""Ensure non-dict or missing 'tasks' YAML is rejected cleanly."""
|
||||||
|
agent = PlaybookBuilderAgent(config)
|
||||||
|
assert agent.validate_playbook("- not a dict\n- just a list")["valid"] is False
|
||||||
|
assert agent.validate_playbook("simple_scalar_string")["valid"] is False
|
||||||
|
assert agent.validate_playbook("other_root: 123")["valid"] is False
|
||||||
|
assert agent.validate_playbook("tasks: 'not a list'")["valid"] is False
|
||||||
|
|
||||||
|
|
||||||
|
# =========================================================================
|
||||||
|
# 4. MethodHook Instance Isolation and Deduplication Tests
|
||||||
|
# =========================================================================
|
||||||
|
|
||||||
|
class TestMethodHookIsolation:
|
||||||
|
"""Tests ensuring MethodHook instances are strictly isolated and deduplicated."""
|
||||||
|
|
||||||
|
def test_instance_hooks_do_not_contaminate_other_instances(self):
|
||||||
|
"""Registering a hook on inst1 must not run on inst2 or modify class hooks."""
|
||||||
|
class NetworkService:
|
||||||
|
@MethodHook
|
||||||
|
def run(self, cmd):
|
||||||
|
return f"executed:{cmd}"
|
||||||
|
|
||||||
|
inst1 = NetworkService()
|
||||||
|
inst2 = NetworkService()
|
||||||
|
|
||||||
|
def inst1_post_hook(*args, **kwargs):
|
||||||
|
return kwargs["result"] + " [audited-by-1]"
|
||||||
|
|
||||||
|
inst1.run.register_post_hook(inst1_post_hook)
|
||||||
|
|
||||||
|
assert inst1.run("show clock") == "executed:show clock [audited-by-1]"
|
||||||
|
assert inst2.run("show clock") == "executed:show clock"
|
||||||
|
assert NetworkService.run.post_hooks == []
|
||||||
|
|
||||||
|
def test_instance_hooks_deduplication(self):
|
||||||
|
"""Registering the same callable multiple times on an instance runs only once."""
|
||||||
|
class NetworkService:
|
||||||
|
@MethodHook
|
||||||
|
def run(self, cmd):
|
||||||
|
return f"res:{cmd}"
|
||||||
|
|
||||||
|
calls = []
|
||||||
|
|
||||||
|
def counting_hook(*args, **kwargs):
|
||||||
|
calls.append("called")
|
||||||
|
return kwargs["result"]
|
||||||
|
|
||||||
|
inst = NetworkService()
|
||||||
|
inst.run.register_post_hook(counting_hook)
|
||||||
|
inst.run.register_post_hook(counting_hook)
|
||||||
|
inst.run.register_post_hook(counting_hook)
|
||||||
|
|
||||||
|
res = inst.run("test")
|
||||||
|
assert res == "res:test"
|
||||||
|
assert calls == ["called"]
|
||||||
|
|
||||||
|
def test_class_level_and_instance_level_execution_order(self):
|
||||||
|
"""Class pre-hooks run before instance pre-hooks; class post-hooks run before instance post-hooks."""
|
||||||
|
class ExecutionDevice:
|
||||||
|
@MethodHook
|
||||||
|
def execute(self, val):
|
||||||
|
trace.append(f"base:{val}")
|
||||||
|
return val
|
||||||
|
|
||||||
|
trace = []
|
||||||
|
|
||||||
|
def class_pre(self, val):
|
||||||
|
trace.append("class_pre")
|
||||||
|
return (self, val), {}
|
||||||
|
|
||||||
|
def inst_pre(self, val):
|
||||||
|
trace.append("inst_pre")
|
||||||
|
return (self, val), {}
|
||||||
|
|
||||||
|
def class_post(*args, **kwargs):
|
||||||
|
trace.append("class_post")
|
||||||
|
return kwargs["result"]
|
||||||
|
|
||||||
|
def inst_post(*args, **kwargs):
|
||||||
|
trace.append("inst_post")
|
||||||
|
return kwargs["result"]
|
||||||
|
|
||||||
|
ExecutionDevice.execute.register_pre_hook(class_pre)
|
||||||
|
ExecutionDevice.execute.register_post_hook(class_post)
|
||||||
|
|
||||||
|
dev = ExecutionDevice()
|
||||||
|
dev.execute.register_pre_hook(inst_pre)
|
||||||
|
dev.execute.register_post_hook(inst_post)
|
||||||
|
|
||||||
|
dev.execute("ping")
|
||||||
|
assert trace == ["class_pre", "inst_pre", "base:ping", "class_post", "inst_post"]
|
||||||
|
|
||||||
|
def test_configfile_instance_isolation(self, config):
|
||||||
|
"""Verify two configfile instances do not leak hooks across instances."""
|
||||||
|
from connpy.configfile import configfile
|
||||||
|
conf2 = configfile(conf=config.file, key=config.key)
|
||||||
|
|
||||||
|
captured = []
|
||||||
|
|
||||||
|
def custom_hook(*args, **kwargs):
|
||||||
|
captured.append("c1_hook")
|
||||||
|
return kwargs["result"]
|
||||||
|
|
||||||
|
config._getallnodes.register_post_hook(custom_hook)
|
||||||
|
|
||||||
|
config._getallnodes(".*")
|
||||||
|
assert captured == ["c1_hook"]
|
||||||
|
|
||||||
|
# Call on second instance — hook must NOT execute
|
||||||
|
captured.clear()
|
||||||
|
conf2._getallnodes(".*")
|
||||||
|
assert captured == []
|
||||||
|
|
||||||
|
|
||||||
|
# =========================================================================
|
||||||
|
# 5. MCPClientManager Multi-Tenancy and Invalidation Tests
|
||||||
|
# =========================================================================
|
||||||
|
|
||||||
|
class DummyConfig:
|
||||||
|
"""Mock config object with config attribute and defaultdir."""
|
||||||
|
|
||||||
|
def __init__(self, defaultdir: str, mcp_servers: dict | None = None):
|
||||||
|
self.defaultdir = defaultdir
|
||||||
|
self.config = {
|
||||||
|
"ai": {
|
||||||
|
"mcp_servers": mcp_servers or {},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class TestMCPClientManagerMultiTenancy:
|
||||||
|
"""Tests ensuring MCPClientManager isolates tenants and invalidates cache properly."""
|
||||||
|
|
||||||
|
@pytest.fixture(autouse=True)
|
||||||
|
def reset_mcp_singletons(self):
|
||||||
|
"""Clean MCPClientManager instances before and after each test."""
|
||||||
|
MCPClientManager._instances.clear()
|
||||||
|
MCPClientManager._instance = None
|
||||||
|
yield
|
||||||
|
MCPClientManager._instances.clear()
|
||||||
|
MCPClientManager._instance = None
|
||||||
|
|
||||||
|
def test_mcp_manager_scoped_by_defaultdir(self):
|
||||||
|
"""Different defaultdir paths produce distinct isolated instances."""
|
||||||
|
cfg_tenant1 = DummyConfig("/var/connpy/tenant1")
|
||||||
|
cfg_tenant2 = DummyConfig("/var/connpy/tenant2")
|
||||||
|
|
||||||
|
mgr1 = MCPClientManager(cfg_tenant1)
|
||||||
|
mgr2 = MCPClientManager(cfg_tenant2)
|
||||||
|
|
||||||
|
assert mgr1 is not mgr2
|
||||||
|
assert mgr1.config is cfg_tenant1
|
||||||
|
assert mgr2.config is cfg_tenant2
|
||||||
|
|
||||||
|
def test_mcp_manager_same_scope_returns_same_instance_and_updates_config(self):
|
||||||
|
"""Same defaultdir reuses manager and updates self.config."""
|
||||||
|
cfg_a = DummyConfig("/var/connpy/shared")
|
||||||
|
cfg_b = DummyConfig("/var/connpy/shared")
|
||||||
|
|
||||||
|
mgr_a = MCPClientManager(cfg_a)
|
||||||
|
mgr_b = MCPClientManager(cfg_b)
|
||||||
|
|
||||||
|
assert mgr_a is mgr_b
|
||||||
|
assert mgr_a.config is cfg_b
|
||||||
|
|
||||||
|
def test_mcp_manager_scoped_by_id_when_no_defaultdir(self):
|
||||||
|
"""Configs without defaultdir are isolated by id(config)."""
|
||||||
|
cfg1 = object()
|
||||||
|
cfg2 = object()
|
||||||
|
|
||||||
|
mgr1 = MCPClientManager(cfg1)
|
||||||
|
mgr2 = MCPClientManager(cfg2)
|
||||||
|
|
||||||
|
assert mgr1 is not mgr2
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_get_tools_invalidates_cache_on_config_change(self):
|
||||||
|
"""When server config changes in mcp_servers, tool_cache is invalidated."""
|
||||||
|
cfg = DummyConfig(
|
||||||
|
"/tmp/test_mcp_invalidation",
|
||||||
|
mcp_servers={"srv1": {"url": "http://127.0.0.1:8001/sse", "enabled": True}},
|
||||||
|
)
|
||||||
|
|
||||||
|
mgr = MCPClientManager(cfg)
|
||||||
|
mgr.tool_cache["srv1"] = [{"type": "function", "function": {"name": "old_tool"}}]
|
||||||
|
mock_stack = AsyncMock()
|
||||||
|
mgr.sessions["srv1"] = {"session": MagicMock(), "stack": mock_stack}
|
||||||
|
mgr._server_configs["srv1"] = {"url": "http://127.0.0.1:8001/sse", "enabled": True}
|
||||||
|
|
||||||
|
# Modify config to point to new URL
|
||||||
|
cfg.config["ai"]["mcp_servers"]["srv1"] = {"url": "http://127.0.0.1:8002/sse", "enabled": True}
|
||||||
|
|
||||||
|
new_session = MagicMock()
|
||||||
|
with patch.object(mgr, "_ensure_connected", new_callable=AsyncMock) as mock_conn, \
|
||||||
|
patch.object(mgr, "_fetch_tools_as_openai", new_callable=AsyncMock) as mock_fetch:
|
||||||
|
mock_conn.return_value = new_session
|
||||||
|
mock_fetch.return_value = [{"type": "function", "function": {"name": "new_tool"}}]
|
||||||
|
|
||||||
|
tools = await mgr.get_tools_for_llm()
|
||||||
|
|
||||||
|
# Ensure old stack was closed
|
||||||
|
mock_stack.aclose.assert_awaited_once()
|
||||||
|
# Ensure new tools returned
|
||||||
|
assert tools == [{"type": "function", "function": {"name": "new_tool"}}]
|
||||||
|
assert mgr.tool_cache["srv1"] == [{"type": "function", "function": {"name": "new_tool"}}]
|
||||||
|
|
||||||
|
await mgr.shutdown()
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_shutdown_cleans_resources_and_resets(self):
|
||||||
|
"""shutdown() closes all stacks, clears caches and removes from _instances."""
|
||||||
|
cfg = DummyConfig("/tmp/test_mcp_shutdown")
|
||||||
|
|
||||||
|
mgr = MCPClientManager(cfg)
|
||||||
|
mock_stack = AsyncMock()
|
||||||
|
mgr.sessions["test"] = {"session": MagicMock(), "stack": mock_stack}
|
||||||
|
mgr.tool_cache["test"] = [{"dummy": 1}]
|
||||||
|
|
||||||
|
await mgr.shutdown()
|
||||||
|
|
||||||
|
mock_stack.aclose.assert_awaited_once()
|
||||||
|
assert mgr.sessions == {}
|
||||||
|
assert mgr.tool_cache == {}
|
||||||
|
assert os.path.abspath(cfg.defaultdir) not in MCPClientManager._instances
|
||||||
@@ -0,0 +1,413 @@
|
|||||||
|
"""Tests for ConnpyMCPServer (spec-03-mcp-server)."""
|
||||||
|
|
||||||
|
import sys
|
||||||
|
from unittest.mock import MagicMock, patch
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from connpy.mcp_server import ConnpyMCPServer
|
||||||
|
from connpy.services.provider import ServiceProvider
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def services(populated_config):
|
||||||
|
"""Provide ServiceProvider initialized with sample connections/profiles."""
|
||||||
|
return ServiceProvider(populated_config)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mcp_server(populated_config, services):
|
||||||
|
"""Provide ConnpyMCPServer in untrusted mode."""
|
||||||
|
return ConnpyMCPServer(config=populated_config, services=services, trust=False)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def trusted_mcp_server(populated_config, services):
|
||||||
|
"""Provide ConnpyMCPServer in trusted mode."""
|
||||||
|
return ConnpyMCPServer(config=populated_config, services=services, trust=True)
|
||||||
|
|
||||||
|
|
||||||
|
# =============================================================================
|
||||||
|
# Level 1 (Read-Only) Tools & Password Masking Tests
|
||||||
|
# =============================================================================
|
||||||
|
|
||||||
|
class TestLevel1Tools:
|
||||||
|
def test_list_nodes_all(self, mcp_server):
|
||||||
|
nodes = mcp_server.list_nodes()
|
||||||
|
assert isinstance(nodes, list)
|
||||||
|
assert "router1" in nodes
|
||||||
|
assert "server1@office" in nodes
|
||||||
|
assert "db1@datacenter@office" in nodes
|
||||||
|
|
||||||
|
def test_list_nodes_filtered(self, mcp_server):
|
||||||
|
nodes = mcp_server.list_nodes(filter_pattern="router.*")
|
||||||
|
assert "router1" in nodes
|
||||||
|
assert "server1@office" not in nodes
|
||||||
|
|
||||||
|
def test_list_nodes_formatted(self, mcp_server):
|
||||||
|
fmt = "{name} - {location}"
|
||||||
|
formatted = mcp_server.list_nodes(filter_pattern="router1", format_str=fmt)
|
||||||
|
assert len(formatted) == 1
|
||||||
|
assert formatted[0] == "router1 - root"
|
||||||
|
|
||||||
|
def test_list_folders(self, mcp_server):
|
||||||
|
folders = mcp_server.list_folders()
|
||||||
|
assert "@office" in folders
|
||||||
|
assert "@datacenter@office" in folders
|
||||||
|
|
||||||
|
def test_list_folders_filtered(self, mcp_server):
|
||||||
|
folders = mcp_server.list_folders(filter_pattern="datacenter")
|
||||||
|
assert "@datacenter@office" in folders
|
||||||
|
assert "@office" not in folders
|
||||||
|
|
||||||
|
def test_get_node_info_masks_password(self, mcp_server):
|
||||||
|
info = mcp_server.get_node_info("router1")
|
||||||
|
assert isinstance(info, dict)
|
||||||
|
assert info["host"] == "10.0.0.1"
|
||||||
|
assert info["user"] == "admin"
|
||||||
|
# CRITICAL requirement: ALWAYS mask password/passphrase/secret with "***"
|
||||||
|
assert info["password"] == "***"
|
||||||
|
assert "pass1" not in str(info)
|
||||||
|
|
||||||
|
def test_get_node_info_masks_nested_secrets(self, mcp_server):
|
||||||
|
test_data = {
|
||||||
|
"password": "plain_password",
|
||||||
|
"secret": "enable_secret",
|
||||||
|
"passphrase": "ssh_passphrase",
|
||||||
|
"api_token": "bearer_token",
|
||||||
|
"nested": {
|
||||||
|
"admin_password": "secret_nested",
|
||||||
|
"normal_key": "safe_value",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
masked = mcp_server._mask_secrets(test_data)
|
||||||
|
assert masked["password"] == "***"
|
||||||
|
assert masked["secret"] == "***"
|
||||||
|
assert masked["passphrase"] == "***"
|
||||||
|
assert masked["api_token"] == "***"
|
||||||
|
assert masked["nested"]["admin_password"] == "***"
|
||||||
|
assert masked["nested"]["normal_key"] == "safe_value"
|
||||||
|
|
||||||
|
def test_get_node_info_not_found(self, mcp_server):
|
||||||
|
res = mcp_server.get_node_info("non_existent_node")
|
||||||
|
assert "error" in res
|
||||||
|
assert "not found" in res["error"].lower()
|
||||||
|
|
||||||
|
def test_list_profiles(self, mcp_server):
|
||||||
|
profiles = mcp_server.list_profiles()
|
||||||
|
assert "default" in profiles
|
||||||
|
assert "office-user" in profiles
|
||||||
|
|
||||||
|
def test_list_profiles_filtered(self, mcp_server):
|
||||||
|
profiles = mcp_server.list_profiles(filter_pattern="office")
|
||||||
|
assert "office-user" in profiles
|
||||||
|
assert "default" not in profiles
|
||||||
|
|
||||||
|
def test_get_active_context(self, mcp_server):
|
||||||
|
ctx = mcp_server.get_active_context()
|
||||||
|
assert isinstance(ctx, dict)
|
||||||
|
assert "context" in ctx
|
||||||
|
assert ctx["context"] == "all"
|
||||||
|
assert "patterns" in ctx
|
||||||
|
assert ".*" in ctx["patterns"]
|
||||||
|
|
||||||
|
|
||||||
|
# =============================================================================
|
||||||
|
# Level 2 Security & Execution Tests (is_safe_command, run, test, playbooks)
|
||||||
|
# =============================================================================
|
||||||
|
|
||||||
|
class TestLevel2Tools:
|
||||||
|
def test_is_safe_command_allowed_patterns(self, mcp_server):
|
||||||
|
safe_samples = [
|
||||||
|
"show version",
|
||||||
|
"show ip route",
|
||||||
|
"ls -la /var/log",
|
||||||
|
"cat /etc/hosts",
|
||||||
|
"ip address",
|
||||||
|
"pwd",
|
||||||
|
"hostname",
|
||||||
|
"uname -a",
|
||||||
|
"df -h",
|
||||||
|
"free -m",
|
||||||
|
"ps aux",
|
||||||
|
"ping -c 4 8.8.8.8",
|
||||||
|
"traceroute 1.1.1.1",
|
||||||
|
"whois example.com",
|
||||||
|
"kubectl get pods -n default",
|
||||||
|
"systemctl status nginx",
|
||||||
|
"journalctl -u ssh -n 50",
|
||||||
|
]
|
||||||
|
for cmd in safe_samples:
|
||||||
|
assert mcp_server.is_safe_command(cmd) is True, f"Expected safe: {cmd}"
|
||||||
|
|
||||||
|
def test_is_safe_command_blocks_unsafe_in_untrusted_mode(self, mcp_server):
|
||||||
|
unsafe_samples = [
|
||||||
|
"reboot",
|
||||||
|
"rm -rf /",
|
||||||
|
"shutdown -h now",
|
||||||
|
"configure terminal",
|
||||||
|
"set system host-name bad",
|
||||||
|
"echo hacked",
|
||||||
|
"dd if=/dev/zero of=/dev/sda",
|
||||||
|
]
|
||||||
|
for cmd in unsafe_samples:
|
||||||
|
assert mcp_server.is_safe_command(cmd) is False, f"Expected unsafe: {cmd}"
|
||||||
|
|
||||||
|
def test_is_safe_command_blocks_injection_characters(self, mcp_server):
|
||||||
|
injection_samples = [
|
||||||
|
"show version; rm -rf /",
|
||||||
|
"show version && reboot",
|
||||||
|
"show version || reboot",
|
||||||
|
"cat /etc/hosts | grep root",
|
||||||
|
"show `whoami`",
|
||||||
|
"cat $(id)",
|
||||||
|
"echo test > /tmp/bad",
|
||||||
|
"cat < /etc/shadow",
|
||||||
|
"show ip\nreboot",
|
||||||
|
"show ip\rreboot",
|
||||||
|
"show version & reboot",
|
||||||
|
]
|
||||||
|
for cmd in injection_samples:
|
||||||
|
assert not mcp_server.is_safe_command(cmd), f"Injection allowed: {cmd}"
|
||||||
|
|
||||||
|
def test_is_safe_command_allows_all_in_trusted_mode(self, trusted_mcp_server):
|
||||||
|
commands = [
|
||||||
|
"reboot",
|
||||||
|
"rm -rf /",
|
||||||
|
"configure terminal",
|
||||||
|
"show version; reboot",
|
||||||
|
"cat /etc/hosts | grep foo",
|
||||||
|
]
|
||||||
|
for cmd in commands:
|
||||||
|
assert trusted_mcp_server.is_safe_command(cmd) is True
|
||||||
|
|
||||||
|
def test_run_commands_blocks_unsafe_when_untrusted(self, mcp_server):
|
||||||
|
res = mcp_server.run_commands("router1", ["show version", "reboot"])
|
||||||
|
assert res["success"] is False
|
||||||
|
assert "Permission denied" in res["error"]
|
||||||
|
assert "reboot" in res["error"]
|
||||||
|
|
||||||
|
def test_run_commands_executes_when_trusted(self, trusted_mcp_server):
|
||||||
|
exec_svc = trusted_mcp_server.services.execution
|
||||||
|
with patch.object(exec_svc, "run_commands") as mock_exec:
|
||||||
|
mock_exec.return_value = {"router1": {"output": "ok", "status": 0}}
|
||||||
|
res = trusted_mcp_server.run_commands("router1", ["reboot"])
|
||||||
|
assert res["success"] is True
|
||||||
|
assert res["results"]["router1"]["output"] == "ok"
|
||||||
|
mock_exec.assert_called_once()
|
||||||
|
|
||||||
|
def test_test_commands_blocks_unsafe_when_untrusted(self, mcp_server):
|
||||||
|
res = mcp_server.test_commands("router1", ["rm -rf /"], expected=["root"])
|
||||||
|
assert res["success"] is False
|
||||||
|
assert "Permission denied" in res["error"]
|
||||||
|
|
||||||
|
def test_test_commands_executes_when_trusted(self, trusted_mcp_server):
|
||||||
|
exec_svc = trusted_mcp_server.services.execution
|
||||||
|
with patch.object(exec_svc, "test_commands") as mock_test:
|
||||||
|
mock_test.return_value = {"router1": {"rm -rf /": True}}
|
||||||
|
res = trusted_mcp_server.test_commands(
|
||||||
|
"router1", ["rm -rf /"], expected=["root"]
|
||||||
|
)
|
||||||
|
assert res["success"] is True
|
||||||
|
assert res["results"]["router1"]["rm -rf /"] is True
|
||||||
|
|
||||||
|
def test_validate_playbook_valid(self, mcp_server):
|
||||||
|
valid_yaml = """
|
||||||
|
tasks:
|
||||||
|
- name: Check Status
|
||||||
|
action: run
|
||||||
|
nodes: router1
|
||||||
|
commands:
|
||||||
|
- show version
|
||||||
|
output: stdout
|
||||||
|
- name: Assert Interface
|
||||||
|
action: test
|
||||||
|
nodes: router1
|
||||||
|
commands:
|
||||||
|
- show ip int brief
|
||||||
|
expected:
|
||||||
|
- GigabitEthernet0/0
|
||||||
|
output: stdout
|
||||||
|
"""
|
||||||
|
res = mcp_server.validate_playbook(valid_yaml)
|
||||||
|
assert res["valid"] is True
|
||||||
|
|
||||||
|
def test_validate_playbook_invalid(self, mcp_server):
|
||||||
|
# Missing root tasks key
|
||||||
|
res1 = mcp_server.validate_playbook("name: invalid")
|
||||||
|
assert res1["valid"] is False
|
||||||
|
assert "tasks" in res1["error"]
|
||||||
|
|
||||||
|
# Missing expected in test action
|
||||||
|
invalid_yaml = """
|
||||||
|
tasks:
|
||||||
|
- name: Bad Test
|
||||||
|
action: test
|
||||||
|
nodes: router1
|
||||||
|
commands:
|
||||||
|
- show version
|
||||||
|
output: stdout
|
||||||
|
"""
|
||||||
|
res2 = mcp_server.validate_playbook(invalid_yaml)
|
||||||
|
assert res2["valid"] is False
|
||||||
|
assert "expected" in res2["error"]
|
||||||
|
|
||||||
|
def test_run_playbook_blocks_unsafe_commands_when_untrusted(self, mcp_server):
|
||||||
|
unsafe_playbook = """
|
||||||
|
tasks:
|
||||||
|
- name: Reboot Device
|
||||||
|
action: run
|
||||||
|
nodes: router1
|
||||||
|
commands:
|
||||||
|
- reboot
|
||||||
|
output: stdout
|
||||||
|
"""
|
||||||
|
res = mcp_server.run_playbook(unsafe_playbook)
|
||||||
|
assert res["success"] is False
|
||||||
|
assert "Permission denied" in res["error"]
|
||||||
|
assert "reboot" in res["error"]
|
||||||
|
|
||||||
|
def test_run_playbook_executes_when_trusted(self, trusted_mcp_server):
|
||||||
|
playbook = """
|
||||||
|
tasks:
|
||||||
|
- name: Safe Task
|
||||||
|
action: run
|
||||||
|
nodes: router1
|
||||||
|
commands:
|
||||||
|
- reboot
|
||||||
|
output: stdout
|
||||||
|
"""
|
||||||
|
exec_svc = trusted_mcp_server.services.execution
|
||||||
|
with patch.object(exec_svc, "run_commands") as mock_exec:
|
||||||
|
mock_exec.return_value = {"router1": {"output": "ok", "status": 0}}
|
||||||
|
res = trusted_mcp_server.run_playbook(playbook)
|
||||||
|
assert res["success"] is True
|
||||||
|
assert "Safe Task" in res["results"]
|
||||||
|
|
||||||
|
|
||||||
|
# =============================================================================
|
||||||
|
# Level 3 (CRUD) Inventory Mutation Tests
|
||||||
|
# =============================================================================
|
||||||
|
|
||||||
|
class TestLevel3CRUD:
|
||||||
|
def test_add_node_blocked_when_untrusted(self, mcp_server):
|
||||||
|
res = mcp_server.add_node("router99", {"host": "10.0.0.99"})
|
||||||
|
assert res["success"] is False
|
||||||
|
assert "Permission denied" in res["error"]
|
||||||
|
|
||||||
|
def test_add_node_allowed_when_trusted(self, trusted_mcp_server):
|
||||||
|
data = {"host": "10.0.0.99", "protocol": "ssh"}
|
||||||
|
res = trusted_mcp_server.add_node("router99", data)
|
||||||
|
assert res["success"] is True
|
||||||
|
nodes = trusted_mcp_server.list_nodes()
|
||||||
|
assert "router99" in nodes
|
||||||
|
|
||||||
|
def test_update_node_blocked_when_untrusted(self, mcp_server):
|
||||||
|
res = mcp_server.update_node("router1", {"host": "10.0.0.254"})
|
||||||
|
assert res["success"] is False
|
||||||
|
assert "Permission denied" in res["error"]
|
||||||
|
|
||||||
|
def test_update_node_allowed_when_trusted(self, trusted_mcp_server):
|
||||||
|
res = trusted_mcp_server.update_node("router1", {"host": "10.0.0.254"})
|
||||||
|
assert res["success"] is True
|
||||||
|
info = trusted_mcp_server.get_node_info("router1")
|
||||||
|
assert info["host"] == "10.0.0.254"
|
||||||
|
|
||||||
|
def test_delete_node_blocked_when_untrusted(self, mcp_server):
|
||||||
|
res = mcp_server.delete_node("router1")
|
||||||
|
assert res["success"] is False
|
||||||
|
assert "Permission denied" in res["error"]
|
||||||
|
|
||||||
|
def test_delete_node_allowed_when_trusted(self, trusted_mcp_server):
|
||||||
|
res = trusted_mcp_server.delete_node("router1")
|
||||||
|
assert res["success"] is True
|
||||||
|
nodes = trusted_mcp_server.list_nodes()
|
||||||
|
assert "router1" not in nodes
|
||||||
|
|
||||||
|
|
||||||
|
# =============================================================================
|
||||||
|
# Transport & Logging Redirection Tests
|
||||||
|
# =============================================================================
|
||||||
|
|
||||||
|
class TestTransportAndRedirection:
|
||||||
|
def test_stdio_redirects_stdout_and_calls_run(self, mcp_server):
|
||||||
|
with patch.object(mcp_server.server, "run") as mock_run, \
|
||||||
|
patch("connpy.printer.set_thread_stream") as mock_set_stream:
|
||||||
|
mcp_server.run(transport="stdio")
|
||||||
|
mock_set_stream.assert_called_once_with(sys.stderr)
|
||||||
|
mock_run.assert_called_once_with(transport="stdio")
|
||||||
|
|
||||||
|
def test_sse_passes_host_and_port(self, mcp_server):
|
||||||
|
with patch.object(mcp_server.server, "run") as mock_run:
|
||||||
|
mcp_server.run(transport="sse", host="0.0.0.0", port=9090)
|
||||||
|
mock_run.assert_called_once_with(transport="sse", host="0.0.0.0", port=9090)
|
||||||
|
|
||||||
|
|
||||||
|
# =============================================================================
|
||||||
|
# Async MCP Protocol Tool Invocation Tests
|
||||||
|
# =============================================================================
|
||||||
|
|
||||||
|
class TestMCPProtocolAsyncCall:
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_async_call_tool_list_nodes(self, mcp_server):
|
||||||
|
res = await mcp_server.call_tool("list_nodes", {"filter_pattern": "router1"})
|
||||||
|
assert not res.is_error
|
||||||
|
text = res.content[0].text
|
||||||
|
assert "router1" in text
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_async_call_tool_get_node_info_masks_password(self, mcp_server):
|
||||||
|
res = await mcp_server.call_tool("get_node_info", {"node_name": "router1"})
|
||||||
|
assert not res.is_error
|
||||||
|
text = res.content[0].text
|
||||||
|
assert '"password": "***"' in text
|
||||||
|
assert "pass1" not in text
|
||||||
|
|
||||||
|
|
||||||
|
# =============================================================================
|
||||||
|
# CLI Dispatch & Completion Tests
|
||||||
|
# =============================================================================
|
||||||
|
|
||||||
|
class TestCLIDispatchAndCompletion:
|
||||||
|
def test_ai_handler_configure_mcp_serve_dispatch(self, populated_config, services):
|
||||||
|
from connpy.cli.ai_handler import AIHandler
|
||||||
|
|
||||||
|
class DummyApp:
|
||||||
|
def __init__(self, conf, svcs):
|
||||||
|
self.config = conf
|
||||||
|
self.services = svcs
|
||||||
|
|
||||||
|
app = DummyApp(populated_config, services)
|
||||||
|
handler = AIHandler(app)
|
||||||
|
|
||||||
|
args = MagicMock()
|
||||||
|
args.mcp = ["serve", "--sse", "--port", "9191", "--trust"]
|
||||||
|
args.trust = False
|
||||||
|
|
||||||
|
with patch("connpy.mcp_server.ConnpyMCPServer.run") as mock_run:
|
||||||
|
handler.configure_mcp(args)
|
||||||
|
mock_run.assert_called_once_with(
|
||||||
|
transport="sse", host="127.0.0.1", port=9191
|
||||||
|
)
|
||||||
|
|
||||||
|
def test_completion_contains_serve_flags(self):
|
||||||
|
from connpy.completion import _build_tree, resolve_completion
|
||||||
|
tree = _build_tree([], [], [], {}, "/tmp")
|
||||||
|
|
||||||
|
# Verify tree hierarchy
|
||||||
|
assert "serve" in tree["ai"]["--mcp"]
|
||||||
|
serve_sub = tree["ai"]["--mcp"]["serve"]
|
||||||
|
assert "--sse" in serve_sub
|
||||||
|
assert "--port" in serve_sub
|
||||||
|
assert "--trust" in serve_sub
|
||||||
|
|
||||||
|
# Verify completions resolution
|
||||||
|
comp_base = resolve_completion(["ai", "--mcp", "serve", ""], tree)
|
||||||
|
assert "--sse" in comp_base
|
||||||
|
assert "--port" in comp_base
|
||||||
|
assert "--trust" in comp_base
|
||||||
|
|
||||||
|
# After --sse, remaining flags are suggested
|
||||||
|
comp_after_sse = resolve_completion(["ai", "--mcp", "serve", "--sse", ""], tree)
|
||||||
|
assert "--port" in comp_after_sse
|
||||||
|
assert "--trust" in comp_after_sse
|
||||||
@@ -0,0 +1,222 @@
|
|||||||
|
"""Tests for YAML safe deserialization and connection options sanitization with allow_unsafe_options."""
|
||||||
|
|
||||||
|
from unittest.mock import MagicMock, patch
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from connpy.cli.run_handler import RunHandler
|
||||||
|
from connpy.core import node
|
||||||
|
from connpy.grpc_layer.server import ImportExportServicer
|
||||||
|
from connpy.services.exceptions import InvalidConfigurationError
|
||||||
|
from connpy.services.import_export_service import ImportExportService
|
||||||
|
|
||||||
|
# =========================================================================
|
||||||
|
# 1. Tests de Deserialización YAML Segura
|
||||||
|
# =========================================================================
|
||||||
|
|
||||||
|
|
||||||
|
class TestYamlSafeDeserialization:
|
||||||
|
def test_import_export_service_rejects_unsafe_yaml(self, config, tmp_path):
|
||||||
|
"""YAML with !!python/object/apply must fail with InvalidConfigurationError and never execute."""
|
||||||
|
malicious_yaml = tmp_path / "malicious.yaml"
|
||||||
|
malicious_yaml.write_text('!!python/object/apply:os.system ["echo pwned"]')
|
||||||
|
|
||||||
|
service = ImportExportService(config)
|
||||||
|
|
||||||
|
with patch("os.system") as mock_system:
|
||||||
|
with pytest.raises(InvalidConfigurationError):
|
||||||
|
service.import_from_file(str(malicious_yaml))
|
||||||
|
mock_system.assert_not_called()
|
||||||
|
|
||||||
|
def test_grpc_import_from_file_rejects_unsafe_yaml(self):
|
||||||
|
"""gRPC import with !!python/object/apply must abort via context and never execute."""
|
||||||
|
payload = '---YAML---\n!!python/object/apply:os.system ["echo pwned"]'
|
||||||
|
request = MagicMock()
|
||||||
|
request.value = payload
|
||||||
|
context = MagicMock()
|
||||||
|
|
||||||
|
provider = MagicMock()
|
||||||
|
servicer = ImportExportServicer(provider)
|
||||||
|
|
||||||
|
with patch("os.system") as mock_system:
|
||||||
|
servicer.import_from_file(request, context)
|
||||||
|
context.abort.assert_called_once()
|
||||||
|
mock_system.assert_not_called()
|
||||||
|
|
||||||
|
def test_run_handler_yaml_run_rejects_unsafe_yaml(self, tmp_path):
|
||||||
|
"""RunHandler.yaml_run with !!python/object/apply exits cleanly with code 10 and never executes."""
|
||||||
|
malicious_playbook = tmp_path / "bad_playbook.yaml"
|
||||||
|
malicious_playbook.write_text(
|
||||||
|
'tasks:\n - name: exploit\n action: run\n nodes: all\n commands:\n - !!python/object/apply:os.system ["echo pwned"]\n'
|
||||||
|
)
|
||||||
|
|
||||||
|
app = MagicMock()
|
||||||
|
handler = RunHandler(app)
|
||||||
|
args = MagicMock()
|
||||||
|
args.data = [str(malicious_playbook)]
|
||||||
|
args.preflight_ai = False
|
||||||
|
args.analyze = None
|
||||||
|
|
||||||
|
with patch("os.system") as mock_system:
|
||||||
|
with pytest.raises(SystemExit) as exc_info:
|
||||||
|
handler.yaml_run(args)
|
||||||
|
assert exc_info.value.code == 10
|
||||||
|
mock_system.assert_not_called()
|
||||||
|
|
||||||
|
def test_run_handler_yaml_run_rejects_non_dict_yaml(self, tmp_path):
|
||||||
|
"""RunHandler.yaml_run with empty or non-dict YAML exits cleanly with code 10."""
|
||||||
|
non_dict_playbook = tmp_path / "list_playbook.yaml"
|
||||||
|
non_dict_playbook.write_text("- item1\n- item2\n")
|
||||||
|
|
||||||
|
app = MagicMock()
|
||||||
|
handler = RunHandler(app)
|
||||||
|
args = MagicMock()
|
||||||
|
args.data = [str(non_dict_playbook)]
|
||||||
|
args.preflight_ai = False
|
||||||
|
args.analyze = None
|
||||||
|
|
||||||
|
with pytest.raises(SystemExit) as exc_info:
|
||||||
|
handler.yaml_run(args)
|
||||||
|
assert exc_info.value.code == 10
|
||||||
|
|
||||||
|
|
||||||
|
# =========================================================================
|
||||||
|
# 2. Tests Negativos de Inyección en Protocolos (allow_unsafe_options=False)
|
||||||
|
# =========================================================================
|
||||||
|
|
||||||
|
|
||||||
|
class TestProtocolInjectionNegative:
|
||||||
|
@pytest.mark.parametrize("proto", ["ssh", "sftp", "telnet", "kubectl", "docker", "ssm"])
|
||||||
|
def test_host_starting_with_dash_raises_value_error(self, proto):
|
||||||
|
"""Host starting with '-' raises ValueError across all protocols."""
|
||||||
|
n = node("bad_node", "-oProxyCommand=id", protocol=proto)
|
||||||
|
with pytest.raises(ValueError, match="Host cannot start with '-'"):
|
||||||
|
n._get_cmd()
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("proto", ["ssh", "sftp", "telnet", "kubectl", "docker", "ssm"])
|
||||||
|
def test_host_with_unsafe_characters_raises_value_error(self, proto):
|
||||||
|
"""Host containing whitespace, control characters or shell metacharacters raises ValueError."""
|
||||||
|
for bad_host in ["host; id", "host | id", "host`id`", "host$id", "host&id"]:
|
||||||
|
n = node("bad_node", bad_host, protocol=proto)
|
||||||
|
with pytest.raises(ValueError, match="Invalid characters in host"):
|
||||||
|
n._get_cmd()
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("proto", ["ssh", "sftp", "telnet", "kubectl", "docker", "ssm"])
|
||||||
|
def test_user_starting_with_dash_raises_value_error(self, proto):
|
||||||
|
"""User starting with '-' raises ValueError across all protocols."""
|
||||||
|
n = node("n1", "10.0.0.1", user="-oProxyCommand=id", protocol=proto)
|
||||||
|
with pytest.raises(ValueError, match="User cannot start with '-'"):
|
||||||
|
n._get_cmd()
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("proto", ["ssh", "sftp", "telnet", "kubectl", "docker", "ssm"])
|
||||||
|
def test_user_with_unsafe_characters_raises_value_error(self, proto):
|
||||||
|
"""User containing shell metacharacters raises ValueError across all protocols."""
|
||||||
|
for bad_user in ["; id", "user | id", "user`id`", "user$id", "user&id"]:
|
||||||
|
n = node("n1", "10.0.0.1", user=bad_user, protocol=proto)
|
||||||
|
with pytest.raises(ValueError, match="Invalid characters in user"):
|
||||||
|
n._get_cmd()
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("proto", ["ssh", "sftp", "telnet"])
|
||||||
|
def test_invalid_port_raises_value_error(self, proto):
|
||||||
|
"""Invalid port in SSH/SFTP/Telnet raises ValueError."""
|
||||||
|
for bad_port in ["22 -oProxyCommand=id", "99999", "abc", "0", "-1"]:
|
||||||
|
n = node("n1", "10.0.0.1", port=bad_port, protocol=proto)
|
||||||
|
with pytest.raises(ValueError, match="Invalid TCP SSH port"):
|
||||||
|
n._get_cmd()
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("proto", ["ssh", "sftp"])
|
||||||
|
def test_dangerous_ssh_directives_in_options_raise_value_error(self, proto):
|
||||||
|
"""Directives like ProxyCommand, LocalCommand, etc. in options raise ValueError."""
|
||||||
|
dangerous_opts = [
|
||||||
|
'-o ProxyCommand="sh -c id"',
|
||||||
|
"-oProxyCommand=id",
|
||||||
|
'-o LocalCommand="id"',
|
||||||
|
"-o PermitLocalCommand=yes",
|
||||||
|
'-o KnownHostsCommand="id"',
|
||||||
|
'-o "ProxyCommand=id"',
|
||||||
|
"-o proxycommand=id",
|
||||||
|
]
|
||||||
|
for opt in dangerous_opts:
|
||||||
|
n = node("n1", "10.0.0.1", options=opt, protocol=proto)
|
||||||
|
with pytest.raises(ValueError, match="Disallowed SSH directive in options"):
|
||||||
|
n._get_cmd()
|
||||||
|
|
||||||
|
@pytest.mark.parametrize("proto", ["ssh", "sftp", "telnet", "kubectl", "docker", "ssm"])
|
||||||
|
def test_unbalanced_quotes_in_options_raise_value_error(self, proto):
|
||||||
|
"""Unbalanced quotes in options raise ValueError."""
|
||||||
|
n = node("n1", "10.0.0.1", options='-o "StrictHostKeyChecking=no', protocol=proto)
|
||||||
|
with pytest.raises(ValueError, match="unbalanced quotes"):
|
||||||
|
n._get_cmd()
|
||||||
|
|
||||||
|
def test_ssm_invalid_region_or_profile_tags_raise_value_error(self):
|
||||||
|
"""SSM tags 'region' and 'profile' reject leading dash and shell metacharacters."""
|
||||||
|
n_reg = node("ssm1", "i-1234567890abcdef0", protocol="ssm", tags={"region": "us-east-1; id"})
|
||||||
|
with pytest.raises(ValueError, match="Invalid characters in SSM tag"):
|
||||||
|
n_reg._get_cmd()
|
||||||
|
|
||||||
|
n_prof = node("ssm2", "i-1234567890abcdef0", protocol="ssm", tags={"profile": "-bad"})
|
||||||
|
with pytest.raises(ValueError, match="cannot start with '-'"):
|
||||||
|
n_prof._get_cmd()
|
||||||
|
|
||||||
|
def test_container_command_validation(self):
|
||||||
|
"""Kube and Docker commands reject newlines and unbalanced quotes."""
|
||||||
|
n_kube = node("k1", "pod-1", protocol="kubectl", tags={"kube_command": 'sh -c "echo\nid"'})
|
||||||
|
with pytest.raises(ValueError, match="cannot contain newlines or null bytes"):
|
||||||
|
n_kube._get_cmd()
|
||||||
|
|
||||||
|
n_kube_q = node("k2", "pod-1", protocol="kubectl", tags={"kube_command": 'sh -c "unclosed'})
|
||||||
|
with pytest.raises(ValueError, match="unbalanced quotes"):
|
||||||
|
n_kube_q._get_cmd()
|
||||||
|
|
||||||
|
n_doc = node("d1", "cont-1", protocol="docker", tags={"docker_command": 'sh -c "echo\nid"'})
|
||||||
|
with pytest.raises(ValueError, match="cannot contain newlines or null bytes"):
|
||||||
|
n_doc._get_cmd()
|
||||||
|
|
||||||
|
n_doc_q = node("d2", "cont-1", protocol="docker", tags={"docker_command": 'sh -c "unclosed'})
|
||||||
|
with pytest.raises(ValueError, match="unbalanced quotes"):
|
||||||
|
n_doc_q._get_cmd()
|
||||||
|
|
||||||
|
|
||||||
|
# =========================================================================
|
||||||
|
# 3. Tests Positivos de Regresión y allow_unsafe_options=True
|
||||||
|
# =========================================================================
|
||||||
|
|
||||||
|
|
||||||
|
class TestPositiveAndAllowUnsafeOptions:
|
||||||
|
def test_ssh_jumphost_proxycommand_allowed_when_safe_mode(self, populated_config):
|
||||||
|
"""Valid jumphost chain still generates -o ProxyCommand correctly when allow_unsafe_options=False."""
|
||||||
|
# populated_config has 'router1'
|
||||||
|
n = node("target", "10.0.0.2", protocol="ssh", jumphost="router1", config=populated_config)
|
||||||
|
cmd = n._get_cmd()
|
||||||
|
assert '-o ProxyCommand="' in cmd
|
||||||
|
assert "ssh -W %h:%p" in cmd
|
||||||
|
assert "admin@10.0.0.1" in cmd
|
||||||
|
|
||||||
|
def test_ssh_safe_options_permitted(self):
|
||||||
|
"""Benign SSH options work without issue."""
|
||||||
|
safe_opts = "-o StrictHostKeyChecking=no -i ~/.ssh/id_rsa -L 8080:localhost:80"
|
||||||
|
n = node("s1", "10.0.0.1", protocol="ssh", options=safe_opts)
|
||||||
|
cmd = n._get_cmd()
|
||||||
|
assert safe_opts in cmd
|
||||||
|
|
||||||
|
def test_user_domain_and_ipv6_bracketed_permitted(self):
|
||||||
|
"""Formats like DOMAIN\\user and bracketed IPv6 [2001:db8::1] remain supported."""
|
||||||
|
n = node("s1", "[2001:db8::1]", user=r"DOMAIN\user", protocol="ssh", port="22")
|
||||||
|
cmd = n._get_cmd()
|
||||||
|
assert r"DOMAIN\user@[2001:db8::1]" in cmd
|
||||||
|
assert "-p 22" in cmd
|
||||||
|
|
||||||
|
def test_allow_unsafe_options_via_config(self, populated_config):
|
||||||
|
"""When config.config['allow_unsafe_options'] is True, ProxyCommand in options is allowed."""
|
||||||
|
populated_config.config["allow_unsafe_options"] = True
|
||||||
|
custom_proxy = '-o ProxyCommand="ssh -W %h:%p bastion"'
|
||||||
|
n = node("s1", "10.0.0.1", protocol="ssh", options=custom_proxy, config=populated_config)
|
||||||
|
cmd = n._get_cmd()
|
||||||
|
assert custom_proxy in cmd
|
||||||
|
|
||||||
|
def test_allow_unsafe_options_via_kwarg(self):
|
||||||
|
"""When allow_unsafe_options=True passed to node, dangerous options are allowed."""
|
||||||
|
custom_proxy = '-o ProxyCommand="ssh -W %h:%p bastion"'
|
||||||
|
n = node("s1", "10.0.0.1", protocol="ssh", options=custom_proxy, allow_unsafe_options=True)
|
||||||
|
cmd = n._get_cmd()
|
||||||
|
assert custom_proxy in cmd
|
||||||
+13
-5
@@ -41,7 +41,7 @@ el.replaceWith(d);
|
|||||||
<p align="center">
|
<p align="center">
|
||||||
<img src="https://nginx.gederico.dynu.net/images/CONNPY-resized.png" alt="App Logo">
|
<img src="https://nginx.gederico.dynu.net/images/CONNPY-resized.png" alt="App Logo">
|
||||||
</p>
|
</p>
|
||||||
<h1 id="connpy-v640b2">Connpy (v6.4.0b2)</h1>
|
<h1 id="connpy-v640b3">Connpy (v6.4.0b3)</h1>
|
||||||
<p><a href="https://pypi.org/pypi/connpy/"><img alt="" src="https://img.shields.io/pypi/v/connpy.svg?style=flat-square"></a>
|
<p><a href="https://pypi.org/pypi/connpy/"><img alt="" src="https://img.shields.io/pypi/v/connpy.svg?style=flat-square"></a>
|
||||||
<a href="https://pypi.org/pypi/connpy/"><img alt="" src="https://img.shields.io/pypi/pyversions/connpy.svg?style=flat-square"></a>
|
<a href="https://pypi.org/pypi/connpy/"><img alt="" src="https://img.shields.io/pypi/pyversions/connpy.svg?style=flat-square"></a>
|
||||||
<a href="https://pypi.org/pypi/connpy/"><img alt="" src="https://img.shields.io/pypi/dm/connpy.svg?style=flat-square&cacheSeconds=86400"></a>
|
<a href="https://pypi.org/pypi/connpy/"><img alt="" src="https://img.shields.io/pypi/dm/connpy.svg?style=flat-square&cacheSeconds=86400"></a>
|
||||||
@@ -69,9 +69,17 @@ el.replaceWith(d);
|
|||||||
<p>Start a standalone persistent session with the AI Copilot. Manage sessions using <code>--list</code>, <code>--resume</code>, <code>--session <id></code> (to restore a specific history), <code>--delete <id></code>, or send a quick single-shot question directly from the terminal prompt:</p>
|
<p>Start a standalone persistent session with the AI Copilot. Manage sessions using <code>--list</code>, <code>--resume</code>, <code>--session <id></code> (to restore a specific history), <code>--delete <id></code>, or send a quick single-shot question directly from the terminal prompt:</p>
|
||||||
<pre><code class="language-bash">conn ai "how do i check bgp summary on cisco?"
|
<pre><code class="language-bash">conn ai "how do i check bgp summary on cisco?"
|
||||||
</code></pre>
|
</code></pre>
|
||||||
<h3 id="1c-mcp-integration">1c. MCP Integration</h3>
|
<h3 id="1c-mcp-integration-client-server">1c. MCP Integration (Client & Server)</h3>
|
||||||
<p>Connect to external data sources and tools dynamically via the Model Context Protocol (MCP). Use the interactive wizard or command actions to configure MCP servers:</p>
|
<p>Connect to external data sources and tools dynamically, or expose <code><a title="connpy" href="#connpy">connpy</a></code> as a native MCP server for external agents (Claude Desktop, Cursor, IDEs):</p>
|
||||||
<pre><code class="language-bash">conn ai --mcp
|
<pre><code class="language-bash"># MCP Client: Manage connected MCP servers
|
||||||
|
conn ai --mcp # Interactive configuration wizard
|
||||||
|
conn ai --mcp list # List configured MCP servers
|
||||||
|
conn ai --mcp add <name> <url> [os] # Add a remote SSE MCP server
|
||||||
|
|
||||||
|
# MCP Server: Serve connpy inventory, diagnostic tools, and playbooks
|
||||||
|
conn ai --mcp serve # Standard I/O mode (default, for local clients)
|
||||||
|
conn ai --mcp serve --sse --port 8080 # Remote SSE mode
|
||||||
|
conn ai --mcp serve --trust # Enable inventory mutations (CRUD) & configuration commands
|
||||||
</code></pre>
|
</code></pre>
|
||||||
<h3 id="1d-local-interactive-shell-conn-shell">1d. Local Interactive Shell (conn shell)</h3>
|
<h3 id="1d-local-interactive-shell-conn-shell">1d. Local Interactive Shell (conn shell)</h3>
|
||||||
<p>Launch a local interactive shell with AI Copilot support enabled directly on your host machine:</p>
|
<p>Launch a local interactive shell with AI Copilot support enabled directly on your host machine:</p>
|
||||||
@@ -4598,7 +4606,7 @@ def test(self, commands, expected, vars = None,*, folder = None, prompt = None,
|
|||||||
<nav id="sidebar">
|
<nav id="sidebar">
|
||||||
<div class="toc">
|
<div class="toc">
|
||||||
<ul>
|
<ul>
|
||||||
<li><a href="#connpy-v640b2">Connpy (v6.4.0b2)</a><ul>
|
<li><a href="#connpy-v640b3">Connpy (v6.4.0b3)</a><ul>
|
||||||
<li><a href="#1-ai-system">1. 🤖 AI System</a><ul>
|
<li><a href="#1-ai-system">1. 🤖 AI System</a><ul>
|
||||||
<li><a href="#1a-terminal-copilot-ctrlspace">1a. Terminal Copilot (Ctrl+Space)</a></li>
|
<li><a href="#1a-terminal-copilot-ctrlspace">1a. Terminal Copilot (Ctrl+Space)</a></li>
|
||||||
<li><a href="#1b-ai-chat-conn-ai">1b. AI Chat (conn ai)</a></li>
|
<li><a href="#1b-ai-chat-conn-ai">1b. AI Chat (conn ai)</a></li>
|
||||||
|
|||||||
Reference in new issue
Block a user