-
Notifications
You must be signed in to change notification settings - Fork 513
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[FR] [DAC] Add exceptions importing from ndjson #3862
[FR] [DAC] Add exceptions importing from ndjson #3862
Conversation
Note, I expect we may want to have a docs update with this PR. |
👋 @eric-forte-elastic We can setup some time to go over these questions, but here are a few solely based on the PR summary.
One other note: If we updating the kibana lib dont forget we'll need to bump the version. |
rule: TOMLRuleContents, | ||
exception_data: List[dict], | ||
creation_date: str = TIME_NOW, | ||
updated_date: str = TIME_NOW, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would advise against generating TIME_NOW
on the module level and using it as a default variable. Often, it's ok to do module-level constants, but the benefit should be significant (usually, we're using it as a simplistic "caching" mechanism). As a rule of thumb with module-level vars - we can't guarantee that the module will always be imported in the way we expected it to be, so we need to be mindful of the lifecycle and the value the var. Especially when the variable is as as dynamic as current datetime (even though we serialize it into a date string).
The cleaner approach would be to make creation_date
and updated_date
either optional (or maybe even do not allow them to be set from the outside the method), and populate them with the current time inside the function with
creation_date = creation_date or datetime.now(timezone.utc).strftime("%Y/%m/%d")
I'm using datetime.now(timezone.utc)
here instead of time
to avoid all those corner cases when local non-UTC timezone can mess up the current date. These issues are so so difficult to debug when they happen.
exception.save_toml() | ||
except Exception as e: | ||
if skip_errors: | ||
print(f'- skipping {exception.rule_name} - {type(e).__name__}') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we use click.echo
here as well, for consistency?
@@ -103,6 +103,7 @@ def __next__(self) -> BaseResource: | |||
|
|||
class RuleResource(BaseResource): | |||
BASE_URI = "/api/detection_engine/rules" | |||
EXCEPTION_LIST_URI = '/api/exception_lists/_find' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe these can be just module-level Kibana path constants?
No longer needed, alternative PR merged. #3870 |
Note
This PR is an old PR that is now replaced by: #3869
Issues
#3674
Summary
This PR adds support for exporting exception lists from Kibana that are attached to rules that we export. This is accomplished by adding a small method
export_exception_list
to our Kibana library's resource.py file containing the definition of a rules resource. At this time we are not supporting managing exceptions/exception lists outside of rules. Meaning, every exception list needs to be tied to a rule. The purpose of this feature is to add support for importing and exporting rules that make use of exception lists to Kibana instances that do not have those lists already available.This PR is the export part of that functionality.
Note: the prior exception list logic does not support importing to Kibana. This PR contains only the export logic, a separate PR will be needed to support importing into Kibana.
Details
Current output if you attempt to import rules with exceptions list (the exceptions list does not get moved with the rule).
Additional note: while it may be desirable to also provide support for pulling exception lists when one has a .ndjson export from Kibana, this may be blocked by #3863.
Kibana API Docs
https://www.elastic.co/guide/en/security/current/exceptions-api-get-item.html
https://www.elastic.co/guide/en/security/current/exceptions-api-create-container.html
Testing
To test this code:
Details
-e
flag to also export the exceptions.Example Command:
python -m detection_rules kibana export-rules -d custom_rules/rules -s -sv -e
Note: Depending on how many custom rules you have in your Kibana instance, your numbers may be slightly different.
Expected Output
detection-rules on 3674-frdac-add-exceptions-importing-from-ndjson [!?] is v0.1.0 via v3.12.4 (detection-rules-build) on eric.forte took 2s
❯ python -m detection_rules kibana export-rules -d custom_rules/rules -s -sv -e
Loaded config file: /home/forteea1/Code/clean_mains/detection-rules/.detection-rules-cfg.json
█▀▀▄ ▄▄▄ ▄▄▄ ▄▄▄ ▄▄▄ ▄▄▄ ▄▄▄ ▄▄▄ ▄ ▄ █▀▀▄ ▄ ▄ ▄ ▄▄▄ ▄▄▄
█ █ █▄▄ █ █▄▄ █ █ █ █ █ █▀▄ █ █▄▄▀ █ █ █ █▄▄ █▄▄
█▄▄▀ █▄▄ █ █▄▄ █▄▄ █ ▄█▄ █▄█ █ ▀▄█ █ ▀▄ █▄▄█ █▄▄ █▄▄ ▄▄█
8 rules exported
8 rules converted
2 exceptions exported
8 saved to custom_rules/rules
exceptions/
folder in your custom rules directory.Example Exception File Contents
Example Rule File Toml (in rules directory of custom rules dir) that is attached to the above exception list