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

Fixing get_rule_file_hash to always return bytes #566

Merged
merged 2 commits into from
Nov 22, 2021
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
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 = ''
Copy link
Contributor Author

@JeffAshton JeffAshton Nov 22, 2021

Choose a reason for hiding this comment

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

Was returning string when file not found, which when concatenated with bytes in the case of import rules,

rule_file_hash += self.get_rule_file_hash(import_rule_file)

throws TypeError: can't concat str to bytes

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