|
| 1 | +# Copyright (c) nexB Inc. and others. All rights reserved. |
| 2 | +# VulnerableCode is a trademark of nexB Inc. |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | +# See http://www.apache.org/licenses/LICENSE-2.0 for the license text. |
| 5 | +# See https://github.com/aboutcode-org/vulnerablecode for support or download. |
| 6 | +# See https://aboutcode.org for more information about nexB OSS projects. |
| 7 | +# |
| 8 | + |
| 9 | +import gzip |
| 10 | +from pathlib import Path |
| 11 | +from typing import Iterable |
| 12 | + |
| 13 | +from aboutcode.pipeline import LoopProgress |
| 14 | +from fetchcode.vcs import fetch_via_vcs |
| 15 | + |
| 16 | +from vulnerabilities.importer import AdvisoryDataV2 |
| 17 | +from vulnerabilities.pipelines import VulnerableCodeBaseImporterPipelineV2 |
| 18 | +from vulnerabilities.pipelines.v2_importers.epss_importer_v2 import parse_epss_advisories |
| 19 | + |
| 20 | + |
| 21 | +class EPSSImporterHistoryPipeline(VulnerableCodeBaseImporterPipelineV2): |
| 22 | + """Exploit Prediction Scoring System (EPSS) History Importer""" |
| 23 | + |
| 24 | + pipeline_id = "epss_importer_v2" |
| 25 | + spdx_license_expression = "unknown" |
| 26 | + importer_name = "EPSS History Importer" |
| 27 | + datasource_id = "epss" |
| 28 | + repo_url = "https://github.com/empiricalsec/epss_scores" |
| 29 | + |
| 30 | + exclude_from_package_todo = True |
| 31 | + run_once = True |
| 32 | + precedence = 200 |
| 33 | + |
| 34 | + @classmethod |
| 35 | + def steps(cls): |
| 36 | + return ( |
| 37 | + cls.clone, |
| 38 | + cls.collect_and_store_advisories, |
| 39 | + cls.clean_up, |
| 40 | + ) |
| 41 | + |
| 42 | + def clone(self): |
| 43 | + self.log(f"Cloning `{self.repo_url}`") |
| 44 | + self.vcs_response = fetch_via_vcs(f"git+{self.repo_url}") |
| 45 | + |
| 46 | + def advisories_count(self) -> int: |
| 47 | + advisory_dir = Path(self.vcs_response.dest_dir) |
| 48 | + return sum(1 for f in advisory_dir.rglob("*.csv.gz") if "beta_scores" not in f.parts) |
| 49 | + |
| 50 | + def collect_advisories(self) -> Iterable[AdvisoryDataV2]: |
| 51 | + advisory_dir = Path(self.vcs_response.dest_dir) |
| 52 | + self.log(f"Scanning for EPSS CSV.gz files in: {advisory_dir}") |
| 53 | + |
| 54 | + epss_files = sorted( |
| 55 | + (f for f in advisory_dir.rglob("*.csv.gz") if "beta_scores" not in f.parts), |
| 56 | + key=lambda f: f.name, |
| 57 | + ) |
| 58 | + self.log(f"Found {len(epss_files)} EPSS files to process.") |
| 59 | + |
| 60 | + if epss_files: |
| 61 | + self.log(f"Processing EPSS data from {epss_files[0].name} " f"to {epss_files[-1].name}") |
| 62 | + |
| 63 | + progress = LoopProgress( |
| 64 | + total_iterations=len(epss_files), |
| 65 | + logger=self.log, |
| 66 | + ) |
| 67 | + |
| 68 | + for file_path in progress.iter(epss_files): |
| 69 | + relative_path = file_path.relative_to(advisory_dir) |
| 70 | + advisory_url = f"{self.repo_url}/blob/main/{relative_path.as_posix()}" |
| 71 | + |
| 72 | + try: |
| 73 | + with gzip.open(file_path, mode="rt", encoding="utf-8") as f: |
| 74 | + lines = f.readlines() |
| 75 | + except (OSError, gzip.BadGzipFile) as e: |
| 76 | + self.log(f"Failed to read {file_path}: {e}. Skipping this file.") |
| 77 | + continue |
| 78 | + |
| 79 | + yield from parse_epss_advisories( |
| 80 | + lines=lines, advisory_url=advisory_url, logger=self.log |
| 81 | + ) |
| 82 | + |
| 83 | + self.log(f"Finished processing all {len(epss_files)} EPSS files.") |
| 84 | + |
| 85 | + def clean_up(self): |
| 86 | + if getattr(self, "vcs_response", None): |
| 87 | + self.vcs_response.delete() |
| 88 | + self.log("Successfully removed cloned EPSS repository") |
| 89 | + |
| 90 | + def on_failure(self): |
| 91 | + self.log("EPSS importer pipeline failed, running cleanup") |
| 92 | + self.clean_up() |
0 commit comments