From 7df8c131f5a6473a0dbb32221e713002f1720d20 Mon Sep 17 00:00:00 2001 From: Marshall Main <55718608+marshallmain@users.noreply.github.com> Date: Mon, 7 Aug 2023 07:33:08 -0700 Subject: [PATCH] [Security Solution] Move risk score API schemas to /common/api (#163032) Closes https://github.com/elastic/security-team/issues/7101 --- .github/CODEOWNERS | 1 + .../create_index/create_index_route.ts | 18 +++++++++++++ ...eate_prebuilt_saved_objects_route.test.ts} | 16 ++++++------ .../create_prebuilt_saved_objects_route.ts | 17 +++++++++++++ .../create_stored_script_route.ts | 25 +++++++++++++++++++ .../delete_indices/delete_indices_route.ts | 12 +++++++++ .../delete_prebuilt_saved_objects_route.ts} | 11 +------- .../delete_stored_script_route.ts | 15 +++++++++++ .../common/api/risk_score/index.ts | 16 ++++++++++++ .../index_status/index_status_route.ts} | 2 +- .../install_modules/install_modules_route.ts} | 4 +-- .../read_prebuilt_dev_tool_content_route.ts} | 2 +- .../lib/risk_score/index_status/index.ts | 4 +-- .../risk_score/indices/create_index_route.ts | 5 ++-- .../indices/delete_indices_route.ts | 8 ++---- .../risk_score/indices/lib/create_index.ts | 14 ++--------- .../onboarding/routes/install_risk_scores.ts | 4 +-- .../read_prebuilt_dev_tool_content_route.ts | 4 +-- .../prebuilt_dev_tool_content/schema.test.ts | 6 ++--- .../routes/create_prebuilt_saved_objects.ts | 4 +-- .../routes/delete_prebuilt_saved_objects.ts | 4 +-- .../stored_scripts/create_script_route.ts | 5 ++-- .../stored_scripts/delete_script_route.ts | 5 ++-- .../stored_scripts/lib/create_script.ts | 22 ++-------------- .../stored_scripts/lib/delete_script.ts | 11 ++------ 25 files changed, 147 insertions(+), 88 deletions(-) create mode 100644 x-pack/plugins/security_solution/common/api/risk_score/create_index/create_index_route.ts rename x-pack/plugins/security_solution/{server/lib/risk_score/prebuilt_saved_objects/schema.test.ts => common/api/risk_score/create_prebuilt_saved_objects/create_prebuilt_saved_objects_route.test.ts} (53%) create mode 100644 x-pack/plugins/security_solution/common/api/risk_score/create_prebuilt_saved_objects/create_prebuilt_saved_objects_route.ts create mode 100644 x-pack/plugins/security_solution/common/api/risk_score/create_stored_script/create_stored_script_route.ts create mode 100644 x-pack/plugins/security_solution/common/api/risk_score/delete_indices/delete_indices_route.ts rename x-pack/plugins/security_solution/{server/lib/risk_score/prebuilt_saved_objects/schema.ts => common/api/risk_score/delete_prebuilt_saved_objects/delete_prebuilt_saved_objects_route.ts} (67%) create mode 100644 x-pack/plugins/security_solution/common/api/risk_score/delete_stored_script/delete_stored_script_route.ts create mode 100644 x-pack/plugins/security_solution/common/api/risk_score/index.ts rename x-pack/plugins/security_solution/{server/lib/risk_score/index_status/schema.ts => common/api/risk_score/index_status/index_status_route.ts} (87%) rename x-pack/plugins/security_solution/{server/lib/risk_score/onboarding/schema.ts => common/api/risk_score/install_modules/install_modules_route.ts} (80%) rename x-pack/plugins/security_solution/{server/lib/risk_score/prebuilt_dev_tool_content/schema.ts => common/api/risk_score/read_prebuilt_dev_tool_content/read_prebuilt_dev_tool_content_route.ts} (91%) diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index f38621c5f5d52..dfcda878c2e4e 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -1098,6 +1098,7 @@ x-pack/plugins/cloud_integrations/cloud_full_story/server/config.ts @elastic/kib /x-pack/plugins/security_solution/server/lib/timeline @elastic/security-threat-hunting-investigations ## Security Solution sub teams - Threat Hunting Explore +/x-pack/plugins/security_solution/common/api/risk_score @elastic/security-threat-hunting-explore /x-pack/plugins/security_solution/common/search_strategy/security_solution/hosts @elastic/security-threat-hunting-explore /x-pack/plugins/security_solution/common/search_strategy/security_solution/matrix_histogram @elastic/security-threat-hunting-explore /x-pack/plugins/security_solution/common/search_strategy/security_solution/network @elastic/security-threat-hunting-explore diff --git a/x-pack/plugins/security_solution/common/api/risk_score/create_index/create_index_route.ts b/x-pack/plugins/security_solution/common/api/risk_score/create_index/create_index_route.ts new file mode 100644 index 0000000000000..5199db8132b79 --- /dev/null +++ b/x-pack/plugins/security_solution/common/api/risk_score/create_index/create_index_route.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. + */ + +import type { TypeOf } from '@kbn/config-schema'; +import { schema } from '@kbn/config-schema'; + +export const createEsIndexRequestBody = schema.object({ + index: schema.string({ minLength: 1 }), + mappings: schema.maybe( + schema.oneOf([schema.string(), schema.recordOf(schema.string({ minLength: 1 }), schema.any())]) + ), +}); + +export type CreateEsIndexRequestBody = TypeOf; diff --git a/x-pack/plugins/security_solution/server/lib/risk_score/prebuilt_saved_objects/schema.test.ts b/x-pack/plugins/security_solution/common/api/risk_score/create_prebuilt_saved_objects/create_prebuilt_saved_objects_route.test.ts similarity index 53% rename from x-pack/plugins/security_solution/server/lib/risk_score/prebuilt_saved_objects/schema.test.ts rename to x-pack/plugins/security_solution/common/api/risk_score/create_prebuilt_saved_objects/create_prebuilt_saved_objects_route.test.ts index 2a7d221bf610a..b37f611a6e2bb 100644 --- a/x-pack/plugins/security_solution/server/lib/risk_score/prebuilt_saved_objects/schema.test.ts +++ b/x-pack/plugins/security_solution/common/api/risk_score/create_prebuilt_saved_objects/create_prebuilt_saved_objects_route.test.ts @@ -5,23 +5,23 @@ * 2.0. */ -import { createPrebuiltSavedObjectsSchema } from './schema'; +import { createPrebuiltSavedObjectsRequestBody } from './create_prebuilt_saved_objects_route'; -describe('createPrebuiltSavedObjectsSchema', () => { +describe('createPrebuiltSavedObjectsRequestBody', () => { it('should throw error', () => { expect(() => - createPrebuiltSavedObjectsSchema.params.validate({ template_name: '123' }) + createPrebuiltSavedObjectsRequestBody.params.validate({ template_name: '123' }) ).toThrow(); }); it.each([['hostRiskScoreDashboards', 'userRiskScoreDashboards']])( 'should allow template %p', async (template) => { - expect(createPrebuiltSavedObjectsSchema.params.validate({ template_name: template })).toEqual( - { - template_name: template, - } - ); + expect( + createPrebuiltSavedObjectsRequestBody.params.validate({ template_name: template }) + ).toEqual({ + template_name: template, + }); } ); }); diff --git a/x-pack/plugins/security_solution/common/api/risk_score/create_prebuilt_saved_objects/create_prebuilt_saved_objects_route.ts b/x-pack/plugins/security_solution/common/api/risk_score/create_prebuilt_saved_objects/create_prebuilt_saved_objects_route.ts new file mode 100644 index 0000000000000..a21d45430791c --- /dev/null +++ b/x-pack/plugins/security_solution/common/api/risk_score/create_prebuilt_saved_objects/create_prebuilt_saved_objects_route.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 { schema } from '@kbn/config-schema'; + +export const createPrebuiltSavedObjectsRequestBody = { + params: schema.object({ + template_name: schema.oneOf([ + schema.literal('hostRiskScoreDashboards'), + schema.literal('userRiskScoreDashboards'), + ]), + }), +}; diff --git a/x-pack/plugins/security_solution/common/api/risk_score/create_stored_script/create_stored_script_route.ts b/x-pack/plugins/security_solution/common/api/risk_score/create_stored_script/create_stored_script_route.ts new file mode 100644 index 0000000000000..4a16cdfa247bc --- /dev/null +++ b/x-pack/plugins/security_solution/common/api/risk_score/create_stored_script/create_stored_script_route.ts @@ -0,0 +1,25 @@ +/* + * 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 { schema } from '@kbn/config-schema'; + +export const createStoredScriptRequestBody = schema.object({ + id: schema.string({ minLength: 1 }), + script: schema.object({ + lang: schema.oneOf([ + schema.string(), + schema.literal('painless'), + schema.literal('expression'), + schema.literal('mustache'), + schema.literal('java'), + ]), + options: schema.maybe(schema.recordOf(schema.string(), schema.string())), + source: schema.string(), + }), +}); + +export type CreateStoredScriptRequestBody = TypeOf; diff --git a/x-pack/plugins/security_solution/common/api/risk_score/delete_indices/delete_indices_route.ts b/x-pack/plugins/security_solution/common/api/risk_score/delete_indices/delete_indices_route.ts new file mode 100644 index 0000000000000..b773a52917a73 --- /dev/null +++ b/x-pack/plugins/security_solution/common/api/risk_score/delete_indices/delete_indices_route.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 deleteIndicesRequestBody = schema.object({ + indices: schema.arrayOf(schema.string()), +}); diff --git a/x-pack/plugins/security_solution/server/lib/risk_score/prebuilt_saved_objects/schema.ts b/x-pack/plugins/security_solution/common/api/risk_score/delete_prebuilt_saved_objects/delete_prebuilt_saved_objects_route.ts similarity index 67% rename from x-pack/plugins/security_solution/server/lib/risk_score/prebuilt_saved_objects/schema.ts rename to x-pack/plugins/security_solution/common/api/risk_score/delete_prebuilt_saved_objects/delete_prebuilt_saved_objects_route.ts index 17280d2a3be34..729197a18ddb6 100644 --- a/x-pack/plugins/security_solution/server/lib/risk_score/prebuilt_saved_objects/schema.ts +++ b/x-pack/plugins/security_solution/common/api/risk_score/delete_prebuilt_saved_objects/delete_prebuilt_saved_objects_route.ts @@ -7,16 +7,7 @@ import { schema } from '@kbn/config-schema'; -export const createPrebuiltSavedObjectsSchema = { - params: schema.object({ - template_name: schema.oneOf([ - schema.literal('hostRiskScoreDashboards'), - schema.literal('userRiskScoreDashboards'), - ]), - }), -}; - -export const deletePrebuiltSavedObjectsSchema = { +export const deletePrebuiltSavedObjectsRequestBody = { params: schema.object({ template_name: schema.oneOf([ schema.literal('hostRiskScoreDashboards'), diff --git a/x-pack/plugins/security_solution/common/api/risk_score/delete_stored_script/delete_stored_script_route.ts b/x-pack/plugins/security_solution/common/api/risk_score/delete_stored_script/delete_stored_script_route.ts new file mode 100644 index 0000000000000..3859e25f427ed --- /dev/null +++ b/x-pack/plugins/security_solution/common/api/risk_score/delete_stored_script/delete_stored_script_route.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. + */ + +import type { TypeOf } from '@kbn/config-schema'; +import { schema } from '@kbn/config-schema'; + +export const deleteStoredScriptRequestBody = schema.object({ + id: schema.string({ minLength: 1 }), +}); + +export type DeleteStoredScriptRequestBody = TypeOf; diff --git a/x-pack/plugins/security_solution/common/api/risk_score/index.ts b/x-pack/plugins/security_solution/common/api/risk_score/index.ts new file mode 100644 index 0000000000000..e3bb3d622c3c0 --- /dev/null +++ b/x-pack/plugins/security_solution/common/api/risk_score/index.ts @@ -0,0 +1,16 @@ +/* + * 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 './create_index/create_index_route'; +export * from './create_prebuilt_saved_objects/create_prebuilt_saved_objects_route'; +export * from './create_stored_script/create_stored_script_route'; +export * from './delete_indices/delete_indices_route'; +export * from './delete_prebuilt_saved_objects/delete_prebuilt_saved_objects_route'; +export * from './delete_stored_script/delete_stored_script_route'; +export * from './index_status/index_status_route'; +export * from './install_modules/install_modules_route'; +export * from './read_prebuilt_dev_tool_content/read_prebuilt_dev_tool_content_route'; diff --git a/x-pack/plugins/security_solution/server/lib/risk_score/index_status/schema.ts b/x-pack/plugins/security_solution/common/api/risk_score/index_status/index_status_route.ts similarity index 87% rename from x-pack/plugins/security_solution/server/lib/risk_score/index_status/schema.ts rename to x-pack/plugins/security_solution/common/api/risk_score/index_status/index_status_route.ts index 7cff67daaa647..afe8529838878 100644 --- a/x-pack/plugins/security_solution/server/lib/risk_score/index_status/schema.ts +++ b/x-pack/plugins/security_solution/common/api/risk_score/index_status/index_status_route.ts @@ -7,7 +7,7 @@ import * as t from 'io-ts'; -export const indexStatusSchema = t.type({ +export const indexStatusRequestQuery = t.type({ indexName: t.string, entity: t.string, }); diff --git a/x-pack/plugins/security_solution/server/lib/risk_score/onboarding/schema.ts b/x-pack/plugins/security_solution/common/api/risk_score/install_modules/install_modules_route.ts similarity index 80% rename from x-pack/plugins/security_solution/server/lib/risk_score/onboarding/schema.ts rename to x-pack/plugins/security_solution/common/api/risk_score/install_modules/install_modules_route.ts index 0fdcfee33d2c9..a17f10d724863 100644 --- a/x-pack/plugins/security_solution/server/lib/risk_score/onboarding/schema.ts +++ b/x-pack/plugins/security_solution/common/api/risk_score/install_modules/install_modules_route.ts @@ -6,9 +6,9 @@ */ import { schema } from '@kbn/config-schema'; -import { RiskScoreEntity } from '../../../../common/search_strategy'; +import { RiskScoreEntity } from '../../../search_strategy'; -export const onboardingRiskScoreSchema = { +export const onboardingRiskScoreRequestBody = { body: schema.object({ riskScoreEntity: schema.oneOf([ schema.literal(RiskScoreEntity.host), diff --git a/x-pack/plugins/security_solution/server/lib/risk_score/prebuilt_dev_tool_content/schema.ts b/x-pack/plugins/security_solution/common/api/risk_score/read_prebuilt_dev_tool_content/read_prebuilt_dev_tool_content_route.ts similarity index 91% rename from x-pack/plugins/security_solution/server/lib/risk_score/prebuilt_dev_tool_content/schema.ts rename to x-pack/plugins/security_solution/common/api/risk_score/read_prebuilt_dev_tool_content/read_prebuilt_dev_tool_content_route.ts index cf01f0ef9f040..8f8ddd62299ff 100644 --- a/x-pack/plugins/security_solution/server/lib/risk_score/prebuilt_dev_tool_content/schema.ts +++ b/x-pack/plugins/security_solution/common/api/risk_score/read_prebuilt_dev_tool_content/read_prebuilt_dev_tool_content_route.ts @@ -7,7 +7,7 @@ import { schema } from '@kbn/config-schema'; -export const ReadConsoleRequestSchema = { +export const readConsoleRequestBody = { params: schema.object({ console_id: schema.oneOf([ schema.literal('enable_host_risk_score'), diff --git a/x-pack/plugins/security_solution/server/lib/risk_score/index_status/index.ts b/x-pack/plugins/security_solution/server/lib/risk_score/index_status/index.ts index e1ecfb037c178..698724545585d 100644 --- a/x-pack/plugins/security_solution/server/lib/risk_score/index_status/index.ts +++ b/x-pack/plugins/security_solution/server/lib/risk_score/index_status/index.ts @@ -11,14 +11,14 @@ import { APP_ID, RISK_SCORE_INDEX_STATUS_API_URL } from '../../../../common/cons import type { SecuritySolutionPluginRouter } from '../../../types'; import { buildRouteValidation } from '../../../utils/build_validation/route_validation'; import { buildSiemResponse } from '../../detection_engine/routes/utils'; -import { indexStatusSchema } from './schema'; +import { indexStatusRequestQuery } from '../../../../common/api/risk_score'; export const getRiskScoreIndexStatusRoute = (router: SecuritySolutionPluginRouter) => { router.get( { path: RISK_SCORE_INDEX_STATUS_API_URL, validate: { - query: buildRouteValidation(indexStatusSchema), + query: buildRouteValidation(indexStatusRequestQuery), }, options: { tags: ['access:securitySolution', `access:${APP_ID}-entity-analytics`], diff --git a/x-pack/plugins/security_solution/server/lib/risk_score/indices/create_index_route.ts b/x-pack/plugins/security_solution/server/lib/risk_score/indices/create_index_route.ts index a22a503a071e3..24391b35bf4bc 100644 --- a/x-pack/plugins/security_solution/server/lib/risk_score/indices/create_index_route.ts +++ b/x-pack/plugins/security_solution/server/lib/risk_score/indices/create_index_route.ts @@ -11,13 +11,14 @@ import type { Logger } from '@kbn/core/server'; import { transformError } from '@kbn/securitysolution-es-utils'; import { RISK_SCORE_CREATE_INDEX } from '../../../../common/constants'; import type { SecuritySolutionPluginRouter } from '../../../types'; -import { createEsIndexBodySchema, createIndex } from './lib/create_index'; +import { createIndex } from './lib/create_index'; +import { createEsIndexRequestBody } from '../../../../common/api/risk_score'; export const createEsIndexRoute = (router: SecuritySolutionPluginRouter, logger: Logger) => { router.put( { path: RISK_SCORE_CREATE_INDEX, - validate: { body: createEsIndexBodySchema }, + validate: { body: createEsIndexRequestBody }, options: { tags: ['access:securitySolution'], }, diff --git a/x-pack/plugins/security_solution/server/lib/risk_score/indices/delete_indices_route.ts b/x-pack/plugins/security_solution/server/lib/risk_score/indices/delete_indices_route.ts index ad202d757a4ee..9413b31765012 100644 --- a/x-pack/plugins/security_solution/server/lib/risk_score/indices/delete_indices_route.ts +++ b/x-pack/plugins/security_solution/server/lib/risk_score/indices/delete_indices_route.ts @@ -4,23 +4,19 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ -import { schema } from '@kbn/config-schema'; import { transformError } from '@kbn/securitysolution-es-utils'; import { buildSiemResponse } from '@kbn/lists-plugin/server/routes/utils'; import { RISK_SCORE_DELETE_INDICES } from '../../../../common/constants'; import type { SecuritySolutionPluginRouter } from '../../../types'; import { deleteEsIndices } from './lib/delete_indices'; - -const bodySchema = schema.object({ - indices: schema.arrayOf(schema.string()), -}); +import { deleteIndicesRequestBody } from '../../../../common/api/risk_score'; export const deleteEsIndicesRoute = (router: SecuritySolutionPluginRouter) => { router.post( { path: RISK_SCORE_DELETE_INDICES, - validate: { body: bodySchema }, + validate: { body: deleteIndicesRequestBody }, options: { tags: ['access:securitySolution'], }, diff --git a/x-pack/plugins/security_solution/server/lib/risk_score/indices/lib/create_index.ts b/x-pack/plugins/security_solution/server/lib/risk_score/indices/lib/create_index.ts index 035be5e7d0e8d..48d3c333dff09 100644 --- a/x-pack/plugins/security_solution/server/lib/risk_score/indices/lib/create_index.ts +++ b/x-pack/plugins/security_solution/server/lib/risk_score/indices/lib/create_index.ts @@ -4,19 +4,9 @@ * 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 { schema } from '@kbn/config-schema'; import type { ElasticsearchClient, Logger } from '@kbn/core/server'; import { transformError } from '@kbn/securitysolution-es-utils'; - -export const createEsIndexBodySchema = schema.object({ - index: schema.string({ minLength: 1 }), - mappings: schema.maybe( - schema.oneOf([schema.string(), schema.recordOf(schema.string({ minLength: 1 }), schema.any())]) - ), -}); - -type CreateEsIndexBodySchema = TypeOf; +import type { CreateEsIndexRequestBody } from '../../../../../common/api/risk_score'; export const createIndex = async ({ esClient, @@ -25,7 +15,7 @@ export const createIndex = async ({ }: { esClient: ElasticsearchClient; logger: Logger; - options: CreateEsIndexBodySchema; + options: CreateEsIndexRequestBody; }) => { try { await esClient.indices.create({ diff --git a/x-pack/plugins/security_solution/server/lib/risk_score/onboarding/routes/install_risk_scores.ts b/x-pack/plugins/security_solution/server/lib/risk_score/onboarding/routes/install_risk_scores.ts index 499cdc4a4df3a..dc71b70148703 100644 --- a/x-pack/plugins/security_solution/server/lib/risk_score/onboarding/routes/install_risk_scores.ts +++ b/x-pack/plugins/security_solution/server/lib/risk_score/onboarding/routes/install_risk_scores.ts @@ -16,7 +16,7 @@ import type { SetupPlugins } from '../../../../plugin'; import { buildSiemResponse } from '../../../detection_engine/routes/utils'; import { installRiskScoreModule } from '../helpers/install_risk_score_module'; -import { onboardingRiskScoreSchema } from '../schema'; +import { onboardingRiskScoreRequestBody } from '../../../../../common/api/risk_score'; export const installRiskScoresRoute = ( router: SecuritySolutionPluginRouter, @@ -26,7 +26,7 @@ export const installRiskScoresRoute = ( router.post( { path: INTERNAL_RISK_SCORE_URL, - validate: onboardingRiskScoreSchema, + validate: onboardingRiskScoreRequestBody, options: { tags: ['access:securitySolution', `access:${APP_ID}-entity-analytics`], }, diff --git a/x-pack/plugins/security_solution/server/lib/risk_score/prebuilt_dev_tool_content/routes/read_prebuilt_dev_tool_content_route.ts b/x-pack/plugins/security_solution/server/lib/risk_score/prebuilt_dev_tool_content/routes/read_prebuilt_dev_tool_content_route.ts index 07f0c15773ecc..766d32456c5e3 100644 --- a/x-pack/plugins/security_solution/server/lib/risk_score/prebuilt_dev_tool_content/routes/read_prebuilt_dev_tool_content_route.ts +++ b/x-pack/plugins/security_solution/server/lib/risk_score/prebuilt_dev_tool_content/routes/read_prebuilt_dev_tool_content_route.ts @@ -14,7 +14,7 @@ import { DEV_TOOL_PREBUILT_CONTENT } from '../../../../../common/constants'; import type { SecuritySolutionPluginRouter } from '../../../../types'; import { consoleMappings } from '../console_mappings'; -import { ReadConsoleRequestSchema } from '../schema'; +import { readConsoleRequestBody } from '../../../../../common/api/risk_score'; import { RiskScoreEntity } from '../../../../../common/search_strategy'; import { getView } from '../utils'; @@ -48,7 +48,7 @@ export const readPrebuiltDevToolContentRoute = (router: SecuritySolutionPluginRo router.get( { path: DEV_TOOL_PREBUILT_CONTENT, - validate: ReadConsoleRequestSchema, + validate: readConsoleRequestBody, options: { tags: ['access:securitySolution'], }, diff --git a/x-pack/plugins/security_solution/server/lib/risk_score/prebuilt_dev_tool_content/schema.test.ts b/x-pack/plugins/security_solution/server/lib/risk_score/prebuilt_dev_tool_content/schema.test.ts index 13b318158aac3..bf02aae37e5ca 100644 --- a/x-pack/plugins/security_solution/server/lib/risk_score/prebuilt_dev_tool_content/schema.test.ts +++ b/x-pack/plugins/security_solution/server/lib/risk_score/prebuilt_dev_tool_content/schema.test.ts @@ -5,17 +5,17 @@ * 2.0. */ -import { ReadConsoleRequestSchema } from './schema'; +import { readConsoleRequestBody } from '../../../../common/api/risk_score'; describe('ReadConsoleRequestSchema', () => { it('should throw error', () => { - expect(() => ReadConsoleRequestSchema.params.validate({ console_id: '123' })).toThrow(); + expect(() => readConsoleRequestBody.params.validate({ console_id: '123' })).toThrow(); }); it.each([['enable_host_risk_score', 'enable_user_risk_score']])( 'should allow console_id %p', async (template) => { - expect(ReadConsoleRequestSchema.params.validate({ console_id: template })).toEqual({ + expect(readConsoleRequestBody.params.validate({ console_id: template })).toEqual({ console_id: template, }); } diff --git a/x-pack/plugins/security_solution/server/lib/risk_score/prebuilt_saved_objects/routes/create_prebuilt_saved_objects.ts b/x-pack/plugins/security_solution/server/lib/risk_score/prebuilt_saved_objects/routes/create_prebuilt_saved_objects.ts index ffdb693278dd1..5c0235418a241 100644 --- a/x-pack/plugins/security_solution/server/lib/risk_score/prebuilt_saved_objects/routes/create_prebuilt_saved_objects.ts +++ b/x-pack/plugins/security_solution/server/lib/risk_score/prebuilt_saved_objects/routes/create_prebuilt_saved_objects.ts @@ -16,7 +16,7 @@ import { buildSiemResponse } from '../../../detection_engine/routes/utils'; import { buildFrameworkRequest } from '../../../timeline/utils/common'; import { bulkCreateSavedObjects } from '../helpers/bulk_create_saved_objects'; -import { createPrebuiltSavedObjectsSchema } from '../schema'; +import { createPrebuiltSavedObjectsRequestBody } from '../../../../../common/api/risk_score'; export const createPrebuiltSavedObjectsRoute = ( router: SecuritySolutionPluginRouter, @@ -26,7 +26,7 @@ export const createPrebuiltSavedObjectsRoute = ( router.post( { path: PREBUILT_SAVED_OBJECTS_BULK_CREATE, - validate: createPrebuiltSavedObjectsSchema, + validate: createPrebuiltSavedObjectsRequestBody, options: { tags: ['access:securitySolution'], }, diff --git a/x-pack/plugins/security_solution/server/lib/risk_score/prebuilt_saved_objects/routes/delete_prebuilt_saved_objects.ts b/x-pack/plugins/security_solution/server/lib/risk_score/prebuilt_saved_objects/routes/delete_prebuilt_saved_objects.ts index 825dd0049e35b..fc3ce460e19bb 100644 --- a/x-pack/plugins/security_solution/server/lib/risk_score/prebuilt_saved_objects/routes/delete_prebuilt_saved_objects.ts +++ b/x-pack/plugins/security_solution/server/lib/risk_score/prebuilt_saved_objects/routes/delete_prebuilt_saved_objects.ts @@ -16,7 +16,7 @@ import { buildSiemResponse } from '../../../detection_engine/routes/utils'; import { buildFrameworkRequest } from '../../../timeline/utils/common'; import { bulkDeleteSavedObjects } from '../helpers/bulk_delete_saved_objects'; -import { deletePrebuiltSavedObjectsSchema } from '../schema'; +import { deletePrebuiltSavedObjectsRequestBody } from '../../../../../common/api/risk_score'; export const deletePrebuiltSavedObjectsRoute = ( router: SecuritySolutionPluginRouter, @@ -25,7 +25,7 @@ export const deletePrebuiltSavedObjectsRoute = ( router.post( { path: PREBUILT_SAVED_OBJECTS_BULK_DELETE, - validate: deletePrebuiltSavedObjectsSchema, + validate: deletePrebuiltSavedObjectsRequestBody, options: { tags: ['access:securitySolution'], }, diff --git a/x-pack/plugins/security_solution/server/lib/risk_score/stored_scripts/create_script_route.ts b/x-pack/plugins/security_solution/server/lib/risk_score/stored_scripts/create_script_route.ts index 0e19e160ea944..86969b76461b2 100644 --- a/x-pack/plugins/security_solution/server/lib/risk_score/stored_scripts/create_script_route.ts +++ b/x-pack/plugins/security_solution/server/lib/risk_score/stored_scripts/create_script_route.ts @@ -8,15 +8,16 @@ import type { Logger } from '@kbn/core/server'; import { buildSiemResponse } from '@kbn/lists-plugin/server/routes/utils'; import { transformError } from '@kbn/securitysolution-es-utils'; +import { createStoredScriptRequestBody } from '../../../../common/api/risk_score'; import { RISK_SCORE_CREATE_STORED_SCRIPT } from '../../../../common/constants'; import type { SecuritySolutionPluginRouter } from '../../../types'; -import { createStoredScriptBodySchema, createStoredScript } from './lib/create_script'; +import { createStoredScript } from './lib/create_script'; export const createStoredScriptRoute = (router: SecuritySolutionPluginRouter, logger: Logger) => { router.put( { path: RISK_SCORE_CREATE_STORED_SCRIPT, - validate: { body: createStoredScriptBodySchema }, + validate: { body: createStoredScriptRequestBody }, options: { tags: ['access:securitySolution'], }, diff --git a/x-pack/plugins/security_solution/server/lib/risk_score/stored_scripts/delete_script_route.ts b/x-pack/plugins/security_solution/server/lib/risk_score/stored_scripts/delete_script_route.ts index 753607966be62..f355c931b8fd4 100644 --- a/x-pack/plugins/security_solution/server/lib/risk_score/stored_scripts/delete_script_route.ts +++ b/x-pack/plugins/security_solution/server/lib/risk_score/stored_scripts/delete_script_route.ts @@ -9,13 +9,14 @@ import { transformError } from '@kbn/securitysolution-es-utils'; import { buildSiemResponse } from '@kbn/lists-plugin/server/routes/utils'; import { RISK_SCORE_DELETE_STORED_SCRIPT } from '../../../../common/constants'; import type { SecuritySolutionPluginRouter } from '../../../types'; -import { deleteStoredScriptBodySchema, deleteStoredScript } from './lib/delete_script'; +import { deleteStoredScript } from './lib/delete_script'; +import { deleteStoredScriptRequestBody } from '../../../../common/api/risk_score'; export const deleteStoredScriptRoute = (router: SecuritySolutionPluginRouter) => { router.delete( { path: RISK_SCORE_DELETE_STORED_SCRIPT, - validate: { body: deleteStoredScriptBodySchema }, + validate: { body: deleteStoredScriptRequestBody }, options: { tags: ['access:securitySolution'], }, diff --git a/x-pack/plugins/security_solution/server/lib/risk_score/stored_scripts/lib/create_script.ts b/x-pack/plugins/security_solution/server/lib/risk_score/stored_scripts/lib/create_script.ts index 6617021d92d6d..fc56a3e049269 100644 --- a/x-pack/plugins/security_solution/server/lib/risk_score/stored_scripts/lib/create_script.ts +++ b/x-pack/plugins/security_solution/server/lib/risk_score/stored_scripts/lib/create_script.ts @@ -4,27 +4,9 @@ * 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 { schema } from '@kbn/config-schema'; import type { ElasticsearchClient, Logger } from '@kbn/core/server'; import { transformError } from '@kbn/securitysolution-es-utils'; - -export const createStoredScriptBodySchema = schema.object({ - id: schema.string({ minLength: 1 }), - script: schema.object({ - lang: schema.oneOf([ - schema.string(), - schema.literal('painless'), - schema.literal('expression'), - schema.literal('mustache'), - schema.literal('java'), - ]), - options: schema.maybe(schema.recordOf(schema.string(), schema.string())), - source: schema.string(), - }), -}); - -type CreateStoredScriptBodySchema = TypeOf; +import type { CreateStoredScriptRequestBody } from '../../../../../common/api/risk_score'; export const createStoredScript = async ({ esClient, @@ -33,7 +15,7 @@ export const createStoredScript = async ({ }: { esClient: ElasticsearchClient; logger: Logger; - options: CreateStoredScriptBodySchema; + options: CreateStoredScriptRequestBody; }) => { try { await esClient.putScript(options); diff --git a/x-pack/plugins/security_solution/server/lib/risk_score/stored_scripts/lib/delete_script.ts b/x-pack/plugins/security_solution/server/lib/risk_score/stored_scripts/lib/delete_script.ts index c4adefc1674ef..b6113b5f9f318 100644 --- a/x-pack/plugins/security_solution/server/lib/risk_score/stored_scripts/lib/delete_script.ts +++ b/x-pack/plugins/security_solution/server/lib/risk_score/stored_scripts/lib/delete_script.ts @@ -4,22 +4,15 @@ * 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 { schema } from '@kbn/config-schema'; import type { IScopedClusterClient } from '@kbn/core-elasticsearch-server'; - -export const deleteStoredScriptBodySchema = schema.object({ - id: schema.string({ minLength: 1 }), -}); - -type DeleteStoredScriptBodySchema = TypeOf; +import type { DeleteStoredScriptRequestBody } from '../../../../../common/api/risk_score'; export const deleteStoredScript = async ({ client, options, }: { client: IScopedClusterClient; - options: DeleteStoredScriptBodySchema; + options: DeleteStoredScriptRequestBody; }) => { await client.asCurrentUser.deleteScript(options); };