Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consolidation of notification creation #8824

Merged
merged 36 commits into from
May 18, 2024

Conversation

kiblik
Copy link
Contributor

@kiblik kiblik commented Oct 13, 2023

Purpose of this PR: If some action triggers the creation of a notification during clicking in UI. The same notification should be triggered also if the same happens via API. Until now many actions have been avoiding these notifications. Implementation is done by Django Signals.

Some notes:

  • General rule: if the object is removed in cascade (e.g. removal product triggers removal of all engagements, tests, ...) notification is created only for the highest removed element (to avoid spamming).
  • The only exception to this ^ general rule is the removal of the Product. If Product Type is removed, notification is triggered for all Products as well
  • PR hasn't announced any new notifications. All of them have been created based on existing implementation.
  • There is a set of notifications which hasn't been migrated and they are performed only for UI actions because they would generate useless noise. E.g. "creation of findings" vs "creation of findings during scan imports". But "TODO" comments have been added.
  • If AuditLog is enabled, the message might contain also the actor's name. If the user disabled Auditlog, the user will be notified without knowing who did it. This is a small step back compared to the previous implementation.
  • Most of the actions have been with the label event="other". Now notification engine accepts any event name as meaningful and for Alerts event is translated to source name. If event type does not exist as the field in Notification, the engine works with the event as with other

@kiblik kiblik force-pushed the notification_api_avoiders branch from da741c1 to 3c0ad81 Compare October 13, 2023 08:36
@kiblik kiblik marked this pull request as draft October 13, 2023 09:08
@kiblik kiblik force-pushed the notification_api_avoiders branch 3 times, most recently from 2782354 to b08896c Compare October 14, 2023 19:18
@kiblik kiblik force-pushed the notification_api_avoiders branch from 3efebbc to a210f12 Compare October 23, 2023 19:12
@kiblik kiblik force-pushed the notification_api_avoiders branch from a210f12 to 10ef9e8 Compare October 31, 2023 09:46
Copy link
Contributor

github-actions bot commented Nov 7, 2023

python: can't open file '/home/runner/work/django-DefectDojo/django-DefectDojo/.github/scripts/git_protect.py': [Errno 2] No such file or directory

@kiblik kiblik force-pushed the notification_api_avoiders branch from 31156ef to 931cee7 Compare November 7, 2023 16:33
Copy link
Contributor

github-actions bot commented Nov 7, 2023

The following files are protected and cannot be modified:
dojo/product_type/views.py
dojo/engagement/views.py
dojo/product/views.py
dojo/test/views.py
dojo/notifications/helper.py
dojo/finding/views.py
unittests/test_notifications.py
dojo/utils.py
dojo/endpoint/views.py

@kiblik kiblik force-pushed the notification_api_avoiders branch from 931cee7 to 877a5a7 Compare November 7, 2023 17:09
Copy link
Contributor

github-actions bot commented Nov 7, 2023

The following files are protected and cannot be modified:
dojo/endpoint/views.py
dojo/engagement/views.py
dojo/product_type/views.py
dojo/utils.py
unittests/test_notifications.py
dojo/product/views.py
dojo/notifications/helper.py
dojo/test/views.py
dojo/finding/views.py

Copy link
Contributor

github-actions bot commented Nov 8, 2023

The following files are protected and cannot be modified:
dojo/finding/views.py
dojo/utils.py
unittests/test_rest_framework.py
dojo/test/views.py
dojo/engagement/views.py
dojo/product_type/views.py
dojo/notifications/helper.py
dojo/endpoint/views.py
unittests/test_notifications.py
dojo/product/views.py

@kiblik kiblik force-pushed the notification_api_avoiders branch from 73cdc7f to f77d4ab Compare November 8, 2023 14:49
Copy link
Contributor

github-actions bot commented Nov 8, 2023

The following files are protected and cannot be modified:
dojo/finding/views.py
unittests/test_rest_framework.py
dojo/utils.py
dojo/product_type/views.py
dojo/engagement/views.py
dojo/endpoint/views.py
dojo/test/views.py
dojo/product/views.py
dojo/notifications/helper.py
.github/workflows/check-protected-files.yml
requirements.txt
unittests/test_notifications.py

@kiblik kiblik force-pushed the notification_api_avoiders branch from f77d4ab to d732f5e Compare November 8, 2023 14:57
Copy link
Contributor

github-actions bot commented Nov 8, 2023

The following files are protected and cannot be modified:
dojo/engagement/views.py
dojo/notifications/helper.py
dojo/product/views.py
dojo/utils.py
dojo/product_type/views.py
requirements.txt
dojo/endpoint/views.py
dojo/finding/views.py
.github/workflows/check-protected-files.yml
dojo/test/views.py
unittests/test_notifications.py
unittests/test_rest_framework.py

Copy link
Contributor

github-actions bot commented Nov 8, 2023

The following files are protected and cannot be modified:
dojo/utils.py
dojo/endpoint/views.py
unittests/test_notifications.py
dojo/product/views.py
unittests/test_rest_framework.py
dojo/product_type/views.py
dojo/notifications/helper.py
requirements.txt
dojo/engagement/views.py
.github/workflows/check-protected-files.yml
dojo/finding/views.py
dojo/test/views.py

@kiblik kiblik force-pushed the notification_api_avoiders branch from 334ae53 to 3e2ddc0 Compare November 8, 2023 17:02
Copy link
Contributor

github-actions bot commented Nov 8, 2023

The following files are protected and cannot be modified:
unittests/test_notifications.py
dojo/product_type/views.py
dojo/engagement/views.py
unittests/test_rest_framework.py
dojo/product/views.py
dojo/notifications/helper.py
dojo/utils.py
dojo/test/views.py
dojo/endpoint/views.py
dojo/finding/views.py

Copy link
Contributor

github-actions bot commented Nov 8, 2023

The following files are protected and cannot be modified:
dojo/endpoint/views.py
unittests/test_notifications.py
dojo/product/views.py
dojo/test/views.py
dojo/engagement/views.py
dojo/notifications/helper.py
dojo/finding/views.py
dojo/product_type/views.py
dojo/utils.py
dojo/endpoint/receivers.py
unittests/test_rest_framework.py

@kiblik kiblik force-pushed the notification_api_avoiders branch 4 times, most recently from 556c299 to 3ec4cb3 Compare November 8, 2023 20:24
@github-actions github-actions bot added the parser label Nov 8, 2023
@kiblik kiblik marked this pull request as ready for review November 8, 2023 21:50
@kiblik kiblik force-pushed the notification_api_avoiders branch from e5f5171 to a5e8119 Compare November 9, 2023 21:44
@kiblik kiblik force-pushed the notification_api_avoiders branch from 2b4a518 to 8524d92 Compare May 3, 2024 06:43
Copy link
Contributor

github-actions bot commented May 3, 2024

Conflicts have been resolved. A maintainer will review the pull request shortly.

@kiblik kiblik closed this May 3, 2024
@kiblik kiblik reopened this May 3, 2024
Copy link
Contributor

@cneill cneill left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Most of the issues I found last time have been fixed! Thanks for digging into this. I think I found the source of the issue with endpoint deletions not triggering alerts: bulk delete (see video). I'm not sure whether that deserves its own PR or if it would be in scope for this one, I'll leave that up to you.

Comment on lines +22 to +23
description = _('The endpoint "%(name)s" was deleted by %(user)s') % {
'name': str(instance), 'user': le.actor}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just checked this again and it looks like the actor is being populated successfully now when the alert is created (API and UI)!

Screenshot 2024-05-16 at 11 45 54

However, I think I may have found the source of the missing notifications that I mentioned last time - using bulk delete on the Endpoints page does not trigger an alert.

Screen.Recording.2024-05-16.at.11.42.58.mov


logger = logging.getLogger(__name__)
@receiver(post_delete, sender=Test)
def test_post_delete(sender, instance, using, origin, **kwargs):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This remains true - no Test deletion alerts when deleting from either the UI or API

dojo/engagement/signals.py Outdated Show resolved Hide resolved
Comment on lines 24 to 28
if settings.ENABLE_AUDITLOG:
le = LogEntry.objects.get(
action=LogEntry.Action.DELETE,
content_type=ContentType.objects.get(app_label='dojo', model='product'),
object_id=instance.id
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is working with both API and UI now 👍

Screenshot 2024-05-16 at 11 58 43

@kiblik
Copy link
Contributor Author

kiblik commented May 17, 2024

Most of the issues I found last time have been fixed! Thanks for digging into this. I think I found the source of the issue with endpoint deletions not triggering alerts: bulk delete (see video). I'm not sure whether that deserves its own PR or if it would be in scope for this one, I'll leave that up to you.

Thank you for your feedback and for testing again.

  • Bulk removal of endpoint: It hasn't been implemented before this PR. Only single removal was implemented. So we would not be speaking only about fixing a bug but implementing the new feature. I would prefer to implement it in the new PR because smaller changes are easier to review and this PR is focused only on the migration of the notification processor.
    • If you are interested in the reason why it is not working now: post_delete signals are sending origin to inform you who triggered deletion. In case of removal of one endpoint (ep = Endpoint.objects.get(id=pk).delete()) origin is the endpoint itself. In case of bulk removal (eps = Endpoint.objects.filter(id__in=[pk1, pk2, pk3]).delete()), origin is QuerySet. This will need some special handling. By the way, Findings support bulk removal so they will need this handling as well.
  • Removal of test: This has been implemented before however I found a bug in the implementation. So this kind of notification was broken before this PR.
    • dojo/test/views.py:delete_test sends in create_notification list of recipients as users (as Django models)
      recipients=[test.engagement.lead],
      but dojo/notifications/helper.py:create_notification expects usernames (as strings)
      for recipient_notifications in Notifications.objects.filter(user__username__in=kwargs['recipients'], user__is_active=True, product=None):
      So same as Endpoints, I would prefer fix this bug in separated PR. Sidenote: I noticed the same bug in some other places as well. I will fix them there as well. Interesting that nobody complained :).

If everybody agrees, I hope this PR is ready for merge.

Copy link
Contributor

@cneill cneill left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the thorough explanation! I agree that it would be better to address the work required for those additional fixes in one or more follow-on PRs 👍

@mtesauro mtesauro merged commit 3273c68 into DefectDojo:dev May 18, 2024
237 of 238 checks passed
@kiblik kiblik deleted the notification_api_avoiders branch May 18, 2024 07:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
parser settings_changes Needs changes to settings.py based on changes in settings.dist.py included in this PR unittests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants