2016-09-14 23:52:59 +00:00
|
|
|
import logging
|
2018-09-15 22:07:50 +00:00
|
|
|
import re
|
|
|
|
|
|
|
|
from alerta.exceptions import RejectException
|
2019-04-23 20:58:14 +00:00
|
|
|
from alerta.plugins import PluginBase
|
2014-08-08 23:20:46 +00:00
|
|
|
|
2019-02-16 20:09:18 +00:00
|
|
|
LOG = logging.getLogger('alerta.plugins')
|
2014-10-11 22:20:21 +00:00
|
|
|
|
2017-09-01 22:22:59 +00:00
|
|
|
|
2014-08-08 23:20:46 +00:00
|
|
|
class RejectPolicy(PluginBase):
|
2018-11-27 11:12:40 +00:00
|
|
|
"""
|
|
|
|
Default reject policy will block alerts that do not have the following
|
|
|
|
required attributes:
|
|
|
|
1) environment - must match an allowed environment. By default it should
|
|
|
|
be either "Production" or "Development". Config setting is `ALLOWED_ENVIRONMENTS`.
|
|
|
|
2) service - must supply a value for service. Any value is acceptable.
|
|
|
|
"""
|
2014-08-08 23:20:46 +00:00
|
|
|
|
2019-04-23 20:58:14 +00:00
|
|
|
def pre_receive(self, alert, **kwargs):
|
|
|
|
|
|
|
|
ORIGIN_BLACKLIST = self.get_config('ORIGIN_BLACKLIST', default=[], type=list, **kwargs)
|
|
|
|
ALLOWED_ENVIRONMENTS = self.get_config('ALLOWED_ENVIRONMENTS', default=[], type=list, **kwargs)
|
|
|
|
|
|
|
|
ORIGIN_BLACKLIST_REGEX = [re.compile(x) for x in ORIGIN_BLACKLIST]
|
|
|
|
ALLOWED_ENVIRONMENT_REGEX = [re.compile(x) for x in ALLOWED_ENVIRONMENTS]
|
2018-10-10 17:52:55 +00:00
|
|
|
|
2015-09-03 21:10:14 +00:00
|
|
|
if any(regex.match(alert.origin) for regex in ORIGIN_BLACKLIST_REGEX):
|
2017-09-09 23:10:52 +00:00
|
|
|
LOG.warning("[POLICY] Alert origin '%s' has been blacklisted", alert.origin)
|
2014-10-11 19:58:54 +00:00
|
|
|
raise RejectException("[POLICY] Alert origin '%s' has been blacklisted" % alert.origin)
|
2014-08-08 23:20:46 +00:00
|
|
|
|
2018-03-16 21:49:41 +00:00
|
|
|
if not any(regex.match(alert.environment) for regex in ALLOWED_ENVIRONMENT_REGEX):
|
2018-09-15 22:20:53 +00:00
|
|
|
LOG.warning('[POLICY] Alert environment does not match one of %s', ', '.join(ALLOWED_ENVIRONMENTS))
|
|
|
|
raise RejectException('[POLICY] Alert environment does not match one of %s' %
|
2018-09-15 22:07:50 +00:00
|
|
|
', '.join(ALLOWED_ENVIRONMENTS))
|
2014-08-08 23:20:46 +00:00
|
|
|
|
|
|
|
if not alert.service:
|
2018-09-15 22:20:53 +00:00
|
|
|
LOG.warning('[POLICY] Alert must define a service')
|
|
|
|
raise RejectException('[POLICY] Alert must define a service')
|
2014-10-11 19:58:54 +00:00
|
|
|
|
|
|
|
return alert
|
2014-08-08 23:20:46 +00:00
|
|
|
|
2019-04-23 20:58:14 +00:00
|
|
|
def post_receive(self, alert, **kwargs):
|
2016-03-07 05:08:13 +00:00
|
|
|
return
|
2014-08-08 23:20:46 +00:00
|
|
|
|
2019-04-23 20:58:14 +00:00
|
|
|
def status_change(self, alert, status, text, **kwargs):
|
2014-12-15 11:32:23 +00:00
|
|
|
return
|
2019-02-11 18:51:06 +00:00
|
|
|
|
|
|
|
def take_action(self, alert, action, text, **kwargs):
|
|
|
|
raise NotImplementedError
|