Skip to content

Commit 5991a67

Browse files
committed
Add ImportProblem model to store conflicts
Signed-off-by: Shivam Sandbhor <shivam.sandbhor@gmail.com>
1 parent 58d0376 commit 5991a67

2 files changed

Lines changed: 33 additions & 14 deletions

File tree

vulnerabilities/migrations/0001_initial.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Generated by Django 3.0.3 on 2020-04-10 15:49
1+
# Generated by Django 3.0.7 on 2020-07-10 05:48
22

33
import django.contrib.postgres.fields.jsonb
44
from django.db import migrations, models
@@ -26,10 +26,17 @@ class Migration(migrations.Migration):
2626
('name', models.CharField(help_text='Name of the importer', max_length=100, unique=True)),
2727
('license', models.CharField(blank=True, help_text='License of the vulnerability data', max_length=100)),
2828
('last_run', models.DateTimeField(help_text='UTC Timestamp of the last run', null=True)),
29-
('data_source', models.CharField(help_text='Class name of the data source implementation importable from vulnerabilities.importers', max_length=100)),
29+
('data_source', models.CharField(help_text='Name of the data source implementation importable from vulnerabilities.importers', max_length=100)),
3030
('data_source_cfg', django.contrib.postgres.fields.jsonb.JSONField(default=dict, help_text='Implementation-specific configuration for the data source')),
3131
],
3232
),
33+
migrations.CreateModel(
34+
name='ImportProblem',
35+
fields=[
36+
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
37+
('conflicting_model', django.contrib.postgres.fields.jsonb.JSONField()),
38+
],
39+
),
3340
migrations.CreateModel(
3441
name='Package',
3542
fields=[
@@ -38,13 +45,9 @@ class Migration(migrations.Migration):
3845
('namespace', models.CharField(blank=True, help_text='Package name prefix, such as Maven groupid, Docker image owner, GitHub user or organization, etc.', max_length=255, null=True)),
3946
('name', models.CharField(blank=True, help_text='Name of the package.', max_length=100, null=True)),
4047
('version', models.CharField(blank=True, help_text='Version of the package.', max_length=100, null=True)),
41-
('qualifiers', django.contrib.postgres.fields.jsonb.JSONField(default=dict, null=True, help_text='Extra qualifying data for a package such as the name of an OS, architecture, distro, etc.')),
4248
('subpath', models.CharField(blank=True, help_text='Extra subpath within a package, relative to the package root.', max_length=200, null=True)),
49+
('qualifiers', django.contrib.postgres.fields.jsonb.JSONField(default=dict, help_text='Extra qualifying data for a package such as the name of an OS, architecture, distro, etc.', null=True)),
4350
],
44-
options={
45-
'unique_together': {('name', 'namespace', 'type', 'version', 'qualifiers', 'subpath')},
46-
'abstract': False,
47-
},
4851
),
4952
migrations.CreateModel(
5053
name='Vulnerability',
@@ -94,6 +97,10 @@ class Migration(migrations.Migration):
9497
'unique_together': {('vulnerability', 'source', 'reference_id', 'url')},
9598
},
9699
),
100+
migrations.AlterUniqueTogether(
101+
name='package',
102+
unique_together={('name', 'namespace', 'type', 'version', 'qualifiers', 'subpath')},
103+
),
97104
migrations.AlterUniqueTogether(
98105
name='impactedpackage',
99106
unique_together={('vulnerability', 'package')},

vulnerabilities/models.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@ class Vulnerability(models.Model):
3939
A software vulnerability with minimal information. Identifiers other than CVE ID are stored as
4040
VulnerabilityReference.
4141
"""
42-
cve_id = models.CharField(max_length=50, help_text='CVE ID', unique=True, null=True)
43-
summary = models.TextField(help_text='Summary of the vulnerability', blank=True)
42+
cve_id = models.CharField(
43+
max_length=50, help_text='CVE ID', unique=True, null=True)
44+
summary = models.TextField(
45+
help_text='Summary of the vulnerability', blank=True)
4446
cvss = models.FloatField(max_length=100, help_text='CVSS Score', null=True)
4547

4648
def __str__(self):
@@ -75,10 +77,12 @@ class Package(PackageURLMixin):
7577
"""
7678
A software package with links to relevant vulnerabilities.
7779
"""
78-
vulnerabilities = models.ManyToManyField(to='Vulnerability', through='ImpactedPackage')
80+
vulnerabilities = models.ManyToManyField(
81+
to='Vulnerability', through='ImpactedPackage')
7982

8083
class Meta:
81-
unique_together = ('name', 'namespace', 'type', 'version', 'qualifiers', 'subpath')
84+
unique_together = ('name', 'namespace', 'type',
85+
'version', 'qualifiers', 'subpath')
8286
# Remove the `qualifers` and `set_package_url` overrides after
8387
# https://github.com/package-url/packageurl-python/pull/35 gets merged
8488
qualifiers = pgfields.JSONField(
@@ -103,7 +107,8 @@ def set_package_url(self, package_url):
103107
model_field = self._meta.get_field(field_name)
104108

105109
if value and len(value) > model_field.max_length:
106-
raise ValidationError(_('Value too long for field "{}".'.format(field_name)))
110+
raise ValidationError(
111+
_('Value too long for field "{}".'.format(field_name)))
107112

108113
setattr(self, field_name, value or None)
109114

@@ -135,15 +140,17 @@ class Importer(models.Model):
135140
Metadata and pointer to the implementation for a source of vulnerability data (aka security
136141
advisories)
137142
"""
138-
name = models.CharField(max_length=100, unique=True, help_text='Name of the importer')
143+
name = models.CharField(max_length=100, unique=True,
144+
help_text='Name of the importer')
139145

140146
license = models.CharField(
141147
max_length=100,
142148
blank=True,
143149
help_text='License of the vulnerability data',
144150
)
145151

146-
last_run = models.DateTimeField(null=True, help_text='UTC Timestamp of the last run')
152+
last_run = models.DateTimeField(
153+
null=True, help_text='UTC Timestamp of the last run')
147154

148155
data_source = models.CharField(
149156
max_length=100,
@@ -176,3 +183,8 @@ def make_data_source(self, batch_size: int, cutoff_date: datetime = None) -> Dat
176183

177184
def __str__(self):
178185
return self.name
186+
187+
188+
class ImportProblem(models.Model):
189+
190+
conflicting_model = pgfields.JSONField()

0 commit comments

Comments
 (0)