-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
269-deliver-notifications: Adds alerts with Gov UK Notify
269-deliver-notifications: Adds elastalert custom alerter tests
- Loading branch information
1 parent
5b249cf
commit 48c9e74
Showing
19 changed files
with
376 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export GITHUB_WEBHOOKSECRET=blah | ||
export ELASTICSEARCH_URL="http://localhost:9200" | ||
export GOVUK_NOTIFY_API_KEY=<_update_me_> | ||
export GOVUK_NOTIFY_TEMPLATE_ID=<_update_me_> | ||
export NOTIFICATION_EMAILS=[email protected] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
.env* | ||
.env | ||
|
||
scanrepo | ||
release | ||
|
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,12 +30,16 @@ services: | |
retries: 5 | ||
|
||
elastalert: | ||
# privileged needed for ntpd | ||
privileged: true | ||
build: | ||
context: elastalert | ||
environment: | ||
- GOVUK_NOTIFY_API_KEY | ||
- GOVUK_NOTIFY_TEMPLATE_ID | ||
- NOTIFICATION_EMAILS | ||
- ELASTICSEARCH_HOST=elasticsearch | ||
- ELASTICSEARCH_PORT=9200 | ||
- [email protected],[email protected] | ||
depends_on: | ||
elasticsearch: | ||
condition: service_healthy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
env | ||
|
||
# Created by https://www.gitignore.io/api/python | ||
|
||
### Python ### | ||
# Byte-compiled / optimized / DLL files | ||
__pycache__/ | ||
*.py[cod] | ||
*$py.class | ||
|
||
# C extensions | ||
*.so | ||
|
||
# Distribution / packaging | ||
.Python | ||
env/ | ||
build/ | ||
develop-eggs/ | ||
dist/ | ||
downloads/ | ||
eggs/ | ||
.eggs/ | ||
lib/ | ||
lib64/ | ||
parts/ | ||
sdist/ | ||
var/ | ||
wheels/ | ||
*.egg-info/ | ||
.installed.cfg | ||
*.egg | ||
|
||
# PyInstaller | ||
# Usually these files are written by a python script from a template | ||
# before PyInstaller builds the exe, so as to inject date/other infos into it. | ||
*.manifest | ||
*.spec | ||
|
||
# Installer logs | ||
pip-log.txt | ||
pip-delete-this-directory.txt | ||
|
||
# Unit test / coverage reports | ||
htmlcov/ | ||
.tox/ | ||
.coverage | ||
.coverage.* | ||
.cache | ||
nosetests.xml | ||
coverage.xml | ||
*,cover | ||
.hypothesis/ | ||
|
||
# Translations | ||
*.mo | ||
*.pot | ||
|
||
# Django stuff: | ||
*.log | ||
local_settings.py | ||
|
||
# Flask stuff: | ||
instance/ | ||
.webassets-cache | ||
|
||
# Scrapy stuff: | ||
.scrapy | ||
|
||
# Sphinx documentation | ||
docs/_build/ | ||
|
||
# PyBuilder | ||
target/ | ||
|
||
# Jupyter Notebook | ||
.ipynb_checkpoints | ||
|
||
# pyenv | ||
.python-version | ||
|
||
# celery beat schedule file | ||
celerybeat-schedule | ||
|
||
# SageMath parsed files | ||
*.sage.py | ||
|
||
# dotenv | ||
.env | ||
|
||
# virtualenv | ||
.venv | ||
venv/ | ||
ENV/ | ||
|
||
# Spyder project settings | ||
.spyderproject | ||
.spyproject | ||
|
||
# Rope project settings | ||
.ropeproject | ||
|
||
# mkdocs documentation | ||
/site | ||
|
||
# End of https://www.gitignore.io/api/python |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
env: | ||
@python3 -m venv env | ||
|
||
deps-update: | ||
@pip install -r requirements-to-freeze.txt --upgrade | ||
@pip freeze > requirements.txt | ||
|
||
deps: | ||
@pip install -r requirements.txt | ||
@pre-commit install | ||
|
||
clean: | ||
@pip uninstall -yr requirements.txt | ||
@pip freeze > requirements.txt | ||
|
||
autopep8: | ||
@autopep8 . --recursive --in-place --pep8-passes 2000 --verbose | ||
|
||
autopep8-stats: | ||
@pep8 --quiet --statistics . | ||
|
||
test: | ||
@pytest tests -vv | ||
|
||
.PHONY: deps lint test* debug clean |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
Message: ((Message)) | ||
Timestamp: ((Timestamp)) | ||
|
||
Filename: ((Filename)) | ||
Reason: ((Reason)) | ||
Organisation: ((Organisation)) | ||
Repo: ((Repo)) | ||
URL: ((URL)) | ||
|
||
Elasticsearch Index: ((ElasticsearchIndex)) | ||
Elasticsearch ID: ((ElasticsearchId)) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import os | ||
from elastalert.alerts import Alerter, BasicMatchString | ||
from notifications_python_client.notifications import NotificationsAPIClient | ||
|
||
|
||
class GovNotifyAlerter(Alerter): | ||
|
||
required_options = set(['log_file_path', 'email']) | ||
|
||
def __init__(self, rule): | ||
Alerter.__init__(self, rule) | ||
self.template_id = os.environ['GOVUK_NOTIFY_TEMPLATE_ID'] | ||
self.email_addresses = os.environ['NOTIFICATION_EMAILS'].split(',') | ||
api_key = os.environ['GOVUK_NOTIFY_API_KEY'] | ||
self.notifications_client = NotificationsAPIClient(api_key) | ||
|
||
@staticmethod | ||
def _generate_personalisation(match_items): | ||
personalisation = {} | ||
for i, v in enumerate(match_items): | ||
if v[0] == 'Message': | ||
personalisation['Message'] = v[1] | ||
elif v[0] == 'Timestamp': | ||
personalisation['Timestamp'] = v[1] | ||
elif v[0] == '_index': | ||
personalisation['ElasticsearchIndex'] = v[1] | ||
elif v[0] == '_id': | ||
personalisation['ElasticsearchId'] = v[1] | ||
elif v[0] == 'Data': | ||
personalisation['Filename'] = v[1]['filename'] | ||
personalisation['Reason'] = v[1]['reason'] | ||
personalisation['Organisation'] = v[1]['organisation'] | ||
personalisation['Repo'] = v[1]['repo'] | ||
personalisation['URL'] = v[1]['url'] | ||
return personalisation | ||
|
||
def _send_notification(self, email_address, personalisation): | ||
return self.notifications_client.send_email_notification( | ||
email_address=email_address, | ||
template_id=self.template_id, | ||
personalisation=personalisation, | ||
reference=None | ||
) | ||
|
||
def alert(self, matches): | ||
# Matches is a list of match dictionaries. | ||
# It contains more than one match when the alert has | ||
# the aggregation option set | ||
for match in matches: | ||
personalisation = self._generate_personalisation(match.items()) | ||
for email_address in self.email_addresses: | ||
self._send_notification( | ||
email_address, personalisation) | ||
|
||
with open(self.rule['log_file_path'], 'a') as output_file: | ||
# basic_match_string will transform the match into the default | ||
# human readable string format | ||
# https://github.com/Yelp/elastalert/blob/3931d7feaf0d07b6531fb53042b9284bb46712ce/elastalert/alerts.py#L128 | ||
match_string = str(BasicMatchString(self.rule, match)) | ||
output_file.write(match_string) | ||
|
||
# get_info is called after an alert is sent to get | ||
# data that is written back to Elasticsearch in the field "alert_info" | ||
# It should return a dict of information relevant to what the alert does | ||
def get_info(self): | ||
return {'type': 'GovUK Notify Alerter', | ||
'email': self.rule['email'], | ||
'log_file_path': self.rule['log_file_path']} |
Oops, something went wrong.