Skip to content

[DaC] additional line break added to query after export/import #6081

@wingiti

Description

@wingiti

Command or doc area

  • kibana export-rules / kibana import-rules
  • import-rules-to-repo
  • export-rules-from-repo
  • Other DaC CLI command (name it in the field below)
  • Documentation in this repo only (link the page/section below)

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions