-
Notifications
You must be signed in to change notification settings - Fork 8k
/
legacy_service.ts
312 lines (279 loc) · 9.75 KB
/
legacy_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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { combineLatest, ConnectableObservable, EMPTY, Observable, Subscription } from 'rxjs';
import { first, map, publishReplay, tap } from 'rxjs/operators';
import { CoreService } from '../../types';
import { CoreSetup, CoreStart } from '../';
import { InternalCoreSetup, InternalCoreStart } from '../internal_types';
import { SavedObjectsLegacyUiExports } from '../types';
import { Config } from '../config';
import { CoreContext } from '../core_context';
import { DevConfig, DevConfigType } from '../dev';
import { BasePathProxyServer, HttpConfig, HttpConfigType } from '../http';
import { Logger } from '../logging';
import { PluginsServiceSetup, PluginsServiceStart } from '../plugins';
import { findLegacyPluginSpecs } from './plugins';
import { LegacyPluginSpec } from './plugins/find_legacy_plugin_specs';
interface LegacyKbnServer {
applyLoggingConfiguration: (settings: Readonly<Record<string, any>>) => void;
listen: () => Promise<void>;
ready: () => Promise<void>;
close: () => Promise<void>;
}
function getLegacyRawConfig(config: Config) {
const rawConfig = config.toRaw();
// Elasticsearch config is solely handled by the core and legacy platform
// shouldn't have direct access to it.
if (rawConfig.elasticsearch !== undefined) {
delete rawConfig.elasticsearch;
}
return rawConfig;
}
/**
* @public
* @deprecated
*/
export interface LegacyServiceSetupDeps {
core: InternalCoreSetup & {
plugins: PluginsServiceSetup;
};
plugins: Record<string, unknown>;
}
/**
* @public
* @deprecated
*/
export interface LegacyServiceStartDeps {
core: InternalCoreStart & {
plugins: PluginsServiceStart;
};
plugins: Record<string, unknown>;
}
/** @internal */
export interface LegacyServiceSetup {
pluginSpecs: LegacyPluginSpec[];
uiExports: SavedObjectsLegacyUiExports;
pluginExtendedConfig: Config;
}
/** @internal */
export class LegacyService implements CoreService<LegacyServiceSetup> {
/** Symbol to represent the legacy platform as a fake "plugin". Used by the ContextService */
public readonly legacyId = Symbol();
private readonly log: Logger;
private readonly devConfig$: Observable<DevConfig>;
private readonly httpConfig$: Observable<HttpConfig>;
private kbnServer?: LegacyKbnServer;
private configSubscription?: Subscription;
private setupDeps?: LegacyServiceSetupDeps;
private update$: ConnectableObservable<Config> | undefined;
private legacyRawConfig: Config | undefined;
private legacyPlugins:
| {
pluginSpecs: LegacyPluginSpec[];
disabledPluginSpecs: LegacyPluginSpec[];
uiExports: SavedObjectsLegacyUiExports;
}
| undefined;
private settings: Record<string, any> | undefined;
constructor(private readonly coreContext: CoreContext) {
this.log = coreContext.logger.get('legacy-service');
this.devConfig$ = coreContext.configService
.atPath<DevConfigType>('dev')
.pipe(map(rawConfig => new DevConfig(rawConfig)));
this.httpConfig$ = coreContext.configService
.atPath<HttpConfigType>('server')
.pipe(map(rawConfig => new HttpConfig(rawConfig, coreContext.env)));
}
public async setup(setupDeps: LegacyServiceSetupDeps) {
this.setupDeps = setupDeps;
this.update$ = this.coreContext.configService.getConfig$().pipe(
tap(config => {
if (this.kbnServer !== undefined) {
this.kbnServer.applyLoggingConfiguration(getLegacyRawConfig(config));
}
}),
tap({ error: err => this.log.error(err) }),
publishReplay(1)
) as ConnectableObservable<Config>;
this.configSubscription = this.update$.connect();
this.settings = await this.update$
.pipe(
first(),
map(config => getLegacyRawConfig(config))
)
.toPromise();
const {
pluginSpecs,
pluginExtendedConfig,
disabledPluginSpecs,
uiExports,
} = await findLegacyPluginSpecs(this.settings, this.coreContext.logger);
this.legacyPlugins = {
pluginSpecs,
disabledPluginSpecs,
uiExports,
};
this.legacyRawConfig = pluginExtendedConfig;
// check for unknown uiExport types
if (uiExports.unknown && uiExports.unknown.length > 0) {
throw new Error(
`Unknown uiExport types: ${uiExports.unknown
.map(({ pluginSpec, type }) => `${type} from ${pluginSpec.getId()}`)
.join(', ')}`
);
}
return {
pluginSpecs,
uiExports,
pluginExtendedConfig,
};
}
public async start(startDeps: LegacyServiceStartDeps) {
const { setupDeps } = this;
if (!setupDeps || !this.legacyRawConfig || !this.legacyPlugins || !this.settings) {
throw new Error('Legacy service is not setup yet.');
}
this.log.debug('starting legacy service');
// Receive initial config and create kbnServer/ClusterManager.
if (this.coreContext.env.isDevClusterMaster) {
await this.createClusterManager(this.legacyRawConfig);
} else {
this.kbnServer = await this.createKbnServer(
this.settings,
this.legacyRawConfig,
setupDeps,
startDeps,
this.legacyPlugins
);
}
}
public async stop() {
this.log.debug('stopping legacy service');
if (this.configSubscription !== undefined) {
this.configSubscription.unsubscribe();
this.configSubscription = undefined;
}
if (this.kbnServer !== undefined) {
await this.kbnServer.close();
this.kbnServer = undefined;
}
}
private async createClusterManager(config: Config) {
const basePathProxy$ = this.coreContext.env.cliArgs.basePath
? combineLatest(this.devConfig$, this.httpConfig$).pipe(
first(),
map(
([devConfig, httpConfig]) =>
new BasePathProxyServer(this.coreContext.logger.get('server'), httpConfig, devConfig)
)
)
: EMPTY;
require('../../../cli/cluster/cluster_manager').create(
this.coreContext.env.cliArgs,
config,
await basePathProxy$.toPromise()
);
}
private async createKbnServer(
settings: Record<string, any>,
config: Config,
setupDeps: LegacyServiceSetupDeps,
startDeps: LegacyServiceStartDeps,
legacyPlugins: {
pluginSpecs: LegacyPluginSpec[];
disabledPluginSpecs: LegacyPluginSpec[];
uiExports: SavedObjectsLegacyUiExports;
}
) {
const coreSetup: CoreSetup = {
context: setupDeps.core.context,
elasticsearch: {
adminClient$: setupDeps.core.elasticsearch.adminClient$,
dataClient$: setupDeps.core.elasticsearch.dataClient$,
createClient: setupDeps.core.elasticsearch.createClient,
},
http: {
createCookieSessionStorageFactory: setupDeps.core.http.createCookieSessionStorageFactory,
registerRouteHandlerContext: setupDeps.core.http.registerRouteHandlerContext.bind(
null,
this.legacyId
),
createRouter: () => setupDeps.core.http.createRouter('', this.legacyId),
registerOnPreAuth: setupDeps.core.http.registerOnPreAuth,
registerAuth: setupDeps.core.http.registerAuth,
registerOnPostAuth: setupDeps.core.http.registerOnPostAuth,
basePath: setupDeps.core.http.basePath,
isTlsEnabled: setupDeps.core.http.isTlsEnabled,
},
uiSettings: {
register: setupDeps.core.uiSettings.register,
},
};
const coreStart: CoreStart = {};
// eslint-disable-next-line @typescript-eslint/no-var-requires
const KbnServer = require('../../../legacy/server/kbn_server');
const kbnServer: LegacyKbnServer = new KbnServer(
settings,
config,
{
env: {
mode: this.coreContext.env.mode,
packageInfo: this.coreContext.env.packageInfo,
},
handledConfigPaths: await this.coreContext.configService.getUsedPaths(),
setupDeps: {
core: coreSetup,
plugins: setupDeps.plugins,
},
startDeps: {
core: coreStart,
plugins: startDeps.plugins,
},
__internals: {
hapiServer: setupDeps.core.http.server,
kibanaMigrator: startDeps.core.savedObjects.migrator,
uiPlugins: setupDeps.core.plugins.uiPlugins,
elasticsearch: setupDeps.core.elasticsearch,
uiSettings: setupDeps.core.uiSettings,
savedObjectsClientProvider: startDeps.core.savedObjects.clientProvider,
},
logger: this.coreContext.logger,
},
legacyPlugins
);
// The kbnWorkerType check is necessary to prevent the repl
// from being started multiple times in different processes.
// We only want one REPL.
if (this.coreContext.env.cliArgs.repl && process.env.kbnWorkerType === 'server') {
require('../../../cli/repl').startRepl(kbnServer);
}
const httpConfig = await this.httpConfig$.pipe(first()).toPromise();
if (httpConfig.autoListen) {
try {
await kbnServer.listen();
} catch (err) {
await kbnServer.close();
throw err;
}
} else {
await kbnServer.ready();
}
return kbnServer;
}
}