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] Migrate Rules schema to OpenAPI #167999

Merged
merged 1 commit into from
Oct 27, 2023

Conversation

xcrzx
Copy link
Contributor

@xcrzx xcrzx commented Oct 4, 2023

Parent meta ticket: https://github.com/elastic/security-team/issues/7491

Resolves: https://github.com/elastic/security-team/issues/7582
Resolves: https://github.com/elastic/security-team/issues/7580
Resolves: https://github.com/elastic/security-team/issues/7581

Summary

This PR migrates the rules schema to OpenAPI, Zod, and code generation.

The following APIs now have complete OpenAPI specifications and are enabled for code generation:

Method Endpoint OpenAPI spec Fully migrated
POST /api/detection_engine/rules
GET /api/detection_engine/rules
PUT /api/detection_engine/rules
PATCH /api/detection_engine/rules
DELETE /api/detection_engine/rules
POST /api/detection_engine/rules/_bulk_create
PUT /api/detection_engine/rules/_bulk_update
PATCH /api/detection_engine/rules/_bulk_update
DELETE /api/detection_engine/rules/_bulk_delete
POST /api/detection_engine/rules/_bulk_delete

Rule schemas are now forward-compatible

We now allow extra fields in schemas for forward compatibility, but we remove them from the payload during parsing. So from now on, extra fields are simply ignored and won't lead to validation errors.

@xcrzx xcrzx self-assigned this Oct 4, 2023
@xcrzx xcrzx force-pushed the rules-schema-openapi branch 16 times, most recently from 52d456e to 2ba0fc7 Compare October 10, 2023 06:53
@xcrzx xcrzx added 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 labels Oct 13, 2023
@xcrzx xcrzx force-pushed the rules-schema-openapi branch 7 times, most recently from 64cf59b to 5f80ffe Compare October 18, 2023 15:26
@xcrzx xcrzx added v8.12.0 release_note:skip Skip the PR/issue when compiling release notes labels Oct 19, 2023
@xcrzx xcrzx force-pushed the rules-schema-openapi branch from 5f80ffe to 1083511 Compare October 19, 2023 10:41
Copy link
Contributor

@vitaliidm vitaliidm left a comment

Choose a reason for hiding this comment

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

Comment not actual anymore, refer to #167999 (review)

@vitaliidm vitaliidm self-requested a review October 24, 2023 16:21
Copy link
Contributor

@michaelolo24 michaelolo24 left a comment

Choose a reason for hiding this comment

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

Investigations codeowners review. Thanks for updating the types!

@vitaliidm vitaliidm self-requested a review October 24, 2023 16:48
Copy link
Contributor

@rylnd rylnd left a comment

Choose a reason for hiding this comment

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

