Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Work around PostgreSQL index issue

Signed-off-by: Tushar Goel <tushar.goel.dav@gmail.com>
  • Loading branch information
TG1999 committed Mar 28, 2022
1 parent 33e083c commit 0d4419d
Show file tree
Hide file tree
Showing 5 changed files with 527 additions and 3 deletions.
19 changes: 19 additions & 0 deletions vulnerabilities/migrations/0004_advisory_unique_content_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 4.0.2 on 2022-03-28 19:29

from django.db import migrations
from django.db import models


class Migration(migrations.Migration):

dependencies = [
("vulnerabilities", "0003_alter_advisory_created_by"),
]

operations = [
migrations.AddField(
model_name="advisory",
name="unique_content_id",
field=models.CharField(blank=True, max_length=32),
),
]
37 changes: 37 additions & 0 deletions vulnerabilities/migrations/0005_auto_20220328_1929.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Generated by Django 4.0.2 on 2022-03-28 19:29

import hashlib
import json

from django.db import migrations


class Migration(migrations.Migration):
def md5hash(apps, schema_editor):
Advisory = apps.get_model("vulnerabilities", "Advisory")
for advisory in Advisory.objects.all():
hash = ""
if advisory.summary:
hash = hashlib.md5(advisory.summary.encode("utf-8")).hexdigest()
json_fields = [advisory.affected_packages, advisory.references]
# Case 1: a ="Hello" and b="World"
# Case 2: a ="HelloWo" and b="rld"
# hash(a) + hash(b) for both the cases will give same result
# hash( hash(a) + hash(b) ) is better than hash(a + b)
for json_field in json_fields:
if json_field:
hash = hashlib.md5(
(
hash + hashlib.md5(json.dumps(json_field).encode("utf-8")).hexdigest()
).encode("utf-8")
).hexdigest()
advisory.unique_content_id = hash
advisory.save()

dependencies = [
("vulnerabilities", "0004_advisory_unique_content_id"),
]

operations = [
migrations.RunPython(md5hash),
]
17 changes: 17 additions & 0 deletions vulnerabilities/migrations/0006_alter_advisory_unique_together.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.0.2 on 2022-03-28 19:34

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("vulnerabilities", "0005_auto_20220328_1929"),
]

operations = [
migrations.AlterUniqueTogether(
name="advisory",
unique_together={("aliases", "unique_content_id", "date_published")},
),
]
25 changes: 22 additions & 3 deletions vulnerabilities/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
# Visit https://github.com/nexB/vulnerablecode/ for support and download.

import dataclasses
import hashlib
import importlib
import json
import logging
Expand Down Expand Up @@ -326,6 +327,26 @@ class Advisory(models.Model):
into structured data
"""

def save(self, *args, **kwargs):
hash = ""
if self.summary:
hash = hashlib.md5(self.summary.encode("utf-8")).hexdigest()
json_fields = [self.affected_packages, self.references]
# Case 1: a ="Hello" and b="World"
# Case 2: a ="HelloWo" and b="rld"
# hash(a) + hash(b) for both the cases will give same result
# therefore hash( hash(a) + hash(b) ) is better than hash(a + b)
for json_field in json_fields:
if json_field:
hash = hashlib.md5(
(hash + hashlib.md5(json.dumps(json_field).encode("utf-8")).hexdigest()).encode(
"utf-8"
)
).hexdigest()
self.unique_content_id = hash
super(Advisory, self).save(*args, **kwargs)

unique_content_id = models.CharField(max_length=32, blank=True)
aliases = models.JSONField(blank=True, default=list, help_text="A list of alias strings")
summary = models.TextField(blank=True, null=True)
# we use a JSON field here to avoid creating a complete relational model for data that
Expand Down Expand Up @@ -356,9 +377,7 @@ class Advisory(models.Model):
class Meta:
unique_together = (
"aliases",
"summary",
"affected_packages",
"references",
"unique_content_id",
"date_published",
)

Expand Down
Loading

0 comments on commit 0d4419d

Please sign in to comment.