Skip to content

Commit

Permalink
[7.x] [Endpoint] EMT-184: change endpoints to metadata up and down th…
Browse files Browse the repository at this point in the history
…e code base. (#58038) (#58745)

[7.x] [Endpoint] EMT-184: change endpoints to metadata up and down the code base.
  • Loading branch information
nnamdifrankie authored Feb 28, 2020
1 parent 1b85349 commit 7850e31
Show file tree
Hide file tree
Showing 13 changed files with 169 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ describe('endpoint list saga', () => {
expect(fakeHttpServices.post).not.toHaveBeenCalled();
dispatch({ type: 'userNavigatedToPage', payload: 'managementPage' });
await sleep();
expect(fakeHttpServices.post).toHaveBeenCalledWith('/api/endpoint/endpoints', {
expect(fakeHttpServices.post).toHaveBeenCalledWith('/api/endpoint/metadata', {
body: JSON.stringify({
paging_properties: [{ page_index: 0 }, { page_size: 10 }],
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const managementMiddlewareFactory: MiddlewareFactory<ManagementListState>
) {
const managementPageIndex = pageIndex(getState());
const managementPageSize = pageSize(getState());
const response = await coreStart.http.post('/api/endpoint/endpoints', {
const response = await coreStart.http.post('/api/endpoint/metadata', {
body: JSON.stringify({
paging_properties: [
{ page_index: managementPageIndex },
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/endpoint/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { createConfig$, EndpointConfigType } from './config';
import { EndpointAppContext } from './types';

import { addRoutes } from './routes';
import { registerEndpointRoutes } from './routes/endpoints';
import { registerEndpointRoutes } from './routes/metadata';
import { registerAlertRoutes } from './routes/alerts';
import { registerResolverRoutes } from './routes/resolver';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import {
} from '../../../../../src/core/server/mocks';
import { EndpointMetadata, EndpointResultList } from '../../common/types';
import { SearchResponse } from 'elasticsearch';
import { registerEndpointRoutes } from './endpoints';
import { registerEndpointRoutes } from './metadata';
import { EndpointConfigSchema } from '../config';
import * as data from '../test_data/all_endpoints_data.json';
import * as data from '../test_data/all_metadata_data.json';

describe('test endpoint route', () => {
let routerMock: jest.Mocked<IRouter>;
Expand Down Expand Up @@ -54,7 +54,7 @@ describe('test endpoint route', () => {
>;
mockScopedClient.callAsCurrentUser.mockImplementationOnce(() => Promise.resolve(response));
[routeConfig, routeHandler] = routerMock.post.mock.calls.find(([{ path }]) =>
path.startsWith('/api/endpoint/endpoints')
path.startsWith('/api/endpoint/metadata')
)!;

await routeHandler(
Expand Down Expand Up @@ -96,7 +96,7 @@ describe('test endpoint route', () => {
Promise.resolve((data as unknown) as SearchResponse<EndpointMetadata>)
);
[routeConfig, routeHandler] = routerMock.post.mock.calls.find(([{ path }]) =>
path.startsWith('/api/endpoint/endpoints')
path.startsWith('/api/endpoint/metadata')
)!;

await routeHandler(
Expand Down Expand Up @@ -143,7 +143,7 @@ describe('test endpoint route', () => {
Promise.resolve((data as unknown) as SearchResponse<EndpointMetadata>)
);
[routeConfig, routeHandler] = routerMock.post.mock.calls.find(([{ path }]) =>
path.startsWith('/api/endpoint/endpoints')
path.startsWith('/api/endpoint/metadata')
)!;

await routeHandler(
Expand Down Expand Up @@ -208,7 +208,7 @@ describe('test endpoint route', () => {
})
);
[routeConfig, routeHandler] = routerMock.get.mock.calls.find(([{ path }]) =>
path.startsWith('/api/endpoint/endpoints')
path.startsWith('/api/endpoint/metadata')
)!;

await routeHandler(
Expand Down Expand Up @@ -239,7 +239,7 @@ describe('test endpoint route', () => {
>;
mockScopedClient.callAsCurrentUser.mockImplementationOnce(() => Promise.resolve(response));
[routeConfig, routeHandler] = routerMock.get.mock.calls.find(([{ path }]) =>
path.startsWith('/api/endpoint/endpoints')
path.startsWith('/api/endpoint/metadata')
)!;

await routeHandler(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ import { SearchResponse } from 'elasticsearch';
import { schema } from '@kbn/config-schema';

import {
kibanaRequestToEndpointListQuery,
kibanaRequestToEndpointFetchQuery,
} from '../services/endpoint/endpoint_query_builders';
kibanaRequestToMetadataListESQuery,
kibanaRequestToMetadataGetESQuery,
} from '../services/endpoint/metadata_query_builders';
import { EndpointMetadata, EndpointResultList } from '../../common/types';
import { EndpointAppContext } from '../types';

Expand All @@ -22,7 +22,7 @@ interface HitSource {
export function registerEndpointRoutes(router: IRouter, endpointAppContext: EndpointAppContext) {
router.post(
{
path: '/api/endpoint/endpoints',
path: '/api/endpoint/metadata',
validate: {
body: schema.nullable(
schema.object({
Expand Down Expand Up @@ -53,7 +53,7 @@ export function registerEndpointRoutes(router: IRouter, endpointAppContext: Endp
},
async (context, req, res) => {
try {
const queryParams = await kibanaRequestToEndpointListQuery(req, endpointAppContext);
const queryParams = await kibanaRequestToMetadataListESQuery(req, endpointAppContext);
const response = (await context.core.elasticsearch.dataClient.callAsCurrentUser(
'search',
queryParams
Expand All @@ -67,15 +67,15 @@ export function registerEndpointRoutes(router: IRouter, endpointAppContext: Endp

router.get(
{
path: '/api/endpoint/endpoints/{id}',
path: '/api/endpoint/metadata/{id}',
validate: {
params: schema.object({ id: schema.string() }),
},
options: { authRequired: true },
},
async (context, req, res) => {
try {
const query = kibanaRequestToEndpointFetchQuery(req, endpointAppContext);
const query = kibanaRequestToMetadataGetESQuery(req, endpointAppContext);
const response = (await context.core.elasticsearch.dataClient.callAsCurrentUser(
'search',
query
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@
import { httpServerMock, loggingServiceMock } from '../../../../../../src/core/server/mocks';
import { EndpointConfigSchema } from '../../config';
import {
kibanaRequestToEndpointListQuery,
kibanaRequestToEndpointFetchQuery,
} from './endpoint_query_builders';
kibanaRequestToMetadataListESQuery,
kibanaRequestToMetadataGetESQuery,
} from './metadata_query_builders';
import { EndpointAppConstants } from '../../../common/types';

describe('query builder', () => {
describe('EndpointListQuery', () => {
it('test default query params for all endpoints when no params or body is provided', async () => {
describe('MetadataListESQuery', () => {
it('test default query params for all endpoints metadata when no params or body is provided', async () => {
const mockRequest = httpServerMock.createKibanaRequest({
body: {},
});
const query = await kibanaRequestToEndpointListQuery(mockRequest, {
const query = await kibanaRequestToMetadataListESQuery(mockRequest, {
logFactory: loggingServiceMock.create(),
config: () => Promise.resolve(EndpointConfigSchema.validate({})),
});
Expand Down Expand Up @@ -50,19 +51,19 @@ describe('query builder', () => {
},
from: 0,
size: 10,
index: 'endpoint-agent*',
index: EndpointAppConstants.ENDPOINT_INDEX_NAME,
} as Record<string, any>);
});
});

describe('test query builder with kql filter', () => {
it('test default query params for all endpoints when no params or body is provided', async () => {
it('test default query params for all endpoints metadata when body filter is provided', async () => {
const mockRequest = httpServerMock.createKibanaRequest({
body: {
filter: 'not host.ip:10.140.73.246',
},
});
const query = await kibanaRequestToEndpointListQuery(mockRequest, {
const query = await kibanaRequestToMetadataListESQuery(mockRequest, {
logFactory: loggingServiceMock.create(),
config: () => Promise.resolve(EndpointConfigSchema.validate({})),
});
Expand Down Expand Up @@ -109,20 +110,20 @@ describe('query builder', () => {
},
from: 0,
size: 10,
index: 'endpoint-agent*',
index: EndpointAppConstants.ENDPOINT_INDEX_NAME,
} as Record<string, any>);
});
});

describe('EndpointFetchQuery', () => {
describe('MetadataGetQuery', () => {
it('searches for the correct ID', () => {
const mockID = 'AABBCCDD-0011-2233-AA44-DEADBEEF8899';
const mockRequest = httpServerMock.createKibanaRequest({
params: {
id: mockID,
},
});
const query = kibanaRequestToEndpointFetchQuery(mockRequest, {
const query = kibanaRequestToMetadataGetESQuery(mockRequest, {
logFactory: loggingServiceMock.create(),
config: () => Promise.resolve(EndpointConfigSchema.validate({})),
});
Expand All @@ -132,7 +133,7 @@ describe('query builder', () => {
sort: [{ 'event.created': { order: 'desc' } }],
size: 1,
},
index: 'endpoint-agent*',
index: EndpointAppConstants.ENDPOINT_INDEX_NAME,
});
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { EndpointAppConstants } from '../../../common/types';
import { EndpointAppContext } from '../../types';
import { esKuery } from '../../../../../../src/plugins/data/server';

export const kibanaRequestToEndpointListQuery = async (
export const kibanaRequestToMetadataListESQuery = async (
request: KibanaRequest<any, any, any>,
endpointAppContext: EndpointAppContext
): Promise<Record<string, any>> => {
Expand Down Expand Up @@ -74,7 +74,7 @@ function buildQueryBody(request: KibanaRequest<any, any, any>): Record<string, a
};
}

export const kibanaRequestToEndpointFetchQuery = (
export const kibanaRequestToMetadataGetESQuery = (
request: KibanaRequest<any, any, any>,
endpointAppContext: EndpointAppContext
) => {
Expand Down
2 changes: 1 addition & 1 deletion x-pack/test/api_integration/apis/endpoint/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export default function endpointAPIIntegrationTests({ loadTestFile }: FtrProvide
describe('Endpoint plugin', function() {
this.tags(['endpoint']);
loadTestFile(require.resolve('./resolver'));
loadTestFile(require.resolve('./endpoints'));
loadTestFile(require.resolve('./metadata'));
loadTestFile(require.resolve('./alerts'));
});
}
Loading

0 comments on commit 7850e31

Please sign in to comment.