Skip to content

Commit 2aa359a

Browse files
[Endpoint] EMT-184: change endpoints to metadata up and down the code base. (#58038)
[Endpoint] EMT-184: change endpoints to metadata up and down the code base.
1 parent 5ad4c3d commit 2aa359a

File tree

13 files changed

+93
-58
lines changed

13 files changed

+93
-58
lines changed

x-pack/plugins/endpoint/public/applications/endpoint/store/managing/middleware.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ describe('endpoint list saga', () => {
7272
expect(fakeHttpServices.post).not.toHaveBeenCalled();
7373
dispatch({ type: 'userNavigatedToPage', payload: 'managementPage' });
7474
await sleep();
75-
expect(fakeHttpServices.post).toHaveBeenCalledWith('/api/endpoint/endpoints', {
75+
expect(fakeHttpServices.post).toHaveBeenCalledWith('/api/endpoint/metadata', {
7676
body: JSON.stringify({
7777
paging_properties: [{ page_index: 0 }, { page_size: 10 }],
7878
}),

x-pack/plugins/endpoint/public/applications/endpoint/store/managing/middleware.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export const managementMiddlewareFactory: MiddlewareFactory<ManagementListState>
1818
) {
1919
const managementPageIndex = pageIndex(getState());
2020
const managementPageSize = pageSize(getState());
21-
const response = await coreStart.http.post('/api/endpoint/endpoints', {
21+
const response = await coreStart.http.post('/api/endpoint/metadata', {
2222
body: JSON.stringify({
2323
paging_properties: [
2424
{ page_index: managementPageIndex },

x-pack/plugins/endpoint/server/plugin.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { createConfig$, EndpointConfigType } from './config';
1010
import { EndpointAppContext } from './types';
1111

1212
import { addRoutes } from './routes';
13-
import { registerEndpointRoutes } from './routes/endpoints';
13+
import { registerEndpointRoutes } from './routes/metadata';
1414
import { registerAlertRoutes } from './routes/alerts';
1515
import { registerResolverRoutes } from './routes/resolver';
1616

x-pack/plugins/endpoint/server/routes/endpoints.test.ts renamed to x-pack/plugins/endpoint/server/routes/metadata.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ import {
2020
} from '../../../../../src/core/server/mocks';
2121
import { EndpointMetadata, EndpointResultList } from '../../common/types';
2222
import { SearchResponse } from 'elasticsearch';
23-
import { registerEndpointRoutes } from './endpoints';
23+
import { registerEndpointRoutes } from './metadata';
2424
import { EndpointConfigSchema } from '../config';
25-
import * as data from '../test_data/all_endpoints_data.json';
25+
import * as data from '../test_data/all_metadata_data.json';
2626

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

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

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

149149
await routeHandler(
@@ -208,7 +208,7 @@ describe('test endpoint route', () => {
208208
})
209209
);
210210
[routeConfig, routeHandler] = routerMock.get.mock.calls.find(([{ path }]) =>
211-
path.startsWith('/api/endpoint/endpoints')
211+
path.startsWith('/api/endpoint/metadata')
212212
)!;
213213

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

245245
await routeHandler(

x-pack/plugins/endpoint/server/routes/endpoints.ts renamed to x-pack/plugins/endpoint/server/routes/metadata.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ import { SearchResponse } from 'elasticsearch';
99
import { schema } from '@kbn/config-schema';
1010

1111
import {
12-
kibanaRequestToEndpointListQuery,
13-
kibanaRequestToEndpointFetchQuery,
14-
} from '../services/endpoint/endpoint_query_builders';
12+
kibanaRequestToMetadataListESQuery,
13+
kibanaRequestToMetadataGetESQuery,
14+
} from '../services/endpoint/metadata_query_builders';
1515
import { EndpointMetadata, EndpointResultList } from '../../common/types';
1616
import { EndpointAppContext } from '../types';
1717

@@ -22,7 +22,7 @@ interface HitSource {
2222
export function registerEndpointRoutes(router: IRouter, endpointAppContext: EndpointAppContext) {
2323
router.post(
2424
{
25-
path: '/api/endpoint/endpoints',
25+
path: '/api/endpoint/metadata',
2626
validate: {
2727
body: schema.nullable(
2828
schema.object({
@@ -53,7 +53,7 @@ export function registerEndpointRoutes(router: IRouter, endpointAppContext: Endp
5353
},
5454
async (context, req, res) => {
5555
try {
56-
const queryParams = await kibanaRequestToEndpointListQuery(req, endpointAppContext);
56+
const queryParams = await kibanaRequestToMetadataListESQuery(req, endpointAppContext);
5757
const response = (await context.core.elasticsearch.dataClient.callAsCurrentUser(
5858
'search',
5959
queryParams
@@ -67,15 +67,15 @@ export function registerEndpointRoutes(router: IRouter, endpointAppContext: Endp
6767

6868
router.get(
6969
{
70-
path: '/api/endpoint/endpoints/{id}',
70+
path: '/api/endpoint/metadata/{id}',
7171
validate: {
7272
params: schema.object({ id: schema.string() }),
7373
},
7474
options: { authRequired: true },
7575
},
7676
async (context, req, res) => {
7777
try {
78-
const query = kibanaRequestToEndpointFetchQuery(req, endpointAppContext);
78+
const query = kibanaRequestToMetadataGetESQuery(req, endpointAppContext);
7979
const response = (await context.core.elasticsearch.dataClient.callAsCurrentUser(
8080
'search',
8181
query

x-pack/plugins/endpoint/server/services/endpoint/endpoint_query_builders.test.ts renamed to x-pack/plugins/endpoint/server/services/endpoint/metadata_query_builders.test.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,18 @@
66
import { httpServerMock, loggingServiceMock } from '../../../../../../src/core/server/mocks';
77
import { EndpointConfigSchema } from '../../config';
88
import {
9-
kibanaRequestToEndpointListQuery,
10-
kibanaRequestToEndpointFetchQuery,
11-
} from './endpoint_query_builders';
9+
kibanaRequestToMetadataListESQuery,
10+
kibanaRequestToMetadataGetESQuery,
11+
} from './metadata_query_builders';
12+
import { EndpointAppConstants } from '../../../common/types';
1213

1314
describe('query builder', () => {
14-
describe('EndpointListQuery', () => {
15-
it('test default query params for all endpoints when no params or body is provided', async () => {
15+
describe('MetadataListESQuery', () => {
16+
it('test default query params for all endpoints metadata when no params or body is provided', async () => {
1617
const mockRequest = httpServerMock.createKibanaRequest({
1718
body: {},
1819
});
19-
const query = await kibanaRequestToEndpointListQuery(mockRequest, {
20+
const query = await kibanaRequestToMetadataListESQuery(mockRequest, {
2021
logFactory: loggingServiceMock.create(),
2122
config: () => Promise.resolve(EndpointConfigSchema.validate({})),
2223
});
@@ -50,19 +51,19 @@ describe('query builder', () => {
5051
},
5152
from: 0,
5253
size: 10,
53-
index: 'endpoint-agent*',
54+
index: EndpointAppConstants.ENDPOINT_INDEX_NAME,
5455
} as Record<string, any>);
5556
});
5657
});
5758

5859
describe('test query builder with kql filter', () => {
59-
it('test default query params for all endpoints when no params or body is provided', async () => {
60+
it('test default query params for all endpoints metadata when body filter is provided', async () => {
6061
const mockRequest = httpServerMock.createKibanaRequest({
6162
body: {
6263
filter: 'not host.ip:10.140.73.246',
6364
},
6465
});
65-
const query = await kibanaRequestToEndpointListQuery(mockRequest, {
66+
const query = await kibanaRequestToMetadataListESQuery(mockRequest, {
6667
logFactory: loggingServiceMock.create(),
6768
config: () => Promise.resolve(EndpointConfigSchema.validate({})),
6869
});
@@ -109,20 +110,20 @@ describe('query builder', () => {
109110
},
110111
from: 0,
111112
size: 10,
112-
index: 'endpoint-agent*',
113+
index: EndpointAppConstants.ENDPOINT_INDEX_NAME,
113114
} as Record<string, any>);
114115
});
115116
});
116117

117-
describe('EndpointFetchQuery', () => {
118+
describe('MetadataGetQuery', () => {
118119
it('searches for the correct ID', () => {
119120
const mockID = 'AABBCCDD-0011-2233-AA44-DEADBEEF8899';
120121
const mockRequest = httpServerMock.createKibanaRequest({
121122
params: {
122123
id: mockID,
123124
},
124125
});
125-
const query = kibanaRequestToEndpointFetchQuery(mockRequest, {
126+
const query = kibanaRequestToMetadataGetESQuery(mockRequest, {
126127
logFactory: loggingServiceMock.create(),
127128
config: () => Promise.resolve(EndpointConfigSchema.validate({})),
128129
});
@@ -132,7 +133,7 @@ describe('query builder', () => {
132133
sort: [{ 'event.created': { order: 'desc' } }],
133134
size: 1,
134135
},
135-
index: 'endpoint-agent*',
136+
index: EndpointAppConstants.ENDPOINT_INDEX_NAME,
136137
});
137138
});
138139
});

x-pack/plugins/endpoint/server/services/endpoint/endpoint_query_builders.ts renamed to x-pack/plugins/endpoint/server/services/endpoint/metadata_query_builders.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { EndpointAppConstants } from '../../../common/types';
88
import { EndpointAppContext } from '../../types';
99
import { esKuery } from '../../../../../../src/plugins/data/server';
1010

11-
export const kibanaRequestToEndpointListQuery = async (
11+
export const kibanaRequestToMetadataListESQuery = async (
1212
request: KibanaRequest<any, any, any>,
1313
endpointAppContext: EndpointAppContext
1414
): Promise<Record<string, any>> => {
@@ -74,7 +74,7 @@ function buildQueryBody(request: KibanaRequest<any, any, any>): Record<string, a
7474
};
7575
}
7676

77-
export const kibanaRequestToEndpointFetchQuery = (
77+
export const kibanaRequestToMetadataGetESQuery = (
7878
request: KibanaRequest<any, any, any>,
7979
endpointAppContext: EndpointAppContext
8080
) => {

x-pack/test/api_integration/apis/endpoint/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export default function endpointAPIIntegrationTests({ loadTestFile }: FtrProvide
1010
describe('Endpoint plugin', function() {
1111
this.tags(['endpoint']);
1212
loadTestFile(require.resolve('./resolver'));
13-
loadTestFile(require.resolve('./endpoints'));
13+
loadTestFile(require.resolve('./metadata'));
1414
loadTestFile(require.resolve('./alerts'));
1515
});
1616
}

x-pack/test/api_integration/apis/endpoint/endpoints.ts renamed to x-pack/test/api_integration/apis/endpoint/metadata.ts

Lines changed: 57 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ import { FtrProviderContext } from '../../ftr_provider_context';
99
export default function({ getService }: FtrProviderContext) {
1010
const esArchiver = getService('esArchiver');
1111
const supertest = getService('supertest');
12-
describe('test endpoints api', () => {
13-
describe('POST /api/endpoint/endpoints when index is empty', () => {
14-
it('endpoints api should return empty result when index is empty', async () => {
15-
await esArchiver.unload('endpoint/endpoints/api_feature');
12+
describe('test metadata api', () => {
13+
describe('POST /api/endpoint/metadata when index is empty', () => {
14+
it('metadata api should return empty result when index is empty', async () => {
15+
await esArchiver.unload('endpoint/metadata/api_feature');
1616
const { body } = await supertest
17-
.post('/api/endpoint/endpoints')
17+
.post('/api/endpoint/metadata')
1818
.set('kbn-xsrf', 'xxx')
1919
.send()
2020
.expect(200);
@@ -25,12 +25,12 @@ export default function({ getService }: FtrProviderContext) {
2525
});
2626
});
2727

28-
describe('POST /api/endpoint/endpoints when index is not empty', () => {
29-
before(() => esArchiver.load('endpoint/endpoints/api_feature'));
30-
after(() => esArchiver.unload('endpoint/endpoints/api_feature'));
31-
it('endpoints api should return one entry for each endpoint with default paging', async () => {
28+
describe('POST /api/endpoint/metadata when index is not empty', () => {
29+
before(() => esArchiver.load('endpoint/metadata/api_feature'));
30+
after(() => esArchiver.unload('endpoint/metadata/api_feature'));
31+
it('metadata api should return one entry for each endpoint with default paging', async () => {
3232
const { body } = await supertest
33-
.post('/api/endpoint/endpoints')
33+
.post('/api/endpoint/metadata')
3434
.set('kbn-xsrf', 'xxx')
3535
.send()
3636
.expect(200);
@@ -40,9 +40,9 @@ export default function({ getService }: FtrProviderContext) {
4040
expect(body.request_page_index).to.eql(0);
4141
});
4242

43-
it('endpoints api should return page based on paging properties passed.', async () => {
43+
it('metadata api should return page based on paging properties passed.', async () => {
4444
const { body } = await supertest
45-
.post('/api/endpoint/endpoints')
45+
.post('/api/endpoint/metadata')
4646
.set('kbn-xsrf', 'xxx')
4747
.send({
4848
paging_properties: [
@@ -61,12 +61,12 @@ export default function({ getService }: FtrProviderContext) {
6161
expect(body.request_page_index).to.eql(1);
6262
});
6363

64-
/* test that when paging properties produces no result, the total should reflect the actual number of endpoints
64+
/* test that when paging properties produces no result, the total should reflect the actual number of metadata
6565
in the index.
6666
*/
67-
it('endpoints api should return accurate total endpoints if page index produces no result', async () => {
67+
it('metadata api should return accurate total metadata if page index produces no result', async () => {
6868
const { body } = await supertest
69-
.post('/api/endpoint/endpoints')
69+
.post('/api/endpoint/metadata')
7070
.set('kbn-xsrf', 'xxx')
7171
.send({
7272
paging_properties: [
@@ -85,9 +85,9 @@ export default function({ getService }: FtrProviderContext) {
8585
expect(body.request_page_index).to.eql(30);
8686
});
8787

88-
it('endpoints api should return 400 when pagingProperties is below boundaries.', async () => {
88+
it('metadata api should return 400 when pagingProperties is below boundaries.', async () => {
8989
const { body } = await supertest
90-
.post('/api/endpoint/endpoints')
90+
.post('/api/endpoint/metadata')
9191
.set('kbn-xsrf', 'xxx')
9292
.send({
9393
paging_properties: [
@@ -103,9 +103,9 @@ export default function({ getService }: FtrProviderContext) {
103103
expect(body.message).to.contain('Value is [0] but it must be equal to or greater than [1]');
104104
});
105105

106-
it('endpoints api should return page based on filters passed.', async () => {
106+
it('metadata api should return page based on filters passed.', async () => {
107107
const { body } = await supertest
108-
.post('/api/endpoint/endpoints')
108+
.post('/api/endpoint/metadata')
109109
.set('kbn-xsrf', 'xxx')
110110
.send({ filter: 'not host.ip:10.101.149.26' })
111111
.expect(200);
@@ -115,10 +115,10 @@ export default function({ getService }: FtrProviderContext) {
115115
expect(body.request_page_index).to.eql(0);
116116
});
117117

118-
it('endpoints api should return page based on filters and paging passed.', async () => {
118+
it('metadata api should return page based on filters and paging passed.', async () => {
119119
const notIncludedIp = '10.101.149.26';
120120
const { body } = await supertest
121-
.post('/api/endpoint/endpoints')
121+
.post('/api/endpoint/metadata')
122122
.set('kbn-xsrf', 'xxx')
123123
.send({
124124
paging_properties: [
@@ -143,10 +143,10 @@ export default function({ getService }: FtrProviderContext) {
143143
expect(body.request_page_index).to.eql(0);
144144
});
145145

146-
it('endpoints api should return page based on host.os.variant filter.', async () => {
146+
it('metadata api should return page based on host.os.variant filter.', async () => {
147147
const variantValue = 'Windows Pro';
148148
const { body } = await supertest
149-
.post('/api/endpoint/endpoints')
149+
.post('/api/endpoint/metadata')
150150
.set('kbn-xsrf', 'xxx')
151151
.send({
152152
filter: `host.os.variant.keyword:${variantValue}`,
@@ -161,6 +161,40 @@ export default function({ getService }: FtrProviderContext) {
161161
expect(body.request_page_size).to.eql(10);
162162
expect(body.request_page_index).to.eql(0);
163163
});
164+
165+
it('metadata api should return the latest event for all the events for an endpoint', async () => {
166+
const targetEndpointIp = '10.192.213.130';
167+
const { body } = await supertest
168+
.post('/api/endpoint/metadata')
169+
.set('kbn-xsrf', 'xxx')
170+
.send({
171+
filter: `host.ip:${targetEndpointIp}`,
172+
})
173+
.expect(200);
174+
expect(body.total).to.eql(1);
175+
const resultIp: string = body.endpoints[0].host.ip.filter(
176+
(ip: string) => ip === targetEndpointIp
177+
);
178+
expect(resultIp).to.eql([targetEndpointIp]);
179+
expect(body.endpoints[0].event.created).to.eql('2020-01-24T16:06:09.541Z');
180+
expect(body.endpoints.length).to.eql(1);
181+
expect(body.request_page_size).to.eql(10);
182+
expect(body.request_page_index).to.eql(0);
183+
});
184+
185+
it('metadata api should return all endpoints when filter is empty string', async () => {
186+
const { body } = await supertest
187+
.post('/api/endpoint/metadata')
188+
.set('kbn-xsrf', 'xxx')
189+
.send({
190+
filter: '',
191+
})
192+
.expect(200);
193+
expect(body.total).to.eql(3);
194+
expect(body.endpoints.length).to.eql(3);
195+
expect(body.request_page_size).to.eql(10);
196+
expect(body.request_page_index).to.eql(0);
197+
});
164198
});
165199
});
166200
}

0 commit comments

Comments
 (0)