-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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 edit max_signals field for custom …
…rules (#179680) **Resolves: #173593 **Fixes: #164234 ## Summary Adds a number component in the create and edit rule forms so that users are able to customize the `max_signals` value for custom rules from the UI. Also adds validations to the rule API's for invalid values being passed in. This PR also exposes the `xpack.alerting.rules.run.alerts.max` config setting from the alerting framework to the frontend and backend so that we can validate against it as it supersedes our own `max_signals` value. [Flaky test run (internal) ](https://buildkite.com/elastic/kibana-flaky-test-suite-runner/builds/5601) ### Screenshots **Form component** <p align="center"> <img width="887" alt="Screenshot 2024-04-08 at 11 02 12 PM" src="https://github.com/elastic/kibana/assets/56367316/58cd2f6d-61b6-4343-8025-ff867c050dd7"> </p> **Details Page** <p align="center"> <img width="595" alt="Screenshot 2024-04-08 at 11 04 04 PM" src="https://github.com/elastic/kibana/assets/56367316/d2c61593-3d35-408e-b047-b4d1f68898f8"> </p> **Error state** <p align="center"> <img width="857" alt="Screenshot 2024-04-08 at 11 01 55 PM" src="https://github.com/elastic/kibana/assets/56367316/86e64280-7b81-46f2-b223-fde8c20066c8"> </p> **Warning state** <p align="center"> <img width="601" alt="Screenshot 2024-04-16 at 3 20 00 PM" src="https://github.com/elastic/kibana/assets/56367316/eab07d62-3d3e-4c85-8468-36c3e56c5a99"> </p> ### Checklist Delete any items that are not applicable to this PR. - [x] Any text added follows [EUI's writing guidelines](https://elastic.github.io/eui/#/guidelines/writing), uses sentence case text and includes [i18n support](https://github.com/elastic/kibana/blob/main/packages/kbn-i18n/README.md) - [x] [Documentation](https://www.elastic.co/guide/en/kibana/master/development-documentation.html) was added for features that require explanation or tutorials - [x] [Unit or functional tests](https://www.elastic.co/guide/en/kibana/master/development-tests.html) were updated or added to match the most common scenarios - [x] [Flaky Test Runner](https://ci-stats.kibana.dev/trigger_flaky_test_runner/1) was used on any tests changed - [x] Any UI touched in this PR is usable by keyboard only (learn more about [keyboard accessibility](https://webaim.org/techniques/keyboard/)) ### 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: Juan Pablo Djeredjian <[email protected]>
- Loading branch information
Showing
54 changed files
with
640 additions
and
85 deletions.
There are no files selected for viewing
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
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
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
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
99 changes: 99 additions & 0 deletions
99
...curity_solution/public/detection_engine/rule_creation_ui/components/max_signals/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,99 @@ | ||
/* | ||
* 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 React, { useMemo, useCallback } from 'react'; | ||
import type { EuiFieldNumberProps } from '@elastic/eui'; | ||
import { EuiTextColor, EuiFormRow, EuiFieldNumber, EuiIcon } from '@elastic/eui'; | ||
import type { FieldHook } from '@kbn/es-ui-shared-plugin/static/forms/hook_form_lib'; | ||
import { css } from '@emotion/css'; | ||
import { DEFAULT_MAX_SIGNALS } from '../../../../../common/constants'; | ||
import * as i18n from './translations'; | ||
import { useKibana } from '../../../../common/lib/kibana'; | ||
|
||
interface MaxSignalsFieldProps { | ||
dataTestSubj: string; | ||
field: FieldHook<number | ''>; | ||
idAria: string; | ||
isDisabled: boolean; | ||
placeholder?: string; | ||
} | ||
|
||
const MAX_SIGNALS_FIELD_WIDTH = 200; | ||
|
||
export const MaxSignals: React.FC<MaxSignalsFieldProps> = ({ | ||
dataTestSubj, | ||
field, | ||
idAria, | ||
isDisabled, | ||
placeholder, | ||
}): JSX.Element => { | ||
const { setValue, value } = field; | ||
const { alerting } = useKibana().services; | ||
const maxAlertsPerRun = alerting.getMaxAlertsPerRun(); | ||
|
||
const [isInvalid, error] = useMemo(() => { | ||
if (typeof value === 'number' && !isNaN(value) && value <= 0) { | ||
return [true, i18n.GREATER_THAN_ERROR]; | ||
} | ||
return [false]; | ||
}, [value]); | ||
|
||
const hasWarning = useMemo( | ||
() => typeof value === 'number' && !isNaN(value) && value > maxAlertsPerRun, | ||
[maxAlertsPerRun, value] | ||
); | ||
|
||
const handleMaxSignalsChange: EuiFieldNumberProps['onChange'] = useCallback( | ||
(e) => { | ||
const maxSignalsValue = (e.target as HTMLInputElement).value; | ||
// Has to handle an empty string as the field is optional | ||
setValue(maxSignalsValue !== '' ? Number(maxSignalsValue.trim()) : ''); | ||
}, | ||
[setValue] | ||
); | ||
|
||
const helpText = useMemo(() => { | ||
const textToRender = []; | ||
if (hasWarning) { | ||
textToRender.push( | ||
<EuiTextColor color="warning">{i18n.LESS_THAN_WARNING(maxAlertsPerRun)}</EuiTextColor> | ||
); | ||
} | ||
textToRender.push(i18n.MAX_SIGNALS_HELP_TEXT(DEFAULT_MAX_SIGNALS)); | ||
return textToRender; | ||
}, [hasWarning, maxAlertsPerRun]); | ||
|
||
return ( | ||
<EuiFormRow | ||
css={css` | ||
.euiFormControlLayout { | ||
width: ${MAX_SIGNALS_FIELD_WIDTH}px; | ||
} | ||
`} | ||
describedByIds={idAria ? [idAria] : undefined} | ||
fullWidth | ||
helpText={helpText} | ||
label={field.label} | ||
labelAppend={field.labelAppend} | ||
isInvalid={isInvalid} | ||
error={error} | ||
> | ||
<EuiFieldNumber | ||
isInvalid={isInvalid} | ||
value={value as EuiFieldNumberProps['value']} | ||
onChange={handleMaxSignalsChange} | ||
isLoading={field.isValidating} | ||
placeholder={placeholder} | ||
data-test-subj={dataTestSubj} | ||
disabled={isDisabled} | ||
append={hasWarning ? <EuiIcon size="s" type="warning" color="warning" /> : undefined} | ||
/> | ||
</EuiFormRow> | ||
); | ||
}; | ||
|
||
MaxSignals.displayName = 'MaxSignals'; |
35 changes: 35 additions & 0 deletions
35
..._solution/public/detection_engine/rule_creation_ui/components/max_signals/translations.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 { i18n } from '@kbn/i18n'; | ||
|
||
export const GREATER_THAN_ERROR = i18n.translate( | ||
'xpack.securitySolution.detectionEngine.createRule.stepAboutRule.maxAlertsFieldGreaterThanError', | ||
{ | ||
defaultMessage: 'Max alerts must be greater than 0.', | ||
} | ||
); | ||
|
||
export const LESS_THAN_WARNING = (maxNumber: number) => | ||
i18n.translate( | ||
'xpack.securitySolution.detectionEngine.createRule.stepAboutRule.maxAlertsFieldLessThanWarning', | ||
{ | ||
values: { maxNumber }, | ||
defaultMessage: | ||
'Kibana only allows a maximum of {maxNumber} {maxNumber, plural, =1 {alert} other {alerts}} per rule run.', | ||
} | ||
); | ||
|
||
export const MAX_SIGNALS_HELP_TEXT = (defaultNumber: number) => | ||
i18n.translate( | ||
'xpack.securitySolution.detectionEngine.createRule.stepAboutRule.fieldMaxAlertsHelpText', | ||
{ | ||
values: { defaultNumber }, | ||
defaultMessage: | ||
'The maximum number of alerts the rule will create each time it runs. Default is {defaultNumber}.', | ||
} | ||
); |
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
Oops, something went wrong.