|
| 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 | +from aboutcode.pipeline import LoopProgress |
| 10 | +from django.db import transaction |
| 11 | + |
| 12 | +from vulnerabilities.importers import IMPORTERS_REGISTRY |
| 13 | +from vulnerabilities.models import Advisory |
| 14 | +from vulnerabilities.models import Alias |
| 15 | +from vulnerabilities.pipelines import VulnerableCodePipeline |
| 16 | + |
| 17 | + |
| 18 | +class AddAdvisoryID(VulnerableCodePipeline): |
| 19 | + """ |
| 20 | + Pipeline to map CVEs from VulnerabilitySeverity to corresponding Advisories with CVSS3.1 scores. |
| 21 | + """ |
| 22 | + |
| 23 | + pipeline_id = "add_advisory_id" |
| 24 | + |
| 25 | + @classmethod |
| 26 | + def steps(cls): |
| 27 | + return (cls.add_advisory_id,) |
| 28 | + |
| 29 | + def add_advisory_id(self): |
| 30 | + |
| 31 | + advisories = Advisory.objects.all() |
| 32 | + |
| 33 | + advisories_to_update = [] |
| 34 | + |
| 35 | + batch_size = 500 |
| 36 | + |
| 37 | + progress = LoopProgress(total_iterations=advisories.count(), logger=self.log) |
| 38 | + |
| 39 | + for advisory in progress.iter(advisories.iterator(chunk_size=batch_size)): |
| 40 | + importer_name = advisory.created_by |
| 41 | + aliases = Alias.objects.filter(advisories=advisory).values_list("alias", flat=True) |
| 42 | + advisory_id = IMPORTERS_REGISTRY[importer_name].get_advisory_id(aliases=aliases) |
| 43 | + advisory.advisory_id = advisory_id |
| 44 | + advisories_to_update.append(advisory) |
| 45 | + if len(advisories_to_update) >= batch_size: |
| 46 | + self.do_bulk_update(advisories_to_update) |
| 47 | + advisories_to_update = [] |
| 48 | + self.do_bulk_update(advisories_to_update) |
| 49 | + self.log(f"Pipeline [{self.pipeline_name}] completed.") |
| 50 | + |
| 51 | + def do_bulk_update(self, advisories_to_update): |
| 52 | + Advisory.objects.bulk_update(advisories_to_update, ["advisory_id"]) |
| 53 | + self.log(f"Updated {len(advisories_to_update)} advisories with advisory_id.") |
0 commit comments