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

[FR] Add source_updated_at to Rule Schema as a Build Time Field #3427

Open
wants to merge 12 commits into
base: main
Choose a base branch
from

Conversation

terrancedejesus
Copy link
Contributor

@terrancedejesus terrancedejesus commented Feb 5, 2024

Issues

Summary

This pull request adds source_updated_at to the BaseRuleSchema, defines a minimum compatible stack for this field and dynamically generates it on rule loading. For more details, please refer to the issue referenced.

Testing

Testing Rule Version Adjustments

Based on the suggested changes, rule versions should not change even though source_updated_at is added to the final rule object. This is because we remove this key:value pair from the dictionary that is SHA256 hashed. This addresses the versioning concern of two separate versions between branches as it is not taken into consideration. We did this for a few reasons:

  • We understand it is an exception, but the date is pulled from metadata, we never expect a rule author to add this in the rule schema, and it is controlled solely by us with no influence upstream
  • We need a mechanism to bypass version breaking changes if we can and this supplies one; we also do this in endpoint rules as well so the approach is not net new to our repositories and logic
  1. Open rule.py and comment out L982 (self._convert_add_source_updated_at(obj))
  2. Run python -m detection_rules view-rule /Users/tdejesus/code/src/detection-rules/rules/windows/persistence_startup_folder_scripts.toml
  3. Note the version is 109
  4. Uncomment step 1. and run the CLI command again
  5. Note the version is still 109

This is because regardless if the field is added or note, we simply remove it if found all the time when calculating SHA256. This would backport and happen for each branch, thus version would be the same but the field would be included in the package moving forward.

For compatibility, the field is still added to the BaseRuleData dataclass to support importing the rule.

Testing Field is Dynamically Generated

Remember that this field will only generate if the stack version (reflected by packages.yml) is >=8.12. Therefore we need to test that the field is included AND not included on on older branches

CLI Command: python -m detection_rules view-rule /Users/tdejesus/code/src/detection-rules/rules/windows/persistence_startup_folder_scripts.toml

The source_updated_at field should be visible in the output.

To re-create this for a lower stack version to ensure it does not appear, copy the rule.py and definitions.py files to a separate folder, checkout an older branch, move them back and then run the CLI command again; source_updated_at should not be added to the final rule output.

This is because of the logic here. This is also why the TestBuildTimeFields unit test class was removed as it is redundant and we do this check for every build time field.

@terrancedejesus terrancedejesus added enhancement New feature or request python Internal python for the repository schema fleet-release Issue tracking rule updates released to (OOB) Fleet integration package Supportability Regarding rule maintenance and support for specific stack versions labels Feb 5, 2024
@terrancedejesus terrancedejesus self-assigned this Feb 5, 2024
@terrancedejesus terrancedejesus changed the title [FR] Add elastic_last_update to Rule Schema as a Build Time Field [FR] Add source_updated_at to Rule Schema as a Build Time Field Feb 21, 2024
@terrancedejesus
Copy link
Contributor Author

@Mikaayenson - When working on DaC and import-rules, we should consider build time fields and implications in general when re-factoring this.

@terrancedejesus terrancedejesus marked this pull request as ready for review February 21, 2024 19:33
Copy link
Contributor

@brokensound77 brokensound77 left a comment

Choose a reason for hiding this comment

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

I think this is one of those times where, this arguably solves the problem we are aiming to solve, but with risks and ambiguity that may not justify it. IOW, potentially saying no to a good idea based on present and future implications.

This sets a precedent that I think introduces risk and ambiguity. This is something we have aimed to explicitly avoid in the past. A much safer approach would be to just add it as another field to the schema, populated by rule author -- or even at version lock time, where we grab the last commit time and insert it as a build time field.

Exempting fields from the calculation breaks the requirements of a build time field (which is why the exemption is needed).

It seems like all approaches have some kind of tradeoff to them, so there is no perfect solution, I just don't know that this is the best solution.

I know we have discussed this a ton, but I am blocking based on the need for further discussion and the implications of this approach

@terrancedejesus
Copy link
Contributor Author

terrancedejesus commented Feb 21, 2024

@brokensound77 - Happy to continue discussion more. Please let me know when you have time.
We have more details in the issue discussion: #2826 (comment)

Copy link
Contributor

@Mikaayenson Mikaayenson left a comment

Choose a reason for hiding this comment

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

Based on the update, this LGTM:

  1. I personally like excluding explicitly this field from sha256 calculation. Whereas the other build time fields could introduce errors (e.g. field types changed over time, integrations populated at different stack versions), this date doesnt appear to have inconsequential consequences.
  2. We talked about potentially a hybrid of both approaches (a. maintaining the field in the schema, and b. just building/including it only for packaging during the release), but the concern was that solution was too far removed from the schemas.

@Mikaayenson Mikaayenson self-requested a review February 23, 2024 20:33
@Mikaayenson
Copy link
Contributor

Update Feb 23

After speaking with D&R, another reason to hold on this is they anticipate more changes. It’s still unclear if they want to have a top-level field source_updated_at or an object with a field source.updated_at to make it future-proof.

So much more to come after our time away.

@Mikaayenson
Copy link
Contributor

Mikaayenson commented Feb 28, 2024

Update 28

Speaking with @terrancedejesus and @brokensound77 at EAH, here's the gist of how we can handle this:

  • Treat the new field as a breaking change (don't push the schema updates until version+3 release is here)
  • We also need to consider that global breaking changes introduce 1100+ rules into the rules package because of historical rules.

@banderror Alternatively is it possible on your end to either:

  1. Could we use the updated_at that exists already?
  2. Just pull the field from our metadata section (we would our updated date to the metadata)

@Mikaayenson
Copy link
Contributor

Update Mar 14 - Simplified Protections Sync

  • We're going to hold until 8.15 to release this.
  • We don't need to update schemas are worry about import/export. The field will not be exported. It only needs to be dynamically generated and pushed on package release.

@Mikaayenson
Copy link
Contributor

Update Aug 1

After speaking with @approksiu, we need to hold off until end of year until the rules management team is able to discuss further.

@shashank-elastic
Copy link
Contributor

Removing this from Q2 docket

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backlog backport: auto enhancement New feature or request fleet-release Issue tracking rule updates released to (OOB) Fleet integration package python Internal python for the repository schema Supportability Regarding rule maintenance and support for specific stack versions v8.15.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[FR] Add source_updated_at to Rule Schema as a Build Time Field
4 participants