Skip to content

Commit

Permalink
Revert "Move consumers off bsearch endpoint (elastic#196962)"
Browse files Browse the repository at this point in the history
This reverts commit 257688d.
  • Loading branch information
davismcphee committed Nov 8, 2024
1 parent 619f330 commit ca0c95c
Show file tree
Hide file tree
Showing 33 changed files with 233 additions and 177 deletions.
2 changes: 1 addition & 1 deletion packages/kbn-ftr-common-functional-services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export type SamlAuthProviderType = ProvidedType<typeof SamlAuthProvider>;
export type { FtrProviderContext } from './services/ftr_provider_context';
export { runSavedObjInfoSvc } from './services/saved_object_info';

export type { SearchService, SendOptions } from './services/search';
export type { BsearchService, SendOptions } from './services/bsearch';
export { SavedObjectInfoService } from './services/saved_object_info';
export { DeploymentService } from './services/deployment';
export { IndexPatternsService } from './services/index_patterns';
Expand Down
4 changes: 2 additions & 2 deletions packages/kbn-ftr-common-functional-services/services/all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { EsArchiverProvider } from './es_archiver';
import { EsProvider } from './es';
import { KibanaServerProvider } from './kibana_server';
import { RetryService } from './retry';
import { SearchService } from './search';
import { BsearchService } from './bsearch';
import { ConsoleProvider } from './console';
import { DeploymentService } from './deployment';
import { EsDeleteAllIndicesProvider } from './es_delete_all_indices';
Expand All @@ -27,7 +27,7 @@ export const services = {
kibanaServer: KibanaServerProvider,
esArchiver: EsArchiverProvider,
retry: RetryService,
search: SearchService,
bsearch: BsearchService,
console: ConsoleProvider,
deployment: DeploymentService,
esDeleteAllIndices: EsDeleteAllIndicesProvider,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,26 @@
*/

import expect from '@kbn/expect';
import request from 'superagent';
import type SuperTest from 'supertest';
import type { IEsSearchResponse } from '@kbn/search-types';
import { ELASTIC_HTTP_VERSION_HEADER } from '@kbn/core-http-common';
import { BFETCH_ROUTE_VERSION_LATEST } from '@kbn/bfetch-plugin/common';
import { FtrService } from './ftr_provider_context';

/**
* Function copied from here:
* test/api_integration/apis/search/bsearch.ts without the compress
*
* Splits the JSON lines from bsearch
*/
const parseBfetchResponse = (resp: request.Response): Array<Record<string, any>> => {
return resp.text
.trim()
.split('\n')
.map((item) => JSON.parse(item));
};

/**
* Function copied from here:
* x-pack/test/rule_registry/common/lib/authentication/spaces.ts
Expand All @@ -33,13 +48,13 @@ export interface SendOptions {
}

/**
* Search Service that can reduce flake on the CI systems when they are under
* pressure and search returns an async search response or a sync response.
* Bsearch Service that can reduce flake on the CI systems when they are under
* pressure and bsearch returns an async search response or a sync response.
*
* @example
* const supertest = getService('supertest');
* const search = getService('search');
* const response = await search.send<MyType>({
* const bsearch = getService('bsearch');
* const response = await bsearch.send<MyType>({
* supertest,
* options: {
* defaultIndex: ['large_volume_dns_data'],
Expand All @@ -49,7 +64,7 @@ export interface SendOptions {
* expect(response).eql({ ... your value ... });
*/

export class SearchService extends FtrService {
export class BsearchService extends FtrService {
private readonly retry = this.ctx.getService('retry');

/** Send method to send in your supertest, url, options, and strategy name */
Expand All @@ -70,13 +85,26 @@ export class SearchService extends FtrService {

const result = await this.retry.try(async () => {
const resp = await supertest
.post(`${spaceUrl}/internal/search/${strategy}/${body.id}`)
.post(`${spaceUrl}/internal/bsearch`)
.set('kbn-xsrf', 'true')
.set(ELASTIC_HTTP_VERSION_HEADER, '1')
.send()
.set(ELASTIC_HTTP_VERSION_HEADER, BFETCH_ROUTE_VERSION_LATEST)
.send({
batch: [
{
request: {
id: body.id,
...options,
},
options: {
strategy,
},
},
],
})
.expect(200);
expect(resp.body.isRunning).equal(false);
return resp.body as T;
const [parsedResponse] = parseBfetchResponse(resp);
expect(parsedResponse.result.isRunning).equal(false);
return parsedResponse.result as T;
});
return result;
}
Expand Down
1 change: 1 addition & 0 deletions packages/kbn-ftr-common-functional-services/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"@kbn/expect",
"@kbn/search-types",
"@kbn/core-http-common",
"@kbn/bfetch-plugin",
"@kbn/data-plugin",
"@kbn/dev-cli-runner",
"@kbn/dev-cli-errors",
Expand Down
4 changes: 2 additions & 2 deletions test/common/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const {
esDeleteAllIndices,
savedObjectInfo,
indexPatterns,
search,
bsearch,
console,
supertest,
esSupertest,
Expand All @@ -42,7 +42,7 @@ export const services = {
esDeleteAllIndices,
savedObjectInfo,
indexPatterns,
search,
bsearch,
console,
supertest,
esSupertest,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ export const generateFindingHit = (finding: CspFinding) => {
};
};

const getFindingsSearchResponse = (findings: CspFinding[]) => {
const getFindingsBsearchResponse = (findings: CspFinding[]) => {
const buckets = findings.reduce(
(acc, finding) => {
if (finding.result.evaluation === 'failed') {
Expand All @@ -174,26 +174,28 @@ const getFindingsSearchResponse = (findings: CspFinding[]) => {
);

return {
id: '1',
rawResponse: {
took: 1,
timed_out: false,
_shards: {
total: 1,
successful: 1,
skipped: 0,
failed: 0,
},
hits: {
total: findings.length,
max_score: null,
hits: findings.map(generateFindingHit),
},
aggregations: {
count: {
doc_count_error_upper_bound: 0,
sum_other_doc_count: 0,
buckets,
id: 0,
result: {
rawResponse: {
took: 1,
timed_out: false,
_shards: {
total: 1,
successful: 1,
skipped: 0,
failed: 0,
},
hits: {
total: findings.length,
max_score: null,
hits: findings.map(generateFindingHit),
},
aggregations: {
count: {
doc_count_error_upper_bound: 0,
sum_other_doc_count: 0,
buckets,
},
},
},
isPartial: false,
Expand All @@ -212,8 +214,8 @@ export const rulesGetStatesHandler = http.get(
}
);

export const searchFindingsHandler = (findings: CspFinding[]) =>
http.post('internal/search', async ({ request }) => {
export const bsearchFindingsHandler = (findings: CspFinding[]) =>
http.post('internal/bsearch', async ({ request }) => {
const jsonRequest = (await request.json()) as Partial<estypes.SearchRequest>;

const filter = jsonRequest?.query?.bool?.filter;
Expand All @@ -231,7 +233,7 @@ export const searchFindingsHandler = (findings: CspFinding[]) =>
return finding.rule.section === termValue;
});

return HttpResponse.json(getFindingsSearchResponse(filteredFindings));
return HttpResponse.json(getFindingsBsearchResponse(filteredFindings));
}

const hasRuleSectionFilter =
Expand All @@ -242,7 +244,7 @@ export const searchFindingsHandler = (findings: CspFinding[]) =>
return finding.rule.section === filter?.[0]?.match_phrase?.['rule.section'];
});

return HttpResponse.json(getFindingsSearchResponse(filteredFindings));
return HttpResponse.json(getFindingsBsearchResponse(filteredFindings));
}

const hasResultEvaluationFilter =
Expand All @@ -253,8 +255,8 @@ export const searchFindingsHandler = (findings: CspFinding[]) =>
return finding.result.evaluation === filter?.[0]?.match_phrase?.['result.evaluation'];
});

return HttpResponse.json(getFindingsSearchResponse(filteredFindings));
return HttpResponse.json(getFindingsBsearchResponse(filteredFindings));
}

return HttpResponse.json(getFindingsSearchResponse(findings));
return HttpResponse.json(getFindingsBsearchResponse(findings));
});
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { FilterManager } from '@kbn/data-plugin/public';
import { CspClientPluginStartDeps } from '@kbn/cloud-security-posture';
import * as statusHandlers from '../../../server/routes/status/status.handlers.mock';
import {
searchFindingsHandler,
bsearchFindingsHandler,
generateCspFinding,
generateMultipleCspFindings,
rulesGetStatesHandler,
Expand Down Expand Up @@ -58,7 +58,7 @@ describe('<Findings />', () => {
const finding2 = generateCspFinding('0004', 'passed');

server.use(statusHandlers.notInstalledHasMisconfigurationsFindingsHandler);
server.use(searchFindingsHandler([finding1, finding2]));
server.use(bsearchFindingsHandler([finding1, finding2]));
renderFindingsPage();

// Loading while checking the status API and fetching the findings
Expand Down Expand Up @@ -89,7 +89,7 @@ describe('<Findings />', () => {
const finding2 = generateCspFinding('0002', 'passed');

server.use(statusHandlers.indexedHandler);
server.use(searchFindingsHandler([finding1, finding2]));
server.use(bsearchFindingsHandler([finding1, finding2]));
renderFindingsPage();

// Loading while checking the status API
Expand Down Expand Up @@ -118,7 +118,7 @@ describe('<Findings />', () => {
const finding2 = generateCspFinding('0002', 'passed');

server.use(statusHandlers.indexedHandler);
server.use(searchFindingsHandler([finding1, finding2]));
server.use(bsearchFindingsHandler([finding1, finding2]));

renderFindingsPage();

Expand Down Expand Up @@ -148,7 +148,7 @@ describe('<Findings />', () => {
const finding2 = generateCspFinding('0002', 'passed');

server.use(statusHandlers.indexedHandler);
server.use(searchFindingsHandler([finding1, finding2]));
server.use(bsearchFindingsHandler([finding1, finding2]));

renderFindingsPage();

Expand Down Expand Up @@ -180,7 +180,7 @@ describe('<Findings />', () => {
const finding2 = generateCspFinding('0002', 'passed');

server.use(statusHandlers.indexedHandler);
server.use(searchFindingsHandler([finding1, finding2]));
server.use(bsearchFindingsHandler([finding1, finding2]));

renderFindingsPage();

Expand Down Expand Up @@ -259,7 +259,7 @@ describe('<Findings />', () => {
};

server.use(statusHandlers.indexedHandler);
server.use(searchFindingsHandler([finding1, finding2]));
server.use(bsearchFindingsHandler([finding1, finding2]));

renderFindingsPage(mockDependenciesWithFilter);

Expand All @@ -286,7 +286,7 @@ describe('<Findings />', () => {
it('renders the distribution bar', async () => {
server.use(statusHandlers.indexedHandler);
server.use(
searchFindingsHandler(
bsearchFindingsHandler(
generateMultipleCspFindings({
count: 10,
failedCount: 3,
Expand Down Expand Up @@ -316,7 +316,7 @@ describe('<Findings />', () => {
it('filters by passed findings when clicking on the passed findings button', async () => {
server.use(statusHandlers.indexedHandler);
server.use(
searchFindingsHandler(
bsearchFindingsHandler(
generateMultipleCspFindings({
count: 2,
failedCount: 1,
Expand Down Expand Up @@ -352,7 +352,7 @@ describe('<Findings />', () => {
it('filters by failed findings when clicking on the failed findings button', async () => {
server.use(statusHandlers.indexedHandler);
server.use(
searchFindingsHandler(
bsearchFindingsHandler(
generateMultipleCspFindings({
count: 2,
failedCount: 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jest.mock('rxjs', () => {
...actual,
lastValueFrom: async (source: Promise<any>) => {
const value = await source;
return value;
return value.result;
},
};
});
Expand Down Expand Up @@ -97,7 +97,7 @@ export const getMockServerDependencies = () => {
search: {
...getMockDependencies().data.search,
search: async ({ params }: { params: any }) => {
const response = await fetch(`${MOCK_SERVER_BASE_URL}/internal/search`, {
const response = await fetch(`${MOCK_SERVER_BASE_URL}/internal/bsearch`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export const useEsSearch = <DocumentSource extends unknown, TParams extends esty
operationName: name,
kibanaRequest: {
route: {
path: '/internal/search',
path: '/internal/bsearch',
method: 'POST',
},
} as any,
Expand Down Expand Up @@ -95,7 +95,7 @@ export const useEsSearch = <DocumentSource extends unknown, TParams extends esty
operationName: name,
kibanaRequest: {
route: {
path: '/internal/search',
path: '/internal/bsearch',
method: 'POST',
},
} as any,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export const executeEsQueryAPI = async ({
operationName: name,
kibanaRequest: {
route: {
path: '/internal/search',
path: '/internal/bsearch',
method: 'POST',
},
} as any,
Expand Down Expand Up @@ -82,7 +82,7 @@ export const executeEsQueryAPI = async ({
operationName: name,
kibanaRequest: {
route: {
path: '/internal/search',
path: '/internal/bsearch',
method: 'POST',
},
} as any,
Expand Down
Loading

0 comments on commit ca0c95c

Please sign in to comment.