Skip to content

Commit

Permalink
Fixing get_rule_file_hash to always return bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffAshton committed Nov 22, 2021
1 parent ddc82f3 commit accc8c7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
4 changes: 3 additions & 1 deletion elastalert/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -606,12 +606,14 @@ def get_import_rule(self, rule):
return expanded_imports

def get_rule_file_hash(self, rule_file):
rule_file_hash = ''
if os.path.exists(rule_file):
with open(rule_file, 'rb') as fh:
rule_file_hash = hashlib.sha1(fh.read()).digest()
for import_rule_file in self.import_rules.get(rule_file, []):
rule_file_hash += self.get_rule_file_hash(import_rule_file)
else:
not_found = 'ENOENT ' + rule_file
rule_file_hash = hashlib.sha1(not_found.encode('utf-8')).digest()
return rule_file_hash

@staticmethod
Expand Down
10 changes: 10 additions & 0 deletions tests/loaders_test.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
from base64 import b64encode
import copy
import datetime
import os
Expand Down Expand Up @@ -488,3 +489,12 @@ def test_get_import_rule():
}
result = RulesLoader.get_import_rule('', rule)
assert 'a' == result


def test_get_rule_file_hash_when_file_not_found():
test_config_copy = copy.deepcopy(test_config)
rules_loader = FileRulesLoader(test_config_copy)
hash = rules_loader.get_rule_file_hash('empty_folder_test/file_not_found.yml')
assert isinstance(hash, bytes)
b64Hash = b64encode(hash).decode('ascii')
assert 'zR1Ml8y8S8Z/I5j7b48OH+DJqUw=' == b64Hash

0 comments on commit accc8c7

Please sign in to comment.