# achraf-farhat/octopus architecture

> Architecture diagram of the GitHub repository achraf-farhat/octopus, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/achraf-farhat/octopus
- Repository: https://github.com/achraf-farhat/octopus

## Overview

1. **System shape**
   - Octopus is a containerized security-operations platform split into a React frontend, a FastAPI backend, and external security/AI services. The backend is the control plane that owns auth, business logic, persistence, and integration with Wazuh and the local LLM.

2. **Frontend presentation layer**
   - `frontend/src` is a single-page app built with Vite + React and styled with Tailwind.
   - Pages are domain-oriented: Dashboard, Incidents, Playbooks, Rules, ThreatHunt, Users, and Login.
   - Shared UI components include alert tables, trend charts, AI explanation panels, permission gating, and route protection.
   - `context/AuthContext.jsx` and `lib/auth.js` manage session state; `lib/api.js` is the API client boundary to the backend.

3. **Frontend access control and session flow**
   - The frontend enforces route protection with `ProtectedRoute` and component-level authorization with `PermissionGate`.
   - Login is a backend-issued JWT flow; refresh-token support is reflected in the backend model set.
   - After authentication, the UI reads role/permission state from the auth context and selectively renders SOC functionality.

4. **Backend API layer**
   - `backend/app/main.py` is the FastAPI entrypoint, with `bootstrap.py` handling startup wiring.
   - API surface is split by domain routers: `auth`, `users`, `alerts`, `incidents`, `rules`, `playbooks`, `threat_hunt`, `ai`, and `health`.
   - `deps.py` likely centralizes request dependencies such as current user, DB session, and RBAC checks.

5. **Security and authorization boundary**
   - `core/security.py` and `core/rbac.py` define JWT handling and role-based access control.
   - The README-defined roles are enforced across the API: Admin, SOC_Manager, SOC_L3, SOC_L2, SOC_L1.
   - Security-sensitive state is persisted through `refresh_token` and `audit_log` models, which support token lifecycle and traceability.

6. **Persistence layer**
   - PostgreSQL is the primary system of record, accessed through SQLAlchemy-style models in `backend/app/models`.
   - The schema is domain-centric: alerts, incidents, custom rules, playbooks, playbook executions, AI analyses, search history, threat-hunt messages, users, refresh tokens, and audit logs.
   - `db/base.py` and `db/session.py` form the database boundary; everything else depends on those shared primitives.

7. **Security operations domain model**
   - Alerts and incidents represent operational detections and their triage lifecycle.
   - Rules and custom rules model detection content and generated Wazuh XML rules.
   - Playbooks and playbook executions represent response automation and execution history.
   - Search history and threat-hunt messages capture analyst-driven investigation context over time.

8. **AI subsystem**
   - `services/ollama_client.py` and `services/ai_prompts.py` connect the backend to the local Ollama-hosted LLM.
   - The AI API is used for natural-language threat hunting, alert explanation, and rule generation.
   - `schemas/threat_hunt.py` and related models indicate conversational workflows with persisted message history and analysis outputs.

9. **External security integrations**
   - `services/wazuh_client.py` is the integration boundary to Wazuh Manager for querying security data and managing rules/agents.
   - Suricata is part of the overall security stack from an architectural perspective, but it is external to this repo and likely feeds events into Wazuh.
   - The backend sits between analysts and these systems, translating UI actions and natural language into operational queries and rule changes.

10. **Async/background processing**
   - `celery_app.py` and `tasks.py` define asynchronous processing on top of Celery with Redis as broker/cache, matching the README.
   - This layer is where alert correlation, risk scoring, and MITRE ATT&CK enrichment are offloaded so the API stays responsive.
   - Background work produces persisted analysis artifacts that the frontend can later display.

11. **Auditability and governance**
   - `services/audit.py` plus the `audit_log` model create an explicit audit trail for security actions and administrative changes.
   - This is important because the platform can generate, explain, and deploy detection content; those actions need traceability and accountability.

