Skip to content

Commit addc622

Browse files
authored
Fixed conflicts (#89631)
1 parent 8307336 commit addc622

File tree

12 files changed

+199
-164
lines changed

12 files changed

+199
-164
lines changed

x-pack/plugins/event_log/server/es/cluster_client_adapter.test.ts

Lines changed: 95 additions & 79 deletions
Large diffs are not rendered by default.

x-pack/plugins/event_log/server/es/cluster_client_adapter.ts

Lines changed: 47 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,18 @@
55
*/
66

77
import { Subject } from 'rxjs';
8-
import { bufferTime, filter, switchMap } from 'rxjs/operators';
8+
import { bufferTime, filter as rxFilter, switchMap } from 'rxjs/operators';
99
import { reject, isUndefined } from 'lodash';
10-
import { Client } from 'elasticsearch';
1110
import type { PublicMethodsOf } from '@kbn/utility-types';
12-
import { Logger, LegacyClusterClient } from 'src/core/server';
13-
import { ESSearchResponse } from '../../../../typings/elasticsearch';
11+
import { Logger, ElasticsearchClient } from 'src/core/server';
1412
import { EsContext } from '.';
1513
import { IEvent, IValidatedEvent, SAVED_OBJECT_REL_PRIMARY } from '../types';
1614
import { FindOptionsType } from '../event_log_client';
15+
import { esKuery } from '../../../../../src/plugins/data/server';
1716

1817
export const EVENT_BUFFER_TIME = 1000; // milliseconds
1918
export const EVENT_BUFFER_LENGTH = 100;
2019

21-
export type EsClusterClient = Pick<LegacyClusterClient, 'callAsInternalUser' | 'asScoped'>;
2220
export type IClusterClientAdapter = PublicMethodsOf<ClusterClientAdapter>;
2321

2422
export interface Doc {
@@ -28,7 +26,7 @@ export interface Doc {
2826

2927
export interface ConstructorOpts {
3028
logger: Logger;
31-
clusterClientPromise: Promise<EsClusterClient>;
29+
elasticsearchClientPromise: Promise<ElasticsearchClient>;
3230
context: EsContext;
3331
}
3432

@@ -41,14 +39,14 @@ export interface QueryEventsBySavedObjectResult {
4139

4240
export class ClusterClientAdapter {
4341
private readonly logger: Logger;
44-
private readonly clusterClientPromise: Promise<EsClusterClient>;
42+
private readonly elasticsearchClientPromise: Promise<ElasticsearchClient>;
4543
private readonly docBuffer$: Subject<Doc>;
4644
private readonly context: EsContext;
4745
private readonly docsBufferedFlushed: Promise<void>;
4846

4947
constructor(opts: ConstructorOpts) {
5048
this.logger = opts.logger;
51-
this.clusterClientPromise = opts.clusterClientPromise;
49+
this.elasticsearchClientPromise = opts.elasticsearchClientPromise;
5250
this.context = opts.context;
5351
this.docBuffer$ = new Subject<Doc>();
5452

@@ -58,7 +56,7 @@ export class ClusterClientAdapter {
5856
this.docsBufferedFlushed = this.docBuffer$
5957
.pipe(
6058
bufferTime(EVENT_BUFFER_TIME, null, EVENT_BUFFER_LENGTH),
61-
filter((docs) => docs.length > 0),
59+
rxFilter((docs) => docs.length > 0),
6260
switchMap(async (docs) => await this.indexDocuments(docs))
6361
)
6462
.toPromise();
@@ -97,7 +95,8 @@ export class ClusterClientAdapter {
9795
}
9896

9997
try {
100-
await this.callEs<ReturnType<Client['bulk']>>('bulk', { body: bulkBody });
98+
const esClient = await this.elasticsearchClientPromise;
99+
await esClient.bulk({ body: bulkBody });
101100
} catch (err) {
102101
this.logger.error(
103102
`error writing bulk events: "${err.message}"; docs: ${JSON.stringify(bulkBody)}`
@@ -111,22 +110,24 @@ export class ClusterClientAdapter {
111110
path: `/_ilm/policy/${policyName}`,
112111
};
113112
try {
114-
await this.callEs('transport.request', request);
113+
const esClient = await this.elasticsearchClientPromise;
114+
await esClient.transport.request(request);
115115
} catch (err) {
116116
if (err.statusCode === 404) return false;
117117
throw new Error(`error checking existance of ilm policy: ${err.message}`);
118118
}
119119
return true;
120120
}
121121

122-
public async createIlmPolicy(policyName: string, policy: unknown): Promise<void> {
122+
public async createIlmPolicy(policyName: string, policy: Record<string, unknown>): Promise<void> {
123123
const request = {
124124
method: 'PUT',
125125
path: `/_ilm/policy/${policyName}`,
126126
body: policy,
127127
};
128128
try {
129-
await this.callEs('transport.request', request);
129+
const esClient = await this.elasticsearchClientPromise;
130+
await esClient.transport.request(request);
130131
} catch (err) {
131132
throw new Error(`error creating ilm policy: ${err.message}`);
132133
}
@@ -135,27 +136,18 @@ export class ClusterClientAdapter {
135136
public async doesIndexTemplateExist(name: string): Promise<boolean> {
136137
let result;
137138
try {
138-
result = await this.callEs<ReturnType<Client['indices']['existsTemplate']>>(
139-
'indices.existsTemplate',
140-
{ name }
141-
);
139+
const esClient = await this.elasticsearchClientPromise;
140+
result = (await esClient.indices.existsTemplate({ name })).body;
142141
} catch (err) {
143142
throw new Error(`error checking existance of index template: ${err.message}`);
144143
}
145144
return result as boolean;
146145
}
147146

148-
public async createIndexTemplate(name: string, template: unknown): Promise<void> {
149-
const addTemplateParams = {
150-
name,
151-
create: true,
152-
body: template,
153-
};
147+
public async createIndexTemplate(name: string, template: Record<string, unknown>): Promise<void> {
154148
try {
155-
await this.callEs<ReturnType<Client['indices']['putTemplate']>>(
156-
'indices.putTemplate',
157-
addTemplateParams
158-
);
149+
const esClient = await this.elasticsearchClientPromise;
150+
await esClient.indices.putTemplate({ name, body: template, create: true });
159151
} catch (err) {
160152
// The error message doesn't have a type attribute we can look to guarantee it's due
161153
// to the template already existing (only long message) so we'll check ourselves to see
@@ -171,19 +163,21 @@ export class ClusterClientAdapter {
171163
public async doesAliasExist(name: string): Promise<boolean> {
172164
let result;
173165
try {
174-
result = await this.callEs<ReturnType<Client['indices']['existsAlias']>>(
175-
'indices.existsAlias',
176-
{ name }
177-
);
166+
const esClient = await this.elasticsearchClientPromise;
167+
result = (await esClient.indices.existsAlias({ name })).body;
178168
} catch (err) {
179169
throw new Error(`error checking existance of initial index: ${err.message}`);
180170
}
181171
return result as boolean;
182172
}
183173

184-
public async createIndex(name: string, body: unknown = {}): Promise<void> {
174+
public async createIndex(
175+
name: string,
176+
body: string | Record<string, unknown> = {}
177+
): Promise<void> {
185178
try {
186-
await this.callEs<ReturnType<Client['indices']['create']>>('indices.create', {
179+
const esClient = await this.elasticsearchClientPromise;
180+
await esClient.indices.create({
187181
index: name,
188182
body,
189183
});
@@ -200,7 +194,7 @@ export class ClusterClientAdapter {
200194
type: string,
201195
ids: string[],
202196
// eslint-disable-next-line @typescript-eslint/naming-convention
203-
{ page, per_page: perPage, start, end, sort_field, sort_order }: FindOptionsType
197+
{ page, per_page: perPage, start, end, sort_field, sort_order, filter }: FindOptionsType
204198
): Promise<QueryEventsBySavedObjectResult> {
205199
const defaultNamespaceQuery = {
206200
bool: {
@@ -220,12 +214,26 @@ export class ClusterClientAdapter {
220214
};
221215
const namespaceQuery = namespace === undefined ? defaultNamespaceQuery : namedNamespaceQuery;
222216

217+
const esClient = await this.elasticsearchClientPromise;
218+
let dslFilterQuery;
219+
try {
220+
dslFilterQuery = filter
221+
? esKuery.toElasticsearchQuery(esKuery.fromKueryExpression(filter))
222+
: [];
223+
} catch (err) {
224+
this.debug(`Invalid kuery syntax for the filter (${filter}) error:`, {
225+
message: err.message,
226+
statusCode: err.statusCode,
227+
});
228+
throw err;
229+
}
223230
const body = {
224231
size: perPage,
225232
from: (page - 1) * perPage,
226233
sort: { [sort_field]: { order: sort_order } },
227234
query: {
228235
bool: {
236+
filter: dslFilterQuery,
229237
must: reject(
230238
[
231239
{
@@ -283,8 +291,10 @@ export class ClusterClientAdapter {
283291

284292
try {
285293
const {
286-
hits: { hits, total },
287-
}: ESSearchResponse<unknown, {}> = await this.callEs('search', {
294+
body: {
295+
hits: { hits, total },
296+
},
297+
} = await esClient.search({
288298
index,
289299
track_total_hits: true,
290300
body,
@@ -293,7 +303,7 @@ export class ClusterClientAdapter {
293303
page,
294304
per_page: perPage,
295305
total: total.value,
296-
data: hits.map((hit) => hit._source) as IValidatedEvent[],
306+
data: hits.map((hit: { _source: unknown }) => hit._source) as IValidatedEvent[],
297307
};
298308
} catch (err) {
299309
throw new Error(
@@ -302,24 +312,6 @@ export class ClusterClientAdapter {
302312
}
303313
}
304314

305-
// We have a common problem typing ES-DSL Queries
306-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
307-
private async callEs<ESQueryResult = unknown>(operation: string, body?: any) {
308-
try {
309-
this.debug(`callEs(${operation}) calls:`, body);
310-
const clusterClient = await this.clusterClientPromise;
311-
const result = await clusterClient.callAsInternalUser(operation, body);
312-
this.debug(`callEs(${operation}) result:`, result);
313-
return result as ESQueryResult;
314-
} catch (err) {
315-
this.debug(`callEs(${operation}) error:`, {
316-
message: err.message,
317-
statusCode: err.statusCode,
318-
});
319-
throw err;
320-
}
321-
}
322-
323315
private debug(message: string, object?: unknown) {
324316
const objectString = object == null ? '' : JSON.stringify(object);
325317
this.logger.debug(`esContext: ${message} ${objectString}`);

x-pack/plugins/event_log/server/es/context.test.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,27 +5,28 @@
55
*/
66

77
import { createEsContext } from './context';
8-
import { LegacyClusterClient, Logger } from '../../../../../src/core/server';
8+
import { ElasticsearchClient, Logger } from '../../../../../src/core/server';
99
import { elasticsearchServiceMock, loggingSystemMock } from '../../../../../src/core/server/mocks';
10+
import { DeeplyMockedKeys } from '@kbn/utility-types/jest';
11+
import { RequestEvent } from '@elastic/elasticsearch';
1012
jest.mock('../lib/../../../../package.json', () => ({ version: '1.2.3' }));
1113
jest.mock('./init');
12-
type EsClusterClient = Pick<jest.Mocked<LegacyClusterClient>, 'callAsInternalUser' | 'asScoped'>;
1314

1415
let logger: Logger;
15-
let clusterClient: EsClusterClient;
16+
let elasticsearchClient: DeeplyMockedKeys<ElasticsearchClient>;
1617

1718
beforeEach(() => {
1819
logger = loggingSystemMock.createLogger();
19-
clusterClient = elasticsearchServiceMock.createLegacyClusterClient();
20+
elasticsearchClient = elasticsearchServiceMock.createClusterClient().asInternalUser;
2021
});
2122

2223
describe('createEsContext', () => {
2324
test('should return is ready state as falsy if not initialized', () => {
2425
const context = createEsContext({
2526
logger,
26-
clusterClientPromise: Promise.resolve(clusterClient),
2727
indexNameRoot: 'test0',
2828
kibanaVersion: '1.2.3',
29+
elasticsearchClientPromise: Promise.resolve(elasticsearchClient),
2930
});
3031

3132
expect(context.initialized).toBeFalsy();
@@ -37,9 +38,9 @@ describe('createEsContext', () => {
3738
test('should return esNames', () => {
3839
const context = createEsContext({
3940
logger,
40-
clusterClientPromise: Promise.resolve(clusterClient),
4141
indexNameRoot: 'test-index',
4242
kibanaVersion: '1.2.3',
43+
elasticsearchClientPromise: Promise.resolve(elasticsearchClient),
4344
});
4445

4546
const esNames = context.esNames;
@@ -57,12 +58,12 @@ describe('createEsContext', () => {
5758
test('should return exist false for esAdapter ilm policy, index template and alias before initialize', async () => {
5859
const context = createEsContext({
5960
logger,
60-
clusterClientPromise: Promise.resolve(clusterClient),
6161
indexNameRoot: 'test1',
6262
kibanaVersion: '1.2.3',
63+
elasticsearchClientPromise: Promise.resolve(elasticsearchClient),
6364
});
64-
(clusterClient.callAsInternalUser as jest.Mock).mockResolvedValue(false);
65-
65+
elasticsearchClient.indices.existsTemplate.mockResolvedValue(asApiResponse(false));
66+
elasticsearchClient.indices.existsAlias.mockResolvedValue(asApiResponse(false));
6667
const doesAliasExist = await context.esAdapter.doesAliasExist(context.esNames.alias);
6768
expect(doesAliasExist).toBeFalsy();
6869

@@ -75,11 +76,11 @@ describe('createEsContext', () => {
7576
test('should return exist true for esAdapter ilm policy, index template and alias after initialize', async () => {
7677
const context = createEsContext({
7778
logger,
78-
clusterClientPromise: Promise.resolve(clusterClient),
7979
indexNameRoot: 'test2',
8080
kibanaVersion: '1.2.3',
81+
elasticsearchClientPromise: Promise.resolve(elasticsearchClient),
8182
});
82-
(clusterClient.callAsInternalUser as jest.Mock).mockResolvedValue(true);
83+
elasticsearchClient.indices.existsTemplate.mockResolvedValue(asApiResponse(true));
8384
context.initialize();
8485

8586
const doesIlmPolicyExist = await context.esAdapter.doesIlmPolicyExist(
@@ -100,12 +101,18 @@ describe('createEsContext', () => {
100101
jest.requireMock('./init').initializeEs.mockResolvedValue(false);
101102
const context = createEsContext({
102103
logger,
103-
clusterClientPromise: Promise.resolve(clusterClient),
104104
indexNameRoot: 'test2',
105105
kibanaVersion: '1.2.3',
106+
elasticsearchClientPromise: Promise.resolve(elasticsearchClient),
106107
});
107108
context.initialize();
108109
const success = await context.waitTillReady();
109110
expect(success).toBe(false);
110111
});
111112
});
113+
114+
function asApiResponse<T>(body: T): RequestEvent<T> {
115+
return {
116+
body,
117+
} as RequestEvent<T>;
118+
}

x-pack/plugins/event_log/server/es/context.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
import { Logger, LegacyClusterClient } from 'src/core/server';
7+
import { Logger, ElasticsearchClient } from 'src/core/server';
88

99
import { EsNames, getEsNames } from './names';
1010
import { initializeEs } from './init';
1111
import { ClusterClientAdapter, IClusterClientAdapter } from './cluster_client_adapter';
1212
import { createReadySignal, ReadySignal } from '../lib/ready_signal';
1313

14-
export type EsClusterClient = Pick<LegacyClusterClient, 'callAsInternalUser' | 'asScoped'>;
15-
1614
export interface EsContext {
1715
logger: Logger;
1816
esNames: EsNames;
@@ -34,9 +32,9 @@ export function createEsContext(params: EsContextCtorParams): EsContext {
3432

3533
export interface EsContextCtorParams {
3634
logger: Logger;
37-
clusterClientPromise: Promise<EsClusterClient>;
3835
indexNameRoot: string;
3936
kibanaVersion: string;
37+
elasticsearchClientPromise: Promise<ElasticsearchClient>;
4038
}
4139

4240
class EsContextImpl implements EsContext {
@@ -53,7 +51,7 @@ class EsContextImpl implements EsContext {
5351
this.initialized = false;
5452
this.esAdapter = new ClusterClientAdapter({
5553
logger: params.logger,
56-
clusterClientPromise: params.clusterClientPromise,
54+
elasticsearchClientPromise: params.elasticsearchClientPromise,
5755
context: this,
5856
});
5957
}

x-pack/plugins/event_log/server/es/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
* you may not use this file except in compliance with the Elastic License.
55
*/
66

7-
export { EsClusterClient, EsContext, createEsContext } from './context';
7+
export { EsContext, createEsContext } from './context';

0 commit comments

Comments
 (0)