created __init__ method and used logger
@@ -6,25 +6,26 @@ import requests
from security_monkey import app
from security_monkey.alerters import custom_alerter
-alerter_registry = []
-slack_config = {}
-slack_hook = ""
-
class SlackAlerter(object):
__metaclass__ = custom_alerter.AlerterType
+ def __init__(self, cls, name, bases, attrs):
+ super().__init__(cls, name, bases, attrs)
+ self.slack_config = {}
+ self.slack_hook = ""
+
if app.config.get('SLACK_HOOK'):
- slack_config = {
+ self.slack_config = {
'channel': app.config.get('SLACK_CHANNEL'),
'username': app.config.get('SLACK_USERNAME'),
'icon_emoji': app.config.get('SLACK_ICON'),
}
- slack_hook = app.config.get('SLACK_HOOK')
+ self.slack_hook = app.config.get('SLACK_HOOK')
- slack_config['attachments'] = []
+ self.slack_config['attachments'] = []
def report_auditor_changes(self, auditor):
for item in auditor.items:
@@ -33,10 +34,11 @@ class SlackAlerter(object):
'text': "New Issue: Index: {!s}\n Account: {!s}\n Region: {!s}\n Name: {!s}".format(item.index, item.account, item.region, item.name)
}
- slack_config['attachments'].append(x)
+ self.slack_config['attachments'].append(x)
try:
- requests.post(slack_hook, data=json.dumps(slack_config))
- slack_config['attachments'] = []
+ requests.post(self.slack_hook, data=json.dumps(self.slack_config))
+ self.slack_config['attachments'] = []
except Exception as e:
- print("something has gone wrong with the slack post. Please check your configuration. " + e)
+ app.logger.exception(e)
+ app.logger.error("something has gone wrong with the slack post. Please check your configuration. " + e)
