# ra1nb0rn/cpe_search architecture

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

- Interactive diagram: https://gitdiagram.com/ra1nb0rn/cpe_search
- Repository: https://github.com/ra1nb0rn/cpe_search

## Overview

##### Project type and purpose
- This is a single-process Python CLI tool named **cpe_search**.
- Its purpose is to let users query software names/titles on the command line, fetch matching CPE 2.3 strings from a precomputed local database (or update that database via the NVD API), and print results for vulnerability searches.

##### File structure
- Top level:
- **cpe_search.py** – the CLI entry point
- **database_wrapper_functions.py** – database abstraction (connect, initialize, query)
- **config.json** + **config_mariadb.json** – runtime configs for SQLite vs. MariaDB
- **create_sql_statements.json** – table definitions/DDL for DB initialization
- **requirements.txt** – Python dependencies
- **test_cpes.py**, **test_cpe_suggestions.py** – unit tests for core functionality
- **.github/workflows/tests.yml** – CI pipeline to install dependencies and run tests
- No separate frontend; it’s a command-line application. It uses:
- A local database (SQLite by default, MariaDB optionally)
- External NVD REST API to download/update the full CPE dataset

##### README for additional insights
- Describes a one-time setup: download all CPE entries from NVD, precompute internal data structures, store them locally.
- Exposes a small set of CLI arguments: update, api-key, count, query, verbose.
- Output is plain text, optionally piped to other tools.

a. Main components
- **CLI Interface** (cpe_search.py)
- **Configuration Loader** (reads config.json / config_mariadb.json)
- **Database Layer** (database_wrapper_functions.py + SQL DDL from create_sql_statements.json)
- **NVD API Client** (internal module in cpe_search.py)
- **Data Preprocessor** (parses downloaded CPE JSON, builds similarity index)
- **Search Engine** (computes string similarity, returns top-K results)
- **Output Formatter** (prints CPE string + similarity scores)
- **Tests & CI** (test suite + GitHub Actions workflow)
b. Relationships and interactions
1. **User** invokes **CLI Interface**
2. CLI calls **Configuration Loader** → obtains DB connection parameters
3. CLI checks DB state via **Database Layer**
– If “--update” flag set or first run → invoke **NVD API Client** → download raw CPE data → pass to **Data Preprocessor** → store processed data in **Database Layer**
4. For each “--query” → CLI passes query text to **Search Engine** → loads similarity index from **Database Layer** → computes nearest matches → sends results to **Output Formatter**
5. **Output Formatter** writes to stdout (possibly piped to other tools)
6. **Tests & CI** run unit tests against **Database Layer**, **Search Engine**, and CLI argument parsing to ensure correctness on each push
c. Architectural patterns/design principles
- **Layered architecture**: clear separation between CLI, config, data access, business logic (preprocessing + search), and output.
- **Adapter/Wrapper pattern** for database interactions, allowing pluggable backends (SQLite vs. MariaDB).
- **Single responsibility**: each module has a distinct role (API client vs. preprocessor vs. search algorithm).
d. Key technologies/frameworks
- Python 3.x
- SQLite (default) / MariaDB (optional)
- Requests (or urllib) for NVD REST API
- JSON for configs and DDL definitions
- pytest (or unittest) for testing
- GitHub Actions for CI

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% External Dependencies
    subgraph "External Services"
        direction TB
        NVD_API["NVD REST API"]:::external
    end

    %% Storage
    subgraph "Storage"
        direction TB
        Local_DB[(Local Database)]:::infra
    end

    %% Core Modules
    subgraph "Core Modules"
        direction TB
        CLI_Interface["CLI Interface"]:::io
        Config_Loader["Configuration Loader"]:::infra
        DB_Layer["Database Layer"]:::infra
        Data_Preprocessor["Data Preprocessor"]:::processing
        Search_Engine["Search Engine"]:::processing
        Output_Formatter["Output Formatter"]:::io
    end

    %% Config Loader Files
    subgraph "Config Files"
        direction TB
        Config_JSON["config.json"]:::infra
        Config_Maria["config_mariadb.json"]:::infra
    end

    %% DB Layer Components
    subgraph "DB Layer Files"
        direction TB
        DB_Wrapper["database_wrapper_functions.py"]:::infra
        DDL_File["create_sql_statements.json"]:::infra
    end

    %% Test & CI
    subgraph "Tests & CI"
        direction TB
        Test_CPEs["test_cpes.py"]:::infra
        Test_Suggestions["test_cpe_suggestions.py"]:::infra
        CI_Workflow[".github/workflows/tests.yml"]:::infra
    end

    %% Flows
    User((User)):::external
    User -->|"invokes"| CLI_Interface
    CLI_Interface -->|"load configs"| Config_Loader
    Config_Loader --> Config_JSON
    Config_Loader --> Config_Maria
    CLI_Interface -->|"check DB state"| DB_Layer
    DB_Layer --> DB_Wrapper
    DB_Layer --> DDL_File
    DB_Layer -->|"execute SQL"| Local_DB

    CLI_Interface -->|"--update flag"| NVD_API
    NVD_API -->|"download CPE JSON"| Data_Preprocessor
    Data_Preprocessor -->|"store processed data"| DB_Layer

    CLI_Interface -->|"--query input"| Search_Engine
    Search_Engine -->|"load index"| DB_Layer
    Search_Engine -->|"query DB"| Local_DB
    Search_Engine -->|"compute similarity"| Search_Engine
    Search_Engine --> Output_Formatter

    Output_Formatter -->|"print results"| Console((stdout)):::io

    %% Tests & CI flows
    CI_Workflow -->|runs tests| Test_CPEs
    CI_Workflow -->|runs tests| Test_Suggestions

    %% Click Events
    click CLI_Interface "https://github.com/ra1nb0rn/cpe_search/blob/master/cpe_search.py"
    click Data_Preprocessor "https://github.com/ra1nb0rn/cpe_search/blob/master/cpe_search.py"
    click Search_Engine "https://github.com/ra1nb0rn/cpe_search/blob/master/cpe_search.py"
    click Output_Formatter "https://github.com/ra1nb0rn/cpe_search/blob/master/cpe_search.py"
    click Config_JSON "https://github.com/ra1nb0rn/cpe_search/blob/master/config.json"
    click Config_Maria "https://github.com/ra1nb0rn/cpe_search/blob/master/config_mariadb.json"
    click DB_Wrapper "https://github.com/ra1nb0rn/cpe_search/blob/master/database_wrapper_functions.py"
    click DDL_File "https://github.com/ra1nb0rn/cpe_search/blob/master/create_sql_statements.json"
    click Test_CPEs "https://github.com/ra1nb0rn/cpe_search/blob/master/test_cpes.py"
    click Test_Suggestions "https://github.com/ra1nb0rn/cpe_search/blob/master/test_cpe_suggestions.py"
    click CI_Workflow "https://github.com/ra1nb0rn/cpe_search/blob/master/.github/workflows/tests.yml"

    %% Styles
    classDef io fill:#ADD8E6,stroke:#333,stroke-width:1px
    classDef processing fill:#90EE90,stroke:#333,stroke-width:1px
    classDef infra fill:#FFA500,stroke:#333,stroke-width:1px
    classDef external fill:#D3D3D3,stroke:#333,stroke-width:1px
```
