-
Notifications
You must be signed in to change notification settings - Fork 334
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #608 from 0xdabbad00/spiper-support_komand
Komand support added
- Loading branch information
Showing
8 changed files
with
166 additions
and
3 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
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
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,77 @@ | ||
""" | ||
Copyright 2017-present, Airbnb Inc. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
""" | ||
from collections import OrderedDict | ||
|
||
from stream_alert.alert_processor import LOGGER | ||
from stream_alert.alert_processor.outputs.output_base import ( | ||
OutputDispatcher, | ||
OutputProperty, | ||
StreamAlertOutput | ||
) | ||
|
||
|
||
@StreamAlertOutput | ||
class KomandOutput(OutputDispatcher): | ||
"""KomandOutput handles all alert dispatching for Komand""" | ||
__service__ = 'komand' | ||
|
||
@classmethod | ||
def get_user_defined_properties(cls): | ||
"""Get properties that must be asssigned by the user when configuring a new Komand | ||
output. This should be sensitive or unique information for this use-case that needs | ||
to come from the user. | ||
Every output should return a dict that contains a 'descriptor' with a description of the | ||
integration being configured. | ||
Returns: | ||
OrderedDict: Contains various OutputProperty items | ||
""" | ||
return OrderedDict([ | ||
('descriptor', | ||
OutputProperty(description='a short and unique descriptor for this ' | ||
'Komand integration')), | ||
('komand_auth_token', | ||
OutputProperty(description='the auth token for this Komand integration. ' | ||
'Example: 00000000-0000-0000-0000-000000000000', | ||
mask_input=True, | ||
cred_requirement=True)), | ||
('url', | ||
OutputProperty(description='the endpoint url for this Komand integration. ' | ||
'Example: https://YOUR-KOMAND-HOST.com/v2/triggers/GUID/events', | ||
mask_input=True, | ||
cred_requirement=True)) | ||
]) | ||
|
||
def dispatch(self, **kwargs): | ||
"""Send alert to Komand | ||
Args: | ||
**kwargs: consists of any combination of the following items: | ||
descriptor (str): Service descriptor (ie: slack channel, pd integration) | ||
alert (dict): Alert relevant to the triggered rule | ||
""" | ||
creds = self._load_creds(kwargs['descriptor']) | ||
if not creds: | ||
return self._log_status(False) | ||
|
||
headers = {'Authorization': creds['komand_auth_token']} | ||
|
||
LOGGER.debug('sending alert to Komand') | ||
|
||
resp = self._post_request(creds['url'], {'data': kwargs['alert']}, headers, False) | ||
success = self._check_http_response(resp) | ||
return self._log_status(success) |
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
74 changes: 74 additions & 0 deletions
74
tests/unit/stream_alert_alert_processor/test_outputs/test_komand.py
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,74 @@ | ||
""" | ||
Copyright 2017-present, Airbnb Inc. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
""" | ||
# pylint: disable=protected-access,attribute-defined-outside-init | ||
from mock import patch | ||
from moto import mock_s3, mock_kms | ||
from nose.tools import assert_false, assert_true | ||
|
||
from stream_alert.alert_processor.outputs.komand import KomandOutput | ||
from stream_alert_cli.helpers import put_mock_creds | ||
from tests.unit.stream_alert_alert_processor import CONFIG, FUNCTION_NAME, KMS_ALIAS, REGION | ||
from tests.unit.stream_alert_alert_processor.helpers import get_alert, remove_temp_secrets | ||
|
||
|
||
@mock_s3 | ||
@mock_kms | ||
@patch('stream_alert.alert_processor.outputs.output_base.OutputDispatcher.MAX_RETRY_ATTEMPTS', 1) | ||
class TestKomandutput(object): | ||
"""Test class for KomandOutput""" | ||
DESCRIPTOR = 'unit_test_komand' | ||
SERVICE = 'komand' | ||
CREDS = {'url': 'http://komand.foo.bar', | ||
'komand_auth_token': 'mocked_auth_token'} | ||
|
||
def setup(self): | ||
"""Setup before each method""" | ||
self._dispatcher = KomandOutput(REGION, FUNCTION_NAME, CONFIG) | ||
remove_temp_secrets() | ||
output_name = self._dispatcher.output_cred_name(self.DESCRIPTOR) | ||
put_mock_creds(output_name, self.CREDS, self._dispatcher.secrets_bucket, REGION, KMS_ALIAS) | ||
|
||
@patch('logging.Logger.info') | ||
@patch('requests.post') | ||
def test_dispatch_existing_container(self, post_mock, log_mock): | ||
"""KomandOutput - Dispatch Success""" | ||
post_mock.return_value.status_code = 200 | ||
|
||
assert_true(self._dispatcher.dispatch(descriptor=self.DESCRIPTOR, | ||
alert=get_alert())) | ||
|
||
log_mock.assert_called_with('Successfully sent alert to %s', self.SERVICE) | ||
|
||
@patch('logging.Logger.error') | ||
@patch('requests.post') | ||
def test_dispatch_container_failure(self, post_mock, log_mock): | ||
"""KomandOutput - Dispatch Failure""" | ||
post_mock.return_value.status_code = 400 | ||
json_error = {'message': 'error message', 'errors': ['error1']} | ||
post_mock.return_value.json.return_value = json_error | ||
|
||
assert_false(self._dispatcher.dispatch(descriptor=self.DESCRIPTOR, | ||
alert=get_alert())) | ||
|
||
log_mock.assert_called_with('Failed to send alert to %s', self.SERVICE) | ||
|
||
@patch('logging.Logger.error') | ||
def test_dispatch_bad_descriptor(self, log_error_mock): | ||
"""KomandOutput - Dispatch Failure, Bad Descriptor""" | ||
assert_false(self._dispatcher.dispatch(descriptor='bad_descriptor', | ||
alert=get_alert())) | ||
|
||
log_error_mock.assert_called_with('Failed to send alert to %s', self.SERVICE) |
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