Risk engine schema changes (updating an import, adding an eslint-disable look good to me.

import { DataViewId } from '../../api/detection_engine';
// TODO https://github.com/elastic/security-team/issues/7491
// eslint-disable-next-line no-restricted-imports
import { DataViewId } from '../../api/detection_engine/model/rule_schema_legacy';
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you happen to know why this is a restricted import? They're both common files in the same plugin.

Copy link
Contributor Author

@xcrzx xcrzx Oct 25, 2023

Choose a reason for hiding this comment

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

The path is restricted because it includes _legacy in it. We have plans to remove those legacy schemas, and in the meantime, we don't want people to use them accidentally. So adding legacy to the path helps with that.

@xcrzx xcrzx force-pushed the rules-schema-openapi branch 2 times, most recently from 062b2c8 to 5aa692f Compare October 25, 2023 11:36
@xcrzx xcrzx requested review from vitaliidm and banderror October 25, 2023 12:26
@@ -6,13 +6,19 @@ paths: {}
components:
x-codegen-enabled: true
schemas:
NonEmptyString:
type: string
pattern: ^(?! *$).+$
Copy link
Contributor

@maximpn maximpn Oct 25, 2023

Choose a reason for hiding this comment

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

Just curious why using this relatively complex regex while it could be ^[^\s]+$ (require one ore more non space characters)? While it's stricter than NonEmptyString defined previously in via io-ts but we still don't expect any kind of space characters as a rule name.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In my honest opinion, we need neither of those regexes 🙂 I'd define a non-empty string as a string with length > 0, regardless of its content.

@xcrzx xcrzx force-pushed the rules-schema-openapi branch from 5aa692f to 62e5aac Compare October 26, 2023 12:18
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.

Fantastic and absolutely humongous work @xcrzx! Thank you for addressing so many comments and moving our architecture in the right direction. This is so, so much better than our legacy schemas written with io-ts. I'm excited about all the other improvements that we can and will do based on this work!

@@ -74,6 +73,7 @@ import {
import { RuleSwitch } from '../../../../detections/components/rules/rule_switch';
import { StepPanel } from '../../../../detections/components/rules/step_panel';
import {
getMachineLearningJobId,
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: the function should be called getMachineLearningJobIds because it returns an array. We could rename it later

@@ -32,6 +32,7 @@ const mockRule: Rule = {
severity: 'low',
type: 'query',
query: 'some query',
language: 'kuery',
Copy link
Contributor

Choose a reason for hiding this comment

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

Thank you for getting rid of the frontend-side Rule "schema"! I guess this diff shows that this highlighted some bugs in our code where we forgot to specify all the fields required by RuleResponse.

Comment on lines -134 to -137
export const RuleSchema = t.intersection([
t.type({
author: RuleAuthorArray,
created_at: t.string,
Copy link
Contributor

Choose a reason for hiding this comment

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

No more frontend-side (duplicated and inaccurate) rule schema anymore! 🙌
cc @elastic/security-detection-rule-management @elastic/security-detection-engine

Comment on lines +33 to +42
export type PrebuiltRuleAsset = z.infer<typeof PrebuiltRuleAsset>;
export const PrebuiltRuleAsset = BaseCreateProps.and(TypeSpecificCreateProps).and(
z.object({
rule_id: RuleSignatureId,
version: RuleVersion,
related_integrations: RelatedIntegrationArray.optional(),
required_fields: RequiredFieldArray.optional(),
setup: SetupGuide.optional(),
})
);
Copy link
Contributor

Choose a reason for hiding this comment

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

Awesome!! 🙌

@xcrzx xcrzx force-pushed the rules-schema-openapi branch from 62e5aac to dbcce09 Compare October 26, 2023 14:58
@vitaliidm vitaliidm dismissed their stale review October 26, 2023 17:10

Rule schema validation should be addressed in subsequent PRs: #167999 (comment)

@xcrzx xcrzx force-pushed the rules-schema-openapi branch 3 times, most recently from 2eb70ec to 92851af Compare October 27, 2023 13:13
@xcrzx xcrzx force-pushed the rules-schema-openapi branch from 92851af to 5c2609e Compare October 27, 2023 14:29
@kibana-ci
Copy link
Collaborator

💛 Build succeeded, but was flaky

Failed CI Steps

Test Failures

  • [job] [logs] Security Solution Cypress Tests #10 / Changing alert status Opening alerts can bulk open alerts can bulk open alerts

Metrics [docs]

Module Count

Fewer modules leads to a faster build time

id before after diff
securitySolution 4640 4651 +11

Public APIs missing comments

Total count of every public API that lacks a comment. Target amount is 0. Run node scripts/build_api_docs --plugin [yourplugin] --stats comments for more detailed information.

id before after diff
@kbn/securitysolution-es-utils 78 76 -2
@kbn/zod-helpers - 9 +9
total +7

Async chunks

Total size of all lazy-loaded chunks that will be downloaded as the user navigates the app

id before after diff
securitySolution 12.8MB 12.8MB +13.8KB

Page load bundle

Size of the bundles that are downloaded on every page load. Target size is below 100kb

id before after diff
securitySolution 64.7KB 64.8KB +94.0B
Unknown metric groups

API count

id before after diff
@kbn/securitysolution-es-utils 89 87 -2
@kbn/zod-helpers - 12 +12
total +10

ESLint disabled line counts

id before after diff
securitySolution 453 472 +19

Total ESLint disabled count

id before after diff
securitySolution 521 540 +19

History

  • 💔 Build #171554 failed 92851af1d9569ebc8e3d3b2d06a88a56662b29bf
  • 💔 Build #171519 failed 2eb70ecb46fb8a5f664cea3f806831725c6a2488
  • 💔 Build #171500 failed 1b9c918e4c5890e2015bd41535d52fbd65df762a
  • 💔 Build #171249 failed dbcce09c105ccb744cec7bb9b14c5153d6b552a0
  • 💚 Build #171164 succeeded 62e5aac237068f21eb496d1095df672b2898fa2e
  • 💛 Build #170766 was flaky 5aa692f7953a4dfb1b6b44c2252937312fc307a8

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

cc @xcrzx

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 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.12.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.