Skip to content

Commit

Permalink
Elasticsearch client: no longer default to using meta: true (#124488)
Browse files Browse the repository at this point in the history
* Use `Client` interface instead of `KibanaClient`

* get rid of getKibanaEsClient and convertToKibanaClient

* get rid of last KibanaClient usages

* update usages and types in @kbn/securitysolution-es-utils

* fix some violations

* add sugar method around client mock

* update SO repository calls

* adapt more core usages

* export mock types

* batch 1

* batch 2

* batch 3

* batch 4

* batch 5

* batch 6

* batch 7

* batch 8

* batch 9

* security - batch 1

* security - batch 2

* security - batch 3

* last batch of initial violations

* fix resolve_time_pattern

* update generated doc

* fix /internal/index-pattern-management/preview_scripted_field endpoint

* fix monitoring's getLegacyClusterShim

* fix /api/snapshot_restore/privileges route

* fix UptimeESClient

* fix transforms/_nodes endpoint

* lint

* unit test fix - batch 1

* unit test fix - batch 2

* unit test fix - batch 3

* integration test fix - batch 1

* lint

* adapt ML client

* unit test fix - batch 4

* fix uptime test helper

* fix /api/transform/transforms/{transformId}/_update route

* fix ES client FTR test

* fix uptime unit test

* fix type errors on last unit tests

* fix RollupSearchStrategy call

* fix /internal/security/fields/{query} route

* fix GET /api/index_lifecycle_management/policies route

* fix mlClient.getDataFrameAnalytics

* fix APMEventClient

* fix security solution getBootstrapIndexExists

* fix data_enhanced's getSearchStatus

* remove unused @ts-expect-error

* fix unit tests due to latest code changes

* fix more calls in security_solution routes

* fix more calls in ml routes

* fix POST /api/index_management/component_templates route

* fix unit tests due to latest changes

* fix rule_registry's ResourceInstaller.createOrUpdateIndexTemplate

* fix more fleet client calls

* fix UA's GET cloud_backup_status route

* fix createLifecycleExecutorApiTest

* fix hasFleetServers

* fix unit tests due to latest changes

* changes due to last merge

* fix ml modelProvider.getModelsPipelines

* fix security_solution LifecycleQuery.search

* fix new CoreUsageDataService usage

* fix security solution's StatsQuery.search

* improve ml FTR assertions

* fix security_solution's EventsQuery.search

* fix EsClient type as we're keeping transport

* NITs

* clean RepositoryEsClient type

* update generated doc

* review comments

* adapt mlClient.anomalySearch signature

* remove unnecessary .then((body) => body)

* nit

* add unit tests for the client mocking functions

* fix new upgrade assistant /remote_clusters endpoint
  • Loading branch information
pgayvallet authored Feb 12, 2022
1 parent 172bf98 commit 6627bd8
Show file tree
Hide file tree
Showing 685 changed files with 6,845 additions and 7,893 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,5 @@ Client used to query the elasticsearch cluster.
<b>Signature:</b>

```typescript
export declare type ElasticsearchClient = Omit<KibanaClient, 'connectionPool' | 'transport' | 'serializer' | 'extend' | 'child' | 'close' | 'diagnostic'> & {
transport: {
request<TResponse = unknown>(params: TransportRequestParams, options?: TransportRequestOptions): Promise<TransportResult<TResponse>>;
};
};
export declare type ElasticsearchClient = Omit<Client, 'connectionPool' | 'serializer' | 'extend' | 'child' | 'close' | 'diagnostic'>;
```
2 changes: 1 addition & 1 deletion examples/preboot_example/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export class PrebootExamplePlugin implements PrebootPlugin {

try {
return response.ok({
body: (await scopedClient.asCurrentUser.security.authenticate()).body,
body: await scopedClient.asCurrentUser.security.authenticate(),
});
} catch (err) {
return response.customError({ statusCode: 500, body: getDetailedErrorMessage(err) });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
// Copied from src/core/server/elasticsearch/client/types.ts
// as these types aren't part of any package yet. Once they are, remove this completely

import type { KibanaClient } from '@elastic/elasticsearch/lib/api/kibana';
import type { Client } from '@elastic/elasticsearch';

/**
* Client used to query the elasticsearch cluster.
* @deprecated At some point use the one from src/core/server/elasticsearch/client/types.ts when it is made into a package. If it never is, then keep using this one.
* @public
*/
export type ElasticsearchClient = Omit<
KibanaClient,
'connectionPool' | 'transport' | 'serializer' | 'extend' | 'child' | 'close' | 'diagnostic'
Client,
'connectionPool' | 'serializer' | 'extend' | 'child' | 'close' | 'diagnostic'
>;
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const getBootstrapIndexExists = async (
index: string
): Promise<boolean> => {
try {
const { body } = await esClient.indices.getAlias({
const body = await esClient.indices.getAlias({
index: `${index}-*`,
name: index,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,6 @@

import type { ElasticsearchClient } from '../elasticsearch_client';

interface AliasesResponse {
[indexName: string]: {
aliases: {
[aliasName: string]: {
is_write_index: boolean;
};
};
};
}

interface IndexAlias {
alias: string;
index: string;
Expand All @@ -39,7 +29,7 @@ export const getIndexAliases = async ({
esClient: ElasticsearchClient;
alias: string;
}): Promise<IndexAlias[]> => {
const response = await esClient.indices.getAlias<AliasesResponse>(
const response = await esClient.indices.getAlias(
{
name: alias,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const getIndexCount = async ({
esClient: ElasticsearchClient;
index: string;
}): Promise<number> => {
const response = await esClient.count<{ count: number }>(
const response = await esClient.count(
{
index,
},
Expand Down
35 changes: 0 additions & 35 deletions packages/kbn-test/src/es/client_to_kibana_client.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/kbn-test/src/es/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@
export { createTestEsCluster } from './test_es_cluster';
export type { CreateTestEsClusterOptions, EsTestCluster, ICluster } from './test_es_cluster';
export { esTestConfig } from './es_test_config';
export { convertToKibanaClient } from './client_to_kibana_client';
export { createEsClientForTesting, createEsClientForFtrConfig } from './es_client_for_testing';
export type { EsClientForTestingOptions } from './es_client_for_testing';
10 changes: 0 additions & 10 deletions packages/kbn-test/src/es/test_es_cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ import del from 'del';
// @ts-expect-error in js
import { Cluster } from '@kbn/es';
import { Client, HttpConnection } from '@elastic/elasticsearch';
import type { KibanaClient } from '@elastic/elasticsearch/lib/api/kibana';
import type { ToolingLog } from '@kbn/dev-utils';
import { CI_PARALLEL_PROCESS_PREFIX } from '../ci_parallel_process_prefix';
import { esTestConfig } from './es_test_config';
import { convertToKibanaClient } from './client_to_kibana_client';

import { KIBANA_ROOT } from '../';

Expand Down Expand Up @@ -53,7 +51,6 @@ export interface ICluster {
stop: () => Promise<void>;
cleanup: () => Promise<void>;
getClient: () => Client;
getKibanaEsClient: () => KibanaClient;
getHostUrls: () => string[];
}

Expand Down Expand Up @@ -289,13 +286,6 @@ export function createTestEsCluster<
});
}

/**
* Returns an ES Client to the configured cluster
*/
getKibanaEsClient(): KibanaClient {
return convertToKibanaClient(this.getClient());
}

getUrl() {
if (this.nodes.length > 1) {
throw new Error(
Expand Down
1 change: 0 additions & 1 deletion packages/kbn-test/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ export type {
export {
esTestConfig,
createTestEsCluster,
convertToKibanaClient,
createEsClientForTesting,
createEsClientForFtrConfig,
} from './es';
Expand Down
5 changes: 1 addition & 4 deletions src/core/public/public.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import { Action } from 'history';
import Boom from '@hapi/boom';
import { ByteSizeValue } from '@kbn/config-schema';
import type { Client } from '@elastic/elasticsearch';
import { ConfigPath } from '@kbn/config';
import { DetailedPeerCertificate } from 'tls';
import type { DocLinks } from '@kbn/doc-links';
Expand All @@ -24,7 +25,6 @@ import { History as History_2 } from 'history';
import { Href } from 'history';
import { IconType } from '@elastic/eui';
import { IncomingHttpHeaders } from 'http';
import type { KibanaClient } from '@elastic/elasticsearch/lib/api/kibana';
import { Location as Location_2 } from 'history';
import { LocationDescriptorObject } from 'history';
import { Logger } from '@kbn/logging';
Expand All @@ -44,9 +44,6 @@ import * as Rx from 'rxjs';
import { SchemaTypeError } from '@kbn/config-schema';
import type { ThemeVersion } from '@kbn/ui-shared-deps-npm';
import { TransitionPromptHook } from 'history';
import type { TransportRequestOptions } from '@elastic/elasticsearch';
import type { TransportRequestParams } from '@elastic/elasticsearch';
import type { TransportResult } from '@elastic/elasticsearch';
import { Type } from '@kbn/config-schema';
import { TypeOf } from '@kbn/config-schema';
import { UiCounterMetricType } from '@kbn/analytics';
Expand Down
64 changes: 27 additions & 37 deletions src/core/server/core_usage_data/core_usage_data_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,47 +207,37 @@ describe('CoreUsageDataService', () => {
});
service.setup({ http, metrics, savedObjectsStartPromise, changedDeprecatedConfigPath$ });
const elasticsearch = elasticsearchServiceMock.createStart();
elasticsearch.client.asInternalUser.cat.indices.mockResolvedValueOnce({
body: [
{
name: '.kibana_task_manager_1',
'docs.count': '10',
'docs.deleted': '10',
'store.size': '1000',
'pri.store.size': '2000',
},
],
} as any);
elasticsearch.client.asInternalUser.count.mockResolvedValueOnce({
body: {
count: '15',
elasticsearch.client.asInternalUser.cat.indices.mockResponseOnce([
{
name: '.kibana_task_manager_1',
'docs.count': '10',
'docs.deleted': '10',
'store.size': '1000',
'pri.store.size': '2000',
},
] as any);
elasticsearch.client.asInternalUser.count.mockResponseOnce({
count: '15',
} as any);
elasticsearch.client.asInternalUser.cat.indices.mockResolvedValueOnce({
body: [
{
name: '.kibana_1',
'docs.count': '20',
'docs.deleted': '20',
'store.size': '2000',
'pri.store.size': '4000',
},
],
} as any);
elasticsearch.client.asInternalUser.count.mockResolvedValueOnce({
body: {
count: '10',
elasticsearch.client.asInternalUser.cat.indices.mockResponseOnce([
{
name: '.kibana_1',
'docs.count': '20',
'docs.deleted': '20',
'store.size': '2000',
'pri.store.size': '4000',
},
] as any);
elasticsearch.client.asInternalUser.count.mockResponseOnce({
count: '10',
} as any);
elasticsearch.client.asInternalUser.search.mockResolvedValueOnce({
body: {
hits: { total: { value: 6 } },
aggregations: {
aliases: {
buckets: {
active: { doc_count: 1 },
disabled: { doc_count: 2 },
},
elasticsearch.client.asInternalUser.search.mockResponseOnce({
hits: { total: { value: 6 } },
aggregations: {
aliases: {
buckets: {
active: { doc_count: 1 },
disabled: { doc_count: 2 },
},
},
},
Expand Down
8 changes: 4 additions & 4 deletions src/core/server/core_usage_data/core_usage_data_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,12 @@ export class CoreUsageDataService
// to map back from the index to the alias. So we have to make an API
// call for every alias. The document count is the lucene document count.
const catIndicesResults = await elasticsearch.client.asInternalUser.cat
.indices<any[]>({
.indices({
index,
format: 'JSON',
bytes: 'b',
})
.then(({ body }) => {
.then((body) => {
const stats = body[0];

return {
Expand All @@ -160,7 +160,7 @@ export class CoreUsageDataService
.count({
index,
})
.then(({ body }) => {
.then((body) => {
return {
savedObjectsDocsCount: body.count ? body.count : 0,
};
Expand All @@ -182,7 +182,7 @@ export class CoreUsageDataService
private async getSavedObjectAliasUsageData(elasticsearch: ElasticsearchServiceStart) {
// Note: this agg can be changed to use `savedObjectsRepository.find` in the future after `filters` is supported.
// See src/core/server/saved_objects/service/lib/aggregations/aggs_types/bucket_aggs.ts for supported aggregations.
const { body: resp } = await elasticsearch.client.asInternalUser.search<
const resp = await elasticsearch.client.asInternalUser.search<
unknown,
{ aliases: UsageDataAggs }
>({
Expand Down
6 changes: 3 additions & 3 deletions src/core/server/elasticsearch/client/cluster_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Side Public License, v 1.
*/

import type { KibanaClient } from '@elastic/elasticsearch/lib/api/kibana';
import type { Client } from '@elastic/elasticsearch';
import { Logger } from '../../logging';
import { IAuthHeadersStorage, Headers, isKibanaRequest, isRealRequest } from '../../http';
import { ensureRawRequest, filterHeaders } from '../../http/router';
Expand Down Expand Up @@ -60,12 +60,12 @@ export interface ICustomClusterClient extends IClusterClient {
export class ClusterClient implements ICustomClusterClient {
private readonly config: ElasticsearchClientConfig;
private readonly authHeaders?: IAuthHeadersStorage;
private readonly rootScopedClient: KibanaClient;
private readonly rootScopedClient: Client;
private readonly getUnauthorizedErrorHandler: () => UnauthorizedErrorHandler | undefined;
private readonly getExecutionContext: () => string | undefined;
private isClosed = false;

public readonly asInternalUser: KibanaClient;
public readonly asInternalUser: Client;

constructor({
config,
Expand Down
5 changes: 2 additions & 3 deletions src/core/server/elasticsearch/client/configure_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
*/

import { Client, HttpConnection } from '@elastic/elasticsearch';
import type { KibanaClient } from '@elastic/elasticsearch/lib/api/kibana';
import { Logger } from '../../logging';
import { parseClientOptions, ElasticsearchClientConfig } from './client_config';
import { instrumentEsQueryAndDeprecationLogger } from './log_query_and_deprecation';
Expand All @@ -28,7 +27,7 @@ export const configureClient = (
scoped?: boolean;
getExecutionContext?: () => string | undefined;
}
): KibanaClient => {
): Client => {
const clientOptions = parseClientOptions(config, scoped);
const KibanaTransport = createTransport({ getExecutionContext });

Expand All @@ -40,5 +39,5 @@ export const configureClient = (

instrumentEsQueryAndDeprecationLogger({ logger, client, type });

return client as KibanaClient;
return client;
};
4 changes: 2 additions & 2 deletions src/core/server/elasticsearch/client/create_transport.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ describe('createTransport', () => {
});

describe('`meta` option', () => {
it('adds `meta: true` to the options when not provided', async () => {
it('does not adds `meta: true` to the options when not provided', async () => {
const transportClass = createTransportClass();
const transport = new transportClass(baseConstructorParams);
const requestOptions = { method: 'GET', path: '/' };
Expand All @@ -126,7 +126,7 @@ describe('createTransport', () => {
expect(transportRequestMock).toHaveBeenCalledTimes(1);
expect(transportRequestMock).toHaveBeenCalledWith(
expect.any(Object),
expect.objectContaining({
expect.not.objectContaining({
meta: true,
})
);
Expand Down
5 changes: 0 additions & 5 deletions src/core/server/elasticsearch/client/create_transport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,6 @@ export const createTransport = ({
// rewrites headers['x-opaque-id'] if it presents
opts.opaqueId = opaqueId;
}
// Enforce the client to return TransportResult.
// It's required for bwc with responses in 7.x version.
if (opts.meta === undefined) {
opts.meta = true;
}

// add stored headers to the options
opts.headers = {
Expand Down
Loading

0 comments on commit 6627bd8

Please sign in to comment.