Skip to content

Commit

Permalink
Add max configuration for minimumScheduleInterval and number of actio…
Browse files Browse the repository at this point in the history
…ns (elastic#130465)

* Add max configuration for minimumScheduleInterval and number of actions

* Fix test execution -> run
  • Loading branch information
mikecote authored and kertal committed May 24, 2022
1 parent 84eb3f7 commit 61f2da7
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 3 deletions.
71 changes: 71 additions & 0 deletions x-pack/plugins/alerting/server/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,75 @@ describe('config validation', () => {
}
`);
});

describe('rules.minimumScheduleInterval.value', () => {
test('allows 1d as a value', () => {
const config: Record<string, unknown> = {
rules: {
minimumScheduleInterval: {
value: '1d',
},
},
};
const validatedConfig = configSchema.validate(config);
expect(validatedConfig.rules.minimumScheduleInterval.value).toMatchInlineSnapshot(`"1d"`);
});

test(`doesn't allow 2d as a value`, () => {
const config: Record<string, unknown> = {
rules: {
minimumScheduleInterval: {
value: '2d',
},
},
};
expect(() => configSchema.validate(config)).toThrowErrorMatchingInlineSnapshot(
`"[rules.minimumScheduleInterval.value]: duration cannot exceed one day"`
);
});

test(`doesn't allow 25h as a value`, () => {
const config: Record<string, unknown> = {
rules: {
minimumScheduleInterval: {
value: '25h',
},
},
};
expect(() => configSchema.validate(config)).toThrowErrorMatchingInlineSnapshot(
`"[rules.minimumScheduleInterval.value]: duration cannot exceed one day"`
);
});
});

describe('rules.run.actions.max', () => {
test('allows 100000 as a value', () => {
const config: Record<string, unknown> = {
rules: {
run: {
actions: {
max: 100000,
},
},
},
};
const validatedConfig = configSchema.validate(config);
expect(validatedConfig.rules.run.actions.max).toMatchInlineSnapshot(`100000`);
});

test(`doesn't allow 100001 as a value`, () => {
const config: Record<string, unknown> = {
rules: {
run: {
actions: {
max: 100001,
},
},
},
};
expect(() => configSchema.validate(config)).toThrowErrorMatchingInlineSnapshot(
`"[rules.run.actions.max]: Value must be equal to or lower than [100000]."`
);
});
});
});
17 changes: 14 additions & 3 deletions x-pack/plugins/alerting/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
*/

import { schema, TypeOf } from '@kbn/config-schema';
import { validateDurationSchema } from './lib';
import { validateDurationSchema, parseDuration } from './lib';

const ONE_DAY_IN_MS = 24 * 60 * 60 * 1000;
const ruleTypeSchema = schema.object({
id: schema.string(),
timeout: schema.maybe(schema.string({ validate: validateDurationSchema })),
Expand All @@ -16,15 +17,25 @@ const ruleTypeSchema = schema.object({
const rulesSchema = schema.object({
minimumScheduleInterval: schema.object({
value: schema.string({
validate: validateDurationSchema,
validate: (duration: string) => {
const validationResult = validateDurationSchema(duration);
if (validationResult) {
return validationResult;
}

const parsedDurationMs = parseDuration(duration);
if (parsedDurationMs > ONE_DAY_IN_MS) {
return 'duration cannot exceed one day';
}
},
defaultValue: '1m',
}),
enforce: schema.boolean({ defaultValue: false }), // if enforce is false, only warnings will be shown
}),
run: schema.object({
timeout: schema.maybe(schema.string({ validate: validateDurationSchema })),
actions: schema.object({
max: schema.number({ defaultValue: 100000 }),
max: schema.number({ defaultValue: 100000, max: 100000 }),
}),
ruleTypeOverrides: schema.maybe(schema.arrayOf(ruleTypeSchema)),
}),
Expand Down

0 comments on commit 61f2da7

Please sign in to comment.