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] Refactor duplicated rule schemas #135479

Merged
merged 10 commits into from
Jul 7, 2022

Conversation

marshallmain
Copy link
Contributor

@marshallmain marshallmain commented Jun 29, 2022

Removes duplicated HTTP detection rules API schemas and builds them instead from a shared schema in rule_schemas.ts. This removes ~25 places where new fields needed to be added manually as parameters to various functions and destructuring operations. The replacement function convertPatchAPIToInternalSchema centralizes the conversion process between HTTP and backend param formats and also results in a type error if a new field is added to the internal schema but not handled in the conversion function.

The result should be much less effort to pass rule params through various functions since they're passed as a single object instead of a set of 40-50 individual fields. It should also be clearer when special logic is being applied to specific rule params since they'll stand out more (e.g. exceptions_list has special processing in importRules()).

The shared schema does not set default values as part of the io-ts schema. This is in part because io-ts does not have good support for separating the expected input type of the schema (before applying defaults) from the output type (after applying defaults). Default values come from the convertCreateAPIToInternalSchema function in the new schema system, and are enforced by Typescript because convertCreateAPIToInternalSchema returns the internal server rule type. This change affects the AddPrepackaged and Import schemas most since they applied default values in the schema previously.

@marshallmain marshallmain added release_note:skip Skip the PR/issue when compiling release notes 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 Team:Detection Alerts Security Detection Alerts Area Team v8.4.0 labels Jun 30, 2022
@marshallmain marshallmain marked this pull request as ready for review June 30, 2022 15:41
@marshallmain marshallmain requested review from a team as code owners June 30, 2022 15:41
@elasticmachine
Copy link
Contributor

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

@elasticmachine
Copy link
Contributor

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

Copy link
Contributor

@xcrzx xcrzx left a comment

Choose a reason for hiding this comment

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

Thanks, @marshallmain, fantastic work 👍 Checked and tested locally, and everything works as expected. The schema and API routes code now look so much nicer 🚀

@@ -130,7 +111,7 @@ describe('patch_rules_type_dependents', () => {
],
},
};
const errors = patchRuleValidateTypeDependents(schema);
const errors = patchRuleValidateTypeDependents(schema as PatchRulesSchema);
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it possible to adjust the return type of getPatchRulesSchemaMock to avoid schema as PatchRulesSchema type castings?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, I added an individual type for each rule type patch schema (ThresholdPatchSchema here) and a mock for ThresholdPatchSchema so we can remove this type of cast

try {
// TODO: Fix these either with an is conversion or by better typing them within io-ts
Copy link
Contributor

Choose a reason for hiding this comment

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

Awesome, this function became much more readable 🚀

@marshallmain marshallmain requested a review from a team July 5, 2022 16:43
@kibana-ci
Copy link
Collaborator

💚 Build Succeeded

Metrics [docs]

✅ unchanged

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

},
group: 'default',
const rulesClient = rulesClientMock.create();
const params = getCreateRulesSchemaMock();
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit, but I think { actions, ...params } is preferable to delete here because it doesn't mutate the returned object.

if (errors != null || validated === null) {
throw new PatchError(`Applying patch would create invalid rule: ${errors}`, 400);
}
const patchedRule = convertPatchAPIToInternalSchema(params, rule);
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

Copy link
Contributor

@madirey madirey left a comment

Choose a reason for hiding this comment

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

There's a lot here, but I tried to be thorough. Looks like a great improvement!

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 release_note:skip Skip the PR/issue when compiling release notes Team:Detection Alerts Security Detection Alerts Area Team 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.4.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants