From 776e0fcd113fe5c772ace58e15d192137ea85b08 Mon Sep 17 00:00:00 2001 From: Harm Weites Date: Sun, 8 May 2016 18:07:16 +0200 Subject: [PATCH] Slack plugin for notifications (#305) --- lemur/plugins/lemur_slack/__init__.py | 5 ++ lemur/plugins/lemur_slack/plugin.py | 68 +++++++++++++++++++++++++++ setup.py | 1 + 3 files changed, 74 insertions(+) create mode 100644 lemur/plugins/lemur_slack/__init__.py create mode 100644 lemur/plugins/lemur_slack/plugin.py diff --git a/lemur/plugins/lemur_slack/__init__.py b/lemur/plugins/lemur_slack/__init__.py new file mode 100644 index 00000000..8ce5a7f3 --- /dev/null +++ b/lemur/plugins/lemur_slack/__init__.py @@ -0,0 +1,5 @@ +try: + VERSION = __import__('pkg_resources') \ + .get_distribution(__name__).version +except Exception as e: + VERSION = 'unknown' diff --git a/lemur/plugins/lemur_slack/plugin.py b/lemur/plugins/lemur_slack/plugin.py new file mode 100644 index 00000000..4505b18b --- /dev/null +++ b/lemur/plugins/lemur_slack/plugin.py @@ -0,0 +1,68 @@ +""" +.. module: lemur.plugins.lemur_slack.slack + :platform: Unix + :copyright: (c) 2015 by Netflix Inc., see AUTHORS for more + :license: Apache, see LICENSE for more details. + +.. moduleauthor:: Harm Weites +""" +from flask import current_app +from lemur.plugins.bases import ExpirationNotificationPlugin +from lemur.plugins import lemur_slack as slack + +import requests + + +def find_value(name, options): + for o in options: + if o['name'] == name: + return o['value'] + + +class SlackNotificationPlugin(ExpirationNotificationPlugin): + title = 'Slack' + slug = 'slack-notification' + description = 'Sends notifications to Slack' + version = slack.VERSION + + author = 'Harm Weites' + author_url = 'https://github.com/netflix/lemur' + + additional_options = [ + { + 'name': 'webhook', + 'type': 'str', + 'required': True, + 'validation': '^https:\/\/hooks\.slack\.com\/services\/.+$', + 'helpMessage': 'The url Slack told you to use for this integration', + }, { + 'name': 'username', + 'type': 'str', + 'required': True, + 'validation': '^.+$', + 'helpMessage': 'The great storyteller', + }, { + 'name': 'recipients', + 'type': 'str', + 'required': True, + 'validation': '^(@|#).+$', + 'helpMessage': 'Where to send to, either @username or #channel', + }, + ] + + @staticmethod + def send(event_type, message, targets, options, **kwargs): + """ + A typical check can be performed using the notify command: + `lemur notify` + """ + msg = 'Certificate expiry pending for certificate:\n*%s*\nCurrent state is: _%s_' % (message[0]['name'], event_type) + body = '{"text": "%s", "channel": "%s", "username": "%s"}' % (msg, find_value('recipients', options), find_value('username', options)) + + current_app.logger.info("Sending message to Slack: %s" % body) + current_app.logger.debug("Sending data to Slack endpoint at %s" % find_value('webhook', options)) + + r = requests.post(find_value('webhook', options), body) + if r.status_code not in [200]: + current_app.logger.error("Slack response: %s" % r.status_code) + raise diff --git a/setup.py b/setup.py index 64944f85..4249ca97 100644 --- a/setup.py +++ b/setup.py @@ -169,6 +169,7 @@ setup( 'aws_destination = lemur.plugins.lemur_aws.plugin:AWSDestinationPlugin', 'aws_source = lemur.plugins.lemur_aws.plugin:AWSSourcePlugin', 'email_notification = lemur.plugins.lemur_email.plugin:EmailNotificationPlugin', + 'slack_notification = lemur.plugins.lemur_slack.plugin:SlackNotificationPlugin', 'java_truststore_export = lemur.plugins.lemur_java.plugin:JavaTruststoreExportPlugin', 'java_keystore_export = lemur.plugins.lemur_java.plugin:JavaKeystoreExportPlugin', 'openssl_export = lemur.plugins.lemur_openssl.plugin:OpenSSLExportPlugin',