Skip to content
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][Detections] Allow synchronous rule actions to be updated via PATCH #67914

Merged
merged 4 commits into from
Jun 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ export const patchRulesBulkRoute = (router: IRouter, ml: SetupPlugins['ml']) =>
version,
anomalyThreshold,
machineLearningJobId,
actions,
});
if (rule != null && rule.enabled != null && rule.name != null) {
const ruleActions = await updateRulesNotifications({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ export const patchRulesRoute = (router: IRouter, ml: SetupPlugins['ml']) => {
version,
anomalyThreshold,
machineLearningJobId,
actions,
});
if (rule != null && rule.enabled != null && rule.name != null) {
const ruleActions = await updateRulesNotifications({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,82 @@ describe('patchRules', () => {
})
);
});

describe('regression tests', () => {
it("updates the rule's actions if provided", async () => {
const existingRule = getResult();

const action = {
action_type_id: '.slack',
id: '2933e581-d81c-4fe3-88fe-c57c6b8a5bfd',
params: {
message: 'Rule {{context.rule.name}} generated {{state.signals_count}} signals',
},
group: 'default',
};

await patchRules({
alertsClient,
savedObjectsClient,
actions: [action],
rule: existingRule,
});

expect(alertsClient.update).toHaveBeenCalledWith(
expect.objectContaining({
data: expect.objectContaining({
actions: [
{
actionTypeId: '.slack',
id: '2933e581-d81c-4fe3-88fe-c57c6b8a5bfd',
params: {
message: 'Rule {{context.rule.name}} generated {{state.signals_count}} signals',
},
group: 'default',
},
],
}),
})
);
});

it('does not update actions if none are specified', async () => {
const existingRule = {
...getResult(),
actions: [
{
actionTypeId: '.slack',
id: '2933e581-d81c-4fe3-88fe-c57c6b8a5bfd',
params: {
message: 'Rule {{context.rule.name}} generated {{state.signals_count}} signals',
},
group: 'default',
},
],
};

await patchRules({
alertsClient,
savedObjectsClient,
rule: existingRule,
});

expect(alertsClient.update).toHaveBeenCalledWith(
expect.objectContaining({
data: expect.objectContaining({
actions: [
{
actionTypeId: '.slack',
id: '2933e581-d81c-4fe3-88fe-c57c6b8a5bfd',
params: {
message: 'Rule {{context.rule.name}} generated {{state.signals_count}} signals',
},
group: 'default',
},
],
}),
})
);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import { defaults } from 'lodash/fp';
import { PartialAlert } from '../../../../../alerts/server';
import { transformRuleToAlertAction } from '../../../../common/detection_engine/transform_actions';
import { PatchRuleParams } from './types';
import { addTags } from './add_tags';
import { calculateVersion, calculateName, calculateInterval } from './utils';
Expand Down Expand Up @@ -44,6 +45,7 @@ export const patchRules = async ({
exceptions_list,
anomalyThreshold,
machineLearningJobId,
actions,
}: PatchRuleParams): Promise<PartialAlert | null> => {
if (rule == null) {
return null;
Expand Down Expand Up @@ -121,7 +123,7 @@ export const patchRules = async ({
schedule: {
interval: calculateInterval(interval, rule.schedule.interval),
},
actions: rule.actions,
actions: actions?.map(transformRuleToAlertAction) ?? rule.actions,
params: nextParams,
},
});
Expand Down