Skip to content

Commit

Permalink
Add data migration for old nvd advisory
Browse files Browse the repository at this point in the history
Signed-off-by: Keshav Priyadarshi <git@keshav.space>
  • Loading branch information
keshav-space committed Sep 27, 2024
1 parent ee3edcb commit 2c2dfff
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
38 changes: 38 additions & 0 deletions vulnerabilities/migrations/0068_update_nvd_advisory_created_by.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Generated by Django 4.2.15 on 2024-09-27 19:38

from django.db import migrations

"""
Update the created_by field on Advisory from the old qualified_name
to the new pipeline_id.
"""


def update_created_by(apps, schema_editor):
from vulnerabilities.pipelines.nvd_importer import NVDImporterPipeline

Advisory = apps.get_model("vulnerabilities", "Advisory")
Advisory.objects.filter(created_by="vulnerabilities.importers.nvd.NVDImporter").update(
created_by=NVDImporterPipeline.pipeline_id
)



def reverse_update_created_by(apps, schema_editor):
from vulnerabilities.pipelines.nvd_importer import NVDImporterPipeline

Advisory = apps.get_model("vulnerabilities", "Advisory")
Advisory.objects.filter(created_by=NVDImporterPipeline.pipeline_id).update(
created_by="vulnerabilities.importers.nvd.NVDImporter"
)


class Migration(migrations.Migration):

dependencies = [
("vulnerabilities", "0067_update_github_advisory_created_by"),
]

operations = [
migrations.RunPython(update_created_by, reverse_code=reverse_update_created_by),
]
39 changes: 39 additions & 0 deletions vulnerabilities/tests/test_data_migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -802,3 +802,42 @@ def test_removal_of_duped_purls(self):
adv.filter(created_by="vulnerabilities.importers.github.GitHubAPIImporter").count() == 0
)
assert adv.filter(created_by="github_importer").count() == 1


class TestUpdateNVDAdvisoryCreatedByField(TestMigrations):
app_name = "vulnerabilities"
migrate_from = "0067_update_github_advisory_created_by"
migrate_to = "0068_update_nvd_advisory_created_by"

advisory_data1 = AdvisoryData(
aliases=["CVE-2020-13371337"],
summary="vulnerability description here",
affected_packages=[
AffectedPackage(
package=PackageURL(type="pypi", name="foobar"),
affected_version_range=VersionRange.from_string("vers:pypi/>=1.0.0|<=2.0.0"),
)
],
references=[Reference(url="https://example.com/with/more/info/CVE-2020-13371337")],
date_published=timezone.now(),
url="https://test.com",
)

def setUpBeforeMigration(self, apps):
Advisory = apps.get_model("vulnerabilities", "Advisory")
adv1 = Advisory.objects.create(
aliases=self.advisory_data1.aliases,
summary=self.advisory_data1.summary,
affected_packages=[pkg.to_dict() for pkg in self.advisory_data1.affected_packages],
references=[ref.to_dict() for ref in self.advisory_data1.references],
url=self.advisory_data1.url,
created_by="vulnerabilities.importers.nvd.NVDImporter",
date_collected=timezone.now(),
)

def test_removal_of_duped_purls(self):
Advisory = apps.get_model("vulnerabilities", "Advisory")
adv = Advisory.objects.all()

assert adv.filter(created_by="vulnerabilities.importers.nvd.NVDImporter").count() == 0
assert adv.filter(created_by="nvd_importer").count() == 1

0 comments on commit 2c2dfff

Please sign in to comment.