# theabdullahraja/database-intrusion-detection-system architecture

> Architecture diagram of the GitHub repository theabdullahraja/database-intrusion-detection-system, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-24). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/theabdullahraja/database-intrusion-detection-system
- Repository: https://github.com/theabdullahraja/database-intrusion-detection-system

## Overview

#### Purpose and execution surfaces
DIDS is a MySQL-backed intrusion detection/prevention layer that inspects SQL before execution, applies role authorization, records security events, and exposes operational reporting. Primary entry points are the interactive CLI in `dids_engine.py`, read-only Flask dashboard in `dashboard.py`, and localhost-only browser query console in `webconsole.py`.

#### Database security foundation
MySQL owns the persistent security model: protected target tables, users, rules, query/alert/audit records, blacklist and brute-force state. Schema creation and seed state are separated in `01_schema.sql` and `02_sample_data.sql`; database roles, reporting views, stored procedures, and triggers are defined in `03_roles_views_triggers.sql`. The Python layer depends on this initialized schema rather than owning a separate persistence model.

#### Protected-query engine
`dids_engine.py` is the central policy boundary. It authenticates application users, resolves their roles, analyzes submitted SQL against injection, DDL, privilege-escalation, command-injection, and data-dump patterns, including MySQL versioned-comment evasion. It computes risk, fails closed on authorization/security violations, and only sends approved SQL to MySQL. Both interactive interfaces route through this engine rather than directly exposing database access.

#### Prevention and incident flow
A query enters from CLI or web console with a client IP and authenticated identity. The engine evaluates IP blacklist/brute-force state, RBAC and role mismatch, then threat signatures and risk. Blocked activity produces alerts/audit records and may update incident or blacklist state; allowed activity is executed and logged. Critical events flow to SMTP when configured, otherwise to console notification. Core implementation is `dids_engine.py`, with database-side logging/enforcement support in `03_roles_views_triggers.sql`.

#### Reporting and administrative operations
Administrative CLI actions export alert, incident, and audit CSVs and generate standalone HTML security reports from persisted security data. Database `vw_*` views form the reporting contract consumed by the dashboard, avoiding a separate analytics store. Relevant paths: `dids_engine.py`, `03_roles_views_triggers.sql`, and `04_detection_queries.sql`.

#### Web boundaries and runtimes
Python is the application runtime; MySQL is required infrastructure. Flask serves the dashboard and web console. `dashboard.py` is read-only and displays security statistics, open critical alerts, brute-force sources, activity, and attack categories from the database views. `webconsole.py` is intentionally bound to localhost and submits SQL to the engine, preserving its inspection and authorization boundary.

#### Configuration and external services
Environment configuration supplies mandatory MySQL credentials and optional dashboard/web-console ports. SMTP is optional and config-gated for critical alert delivery; absent SMTP configuration uses safe console fallback. `.env.example` documents the expected environment contract, while `requirements.txt` supplies Python dependencies.

Main entry points: [`dids_engine.py`](https://github.com/theabdullahraja/database-intrusion-detection-system/blob/main/dids_engine.py) (Protected-query engine), [`.env.example`](https://github.com/theabdullahraja/database-intrusion-detection-system/blob/main/.env.example) (Environment contract), [`03_roles_views_triggers.sql`](https://github.com/theabdullahraja/database-intrusion-detection-system/blob/main/03_roles_views_triggers.sql) (vw_* reporting views), [`webconsole.py`](https://github.com/theabdullahraja/database-intrusion-detection-system/blob/main/webconsole.py) (Local web console)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_interfaces["Execution surfaces"]
  node_cli["Interactive CLI<br/>CLI entry point<br/>[dids_engine.py]"]
  node_webconsole["Local web console<br/>Flask interface<br/>[webconsole.py]"]
  node_dashboard["Read-only dashboard<br/>Flask interface<br/>[dashboard.py]"]
end

subgraph group_application["Python security layer"]
  node_engine{{"Protected-query engine<br/>policy boundary<br/>[dids_engine.py]"}}
  node_policy["RBAC and threat analysis<br/>security policy<br/>[dids_engine.py]"]
  node_incident["Incident handling<br/>security workflow<br/>[dids_engine.py]"]
  node_reports["Exports and HTML reports<br/>admin reporting<br/>[dids_engine.py]"]
end

subgraph group_database["MySQL security model"]
  node_schema[("Schema and seed data<br/>database initialization<br/>[01_schema.sql]")]
  node_seed["Sample security data<br/>seed data<br/>[02_sample_data.sql]"]
  node_db_enforcement["Roles, views, procedures, triggers<br/>database security layer"]
  node_mysql[("MySQL security database<br/>persistent store")]
  node_views["vw_* reporting views<br/>reporting contract"]
  node_detection_queries["Detection queries<br/>security queries"]
end

subgraph group_external["External services"]
  node_smtp["SMTP or console fallback<br/>notification delivery"]
  node_environment["Environment contract<br/>runtime configuration<br/>[.env.example]"]
  node_dependencies["Python dependencies<br/>dependency manifest<br/>[requirements.txt]"]
end

node_cli -->|"submits SQL / admin actions"| node_engine
node_webconsole -->|"submits inspected SQL"| node_engine
node_dashboard -->|"reads reporting data"| node_views
node_engine -->|"evaluates request"| node_policy
node_policy -->|"reads identity and security state"| node_mysql
node_policy -->|"blocked or critical activity"| node_incident
node_engine -->|"executes approved SQL and logs activity"| node_mysql
node_incident -->|"persists alerts and incident state"| node_mysql
node_incident -->|"critical notifications"| node_smtp
node_reports -->|"reads persisted security data"| node_views
node_cli -->|"administrative reporting"| node_reports
node_schema -->|"creates security model"| node_mysql
node_seed -->|"loads sample state"| node_mysql
node_db_enforcement -->|"installs database logic"| node_mysql
node_db_enforcement -->|"defines"| node_views
node_detection_queries -.->|"queries security data"| node_mysql
node_environment -.->|"MySQL and runtime settings"| node_engine
node_environment -.->|"port settings"| node_dashboard
node_environment -.->|"port settings"| node_webconsole
node_dependencies -.->|"provides runtime libraries"| node_engine

click node_cli "https://github.com/theabdullahraja/database-intrusion-detection-system/blob/main/dids_engine.py"
click node_webconsole "https://github.com/theabdullahraja/database-intrusion-detection-system/blob/main/webconsole.py"
click node_dashboard "https://github.com/theabdullahraja/database-intrusion-detection-system/blob/main/dashboard.py"
click node_engine "https://github.com/theabdullahraja/database-intrusion-detection-system/blob/main/dids_engine.py"
click node_policy "https://github.com/theabdullahraja/database-intrusion-detection-system/blob/main/dids_engine.py"
click node_incident "https://github.com/theabdullahraja/database-intrusion-detection-system/blob/main/dids_engine.py"
click node_reports "https://github.com/theabdullahraja/database-intrusion-detection-system/blob/main/dids_engine.py"
click node_schema "https://github.com/theabdullahraja/database-intrusion-detection-system/blob/main/01_schema.sql"
click node_seed "https://github.com/theabdullahraja/database-intrusion-detection-system/blob/main/02_sample_data.sql"
click node_db_enforcement "https://github.com/theabdullahraja/database-intrusion-detection-system/blob/main/03_roles_views_triggers.sql"
click node_views "https://github.com/theabdullahraja/database-intrusion-detection-system/blob/main/03_roles_views_triggers.sql"
click node_detection_queries "https://github.com/theabdullahraja/database-intrusion-detection-system/blob/main/04_detection_queries.sql"
click node_environment "https://github.com/theabdullahraja/database-intrusion-detection-system/blob/main/.env.example"
click node_dependencies "https://github.com/theabdullahraja/database-intrusion-detection-system/blob/main/requirements.txt"

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_cli,node_webconsole,node_dashboard toneBlue
class node_engine,node_policy,node_incident,node_reports toneAmber
class node_schema,node_seed,node_db_enforcement,node_mysql,node_views,node_detection_queries toneMint
class node_smtp,node_environment,node_dependencies toneRose
```

## Components

### Execution surfaces

- [Interactive CLI](https://github.com/theabdullahraja/database-intrusion-detection-system/blob/main/dids_engine.py): CLI entry point · Runs query and administrative operations. · `dids_engine.py`
- [Local web console](https://github.com/theabdullahraja/database-intrusion-detection-system/blob/main/webconsole.py): Flask interface · Localhost-only SQL submission interface. · `webconsole.py`
- [Read-only dashboard](https://github.com/theabdullahraja/database-intrusion-detection-system/blob/main/dashboard.py): Flask interface · Displays operational security reporting. · `dashboard.py`

### Python security layer

- [Protected-query engine](https://github.com/theabdullahraja/database-intrusion-detection-system/blob/main/dids_engine.py): policy boundary · Authenticates, authorizes, inspects, and executes approved SQL. · `dids_engine.py`
- [RBAC and threat analysis](https://github.com/theabdullahraja/database-intrusion-detection-system/blob/main/dids_engine.py): security policy · Checks identity, role mismatch, blacklist state, signatures, and risk. · `dids_engine.py`
- [Incident handling](https://github.com/theabdullahraja/database-intrusion-detection-system/blob/main/dids_engine.py): security workflow · Records blocked activity and updates alert or blacklist state. · `dids_engine.py`
- [Exports and HTML reports](https://github.com/theabdullahraja/database-intrusion-detection-system/blob/main/dids_engine.py): admin reporting · Generates CSV exports and standalone security reports. · `dids_engine.py`

### MySQL security model

- [Schema and seed data](https://github.com/theabdullahraja/database-intrusion-detection-system/blob/main/01_schema.sql): database initialization · Creates persistent security records, rules, users, and protected targets. · `01_schema.sql`
- [Sample security data](https://github.com/theabdullahraja/database-intrusion-detection-system/blob/main/02_sample_data.sql): seed data · `02_sample_data.sql`
- [Roles, views, procedures, triggers](https://github.com/theabdullahraja/database-intrusion-detection-system/blob/main/03_roles_views_triggers.sql): database security layer · Provides database-side enforcement, logging, and reporting views. · `03_roles_views_triggers.sql`
- **MySQL security database**: persistent store · Stores protected data, identities, rules, audits, alerts, and incident state.
- [vw_* reporting views](https://github.com/theabdullahraja/database-intrusion-detection-system/blob/main/03_roles_views_triggers.sql): reporting contract · Supplies security statistics and activity data to reporting consumers. · `03_roles_views_triggers.sql`
- [Detection queries](https://github.com/theabdullahraja/database-intrusion-detection-system/blob/main/04_detection_queries.sql): security queries · `04_detection_queries.sql`

### External services

- **SMTP or console fallback**: notification delivery · Critical events use SMTP when configured, otherwise console notification.
- [Environment contract](https://github.com/theabdullahraja/database-intrusion-detection-system/blob/main/.env.example): runtime configuration · Provides required MySQL credentials and optional service settings. · `.env.example`
- [Python dependencies](https://github.com/theabdullahraja/database-intrusion-detection-system/blob/main/requirements.txt): dependency manifest · `requirements.txt`

## Connections

- Interactive CLI → Protected-query engine: submits SQL / admin actions
- Local web console → Protected-query engine: submits inspected SQL
- Read-only dashboard → vw_* reporting views: reads reporting data
- Protected-query engine → RBAC and threat analysis: evaluates request
- RBAC and threat analysis → MySQL security database: reads identity and security state
- RBAC and threat analysis → Incident handling: blocked or critical activity
- Protected-query engine → MySQL security database: executes approved SQL and logs activity
- Incident handling → MySQL security database: persists alerts and incident state
- Incident handling → SMTP or console fallback: critical notifications
- Exports and HTML reports → vw_* reporting views: reads persisted security data
- Interactive CLI → Exports and HTML reports: administrative reporting
- Schema and seed data → MySQL security database: creates security model
- Sample security data → MySQL security database: loads sample state
- Roles, views, procedures, triggers → MySQL security database: installs database logic
- Roles, views, procedures, triggers → vw_* reporting views: defines
- Detection queries → MySQL security database: queries security data
- Environment contract → Protected-query engine: MySQL and runtime settings
- Environment contract → Read-only dashboard: port settings
- Environment contract → Local web console: port settings
- Python dependencies → Protected-query engine: provides runtime libraries
