Spaces:
Paused
Paused
File size: 8,498 Bytes
f3bcb28 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 | """
Security Plugin Manager
Manages registration, execution, and lifecycle of security plugins.
"""
import asyncio
import logging
from typing import Optional
from pydantic import BaseModel
from backend.application.engines.security_plugins.base import (
SecurityPlugin,
AsyncSecurityPlugin,
SecurityFinding,
RiskLevel,
)
logger = logging.getLogger(__name__)
class PluginCheckResult(BaseModel):
"""Aggregated result from all plugins."""
safe: bool
risk_level: str
risk_score: float
findings: list[SecurityFinding]
plugins_executed: list[str]
plugins_failed: list[str]
def to_api_response(self) -> dict:
"""Format for API response."""
return {
"safe": self.safe,
"risk_level": self.risk_level,
"risk_score": self.risk_score,
"issues_count": len(self.findings),
"plugins_executed": self.plugins_executed,
"findings": [f.model_dump() for f in self.findings],
}
class PluginManager:
"""
Manages security plugins.
Features:
- Register/unregister plugins
- Execute all plugins in parallel
- Aggregate findings
- Enable/disable plugins at runtime
"""
def __init__(self):
self._plugins: dict[str, SecurityPlugin] = {}
self._initialized: bool = False
def register(self, plugin: SecurityPlugin) -> None:
"""
Register a security plugin.
Args:
plugin: Plugin instance to register
"""
if plugin.name in self._plugins:
logger.warning(f"Replacing existing plugin: {plugin.name}")
self._plugins[plugin.name] = plugin
logger.info(f"Registered security plugin: {plugin.name} v{plugin.version}")
# Initialize if manager is already initialized
if self._initialized:
try:
plugin.initialize()
except Exception as e:
logger.error(f"Failed to initialize plugin {plugin.name}: {e}")
def unregister(self, name: str) -> bool:
"""
Unregister a plugin by name.
Args:
name: Plugin name
Returns:
True if plugin was unregistered
"""
if name in self._plugins:
plugin = self._plugins.pop(name)
try:
plugin.cleanup()
except Exception as e:
logger.error(f"Error during cleanup of {name}: {e}")
logger.info(f"Unregistered security plugin: {name}")
return True
return False
def get_plugin(self, name: str) -> Optional[SecurityPlugin]:
"""Get a plugin by name."""
return self._plugins.get(name)
def list_plugins(self) -> list[dict]:
"""List all registered plugins."""
return [
{
"name": p.name,
"description": p.description,
"version": p.version,
"enabled": p.enabled,
"async": isinstance(p, AsyncSecurityPlugin),
}
for p in self._plugins.values()
]
def enable_plugin(self, name: str) -> bool:
"""Enable a plugin."""
if name in self._plugins:
self._plugins[name].enabled = True
return True
return False
def disable_plugin(self, name: str) -> bool:
"""Disable a plugin."""
if name in self._plugins:
self._plugins[name].enabled = False
return True
return False
def initialize_all(self) -> None:
"""Initialize all registered plugins."""
for name, plugin in self._plugins.items():
try:
plugin.initialize()
logger.debug(f"Initialized plugin: {name}")
except Exception as e:
logger.error(f"Failed to initialize plugin {name}: {e}")
self._initialized = True
def cleanup_all(self) -> None:
"""Cleanup all registered plugins."""
for name, plugin in self._plugins.items():
try:
plugin.cleanup()
except Exception as e:
logger.error(f"Error during cleanup of {name}: {e}")
self._initialized = False
def check(self, messages: list[dict]) -> PluginCheckResult:
"""
Run all sync plugins and return aggregated results.
For async plugins, use check_async().
"""
findings: list[SecurityFinding] = []
plugins_executed: list[str] = []
plugins_failed: list[str] = []
for name, plugin in self._plugins.items():
if not plugin.enabled:
continue
if isinstance(plugin, AsyncSecurityPlugin):
# Skip async plugins in sync check
continue
try:
plugin_findings = plugin.check(messages)
findings.extend(plugin_findings)
plugins_executed.append(name)
except Exception as e:
logger.error(f"Plugin {name} failed: {e}")
plugins_failed.append(name)
return self._aggregate_results(findings, plugins_executed, plugins_failed)
async def check_async(self, messages: list[dict]) -> PluginCheckResult:
"""
Run all plugins (sync and async) and return aggregated results.
"""
findings: list[SecurityFinding] = []
plugins_executed: list[str] = []
plugins_failed: list[str] = []
# Prepare tasks
sync_plugins = []
async_tasks = []
for name, plugin in self._plugins.items():
if not plugin.enabled:
continue
if isinstance(plugin, AsyncSecurityPlugin):
async_tasks.append((name, plugin.check_async(messages)))
else:
sync_plugins.append((name, plugin))
# Run sync plugins
for name, plugin in sync_plugins:
try:
plugin_findings = plugin.check(messages)
findings.extend(plugin_findings)
plugins_executed.append(name)
except Exception as e:
logger.error(f"Plugin {name} failed: {e}")
plugins_failed.append(name)
# Run async plugins concurrently
if async_tasks:
results = await asyncio.gather(
*[task for _, task in async_tasks], return_exceptions=True
)
for (name, _), result in zip(async_tasks, results):
if isinstance(result, Exception):
logger.error(f"Async plugin {name} failed: {result}")
plugins_failed.append(name)
else:
findings.extend(result)
plugins_executed.append(name)
return self._aggregate_results(findings, plugins_executed, plugins_failed)
def _aggregate_results(
self,
findings: list[SecurityFinding],
plugins_executed: list[str],
plugins_failed: list[str],
) -> PluginCheckResult:
"""Aggregate findings into a single result."""
if not findings:
return PluginCheckResult(
safe=True,
risk_level="low",
risk_score=0.0,
findings=[],
plugins_executed=plugins_executed,
plugins_failed=plugins_failed,
)
# Calculate max risk level
risk_order = [
RiskLevel.LOW,
RiskLevel.MEDIUM,
RiskLevel.HIGH,
RiskLevel.CRITICAL,
]
max_risk = max(findings, key=lambda f: risk_order.index(RiskLevel(f.severity)))
# Calculate risk score
weights = {
RiskLevel.LOW: 0.1,
RiskLevel.MEDIUM: 0.3,
RiskLevel.HIGH: 0.7,
RiskLevel.CRITICAL: 1.0,
}
total_weight = sum(
weights.get(RiskLevel(f.severity), 0.1) * f.confidence for f in findings
)
risk_score = min(total_weight / 2.0, 1.0)
return PluginCheckResult(
safe=False,
risk_level=(
max_risk.severity
if isinstance(max_risk.severity, str)
else max_risk.severity.value
),
risk_score=round(risk_score, 2),
findings=findings,
plugins_executed=plugins_executed,
plugins_failed=plugins_failed,
)
|