2026-07-23 19:01:18 +02:00
|
|
|
"""Plugin routes — list, install, activate, deactivate, uninstall, manifest schema, config, upload, install-url."""
|
2026-06-29 01:18:46 +02:00
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
2026-07-23 19:01:18 +02:00
|
|
|
import logging
|
|
|
|
|
from typing import Any
|
|
|
|
|
|
|
|
|
|
from fastapi import APIRouter, Depends, File, HTTPException, Query, UploadFile
|
2026-07-03 16:49:57 +00:00
|
|
|
from pydantic import BaseModel
|
2026-06-29 01:18:46 +02:00
|
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
|
|
|
|
|
|
from app.core.db import get_db
|
2026-08-16 01:17:18 +02:00
|
|
|
from app.deps import require_admin, require_permission
|
2026-06-29 01:18:46 +02:00
|
|
|
from app.plugins.migration_runner import MigrationValidationError
|
|
|
|
|
from app.services.plugin_service import get_plugin_service
|
|
|
|
|
|
2026-07-23 19:01:18 +02:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
2026-06-29 01:18:46 +02:00
|
|
|
router = APIRouter(prefix="/api/v1/plugins", tags=["plugins"])
|
|
|
|
|
|
|
|
|
|
|
2026-07-03 16:49:57 +00:00
|
|
|
class PluginConfigUpdate(BaseModel):
|
|
|
|
|
"""Request body for updating plugin configuration."""
|
|
|
|
|
config: dict
|
|
|
|
|
|
|
|
|
|
|
2026-07-23 19:01:18 +02:00
|
|
|
class PluginUrlInstall(BaseModel):
|
|
|
|
|
"""Request body for installing a plugin from a URL."""
|
|
|
|
|
url: str
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MAX_UPLOAD_SIZE = 10 * 1024 * 1024 # 10 MB
|
|
|
|
|
|
|
|
|
|
|
2026-06-29 01:18:46 +02:00
|
|
|
@router.get("")
|
|
|
|
|
async def list_plugins(
|
|
|
|
|
db: AsyncSession = Depends(get_db),
|
2026-07-15 22:35:50 +02:00
|
|
|
current_user: dict = Depends(require_permission("plugins:read")),
|
2026-06-29 01:18:46 +02:00
|
|
|
):
|
|
|
|
|
"""List all plugins with their current status (discovered, installed, active, inactive)."""
|
|
|
|
|
service = get_plugin_service()
|
|
|
|
|
plugins = await service.list_plugins(db)
|
2026-07-24 17:17:53 +02:00
|
|
|
return {"plugins": plugins, "total": len(plugins)}
|
2026-06-29 01:18:46 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.get("/manifest")
|
|
|
|
|
async def get_manifest_schema(
|
2026-07-15 22:35:50 +02:00
|
|
|
current_user: dict = Depends(require_permission("plugins:read")),
|
2026-06-29 01:18:46 +02:00
|
|
|
):
|
|
|
|
|
"""Get the plugin manifest schema documentation."""
|
|
|
|
|
service = get_plugin_service()
|
|
|
|
|
return service.get_manifest_schema()
|
|
|
|
|
|
|
|
|
|
|
2026-07-26 23:15:34 +02:00
|
|
|
@router.get("/updates")
|
|
|
|
|
async def check_plugin_updates(
|
|
|
|
|
db: AsyncSession = Depends(get_db),
|
|
|
|
|
current_user: dict = Depends(require_permission("plugins:read")),
|
|
|
|
|
):
|
|
|
|
|
"""Check for available plugin updates.
|
|
|
|
|
|
|
|
|
|
Compares installed plugin versions with discovered plugin versions.
|
|
|
|
|
Returns a list of plugins where the discovered version is newer.
|
|
|
|
|
"""
|
|
|
|
|
from app.plugins.semver import SemVer
|
|
|
|
|
|
|
|
|
|
service = get_plugin_service()
|
|
|
|
|
plugins = await service.list_plugins(db)
|
|
|
|
|
updates: list[dict[str, Any]] = []
|
|
|
|
|
|
|
|
|
|
for plugin in plugins:
|
|
|
|
|
if not plugin.get("installed"):
|
|
|
|
|
continue
|
|
|
|
|
installed_version = plugin.get("version", "0.0.0")
|
|
|
|
|
# The discovered version is always the manifest version
|
|
|
|
|
discovered_version = plugin.get("version", "0.0.0")
|
|
|
|
|
# In a real marketplace scenario, we'd compare with a remote registry
|
|
|
|
|
# For now, we check if the manifest version differs from the DB version
|
|
|
|
|
# This is a placeholder for marketplace integration
|
|
|
|
|
try:
|
|
|
|
|
if SemVer.parse(discovered_version) > SemVer.parse(installed_version):
|
|
|
|
|
updates.append({
|
|
|
|
|
"name": plugin["name"],
|
|
|
|
|
"display_name": plugin.get("display_name", plugin["name"]),
|
|
|
|
|
"current_version": installed_version,
|
|
|
|
|
"available_version": discovered_version,
|
|
|
|
|
})
|
|
|
|
|
except ValueError:
|
|
|
|
|
pass # Skip if version is not valid SemVer
|
|
|
|
|
|
|
|
|
|
return {"updates": updates, "total": len(updates)}
|
|
|
|
|
|
|
|
|
|
|
2026-07-23 19:01:18 +02:00
|
|
|
@router.get("/active-manifests")
|
|
|
|
|
async def get_active_manifests(
|
|
|
|
|
db: AsyncSession = Depends(get_db),
|
|
|
|
|
current_user: dict = Depends(require_permission("plugins:read")),
|
|
|
|
|
):
|
|
|
|
|
"""Get UI manifests for all active plugins.
|
|
|
|
|
|
|
|
|
|
Returns menu_items, page_routes, detail_tabs, settings_pages, and
|
|
|
|
|
dashboard_widgets contributed by each active plugin. Used by the
|
|
|
|
|
frontend PluginRegistry to dynamically register routes, sidebar items,
|
|
|
|
|
settings pages, and detail tabs.
|
|
|
|
|
"""
|
|
|
|
|
service = get_plugin_service()
|
|
|
|
|
manifests = await service.get_active_manifests(db)
|
2026-07-24 17:17:53 +02:00
|
|
|
return {"plugins": manifests, "total": len(manifests)}
|
2026-07-23 19:01:18 +02:00
|
|
|
|
|
|
|
|
|
2026-07-03 16:49:57 +00:00
|
|
|
@router.get("/{name}/config")
|
|
|
|
|
async def get_plugin_config(
|
|
|
|
|
name: str,
|
|
|
|
|
db: AsyncSession = Depends(get_db),
|
2026-07-15 22:35:50 +02:00
|
|
|
current_user: dict = Depends(require_permission("plugins:read")),
|
2026-07-03 16:49:57 +00:00
|
|
|
):
|
|
|
|
|
"""Get the configuration for a specific plugin.
|
|
|
|
|
|
|
|
|
|
Returns the plugin's config field as a JSON object.
|
|
|
|
|
"""
|
|
|
|
|
service = get_plugin_service()
|
|
|
|
|
try:
|
|
|
|
|
config = await service.get_plugin_config(db, name)
|
|
|
|
|
return {"name": name, "config": config}
|
|
|
|
|
except ValueError as exc:
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
404, detail={"detail": str(exc), "code": "plugin_not_found"}
|
|
|
|
|
) from None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.patch("/{name}/config")
|
|
|
|
|
async def update_plugin_config(
|
|
|
|
|
name: str,
|
|
|
|
|
body: PluginConfigUpdate,
|
|
|
|
|
db: AsyncSession = Depends(get_db),
|
2026-07-29 12:33:46 +02:00
|
|
|
current_user: dict = Depends(require_admin),
|
2026-07-03 16:49:57 +00:00
|
|
|
):
|
|
|
|
|
"""Update the configuration for a specific plugin.
|
|
|
|
|
|
|
|
|
|
Stores the config as a JSON string in the plugin's config field.
|
|
|
|
|
"""
|
|
|
|
|
import uuid as uuid_mod
|
|
|
|
|
|
|
|
|
|
service = get_plugin_service()
|
|
|
|
|
try:
|
|
|
|
|
result = await service.update_plugin_config(
|
|
|
|
|
db,
|
|
|
|
|
name,
|
|
|
|
|
config=body.config,
|
|
|
|
|
tenant_id=uuid_mod.UUID(current_user["tenant_id"]),
|
|
|
|
|
user_id=uuid_mod.UUID(current_user["user_id"]),
|
|
|
|
|
)
|
|
|
|
|
return result
|
|
|
|
|
except ValueError as exc:
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
404, detail={"detail": str(exc), "code": "plugin_not_found"}
|
|
|
|
|
) from None
|
|
|
|
|
|
|
|
|
|
|
2026-06-29 01:18:46 +02:00
|
|
|
@router.post("/{name}/install")
|
|
|
|
|
async def install_plugin(
|
|
|
|
|
name: str,
|
|
|
|
|
db: AsyncSession = Depends(get_db),
|
2026-07-29 12:33:46 +02:00
|
|
|
current_user: dict = Depends(require_admin),
|
2026-06-29 01:18:46 +02:00
|
|
|
):
|
|
|
|
|
"""Install a plugin by name. Runs migrations and creates DB record.
|
|
|
|
|
|
|
|
|
|
Idempotent: returns 200 if already installed.
|
|
|
|
|
"""
|
|
|
|
|
import uuid as uuid_mod
|
2026-06-29 17:43:56 +02:00
|
|
|
|
2026-06-29 01:18:46 +02:00
|
|
|
service = get_plugin_service()
|
|
|
|
|
try:
|
|
|
|
|
result = await service.install_plugin(
|
2026-06-29 17:43:56 +02:00
|
|
|
db,
|
|
|
|
|
name,
|
2026-06-29 01:18:46 +02:00
|
|
|
tenant_id=uuid_mod.UUID(current_user["tenant_id"]),
|
|
|
|
|
user_id=uuid_mod.UUID(current_user["user_id"]),
|
|
|
|
|
)
|
|
|
|
|
return result
|
|
|
|
|
except ValueError as exc:
|
|
|
|
|
if "not found" in str(exc).lower():
|
2026-06-29 17:43:56 +02:00
|
|
|
raise HTTPException(
|
|
|
|
|
404, detail={"detail": str(exc), "code": "plugin_not_found"}
|
|
|
|
|
) from None
|
|
|
|
|
raise HTTPException(400, detail={"detail": str(exc), "code": "plugin_error"}) from None
|
2026-06-29 01:18:46 +02:00
|
|
|
except MigrationValidationError as exc:
|
2026-06-29 17:43:56 +02:00
|
|
|
raise HTTPException(
|
|
|
|
|
422, detail={"detail": str(exc), "code": "migration_validation_error"}
|
|
|
|
|
) from None
|
2026-06-29 01:18:46 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.post("/{name}/activate")
|
|
|
|
|
async def activate_plugin(
|
|
|
|
|
name: str,
|
|
|
|
|
db: AsyncSession = Depends(get_db),
|
2026-07-29 12:33:46 +02:00
|
|
|
current_user: dict = Depends(require_admin),
|
2026-06-29 01:18:46 +02:00
|
|
|
):
|
|
|
|
|
"""Activate a plugin by name. Registers event listeners and routes.
|
|
|
|
|
|
|
|
|
|
Idempotent: returns 200 if already active.
|
|
|
|
|
"""
|
|
|
|
|
import uuid as uuid_mod
|
2026-06-29 17:43:56 +02:00
|
|
|
|
2026-06-29 01:18:46 +02:00
|
|
|
service = get_plugin_service()
|
|
|
|
|
try:
|
|
|
|
|
result = await service.activate_plugin(
|
2026-06-29 17:43:56 +02:00
|
|
|
db,
|
|
|
|
|
name,
|
2026-06-29 01:18:46 +02:00
|
|
|
tenant_id=uuid_mod.UUID(current_user["tenant_id"]),
|
|
|
|
|
user_id=uuid_mod.UUID(current_user["user_id"]),
|
|
|
|
|
)
|
|
|
|
|
return result
|
|
|
|
|
except ValueError as exc:
|
|
|
|
|
if "not installed" in str(exc).lower():
|
2026-06-29 17:43:56 +02:00
|
|
|
raise HTTPException(
|
|
|
|
|
400, detail={"detail": str(exc), "code": "plugin_not_installed"}
|
|
|
|
|
) from None
|
2026-06-29 01:18:46 +02:00
|
|
|
if "not found" in str(exc).lower():
|
2026-06-29 17:43:56 +02:00
|
|
|
raise HTTPException(
|
|
|
|
|
404, detail={"detail": str(exc), "code": "plugin_not_found"}
|
|
|
|
|
) from None
|
|
|
|
|
raise HTTPException(400, detail={"detail": str(exc), "code": "plugin_error"}) from None
|
2026-06-29 01:18:46 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.post("/{name}/deactivate")
|
|
|
|
|
async def deactivate_plugin(
|
|
|
|
|
name: str,
|
|
|
|
|
db: AsyncSession = Depends(get_db),
|
2026-07-29 12:33:46 +02:00
|
|
|
current_user: dict = Depends(require_admin),
|
2026-06-29 01:18:46 +02:00
|
|
|
):
|
|
|
|
|
"""Deactivate a plugin by name. Unregisters event listeners and routes.
|
|
|
|
|
|
|
|
|
|
Idempotent: returns 200 if already inactive.
|
|
|
|
|
"""
|
|
|
|
|
import uuid as uuid_mod
|
2026-06-29 17:43:56 +02:00
|
|
|
|
2026-06-29 01:18:46 +02:00
|
|
|
service = get_plugin_service()
|
|
|
|
|
try:
|
|
|
|
|
result = await service.deactivate_plugin(
|
2026-06-29 17:43:56 +02:00
|
|
|
db,
|
|
|
|
|
name,
|
2026-06-29 01:18:46 +02:00
|
|
|
tenant_id=uuid_mod.UUID(current_user["tenant_id"]),
|
|
|
|
|
user_id=uuid_mod.UUID(current_user["user_id"]),
|
|
|
|
|
)
|
|
|
|
|
return result
|
|
|
|
|
except ValueError as exc:
|
|
|
|
|
if "not found" in str(exc).lower():
|
2026-06-29 17:43:56 +02:00
|
|
|
raise HTTPException(
|
|
|
|
|
404, detail={"detail": str(exc), "code": "plugin_not_found"}
|
|
|
|
|
) from None
|
|
|
|
|
raise HTTPException(400, detail={"detail": str(exc), "code": "plugin_error"}) from None
|
2026-06-29 01:18:46 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.delete("/{name}")
|
|
|
|
|
async def uninstall_plugin(
|
|
|
|
|
name: str,
|
|
|
|
|
remove_data: bool = Query(False),
|
|
|
|
|
db: AsyncSession = Depends(get_db),
|
2026-07-29 12:33:46 +02:00
|
|
|
current_user: dict = Depends(require_admin),
|
2026-06-29 01:18:46 +02:00
|
|
|
):
|
2026-07-23 19:01:18 +02:00
|
|
|
"""Uninstall a plugin by name. Deactivates, drops tables, removes DB record.
|
2026-06-29 01:18:46 +02:00
|
|
|
|
2026-07-23 19:01:18 +02:00
|
|
|
Idempotent: returns 200 if already uninstalled.
|
2026-06-29 01:18:46 +02:00
|
|
|
"""
|
|
|
|
|
import uuid as uuid_mod
|
2026-06-29 17:43:56 +02:00
|
|
|
|
2026-06-29 01:18:46 +02:00
|
|
|
service = get_plugin_service()
|
|
|
|
|
try:
|
|
|
|
|
result = await service.uninstall_plugin(
|
2026-06-29 17:43:56 +02:00
|
|
|
db,
|
|
|
|
|
name,
|
2026-06-29 01:18:46 +02:00
|
|
|
remove_data=remove_data,
|
|
|
|
|
tenant_id=uuid_mod.UUID(current_user["tenant_id"]),
|
|
|
|
|
user_id=uuid_mod.UUID(current_user["user_id"]),
|
|
|
|
|
)
|
|
|
|
|
return result
|
|
|
|
|
except ValueError as exc:
|
|
|
|
|
if "not found" in str(exc).lower():
|
2026-06-29 17:43:56 +02:00
|
|
|
raise HTTPException(
|
|
|
|
|
404, detail={"detail": str(exc), "code": "plugin_not_found"}
|
|
|
|
|
) from None
|
|
|
|
|
raise HTTPException(400, detail={"detail": str(exc), "code": "plugin_error"}) from None
|
2026-07-23 19:01:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.post("/upload")
|
|
|
|
|
async def upload_plugin(
|
|
|
|
|
file: UploadFile = File(...),
|
|
|
|
|
db: AsyncSession = Depends(get_db),
|
2026-07-29 12:33:46 +02:00
|
|
|
current_user: dict = Depends(require_admin),
|
2026-07-23 19:01:18 +02:00
|
|
|
):
|
|
|
|
|
"""Upload and install a plugin from a ZIP file.
|
|
|
|
|
|
2026-07-25 21:03:46 +02:00
|
|
|
DISABLED — Plugin upload is deactivated due to security vulnerabilities (RCE via exec_module before validation).
|
|
|
|
|
Will be re-enabled with signed plugin artifacts and sandboxed execution.
|
2026-07-23 19:01:18 +02:00
|
|
|
"""
|
2026-07-25 21:03:46 +02:00
|
|
|
raise HTTPException(
|
|
|
|
|
status_code=403,
|
|
|
|
|
detail={"detail": "Plugin upload is disabled. Use signed plugin artifacts from the allowlist.", "code": "upload_disabled"},
|
|
|
|
|
)
|
2026-07-23 19:01:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.post("/install-url")
|
|
|
|
|
async def install_plugin_from_url(
|
|
|
|
|
body: PluginUrlInstall,
|
|
|
|
|
db: AsyncSession = Depends(get_db),
|
2026-07-29 12:33:46 +02:00
|
|
|
current_user: dict = Depends(require_admin),
|
2026-07-23 19:01:18 +02:00
|
|
|
):
|
2026-07-25 21:03:46 +02:00
|
|
|
"""Install a plugin from a URL (downloads ZIP and installs).
|
2026-07-23 19:01:18 +02:00
|
|
|
|
2026-07-25 21:03:46 +02:00
|
|
|
DISABLED — URL installation is deactivated due to SSRF and RCE vulnerabilities.
|
|
|
|
|
Will be re-enabled with signed plugin artifacts and allowlist.
|
|
|
|
|
"""
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
status_code=403,
|
|
|
|
|
detail={"detail": "Plugin URL installation is disabled. Use signed plugin artifacts from the allowlist.", "code": "install_url_disabled"},
|
|
|
|
|
)
|
2026-07-26 23:15:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
# ── Marketplace (Phase 5) ──────────────────────────────────────────────────────
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MarketplaceInstall(BaseModel):
|
|
|
|
|
"""Request body for installing a plugin from the marketplace."""
|
|
|
|
|
url: str
|
|
|
|
|
signature: str | None = None
|
|
|
|
|
public_key: str | None = None
|
|
|
|
|
activate: bool = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@router.post("/install-marketplace")
|
|
|
|
|
async def install_from_marketplace(
|
|
|
|
|
body: MarketplaceInstall,
|
|
|
|
|
db: AsyncSession = Depends(get_db),
|
2026-07-29 12:33:46 +02:00
|
|
|
current_user: dict = Depends(require_admin),
|
2026-07-26 23:15:34 +02:00
|
|
|
):
|
|
|
|
|
"""Install a plugin from the marketplace.
|
|
|
|
|
|
|
|
|
|
1. Download ZIP from marketplace URL
|
|
|
|
|
2. Verify signature against allowlist (if provided)
|
|
|
|
|
3. Quarantine: validate manifest, check dangerous imports, validate SQL
|
|
|
|
|
4. Install (migrations + DB record)
|
|
|
|
|
5. Activate (optional)
|
|
|
|
|
|
|
|
|
|
DISABLED until marketplace is live — requires allowlist entry.
|
|
|
|
|
"""
|
|
|
|
|
raise HTTPException(
|
|
|
|
|
status_code=403,
|
|
|
|
|
detail={
|
|
|
|
|
"detail": "Marketplace installation is not yet available. Use built-in plugins.",
|
|
|
|
|
"code": "marketplace_not_available",
|
|
|
|
|
},
|
|
|
|
|
)
|