Buckets:

download
raw
11.9 kB
"""Plugin system for progressive enhancement of Nexus.
This module implements a plugin architecture that allows users to
progressively enhance their Nexus instance with additional features
like authentication, monitoring, and enterprise capabilities.
"""
import importlib
import logging
import os
from abc import ABC, abstractmethod
from pathlib import Path
from typing import Any, Callable, Dict, List, Optional, Protocol
logger = logging.getLogger(__name__)
class NexusPlugin(ABC):
"""Base class for Nexus plugins."""
@property
@abstractmethod
def name(self) -> str:
"""Plugin name."""
pass
@property
@abstractmethod
def description(self) -> str:
"""Plugin description."""
pass
@abstractmethod
def apply(self, nexus_instance: Any) -> None:
"""Apply plugin to the Nexus instance.
Args:
nexus_instance: The Nexus instance to enhance
"""
pass
def validate(self) -> bool:
"""Validate plugin can be applied.
Performs basic validation checks:
- Plugin has valid name
- Plugin has callable apply method
- Subclasses can override for additional validation
Returns:
True if plugin can be applied, False otherwise
"""
# Validate plugin has a name
try:
name = self.name
if not name or not isinstance(name, str):
logger.error(f"Plugin validation failed: invalid name '{name}'")
return False
except Exception as e:
logger.error(f"Plugin validation failed: unable to get name - {e}")
return False
# Validate plugin has apply method
if not hasattr(self, "apply") or not callable(getattr(self, "apply", None)):
logger.error(
f"Plugin '{name}' validation failed: missing or invalid apply method"
)
return False
# Validation passed
logger.debug(f"Plugin '{name}' validation passed")
return True
class AuthPlugin(NexusPlugin):
"""Authentication plugin using SDK's MiddlewareAuthManager."""
@property
def name(self) -> str:
return "auth"
@property
def description(self) -> str:
return "Enables authentication using SDK enterprise auth components"
def apply(self, nexus_instance: Any) -> None:
"""Install real HTTP authentication middleware on the Nexus app.
Before #2013 this built a ``MiddlewareAuthManager`` and then handed it
to the gateway behind ``if hasattr(gateway, "set_auth_manager")``.
``set_auth_manager`` has ZERO definitions in this repo or any installed
dependency, so the guard was permanently False: the manager was
constructed, dropped on the floor, and the plugin reported "SDK-based
authentication enabled" over a completely open API. That is the
structural-``hasattr``-resolving-False silent fallback enumerated in
``rules/zero-tolerance.md`` Rule 3d.
The install now goes through :func:`nexus.auth_bootstrap.
install_auth_middleware`, which adds a Starlette middleware that
actually rejects unauthenticated requests -- and raises when it cannot,
rather than reporting success.
Raises:
AuthNotConfiguredError: No credential source configured, or the
Nexus instance has no gateway to protect.
"""
logger.info("Applying authentication plugin")
from nexus.auth_bootstrap import install_auth_middleware
install_auth_middleware(nexus_instance)
# Preserve the previously-published attribute for callers that read it.
# It is populated only when the optional middleware extra is present;
# the auth control itself no longer depends on it.
if getattr(nexus_instance, "_auth_manager", None) is None:
try:
from kailash.middleware.auth.auth_manager import MiddlewareAuthManager
nexus_instance._auth_manager = MiddlewareAuthManager(
enable_api_keys=True, enable_audit=True
)
except ImportError as e:
# Named degradation, not a silent pass: the HTTP auth layer is
# installed and enforcing either way, so this only affects the
# optional `_auth_manager` accessor.
logger.warning(
"nexus._auth_manager unavailable (%s); HTTP authentication "
"is installed and enforcing regardless.",
e,
)
class MonitoringPlugin(NexusPlugin):
"""Built-in monitoring plugin."""
@property
def name(self) -> str:
return "monitoring"
@property
def description(self) -> str:
return "Adds performance monitoring and metrics collection"
def apply(self, nexus_instance: Any) -> None:
"""Apply monitoring to Nexus."""
logger.info("Applying monitoring plugin")
# Add monitoring configuration
nexus_instance._monitoring_enabled = True
nexus_instance._metrics = {}
# Hook into workflow execution for metrics
logger.info("Monitoring enabled")
class RateLimitPlugin(NexusPlugin):
"""Rate limiting plugin."""
def __init__(self, requests_per_minute: int = 60):
"""Initialize rate limit plugin.
Args:
requests_per_minute: Maximum requests per minute
"""
self.requests_per_minute = requests_per_minute
@property
def name(self) -> str:
return "rate_limit"
@property
def description(self) -> str:
return f"Adds rate limiting ({self.requests_per_minute} req/min)"
def apply(self, nexus_instance: Any) -> None:
"""Apply rate limiting.
COERCED, because this is the FOURTH surface that writes
``_rate_limit`` and it was the last one still writing raw. The other
three -- the constructor, the config default, and the ``endpoint()``
kwarg -- all route through ``_coerce_rate_limit``; writing raw here
would reopen exactly what they reject, since
``sse.py::_rate_limit_exceeded`` treats any value failing ``> 0`` as
"no limit configured". So ``RateLimitPlugin(requests_per_minute=-5)``
silently disabled the limiter, and ``True`` -- an ``int`` subclass --
registered as ONE request per minute.
Coerced at the WRITE rather than in ``__init__`` deliberately: the
attribute is public and mutable, so validating only at construction
leaves ``plugin.requests_per_minute = -5`` as an unguarded path to the
same assignment.
"""
from nexus.core import _coerce_rate_limit
limit = _coerce_rate_limit(
self.requests_per_minute, "RateLimitPlugin(requests_per_minute=...)"
)
logger.info(f"Applying rate limit plugin: {limit} req/min")
nexus_instance._rate_limit = limit
class PluginRegistry:
"""Registry for managing Nexus plugins."""
def __init__(self):
"""Initialize plugin registry."""
self._plugins: Dict[str, NexusPlugin] = {}
self._load_builtin_plugins()
def _load_builtin_plugins(self):
"""Load built-in plugins."""
self.register(AuthPlugin())
self.register(MonitoringPlugin())
self.register(RateLimitPlugin())
def register(self, plugin: NexusPlugin) -> None:
"""Register a plugin.
Args:
plugin: Plugin instance to register
"""
if not isinstance(plugin, NexusPlugin):
raise ValueError("Plugin must inherit from NexusPlugin")
if not plugin.validate():
raise ValueError(f"Plugin {plugin.name} validation failed")
self._plugins[plugin.name] = plugin
logger.info(f"Registered plugin: {plugin.name}")
def get(self, name: str) -> Optional[NexusPlugin]:
"""Get a plugin by name.
Args:
name: Plugin name
Returns:
Plugin instance or None if not found
"""
return self._plugins.get(name)
def list(self) -> List[str]:
"""List all available plugins.
Returns:
List of plugin names
"""
return list(self._plugins.keys())
def apply(self, name: str, nexus_instance: Any) -> None:
"""Apply a plugin to Nexus instance.
Args:
name: Plugin name
nexus_instance: Nexus instance
"""
plugin = self.get(name)
if not plugin:
raise ValueError(f"Plugin '{name}' not found")
plugin.apply(nexus_instance)
logger.info(f"Applied plugin: {name}")
class PluginLoader:
"""Loads external plugins from filesystem."""
PLUGIN_PATTERNS = [
"nexus_plugins/*.py",
"plugins/*.py",
"*_plugin.py",
]
@staticmethod
def load_from_directory(directory: str = None) -> Dict[str, NexusPlugin]:
"""Load plugins from directory.
Args:
directory: Directory to search (defaults to current)
Returns:
Dictionary of loaded plugins
"""
directory = Path(directory or os.getcwd())
plugins = {}
for pattern in PluginLoader.PLUGIN_PATTERNS:
for file_path in directory.glob(pattern):
if file_path.name.startswith("_"):
continue
try:
plugin = PluginLoader._load_plugin_from_file(file_path)
if plugin:
plugins[plugin.name] = plugin
except Exception as e:
logger.warning(f"Failed to load plugin from {file_path}: {e}")
return plugins
@staticmethod
def _load_plugin_from_file(file_path: Path) -> Optional[NexusPlugin]:
"""Load a plugin from a Python file.
Args:
file_path: Path to plugin file
Returns:
Plugin instance or None
"""
spec = importlib.util.spec_from_file_location(file_path.stem, file_path)
if not spec or not spec.loader:
return None
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
# Look for NexusPlugin subclasses
for name, obj in vars(module).items():
if (
isinstance(obj, type)
and issubclass(obj, NexusPlugin)
and obj is not NexusPlugin
):
try:
return obj()
except TypeError as e:
# Plugin constructor requires arguments
logger.warning(
f"Cannot instantiate plugin {name} from {file_path}: "
f"constructor requires arguments ({e})"
)
except Exception as e:
# Plugin initialization failed
logger.error(
f"Failed to instantiate plugin {name} from {file_path}: "
f"{type(e).__name__}: {e}"
)
return None
# Global plugin registry
_registry = None
def get_plugin_registry() -> PluginRegistry:
"""Get or create the global plugin registry.
Returns:
Global PluginRegistry instance
"""
global _registry
if _registry is None:
_registry = PluginRegistry()
# Load external plugins
external = PluginLoader.load_from_directory()
for plugin in external.values():
try:
_registry.register(plugin)
except Exception as e:
logger.warning(f"Failed to register external plugin {plugin.name}: {e}")
return _registry

Xet Storage Details

Size:
11.9 kB
·
Xet hash:
101ac3864331b7b0c7f5fca5acdc109ec966e346394df2e6daa6d56cd1ef4a2c

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.