From 62037914234737f48889735f2bcb26c38b0a8cf5 Mon Sep 17 00:00:00 2001 From: "Antoine.Blaud" Date: Thu, 21 Oct 2021 17:28:59 +0200 Subject: [PATCH 1/6] add a path exist check for rules_folder config var --- elastalert/loaders.py | 2 ++ tests/loaders_test.py | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/elastalert/loaders.py b/elastalert/loaders.py index 218c4080..eb20fca0 100644 --- a/elastalert/loaders.py +++ b/elastalert/loaders.py @@ -552,6 +552,8 @@ def get_names(self, conf, use_rule=None): rule_files = [] if 'scan_subdirectories' in conf and conf['scan_subdirectories']: for ruledir in rule_folders: + if not os.path.exists(ruledir): + raise EAException('Specified rule_folder does not exist: %s ' % ruledir) for root, folders, files in os.walk(ruledir, followlinks=True): # Openshift/k8s configmap fix for ..data and ..2021_05..date directories that loop with os.walk() folders[:] = [d for d in folders if not d.startswith('..')] diff --git a/tests/loaders_test.py b/tests/loaders_test.py index 51184aee..69184a82 100644 --- a/tests/loaders_test.py +++ b/tests/loaders_test.py @@ -14,7 +14,7 @@ from elastalert.loaders import RulesLoader from elastalert.util import EAException -test_config = {'rules_folder': 'test_folder', +test_config = {'rules_folder': './empty_folder_test', 'run_every': {'minutes': 10}, 'buffer_time': {'minutes': 10}, 'es_host': 'elasticsearch.test', @@ -168,7 +168,7 @@ def test_load_inline_alert_rule(): def test_file_rules_loader_get_names_recursive(): - conf = {'scan_subdirectories': True, 'rules_folder': 'root'} + conf = {'scan_subdirectories': True, 'rules_folder': './empty_folder_test'} rules_loader = FileRulesLoader(conf) walk_paths = (('root', ['folder_a', 'folder_b'], ('rule.yaml',)), ('root/folder_a', [], ('a.yaml', 'ab.yaml')), From 6c034cc2d57be7b3312b60bc8af6c03558fd20c6 Mon Sep 17 00:00:00 2001 From: "Antoine.Blaud" Date: Thu, 21 Oct 2021 17:55:05 +0200 Subject: [PATCH 2/6] add .empty --- tests/empty_folder_test/.empty | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 tests/empty_folder_test/.empty diff --git a/tests/empty_folder_test/.empty b/tests/empty_folder_test/.empty new file mode 100644 index 00000000..e69de29b From 1421d2376e294b10fcb16cd39f434b1f827f7372 Mon Sep 17 00:00:00 2001 From: "Antoine.Blaud" Date: Thu, 28 Oct 2021 14:03:13 +0200 Subject: [PATCH 3/6] Add simple test for invalid rules path --- tests/loaders_test.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/loaders_test.py b/tests/loaders_test.py index 69184a82..f86d742a 100644 --- a/tests/loaders_test.py +++ b/tests/loaders_test.py @@ -186,6 +186,15 @@ def test_file_rules_loader_get_names_recursive(): assert len(paths) == 4 +def test_file_rules_loader_get_names_invalid_path(): + conf = {'scan_subdirectories': True, 'rules_folder': './folder_missing#XYZ'} + try: + # folder missing so FileRulesLoader must throws an error + rules_loader = FileRulesLoader(conf) + assert False + except: + pass + def test_file_rules_loader_get_names(): class MockDirEntry: From 7cac908a665f113cd5d47c9c6aa57881c7700a31 Mon Sep 17 00:00:00 2001 From: "Antoine.Blaud" Date: Thu, 28 Oct 2021 14:50:45 +0200 Subject: [PATCH 4/6] fix tests (update check and pass py310) --- tests/loaders_test.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tests/loaders_test.py b/tests/loaders_test.py index f86d742a..9e232f81 100644 --- a/tests/loaders_test.py +++ b/tests/loaders_test.py @@ -190,11 +190,12 @@ def test_file_rules_loader_get_names_invalid_path(): conf = {'scan_subdirectories': True, 'rules_folder': './folder_missing#XYZ'} try: # folder missing so FileRulesLoader must throws an error - rules_loader = FileRulesLoader(conf) - assert False - except: + if FileRulesLoader(conf).get_names(conf): + assert False + except EAException: pass + def test_file_rules_loader_get_names(): class MockDirEntry: From 8027e2dc21232325bae8cc63f8843916559c0a89 Mon Sep 17 00:00:00 2001 From: "Antoine.Blaud" Date: Thu, 28 Oct 2021 15:37:40 +0200 Subject: [PATCH 5/6] updated CHANGELOG.md --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index d21ae951..80920c71 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -46,6 +46,8 @@ - [Docs] The documentation has been updated as the name of Amazon Elasticsearch Service has changed to Amazon OpenSearch Service. - [#478](https://github.com/jertel/elastalert2/pull/478) - @nsano-rururu - [Tests] Improve test coverage of tencentsms.py - [#479](https://github.com/jertel/elastalert2/pull/479) - @liuxingjun - [Docs] Tidy Exotel documentation - [#488](https://github.com/jertel/elastalert2/pull/488) - @ferozsalam +- Added a check on the value of the path "rules_folder" to make sure it exists. - @AntoineBlaud +- [Tests] Updated loaders_test by adding test_file_rules_loader_get_names_invalid_path. - @AntoineBlaud # 2.2.1 From 130e53416cbe9cfe3a06262260a6622daff63de5 Mon Sep 17 00:00:00 2001 From: Jason Ertel Date: Thu, 28 Oct 2021 09:42:27 -0400 Subject: [PATCH 6/6] Correct changelog for PR 519 Moved to upcoming release section. Remove line item for adding a unit test since it's implied with the actual PR change. Added link to PR. --- CHANGELOG.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8125ece9..eaea15b5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -25,6 +25,7 @@ - Upgrade to Python 3.10 and Sphinx 4.2.0 - [#501](https://github.com/jertel/elastalert2/pull/501) - @jertel - max_scrolling_count now has a default value of 990 to avoid stack overflow crashes - [#509](https://github.com/jertel/elastalert2/pull/509) - @jertel - Update pytest 6.2.5, pytest-cov 3.0.0, pytest-xdist 2.4.0, pylint<2.12, tox 3.24.4 - [#511](https://github.com/jertel/elastalert2/pull/511) - @nsano-rururu +- Added a check on the value of the path "rules_folder" to make sure it exists - [#519](https://github.com/jertel/elastalert2/pull/519) - @AntoineBlaud # 2.2.2 @@ -47,8 +48,6 @@ - [Docs] The documentation has been updated as the name of Amazon Elasticsearch Service has changed to Amazon OpenSearch Service. - [#478](https://github.com/jertel/elastalert2/pull/478) - @nsano-rururu - [Tests] Improve test coverage of tencentsms.py - [#479](https://github.com/jertel/elastalert2/pull/479) - @liuxingjun - [Docs] Tidy Exotel documentation - [#488](https://github.com/jertel/elastalert2/pull/488) - @ferozsalam -- Added a check on the value of the path "rules_folder" to make sure it exists. - @AntoineBlaud -- [Tests] Updated loaders_test by adding test_file_rules_loader_get_names_invalid_path. - @AntoineBlaud # 2.2.1