From 4883d4bb2a89c3d16203fae837d555c3f5eb0b50 Mon Sep 17 00:00:00 2001 From: Yuliia Naumenko Date: Thu, 5 Mar 2020 07:52:24 -0800 Subject: [PATCH] Fixed ci type check errors for alerting and actions properties for SIEM RouteHandlerContext (#59400) * Fixed ci type check errors for alerting and actions optional properties on RouteHandlerContext * Reverted git revert b83f81458c11cbb890023e662920f3679c615ab7 --- .../routes/rules/add_prepackaged_rules_route.test.ts | 2 +- .../routes/rules/add_prepackaged_rules_route.ts | 3 +++ .../routes/rules/create_rules_bulk_route.test.ts | 2 +- .../routes/rules/create_rules_bulk_route.ts | 5 ++++- .../routes/rules/create_rules_route.test.ts | 2 +- .../lib/detection_engine/routes/rules/create_rules_route.ts | 3 +++ .../routes/rules/delete_rules_bulk_route.test.ts | 2 +- .../routes/rules/delete_rules_bulk_route.ts | 6 +++++- .../routes/rules/delete_rules_route.test.ts | 2 +- .../lib/detection_engine/routes/rules/delete_rules_route.ts | 3 +++ .../lib/detection_engine/routes/rules/export_rules_route.ts | 5 ++++- .../detection_engine/routes/rules/find_rules_route.test.ts | 2 +- .../lib/detection_engine/routes/rules/find_rules_route.ts | 3 +++ .../routes/rules/find_rules_status_route.test.ts | 2 +- .../routes/rules/find_rules_status_route.ts | 5 ++++- .../routes/rules/get_prepackaged_rules_status_route.test.ts | 2 +- .../routes/rules/get_prepackaged_rules_status_route.ts | 5 ++++- .../routes/rules/import_rules_route.test.ts | 4 ++-- .../lib/detection_engine/routes/rules/import_rules_route.ts | 6 +++++- .../routes/rules/patch_rules_bulk_route.test.ts | 2 +- .../detection_engine/routes/rules/patch_rules_bulk_route.ts | 6 +++++- .../detection_engine/routes/rules/patch_rules_route.test.ts | 2 +- .../lib/detection_engine/routes/rules/patch_rules_route.ts | 4 ++++ .../detection_engine/routes/rules/read_rules_route.test.ts | 2 +- .../lib/detection_engine/routes/rules/read_rules_route.ts | 6 +++++- .../routes/rules/update_rules_bulk_route.test.ts | 2 +- .../routes/rules/update_rules_bulk_route.ts | 6 +++++- .../routes/rules/update_rules_route.test.ts | 2 +- .../lib/detection_engine/routes/rules/update_rules_route.ts | 3 +++ .../lib/detection_engine/routes/tags/read_tags_route.ts | 6 +++++- x-pack/plugins/actions/server/routes/create.ts | 3 +++ x-pack/plugins/actions/server/routes/delete.ts | 3 +++ x-pack/plugins/actions/server/routes/find.ts | 3 +++ x-pack/plugins/actions/server/routes/get.ts | 3 +++ .../plugins/actions/server/routes/list_action_types.test.ts | 2 +- x-pack/plugins/actions/server/routes/list_action_types.ts | 3 +++ x-pack/plugins/actions/server/routes/update.ts | 3 +++ x-pack/plugins/actions/server/types.ts | 2 +- x-pack/plugins/alerting/server/routes/create.ts | 3 +++ x-pack/plugins/alerting/server/routes/delete.ts | 3 +++ x-pack/plugins/alerting/server/routes/disable.ts | 3 +++ x-pack/plugins/alerting/server/routes/enable.ts | 3 +++ x-pack/plugins/alerting/server/routes/find.ts | 3 +++ x-pack/plugins/alerting/server/routes/get.ts | 3 +++ x-pack/plugins/alerting/server/routes/get_alert_state.ts | 3 +++ .../plugins/alerting/server/routes/list_alert_types.test.ts | 2 +- x-pack/plugins/alerting/server/routes/list_alert_types.ts | 3 +++ x-pack/plugins/alerting/server/routes/mute_all.ts | 3 +++ x-pack/plugins/alerting/server/routes/mute_instance.ts | 3 +++ x-pack/plugins/alerting/server/routes/unmute_all.ts | 3 +++ x-pack/plugins/alerting/server/routes/unmute_instance.ts | 3 +++ x-pack/plugins/alerting/server/routes/update.ts | 3 +++ x-pack/plugins/alerting/server/routes/update_api_key.ts | 3 +++ x-pack/plugins/alerting/server/types.ts | 2 +- 54 files changed, 144 insertions(+), 29 deletions(-) diff --git a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/add_prepackaged_rules_route.test.ts b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/add_prepackaged_rules_route.test.ts index ec3db96ddc2f4..2b4fb8fa08a60 100644 --- a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/add_prepackaged_rules_route.test.ts +++ b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/add_prepackaged_rules_route.test.ts @@ -63,7 +63,7 @@ describe('add_prepackaged_rules_route', () => { }); test('returns 404 if alertClient is not available on the route', async () => { - context.alerting.getAlertsClient = jest.fn(); + context.alerting!.getAlertsClient = jest.fn(); const request = addPrepackagedRulesRequest(); const response = await server.inject(request, context); diff --git a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/add_prepackaged_rules_route.ts b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/add_prepackaged_rules_route.ts index 13373a2c2bbf0..4e08188af0d12 100644 --- a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/add_prepackaged_rules_route.ts +++ b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/add_prepackaged_rules_route.ts @@ -33,6 +33,9 @@ export const addPrepackedRulesRoute = (router: IRouter) => { const siemResponse = buildSiemResponse(response); try { + if (!context.alerting || !context.actions) { + return siemResponse.error({ statusCode: 404 }); + } const alertsClient = context.alerting.getAlertsClient(); const actionsClient = context.actions.getActionsClient(); const clusterClient = context.core.elasticsearch.dataClient; diff --git a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/create_rules_bulk_route.test.ts b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/create_rules_bulk_route.test.ts index a497890b0599a..6ad9efebce2dd 100644 --- a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/create_rules_bulk_route.test.ts +++ b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/create_rules_bulk_route.test.ts @@ -39,7 +39,7 @@ describe('create_rules_bulk', () => { }); test('returns 404 if alertClient is not available on the route', async () => { - context.alerting.getAlertsClient = jest.fn(); + context.alerting!.getAlertsClient = jest.fn(); const response = await server.inject(getReadBulkRequest(), context); expect(response.status).toEqual(404); expect(response.body).toEqual({ message: 'Not Found', status_code: 404 }); diff --git a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/create_rules_bulk_route.ts b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/create_rules_bulk_route.ts index 84841481a6c6f..ee8539faacf3e 100644 --- a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/create_rules_bulk_route.ts +++ b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/create_rules_bulk_route.ts @@ -35,11 +35,14 @@ export const createRulesBulkRoute = (router: IRouter) => { }, }, async (context, request, response) => { + const siemResponse = buildSiemResponse(response); + if (!context.alerting || !context.actions) { + return siemResponse.error({ statusCode: 404 }); + } const alertsClient = context.alerting.getAlertsClient(); const actionsClient = context.actions.getActionsClient(); const clusterClient = context.core.elasticsearch.dataClient; const siemClient = context.siem.getSiemClient(); - const siemResponse = buildSiemResponse(response); if (!actionsClient || !alertsClient) { return siemResponse.error({ statusCode: 404 }); diff --git a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/create_rules_route.test.ts b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/create_rules_route.test.ts index ab92f07852bfb..d019668e2a8d1 100644 --- a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/create_rules_route.test.ts +++ b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/create_rules_route.test.ts @@ -41,7 +41,7 @@ describe('create_rules', () => { }); test('returns 404 if alertClient is not available on the route', async () => { - context.alerting.getAlertsClient = jest.fn(); + context.alerting!.getAlertsClient = jest.fn(); const response = await server.inject(getCreateRequest(), context); expect(response.status).toEqual(404); expect(response.body).toEqual({ message: 'Not Found', status_code: 404 }); diff --git a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/create_rules_route.ts b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/create_rules_route.ts index 312ebbee3cd8c..cef7ded2b50b4 100644 --- a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/create_rules_route.ts +++ b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/create_rules_route.ts @@ -59,6 +59,9 @@ export const createRulesRoute = (router: IRouter): void => { const siemResponse = buildSiemResponse(response); try { + if (!context.alerting || !context.actions) { + return siemResponse.error({ statusCode: 404 }); + } const alertsClient = context.alerting.getAlertsClient(); const actionsClient = context.actions.getActionsClient(); const clusterClient = context.core.elasticsearch.dataClient; diff --git a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/delete_rules_bulk_route.test.ts b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/delete_rules_bulk_route.test.ts index f804d4c2e55ce..16f9a9524df55 100644 --- a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/delete_rules_bulk_route.test.ts +++ b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/delete_rules_bulk_route.test.ts @@ -82,7 +82,7 @@ describe('delete_rules', () => { }); test('returns 404 if alertClient is not available on the route', async () => { - context.alerting.getAlertsClient = jest.fn(); + context.alerting!.getAlertsClient = jest.fn(); const response = await server.inject(getDeleteBulkRequest(), context); expect(response.status).toEqual(404); expect(response.body).toEqual({ message: 'Not Found', status_code: 404 }); diff --git a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/delete_rules_bulk_route.ts b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/delete_rules_bulk_route.ts index c4a1e0bdb2c18..c56f34588cbc6 100644 --- a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/delete_rules_bulk_route.ts +++ b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/delete_rules_bulk_route.ts @@ -32,10 +32,14 @@ export const deleteRulesBulkRoute = (router: IRouter) => { }, }; const handler: Handler = async (context, request, response) => { + const siemResponse = buildSiemResponse(response); + + if (!context.alerting || !context.actions) { + return siemResponse.error({ statusCode: 404 }); + } const alertsClient = context.alerting.getAlertsClient(); const actionsClient = context.actions.getActionsClient(); const savedObjectsClient = context.core.savedObjects.client; - const siemResponse = buildSiemResponse(response); if (!actionsClient || !alertsClient) { return siemResponse.error({ statusCode: 404 }); diff --git a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/delete_rules_route.test.ts b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/delete_rules_route.test.ts index 0e4c22057d706..0519addb275d6 100644 --- a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/delete_rules_route.test.ts +++ b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/delete_rules_route.test.ts @@ -56,7 +56,7 @@ describe('delete_rules', () => { }); test('returns 404 if alertClient is not available on the route', async () => { - context.alerting.getAlertsClient = jest.fn(); + context.alerting!.getAlertsClient = jest.fn(); const response = await server.inject(getDeleteRequest(), context); expect(response.status).toEqual(404); diff --git a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/delete_rules_route.ts b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/delete_rules_route.ts index a637b7e0ef73e..753b281dbc09e 100644 --- a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/delete_rules_route.ts +++ b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/delete_rules_route.ts @@ -33,6 +33,9 @@ export const deleteRulesRoute = (router: IRouter) => { try { const { id, rule_id: ruleId } = request.query; + if (!context.alerting || !context.actions) { + return siemResponse.error({ statusCode: 404 }); + } const alertsClient = context.alerting.getAlertsClient(); const actionsClient = context.actions.getActionsClient(); diff --git a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/export_rules_route.ts b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/export_rules_route.ts index 88e14ad2b410b..c434f42780e47 100644 --- a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/export_rules_route.ts +++ b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/export_rules_route.ts @@ -27,8 +27,11 @@ export const exportRulesRoute = (router: IRouter, config: LegacyServices['config }, }, async (context, request, response) => { - const alertsClient = context.alerting.getAlertsClient(); const siemResponse = buildSiemResponse(response); + if (!context.alerting) { + return siemResponse.error({ statusCode: 404 }); + } + const alertsClient = context.alerting.getAlertsClient(); if (!alertsClient) { return siemResponse.error({ statusCode: 404 }); diff --git a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/find_rules_route.test.ts b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/find_rules_route.test.ts index 4271dcd240546..57759844c100d 100644 --- a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/find_rules_route.test.ts +++ b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/find_rules_route.test.ts @@ -36,7 +36,7 @@ describe('find_rules', () => { }); test('returns 404 if alertClient is not available on the route', async () => { - context.alerting.getAlertsClient = jest.fn(); + context.alerting!.getAlertsClient = jest.fn(); const response = await server.inject(getFindRequest(), context); expect(response.status).toEqual(404); expect(response.body).toEqual({ message: 'Not Found', status_code: 404 }); diff --git a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/find_rules_route.ts b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/find_rules_route.ts index 936957a3bb1ae..961859417ef1b 100644 --- a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/find_rules_route.ts +++ b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/find_rules_route.ts @@ -32,6 +32,9 @@ export const findRulesRoute = (router: IRouter) => { try { const { query } = request; + if (!context.alerting) { + return siemResponse.error({ statusCode: 404 }); + } const alertsClient = context.alerting.getAlertsClient(); const savedObjectsClient = context.core.savedObjects.client; diff --git a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/find_rules_status_route.test.ts b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/find_rules_status_route.test.ts index 182a2c66b67c9..9c86b70b88270 100644 --- a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/find_rules_status_route.test.ts +++ b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/find_rules_status_route.test.ts @@ -29,7 +29,7 @@ describe('find_statuses', () => { }); test('returns 404 if alertClient is not available on the route', async () => { - context.alerting.getAlertsClient = jest.fn(); + context.alerting!.getAlertsClient = jest.fn(); const response = await server.inject(ruleStatusRequest(), context); expect(response.status).toEqual(404); expect(response.body).toEqual({ message: 'Not Found', status_code: 404 }); diff --git a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/find_rules_status_route.ts b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/find_rules_status_route.ts index f222fa419f440..4f4ae7c2c1fa6 100644 --- a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/find_rules_status_route.ts +++ b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/find_rules_status_route.ts @@ -34,9 +34,12 @@ export const findRulesStatusesRoute = (router: IRouter) => { }, async (context, request, response) => { const { query } = request; + const siemResponse = buildSiemResponse(response); + if (!context.alerting) { + return siemResponse.error({ statusCode: 404 }); + } const alertsClient = context.alerting.getAlertsClient(); const savedObjectsClient = context.core.savedObjects.client; - const siemResponse = buildSiemResponse(response); if (!alertsClient) { return siemResponse.error({ statusCode: 404 }); diff --git a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/get_prepackaged_rules_status_route.test.ts b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/get_prepackaged_rules_status_route.test.ts index 23309944f511e..03059ed5ec5cc 100644 --- a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/get_prepackaged_rules_status_route.test.ts +++ b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/get_prepackaged_rules_status_route.test.ts @@ -58,7 +58,7 @@ describe('get_prepackaged_rule_status_route', () => { }); test('returns 404 if alertClient is not available on the route', async () => { - context.alerting.getAlertsClient = jest.fn(); + context.alerting!.getAlertsClient = jest.fn(); const response = await server.inject(getPrepackagedRulesStatusRequest(), context); expect(response.status).toEqual(404); expect(response.body).toEqual({ message: 'Not Found', status_code: 404 }); diff --git a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/get_prepackaged_rules_status_route.ts b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/get_prepackaged_rules_status_route.ts index ea20c2763886c..7e16b4495593e 100644 --- a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/get_prepackaged_rules_status_route.ts +++ b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/get_prepackaged_rules_status_route.ts @@ -28,8 +28,11 @@ export const getPrepackagedRulesStatusRoute = (router: IRouter) => { }, }, async (context, request, response) => { - const alertsClient = context.alerting.getAlertsClient(); const siemResponse = buildSiemResponse(response); + if (!context.alerting) { + return siemResponse.error({ statusCode: 404 }); + } + const alertsClient = context.alerting.getAlertsClient(); if (!alertsClient) { return siemResponse.error({ statusCode: 404 }); diff --git a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/import_rules_route.test.ts b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/import_rules_route.test.ts index c2daa5e8f2f9f..c224e0f055b85 100644 --- a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/import_rules_route.test.ts +++ b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/import_rules_route.test.ts @@ -78,14 +78,14 @@ describe('import_rules_route', () => { }); test('returns 404 if alertClient is not available on the route', async () => { - context.alerting.getAlertsClient = jest.fn(); + context.alerting!.getAlertsClient = jest.fn(); const response = await server.inject(request, context); expect(response.status).toEqual(404); expect(response.body).toEqual({ message: 'Not Found', status_code: 404 }); }); test('returns 404 if actionsClient is not available on the route', async () => { - context.actions.getActionsClient = jest.fn(); + context.actions!.getActionsClient = jest.fn(); const response = await server.inject(request, context); expect(response.status).toEqual(404); expect(response.body).toEqual({ message: 'Not Found', status_code: 404 }); diff --git a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/import_rules_route.ts b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/import_rules_route.ts index 38b409cc1dc5b..d9fc9b4e3c04f 100644 --- a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/import_rules_route.ts +++ b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/import_rules_route.ts @@ -54,12 +54,16 @@ export const importRulesRoute = (router: IRouter, config: LegacyServices['config }, }, async (context, request, response) => { + const siemResponse = buildSiemResponse(response); + + if (!context.alerting || !context.actions) { + return siemResponse.error({ statusCode: 404 }); + } const alertsClient = context.alerting.getAlertsClient(); const actionsClient = context.actions.getActionsClient(); const clusterClient = context.core.elasticsearch.dataClient; const savedObjectsClient = context.core.savedObjects.client; const siemClient = context.siem.getSiemClient(); - const siemResponse = buildSiemResponse(response); if (!actionsClient || !alertsClient) { return siemResponse.error({ statusCode: 404 }); diff --git a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/patch_rules_bulk_route.test.ts b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/patch_rules_bulk_route.test.ts index 1a7294682688a..19bcd2e7f0596 100644 --- a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/patch_rules_bulk_route.test.ts +++ b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/patch_rules_bulk_route.test.ts @@ -48,7 +48,7 @@ describe('patch_rules_bulk', () => { }); test('returns 404 if alertClient is not available on the route', async () => { - context.alerting.getAlertsClient = jest.fn(); + context.alerting!.getAlertsClient = jest.fn(); const response = await server.inject(getPatchBulkRequest(), context); expect(response.status).toEqual(404); expect(response.body).toEqual({ message: 'Not Found', status_code: 404 }); diff --git a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/patch_rules_bulk_route.ts b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/patch_rules_bulk_route.ts index 40250aaa5d532..7ca16a75fb562 100644 --- a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/patch_rules_bulk_route.ts +++ b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/patch_rules_bulk_route.ts @@ -30,10 +30,14 @@ export const patchRulesBulkRoute = (router: IRouter) => { }, }, async (context, request, response) => { + const siemResponse = buildSiemResponse(response); + + if (!context.alerting || !context.actions) { + return siemResponse.error({ statusCode: 404 }); + } const alertsClient = context.alerting.getAlertsClient(); const actionsClient = context.actions.getActionsClient(); const savedObjectsClient = context.core.savedObjects.client; - const siemResponse = buildSiemResponse(response); if (!actionsClient || !alertsClient) { return siemResponse.error({ statusCode: 404 }); diff --git a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/patch_rules_route.test.ts b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/patch_rules_route.test.ts index 712adb460d6f2..1658de77e3390 100644 --- a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/patch_rules_route.test.ts +++ b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/patch_rules_route.test.ts @@ -49,7 +49,7 @@ describe('patch_rules', () => { }); test('returns 404 if alertClient is not available on the route', async () => { - context.alerting.getAlertsClient = jest.fn(); + context.alerting!.getAlertsClient = jest.fn(); const response = await server.inject(getPatchRequest(), context); expect(response.status).toEqual(404); expect(response.body).toEqual({ message: 'Not Found', status_code: 404 }); diff --git a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/patch_rules_route.ts b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/patch_rules_route.ts index 951a5c5abdb33..dce5f4037db1c 100644 --- a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/patch_rules_route.ts +++ b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/patch_rules_route.ts @@ -60,6 +60,10 @@ export const patchRulesRoute = (router: IRouter) => { const siemResponse = buildSiemResponse(response); try { + if (!context.alerting || !context.actions) { + return siemResponse.error({ statusCode: 404 }); + } + const alertsClient = context.alerting.getAlertsClient(); const actionsClient = context.actions.getActionsClient(); const savedObjectsClient = context.core.savedObjects.client; diff --git a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/read_rules_route.test.ts b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/read_rules_route.test.ts index a6e84c45f17b4..7ebac9b785c82 100644 --- a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/read_rules_route.test.ts +++ b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/read_rules_route.test.ts @@ -36,7 +36,7 @@ describe('read_signals', () => { }); test('returns 404 if alertClient is not available on the route', async () => { - context.alerting.getAlertsClient = jest.fn(); + context.alerting!.getAlertsClient = jest.fn(); const response = await server.inject(getReadRequest(), context); expect(response.status).toEqual(404); expect(response.body).toEqual({ message: 'Not Found', status_code: 404 }); diff --git a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/read_rules_route.ts b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/read_rules_route.ts index 584beffa7abb1..e4117166ed4fa 100644 --- a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/read_rules_route.ts +++ b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/read_rules_route.ts @@ -30,9 +30,13 @@ export const readRulesRoute = (router: IRouter) => { }, async (context, request, response) => { const { id, rule_id: ruleId } = request.query; + const siemResponse = buildSiemResponse(response); + + if (!context.alerting) { + return siemResponse.error({ statusCode: 404 }); + } const alertsClient = context.alerting.getAlertsClient(); const savedObjectsClient = context.core.savedObjects.client; - const siemResponse = buildSiemResponse(response); try { if (!alertsClient) { diff --git a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/update_rules_bulk_route.test.ts b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/update_rules_bulk_route.test.ts index 438b80302fae4..7a9159ecc852b 100644 --- a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/update_rules_bulk_route.test.ts +++ b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/update_rules_bulk_route.test.ts @@ -53,7 +53,7 @@ describe('update_rules_bulk', () => { }); test('returns 404 if alertClient is not available on the route', async () => { - context.alerting.getAlertsClient = jest.fn(); + context.alerting!.getAlertsClient = jest.fn(); const response = await server.inject(getUpdateBulkRequest(), context); expect(response.status).toEqual(404); expect(response.body).toEqual({ message: 'Not Found', status_code: 404 }); diff --git a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/update_rules_bulk_route.ts b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/update_rules_bulk_route.ts index 4607af524139d..953fb16d26ac6 100644 --- a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/update_rules_bulk_route.ts +++ b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/update_rules_bulk_route.ts @@ -30,11 +30,15 @@ export const updateRulesBulkRoute = (router: IRouter) => { }, }, async (context, request, response) => { + const siemResponse = buildSiemResponse(response); + + if (!context.alerting || !context.actions) { + return siemResponse.error({ statusCode: 404 }); + } const alertsClient = context.alerting.getAlertsClient(); const actionsClient = context.actions.getActionsClient(); const savedObjectsClient = context.core.savedObjects.client; const siemClient = context.siem.getSiemClient(); - const siemResponse = buildSiemResponse(response); if (!actionsClient || !alertsClient) { return siemResponse.error({ statusCode: 404 }); diff --git a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/update_rules_route.test.ts b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/update_rules_route.test.ts index ccdfacd7c3d5b..6ef508b817713 100644 --- a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/update_rules_route.test.ts +++ b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/update_rules_route.test.ts @@ -50,7 +50,7 @@ describe('update_rules', () => { }); test('returns 404 if alertClient is not available on the route', async () => { - context.alerting.getAlertsClient = jest.fn(); + context.alerting!.getAlertsClient = jest.fn(); const response = await server.inject(getUpdateRequest(), context); expect(response.status).toEqual(404); diff --git a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/update_rules_route.ts b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/update_rules_route.ts index b5825a19f4762..fbb930d780f01 100644 --- a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/update_rules_route.ts +++ b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/rules/update_rules_route.ts @@ -60,6 +60,9 @@ export const updateRulesRoute = (router: IRouter) => { const siemResponse = buildSiemResponse(response); try { + if (!context.alerting || !context.actions) { + return siemResponse.error({ statusCode: 404 }); + } const alertsClient = context.alerting.getAlertsClient(); const actionsClient = context.actions.getActionsClient(); const savedObjectsClient = context.core.savedObjects.client; diff --git a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/tags/read_tags_route.ts b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/tags/read_tags_route.ts index 4663928ac1e46..e12bf50169c17 100644 --- a/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/tags/read_tags_route.ts +++ b/x-pack/legacy/plugins/siem/server/lib/detection_engine/routes/tags/read_tags_route.ts @@ -19,9 +19,13 @@ export const readTagsRoute = (router: IRouter) => { }, }, async (context, request, response) => { - const alertsClient = context.alerting.getAlertsClient(); const siemResponse = buildSiemResponse(response); + if (!context.alerting) { + return siemResponse.error({ statusCode: 404 }); + } + const alertsClient = context.alerting.getAlertsClient(); + if (!alertsClient) { return siemResponse.error({ statusCode: 404 }); } diff --git a/x-pack/plugins/actions/server/routes/create.ts b/x-pack/plugins/actions/server/routes/create.ts index f8f9aff9323a0..2150dc4076449 100644 --- a/x-pack/plugins/actions/server/routes/create.ts +++ b/x-pack/plugins/actions/server/routes/create.ts @@ -41,6 +41,9 @@ export const createActionRoute = (router: IRouter, licenseState: LicenseState) = ): Promise> { verifyApiAccess(licenseState); + if (!context.actions) { + return res.badRequest({ body: 'RouteHandlerContext is not registered for actions' }); + } const actionsClient = context.actions.getActionsClient(); const action = req.body; const actionRes: ActionResult = await actionsClient.create({ action }); diff --git a/x-pack/plugins/actions/server/routes/delete.ts b/x-pack/plugins/actions/server/routes/delete.ts index d96523997ad34..8508137b97750 100644 --- a/x-pack/plugins/actions/server/routes/delete.ts +++ b/x-pack/plugins/actions/server/routes/delete.ts @@ -41,6 +41,9 @@ export const deleteActionRoute = (router: IRouter, licenseState: LicenseState) = res: KibanaResponseFactory ): Promise> { verifyApiAccess(licenseState); + if (!context.actions) { + return res.badRequest({ body: 'RouteHandlerContext is not registered for actions' }); + } const actionsClient = context.actions.getActionsClient(); const { id } = req.params; await actionsClient.delete({ id }); diff --git a/x-pack/plugins/actions/server/routes/find.ts b/x-pack/plugins/actions/server/routes/find.ts index e791aff4fb598..71d4274980fcc 100644 --- a/x-pack/plugins/actions/server/routes/find.ts +++ b/x-pack/plugins/actions/server/routes/find.ts @@ -57,6 +57,9 @@ export const findActionRoute = (router: IRouter, licenseState: LicenseState) => res: KibanaResponseFactory ): Promise> { verifyApiAccess(licenseState); + if (!context.actions) { + return res.badRequest({ body: 'RouteHandlerContext is not registered for actions' }); + } const actionsClient = context.actions.getActionsClient(); const query = req.query; const options: FindOptions['options'] = { diff --git a/x-pack/plugins/actions/server/routes/get.ts b/x-pack/plugins/actions/server/routes/get.ts index 26aa74da5d36b..836f46bfe55fd 100644 --- a/x-pack/plugins/actions/server/routes/get.ts +++ b/x-pack/plugins/actions/server/routes/get.ts @@ -36,6 +36,9 @@ export const getActionRoute = (router: IRouter, licenseState: LicenseState) => { res: KibanaResponseFactory ): Promise> { verifyApiAccess(licenseState); + if (!context.actions) { + return res.badRequest({ body: 'RouteHandlerContext is not registered for actions' }); + } const actionsClient = context.actions.getActionsClient(); const { id } = req.params; return res.ok({ diff --git a/x-pack/plugins/actions/server/routes/list_action_types.test.ts b/x-pack/plugins/actions/server/routes/list_action_types.test.ts index 87cc4dfee5336..e983b8d1f2f84 100644 --- a/x-pack/plugins/actions/server/routes/list_action_types.test.ts +++ b/x-pack/plugins/actions/server/routes/list_action_types.test.ts @@ -58,7 +58,7 @@ describe('listActionTypesRoute', () => { } `); - expect(context.actions.listTypes).toHaveBeenCalledTimes(1); + expect(context.actions!.listTypes).toHaveBeenCalledTimes(1); expect(res.ok).toHaveBeenCalledWith({ body: listTypes, diff --git a/x-pack/plugins/actions/server/routes/list_action_types.ts b/x-pack/plugins/actions/server/routes/list_action_types.ts index 0b9791eedb39c..46f62e3a9c8bb 100644 --- a/x-pack/plugins/actions/server/routes/list_action_types.ts +++ b/x-pack/plugins/actions/server/routes/list_action_types.ts @@ -29,6 +29,9 @@ export const listActionTypesRoute = (router: IRouter, licenseState: LicenseState res: KibanaResponseFactory ): Promise> { verifyApiAccess(licenseState); + if (!context.actions) { + return res.badRequest({ body: 'RouteHandlerContext is not registered for actions' }); + } return res.ok({ body: context.actions.listTypes(), }); diff --git a/x-pack/plugins/actions/server/routes/update.ts b/x-pack/plugins/actions/server/routes/update.ts index 9c5f32e8b9119..315695382b2d9 100644 --- a/x-pack/plugins/actions/server/routes/update.ts +++ b/x-pack/plugins/actions/server/routes/update.ts @@ -43,6 +43,9 @@ export const updateActionRoute = (router: IRouter, licenseState: LicenseState) = res: KibanaResponseFactory ): Promise> { verifyApiAccess(licenseState); + if (!context.actions) { + return res.badRequest({ body: 'RouteHandlerContext is not registered for actions' }); + } const actionsClient = context.actions.getActionsClient(); const { id } = req.params; const { name, config, secrets } = req.body; diff --git a/x-pack/plugins/actions/server/types.ts b/x-pack/plugins/actions/server/types.ts index 2358f499c9f98..635c0829e02c3 100644 --- a/x-pack/plugins/actions/server/types.ts +++ b/x-pack/plugins/actions/server/types.ts @@ -22,7 +22,7 @@ export interface Services { declare module 'src/core/server' { interface RequestHandlerContext { - actions: { + actions?: { getActionsClient: () => ActionsClient; listTypes: ActionTypeRegistry['list']; }; diff --git a/x-pack/plugins/alerting/server/routes/create.ts b/x-pack/plugins/alerting/server/routes/create.ts index 8d854e0df8467..af518499a9abb 100644 --- a/x-pack/plugins/alerting/server/routes/create.ts +++ b/x-pack/plugins/alerting/server/routes/create.ts @@ -57,6 +57,9 @@ export const createAlertRoute = (router: IRouter, licenseState: LicenseState) => ): Promise> { verifyApiAccess(licenseState); + if (!context.alerting) { + return res.badRequest({ body: 'RouteHandlerContext is not registered for alerting' }); + } const alertsClient = context.alerting.getAlertsClient(); const alert = req.body; const alertRes: Alert = await alertsClient.create({ data: alert }); diff --git a/x-pack/plugins/alerting/server/routes/delete.ts b/x-pack/plugins/alerting/server/routes/delete.ts index 0556ef3d66982..fc36cf91fdad2 100644 --- a/x-pack/plugins/alerting/server/routes/delete.ts +++ b/x-pack/plugins/alerting/server/routes/delete.ts @@ -36,6 +36,9 @@ export const deleteAlertRoute = (router: IRouter, licenseState: LicenseState) => res: KibanaResponseFactory ): Promise> { verifyApiAccess(licenseState); + if (!context.alerting) { + return res.badRequest({ body: 'RouteHandlerContext is not registered for alerting' }); + } const alertsClient = context.alerting.getAlertsClient(); const { id } = req.params; await alertsClient.delete({ id }); diff --git a/x-pack/plugins/alerting/server/routes/disable.ts b/x-pack/plugins/alerting/server/routes/disable.ts index 5c6d977e62c38..da6562fb82af1 100644 --- a/x-pack/plugins/alerting/server/routes/disable.ts +++ b/x-pack/plugins/alerting/server/routes/disable.ts @@ -36,6 +36,9 @@ export const disableAlertRoute = (router: IRouter, licenseState: LicenseState) = res: KibanaResponseFactory ): Promise> { verifyApiAccess(licenseState); + if (!context.alerting) { + return res.badRequest({ body: 'RouteHandlerContext is not registered for alerting' }); + } const alertsClient = context.alerting.getAlertsClient(); const { id } = req.params; await alertsClient.disable({ id }); diff --git a/x-pack/plugins/alerting/server/routes/enable.ts b/x-pack/plugins/alerting/server/routes/enable.ts index f75344ad85998..1b995b7eb79b3 100644 --- a/x-pack/plugins/alerting/server/routes/enable.ts +++ b/x-pack/plugins/alerting/server/routes/enable.ts @@ -36,6 +36,9 @@ export const enableAlertRoute = (router: IRouter, licenseState: LicenseState) => res: KibanaResponseFactory ): Promise> { verifyApiAccess(licenseState); + if (!context.alerting) { + return res.badRequest({ body: 'RouteHandlerContext is not registered for alerting' }); + } const alertsClient = context.alerting.getAlertsClient(); const { id } = req.params; await alertsClient.enable({ id }); diff --git a/x-pack/plugins/alerting/server/routes/find.ts b/x-pack/plugins/alerting/server/routes/find.ts index 16f53aa218895..efc5c3ea97183 100644 --- a/x-pack/plugins/alerting/server/routes/find.ts +++ b/x-pack/plugins/alerting/server/routes/find.ts @@ -57,6 +57,9 @@ export const findAlertRoute = (router: IRouter, licenseState: LicenseState) => { res: KibanaResponseFactory ): Promise> { verifyApiAccess(licenseState); + if (!context.alerting) { + return res.badRequest({ body: 'RouteHandlerContext is not registered for alerting' }); + } const alertsClient = context.alerting.getAlertsClient(); const query = req.query; const options: FindOptions['options'] = { diff --git a/x-pack/plugins/alerting/server/routes/get.ts b/x-pack/plugins/alerting/server/routes/get.ts index 407d80b0f87ab..3fa2040aabc1f 100644 --- a/x-pack/plugins/alerting/server/routes/get.ts +++ b/x-pack/plugins/alerting/server/routes/get.ts @@ -36,6 +36,9 @@ export const getAlertRoute = (router: IRouter, licenseState: LicenseState) => { res: KibanaResponseFactory ): Promise> { verifyApiAccess(licenseState); + if (!context.alerting) { + return res.badRequest({ body: 'RouteHandlerContext is not registered for alerting' }); + } const alertsClient = context.alerting.getAlertsClient(); const { id } = req.params; return res.ok({ diff --git a/x-pack/plugins/alerting/server/routes/get_alert_state.ts b/x-pack/plugins/alerting/server/routes/get_alert_state.ts index b419889eea422..725b9139b2837 100644 --- a/x-pack/plugins/alerting/server/routes/get_alert_state.ts +++ b/x-pack/plugins/alerting/server/routes/get_alert_state.ts @@ -36,6 +36,9 @@ export const getAlertStateRoute = (router: IRouter, licenseState: LicenseState) res: KibanaResponseFactory ): Promise> { verifyApiAccess(licenseState); + if (!context.alerting) { + return res.badRequest({ body: 'RouteHandlerContext is not registered for alerting' }); + } const alertsClient = context.alerting.getAlertsClient(); const { id } = req.params; const state = await alertsClient.getAlertState({ id }); diff --git a/x-pack/plugins/alerting/server/routes/list_alert_types.test.ts b/x-pack/plugins/alerting/server/routes/list_alert_types.test.ts index 96ee8c5717453..723fd86fca8b5 100644 --- a/x-pack/plugins/alerting/server/routes/list_alert_types.test.ts +++ b/x-pack/plugins/alerting/server/routes/list_alert_types.test.ts @@ -70,7 +70,7 @@ describe('listAlertTypesRoute', () => { } `); - expect(context.alerting.listTypes).toHaveBeenCalledTimes(1); + expect(context.alerting!.listTypes).toHaveBeenCalledTimes(1); expect(res.ok).toHaveBeenCalledWith({ body: listTypes, diff --git a/x-pack/plugins/alerting/server/routes/list_alert_types.ts b/x-pack/plugins/alerting/server/routes/list_alert_types.ts index e33bb9a010bf7..6e2b7ebb9014c 100644 --- a/x-pack/plugins/alerting/server/routes/list_alert_types.ts +++ b/x-pack/plugins/alerting/server/routes/list_alert_types.ts @@ -29,6 +29,9 @@ export const listAlertTypesRoute = (router: IRouter, licenseState: LicenseState) res: KibanaResponseFactory ): Promise> { verifyApiAccess(licenseState); + if (!context.alerting) { + return res.badRequest({ body: 'RouteHandlerContext is not registered for alerting' }); + } return res.ok({ body: context.alerting.listTypes(), }); diff --git a/x-pack/plugins/alerting/server/routes/mute_all.ts b/x-pack/plugins/alerting/server/routes/mute_all.ts index 796efd457f478..224c7e3bf7ea9 100644 --- a/x-pack/plugins/alerting/server/routes/mute_all.ts +++ b/x-pack/plugins/alerting/server/routes/mute_all.ts @@ -36,6 +36,9 @@ export const muteAllAlertRoute = (router: IRouter, licenseState: LicenseState) = res: KibanaResponseFactory ): Promise> { verifyApiAccess(licenseState); + if (!context.alerting) { + return res.badRequest({ body: 'RouteHandlerContext is not registered for alerting' }); + } const alertsClient = context.alerting.getAlertsClient(); const { id } = req.params; await alertsClient.muteAll({ id }); diff --git a/x-pack/plugins/alerting/server/routes/mute_instance.ts b/x-pack/plugins/alerting/server/routes/mute_instance.ts index bae7b00548a26..c0d9f01a99e23 100644 --- a/x-pack/plugins/alerting/server/routes/mute_instance.ts +++ b/x-pack/plugins/alerting/server/routes/mute_instance.ts @@ -37,6 +37,9 @@ export const muteAlertInstanceRoute = (router: IRouter, licenseState: LicenseSta res: KibanaResponseFactory ): Promise> { verifyApiAccess(licenseState); + if (!context.alerting) { + return res.badRequest({ body: 'RouteHandlerContext is not registered for alerting' }); + } const alertsClient = context.alerting.getAlertsClient(); const { alertId, alertInstanceId } = req.params; await alertsClient.muteInstance({ alertId, alertInstanceId }); diff --git a/x-pack/plugins/alerting/server/routes/unmute_all.ts b/x-pack/plugins/alerting/server/routes/unmute_all.ts index 5483f691b5462..4ab009b5722a9 100644 --- a/x-pack/plugins/alerting/server/routes/unmute_all.ts +++ b/x-pack/plugins/alerting/server/routes/unmute_all.ts @@ -36,6 +36,9 @@ export const unmuteAllAlertRoute = (router: IRouter, licenseState: LicenseState) res: KibanaResponseFactory ): Promise> { verifyApiAccess(licenseState); + if (!context.alerting) { + return res.badRequest({ body: 'RouteHandlerContext is not registered for alerting' }); + } const alertsClient = context.alerting.getAlertsClient(); const { id } = req.params; await alertsClient.unmuteAll({ id }); diff --git a/x-pack/plugins/alerting/server/routes/unmute_instance.ts b/x-pack/plugins/alerting/server/routes/unmute_instance.ts index fc24ea88ddb67..26439d47f430e 100644 --- a/x-pack/plugins/alerting/server/routes/unmute_instance.ts +++ b/x-pack/plugins/alerting/server/routes/unmute_instance.ts @@ -37,6 +37,9 @@ export const unmuteAlertInstanceRoute = (router: IRouter, licenseState: LicenseS res: KibanaResponseFactory ): Promise> { verifyApiAccess(licenseState); + if (!context.alerting) { + return res.badRequest({ body: 'RouteHandlerContext is not registered for alerting' }); + } const alertsClient = context.alerting.getAlertsClient(); const { alertId, alertInstanceId } = req.params; await alertsClient.unmuteInstance({ alertId, alertInstanceId }); diff --git a/x-pack/plugins/alerting/server/routes/update.ts b/x-pack/plugins/alerting/server/routes/update.ts index a402d13c5fbab..76b864a51aec6 100644 --- a/x-pack/plugins/alerting/server/routes/update.ts +++ b/x-pack/plugins/alerting/server/routes/update.ts @@ -57,6 +57,9 @@ export const updateAlertRoute = (router: IRouter, licenseState: LicenseState) => res: KibanaResponseFactory ): Promise> { verifyApiAccess(licenseState); + if (!context.alerting) { + return res.badRequest({ body: 'RouteHandlerContext is not registered for alerting' }); + } const alertsClient = context.alerting.getAlertsClient(); const { id } = req.params; const { name, actions, params, schedule, tags } = req.body; diff --git a/x-pack/plugins/alerting/server/routes/update_api_key.ts b/x-pack/plugins/alerting/server/routes/update_api_key.ts index 0951b6c7b939e..3c8a7d911b158 100644 --- a/x-pack/plugins/alerting/server/routes/update_api_key.ts +++ b/x-pack/plugins/alerting/server/routes/update_api_key.ts @@ -36,6 +36,9 @@ export const updateApiKeyRoute = (router: IRouter, licenseState: LicenseState) = res: KibanaResponseFactory ): Promise> { verifyApiAccess(licenseState); + if (!context.alerting) { + return res.badRequest({ body: 'RouteHandlerContext is not registered for alerting' }); + } const alertsClient = context.alerting.getAlertsClient(); const { id } = req.params; await alertsClient.updateApiKey({ id }); diff --git a/x-pack/plugins/alerting/server/types.ts b/x-pack/plugins/alerting/server/types.ts index 90bc7996729a6..635cf0cbd1371 100644 --- a/x-pack/plugins/alerting/server/types.ts +++ b/x-pack/plugins/alerting/server/types.ts @@ -21,7 +21,7 @@ export type SpaceIdToNamespaceFunction = (spaceId?: string) => string | undefine declare module 'src/core/server' { interface RequestHandlerContext { - alerting: { + alerting?: { getAlertsClient: () => AlertsClient; listTypes: AlertTypeRegistry['list']; };