Command or doc area
Other command name or doc link
No response
Summary
If you export and import a rule, the query at the destination contains an additional line break (\n) at the end.
This might not impact the functionality but it shows up if you compare the rule at source and destination system. I also expect you will get multiple trailing newlines if you redo the procedure.
The issue for this can be found at the point where the query is saved to the toml file:
|
# we want to preserve the threat_query format, but want to modify it in the context of encoded dump |
|
if threat_query: |
|
formatted_threat_query = "\nthreat_query = '''\n{}\n'''{}".format(threat_query, "\n\n" if bottom else "") |
|
top_out = top_out.replace('threat_query = "XXxXX"', formatted_threat_query) |
|
|
|
# we want to preserve the query format, but want to modify it in the context of encoded dump |
|
if query: |
|
formatted_query = "\nquery = '''\n{}\n'''{}".format(query, "\n\n" if bottom else "") |
|
top_out = top_out.replace('query = "XXxXX"', formatted_query) |
The original query is prefixed and suffixed with a line break (\n) before written to the toml file.
When the query is imported again, the toml parser removes the prefix newline but not the trailing one.
Multi-line basic strings are surrounded by three quotation marks on each side and allow newlines. A newline immediately following the opening delimiter will be trimmed. All other whitespace and newline characters remain intact.
Source: https://github.com/toml-lang/toml/blob/main/toml.md
I would suggest to not add line breaks to the original query.
UPDATE:
I tested it locally and removed the surrounding newlines which worked well. But then an additional issue occurred.
I identified that already the query part is stripped, which leads to the fact that if a rule on the source system contains an "intentional" white space or new line at the end, will loose it on the target system.
|
if _data == "rule": |
|
# - We want to avoid the encoder for the query and instead use kql-lint. |
|
# - Linting is done in rule.normalize() which is also called in rule.validate(). |
|
# - Until lint has tabbing, this is going to result in all queries being flattened with no wrapping, |
|
# but will at least purge extraneous white space |
|
query = contents["rule"].pop("query", "").strip() |
|
|
|
# - As tags are expanding, we may want to reconsider the need to have them in alphabetical order |
|
threat_query = contents["rule"].pop("threat_query", "").strip() |
Would be better if the query part is not touched to stay the same all the time.
Expected vs actual
No response
Environment (if relevant)
No response
Reproduction (if applicable)
Export/Import a rule with a multiline query.
Related issues (optional)
No response
Command or doc area
kibana export-rules/kibana import-rulesimport-rules-to-repoexport-rules-from-repoOther command name or doc link
No response
Summary
If you export and import a rule, the query at the destination contains an additional line break (\n) at the end.
This might not impact the functionality but it shows up if you compare the rule at source and destination system. I also expect you will get multiple trailing newlines if you redo the procedure.
The issue for this can be found at the point where the query is saved to the toml file:
detection-rules/detection_rules/rule_formatter.py
Lines 294 to 302 in 36e6f54
The original query is prefixed and suffixed with a line break (\n) before written to the toml file.
When the query is imported again, the toml parser removes the prefix newline but not the trailing one.
Source: https://github.com/toml-lang/toml/blob/main/toml.md
I would suggest to not add line breaks to the original query.
UPDATE:
I tested it locally and removed the surrounding newlines which worked well. But then an additional issue occurred.
I identified that already the query part is stripped, which leads to the fact that if a rule on the source system contains an "intentional" white space or new line at the end, will loose it on the target system.
detection-rules/detection_rules/rule_formatter.py
Lines 224 to 232 in 36e6f54
Would be better if the query part is not touched to stay the same all the time.
Expected vs actual
No response
Environment (if relevant)
No response
Reproduction (if applicable)
Export/Import a rule with a multiline query.
Related issues (optional)
No response