diff --git a/CHANGELOG.md b/CHANGELOG.md index b367b18b..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 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/empty_folder_test/.empty b/tests/empty_folder_test/.empty new file mode 100644 index 00000000..e69de29b diff --git a/tests/loaders_test.py b/tests/loaders_test.py index 51184aee..9e232f81 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')), @@ -186,6 +186,16 @@ 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 + if FileRulesLoader(conf).get_names(conf): + assert False + except EAException: + pass + + def test_file_rules_loader_get_names(): class MockDirEntry: