elastic / detection-rules

https://www.elastic.co/guide/en/security/current/detection-engine-overview.html
Other
1.92k stars 492 forks source link

[FR] Add Cleaner Error Handling for Improper CUSTOM_RULES_DIR Directories #3989

Closed eric-forte-elastic closed 1 month ago

eric-forte-elastic commented 1 month ago

Repository Feature

Detections-as-Code (DaC) - (primarily custom rule management)

Problem Description

If a user sets their CUSTOM_RULES_DIR environment variable before the directory exists, the resulting error message when attempting to parse the config could be more intuitive. See community Slack thread for more detail.

Regardless of the command run, an error message similar to the following is returned.

Traceback (most recent call last):
  File "<frozen runpy>", line 189, in _run_module_as_main
  File "<frozen runpy>", line 148, in _get_module_details
  File "<frozen runpy>", line 112, in _get_module_details
  File "/Users/***/Developer/detection-rules/detection_rules/__init__.py", line 13, in <module>
    from . import (  # noqa: E402
  File "/Users/***/Developer/detection-rules/detection_rules/custom_schemas.py", line 17, in <module>
    RULES_CONFIG = parse_rules_config()
                   ^^^^^^^^^^^^^^^^^^^^
  File "/Users/***/Developer/detection-rules/detection_rules/utils.py", line 323, in wrapped
    _cache[func_key][cache_key] = f(*args, **kwargs)
                                  ^^^^^^^^^^^^^^^^^^
  File "/Users/***/Developer/detection-rules/detection_rules/config.py", line 214, in parse_rules_config
    loaded = yaml.safe_load(path.read_text())
                            ^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/pathlib.py", line 1028, in read_text
    with self.open(mode='r', encoding=encoding, errors=errors) as f:
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/pathlib.py", line 1014, in open
    return io.open(self, mode, buffering, encoding, errors, newline)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: 'dac_custom_rules_dir/_config.yaml'

Desired Solution

This error message should be more intuitive, stating the error in such a way that the user is directed to a potential solution.

Considered Alternatives

No response

Additional Context

No response