How ra1nb0rn/cpe_search works
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