From a8f28df839c547be2b6e118869e05f79055d545f Mon Sep 17 00:00:00 2001 From: Tomasz Ciecierski Date: Tue, 1 Aug 2023 13:00:10 +0200 Subject: [PATCH] [Defend workflows] Establish /common/api folder in osquery plugin (#161944) --- .../kbn-io-ts-utils/src/to_number_rt/index.ts | 2 +- .../asset/get_assets_status_route.ts} | 8 ++- .../api/asset/update_assets_status_route.ts | 14 +++++ .../fleet_wrapper/get_agent_details_route.ts | 14 +++++ .../fleet_wrapper/get_agent_policies_route.ts | 20 +++++++ .../fleet_wrapper/get_agent_policy_route.ts | 16 ++++++ ...get_agent_status_for_agent_policy_route.ts | 23 ++++++++ .../fleet_wrapper/get_agents_route.ts} | 6 +- .../get_package_policies_route.ts | 14 +++++ x-pack/plugins/osquery/common/api/index.ts | 31 +++++++++++ .../live_query/create_live_query_route.ts} | 0 .../api/live_query/find_live_query_route.ts | 19 +++++++ .../get_live_query_details_route.ts | 22 ++++++++ .../get_live_query_results_route.ts | 30 ++++++++++ .../types => api/model}/default_uuid.test.ts | 0 .../types => api/model}/default_uuid.ts | 0 .../model}/non_empty_string.test.ts | 0 .../types => api/model}/non_empty_string.ts | 0 .../common/api/packs/create_pack_route.ts | 40 ++++++++++++++ .../common/api/packs/delete_packs_route.ts | 14 +++++ .../common/api/packs/find_packs_route.ts | 18 ++++++ .../packs/read_packs_route.ts} | 8 ++- .../common/api/packs/update_packs_route.ts | 49 +++++++++++++++++ .../saved_query/create_saved_query_route.ts} | 0 .../saved_query/delete_saved_query_route.ts | 16 ++++++ .../api/saved_query/find_saved_query_route.ts | 18 ++++++ .../api/saved_query/read_saved_query_route.ts | 16 ++++++ .../saved_query/update_saved_query_route.ts | 42 ++++++++++++++ .../common/utils.ts => utils/converters.ts} | 0 .../use_create_live_query_action.tsx | 2 +- .../packs/form/shards/pack_shards_field.tsx | 4 +- .../public/packs/form/shards/shards_form.tsx | 2 +- .../queries/ecs_mapping_editor_field.tsx | 36 ++++++------ .../packs/queries/use_pack_query_form.tsx | 2 +- x-pack/plugins/osquery/public/packs/types.ts | 2 +- .../handlers/action/create_action_handler.ts | 2 +- .../handlers/action/create_action_service.ts | 2 +- .../server/handlers/action/create_queries.ts | 2 +- .../routes/asset/get_assets_status_route.ts | 9 ++- .../routes/asset/update_assets_route.ts | 9 ++- .../routes/fleet_wrapper/get_agent_details.ts | 9 ++- .../fleet_wrapper/get_agent_policies.ts | 20 ++++++- .../routes/fleet_wrapper/get_agent_policy.ts | 11 ++-- .../get_agent_status_for_agent_policy.ts | 23 ++++++-- .../server/routes/fleet_wrapper/get_agents.ts | 9 ++- .../fleet_wrapper/get_package_policies.ts | 9 ++- .../live_query/create_live_query_route.ts | 4 +- .../live_query/find_live_query_route.ts | 20 +++---- .../get_live_query_details_route.ts | 27 ++++++--- .../get_live_query_results_route.ts | 37 ++++++------- .../server/routes/pack/create_pack_route.ts | 40 +++----------- .../server/routes/pack/delete_pack_route.ts | 11 ++-- .../server/routes/pack/find_pack_route.ts | 19 +++---- .../server/routes/pack/read_pack_route.ts | 11 ++-- .../server/routes/pack/update_pack_route.ts | 55 +++++-------------- .../osquery/server/routes/pack/utils.ts | 2 +- .../saved_query/create_saved_query_route.ts | 4 +- .../saved_query/delete_saved_query_route.ts | 11 ++-- .../saved_query/find_saved_query_route.ts | 22 ++++---- .../saved_query/read_saved_query_route.ts | 11 ++-- .../saved_query/update_saved_query_route.ts | 45 ++++++--------- x-pack/plugins/osquery/server/routes/utils.ts | 2 +- x-pack/plugins/osquery/tsconfig.json | 3 +- 63 files changed, 672 insertions(+), 245 deletions(-) rename x-pack/plugins/osquery/common/{schemas/routes/saved_query/index.ts => api/asset/get_assets_status_route.ts} (56%) create mode 100644 x-pack/plugins/osquery/common/api/asset/update_assets_status_route.ts create mode 100644 x-pack/plugins/osquery/common/api/fleet_wrapper/get_agent_details_route.ts create mode 100644 x-pack/plugins/osquery/common/api/fleet_wrapper/get_agent_policies_route.ts create mode 100644 x-pack/plugins/osquery/common/api/fleet_wrapper/get_agent_policy_route.ts create mode 100644 x-pack/plugins/osquery/common/api/fleet_wrapper/get_agent_status_for_agent_policy_route.ts rename x-pack/plugins/osquery/common/{schemas/common/index.ts => api/fleet_wrapper/get_agents_route.ts} (59%) create mode 100644 x-pack/plugins/osquery/common/api/fleet_wrapper/get_package_policies_route.ts create mode 100644 x-pack/plugins/osquery/common/api/index.ts rename x-pack/plugins/osquery/common/{schemas/routes/live_query/create_live_query_request_body_schema.ts => api/live_query/create_live_query_route.ts} (100%) create mode 100644 x-pack/plugins/osquery/common/api/live_query/find_live_query_route.ts create mode 100644 x-pack/plugins/osquery/common/api/live_query/get_live_query_details_route.ts create mode 100644 x-pack/plugins/osquery/common/api/live_query/get_live_query_results_route.ts rename x-pack/plugins/osquery/common/{schemas/types => api/model}/default_uuid.test.ts (100%) rename x-pack/plugins/osquery/common/{schemas/types => api/model}/default_uuid.ts (100%) rename x-pack/plugins/osquery/common/{schemas/types => api/model}/non_empty_string.test.ts (100%) rename x-pack/plugins/osquery/common/{schemas/types => api/model}/non_empty_string.ts (100%) create mode 100644 x-pack/plugins/osquery/common/api/packs/create_pack_route.ts create mode 100644 x-pack/plugins/osquery/common/api/packs/delete_packs_route.ts create mode 100644 x-pack/plugins/osquery/common/api/packs/find_packs_route.ts rename x-pack/plugins/osquery/common/{schemas/routes/live_query/index.ts => api/packs/read_packs_route.ts} (56%) create mode 100644 x-pack/plugins/osquery/common/api/packs/update_packs_route.ts rename x-pack/plugins/osquery/common/{schemas/routes/saved_query/create_saved_query_request_schema.ts => api/saved_query/create_saved_query_route.ts} (100%) create mode 100644 x-pack/plugins/osquery/common/api/saved_query/delete_saved_query_route.ts create mode 100644 x-pack/plugins/osquery/common/api/saved_query/find_saved_query_route.ts create mode 100644 x-pack/plugins/osquery/common/api/saved_query/read_saved_query_route.ts create mode 100644 x-pack/plugins/osquery/common/api/saved_query/update_saved_query_route.ts rename x-pack/plugins/osquery/common/{schemas/common/utils.ts => utils/converters.ts} (100%) diff --git a/packages/kbn-io-ts-utils/src/to_number_rt/index.ts b/packages/kbn-io-ts-utils/src/to_number_rt/index.ts index ae6bb4911c907e..2edf7816981908 100644 --- a/packages/kbn-io-ts-utils/src/to_number_rt/index.ts +++ b/packages/kbn-io-ts-utils/src/to_number_rt/index.ts @@ -8,7 +8,7 @@ import * as t from 'io-ts'; -export const toNumberRt = new t.Type( +export const toNumberRt = new t.Type( 'ToNumber', t.number.is, (input, context) => { diff --git a/x-pack/plugins/osquery/common/schemas/routes/saved_query/index.ts b/x-pack/plugins/osquery/common/api/asset/get_assets_status_route.ts similarity index 56% rename from x-pack/plugins/osquery/common/schemas/routes/saved_query/index.ts rename to x-pack/plugins/osquery/common/api/asset/get_assets_status_route.ts index 6fbd10c7db6ca4..cfb19da0784edd 100644 --- a/x-pack/plugins/osquery/common/schemas/routes/saved_query/index.ts +++ b/x-pack/plugins/osquery/common/api/asset/get_assets_status_route.ts @@ -5,4 +5,10 @@ * 2.0. */ -export * from './create_saved_query_request_schema'; +import * as t from 'io-ts'; + +export const getAssetsStatusRequestParamsSchema = t.unknown; + +export type GetAssetsStatusRequestParamsSchema = t.OutputOf< + typeof getAssetsStatusRequestParamsSchema +>; diff --git a/x-pack/plugins/osquery/common/api/asset/update_assets_status_route.ts b/x-pack/plugins/osquery/common/api/asset/update_assets_status_route.ts new file mode 100644 index 00000000000000..9c1b4a60be16ea --- /dev/null +++ b/x-pack/plugins/osquery/common/api/asset/update_assets_status_route.ts @@ -0,0 +1,14 @@ +/* + * 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 * as t from 'io-ts'; + +export const updateAssetsStatusRequestParamsSchema = t.unknown; + +export type UpdateAssetsStatusRequestParamsSchema = t.OutputOf< + typeof updateAssetsStatusRequestParamsSchema +>; diff --git a/x-pack/plugins/osquery/common/api/fleet_wrapper/get_agent_details_route.ts b/x-pack/plugins/osquery/common/api/fleet_wrapper/get_agent_details_route.ts new file mode 100644 index 00000000000000..fcc7dad089babb --- /dev/null +++ b/x-pack/plugins/osquery/common/api/fleet_wrapper/get_agent_details_route.ts @@ -0,0 +1,14 @@ +/* + * 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 * as t from 'io-ts'; + +export const getAgentDetailsRequestParamsSchema = t.unknown; + +export type GetAgentDetailsRequestParamsSchema = t.OutputOf< + typeof getAgentDetailsRequestParamsSchema +>; diff --git a/x-pack/plugins/osquery/common/api/fleet_wrapper/get_agent_policies_route.ts b/x-pack/plugins/osquery/common/api/fleet_wrapper/get_agent_policies_route.ts new file mode 100644 index 00000000000000..84a68e5fbf4c70 --- /dev/null +++ b/x-pack/plugins/osquery/common/api/fleet_wrapper/get_agent_policies_route.ts @@ -0,0 +1,20 @@ +/* + * 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 * as t from 'io-ts'; + +export const getAgentPoliciesRequestParamsSchema = t.unknown; + +export type GetAgentPoliciesRequestParamsSchema = t.OutputOf< + typeof getAgentPoliciesRequestParamsSchema +>; + +export const getAgentPoliciesRequestQuerySchema = t.unknown; + +export type GetAgentPoliciesRequestQuerySchema = t.OutputOf< + typeof getAgentPoliciesRequestQuerySchema +>; diff --git a/x-pack/plugins/osquery/common/api/fleet_wrapper/get_agent_policy_route.ts b/x-pack/plugins/osquery/common/api/fleet_wrapper/get_agent_policy_route.ts new file mode 100644 index 00000000000000..9ec047978051e9 --- /dev/null +++ b/x-pack/plugins/osquery/common/api/fleet_wrapper/get_agent_policy_route.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. + */ + +import * as t from 'io-ts'; + +export const getAgentPolicyRequestParamsSchema = t.type({ + id: t.string, +}); + +export type GetAgentPolicyRequestParamsSchema = t.OutputOf< + typeof getAgentPolicyRequestParamsSchema +>; diff --git a/x-pack/plugins/osquery/common/api/fleet_wrapper/get_agent_status_for_agent_policy_route.ts b/x-pack/plugins/osquery/common/api/fleet_wrapper/get_agent_status_for_agent_policy_route.ts new file mode 100644 index 00000000000000..83d3ebb7a98670 --- /dev/null +++ b/x-pack/plugins/osquery/common/api/fleet_wrapper/get_agent_status_for_agent_policy_route.ts @@ -0,0 +1,23 @@ +/* + * 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 * as t from 'io-ts'; + +export const getAgentStatusForAgentPolicyRequestParamsSchema = t.unknown; + +export type GetAgentStatusForAgentPolicyRequestParamsSchema = t.OutputOf< + typeof getAgentStatusForAgentPolicyRequestParamsSchema +>; + +export const getAgentStatusForAgentPolicyRequestQuerySchema = t.type({ + policyId: t.string, + kuery: t.union([t.string, t.undefined]), +}); + +export type GetAgentStatusForAgentPolicyRequestQuerySchema = t.OutputOf< + typeof getAgentStatusForAgentPolicyRequestQuerySchema +>; diff --git a/x-pack/plugins/osquery/common/schemas/common/index.ts b/x-pack/plugins/osquery/common/api/fleet_wrapper/get_agents_route.ts similarity index 59% rename from x-pack/plugins/osquery/common/schemas/common/index.ts rename to x-pack/plugins/osquery/common/api/fleet_wrapper/get_agents_route.ts index 2478c7bb4f7fa6..667167a9bb3dfa 100644 --- a/x-pack/plugins/osquery/common/schemas/common/index.ts +++ b/x-pack/plugins/osquery/common/api/fleet_wrapper/get_agents_route.ts @@ -5,4 +5,8 @@ * 2.0. */ -export * from './utils'; +import * as t from 'io-ts'; + +export const getAgentsRequestQuerySchema = t.unknown; + +export type GetAgentsRequestQuerySchema = t.OutputOf; diff --git a/x-pack/plugins/osquery/common/api/fleet_wrapper/get_package_policies_route.ts b/x-pack/plugins/osquery/common/api/fleet_wrapper/get_package_policies_route.ts new file mode 100644 index 00000000000000..31a20196e9b981 --- /dev/null +++ b/x-pack/plugins/osquery/common/api/fleet_wrapper/get_package_policies_route.ts @@ -0,0 +1,14 @@ +/* + * 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 * as t from 'io-ts'; + +export const getPackagePoliciesRequestQuerySchema = t.unknown; + +export type GetPackagePoliciesRequestQuerySchema = t.OutputOf< + typeof getPackagePoliciesRequestQuerySchema +>; diff --git a/x-pack/plugins/osquery/common/api/index.ts b/x-pack/plugins/osquery/common/api/index.ts new file mode 100644 index 00000000000000..681eaab583ca88 --- /dev/null +++ b/x-pack/plugins/osquery/common/api/index.ts @@ -0,0 +1,31 @@ +/* + * 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 './asset/get_assets_status_route'; +export * from './asset/update_assets_status_route'; +export * from './fleet_wrapper/get_agent_policies_route'; +export * from './fleet_wrapper/get_agent_details_route'; +export * from './fleet_wrapper/get_agent_policy_route'; +export * from './fleet_wrapper/get_agent_status_for_agent_policy_route'; +export * from './fleet_wrapper/get_agents_route'; +export * from './fleet_wrapper/get_package_policies_route'; +export * from './live_query/create_live_query_route'; +export * from './live_query/find_live_query_route'; +export * from './live_query/get_live_query_results_route'; +export * from './live_query/get_live_query_details_route'; +export * from './saved_query/create_saved_query_route'; +export * from './saved_query/delete_saved_query_route'; +export * from './saved_query/find_saved_query_route'; +export * from './saved_query/update_saved_query_route'; +export * from './saved_query/read_saved_query_route'; +export * from './packs/create_pack_route'; +export * from './packs/delete_packs_route'; +export * from './packs/find_packs_route'; +export * from './packs/read_packs_route'; +export * from './packs/update_packs_route'; +export * from './model/default_uuid'; +export * from './model/non_empty_string'; diff --git a/x-pack/plugins/osquery/common/schemas/routes/live_query/create_live_query_request_body_schema.ts b/x-pack/plugins/osquery/common/api/live_query/create_live_query_route.ts similarity index 100% rename from x-pack/plugins/osquery/common/schemas/routes/live_query/create_live_query_request_body_schema.ts rename to x-pack/plugins/osquery/common/api/live_query/create_live_query_route.ts diff --git a/x-pack/plugins/osquery/common/api/live_query/find_live_query_route.ts b/x-pack/plugins/osquery/common/api/live_query/find_live_query_route.ts new file mode 100644 index 00000000000000..110466a7064573 --- /dev/null +++ b/x-pack/plugins/osquery/common/api/live_query/find_live_query_route.ts @@ -0,0 +1,19 @@ +/* + * 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 * as t from 'io-ts'; +import { toNumberRt } from '@kbn/io-ts-utils'; + +export const findLiveQueryRequestQuerySchema = t.type({ + filterQuery: t.union([t.string, t.undefined]), + page: t.union([toNumberRt, t.undefined]), + pageSize: t.union([toNumberRt, t.undefined]), + sort: t.union([t.string, t.undefined]), + sortOrder: t.union([t.union([t.literal('asc'), t.literal('desc')]), t.undefined]), +}); + +export type FindLiveQueryRequestQuerySchema = t.OutputOf; diff --git a/x-pack/plugins/osquery/common/api/live_query/get_live_query_details_route.ts b/x-pack/plugins/osquery/common/api/live_query/get_live_query_details_route.ts new file mode 100644 index 00000000000000..776eeb13946b1e --- /dev/null +++ b/x-pack/plugins/osquery/common/api/live_query/get_live_query_details_route.ts @@ -0,0 +1,22 @@ +/* + * 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 * as t from 'io-ts'; + +export const getLiveQueryDetailsRequestQuerySchema = t.unknown; + +export type GetLiveQueryDetailsRequestQuerySchema = t.OutputOf< + typeof getLiveQueryDetailsRequestQuerySchema +>; + +export const getLiveQueryDetailsRequestParamsSchema = t.type({ + id: t.string, +}); + +export type GetLiveQueryDetailsRequestParamsSchema = t.OutputOf< + typeof getLiveQueryDetailsRequestParamsSchema +>; diff --git a/x-pack/plugins/osquery/common/api/live_query/get_live_query_results_route.ts b/x-pack/plugins/osquery/common/api/live_query/get_live_query_results_route.ts new file mode 100644 index 00000000000000..553e3eae10cd89 --- /dev/null +++ b/x-pack/plugins/osquery/common/api/live_query/get_live_query_results_route.ts @@ -0,0 +1,30 @@ +/* + * 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 * as t from 'io-ts'; +import { toNumberRt } from '@kbn/io-ts-utils'; + +export const getLiveQueryResultsRequestQuerySchema = t.type({ + filterQuery: t.union([t.string, t.undefined]), + page: t.union([toNumberRt, t.undefined]), + pageSize: t.union([toNumberRt, t.undefined]), + sort: t.union([t.string, t.undefined]), + sortOrder: t.union([t.union([t.literal('asc'), t.literal('desc')]), t.undefined]), +}); + +export type GetLiveQueryResultsRequestQuerySchema = t.OutputOf< + typeof getLiveQueryResultsRequestQuerySchema +>; + +export const getLiveQueryResultsRequestParamsSchema = t.type({ + id: t.string, + actionId: t.string, +}); + +export type GetLiveQueryResultsRequestParamsSchema = t.OutputOf< + typeof getLiveQueryResultsRequestParamsSchema +>; diff --git a/x-pack/plugins/osquery/common/schemas/types/default_uuid.test.ts b/x-pack/plugins/osquery/common/api/model/default_uuid.test.ts similarity index 100% rename from x-pack/plugins/osquery/common/schemas/types/default_uuid.test.ts rename to x-pack/plugins/osquery/common/api/model/default_uuid.test.ts diff --git a/x-pack/plugins/osquery/common/schemas/types/default_uuid.ts b/x-pack/plugins/osquery/common/api/model/default_uuid.ts similarity index 100% rename from x-pack/plugins/osquery/common/schemas/types/default_uuid.ts rename to x-pack/plugins/osquery/common/api/model/default_uuid.ts diff --git a/x-pack/plugins/osquery/common/schemas/types/non_empty_string.test.ts b/x-pack/plugins/osquery/common/api/model/non_empty_string.test.ts similarity index 100% rename from x-pack/plugins/osquery/common/schemas/types/non_empty_string.test.ts rename to x-pack/plugins/osquery/common/api/model/non_empty_string.test.ts diff --git a/x-pack/plugins/osquery/common/schemas/types/non_empty_string.ts b/x-pack/plugins/osquery/common/api/model/non_empty_string.ts similarity index 100% rename from x-pack/plugins/osquery/common/schemas/types/non_empty_string.ts rename to x-pack/plugins/osquery/common/api/model/non_empty_string.ts diff --git a/x-pack/plugins/osquery/common/api/packs/create_pack_route.ts b/x-pack/plugins/osquery/common/api/packs/create_pack_route.ts new file mode 100644 index 00000000000000..9205668d6f73d2 --- /dev/null +++ b/x-pack/plugins/osquery/common/api/packs/create_pack_route.ts @@ -0,0 +1,40 @@ +/* + * 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 * as t from 'io-ts'; +import { toNumberRt } from '@kbn/io-ts-utils'; + +export const createPackRequestBodySchema = t.type({ + name: t.string, + description: t.union([t.string, t.undefined]), + enabled: t.union([t.boolean, t.undefined]), + policy_ids: t.union([t.array(t.string), t.undefined]), + shards: t.record(t.string, toNumberRt), + queries: t.record( + t.string, + t.type({ + query: t.string, + interval: t.union([toNumberRt, t.undefined]), + snapshot: t.union([t.boolean, t.undefined]), + removed: t.union([t.boolean, t.undefined]), + platform: t.union([t.string, t.undefined]), + version: t.union([t.string, t.undefined]), + ecs_mapping: t.union([ + t.record( + t.string, + t.type({ + field: t.union([t.string, t.undefined]), + value: t.union([t.string, t.array(t.string), t.undefined]), + }) + ), + t.undefined, + ]), + }) + ), +}); + +export type CreatePackRequestBodySchema = t.OutputOf; diff --git a/x-pack/plugins/osquery/common/api/packs/delete_packs_route.ts b/x-pack/plugins/osquery/common/api/packs/delete_packs_route.ts new file mode 100644 index 00000000000000..2f2c6e9346a423 --- /dev/null +++ b/x-pack/plugins/osquery/common/api/packs/delete_packs_route.ts @@ -0,0 +1,14 @@ +/* + * 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 * as t from 'io-ts'; + +export const deletePacksRequestParamsSchema = t.type({ + id: t.string, +}); + +export type DeletePacksRequestParamsSchema = t.OutputOf; diff --git a/x-pack/plugins/osquery/common/api/packs/find_packs_route.ts b/x-pack/plugins/osquery/common/api/packs/find_packs_route.ts new file mode 100644 index 00000000000000..2313d4b4aa8d4b --- /dev/null +++ b/x-pack/plugins/osquery/common/api/packs/find_packs_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 * as t from 'io-ts'; +import { toNumberRt } from '@kbn/io-ts-utils'; + +export const findPacksRequestQuerySchema = t.type({ + page: t.union([toNumberRt, t.undefined]), + pageSize: t.union([toNumberRt, t.undefined]), + sort: t.union([t.string, t.undefined]), + sortOrder: t.union([t.union([t.literal('asc'), t.literal('desc')]), t.undefined]), +}); + +export type FindPacksRequestQuerySchema = t.OutputOf; diff --git a/x-pack/plugins/osquery/common/schemas/routes/live_query/index.ts b/x-pack/plugins/osquery/common/api/packs/read_packs_route.ts similarity index 56% rename from x-pack/plugins/osquery/common/schemas/routes/live_query/index.ts rename to x-pack/plugins/osquery/common/api/packs/read_packs_route.ts index 0438204e6c86e6..01865d014aab41 100644 --- a/x-pack/plugins/osquery/common/schemas/routes/live_query/index.ts +++ b/x-pack/plugins/osquery/common/api/packs/read_packs_route.ts @@ -5,4 +5,10 @@ * 2.0. */ -export * from './create_live_query_request_body_schema'; +import * as t from 'io-ts'; + +export const readPacksRequestParamsSchema = t.type({ + id: t.string, +}); + +export type ReadPacksRequestParamsSchema = t.OutputOf; diff --git a/x-pack/plugins/osquery/common/api/packs/update_packs_route.ts b/x-pack/plugins/osquery/common/api/packs/update_packs_route.ts new file mode 100644 index 00000000000000..0a61d88813ad52 --- /dev/null +++ b/x-pack/plugins/osquery/common/api/packs/update_packs_route.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 * as t from 'io-ts'; +import { toNumberRt } from '@kbn/io-ts-utils'; + +export const updatePacksRequestBodySchema = t.type({ + name: t.union([t.string, t.undefined]), + description: t.union([t.string, t.undefined]), + enabled: t.union([t.boolean, t.undefined]), + policy_ids: t.union([t.array(t.string), t.undefined]), + shards: t.union([t.record(t.string, toNumberRt), t.undefined]), + queries: t.union([ + t.record( + t.string, + t.type({ + query: t.string, + interval: t.union([toNumberRt, t.undefined]), + snapshot: t.union([t.boolean, t.undefined]), + removed: t.union([t.boolean, t.undefined]), + platform: t.union([t.string, t.undefined]), + version: t.union([t.string, t.undefined]), + ecs_mapping: t.union([ + t.record( + t.string, + t.type({ + field: t.union([t.string, t.undefined]), + value: t.union([t.string, t.array(t.string), t.undefined]), + }) + ), + t.undefined, + ]), + }) + ), + t.undefined, + ]), +}); + +export type UpdatePacksRequestBodySchema = t.OutputOf; + +export const updatePacksRequestParamsSchema = t.type({ + id: t.string, +}); + +export type UpdatePacksRequestParamsSchema = t.OutputOf; diff --git a/x-pack/plugins/osquery/common/schemas/routes/saved_query/create_saved_query_request_schema.ts b/x-pack/plugins/osquery/common/api/saved_query/create_saved_query_route.ts similarity index 100% rename from x-pack/plugins/osquery/common/schemas/routes/saved_query/create_saved_query_request_schema.ts rename to x-pack/plugins/osquery/common/api/saved_query/create_saved_query_route.ts diff --git a/x-pack/plugins/osquery/common/api/saved_query/delete_saved_query_route.ts b/x-pack/plugins/osquery/common/api/saved_query/delete_saved_query_route.ts new file mode 100644 index 00000000000000..5c671317e72e2f --- /dev/null +++ b/x-pack/plugins/osquery/common/api/saved_query/delete_saved_query_route.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. + */ + +import * as t from 'io-ts'; + +export const deleteSavedQueryRequestParamsSchema = t.type({ + id: t.string, +}); + +export type DeleteSavedQueryRequestParamsSchema = t.OutputOf< + typeof deleteSavedQueryRequestParamsSchema +>; diff --git a/x-pack/plugins/osquery/common/api/saved_query/find_saved_query_route.ts b/x-pack/plugins/osquery/common/api/saved_query/find_saved_query_route.ts new file mode 100644 index 00000000000000..10ce1dc26fd0f4 --- /dev/null +++ b/x-pack/plugins/osquery/common/api/saved_query/find_saved_query_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 * as t from 'io-ts'; +import { toNumberRt } from '@kbn/io-ts-utils'; + +export const findSavedQueryRequestQuerySchema = t.type({ + page: t.union([toNumberRt, t.undefined]), + pageSize: t.union([toNumberRt, t.undefined]), + sort: t.union([t.string, t.undefined]), + sortOrder: t.union([t.union([t.literal('asc'), t.literal('desc')]), t.undefined]), +}); + +export type FindSavedQueryRequestQuerySchema = t.OutputOf; diff --git a/x-pack/plugins/osquery/common/api/saved_query/read_saved_query_route.ts b/x-pack/plugins/osquery/common/api/saved_query/read_saved_query_route.ts new file mode 100644 index 00000000000000..43e9184901b077 --- /dev/null +++ b/x-pack/plugins/osquery/common/api/saved_query/read_saved_query_route.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. + */ + +import * as t from 'io-ts'; + +export const readSavedQueryRequestParamsSchema = t.type({ + id: t.string, +}); + +export type ReadSavedQueryRequestParamsSchema = t.OutputOf< + typeof readSavedQueryRequestParamsSchema +>; diff --git a/x-pack/plugins/osquery/common/api/saved_query/update_saved_query_route.ts b/x-pack/plugins/osquery/common/api/saved_query/update_saved_query_route.ts new file mode 100644 index 00000000000000..274b52094b1b4a --- /dev/null +++ b/x-pack/plugins/osquery/common/api/saved_query/update_saved_query_route.ts @@ -0,0 +1,42 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import * as t from 'io-ts'; +import { toNumberRt } from '@kbn/io-ts-utils'; + +export const updateSavedQueryRequestBodySchema = t.type({ + id: t.string, + query: t.string, + description: t.union([t.string, t.undefined]), + interval: t.union([toNumberRt, t.undefined]), + snapshot: t.union([t.boolean, t.undefined]), + removed: t.union([t.boolean, t.undefined]), + platform: t.union([t.string, t.undefined]), + version: t.union([t.string, t.undefined]), + ecs_mapping: t.union([ + t.record( + t.string, + t.type({ + field: t.union([t.string, t.undefined]), + value: t.union([t.string, t.array(t.string), t.undefined]), + }) + ), + t.undefined, + ]), +}); + +export type UpdateSavedQueryRequestBodySchema = t.OutputOf< + typeof updateSavedQueryRequestBodySchema +>; + +export const updateSavedQueryRequestParamsSchema = t.type({ + id: t.string, +}); + +export type UpdateSavedQueryRequestParamsSchema = t.OutputOf< + typeof updateSavedQueryRequestParamsSchema +>; diff --git a/x-pack/plugins/osquery/common/schemas/common/utils.ts b/x-pack/plugins/osquery/common/utils/converters.ts similarity index 100% rename from x-pack/plugins/osquery/common/schemas/common/utils.ts rename to x-pack/plugins/osquery/common/utils/converters.ts diff --git a/x-pack/plugins/osquery/public/live_queries/use_create_live_query_action.tsx b/x-pack/plugins/osquery/public/live_queries/use_create_live_query_action.tsx index 0fa7c538e6737b..ae2d840bc8eae6 100644 --- a/x-pack/plugins/osquery/public/live_queries/use_create_live_query_action.tsx +++ b/x-pack/plugins/osquery/public/live_queries/use_create_live_query_action.tsx @@ -7,8 +7,8 @@ import { useMutation } from '@tanstack/react-query'; import type { AgentSelection } from '@kbn/osquery-io-ts-types'; +import type { CreateLiveQueryRequestBodySchema } from '../../common/api'; import { API_VERSIONS } from '../../common/constants'; -import type { CreateLiveQueryRequestBodySchema } from '../../common/schemas/routes/live_query'; import { useKibana } from '../common/lib/kibana'; import { useErrorToast } from '../common/hooks/use_error_toast'; import type { LiveQueryDetailsItem } from '../actions/use_live_query_details'; diff --git a/x-pack/plugins/osquery/public/packs/form/shards/pack_shards_field.tsx b/x-pack/plugins/osquery/public/packs/form/shards/pack_shards_field.tsx index a658e5cb12fbb8..1320845bc80159 100644 --- a/x-pack/plugins/osquery/public/packs/form/shards/pack_shards_field.tsx +++ b/x-pack/plugins/osquery/public/packs/form/shards/pack_shards_field.tsx @@ -13,9 +13,9 @@ import { EuiFlexItem } from '@elastic/eui'; import { EuiSpacer } from '@elastic/eui'; import deepEqual from 'fast-deep-equal'; import { isEmpty, last, reject } from 'lodash'; +import { convertShardsToArray, convertShardsToObject } from '../../../../common/utils/converters'; +import type { ShardsArray } from '../../../../common/utils/converters'; import { useAgentPolicies } from '../../../agent_policies'; -import type { ShardsArray } from '../../../../common/schemas/common'; -import { convertShardsToArray, convertShardsToObject } from '../../../../common/schemas/common'; import { ShardsForm } from './shards_form'; export const defaultShardData = { diff --git a/x-pack/plugins/osquery/public/packs/form/shards/shards_form.tsx b/x-pack/plugins/osquery/public/packs/form/shards/shards_form.tsx index 892e223ded741f..c46666d3636606 100644 --- a/x-pack/plugins/osquery/public/packs/form/shards/shards_form.tsx +++ b/x-pack/plugins/osquery/public/packs/form/shards/shards_form.tsx @@ -11,7 +11,7 @@ import type { EuiComboBoxOptionOption } from '@elastic/eui'; import { EuiButtonIcon, EuiFlexGroup, EuiFlexItem, EuiSpacer } from '@elastic/eui'; import styled from 'styled-components'; import type { UseFieldArrayRemove, UseFormReturn } from 'react-hook-form'; -import type { ShardsArray } from '../../../../common/schemas/common/utils'; +import type { ShardsArray } from '../../../../common/utils/converters'; import { ShardsPolicyField } from './shards_policy_field'; import { ShardsPercentageField } from './shards_percentage_field'; import { overflowCss } from '../../utils'; diff --git a/x-pack/plugins/osquery/public/packs/queries/ecs_mapping_editor_field.tsx b/x-pack/plugins/osquery/public/packs/queries/ecs_mapping_editor_field.tsx index fbd9ea4f28d874..7705e0dfcab8ce 100644 --- a/x-pack/plugins/osquery/public/packs/queries/ecs_mapping_editor_field.tsx +++ b/x-pack/plugins/osquery/public/packs/queries/ecs_mapping_editor_field.tsx @@ -43,11 +43,11 @@ import type { FieldErrors, UseFieldArrayRemove, UseFormReturn } from 'react-hook import { useForm, useController, useFieldArray, useFormContext } from 'react-hook-form'; import type { ECSMapping } from '@kbn/osquery-io-ts-types'; -import type { ECSMappingArray } from '../../../common/schemas/common/utils'; +import type { ECSMappingArray } from '../../../common/utils/converters'; import { convertECSMappingToArray, convertECSMappingToObject, -} from '../../../common/schemas/common/utils'; +} from '../../../common/utils/converters'; import ECSSchema from '../../common/schemas/ecs/v8.7.0.json'; import osquerySchema from '../../common/schemas/osquery/v5.7.0.json'; @@ -927,8 +927,8 @@ export const ECSMappingEditorField = React.memo(({ euiFieldProps }: ECSMappingEd ?.map((selectItem: { type: string; name: string; alias?: string }) => { if (selectItem.type === 'identifier') { /* - select * from routes, uptime; - */ + select * from routes, uptime; + */ if (ast?.result.length === 1 && selectItem.name === '*') { return reduce( astOsqueryTables, @@ -953,8 +953,8 @@ export const ECSMappingEditorField = React.memo(({ euiFieldProps }: ECSMappingEd } /* - select i.*, p.resident_size, p.user_time, p.system_time, time.minutes as counter from osquery_info i, processes p, time where p.pid = i.pid; - */ + select i.*, p.resident_size, p.user_time, p.system_time, time.minutes as counter from osquery_info i, processes p, time where p.pid = i.pid; + */ const [table, column] = selectItem.name.includes('.') ? selectItem.name?.split('.') @@ -998,18 +998,18 @@ export const ECSMappingEditorField = React.memo(({ euiFieldProps }: ECSMappingEd } /* - SELECT pid, uid, name, ROUND(( - (user_time + system_time) / (cpu_time.tsb - cpu_time.itsb) - ) * 100, 2) AS percentage - FROM processes, ( - SELECT ( - SUM(user) + SUM(nice) + SUM(system) + SUM(idle) * 1.0) AS tsb, - SUM(COALESCE(idle, 0)) + SUM(COALESCE(iowait, 0)) AS itsb - FROM cpu_time - ) AS cpu_time - ORDER BY user_time+system_time DESC - LIMIT 5; - */ + SELECT pid, uid, name, ROUND(( + (user_time + system_time) / (cpu_time.tsb - cpu_time.itsb) + ) * 100, 2) AS percentage + FROM processes, ( + SELECT ( + SUM(user) + SUM(nice) + SUM(system) + SUM(idle) * 1.0) AS tsb, + SUM(COALESCE(idle, 0)) + SUM(COALESCE(iowait, 0)) AS itsb + FROM cpu_time + ) AS cpu_time + ORDER BY user_time+system_time DESC + LIMIT 5; + */ if (selectItem.type === 'function' && selectItem.alias) { return [ diff --git a/x-pack/plugins/osquery/public/packs/queries/use_pack_query_form.tsx b/x-pack/plugins/osquery/public/packs/queries/use_pack_query_form.tsx index d5f222af8f1a08..1a0ecdbe71c59a 100644 --- a/x-pack/plugins/osquery/public/packs/queries/use_pack_query_form.tsx +++ b/x-pack/plugins/osquery/public/packs/queries/use_pack_query_form.tsx @@ -11,7 +11,7 @@ import type { Draft } from 'immer'; import { produce } from 'immer'; import { useMemo } from 'react'; import type { ECSMapping } from '@kbn/osquery-io-ts-types'; -import type { Shard } from '../../../common/schemas/common/utils'; +import type { Shard } from '../../../common/utils/converters'; export interface UsePackQueryFormProps { uniqueQueryIds: string[]; diff --git a/x-pack/plugins/osquery/public/packs/types.ts b/x-pack/plugins/osquery/public/packs/types.ts index 071795c1d966f9..99ad6b1e8adf2d 100644 --- a/x-pack/plugins/osquery/public/packs/types.ts +++ b/x-pack/plugins/osquery/public/packs/types.ts @@ -4,7 +4,7 @@ * 2.0; you may not use this file except in compliance with the Elastic License * 2.0. */ -import type { Shard } from '../../common/schemas/common/utils'; +import type { Shard } from '../../common/utils/converters'; import type { PackQueryFormData } from './queries/use_pack_query_form'; export interface PackSavedObject { diff --git a/x-pack/plugins/osquery/server/handlers/action/create_action_handler.ts b/x-pack/plugins/osquery/server/handlers/action/create_action_handler.ts index 4a38cf383c6436..a6217f7c916fa4 100644 --- a/x-pack/plugins/osquery/server/handlers/action/create_action_handler.ts +++ b/x-pack/plugins/osquery/server/handlers/action/create_action_handler.ts @@ -11,12 +11,12 @@ import { filter, flatten, isEmpty, map, omit, pick, pickBy, some } from 'lodash' import { AGENT_ACTIONS_INDEX } from '@kbn/fleet-plugin/common'; import type { SavedObjectsClientContract } from '@kbn/core/server'; import type { ParsedTechnicalFields } from '@kbn/rule-registry-plugin/common'; +import type { CreateLiveQueryRequestBodySchema } from '../../../common/api'; import { createDynamicQueries, replacedQueries } from './create_queries'; import { getInternalSavedObjectsClient } from '../../routes/utils'; import { parseAgentSelection } from '../../lib/parse_agent_groups'; import { packSavedObjectType } from '../../../common/types'; import type { OsqueryAppContext } from '../../lib/osquery_app_context_services'; -import type { CreateLiveQueryRequestBodySchema } from '../../../common/schemas/routes/live_query'; import { convertSOQueriesToPack } from '../../routes/pack/utils'; import { ACTIONS_INDEX } from '../../../common/constants'; import { TELEMETRY_EBT_LIVE_QUERY_EVENT } from '../../lib/telemetry/constants'; diff --git a/x-pack/plugins/osquery/server/handlers/action/create_action_service.ts b/x-pack/plugins/osquery/server/handlers/action/create_action_service.ts index e0e8098fd16bdf..1ec052e0bbb77b 100644 --- a/x-pack/plugins/osquery/server/handlers/action/create_action_service.ts +++ b/x-pack/plugins/osquery/server/handlers/action/create_action_service.ts @@ -7,8 +7,8 @@ import type { ParsedTechnicalFields } from '@kbn/rule-registry-plugin/common'; import type { Subscription } from 'rxjs'; +import type { CreateLiveQueryRequestBodySchema } from '../../../common/api'; import type { OsqueryAppContext } from '../../lib/osquery_app_context_services'; -import type { CreateLiveQueryRequestBodySchema } from '../../../common/schemas/routes/live_query'; import type { OsqueryActiveLicenses } from './validate_license'; import { validateLicense } from './validate_license'; import { createActionHandler } from './create_action_handler'; diff --git a/x-pack/plugins/osquery/server/handlers/action/create_queries.ts b/x-pack/plugins/osquery/server/handlers/action/create_queries.ts index 4185353fdacff3..78092859a39900 100644 --- a/x-pack/plugins/osquery/server/handlers/action/create_queries.ts +++ b/x-pack/plugins/osquery/server/handlers/action/create_queries.ts @@ -9,9 +9,9 @@ import { isEmpty, map, pickBy } from 'lodash'; import { v4 as uuidv4 } from 'uuid'; import type { ParsedTechnicalFields } from '@kbn/rule-registry-plugin/common'; +import type { CreateLiveQueryRequestBodySchema } from '../../../common/api'; import { PARAMETER_NOT_FOUND } from '../../../common/translations/errors'; import type { OsqueryAppContext } from '../../lib/osquery_app_context_services'; -import type { CreateLiveQueryRequestBodySchema } from '../../../common/schemas/routes/live_query'; import { replaceParamsQuery } from '../../../common/utils/replace_params_query'; import { isSavedQueryPrebuilt } from '../../routes/saved_query/utils'; diff --git a/x-pack/plugins/osquery/server/routes/asset/get_assets_status_route.ts b/x-pack/plugins/osquery/server/routes/asset/get_assets_status_route.ts index a76ec52350adeb..f217b60bf24591 100644 --- a/x-pack/plugins/osquery/server/routes/asset/get_assets_status_route.ts +++ b/x-pack/plugins/osquery/server/routes/asset/get_assets_status_route.ts @@ -6,15 +6,17 @@ */ import { filter } from 'lodash/fp'; -import { schema } from '@kbn/config-schema'; import { asyncForEach } from '@kbn/std'; import type { IRouter } from '@kbn/core/server'; import type { KibanaAssetReference } from '@kbn/fleet-plugin/common'; +import type { GetAssetsStatusRequestParamsSchema } from '../../../common/api'; +import { buildRouteValidation } from '../../utils/build_validation/route_validation'; import { API_VERSIONS } from '../../../common/constants'; import { packAssetSavedObjectType, packSavedObjectType } from '../../../common/types'; import { PLUGIN_ID, OSQUERY_INTEGRATION_NAME } from '../../../common'; import type { OsqueryAppContext } from '../../lib/osquery_app_context_services'; +import { getAssetsStatusRequestParamsSchema } from '../../../common/api'; export const getAssetsStatusRoute = (router: IRouter, osqueryContext: OsqueryAppContext) => { router.versioned @@ -28,7 +30,10 @@ export const getAssetsStatusRoute = (router: IRouter, osqueryContext: OsqueryApp version: API_VERSIONS.internal.v1, validate: { request: { - params: schema.object({}, { unknowns: 'allow' }), + params: buildRouteValidation< + typeof getAssetsStatusRequestParamsSchema, + GetAssetsStatusRequestParamsSchema + >(getAssetsStatusRequestParamsSchema), }, }, }, diff --git a/x-pack/plugins/osquery/server/routes/asset/update_assets_route.ts b/x-pack/plugins/osquery/server/routes/asset/update_assets_route.ts index 5cfef0d84c2893..57800729d64354 100644 --- a/x-pack/plugins/osquery/server/routes/asset/update_assets_route.ts +++ b/x-pack/plugins/osquery/server/routes/asset/update_assets_route.ts @@ -7,12 +7,13 @@ import moment from 'moment-timezone'; import { filter, omit, some } from 'lodash'; -import { schema } from '@kbn/config-schema'; import { asyncForEach } from '@kbn/std'; import deepmerge from 'deepmerge'; import type { IRouter } from '@kbn/core/server'; import type { KibanaAssetReference } from '@kbn/fleet-plugin/common'; +import type { UpdateAssetsStatusRequestParamsSchema } from '../../../common/api'; +import { buildRouteValidation } from '../../utils/build_validation/route_validation'; import { API_VERSIONS } from '../../../common/constants'; import { packAssetSavedObjectType, packSavedObjectType } from '../../../common/types'; import { combineMerge } from './utils'; @@ -20,6 +21,7 @@ import { PLUGIN_ID, OSQUERY_INTEGRATION_NAME } from '../../../common'; import type { OsqueryAppContext } from '../../lib/osquery_app_context_services'; import { convertSOQueriesToPack, convertPackQueriesToSO } from '../pack/utils'; import type { PackSavedObject } from '../../common/types'; +import { updateAssetsStatusRequestParamsSchema } from '../../../common/api'; export const updateAssetsRoute = (router: IRouter, osqueryContext: OsqueryAppContext) => { router.versioned @@ -33,7 +35,10 @@ export const updateAssetsRoute = (router: IRouter, osqueryContext: OsqueryAppCon version: API_VERSIONS.internal.v1, validate: { request: { - params: schema.object({}, { unknowns: 'allow' }), + params: buildRouteValidation< + typeof updateAssetsStatusRequestParamsSchema, + UpdateAssetsStatusRequestParamsSchema + >(updateAssetsStatusRequestParamsSchema), }, }, }, diff --git a/x-pack/plugins/osquery/server/routes/fleet_wrapper/get_agent_details.ts b/x-pack/plugins/osquery/server/routes/fleet_wrapper/get_agent_details.ts index 65622569a446e9..b3b6539f9fc35a 100644 --- a/x-pack/plugins/osquery/server/routes/fleet_wrapper/get_agent_details.ts +++ b/x-pack/plugins/osquery/server/routes/fleet_wrapper/get_agent_details.ts @@ -5,11 +5,13 @@ * 2.0. */ -import { schema } from '@kbn/config-schema'; import type { IRouter } from '@kbn/core/server'; +import type { GetAgentDetailsRequestParamsSchema } from '../../../common/api'; +import { buildRouteValidation } from '../../utils/build_validation/route_validation'; import { API_VERSIONS } from '../../../common/constants'; import { PLUGIN_ID } from '../../../common'; import type { OsqueryAppContext } from '../../lib/osquery_app_context_services'; +import { getAgentDetailsRequestParamsSchema } from '../../../common/api'; export const getAgentDetailsRoute = (router: IRouter, osqueryContext: OsqueryAppContext) => { router.versioned @@ -23,7 +25,10 @@ export const getAgentDetailsRoute = (router: IRouter, osqueryContext: OsqueryApp version: API_VERSIONS.internal.v1, validate: { request: { - params: schema.object({}, { unknowns: 'allow' }), + params: buildRouteValidation< + typeof getAgentDetailsRequestParamsSchema, + GetAgentDetailsRequestParamsSchema + >(getAgentDetailsRequestParamsSchema), }, }, }, diff --git a/x-pack/plugins/osquery/server/routes/fleet_wrapper/get_agent_policies.ts b/x-pack/plugins/osquery/server/routes/fleet_wrapper/get_agent_policies.ts index 0fc992c69760ba..af02b0ae97e329 100644 --- a/x-pack/plugins/osquery/server/routes/fleet_wrapper/get_agent_policies.ts +++ b/x-pack/plugins/osquery/server/routes/fleet_wrapper/get_agent_policies.ts @@ -6,16 +6,24 @@ */ import pMap from 'p-map'; -import { schema } from '@kbn/config-schema'; import { filter, uniq, map } from 'lodash'; import { satisfies } from 'semver'; import type { GetAgentPoliciesResponseItem, PackagePolicy } from '@kbn/fleet-plugin/common'; import { PACKAGE_POLICY_SAVED_OBJECT_TYPE } from '@kbn/fleet-plugin/common'; import type { IRouter } from '@kbn/core/server'; +import type { + GetAgentPoliciesRequestParamsSchema, + GetAgentPoliciesRequestQuerySchema, +} from '../../../common/api'; +import { buildRouteValidation } from '../../utils/build_validation/route_validation'; import { API_VERSIONS } from '../../../common/constants'; import { OSQUERY_INTEGRATION_NAME, PLUGIN_ID } from '../../../common'; import type { OsqueryAppContext } from '../../lib/osquery_app_context_services'; import { getInternalSavedObjectsClient } from '../utils'; +import { + getAgentPoliciesRequestParamsSchema, + getAgentPoliciesRequestQuerySchema, +} from '../../../common/api'; export const getAgentPoliciesRoute = (router: IRouter, osqueryContext: OsqueryAppContext) => { router.versioned @@ -29,8 +37,14 @@ export const getAgentPoliciesRoute = (router: IRouter, osqueryContext: OsqueryAp version: API_VERSIONS.internal.v1, validate: { request: { - params: schema.object({}, { unknowns: 'allow' }), - query: schema.object({}, { unknowns: 'allow' }), + params: buildRouteValidation< + typeof getAgentPoliciesRequestParamsSchema, + GetAgentPoliciesRequestParamsSchema + >(getAgentPoliciesRequestParamsSchema), + query: buildRouteValidation< + typeof getAgentPoliciesRequestQuerySchema, + GetAgentPoliciesRequestQuerySchema + >(getAgentPoliciesRequestQuerySchema), }, }, }, diff --git a/x-pack/plugins/osquery/server/routes/fleet_wrapper/get_agent_policy.ts b/x-pack/plugins/osquery/server/routes/fleet_wrapper/get_agent_policy.ts index b5fe59aa9d34df..85de68f7e44d9a 100644 --- a/x-pack/plugins/osquery/server/routes/fleet_wrapper/get_agent_policy.ts +++ b/x-pack/plugins/osquery/server/routes/fleet_wrapper/get_agent_policy.ts @@ -5,12 +5,14 @@ * 2.0. */ -import { schema } from '@kbn/config-schema'; import type { IRouter } from '@kbn/core/server'; +import type { GetAgentPolicyRequestParamsSchema } from '../../../common/api'; +import { buildRouteValidation } from '../../utils/build_validation/route_validation'; import { API_VERSIONS } from '../../../common/constants'; import { PLUGIN_ID } from '../../../common'; import type { OsqueryAppContext } from '../../lib/osquery_app_context_services'; import { getInternalSavedObjectsClient } from '../utils'; +import { getAgentPolicyRequestParamsSchema } from '../../../common/api'; export const getAgentPolicyRoute = (router: IRouter, osqueryContext: OsqueryAppContext) => { router.versioned @@ -24,9 +26,10 @@ export const getAgentPolicyRoute = (router: IRouter, osqueryContext: OsqueryAppC version: API_VERSIONS.internal.v1, validate: { request: { - params: schema.object({ - id: schema.string(), - }), + params: buildRouteValidation< + typeof getAgentPolicyRequestParamsSchema, + GetAgentPolicyRequestParamsSchema + >(getAgentPolicyRequestParamsSchema), }, }, }, diff --git a/x-pack/plugins/osquery/server/routes/fleet_wrapper/get_agent_status_for_agent_policy.ts b/x-pack/plugins/osquery/server/routes/fleet_wrapper/get_agent_status_for_agent_policy.ts index 61a7e24ddaa766..64cd9d9f8ddd06 100644 --- a/x-pack/plugins/osquery/server/routes/fleet_wrapper/get_agent_status_for_agent_policy.ts +++ b/x-pack/plugins/osquery/server/routes/fleet_wrapper/get_agent_status_for_agent_policy.ts @@ -5,12 +5,20 @@ * 2.0. */ -import { schema } from '@kbn/config-schema'; import type { GetAgentStatusResponse } from '@kbn/fleet-plugin/common'; import type { IRouter } from '@kbn/core/server'; +import type { + GetAgentStatusForAgentPolicyRequestParamsSchema, + GetAgentStatusForAgentPolicyRequestQuerySchema, +} from '../../../common/api'; +import { buildRouteValidation } from '../../utils/build_validation/route_validation'; import { API_VERSIONS } from '../../../common/constants'; import { PLUGIN_ID } from '../../../common'; import type { OsqueryAppContext } from '../../lib/osquery_app_context_services'; +import { + getAgentStatusForAgentPolicyRequestParamsSchema, + getAgentStatusForAgentPolicyRequestQuerySchema, +} from '../../../common/api'; export const getAgentStatusForAgentPolicyRoute = ( router: IRouter, @@ -27,11 +35,14 @@ export const getAgentStatusForAgentPolicyRoute = ( version: API_VERSIONS.internal.v1, validate: { request: { - query: schema.object({ - policyId: schema.string(), - kuery: schema.maybe(schema.string()), - }), - params: schema.object({}, { unknowns: 'allow' }), + params: buildRouteValidation< + typeof getAgentStatusForAgentPolicyRequestParamsSchema, + GetAgentStatusForAgentPolicyRequestParamsSchema + >(getAgentStatusForAgentPolicyRequestParamsSchema), + query: buildRouteValidation< + typeof getAgentStatusForAgentPolicyRequestQuerySchema, + GetAgentStatusForAgentPolicyRequestQuerySchema + >(getAgentStatusForAgentPolicyRequestQuerySchema), }, }, }, diff --git a/x-pack/plugins/osquery/server/routes/fleet_wrapper/get_agents.ts b/x-pack/plugins/osquery/server/routes/fleet_wrapper/get_agents.ts index 9b86261aa07b42..44ce43305e697b 100644 --- a/x-pack/plugins/osquery/server/routes/fleet_wrapper/get_agents.ts +++ b/x-pack/plugins/osquery/server/routes/fleet_wrapper/get_agents.ts @@ -5,8 +5,10 @@ * 2.0. */ -import { schema } from '@kbn/config-schema'; import type { IRouter } from '@kbn/core/server'; +import { getAgentsRequestQuerySchema } from '../../../common/api'; +import type { GetAgentsRequestQuerySchema } from '../../../common/api'; +import { buildRouteValidation } from '../../utils/build_validation/route_validation'; import { API_VERSIONS } from '../../../common/constants'; import { PLUGIN_ID } from '../../../common'; import type { OsqueryAppContext } from '../../lib/osquery_app_context_services'; @@ -23,7 +25,10 @@ export const getAgentsRoute = (router: IRouter, osqueryContext: OsqueryAppContex version: API_VERSIONS.internal.v1, validate: { request: { - query: schema.object({}, { unknowns: 'allow' }), + query: buildRouteValidation< + typeof getAgentsRequestQuerySchema, + GetAgentsRequestQuerySchema + >(getAgentsRequestQuerySchema), }, }, }, diff --git a/x-pack/plugins/osquery/server/routes/fleet_wrapper/get_package_policies.ts b/x-pack/plugins/osquery/server/routes/fleet_wrapper/get_package_policies.ts index cbf5414eb4f879..887fa4811e73e4 100644 --- a/x-pack/plugins/osquery/server/routes/fleet_wrapper/get_package_policies.ts +++ b/x-pack/plugins/osquery/server/routes/fleet_wrapper/get_package_policies.ts @@ -5,13 +5,15 @@ * 2.0. */ -import { schema } from '@kbn/config-schema'; import type { IRouter } from '@kbn/core/server'; import { PACKAGE_POLICY_SAVED_OBJECT_TYPE } from '@kbn/fleet-plugin/common'; +import type { GetPackagePoliciesRequestQuerySchema } from '../../../common/api'; +import { buildRouteValidation } from '../../utils/build_validation/route_validation'; import { API_VERSIONS } from '../../../common/constants'; import { PLUGIN_ID, OSQUERY_INTEGRATION_NAME } from '../../../common'; import type { OsqueryAppContext } from '../../lib/osquery_app_context_services'; import { getInternalSavedObjectsClient } from '../utils'; +import { getPackagePoliciesRequestQuerySchema } from '../../../common/api'; export const getPackagePoliciesRoute = (router: IRouter, osqueryContext: OsqueryAppContext) => { router.versioned @@ -25,7 +27,10 @@ export const getPackagePoliciesRoute = (router: IRouter, osqueryContext: Osquery version: API_VERSIONS.internal.v1, validate: { request: { - query: schema.object({}, { unknowns: 'allow' }), + query: buildRouteValidation< + typeof getPackagePoliciesRequestQuerySchema, + GetPackagePoliciesRequestQuerySchema + >(getPackagePoliciesRequestQuerySchema), }, }, }, diff --git a/x-pack/plugins/osquery/server/routes/live_query/create_live_query_route.ts b/x-pack/plugins/osquery/server/routes/live_query/create_live_query_route.ts index 971f2c3b45a4c9..586ebe3716a96f 100644 --- a/x-pack/plugins/osquery/server/routes/live_query/create_live_query_route.ts +++ b/x-pack/plugins/osquery/server/routes/live_query/create_live_query_route.ts @@ -11,11 +11,11 @@ import markdown from 'remark-parse-no-trim'; import { some, filter } from 'lodash'; import deepEqual from 'fast-deep-equal'; import type { ECSMappingOrUndefined } from '@kbn/osquery-io-ts-types'; +import type { CreateLiveQueryRequestBodySchema } from '../../../common/api'; +import { createLiveQueryRequestBodySchema } from '../../../common/api'; import { API_VERSIONS } from '../../../common/constants'; import { PARAMETER_NOT_FOUND } from '../../../common/translations/errors'; import { replaceParamsQuery } from '../../../common/utils/replace_params_query'; -import { createLiveQueryRequestBodySchema } from '../../../common/schemas/routes/live_query'; -import type { CreateLiveQueryRequestBodySchema } from '../../../common/schemas/routes/live_query'; import { buildRouteValidation } from '../../utils/build_validation/route_validation'; import type { OsqueryAppContext } from '../../lib/osquery_app_context_services'; import { createActionHandler } from '../../handlers'; diff --git a/x-pack/plugins/osquery/server/routes/live_query/find_live_query_route.ts b/x-pack/plugins/osquery/server/routes/live_query/find_live_query_route.ts index 858b76315c8e01..2e12d1cd967b1d 100644 --- a/x-pack/plugins/osquery/server/routes/live_query/find_live_query_route.ts +++ b/x-pack/plugins/osquery/server/routes/live_query/find_live_query_route.ts @@ -5,12 +5,13 @@ * 2.0. */ -import { schema } from '@kbn/config-schema'; import type { IRouter } from '@kbn/core/server'; import { omit } from 'lodash'; import type { Observable } from 'rxjs'; import { lastValueFrom } from 'rxjs'; import type { DataRequestHandlerContext } from '@kbn/data-plugin/server'; +import type { FindLiveQueryRequestQuerySchema } from '../../../common/api'; +import { buildRouteValidation } from '../../utils/build_validation/route_validation'; import { API_VERSIONS } from '../../../common/constants'; import { PLUGIN_ID } from '../../../common'; @@ -21,6 +22,7 @@ import type { } from '../../../common/search_strategy'; import { OsqueryQueries } from '../../../common/search_strategy'; import { createFilter, generateTablePaginationOptions } from '../../../common/utils/build_query'; +import { findLiveQueryRequestQuerySchema } from '../../../common/api'; export const findLiveQueryRoute = (router: IRouter) => { router.versioned @@ -34,18 +36,10 @@ export const findLiveQueryRoute = (router: IRouter) = version: API_VERSIONS.public.v1, validate: { request: { - query: schema.object( - { - filterQuery: schema.maybe(schema.string()), - page: schema.maybe(schema.number()), - pageSize: schema.maybe(schema.number()), - sort: schema.maybe(schema.string()), - sortOrder: schema.maybe( - schema.oneOf([schema.literal('asc'), schema.literal('desc')]) - ), - }, - { unknowns: 'allow' } - ), + query: buildRouteValidation< + typeof findLiveQueryRequestQuerySchema, + FindLiveQueryRequestQuerySchema + >(findLiveQueryRequestQuerySchema), }, }, }, diff --git a/x-pack/plugins/osquery/server/routes/live_query/get_live_query_details_route.ts b/x-pack/plugins/osquery/server/routes/live_query/get_live_query_details_route.ts index 6bd2bf3a11963d..a6b6b06dcaca4e 100644 --- a/x-pack/plugins/osquery/server/routes/live_query/get_live_query_details_route.ts +++ b/x-pack/plugins/osquery/server/routes/live_query/get_live_query_details_route.ts @@ -5,12 +5,16 @@ * 2.0. */ -import { schema } from '@kbn/config-schema'; import type { IRouter } from '@kbn/core/server'; import { every, map, mapKeys, pick, reduce } from 'lodash'; import type { Observable } from 'rxjs'; import { lastValueFrom, zip } from 'rxjs'; import type { DataRequestHandlerContext } from '@kbn/data-plugin/server'; +import type { + GetLiveQueryDetailsRequestParamsSchema, + GetLiveQueryDetailsRequestQuerySchema, +} from '../../../common/api'; +import { buildRouteValidation } from '../../utils/build_validation/route_validation'; import { API_VERSIONS } from '../../../common/constants'; import { PLUGIN_ID } from '../../../common'; import { getActionResponses } from './utils'; @@ -20,6 +24,10 @@ import type { ActionDetailsStrategyResponse, } from '../../../common/search_strategy'; import { OsqueryQueries } from '../../../common/search_strategy'; +import { + getLiveQueryDetailsRequestParamsSchema, + getLiveQueryDetailsRequestQuerySchema, +} from '../../../common/api'; export const getLiveQueryDetailsRoute = (router: IRouter) => { router.versioned @@ -33,13 +41,14 @@ export const getLiveQueryDetailsRoute = (router: IRouter(getLiveQueryDetailsRequestParamsSchema), + query: buildRouteValidation< + typeof getLiveQueryDetailsRequestQuerySchema, + GetLiveQueryDetailsRequestQuerySchema + >(getLiveQueryDetailsRequestQuerySchema), }, }, }, @@ -52,7 +61,7 @@ export const getLiveQueryDetailsRoute = (router: IRouter( { actionId: request.params.id, - filterQuery: request.query, + filterQuery: {}, factoryQueryType: OsqueryQueries.actionDetails, }, { abortSignal, strategy: 'osquerySearchStrategy' } diff --git a/x-pack/plugins/osquery/server/routes/live_query/get_live_query_results_route.ts b/x-pack/plugins/osquery/server/routes/live_query/get_live_query_results_route.ts index c7a86d440e3443..bd924e9c93b34c 100644 --- a/x-pack/plugins/osquery/server/routes/live_query/get_live_query_results_route.ts +++ b/x-pack/plugins/osquery/server/routes/live_query/get_live_query_results_route.ts @@ -5,12 +5,16 @@ * 2.0. */ -import { schema } from '@kbn/config-schema'; import type { IRouter } from '@kbn/core/server'; import { map } from 'lodash'; import type { Observable } from 'rxjs'; import { lastValueFrom, zip } from 'rxjs'; import type { DataRequestHandlerContext } from '@kbn/data-plugin/server'; +import type { + GetLiveQueryResultsRequestQuerySchema, + GetLiveQueryResultsRequestParamsSchema, +} from '../../../common/api'; +import { buildRouteValidation } from '../../utils/build_validation/route_validation'; import { API_VERSIONS } from '../../../common/constants'; import { PLUGIN_ID } from '../../../common'; import type { @@ -20,6 +24,10 @@ import type { import { OsqueryQueries } from '../../../common/search_strategy'; import { createFilter, generateTablePaginationOptions } from '../../../common/utils/build_query'; import { getActionResponses } from './utils'; +import { + getLiveQueryResultsRequestParamsSchema, + getLiveQueryResultsRequestQuerySchema, +} from '../../../common/api'; export const getLiveQueryResultsRoute = (router: IRouter) => { router.versioned @@ -33,25 +41,14 @@ export const getLiveQueryResultsRoute = (router: IRouter(getLiveQueryResultsRequestQuerySchema), + params: buildRouteValidation< + typeof getLiveQueryResultsRequestParamsSchema, + GetLiveQueryResultsRequestParamsSchema + >(getLiveQueryResultsRequestParamsSchema), }, }, }, diff --git a/x-pack/plugins/osquery/server/routes/pack/create_pack_route.ts b/x-pack/plugins/osquery/server/routes/pack/create_pack_route.ts index 5d12a41f03c4d2..8429e7a91f68bc 100644 --- a/x-pack/plugins/osquery/server/routes/pack/create_pack_route.ts +++ b/x-pack/plugins/osquery/server/routes/pack/create_pack_route.ts @@ -8,7 +8,6 @@ import moment from 'moment-timezone'; import { set } from '@kbn/safer-lodash-set'; import { has, unset, find, some, mapKeys } from 'lodash'; -import { schema } from '@kbn/config-schema'; import { produce } from 'immer'; import type { PackagePolicy } from '@kbn/fleet-plugin/common'; import { @@ -16,6 +15,8 @@ import { PACKAGE_POLICY_SAVED_OBJECT_TYPE, } from '@kbn/fleet-plugin/common'; import type { IRouter } from '@kbn/core/server'; +import type { CreatePackRequestBodySchema } from '../../../common/api'; +import { buildRouteValidation } from '../../utils/build_validation/route_validation'; import { API_VERSIONS } from '../../../common/constants'; import type { OsqueryAppContext } from '../../lib/osquery_app_context_services'; import { OSQUERY_INTEGRATION_NAME } from '../../../common'; @@ -30,6 +31,7 @@ import { import { convertShardsToArray, getInternalSavedObjectsClient } from '../utils'; import type { PackSavedObject } from '../../common/types'; import type { PackResponseData } from './types'; +import { createPackRequestBodySchema } from '../../../common/api'; type PackSavedObjectLimited = Omit; @@ -45,38 +47,10 @@ export const createPackRoute = (router: IRouter, osqueryContext: OsqueryAppConte version: API_VERSIONS.public.v1, validate: { request: { - body: schema.object( - { - name: schema.string(), - description: schema.maybe(schema.string()), - enabled: schema.maybe(schema.boolean()), - policy_ids: schema.maybe(schema.arrayOf(schema.string())), - shards: schema.recordOf(schema.string(), schema.number()), - queries: schema.recordOf( - schema.string(), - schema.object({ - query: schema.string(), - interval: schema.maybe(schema.number()), - snapshot: schema.maybe(schema.boolean()), - removed: schema.maybe(schema.boolean()), - platform: schema.maybe(schema.string()), - version: schema.maybe(schema.string()), - ecs_mapping: schema.maybe( - schema.recordOf( - schema.string(), - schema.object({ - field: schema.maybe(schema.string()), - value: schema.maybe( - schema.oneOf([schema.string(), schema.arrayOf(schema.string())]) - ), - }) - ) - ), - }) - ), - }, - { unknowns: 'allow' } - ), + body: buildRouteValidation< + typeof createPackRequestBodySchema, + CreatePackRequestBodySchema + >(createPackRequestBodySchema), }, }, }, diff --git a/x-pack/plugins/osquery/server/routes/pack/delete_pack_route.ts b/x-pack/plugins/osquery/server/routes/pack/delete_pack_route.ts index 33a569653ea1d1..3408696d389e0c 100644 --- a/x-pack/plugins/osquery/server/routes/pack/delete_pack_route.ts +++ b/x-pack/plugins/osquery/server/routes/pack/delete_pack_route.ts @@ -7,15 +7,17 @@ import { has, filter, unset } from 'lodash'; import { produce } from 'immer'; -import { schema } from '@kbn/config-schema'; import { PACKAGE_POLICY_SAVED_OBJECT_TYPE } from '@kbn/fleet-plugin/common'; import type { IRouter } from '@kbn/core/server'; +import type { DeletePacksRequestParamsSchema } from '../../../common/api'; +import { buildRouteValidation } from '../../utils/build_validation/route_validation'; import { API_VERSIONS } from '../../../common/constants'; import { OSQUERY_INTEGRATION_NAME } from '../../../common'; import { PLUGIN_ID } from '../../../common'; import { packSavedObjectType } from '../../../common/types'; import type { OsqueryAppContext } from '../../lib/osquery_app_context_services'; +import { deletePacksRequestParamsSchema } from '../../../common/api'; export const deletePackRoute = (router: IRouter, osqueryContext: OsqueryAppContext) => { router.versioned @@ -29,9 +31,10 @@ export const deletePackRoute = (router: IRouter, osqueryContext: OsqueryAppConte version: API_VERSIONS.public.v1, validate: { request: { - params: schema.object({ - id: schema.string(), - }), + params: buildRouteValidation< + typeof deletePacksRequestParamsSchema, + DeletePacksRequestParamsSchema + >(deletePacksRequestParamsSchema), }, }, }, diff --git a/x-pack/plugins/osquery/server/routes/pack/find_pack_route.ts b/x-pack/plugins/osquery/server/routes/pack/find_pack_route.ts index 394fc50ff4e29f..808bb1bdbb8646 100644 --- a/x-pack/plugins/osquery/server/routes/pack/find_pack_route.ts +++ b/x-pack/plugins/osquery/server/routes/pack/find_pack_route.ts @@ -6,15 +6,17 @@ */ import { filter, map, omit } from 'lodash'; -import { schema } from '@kbn/config-schema'; import { AGENT_POLICY_SAVED_OBJECT_TYPE } from '@kbn/fleet-plugin/common'; import type { IRouter } from '@kbn/core/server'; +import type { FindPacksRequestQuerySchema } from '../../../common/api'; +import { buildRouteValidation } from '../../utils/build_validation/route_validation'; import { API_VERSIONS } from '../../../common/constants'; import { packSavedObjectType } from '../../../common/types'; import { PLUGIN_ID } from '../../../common'; import type { PackSavedObject } from '../../common/types'; import type { PackResponseData } from './types'; +import { findPacksRequestQuerySchema } from '../../../common/api'; export const findPackRoute = (router: IRouter) => { router.versioned @@ -28,17 +30,10 @@ export const findPackRoute = (router: IRouter) => { version: API_VERSIONS.public.v1, validate: { request: { - query: schema.object( - { - page: schema.maybe(schema.number()), - pageSize: schema.maybe(schema.number()), - sort: schema.maybe(schema.string()), - sortOrder: schema.maybe( - schema.oneOf([schema.literal('asc'), schema.literal('desc')]) - ), - }, - { unknowns: 'allow' } - ), + query: buildRouteValidation< + typeof findPacksRequestQuerySchema, + FindPacksRequestQuerySchema + >(findPacksRequestQuerySchema), }, }, }, diff --git a/x-pack/plugins/osquery/server/routes/pack/read_pack_route.ts b/x-pack/plugins/osquery/server/routes/pack/read_pack_route.ts index 0a61b0310def0b..d42d3f95bebac5 100644 --- a/x-pack/plugins/osquery/server/routes/pack/read_pack_route.ts +++ b/x-pack/plugins/osquery/server/routes/pack/read_pack_route.ts @@ -6,9 +6,10 @@ */ import { filter, map } from 'lodash'; -import { schema } from '@kbn/config-schema'; import { AGENT_POLICY_SAVED_OBJECT_TYPE } from '@kbn/fleet-plugin/common'; import type { IRouter } from '@kbn/core/server'; +import type { ReadPacksRequestParamsSchema } from '../../../common/api'; +import { buildRouteValidation } from '../../utils/build_validation/route_validation'; import { API_VERSIONS } from '../../../common/constants'; import type { PackSavedObject } from '../../common/types'; import { PLUGIN_ID } from '../../../common'; @@ -17,6 +18,7 @@ import { packSavedObjectType } from '../../../common/types'; import { convertSOQueriesToPack } from './utils'; import { convertShardsToObject } from '../utils'; import type { ReadPackResponseData } from './types'; +import { readPacksRequestParamsSchema } from '../../../common/api'; export const readPackRoute = (router: IRouter) => { router.versioned @@ -30,9 +32,10 @@ export const readPackRoute = (router: IRouter) => { version: API_VERSIONS.public.v1, validate: { request: { - params: schema.object({ - id: schema.string(), - }), + params: buildRouteValidation< + typeof readPacksRequestParamsSchema, + ReadPacksRequestParamsSchema + >(readPacksRequestParamsSchema), }, }, }, diff --git a/x-pack/plugins/osquery/server/routes/pack/update_pack_route.ts b/x-pack/plugins/osquery/server/routes/pack/update_pack_route.ts index 0d05a4d1b30cb6..1697718a00ba0a 100644 --- a/x-pack/plugins/osquery/server/routes/pack/update_pack_route.ts +++ b/x-pack/plugins/osquery/server/routes/pack/update_pack_route.ts @@ -8,7 +8,6 @@ import moment from 'moment-timezone'; import { set } from '@kbn/safer-lodash-set'; import { unset, has, difference, filter, find, map, mapKeys, uniq, some, isEmpty } from 'lodash'; -import { schema } from '@kbn/config-schema'; import { produce } from 'immer'; import type { PackagePolicy } from '@kbn/fleet-plugin/common'; import { @@ -17,6 +16,11 @@ import { } from '@kbn/fleet-plugin/common'; import type { IRouter } from '@kbn/core/server'; +import type { + UpdatePacksRequestParamsSchema, + UpdatePacksRequestBodySchema, +} from '../../../common/api'; +import { buildRouteValidation } from '../../utils/build_validation/route_validation'; import { API_VERSIONS } from '../../../common/constants'; import { OSQUERY_INTEGRATION_NAME } from '../../../common'; import { packSavedObjectType } from '../../../common/types'; @@ -33,6 +37,7 @@ import { import { convertShardsToArray, getInternalSavedObjectsClient } from '../utils'; import type { PackSavedObject } from '../../common/types'; import type { PackResponseData } from './types'; +import { updatePacksRequestBodySchema, updatePacksRequestParamsSchema } from '../../../common/api'; export const updatePackRoute = (router: IRouter, osqueryContext: OsqueryAppContext) => { router.versioned @@ -46,46 +51,14 @@ export const updatePackRoute = (router: IRouter, osqueryContext: OsqueryAppConte version: API_VERSIONS.public.v1, validate: { request: { - params: schema.object( - { - id: schema.string(), - }, - { unknowns: 'allow' } - ), - body: schema.object( - { - name: schema.maybe(schema.string()), - description: schema.maybe(schema.string()), - enabled: schema.maybe(schema.boolean()), - policy_ids: schema.maybe(schema.arrayOf(schema.string())), - shards: schema.maybe(schema.recordOf(schema.string(), schema.number())), - queries: schema.maybe( - schema.recordOf( - schema.string(), - schema.object({ - query: schema.string(), - interval: schema.maybe(schema.number()), - snapshot: schema.maybe(schema.boolean()), - removed: schema.maybe(schema.boolean()), - platform: schema.maybe(schema.string()), - version: schema.maybe(schema.string()), - ecs_mapping: schema.maybe( - schema.recordOf( - schema.string(), - schema.object({ - field: schema.maybe(schema.string()), - value: schema.maybe( - schema.oneOf([schema.string(), schema.arrayOf(schema.string())]) - ), - }) - ) - ), - }) - ) - ), - }, - { unknowns: 'allow' } - ), + params: buildRouteValidation< + typeof updatePacksRequestParamsSchema, + UpdatePacksRequestParamsSchema + >(updatePacksRequestParamsSchema), + body: buildRouteValidation< + typeof updatePacksRequestBodySchema, + UpdatePacksRequestBodySchema + >(updatePacksRequestBodySchema), }, }, }, diff --git a/x-pack/plugins/osquery/server/routes/pack/utils.ts b/x-pack/plugins/osquery/server/routes/pack/utils.ts index 3be425ad1ec288..a458e6edadfa14 100644 --- a/x-pack/plugins/osquery/server/routes/pack/utils.ts +++ b/x-pack/plugins/osquery/server/routes/pack/utils.ts @@ -8,7 +8,7 @@ import { isEmpty, pick, reduce, isArray, filter, uniq, map, mapKeys } from 'lodash'; import { satisfies } from 'semver'; import type { AgentPolicy, PackagePolicy } from '@kbn/fleet-plugin/common'; -import type { Shard } from '../../../common/schemas/common/utils'; +import type { Shard } from '../../../common/utils/converters'; import { DEFAULT_PLATFORM } from '../../../common/constants'; import { removeMultilines } from '../../../common/utils/build_query/remove_multilines'; import { convertECSMappingToArray, convertECSMappingToObject } from '../utils'; diff --git a/x-pack/plugins/osquery/server/routes/saved_query/create_saved_query_route.ts b/x-pack/plugins/osquery/server/routes/saved_query/create_saved_query_route.ts index 8f40dbc28924a8..bccd521f6323dc 100644 --- a/x-pack/plugins/osquery/server/routes/saved_query/create_saved_query_route.ts +++ b/x-pack/plugins/osquery/server/routes/saved_query/create_saved_query_route.ts @@ -7,16 +7,16 @@ import { isEmpty, pickBy, some, isBoolean } from 'lodash'; import type { IRouter } from '@kbn/core/server'; +import type { CreateSavedQueryRequestSchemaDecoded } from '../../../common/api'; import { API_VERSIONS } from '../../../common/constants'; import type { SavedQueryResponse } from './types'; import type { SavedQuerySavedObject } from '../../common/types'; import { PLUGIN_ID } from '../../../common'; -import type { CreateSavedQueryRequestSchemaDecoded } from '../../../common/schemas/routes/saved_query/create_saved_query_request_schema'; -import { createSavedQueryRequestSchema } from '../../../common/schemas/routes/saved_query/create_saved_query_request_schema'; import { savedQuerySavedObjectType } from '../../../common/types'; import { buildRouteValidation } from '../../utils/build_validation/route_validation'; import type { OsqueryAppContext } from '../../lib/osquery_app_context_services'; import { convertECSMappingToArray } from '../utils'; +import { createSavedQueryRequestSchema } from '../../../common/api'; export const createSavedQueryRoute = (router: IRouter, osqueryContext: OsqueryAppContext) => { router.versioned diff --git a/x-pack/plugins/osquery/server/routes/saved_query/delete_saved_query_route.ts b/x-pack/plugins/osquery/server/routes/saved_query/delete_saved_query_route.ts index ccecbf7a66dd90..f33040e167c620 100644 --- a/x-pack/plugins/osquery/server/routes/saved_query/delete_saved_query_route.ts +++ b/x-pack/plugins/osquery/server/routes/saved_query/delete_saved_query_route.ts @@ -5,13 +5,15 @@ * 2.0. */ -import { schema } from '@kbn/config-schema'; import type { IRouter } from '@kbn/core/server'; +import { buildRouteValidation } from '../../utils/build_validation/route_validation'; import { API_VERSIONS } from '../../../common/constants'; import { PLUGIN_ID } from '../../../common'; import { savedQuerySavedObjectType } from '../../../common/types'; import type { OsqueryAppContext } from '../../lib/osquery_app_context_services'; import { isSavedQueryPrebuilt } from './utils'; +import type { DeleteSavedQueryRequestParamsSchema } from '../../../common/api/saved_query/delete_saved_query_route'; +import { deleteSavedQueryRequestParamsSchema } from '../../../common/api/saved_query/delete_saved_query_route'; export const deleteSavedQueryRoute = (router: IRouter, osqueryContext: OsqueryAppContext) => { router.versioned @@ -25,9 +27,10 @@ export const deleteSavedQueryRoute = (router: IRouter, osqueryContext: OsqueryAp version: API_VERSIONS.public.v1, validate: { request: { - params: schema.object({ - id: schema.string(), - }), + params: buildRouteValidation< + typeof deleteSavedQueryRequestParamsSchema, + DeleteSavedQueryRequestParamsSchema + >(deleteSavedQueryRequestParamsSchema), }, }, }, diff --git a/x-pack/plugins/osquery/server/routes/saved_query/find_saved_query_route.ts b/x-pack/plugins/osquery/server/routes/saved_query/find_saved_query_route.ts index ad4b151554e4df..a176d581329b5f 100644 --- a/x-pack/plugins/osquery/server/routes/saved_query/find_saved_query_route.ts +++ b/x-pack/plugins/osquery/server/routes/saved_query/find_saved_query_route.ts @@ -5,10 +5,10 @@ * 2.0. */ -import { schema } from '@kbn/config-schema'; import type { IRouter } from '@kbn/core/server'; import { omit } from 'lodash'; +import { buildRouteValidation } from '../../utils/build_validation/route_validation'; import { API_VERSIONS } from '../../../common/constants'; import type { SavedQueryResponse } from './types'; import type { SavedQuerySavedObject } from '../../common/types'; @@ -17,6 +17,8 @@ import { PLUGIN_ID } from '../../../common'; import { savedQuerySavedObjectType } from '../../../common/types'; import { convertECSMappingToObject } from '../utils'; import { getInstalledSavedQueriesMap } from './utils'; +import type { FindSavedQueryRequestQuerySchema } from '../../../common/api/saved_query/find_saved_query_route'; +import { findSavedQueryRequestQuerySchema } from '../../../common/api/saved_query/find_saved_query_route'; export const findSavedQueryRoute = (router: IRouter, osqueryContext: OsqueryAppContext) => { router.versioned @@ -30,14 +32,10 @@ export const findSavedQueryRoute = (router: IRouter, osqueryContext: OsqueryAppC version: API_VERSIONS.public.v1, validate: { request: { - query: schema.object({ - page: schema.number({ defaultValue: 1 }), - pageSize: schema.maybe(schema.number()), - sort: schema.string({ defaultValue: 'id' }), - sortOrder: schema.oneOf([schema.literal('asc'), schema.literal('desc')], { - defaultValue: 'desc', - }), - }), + query: buildRouteValidation< + typeof findSavedQueryRequestQuerySchema, + FindSavedQueryRequestQuerySchema + >(findSavedQueryRequestQuerySchema), }, }, }, @@ -48,10 +46,10 @@ export const findSavedQueryRoute = (router: IRouter, osqueryContext: OsqueryAppC try { const savedQueries = await savedObjectsClient.find({ type: savedQuerySavedObjectType, - page: request.query.page, + page: request.query.page || 1, perPage: request.query.pageSize, - sortField: request.query.sort, - sortOrder: request.query.sortOrder, + sortField: request.query.sort || 'id', + sortOrder: request.query.sortOrder || 'desc', }); const prebuiltSavedQueriesMap = await getInstalledSavedQueriesMap( diff --git a/x-pack/plugins/osquery/server/routes/saved_query/read_saved_query_route.ts b/x-pack/plugins/osquery/server/routes/saved_query/read_saved_query_route.ts index 81d14e160a1a85..fb355c8b732b0a 100644 --- a/x-pack/plugins/osquery/server/routes/saved_query/read_saved_query_route.ts +++ b/x-pack/plugins/osquery/server/routes/saved_query/read_saved_query_route.ts @@ -5,8 +5,8 @@ * 2.0. */ -import { schema } from '@kbn/config-schema'; import type { IRouter } from '@kbn/core/server'; +import { buildRouteValidation } from '../../utils/build_validation/route_validation'; import { API_VERSIONS } from '../../../common/constants'; import type { SavedQueryResponse } from './types'; import type { SavedQuerySavedObject } from '../../common/types'; @@ -15,6 +15,8 @@ import type { OsqueryAppContext } from '../../lib/osquery_app_context_services'; import { PLUGIN_ID } from '../../../common'; import { savedQuerySavedObjectType } from '../../../common/types'; import { convertECSMappingToObject } from '../utils'; +import type { ReadSavedQueryRequestParamsSchema } from '../../../common/api/saved_query/read_saved_query_route'; +import { readSavedQueryRequestParamsSchema } from '../../../common/api/saved_query/read_saved_query_route'; export const readSavedQueryRoute = (router: IRouter, osqueryContext: OsqueryAppContext) => { router.versioned @@ -28,9 +30,10 @@ export const readSavedQueryRoute = (router: IRouter, osqueryContext: OsqueryAppC version: API_VERSIONS.public.v1, validate: { request: { - params: schema.object({ - id: schema.string(), - }), + params: buildRouteValidation< + typeof readSavedQueryRequestParamsSchema, + ReadSavedQueryRequestParamsSchema + >(readSavedQueryRequestParamsSchema), }, }, }, diff --git a/x-pack/plugins/osquery/server/routes/saved_query/update_saved_query_route.ts b/x-pack/plugins/osquery/server/routes/saved_query/update_saved_query_route.ts index 758016830b8925..d6cd11805c57a4 100644 --- a/x-pack/plugins/osquery/server/routes/saved_query/update_saved_query_route.ts +++ b/x-pack/plugins/osquery/server/routes/saved_query/update_saved_query_route.ts @@ -6,9 +6,9 @@ */ import { filter, some } from 'lodash'; -import { schema } from '@kbn/config-schema'; import type { IRouter } from '@kbn/core/server'; +import { buildRouteValidation } from '../../utils/build_validation/route_validation'; import { API_VERSIONS } from '../../../common/constants'; import { isSavedQueryPrebuilt } from './utils'; import { PLUGIN_ID } from '../../../common'; @@ -16,6 +16,14 @@ import { savedQuerySavedObjectType } from '../../../common/types'; import type { OsqueryAppContext } from '../../lib/osquery_app_context_services'; import { convertECSMappingToArray, convertECSMappingToObject } from '../utils'; import type { UpdateSavedQueryResponse } from './types'; +import type { + UpdateSavedQueryRequestBodySchema, + UpdateSavedQueryRequestParamsSchema, +} from '../../../common/api/saved_query/update_saved_query_route'; +import { + updateSavedQueryRequestBodySchema, + updateSavedQueryRequestParamsSchema, +} from '../../../common/api/saved_query/update_saved_query_route'; export const updateSavedQueryRoute = (router: IRouter, osqueryContext: OsqueryAppContext) => { router.versioned @@ -29,33 +37,14 @@ export const updateSavedQueryRoute = (router: IRouter, osqueryContext: OsqueryAp version: API_VERSIONS.public.v1, validate: { request: { - params: schema.object({ - id: schema.string(), - }), - body: schema.object( - { - id: schema.string(), - query: schema.string(), - description: schema.maybe(schema.string()), - interval: schema.maybe(schema.number()), - snapshot: schema.maybe(schema.boolean()), - removed: schema.maybe(schema.boolean()), - platform: schema.maybe(schema.string()), - version: schema.maybe(schema.string()), - ecs_mapping: schema.maybe( - schema.recordOf( - schema.string(), - schema.object({ - field: schema.maybe(schema.string()), - value: schema.maybe( - schema.oneOf([schema.string(), schema.arrayOf(schema.string())]) - ), - }) - ) - ), - }, - { unknowns: 'allow' } - ), + params: buildRouteValidation< + typeof updateSavedQueryRequestParamsSchema, + UpdateSavedQueryRequestParamsSchema + >(updateSavedQueryRequestParamsSchema), + body: buildRouteValidation< + typeof updateSavedQueryRequestBodySchema, + UpdateSavedQueryRequestBodySchema + >(updateSavedQueryRequestBodySchema), }, }, }, diff --git a/x-pack/plugins/osquery/server/routes/utils.ts b/x-pack/plugins/osquery/server/routes/utils.ts index 5395d1c3968cbd..5c803d27719eaa 100644 --- a/x-pack/plugins/osquery/server/routes/utils.ts +++ b/x-pack/plugins/osquery/server/routes/utils.ts @@ -8,7 +8,7 @@ import type { CoreSetup } from '@kbn/core/server'; import { SavedObjectsClient } from '@kbn/core/server'; import { reduce } from 'lodash'; -import type { Shard } from '../../common/schemas/common/utils'; +import type { Shard } from '../../common/utils/converters'; import type { SOShard } from '../common/types'; export const convertECSMappingToArray = (ecsMapping: Record | undefined) => diff --git a/x-pack/plugins/osquery/tsconfig.json b/x-pack/plugins/osquery/tsconfig.json index df3fcbb546e871..1ab04ddd7d58d6 100644 --- a/x-pack/plugins/osquery/tsconfig.json +++ b/x-pack/plugins/osquery/tsconfig.json @@ -72,6 +72,7 @@ "@kbn/licensing-plugin", "@kbn/core-lifecycle-browser", "@kbn/core-saved-objects-server", - "@kbn/monaco" + "@kbn/monaco", + "@kbn/io-ts-utils" ] }