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 Better Error Handling for CUSTOM_RULES_DIR #3990

Closed eric-forte-elastic closed 1 month ago

eric-forte-elastic commented 1 month ago

Pull Request

Issue link(s): https://github.com/elastic/detection-rules/issues/3989

Summary - What I changed

Added error handling for CUSTOM_RULES_DIR, so that if the config file is not found in this directory, a more informative error message is now displayed suggesting to the user a method for fixing the issue.

How To Test

Set the CUSTOM_RULES_DIR environment variable to a non-existent directory, then run a detection rules command (e.g. setup-confg) as shown below.

(detection-rules-build) forteea1@forteea1-el1:/tmp/detection-rules$ export CUSTOM_RULES_DIR=/tmp/detection-rules/custom_rules_dir
(detection-rules-build) forteea1@forteea1-el1:/tmp/detection-rules$ python -m detection_rules custom-rules setup-config custom_rules_dir
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 "/tmp/detection-rules/detection_rules/__init__.py", line 13, in <module>
    from . import (  # noqa: E402
  File "/tmp/detection-rules/detection_rules/custom_schemas.py", line 17, in <module>
    RULES_CONFIG = parse_rules_config()
                   ^^^^^^^^^^^^^^^^^^^^
  File "/tmp/detection-rules/detection_rules/utils.py", line 323, in wrapped
    _cache[func_key][cache_key] = f(*args, **kwargs)
                                  ^^^^^^^^^^^^^^^^^^
  File "/tmp/detection-rules/detection_rules/config.py", line 215, in parse_rules_config
    raise FileNotFoundError(
FileNotFoundError: 
                Configuration file not found.
                Please create a configuration file. You may wish to use the 'setup-config' command
                and update the 'CUSTOM_RULES_DIR' environment variable as needed.

Checklist

Contributor checklist

protectionsmachine commented 1 month ago

Enhancement - Guidelines

These guidelines serve as a reminder set of considerations when addressing adding a feature to the code.

Documentation and Context

Code Standards and Practices

Testing

Additional Checks