12. **Deployment/runtime boundaries**
   - The repo is designed for Docker Compose orchestration, with separate Dockerfiles for backend and frontend.
   - The backend expects environment-based configuration (`.env.example` files); the frontend is built and served through Nginx in production.
   - Runtime dependencies called out by the repo are PostgreSQL, Redis, a local Ollama instance, and external Wazuh connectivity.

13. **Primary end-to-end data flows**
   - Analyst logs in through the React app → FastAPI auth issues JWT/refresh tokens → frontend stores session state.
   - UI actions call domain routers → backend validates RBAC → persists or reads from PostgreSQL → optionally calls Wazuh or Ollama.
   - Long-running enrichment/correlation jobs are queued to Celery → results are stored back in the database → frontend consumes them in dashboards and detail views.

14. **How to draw the architecture**
   - Place the React SPA at the edge, the FastAPI backend in the center, PostgreSQL/Redis as internal infrastructure, and Wazuh/Ollama as external engines.
   - Show the backend as the hub with distinct lanes for auth/security, SOC domain APIs, AI services, and async tasks.
   - Represent PostgreSQL as the source of truth for users, incidents, alerts, rules, playbooks, audit logs, and AI/chat history.

Main entry points: [`backend/app/routers`](https://github.com/achraf-farhat/octopus/tree/main/backend/app/routers) (Domain Routers), [`frontend/src/App.jsx`](https://github.com/achraf-farhat/octopus/blob/main/frontend/src/App.jsx) (Octopus UI), [`backend/app/models`](https://github.com/achraf-farhat/octopus/tree/main/backend/app/models) (Domain Models), [`backend/app/tasks.py`](https://github.com/achraf-farhat/octopus/blob/main/backend/app/tasks.py) (Async Tasks)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_frontend["Frontend SPA"]
  node_frontend_app["Octopus UI<br/>React SPA<br/>[App.jsx]"]
  node_frontend_pages["Domain Pages<br/>views"]
  node_frontend_components["Shared UI<br/>components"]
  node_frontend_auth["Session State<br/>auth client<br/>[AuthContext.jsx]"]
end

subgraph group_backend["Backend API"]
  node_backend_main["API Entry<br/>FastAPI app<br/>[main.py]"]
  node_backend_bootstrap["Bootstrap<br/>startup wiring<br/>[bootstrap.py]"]
  node_backend_routers["Domain Routers<br/>API routes"]
  node_backend_security["Security Core<br/>jwt/rbac<br/>[security.py]"]
  node_backend_deps["Request Deps<br/>dependencies<br/>[deps.py]"]
  node_backend_models[("Domain Models<br/>SQLAlchemy models")]
  node_backend_db[("DB Boundary<br/>database layer")]
  node_backend_services_ai["AI Service<br/>llm integration<br/>[ollama_client.py]"]
  node_backend_services_wazuh["Wazuh Service<br/>security integration<br/>[wazuh_client.py]"]
  node_backend_services_audit["Audit Service<br/>governance<br/>[audit.py]"]
  node_backend_tasks["Async Tasks<br/>celery jobs<br/>[tasks.py]"]
end

subgraph group_infra["Runtime Infra"]
  node_infra_pg[("PostgreSQL<br/>system of record")]
  node_infra_redis["Redis<br/>broker/cache"]
end

subgraph group_external["External Engines"]
  node_external_ollama(("Ollama<br/>local LLM"))
  node_external_wazuh{{"Wazuh Manager<br/>security platform"}}
end

node_frontend_app -->|"renders"| node_frontend_pages
node_frontend_app -->|"composes"| node_frontend_components
node_frontend_app -->|"uses"| node_frontend_auth
node_frontend_auth -->|"calls API"| node_backend_routers
node_frontend_pages -->|"requests data"| node_backend_routers
node_backend_main -->|"starts with"| node_backend_bootstrap
node_backend_main -->|"mounts"| node_backend_routers
node_backend_routers -->|"guards with"| node_backend_deps
node_backend_deps -->|"enforces"| node_backend_security
node_backend_routers -->|"reads/writes"| node_backend_models
node_backend_models -->|"mapped on"| node_backend_db
node_backend_db -->|"persists to"| node_infra_pg
node_backend_routers -->|"records"| node_backend_services_audit
node_backend_services_audit -->|"writes logs"| node_backend_models
node_backend_routers -->|"invokes"| node_backend_services_ai
node_backend_services_ai -->|"queries"| node_external_ollama
node_backend_routers -->|"integrates"| node_backend_services_wazuh
node_backend_services_wazuh -->|"talks to"| node_external_wazuh
node_backend_tasks -->|"queued by"| node_infra_redis
node_backend_tasks -->|"stores results"| node_backend_models
node_backend_routers -->|"offloads"| node_backend_tasks
node_frontend_app -->|"consumes"| node_backend_routers

click node_frontend_app "https://github.com/achraf-farhat/octopus/blob/main/frontend/src/App.jsx"
click node_frontend_pages "https://github.com/achraf-farhat/octopus/tree/main/frontend/src/pages"
click node_frontend_components "https://github.com/achraf-farhat/octopus/tree/main/frontend/src/components"
click node_frontend_auth "https://github.com/achraf-farhat/octopus/blob/main/frontend/src/context/AuthContext.jsx"
click node_backend_main "https://github.com/achraf-farhat/octopus/blob/main/backend/app/main.py"
click node_backend_bootstrap "https://github.com/achraf-farhat/octopus/blob/main/backend/app/bootstrap.py"
click node_backend_routers "https://github.com/achraf-farhat/octopus/tree/main/backend/app/routers"
click node_backend_security "https://github.com/achraf-farhat/octopus/blob/main/backend/app/core/security.py"
click node_backend_deps "https://github.com/achraf-farhat/octopus/blob/main/backend/app/deps.py"
click node_backend_models "https://github.com/achraf-farhat/octopus/tree/main/backend/app/models"
click node_backend_db "https://github.com/achraf-farhat/octopus/tree/main/backend/app/db"
click node_backend_services_ai "https://github.com/achraf-farhat/octopus/blob/main/backend/app/services/ollama_client.py"
click node_backend_services_wazuh "https://github.com/achraf-farhat/octopus/blob/main/backend/app/services/wazuh_client.py"
click node_backend_services_audit "https://github.com/achraf-farhat/octopus/blob/main/backend/app/services/audit.py"
click node_backend_tasks "https://github.com/achraf-farhat/octopus/blob/main/backend/app/tasks.py"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_frontend_app,node_frontend_pages,node_frontend_components,node_frontend_auth toneBlue
class node_backend_main,node_backend_bootstrap,node_backend_routers,node_backend_security,node_backend_deps,node_backend_models,node_backend_db,node_backend_services_ai,node_backend_services_wazuh,node_backend_services_audit,node_backend_tasks toneAmber
class node_infra_pg,node_infra_redis toneMint
class node_external_ollama,node_external_wazuh toneRose
```

## Components

### Frontend SPA

React/Vite analyst interface and client-side access control

- [Octopus UI](https://github.com/achraf-farhat/octopus/blob/main/frontend/src/App.jsx): React SPA · Main analyst interface for SOC workflows · `frontend/src/App.jsx`
- [Domain Pages](https://github.com/achraf-farhat/octopus/tree/main/frontend/src/pages): views · Dashboard, incidents, playbooks, rules, threat hunt, users, login · `frontend/src/pages`
- [Shared UI](https://github.com/achraf-farhat/octopus/tree/main/frontend/src/components): components · Tables, charts, explanation panels, route protection, permission gating · `frontend/src/components`
- [Session State](https://github.com/achraf-farhat/octopus/blob/main/frontend/src/context/AuthContext.jsx): auth client · Auth context, JWT storage, and API client boundary · `frontend/src/context/AuthContext.jsx`

### Backend API

FastAPI control plane, domain services, and persistence

- [API Entry](https://github.com/achraf-farhat/octopus/blob/main/backend/app/main.py): FastAPI app · Application bootstrap and server entrypoint · `backend/app/main.py`
- [Bootstrap](https://github.com/achraf-farhat/octopus/blob/main/backend/app/bootstrap.py): startup wiring · Startup configuration and dependency setup · `backend/app/bootstrap.py`
- [Domain Routers](https://github.com/achraf-farhat/octopus/tree/main/backend/app/routers): API routes · Auth, users, alerts, incidents, rules, playbooks, threat hunt, AI, health · `backend/app/routers`
- [Security Core](https://github.com/achraf-farhat/octopus/blob/main/backend/app/core/security.py): jwt/rbac · JWT handling and role-based authorization · `backend/app/core/security.py`
- [Request Deps](https://github.com/achraf-farhat/octopus/blob/main/backend/app/deps.py): dependencies · Current user, DB session, and RBAC guards · `backend/app/deps.py`
- [Domain Models](https://github.com/achraf-farhat/octopus/tree/main/backend/app/models): SQLAlchemy models · Alerts, incidents, rules, playbooks, users, tokens, audits, and AI/chat history · `backend/app/models`
- [DB Boundary](https://github.com/achraf-farhat/octopus/tree/main/backend/app/db): database layer · SQLAlchemy base and session primitives · `backend/app/db`
- [AI Service](https://github.com/achraf-farhat/octopus/blob/main/backend/app/services/ollama_client.py): llm integration · Prompts and Ollama client for explanation, hunting, and rule generation · `backend/app/services/ollama_client.py`
- [Wazuh Service](https://github.com/achraf-farhat/octopus/blob/main/backend/app/services/wazuh_client.py): security integration · Query and manage Wazuh data and rules · `backend/app/services/wazuh_client.py`
- [Audit Service](https://github.com/achraf-farhat/octopus/blob/main/backend/app/services/audit.py): governance · Security action logging and traceability · `backend/app/services/audit.py`
- [Async Tasks](https://github.com/achraf-farhat/octopus/blob/main/backend/app/tasks.py): celery jobs · Background correlation, scoring, and enrichment work · `backend/app/tasks.py`

### Runtime Infra

Shared data and background processing services

- **PostgreSQL**: system of record · Persistent store for operational and governance data
- **Redis**: broker/cache · Celery broker and cache runtime

### External Engines

Third-party or out-of-repo security systems

- **Ollama**: local LLM · Local model runtime used by backend AI features
- **Wazuh Manager**: security platform · External detection and rule management system

## Connections

- Octopus UI → Domain Pages: renders · SPA routes to domain-specific screens
- Octopus UI → Shared UI: composes · Uses shared UI building blocks
- Octopus UI → Session State: uses · Reads session and permission state
- Session State → Domain Routers: calls API · Client API boundary into backend domains
- Domain Pages → Domain Routers: requests data · Dashboard and workflow screens fetch backend resources
- API Entry → Bootstrap: starts with · Entrypoint delegates startup wiring
- API Entry → Domain Routers: mounts · Registers domain API surface
- Domain Routers → Request Deps: guards with · Applies dependency-based auth and DB context
- Request Deps → Security Core: enforces · Current user and RBAC checks
- Domain Routers → Domain Models: reads/writes · Domain endpoints persist and fetch operational state
- Domain Models → DB Boundary: mapped on · Models sit on shared DB primitives
- DB Boundary → PostgreSQL: persists to · Primary database backend
- Domain Routers → Audit Service: records · Administrative and security actions are audited
- Audit Service → Domain Models: writes logs · Creates audit trail records
- Domain Routers → AI Service: invokes · AI explanations, hunting, and rule assistance
- AI Service → Ollama: queries · LLM inference boundary
- Domain Routers → Wazuh Service: integrates · Reads detections and manages rules/agents
- Wazuh Service → Wazuh Manager: talks to · External SOC platform integration
- Async Tasks → Redis: queued by · Celery broker/cache runtime
- Async Tasks → Domain Models: stores results · Background jobs persist analysis artifacts
- Domain Routers → Async Tasks: offloads · Long-running enrichment and correlation work
- Octopus UI → Domain Routers: consumes · UI drives backend workflows via API
