Skip to content

Commit

Permalink
Merge branch 'master' into bugfix_gitter
Browse files Browse the repository at this point in the history
  • Loading branch information
jertel authored Jun 4, 2021
2 parents 2bf9f60 + f96b196 commit 73204d5
Show file tree
Hide file tree
Showing 16 changed files with 353 additions and 55 deletions.
10 changes: 5 additions & 5 deletions docs/source/ruletypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1779,7 +1779,7 @@ Required:

``dingtalk_access_token``: Dingtalk access token.

``dingtalk_msgtype``: Dingtalk msgtype. ``text``, ``markdown``, ``single_action_card``, ``action_card``.
``dingtalk_msgtype``: Dingtalk msgtype, default to ``text``. ``markdown``, ``single_action_card``, ``action_card``.

dingtalk_msgtype single_action_card Required:

Expand Down Expand Up @@ -2233,10 +2233,10 @@ The alerter requires the following options:
``ms_teams_webhook_url``: The webhook URL that includes your auth data and the ID of the channel you want to post to. Go to the Connectors
menu in your channel and configure an Incoming Webhook, then copy the resulting URL. You can use a list of URLs to send to multiple channels.

``ms_teams_alert_summary``: Summary should be configured according to `MS documentation <https://docs.microsoft.com/en-us/outlook/actionable-messages/card-reference>`_, although it seems not displayed by Teams currently.

Optional:

``ms_teams_alert_summary``: Summary should be configured according to `MS documentation <https://docs.microsoft.com/en-us/outlook/actionable-messages/card-reference>`_, although it seems not displayed by Teams currently, defaults to ``ElastAlert Message``.

``ms_teams_theme_color``: By default the alert will be posted without any color line. To add color, set this attribute to a HTML color value e.g. ``#ff0000`` for red.

``ms_teams_proxy``: By default ElastAlert will not use a network proxy to send notifications to MS Teams. Set this option using ``hostname:port`` if you need to use a proxy.
Expand Down Expand Up @@ -2768,9 +2768,9 @@ Zabbix will send notification to a Zabbix server. The item in the host specified

Required:

``zbx_sender_host``: The address where zabbix server is running.
``zbx_sender_host``: The address where zabbix server is running, defaults to ``'localhost'``.

``zbx_sender_port``: The port where zabbix server is listenning.
``zbx_sender_port``: The port where zabbix server is listenning, defaults to ``10051``.

``zbx_host``: This field setup the host in zabbix that receives the value sent by ElastAlert 2.

Expand Down
4 changes: 2 additions & 2 deletions elastalert/alerters/chatwork.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class ChatworkAlerter(Alerter):

def __init__(self, rule):
super(ChatworkAlerter, self).__init__(rule)
self.chatwork_apikey = self.rule.get('chatwork_apikey')
self.chatwork_room_id = self.rule.get('chatwork_room_id')
self.chatwork_apikey = self.rule['chatwork_apikey']
self.chatwork_room_id = self.rule['chatwork_room_id']
self.url = 'https://api.chatwork.com/v2/rooms/%s/messages' % (self.chatwork_room_id)
self.chatwork_proxy = self.rule.get('chatwork_proxy', None)
self.chatwork_proxy_login = self.rule.get('chatwork_proxy_login', None)
Expand Down
6 changes: 3 additions & 3 deletions elastalert/alerters/datadog.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@


class DatadogAlerter(Alerter):
''' Creates a Datadog Event for each alert '''
""" Creates a Datadog Event for each alert """
required_options = frozenset(['datadog_api_key', 'datadog_app_key'])

def __init__(self, rule):
super(DatadogAlerter, self).__init__(rule)
self.dd_api_key = self.rule.get('datadog_api_key', None)
self.dd_app_key = self.rule.get('datadog_app_key', None)
self.dd_api_key = self.rule['datadog_api_key']
self.dd_app_key = self.rule['datadog_app_key']

def alert(self, matches):
url = 'https://api.datadoghq.com/api/v1/events'
Expand Down
6 changes: 3 additions & 3 deletions elastalert/alerters/dingtalk.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@

class DingTalkAlerter(Alerter):
""" Creates a DingTalk room message for each alert """
required_options = frozenset(['dingtalk_access_token', 'dingtalk_msgtype'])
required_options = frozenset(['dingtalk_access_token'])

def __init__(self, rule):
super(DingTalkAlerter, self).__init__(rule)
self.dingtalk_access_token = self.rule.get('dingtalk_access_token')
self.dingtalk_access_token = self.rule['dingtalk_access_token']
self.dingtalk_webhook_url = 'https://oapi.dingtalk.com/robot/send?access_token=%s' % (self.dingtalk_access_token)
self.dingtalk_msgtype = self.rule.get('dingtalk_msgtype')
self.dingtalk_msgtype = self.rule.get('dingtalk_msgtype', 'text')
self.dingtalk_single_title = self.rule.get('dingtalk_single_title', 'elastalert')
self.dingtalk_single_url = self.rule.get('dingtalk_single_url', '')
self.dingtalk_btn_orientation = self.rule.get('dingtalk_btn_orientation', '')
Expand Down
6 changes: 4 additions & 2 deletions elastalert/alerters/httppost.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ class HTTPPostAlerter(Alerter):

def __init__(self, rule):
super(HTTPPostAlerter, self).__init__(rule)
post_url = self.rule.get('http_post_url')
post_url = self.rule['http_post_url']
if isinstance(post_url, str):
post_url = [post_url]
self.post_url = post_url
self.post_proxy = self.rule.get('http_post_proxy')
self.post_proxy = self.rule.get('http_post_proxy', None)
self.post_payload = self.rule.get('http_post_payload', {})
self.post_static_payload = self.rule.get('http_post_static_payload', {})
self.post_all_values = self.rule.get('http_post_all_values', not self.post_payload)
Expand All @@ -41,6 +41,8 @@ def alert(self, matches):
verify = self.post_ca_certs
else:
verify = not self.post_ignore_ssl_errors
if self.post_ignore_ssl_errors:
requests.packages.urllib3.disable_warnings()

headers.update(self.post_http_headers)
proxies = {'https': self.post_proxy} if self.post_proxy else None
Expand Down
2 changes: 1 addition & 1 deletion elastalert/alerters/teams.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

class MsTeamsAlerter(Alerter):
""" Creates a Microsoft Teams Conversation Message for each alert """
required_options = frozenset(['ms_teams_webhook_url', 'ms_teams_alert_summary'])
required_options = frozenset(['ms_teams_webhook_url'])

def __init__(self, rule):
super(MsTeamsAlerter, self).__init__(rule)
Expand Down
4 changes: 2 additions & 2 deletions elastalert/alerters/zabbix.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ def __init__(self, *args):

self.zbx_sender_host = self.rule.get('zbx_sender_host', 'localhost')
self.zbx_sender_port = self.rule.get('zbx_sender_port', 10051)
self.zbx_host = self.rule.get('zbx_host')
self.zbx_key = self.rule.get('zbx_key')
self.zbx_host = self.rule['zbx_host']
self.zbx_key = self.rule['zbx_key']
self.timestamp_field = self.rule.get('timestamp_field', '@timestamp')
self.timestamp_type = self.rule.get('timestamp_type', 'iso')
self.timestamp_strptime = self.rule.get('timestamp_strptime', '%Y-%m-%dT%H:%M:%S.%fZ')
Expand Down
2 changes: 2 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
[pytest]
markers =
elasticsearch: mark a test as using elasticsearch.
filterwarnings =
ignore::pytest.PytestUnhandledThreadExceptionWarning
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ m2r2
pluggy>=0.12.0
pre-commit
pylint<2.9
pytest==6.1.2
pytest==6.2.4
pytest-xdist==2.2.1
setuptools
sphinx_rtd_theme
Expand Down
54 changes: 54 additions & 0 deletions tests/alerters/chatwork_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,57 @@ def test_chatwork_ea_exception():
alert.alert([match])
except EAException:
assert True


def test_chatwork_getinfo():
rule = {
'name': 'Test Chatwork Rule',
'type': 'any',
'chatwork_apikey': 'xxxx1',
'chatwork_room_id': 'xxxx2',
'alert': []
}
rules_loader = FileRulesLoader({})
rules_loader.load_modules(rule)
alert = ChatworkAlerter(rule)

expected_data = {
"type": "chatwork",
"chatwork_room_id": "xxxx2"
}
actual_data = alert.get_info()
assert expected_data == actual_data


