blackstork-io / fabric

An open-source command-line tool for cybersecurity reporting automation and a configuration language for reusable templates. Reporting-as-Code
https://blackstork.io/fabric/
Apache License 2.0
27 stars 2 forks source link

Support content selection using meta tags #99

Open traut opened 7 months ago

traut commented 7 months ago

Background

In some cases, the structure of a produced document must slightly differ based on requirements. To avoid creating multiple templates for slightly different use cases, it should be possible to configure the rendering of the same template during execution.

Fabric already supports a version of this with no results condition and it should also be possible to mutate the template structure by runtime configuration.

Design

A straightforward way of achieving the reconfigurability of the template structure is to support tag filtering of the target blocks.

[!Note] The nesting nature of the templates requires the leaves-first filtering, meaning that if the parent block does not match the tag filter, but any of its descendants do match, the parent block is included in the output with all its descendants that match.

It also means all its descendants are included by default if the parent block matches the filter.

[!Note] To simplify the logic that figures out what to render, keeping in mind the depends_on dependencies, the filtering can happen post rendering, meaning the whole tree is evaluated but only the matching blocks are included in the output.

For example, with the template

document "test_doc" {

  meta {
    name = "Test Template"
    tags = ["foo", "bar"]
  }

  data inline "x" {
    items = ["aaa", "bbb", "ccc"]
  }

  content text {
    meta {
      tags = ["bar", "qux"]
    }
    query = ".data.inline.x.items | length"
    text = "There are {{ .query_result }} items"
  }

  content text {
    meta {
      tags = ["qux", ]
    }
    text = "Other static text"
  }
}

The results will differ depending on the CLI command:

traut commented 4 months ago

moved to milestone 0.5 to wait for https://github.com/blackstork-io/fabric/issues/159 as it is an enabler for this issue