Legitimate Infrastructure Simulation Agent - Management API
A FastAPI-based backend system for managing and monitoring software agents that simulate legitimate user activity on target systems.
- Agent Management: Create, configure, and deploy software agents
- Real-time Monitoring: WebSocket-based live agent monitoring
- Role-based Configuration: Define user roles and behaviors
- Template System: Reusable behavior and application templates
- CI/CD Integration: Automated agent building and deployment
- Heartbeat Monitoring: Real-time agent health and activity tracking
- PostgreSQL Support: Database backend
- Quick Start
- Installation
- API Documentation
- Database Schema
- Docker Setup
- Development
- Agent Integration
# Clone the repository
git clone <https://github.com/LISA-SWP25/backend>
cd lisa/backend
# Start with Docker Compose
docker-compose up -d
# Access the API documentation
open http://localhost:8000/docs# Install dependencies
pip install -r requirements.txt
# Run the server
uvicorn app.main:app --reload
# API available at http://localhost:8000- Agent Manager: Handles agent lifecycle and configuration
- Heartbeat System: Monitors agent health and activity
- Template Engine: Manages behavior and application templates
- Build Pipeline: Compiles agents for deployment
- Monitoring Dashboard: Real-time system overview
- Python 3.11+
- PostgreSQL 15+
- Docker & Docker Compose
-
Clone and Install
git clone <https://github.com/LISA-SWP25/backend> cd lisa/backend pip install -r requirements.txt
-
Database Configuration
# app/database.py DATABASE_URL = 'postgresql://lisa:pass@localhost:5432/lisa_dev'
-
Environment Variables
export DATABASE_URL="postgresql://lisa:pass@localhost:5432/lisa_dev" export CORS_ORIGINS="http://localhost:3000"
-
Run the Server
source venv/bin/activate uvicorn app.main:app --reload
POST /api/agents/generate # Create agent configuration
GET /api/agents # List all agents
GET /api/agents/{agent_id}/status # Get agent status
POST /api/agents/{agent_id}/deploy # Deploy agent
GET /api/agents/{agent_id}/config # Download agent configPOST /api/agents/heartbeat # Agent heartbeat (main endpoint)
GET /api/agents/active # List active agents
GET /api/agents/statistics/summary # System statistics
GET /api/monitoring/overview # Monitoring dashboardPOST /api/roles # Create user role
GET /api/roles # List roles
POST /api/behavior-templates # Create behavior template
GET /api/behavior-templates # List templates
POST /api/application-templates # Create app templateGET /api/health # System health check
GET /api/stats # System statistics
WS /api/ws/agents/{agent_id} # WebSocket monitoringPOST /api/agents/heartbeat
{
"timestamp": "2025-01-15T14:30:45.123456",
"agent_id": "USR0012345",
"username": "john_doe",
"role": "Junior Developer",
"department": "Development",
"location": "Headquarters",
"system_info": {
"hostname": "workstation-01",
"platform": "Linux-5.15.0-56-generic-x86_64",
"python_version": "3.10.6",
"cpu_count": 8,
"agent_version": "1.0.0"
},
"status": "active",
"statistics": {
"agent_uptime": 3600.5,
"current_app": "Visual Studio Code",
"plugin_mode": true,
"total_plugins": 5,
"available_apps": 7
},
"current_activity": {
"application": "Visual Studio Code",
"is_plugin": true
}
}-- Agents table
agents (
id SERIAL PRIMARY KEY,
agent_id VARCHAR(50) UNIQUE,
name VARCHAR(100),
status VARCHAR(20),
os_type VARCHAR(20),
last_seen TIMESTAMP,
config JSON
)
-- Agent Activities (heartbeats, logs)
agent_activities (
id SERIAL PRIMARY KEY,
agent_id INTEGER REFERENCES agents(id),
activity_type VARCHAR(50),
activity_data JSON,
timestamp TIMESTAMP
)
-- Roles and Templates
roles (id, name, description, category)
behavior_templates (id, name, description, template_data)
application_templates (id, name, template_config)# docker-compose.yml
services:
# PostgreSQL
lisa_postgres_quick:
image: postgres:15-alpine
container_name: lisa_postgres_quick
environment:
POSTGRES_DB: lisa_dev
POSTGRES_USER: lisa
POSTGRES_PASSWORD: pass
ports:
- "5432:5432"
volumes:
- postgres_data:/var/lib/postgresql/data
networks:
- lisa_network
healthcheck:
test: ["CMD-SHELL", "pg_isready -U lisa -d lisa_dev"]
interval: 10s
timeout: 5s
retries: 5
# Backend
backend:
build:
context: ./backend
dockerfile: Dockerfile.fast
container_name: lisa_backend
environment:
- DATABASE_URL=postgresql://lisa:pass@lisa_postgres_quick:5432/lisa_dev
- CORS_ORIGINS=http://localhost:3000
ports:
- "8000:8000"
volumes:
- ./backend:/app
networks:
- lisa_network
depends_on:
lisa_postgres_quick:
condition: service_healthy
# Frontend
frontend:
image: nginx:alpine
container_name: lisa_frontend
ports:
- "3000:80"
volumes:
- ./frontend-static:/usr/share/nginx/html:ro
networks:
- lisa_network
depends_on:
- backend
# Agent Deployer
agent-deployer:
image: python:3.11-slim
container_name: lisa_agent_deployer
working_dir: /app
command: >
sh -c "pip install fastapi uvicorn --no-cache-dir &&
python -c 'from fastapi import FastAPI; import uvicorn; app = FastAPI(); app.add_api_route(\"/\", lambda: {\"status\": \"running\", \"service\": \"agent-deployer\"}); app.add_api_route(\"/health\", lambda: {\"status\": \"healthy\"}); uvicorn.run(app, host=\"0.0.0.0\", port=8001)'"
ports:
- "8001:8001"
networks:
- lisa_network
networks:
lisa_network:
driver: bridge
volumes:
postgres_data:# Start all services
docker-compose up -d
# View logs
docker-compose logs -f backend
# Stop services
docker-compose down
# Rebuild and restart
docker-compose up --build -d# Install test dependencies
pip install pytest pytest-asyncio httpx
# Run tests
pytest
# Run with coverage
pytest --cov=app tests/- Create endpoint in
app/api/endpoints/ - Add route to
app/main.py - Define schemas in
app/schemas.py - Update database models if needed
import requests
import time
def send_heartbeat(agent_data):
response = requests.post(
"http://localhost:8000/api/agents/heartbeat",
json=agent_data,
headers={"Content-Type": "application/json"}
)
return response.json()
# Example usage
while True:
heartbeat_data = {
"agent_id": "USR0012345",
"username": "john_doe",
"status": "active",
"timestamp": datetime.utcnow().isoformat(),
# ... other fields
}
result = send_heartbeat(heartbeat_data)
time.sleep(60) # Send every minute# Download agent configuration
curl -o agent_config.json \
"http://localhost:8000/api/agents/USR0012345/config"
# Deploy agent with configuration
python agent_deployer.py --config agent_config.jsonDATABASE_URL="postgresql://user:pass@host:port/database"
CORS_ORIGINS="http://localhost:3000,http://localhost:5173"
API_KEY="your-api-key-here"
LOG_LEVEL="INFO"# For PostgreSQL
DATABASE_URL = 'postgresql://lisa:pass@localhost:5432/lisa_dev'
# For SQLite (fallback)
DATABASE_URL = 'sqlite:///./fallback.db'curl http://localhost:8000/api/healthcurl http://localhost:8000/api/statsconst ws = new WebSocket('ws://localhost:8000/api/ws/agents/USR0012345');
ws.onmessage = (event) => {
const data = JSON.parse(event.data);
console.log('Agent update:', data);
};-
Database Connection Failed
# Check PostgreSQL is running docker ps | grep postgres # Check connection psql -h localhost -U lisa -d lisa_dev
-
Port Already in Use
# Find process using port 8000 lsof -i :8000 # Kill process kill -9 <PID>
-
Agent Not Receiving Config
# Check agent endpoint curl http://localhost:8000/api/agents/USR0012345/status # Verify agent is registered curl http://localhost:8000/api/agents
{
"status": "received",
"agent_id": "USR0012345",
"timestamp": "2025-01-15T14:30:45.123456",
"message": "Heartbeat processed successfully",
"next_heartbeat_in": 86400
}{
"agent": {
"agent_id": "USR0012345",
"name": "john_doe",
"status": "active",
"last_seen": "2025-01-15T14:30:45.123456",
"role": "Junior Developer"
},
"recent_activities": [...]
}- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -m 'Add amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
This project is licensed under the MIT License.
