-
Notifications
You must be signed in to change notification settings - Fork 8.6k
[Security Solution] Adds ability to revert prebuilt rules to their base version #223301
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
Changes from all commits
a71d337
78348c1
3229873
44ff894
e1cbace
a1ae0e5
69e50af
6d0a3de
891e81e
e0563c8
2421863
a326beb
0e6c999
1a0b0fe
39c9683
0c4c2e2
b39590d
c370d9c
73ad4a4
3bcdfbf
1acdf79
78db1b9
2ebde50
917c65c
0174558
f2beeea
20f5c26
32bb30d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| import { z } from '@kbn/zod'; | ||
| import type { RuleResponse } from '../../model/rule_schema/rule_schemas.gen'; | ||
| import type { PartialRuleDiff } from '../model'; | ||
|
|
||
| export type GetPrebuiltRuleBaseVersionRequest = z.infer<typeof GetPrebuiltRuleBaseVersionRequest>; | ||
| export const GetPrebuiltRuleBaseVersionRequest = z.object({ | ||
| id: z.string(), | ||
| }); | ||
|
|
||
| export interface GetPrebuiltRuleBaseVersionResponseBody { | ||
| /** The base version of the rule */ | ||
| base_version: RuleResponse; | ||
|
|
||
| /** The current version of the rule */ | ||
| current_version: RuleResponse; | ||
|
|
||
| /** The resulting diff between the base and current versions of the rule */ | ||
| diff: PartialRuleDiff; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| import { z } from '@kbn/zod'; | ||
| import { RuleResponse } from '../../model/rule_schema/rule_schemas.gen'; | ||
| import { NormalizedRuleError } from '../../rule_management'; | ||
|
|
||
| export type RevertPrebuiltRulesRequest = z.infer<typeof RevertPrebuiltRulesRequest>; | ||
| export const RevertPrebuiltRulesRequest = z.object({ | ||
| /** ID of rule to revert */ | ||
| id: z.string(), | ||
|
|
||
| /** Revision of rule to guard against concurrence */ | ||
| revision: z.number(), | ||
|
|
||
| /** Version of rule to guard against concurrence */ | ||
| version: z.number(), | ||
| }); | ||
|
|
||
| export type BulkRevertSkipReason = z.infer<typeof BulkRevertSkipReason>; | ||
| export const BulkRevertSkipReason = z.enum(['RULE_NOT_PREBUILT', 'RULE_NOT_CUSTOMIZED']); | ||
| export type BulkRevertSkipReasonEnum = typeof BulkRevertSkipReason.enum; | ||
| export const BulkRevertSkipReasonEnum = BulkRevertSkipReason.enum; | ||
|
|
||
| export type BulkActionReversionSkipResult = z.infer<typeof BulkActionReversionSkipResult>; | ||
| export const BulkActionReversionSkipResult = z.object({ | ||
| id: z.string(), | ||
| skip_reason: BulkRevertSkipReason, | ||
| }); | ||
|
|
||
| export type RevertPrebuiltRulesResponseBody = z.infer<typeof RevertPrebuiltRulesResponseBody>; | ||
| export const RevertPrebuiltRulesResponseBody = z.object({ | ||
| success: z.boolean().optional(), | ||
| status_code: z.number().int().optional(), | ||
| message: z.string().optional(), | ||
| rules_count: z.number().int().optional(), | ||
| attributes: z.object({ | ||
| results: z.object({ | ||
| updated: z.array(RuleResponse), // An array of the rule objects reverted to their base version | ||
| created: z.array(RuleResponse), | ||
| deleted: z.array(RuleResponse), | ||
| skipped: z.array(BulkActionReversionSkipResult), // An array of the rule ids and reasons that were skipped during reversion (due to being already non-customized) | ||
| }), | ||
| summary: z.object({ | ||
| failed: z.number().int(), | ||
| skipped: z.number().int(), | ||
| succeeded: z.number().int(), | ||
| total: z.number().int(), | ||
| }), | ||
| errors: z.array(NormalizedRuleError).optional(), // An array of error objects, something containing the id of the rule causing the error and the reason behind it (e.g. no base version, rule is not a prebuilt Elastic rule) | ||
| }), | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| import { normalizeRuleThreshold } from './normalize_rule_threshold'; | ||
|
|
||
| describe('normalizeRuleThreshold', () => { | ||
| it('returns threshold without cardinality field when array is empty', () => { | ||
| const normalizedField = normalizeRuleThreshold({ | ||
| value: 30, | ||
| field: ['field'], | ||
| cardinality: [], | ||
| }); | ||
|
|
||
| expect(normalizedField).toEqual({ value: 30, field: ['field'] }); | ||
| }); | ||
|
|
||
| it('returns cardinality field when it is populated', () => { | ||
| const normalizedField = normalizeRuleThreshold({ | ||
| value: 30, | ||
| field: ['field'], | ||
| cardinality: [{ value: 20, field: 'field-cardinality' }], | ||
| }); | ||
|
|
||
| expect(normalizedField).toEqual({ | ||
| value: 30, | ||
| field: ['field'], | ||
| cardinality: [{ value: 20, field: 'field-cardinality' }], | ||
| }); | ||
| }); | ||
| }); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| import type { Threshold } from '../../../api/detection_engine/model/rule_schema'; | ||
|
|
||
| export const normalizeRuleThreshold = (threshold: Threshold): Threshold => { | ||
| const cardinality = | ||
| threshold.cardinality && threshold.cardinality.length ? threshold.cardinality : undefined; | ||
| return { | ||
| value: threshold.value, | ||
| field: threshold.field, | ||
| cardinality, | ||
| }; | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -155,6 +155,7 @@ import { useLegacyUrlRedirect } from './use_redirect_legacy_url'; | |
| import { RuleDetailTabs, useRuleDetailsTabs } from './use_rule_details_tabs'; | ||
| import { useIsExperimentalFeatureEnabled } from '../../../../common/hooks/use_experimental_features'; | ||
| import { useRuleUpdateCallout } from '../../../rule_management/hooks/use_rule_update_callout'; | ||
| import { usePrebuiltRulesViewBaseDiff } from '../../../rule_management/hooks/use_prebuilt_rules_view_base_diff'; | ||
|
|
||
| const RULE_EXCEPTION_LIST_TYPES = [ | ||
| ExceptionListTypeEnum.DETECTION, | ||
|
|
@@ -432,6 +433,18 @@ const RuleDetailsPageComponent: React.FC<DetectionEngineComponentProps> = ({ | |
| onUpgrade: refreshRule, | ||
| }); | ||
|
|
||
| const { | ||
| baseVersionFlyout, | ||
| openFlyout, | ||
| doesBaseVersionExist, | ||
| isLoading: isBaseVersionLoading, | ||
| } = usePrebuiltRulesViewBaseDiff({ rule, onRevert: refreshRule }); | ||
|
|
||
| const isRevertBaseVersionDisabled = useMemo( | ||
| () => !doesBaseVersionExist || isBaseVersionLoading, | ||
| [doesBaseVersionExist, isBaseVersionLoading] | ||
| ); | ||
|
|
||
| const ruleStatusInfo = useMemo(() => { | ||
| return ( | ||
| <> | ||
|
|
@@ -608,6 +621,7 @@ const RuleDetailsPageComponent: React.FC<DetectionEngineComponentProps> = ({ | |
| <NeedAdminForUpdateRulesCallOut /> | ||
| <MissingPrivilegesCallOut /> | ||
| {upgradeCallout} | ||
| {baseVersionFlyout} | ||
| {isBulkDuplicateConfirmationVisible && ( | ||
| <BulkActionDuplicateExceptionsConfirmation | ||
| onCancel={cancelRuleDuplication} | ||
|
|
@@ -722,6 +736,8 @@ const RuleDetailsPageComponent: React.FC<DetectionEngineComponentProps> = ({ | |
| showBulkDuplicateExceptionsConfirmation={showBulkDuplicateConfirmation} | ||
| showManualRuleRunConfirmation={showManualRuleRunConfirmation} | ||
| confirmDeletion={confirmDeletion} | ||
| isRevertBaseVersionDisabled={isRevertBaseVersionDisabled} | ||
| openRuleDiffFlyout={openFlyout} | ||
|
Comment on lines
+739
to
+740
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like inversion of control is required for Anyway the refactoring might be quite bulky and should happen outside of this PR. |
||
| /> | ||
| </EuiFlexItem> | ||
| </EuiFlexGroup> | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As an improvement you could return
fieldsDiff: Partial<RuleFieldsDiff>and add the necessary fallback fields for example inx-pack/solutions/security/plugins/security_solution/public/detection_engine/rule_management/components/rule_details/base_version_diff/base_version_flyout.tsxor in the upstream data fetching hook.