Skip to content

Commit

Permalink
Add vulnerability_impact_notification DejacodeUser field #106
Browse files Browse the repository at this point in the history
Signed-off-by: tdruez <[email protected]>
  • Loading branch information
tdruez committed Dec 30, 2024
1 parent 1cc6f03 commit c20c636
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 3 deletions.
2 changes: 2 additions & 0 deletions dje/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1347,6 +1347,7 @@ class DejacodeUserAdmin(
"data_email_notification",
"workflow_email_notification",
"updates_email_notification",
"vulnerability_impact_notification",
"company",
"last_login",
"last_api_access",
Expand Down Expand Up @@ -1379,6 +1380,7 @@ class DejacodeUserAdmin(
"data_email_notification",
"workflow_email_notification",
"updates_email_notification",
"vulnerability_impact_notification",
)
},
),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 5.0.9 on 2024-12-30 13:55

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('dje', '0005_dataspaceconfiguration_vulnerabilities_risk_threshold'),
]

operations = [
migrations.AddField(
model_name='dejacodeuser',
name='vulnerability_impact_notification',
field=models.BooleanField(default=False, help_text='Enable to receive internal notifications about new and updated vulnerabilities, including their impact on your packages and products.'),
),
]
14 changes: 14 additions & 0 deletions dje/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1575,6 +1575,12 @@ def get_data_update_recipients(self, dataspace):
# Need to be converted as a list to be serializable
return list(qs.distinct().values_list("email", flat=True))

def get_vulnerability_notifications_users(self, dataspace):
"""Return the Users with `vulnerability_notification` enabled for a given dataspace."""
return self.get_queryset().filter(
vulnerability_impact_notification=True, dataspace=dataspace
)


class DejacodeUser(AbstractUser):
uuid = models.UUIDField(
Expand Down Expand Up @@ -1616,6 +1622,14 @@ class DejacodeUser(AbstractUser):
),
)

vulnerability_impact_notification = models.BooleanField(
default=False,
help_text=_(
"Enable to receive internal notifications about new and updated vulnerabilities, "
"including their impact on your packages and products."
),
)

company = models.CharField(
max_length=30,
blank=True,
Expand Down
19 changes: 16 additions & 3 deletions vulnerabilities/fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@

from timeit import default_timer as timer

from django.contrib.auth import get_user_model
from django.contrib.humanize.templatetags.humanize import intcomma
from django.core.management.base import CommandError
from django.urls import reverse
from django.utils import timezone

from notifications.signals import notify

from component_catalog.models import PACKAGE_URL_FIELDS
from component_catalog.models import Package
from dejacode_toolkit.vulnerablecode import VulnerableCode
Expand Down Expand Up @@ -106,6 +109,7 @@ def fetch_for_packages(

product_package_qs = ProductPackage.objects.filter(package__in=batch_affected_packages)
product_package_qs.update_weighted_risk_score()
break

return results

Expand Down Expand Up @@ -142,9 +146,9 @@ def notify_vulnerability_data_update(dataspace):
VulnerableCode.
"""
today = timezone.now().date()
vulnerability_qs = Vulnerability.objects.scope(dataspace).filter(last_modified_date__date=today)
vulnerability_qs = Vulnerability.objects.scope(dataspace) #.filter(last_modified_date__date=today)
package_qs = Package.objects.scope(dataspace).filter(
affected_by_vulnerabilities=vulnerability_qs
affected_by_vulnerabilities__in=vulnerability_qs
)
# product_qs = Product.objects.scope(dataspace).filter(packages=package_qs)

Expand All @@ -164,10 +168,19 @@ def notify_vulnerability_data_update(dataspace):
f"{package_count} packages affected at {package_list_url}?is_vulnerable=yes\n"
)

# 1. Webhooks
find_and_fire_hook(
"vulnerability.data_update",
instance=None,
dataspace=dataspace,
payload_override={"text": f"{subject}\n{message}"},
)
print(f"{subject}\n{message}")

# 2. Internal notifications
users_to_notify = get_user_model().objects.get_vulnerability_notifications_users(dataspace)
notify.send(
sender=Vulnerability,
verb="New vulnerabilities detected",
recipient=users_to_notify,
description=f"{message}",
)

0 comments on commit c20c636

Please sign in to comment.