Imported from samiulsun/ai-security-monitor (
AGENTS.md). Install upstream withnpx skills add samiulsun/ai-security-monitor. Copyright stays with the author.
AGENTS.md — AI Security Monitor
Compact cheat-sheet and persistent project context for OpenCode sessions. If a fact is obvious from filenames or a standard framework default, it’s omitted.
Project Overview
AI Security Monitor is a full-stack security monitoring demo for academic cloud-computing projects. It analyzes login attempts in real time using a rule-based engine and an IsolationForest ML anomaly detector, then visualizes the results in a React dashboard.
Core flows:
- User submits credentials on
/login. - FastAPI backend checks the IP block list, validates credentials, and runs the
SecurityDetector. - Results (attack type, severity, risk score) are returned and displayed.
- The dashboard at
/dashboardpolls for stats, alerts, logs, and charts.
Architecture
User/Browser
↓
Nginx Reverse Proxy (port 80)
├── /api/* → FastAPI Backend (port 8000)
└── /* → React/Vite Frontend (port 80 internal)
↓
PostgreSQL (persistent database)
Background services:
LogMonitoris disabled by default in cloud deployments to prevent duplicate ingestion across replicas. A dedicated ingestion worker can opt in to tailing/var/log/nginx/access.log; synthetic traffic is separately opt-in.SecurityDetectorretrain runs periodically on synthetic events.
See CLOUD_DEPLOYMENT.md and AWS_DEPLOYMENT.md for cloud target architectures.
Technology Stack
| Layer | Technology |
|---|---|
| Frontend | React 18, Vite, React Router, Tailwind CSS, Axios, Recharts, date-fns |
| Backend | Python 3.12, FastAPI, Uvicorn, pydantic-settings |
| Database | PostgreSQL 16 via databases + asyncpg |
| ML | scikit-learn IsolationForest |
| Reverse proxy | Nginx 1.25-alpine |
| Orchestration | Docker Compose |
| CI/CD | GitHub Actions |
No lint/format/typecheck tooling is configured. CI only runs tests and the frontend build.
Repository Structure
ai-security-monitor/
├── .github/workflows/ci.yml
├── .opencode/ # OpenCode persistent context
├── backend/
│ ├── app/
│ │ ├── api/ # auth, alerts, logs, stats, simulate routers
│ │ ├── core/ # config, database, logging
│ │ ├── ml/ # SecurityDetector + IsolationForest
│ │ ├── services/ # LogMonitor
│ │ └── main.py # FastAPI entrypoint
│ ├── migrations/ # SQL migration files
│ ├── tests/
│ ├── Dockerfile
│ ├── start.sh # DEBUG-aware uvicorn startup
│ └── requirements.txt
├── frontend/
│ ├── src/
│ ├── Dockerfile
│ └── package.json
├── nginx/
│ └── nginx.conf
├── scripts/
│ ├── deploy.bat # Windows self-hosted runner
│ ├── deploy.sh # Linux/macOS self-hosted runner
│ └── simulate_attacks.py # CLI attack simulator
├── docker-compose.yml
├── .env.example
├── AGENTS.md
├── AWS_DEPLOYMENT.md
└── CLOUD_DEPLOYMENT.md
Development Commands
Backend only
cd backend
python -m venv venv
source venv/bin/activate
pip install -r requirements.txt
# Ensure DATABASE_URL points to PostgreSQL, e.g.:
# export DATABASE_URL=postgresql+asyncpg://postgres:postgres@localhost:5432/security
uvicorn app.main:app --reload
- Entrypoint:
app.main:app - Runs on http://localhost:8000
- Tables and indexes are created automatically on startup.
- Logs written to
backend/logs/app.log(must be writable).
Frontend only
cd frontend
npm install
npm run dev
- Dev server: http://localhost:3000
- Vite proxies
/api/*tohttp://localhost:8000. - Production build:
npm run build→frontend/dist/
Frontend has no test framework configured.
Test Commands
cd backend
pytest tests/ -v
pytest.inisetspythonpath = .; tests must run frombackend/.tests/conftest.pyinjects the project root intosys.path.
Docker Commands
Run everything locally
docker compose up --build
- Main app: http://localhost
- Direct backend / Swagger: http://localhost:8000/api/docs
- Stop:
docker compose down - Reset including DB volume:
docker compose down -v
Services
| Service | Image/Build | Ports | Notes |
|---|---|---|---|
| backend | ./backend |
8000 | Healthcheck on /api/health; start.sh enables --reload only when DEBUG=true |
| frontend | ./frontend |
none | Served by internal Nginx |
| nginx | nginx:1.25-alpine |
80 | Reverse proxy; mounts ./nginx/nginx.conf |
| postgres | postgres:16-alpine |
none | Persistent volume postgres-data |
Named volumes: postgres-data, backend-logs, nginx-logs.
Deployment Commands
Local / self-hosted
# Linux/macOS
./scripts/deploy.sh
# Windows
scripts/deploy.bat
Cloud
See:
CLOUD_DEPLOYMENT.md— general VM and managed-container options.AWS_DEPLOYMENT.md— EC2 + Docker Compose and ECS Fargate + RDS + ALB.
Environment Configuration
Copy .env.example to .env and fill in values. .env is gitignored.
Key variables:
| Variable | Purpose |
|---|---|
SECRET_KEY |
JWT/signing secret; replace default in production |
DEBUG |
true enables uvicorn --reload; use false in production |
DATABASE_URL |
PostgreSQL connection string, e.g. postgresql+asyncpg://postgres:postgres@postgres:5432/security |
DB_POOL_MIN_SIZE / DB_POOL_MAX_SIZE |
Connection pool size |
LOG_LEVEL |
DEBUG/INFO/WARNING/ERROR/CRITICAL |
LOG_FORMAT |
json for cloud, text for local dev |
ENABLE_SIMULATION_ENDPOINTS |
true for local demo; set false in production/AWS |
NGINX_LOG_PATH |
Path to Nginx access log (used by LogMonitor) |
Never commit actual secrets.
Important Technical Decisions
- PostgreSQL over SQLite — Replaced SQLite with PostgreSQL to support horizontal scaling and cloud-managed databases. Used the lightweight
databaseslibrary with asyncpg instead of SQLAlchemy/Alembic to keep the academic demo simple. - Redis-backed shared state — Redis stores sliding-window counters and publishes security events for SSE. Heavy background processing is still intentionally outside the API process.
- Simulation endpoints gated — Added
ENABLE_SIMULATION_ENDPOINTSso destructive demo endpoints can be disabled in production. - Structured JSON logging —
LOG_FORMAT=jsonoutputs cloud-friendly logs with request IDs. - Health checks —
/api/healthfor liveness;/api/health/readychecks PostgreSQL connectivity. - Nginx as single entry point — All traffic goes through Nginx;
/api/*is proxied to backend, everything else to frontend.
Current Project Status
- ✅ PostgreSQL migration complete
- ✅ Docker Compose with PostgreSQL working
- ✅ Health checks (
/api/health,/api/health/ready) - ✅ Structured JSON logging with request IDs
- ✅ Frontend polling with error backoff
- ✅ Cloud deployment guides (general + AWS)
- ✅ CI/CD with PostgreSQL service, security scans, Docker build
- ✅ Redis-backed shared counters and event streaming
- ✅ Database-backed bcrypt authentication, JWTs, and RBAC
- ❌ Background workers not implemented (uses FastAPI
BackgroundTasks) - ❌ No frontend tests
- ❌ HTTPS/TLS not configured in local Nginx (use ALB/ACM in AWS)
Known Issues
- Log ingestion ownership — Keep
ENABLE_LOG_MONITOR=falseon every horizontally scaled API replica. Run one dedicated ingestion worker if Nginx log monitoring is required. - ML training buffer — The detector model is process-local and retrained from baseline data; production-grade model training should move to a dedicated worker/model store.
- Demo credentials — Demo users are seeded from environment-configured passwords for local/course use; replace them with rotated deployment secrets or an external identity provider in production.
- README.md is outdated — Still mentions SQLite and a standalone
/api/simulate/xssendpoint.AGENTS.mdandAWS_DEPLOYMENT.mdreflect the current state.
Remaining Enhancements
- (Optional) Move heavy simulations/ML retraining to Celery workers.
- (Optional) Add HTTPS/TLS to local Nginx config.
- (Optional) Add frontend/component tests.
- (Optional) Update
README.mdto match the current PostgreSQL/cloud-ready architecture.
Important Warnings
- Never commit
.envor any secrets. It is gitignored, but double-check before pushing. - Disable simulation endpoints in production by setting
ENABLE_SIMULATION_ENDPOINTS=false. - Nginx rate limits will trigger HTTP 429 during high-rate simulations through
http://localhost. Usehttp://localhost:8000for simulator traffic. - Run backend commands from
backend/sopythonpath = .resolvesapp.*correctly. - ML cold start — The IsolationForest model trains on startup; first launch may take a moment.
Quick Reference
API endpoints (all under /api)
POST /api/auth/login— login + security analysisGET /api/auth/me— stubbed current userGET /api/alerts— alertsGET /api/stats/*— dashboard statisticsGET /api/logs— login attemptsGET /api/logs/events— raw log eventsPOST /api/simulate/*— attack simulation (dev only)DELETE /api/simulate/reset— wipe all data (dev only)GET /api/health— livenessGET /api/health/ready— readiness (PostgreSQL check)
Test credentials (demo only)
Demo users are seeded into PostgreSQL from DEMO_*_PASSWORD environment settings. Do not use demo passwords in production.
Attack simulator CLI
scripts/simulate_attacks.py requires requests (not in requirements.txt):
pip install requests
python scripts/simulate_attacks.py --mode all