Skip to content

Commit d547a93

Browse files
committed
feat: add message about mirror when nvd is down
Signed-off-by: Aryan Bakliwal <[email protected]>
1 parent 0e39831 commit d547a93

File tree

2 files changed

+68
-43
lines changed

2 files changed

+68
-43
lines changed

cve_bin_tool/data_sources/nvd_source.py

Lines changed: 62 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -409,19 +409,30 @@ async def fetch_cves(self):
409409
tasks = []
410410
LOGGER.info("Getting NVD CVE data...")
411411
if self.nvd_type == "api2":
412+
self.LOGGER.info("Using mirror, with nvd_type %s and feed %s", self.nvd_type, self.feed)
412413
self.all_cve_entries = await asyncio.create_task(
413414
self.nist_fetch_using_api(),
414415
)
415416
else:
416-
nvd_metadata = await asyncio.create_task(
417-
self.nist_scrape(self.session),
418-
)
417+
self.LOGGER.info("Using NVD, with nvd_type %s and feed %s", self.nvd_type, self.feed)
418+
try:
419+
nvd_metadata = await asyncio.create_task(
420+
self.nist_scrape(self.session),
421+
)
422+
423+
tasks = [
424+
self.cache_update(self.session, url, meta["sha256"])
425+
for url, meta in nvd_metadata.items()
426+
if meta is not None
427+
]
428+
except Exception as e:
429+
self.LOGGER.info(f"nvd_source.py 429 fetch_cves() {e}")
430+
self.nvd_type = "api2"
431+
self.LOGGER.info("Falling back to mirror, with nvd_type %s and feed %s", self.nvd_type, self.feed)
432+
self.all_cve_entries = await asyncio.create_task(
433+
self.nist_fetch_using_api(),
434+
)
419435

420-
tasks = [
421-
self.cache_update(self.session, url, meta["sha256"])
422-
for url, meta in nvd_metadata.items()
423-
if meta is not None
424-
]
425436

426437
total_tasks = len(tasks)
427438

@@ -464,14 +475,17 @@ async def nist_fetch_using_api(self) -> list:
464475
api_key=self.nvd_api_key,
465476
api_version=api_version,
466477
)
467-
if self.incremental_update:
468-
await nvd_api.get_nvd_params(
469-
time_of_last_update=datetime.datetime.fromtimestamp(
470-
db.get_db_update_date()
478+
try:
479+
if self.incremental_update:
480+
await nvd_api.get_nvd_params(
481+
time_of_last_update=datetime.datetime.fromtimestamp(
482+
db.get_db_update_date()
483+
)
471484
)
472-
)
473-
else:
474-
await nvd_api.get_nvd_params()
485+
else:
486+
await nvd_api.get_nvd_params()
487+
except Exception as e:
488+
self.LOGGER.info(f"nvd_source.py 488 nist_fetch_using_api() {e}")
475489
await nvd_api.get()
476490
await nvd_api.session.close()
477491
nvd_api.session = None
@@ -490,18 +504,21 @@ async def getmeta(
490504
Returns:
491505
tuple: A tuple containing the URL for the JSON data and a dictionary of metadata.
492506
"""
493-
async with await session.get(meta_url) as response:
494-
response.raise_for_status()
495-
return (
496-
meta_url.replace(".meta", ".json.gz"),
497-
dict(
498-
[
499-
line.split(":", maxsplit=1)
500-
for line in (await response.text()).splitlines()
501-
if ":" in line
502-
]
503-
),
504-
)
507+
try:
508+
async with await session.get(meta_url) as response:
509+
response.raise_for_status()
510+
return (
511+
meta_url.replace(".meta", ".json.gz"),
512+
dict(
513+
[
514+
line.split(":", maxsplit=1)
515+
for line in (await response.text()).splitlines()
516+
if ":" in line
517+
]
518+
),
519+
)
520+
except Exception as e:
521+
self.LOGGER.info(f"nvd_source.py 521 getmeta() {e}")
505522

506523
async def nist_scrape(self, session: RateLimiter):
507524
"""
@@ -513,23 +530,26 @@ async def nist_scrape(self, session: RateLimiter):
513530
Returns:
514531
dict: A dictionary containing metadata links and their corresponding SHA values.
515532
"""
516-
async with await session.get(self.feed) as response:
517-
response.raise_for_status()
518-
page = await response.text()
519-
if self.nvd_type == "json-nvd":
520-
json_meta_links = self.META_REGEX_NVD.findall(page)
521-
meta_host = self.META_LINK_NVD
522-
else:
523-
json_meta_links = self.META_REGEX_MIRROR.findall(page)
524-
meta_host = self.META_LINK_MIRROR
525-
return dict(
526-
await asyncio.gather(
527-
*(
528-
self.getmeta(session, f"{meta_host}/{meta_url}")
529-
for meta_url in json_meta_links
533+
try:
534+
async with await session.get(self.feed) as response:
535+
response.raise_for_status()
536+
page = await response.text()
537+
if self.nvd_type == "json-nvd":
538+
json_meta_links = self.META_REGEX_NVD.findall(page)
539+
meta_host = self.META_LINK_NVD
540+
else:
541+
json_meta_links = self.META_REGEX_MIRROR.findall(page)
542+
meta_host = self.META_LINK_MIRROR
543+
return dict(
544+
await asyncio.gather(
545+
*(
546+
self.getmeta(session, f"{meta_host}/{meta_url}")
547+
for meta_url in json_meta_links
548+
)
530549
)
531550
)
532-
)
551+
except Exception as e:
552+
self.LOGGER.info(f"nvd_source.py 552 nist_scrape() {e}")
533553

534554
async def cache_update(
535555
self,

cve_bin_tool/nvd_api.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,11 @@ async def get_nvd_params(
163163
)
164164

165165
self.logger.info("Fetching metadata from NVD...")
166-
cve_count = await self.nvd_count_metadata(self.session)
166+
try:
167+
cve_count = await self.nvd_count_metadata(self.session)
168+
except Exception as e:
169+
self.logger.info(f"nvd_api.py 169 get_nvd_params() {e}")
170+
raise e
167171
self.logger.debug(f"NVD metadata {cve_count}")
168172

169173
await self.validate_nvd_api()
@@ -316,6 +320,7 @@ async def load_nvd_request(self, start_index):
316320
else:
317321
self.logger.debug(f"Pausing requests for {self.interval} seconds")
318322
time.sleep(self.interval)
323+
raise error
319324

320325
async def get(self):
321326
"""Calls load_nvd_request() multiple times to fetch all NVD feeds"""

0 commit comments

Comments
 (0)