From 6577ba0d946ca4908a9edb205cdeab557ae41557 Mon Sep 17 00:00:00 2001 From: nnamdifrankie Date: Tue, 14 Jan 2020 12:35:27 -0500 Subject: [PATCH] EMT-65: make endpoint data types common, restructure --- x-pack/plugins/endpoint/common/types.ts | 58 +++++++++++++++++++ x-pack/plugins/endpoint/server/plugin.ts | 2 +- .../endpoint/server/routes/endpoints.test.ts | 8 +-- .../endpoint/server/routes/endpoints.ts | 19 ++---- .../endpoint/endpoint_query_builders.ts | 3 +- x-pack/plugins/endpoint/server/types.ts | 42 -------------- .../apis/endpoint/endpoints.ts | 8 +-- 7 files changed, 74 insertions(+), 66 deletions(-) create mode 100644 x-pack/plugins/endpoint/common/types.ts diff --git a/x-pack/plugins/endpoint/common/types.ts b/x-pack/plugins/endpoint/common/types.ts new file mode 100644 index 00000000000000..245e79d049236e --- /dev/null +++ b/x-pack/plugins/endpoint/common/types.ts @@ -0,0 +1,58 @@ +/* + * 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. + */ + +export class EndpointAppConstants { + static ENDPOINT_INDEX_NAME = 'endpoint-agent*'; +} + +export interface EndpointResultList { + // the endpoint restricted by the page size + endpoints: EndpointData[]; + // the total number of unique endpoints in the index + total: number; + // the page size requested + request_page_size: number; + // the index requested + request_page_index: number; +} + +export interface EndpointData { + machine_id: string; + created_at: Date; + host: { + name: string; + hostname: string; + ip: string; + mac_address: string; + os: { + name: string; + full: string; + }; + }; + endpoint: { + domain: string; + is_base_image: boolean; + active_directory_distinguished_name: string; + active_directory_hostname: string; + upgrade: { + status?: string; + updated_at?: Date; + }; + isolation: { + status: boolean; + request_status?: string | boolean; + updated_at?: Date; + }; + policy: { + name: string; + id: string; + }; + sensor: { + persistence: boolean; + status: object; + }; + }; +} diff --git a/x-pack/plugins/endpoint/server/plugin.ts b/x-pack/plugins/endpoint/server/plugin.ts index 7ed116ba211407..b1ae2adbdbb357 100644 --- a/x-pack/plugins/endpoint/server/plugin.ts +++ b/x-pack/plugins/endpoint/server/plugin.ts @@ -8,8 +8,8 @@ import { first } from 'rxjs/operators'; import { addRoutes } from './routes'; import { PluginSetupContract as FeaturesPluginSetupContract } from '../../features/server'; import { createConfig$, EndpointConfigType } from './config'; -import { EndpointAppContext } from './types'; import { registerEndpointRoutes } from './routes/endpoints'; +import { EndpointAppContext } from './types'; export type EndpointPluginStart = void; export type EndpointPluginSetup = void; diff --git a/x-pack/plugins/endpoint/server/routes/endpoints.test.ts b/x-pack/plugins/endpoint/server/routes/endpoints.test.ts index 60433f86b6f7ed..c9cbb08428648b 100644 --- a/x-pack/plugins/endpoint/server/routes/endpoints.test.ts +++ b/x-pack/plugins/endpoint/server/routes/endpoints.test.ts @@ -18,9 +18,9 @@ import { httpServiceMock, loggingServiceMock, } from '../../../../../src/core/server/mocks'; -import { EndpointData } from '../types'; +import { EndpointData, EndpointResultList } from '../../common/types'; import { SearchResponse } from 'elasticsearch'; -import { EndpointResultList, registerEndpointRoutes } from './endpoints'; +import { registerEndpointRoutes } from './endpoints'; import { EndpointConfigSchema } from '../config'; import * as data from '../test_data/all_endpoints_data.json'; @@ -75,7 +75,7 @@ describe('test endpoint route', () => { const endpointResultList = mockResponse.ok.mock.calls[0][0]?.body as EndpointResultList; expect(endpointResultList.endpoints.length).toEqual(3); expect(endpointResultList.total).toEqual(3); - expect(endpointResultList.request_index).toEqual(0); + expect(endpointResultList.request_page_index).toEqual(0); expect(endpointResultList.request_page_size).toEqual(10); }); @@ -117,7 +117,7 @@ describe('test endpoint route', () => { const endpointResultList = mockResponse.ok.mock.calls[0][0]?.body as EndpointResultList; expect(endpointResultList.endpoints.length).toEqual(3); expect(endpointResultList.total).toEqual(3); - expect(endpointResultList.request_index).toEqual(10); + expect(endpointResultList.request_page_index).toEqual(10); expect(endpointResultList.request_page_size).toEqual(10); }); }); diff --git a/x-pack/plugins/endpoint/server/routes/endpoints.ts b/x-pack/plugins/endpoint/server/routes/endpoints.ts index 9d2babc61f11f9..e78b8479e17585 100644 --- a/x-pack/plugins/endpoint/server/routes/endpoints.ts +++ b/x-pack/plugins/endpoint/server/routes/endpoints.ts @@ -7,24 +7,15 @@ import { IRouter } from 'kibana/server'; import { SearchResponse } from 'elasticsearch'; import { schema } from '@kbn/config-schema'; -import { EndpointAppContext, EndpointData } from '../types'; + import { kibanaRequestToEndpointListQuery } from '../services/endpoint/endpoint_query_builders'; +import { EndpointData, EndpointResultList } from '../../common/types'; +import { EndpointAppContext } from '../types'; interface HitSource { _source: EndpointData; } -export interface EndpointResultList { - // the endpoint restricted by the page size - endpoints: EndpointData[]; - // the total number of unique endpoints in the index - total: number; - // the page size requested - request_page_size: number; - // the index requested - request_index: number; -} - export function registerEndpointRoutes(router: IRouter, endpointAppContext: EndpointAppContext) { router.post( { @@ -70,7 +61,7 @@ function mapToEndpointResultList( if (searchResponse.hits.hits.length > 0) { return { request_page_size: queryParams.size, - request_index: queryParams.from, + request_page_index: queryParams.from, endpoints: searchResponse.hits.hits .map(response => response.inner_hits.most_recent.hits.hits) .flatMap(data => data as HitSource) @@ -80,7 +71,7 @@ function mapToEndpointResultList( } else { return { request_page_size: queryParams.size, - request_index: queryParams.from, + request_page_index: queryParams.from, total: totalNumberOfEndpoints, endpoints: [], }; diff --git a/x-pack/plugins/endpoint/server/services/endpoint/endpoint_query_builders.ts b/x-pack/plugins/endpoint/server/services/endpoint/endpoint_query_builders.ts index 7430ba97216083..9e32907fba446d 100644 --- a/x-pack/plugins/endpoint/server/services/endpoint/endpoint_query_builders.ts +++ b/x-pack/plugins/endpoint/server/services/endpoint/endpoint_query_builders.ts @@ -4,7 +4,8 @@ * you may not use this file except in compliance with the Elastic License. */ import { KibanaRequest } from 'kibana/server'; -import { EndpointAppConstants, EndpointAppContext } from '../../types'; +import { EndpointAppConstants } from '../../../common/types'; +import { EndpointAppContext } from '../../types'; export const kibanaRequestToEndpointListQuery = async ( request: KibanaRequest, diff --git a/x-pack/plugins/endpoint/server/types.ts b/x-pack/plugins/endpoint/server/types.ts index c6d0e3dea70cf7..f06cc10f16709d 100644 --- a/x-pack/plugins/endpoint/server/types.ts +++ b/x-pack/plugins/endpoint/server/types.ts @@ -10,45 +10,3 @@ export interface EndpointAppContext { logFactory: LoggerFactory; config(): Promise; } - -export class EndpointAppConstants { - static ENDPOINT_INDEX_NAME = 'endpoint-agent*'; -} - -export interface EndpointData { - machine_id: string; - created_at: Date; - host: { - name: string; - hostname: string; - ip: string; - mac_address: string; - os: { - name: string; - full: string; - }; - }; - endpoint: { - domain: string; - is_base_image: boolean; - active_directory_distinguished_name: string; - active_directory_hostname: string; - upgrade: { - status?: string; - updated_at?: Date; - }; - isolation: { - status: boolean; - request_status?: string | boolean; - updated_at?: Date; - }; - policy: { - name: string; - id: string; - }; - sensor: { - persistence: boolean; - status: object; - }; - }; -} diff --git a/x-pack/test/api_integration/apis/endpoint/endpoints.ts b/x-pack/test/api_integration/apis/endpoint/endpoints.ts index 32864489d37867..c726231a82778b 100644 --- a/x-pack/test/api_integration/apis/endpoint/endpoints.ts +++ b/x-pack/test/api_integration/apis/endpoint/endpoints.ts @@ -21,7 +21,7 @@ export default function({ getService }: FtrProviderContext) { expect(body.total).to.eql(0); expect(body.endpoints.length).to.eql(0); expect(body.request_page_size).to.eql(10); - expect(body.request_index).to.eql(0); + expect(body.request_page_index).to.eql(0); }); }); @@ -37,7 +37,7 @@ export default function({ getService }: FtrProviderContext) { expect(body.total).to.eql(3); expect(body.endpoints.length).to.eql(3); expect(body.request_page_size).to.eql(10); - expect(body.request_index).to.eql(0); + expect(body.request_page_index).to.eql(0); }); it('endpoints api should return page based on params passed.', async () => { @@ -58,7 +58,7 @@ export default function({ getService }: FtrProviderContext) { expect(body.total).to.eql(3); expect(body.endpoints.length).to.eql(1); expect(body.request_page_size).to.eql(1); - expect(body.request_index).to.eql(1); + expect(body.request_page_index).to.eql(1); }); /* test that when paging properties produces no result, the total should reflect the actual number of endpoints @@ -82,7 +82,7 @@ export default function({ getService }: FtrProviderContext) { expect(body.total).to.eql(3); expect(body.endpoints.length).to.eql(0); expect(body.request_page_size).to.eql(10); - expect(body.request_index).to.eql(30); + expect(body.request_page_index).to.eql(30); }); it('endpoints api should return 400 when pagingProperties is below boundaries.', async () => {