-
Notifications
You must be signed in to change notification settings - Fork 8.1k
/
saved_objects_service.ts
533 lines (490 loc) · 18.3 KB
/
saved_objects_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
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
/*
* 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 { Subject, Observable } from 'rxjs';
import { first, filter, take, switchMap } from 'rxjs/operators';
import { CoreService } from '../../types';
import {
SavedObjectsClient,
SavedObjectsClientProvider,
SavedObjectsClientProviderOptions,
} from './';
import { KibanaMigrator, IKibanaMigrator } from './migrations';
import { CoreContext } from '../core_context';
import { InternalCoreUsageDataSetup } from '../core_usage_data';
import {
ElasticsearchClient,
InternalElasticsearchServiceSetup,
InternalElasticsearchServiceStart,
} from '../elasticsearch';
import { InternalDeprecationsServiceSetup } from '../deprecations';
import {
SavedObjectsConfigType,
SavedObjectsMigrationConfigType,
SavedObjectConfig,
} from './saved_objects_config';
import { KibanaRequest, InternalHttpServiceSetup } from '../http';
import {
SavedObjectsClientContract,
SavedObjectsType,
SavedObjectStatusMeta,
SavedObjectAttributes,
} from './types';
import { ISavedObjectsRepository, SavedObjectsRepository } from './service/lib/repository';
import {
SavedObjectsClientFactoryProvider,
SavedObjectsClientWrapperFactory,
} from './service/lib/scoped_client_provider';
import { Logger } from '../logging';
import { SavedObjectTypeRegistry, ISavedObjectTypeRegistry } from './saved_objects_type_registry';
import { SavedObjectsSerializer } from './serialization';
import { SavedObjectsExporter, ISavedObjectsExporter } from './export';
import { SavedObjectsImporter, ISavedObjectsImporter } from './import';
import { registerRoutes } from './routes';
import { ServiceStatus } from '../status';
import { calculateStatus$ } from './status';
import { registerCoreObjectTypes } from './object_types';
import { getSavedObjectsDeprecationsProvider } from './deprecations';
const kibanaIndex = '.kibana';
/**
* Saved Objects is Kibana's data persistence mechanism allowing plugins to
* use Elasticsearch for storing and querying state. The SavedObjectsServiceSetup API exposes methods
* for registering Saved Object types, creating and registering Saved Object client wrappers and factories.
*
* @remarks
* When plugins access the Saved Objects client, a new client is created using
* the factory provided to `setClientFactory` and wrapped by all wrappers
* registered through `addClientWrapper`.
*
* @example
* ```ts
* import { SavedObjectsClient, CoreSetup } from 'src/core/server';
*
* export class Plugin() {
* setup: (core: CoreSetup) => {
* core.savedObjects.setClientFactory(({ request: KibanaRequest }) => {
* return new SavedObjectsClient(core.savedObjects.scopedRepository(request));
* })
* }
* }
* ```
*
* @example
* ```ts
* import { SavedObjectsClient, CoreSetup } from 'src/core/server';
* import { mySoType } from './saved_objects'
*
* export class Plugin() {
* setup: (core: CoreSetup) => {
* core.savedObjects.registerType(mySoType);
* }
* }
* ```
*
* @public
*/
export interface SavedObjectsServiceSetup {
/**
* Set the default {@link SavedObjectsClientFactoryProvider | factory provider} for creating Saved Objects clients.
* Only one provider can be set, subsequent calls to this method will fail.
*/
setClientFactoryProvider: (clientFactoryProvider: SavedObjectsClientFactoryProvider) => void;
/**
* Add a {@link SavedObjectsClientWrapperFactory | client wrapper factory} with the given priority.
*/
addClientWrapper: (
priority: number,
id: string,
factory: SavedObjectsClientWrapperFactory
) => void;
/**
* Register a {@link SavedObjectsType | savedObjects type} definition.
*
* See the {@link SavedObjectsTypeMappingDefinition | mappings format} and
* {@link SavedObjectMigrationMap | migration format} for more details about these.
*
* @example
* ```ts
* // src/plugins/my_plugin/server/saved_objects/my_type.ts
* import { SavedObjectsType } from 'src/core/server';
* import * as migrations from './migrations';
* import * as schemas from './schemas';
*
* export const myType: SavedObjectsType = {
* name: 'MyType',
* hidden: false,
* namespaceType: 'multiple',
* mappings: {
* properties: {
* textField: {
* type: 'text',
* },
* boolField: {
* type: 'boolean',
* },
* },
* },
* migrations: {
* '2.0.0': migrations.migrateToV2,
* '2.1.0': migrations.migrateToV2_1
* },
* schemas: {
* '2.0.0': schemas.v2,
* '2.1.0': schemas.v2_1,
* },
* };
*
* // src/plugins/my_plugin/server/plugin.ts
* import { SavedObjectsClient, CoreSetup } from 'src/core/server';
* import { myType } from './saved_objects';
*
* export class Plugin() {
* setup: (core: CoreSetup) => {
* core.savedObjects.registerType(myType);
* }
* }
* ```
*/
registerType: <Attributes extends SavedObjectAttributes = any>(
type: SavedObjectsType<Attributes>
) => void;
/**
* Returns the default index used for saved objects.
*/
getKibanaIndex: () => string;
}
/**
* @internal
*/
export interface InternalSavedObjectsServiceSetup extends SavedObjectsServiceSetup {
status$: Observable<ServiceStatus<SavedObjectStatusMeta>>;
/** Note: this must be called after server.setup to get all plugin SO types */
getTypeRegistry: () => ISavedObjectTypeRegistry;
}
/**
* Saved Objects is Kibana's data persisentence mechanism allowing plugins to
* use Elasticsearch for storing and querying state. The
* SavedObjectsServiceStart API provides a scoped Saved Objects client for
* interacting with Saved Objects.
*
* @public
*/
export interface SavedObjectsServiceStart {
/**
* Creates a {@link SavedObjectsClientContract | Saved Objects client} that
* uses the credentials from the passed in request to authenticate with
* Elasticsearch. If other plugins have registered Saved Objects client
* wrappers, these will be applied to extend the functionality of the client.
*
* A client that is already scoped to the incoming request is also exposed
* from the route handler context see {@link RequestHandlerContext}.
*/
getScopedClient: (
req: KibanaRequest,
options?: SavedObjectsClientProviderOptions
) => SavedObjectsClientContract;
/**
* Creates a {@link ISavedObjectsRepository | Saved Objects repository} that
* uses the credentials from the passed in request to authenticate with
* Elasticsearch.
*
* @param req - The request to create the scoped repository from.
* @param includedHiddenTypes - A list of additional hidden types the repository should have access to.
*
* @remarks
* Prefer using `getScopedClient`. This should only be used when using methods
* not exposed on {@link SavedObjectsClientContract}
*/
createScopedRepository: (
req: KibanaRequest,
includedHiddenTypes?: string[]
) => ISavedObjectsRepository;
/**
* Creates a {@link ISavedObjectsRepository | Saved Objects repository} that
* uses the internal Kibana user for authenticating with Elasticsearch.
*
* @param includedHiddenTypes - A list of additional hidden types the repository should have access to.
*/
createInternalRepository: (includedHiddenTypes?: string[]) => ISavedObjectsRepository;
/**
* Creates a {@link SavedObjectsSerializer | serializer} that is aware of all registered types.
*/
createSerializer: () => SavedObjectsSerializer;
/**
* Creates an {@link ISavedObjectsExporter | exporter} bound to given client.
*/
createExporter: (client: SavedObjectsClientContract) => ISavedObjectsExporter;
/**
* Creates an {@link ISavedObjectsImporter | importer} bound to given client.
*/
createImporter: (client: SavedObjectsClientContract) => ISavedObjectsImporter;
/**
* Returns the {@link ISavedObjectTypeRegistry | registry} containing all registered
* {@link SavedObjectsType | saved object types}
*/
getTypeRegistry: () => ISavedObjectTypeRegistry;
}
export type InternalSavedObjectsServiceStart = SavedObjectsServiceStart;
/**
* Factory provided when invoking a {@link SavedObjectsClientFactoryProvider | client factory provider}
* See {@link SavedObjectsServiceSetup.setClientFactoryProvider}
*
* @public
*/
export interface SavedObjectsRepositoryFactory {
/**
* Creates a {@link ISavedObjectsRepository | Saved Objects repository} that
* uses the credentials from the passed in request to authenticate with
* Elasticsearch.
*
* @param includedHiddenTypes - A list of additional hidden types the repository should have access to.
*/
createScopedRepository: (
req: KibanaRequest,
includedHiddenTypes?: string[]
) => ISavedObjectsRepository;
/**
* Creates a {@link ISavedObjectsRepository | Saved Objects repository} that
* uses the internal Kibana user for authenticating with Elasticsearch.
*
* @param includedHiddenTypes - A list of additional hidden types the repository should have access to.
*/
createInternalRepository: (includedHiddenTypes?: string[]) => ISavedObjectsRepository;
}
/** @internal */
export interface SavedObjectsSetupDeps {
http: InternalHttpServiceSetup;
elasticsearch: InternalElasticsearchServiceSetup;
coreUsageData: InternalCoreUsageDataSetup;
deprecations: InternalDeprecationsServiceSetup;
}
interface WrappedClientFactoryWrapper {
priority: number;
id: string;
factory: SavedObjectsClientWrapperFactory;
}
/** @internal */
export interface SavedObjectsStartDeps {
elasticsearch: InternalElasticsearchServiceStart;
pluginsInitialized?: boolean;
}
export class SavedObjectsService
implements CoreService<InternalSavedObjectsServiceSetup, InternalSavedObjectsServiceStart>
{
private logger: Logger;
private readonly kibanaVersion: string;
private setupDeps?: SavedObjectsSetupDeps;
private config?: SavedObjectConfig;
private clientFactoryProvider?: SavedObjectsClientFactoryProvider;
private clientFactoryWrappers: WrappedClientFactoryWrapper[] = [];
private migrator$ = new Subject<IKibanaMigrator>();
private typeRegistry = new SavedObjectTypeRegistry();
private started = false;
constructor(private readonly coreContext: CoreContext) {
this.logger = coreContext.logger.get('savedobjects-service');
this.kibanaVersion = SavedObjectsService.stripVersionQualifier(
this.coreContext.env.packageInfo.version
);
}
public async setup(setupDeps: SavedObjectsSetupDeps): Promise<InternalSavedObjectsServiceSetup> {
this.logger.debug('Setting up SavedObjects service');
this.setupDeps = setupDeps;
const { http, elasticsearch, coreUsageData, deprecations } = setupDeps;
const savedObjectsConfig = await this.coreContext.configService
.atPath<SavedObjectsConfigType>('savedObjects')
.pipe(first())
.toPromise();
const savedObjectsMigrationConfig = await this.coreContext.configService
.atPath<SavedObjectsMigrationConfigType>('migrations')
.pipe(first())
.toPromise();
this.config = new SavedObjectConfig(savedObjectsConfig, savedObjectsMigrationConfig);
deprecations.getRegistry('savedObjects').registerDeprecations(
getSavedObjectsDeprecationsProvider({
kibanaIndex,
savedObjectsConfig: this.config,
kibanaVersion: this.kibanaVersion,
typeRegistry: this.typeRegistry,
})
);
coreUsageData.registerType(this.typeRegistry);
registerRoutes({
http,
coreUsageData,
logger: this.logger,
config: this.config,
migratorPromise: this.migrator$.pipe(first()).toPromise(),
kibanaIndex,
kibanaVersion: this.kibanaVersion,
});
registerCoreObjectTypes(this.typeRegistry);
return {
status$: calculateStatus$(
this.migrator$.pipe(switchMap((migrator) => migrator.getStatus$())),
elasticsearch.status$
),
setClientFactoryProvider: (provider) => {
if (this.started) {
throw new Error('cannot call `setClientFactoryProvider` after service startup.');
}
if (this.clientFactoryProvider) {
throw new Error('custom client factory is already set, and can only be set once');
}
this.clientFactoryProvider = provider;
},
addClientWrapper: (priority, id, factory) => {
if (this.started) {
throw new Error('cannot call `addClientWrapper` after service startup.');
}
this.clientFactoryWrappers.push({
priority,
id,
factory,
});
},
registerType: (type) => {
if (this.started) {
throw new Error('cannot call `registerType` after service startup.');
}
this.typeRegistry.registerType(type);
},
getTypeRegistry: () => this.typeRegistry,
getKibanaIndex: () => kibanaIndex,
};
}
public async start({
elasticsearch,
pluginsInitialized = true,
}: SavedObjectsStartDeps): Promise<InternalSavedObjectsServiceStart> {
if (!this.setupDeps || !this.config) {
throw new Error('#setup() needs to be run first');
}
this.logger.debug('Starting SavedObjects service');
const client = elasticsearch.client;
const migrator = this.createMigrator(
this.config.migration,
elasticsearch.client.asInternalUser
);
this.migrator$.next(migrator);
/**
* Note: We want to ensure that migrations have completed before
* continuing with further Core start steps that might use SavedObjects
* such as running the legacy server, legacy plugins and allowing incoming
* HTTP requests.
*
* However, our build system optimize step and some tests depend on the
* HTTP server running without an Elasticsearch server being available.
* So, when the `migrations.skip` is true, we skip migrations altogether.
*
* We also cannot safely run migrations if plugins are not initialized since
* not plugin migrations won't be registered.
*/
const skipMigrations = this.config.migration.skip || !pluginsInitialized;
/**
* Note: Prepares all migrations maps. If a saved object type was registered with property `migrations`
* of type function; this function will be called to get the type's SavedObjectMigrationMap.
*/
migrator.prepareMigrations();
if (skipMigrations) {
this.logger.warn(
'Skipping Saved Object migrations on startup. Note: Individual documents will still be migrated when read or written.'
);
} else {
this.logger.info(
'Waiting until all Elasticsearch nodes are compatible with Kibana before starting saved objects migrations...'
);
// The Elasticsearch service should already ensure that, but let's double check just in case.
// Should it be replaced with elasticsearch.status$ API instead?
const compatibleNodes = await this.setupDeps!.elasticsearch.esNodesCompatibility$.pipe(
filter((nodes) => nodes.isCompatible),
take(1)
).toPromise();
// Running migrations only if we got compatible nodes.
// It may happen that the observable completes due to Kibana shutting down
// and the promise above fulfils as undefined. We shouldn't trigger migrations at that point.
if (compatibleNodes) {
this.logger.info('Starting saved objects migrations');
await migrator.runMigrations();
}
}
const createRepository = (
esClient: ElasticsearchClient,
includedHiddenTypes: string[] = []
) => {
return SavedObjectsRepository.createRepository(
migrator,
this.typeRegistry,
kibanaIndex,
esClient,
this.logger.get('repository'),
includedHiddenTypes
);
};
const repositoryFactory: SavedObjectsRepositoryFactory = {
createInternalRepository: (includedHiddenTypes?: string[]) =>
createRepository(client.asInternalUser, includedHiddenTypes),
createScopedRepository: (req: KibanaRequest, includedHiddenTypes?: string[]) =>
createRepository(client.asScoped(req).asCurrentUser, includedHiddenTypes),
};
const clientProvider = new SavedObjectsClientProvider({
defaultClientFactory({ request, includedHiddenTypes }) {
const repository = repositoryFactory.createScopedRepository(request, includedHiddenTypes);
return new SavedObjectsClient(repository);
},
typeRegistry: this.typeRegistry,
});
if (this.clientFactoryProvider) {
const clientFactory = this.clientFactoryProvider(repositoryFactory);
clientProvider.setClientFactory(clientFactory);
}
this.clientFactoryWrappers.forEach(({ id, factory, priority }) => {
clientProvider.addClientWrapperFactory(priority, id, factory);
});
this.started = true;
return {
getScopedClient: clientProvider.getClient.bind(clientProvider),
createScopedRepository: repositoryFactory.createScopedRepository,
createInternalRepository: repositoryFactory.createInternalRepository,
createSerializer: () => new SavedObjectsSerializer(this.typeRegistry),
createExporter: (savedObjectsClient) =>
new SavedObjectsExporter({
savedObjectsClient,
typeRegistry: this.typeRegistry,
exportSizeLimit: this.config!.maxImportExportSize,
logger: this.logger.get('exporter'),
}),
createImporter: (savedObjectsClient) =>
new SavedObjectsImporter({
savedObjectsClient,
typeRegistry: this.typeRegistry,
importSizeLimit: this.config!.maxImportExportSize,
}),
getTypeRegistry: () => this.typeRegistry,
};
}
public async stop() {}
private createMigrator(
soMigrationsConfig: SavedObjectsMigrationConfigType,
client: ElasticsearchClient
): IKibanaMigrator {
return new KibanaMigrator({
typeRegistry: this.typeRegistry,
logger: this.logger,
kibanaVersion: this.kibanaVersion,
soMigrationsConfig,
kibanaIndex,
client,
});
}
/**
* Coerce a semver-like string (x.y.z-SNAPSHOT) or prerelease version (x.y.z-alpha)
* to regular semver (x.y.z).
*/
private static stripVersionQualifier(version: string) {
return version.split('-')[0];
}
}