Skip to content

Commit 893b296

Browse files
authored
[Security Solution][Detections] Fix adding an action to detection rules (#83722)
1 parent 3a8ea29 commit 893b296

File tree

6 files changed

+49
-22
lines changed

6 files changed

+49
-22
lines changed

x-pack/plugins/security_solution/public/detections/components/rules/rule_actions_field/index.test.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,20 @@ describe('RuleActionsField', () => {
3131
},
3232
},
3333
});
34+
35+
const messageVariables = {
36+
context: [],
37+
state: [],
38+
params: [],
39+
};
40+
3441
const Component = () => {
3542
const field = useFormFieldMock();
3643
const { form } = useForm();
3744

3845
return (
3946
<Form form={form}>
40-
<RuleActionsField euiFieldProps={{ options: [] }} field={field} />
47+
<RuleActionsField field={field} messageVariables={messageVariables} />
4148
</Form>
4249
);
4350
};

x-pack/plugins/security_solution/public/detections/components/rules/rule_actions_field/index.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,21 @@ import ReactMarkdown from 'react-markdown';
1212
import styled from 'styled-components';
1313

1414
import { NOTIFICATION_SUPPORTED_ACTION_TYPES_IDS } from '../../../../../common/constants';
15-
import { SelectField, useFormContext } from '../../../../shared_imports';
15+
import { FieldHook, useFormContext } from '../../../../shared_imports';
1616
import {
1717
ActionForm,
1818
ActionType,
1919
loadActionTypes,
20+
ActionVariables,
2021
} from '../../../../../../triggers_actions_ui/public';
2122
import { AlertAction } from '../../../../../../alerts/common';
2223
import { useKibana } from '../../../../common/lib/kibana';
2324
import { FORM_ERRORS_TITLE } from './translations';
2425

25-
type ThrottleSelectField = typeof SelectField;
26+
interface Props {
27+
field: FieldHook;
28+
messageVariables: ActionVariables;
29+
}
2630

2731
const DEFAULT_ACTION_GROUP_ID = 'default';
2832
const DEFAULT_ACTION_MESSAGE =
@@ -34,7 +38,7 @@ const FieldErrorsContainer = styled.div`
3438
}
3539
`;
3640

37-
export const RuleActionsField: ThrottleSelectField = ({ field, messageVariables }) => {
41+
export const RuleActionsField: React.FC<Props> = ({ field, messageVariables }) => {
3842
const [fieldErrors, setFieldErrors] = useState<string | null>(null);
3943
const [supportedActionTypes, setSupportedActionTypes] = useState<ActionType[] | undefined>();
4044
const form = useFormContext();

x-pack/plugins/security_solution/public/detections/components/rules/step_rule_actions/index.test.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,20 @@ jest.mock('../../../../common/lib/kibana', () => ({
3030
}),
3131
}));
3232

33+
const actionMessageParams = {
34+
context: [],
35+
state: [],
36+
params: [],
37+
};
38+
3339
describe('StepRuleActions', () => {
3440
it('renders correctly', () => {
3541
const wrapper = shallow(
36-
<StepRuleActions actionMessageParams={[]} isReadOnlyView={false} isLoading={false} />
42+
<StepRuleActions
43+
actionMessageParams={actionMessageParams}
44+
isReadOnlyView={false}
45+
isLoading={false}
46+
/>
3747
);
3848

3949
expect(wrapper.find('[data-test-subj="stepRuleActions"]')).toHaveLength(1);

x-pack/plugins/security_solution/public/detections/components/rules/step_rule_actions/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
import { findIndex } from 'lodash/fp';
1717
import React, { FC, memo, useCallback, useEffect, useMemo } from 'react';
1818

19-
import { ActionVariable } from '../../../../../../triggers_actions_ui/public';
19+
import { ActionVariables } from '../../../../../../triggers_actions_ui/public';
2020
import {
2121
RuleStep,
2222
RuleStepProps,
@@ -38,7 +38,7 @@ import { APP_ID } from '../../../../../common/constants';
3838

3939
interface StepRuleActionsProps extends RuleStepProps {
4040
defaultValues?: ActionsStepRule | null;
41-
actionMessageParams: ActionVariable[];
41+
actionMessageParams: ActionVariables;
4242
}
4343

4444
const stepActionsDefaultValue: ActionsStepRule = {

x-pack/plugins/security_solution/public/detections/pages/detection_engine/rules/helpers.tsx

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { useLocation } from 'react-router-dom';
1111

1212
import styled from 'styled-components';
1313
import { EuiFlexItem } from '@elastic/eui';
14-
import { ActionVariable } from '../../../../../../triggers_actions_ui/public';
14+
import { ActionVariables } from '../../../../../../triggers_actions_ui/public';
1515
import { RuleAlertAction } from '../../../../../common/detection_engine/types';
1616
import { assertUnreachable } from '../../../../../common/utility_types';
1717
import { transformRuleToAlertAction } from '../../../../../common/detection_engine/transform_actions';
@@ -366,21 +366,26 @@ export const getActionMessageRuleParams = (ruleType: Type): string[] => {
366366
return ruleParamsKeys;
367367
};
368368

369-
export const getActionMessageParams = memoizeOne((ruleType: Type | undefined): ActionVariable[] => {
370-
if (!ruleType) {
371-
return [];
369+
export const getActionMessageParams = memoizeOne(
370+
(ruleType: Type | undefined): ActionVariables => {
371+
if (!ruleType) {
372+
return { state: [], params: [] };
373+
}
374+
const actionMessageRuleParams = getActionMessageRuleParams(ruleType);
375+
// Prefixes are being added automatically by the ActionTypeForm
376+
return {
377+
state: [{ name: 'signals_count', description: 'state.signals_count' }],
378+
params: [],
379+
context: [
380+
{ name: 'results_link', description: 'context.results_link' },
381+
...actionMessageRuleParams.map((param) => {
382+
const extendedParam = `rule.${param}`;
383+
return { name: extendedParam, description: `context.${extendedParam}` };
384+
}),
385+
],
386+
};
372387
}
373-
const actionMessageRuleParams = getActionMessageRuleParams(ruleType);
374-
375-
return [
376-
{ name: 'state.signals_count', description: 'state.signals_count' },
377-
{ name: '{context.results_link}', description: 'context.results_link' },
378-
...actionMessageRuleParams.map((param) => {
379-
const extendedParam = `context.rule.${param}`;
380-
return { name: extendedParam, description: extendedParam };
381-
}),
382-
];
383-
});
388+
);
384389

385390
// typed as null not undefined as the initial state for this value is null.
386391
export const userHasNoPermissions = (canUserCRUD: boolean | null): boolean =>

x-pack/plugins/triggers_actions_ui/public/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export {
2121
AlertTypeParamsExpressionProps,
2222
ValidationResult,
2323
ActionVariable,
24+
ActionVariables,
2425
ActionConnector,
2526
IErrorObject,
2627
} from './types';

0 commit comments

Comments
 (0)