From accc8c7f25e828b88c756362b1d50f3ce586858f Mon Sep 17 00:00:00 2001 From: Jeff Ashton Date: Mon, 22 Nov 2021 10:41:54 -0500 Subject: [PATCH 1/2] Fixing get_rule_file_hash to always return bytes --- elastalert/loaders.py | 4 +++- tests/loaders_test.py | 10 ++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/elastalert/loaders.py b/elastalert/loaders.py index 3ffc1cde..f7782c39 100644 --- a/elastalert/loaders.py +++ b/elastalert/loaders.py @@ -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 diff --git a/tests/loaders_test.py b/tests/loaders_test.py index 9e232f81..2f28a918 100644 --- a/tests/loaders_test.py +++ b/tests/loaders_test.py @@ -1,4 +1,5 @@ # -*- coding: utf-8 -*- +from base64 import b64encode import copy import datetime import os @@ -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 From a13f8f0dfe544c184b04a4862a34293b3b69bfc7 Mon Sep 17 00:00:00 2001 From: Jeff Ashton Date: Mon, 22 Nov 2021 10:58:24 -0500 Subject: [PATCH 2/2] Updating CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index fc9233d0..9987ffcc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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