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

[Security Solution] Handle specific fields in /upgrade/_review endpoint and refactor diff logic to use Zod #186615

Merged
merged 32 commits into from
Jul 11, 2024

Conversation

jpdjere
Copy link
Contributor

@jpdjere jpdjere commented Jun 21, 2024

Fixes: #180393

Summary

Handles specific fields in /upgrade/_review endpoint upgrade workflow, as described in #180393.

Achieves this with two mechanisms:

  1. Removing fields from the PrebuiltRuleAsset schema, which excludes the field from the diff calculation completely.
  2. Manually removing the diff calculation for certain fields, by excluding them from /common/api/detection_engine/prebuilt_rules/model/diff/diffable_rule/diffable_rule.ts

Also, refactors a part of the codebase from its prior usage of io-ts schema types to use autogenerated Zod types.

With this refactor, most of the x-pack/plugins/security_solution/common/api/detection_engine/model/rule_schema_legacy could be deleted. Unluckily some of the types manually created there are still used in some complex types elsewhere, so I added a note to that file indicating that those should be migrated to Zod, so that the legacy folder can finally be deleted.

Checklist

Delete any items that are not applicable to this PR.

For maintainers

@jpdjere jpdjere changed the title Omit props from prebuilt rule asset schema [Security Solution] Handle specific fields in /upgrade/_review endpoint and refactor diff logic to use Zod Jun 27, 2024
@jpdjere jpdjere marked this pull request as ready for review June 27, 2024 10:48
@jpdjere jpdjere requested review from a team as code owners June 27, 2024 10:48
@jpdjere jpdjere marked this pull request as draft June 27, 2024 12:25
@jpdjere
Copy link
Contributor Author

jpdjere commented Jun 28, 2024

/ci

@jpdjere jpdjere marked this pull request as ready for review June 28, 2024 12:36
@jpdjere
Copy link
Contributor Author

jpdjere commented Jun 28, 2024

/ci

@jpdjere jpdjere marked this pull request as draft June 28, 2024 13:14
@jpdjere jpdjere marked this pull request as ready for review June 28, 2024 15:02
@jpdjere jpdjere self-assigned this Jun 28, 2024
@jpdjere jpdjere added refactoring backport:skip This commit does not require backporting Team:Detections and Resp Security Detection Response Team Team: SecuritySolution Security Solutions Team working on SIEM, Endpoint, Timeline, Resolver, etc. Team:Detection Rule Management Security Detection Rule Management Team v8.15.0 labels Jun 28, 2024
@elasticmachine
Copy link
Contributor

Pinging @elastic/security-detections-response (Team:Detections and Resp)

@elasticmachine
Copy link
Contributor

Pinging @elastic/security-solution (Team: SecuritySolution)

@elasticmachine
Copy link
Contributor

Pinging @elastic/security-detection-rule-management (Team:Detection Rule Management)

@jpdjere jpdjere added release_note:skip Skip the PR/issue when compiling release notes Feature:Prebuilt Detection Rules Security Solution Prebuilt Detection Rules area labels Jun 28, 2024
@banderror banderror self-requested a review June 28, 2024 18:19
@banderror banderror removed backport:skip This commit does not require backporting labels Jun 28, 2024
Copy link
Contributor

@banderror banderror left a comment

Choose a reason for hiding this comment

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

Thank you for addressing the comments @jpdjere, the PR looks great! Approving it ✅

Let's chat about any follow-ups separately.

Comment on lines +40 to +61
// `response_actions` is only part of the optional fields in QueryRuleCreateFields and SavedQueryRuleCreateFields
const TYPE_SPECIFIC_PROPS_REMOVED_FROM_PREBUILT_RULE_ASSET = zodMaskFor<
QueryRuleCreateFields | SavedQueryRuleCreateFields
>()(['response_actions']);