@pytest.mark.parametrize('chatwork_apikey, chatwork_room_id, expected_data', [
('', '', True),
('xxxx1', '', True),
('', 'xxxx2', True),
('xxxx1', 'xxxx2',
{
"type": "chatwork",
"chatwork_room_id": "xxxx2"
}),
])
def test_chatwork_key_error(chatwork_apikey, chatwork_room_id, expected_data):
try:
rule = {
'name': 'Test Chatwork Rule',
'type': 'any',
'alert': []
}

if chatwork_apikey != '':
rule['chatwork_apikey'] = chatwork_apikey

if chatwork_room_id != '':
rule['chatwork_room_id'] = chatwork_room_id

rules_loader = FileRulesLoader({})
rules_loader.load_modules(rule)
alert = ChatworkAlerter(rule)

actual_data = alert.get_info()
assert expected_data == actual_data
except KeyError:
assert expected_data
53 changes: 53 additions & 0 deletions tests/alerters/datadog_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,56 @@ def test_datadog_alerterea_exception():
alert.alert([match])
except EAException:
assert True


def test_datadog_getinfo():
rule = {
'name': 'Test Datadog Event Alerter',
'type': 'any',
'datadog_api_key': 'test-api-key',
'datadog_app_key': 'test-app-key',
'alert': [],
'alert_subject': 'Test Datadog Event Alert'
}
rules_loader = FileRulesLoader({})
rules_loader.load_modules(rule)
alert = DatadogAlerter(rule)

expected_data = {'type': 'datadog'}
actual_data = alert.get_info()
assert expected_data == actual_data


@pytest.mark.parametrize('datadog_api_key, datadog_app_key, expected_data', [
('', '', True),
('xxxx1', '', True),
('', 'xxxx2', True),
('xxxx1', 'xxxx2',
{
'type': 'datadog'
}),
])
def test_datadog_key_error(datadog_api_key, datadog_app_key, expected_data):
try:
rule = {
'name': 'Test Datadog Event Alerter',
'type': 'any',
'alert': [],
'alert_subject': 'Test Datadog Event Alert'
}

if datadog_api_key != '':
rule['datadog_api_key'] = datadog_api_key

if datadog_app_key != '':
rule['datadog_app_key'] = datadog_app_key

rules_loader = FileRulesLoader({})
rules_loader.load_modules(rule)
alert = DatadogAlerter(rule)

expected_data = {'type': 'datadog'}
actual_data = alert.get_info()
assert expected_data == actual_data
except KeyError:
assert expected_data
50 changes: 50 additions & 0 deletions tests/alerters/dingtalk_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,3 +292,53 @@ def test_dingtalk_ea_exception():
alert.alert([match])
except EAException:
assert True


def test_dingtalk_getinfo():
rule = {
'name': 'Test DingTalk Rule',
'type': 'any',
'dingtalk_access_token': 'xxxxxxx',
'alert': [],
'alert_subject': 'Test DingTalk'
}
rules_loader = FileRulesLoader({})
rules_loader.load_modules(rule)
alert = DingTalkAlerter(rule)

expected_data = {
'type': 'dingtalk',
"dingtalk_webhook_url": 'https://oapi.dingtalk.com/robot/send?access_token=xxxxxxx'
}
actual_data = alert.get_info()
assert expected_data == actual_data


@pytest.mark.parametrize('dingtalk_access_token,, expected_data', [
('', True),
('xxxxxxx',
{
'type': 'dingtalk',
"dingtalk_webhook_url": 'https://oapi.dingtalk.com/robot/send?access_token=xxxxxxx'
}),
])
def test_dingtalk_key_error(dingtalk_access_token, expected_data):
try:
rule = {
'name': 'Test DingTalk Rule',
'type': 'any',
'alert': [],
'alert_subject': 'Test DingTalk'
}

if dingtalk_access_token != '':
rule['dingtalk_access_token'] = dingtalk_access_token

rules_loader = FileRulesLoader({})
rules_loader.load_modules(rule)
alert = DingTalkAlerter(rule)

actual_data = alert.get_info()
assert expected_data == actual_data
except KeyError:
assert expected_data
Loading

0 comments on commit 73204d5

Please sign in to comment.