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

[RAM][SECURITYSOLUTION][ALERTS] - Throw an error when user tries to set schedule interval shorter than any action frequency #156644

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
38 changes: 36 additions & 2 deletions x-pack/plugins/alerting/server/rules_client/methods/bulk_edit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
RuleWithLegacyId,
RuleTypeRegistry,
RawRuleAction,
RuleNotifyWhen,
} from '../../types';
import {
validateRuleTypeParams,
Expand Down Expand Up @@ -685,6 +686,9 @@ async function getUpdatedAttributesFromOperations(
break;
}
default: {
if (operation.field === 'schedule') {
validateScheduleOperation(operation.value, attributes.actions);
}
const { modifiedAttributes, isAttributeModified } = applyBulkEditOperation(
operation,
rule.attributes
Expand Down Expand Up @@ -725,8 +729,7 @@ function validateScheduleInterval(
if (!scheduleInterval) {
return;
}
const isIntervalInvalid =
parseDuration(scheduleInterval as string) < context.minimumScheduleIntervalInMs;
const isIntervalInvalid = parseDuration(scheduleInterval) < context.minimumScheduleIntervalInMs;
if (isIntervalInvalid && context.minimumScheduleInterval.enforce) {
throw Error(
`Error updating rule: the interval is less than the allowed minimum interval of ${context.minimumScheduleInterval.value}`
Expand All @@ -738,6 +741,37 @@ function validateScheduleInterval(
}
}

/**
* Validate that updated schedule interval is not longer than any of the existing action frequencies
* @param schedule Schedule interval that user tries to set
* @param actions Rule actions
*/
function validateScheduleOperation(
schedule: RawRule['schedule'],
actions: RawRule['actions']
): void {
const scheduleInterval = parseDuration(schedule.interval);
const actionsWithInvalidThrottles = [];

for (const action of actions) {
// check for actions throttled shorter than the rule schedule
if (
action.frequency?.notifyWhen === RuleNotifyWhen.THROTTLE &&
parseDuration(action.frequency.throttle!) < scheduleInterval
) {
actionsWithInvalidThrottles.push(action);
}
}

if (actionsWithInvalidThrottles.length > 0) {
throw Error(
`Error updating rule: the interval is longer than the action frequencies: ${actionsWithInvalidThrottles
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we just add the ruleId and/or the ruleName in the error?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah yeah, that would be more useful!

.map((action) => action.frequency?.throttle)
.join(', ')}`
);
}
}

async function prepareApiKeys(
context: RulesClientContext,
rule: SavedObjectsFindResult<RawRule>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2359,6 +2359,67 @@ describe('bulkEdit()', () => {
'Error updating rule: the interval is less than the allowed minimum interval of 3m'
);
});

test('should not update saved object and return error if schedule interval is shorter than any action frequency in the rule', async () => {
mockCreatePointInTimeFinderAsInternalUser({
saved_objects: [
{
...existingDecryptedRule,
attributes: {
...existingDecryptedRule.attributes,
actions: [
{
actionRef: 'action_0',
actionTypeId: 'test',
frequency: { notifyWhen: 'onThrottleInterval', summary: false, throttle: '5m' },
group: 'default',
params: {},
uuid: '111',
},
{
actionRef: 'action_1',
actionTypeId: '',
frequency: { notifyWhen: 'onThrottleInterval', summary: true, throttle: '10s' },
group: 'default',
params: {},
uuid: '100',
},
],
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} as any,
references: [
{
name: 'action_0',
type: 'action',
id: '1',
},
{
name: 'action_1',
type: 'action',
id: '2',
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
] as any,
},
],
});

const result = await rulesClient.bulkEdit({
operations: [
{
field: 'schedule',
operation: 'set',
value: { interval: '10m' },
},
],
});

expect(result.errors).toHaveLength(1);
expect(result.rules).toHaveLength(0);
expect(result.errors[0].message).toBe(
'Error updating rule: the interval is longer than the action frequencies: 5m, 10s'
);
});
});

describe('paramsModifier', () => {
Expand Down