- 
                Notifications
    You must be signed in to change notification settings 
- Fork 449
Feature: make ecs more flexible #2019
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
base: main
Are you sure you want to change the base?
Changes from 11 commits
798978f
              369f060
              fe04b23
              0a8be31
              1ef397e
              46968b0
              708f9d1
              b7aa72f
              a9e6c4f
              649b215
              3ce1373
              2096c6b
              bef27cd
              c7c2292
              7e38564
              a225031
              0871ba7
              File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -143,8 +143,14 @@ def save_asciidoc(f, text): | |
| # jinja2 setup | ||
|  | ||
|  | ||
| cur_dir = path.abspath(path.curdir) | ||
| local_dir = path.dirname(path.abspath(__file__)) | ||
| TEMPLATE_DIR = path.join(local_dir, '../templates') | ||
| CUR_TEMPLATE_DIR = path.join(cur_dir, 'templates') | ||
| LOCAL_TEMPLATE_DIR = path.join(local_dir, '../templates') | ||
| if path.exists(CUR_TEMPLATE_DIR): | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If these conditions both evaluate to false, this will exit with a TypeError. It would be better to catch this and print a friendly error message | ||
| TEMPLATE_DIR = CUR_TEMPLATE_DIR | ||
| elif path.exists(LOCAL_TEMPLATE_DIR): | ||
| TEMPLATE_DIR = LOCAL_TEMPLATE_DIR | ||
| template_loader = jinja2.FileSystemLoader(searchpath=TEMPLATE_DIR) | ||
| template_env = jinja2.Environment(loader=template_loader, keep_trailing_newline=True) | ||
|  | ||
|  | @@ -199,6 +205,7 @@ def page_field_values(nested, template_name='field_values_template.j2'): | |
| category_fields = ['event.kind', 'event.category', 'event.type', 'event.outcome'] | ||
| nested_fields = [] | ||
| for cat_field in category_fields: | ||
| nested_fields.append(nested['event']['fields'][cat_field]) | ||
| if nested.get("event", {}).get("fields", {}).get(cat_field) is not None: | ||
| nested_fields.append(nested['event']['fields'][cat_field]) | ||
|  | ||
| return dict(fields=nested_fields) | ||
| Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -15,7 +15,7 @@ | |
| # specific language governing permissions and limitations | ||
| # under the License. | ||
|  | ||
| from os.path import join | ||
| from os.path import join, dirname | ||
| from collections import OrderedDict | ||
| from typing import ( | ||
| Dict, | ||
|  | @@ -29,15 +29,21 @@ | |
| FieldNestedEntry, | ||
| ) | ||
|  | ||
| BEATS_DEFAULT_FIELDS = join(dirname(ecs_helpers.__file__), "beats_default_fields_allowlist.yml") | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this file is intended to be customized, I think it would be better to add a generator.py argument that can be used to specify the file location used | ||
|  | ||
|  | ||
| def generate( | ||
| ecs_nested: Dict[str, FieldNestedEntry], | ||
| ecs_version: str, | ||
| out_dir: str | ||
| ) -> None: | ||
| # base first | ||
| ecs_nested = ecs_helpers.remove_top_level_reusable_false(ecs_nested) | ||
| beats_fields: List[OrderedDict] = fieldset_field_array(ecs_nested['base']['fields'], ecs_nested['base']['prefix']) | ||
| if 'base' in ecs_nested: | ||
| beats_fields: List[OrderedDict] = fieldset_field_array( | ||
| ecs_nested['base']['fields'], ecs_nested['base']['prefix']) | ||
| else: | ||
| beats_fields = [] | ||
|  | ||
|  | ||
| allowed_fieldset_keys: List[str] = ['name', 'title', 'group', 'description', 'footnote', 'type'] | ||
| # other fieldsets | ||
|  | @@ -56,7 +62,7 @@ def generate( | |
| beats_fields.append(beats_field) | ||
|  | ||
| # Load temporary allowlist for default_fields workaround. | ||
| df_allowlist = ecs_helpers.yaml_load('scripts/generators/beats_default_fields_allowlist.yml') | ||
| df_allowlist = ecs_helpers.yaml_load(BEATS_DEFAULT_FIELDS) | ||
| # Set default_field configuration. | ||
| set_default_field(beats_fields, df_allowlist) | ||
|  | ||
|  | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|  | @@ -38,13 +38,14 @@ | |||||||||||||||||||||||||||||||||
| def generate( | ||||||||||||||||||||||||||||||||||
| ecs_nested: Dict[str, FieldNestedEntry], | ||||||||||||||||||||||||||||||||||
| ecs_version: str, | ||||||||||||||||||||||||||||||||||
| ecs_component_name_prefix: str, | ||||||||||||||||||||||||||||||||||
| out_dir: str, | ||||||||||||||||||||||||||||||||||
| mapping_settings_file: str, | ||||||||||||||||||||||||||||||||||
| template_settings_file: str | ||||||||||||||||||||||||||||||||||
| ) -> None: | ||||||||||||||||||||||||||||||||||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not really having context for this tool, adding a new argument like this in the middle seems like it might not be the most compatible way of doing this.. Usually I'd expect new arguments to show up in the  
        Suggested change
       
 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Moved to kwarg There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LGTM | ||||||||||||||||||||||||||||||||||
| """This generates all artifacts for the composable template approach""" | ||||||||||||||||||||||||||||||||||
| all_component_templates(ecs_nested, ecs_version, out_dir) | ||||||||||||||||||||||||||||||||||
| component_names = component_name_convention(ecs_version, ecs_nested) | ||||||||||||||||||||||||||||||||||
| component_names = component_name_convention(ecs_version, ecs_nested, ecs_component_name_prefix) | ||||||||||||||||||||||||||||||||||
| save_composable_template(ecs_version, component_names, out_dir, mapping_settings_file, template_settings_file) | ||||||||||||||||||||||||||||||||||
|  | ||||||||||||||||||||||||||||||||||
|  | ||||||||||||||||||||||||||||||||||
|  | @@ -100,12 +101,13 @@ def save_component_template( | |||||||||||||||||||||||||||||||||
|  | ||||||||||||||||||||||||||||||||||
| def component_name_convention( | ||||||||||||||||||||||||||||||||||
| ecs_version: str, | ||||||||||||||||||||||||||||||||||
| ecs_nested: Dict[str, FieldNestedEntry] | ||||||||||||||||||||||||||||||||||
| ecs_nested: Dict[str, FieldNestedEntry], | ||||||||||||||||||||||||||||||||||
| ecs_component_name_prefix: str="ecs" | ||||||||||||||||||||||||||||||||||
| ) -> List[str]: | ||||||||||||||||||||||||||||||||||
| version: str = ecs_version.replace('+', '-') | ||||||||||||||||||||||||||||||||||
| names: List[str] = [] | ||||||||||||||||||||||||||||||||||
| for (fieldset_name, fieldset) in ecs_helpers.remove_top_level_reusable_false(ecs_nested).items(): | ||||||||||||||||||||||||||||||||||
| names.append("ecs_{}_{}".format(version, fieldset_name.lower())) | ||||||||||||||||||||||||||||||||||
| names.append("{}_{}_{}".format(ecs_component_name_prefix, version, fieldset_name.lower())) | ||||||||||||||||||||||||||||||||||
| return names | ||||||||||||||||||||||||||||||||||
|  | ||||||||||||||||||||||||||||||||||
|  | ||||||||||||||||||||||||||||||||||
|  | ||||||||||||||||||||||||||||||||||
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.
Could add more documentation to
helpfor the new arguments, to explain more about how these are intended to be used, and to mention they are for custom use cases. It's fine to use multiline strings here, so the help message can be longer.