-
Notifications
You must be signed in to change notification settings - Fork 334
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[outputs] Komand support added (contribution from @0xdabbad00) #608
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
884f40d
Komand support added
0xdabbad00 5d5d701
Fix-ups from review and unit tests
0xdabbad00 fb01e51
Removed unneeded declaration
0xdabbad00 adb11d8
Merge branch 'master' into spiper-support_komand
0xdabbad00 b010778
Fixed linting
0xdabbad00 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,82 @@ | ||
""" | ||
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 | ||
import os | ||
|
||
from stream_alert.alert_processor import LOGGER | ||
from stream_alert.alert_processor.outputs.output_base import ( | ||
OutputDispatcher, | ||
OutputProperty, | ||
OutputRequestFailure, | ||
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/00000000-0000-0000-0000-000000000000/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') | ||
|
||
success = False | ||
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
76 changes: 76 additions & 0 deletions
76
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,76 @@ | ||
""" | ||
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 call, patch, PropertyMock | ||
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.get') | ||
@patch('requests.post') | ||
def test_dispatch_existing_container(self, post_mock, get_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.get') | ||
@patch('requests.post') | ||
def test_dispatch_container_failure(self, post_mock, get_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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
remove this declaration as it's unnecessary