Skip to content
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

Add optional pattern attribute #1834

Merged
merged 10 commits into from
Mar 10, 2022

Conversation

ebeahan
Copy link
Member

@ebeahan ebeahan commented Mar 8, 2022

Overview

Introduces a new optional field definition attribute: pattern. The pattern attribute holds a regular expression (regex) which expresses the expected constraint on a string field's value.

pattern: '[.*]'

The ECS tooling checks the regex itself to ensure validity. However, The ECS tooling cannot evaluate the regex against provided field values.

Motivation

Allow automated testing to use the regex found in the pattern fields against values populating ECS fields.

Implementation details

Regex validation

If specified, the regex value from pattern goes through a check using re.compile if the --strict flag is present. The generator will only generate a warning if --strict isn't specified.

Generated artifacts

The pattern field appears in the following generated artifacts:

  • generated/beats/fields.ecs.yml
  • generated/ecs/ecs_flat.yml
  • generated/ecs/ecs_nested.yml

Misc refactoring

I took the opportunity to dedup how --strict checks are handled in schema.cleaner.

Example usage

    - name: mac
      level: core
      type: keyword
      short: MAC address of the source.
      example: 00-00-5E-00-53-23
      pattern: '^[A-F0-9]{2}(-[A-F0-9]{2}){5,}$'
      description: >
        MAC address of the source.

        The notation format from RFC 7042 is suggested: Each octet (that is, 8-bit byte) is
        represented by two [uppercase] hexadecimal digits giving the value of
        the octet as an unsigned integer. Successive octets are separated by a
        hyphen.

Resolves #1741

@ebeahan ebeahan requested a review from a team March 8, 2022 20:15
@ebeahan
Copy link
Member Author

ebeahan commented Mar 8, 2022

@andrewkroh Does the proposed implementation match up with your expectations?

I added regex validation ECS-side, but I know the support of expressions can vary between different languages/engines/implementations. I'm open to removing the validation step if it's best to remove any sort of regexp validation ECS-side.

Copy link
Member

@andrewkroh andrewkroh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Thanks for adding this to the schema.

@ebeahan ebeahan merged commit 9887aef into elastic:main Mar 10, 2022
@ebeahan ebeahan deleted the string-value-validation-patterns branch March 10, 2022 14:55
ebeahan added a commit to ebeahan/ecs that referenced this pull request Mar 10, 2022
* add validator for new pattern attribute; also consolidate dup'd logic

* remove unused import

* add unit tests for validate_pattern_regex

* document pattern field attr

* add pattern to allow list

* add testing for allowed vs. unallowed field values

* mark pattern as optional

* adding changelog

* check example value if pattern is present

(cherry picked from commit 9887aef)
@ebeahan
Copy link
Member Author

ebeahan commented Mar 10, 2022

💚 All backports created successfully

Status Branch Result
8.2

Questions ?

Please refer to the Backport tool documentation

ebeahan added a commit that referenced this pull request Mar 10, 2022
* add validator for new pattern attribute; also consolidate dup'd logic

* remove unused import

* add unit tests for validate_pattern_regex

* document pattern field attr

* add pattern to allow list

* add testing for allowed vs. unallowed field values

* mark pattern as optional

* adding changelog

* check example value if pattern is present

(cherry picked from commit 9887aef)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow regex pattern to be specified in schema to improve data consistency
2 participants