This repository contains an end‑to‑end Zero‑Trust Anomaly Detection platform for authentication logs:
-
Backend (Python / FastAPI) under
backend/- Trained Isolation Forest–based anomaly model, scaler and preprocessing.
- REST API for real‑time login scoring and analytics.
- Zero‑Trust policy: any
event_label != "normal"is treated as an anomaly. - Email alerts on anomalous logins.
- Real‑time event logging to
backend/data/realtime_events.csv.
-
Frontend (Next.js / TypeScript) under
frontend/- Landing page at
/explaining the system. - User login UI at
/userthat calls the backend/predictendpoint. - SOC / admin dashboard at
/dashboardthat visualises metrics, time series, locations and detailed events using the same data as the backend.
- Landing page at
For a full written project narrative and business context, see backend/PROJECT_REPORT.md.
-
backend/anomaly_api.py– FastAPI application exposing:POST /predict– score a single login event, send email alert on anomaly, append torealtime_events.csv.GET /events– JSON list of events fromauth_logs_raw.csv+realtime_events.csv(supportslimit).GET /metrics– aggregated metrics (total,anomalies,anomalyRate,avgMttdMinutes) plus series and breakdowns.
model_training.ipynb– data exploration, feature engineering and model training notebook.data/auth_logs_raw.csv– baseline synthetic authentication dataset.data/realtime_events.csv– runtime log of scored logins from the UI (created/appended by/predict).data/processed/– derived features and preprocessing pipeline (ignored in git by default).isoforest_model.pkl,scaler.pkl– trained anomaly model and feature scaler.producer.py– optional Kafka producer for streaming auth events (if you usedocker-compose.yml).docker-compose.yml– local Kafka/ZooKeeper stack (optional).EMAIL_SETUP.md– configuration details for SMTP / email alerts.requirements.txt– Python backend dependencies.
-
frontend/src/app/page.tsx– marketing-style landing page, with entry points to user and admin flows.src/app/user/page.tsx– user login UI:- Captures
user_id,device_id,ip_address,location,resource_accessed,login_success,bytes_transferred,password. - Sends JSON to
POST /predicton the backend and displays model decision (NORMAL/ANOMALY).
- Captures
src/app/dashboard/page.tsx– SOC dashboard:- Uses React Query + custom hooks (
useEvents,useStats,useShap) to talk to the backend. - Shows key KPIs, login events over time, anomalies by type, location analytics, SHAP explainability, and a filterable / sortable events table.
- Uses React Query + custom hooks (
src/hooks/– React hooks for filters and API data.src/lib/api.ts– frontend API client (respectsNEXT_PUBLIC_BACKEND_URLor falls back tohttp://<host>:8000).src/components/ui/– small shadcn‑style UI primitives (Card,Badge,Accordion).
- Clone and enter the repo
git clone <your-repo-url>.git
cd joan-testing- Create and activate a Python environment
You can use conda (e.g. your existing llms env) or a plain venv:
cd backend
python -m venv .venv # or: conda create -n llms python=3.11
source .venv/bin/activate # on macOS/Linux
# .venv\Scripts\activate # on Windows
pip install -r requirements.txt- (Optional) Train or refresh the model
Open backend/model_training.ipynb in Jupyter/VS Code and run it to:
- Load
data/auth_logs_raw.csv. - Engineer features and fit the Isolation Forest.
- Save
isoforest_model.pklandscaler.pkl.
- Start the FastAPI backend
If you are using a conda environment (e.g. llms):
cd backend
conda run -n llms python -m uvicorn anomaly_api:app --host 0.0.0.0 --port 8000If you are using a virtualenv / venv and have it activated:
cd backend
python -m uvicorn anomaly_api:app --host 0.0.0.0 --port 8000Key endpoints:
POST /predict– score a single login event, send email alert on anomaly, log todata/realtime_events.csv.GET /events?limit=0– all historical + real‑time events.GET /metrics– global metrics and series for dashboards.
- Install dependencies
cd frontend
npm install- Configure backend URL (optional)
By default the frontend will call http://<current-host>:8000.
To override, create frontend/.env.local:
NEXT_PUBLIC_BACKEND_URL=http://127.0.0.1:8000- Run the Next.js dev server
cd frontend
npm run devThen open:
http://localhost:3000/– landing page.http://localhost:3000/user– user login UI (calls/predict).http://localhost:3000/dashboard– SOC dashboard (consumes/eventsand/metrics).
-
Data & model
- Historical auth data lives in
backend/data/auth_logs_raw.csv. model_training.ipynbprepares features and trains the Isolation Forest and scaler.anomaly_api.pyloads both and enforces a Zero‑Trust rule (event_label != "normal"→ anomaly).
- Historical auth data lives in
-
Real‑time scoring
- Frontend
/userform sends a JSON login event toPOST /predict. - Backend:
- Scores the event with the Isolation Forest.
- Classifies as
"normal"or"anomaly". - Sends an email alert when an anomaly is found.
- Appends a row to
backend/data/realtime_events.csvtagged with the decision.
- Frontend
-
Dashboards
- Backend helper
_load_events_df()merges:- Historical
auth_logs_raw.csv - Real‑time
realtime_events.csv
- Historical
/metricsand/eventsfeed both:- The legacy Streamlit views (if you re‑add them) and
- The Next.js dashboard at
/dashboard, which:- Shows filtered KPIs (total events, anomalies, anomaly rate, MTTD).
- Renders login events over time (by date) and anomalies by type.
- Provides location‑level analytics and a filterable event table.
- Backend helper
Email alerts are sent when anomalies are detected by the backend.
Create a .env file (not committed to git) with:
EMAIL_USER="[email protected]"
EMAIL_PASSWORD="your_16_char_app_password"
SMTP_SERVER="smtp.gmail.com"
SMTP_PORT=587See backend/EMAIL_SETUP.md for detailed guidance on configuring Gmail / SMTP securely.
- Large derived artifacts and secrets are ignored via
.gitignore:backend/data/processed/,*.pkl,*.joblib,.env, etc.
- Before pushing, double‑check:
- No secrets are committed (especially
.env, API keys, or real email passwords). requirements.txtandfrontend/package.jsonreflect the dependencies you’re using.
- No secrets are committed (especially
Example initialisation for a new GitHub repo:
git init
git add .
git commit -m "Initial commit: Zero-Trust anomaly detection platform"
git branch -M main
git remote add origin <your-repo-url>
git push -u origin main