Add Python dependency installation and virtual environment support

Introduce a new script to install Python dependencies in a virtual environment, update systemd services to utilize this environment, and modify the setup script to automate dependency checks and installation.

Replit-Commit-Author: Agent
Replit-Commit-Session-Id: 7a657272-55ba-4a79-9a2e-f1ed9bc7a528
Replit-Commit-Checkpoint-Type: full_checkpoint
Replit-Commit-Event-Id: ea2a08f4-46e1-463d-9c58-16219914ad23
Replit-Commit-Screenshot-Url: https://storage.googleapis.com/screenshot-production-us-central1/449cf7c4-c97a-45ae-8234-e5c5b8d6a84f/7a657272-55ba-4a79-9a2e-f1ed9bc7a528/n4Q2eeE
This commit is contained in:
marco370 2025-11-22 09:40:57 +00:00
parent d187aa533a
commit 5b350ff95f
6 changed files with 154 additions and 7 deletions

View File

@ -0,0 +1,52 @@
tail -50 /var/log/ids/ml_backend.log
from fastapi import FastAPI, HTTPException, BackgroundTasks, Security, Header
ModuleNotFoundError: No module named 'fastapi'
Traceback (most recent call last):
File "/opt/ids/python_ml/main.py", line 6, in <module>
from fastapi import FastAPI, HTTPException, BackgroundTasks, Security, Header
ModuleNotFoundError: No module named 'fastapi'
Traceback (most recent call last):
File "/opt/ids/python_ml/main.py", line 6, in <module>
from fastapi import FastAPI, HTTPException, BackgroundTasks, Security, Header
ModuleNotFoundError: No module named 'fastapi'
Traceback (most recent call last):
File "/opt/ids/python_ml/main.py", line 6, in <module>
from fastapi import FastAPI, HTTPException, BackgroundTasks, Security, Header
ModuleNotFoundError: No module named 'fastapi'
Traceback (most recent call last):
File "/opt/ids/python_ml/main.py", line 6, in <module>
from fastapi import FastAPI, HTTPException, BackgroundTasks, Security, Header
ModuleNotFoundError: No module named 'fastapi'
Traceback (most recent call last):
File "/opt/ids/python_ml/main.py", line 6, in <module>
from fastapi import FastAPI, HTTPException, BackgroundTasks, Security, Header
ModuleNotFoundError: No module named 'fastapi'
Traceback (most recent call last):
File "/opt/ids/python_ml/main.py", line 6, in <module>
from fastapi import FastAPI, HTTPException, BackgroundTasks, Security, Header
ModuleNotFoundError: No module named 'fastapi'
Traceback (most recent call last):
File "/opt/ids/python_ml/main.py", line 6, in <module>
from fastapi import FastAPI, HTTPException, BackgroundTasks, Security, Header
ModuleNotFoundError: No module named 'fastapi'
Traceback (most recent call last):
File "/opt/ids/python_ml/main.py", line 6, in <module>
from fastapi import FastAPI, HTTPException, BackgroundTasks, Security, Header
ModuleNotFoundError: No module named 'fastapi'
Traceback (most recent call last):
File "/opt/ids/python_ml/main.py", line 6, in <module>
from fastapi import FastAPI, HTTPException, BackgroundTasks, Security, Header
ModuleNotFoundError: No module named 'fastapi'
Traceback (most recent call last):
File "/opt/ids/python_ml/main.py", line 6, in <module>
from fastapi import FastAPI, HTTPException, BackgroundTasks, Security, Header
ModuleNotFoundError: No module named 'fastapi'
Traceback (most recent call last):
File "/opt/ids/python_ml/main.py", line 6, in <module>
from fastapi import FastAPI, HTTPException, BackgroundTasks, Security, Header
ModuleNotFoundError: No module named 'fastapi'
Traceback (most recent call last):
File "/opt/ids/python_ml/main.py", line 6, in <module>
from fastapi import FastAPI, HTTPException, BackgroundTasks, Security, Header
ModuleNotFoundError: No module named 'fastapi'
[root@ids ids]#

View File

@ -0,0 +1,82 @@
#!/bin/bash
# =========================================================
# INSTALL PYTHON DEPENDENCIES - IDS System
# =========================================================
# Installa tutte le dipendenze Python necessarie per IDS
set -e
# Colors
RED='\033[0;31m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
IDS_DIR="/opt/ids"
VENV_DIR="${IDS_DIR}/python_ml/venv"
echo -e "${BLUE}🐍 Installazione Dipendenze Python per IDS${NC}\n"
# Check se script è eseguito da root
if [ "$EUID" -ne 0 ]; then
echo -e "${RED}❌ Questo script deve essere eseguito come root (usa sudo)${NC}"
exit 1
fi
# Installa python3-venv se non presente
echo -e "${BLUE}📦 Verifica python3-venv...${NC}"
if ! rpm -q python3.11-pip &>/dev/null; then
echo -e "${YELLOW}⚙️ Installazione python3.11-pip...${NC}"
dnf install -y python3.11-pip
fi
# Crea virtual environment
echo -e "${BLUE}🔧 Creazione virtual environment...${NC}"
if [ -d "$VENV_DIR" ]; then
echo -e "${YELLOW}♻️ Virtual environment già esistente, rimuovo...${NC}"
rm -rf "$VENV_DIR"
fi
python3.11 -m venv "$VENV_DIR"
echo -e "${GREEN}✅ Virtual environment creato in ${VENV_DIR}${NC}"
# Attiva virtual environment e installa dipendenze
echo -e "${BLUE}📥 Installazione dipendenze Python...${NC}"
source "${VENV_DIR}/bin/activate"
# Upgrade pip
pip install --upgrade pip
# Installa dipendenze principali
pip install fastapi==0.104.1
pip install uvicorn[standard]==0.24.0
pip install pydantic==2.5.0
pip install python-dotenv==1.0.0
pip install psycopg2-binary==2.9.9
pip install pandas==2.1.3
pip install numpy==1.26.2
pip install scikit-learn==1.3.2
echo -e "${GREEN}✅ Dipendenze Python installate${NC}"
# Cambia ownership a utente ids
echo -e "${BLUE}🔐 Impostazione permessi...${NC}"
chown -R ids:ids "$VENV_DIR"
# Verifica installazione
echo -e "\n${BLUE}🔍 Verifica installazione:${NC}"
source "${VENV_DIR}/bin/activate"
python3 -c "import fastapi; print(f'FastAPI: {fastapi.__version__}')"
python3 -c "import uvicorn; print(f'Uvicorn: {uvicorn.__version__}')"
python3 -c "import sklearn; print(f'Scikit-learn: {sklearn.__version__}')"
python3 -c "import pandas; print(f'Pandas: {pandas.__version__}')"
echo -e "\n${GREEN}╔═══════════════════════════════════════════════╗${NC}"
echo -e "${GREEN}║ ✅ DIPENDENZE PYTHON INSTALLATE ║${NC}"
echo -e "${GREEN}╚═══════════════════════════════════════════════╝${NC}"
echo -e "\n${BLUE}📝 NOTA:${NC}"
echo -e " Il virtual environment è in: ${YELLOW}${VENV_DIR}${NC}"
echo -e " I systemd services useranno automaticamente questo venv"
echo ""

View File

@ -26,6 +26,14 @@ if [ "$EUID" -ne 0 ]; then
exit 1 exit 1
fi fi
# Verifica virtual environment Python
if [ ! -d "${IDS_DIR}/python_ml/venv" ]; then
echo -e "${YELLOW}⚠️ Virtual environment Python non trovato${NC}"
echo -e "${BLUE}📦 Eseguo installazione dipendenze...${NC}\n"
"${IDS_DIR}/deployment/install_python_deps.sh"
echo ""
fi
# Verifica esistenza file .env # Verifica esistenza file .env
if [ ! -f "${IDS_DIR}/.env" ]; then if [ ! -f "${IDS_DIR}/.env" ]; then
echo -e "${RED}❌ File .env non trovato in ${IDS_DIR}/.env${NC}" echo -e "${RED}❌ File .env non trovato in ${IDS_DIR}/.env${NC}"

View File

@ -10,8 +10,8 @@ Group=ids
WorkingDirectory=/opt/ids/python_ml WorkingDirectory=/opt/ids/python_ml
EnvironmentFile=/opt/ids/.env EnvironmentFile=/opt/ids/.env
# Comando esecuzione # Comando esecuzione (usa virtual environment)
ExecStart=/usr/bin/python3 main.py ExecStart=/opt/ids/python_ml/venv/bin/python3 main.py
# Restart automatico in caso di crash # Restart automatico in caso di crash
Restart=on-failure Restart=on-failure

View File

@ -10,8 +10,8 @@ Group=ids
WorkingDirectory=/opt/ids/python_ml WorkingDirectory=/opt/ids/python_ml
EnvironmentFile=/opt/ids/.env EnvironmentFile=/opt/ids/.env
# Comando esecuzione # Comando esecuzione (usa virtual environment)
ExecStart=/usr/bin/python3 syslog_parser.py ExecStart=/opt/ids/python_ml/venv/bin/python3 syslog_parser.py
# Restart automatico in caso di crash # Restart automatico in caso di crash
Restart=on-failure Restart=on-failure

View File

@ -96,9 +96,14 @@ The IDS features a React-based frontend for real-time monitoring, detection visu
- Script `setup_systemd_services.sh` per installazione automatica - Script `setup_systemd_services.sh` per installazione automatica
- **Deployment**: - **Deployment**:
1. Utente esegue: `sudo ./deployment/setup_systemd_services.sh` 1. Utente esegue: `sudo ./deployment/setup_systemd_services.sh`
2. Script genera `IDS_API_KEY` se mancante 2. Script verifica e installa dipendenze Python (virtual environment)
3. Installa systemd units e avvia servizi 3. Script genera `IDS_API_KEY` se mancante
4. Frontend usa controlli per gestione remota servizi 4. Installa systemd units e avvia servizi
5. Frontend usa controlli per gestione remota servizi
- **Virtual Environment**:
- Dipendenze Python in `/opt/ids/python_ml/venv`
- Systemd services usano automaticamente il venv
- Script `install_python_deps.sh` per installazione manuale
- **Benefici**: - **Benefici**:
- 🔒 Autenticazione API end-to-end (zero vulnerabilità) - 🔒 Autenticazione API end-to-end (zero vulnerabilità)
- 🎯 Controlli UX funzionanti (no comandi manuali) - 🎯 Controlli UX funzionanti (no comandi manuali)