コード例 #1
0
def test_mattermost_msg_fields():
    rule = {
        'name': 'Test Mattermost Rule',
        'type': 'any',
        'alert_text_type': 'alert_text_only',
        'mattermost_webhook_url': 'http://xxxxx',
        'mattermost_msg_pretext': 'aaaaa',
        'mattermost_msg_color': 'danger',
        'mattermost_msg_fields': [
            {
                'title': 'Stack',
                'value': "{0} {1}",
                'short': False,
                'args': ["type", "msg.status_code"]
            },
            {
                'title': 'Name',
                'value': 'static field',
                'short': False
            }
        ],
        'alert': [],
        'alert_subject': 'Test Mattermost'
    }
    rules_loader = FileRulesLoader({})
    rules_loader.load_modules(rule)
    alert = MattermostAlerter(rule)
    match = {
        '@timestamp': '2021-01-01T00:00:00',
        'somefield': 'foobarbaz'
    }
    with mock.patch('requests.post') as mock_post_request:
        alert.alert([match])

    expected_data = {
        'attachments': [
            {
                'fallback': 'Test Mattermost: aaaaa',
                'color': 'danger',
                'title': 'Test Mattermost',
                'pretext': 'aaaaa',
                'fields': [
                    {'title': 'Stack', 'value': '<MISSING VALUE> <MISSING VALUE>', 'short': False},
                    {'title': 'Name', 'value': 'static field', 'short': False}
                ],
                'text': 'Test Mattermost Rule\n\n'
            }
        ], 'username': '******'
    }

    mock_post_request.assert_called_once_with(
        rule['mattermost_webhook_url'],
        data=mock.ANY,
        headers={'content-type': 'application/json'},
        verify=True,
        proxies=None
    )

    actual_data = json.loads(mock_post_request.call_args_list[0][1]['data'])
    assert expected_data == actual_data
コード例 #2
0
def test_mattermost_msg_color(msg_color, except_msg_color):
    rule = {
        'name': 'Test Mattermost Rule',
        'type': 'any',
        'alert_text_type': 'alert_text_only',
        'mattermost_webhook_url': 'http://xxxxx',
        'mattermost_msg_pretext': 'aaaaa',
        'mattermost_author_icon': 'http://author.icon.url',
        'alert': [],
        'alert_subject': 'Test Mattermost'
    }

    if msg_color:
        rule['mattermost_msg_color'] = msg_color

    rules_loader = FileRulesLoader({})
    rules_loader.load_modules(rule)
    alert = MattermostAlerter(rule)
    match = {
        '@timestamp': '2021-01-01T00:00:00',
        'somefield': 'foobarbaz'
    }
    with mock.patch('requests.post') as mock_post_request:
        alert.alert([match])

    expected_data = {
        'attachments': [
            {
                'fallback': 'Test Mattermost: aaaaa',
                'color': except_msg_color,
                'title': 'Test Mattermost',
                'pretext': 'aaaaa',
                'fields': [],
                'text': 'Test Mattermost Rule\n\n',
                'author_icon': 'http://author.icon.url'
            }
        ],
        'username': '******'
    }

    mock_post_request.assert_called_once_with(
        rule['mattermost_webhook_url'],
        data=mock.ANY,
        headers={'content-type': 'application/json'},
        verify=True,
        proxies=None
    )

    actual_data = json.loads(mock_post_request.call_args_list[0][1]['data'])
    assert expected_data == actual_data
コード例 #3
0
def test_mattermost_get_aggregation_summary_text__maximum_width():
    rule = {
        'name': 'Test Mattermost Rule',
        'type': 'any',
        'alert_text_type': 'alert_text_only',
        'mattermost_webhook_url': 'http://xxxxx',
        'mattermost_msg_pretext': 'aaaaa',
        'mattermost_msg_color': 'danger',
        'mattermost_author_icon': 'http://author.icon.url',
        'alert': [],
        'alert_subject': 'Test Mattermost'
    }
    rules_loader = FileRulesLoader({})
    rules_loader.load_modules(rule)
    alert = MattermostAlerter(rule)
    assert 75 == alert.get_aggregation_summary_text__maximum_width()
コード例 #4
0
def test_mattermost_proxy(caplog):
    caplog.set_level(logging.INFO)
    rule = {
        'name': 'Test Mattermost Rule',
        'type': 'any',
        'alert_text_type': 'alert_text_only',
        'mattermost_webhook_url': 'http://xxxxx',
        'mattermost_msg_pretext': 'aaaaa',
        'mattermost_msg_color': 'danger',
        'mattermost_proxy': 'https://proxy.url',
        'alert': [],
        'alert_subject': 'Test Mattermost'
    }
    rules_loader = FileRulesLoader({})
    rules_loader.load_modules(rule)
    alert = MattermostAlerter(rule)
    match = {
        '@timestamp': '2021-01-01T00:00:00',
        'somefield': 'foobarbaz'
    }
    with mock.patch('requests.post') as mock_post_request:
        alert.alert([match])

    expected_data = {
        'attachments': [
            {
                'fallback': 'Test Mattermost: aaaaa',
                'color': 'danger',
                'title': 'Test Mattermost',
                'pretext': 'aaaaa',
                'fields': [],
                'text': 'Test Mattermost Rule\n\n'
            }
        ], 'username': '******'
    }

    mock_post_request.assert_called_once_with(
        rule['mattermost_webhook_url'],
        data=mock.ANY,
        headers={'content-type': 'application/json'},
        verify=True,
        proxies={'https': 'https://proxy.url'}
    )

    actual_data = json.loads(mock_post_request.call_args_list[0][1]['data'])
    assert expected_data == actual_data
    assert ('elastalert', logging.INFO, 'Alert sent to Mattermost') == caplog.record_tuples[0]
コード例 #5
0
def test_mattermost_kibana_discover_color():
    rule = {
        'name': 'Test Rule',
        'type': 'any',
        'alert_text_type': 'alert_text_only',
        'mattermost_attach_kibana_discover_url': True,
        'mattermost_kibana_discover_color': 'blue',
        'mattermost_webhook_url': 'http://please.dontgohere.mattermost',
        'alert': []
    }
    rules_loader = FileRulesLoader({})
    rules_loader.load_modules(rule)
    alert = MattermostAlerter(rule)
    match = {
        '@timestamp': '2021-01-01T00:00:00',
        'kibana_discover_url': 'http://localhost:5601/app/discover#/'
    }
    with mock.patch('requests.post') as mock_post_request:
        alert.alert([match])

    expected_data = {
        'attachments': [
            {
                'fallback': 'Test Rule: ',
                'color': 'danger',
                'title': 'Test Rule',
                'pretext': '',
                'fields': [],
                'text': 'Test Rule\n\n'
            },
            {
                'color': 'blue',
                'title': 'Discover in Kibana',
                'title_link': 'http://localhost:5601/app/discover#/'
            }
        ], 'username': '******'
    }
    mock_post_request.assert_called_once_with(
        rule['mattermost_webhook_url'],
        data=mock.ANY,
        headers={'content-type': 'application/json'},
        verify=True,
        proxies=None
    )

    actual_data = json.loads(mock_post_request.call_args_list[0][1]['data'])
    assert expected_data == actual_data
コード例 #6
0
def test_mattermost_getinfo():
    rule = {
        'name': 'Test Mattermost Rule',
        'type': 'any',
        'alert_text_type': 'alert_text_only',
        'mattermost_webhook_url': 'http://xxxxx',
        'alert': [],
        'alert_subject': 'Test Mattermost'
    }
    rules_loader = FileRulesLoader({})
    rules_loader.load_modules(rule)
    alert = MattermostAlerter(rule)

    expected_data = {
        'type': 'mattermost',
        'mattermost_username_override': 'elastalert',
        'mattermost_webhook_url': ['http://xxxxx']
    }
    actual_data = alert.get_info()
    assert expected_data == actual_data
コード例 #7
0
def test_mattermost_required_error(mattermost_webhook_url, expected_data):
    try:
        rule = {
            'name': 'Test Mattermost Rule',
            'type': 'any',
            'alert_text_type': 'alert_text_only',
            'alert': [],
            'alert_subject': 'Test Mattermost'
        }

        if mattermost_webhook_url:
            rule['mattermost_webhook_url'] = mattermost_webhook_url

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

        actual_data = alert.get_info()
        assert expected_data == actual_data
    except Exception as ea:
        assert expected_data in str(ea)
コード例 #8
0
def test_mattermost_ea_exception():
    with pytest.raises(EAException) as ea:
        rule = {
            'name': 'Test Mattermost Rule',
            'type': 'any',
            'alert_text_type': 'alert_text_only',
            'mattermost_webhook_url': 'http://xxxxx',
            'mattermost_msg_pretext': 'aaaaa',
            'mattermost_msg_color': 'danger',
            'mattermost_author_icon': 'http://author.icon.url',
            'alert': [],
            'alert_subject': 'Test Mattermost'
        }
        rules_loader = FileRulesLoader({})
        rules_loader.load_modules(rule)
        alert = MattermostAlerter(rule)
        match = {
            '@timestamp': '2021-01-01T00:00:00',
            'somefield': 'foobarbaz'
        }
        mock_run = mock.MagicMock(side_effect=RequestException)
        with mock.patch('requests.post', mock_run), pytest.raises(RequestException):
            alert.alert([match])
    assert 'Error posting to Mattermost: ' in str(ea)