-
Notifications
You must be signed in to change notification settings - Fork 915
/
server.ts
336 lines (293 loc) · 11.7 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
/*
* 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 { 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 { 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 { 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';
const coreId = Symbol('core');
const rootConfigPath = '';
export class Server {
public readonly configService: ConfigService;
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 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;
#pluginsInitialized?: boolean;
private coreStart?: InternalCoreStart;
private readonly logger: LoggerFactory;
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);
const core = { coreId, configService: this.configService, 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.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);
}
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 } = await this.plugins.discover({
environment: environmentSetup,
});
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);
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,
});
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 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,
});
const httpResourcesSetup = this.httpResources.setup({
http: httpSetup,
rendering: renderingSetup,
});
const loggingSetup = this.logging.setup({
loggingSystem: this.loggingSystem,
});
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,
};
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 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 metricsStart = await this.metrics.start();
const httpStart = this.http.getStartContract();
const coreUsageDataStart = this.coreUsageData.start({
opensearch: opensearchStart,
savedObjects: savedObjectsStart,
});
this.coreStart = {
capabilities: capabilitiesStart,
opensearch: opensearchStart,
http: httpStart,
metrics: metricsStart,
savedObjects: savedObjectsStart,
uiSettings: uiSettingsStart,
auditTrail: auditTrailStart,
coreUsageData: coreUsageDataStart,
};
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();
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();
}
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,
];
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);
}
}
}