Skip to content

Commit

Permalink
Merge pull request #1 from XavierM/pr/127081
Browse files Browse the repository at this point in the history
review
  • Loading branch information
Zacqary authored Mar 17, 2022
2 parents 884e94b + 544c7f0 commit 1b15ef4
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 23 deletions.
2 changes: 1 addition & 1 deletion x-pack/plugins/alerting/common/alert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export interface Alert<Params extends AlertTypeParams = never> {
mutedInstanceIds: string[];
executionStatus: AlertExecutionStatus;
monitoring?: RuleMonitoring;
snoozeEndTime?: Date | null;
snoozeEndTime: Date | null;
}

export type SanitizedAlert<Params extends AlertTypeParams = never> = Omit<Alert<Params>, 'apiKey'>;
Expand Down
13 changes: 13 additions & 0 deletions x-pack/plugins/alerting/server/lib/validate_snooze_date.ts
Original file line number Diff line number Diff line change
@@ -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;
};
8 changes: 2 additions & 6 deletions x-pack/plugins/alerting/server/routes/snooze_rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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),
]),
Expand Down
26 changes: 11 additions & 15 deletions x-pack/plugins/alerting/server/rules_client/rules_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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[];
Expand Down Expand Up @@ -212,6 +209,7 @@ export interface CreateOptions<Params extends RuleTypeParams> {
| 'mutedInstanceIds'
| 'actions'
| 'executionStatus'
| 'snoozeEndTime'
> & { actions: NormalizedAlertAction[] };
options?: {
id?: string;
Expand Down Expand Up @@ -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<RawRule>(
'alert',
id
Expand All @@ -1519,15 +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');
}

if (snoozeEndTime !== -1 && isNaN(Date.parse(snoozeEndTime))) {
throw new Error('snoozeEndTime must be a valid date or -1');
}
} catch (error) {
this.auditLogger?.log(
ruleAuditEvent({
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/alerting/server/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<
Expand Down

0 comments on commit 1b15ef4

Please sign in to comment.