Skip to content
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

Rule context can be modified within rule #526

Merged
merged 2 commits into from
Dec 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion stream_alert/rule_processor/rules_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,10 @@ def process_rule(record, rule):
(bool): The return function of the rule
"""
try:
rule_result = rule.rule_function(record)
if rule.context:
rule_result = rule.rule_function(record, rule.context)
else:
rule_result = rule.rule_function(record)
except Exception: # pylint: disable=broad-except
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@javuto I know this isn't part of your changes, but can you get rid of this except Exception: line and just have this be try: ... except: ... . This would get rid of that pylint disable message too

rule_result = False
LOGGER.exception(
Expand Down
36 changes: 36 additions & 0 deletions tests/unit/stream_alert_rule_processor/test_rules_engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -852,3 +852,39 @@ def match_ipaddress(_): # pylint: disable=unused-variable
payload = load_and_classify_payload(toggled_config, service, entity, raw_record)

assert_equal(len(new_rules_engine.process(payload)), 1)

def test_rule_modify_context(self):
"""Rules Engine - Testing Context Modification"""
@rule(logs=['test_log_type_json_nested_with_data'],
outputs=['s3:sample_bucket'],
context={'assigned_user': 'not_set', 'assigned_policy': 'not_set2'})
def modify_context_test(rec, context): # pylint: disable=unused-variable
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you shouldn't need this # pylint: disable=unused-variable do you?

"""Modify context rule"""
context['assigned_user'] = 'valid_user'
context['assigned_policy'] = 'valid_policy'
return rec['application'] == 'web-app'

kinesis_data = json.dumps({
'date': 'Dec 01 2016',
'unixtime': '1483139547',
'host': 'host1.web.prod.net',
'application': 'web-app',
'environment': 'prod',
'data': {
'category': 'web-server',
'type': '1',
'source': 'eu'
}
})

# prepare the payloads
service, entity = 'kinesis', 'test_kinesis_stream'
raw_record = make_kinesis_raw_record(entity, kinesis_data)
payload = load_and_classify_payload(self.config, service, entity, raw_record)

# process payloads
alerts = self.rules_engine.process(payload)

# alert tests
assert_equal(alerts[0]['context']['assigned_user'], 'valid_user')
assert_equal(alerts[0]['context']['assigned_policy'], 'valid_policy')