Skip to content

Commit

Permalink
Add Signals to cascade VulnerabilityAssignment on Asset deletion, Fix #…
Browse files Browse the repository at this point in the history
  • Loading branch information
renatoalmeidaoliveira committed Sep 7, 2024
1 parent c079305 commit 5571465
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 4 deletions.
1 change: 1 addition & 0 deletions develop/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ RUN git clone --single-branch --branch ${netbox_ver} https://github.com/netbox-c
# Work around https://github.com/pallets/markupsafe/issues/284
RUN pip install markupsafe==2.0.1
RUN pip install git+https://github.com/python/tzdata
RUN pip install tblib

# -------------------------------------------------------------------------------------
# Install Netbox Plugin
Expand Down
4 changes: 4 additions & 0 deletions nb_risk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,9 @@ class NbriskConfig(PluginConfig):
"additional_assets": [],
"proxies": {},
}

def ready(self):
from . import signals
super().ready()

config = NbriskConfig # noqa
4 changes: 2 additions & 2 deletions nb_risk/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class VulnerabilityAssignmentSerializer(NetBoxModelSerializer):
asset = serializers.SerializerMethodField('get_asset')
vulnerability = serializers.SlugRelatedField(slug_field="name", queryset=models.Vulnerability.objects.all())

asset_id = serializers.IntegerField(source='asset.id')
asset_id = serializers.IntegerField(source='asset.id', write_only=True)

def validate(self, data):
asset_id = data['asset']['id']
Expand All @@ -117,7 +117,7 @@ def get_asset(self, obj):
serializer = get_serializer_for_model(obj.asset, prefix='Nested')
context = {'request': self.context['request']}
return serializer(obj.asset, context=context).data

def get_display(self, obj):
return obj.name

Expand Down
2 changes: 1 addition & 1 deletion nb_risk/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def name(self):

def __str__(self):
return f"{self.asset} - {self.vulnerability.name}"

class Meta:
constraints = (
models.UniqueConstraint(
Expand Down
29 changes: 29 additions & 0 deletions nb_risk/signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from django.db.models.signals import post_delete
from django.dispatch import receiver
from django.contrib.contenttypes.models import ContentType
from netbox.plugins.utils import get_plugin_config


from . import models

@receiver(post_delete)
def handle_vulnerable_asset_delete(sender,instance, **kwargs):

supported_model_class = False
supported_assets = get_plugin_config("nb_risk", "supported_assets")
additional_assets = get_plugin_config("nb_risk", "additional_assets")
supported_assets = supported_assets + additional_assets

for asset in supported_assets:
app_label, model = asset.split(".")
model = ContentType.objects.get(app_label=app_label, model=model).model_class()
if isinstance(instance, model):
supported_model_class = True
break

if supported_model_class:
related_VulAssings = models.VulnerabilityAssignment.objects.filter(
asset_object_type=ContentType.objects.get_for_model(instance),
asset_id=instance.id )
for vulnAssign in related_VulAssings:
vulnAssign.delete()
2 changes: 1 addition & 1 deletion nb_risk/tests/vulnerabilityassignment/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,6 @@ def setUpTestData(cls):
},
]

cls.validation_excluded_fields = ['vulnerability']
cls.validation_excluded_fields = ['vulnerability', 'asset_id']

cls.brief_fields = [ 'display', 'id', 'url' ]

0 comments on commit 5571465

Please sign in to comment.