Fixing up notification testing (#575)
This commit is contained in:
@ -23,28 +23,28 @@ def create_certificate_url(name):
|
||||
)
|
||||
|
||||
|
||||
def create_expiration_attachments(messages):
|
||||
def create_expiration_attachments(certificates):
|
||||
attachments = []
|
||||
for message in messages:
|
||||
for certificate in certificates:
|
||||
attachments.append({
|
||||
'title': message['name'],
|
||||
'title_link': create_certificate_url(message['name']),
|
||||
'title': certificate['name'],
|
||||
'title_link': create_certificate_url(certificate['name']),
|
||||
'color': 'danger',
|
||||
'fallback': '',
|
||||
'fields': [
|
||||
{
|
||||
'title': 'Owner',
|
||||
'value': message['owner'],
|
||||
'value': certificate['owner'],
|
||||
'short': True
|
||||
},
|
||||
{
|
||||
'title': 'Expires',
|
||||
'value': arrow.get(message['not_after']).format('dddd, MMMM D, YYYY'),
|
||||
'value': arrow.get(certificate['validityEnd']).format('dddd, MMMM D, YYYY'),
|
||||
'short': True
|
||||
},
|
||||
{
|
||||
'title': 'Endpoints Detected',
|
||||
'value': len(message['endpoints']),
|
||||
'value': len(certificate['endpoints']),
|
||||
'short': True
|
||||
}
|
||||
],
|
||||
@ -54,6 +54,37 @@ def create_expiration_attachments(messages):
|
||||
return attachments
|
||||
|
||||
|
||||
def create_rotation_attachments(certificate):
|
||||
return {
|
||||
'title': certificate['name'],
|
||||
'title_link': create_certificate_url(certificate['name']),
|
||||
'fields': [
|
||||
{
|
||||
{
|
||||
'title': 'Owner',
|
||||
'value': certificate['owner'],
|
||||
'short': True
|
||||
},
|
||||
{
|
||||
'title': 'Expires',
|
||||
'value': arrow.get(certificate['validityEnd']).format('dddd, MMMM D, YYYY'),
|
||||
'short': True
|
||||
},
|
||||
{
|
||||
'title': 'Replaced By',
|
||||
'value': len(certificate['replaced'][0]['name']),
|
||||
'short': True
|
||||
},
|
||||
{
|
||||
'title': 'Endpoints Rotated',
|
||||
'value': len(certificate['endpoints']),
|
||||
'short': True
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
class SlackNotificationPlugin(ExpirationNotificationPlugin):
|
||||
title = 'Slack'
|
||||
slug = 'slack-notification'
|
||||
@ -85,25 +116,31 @@ class SlackNotificationPlugin(ExpirationNotificationPlugin):
|
||||
},
|
||||
]
|
||||
|
||||
def send(self, event_type, message, targets, options, **kwargs):
|
||||
def send(self, notification_type, message, targets, options, **kwargs):
|
||||
"""
|
||||
A typical check can be performed using the notify command:
|
||||
`lemur notify`
|
||||
"""
|
||||
if event_type == 'expiration':
|
||||
attachments = None
|
||||
if notification_type == 'expiration':
|
||||
attachments = create_expiration_attachments(message)
|
||||
|
||||
elif notification_type == 'rotation':
|
||||
attachments = create_rotation_attachments(message)
|
||||
|
||||
if not attachments:
|
||||
raise Exception('Unable to create message attachments')
|
||||
|
||||
body = {
|
||||
'text': 'Lemur Expiration Notification',
|
||||
'text': 'Lemur {0} Notification'.format(notification_type.capitalize()),
|
||||
'attachments': attachments,
|
||||
'channel': self.get_option('recipients', options),
|
||||
'username': self.get_option('username', options)
|
||||
}
|
||||
|
||||
r = requests.post(self.get_option('webhook', options), json.dumps(body))
|
||||
|
||||
if r.status_code not in [200]:
|
||||
raise Exception('Failed to send message')
|
||||
|
||||
current_app.logger.error("Slack response: {0} Message Body: {1}".format(r.status_code, body))
|
||||
|
@ -2,8 +2,8 @@
|
||||
|
||||
def test_formatting(certificate):
|
||||
from lemur.plugins.lemur_slack.plugin import create_expiration_attachments
|
||||
from lemur.notifications.service import _get_message_data
|
||||
data = [_get_message_data(certificate)]
|
||||
from lemur.certificates.schemas import certificate_notification_output_schema
|
||||
data = [certificate_notification_output_schema.dump(certificate).data]
|
||||
|
||||
attachment = {
|
||||
'title': certificate.name,
|
||||
|
Reference in New Issue
Block a user