Skip to content

Commit

Permalink
Raise error on invalid alert address in alert send
Browse files Browse the repository at this point in the history
  • Loading branch information
johannaengland committed Sep 6, 2023
1 parent 44bcc84 commit d315678
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
7 changes: 7 additions & 0 deletions python/nav/alertengine/dispatchers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ class FatalDispatcherException(DispatcherException):
pass


class InvalidAlertAddressError(Exception):
"""Raised when an alert address is invalid, which is determined by the
is_valid_address method of each dispatcher"""

pass


def is_valid_email(address):
"""Validates a string as an e-mail address"""
try:
Expand Down
17 changes: 10 additions & 7 deletions python/nav/models/profiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@
import nav.buildconf
import nav.pwhash
from nav.config import getconfig as get_alertengine_config
from nav.alertengine.dispatchers import DispatcherException
from nav.alertengine.dispatchers import FatalDispatcherException
from nav.alertengine.dispatchers import (
DispatcherException,
FatalDispatcherException,
InvalidAlertAddressError,
)

from nav.models.event import AlertQueue, AlertType, EventType
from nav.models.manage import Arp, Cam, Category, Device, Location
Expand Down Expand Up @@ -423,10 +426,10 @@ def send(self, alert, subscription):
# Determine the right language for the user.
lang = self.account.preferences.get(Account.PREFERENCE_KEY_LANGUAGE, 'en')

if not (self.address or '').strip():
if not self.has_valid_address():
_logger.error(
'Ignoring alert %d (%s: %s)! Account %s does not have an '
'address set for the alertaddress with id %d, this needs '
'Ignoring alert %d (%s: %s)! Account %s does not have a '
'valid address for the alertaddress with id %d, this needs '
'to be fixed before the user will recieve any alerts.',
alert.id,
alert,
Expand All @@ -435,7 +438,7 @@ def send(self, alert, subscription):
self.id,
)

return True
raise InvalidAlertAddressError

if self.type.is_blacklisted():
_logger.debug(
Expand Down Expand Up @@ -1364,7 +1367,7 @@ def send(self):

super(AccountAlertQueue, self).delete()
return False
except FatalDispatcherException:
except (FatalDispatcherException, InvalidAlertAddressError):
self.delete()
return False

Expand Down

0 comments on commit d315678

Please sign in to comment.