const QueryRuleAssetFields = QueryRuleCreateFields.omit(
TYPE_SPECIFIC_PROPS_REMOVED_FROM_PREBUILT_RULE_ASSET
);
const SavedQueryRuleAssetFields = SavedQueryRuleCreateFields.omit(
TYPE_SPECIFIC_PROPS_REMOVED_FROM_PREBUILT_RULE_ASSET
);

export const RuleAssetTypeSpecificCreateProps = z.discriminatedUnion('type', [
EqlRuleCreateFields,
QueryRuleAssetFields,
SavedQueryRuleAssetFields,
ThresholdRuleCreateFields,
ThreatMatchRuleCreateFields,
MachineLearningRuleCreateFields,
NewTermsRuleCreateFields,
EsqlRuleCreateFields,
]);
Copy link
Contributor

Choose a reason for hiding this comment

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

The added complexity worries me a bit, + this is yet another place where we should add a new rule type once it is added to RuleResponse. Let's chat over zoom.

@jpdjere
Copy link
Contributor Author

jpdjere commented Jul 11, 2024

Four pending follow-ups from this PR, as discussed with @banderror during sync review:

  • Create a ticket for creating a specialised diff algorithm for items_per_search and concurrent_searches algorithm, that calculates the diff and returns it in the endpoint, but has always a response of conflict: NO for all scenarios.
  • Remove Changes in the UI from this PR's related ticket and create a new ticket with it.
  • Attempt to wrangle the Zod types to add safety guards to the x-pack/plugins/security_solution/server/lib/detection_engine/prebuilt_rules/model/rule_assets/prebuilt_rule_asset.ts for the case a new rule type is added. This file shouldn't need to be modified if that happens; or in the worst case, we should have a test that fails if that happens. PR
  • Remove rule_schema_legacy leftover types by inline their remaining usage. See comment. OPEN PR

@jpdjere jpdjere merged commit 7950fb8 into elastic:main Jul 11, 2024
44 checks passed
@kibanamachine kibanamachine added the backport:skip This commit does not require backporting label Jul 11, 2024
jpdjere added a commit that referenced this pull request Jul 12, 2024
…h Zod transform (#188092)

## Summary

Pending work from: #186615

- The previous implementation to create `PrebuiltRuleAsset` with some
RuleResponse fields ommited from it had the disadvantage of being built
with a discriminated union where all rule types had to be re-listed. If
a new type was added, then it would have required manually adding the
type to that union as well, which would have been surely forgotten.
- This replaces that schema construction to use a Zod transform which
simply eliminates the omitted fields using a Zod transform.


### For maintainers

- [ ] This was checked for breaking API changes and was [labeled
appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)
jpdjere added a commit that referenced this pull request Jul 22, 2024
#188079)

## Summary

Leftover work from #186615

- Removes remaining usage of `rule_schema_legacy` types. In this PR,
simply inlines the last io-ts types used, to be able to get rid of the
legacy folder.
- The remaining files that need to be migrated to using Zod schema types
are:
-
`x-pack/plugins/security_solution/common/api/detection_engine/rule_exceptions/find_exception_references/find_exception_references_route.ts`
- `x-pack/plugins/security_solution/common/api/timeline/model/api.ts`

### For maintainers

- [ ] This was checked for breaking API changes and was [labeled
appropriately](https://www.elastic.co/guide/en/kibana/master/contributing.html#kibana-release-notes-process)

---------

Co-authored-by: Georgii Gorbachev <georgii.gorbachev@elastic.co>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport:skip This commit does not require backporting Feature:Prebuilt Detection Rules Security Solution Prebuilt Detection Rules area refactoring release_note:skip Skip the PR/issue when compiling release notes Team:Detection Rule Management Security Detection Rule Management Team Team:Detections and Resp Security Detection Response Team Team: SecuritySolution Security Solutions Team working on SIEM, Endpoint, Timeline, Resolver, etc. v8.16.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Security Solution] Handle specific fields in /upgrade/_review upgrade workflow
6 participants