-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
[SIEM][Detection Engine] Adds actions to Rule Details #54828
Changes from all commits
115f9ae
290742c
23c1cc7
760c8d5
9ec5a7d
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 |
---|---|---|
|
@@ -16,7 +16,11 @@ import { | |
} from '../../../../containers/detection_engine/rules'; | ||
import { Action } from './reducer'; | ||
|
||
import { ActionToaster, displayErrorToast } from '../../../../components/toasters'; | ||
import { | ||
ActionToaster, | ||
displayErrorToast, | ||
displaySuccessToast, | ||
} from '../../../../components/toasters'; | ||
|
||
import * as i18n from '../translations'; | ||
import { bucketRulesResponse } from './helpers'; | ||
|
@@ -25,8 +29,6 @@ export const editRuleAction = (rule: Rule, history: H.History) => { | |
history.push(`/${DETECTION_ENGINE_PAGE_NAME}/rules/id/${rule.id}/edit`); | ||
}; | ||
|
||
export const runRuleAction = () => {}; | ||
|
||
export const duplicateRuleAction = async ( | ||
rule: Rule, | ||
dispatch: React.Dispatch<Action>, | ||
|
@@ -37,6 +39,7 @@ export const duplicateRuleAction = async ( | |
const duplicatedRule = await duplicateRules({ rules: [rule] }); | ||
dispatch({ type: 'updateLoading', ids: [rule.id], isLoading: false }); | ||
dispatch({ type: 'updateRules', rules: duplicatedRule, appendRuleId: rule.id }); | ||
displaySuccessToast(i18n.SUCCESSFULLY_DUPLICATED_RULES(duplicatedRule.length), dispatchToaster); | ||
} catch (e) { | ||
displayErrorToast(i18n.DUPLICATE_RULE_ERROR, [e.message], dispatchToaster); | ||
} | ||
|
@@ -49,7 +52,8 @@ export const exportRulesAction = async (rules: Rule[], dispatch: React.Dispatch< | |
export const deleteRulesAction = async ( | ||
ids: string[], | ||
dispatch: React.Dispatch<Action>, | ||
dispatchToaster: Dispatch<ActionToaster> | ||
dispatchToaster: Dispatch<ActionToaster>, | ||
onRuleDeleted?: () => void | ||
) => { | ||
try { | ||
dispatch({ type: 'updateLoading', ids, isLoading: true }); | ||
|
@@ -65,6 +69,9 @@ export const deleteRulesAction = async ( | |
errors.map(e => e.error.message), | ||
dispatchToaster | ||
); | ||
} else { | ||
// FP: See https://github.com/typescript-eslint/typescript-eslint/issues/1138#issuecomment-566929566 | ||
onRuleDeleted?.(); // eslint-disable-line no-unused-expressions | ||
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. Thanks for the link when putting the disable in 👍 , really helpful and interesting to read. 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. Yeah, they're stilling working out some quirks with linting these new language features it seems. FWIW it was happy with the below, but I wanted the code feng shui.... 😬 if (onRuleDeleted) {
onRuleDeleted();
} |
||
} | ||
} catch (e) { | ||
displayErrorToast( | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { shallow } from 'enzyme'; | ||
import React from 'react'; | ||
|
||
import { RuleActionsOverflow } from './index'; | ||
import { mockRule } from '../../all/__mocks__/mock'; | ||
|
||
jest.mock('react-router-dom', () => ({ | ||
useHistory: () => ({ | ||
push: jest.fn(), | ||
}), | ||
})); | ||
|
||
describe('RuleActionsOverflow', () => { | ||
test('renders correctly against snapshot', () => { | ||
const wrapper = shallow( | ||
<RuleActionsOverflow rule={mockRule('id')} userHasNoPermissions={false} /> | ||
); | ||
expect(wrapper).toMatchSnapshot(); | ||
}); | ||
}); | ||
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. 👍 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { | ||
EuiButtonIcon, | ||
EuiContextMenuItem, | ||
EuiContextMenuPanel, | ||
EuiPopover, | ||
EuiToolTip, | ||
} from '@elastic/eui'; | ||
import React, { useCallback, useMemo, useState } from 'react'; | ||
|
||
import { noop } from 'lodash/fp'; | ||
import { useHistory } from 'react-router-dom'; | ||
import { Rule } from '../../../../../containers/detection_engine/rules'; | ||
import * as i18n from './translations'; | ||
import * as i18nActions from '../../../rules/translations'; | ||
import { deleteRulesAction, duplicateRuleAction } from '../../all/actions'; | ||
import { displaySuccessToast, useStateToaster } from '../../../../../components/toasters'; | ||
import { RuleDownloader } from '../rule_downloader'; | ||
import { DETECTION_ENGINE_PAGE_NAME } from '../../../../../components/link_to/redirect_to_detection_engine'; | ||
|
||
interface RuleActionsOverflowComponentProps { | ||
rule: Rule | null; | ||
userHasNoPermissions: boolean; | ||
} | ||
|
||
/** | ||
* Overflow Actions for a Rule | ||
*/ | ||
const RuleActionsOverflowComponent = ({ | ||
rule, | ||
userHasNoPermissions, | ||
}: RuleActionsOverflowComponentProps) => { | ||
const [isPopoverOpen, setIsPopoverOpen] = useState(false); | ||
const [rulesToExport, setRulesToExport] = useState<Rule[] | undefined>(undefined); | ||
const history = useHistory(); | ||
const [, dispatchToaster] = useStateToaster(); | ||
|
||
const onRuleDeletedCallback = useCallback(() => { | ||
history.push(`/${DETECTION_ENGINE_PAGE_NAME}/rules`); | ||
}, [history]); | ||
|
||
const actions = useMemo( | ||
() => | ||
rule != null | ||
? [ | ||
<EuiContextMenuItem | ||
key={i18nActions.DUPLICATE_RULE} | ||
icon="exportAction" | ||
disabled={userHasNoPermissions} | ||
onClick={async () => { | ||
setIsPopoverOpen(false); | ||
await duplicateRuleAction(rule, noop, dispatchToaster); | ||
}} | ||
> | ||
{i18nActions.DUPLICATE_RULE} | ||
</EuiContextMenuItem>, | ||
<EuiContextMenuItem | ||
key={i18nActions.EXPORT_RULE} | ||
icon="indexEdit" | ||
disabled={userHasNoPermissions || rule.immutable} | ||
onClick={async () => { | ||
setIsPopoverOpen(false); | ||
setRulesToExport([rule]); | ||
}} | ||
> | ||
{i18nActions.EXPORT_RULE} | ||
</EuiContextMenuItem>, | ||
<EuiContextMenuItem | ||
key={i18nActions.DELETE_RULE} | ||
icon="trash" | ||
disabled={userHasNoPermissions || rule.immutable} | ||
onClick={async () => { | ||
setIsPopoverOpen(false); | ||
await deleteRulesAction([rule.id], noop, dispatchToaster, onRuleDeletedCallback); | ||
}} | ||
> | ||
{i18nActions.DELETE_RULE} | ||
</EuiContextMenuItem>, | ||
] | ||
: [], | ||
[rule, userHasNoPermissions] | ||
); | ||
|
||
return ( | ||
<> | ||
<EuiPopover | ||
anchorPosition="leftCenter" | ||
button={ | ||
<EuiToolTip position="top" content={i18n.ALL_ACTIONS}> | ||
<EuiButtonIcon | ||
iconType="boxesHorizontal" | ||
aria-label={i18n.ALL_ACTIONS} | ||
isDisabled={userHasNoPermissions} | ||
onClick={() => setIsPopoverOpen(!isPopoverOpen)} | ||
/> | ||
</EuiToolTip> | ||
} | ||
closePopover={() => setIsPopoverOpen(false)} | ||
id="ruleActionsOverflow" | ||
isOpen={isPopoverOpen} | ||
ownFocus={true} | ||
panelPaddingSize="none" | ||
> | ||
<EuiContextMenuPanel items={actions} /> | ||
</EuiPopover> | ||
<RuleDownloader | ||
filename={`${i18nActions.EXPORT_FILENAME}.ndjson`} | ||
rules={rulesToExport} | ||
onExportComplete={exportCount => { | ||
displaySuccessToast( | ||
i18nActions.SUCCESSFULLY_EXPORTED_RULES(exportCount), | ||
dispatchToaster | ||
); | ||
}} | ||
/> | ||
</> | ||
); | ||
}; | ||
|
||
export const RuleActionsOverflow = React.memo(RuleActionsOverflowComponent); | ||
|
||
RuleActionsOverflow.displayName = 'RuleActionsOverflow'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { i18n } from '@kbn/i18n'; | ||
|
||
export const ALL_ACTIONS = i18n.translate( | ||
'xpack.siem.detectionEngine.rules.components.ruleActionsOverflow.allActionsTitle', | ||
{ | ||
defaultMessage: 'All actions', | ||
} | ||
); |
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.
nice catch! 👍