fix(ingest): skip known KEV/FIRMS rows; CI skips unchanged images

CISA KEV republished 1685 NIST URLs every 5 min; FIRMS re-inserted
~325k global hotspots every 15 min. Producer now skips URLs already
in event_dedup and FIRMS CSVs that are unchanged (delta-only persist).

CI rebuilds only images whose paths changed and never pulls/rebuilds
Timescale or bounces osint-db unless Dockerfile.pg changes.
This commit is contained in:
Sirius DevOps 2026-08-29 19:32:00 -04:00
commit 5651d251d0
No known key found for this signature in database
9 changed files with 438 additions and 94 deletions

View file

@ -131,6 +131,100 @@ def test_ingest_fires_uses_keystore_key(monkeypatch):
assert any("VIIRS_NOAA21_NRT" in u for u in captured["urls"])
def _reset_firms_poll_state():
from upstream_cache import firms_cache
import fire_sources
firms_cache.clear()
if hasattr(fire_sources, "_csv_digest"):
fire_sources._csv_digest.clear()
if hasattr(fire_sources, "_seen_ids"):
fire_sources._seen_ids.clear()
def _fake_firms_http(monkeypatch, bodies_by_call: list[str] | None = None, body: str = SAMPLE_CSV):
hits = {"n": 0}
class FakeResp:
def __init__(self, text):
self.text = text
def raise_for_status(self):
pass
class FakeClient:
def __init__(self, **kw):
pass
async def __aenter__(self):
return self
async def __aexit__(self, *exc):
return False
async def get(self, url):
idx = hits["n"]
hits["n"] += 1
if bodies_by_call is not None:
text = bodies_by_call[min(idx, len(bodies_by_call) - 1)]
else:
text = body
return FakeResp(text)
monkeypatch.setenv("FIRMS_MAP_KEY", "k" * 32)
monkeypatch.setattr("fire_sources.FIRMS_DATASETS", ["VIIRS_NOAA20_NRT"])
monkeypatch.setattr("fire_sources.httpx.AsyncClient", FakeClient)
return hits
def test_ingest_fires_skips_unchanged_csv(monkeypatch):
"""Same FIRMS CSV must not be re-parsed into a 100k-row ON CONFLICT insert."""
_reset_firms_poll_state()
hits = _fake_firms_http(monkeypatch)
persisted = []
async def fake_persist(points):
persisted.append(len(points))
return len(points)
monkeypatch.setattr("fire_sources.persist_hotspots", fake_persist)
assert asyncio.run(ingest_fires()) == 5
assert persisted == [5]
firms_cache_hits = hits["n"]
persisted.clear()
assert asyncio.run(ingest_fires()) == 0
assert persisted == []
# TTL cache may skip HTTP; either way we must not persist again.
assert hits["n"] >= firms_cache_hits
def test_ingest_fires_persists_only_new_hotspots(monkeypatch):
"""When the CSV grows, persist the delta — not the whole 2-day dump."""
_reset_firms_poll_state()
extra = (
SAMPLE_CSV
+ "16.00000,-12.00000,340.00,0.40,0.40,2025-06-06,1500,N20,VIIRS,h,2.0NRT,310.00,8.00,D\n"
)
hits = _fake_firms_http(monkeypatch, bodies_by_call=[SAMPLE_CSV, extra])
persisted = []
async def fake_persist(points):
persisted.append([p["latitude"] for p in points])
return len(points)
monkeypatch.setattr("fire_sources.persist_hotspots", fake_persist)
from upstream_cache import firms_cache
assert asyncio.run(ingest_fires()) == 5
firms_cache.clear() # force the next poll to see the grown CSV
persisted.clear()
assert asyncio.run(ingest_fires()) == 1
assert persisted == [[16.0]]
assert hits["n"] == 2
def _async_return(value):
async def inner():
return value

View file

@ -64,3 +64,65 @@ def test_parse_cisa_kev_emits_cve_url_no_coords():
assert "cisa-kev" in ev["tags"]
assert "CVE-2024-1234" in ev["tags"]
assert ev["raw"]["cveID"] == "CVE-2024-1234"
def test_ingest_cisa_kev_does_not_republish_known_nist_urls(monkeypatch):
"""Producer must not push the whole KEV catalog to NATS every cycle."""
import asyncio
from sources import ingest_cisa_kev
payload = {
"vulnerabilities": [
{
"cveID": "CVE-2024-1111",
"vulnerabilityName": "old",
"dateAdded": "2024-01-01",
"shortDescription": "already in db",
},
{
"cveID": "CVE-2024-2222",
"vulnerabilityName": "new",
"dateAdded": "2024-06-01",
"shortDescription": "not in db yet",
},
]
}
class FakeResp:
def raise_for_status(self):
pass
def json(self):
return payload
class FakeClient:
def __init__(self, **kw):
pass
async def __aenter__(self):
return self
async def __aexit__(self, *exc):
return False
async def get(self, url):
return FakeResp()
published: list[str] = []
async def fake_publish(subject, event):
published.append(event["url"])
known = {"https://nvd.nist.gov/vuln/detail/CVE-2024-1111"}
async def fake_existing(urls):
return {u for u in urls if u in known}
monkeypatch.setattr("sources.httpx.AsyncClient", FakeClient)
monkeypatch.setattr("sources.publish_event", fake_publish)
monkeypatch.setattr("sources.existing_event_urls", fake_existing, raising=False)
n = asyncio.run(ingest_cisa_kev())
assert n == 1
assert published == ["https://nvd.nist.gov/vuln/detail/CVE-2024-2222"]

View file

@ -22,7 +22,11 @@ def test_firms_and_rss_caches_are_ttlcache():
def test_ingest_fires_hits_http_once_within_ttl(monkeypatch):
from fire_sources import _csv_digest, _seen_ids
firms_cache.clear()
_csv_digest.clear()
_seen_ids.clear()
monkeypatch.setenv("FIRMS_MAP_KEY", "k" * 32)
monkeypatch.setenv("FIRMS_DATASETS", "VIIRS_NOAA20_NRT")
# fire_sources already imported FIRMS_DATASETS — patch the module attr
@ -58,7 +62,7 @@ def test_ingest_fires_hits_http_once_within_ttl(monkeypatch):
monkeypatch.setattr("fire_sources.persist_hotspots", fake_publish)
assert asyncio.run(ingest_fires()) == 5
assert asyncio.run(ingest_fires()) == 5
assert asyncio.run(ingest_fires()) == 0
assert hits["n"] == 1