Skip to content

Commit

Permalink
[Security Solution][Detections] Fix adding an action to detection rul…
Browse files Browse the repository at this point in the history
  • Loading branch information
cnasikas authored and chrisronline committed Nov 19, 2020
1 parent 6bfe02a commit b8587d7
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,20 @@ describe('RuleActionsField', () => {
},
},
});

const messageVariables = {
context: [],
state: [],
params: [],
};

const Component = () => {
const field = useFormFieldMock();
const { form } = useForm();

return (
<Form form={form}>
<RuleActionsField euiFieldProps={{ options: [] }} field={field} />
<RuleActionsField field={field} messageVariables={messageVariables} />
</Form>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,21 @@ import ReactMarkdown from 'react-markdown';
import styled from 'styled-components';

import { NOTIFICATION_SUPPORTED_ACTION_TYPES_IDS } from '../../../../../common/constants';
import { SelectField, useFormContext } from '../../../../shared_imports';
import { FieldHook, useFormContext } from '../../../../shared_imports';
import {
ActionForm,
ActionType,
loadActionTypes,
ActionVariables,
} from '../../../../../../triggers_actions_ui/public';
import { AlertAction } from '../../../../../../alerts/common';
import { useKibana } from '../../../../common/lib/kibana';
import { FORM_ERRORS_TITLE } from './translations';

type ThrottleSelectField = typeof SelectField;
interface Props {
field: FieldHook;
messageVariables: ActionVariables;
}

const DEFAULT_ACTION_GROUP_ID = 'default';
const DEFAULT_ACTION_MESSAGE =
Expand All @@ -34,7 +38,7 @@ const FieldErrorsContainer = styled.div`
}
`;

export const RuleActionsField: ThrottleSelectField = ({ field, messageVariables }) => {
export const RuleActionsField: React.FC<Props> = ({ field, messageVariables }) => {
const [fieldErrors, setFieldErrors] = useState<string | null>(null);
const [supportedActionTypes, setSupportedActionTypes] = useState<ActionType[] | undefined>();
const form = useFormContext();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,20 @@ jest.mock('../../../../common/lib/kibana', () => ({
}),
}));

const actionMessageParams = {
context: [],
state: [],
params: [],
};

describe('StepRuleActions', () => {
it('renders correctly', () => {
const wrapper = shallow(
<StepRuleActions actionMessageParams={[]} isReadOnlyView={false} isLoading={false} />
<StepRuleActions
actionMessageParams={actionMessageParams}
isReadOnlyView={false}
isLoading={false}
/>
);

expect(wrapper.find('[data-test-subj="stepRuleActions"]')).toHaveLength(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
import { findIndex } from 'lodash/fp';
import React, { FC, memo, useCallback, useEffect, useMemo } from 'react';

import { ActionVariable } from '../../../../../../triggers_actions_ui/public';
import { ActionVariables } from '../../../../../../triggers_actions_ui/public';
import {
RuleStep,
RuleStepProps,
Expand All @@ -38,7 +38,7 @@ import { APP_ID } from '../../../../../common/constants';

interface StepRuleActionsProps extends RuleStepProps {
defaultValues?: ActionsStepRule | null;
actionMessageParams: ActionVariable[];
actionMessageParams: ActionVariables;
}

const stepActionsDefaultValue: ActionsStepRule = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { useLocation } from 'react-router-dom';

import styled from 'styled-components';
import { EuiFlexItem } from '@elastic/eui';
import { ActionVariable } from '../../../../../../triggers_actions_ui/public';
import { ActionVariables } from '../../../../../../triggers_actions_ui/public';
import { RuleAlertAction } from '../../../../../common/detection_engine/types';
import { assertUnreachable } from '../../../../../common/utility_types';
import { transformRuleToAlertAction } from '../../../../../common/detection_engine/transform_actions';
Expand Down Expand Up @@ -366,21 +366,26 @@ export const getActionMessageRuleParams = (ruleType: Type): string[] => {
return ruleParamsKeys;
};

export const getActionMessageParams = memoizeOne((ruleType: Type | undefined): ActionVariable[] => {
if (!ruleType) {
return [];
export const getActionMessageParams = memoizeOne(
(ruleType: Type | undefined): ActionVariables => {
if (!ruleType) {
return { state: [], params: [] };
}
const actionMessageRuleParams = getActionMessageRuleParams(ruleType);
// Prefixes are being added automatically by the ActionTypeForm
return {
state: [{ name: 'signals_count', description: 'state.signals_count' }],
params: [],
context: [
{ name: 'results_link', description: 'context.results_link' },
...actionMessageRuleParams.map((param) => {
const extendedParam = `rule.${param}`;
return { name: extendedParam, description: `context.${extendedParam}` };
}),
],
};
}
const actionMessageRuleParams = getActionMessageRuleParams(ruleType);

return [
{ name: 'state.signals_count', description: 'state.signals_count' },
{ name: '{context.results_link}', description: 'context.results_link' },
...actionMessageRuleParams.map((param) => {
const extendedParam = `context.rule.${param}`;
return { name: extendedParam, description: extendedParam };
}),
];
});
);

// typed as null not undefined as the initial state for this value is null.
export const userHasNoPermissions = (canUserCRUD: boolean | null): boolean =>
Expand Down
1 change: 1 addition & 0 deletions x-pack/plugins/triggers_actions_ui/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export {
AlertTypeParamsExpressionProps,
ValidationResult,
ActionVariable,
ActionVariables,
ActionConnector,
IErrorObject,
} from './types';
Expand Down

0 comments on commit b8587d7

Please sign in to comment.