-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[EDR Workflows] Osquery Live Query PIT Search (#171556)
Closes #169726 Current implementation do not use Point In Time search but tries to paginate through the responses to fetch all available agents with a page size of 9000. This is causing the error reported in the linked ticket when we try to fetch results from 9,000 to 18,000 ![277793068-f777179c-7eab-4349-8b5b-2afdf9318769](https://github.com/elastic/kibana/assets/29123534/4a75d685-5042-4387-ba5e-dd0585904f85) This PR adds Point In Time search functionality when trying to fetch: 1. All agents 2. All agents of a policy 3. All agents of a platform Since I believe querying 10k+ agents plus isn't done often I always start with no PIT search and if pages total is greater than 1 I refetch, this time, with PIT and searchAfter params. Tested for packs and single query live queries. https://github.com/elastic/kibana/assets/29123534/b0bb831c-9633-4fbb-b0a8-291889f68543 https://github.com/elastic/kibana/assets/29123534/49393627-e3c5-4346-ab74-287fdde9ecf8 https://github.com/elastic/kibana/assets/29123534/62063fa4-b2cd-4b6d-9976-23c89501652f
- Loading branch information
1 parent
2eba909
commit 25177aa
Showing
3 changed files
with
192 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
x-pack/plugins/osquery/server/lib/parse_agent_groups.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* 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 { aggregateResults } from './parse_agent_groups'; | ||
import type { ElasticsearchClientMock } from '@kbn/core-elasticsearch-client-server-mocks'; | ||
import type { OsqueryAppContext } from './osquery_app_context_services'; | ||
|
||
const mockOpenPointInTime = jest.fn().mockResolvedValue({ id: 'mockedPitId' }); | ||
const mockClosePointInTime = jest.fn(); | ||
|
||
const mockElasticsearchClient = { | ||
openPointInTime: mockOpenPointInTime, | ||
closePointInTime: mockClosePointInTime, | ||
} as unknown as ElasticsearchClientMock; | ||
|
||
const mockContext = {} as unknown as OsqueryAppContext; | ||
|
||
describe('aggregateResults', () => { | ||
it('should handle one page of results', async () => { | ||
const generatorMock = jest.fn().mockResolvedValue({ | ||
results: ['result1', 'result2'], | ||
total: 2, | ||
}); | ||
|
||
const result = await aggregateResults(generatorMock, mockElasticsearchClient, mockContext); | ||
|
||
expect(generatorMock).toHaveBeenCalledWith(1, expect.any(Number)); // 1st page, PER_PAGE | ||
expect(mockOpenPointInTime).not.toHaveBeenCalled(); | ||
expect(mockClosePointInTime).not.toHaveBeenCalled(); | ||
|
||
expect(result).toEqual(['result1', 'result2']); | ||
}); | ||
|
||
it('should handle multiple pages of results', async () => { | ||
const generateResults = (run = 1, length = 9000) => | ||
Array.from({ length }, (_, index) => `result_${index + 1 + (run - 1) * length}`); | ||
|
||
const generatorMock = jest | ||
.fn() | ||
.mockResolvedValueOnce({ | ||
results: generateResults(), | ||
total: 18001, | ||
}) | ||
.mockResolvedValueOnce({ | ||
results: generateResults(), | ||
total: 18001, | ||
searchAfter: ['firstSort'], | ||
}) | ||
.mockResolvedValueOnce({ | ||
results: generateResults(2), | ||
total: 18001, | ||
searchAfter: ['secondSort'], | ||
}) | ||
.mockResolvedValueOnce({ | ||
results: ['result_18001'], | ||
total: 18001, | ||
searchAfter: ['thirdSort'], | ||
}); | ||
|
||
const result = await aggregateResults(generatorMock, mockElasticsearchClient, mockContext); | ||
expect(generatorMock).toHaveBeenCalledWith(1, expect.any(Number)); | ||
expect(generatorMock).toHaveBeenCalledWith(1, expect.any(Number), undefined, 'mockedPitId'); | ||
expect(generatorMock).toHaveBeenCalledWith(2, expect.any(Number), ['firstSort'], 'mockedPitId'); | ||
expect(generatorMock).toHaveBeenCalledWith( | ||
3, | ||
expect.any(Number), | ||
['secondSort'], | ||
'mockedPitId' | ||
); | ||
expect(mockOpenPointInTime).toHaveBeenCalledTimes(1); | ||
expect(mockClosePointInTime).toHaveBeenCalledTimes(1); | ||
expect(mockClosePointInTime).toHaveBeenCalledWith({ id: 'mockedPitId' }); | ||
expect(result.length).toEqual(18001); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters