# hanchuanchuan/inception architecture

> Architecture diagram of the GitHub repository hanchuanchuan/inception, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-12). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/hanchuanchuan/inception
- Repository: https://github.com/hanchuanchuan/inception

## Overview

##### Project type and purpose
- This is an open-source SQL auditing and execution tool (“Inception”) built as a fork of MySQL 5.6.10.
- It embeds the real MySQL parser and execution path (with storage engines stripped out) to analyze, enforce rules on, execute or roll back arbitrary SQL statements.
- Offers two user-facing fronts: a command-line shell (client) and a web UI.

##### High-level architecture and main components
a. Presentation layer
– CLI client: located under `/client`, compiled into `inception` binary.
– Web UI: a lightweight HTTP server front (not shown in this tree, but part of higher-level docs) that calls the same engine APIs.
b. Core engine
– Embedded server library (`/libmysqld`): hosts the modified MySQL server core in process.
– Parser & analyzer (`/sql`): MySQL’s SQL parser, optimizer stubs, rule-engine hooks.
– Rule engine & checker: custom validation modules inserted into the parse/optimize path.
– Rollback generator: produces inverse SQL statements based on DML analysis.
c. Client library
– `/libmysql`: client-side API used by both the CLI and the web UI to talk to the embedded engine or remote servers.
d. I/O abstraction
– `/vio`: abstracts sockets, SSL, pipes for communication with back-end MySQL servers.
– `/libevent`: event loop for nonblocking I/O (used by both CLI readline and optional network modules).
e. External services
– Upstream MySQL instances: Inception connects over TCP/SSL to target production servers, forwards approved statements, and captures results/metadata.

##### Relationships and data flow
1. User → CLI or Web UI (HTTP).
2. UI calls libmysql/libmysqld interfaces → passes raw SQL.
3. Core engine (embedded server) invokes parser → rule engine → if violation, returns error.
4. If approved or in “execute” mode: engine forwards SQL to external MySQL over vio/libevent.
5. Results and binlog events are captured; rollback SQL is generated and stored (in memory or log files).
6. UI presents execution outcome and rollback script.

##### Important architectural patterns
- Modular monolith in C/C++: separates client, engine, I/O, utilities into libraries.
- Plugin–style services: `/include/mysql/...` and `/mysys`, `/service_*.h` define service interfaces for authentication, thread scheduling, audit.
- Build configurability via CMake: each component (client, libmysql, libmysqld, libevent, vio, testing, packaging) is a CMake target.

##### Technologies and frameworks
- CMake (configuration & cross-platform build)
- libevent (event notifications)
- Embedded MySQL server core (parser + optimizer stubs)
- Custom rule engine modules in `/sql`
- Readline/libedit (`/cmd-line-utils/libedit`) for interactive shell
- Packaging scripts for MSI, RPM, Solaris

- Clear component boundaries and labels
- Notes on key interactions (e.g., “SQL parse → validate → execute/rollback”)
- Reference to major directories as documentation call-outs (e.g., `/sql`, `/client`, `/libmysqld`)

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% User and UI Layer
    User("User"):::actor
    subgraph "Presentation Layer"
        CLI["CLI Client"]:::ui
        WebUI["Web UI"]:::ui
    end

    %% Client Library
    subgraph "Client Library"
        LibMySQL["libmysql"]:::library
    end

    %% Embedded Engine Core
    subgraph "Embedded Engine" 
        LibMySQLD["Embedded Server Library<br/>(libmysqld)"]:::engine
        Parser["SQL Parser & Analyzer<br/>(sql)"]:::engine
        RuleEngine["Rule Engine & Checker"]:::engine
        Rollback["Rollback Generator"]:::engine
    end

    %% I/O Abstraction
    subgraph "I/O Abstraction"
        VIO["VIO"]:::io
        LibEvent["libevent"]:::io
    end

    %% External Services
    ExternalDB["External MySQL Instances"]:::external

    %% Data Flows
    User -->|Uses| CLI
    User -->|Uses HTTP| WebUI

    CLI -->|libmysql API| LibMySQL
    WebUI -->|libmysql API| LibMySQL

    LibMySQL -->|In-process API| LibMySQLD

    LibMySQLD -->|invokes| Parser
    Parser -->|hooks into| RuleEngine
    RuleEngine -->|on approve| Rollback

    Rollback -->|for DML| LibMySQLD
    LibMySQLD -->|TCP/SSL via vio| VIO
    VIO -->|event loop| LibEvent
    LibEvent -->|sends SQL| ExternalDB

    ExternalDB -->|results/binlog| LibEvent
    LibEvent --> LibMySQLD
    LibMySQLD -->|returns result/rollback| LibMySQL
    LibMySQL --> CLI
    LibMySQL --> WebUI

    %% Click Events
    click CLI "https://github.com/hanchuanchuan/inception/tree/master/client/"
    click LibMySQLD "https://github.com/hanchuanchuan/inception/tree/master/libmysqld/"
    click Parser "https://github.com/hanchuanchuan/inception/tree/master/sql/"
    click RuleEngine "https://github.com/hanchuanchuan/inception/blob/master/sql/sql_audit.cc"
    click RuleEngine "https://github.com/hanchuanchuan/inception/blob/master/sql/sql_audit.h"
    click Rollback "https://github.com/hanchuanchuan/inception/blob/master/sql/binlog.cc"
    click Rollback "https://github.com/hanchuanchuan/inception/blob/master/sql/binlog.h"
    click LibMySQL "https://github.com/hanchuanchuan/inception/tree/master/libmysql/"
    click VIO "https://github.com/hanchuanchuan/inception/tree/master/vio/"
    click LibEvent "https://github.com/hanchuanchuan/inception/tree/master/libevent/"
    click ExternalDB "https://github.com/hanchuanchuan/inception/tree/master/include/mysql/"

    %% Styles
    classDef actor fill:#f9f,stroke:#333,stroke-width:2px
    classDef ui fill:#bbdefb,stroke:#0d47a1,stroke-width:1px
    classDef library fill:#ffe0b2,stroke:#e65100,stroke-width:1px
    classDef engine fill:#c8e6c9,stroke:#1b5e20,stroke-width:1px
    classDef io fill:#ffccbc,stroke:#bf360c,stroke-width:1px
    classDef external fill:#eceff1,stroke:#37474f,stroke-width:1px
```
