opencra-cli 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- opencra_cli/__init__.py +3 -0
- opencra_cli/app.py +257 -0
- opencra_cli/db.py +176 -0
- opencra_cli/httputil.py +18 -0
- opencra_cli/kev.py +78 -0
- opencra_cli/match.py +88 -0
- opencra_cli/nvd.py +43 -0
- opencra_cli/osv.py +79 -0
- opencra_cli/pdf.py +125 -0
- opencra_cli/py.typed +1 -0
- opencra_cli/render.py +87 -0
- opencra_cli/syft.py +132 -0
- opencra_cli/sync.py +46 -0
- opencra_cli-0.1.0.dist-info/METADATA +13 -0
- opencra_cli-0.1.0.dist-info/RECORD +17 -0
- opencra_cli-0.1.0.dist-info/WHEEL +4 -0
- opencra_cli-0.1.0.dist-info/entry_points.txt +2 -0
opencra_cli/__init__.py
ADDED
opencra_cli/app.py
ADDED
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
"""OpenCRA Typer CLI."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import logging
|
|
6
|
+
from datetime import datetime, timezone
|
|
7
|
+
from enum import Enum
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
import typer
|
|
11
|
+
from opencra_shared.models import FailOn, OutputFormat, ScanResult
|
|
12
|
+
from rich.console import Console
|
|
13
|
+
|
|
14
|
+
from opencra_cli import __version__
|
|
15
|
+
from opencra_cli.db import Cache, default_db_path
|
|
16
|
+
from opencra_cli.kev import KevError, load_index, refresh
|
|
17
|
+
from opencra_cli.match import merge_matches
|
|
18
|
+
from opencra_cli.nvd import cvss_from_nvd, enrich_cve
|
|
19
|
+
from opencra_cli.osv import ping as osv_ping
|
|
20
|
+
from opencra_cli.osv import query_batch
|
|
21
|
+
from opencra_cli.pdf import PDF_HINT, export_report, weasyprint_status
|
|
22
|
+
from opencra_cli.render import format_payload, print_table, result_to_json, write_output
|
|
23
|
+
from opencra_cli.syft import SyftError, resolve_syft, scan_to_document, syft_version, version_ok
|
|
24
|
+
from opencra_cli.sync import ingest
|
|
25
|
+
|
|
26
|
+
app = typer.Typer(
|
|
27
|
+
name="opencra",
|
|
28
|
+
help="CRA Article 14 reporting readiness for your SBOM. A KEV hit is a candidate, not awareness.",
|
|
29
|
+
no_args_is_help=True,
|
|
30
|
+
)
|
|
31
|
+
console = Console()
|
|
32
|
+
err_console = Console(stderr=True)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
class FailOnOpt(str, Enum):
|
|
36
|
+
none = "none"
|
|
37
|
+
kev = "kev"
|
|
38
|
+
critical = "critical"
|
|
39
|
+
high = "high"
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
class FormatOpt(str, Enum):
|
|
43
|
+
table = "table"
|
|
44
|
+
json = "json"
|
|
45
|
+
cyclonedx = "cyclonedx"
|
|
46
|
+
spdx = "spdx"
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class EnrichOpt(str, Enum):
|
|
50
|
+
none = "none"
|
|
51
|
+
nvd = "nvd"
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
def _configure_logging(verbose: bool, quiet: bool) -> None:
|
|
55
|
+
level = logging.WARNING
|
|
56
|
+
if verbose:
|
|
57
|
+
level = logging.DEBUG
|
|
58
|
+
if quiet:
|
|
59
|
+
level = logging.ERROR
|
|
60
|
+
logging.basicConfig(level=level, format="%(levelname)s %(name)s: %(message)s")
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def _exit(code: int, message: str | None = None) -> None:
|
|
64
|
+
if message:
|
|
65
|
+
err_console.print(f"[red]{message}[/red]")
|
|
66
|
+
raise typer.Exit(code)
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def _run_scan(
|
|
70
|
+
target: str,
|
|
71
|
+
*,
|
|
72
|
+
offline: bool,
|
|
73
|
+
enrich: EnrichOpt,
|
|
74
|
+
syft_bin: str | None,
|
|
75
|
+
cache: Cache,
|
|
76
|
+
) -> ScanResult:
|
|
77
|
+
document = scan_to_document(target, syft_bin=syft_bin)
|
|
78
|
+
skipped = [c for c in document.components if not c.purl]
|
|
79
|
+
valid_purls = sorted({c.purl for c in document.components if c.purl})
|
|
80
|
+
warnings: list[str] = []
|
|
81
|
+
if skipped:
|
|
82
|
+
warnings.append(
|
|
83
|
+
f"Skipped {len(skipped)} component(s) with invalid or missing PURLs "
|
|
84
|
+
"(they were not sent to OSV)."
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
kev_index = load_index(cache, offline=offline)
|
|
88
|
+
osv_by_purl = query_batch(valid_purls, cache, offline=offline)
|
|
89
|
+
matches = merge_matches(document.components, osv_by_purl, kev_index)
|
|
90
|
+
|
|
91
|
+
if enrich is EnrichOpt.nvd and not offline:
|
|
92
|
+
for match in matches:
|
|
93
|
+
if match.cve_id and match.cvss_v3 is None:
|
|
94
|
+
payload = enrich_cve(match.cve_id)
|
|
95
|
+
if payload:
|
|
96
|
+
match.cvss_v3 = cvss_from_nvd(payload)
|
|
97
|
+
|
|
98
|
+
return ScanResult(
|
|
99
|
+
target=target,
|
|
100
|
+
scanned_at=datetime.now(timezone.utc),
|
|
101
|
+
sbom=document,
|
|
102
|
+
matches=matches,
|
|
103
|
+
skipped_components=skipped,
|
|
104
|
+
offline=offline,
|
|
105
|
+
warnings=warnings,
|
|
106
|
+
)
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
@app.command()
|
|
110
|
+
def scan(
|
|
111
|
+
target: str = typer.Argument(
|
|
112
|
+
".", help="Path, image, Syft target, or CycloneDX JSON file."
|
|
113
|
+
),
|
|
114
|
+
format: FormatOpt = typer.Option(FormatOpt.table, "--format", help="Output format."),
|
|
115
|
+
output: Path | None = typer.Option(None, "--output", help="Write formatted output to PATH."),
|
|
116
|
+
export_pdf: Path | None = typer.Option(
|
|
117
|
+
None, "--export-pdf", help="Write a community PDF (falls back to HTML/Markdown)."
|
|
118
|
+
),
|
|
119
|
+
fail_on: FailOnOpt = typer.Option(
|
|
120
|
+
FailOnOpt.kev, "--fail-on", help="Exit 1 when threshold is met. Default: kev."
|
|
121
|
+
),
|
|
122
|
+
offline: bool = typer.Option(False, "--offline", help="Use SQLite caches only. No network."),
|
|
123
|
+
enrich: EnrichOpt = typer.Option(EnrichOpt.none, "--enrich"),
|
|
124
|
+
sync_cloud: bool = typer.Option(False, "--sync-cloud", help="POST results to CRA-Shield."),
|
|
125
|
+
syft_bin: str | None = typer.Option(None, "--syft-bin", help="Path to the Syft binary."),
|
|
126
|
+
quiet: bool = typer.Option(False, "--quiet"),
|
|
127
|
+
verbose: bool = typer.Option(False, "--verbose"),
|
|
128
|
+
) -> None:
|
|
129
|
+
"""Generate or ingest an SBOM and match components against OSV + CISA KEV."""
|
|
130
|
+
_configure_logging(verbose, quiet)
|
|
131
|
+
try:
|
|
132
|
+
with Cache() as cache:
|
|
133
|
+
result = _run_scan(
|
|
134
|
+
target,
|
|
135
|
+
offline=offline,
|
|
136
|
+
enrich=enrich,
|
|
137
|
+
syft_bin=syft_bin,
|
|
138
|
+
cache=cache,
|
|
139
|
+
)
|
|
140
|
+
cache.save_scan(target, result_to_json(result), result.sbom.model_dump(mode="json"))
|
|
141
|
+
except SyftError as exc:
|
|
142
|
+
_exit(exc.exit_code, str(exc))
|
|
143
|
+
except KevError as exc:
|
|
144
|
+
_exit(2, str(exc))
|
|
145
|
+
|
|
146
|
+
fmt = OutputFormat(format.value)
|
|
147
|
+
if fmt is OutputFormat.TABLE and not quiet:
|
|
148
|
+
print_table(result, console)
|
|
149
|
+
elif fmt is not OutputFormat.TABLE:
|
|
150
|
+
payload = format_payload(result, fmt)
|
|
151
|
+
if output:
|
|
152
|
+
write_output(result, fmt, output)
|
|
153
|
+
if not quiet:
|
|
154
|
+
console.print(f"Wrote {fmt.value} to {output}")
|
|
155
|
+
else:
|
|
156
|
+
console.print(payload)
|
|
157
|
+
elif output:
|
|
158
|
+
write_output(result, OutputFormat.JSON, output)
|
|
159
|
+
|
|
160
|
+
if export_pdf:
|
|
161
|
+
written = export_report(result, export_pdf)
|
|
162
|
+
if not quiet:
|
|
163
|
+
if written.suffix == ".pdf":
|
|
164
|
+
console.print(f"Wrote PDF to {written}")
|
|
165
|
+
else:
|
|
166
|
+
console.print(
|
|
167
|
+
f"[yellow]PDF engine unavailable.[/yellow] Wrote {written} instead.\n{PDF_HINT}"
|
|
168
|
+
)
|
|
169
|
+
|
|
170
|
+
if sync_cloud:
|
|
171
|
+
response = ingest(result)
|
|
172
|
+
if not quiet:
|
|
173
|
+
console.print(response.get("message") or response)
|
|
174
|
+
|
|
175
|
+
threshold = FailOn(fail_on.value)
|
|
176
|
+
if result.fails(threshold):
|
|
177
|
+
_exit(1, f"Scan failed --fail-on {threshold.value}.")
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
@app.command()
|
|
181
|
+
def doctor(
|
|
182
|
+
syft_bin: str | None = typer.Option(None, "--syft-bin"),
|
|
183
|
+
) -> None:
|
|
184
|
+
"""Check Syft, network, cache, and PDF engine. Friendly, not a stack trace."""
|
|
185
|
+
console.print(f"[bold]OpenCRA[/bold] {__version__}")
|
|
186
|
+
try:
|
|
187
|
+
path = resolve_syft(syft_bin)
|
|
188
|
+
label, parsed = syft_version(syft_bin)
|
|
189
|
+
ok = version_ok(parsed)
|
|
190
|
+
status = "[green]ok[/green]" if ok else "[yellow]old[/yellow]"
|
|
191
|
+
console.print(f"Syft: {status} {path} ({label})")
|
|
192
|
+
if not ok:
|
|
193
|
+
console.print(" Install Syft >= 1.0.0 for CycloneDX 1.6 output.")
|
|
194
|
+
except SyftError as exc:
|
|
195
|
+
console.print(f"Syft: [red]missing[/red]\n{exc}")
|
|
196
|
+
|
|
197
|
+
cache_path = default_db_path()
|
|
198
|
+
console.print(f"Cache: {cache_path} ({'exists' if cache_path.exists() else 'will be created'})")
|
|
199
|
+
with Cache() as cache:
|
|
200
|
+
fetched = cache.get_kev_fetched_at()
|
|
201
|
+
console.print(f"KEV cache: {fetched.isoformat() if fetched else 'empty'}")
|
|
202
|
+
|
|
203
|
+
console.print(f"OSV: {'reachable' if osv_ping() else 'unreachable (offline scans still work)'}")
|
|
204
|
+
pdf_ok, pdf_reason = weasyprint_status()
|
|
205
|
+
console.print(f"PDF engine: {'ok' if pdf_ok else 'fallback'} — {pdf_reason}")
|
|
206
|
+
console.print(
|
|
207
|
+
"[dim]A KEV match is a CRA candidate. Clocks start only after human awareness.[/dim]"
|
|
208
|
+
)
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
@app.command("kev")
|
|
212
|
+
def kev_cmd(
|
|
213
|
+
action: str = typer.Argument("refresh", help="Only 'refresh' is supported."),
|
|
214
|
+
) -> None:
|
|
215
|
+
"""Download or refresh the CISA KEV catalog into the local cache."""
|
|
216
|
+
if action != "refresh":
|
|
217
|
+
_exit(2, "Usage: opencra kev refresh")
|
|
218
|
+
try:
|
|
219
|
+
with Cache() as cache:
|
|
220
|
+
count, fetched = refresh(cache)
|
|
221
|
+
except KevError as exc:
|
|
222
|
+
_exit(2, str(exc))
|
|
223
|
+
console.print(f"Cached {count} CISA KEV entries ({fetched.isoformat()}).")
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
@app.command()
|
|
227
|
+
def report(
|
|
228
|
+
last: bool = typer.Option(False, "--last", help="Re-render the most recent local scan."),
|
|
229
|
+
export_pdf: Path | None = typer.Option(None, "--export-pdf"),
|
|
230
|
+
format: FormatOpt = typer.Option(FormatOpt.table, "--format"),
|
|
231
|
+
) -> None:
|
|
232
|
+
"""Show the last cached scan without invoking Syft."""
|
|
233
|
+
if not last:
|
|
234
|
+
_exit(2, "Pass --last to print the most recent scan.")
|
|
235
|
+
with Cache() as cache:
|
|
236
|
+
raw = cache.last_scan()
|
|
237
|
+
if raw is None:
|
|
238
|
+
_exit(2, "No cached scans. Run `opencra scan .` first.")
|
|
239
|
+
result = ScanResult.model_validate(raw)
|
|
240
|
+
fmt = OutputFormat(format.value)
|
|
241
|
+
if fmt is OutputFormat.TABLE:
|
|
242
|
+
print_table(result, console)
|
|
243
|
+
else:
|
|
244
|
+
console.print(format_payload(result, fmt))
|
|
245
|
+
if export_pdf:
|
|
246
|
+
written = export_report(result, export_pdf)
|
|
247
|
+
console.print(f"Wrote report to {written}")
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
@app.callback()
|
|
251
|
+
def main() -> None:
|
|
252
|
+
"""OpenCRA CLI."""
|
|
253
|
+
return
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
if __name__ == "__main__":
|
|
257
|
+
app()
|
opencra_cli/db.py
ADDED
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
"""Local SQLite cache at ~/.opencra/cache.db with WAL mode."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
import os
|
|
7
|
+
import sqlite3
|
|
8
|
+
from datetime import datetime, timedelta, timezone
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
from typing import Any
|
|
11
|
+
|
|
12
|
+
OSV_TTL = timedelta(hours=12)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def default_db_path() -> Path:
|
|
16
|
+
override = os.environ.get("OPENCRA_CACHE")
|
|
17
|
+
if override:
|
|
18
|
+
return Path(override).expanduser()
|
|
19
|
+
return Path.home() / ".opencra" / "cache.db"
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class Cache:
|
|
23
|
+
def __init__(self, path: Path | None = None) -> None:
|
|
24
|
+
self.path = path or default_db_path()
|
|
25
|
+
self.path.parent.mkdir(parents=True, exist_ok=True)
|
|
26
|
+
self.conn = sqlite3.connect(self.path)
|
|
27
|
+
self.conn.row_factory = sqlite3.Row
|
|
28
|
+
self.conn.execute("PRAGMA journal_mode=WAL;")
|
|
29
|
+
self.conn.execute("PRAGMA foreign_keys=ON;")
|
|
30
|
+
self._init()
|
|
31
|
+
|
|
32
|
+
def close(self) -> None:
|
|
33
|
+
self.conn.close()
|
|
34
|
+
|
|
35
|
+
def __enter__(self) -> Cache:
|
|
36
|
+
return self
|
|
37
|
+
|
|
38
|
+
def __exit__(self, *args: object) -> None:
|
|
39
|
+
self.close()
|
|
40
|
+
|
|
41
|
+
def _init(self) -> None:
|
|
42
|
+
self.conn.executescript(
|
|
43
|
+
"""
|
|
44
|
+
CREATE TABLE IF NOT EXISTS kev_meta (
|
|
45
|
+
id INTEGER PRIMARY KEY CHECK (id = 1),
|
|
46
|
+
fetched_at TEXT NOT NULL,
|
|
47
|
+
catalog_json TEXT NOT NULL
|
|
48
|
+
);
|
|
49
|
+
CREATE TABLE IF NOT EXISTS osv_cache (
|
|
50
|
+
purl TEXT PRIMARY KEY,
|
|
51
|
+
fetched_at TEXT NOT NULL,
|
|
52
|
+
vulns_json TEXT NOT NULL
|
|
53
|
+
);
|
|
54
|
+
CREATE TABLE IF NOT EXISTS scans (
|
|
55
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
56
|
+
target TEXT NOT NULL,
|
|
57
|
+
scanned_at TEXT NOT NULL,
|
|
58
|
+
serial_number TEXT,
|
|
59
|
+
sbom_json TEXT NOT NULL,
|
|
60
|
+
result_json TEXT NOT NULL
|
|
61
|
+
);
|
|
62
|
+
CREATE TABLE IF NOT EXISTS components (
|
|
63
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
64
|
+
scan_id INTEGER NOT NULL,
|
|
65
|
+
purl TEXT,
|
|
66
|
+
name TEXT NOT NULL,
|
|
67
|
+
version TEXT,
|
|
68
|
+
FOREIGN KEY (scan_id) REFERENCES scans(id)
|
|
69
|
+
);
|
|
70
|
+
CREATE TABLE IF NOT EXISTS matches (
|
|
71
|
+
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
72
|
+
scan_id INTEGER NOT NULL,
|
|
73
|
+
purl TEXT NOT NULL,
|
|
74
|
+
osv_id TEXT,
|
|
75
|
+
cve_id TEXT,
|
|
76
|
+
severity TEXT,
|
|
77
|
+
in_kev INTEGER NOT NULL DEFAULT 0,
|
|
78
|
+
summary TEXT,
|
|
79
|
+
FOREIGN KEY (scan_id) REFERENCES scans(id)
|
|
80
|
+
);
|
|
81
|
+
"""
|
|
82
|
+
)
|
|
83
|
+
self.conn.commit()
|
|
84
|
+
|
|
85
|
+
def get_kev_catalog(self) -> dict[str, Any] | None:
|
|
86
|
+
row = self.conn.execute("SELECT catalog_json FROM kev_meta WHERE id = 1").fetchone()
|
|
87
|
+
if not row:
|
|
88
|
+
return None
|
|
89
|
+
return json.loads(row["catalog_json"])
|
|
90
|
+
|
|
91
|
+
def get_kev_fetched_at(self) -> datetime | None:
|
|
92
|
+
row = self.conn.execute("SELECT fetched_at FROM kev_meta WHERE id = 1").fetchone()
|
|
93
|
+
if not row:
|
|
94
|
+
return None
|
|
95
|
+
return datetime.fromisoformat(row["fetched_at"])
|
|
96
|
+
|
|
97
|
+
def save_kev_catalog(self, payload: dict[str, Any]) -> None:
|
|
98
|
+
now = datetime.now(timezone.utc).isoformat()
|
|
99
|
+
self.conn.execute(
|
|
100
|
+
"""
|
|
101
|
+
INSERT INTO kev_meta (id, fetched_at, catalog_json)
|
|
102
|
+
VALUES (1, ?, ?)
|
|
103
|
+
ON CONFLICT(id) DO UPDATE SET fetched_at = excluded.fetched_at,
|
|
104
|
+
catalog_json = excluded.catalog_json
|
|
105
|
+
""",
|
|
106
|
+
(now, json.dumps(payload)),
|
|
107
|
+
)
|
|
108
|
+
self.conn.commit()
|
|
109
|
+
|
|
110
|
+
def get_osv(self, purl: str) -> list[dict[str, Any]] | None:
|
|
111
|
+
row = self.conn.execute(
|
|
112
|
+
"SELECT fetched_at, vulns_json FROM osv_cache WHERE purl = ?",
|
|
113
|
+
(purl,),
|
|
114
|
+
).fetchone()
|
|
115
|
+
if not row:
|
|
116
|
+
return None
|
|
117
|
+
fetched = datetime.fromisoformat(row["fetched_at"])
|
|
118
|
+
if datetime.now(timezone.utc) - fetched > OSV_TTL:
|
|
119
|
+
return None
|
|
120
|
+
return json.loads(row["vulns_json"])
|
|
121
|
+
|
|
122
|
+
def save_osv(self, purl: str, vulns: list[dict[str, Any]]) -> None:
|
|
123
|
+
now = datetime.now(timezone.utc).isoformat()
|
|
124
|
+
self.conn.execute(
|
|
125
|
+
"""
|
|
126
|
+
INSERT INTO osv_cache (purl, fetched_at, vulns_json)
|
|
127
|
+
VALUES (?, ?, ?)
|
|
128
|
+
ON CONFLICT(purl) DO UPDATE SET fetched_at = excluded.fetched_at,
|
|
129
|
+
vulns_json = excluded.vulns_json
|
|
130
|
+
""",
|
|
131
|
+
(purl, now, json.dumps(vulns)),
|
|
132
|
+
)
|
|
133
|
+
self.conn.commit()
|
|
134
|
+
|
|
135
|
+
def save_scan(self, target: str, result_json: dict[str, Any], sbom_json: dict[str, Any]) -> int:
|
|
136
|
+
scanned_at = result_json.get("scanned_at") or datetime.now(timezone.utc).isoformat()
|
|
137
|
+
serial = (result_json.get("sbom") or {}).get("serial_number")
|
|
138
|
+
cur = self.conn.execute(
|
|
139
|
+
"""
|
|
140
|
+
INSERT INTO scans (target, scanned_at, serial_number, sbom_json, result_json)
|
|
141
|
+
VALUES (?, ?, ?, ?, ?)
|
|
142
|
+
""",
|
|
143
|
+
(target, scanned_at, serial, json.dumps(sbom_json), json.dumps(result_json)),
|
|
144
|
+
)
|
|
145
|
+
scan_id = int(cur.lastrowid or 0)
|
|
146
|
+
for component in (result_json.get("sbom") or {}).get("components") or []:
|
|
147
|
+
self.conn.execute(
|
|
148
|
+
"INSERT INTO components (scan_id, purl, name, version) VALUES (?, ?, ?, ?)",
|
|
149
|
+
(scan_id, component.get("purl"), component.get("name"), component.get("version")),
|
|
150
|
+
)
|
|
151
|
+
for match in result_json.get("matches") or []:
|
|
152
|
+
self.conn.execute(
|
|
153
|
+
"""
|
|
154
|
+
INSERT INTO matches (scan_id, purl, osv_id, cve_id, severity, in_kev, summary)
|
|
155
|
+
VALUES (?, ?, ?, ?, ?, ?, ?)
|
|
156
|
+
""",
|
|
157
|
+
(
|
|
158
|
+
scan_id,
|
|
159
|
+
match.get("purl"),
|
|
160
|
+
match.get("osv_id"),
|
|
161
|
+
match.get("cve_id"),
|
|
162
|
+
match.get("severity"),
|
|
163
|
+
1 if match.get("in_kev") else 0,
|
|
164
|
+
match.get("summary"),
|
|
165
|
+
),
|
|
166
|
+
)
|
|
167
|
+
self.conn.commit()
|
|
168
|
+
return scan_id
|
|
169
|
+
|
|
170
|
+
def last_scan(self) -> dict[str, Any] | None:
|
|
171
|
+
row = self.conn.execute(
|
|
172
|
+
"SELECT result_json FROM scans ORDER BY id DESC LIMIT 1"
|
|
173
|
+
).fetchone()
|
|
174
|
+
if not row:
|
|
175
|
+
return None
|
|
176
|
+
return json.loads(row["result_json"])
|
opencra_cli/httputil.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"""Shared HTTP defaults: User-Agent and a hard 10s timeout."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import httpx
|
|
6
|
+
|
|
7
|
+
from opencra_cli import __version__
|
|
8
|
+
|
|
9
|
+
USER_AGENT = f"opencra/{__version__}"
|
|
10
|
+
HTTP_TIMEOUT = 10.0
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def client(**kwargs: object) -> httpx.Client:
|
|
14
|
+
headers = {"User-Agent": USER_AGENT}
|
|
15
|
+
extra = kwargs.pop("headers", None)
|
|
16
|
+
if isinstance(extra, dict):
|
|
17
|
+
headers.update(extra)
|
|
18
|
+
return httpx.Client(timeout=HTTP_TIMEOUT, headers=headers, follow_redirects=True, **kwargs) # type: ignore[arg-type]
|
opencra_cli/kev.py
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"""CISA KEV catalog download and local index."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import logging
|
|
6
|
+
from datetime import datetime, timedelta, timezone
|
|
7
|
+
from typing import Any
|
|
8
|
+
|
|
9
|
+
import httpx
|
|
10
|
+
from opencra_shared.kev import KevEntry, index_kev_by_cve, parse_kev_catalog
|
|
11
|
+
|
|
12
|
+
from opencra_cli.db import Cache
|
|
13
|
+
from opencra_cli.httputil import client
|
|
14
|
+
|
|
15
|
+
logger = logging.getLogger("opencra.kev")
|
|
16
|
+
|
|
17
|
+
KEV_URL = "https://www.cisa.gov/sites/default/files/feeds/known_exploited_vulnerabilities.json"
|
|
18
|
+
KEV_FALLBACK_URL = (
|
|
19
|
+
"https://raw.githubusercontent.com/cisagov/known-exploited-vulnerabilities-data/"
|
|
20
|
+
"main/known_exploited_vulnerabilities.json"
|
|
21
|
+
)
|
|
22
|
+
KEV_SOURCES = (KEV_URL, KEV_FALLBACK_URL)
|
|
23
|
+
KEV_TTL = timedelta(hours=24)
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class KevError(Exception):
|
|
27
|
+
def __init__(self, message: str) -> None:
|
|
28
|
+
super().__init__(message)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def _fetch_kev_payload(http: httpx.Client, url: str) -> dict[str, Any]:
|
|
32
|
+
response = http.get(url)
|
|
33
|
+
response.raise_for_status()
|
|
34
|
+
payload = response.json()
|
|
35
|
+
if not isinstance(payload, dict) or "vulnerabilities" not in payload:
|
|
36
|
+
raise KevError("KEV catalog JSON is missing a vulnerabilities array.")
|
|
37
|
+
return payload
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def refresh(cache: Cache) -> tuple[int, datetime]:
|
|
41
|
+
last_error: Exception | None = None
|
|
42
|
+
payload: dict[str, Any] | None = None
|
|
43
|
+
with client() as http:
|
|
44
|
+
for url in KEV_SOURCES:
|
|
45
|
+
try:
|
|
46
|
+
payload = _fetch_kev_payload(http, url)
|
|
47
|
+
logger.info("Downloaded CISA KEV catalog from %s", url)
|
|
48
|
+
break
|
|
49
|
+
except (httpx.HTTPError, ValueError, KevError) as exc:
|
|
50
|
+
last_error = exc
|
|
51
|
+
logger.warning("KEV download failed from %s: %s", url, exc)
|
|
52
|
+
if payload is None:
|
|
53
|
+
raise KevError(f"Failed to download CISA KEV catalog: {last_error}") from last_error
|
|
54
|
+
cache.save_kev_catalog(payload)
|
|
55
|
+
entries = parse_kev_catalog(payload)
|
|
56
|
+
fetched = cache.get_kev_fetched_at() or datetime.now(timezone.utc)
|
|
57
|
+
return len(entries), fetched
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def load_index(cache: Cache, *, offline: bool = False, force_refresh: bool = False) -> dict[str, KevEntry]:
|
|
61
|
+
fetched = cache.get_kev_fetched_at()
|
|
62
|
+
stale = fetched is None or datetime.now(timezone.utc) - fetched > KEV_TTL
|
|
63
|
+
if (stale or force_refresh) and not offline:
|
|
64
|
+
try:
|
|
65
|
+
refresh(cache)
|
|
66
|
+
except KevError as exc:
|
|
67
|
+
if cache.get_kev_catalog() is None:
|
|
68
|
+
raise
|
|
69
|
+
logger.warning("KEV refresh failed, using cached catalog: %s", exc)
|
|
70
|
+
payload = cache.get_kev_catalog()
|
|
71
|
+
if payload is None:
|
|
72
|
+
if offline:
|
|
73
|
+
raise KevError(
|
|
74
|
+
"No cached CISA KEV catalog. Re-run without --offline after "
|
|
75
|
+
"`opencra kev refresh` while online."
|
|
76
|
+
)
|
|
77
|
+
raise KevError("CISA KEV catalog is unavailable.")
|
|
78
|
+
return index_kev_by_cve(parse_kev_catalog(payload))
|
opencra_cli/match.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"""Merge OSV results with the CISA KEV index into VulnMatch rows."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
from opencra_shared.kev import KevEntry, extract_cves
|
|
8
|
+
from opencra_shared.models import Component, VulnMatch
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def _severity(vuln: dict[str, Any]) -> tuple[str | None, float | None]:
|
|
12
|
+
severity = None
|
|
13
|
+
score = None
|
|
14
|
+
for item in vuln.get("severity") or []:
|
|
15
|
+
if not isinstance(item, dict):
|
|
16
|
+
continue
|
|
17
|
+
typ = str(item.get("type") or "").upper()
|
|
18
|
+
raw = item.get("score")
|
|
19
|
+
if typ.startswith("CVSS") and raw is not None:
|
|
20
|
+
try:
|
|
21
|
+
# CVSS vector or numeric
|
|
22
|
+
if isinstance(raw, int | float):
|
|
23
|
+
score = float(raw)
|
|
24
|
+
else:
|
|
25
|
+
text = str(raw)
|
|
26
|
+
if text.replace(".", "", 1).isdigit():
|
|
27
|
+
score = float(text)
|
|
28
|
+
except ValueError:
|
|
29
|
+
pass
|
|
30
|
+
db = vuln.get("database_specific") or {}
|
|
31
|
+
sev = db.get("severity")
|
|
32
|
+
if isinstance(sev, str):
|
|
33
|
+
severity = sev.upper()
|
|
34
|
+
if score is not None and severity is None:
|
|
35
|
+
if score >= 9.0:
|
|
36
|
+
severity = "CRITICAL"
|
|
37
|
+
elif score >= 7.0:
|
|
38
|
+
severity = "HIGH"
|
|
39
|
+
elif score >= 4.0:
|
|
40
|
+
severity = "MEDIUM"
|
|
41
|
+
else:
|
|
42
|
+
severity = "LOW"
|
|
43
|
+
return severity, score
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def merge_matches(
|
|
47
|
+
components: list[Component],
|
|
48
|
+
osv_by_purl: dict[str, list[dict[str, Any]]],
|
|
49
|
+
kev_index: dict[str, KevEntry],
|
|
50
|
+
) -> list[VulnMatch]:
|
|
51
|
+
matches: list[VulnMatch] = []
|
|
52
|
+
seen: set[tuple[str, str]] = set()
|
|
53
|
+
for component in components:
|
|
54
|
+
if not component.purl:
|
|
55
|
+
continue
|
|
56
|
+
for vuln in osv_by_purl.get(component.purl, []):
|
|
57
|
+
osv_id = str(vuln.get("id") or "")
|
|
58
|
+
aliases = [str(a) for a in (vuln.get("aliases") or [])]
|
|
59
|
+
cves = extract_cves(aliases, osv_id)
|
|
60
|
+
cve_id = cves[0] if cves else None
|
|
61
|
+
key = (component.purl, osv_id or cve_id or "")
|
|
62
|
+
if key in seen:
|
|
63
|
+
continue
|
|
64
|
+
seen.add(key)
|
|
65
|
+
kev = None
|
|
66
|
+
for cve in cves:
|
|
67
|
+
if cve in kev_index:
|
|
68
|
+
kev = kev_index[cve]
|
|
69
|
+
cve_id = cve
|
|
70
|
+
break
|
|
71
|
+
severity, score = _severity(vuln)
|
|
72
|
+
matches.append(
|
|
73
|
+
VulnMatch(
|
|
74
|
+
purl=component.purl,
|
|
75
|
+
component_name=component.name,
|
|
76
|
+
component_version=component.version,
|
|
77
|
+
osv_id=osv_id or None,
|
|
78
|
+
cve_id=cve_id,
|
|
79
|
+
severity=severity,
|
|
80
|
+
cvss_v3=score,
|
|
81
|
+
in_kev=kev is not None,
|
|
82
|
+
kev_added_at=kev.date_added if kev else None,
|
|
83
|
+
kev_ransomware=kev.known_ransomware if kev else None,
|
|
84
|
+
aliases=aliases,
|
|
85
|
+
summary=vuln.get("summary"),
|
|
86
|
+
)
|
|
87
|
+
)
|
|
88
|
+
return matches
|
opencra_cli/nvd.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"""Optional NVD 2.0 enrichment. Off by default to avoid rate limits."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import logging
|
|
6
|
+
import os
|
|
7
|
+
from typing import Any
|
|
8
|
+
|
|
9
|
+
import httpx
|
|
10
|
+
|
|
11
|
+
from opencra_cli.httputil import client
|
|
12
|
+
|
|
13
|
+
logger = logging.getLogger("opencra.nvd")
|
|
14
|
+
|
|
15
|
+
NVD_URL = "https://services.nvd.nist.gov/rest/json/cves/2.0"
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def enrich_cve(cve_id: str) -> dict[str, Any] | None:
|
|
19
|
+
headers: dict[str, str] = {}
|
|
20
|
+
api_key = os.environ.get("NVD_API_KEY")
|
|
21
|
+
if api_key:
|
|
22
|
+
headers["apiKey"] = api_key
|
|
23
|
+
try:
|
|
24
|
+
with client(headers=headers) as http:
|
|
25
|
+
response = http.get(NVD_URL, params={"cveId": cve_id})
|
|
26
|
+
response.raise_for_status()
|
|
27
|
+
return response.json()
|
|
28
|
+
except httpx.HTTPError as exc:
|
|
29
|
+
logger.warning("NVD lookup failed for %s: %s", cve_id, exc)
|
|
30
|
+
return None
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def cvss_from_nvd(payload: dict[str, Any]) -> float | None:
|
|
34
|
+
try:
|
|
35
|
+
vuln = payload["vulnerabilities"][0]["cve"]
|
|
36
|
+
metrics = vuln.get("metrics") or {}
|
|
37
|
+
for key in ("cvssMetricV31", "cvssMetricV30", "cvssMetricV2"):
|
|
38
|
+
items = metrics.get(key)
|
|
39
|
+
if items:
|
|
40
|
+
return float(items[0]["cvssData"]["baseScore"])
|
|
41
|
+
except (KeyError, IndexError, TypeError, ValueError):
|
|
42
|
+
return None
|
|
43
|
+
return None
|
opencra_cli/osv.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
"""OSV Query Batch client with 100-item chunks and SQLite cache."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import logging
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
import httpx
|
|
9
|
+
|
|
10
|
+
from opencra_cli.db import Cache
|
|
11
|
+
from opencra_cli.httputil import USER_AGENT, client
|
|
12
|
+
|
|
13
|
+
logger = logging.getLogger("opencra.osv")
|
|
14
|
+
|
|
15
|
+
OSV_QUERYBATCH = "https://api.osv.dev/v1/querybatch"
|
|
16
|
+
BATCH_SIZE = 100
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def _chunks(items: list[str], size: int) -> list[list[str]]:
|
|
20
|
+
return [items[i : i + size] for i in range(0, len(items), size)]
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def query_batch(
|
|
24
|
+
purls: list[str],
|
|
25
|
+
cache: Cache,
|
|
26
|
+
*,
|
|
27
|
+
offline: bool = False,
|
|
28
|
+
) -> dict[str, list[dict[str, Any]]]:
|
|
29
|
+
"""Return {purl: [osv vuln dicts]} for validated PURLs only."""
|
|
30
|
+
results: dict[str, list[dict[str, Any]]] = {}
|
|
31
|
+
missing: list[str] = []
|
|
32
|
+
for purl in purls:
|
|
33
|
+
cached = cache.get_osv(purl)
|
|
34
|
+
if cached is not None:
|
|
35
|
+
results[purl] = cached
|
|
36
|
+
else:
|
|
37
|
+
missing.append(purl)
|
|
38
|
+
|
|
39
|
+
if not missing:
|
|
40
|
+
return results
|
|
41
|
+
if offline:
|
|
42
|
+
logger.warning("Offline mode: %s PURLs have no cached OSV data", len(missing))
|
|
43
|
+
for purl in missing:
|
|
44
|
+
results[purl] = []
|
|
45
|
+
return results
|
|
46
|
+
|
|
47
|
+
for chunk in _chunks(missing, BATCH_SIZE):
|
|
48
|
+
payload = {"queries": [{"package": {"purl": purl}} for purl in chunk]}
|
|
49
|
+
try:
|
|
50
|
+
with client() as http:
|
|
51
|
+
response = http.post(OSV_QUERYBATCH, json=payload)
|
|
52
|
+
response.raise_for_status()
|
|
53
|
+
body = response.json()
|
|
54
|
+
except httpx.HTTPError as exc:
|
|
55
|
+
logger.warning("OSV querybatch failed for %s items: %s", len(chunk), exc)
|
|
56
|
+
for purl in chunk:
|
|
57
|
+
results.setdefault(purl, [])
|
|
58
|
+
continue
|
|
59
|
+
|
|
60
|
+
results_list = body.get("results") or []
|
|
61
|
+
for purl, item in zip(chunk, results_list, strict=False):
|
|
62
|
+
vulns = item.get("vulns") or [] if isinstance(item, dict) else []
|
|
63
|
+
cache.save_osv(purl, vulns)
|
|
64
|
+
results[purl] = vulns
|
|
65
|
+
# If OSV returned fewer results than queries, mark leftovers empty.
|
|
66
|
+
if len(results_list) < len(chunk):
|
|
67
|
+
for purl in chunk[len(results_list) :]:
|
|
68
|
+
cache.save_osv(purl, [])
|
|
69
|
+
results[purl] = []
|
|
70
|
+
return results
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def ping() -> bool:
|
|
74
|
+
try:
|
|
75
|
+
with client() as http:
|
|
76
|
+
response = http.get("https://api.osv.dev/v1/querybatch", headers={"User-Agent": USER_AGENT})
|
|
77
|
+
return response.status_code in {200, 405, 415}
|
|
78
|
+
except httpx.HTTPError:
|
|
79
|
+
return False
|
opencra_cli/pdf.py
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
"""Community PDF export with WeasyPrint, falling back to HTML/Markdown."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from pathlib import Path
|
|
6
|
+
|
|
7
|
+
from jinja2 import Template
|
|
8
|
+
from opencra_shared.models import ScanResult
|
|
9
|
+
|
|
10
|
+
from opencra_cli.render import DISCLAIMER
|
|
11
|
+
|
|
12
|
+
_HTML_TEMPLATE = Template(
|
|
13
|
+
"""
|
|
14
|
+
<!DOCTYPE html>
|
|
15
|
+
<html lang="en">
|
|
16
|
+
<head>
|
|
17
|
+
<meta charset="utf-8"/>
|
|
18
|
+
<title>OpenCRA scan report</title>
|
|
19
|
+
<style>
|
|
20
|
+
body { font-family: Helvetica, Arial, sans-serif; margin: 32px; color: #111; }
|
|
21
|
+
h1 { font-size: 20px; }
|
|
22
|
+
.disclaimer { background: #fff6e5; border: 1px solid #e6c36a; padding: 12px; }
|
|
23
|
+
table { border-collapse: collapse; width: 100%; margin-top: 16px; font-size: 12px; }
|
|
24
|
+
th, td { border: 1px solid #ccc; padding: 6px 8px; text-align: left; }
|
|
25
|
+
th { background: #f4f4f4; }
|
|
26
|
+
.kev { color: #b00020; font-weight: bold; }
|
|
27
|
+
</style>
|
|
28
|
+
</head>
|
|
29
|
+
<body>
|
|
30
|
+
<h1>OpenCRA community scan report</h1>
|
|
31
|
+
<p>Target: {{ result.target }}<br/>Scanned: {{ result.scanned_at }}</p>
|
|
32
|
+
<p class="disclaimer">{{ disclaimer }}</p>
|
|
33
|
+
<p>Components: {{ result.sbom.components|length }} ·
|
|
34
|
+
Matches: {{ result.matches|length }} ·
|
|
35
|
+
KEV hits: {{ result.kev_hits|length }}</p>
|
|
36
|
+
<table>
|
|
37
|
+
<thead>
|
|
38
|
+
<tr><th>Package</th><th>Version</th><th>ID</th><th>Severity</th><th>KEV</th></tr>
|
|
39
|
+
</thead>
|
|
40
|
+
<tbody>
|
|
41
|
+
{% for m in result.matches %}
|
|
42
|
+
<tr>
|
|
43
|
+
<td>{{ m.component_name or m.purl }}</td>
|
|
44
|
+
<td>{{ m.component_version or "" }}</td>
|
|
45
|
+
<td>{{ m.cve_id or m.osv_id or "" }}</td>
|
|
46
|
+
<td>{{ m.severity or "" }}</td>
|
|
47
|
+
<td class="{{ 'kev' if m.in_kev else '' }}">{{ "KEV" if m.in_kev else "" }}</td>
|
|
48
|
+
</tr>
|
|
49
|
+
{% endfor %}
|
|
50
|
+
</tbody>
|
|
51
|
+
</table>
|
|
52
|
+
</body>
|
|
53
|
+
</html>
|
|
54
|
+
""".strip()
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
PDF_HINT = (
|
|
58
|
+
"WeasyPrint needs Cairo and Pango. Install them, or use the HTML/Markdown fallback:\n"
|
|
59
|
+
" macOS: brew install cairo pango\n"
|
|
60
|
+
" Debian: sudo apt install libcairo2 libpango-1.0-0 libgdk-pixbuf-2.0-0 "
|
|
61
|
+
"libffi-dev shared-mime-info"
|
|
62
|
+
)
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def weasyprint_status() -> tuple[bool, str]:
|
|
66
|
+
try:
|
|
67
|
+
import weasyprint # noqa: F401
|
|
68
|
+
except ImportError:
|
|
69
|
+
return False, "weasyprint is not installed (pip install 'opencra-cli[pdf]')"
|
|
70
|
+
try:
|
|
71
|
+
from weasyprint import HTML # noqa: F401
|
|
72
|
+
except Exception as exc: # native cairo/pango missing
|
|
73
|
+
return False, f"WeasyPrint native libraries missing: {exc}\n{PDF_HINT}"
|
|
74
|
+
return True, "WeasyPrint available"
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def render_html(result: ScanResult) -> str:
|
|
78
|
+
return _HTML_TEMPLATE.render(result=result, disclaimer=DISCLAIMER)
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
def render_markdown(result: ScanResult) -> str:
|
|
82
|
+
lines = [
|
|
83
|
+
"# OpenCRA community scan report",
|
|
84
|
+
"",
|
|
85
|
+
f"Target: `{result.target}`",
|
|
86
|
+
f"Scanned: {result.scanned_at}",
|
|
87
|
+
"",
|
|
88
|
+
f"> {DISCLAIMER}",
|
|
89
|
+
"",
|
|
90
|
+
f"Components: {len(result.sbom.components)} · "
|
|
91
|
+
f"Matches: {len(result.matches)} · KEV hits: {len(result.kev_hits)}",
|
|
92
|
+
"",
|
|
93
|
+
"| Package | Version | ID | Severity | KEV |",
|
|
94
|
+
"|---|---|---|---|---|",
|
|
95
|
+
]
|
|
96
|
+
for match in result.matches:
|
|
97
|
+
lines.append(
|
|
98
|
+
f"| {match.component_name or match.purl} | {match.component_version or ''} | "
|
|
99
|
+
f"{match.cve_id or match.osv_id or ''} | {match.severity or ''} | "
|
|
100
|
+
f"{'KEV' if match.in_kev else ''} |"
|
|
101
|
+
)
|
|
102
|
+
return "\n".join(lines) + "\n"
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def export_report(result: ScanResult, path: Path) -> Path:
|
|
106
|
+
"""Write PDF if possible; otherwise write HTML (and Markdown alongside)."""
|
|
107
|
+
html = render_html(result)
|
|
108
|
+
ok, reason = weasyprint_status()
|
|
109
|
+
suffix = path.suffix.lower()
|
|
110
|
+
if ok and suffix in {"", ".pdf"}:
|
|
111
|
+
from weasyprint import HTML
|
|
112
|
+
|
|
113
|
+
dest = path if suffix == ".pdf" else path.with_suffix(".pdf")
|
|
114
|
+
HTML(string=html).write_pdf(dest)
|
|
115
|
+
return dest
|
|
116
|
+
|
|
117
|
+
html_path = path.with_suffix(".html") if suffix == ".pdf" else path
|
|
118
|
+
if html_path.suffix.lower() != ".html":
|
|
119
|
+
html_path = path.with_suffix(".html")
|
|
120
|
+
html_path.write_text(html, encoding="utf-8")
|
|
121
|
+
md_path = html_path.with_suffix(".md")
|
|
122
|
+
md_path.write_text(render_markdown(result), encoding="utf-8")
|
|
123
|
+
# Surface the reason to the caller via a sidecar note.
|
|
124
|
+
html_path.with_suffix(".pdf-fallback.txt").write_text(reason + "\n", encoding="utf-8")
|
|
125
|
+
return html_path
|
opencra_cli/py.typed
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
opencra_cli/render.py
ADDED
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
"""Rich table and file exporters."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
from opencra_shared.models import OutputFormat, ScanResult
|
|
9
|
+
from opencra_shared.sbom import cyclonedx_to_spdx, serialize_cyclonedx
|
|
10
|
+
from rich.console import Console
|
|
11
|
+
from rich.table import Table
|
|
12
|
+
|
|
13
|
+
DISCLAIMER = (
|
|
14
|
+
"OpenCRA prepares evidence. A KEV match is a CRA candidate, not legal awareness. "
|
|
15
|
+
"Article 14 clocks start only after a human assessment."
|
|
16
|
+
)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def result_to_json(result: ScanResult) -> dict:
|
|
20
|
+
return result.model_dump(mode="json")
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def format_payload(result: ScanResult, fmt: OutputFormat) -> str:
|
|
24
|
+
if fmt is OutputFormat.JSON:
|
|
25
|
+
return json.dumps(result_to_json(result), indent=2)
|
|
26
|
+
if fmt is OutputFormat.CYCLONEDX:
|
|
27
|
+
return json.dumps(serialize_cyclonedx(result.sbom), indent=2)
|
|
28
|
+
if fmt is OutputFormat.SPDX:
|
|
29
|
+
return json.dumps(cyclonedx_to_spdx(result.sbom), indent=2)
|
|
30
|
+
return ""
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def write_output(result: ScanResult, fmt: OutputFormat, path: Path) -> None:
|
|
34
|
+
path.write_text(format_payload(result, fmt), encoding="utf-8")
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def print_table(result: ScanResult, console: Console) -> None:
|
|
38
|
+
console.print(f"[bold]OpenCRA[/bold] scan of [cyan]{result.target}[/cyan]")
|
|
39
|
+
console.print(f"[dim]{DISCLAIMER}[/dim]")
|
|
40
|
+
if result.warnings:
|
|
41
|
+
for warning in result.warnings:
|
|
42
|
+
console.print(f"[yellow]warn[/yellow] {warning}")
|
|
43
|
+
|
|
44
|
+
kev_count = len(result.kev_hits)
|
|
45
|
+
console.print(
|
|
46
|
+
f"Components: {len(result.sbom.components)} "
|
|
47
|
+
f"Matches: {len(result.matches)} "
|
|
48
|
+
f"[red]KEV hits: {kev_count}[/red]"
|
|
49
|
+
if kev_count
|
|
50
|
+
else f"Components: {len(result.sbom.components)} Matches: {len(result.matches)} KEV hits: 0"
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
table = Table(show_header=True, header_style="bold")
|
|
54
|
+
table.add_column("Package")
|
|
55
|
+
table.add_column("Version")
|
|
56
|
+
table.add_column("ID")
|
|
57
|
+
table.add_column("Severity")
|
|
58
|
+
table.add_column("KEV")
|
|
59
|
+
table.add_column("Action")
|
|
60
|
+
|
|
61
|
+
rows = sorted(result.matches, key=lambda m: (not m.in_kev, m.severity or "ZZ", m.purl))
|
|
62
|
+
if not rows:
|
|
63
|
+
console.print("[green]No vulnerability matches.[/green]")
|
|
64
|
+
return
|
|
65
|
+
|
|
66
|
+
for match in rows:
|
|
67
|
+
kev = "[bold red]KEV[/bold red]" if match.in_kev else ""
|
|
68
|
+
action = (
|
|
69
|
+
"Candidate — assess awareness (do not auto-file)"
|
|
70
|
+
if match.in_kev
|
|
71
|
+
else "Triage"
|
|
72
|
+
)
|
|
73
|
+
ident = match.cve_id or match.osv_id or "—"
|
|
74
|
+
table.add_row(
|
|
75
|
+
match.component_name or match.purl,
|
|
76
|
+
match.component_version or "—",
|
|
77
|
+
ident,
|
|
78
|
+
match.severity or "—",
|
|
79
|
+
kev,
|
|
80
|
+
action,
|
|
81
|
+
)
|
|
82
|
+
console.print(table)
|
|
83
|
+
if kev_count:
|
|
84
|
+
console.print(
|
|
85
|
+
"[bold]KEV hits are CRA candidates.[/bold] "
|
|
86
|
+
"Acknowledge awareness in CRA-Shield to start the 24-hour clock."
|
|
87
|
+
)
|
opencra_cli/syft.py
ADDED
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
"""Safe Syft subprocess wrapper. Never uses shell=True."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
import logging
|
|
7
|
+
import shutil
|
|
8
|
+
import subprocess
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
from typing import Any
|
|
11
|
+
|
|
12
|
+
from opencra_shared.sbom import parse_cyclonedx
|
|
13
|
+
|
|
14
|
+
logger = logging.getLogger("opencra.syft")
|
|
15
|
+
|
|
16
|
+
MIN_SYFT_VERSION = (1, 0, 0)
|
|
17
|
+
INSTALL_HINT = (
|
|
18
|
+
"Syft is required. Install it, then re-run:\n"
|
|
19
|
+
" macOS: brew install syft\n"
|
|
20
|
+
" Linux: curl -sSfL https://raw.githubusercontent.com/anchore/syft/main/install.sh "
|
|
21
|
+
"| sh -s -- -b ~/.local/bin"
|
|
22
|
+
)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class SyftError(Exception):
|
|
26
|
+
def __init__(self, message: str, *, exit_code: int = 2) -> None:
|
|
27
|
+
super().__init__(message)
|
|
28
|
+
self.exit_code = exit_code
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def resolve_syft(syft_bin: str | None = None) -> str:
|
|
32
|
+
if syft_bin:
|
|
33
|
+
path = Path(syft_bin).expanduser()
|
|
34
|
+
if path.is_file():
|
|
35
|
+
return str(path)
|
|
36
|
+
which = shutil.which(syft_bin)
|
|
37
|
+
if which:
|
|
38
|
+
return which
|
|
39
|
+
raise SyftError(f"Syft binary not found at {syft_bin}.\n{INSTALL_HINT}")
|
|
40
|
+
found = shutil.which("syft")
|
|
41
|
+
if not found:
|
|
42
|
+
raise SyftError(f"Syft is not on PATH.\n{INSTALL_HINT}")
|
|
43
|
+
return found
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def parse_syft_version(text: str) -> tuple[int, int, int] | None:
|
|
47
|
+
# Typical: "syft 1.18.1"
|
|
48
|
+
for token in text.replace(",", " ").split():
|
|
49
|
+
parts = token.strip().split(".")
|
|
50
|
+
if len(parts) >= 2 and parts[0].isdigit() and parts[1].isdigit():
|
|
51
|
+
patch = int(parts[2]) if len(parts) > 2 and parts[2].isdigit() else 0
|
|
52
|
+
return int(parts[0]), int(parts[1]), patch
|
|
53
|
+
return None
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def syft_version(syft_bin: str | None = None) -> tuple[str, tuple[int, int, int] | None]:
|
|
57
|
+
binary = resolve_syft(syft_bin)
|
|
58
|
+
result = subprocess.run(
|
|
59
|
+
[binary, "version"],
|
|
60
|
+
capture_output=True,
|
|
61
|
+
text=True,
|
|
62
|
+
check=False,
|
|
63
|
+
)
|
|
64
|
+
output = (result.stdout or "") + (result.stderr or "")
|
|
65
|
+
if result.returncode != 0:
|
|
66
|
+
# Older syft prints version via --version
|
|
67
|
+
result = subprocess.run(
|
|
68
|
+
[binary, "--version"],
|
|
69
|
+
capture_output=True,
|
|
70
|
+
text=True,
|
|
71
|
+
check=False,
|
|
72
|
+
)
|
|
73
|
+
output = (result.stdout or "") + (result.stderr or "")
|
|
74
|
+
parsed = parse_syft_version(output)
|
|
75
|
+
return output.strip().splitlines()[0] if output.strip() else binary, parsed
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def version_ok(version: tuple[int, int, int] | None) -> bool:
|
|
79
|
+
if version is None:
|
|
80
|
+
return True
|
|
81
|
+
return version >= MIN_SYFT_VERSION
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def scan_target(target: str, *, syft_bin: str | None = None) -> dict[str, Any]:
|
|
85
|
+
binary = resolve_syft(syft_bin)
|
|
86
|
+
cmd = [binary, "scan", target, "-o", "cyclonedx-json"]
|
|
87
|
+
logger.debug("Running %s", cmd)
|
|
88
|
+
try:
|
|
89
|
+
result = subprocess.run(
|
|
90
|
+
cmd,
|
|
91
|
+
capture_output=True,
|
|
92
|
+
text=True,
|
|
93
|
+
check=True,
|
|
94
|
+
)
|
|
95
|
+
except subprocess.CalledProcessError as exc:
|
|
96
|
+
stderr = (exc.stderr or exc.stdout or "").strip()
|
|
97
|
+
raise SyftError(f"Syft failed on {target}: {stderr or exc}") from exc
|
|
98
|
+
except FileNotFoundError as exc:
|
|
99
|
+
raise SyftError(f"Syft is not on PATH.\n{INSTALL_HINT}") from exc
|
|
100
|
+
|
|
101
|
+
stdout = result.stdout.strip()
|
|
102
|
+
if not stdout:
|
|
103
|
+
raise SyftError("Syft produced empty CycloneDX output.")
|
|
104
|
+
try:
|
|
105
|
+
payload = json.loads(stdout)
|
|
106
|
+
except json.JSONDecodeError as exc:
|
|
107
|
+
raise SyftError(f"Syft output is not valid JSON: {exc}") from exc
|
|
108
|
+
if not isinstance(payload, dict):
|
|
109
|
+
raise SyftError("Syft output is not a CycloneDX object.")
|
|
110
|
+
return payload
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def load_cyclonedx_file(path: Path) -> dict[str, Any] | None:
|
|
114
|
+
"""Return a CycloneDX object if path is an existing CDX JSON file."""
|
|
115
|
+
if not path.is_file():
|
|
116
|
+
return None
|
|
117
|
+
try:
|
|
118
|
+
payload = json.loads(path.read_text(encoding="utf-8"))
|
|
119
|
+
except (OSError, UnicodeDecodeError, json.JSONDecodeError):
|
|
120
|
+
return None
|
|
121
|
+
if not isinstance(payload, dict):
|
|
122
|
+
return None
|
|
123
|
+
if str(payload.get("bomFormat") or "").lower() == "cyclonedx":
|
|
124
|
+
return payload
|
|
125
|
+
return None
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def scan_to_document(target: str, *, syft_bin: str | None = None):
|
|
129
|
+
payload = load_cyclonedx_file(Path(target).expanduser())
|
|
130
|
+
if payload is not None:
|
|
131
|
+
return parse_cyclonedx(payload)
|
|
132
|
+
return parse_cyclonedx(scan_target(target, syft_bin=syft_bin))
|
opencra_cli/sync.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"""Optional CRA-Shield cloud ingest. Never required for local scans."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
from typing import Any
|
|
7
|
+
|
|
8
|
+
import httpx
|
|
9
|
+
from opencra_shared.models import ScanResult
|
|
10
|
+
|
|
11
|
+
from opencra_cli.httputil import client
|
|
12
|
+
|
|
13
|
+
DEFAULT_API = "https://api.crashield.dev"
|
|
14
|
+
SIGNUP_URL = "https://crashield.dev/signup"
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def ingest(
|
|
18
|
+
result: ScanResult,
|
|
19
|
+
*,
|
|
20
|
+
api_url: str | None = None,
|
|
21
|
+
api_key: str | None = None,
|
|
22
|
+
) -> dict[str, Any]:
|
|
23
|
+
key = api_key or os.environ.get("OPENCRA_API_KEY")
|
|
24
|
+
base = (api_url or os.environ.get("OPENCRA_API_URL") or DEFAULT_API).rstrip("/")
|
|
25
|
+
if not key:
|
|
26
|
+
return {
|
|
27
|
+
"ok": False,
|
|
28
|
+
"skipped": True,
|
|
29
|
+
"message": (
|
|
30
|
+
"No OPENCRA_API_KEY set. Local scan is complete. "
|
|
31
|
+
f"Create a CRA-Shield workspace at {SIGNUP_URL}"
|
|
32
|
+
),
|
|
33
|
+
"signup_url": SIGNUP_URL,
|
|
34
|
+
}
|
|
35
|
+
payload = result.model_dump(mode="json")
|
|
36
|
+
try:
|
|
37
|
+
with client() as http:
|
|
38
|
+
response = http.post(
|
|
39
|
+
f"{base}/v1/ingest",
|
|
40
|
+
json=payload,
|
|
41
|
+
headers={"Authorization": f"Bearer {key}"},
|
|
42
|
+
)
|
|
43
|
+
response.raise_for_status()
|
|
44
|
+
return {"ok": True, "response": response.json()}
|
|
45
|
+
except httpx.HTTPError as exc:
|
|
46
|
+
return {"ok": False, "message": f"CRA-Shield ingest failed: {exc}"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: opencra-cli
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: OpenCRA command-line scanner — Syft wrapper, OSV + CISA KEV matching.
|
|
5
|
+
License: Apache-2.0
|
|
6
|
+
Requires-Python: >=3.12
|
|
7
|
+
Requires-Dist: httpx>=0.27
|
|
8
|
+
Requires-Dist: jinja2>=3.1
|
|
9
|
+
Requires-Dist: opencra-shared
|
|
10
|
+
Requires-Dist: rich>=13.9
|
|
11
|
+
Requires-Dist: typer>=0.15
|
|
12
|
+
Provides-Extra: pdf
|
|
13
|
+
Requires-Dist: weasyprint>=63.0; extra == 'pdf'
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
opencra_cli/__init__.py,sha256=iKbZoSTYswhd1ABTIP9bn6E6kj2LgKr-lhgLqaJXD2M,84
|
|
2
|
+
opencra_cli/app.py,sha256=6wiYso_vDoUn09HM-WnV69ihjEhqif_1lDmMIKZtcrw,8651
|
|
3
|
+
opencra_cli/db.py,sha256=is9zjXxkteo25RjJftTKyGSG8cWyiUe0edG6FU4MIsQ,6406
|
|
4
|
+
opencra_cli/httputil.py,sha256=PzADwxfk4QFcULncmbJx1-JKJN3n68C8RBChDzz0290,522
|
|
5
|
+
opencra_cli/kev.py,sha256=lceyHlo3OY0127qQIzteDx9xbYJSKWMhaMfH_r1OJ5g,2861
|
|
6
|
+
opencra_cli/match.py,sha256=228BK7uGJeMpAARE2m69SfghRWLScXMzteIiI29QC0I,3014
|
|
7
|
+
opencra_cli/nvd.py,sha256=IzKRd6AC9kPr7I5EmAqaupDdbJxuFHhWh6b3wzi2ALY,1277
|
|
8
|
+
opencra_cli/osv.py,sha256=iKXfPM2pmKmbjoUfERc2jgb4SxFLTqPS7TZg7Q5sCG0,2525
|
|
9
|
+
opencra_cli/pdf.py,sha256=iHzzSeVSPLTKxnz-ZQYYLxJfR4C77-vEVVZypUualMc,4292
|
|
10
|
+
opencra_cli/py.typed,sha256=AbpHGcgLb-kRsJGnwFEktk7uzpZOCcBY74-YBdrKVGs,1
|
|
11
|
+
opencra_cli/render.py,sha256=ES6NmTz7VHmKYSvuCtWeQlg9bntO1Gz4YDNnuNiibs8,2892
|
|
12
|
+
opencra_cli/syft.py,sha256=IuNMXDK_ozlw3QiNa1fz-B3rS2VOCSr-HVXcX-S190Y,4327
|
|
13
|
+
opencra_cli/sync.py,sha256=DT5_ponrfKsPlS-25bQRTsUhsqdwHwSLJJqkuij-tg8,1378
|
|
14
|
+
opencra_cli-0.1.0.dist-info/METADATA,sha256=CexHTYNopn9nV4IvS8SKaRukl57I9E68TMgsbye5pv4,385
|
|
15
|
+
opencra_cli-0.1.0.dist-info/WHEEL,sha256=zOwg4jB6zX2kU910N-cMawjivD6tO8NEWvE12je1bVk,87
|
|
16
|
+
opencra_cli-0.1.0.dist-info/entry_points.txt,sha256=OxRu28JUVu-jbtrM8DL6FPQz-4iX1TNwo2OY60ttLW4,48
|
|
17
|
+
opencra_cli-0.1.0.dist-info/RECORD,,
|