forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Security Solution] Allow users to save rule query with non critical …
…validation errors (elastic#202544) **Addresses:** elastic#171520 ## Summary This PR adds functionality to allow users save EQL and ES|QL queries in Prebuilt Rule Customization workflow by displaying a confirmation modal with non critical validation errors (a.k.a warnings). It also refactors confirmation modal usage in rule creation/editing forms for better reusability. ## Screenshots <img width="1909" alt="Screenshot 2024-12-03 at 14 01 36" src="https://github.com/user-attachments/assets/c0054fc1-b52b-400f-80d7-af42391f4e18"> https://github.com/user-attachments/assets/2a20fcfe-ffc0-4547-8621-7ac6873c8dc9 https://github.com/user-attachments/assets/50b5cf5a-ea3f-4c22-a443-b5d4056a92c8 <img width="2541" alt="Screenshot 2024-12-03 at 14 06 29" src="https://github.com/user-attachments/assets/dde3fd60-6c69-4f8e-a65a-837b2319e4ac"> <img width="2552" alt="Screenshot 2024-12-03 at 14 06 51" src="https://github.com/user-attachments/assets/220817a6-991f-4361-88d2-ee3a47a36ad6"> <img width="2555" alt="Screenshot 2024-12-03 at 14 07 52" src="https://github.com/user-attachments/assets/c46fc49c-9ce1-4472-bdea-f9507aa62ece"> <img width="2553" alt="Screenshot 2024-12-03 at 14 08 18" src="https://github.com/user-attachments/assets/92388c56-8644-4c54-8727-b9a73b3497d1">
- Loading branch information
1 parent
07a5130
commit 02187ca
Showing
33 changed files
with
827 additions
and
708 deletions.
There are no files selected for viewing
Validating CODEOWNERS rules …
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
...ugins/security_solution/public/common/hooks/use_confirm_validation_errors_modal/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
export * from './use_confirm_validation_errors_modal'; |
36 changes: 36 additions & 0 deletions
36
...ecurity_solution/public/common/hooks/use_confirm_validation_errors_modal/translations.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* | ||
* 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 { i18n } from '@kbn/i18n'; | ||
|
||
export const SAVE_WITH_ERRORS_MODAL_TITLE = i18n.translate( | ||
'xpack.securitySolution.detectionEngine.rules.upgradeRules.saveWithErrorsConfirmationModal.title', | ||
{ | ||
defaultMessage: 'There are validation errors', | ||
} | ||
); | ||
|
||
export const CANCEL = i18n.translate( | ||
'xpack.securitySolution.detectionEngine.rules.upgradeRules.saveWithErrorsConfirmationModal.cancel', | ||
{ | ||
defaultMessage: 'Cancel', | ||
} | ||
); | ||
|
||
export const CONFIRM = i18n.translate( | ||
'xpack.securitySolution.detectionEngine.rules.upgradeRules.saveWithErrorsConfirmationModal.confirm', | ||
{ | ||
defaultMessage: 'Confirm', | ||
} | ||
); | ||
|
||
export const SAVE_WITH_ERRORS_MESSAGE = (errorsCount: number) => | ||
i18n.translate('xpack.securitySolution.detectionEngine.createRule.saveWithErrorsModalMessage', { | ||
defaultMessage: | ||
'There {errorsCount, plural, one {is} other {are}} {errorsCount} validation {errorsCount, plural, one {error} other {errors}} which can lead to failed rule executions, save anyway?', | ||
values: { errorsCount }, | ||
}); |
56 changes: 56 additions & 0 deletions
56
.../common/hooks/use_confirm_validation_errors_modal/use_confirm_validation_errors_modal.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 type { ReactNode } from 'react'; | ||
import React, { useCallback, useState, useMemo } from 'react'; | ||
import { useBoolean } from '@kbn/react-hooks'; | ||
import { useAsyncConfirmation } from '../../../detection_engine/rule_management_ui/components/rules_table/rules_table/use_async_confirmation'; | ||
import { ConfirmValidationErrorsModal } from './confirm_validation_errors_modal'; | ||
|
||
interface UseFieldConfirmValidationErrorsModalResult { | ||
modal: ReactNode; | ||
confirmValidationErrors: (errorMessages: string[]) => Promise<boolean>; | ||
} | ||
|
||
export function useConfirmValidationErrorsModal(): UseFieldConfirmValidationErrorsModalResult { | ||
const [visible, { on: showModal, off: hideModal }] = useBoolean(false); | ||
const [initModal, confirm, cancel] = useAsyncConfirmation({ | ||
onInit: showModal, | ||
onFinish: hideModal, | ||
}); | ||
const [errorsToConfirm, setErrorsToConfirm] = useState<string[]>([]); | ||
|
||
const confirmValidationErrors = useCallback( | ||
(errorMessages: string[]) => { | ||
if (errorMessages.length === 0) { | ||
return Promise.resolve(true); | ||
} | ||
|
||
setErrorsToConfirm(errorMessages); | ||
|
||
return initModal(); | ||
}, | ||
[initModal, setErrorsToConfirm] | ||
); | ||
|
||
const modal = useMemo( | ||
() => | ||
visible ? ( | ||
<ConfirmValidationErrorsModal | ||
errors={errorsToConfirm} | ||
onConfirm={confirm} | ||
onCancel={cancel} | ||
/> | ||
) : null, | ||
[visible, errorsToConfirm, confirm, cancel] | ||
); | ||
|
||
return { | ||
modal, | ||
confirmValidationErrors, | ||
}; | ||
} |
35 changes: 35 additions & 0 deletions
35
...ecurity_solution/public/common/hooks/use_form_with_warnings/extract_validation_results.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* 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 { FieldHook, ValidationError } from '../../../shared_imports'; | ||
import type { ValidationResults } from './validation_results'; | ||
|
||
export function extractValidationResults( | ||
formFields: Readonly<FieldHook[]>, | ||
warningValidationCodes: Readonly<string[]> | ||
): ValidationResults { | ||
const warningValidationCodesSet = new Set(warningValidationCodes); | ||
const errors: ValidationError[] = []; | ||
const warnings: ValidationError[] = []; | ||
|
||
for (const field of formFields) { | ||
for (const error of field.errors) { | ||
const path = error.path ?? field.path; | ||
|
||
if (!error.code || !warningValidationCodesSet.has(error.code)) { | ||
errors.push({ ...error, path }); | ||
} else { | ||
warnings.push({ ...error, path }); | ||
} | ||
} | ||
} | ||
|
||
return { | ||
errors, | ||
warnings, | ||
}; | ||
} |
13 changes: 13 additions & 0 deletions
13
...s/security_solution/public/common/hooks/use_form_with_warnings/form_hook_with_warnings.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/* | ||
* 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 { FormHook, FormData, ValidationError } from '../../../shared_imports'; | ||
|
||
export interface FormHookWithWarnings<T extends FormData = FormData, I extends FormData = T> | ||
extends FormHook<T, I> { | ||
getValidationWarnings(): ValidationError[]; | ||
} |
9 changes: 9 additions & 0 deletions
9
x-pack/plugins/security_solution/public/common/hooks/use_form_with_warnings/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
export type * from './form_hook_with_warnings'; | ||
export * from './use_form_with_warnings'; |
Oops, something went wrong.