Skip to content

Commit

Permalink
Merge pull request #566 from JeffAshton/get_rule_file_hash-fix-return…
Browse files Browse the repository at this point in the history
…-type

Fixing get_rule_file_hash to always return bytes
  • Loading branch information
jertel authored Nov 22, 2021
2 parents ddc82f3 + a13f8f0 commit f6fb434
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
## Other changes
- sphinx 4.2.0 to 4.3.0 and tzlocal==2.1 - [#561](https://github.com/jertel/elastalert2/pull/561) - @nsano-rururu
- jinja2 3.0.1 to 3.0.3 - [#562](https://github.com/jertel/elastalert2/pull/562) - @nsano-rururu
- Fix `get_rule_file_hash` TypeError - [#566](https://github.com/jertel/elastalert2/pull/566) - @JeffAshton

# 2.2.3

Expand Down
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 f6fb434

Please sign in to comment.