-
Notifications
You must be signed in to change notification settings - Fork 917
/
server.ts
393 lines (344 loc) · 14.1 KB
/
server.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*
* Any modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/
/*
* 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 apm from 'elastic-apm-node';
import { config as pathConfig } from '@osd/utils';
import { mapToObject } from '@osd/std';
import { ConfigService, Env, RawConfigurationProvider, coreDeprecationProvider } from './config';
import { CoreApp } from './core_app';
import { AuditTrailService } from './audit_trail';
import { OpenSearchService } from './opensearch';
import { HttpService } from './http';
import { HttpResourcesService } from './http_resources';
import { RenderingService } from './rendering';
import { LegacyService, ensureValidConfiguration } from './legacy';
import { Logger, LoggerFactory, LoggingService, ILoggingSystem } from './logging';
import { UiSettingsService } from './ui_settings';
import {
CompatibleEnginePluginVersions,
PluginName,
PluginsService,
config as pluginsConfig,
} from './plugins';
import { SavedObjectsService } from '../server/saved_objects';
import { MetricsService, opsConfig } from './metrics';
import { CapabilitiesService } from './capabilities';
import { EnvironmentService, config as pidConfig } from './environment';
import { StatusService } from './status/status_service';
import { SecurityService } from './security/security_service';
import { CrossCompatibilityService } from './cross_compatibility';
import { config as cspConfig } from './csp';
import { config as opensearchConfig } from './opensearch';
import { config as httpConfig } from './http';
import { config as loggingConfig } from './logging';
import { config as devConfig } from './dev';
import { config as opensearchDashboardsConfig } from './opensearch_dashboards_config';
import { savedObjectsConfig, savedObjectsMigrationConfig } from './saved_objects';
import { config as uiSettingsConfig } from './ui_settings';
import { config as statusConfig } from './status';
import { config as dynamicConfigServiceConfig } from './config';
import { ContextService } from './context';
import { RequestHandlerContext } from '.';
import { InternalCoreSetup, InternalCoreStart, ServiceConfigDescriptor } from './internal_types';
import { CoreUsageDataService } from './core_usage_data';
import { CoreRouteHandlerContext } from './core_route_handler_context';
import { DynamicConfigService } from './config/dynamic_config_service';
import { WorkspaceService } from './workspace/workspace_service';
const coreId = Symbol('core');
const rootConfigPath = '';
export class Server {
public readonly configService: ConfigService;
public readonly dynamicConfigService: DynamicConfigService;
private readonly capabilities: CapabilitiesService;
private readonly context: ContextService;
private readonly opensearch: OpenSearchService;
private readonly http: HttpService;
private readonly rendering: RenderingService;
private readonly legacy: LegacyService;
private readonly log: Logger;
private readonly plugins: PluginsService;
private readonly savedObjects: SavedObjectsService;
private readonly uiSettings: UiSettingsService;
private readonly workspace: WorkspaceService;
private readonly environment: EnvironmentService;
private readonly metrics: MetricsService;
private readonly httpResources: HttpResourcesService;
private readonly status: StatusService;
private readonly logging: LoggingService;
private readonly coreApp: CoreApp;
private readonly auditTrail: AuditTrailService;
private readonly coreUsageData: CoreUsageDataService;
private readonly security: SecurityService;
private readonly crossCompatibility: CrossCompatibilityService;
#pluginsInitialized?: boolean;
private coreStart?: InternalCoreStart;
private readonly logger: LoggerFactory;
private openSearchPluginDeps: Map<PluginName, CompatibleEnginePluginVersions> = new Map();
constructor(
rawConfigProvider: RawConfigurationProvider,
public readonly env: Env,
private readonly loggingSystem: ILoggingSystem
) {
this.logger = this.loggingSystem.asLoggerFactory();
this.log = this.logger.get('server');
this.configService = new ConfigService(rawConfigProvider, env, this.logger);
this.dynamicConfigService = new DynamicConfigService(this.configService, env, this.logger);
const core = {
coreId,
configService: this.configService,
dynamicConfigService: this.dynamicConfigService,
env,
logger: this.logger,
};
this.context = new ContextService(core);
this.http = new HttpService(core);
this.rendering = new RenderingService(core);
this.plugins = new PluginsService(core);
this.legacy = new LegacyService(core);
this.opensearch = new OpenSearchService(core);
this.savedObjects = new SavedObjectsService(core);
this.uiSettings = new UiSettingsService(core);
this.workspace = new WorkspaceService(core);
this.capabilities = new CapabilitiesService(core);
this.environment = new EnvironmentService(core);
this.metrics = new MetricsService(core);
this.status = new StatusService(core);
this.coreApp = new CoreApp(core);
this.httpResources = new HttpResourcesService(core);
this.auditTrail = new AuditTrailService(core);
this.logging = new LoggingService(core);
this.coreUsageData = new CoreUsageDataService(core);
this.security = new SecurityService(core);
this.crossCompatibility = new CrossCompatibilityService(core);
}
public async setup() {
this.log.debug('setting up server');
const setupTransaction = apm.startTransaction('server_setup', 'opensearch_dashboards_platform');
const environmentSetup = await this.environment.setup();
// Discover any plugins before continuing. This allows other systems to utilize the plugin dependency graph.
const { pluginTree, uiPlugins, requiredEnginePlugins } = await this.plugins.discover({
environment: environmentSetup,
});
this.openSearchPluginDeps = requiredEnginePlugins;
const legacyConfigSetup = await this.legacy.setupLegacyConfig();
// Immediately terminate in case of invalid configuration
// This needs to be done after plugin discovery
await this.configService.validate();
await ensureValidConfiguration(this.configService, legacyConfigSetup);
// Once the configs have been validated, setup the dynamic config as schemas have also been verified
const dynamicConfigServiceSetup = await this.dynamicConfigService.setup();
const contextServiceSetup = this.context.setup({
// We inject a fake "legacy plugin" with dependencies on every plugin so that legacy plugins:
// 1) Can access context from any KP plugin
// 2) Can register context providers that will only be available to other legacy plugins and will not leak into
// New Platform plugins.
pluginDependencies: new Map([
...pluginTree.asOpaqueIds,
[this.legacy.legacyId, [...pluginTree.asOpaqueIds.keys()]],
]),
});
const auditTrailSetup = this.auditTrail.setup();
const httpSetup = await this.http.setup({
context: contextServiceSetup,
});
// Once http is setup, register routes and async local storage
await this.dynamicConfigService.registerRoutesAndHandlers({ http: httpSetup });
const capabilitiesSetup = this.capabilities.setup({ http: httpSetup });
const opensearchServiceSetup = await this.opensearch.setup({
http: httpSetup,
});
const savedObjectsSetup = await this.savedObjects.setup({
http: httpSetup,
opensearch: opensearchServiceSetup,
});
const uiSettingsSetup = await this.uiSettings.setup({
http: httpSetup,
savedObjects: savedObjectsSetup,
});
const workspaceSetup = await this.workspace.setup();
const metricsSetup = await this.metrics.setup({ http: httpSetup });
const statusSetup = await this.status.setup({
opensearch: opensearchServiceSetup,
pluginDependencies: pluginTree.asNames,
savedObjects: savedObjectsSetup,
environment: environmentSetup,
http: httpSetup,
metrics: metricsSetup,
});
const renderingSetup = await this.rendering.setup({
http: httpSetup,
status: statusSetup,
uiPlugins,
dynamicConfig: dynamicConfigServiceSetup,
});
const httpResourcesSetup = this.httpResources.setup({
http: httpSetup,
rendering: renderingSetup,
});
const loggingSetup = this.logging.setup({
loggingSystem: this.loggingSystem,
});
const securitySetup = this.security.setup();
this.coreUsageData.setup({ metrics: metricsSetup });
const coreSetup: InternalCoreSetup = {
capabilities: capabilitiesSetup,
context: contextServiceSetup,
opensearch: opensearchServiceSetup,
environment: environmentSetup,
http: httpSetup,
savedObjects: savedObjectsSetup,
status: statusSetup,
uiSettings: uiSettingsSetup,
rendering: renderingSetup,
httpResources: httpResourcesSetup,
auditTrail: auditTrailSetup,
logging: loggingSetup,
metrics: metricsSetup,
security: securitySetup,
dynamicConfig: dynamicConfigServiceSetup,
workspace: workspaceSetup,
};
const pluginsSetup = await this.plugins.setup(coreSetup);
this.#pluginsInitialized = pluginsSetup.initialized;
await this.legacy.setup({
core: { ...coreSetup, plugins: pluginsSetup, rendering: renderingSetup },
plugins: mapToObject(pluginsSetup.contracts),
uiPlugins,
});
this.registerCoreContext(coreSetup);
this.coreApp.setup(coreSetup);
setupTransaction?.end();
return coreSetup;
}
public async start() {
this.log.debug('starting server');
const startTransaction = apm.startTransaction('server_start', 'opensearch_dashboards_platform');
const auditTrailStart = this.auditTrail.start();
const opensearchStart = await this.opensearch.start({
auditTrail: auditTrailStart,
});
const dynamicConfigServiceStart = await this.dynamicConfigService.start({
opensearch: opensearchStart,
});
const soStartSpan = startTransaction?.startSpan('saved_objects.migration', 'migration');
const savedObjectsStart = await this.savedObjects.start({
opensearch: opensearchStart,
pluginsInitialized: this.#pluginsInitialized,
});
soStartSpan?.end();
const capabilitiesStart = this.capabilities.start();
const uiSettingsStart = await this.uiSettings.start();
const workspaceStart = await this.workspace.start();
const metricsStart = await this.metrics.start();
const httpStart = this.http.getStartContract();
const coreUsageDataStart = this.coreUsageData.start({
opensearch: opensearchStart,
savedObjects: savedObjectsStart,
});
const crossCompatibilityServiceStart = this.crossCompatibility.start({
opensearch: opensearchStart,
plugins: this.openSearchPluginDeps,
});
this.coreStart = {
capabilities: capabilitiesStart,
opensearch: opensearchStart,
http: httpStart,
metrics: metricsStart,
savedObjects: savedObjectsStart,
uiSettings: uiSettingsStart,
auditTrail: auditTrailStart,
coreUsageData: coreUsageDataStart,
crossCompatibility: crossCompatibilityServiceStart,
dynamicConfig: dynamicConfigServiceStart,
workspace: workspaceStart,
};
const pluginsStart = await this.plugins.start(this.coreStart);
await this.legacy.start({
core: {
...this.coreStart,
plugins: pluginsStart,
},
plugins: mapToObject(pluginsStart.contracts),
});
await this.http.start({ dynamicConfigService: dynamicConfigServiceStart });
await this.security.start();
startTransaction?.end();
return this.coreStart;
}
public async stop() {
this.log.debug('stopping server');
await this.legacy.stop();
await this.plugins.stop();
await this.savedObjects.stop();
await this.opensearch.stop();
await this.http.stop();
await this.uiSettings.stop();
await this.rendering.stop();
await this.metrics.stop();
await this.status.stop();
await this.logging.stop();
await this.auditTrail.stop();
await this.security.stop();
}
private registerCoreContext(coreSetup: InternalCoreSetup) {
coreSetup.http.registerRouteHandlerContext(
coreId,
'core',
async (context, req, res): Promise<RequestHandlerContext['core']> => {
return new CoreRouteHandlerContext(this.coreStart!, req);
}
);
}
public async setupCoreConfig() {
const configDescriptors: Array<ServiceConfigDescriptor<unknown>> = [
pathConfig,
cspConfig,
opensearchConfig,
loggingConfig,
httpConfig,
pluginsConfig,
devConfig,
opensearchDashboardsConfig,
savedObjectsConfig,
savedObjectsMigrationConfig,
uiSettingsConfig,
opsConfig,
statusConfig,
pidConfig,
dynamicConfigServiceConfig,
];
this.configService.addDeprecationProvider(rootConfigPath, coreDeprecationProvider);
for (const descriptor of configDescriptors) {
if (descriptor.deprecations) {
this.configService.addDeprecationProvider(descriptor.path, descriptor.deprecations);
}
await this.configService.setSchema(descriptor.path, descriptor.schema);
this.dynamicConfigService.setSchema(descriptor.path, descriptor.schema);
}
}
}