Report catalogue pace per document, not only per byte
CI / Python 3.13 (push) Successful in 42s
CI / Python 3.12 (push) Successful in 47s
CI / Build and smoke-test image (push) Successful in 18s

This commit is contained in:
omar committed 2026-11-29 09:26:18 +03:00
1 parent 58e4ec35ac
commit 7763634e6f
2 files changed
+47 -2

No files matched your search

+12 -1
View File
@@ -11,6 +11,8 @@ from datetime import datetime
from pathlib import Path
from urllib.parse import urljoin, urlsplit
from time import monotonic
from .feeds_common import (BATCH_SIZE, JSON_LIMIT, MANIFEST_LIMIT, RECORD_LIMIT, FeedStage,
FeedState, IndexFeedHttp, RefreshResult, conditional, hash_file,
instant, read_json, response_validators, worker)
@@ -18,6 +20,7 @@ from ..transport import HttpError
from .parsers import InvalidData, parse
_LOG = logging.getLogger(__name__)
_PROGRESS_EVERY = 100
SIEMENS_METADATA = "https://cert-portal.siemens.com/productcert/csaf/provider-metadata.json"
MSRC_METADATA = "https://api.msrc.microsoft.com/cvrf/v3.0/csaf/provider-metadata.json"
@@ -172,13 +175,21 @@ async def _download_docs(
hash_path = staging.directory / "advisory.sha512"
batch = []
kept = 0
for entry in entries:
skipped = 0
started = monotonic()
for done, entry in enumerate(entries, 1):
if done % _PROGRESS_EVERY == 0:
elapsed = max(monotonic() - started, 0.001)
# Many small documents: pace is per file, so report that instead of bytes.
_LOG.info("%s import %d/%d %.1f/s eta %ds skipped %d", source, done, len(entries),
done / elapsed, int((len(entries) - done) / (done / elapsed)), skipped)
try:
docs = await _one_document(entry, source, http=http, staging=staging, path=path, hash_path=hash_path)
except (InvalidData, HttpError) as exc:
# One broken or withdrawn document must not cost the whole catalogue.
if isinstance(exc, HttpError) and exc.status != "not_found":
raise
skipped += 1
_LOG.warning("%s skipped %s %s", source, entry.id, getattr(exc, "status", type(exc).__name__))
continue
kept += 1
+35 -1
View File
@@ -141,7 +141,7 @@ async def test_one_broken_document_does_not_cost_the_catalogue(tmp_path, monkeyp
return original_client(transport=httpx.MockTransport(respond), trust_env=False)
monkeypatch.setattr(httpx, "AsyncClient", make_client)
caplog.set_level("WARNING")
caplog.set_level("INFO")
settings = SimpleNamespace(proxies=("http://proxy.test:8080",), max_retries=0, live_route="proxy", live_timeout=9.5)
async with Store(tmp_path) as store, HttpClients(settings, store) as clients:
assert await Sync(store, clients.index).refresh_once("msrc")
@@ -149,3 +149,37 @@ async def test_one_broken_document_does_not_cost_the_catalogue(tmp_path, monkeyp
assert cve.native_id == "msrc_" + _CVE
assert not await store.by_id(_ADV)
assert "msrc skipped msrc_adv260001" in caplog.text
def _identifier(url: str) -> str:
return url.rsplit("/", 1)[-1].removesuffix(".json").removeprefix("msrc_")
@pytest.mark.anyio
async def test_long_catalogue_reports_pace(tmp_path, monkeypatch, caplog) -> None:
metadata = {"metadata_version": "2.0", "list_on_CSAF_aggregators": True,
"distributions": [{"directory_url": _ROOT.rstrip("/")}]}
rows = b"".join(('"2026/msrc_adv%06d.json","2026-10-13T07:00:00Z"\r\n' % n).encode() for n in range(100))
rows += b'"2026/msrc_cve-2026-70175.json","2026-11-23T08:00:00Z"\r\n'
original_client = httpx.AsyncClient
def make_client(*, proxy: str | None, trust_env: bool) -> httpx.AsyncClient:
def respond(request: httpx.Request) -> httpx.Response:
url = str(request.url)
if url == _MANIFEST:
return httpx.Response(200, content=rows)
if url == _METADATA:
return httpx.Response(200, json=metadata)
if url == _CVE_URL:
return httpx.Response(200, json=_cve_document())
return httpx.Response(200, json=_document(_identifier(url), "2026-10-13T07:00:00.000Z"))
return original_client(transport=httpx.MockTransport(respond), trust_env=False)
monkeypatch.setattr(httpx, "AsyncClient", make_client)
caplog.set_level("INFO", logger="vuln_mcp.sources.feeds_csaf")
settings = SimpleNamespace(proxies=("http://proxy.test:8080",), max_retries=0, live_route="proxy", live_timeout=9.5)
async with Store(tmp_path) as store, HttpClients(settings, store) as clients:
assert await Sync(store, clients.index).refresh_once("msrc")
lines = [r.getMessage() for r in caplog.records if "import" in r.getMessage()]
assert len(rows.splitlines()) == 101
assert lines and lines[0].startswith("msrc import 100/101"), (lines, len(rows.splitlines()))