File size: 7,325 Bytes
28a08e7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import json
import time
import uuid
import logging
import hashlib
from typing import List, Dict, Optional, Any
from pydantic import BaseModel, Field
from pathlib import Path
from fastapi import APIRouter, Depends
from .auth_guard import require_role, AuthRole

_logger = logging.getLogger("api.plugins")

router = APIRouter(prefix="/api/plugins", tags=["plugins"], dependencies=[Depends(require_role(AuthRole.MACHINE))])

class PluginPermission(str):
    FS_READ = "fs:read"
    FS_WRITE = "fs:write"
    NET_API = "net:api"
    SHELL_LIMITED = "shell:limited"

class PluginManifest(BaseModel):
    id: str
    name: str
    version: str
    description: Optional[str] = None
    author: Optional[str] = None
    permissions: List[str] = []
    dependencies: Dict[str, str] = {}
    entry_point: str = "main.py"
    signature: Optional[str] = None

class Plugin(BaseModel):
    manifest: PluginManifest
    code: str
    registered_at: int = Field(default_factory=lambda: int(time.time()))
    status: str = "active" # active, disabled, error

PLUGINS_REGISTRY: Dict[str, Plugin] = {}

class PluginManager:
    """
    ARCH-E3.3: Plugin System sandboxato
    Gestisce il ciclo di vita dei plugin e la loro esecuzione sicura.
    """
    
    @staticmethod
    def _verify_signature(manifest: PluginManifest, code: str) -> bool:
        """Verifica l'integritΓ  del plugin tramite hash del codice."""
        if not manifest.signature:
            return True # In dev mode accettiamo senza firma
        actual_hash = hashlib.sha256(code.encode()).hexdigest()
        return actual_hash == manifest.signature

    @staticmethod
    async def register(manifest_dict: dict, code: str) -> Dict[str, Any]:
        """Registra un nuovo plugin nel sistema."""
        try:
            manifest = PluginManifest(**manifest_dict)
            
            if not PluginManager._verify_signature(manifest, code):
                return {"status": "error", "message": "Firma del plugin non valida o codice corrotto"}
            
            plugin = Plugin(manifest=manifest, code=code)
            PLUGINS_REGISTRY[manifest.id] = plugin
            
            _logger.info(f"Plugin registrato: {manifest.id} v{manifest.version}")
            return {"status": "registered", "id": manifest.id, "version": manifest.version}
        except Exception as e:
            _logger.error(f"Errore registrazione plugin: {e}")
            return {"status": "error", "message": str(e)}

    @staticmethod
    async def list_plugins() -> List[Dict[str, Any]]:
        """Elenca tutti i plugin registrati e il loro stato."""
        return [
            {
                "id": p.manifest.id,
                "name": p.manifest.name,
                "version": p.manifest.version,
                "status": p.status,
                "permissions": p.manifest.permissions
            } for p in PLUGINS_REGISTRY.values()
        ]

    @staticmethod
    async def execute(plugin_id: str, input_data: Any, session_id: str = "default") -> Dict[str, Any]:
        """
        Esegue un plugin in una sandbox sicura.
        Applica restrizioni basate sui permessi del manifest.
        """
        if plugin_id not in PLUGINS_REGISTRY:
            return {"status": "error", "message": f"Plugin {plugin_id} non trovato"}
        
        plugin = PLUGINS_REGISTRY[plugin_id]
        if plugin.status != "active":
            return {"status": "error", "message": f"Plugin {plugin_id} Γ¨ in stato: {plugin.status}"}

        # Preparazione dell'ambiente di esecuzione (Sandbox)
        # Sfrutta backend/api/exec_sandbox.py
        try:
            from .exec_sandbox import run_in_sandbox_session
            
            # Wrapper del codice per iniettare input e catturare output
            # Il plugin deve definire una funzione 'main(input_data)'
            execution_wrapper = f"""
import json
import sys

# Input data iniettato
input_data = {json.dumps(input_data)}

# Codice del plugin
{plugin.code}

# Esecuzione
try:
    if 'main' in globals():
        result = main(input_data)
        print("---PLUGIN_RESULT_START---")
        print(json.dumps(result))
        print("---PLUGIN_RESULT_END---")
    else:
        print("Error: La funzione 'main(input_data)' non Γ¨ definita nel plugin.", file=sys.stderr)
except Exception as e:
    print(f"Plugin Execution Error: {{e}}", file=sys.stderr)
    sys.exit(1)
"""
            
            # TODO: In futuro, iniettare proxy limitati per FS/NET in base ai permessi
            # Per ora usiamo la sandbox standard che Γ¨ giΓ  isolata
            
            res = await run_in_sandbox_session(
                code=execution_wrapper,
                lang="python",
                session_id=f"plugin_{plugin_id}_{session_id}",
                timeout=60.0
            )
            
            # Parsing del risultato dall'output standard
            stdout = res.get("stdout", "")
            if "---PLUGIN_RESULT_START---" in stdout:
                try:
                    parts = stdout.split("---PLUGIN_RESULT_START---")[1].split("---PLUGIN_RESULT_END---")
                    plugin_output = json.loads(parts[0].strip())
                    return {
                        "status": "success",
                        "plugin_id": plugin_id,
                        "output": plugin_output,
                        "logs": stdout.split("---PLUGIN_RESULT_START---")[0]
                    }
                except Exception as e:
                    return {"status": "error", "message": f"Errore parsing output plugin: {e}", "raw_stdout": stdout}
            
            return {
                "status": "error" if res.get("returncode") != 0 else "completed_no_output",
                "plugin_id": plugin_id,
                "returncode": res.get("returncode"),
                "stderr": res.get("stderr"),
                "stdout": stdout
            }
            
        except Exception as e:
            _logger.error(f"Errore esecuzione plugin {plugin_id}: {e}")
            return {"status": "error", "message": str(e)}

# Singleton
plugin_manager = PluginManager()

# ── HTTP Endpoints ─────────────────────────────────────────────────────────────

class RegisterPluginRequest(BaseModel):
    manifest: dict
    code: str

class ExecutePluginRequest(BaseModel):
    plugin_id: str
    input_data: Any
    session_id: Optional[str] = "default"

@router.post("/register")
async def http_register_plugin(req: RegisterPluginRequest):
    return await plugin_manager.register(req.manifest, req.code)

@router.get("/list")
async def http_list_plugins():
    return await plugin_manager.list_plugins()

@router.post("/execute")
async def http_execute_plugin(req: ExecutePluginRequest):
    return await plugin_manager.execute(req.plugin_id, req.input_data, req.session_id)

@router.get("/health/{plugin_id}")
async def http_plugin_health(plugin_id: str):
    if plugin_id not in PLUGINS_REGISTRY:
        return {"status": "not_found"}
    p = PLUGINS_REGISTRY[plugin_id]
    return {
        "status": p.status,
        "id": p.manifest.id,
        "version": p.manifest.version,
        "uptime": int(time.time()) - p.registered_at
    }