-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathcli.py
More file actions
203 lines (173 loc) · 7.02 KB
/
Copy pathcli.py
File metadata and controls
203 lines (173 loc) · 7.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
"""Click sub-group mounted as `patchdiff-ai windows ...`.
Owns the platform-specific commands:
* `cve <CVE-ID> [--platform-id N]` — single-CVE run forced through
the Windows path (skips NVD auto-detect).
* `health-check` — run `WindowsProvider.health_check()`.
* `index <winsxs_path> ...` — index a WinSxS dump into a platform entry.
* `install` — run `WindowsProvider.install()`.
* `month <YYYY-MMM> [--platform-id N]` — Patch Tuesday batch run.
* `month-stats <YYYY-MMM>` — aggregate cached-report stats for a cycle.
* `recover-cache` — rebuild stale `report.cache` files under the temp dir.
"""
from __future__ import annotations
import asyncio
from typing import TYPE_CHECKING
import click
import structlog
from patchdiff_ai.cli.options import cve_options
from patchdiff_ai.platforms.windows.cycle import (
collect_cves,
download_cvrf,
normalize_month,
pick_ids,
)
from patchdiff_ai.platforms.windows.index import index_command
from patchdiff_ai.platforms.windows.month_stats import month_stats_command
from patchdiff_ai.platforms.windows.recover_cache import recover_cache_command
if TYPE_CHECKING:
from patchdiff_ai.platforms.base import Platform
from patchdiff_ai.platforms.windows.provider import WindowsProvider
log = structlog.get_logger(__name__)
_NATIVE_RESOLVE_CONCURRENCY = 10
def build_windows_group(provider: "WindowsProvider") -> click.Group:
grp = click.Group(name=provider.name, help="Windows (MSRC) operations.")
grp.add_command(index_command)
grp.add_command(month_stats_command)
grp.add_command(recover_cache_command)
@grp.command("health-check", help="Validate Windows-side prerequisites.")
def _hc() -> None:
ok = provider.health_check()
if not ok:
raise click.ClickException("Windows health-check reported failures.")
@grp.command("install", help="Install Windows-side prerequisites.")
def _install() -> None:
provider.install()
@grp.command("cve", help="Run RCA on a single CVE through the Windows path.")
@click.argument("cve_id", metavar="CVE-YYYY-NNNNN")
@click.option(
"--platform-id",
type=int,
default=None,
help="Force a specific MSRC product ID (Windows release).",
)
@cve_options
@click.pass_context
def _cve(
ctx: click.Context,
cve_id: str,
platform_id: int | None,
eval_mode: bool,
interrupt: bool,
chat: bool,
chat_permissive: bool,
) -> None:
from patchdiff_ai.cli.runner import run_single_cve
platform = provider.resolve(platform_id=platform_id)
run_single_cve(
ctx,
cve_id=cve_id,
platform=platform,
eval_mode=eval_mode,
interactive=interrupt,
chat=chat,
chat_permissive=chat_permissive,
)
@grp.command("month", help="Run analysis for an entire Patch Tuesday cycle.")
@click.argument("cycle_id", metavar="YYYY-MMM")
@click.option(
"--platform-id",
type=int,
default=None,
help="Force a specific MSRC product ID. Filters the cycle's CVE list "
"to those affecting that product AND runs every selected CVE "
"against that Windows release. Default: include CVEs affecting "
"any product in this cycle and pick the right Windows release "
"per CVE via MSRC.",
)
@click.option(
"--platform-name",
default="",
help="MSRC product-name filter (e.g. 'Windows 11 Version 24H2'). "
"Filter-only — does not force a Windows version. Combine with "
"--platform-id if you also want to force one.",
)
@click.option(
"--eval",
"eval_mode",
is_flag=True,
help="Generate reports across multiple models.",
)
@click.pass_context
def _month(
ctx: click.Context,
cycle_id: str,
platform_id: int | None,
platform_name: str,
eval_mode: bool,
) -> None:
# Cheap validation first — fail fast before the CVRF download.
try:
month = normalize_month(cycle_id)
except ValueError as exc:
raise click.BadParameter(str(exc))
from patchdiff_ai.cli.runner import run_batch_cves
cvrf = download_cvrf(month)
# Default product-ID filter when no flags given: every MSRC product
# this provider's configured versions know about. Without this the
# cycle returns zero CVEs because the legacy `pick_ids(set(), None)`
# contract was "filter by nothing → return nothing".
if platform_id is not None:
ids: set[str] = {str(platform_id)}
elif platform_name:
ids = set() # pick_ids resolves the name → ids itself
else:
ids = {
str(pid)
for v in provider.versions
for pid in v.spec.msrc_product_ids
}
targets, names = pick_ids(cvrf, platform_name or None, ids)
if not targets:
raise click.ClickException("No matching ProductIDs")
cve_rows = collect_cves(cvrf, targets)
if not cve_rows:
raise click.ClickException("No CVEs found")
click.echo(f"[*] Selected {names}: {len(cve_rows)} CVEs")
# Per-CVE platform resolution.
# --platform-id given → force that version for every CVE.
# --platform-id absent → ask MSRC per CVE (matches_native) and
# fall back to the newest manifest entry for any CVE MSRC misses.
if platform_id is not None:
forced = provider.resolve(platform_id=platform_id)
cves: list[tuple[str, "Platform"]] = [(row["CVE"], forced) for row in cve_rows]
else:
cves = asyncio.run(
_resolve_per_cve(provider, [row["CVE"] for row in cve_rows])
)
run_batch_cves(ctx, cves=cves, eval_mode=eval_mode)
return grp
async def _resolve_per_cve(
provider: "WindowsProvider", cve_ids: list[str]
) -> list[tuple[str, "Platform"]]:
"""Resolve each CVE's Windows version via MSRC, in parallel up to
`_NATIVE_RESOLVE_CONCURRENCY`. CVEs MSRC can't claim fall back to
`provider.resolve()` (newest manifest entry) and a warning log."""
sem = asyncio.Semaphore(_NATIVE_RESOLVE_CONCURRENCY)
async def _one(cve_id: str) -> "Platform":
async with sem:
try:
plat = await provider.matches_native(cve_id)
except Exception as exc:
log.warning("month_native_match_error", cve=cve_id, error=str(exc))
plat = None
if plat is None:
log.warning(
"month_native_match_fallback",
cve=cve_id,
hint="MSRC didn't claim this CVE for any configured Windows version. "
"Falling back to newest. Pass --platform-id to force.",
)
plat = provider.resolve()
return plat
plats = await asyncio.gather(*[_one(c) for c in cve_ids])
return list(zip(cve_ids, plats))