Codebase Deep Dive¶
This document explains what exists folder-by-folder and module-by-module, with practical guidance for extending behavior safely.
Repository layout¶
cli/: CLI package (Typer app, interactive UX, subcommands) -- replaces former monolithiccli.pymain.py: FastAPI app, static mounts, websocket endpointsapi/: REST handlers for scan/tools/report/health operationscore/: install logic, wrappers, parsers, persistence, reporting, logging/comms, shared utilitiescore/parsers/: per-tool output parsers -- extracted from formerreporter.pyfrontend/: multi-page SPA browser dashboardtemplates/: report rendering templatesscripts/: install/release/VM orchestration scriptstests/: unit and integration testsdocs/: maintained documentation setpyproject.toml: project metadata and dependencies -- replaces deletedsetup.py
Deleted files¶
| Former file | Replacement |
|---|---|
cli.py (monolithic, 973 lines) | cli/ package |
core/report_gen.py | Merged into core/reporter.py |
setup.py | pyproject.toml |
cli/ (package)¶
The former monolithic cli.py has been split into focused submodules:
| Module | Responsibilities |
|---|---|
cli/__init__.py | Typer app construction, top-level command wiring, version callback (via importlib.metadata) |
cli/styles.py | Rich console themes, shared style constants |
cli/menu.py | Interactive menu and UX loops |
cli/server.py | Server start/stop/status/logs wrappers |
cli/scan.py | Scan orchestration (run_quick_scan), flags: --json, --exit-code, --quiet, --verbose, --only-failures |
cli/tools_cmd.py | tools status / tools install subcommands |
cli/history.py | History browsing and report export |
cli/elevation.py | Privilege elevation gating (ensure_root) |
Key commands:
scanhistorytools status/tools installserver run|start|status|logs|stopdoctor(new -- environment health check)findings <scan_id>(new -- show parsed findings for a scan)
Notes:
- Uses command whitelist for non-root commands.
- Background server process writes timestamped logs under
<runtime_home>/logs/web_server_<YYYYmmdd_HHMMSS>.log.
main.py¶
Responsibilities:
- FastAPI construction + lifespan checks.
- Static routes for UI assets.
- WebSocket handlers (
/ws,/ws/, legacy compatibility routes). - Router include wiring (
api/scan.py,api/report.py,api/tools.py).
Operational details:
- WebSocket limits patched via
core/ws_patch.pyto avoid header-size handshake failures. - CORS enabled permissively for local dashboard and testing scenarios.
api/¶
scan.py¶
- Start background scans.
- Return status/history with pagination support.
- Generate consolidated report for scan IDs.
GET /scan/{id}/findings-- parsed findings for a scan.GET /scan/{id}/timeline-- scan event timeline.GET /scan/{id}/logs-- per-tool log retrieval.DELETE /scan/{id}-- delete a scan and its artifacts.
tools.py¶
- Tool compatibility/status inspection.
- Tool install triggering.
report.py¶
- PDF endpoint and report-file download helpers.
Health¶
GET /api/health-- application health check endpoint.
core/¶
New shared modules¶
| Module | Purpose |
|---|---|
core/deps.py | Singleton ToolManager instance (avoids re-creating across modules) |
core/constants.py | Shared regex patterns, magic values, and reusable constants |
core/os_detect.py | Unified OS/distro/version detection (replaces scattered detection in tool_manager.py and wrappers) |
core/ws_patch.py | WebSocket header-size and handshake patch applied at startup |
tool_manager.py¶
Core installer/orchestration logic:
- Package manager dispatch (
apt,dnf/yum,apk,pacman,zypper) - Distro-version package mapping resolution (OS detection now delegates to
core/os_detect.py) - Unsupported distro/version skip guards
- Tool verify hooks
- Windows Java bootstrap for CIS-CAT
tools_config.py¶
Static metadata:
- Tool definitions per OS
- Package mapping table (
PACKAGE_MAPPINGS) - Per-tool notes and compatibility restrictions
wrappers/linux.py¶
Scanner execution:
- OpenSCAP content autodiscovery and profile selection
- Lynis invocation + output paths
- USG invocation (when available)
- CIS-CAT logic extracted to
wrappers/ciscat_linux.py
wrappers/ciscat_linux.py (new)¶
- CIS-CAT benchmark selection and execution on Linux
- Extracted from
linux.pyfor separation of concerns
wrappers/windows.py¶
Scanner execution:
- HardeningKitty invocation (PowerShell)
- CIS-CAT runner invocation
- SCT (
LGPO) integration path
core/parsers/ (new package)¶
Per-tool output parsers, extracted from the former monolithic reporter.py:
| Module | Parses |
|---|---|
core/parsers/oscap.py | OpenSCAP XML ARF output |
core/parsers/lynis.py | Lynis .dat log files |
core/parsers/hardeningkitty.py | HardeningKitty CSV output |
core/parsers/ciscat.py | CIS-CAT results |
reporter.py (slim orchestrator)¶
- Aggregates parsed findings from
core/parsers/* - Generates consolidated PDF/HTML reports
report_gen.pyhas been merged into this module and deleted- Handles partial/missing artifacts with explicit failure entries
database.py¶
- SQLite with WAL mode
- Lazy initialization -- no module-level
init_db()call; DB is created on first access completed_atcolumn for scan completion trackingdelete_scan()andcount_scans()helpers- Pagination support for scan queries
scan_logger.py¶
- Per-tool log files (e.g.,
<scan_id>/lynis.ndjson,<scan_id>/openscap.ndjson) - Lazy directory initialization (dirs created on first write)
logger.py¶
- Standard logging configuration
- JSON structured logging via
LOG_FORMAT=jsonenvironment variable
utils.py¶
- OS helper utilities
get_clean_env()for subprocess safety- command stream helpers
comms.py¶
- WebSocket connection management + broadcasts
scripts/¶
- Artifact download and VM test orchestration
- In-VM Linux and Windows test runners
- Install scripts (
install.sh,install.ps1) - Inno Setup file for Windows installer
frontend/ and templates/¶
The frontend is a multi-page SPA with hash-based routing:
| Page | Description |
|---|---|
| Dashboard | KPI cards, compliance overview |
| Scans | Scan list with pagination, per-scan inline logs, modal log viewer |
| Tools | Tool status and install controls |
| Logs | Aggregated log browser |
Additional features: - Dark/light theme toggle - Toast notifications - CSV/JSON export
Report HTML templates for PDF generation are in templates/. Both frontend/ and templates/ must be bundled in packaged binaries.
tests/¶
- API, DB, wrappers, websocket, tool-manager unit coverage
- integration and docker-assisted test options
Version management¶
The package version is read from importlib.metadata at runtime. It is defined solely in pyproject.toml (not hardcoded anywhere in source).
Extension guidelines¶
- Add or change tool definitions in
core/tools_config.pyfirst. - Keep installer behavior in
core/tool_manager.pygeneric and data-driven. - If scanner invocation changes, update wrapper + parser (in
core/parsers/) + docs together. - New parsers go in
core/parsers/and are wired intocore/reporter.py. - New CLI subcommands go in
cli/as separate modules and are registered incli/__init__.py. - For any packaging-affecting path change, update
.github/workflows/release.yml. - For any release behavior change, update VM runner scripts and docs in one PR.