From 44d72649a356026a58dcf280487cf2b67c5255df Mon Sep 17 00:00:00 2001 From: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Date: Sat, 19 Oct 2024 02:11:31 +1100 Subject: [PATCH] [8.x] [Response Ops][Alerting] HTTP Version health API (#196690) (#196878) # Backport This will backport the following commits from `main` to `8.x`: - [[Response Ops][Alerting] HTTP Version health API (#196690)](https://github.com/elastic/kibana/pull/196690) ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) Co-authored-by: Julian Gernun <17549662+jcger@users.noreply.github.com> --- .../routes/framework/apis/health/index.ts | 15 ++++++ .../framework/apis/health/schemas/latest.ts | 8 +++ .../framework/apis/health/schemas/v1.ts | 49 +++++++++++++++++++ .../framework/apis/health/types/latest.ts | 8 +++ .../routes/framework/apis/health/types/v1.ts | 13 +++++ .../apis/health}/health.test.ts | 16 +++--- .../{ => framework/apis/health}/health.ts | 41 +++++++--------- .../routes/framework/apis/health/index.ts | 8 +++ .../framework/apis/health/transforms/index.ts | 9 ++++ .../transform_health_response/latest.ts | 8 +++ .../transform_health_response/v1.ts | 21 ++++++++ .../plugins/alerting/server/routes/index.ts | 2 +- 12 files changed, 167 insertions(+), 31 deletions(-) create mode 100644 x-pack/plugins/alerting/common/routes/framework/apis/health/index.ts create mode 100644 x-pack/plugins/alerting/common/routes/framework/apis/health/schemas/latest.ts create mode 100644 x-pack/plugins/alerting/common/routes/framework/apis/health/schemas/v1.ts create mode 100644 x-pack/plugins/alerting/common/routes/framework/apis/health/types/latest.ts create mode 100644 x-pack/plugins/alerting/common/routes/framework/apis/health/types/v1.ts rename x-pack/plugins/alerting/server/routes/{ => framework/apis/health}/health.test.ts (95%) rename x-pack/plugins/alerting/server/routes/{ => framework/apis/health}/health.ts (72%) create mode 100644 x-pack/plugins/alerting/server/routes/framework/apis/health/index.ts create mode 100644 x-pack/plugins/alerting/server/routes/framework/apis/health/transforms/index.ts create mode 100644 x-pack/plugins/alerting/server/routes/framework/apis/health/transforms/transform_health_response/latest.ts create mode 100644 x-pack/plugins/alerting/server/routes/framework/apis/health/transforms/transform_health_response/v1.ts diff --git a/x-pack/plugins/alerting/common/routes/framework/apis/health/index.ts b/x-pack/plugins/alerting/common/routes/framework/apis/health/index.ts new file mode 100644 index 0000000000000..1cde6fe4d90ce --- /dev/null +++ b/x-pack/plugins/alerting/common/routes/framework/apis/health/index.ts @@ -0,0 +1,15 @@ +/* + * 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 { healthFrameworkResponseSchema } from './schemas/latest'; +export type { HealthFrameworkResponse } from './types/latest'; + +export { + healthFrameworkResponseBodySchema as healthFrameworkResponseBodySchemaV1, + healthFrameworkResponseSchema as healthFrameworkResponseSchemaV1, +} from './schemas/v1'; +export type { HealthFrameworkResponseBody as HealthFrameworkResponseBodyV1 } from './types/v1'; diff --git a/x-pack/plugins/alerting/common/routes/framework/apis/health/schemas/latest.ts b/x-pack/plugins/alerting/common/routes/framework/apis/health/schemas/latest.ts new file mode 100644 index 0000000000000..25300c97a6d2e --- /dev/null +++ b/x-pack/plugins/alerting/common/routes/framework/apis/health/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/framework/apis/health/schemas/v1.ts b/x-pack/plugins/alerting/common/routes/framework/apis/health/schemas/v1.ts new file mode 100644 index 0000000000000..c97dc4d3cc72d --- /dev/null +++ b/x-pack/plugins/alerting/common/routes/framework/apis/health/schemas/v1.ts @@ -0,0 +1,49 @@ +/* + * 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 alertingFrameworkHealthSchemaObject = { + status: schema.oneOf([schema.literal('ok'), schema.literal('warn'), schema.literal('error')]), + timestamp: schema.string(), +}; + +export const healthFrameworkResponseBodySchema = schema.object({ + is_sufficiently_secure: schema.boolean({ + meta: { + description: 'If false, security is enabled but TLS is not.', + }, + }), + has_permanent_encryption_key: schema.boolean({ + meta: { + description: 'If false, the encryption key is not set', + }, + }), + alerting_framework_health: schema.object( + { + decryption_health: schema.object(alertingFrameworkHealthSchemaObject, { + meta: { description: 'The timestamp and status of the alert decryption.' }, + }), + execution_health: schema.object(alertingFrameworkHealthSchemaObject, { + meta: { description: 'The timestamp and status of the alert execution.' }, + }), + read_health: schema.object(alertingFrameworkHealthSchemaObject, { + meta: { description: 'The timestamp and status of the alert reading events.' }, + }), + }, + { + meta: { + description: + 'Three substates identify the health of the alerting framework: decryptionHealth, executionHealth, and readHealth.', + }, + } + ), +}); + +export const healthFrameworkResponseSchema = schema.object({ + body: healthFrameworkResponseBodySchema, +}); diff --git a/x-pack/plugins/alerting/common/routes/framework/apis/health/types/latest.ts b/x-pack/plugins/alerting/common/routes/framework/apis/health/types/latest.ts new file mode 100644 index 0000000000000..25300c97a6d2e --- /dev/null +++ b/x-pack/plugins/alerting/common/routes/framework/apis/health/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/framework/apis/health/types/v1.ts b/x-pack/plugins/alerting/common/routes/framework/apis/health/types/v1.ts new file mode 100644 index 0000000000000..188e3d0816a54 --- /dev/null +++ b/x-pack/plugins/alerting/common/routes/framework/apis/health/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 { healthFrameworkResponseBodySchemaV1, healthFrameworkResponseSchemaV1 } from '..'; + +export type HealthFrameworkResponseBody = TypeOf; +export type HealthFrameworkResponse = TypeOf; diff --git a/x-pack/plugins/alerting/server/routes/health.test.ts b/x-pack/plugins/alerting/server/routes/framework/apis/health/health.test.ts similarity index 95% rename from x-pack/plugins/alerting/server/routes/health.test.ts rename to x-pack/plugins/alerting/server/routes/framework/apis/health/health.test.ts index 28117eaeeb55b..f726073ed0844 100644 --- a/x-pack/plugins/alerting/server/routes/health.test.ts +++ b/x-pack/plugins/alerting/server/routes/framework/apis/health/health.test.ts @@ -8,18 +8,18 @@ import { healthRoute } from './health'; import { httpServiceMock } from '@kbn/core/server/mocks'; import { HealthStatus } from '@kbn/alerting-types'; -import { mockHandlerArguments } from './_mock_handler_arguments'; -import { verifyApiAccess } from '../lib/license_api_access'; -import { licenseStateMock } from '../lib/license_state.mock'; +import { mockHandlerArguments } from '../../../_mock_handler_arguments'; +import { verifyApiAccess } from '../../../../lib/license_api_access'; +import { licenseStateMock } from '../../../../lib/license_state.mock'; import { encryptedSavedObjectsMock } from '@kbn/encrypted-saved-objects-plugin/server/mocks'; -import { rulesClientMock } from '../rules_client.mock'; -import { alertsMock } from '../mocks'; -import { RecoveredActionGroup } from '../../common'; -import { RegistryAlertTypeWithAuth } from '../authorization'; +import { rulesClientMock } from '../../../../rules_client.mock'; +import { alertsMock } from '../../../../mocks'; +import { RecoveredActionGroup } from '../../../../../common'; +import { RegistryAlertTypeWithAuth } from '../../../../authorization'; 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/health.ts b/x-pack/plugins/alerting/server/routes/framework/apis/health/health.ts similarity index 72% rename from x-pack/plugins/alerting/server/routes/health.ts rename to x-pack/plugins/alerting/server/routes/framework/apis/health/health.ts index 478f0afda594a..34ef56a51daaa 100644 --- a/x-pack/plugins/alerting/server/routes/health.ts +++ b/x-pack/plugins/alerting/server/routes/framework/apis/health/health.ts @@ -7,30 +7,16 @@ import { IRouter } from '@kbn/core/server'; import { EncryptedSavedObjectsPluginSetup } from '@kbn/encrypted-saved-objects-plugin/server'; -import { ILicenseState } from '../lib'; -import { RewriteResponseCase, verifyAccessAndContext } from './lib'; +import { healthFrameworkResponseSchemaV1 } from '../../../../../common/routes/framework/apis/health'; +import { ILicenseState } from '../../../../lib'; +import { verifyAccessAndContext } from '../../../lib'; import { AlertingRequestHandlerContext, BASE_ALERTING_API_PATH, AlertingFrameworkHealth, -} from '../types'; -import { getSecurityHealth } from '../lib/get_security_health'; - -const rewriteBodyRes: RewriteResponseCase = ({ - isSufficientlySecure, - hasPermanentEncryptionKey, - alertingFrameworkHealth, - ...rest -}) => ({ - ...rest, - is_sufficiently_secure: isSufficientlySecure, - has_permanent_encryption_key: hasPermanentEncryptionKey, - alerting_framework_health: { - decryption_health: alertingFrameworkHealth.decryptionHealth, - execution_health: alertingFrameworkHealth.executionHealth, - read_health: alertingFrameworkHealth.readHealth, - }, -}); +} from '../../../../types'; +import { getSecurityHealth } from '../../../../lib/get_security_health'; +import { transformHealthBodyResponse } from './transforms/transform_health_response/v1'; export const healthRoute = ( router: IRouter, @@ -45,7 +31,18 @@ export const healthRoute = ( summary: `Get the alerting framework health`, tags: ['oas-tag:alerting'], }, - validate: false, + validate: { + request: {}, + response: { + 200: { + body: () => healthFrameworkResponseSchemaV1, + description: 'Indicates a successful call.', + }, + 401: { + description: 'Authorization information is missing or invalid.', + }, + }, + }, }, router.handleLegacyErrors( verifyAccessAndContext(licenseState, async function (context, req, res) { @@ -68,7 +65,7 @@ export const healthRoute = ( }; return res.ok({ - body: rewriteBodyRes(frameworkHealth), + body: transformHealthBodyResponse(frameworkHealth), }); } else { return res.forbidden({ diff --git a/x-pack/plugins/alerting/server/routes/framework/apis/health/index.ts b/x-pack/plugins/alerting/server/routes/framework/apis/health/index.ts new file mode 100644 index 0000000000000..a3b39bc8eb752 --- /dev/null +++ b/x-pack/plugins/alerting/server/routes/framework/apis/health/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 { healthRoute } from './health'; diff --git a/x-pack/plugins/alerting/server/routes/framework/apis/health/transforms/index.ts b/x-pack/plugins/alerting/server/routes/framework/apis/health/transforms/index.ts new file mode 100644 index 0000000000000..7e16df7405434 --- /dev/null +++ b/x-pack/plugins/alerting/server/routes/framework/apis/health/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 { transformHealthBodyResponse } from './transform_health_response/latest'; +export { transformHealthBodyResponse as transformHealthBodyResponseV1 } from './transform_health_response/v1'; diff --git a/x-pack/plugins/alerting/server/routes/framework/apis/health/transforms/transform_health_response/latest.ts b/x-pack/plugins/alerting/server/routes/framework/apis/health/transforms/transform_health_response/latest.ts new file mode 100644 index 0000000000000..25300c97a6d2e --- /dev/null +++ b/x-pack/plugins/alerting/server/routes/framework/apis/health/transforms/transform_health_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/framework/apis/health/transforms/transform_health_response/v1.ts b/x-pack/plugins/alerting/server/routes/framework/apis/health/transforms/transform_health_response/v1.ts new file mode 100644 index 0000000000000..76c4a46aee5d1 --- /dev/null +++ b/x-pack/plugins/alerting/server/routes/framework/apis/health/transforms/transform_health_response/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 { AlertingFrameworkHealth } from '../../../../../../types'; +import type { HealthFrameworkResponseBodyV1 } from '../../../../../../../common/routes/framework/apis/health'; + +export const transformHealthBodyResponse = ( + frameworkHealth: AlertingFrameworkHealth +): HealthFrameworkResponseBodyV1 => ({ + is_sufficiently_secure: frameworkHealth.isSufficientlySecure, + has_permanent_encryption_key: frameworkHealth.hasPermanentEncryptionKey, + alerting_framework_health: { + decryption_health: frameworkHealth.alertingFrameworkHealth.decryptionHealth, + execution_health: frameworkHealth.alertingFrameworkHealth.executionHealth, + read_health: frameworkHealth.alertingFrameworkHealth.readHealth, + }, +}); diff --git a/x-pack/plugins/alerting/server/routes/index.ts b/x-pack/plugins/alerting/server/routes/index.ts index eee0382dd834c..97fdf8c90f8d6 100644 --- a/x-pack/plugins/alerting/server/routes/index.ts +++ b/x-pack/plugins/alerting/server/routes/index.ts @@ -28,7 +28,7 @@ import { getGlobalExecutionKPIRoute } from './get_global_execution_kpi'; import { getActionErrorLogRoute } from './get_action_error_log'; import { getRuleExecutionKPIRoute } from './get_rule_execution_kpi'; import { getRuleStateRoute } from './get_rule_state'; -import { healthRoute } from './health'; +import { healthRoute } from './framework/apis/health'; import { resolveRuleRoute } from './rule/apis/resolve'; import { ruleTypesRoute } from './rule/apis/list_types/rule_types'; import { muteAllRuleRoute } from './rule/apis/mute_all/mute_all_rule';