diff --git a/x-pack/plugins/endpoint/public/applications/endpoint/store/alerts/action.ts b/x-pack/plugins/endpoint/public/applications/endpoint/store/alerts/action.ts index 7f8ac5a40d1e20..464a04eff5ebda 100644 --- a/x-pack/plugins/endpoint/public/applications/endpoint/store/alerts/action.ts +++ b/x-pack/plugins/endpoint/public/applications/endpoint/store/alerts/action.ts @@ -4,7 +4,7 @@ * you may not use this file except in compliance with the Elastic License. */ -import { AlertListData } from './types'; +import { AlertListData } from '../../types'; interface ServerReturnedAlertsData { type: 'serverReturnedAlertsData'; diff --git a/x-pack/plugins/endpoint/public/applications/endpoint/store/alerts/index.ts b/x-pack/plugins/endpoint/public/applications/endpoint/store/alerts/index.ts index 4b935d22650dc3..f63910a1c305ef 100644 --- a/x-pack/plugins/endpoint/public/applications/endpoint/store/alerts/index.ts +++ b/x-pack/plugins/endpoint/public/applications/endpoint/store/alerts/index.ts @@ -6,4 +6,4 @@ export { alertListReducer } from './reducer'; export { AlertAction } from './action'; -export * from './types'; +export * from '../../types'; diff --git a/x-pack/plugins/endpoint/public/applications/endpoint/store/alerts/reducer.ts b/x-pack/plugins/endpoint/public/applications/endpoint/store/alerts/reducer.ts index 2c497c3fc4d571..fd74abe9e34329 100644 --- a/x-pack/plugins/endpoint/public/applications/endpoint/store/alerts/reducer.ts +++ b/x-pack/plugins/endpoint/public/applications/endpoint/store/alerts/reducer.ts @@ -5,7 +5,7 @@ */ import { Reducer } from 'redux'; -import { AlertListState } from './types'; +import { AlertListState } from '../../types'; import { AppAction } from '../action'; const initialState = (): AlertListState => { @@ -25,7 +25,7 @@ export const alertListReducer: Reducer = ( if (action.type === 'serverReturnedAlertsData') { return { ...state, - ...action.payload, + alerts: action.payload.alerts, }; } diff --git a/x-pack/plugins/endpoint/public/applications/endpoint/store/alerts/selectors.ts b/x-pack/plugins/endpoint/public/applications/endpoint/store/alerts/selectors.ts index 47269f86fdaab3..51903a0a641e8d 100644 --- a/x-pack/plugins/endpoint/public/applications/endpoint/store/alerts/selectors.ts +++ b/x-pack/plugins/endpoint/public/applications/endpoint/store/alerts/selectors.ts @@ -4,6 +4,6 @@ * you may not use this file except in compliance with the Elastic License. */ -import { AlertListState } from './types'; +import { AlertListState } from '../../types'; export const alertListData = (state: AlertListState) => state.alerts; diff --git a/x-pack/plugins/endpoint/public/applications/endpoint/store/alerts/types.ts b/x-pack/plugins/endpoint/public/applications/endpoint/store/alerts/types.ts deleted file mode 100644 index 514188576430a9..00000000000000 --- a/x-pack/plugins/endpoint/public/applications/endpoint/store/alerts/types.ts +++ /dev/null @@ -1,10 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License; - * you may not use this file except in compliance with the Elastic License. - */ - -import { AlertResultList } from '../../../../../common/types'; - -export type AlertListData = AlertResultList; -export type AlertListState = AlertResultList; diff --git a/x-pack/plugins/endpoint/public/applications/endpoint/types.ts b/x-pack/plugins/endpoint/public/applications/endpoint/types.ts index a0094bbd3f5fb9..5f02d36308053d 100644 --- a/x-pack/plugins/endpoint/public/applications/endpoint/types.ts +++ b/x-pack/plugins/endpoint/public/applications/endpoint/types.ts @@ -6,9 +6,9 @@ import { Dispatch, MiddlewareAPI } from 'redux'; import { CoreStart } from 'kibana/public'; -import { AlertListState } from './store/alerts'; import { EndpointListState } from './store/endpoint_list'; import { AppAction } from './store/action'; +import { AlertResultList } from '../../../common/types'; export type MiddlewareFactory = ( coreStart: CoreStart @@ -20,3 +20,6 @@ export interface GlobalState { readonly endpointList: EndpointListState; readonly alertList: AlertListState; } + +export type AlertListData = AlertResultList; +export type AlertListState = AlertResultList; diff --git a/x-pack/plugins/endpoint/server/routes/alerts.ts b/x-pack/plugins/endpoint/server/routes/alerts.ts index 4f19ae73fc070d..541cf4af527697 100644 --- a/x-pack/plugins/endpoint/server/routes/alerts.ts +++ b/x-pack/plugins/endpoint/server/routes/alerts.ts @@ -4,18 +4,17 @@ * you may not use this file except in compliance with the Elastic License. */ -import { IRouter } from 'kibana/server'; -import { RequestHandler } from 'kibana/server'; +import { IRouter, KibanaRequest, RequestHandler } from 'kibana/server'; import { SearchResponse } from 'elasticsearch'; -import { schema, TypeOf } from '@kbn/config-schema'; +import { schema } from '@kbn/config-schema'; import { getPagingProperties, - kibanaRequestToAlertListQuery, + buildAlertListESQuery, } from '../services/endpoint/alert_query_builders'; import { AlertData, AlertResultList } from '../../common/types'; -import { EndpointAppContext } from '../types'; +import { AlertRequestParams, EndpointAppContext } from '../types'; const ALERTS_ROUTE = '/api/endpoint/alerts'; @@ -25,14 +24,13 @@ export const reqSchema = schema.object({ }); export function registerAlertRoutes(router: IRouter, endpointAppContext: EndpointAppContext) { - const alertsHandler: RequestHandler> = async ( - ctx, - req, - res - ) => { + const alertsHandler: RequestHandler = async (ctx, req, res) => { try { - const queryParams = await getPagingProperties(req, endpointAppContext); - const reqBody = await kibanaRequestToAlertListQuery(queryParams, endpointAppContext); + const queryParams = await getPagingProperties( + req as KibanaRequest, + endpointAppContext + ); + const reqBody = await buildAlertListESQuery(queryParams); const response = (await ctx.core.elasticsearch.dataClient.callAsCurrentUser( 'search', reqBody diff --git a/x-pack/plugins/endpoint/server/services/endpoint/alert_query_builders.test.ts b/x-pack/plugins/endpoint/server/services/endpoint/alert_query_builders.test.ts index 31ef024d9090b4..a4d7de8fdcfdb1 100644 --- a/x-pack/plugins/endpoint/server/services/endpoint/alert_query_builders.test.ts +++ b/x-pack/plugins/endpoint/server/services/endpoint/alert_query_builders.test.ts @@ -5,20 +5,18 @@ */ import { httpServerMock, loggingServiceMock } from 'src/core/server/mocks'; import { EndpointConfigSchema } from '../../config'; -import { getPagingProperties, kibanaRequestToAlertListQuery } from './alert_query_builders'; +import { getPagingProperties, buildAlertListESQuery } from './alert_query_builders'; describe('test query builder', () => { describe('test query builder request processing', () => { it('should execute the correct Elasticsearch query for a default request', async () => { - const mockRequest = httpServerMock.createKibanaRequest({ - body: {}, - }); + const mockRequest = httpServerMock.createKibanaRequest({}); const mockCtx = { logFactory: loggingServiceMock.create(), config: () => Promise.resolve(EndpointConfigSchema.validate({})), }; const queryParams = await getPagingProperties(mockRequest, mockCtx); - const query = await kibanaRequestToAlertListQuery(queryParams, mockCtx); + const query = await buildAlertListESQuery(queryParams); expect(query).toEqual({ body: { @@ -51,7 +49,7 @@ describe('test query builder', () => { config: () => Promise.resolve(EndpointConfigSchema.validate({})), }; const queryParams = await getPagingProperties(mockRequest, mockCtx); - const query = await kibanaRequestToAlertListQuery(queryParams, mockCtx); + const query = await buildAlertListESQuery(queryParams); expect(query).toEqual({ body: { diff --git a/x-pack/plugins/endpoint/server/services/endpoint/alert_query_builders.ts b/x-pack/plugins/endpoint/server/services/endpoint/alert_query_builders.ts index d02c3a76f0f8d2..a20f2ae1cdecd5 100644 --- a/x-pack/plugins/endpoint/server/services/endpoint/alert_query_builders.ts +++ b/x-pack/plugins/endpoint/server/services/endpoint/alert_query_builders.ts @@ -5,12 +5,11 @@ */ import { KibanaRequest } from 'kibana/server'; import { EndpointAppConstants } from '../../../common/types'; -import { EndpointAppContext } from '../../types'; +import { EndpointAppContext, AlertRequestParams, JSONish } from '../../types'; -export const kibanaRequestToAlertListQuery = async ( - pagingProperties: Record, - endpointAppContext: EndpointAppContext -): Promise> => { +export const buildAlertListESQuery = async ( + pagingProperties: Record +): Promise => { const DEFAULT_TOTAL_HITS = 10000; // Calculate minimum total hits set to indicate there's a next page @@ -40,9 +39,9 @@ export const kibanaRequestToAlertListQuery = async ( }; export const getPagingProperties = async ( - request: KibanaRequest, + request: KibanaRequest, endpointAppContext: EndpointAppContext -): Promise> => { +): Promise> => { const config = await endpointAppContext.config(); const pagingProperties: { page_size?: number; page_index?: number } = {}; diff --git a/x-pack/plugins/endpoint/server/types.ts b/x-pack/plugins/endpoint/server/types.ts index f06cc10f16709d..a0c9cd4b90266d 100644 --- a/x-pack/plugins/endpoint/server/types.ts +++ b/x-pack/plugins/endpoint/server/types.ts @@ -6,7 +6,25 @@ import { LoggerFactory } from 'kibana/server'; import { EndpointConfigType } from './config'; +/** + * A JSON-like structure. + */ +export interface JSONish { + [key: string]: number | string | null | undefined | JSONish | JSONish[]; +} + +/** + * The context for Endpoint apps. + */ export interface EndpointAppContext { logFactory: LoggerFactory; config(): Promise; } + +/** + * Request params for alert queries. + */ +export interface AlertRequestParams { + page_index?: number; + page_size?: number; +}