Skip to content

Commit 2c2dfff

Browse files
committed
Add data migration for old nvd advisory
Signed-off-by: Keshav Priyadarshi <[email protected]>
1 parent ee3edcb commit 2c2dfff

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Generated by Django 4.2.15 on 2024-09-27 19:38
2+
3+
from django.db import migrations
4+
5+
"""
6+
Update the created_by field on Advisory from the old qualified_name
7+
to the new pipeline_id.
8+
"""
9+
10+
11+
def update_created_by(apps, schema_editor):
12+
from vulnerabilities.pipelines.nvd_importer import NVDImporterPipeline
13+
14+
Advisory = apps.get_model("vulnerabilities", "Advisory")
15+
Advisory.objects.filter(created_by="vulnerabilities.importers.nvd.NVDImporter").update(
16+
created_by=NVDImporterPipeline.pipeline_id
17+
)
18+
19+
20+
21+
def reverse_update_created_by(apps, schema_editor):
22+
from vulnerabilities.pipelines.nvd_importer import NVDImporterPipeline
23+
24+
Advisory = apps.get_model("vulnerabilities", "Advisory")
25+
Advisory.objects.filter(created_by=NVDImporterPipeline.pipeline_id).update(
26+
created_by="vulnerabilities.importers.nvd.NVDImporter"
27+
)
28+
29+
30+
class Migration(migrations.Migration):
31+
32+
dependencies = [
33+
("vulnerabilities", "0067_update_github_advisory_created_by"),
34+
]
35+
36+
operations = [
37+
migrations.RunPython(update_created_by, reverse_code=reverse_update_created_by),
38+
]

vulnerabilities/tests/test_data_migrations.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,3 +802,42 @@ def test_removal_of_duped_purls(self):
802802
adv.filter(created_by="vulnerabilities.importers.github.GitHubAPIImporter").count() == 0
803803
)
804804
assert adv.filter(created_by="github_importer").count() == 1
805+
806+
807+
class TestUpdateNVDAdvisoryCreatedByField(TestMigrations):
808+
app_name = "vulnerabilities"
809+
migrate_from = "0067_update_github_advisory_created_by"
810+
migrate_to = "0068_update_nvd_advisory_created_by"
811+
812+
advisory_data1 = AdvisoryData(
813+
aliases=["CVE-2020-13371337"],
814+
summary="vulnerability description here",
815+
affected_packages=[
816+
AffectedPackage(
817+
package=PackageURL(type="pypi", name="foobar"),
818+
affected_version_range=VersionRange.from_string("vers:pypi/>=1.0.0|<=2.0.0"),
819+
)
820+
],
821+
references=[Reference(url="https://example.com/with/more/info/CVE-2020-13371337")],
822+
date_published=timezone.now(),
823+
url="https://test.com",
824+
)
825+
826+
def setUpBeforeMigration(self, apps):
827+
Advisory = apps.get_model("vulnerabilities", "Advisory")
828+
adv1 = Advisory.objects.create(
829+
aliases=self.advisory_data1.aliases,
830+
summary=self.advisory_data1.summary,
831+
affected_packages=[pkg.to_dict() for pkg in self.advisory_data1.affected_packages],
832+
references=[ref.to_dict() for ref in self.advisory_data1.references],
833+
url=self.advisory_data1.url,
834+
created_by="vulnerabilities.importers.nvd.NVDImporter",
835+
date_collected=timezone.now(),
836+
)
837+
838+
def test_removal_of_duped_purls(self):
839+
Advisory = apps.get_model("vulnerabilities", "Advisory")
840+
adv = Advisory.objects.all()
841+
842+
assert adv.filter(created_by="vulnerabilities.importers.nvd.NVDImporter").count() == 0
843+
assert adv.filter(created_by="nvd_importer").count() == 1

0 commit comments

Comments
 (0)