-
Notifications
You must be signed in to change notification settings - Fork 8.2k
/
elasticsearch_service.ts
164 lines (142 loc) · 5.9 KB
/
elasticsearch_service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import { Observable, Subject } from 'rxjs';
import { first, map, shareReplay, takeUntil } from 'rxjs/operators';
import { merge } from '@kbn/std';
import { CoreService } from '../../types';
import { CoreContext } from '../core_context';
import { Logger } from '../logging';
import { ClusterClient, ElasticsearchClientConfig } from './client';
import { ElasticsearchConfig, ElasticsearchConfigType } from './elasticsearch_config';
import type { InternalHttpServiceSetup, GetAuthHeaders } from '../http';
import type { InternalExecutionContextSetup, IExecutionContext } from '../execution_context';
import {
InternalElasticsearchServicePreboot,
InternalElasticsearchServiceSetup,
InternalElasticsearchServiceStart,
} from './types';
import type { NodesVersionCompatibility } from './version_check/ensure_es_version';
import { pollEsNodesVersion } from './version_check/ensure_es_version';
import { calculateStatus$ } from './status';
import { isValidConnection } from './is_valid_connection';
import { isInlineScriptingEnabled } from './is_scripting_enabled';
export interface SetupDeps {
http: InternalHttpServiceSetup;
executionContext: InternalExecutionContextSetup;
}
/** @internal */
export class ElasticsearchService
implements CoreService<InternalElasticsearchServiceSetup, InternalElasticsearchServiceStart>
{
private readonly log: Logger;
private readonly config$: Observable<ElasticsearchConfig>;
private stop$ = new Subject();
private kibanaVersion: string;
private getAuthHeaders?: GetAuthHeaders;
private executionContextClient?: IExecutionContext;
private esNodesCompatibility$?: Observable<NodesVersionCompatibility>;
private client?: ClusterClient;
constructor(private readonly coreContext: CoreContext) {
this.kibanaVersion = coreContext.env.packageInfo.version;
this.log = coreContext.logger.get('elasticsearch-service');
this.config$ = coreContext.configService
.atPath<ElasticsearchConfigType>('elasticsearch')
.pipe(map((rawConfig) => new ElasticsearchConfig(rawConfig)));
}
public async preboot(): Promise<InternalElasticsearchServicePreboot> {
this.log.debug('Prebooting elasticsearch service');
const config = await this.config$.pipe(first()).toPromise();
return {
config: {
hosts: config.hosts,
credentialsSpecified:
config.username !== undefined ||
config.password !== undefined ||
config.serviceAccountToken !== undefined,
},
createClient: (type, clientConfig) => this.createClusterClient(type, config, clientConfig),
};
}
public async setup(deps: SetupDeps): Promise<InternalElasticsearchServiceSetup> {
this.log.debug('Setting up elasticsearch service');
const config = await this.config$.pipe(first()).toPromise();
this.getAuthHeaders = deps.http.getAuthHeaders;
this.executionContextClient = deps.executionContext;
this.client = this.createClusterClient('data', config);
const esNodesCompatibility$ = pollEsNodesVersion({
internalClient: this.client.asInternalUser,
log: this.log,
ignoreVersionMismatch: config.ignoreVersionMismatch,
esVersionCheckInterval: config.healthCheckDelay.asMilliseconds(),
kibanaVersion: this.kibanaVersion,
}).pipe(takeUntil(this.stop$), shareReplay({ refCount: true, bufferSize: 1 }));
this.esNodesCompatibility$ = esNodesCompatibility$;
return {
legacy: {
config$: this.config$,
},
esNodesCompatibility$,
status$: calculateStatus$(esNodesCompatibility$),
};
}
public async start(): Promise<InternalElasticsearchServiceStart> {
if (!this.client || !this.esNodesCompatibility$) {
throw new Error('ElasticsearchService needs to be setup before calling start');
}
const config = await this.config$.pipe(first()).toPromise();
// Log every error we may encounter in the connection to Elasticsearch
this.esNodesCompatibility$.subscribe(({ isCompatible, message }) => {
if (!isCompatible && message) {
this.log.error(message);
}
});
if (!config.skipStartupConnectionCheck) {
// Ensure that the connection is established and the product is valid before moving on
await isValidConnection(this.esNodesCompatibility$);
// Ensure inline scripting is enabled on the ES cluster
const scriptingEnabled = await isInlineScriptingEnabled({
client: this.client.asInternalUser,
});
if (!scriptingEnabled) {
throw new Error(
'Inline scripting is disabled on the Elasticsearch cluster, and is mandatory for Kibana to function. ' +
'Please enabled inline scripting, then restart Kibana. ' +
'Refer to https://www.elastic.co/guide/en/elasticsearch/reference/master/modules-scripting-security.html for more info.'
);
}
}
return {
client: this.client!,
createClient: (type, clientConfig) => this.createClusterClient(type, config, clientConfig),
legacy: {
config$: this.config$,
},
};
}
public async stop() {
this.log.debug('Stopping elasticsearch service');
this.stop$.next();
if (this.client) {
await this.client.close();
}
}
private createClusterClient(
type: string,
baseConfig: ElasticsearchConfig,
clientConfig?: Partial<ElasticsearchClientConfig>
) {
const config = clientConfig ? merge({}, baseConfig, clientConfig) : baseConfig;
return new ClusterClient(
config,
this.coreContext.logger.get('elasticsearch'),
type,
this.getAuthHeaders,
() => this.executionContextClient?.getAsHeader()
);
}
}