diff --git a/x-pack/plugins/alerting/common/routes/rule/apis/unmute_alert/index.ts b/x-pack/plugins/alerting/common/routes/rule/apis/unmute_alert/index.ts new file mode 100644 index 000000000000000..202402bdda60706 --- /dev/null +++ b/x-pack/plugins/alerting/common/routes/rule/apis/unmute_alert/index.ts @@ -0,0 +1,12 @@ +/* + * 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 { unmuteAlertParamsSchema } from './schemas/latest'; +export { unmuteAlertParamsSchema as unmuteAlertParamsSchemaV1 } from './schemas/v1'; + +export type { UnmuteAlertRequestParams } from './types/latest'; +export type { UnmuteAlertRequestParams as UnmuteAlertRequestParamsV1 } from './types/v1'; diff --git a/x-pack/plugins/alerting/common/routes/rule/apis/unmute_alert/schemas/latest.ts b/x-pack/plugins/alerting/common/routes/rule/apis/unmute_alert/schemas/latest.ts new file mode 100644 index 000000000000000..e560bd87e049198 --- /dev/null +++ b/x-pack/plugins/alerting/common/routes/rule/apis/unmute_alert/schemas/latest.ts @@ -0,0 +1,7 @@ +/* + * 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 { unmuteAlertParamsSchema } from './v1'; diff --git a/x-pack/plugins/alerting/common/routes/rule/apis/unmute_alert/schemas/v1.ts b/x-pack/plugins/alerting/common/routes/rule/apis/unmute_alert/schemas/v1.ts new file mode 100644 index 000000000000000..4ae0dccb9697802 --- /dev/null +++ b/x-pack/plugins/alerting/common/routes/rule/apis/unmute_alert/schemas/v1.ts @@ -0,0 +1,21 @@ +/* + * 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. + */ + +import { schema } from '@kbn/config-schema'; + +export const unmuteAlertParamsSchema = schema.object({ + rule_id: schema.string({ + meta: { + description: 'The identifier for the rule.', + }, + }), + alert_id: schema.string({ + meta: { + description: 'The identifier for the alert.', + }, + }), +}); diff --git a/x-pack/plugins/alerting/common/routes/rule/apis/unmute_alert/types/latest.ts b/x-pack/plugins/alerting/common/routes/rule/apis/unmute_alert/types/latest.ts new file mode 100644 index 000000000000000..cab31be4e070e10 --- /dev/null +++ b/x-pack/plugins/alerting/common/routes/rule/apis/unmute_alert/types/latest.ts @@ -0,0 +1,8 @@ +/* + * 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 type { UnmuteAlertRequestParams } from './v1'; diff --git a/x-pack/plugins/alerting/common/routes/rule/apis/unmute_alert/types/v1.ts b/x-pack/plugins/alerting/common/routes/rule/apis/unmute_alert/types/v1.ts new file mode 100644 index 000000000000000..0de4e0e767cebfa --- /dev/null +++ b/x-pack/plugins/alerting/common/routes/rule/apis/unmute_alert/types/v1.ts @@ -0,0 +1,10 @@ +/* + * 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. + */ +import type { TypeOf } from '@kbn/config-schema'; +import { unmuteAlertParamsSchemaV1 } from '..'; + +export type UnmuteAlertRequestParams = TypeOf; diff --git a/x-pack/plugins/alerting/server/application/rule/methods/unmute_alert/schemas/index.ts b/x-pack/plugins/alerting/server/application/rule/methods/unmute_alert/schemas/index.ts new file mode 100644 index 000000000000000..7fc0a21218fcb7c --- /dev/null +++ b/x-pack/plugins/alerting/server/application/rule/methods/unmute_alert/schemas/index.ts @@ -0,0 +1,7 @@ +/* + * 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 { unmuteAlertParamsSchema } from './unmute_alert_params_schema'; diff --git a/x-pack/plugins/alerting/server/application/rule/methods/unmute_alert/schemas/unmute_alert_params_schema.ts b/x-pack/plugins/alerting/server/application/rule/methods/unmute_alert/schemas/unmute_alert_params_schema.ts new file mode 100644 index 000000000000000..edc85497ded29e2 --- /dev/null +++ b/x-pack/plugins/alerting/server/application/rule/methods/unmute_alert/schemas/unmute_alert_params_schema.ts @@ -0,0 +1,12 @@ +/* + * 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. + */ +import { schema } from '@kbn/config-schema'; + +export const unmuteAlertParamsSchema = schema.object({ + alertId: schema.string(), + alertInstanceId: schema.string(), +}); diff --git a/x-pack/plugins/alerting/server/application/rule/methods/unmute_alert/types/index.ts b/x-pack/plugins/alerting/server/application/rule/methods/unmute_alert/types/index.ts new file mode 100644 index 000000000000000..8d97bd968467c3c --- /dev/null +++ b/x-pack/plugins/alerting/server/application/rule/methods/unmute_alert/types/index.ts @@ -0,0 +1,8 @@ +/* + * 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 type { UnmuteAlertParams } from './unmute_alert_params'; diff --git a/x-pack/plugins/alerting/server/application/rule/methods/unmute_alert/types/unmute_alert_params.ts b/x-pack/plugins/alerting/server/application/rule/methods/unmute_alert/types/unmute_alert_params.ts new file mode 100644 index 000000000000000..ae83c5b6d4b7e77 --- /dev/null +++ b/x-pack/plugins/alerting/server/application/rule/methods/unmute_alert/types/unmute_alert_params.ts @@ -0,0 +1,11 @@ +/* + * 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. + */ + +import { TypeOf } from '@kbn/config-schema'; +import { unmuteAlertParamsSchema } from '../schemas'; + +export type UnmuteAlertParams = TypeOf; diff --git a/x-pack/plugins/alerting/server/rules_client/methods/unmute_instance.ts b/x-pack/plugins/alerting/server/application/rule/methods/unmute_alert/unmute_instance.ts similarity index 56% rename from x-pack/plugins/alerting/server/rules_client/methods/unmute_instance.ts rename to x-pack/plugins/alerting/server/application/rule/methods/unmute_alert/unmute_instance.ts index 0b8e422f1a94620..220a1b14e728c10 100644 --- a/x-pack/plugins/alerting/server/rules_client/methods/unmute_instance.ts +++ b/x-pack/plugins/alerting/server/application/rule/methods/unmute_alert/unmute_instance.ts @@ -5,39 +5,43 @@ * 2.0. */ -import { Rule, RawRule } from '../../types'; -import { WriteOperations, AlertingAuthorizationEntity } from '../../authorization'; -import { retryIfConflicts } from '../../lib/retry_if_conflicts'; -import { ruleAuditEvent, RuleAuditAction } from '../common/audit_events'; -import { MuteOptions } from '../types'; -import { RulesClientContext } from '../types'; -import { updateMeta } from '../lib'; -import { RULE_SAVED_OBJECT_TYPE } from '../../saved_objects'; +import Boom from '@hapi/boom'; +import type { Rule } from '../../../../types'; +import type { RulesClientContext } from '../../../../rules_client/types'; +import type { UnmuteAlertParams } from './types'; +import { retryIfConflicts } from '../../../../lib/retry_if_conflicts'; +import { RULE_SAVED_OBJECT_TYPE } from '../../../../saved_objects'; +import { ruleAuditEvent, RuleAuditAction } from '../../../../rules_client/common/audit_events'; +import { unmuteAlertParamsSchema } from './schemas'; +import { updateMeta } from '../../../../rules_client/lib'; +import { updateRuleSo } from '../../../../data/rule'; +import { WriteOperations, AlertingAuthorizationEntity } from '../../../../authorization'; export async function unmuteInstance( context: RulesClientContext, - { alertId, alertInstanceId }: MuteOptions + params: UnmuteAlertParams ): Promise { + const ruleId = params.alertId; + try { + unmuteAlertParamsSchema.validate(params); + } catch (error) { + throw Boom.badRequest(`Failed to validate params: ${error.message}`); + } + return await retryIfConflicts( context.logger, - `rulesClient.unmuteInstance('${alertId}')`, - async () => await unmuteInstanceWithOCC(context, { alertId, alertInstanceId }) + `rulesClient.unmuteInstance('${ruleId}')`, + async () => await unmuteInstanceWithOCC(context, params) ); } async function unmuteInstanceWithOCC( context: RulesClientContext, - { - alertId, - alertInstanceId, - }: { - alertId: string; - alertInstanceId: string; - } + { alertId: ruleId, alertInstanceId }: UnmuteAlertParams ) { const { attributes, version } = await context.unsecuredSavedObjectsClient.get( RULE_SAVED_OBJECT_TYPE, - alertId + ruleId ); try { @@ -54,7 +58,7 @@ async function unmuteInstanceWithOCC( context.auditLogger?.log( ruleAuditEvent({ action: RuleAuditAction.UNMUTE_ALERT, - savedObject: { type: RULE_SAVED_OBJECT_TYPE, id: alertId }, + savedObject: { type: RULE_SAVED_OBJECT_TYPE, id: ruleId }, error, }) ); @@ -65,7 +69,7 @@ async function unmuteInstanceWithOCC( ruleAuditEvent({ action: RuleAuditAction.UNMUTE_ALERT, outcome: 'unknown', - savedObject: { type: RULE_SAVED_OBJECT_TYPE, id: alertId }, + savedObject: { type: RULE_SAVED_OBJECT_TYPE, id: ruleId }, }) ); @@ -73,15 +77,15 @@ async function unmuteInstanceWithOCC( const mutedInstanceIds = attributes.mutedInstanceIds || []; if (!attributes.muteAll && mutedInstanceIds.includes(alertInstanceId)) { - await context.unsecuredSavedObjectsClient.update( - RULE_SAVED_OBJECT_TYPE, - alertId, - updateMeta(context, { + await updateRuleSo({ + savedObjectsClient: context.unsecuredSavedObjectsClient, + savedObjectsUpdateOptions: { version }, + id: ruleId, + updateRuleAttributes: updateMeta(context, { + mutedInstanceIds: mutedInstanceIds.filter((id: string) => id !== alertInstanceId), updatedBy: await context.getUserName(), updatedAt: new Date().toISOString(), - mutedInstanceIds: mutedInstanceIds.filter((id: string) => id !== alertInstanceId), }), - { version } - ); + }); } } diff --git a/x-pack/plugins/alerting/server/routes/index.ts b/x-pack/plugins/alerting/server/routes/index.ts index c695a0420c55b47..58ccbc33001c34d 100644 --- a/x-pack/plugins/alerting/server/routes/index.ts +++ b/x-pack/plugins/alerting/server/routes/index.ts @@ -34,7 +34,7 @@ import { ruleTypesRoute } from './rule_types'; import { muteAllRuleRoute } from './mute_all_rule'; import { muteAlertRoute } from './rule/apis/mute_alert/mute_alert'; import { unmuteAllRuleRoute } from './unmute_all_rule'; -import { unmuteAlertRoute } from './unmute_alert'; +import { unmuteAlertRoute } from './rule/apis/unmute_alert/unmute_alert'; import { updateRuleApiKeyRoute } from './update_rule_api_key'; import { bulkEditInternalRulesRoute } from './rule/apis/bulk_edit/bulk_edit_rules_route'; import { snoozeRuleRoute } from './rule/apis/snooze'; diff --git a/x-pack/plugins/alerting/server/routes/rule/apis/unmute_alert/transforms/index.ts b/x-pack/plugins/alerting/server/routes/rule/apis/unmute_alert/transforms/index.ts new file mode 100644 index 000000000000000..21a7250aed4e2ed --- /dev/null +++ b/x-pack/plugins/alerting/server/routes/rule/apis/unmute_alert/transforms/index.ts @@ -0,0 +1,8 @@ +/* + * 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 { transformRequestParamsToApplication } from './transform_request_params_to_application/latest'; +export { transformRequestParamsToApplication as transformRequestParamsToApplicationV1 } from './transform_request_params_to_application/v1'; diff --git a/x-pack/plugins/alerting/server/routes/rule/apis/unmute_alert/transforms/transform_request_params_to_application/latest.ts b/x-pack/plugins/alerting/server/routes/rule/apis/unmute_alert/transforms/transform_request_params_to_application/latest.ts new file mode 100644 index 000000000000000..5983069f0d8fd27 --- /dev/null +++ b/x-pack/plugins/alerting/server/routes/rule/apis/unmute_alert/transforms/transform_request_params_to_application/latest.ts @@ -0,0 +1,8 @@ +/* + * 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 { transformRequestParamsToApplication } from './v1'; diff --git a/x-pack/plugins/alerting/server/routes/rule/apis/unmute_alert/transforms/transform_request_params_to_application/v1.ts b/x-pack/plugins/alerting/server/routes/rule/apis/unmute_alert/transforms/transform_request_params_to_application/v1.ts new file mode 100644 index 000000000000000..956ebe9085cc8de --- /dev/null +++ b/x-pack/plugins/alerting/server/routes/rule/apis/unmute_alert/transforms/transform_request_params_to_application/v1.ts @@ -0,0 +1,17 @@ +/* + * 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. + */ + +import { UnmuteAlertParams } from '../../../../../../application/rule/methods/unmute_alert/types'; +import { RewriteRequestCase } from '../../../../../lib'; + +export const transformRequestParamsToApplication: RewriteRequestCase = ({ + rule_id: alertId, + alert_id: alertInstanceId, +}) => ({ + alertId, + alertInstanceId, +}); diff --git a/x-pack/plugins/alerting/server/routes/unmute_alert.test.ts b/x-pack/plugins/alerting/server/routes/rule/apis/unmute_alert/unmute_alert.test.ts similarity index 86% rename from x-pack/plugins/alerting/server/routes/unmute_alert.test.ts rename to x-pack/plugins/alerting/server/routes/rule/apis/unmute_alert/unmute_alert.test.ts index 6f9c553831e77dd..12828e81c6b928f 100644 --- a/x-pack/plugins/alerting/server/routes/unmute_alert.test.ts +++ b/x-pack/plugins/alerting/server/routes/rule/apis/unmute_alert/unmute_alert.test.ts @@ -7,13 +7,13 @@ import { unmuteAlertRoute } from './unmute_alert'; import { httpServiceMock } from '@kbn/core/server/mocks'; -import { licenseStateMock } from '../lib/license_state.mock'; -import { mockHandlerArguments } from './_mock_handler_arguments'; -import { rulesClientMock } from '../rules_client.mock'; -import { RuleTypeDisabledError } from '../lib/errors/rule_type_disabled'; +import { licenseStateMock } from '../../../../lib/license_state.mock'; +import { mockHandlerArguments } from '../../../_mock_handler_arguments'; +import { rulesClientMock } from '../../../../rules_client.mock'; +import { RuleTypeDisabledError } from '../../../../lib/errors/rule_type_disabled'; const rulesClient = rulesClientMock.create(); -jest.mock('../lib/license_api_access', () => ({ +jest.mock('../../../../lib/license_api_access', () => ({ verifyApiAccess: jest.fn(), })); diff --git a/x-pack/plugins/alerting/server/routes/unmute_alert.ts b/x-pack/plugins/alerting/server/routes/rule/apis/unmute_alert/unmute_alert.ts similarity index 64% rename from x-pack/plugins/alerting/server/routes/unmute_alert.ts rename to x-pack/plugins/alerting/server/routes/rule/apis/unmute_alert/unmute_alert.ts index b0d124e0d3945ae..871200882c426e9 100644 --- a/x-pack/plugins/alerting/server/routes/unmute_alert.ts +++ b/x-pack/plugins/alerting/server/routes/rule/apis/unmute_alert/unmute_alert.ts @@ -6,24 +6,14 @@ */ import { IRouter } from '@kbn/core/server'; -import { schema } from '@kbn/config-schema'; -import { ILicenseState, RuleTypeDisabledError } from '../lib'; -import { MuteOptions } from '../rules_client'; -import { RewriteRequestCase, verifyAccessAndContext } from './lib'; -import { AlertingRequestHandlerContext, BASE_ALERTING_API_PATH } from '../types'; - -const paramSchema = schema.object({ - rule_id: schema.string(), - alert_id: schema.string(), -}); - -const rewriteParamsReq: RewriteRequestCase = ({ - rule_id: alertId, - alert_id: alertInstanceId, -}) => ({ - alertId, - alertInstanceId, -}); +import { ILicenseState, RuleTypeDisabledError } from '../../../../lib'; +import { verifyAccessAndContext } from '../../../lib'; +import { AlertingRequestHandlerContext, BASE_ALERTING_API_PATH } from '../../../../types'; +import { + unmuteAlertParamsSchemaV1, + UnmuteAlertRequestParamsV1, +} from '../../../../../common/routes/rule/apis/unmute_alert'; +import { transformRequestParamsToApplicationV1 } from './transforms'; export const unmuteAlertRoute = ( router: IRouter, @@ -37,15 +27,17 @@ export const unmuteAlertRoute = ( summary: `Unmute an alert`, }, validate: { - params: paramSchema, + request: { + params: unmuteAlertParamsSchemaV1, + }, }, }, router.handleLegacyErrors( verifyAccessAndContext(licenseState, async function (context, req, res) { const rulesClient = (await context.alerting).getRulesClient(); - const params = rewriteParamsReq(req.params); + const params: UnmuteAlertRequestParamsV1 = req.params; try { - await rulesClient.unmuteInstance(params); + await rulesClient.unmuteInstance(transformRequestParamsToApplicationV1(params)); return res.noContent(); } catch (e) { if (e instanceof RuleTypeDisabledError) { 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 bc5918875c19339..34b1d3f669df9ec 100644 --- a/x-pack/plugins/alerting/server/rules_client/rules_client.ts +++ b/x-pack/plugins/alerting/server/rules_client/rules_client.ts @@ -60,7 +60,7 @@ import { clearExpiredSnoozes } from './methods/clear_expired_snoozes'; import { muteInstance } from '../application/rule/methods/mute_alert/mute_instance'; import { muteAll } from './methods/mute_all'; import { unmuteAll } from './methods/unmute_all'; -import { unmuteInstance } from './methods/unmute_instance'; +import { unmuteInstance } from '../application/rule/methods/unmute_alert/unmute_instance'; import { runSoon } from './methods/run_soon'; import { listRuleTypes } from './methods/list_rule_types'; import { getAlertFromRaw, GetAlertFromRawParams } from './lib/get_alert_from_raw';