diff --git a/x-pack/plugins/alerting/common/routes/rule/apis/list_types/index.ts b/x-pack/plugins/alerting/common/routes/rule/apis/list_types/index.ts new file mode 100644 index 0000000000000..daac46c1381d6 --- /dev/null +++ b/x-pack/plugins/alerting/common/routes/rule/apis/list_types/index.ts @@ -0,0 +1,18 @@ +/* + * 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 { typesRulesResponseSchema, typesRulesResponseBodySchema } from './schemas/latest'; +export type { TypesRulesResponse, TypesRulesResponseBody } from './types/latest'; + +export { + typesRulesResponseSchema as typesRulesResponseSchemaV1, + typesRulesResponseBodySchema as typesRulesResponseBodySchemaV1, +} from './schemas/v1'; +export type { + TypesRulesResponse as TypesRulesResponseV1, + TypesRulesResponseBody as TypesRulesResponseBodyV1, +} from './types/v1'; diff --git a/x-pack/plugins/alerting/common/routes/rule/apis/list_types/schemas/latest.ts b/x-pack/plugins/alerting/common/routes/rule/apis/list_types/schemas/latest.ts new file mode 100644 index 0000000000000..25300c97a6d2e --- /dev/null +++ b/x-pack/plugins/alerting/common/routes/rule/apis/list_types/schemas/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 * from './v1'; diff --git a/x-pack/plugins/alerting/common/routes/rule/apis/list_types/schemas/v1.ts b/x-pack/plugins/alerting/common/routes/rule/apis/list_types/schemas/v1.ts new file mode 100644 index 0000000000000..bc38ef051ed90 --- /dev/null +++ b/x-pack/plugins/alerting/common/routes/rule/apis/list_types/schemas/v1.ts @@ -0,0 +1,75 @@ +/* + * 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'; + +const actionVariableSchema = schema.object({ + name: schema.string(), + description: schema.string(), + usesPublicBaseUrl: schema.maybe(schema.boolean()), +}); + +const actionGroupSchema = schema.object({ + id: schema.string(), + name: schema.string(), +}); + +export const typesRulesResponseBodySchema = schema.arrayOf( + schema.object({ + action_groups: schema.maybe(schema.arrayOf(actionGroupSchema)), + action_variables: schema.maybe( + schema.object({ + context: schema.maybe(schema.arrayOf(actionVariableSchema)), + state: schema.maybe(schema.arrayOf(actionVariableSchema)), + params: schema.maybe(schema.arrayOf(actionVariableSchema)), + }) + ), + alerts: schema.maybe( + schema.object({ + context: schema.string(), + mappings: schema.maybe( + schema.object({ + dynamic: schema.maybe(schema.oneOf([schema.literal(false), schema.literal('strict')])), + fieldMap: schema.recordOf(schema.string(), schema.any()), + shouldWrite: schema.maybe(schema.boolean()), + useEcs: schema.maybe(schema.boolean()), + }) + ), + }) + ), + authorized_consumers: schema.recordOf( + schema.string(), + schema.object({ read: schema.boolean(), all: schema.boolean() }) + ), + category: schema.string(), + default_action_group_id: schema.string(), + default_schedule_interval: schema.maybe(schema.string()), + does_set_recovery_context: schema.maybe(schema.boolean()), + enabled_in_license: schema.boolean(), + fieldsForAAD: schema.maybe(schema.arrayOf(schema.string())), + has_alerts_mappings: schema.boolean(), + has_fields_for_a_a_d: schema.boolean(), + id: schema.string(), + is_exportable: schema.boolean(), + minimum_license_required: schema.oneOf([ + schema.literal('basic'), + schema.literal('gold'), + schema.literal('platinum'), + schema.literal('standard'), + schema.literal('enterprise'), + schema.literal('trial'), + ]), + name: schema.string(), + producer: schema.string(), + recovery_action_group: actionGroupSchema, + rule_task_timeout: schema.maybe(schema.string()), + }) +); + +export const typesRulesResponseSchema = schema.object({ + body: typesRulesResponseBodySchema, +}); diff --git a/x-pack/plugins/alerting/common/routes/rule/apis/list_types/types/latest.ts b/x-pack/plugins/alerting/common/routes/rule/apis/list_types/types/latest.ts new file mode 100644 index 0000000000000..25300c97a6d2e --- /dev/null +++ b/x-pack/plugins/alerting/common/routes/rule/apis/list_types/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 * from './v1'; diff --git a/x-pack/plugins/alerting/common/routes/rule/apis/list_types/types/v1.ts b/x-pack/plugins/alerting/common/routes/rule/apis/list_types/types/v1.ts new file mode 100644 index 0000000000000..380b48c1c6615 --- /dev/null +++ b/x-pack/plugins/alerting/common/routes/rule/apis/list_types/types/v1.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. + */ + +import type { TypeOf } from '@kbn/config-schema'; + +import { typesRulesResponseSchemaV1, typesRulesResponseBodySchemaV1 } from '..'; + +export type TypesRulesResponse = TypeOf; +export type TypesRulesResponseBody = TypeOf; diff --git a/x-pack/plugins/alerting/server/rules_client/methods/list_rule_types.ts b/x-pack/plugins/alerting/server/application/rule/methods/rule_types/rule_types.ts similarity index 74% rename from x-pack/plugins/alerting/server/rules_client/methods/list_rule_types.ts rename to x-pack/plugins/alerting/server/application/rule/methods/rule_types/rule_types.ts index 51f5b8f8af7ad..66256b4b7d7eb 100644 --- a/x-pack/plugins/alerting/server/rules_client/methods/list_rule_types.ts +++ b/x-pack/plugins/alerting/server/application/rule/methods/rule_types/rule_types.ts @@ -5,8 +5,12 @@ * 2.0. */ -import { WriteOperations, ReadOperations, AlertingAuthorizationEntity } from '../../authorization'; -import { RulesClientContext } from '../types'; +import { + WriteOperations, + ReadOperations, + AlertingAuthorizationEntity, +} from '../../../../authorization'; +import { RulesClientContext } from '../../../../rules_client/types'; export async function listRuleTypes(context: RulesClientContext) { return await context.authorization.filterByRuleTypeAuthorization( diff --git a/x-pack/plugins/alerting/server/routes/index.ts b/x-pack/plugins/alerting/server/routes/index.ts index c9ecb0b0ac17c..eee0382dd834c 100644 --- a/x-pack/plugins/alerting/server/routes/index.ts +++ b/x-pack/plugins/alerting/server/routes/index.ts @@ -30,7 +30,7 @@ import { getRuleExecutionKPIRoute } from './get_rule_execution_kpi'; import { getRuleStateRoute } from './get_rule_state'; import { healthRoute } from './health'; import { resolveRuleRoute } from './rule/apis/resolve'; -import { ruleTypesRoute } from './rule_types'; +import { ruleTypesRoute } from './rule/apis/list_types/rule_types'; import { muteAllRuleRoute } from './rule/apis/mute_all/mute_all_rule'; import { muteAlertRoute } from './rule/apis/mute_alert/mute_alert'; import { unmuteAllRuleRoute } from './rule/apis/unmute_all'; diff --git a/x-pack/plugins/alerting/server/routes/rule/apis/list_types/index.ts b/x-pack/plugins/alerting/server/routes/rule/apis/list_types/index.ts new file mode 100644 index 0000000000000..01f4d106a62ba --- /dev/null +++ b/x-pack/plugins/alerting/server/routes/rule/apis/list_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 { ruleTypesRoute } from './rule_types'; diff --git a/x-pack/plugins/alerting/server/routes/rule_types.test.ts b/x-pack/plugins/alerting/server/routes/rule/apis/list_types/rule_types.test.ts similarity index 93% rename from x-pack/plugins/alerting/server/routes/rule_types.test.ts rename to x-pack/plugins/alerting/server/routes/rule/apis/list_types/rule_types.test.ts index a6483f15f9f1c..e6293a589743b 100644 --- a/x-pack/plugins/alerting/server/routes/rule_types.test.ts +++ b/x-pack/plugins/alerting/server/routes/rule/apis/list_types/rule_types.test.ts @@ -7,17 +7,17 @@ import { ruleTypesRoute } from './rule_types'; import { httpServiceMock } from '@kbn/core/server/mocks'; -import { licenseStateMock } from '../lib/license_state.mock'; -import { verifyApiAccess } from '../lib/license_api_access'; -import { mockHandlerArguments } from './_mock_handler_arguments'; -import { rulesClientMock } from '../rules_client.mock'; -import { RecoveredActionGroup } from '../../common'; -import { RegistryAlertTypeWithAuth } from '../authorization'; -import { AsApiContract } from './lib'; +import { licenseStateMock } from '../../../../lib/license_state.mock'; +import { verifyApiAccess } from '../../../../lib/license_api_access'; +import { mockHandlerArguments } from '../../../_mock_handler_arguments'; +import { rulesClientMock } from '../../../../rules_client.mock'; +import { RecoveredActionGroup } from '../../../../../common'; +import { RegistryAlertTypeWithAuth } from '../../../../authorization'; +import { AsApiContract } from '../../../lib'; 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/rule/apis/list_types/rule_types.ts b/x-pack/plugins/alerting/server/routes/rule/apis/list_types/rule_types.ts new file mode 100644 index 0000000000000..d6f2ffbe9af0c --- /dev/null +++ b/x-pack/plugins/alerting/server/routes/rule/apis/list_types/rule_types.ts @@ -0,0 +1,42 @@ +/* + * 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 { IRouter } from '@kbn/core/server'; +import { TypesRulesResponseBodyV1 } from '../../../../../common/routes/rule/apis/list_types'; +import { ILicenseState } from '../../../../lib'; +import { verifyAccessAndContext } from '../../../lib'; +import { AlertingRequestHandlerContext, BASE_ALERTING_API_PATH } from '../../../../types'; +import { transformRuleTypesResponseV1 } from './transforms'; + +export const ruleTypesRoute = ( + router: IRouter, + licenseState: ILicenseState +) => { + router.get( + { + path: `${BASE_ALERTING_API_PATH}/rule_types`, + options: { + access: 'public', + summary: `Get the rule types`, + tags: ['oas-tag:alerting'], + }, + validate: {}, + }, + router.handleLegacyErrors( + verifyAccessAndContext(licenseState, async function (context, req, res) { + const rulesClient = (await context.alerting).getRulesClient(); + const ruleTypes = await rulesClient.listRuleTypes(); + + const responseBody: TypesRulesResponseBodyV1 = transformRuleTypesResponseV1(ruleTypes); + + return res.ok({ + body: responseBody, + }); + }) + ) + ); +}; diff --git a/x-pack/plugins/alerting/server/routes/rule/apis/list_types/transforms/index.ts b/x-pack/plugins/alerting/server/routes/rule/apis/list_types/transforms/index.ts new file mode 100644 index 0000000000000..ac825ed771c48 --- /dev/null +++ b/x-pack/plugins/alerting/server/routes/rule/apis/list_types/transforms/index.ts @@ -0,0 +1,9 @@ +/* + * 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 { transformRuleTypesResponse } from './transform_rule_types_response/latest'; +export { transformRuleTypesResponse as transformRuleTypesResponseV1 } from './transform_rule_types_response/v1'; diff --git a/x-pack/plugins/alerting/server/routes/rule/apis/list_types/transforms/transform_rule_types_response/latest.ts b/x-pack/plugins/alerting/server/routes/rule/apis/list_types/transforms/transform_rule_types_response/latest.ts new file mode 100644 index 0000000000000..25300c97a6d2e --- /dev/null +++ b/x-pack/plugins/alerting/server/routes/rule/apis/list_types/transforms/transform_rule_types_response/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 * from './v1'; diff --git a/x-pack/plugins/alerting/server/routes/rule/apis/list_types/transforms/transform_rule_types_response/v1.ts b/x-pack/plugins/alerting/server/routes/rule/apis/list_types/transforms/transform_rule_types_response/v1.ts new file mode 100644 index 0000000000000..54a5874331c86 --- /dev/null +++ b/x-pack/plugins/alerting/server/routes/rule/apis/list_types/transforms/transform_rule_types_response/v1.ts @@ -0,0 +1,42 @@ +/* + * 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 { isBoolean } from 'lodash/fp'; +import { RegistryAlertTypeWithAuth } from '../../../../../../authorization'; +import type { TypesRulesResponseBodyV1 } from '../../../../../../../common/routes/rule/apis/list_types'; + +export const transformRuleTypesResponse = ( + ruleTypes: Set +): TypesRulesResponseBodyV1 => { + return Array.from(ruleTypes).map((ruleType: RegistryAlertTypeWithAuth) => { + return { + ...(ruleType.actionGroups ? { action_groups: ruleType.actionGroups } : {}), + ...(ruleType.actionVariables ? { action_variables: ruleType.actionVariables } : {}), + ...(ruleType.alerts ? { alerts: ruleType.alerts } : {}), + authorized_consumers: ruleType.authorizedConsumers, + category: ruleType.category, + default_action_group_id: ruleType.defaultActionGroupId, + ...(ruleType.defaultScheduleInterval + ? { default_schedule_interval: ruleType.defaultScheduleInterval } + : {}), + ...(isBoolean(ruleType.doesSetRecoveryContext) + ? { does_set_recovery_context: ruleType.doesSetRecoveryContext } + : {}), + enabled_in_license: ruleType.enabledInLicense, + ...(ruleType.fieldsForAAD ? { fieldsForAAD: ruleType.fieldsForAAD } : {}), + has_alerts_mappings: ruleType.hasAlertsMappings, + has_fields_for_a_a_d: ruleType.hasFieldsForAAD, + id: ruleType.id, + is_exportable: ruleType.isExportable, + minimum_license_required: ruleType.minimumLicenseRequired, + name: ruleType.name, + producer: ruleType.producer, + recovery_action_group: ruleType.recoveryActionGroup, + ...(ruleType.ruleTaskTimeout ? { rule_task_timeout: ruleType.ruleTaskTimeout } : {}), + }; + }); +}; diff --git a/x-pack/plugins/alerting/server/routes/rule_types.ts b/x-pack/plugins/alerting/server/routes/rule_types.ts deleted file mode 100644 index afbe0c42696bd..0000000000000 --- a/x-pack/plugins/alerting/server/routes/rule_types.ts +++ /dev/null @@ -1,75 +0,0 @@ -/* - * 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 { IRouter } from '@kbn/core/server'; -import { ILicenseState } from '../lib'; -import { RegistryAlertTypeWithAuth } from '../authorization'; -import { verifyAccessAndContext } from './lib'; -import { AlertingRequestHandlerContext, BASE_ALERTING_API_PATH } from '../types'; - -const rewriteBodyRes = (results: RegistryAlertTypeWithAuth[]) => { - return results.map( - ({ - enabledInLicense, - recoveryActionGroup, - actionGroups, - defaultActionGroupId, - minimumLicenseRequired, - isExportable, - ruleTaskTimeout, - actionVariables, - authorizedConsumers, - defaultScheduleInterval, - doesSetRecoveryContext, - hasAlertsMappings, - hasFieldsForAAD, - validLegacyConsumers, - ...rest - }: RegistryAlertTypeWithAuth) => ({ - ...rest, - enabled_in_license: enabledInLicense, - recovery_action_group: recoveryActionGroup, - action_groups: actionGroups, - default_action_group_id: defaultActionGroupId, - minimum_license_required: minimumLicenseRequired, - is_exportable: isExportable, - rule_task_timeout: ruleTaskTimeout, - action_variables: actionVariables, - authorized_consumers: authorizedConsumers, - default_schedule_interval: defaultScheduleInterval, - does_set_recovery_context: doesSetRecoveryContext, - has_alerts_mappings: !!hasAlertsMappings, - has_fields_for_a_a_d: !!hasFieldsForAAD, - }) - ); -}; - -export const ruleTypesRoute = ( - router: IRouter, - licenseState: ILicenseState -) => { - router.get( - { - path: `${BASE_ALERTING_API_PATH}/rule_types`, - options: { - access: 'public', - summary: `Get the rule types`, - tags: ['oas-tag:alerting'], - }, - validate: {}, - }, - router.handleLegacyErrors( - verifyAccessAndContext(licenseState, async function (context, req, res) { - const rulesClient = (await context.alerting).getRulesClient(); - const ruleTypes = Array.from(await rulesClient.listRuleTypes()); - return res.ok({ - body: rewriteBodyRes(ruleTypes), - }); - }) - ) - ); -}; diff --git a/x-pack/plugins/alerting/server/rule_type_registry.ts b/x-pack/plugins/alerting/server/rule_type_registry.ts index bc7a10d767ff0..7562942f0262d 100644 --- a/x-pack/plugins/alerting/server/rule_type_registry.ts +++ b/x-pack/plugins/alerting/server/rule_type_registry.ts @@ -72,6 +72,7 @@ export interface RegistryRuleType | 'defaultScheduleInterval' | 'doesSetRecoveryContext' | 'fieldsForAAD' + | 'alerts' > { id: string; enabledInLicense: boolean; 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 c889587469cea..4c86469f11a29 100644 --- a/x-pack/plugins/alerting/server/rules_client/rules_client.ts +++ b/x-pack/plugins/alerting/server/rules_client/rules_client.ts @@ -62,7 +62,7 @@ import { unmuteAll } from '../application/rule/methods/unmute_all'; import { muteAll } from '../application/rule/methods/mute_all'; import { unmuteInstance } from '../application/rule/methods/unmute_alert/unmute_instance'; import { runSoon } from './methods/run_soon'; -import { listRuleTypes } from './methods/list_rule_types'; +import { listRuleTypes } from '../application/rule/methods/rule_types/rule_types'; import { getScheduleFrequency } from '../application/rule/methods/get_schedule_frequency/get_schedule_frequency'; import { bulkUntrackAlerts,