[Elasticsearch] Skip the Product Check on the scopeable client (#110767)

This commit is contained in:
Alejandro Fernández Haro 2021-09-01 13:07:17 +01:00 committed by GitHub
parent a386dfe994
commit 3a434d710e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 2 deletions

View file

@ -10,6 +10,7 @@ import { Buffer } from 'buffer';
import { Readable } from 'stream';
import { RequestEvent, errors } from '@elastic/elasticsearch';
import type { Client } from '@elastic/elasticsearch';
import type {
TransportRequestOptions,
TransportRequestParams,
@ -18,7 +19,6 @@ import type {
import { parseClientOptionsMock, ClientMock } from './configure_client.test.mocks';
import { loggingSystemMock } from '../../logging/logging_system.mock';
import { EventEmitter } from 'events';
import type { ElasticsearchClientConfig } from './client_config';
import { configureClient } from './configure_client';
@ -32,7 +32,10 @@ const createFakeConfig = (
};
const createFakeClient = () => {
const client = new EventEmitter();
const actualEs = jest.requireActual('@elastic/elasticsearch');
const client = new actualEs.Client({
nodes: ['http://localhost'], // Enforcing `nodes` because it's mandatory
});
jest.spyOn(client, 'on');
return client;
};
@ -67,6 +70,14 @@ const createApiResponse = <T>({
};
};
function getProductCheckValue(client: Client) {
const tSymbol = Object.getOwnPropertySymbols(client.transport || client).filter(
(symbol) => symbol.description === 'product check'
)[0];
// @ts-expect-error `tSymbol` is missing in the index signature of Transport
return (client.transport || client)[tSymbol];
}
describe('configureClient', () => {
let logger: ReturnType<typeof loggingSystemMock.createLogger>;
let config: ElasticsearchClientConfig;
@ -117,6 +128,24 @@ describe('configureClient', () => {
expect(client.on).toHaveBeenCalledWith('response', expect.any(Function));
});
describe('Product check', () => {
it('should not skip the product check for the unscoped client', () => {
const client = configureClient(config, { logger, type: 'test', scoped: false });
expect(getProductCheckValue(client)).toBe(0);
});
it('should skip the product check for the scoped client', () => {
const client = configureClient(config, { logger, type: 'test', scoped: true });
expect(getProductCheckValue(client)).toBe(2);
});
it('should skip the product check for the children of the scoped client', () => {
const client = configureClient(config, { logger, type: 'test', scoped: true });
const asScoped = client.child({ headers: { 'x-custom-header': 'Custom value' } });
expect(getProductCheckValue(asScoped)).toBe(2);
});
});
describe('Client logging', () => {
function createResponseWithBody(body?: RequestBody) {
return createApiResponse({

View file

@ -49,6 +49,12 @@ export const configureClient = (
const client = new Client({ ...clientOptions, Transport: KibanaTransport });
addLogging(client, logger.get('query', type));
// --------------------------------------------------------------------------------- //
// Hack to disable the "Product check" only in the scoped clients while we //
// come up with a better approach in https://github.com/elastic/kibana/issues/110675 //
if (scoped) skipProductCheck(client);
// --------------------------------------------------------------------------------- //
return client;
};
@ -131,3 +137,21 @@ const addLogging = (client: Client, logger: Logger) => {
}
});
};
/**
* Hack to skip the Product Check performed by the Elasticsearch-js client.
* We noticed that the scoped clients are always performing this check because
* of the way we initialize the clients. We'll discuss changing this in the issue
* https://github.com/elastic/kibana/issues/110675. In the meanwhile, let's skip
* it for the scoped clients.
*
* The hack is copied from the test/utils in the elasticsearch-js repo
* (https://github.com/elastic/elasticsearch-js/blob/master/test/utils/index.js#L45-L56)
*/
function skipProductCheck(client: Client) {
const tSymbol = Object.getOwnPropertySymbols(client.transport || client).filter(
(symbol) => symbol.description === 'product check'
)[0];
// @ts-expect-error `tSymbol` is missing in the index signature of Transport
(client.transport || client)[tSymbol] = 2;
}