From 1b09795e4caf094587cf46f178c5b80192a04d55 Mon Sep 17 00:00:00 2001 From: Xavier Mouligneau Date: Thu, 17 Mar 2022 12:29:16 -0400 Subject: [PATCH] ocd xavier --- x-pack/plugins/alerting/common/alert.ts | 2 +- .../server/lib/validate_snooze_date.ts | 13 ++++++++++ .../alerting/server/routes/snooze_rule.ts | 8 ++----- .../server/rules_client/rules_client.ts | 24 +++++++++---------- x-pack/plugins/alerting/server/types.ts | 2 +- 5 files changed, 29 insertions(+), 20 deletions(-) create mode 100644 x-pack/plugins/alerting/server/lib/validate_snooze_date.ts diff --git a/x-pack/plugins/alerting/common/alert.ts b/x-pack/plugins/alerting/common/alert.ts index 890dae04d27ab..2a4f839035fdd 100644 --- a/x-pack/plugins/alerting/common/alert.ts +++ b/x-pack/plugins/alerting/common/alert.ts @@ -94,7 +94,7 @@ export interface Alert { mutedInstanceIds: string[]; executionStatus: AlertExecutionStatus; monitoring?: RuleMonitoring; - snoozeEndTime?: Date; + snoozeEndTime: Date | null; } export type SanitizedAlert = Omit, 'apiKey'>; diff --git a/x-pack/plugins/alerting/server/lib/validate_snooze_date.ts b/x-pack/plugins/alerting/server/lib/validate_snooze_date.ts new file mode 100644 index 0000000000000..deb951a717a06 --- /dev/null +++ b/x-pack/plugins/alerting/server/lib/validate_snooze_date.ts @@ -0,0 +1,13 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +export const validateSnoozeDate = (date: string) => { + const parsedValue = Date.parse(date); + if (isNaN(parsedValue)) return `Invalid date: ${date}`; + if (parsedValue <= Date.now()) return `Invalid snooze date as it is in the past: ${date}`; + return; +}; diff --git a/x-pack/plugins/alerting/server/routes/snooze_rule.ts b/x-pack/plugins/alerting/server/routes/snooze_rule.ts index 29b11f4cfd1c4..3b2167cc79fa3 100644 --- a/x-pack/plugins/alerting/server/routes/snooze_rule.ts +++ b/x-pack/plugins/alerting/server/routes/snooze_rule.ts @@ -11,6 +11,7 @@ import { ILicenseState, RuleMutedError } from '../lib'; import { verifyAccessAndContext, RewriteRequestCase } from './lib'; import { SnoozeOptions } from '../rules_client'; import { AlertingRequestHandlerContext, INTERNAL_BASE_ALERTING_API_PATH } from '../types'; +import { validateSnoozeDate } from '../lib/validate_snooze_date'; const paramSchema = schema.object({ id: schema.string(), @@ -19,12 +20,7 @@ const paramSchema = schema.object({ const bodySchema = schema.object({ snooze_end_time: schema.oneOf([ schema.string({ - validate: (value) => { - const parsedValue = Date.parse(value); - if (isNaN(parsedValue)) return `Invalid date: ${value}`; - if (parsedValue <= Date.now()) return `Invalid snooze date as it is in the past: ${value}`; - return; - }, + validate: validateSnoozeDate, }), schema.literal(-1), ]), diff --git a/x-pack/plugins/alerting/server/rules_client/rules_client.ts b/x-pack/plugins/alerting/server/rules_client/rules_client.ts index caf2e296c6643..b2c18b0fab783 100644 --- a/x-pack/plugins/alerting/server/rules_client/rules_client.ts +++ b/x-pack/plugins/alerting/server/rules_client/rules_client.ts @@ -40,12 +40,7 @@ import { PartialAlertWithLegacyId as PartialRuleWithLegacyId, RawAlertInstance as RawAlert, } from '../types'; -import { - validateRuleTypeParams, - ruleExecutionStatusFromRaw, - getAlertNotifyWhenType, - RuleMutedError, -} from '../lib'; +import { validateRuleTypeParams, ruleExecutionStatusFromRaw, getAlertNotifyWhenType } from '../lib'; import { GrantAPIKeyResult as SecurityPluginGrantAPIKeyResult, InvalidateAPIKeyResult as SecurityPluginInvalidateAPIKeyResult, @@ -90,6 +85,8 @@ import { getModifiedSearch, modifyFilterKueryNode, } from './lib/mapped_params_utils'; +import { validateSnoozeDate } from '../lib/validate_snooze_date'; +import { RuleMutedError } from '../lib/errors/rule_muted'; export interface RegistryAlertTypeWithAuth extends RegistryRuleType { authorizedConsumers: string[]; @@ -212,6 +209,7 @@ export interface CreateOptions { | 'mutedInstanceIds' | 'actions' | 'executionStatus' + | 'snoozeEndTime' > & { actions: NormalizedAlertAction[] }; options?: { id?: string; @@ -383,7 +381,7 @@ export class RulesClient { updatedBy: username, createdAt: new Date(createTime).toISOString(), updatedAt: new Date(createTime).toISOString(), - snoozeEndTime: data.snoozeEndTime ? new Date(data.snoozeEndTime).toISOString() : null, + snoozeEndTime: null, params: updatedParams as RawRule['params'], muteAll: false, mutedInstanceIds: [], @@ -1503,6 +1501,13 @@ export class RulesClient { } private async snoozeWithOCC({ id, snoozeEndTime }: { id: string; snoozeEndTime: string | -1 }) { + if (typeof snoozeEndTime === 'string') { + const snoozeDateValidationMsg = validateSnoozeDate(snoozeEndTime); + if (snoozeDateValidationMsg) { + throw new RuleMutedError(snoozeDateValidationMsg); + } + } + const { attributes, version } = await this.unsecuredSavedObjectsClient.get( 'alert', id @@ -1519,11 +1524,6 @@ export class RulesClient { if (attributes.actions.length) { await this.actionsAuthorization.ensureAuthorized('execute'); } - - const currentRule = await this.get({ id }); - if (currentRule.muteAll) { - throw new RuleMutedError('Cannot snooze a rule that is already muted'); - } } catch (error) { this.auditLogger?.log( ruleAuditEvent({ diff --git a/x-pack/plugins/alerting/server/types.ts b/x-pack/plugins/alerting/server/types.ts index 7ea39f79c84e7..c4fc282a9cfc9 100644 --- a/x-pack/plugins/alerting/server/types.ts +++ b/x-pack/plugins/alerting/server/types.ts @@ -244,7 +244,7 @@ export interface RawRule extends SavedObjectAttributes { meta?: AlertMeta; executionStatus: RawRuleExecutionStatus; monitoring?: RuleMonitoring; - snoozeEndTime?: string | null; + snoozeEndTime: string | null; } export type AlertInfoParams = Pick<