From ea7c78d10eaf2f80265eea0a564eda0607c58147 Mon Sep 17 00:00:00 2001 From: Pierre Gayvallet Date: Thu, 6 Feb 2020 10:30:55 +0100 Subject: [PATCH] Restructure SavedObject types internal representation (#56378) * adapt types and tests to prepare for new NP api * rename and export public types * update generated doc * first implementation of registerMappings * adapt es archiver to convert legacy mappings * update generated doc * fix more tests * add unit tests * add legacy-compat unit test * add documentation and examples * Introduce SavedObjectTypeRegistry and SavedObjectType types * add and fix tests * expose createSerializer API and fix usages * remove registerMappings API, add internal registerType * revert changes to migration guide * adapt ES-archiver migrator creation * export serializer-related types * update generated doc * add and use convertTypesToLegacySchema * remove / move to internal some mapping types * fix forEach closure context * add missing docs * fix core path * some nits * fix so_mixin tests * fix integration tests * fix integration tests for real * add documentation on serializer + restructure files and types * nit * add and use the ISavedObjectTypeRegistry interface * Add documentation, deprecates migrationLogger#warning * better typing for SavedObjectsRawDoc._source * nits * update generated doc * remove exposition of SavedObjectsTypeMappingDefinitions, update doc * creates so internal contracts mocks * improve documentation --- .../core/server/kibana-plugin-server.md | 10 + ...na-plugin-server.savedobjectmigrationfn.md | 13 + ...a-plugin-server.savedobjectmigrationmap.md | 27 ++ ...a-plugin-server.savedobjectsanitizeddoc.md | 12 + ...savedobjectscomplexfieldmapping.dynamic.md | 11 + ...-server.savedobjectscomplexfieldmapping.md | 22 ++ ...edobjectscomplexfieldmapping.properties.md | 11 + ...er.savedobjectscomplexfieldmapping.type.md | 11 + ...er.savedobjectscorefieldmapping.enabled.md | 11 + ...ver.savedobjectscorefieldmapping.fields.md | 15 + ...rver.savedobjectscorefieldmapping.index.md | 11 + ...gin-server.savedobjectscorefieldmapping.md | 23 ++ ...erver.savedobjectscorefieldmapping.type.md | 11 + ...-plugin-server.savedobjectsfieldmapping.md | 15 + ...in-server.savedobjectsmappingproperties.md | 13 + ...ugin-server.savedobjectsmigrationlogger.md | 1 + ...server.savedobjectsmigrationlogger.warn.md | 11 + ...ver.savedobjectsmigrationlogger.warning.md | 5 + ...lugin-server.savedobjectsrawdoc._source.md | 2 +- ...kibana-plugin-server.savedobjectsrawdoc.md | 4 +- ...er.savedobjectsserializer.generaterawid.md | 26 ++ ...savedobjectsserializer.israwsavedobject.md | 24 ++ ...na-plugin-server.savedobjectsserializer.md | 29 ++ ...savedobjectsserializer.rawtosavedobject.md | 24 ++ ...savedobjectsserializer.savedobjecttoraw.md | 24 ++ ...-plugin-server.savedobjectsservicesetup.md | 12 +- ...vedobjectsservicestart.createserializer.md | 13 + ...-plugin-server.savedobjectsservicestart.md | 1 + ...r.savedobjectstype.converttoaliasscript.md | 13 + ...a-plugin-server.savedobjectstype.hidden.md | 15 + ...in-server.savedobjectstype.indexpattern.md | 13 + ...plugin-server.savedobjectstype.mappings.md | 13 + .../kibana-plugin-server.savedobjectstype.md | 28 ++ ...ugin-server.savedobjectstype.migrations.md | 13 + ...ana-plugin-server.savedobjectstype.name.md | 13 + ...rver.savedobjectstype.namespaceagnostic.md | 13 + ...erver.savedobjectstypemappingdefinition.md | 45 +++ ...objectstypemappingdefinition.properties.md | 11 + src/core/server/index.ts | 10 + src/core/server/legacy/legacy_service.mock.ts | 17 +- src/core/server/legacy/legacy_service.test.ts | 4 +- src/core/server/legacy/legacy_service.ts | 2 + src/core/server/mocks.ts | 14 +- src/core/server/plugins/plugin_context.ts | 1 + .../__snapshots__/utils.test.ts.snap | 144 ++++++++ src/core/server/saved_objects/index.ts | 21 +- .../server/saved_objects/mappings/index.ts | 11 +- .../mappings/lib/get_property.test.ts | 4 +- .../mappings/lib/get_property.ts | 13 +- .../lib/get_root_properties_objects.ts | 10 +- .../server/saved_objects/mappings/types.ts | 142 ++++++-- .../core/build_active_mappings.test.ts | 10 +- .../migrations/core/build_active_mappings.ts | 36 +- .../migrations/core/build_index_map.test.ts | 70 ++-- .../migrations/core/build_index_map.ts | 27 +- .../migrations/core/document_migrator.test.ts | 311 +++++++++++------- .../migrations/core/document_migrator.ts | 140 ++++---- .../migrations/core/index_migrator.test.ts | 8 +- .../migrations/core/migrate_raw_docs.test.ts | 26 +- .../migrations/core/migrate_raw_docs.ts | 10 +- .../migrations/core/migration_context.ts | 9 +- .../core/migration_coordinator.test.ts | 1 + .../migrations/core/migration_logger.ts | 5 + .../server/saved_objects/migrations/index.ts | 1 + .../kibana_migrator.test.ts.snap | 16 +- .../migrations/kibana/kibana_migrator.mock.ts | 29 +- .../migrations/kibana/kibana_migrator.test.ts | 103 +++--- .../migrations/kibana/kibana_migrator.ts | 60 ++-- .../server/saved_objects/migrations/types.ts | 51 +++ .../saved_objects_service.mock.ts | 44 ++- .../saved_objects_service.test.mocks.ts | 6 + .../saved_objects_service.test.ts | 20 ++ .../saved_objects/saved_objects_service.ts | 59 ++-- ...ts => saved_objects_type_registry.mock.ts} | 28 +- .../saved_objects_type_registry.test.ts | 215 ++++++++++++ .../saved_objects_type_registry.ts | 86 +++++ .../server/saved_objects/schema/schema.ts | 17 +- .../saved_objects/serialization/index.ts | 166 +--------- ...rialization.test.ts => serializer.test.ts} | 205 ++++++------ .../saved_objects/serialization/serializer.ts | 151 +++++++++ .../saved_objects/serialization/types.ts | 79 +++++ .../service/lib/repository.test.js | 115 ++++++- .../saved_objects/service/lib/repository.ts | 55 ++-- .../lib/repository_create_repository.test.ts | 72 ++-- .../lib/search_dsl/query_params.test.ts | 61 ++-- .../service/lib/search_dsl/query_params.ts | 16 +- .../service/lib/search_dsl/search_dsl.test.ts | 22 +- .../service/lib/search_dsl/search_dsl.ts | 6 +- src/core/server/saved_objects/types.ts | 87 ++++- src/core/server/saved_objects/utils.test.ts | 285 ++++++++++++++++ src/core/server/saved_objects/utils.ts | 76 +++++ src/core/server/server.api.md | 108 +++++- src/es_archiver/lib/indices/kibana_index.ts | 16 +- .../migrations/migrations_730.test.ts | 1 + src/legacy/server/kbn_server.d.ts | 1 + .../saved_objects/saved_objects_mixin.js | 10 +- .../saved_objects/saved_objects_mixin.test.js | 54 ++- .../apis/saved_objects/migrations.js | 25 +- .../plugins/task_manager/server/index.ts | 4 +- .../server/create_task_manager.test.ts | 9 +- .../server/create_task_manager.ts | 10 +- x-pack/plugins/task_manager/server/plugin.ts | 7 +- .../task_manager/server/task_manager.test.ts | 4 +- .../task_manager/server/task_store.test.ts | 4 +- 104 files changed, 3055 insertions(+), 944 deletions(-) create mode 100644 docs/development/core/server/kibana-plugin-server.savedobjectmigrationfn.md create mode 100644 docs/development/core/server/kibana-plugin-server.savedobjectmigrationmap.md create mode 100644 docs/development/core/server/kibana-plugin-server.savedobjectsanitizeddoc.md create mode 100644 docs/development/core/server/kibana-plugin-server.savedobjectscomplexfieldmapping.dynamic.md create mode 100644 docs/development/core/server/kibana-plugin-server.savedobjectscomplexfieldmapping.md create mode 100644 docs/development/core/server/kibana-plugin-server.savedobjectscomplexfieldmapping.properties.md create mode 100644 docs/development/core/server/kibana-plugin-server.savedobjectscomplexfieldmapping.type.md create mode 100644 docs/development/core/server/kibana-plugin-server.savedobjectscorefieldmapping.enabled.md create mode 100644 docs/development/core/server/kibana-plugin-server.savedobjectscorefieldmapping.fields.md create mode 100644 docs/development/core/server/kibana-plugin-server.savedobjectscorefieldmapping.index.md create mode 100644 docs/development/core/server/kibana-plugin-server.savedobjectscorefieldmapping.md create mode 100644 docs/development/core/server/kibana-plugin-server.savedobjectscorefieldmapping.type.md create mode 100644 docs/development/core/server/kibana-plugin-server.savedobjectsfieldmapping.md create mode 100644 docs/development/core/server/kibana-plugin-server.savedobjectsmappingproperties.md create mode 100644 docs/development/core/server/kibana-plugin-server.savedobjectsmigrationlogger.warn.md create mode 100644 docs/development/core/server/kibana-plugin-server.savedobjectsserializer.generaterawid.md create mode 100644 docs/development/core/server/kibana-plugin-server.savedobjectsserializer.israwsavedobject.md create mode 100644 docs/development/core/server/kibana-plugin-server.savedobjectsserializer.md create mode 100644 docs/development/core/server/kibana-plugin-server.savedobjectsserializer.rawtosavedobject.md create mode 100644 docs/development/core/server/kibana-plugin-server.savedobjectsserializer.savedobjecttoraw.md create mode 100644 docs/development/core/server/kibana-plugin-server.savedobjectsservicestart.createserializer.md create mode 100644 docs/development/core/server/kibana-plugin-server.savedobjectstype.converttoaliasscript.md create mode 100644 docs/development/core/server/kibana-plugin-server.savedobjectstype.hidden.md create mode 100644 docs/development/core/server/kibana-plugin-server.savedobjectstype.indexpattern.md create mode 100644 docs/development/core/server/kibana-plugin-server.savedobjectstype.mappings.md create mode 100644 docs/development/core/server/kibana-plugin-server.savedobjectstype.md create mode 100644 docs/development/core/server/kibana-plugin-server.savedobjectstype.migrations.md create mode 100644 docs/development/core/server/kibana-plugin-server.savedobjectstype.name.md create mode 100644 docs/development/core/server/kibana-plugin-server.savedobjectstype.namespaceagnostic.md create mode 100644 docs/development/core/server/kibana-plugin-server.savedobjectstypemappingdefinition.md create mode 100644 docs/development/core/server/kibana-plugin-server.savedobjectstypemappingdefinition.properties.md create mode 100644 src/core/server/saved_objects/__snapshots__/utils.test.ts.snap create mode 100644 src/core/server/saved_objects/migrations/types.ts rename src/core/server/saved_objects/{schema/schema.mock.ts => saved_objects_type_registry.mock.ts} (58%) create mode 100644 src/core/server/saved_objects/saved_objects_type_registry.test.ts create mode 100644 src/core/server/saved_objects/saved_objects_type_registry.ts rename src/core/server/saved_objects/serialization/{serialization.test.ts => serializer.test.ts} (79%) create mode 100644 src/core/server/saved_objects/serialization/serializer.ts create mode 100644 src/core/server/saved_objects/serialization/types.ts create mode 100644 src/core/server/saved_objects/utils.test.ts create mode 100644 src/core/server/saved_objects/utils.ts diff --git a/docs/development/core/server/kibana-plugin-server.md b/docs/development/core/server/kibana-plugin-server.md index a3abeff44c25c..482f014b226b9 100644 --- a/docs/development/core/server/kibana-plugin-server.md +++ b/docs/development/core/server/kibana-plugin-server.md @@ -25,6 +25,7 @@ The plugin integrates with the core system via lifecycle events: `setup` | [SavedObjectsClient](./kibana-plugin-server.savedobjectsclient.md) | | | [SavedObjectsErrorHelpers](./kibana-plugin-server.savedobjectserrorhelpers.md) | | | [SavedObjectsRepository](./kibana-plugin-server.savedobjectsrepository.md) | | +| [SavedObjectsSerializer](./kibana-plugin-server.savedobjectsserializer.md) | A serializer that can be used to manually convert [raw](./kibana-plugin-server.savedobjectsrawdoc.md) or [sanitized](./kibana-plugin-server.savedobjectsanitizeddoc.md) documents to the other kind. | | [ScopedClusterClient](./kibana-plugin-server.scopedclusterclient.md) | Serves the same purpose as "normal" ClusterClient but exposes additional callAsCurrentUser method that doesn't use credentials of the Kibana internal user (as callAsInternalUser does) to request Elasticsearch API, but rather passes HTTP headers extracted from the current user request to the API.See [ScopedClusterClient](./kibana-plugin-server.scopedclusterclient.md). | ## Enumerations @@ -106,6 +107,7 @@ The plugin integrates with the core system via lifecycle events: `setup` | [RouteValidatorOptions](./kibana-plugin-server.routevalidatoroptions.md) | Additional options for the RouteValidator class to modify its default behaviour. | | [SavedObject](./kibana-plugin-server.savedobject.md) | | | [SavedObjectAttributes](./kibana-plugin-server.savedobjectattributes.md) | The data for a Saved Object is stored as an object in the attributes property. | +| [SavedObjectMigrationMap](./kibana-plugin-server.savedobjectmigrationmap.md) | A map of [migration functions](./kibana-plugin-server.savedobjectmigrationfn.md) to be used for a given type. The map's keys must be valid semver versions.For a given document, only migrations with a higher version number than that of the document will be applied. Migrations are executed in order, starting from the lowest version and ending with the highest one. | | [SavedObjectReference](./kibana-plugin-server.savedobjectreference.md) | A reference to another saved object. | | [SavedObjectsBaseOptions](./kibana-plugin-server.savedobjectsbaseoptions.md) | | | [SavedObjectsBulkCreateObject](./kibana-plugin-server.savedobjectsbulkcreateobject.md) | | @@ -116,6 +118,8 @@ The plugin integrates with the core system via lifecycle events: `setup` | [SavedObjectsBulkUpdateResponse](./kibana-plugin-server.savedobjectsbulkupdateresponse.md) | | | [SavedObjectsClientProviderOptions](./kibana-plugin-server.savedobjectsclientprovideroptions.md) | Options to control the creation of the Saved Objects Client. | | [SavedObjectsClientWrapperOptions](./kibana-plugin-server.savedobjectsclientwrapperoptions.md) | Options passed to each SavedObjectsClientWrapperFactory to aid in creating the wrapper instance. | +| [SavedObjectsComplexFieldMapping](./kibana-plugin-server.savedobjectscomplexfieldmapping.md) | See [SavedObjectsFieldMapping](./kibana-plugin-server.savedobjectsfieldmapping.md) for documentation. | +| [SavedObjectsCoreFieldMapping](./kibana-plugin-server.savedobjectscorefieldmapping.md) | See [SavedObjectsFieldMapping](./kibana-plugin-server.savedobjectsfieldmapping.md) for documentation. | | [SavedObjectsCreateOptions](./kibana-plugin-server.savedobjectscreateoptions.md) | | | [SavedObjectsDeleteByNamespaceOptions](./kibana-plugin-server.savedobjectsdeletebynamespaceoptions.md) | | | [SavedObjectsDeleteOptions](./kibana-plugin-server.savedobjectsdeleteoptions.md) | | @@ -132,6 +136,7 @@ The plugin integrates with the core system via lifecycle events: `setup` | [SavedObjectsImportUnknownError](./kibana-plugin-server.savedobjectsimportunknownerror.md) | Represents a failure to import due to an unknown reason. | | [SavedObjectsImportUnsupportedTypeError](./kibana-plugin-server.savedobjectsimportunsupportedtypeerror.md) | Represents a failure to import due to having an unsupported saved object type. | | [SavedObjectsIncrementCounterOptions](./kibana-plugin-server.savedobjectsincrementcounteroptions.md) | | +| [SavedObjectsMappingProperties](./kibana-plugin-server.savedobjectsmappingproperties.md) | Describe the fields of a [saved object type](./kibana-plugin-server.savedobjectstypemappingdefinition.md). | | [SavedObjectsMigrationLogger](./kibana-plugin-server.savedobjectsmigrationlogger.md) | | | [SavedObjectsMigrationVersion](./kibana-plugin-server.savedobjectsmigrationversion.md) | Information about the migrations that have been applied to this SavedObject. When Kibana starts up, KibanaMigrator detects outdated documents and migrates them based on this value. For each migration that has been applied, the plugin's name is used as a key and the latest migration version as the value. | | [SavedObjectsRawDoc](./kibana-plugin-server.savedobjectsrawdoc.md) | A raw document as represented directly in the saved object index. | @@ -139,6 +144,8 @@ The plugin integrates with the core system via lifecycle events: `setup` | [SavedObjectsResolveImportErrorsOptions](./kibana-plugin-server.savedobjectsresolveimporterrorsoptions.md) | Options to control the "resolve import" operation. | | [SavedObjectsServiceSetup](./kibana-plugin-server.savedobjectsservicesetup.md) | Saved Objects is Kibana's data persistence mechanism allowing plugins to use Elasticsearch for storing and querying state. The SavedObjectsServiceSetup API exposes methods for creating and registering Saved Object client wrappers. | | [SavedObjectsServiceStart](./kibana-plugin-server.savedobjectsservicestart.md) | 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. | +| [SavedObjectsType](./kibana-plugin-server.savedobjectstype.md) | | +| [SavedObjectsTypeMappingDefinition](./kibana-plugin-server.savedobjectstypemappingdefinition.md) | Describe a saved object type mapping. | | [SavedObjectsUpdateOptions](./kibana-plugin-server.savedobjectsupdateoptions.md) | | | [SavedObjectsUpdateResponse](./kibana-plugin-server.savedobjectsupdateresponse.md) | | | [SessionCookieValidationResult](./kibana-plugin-server.sessioncookievalidationresult.md) | Return type from a function to validate cookie contents. | @@ -218,10 +225,13 @@ The plugin integrates with the core system via lifecycle events: `setup` | [RouteValidatorFullConfig](./kibana-plugin-server.routevalidatorfullconfig.md) | Route validations config and options merged into one object | | [SavedObjectAttribute](./kibana-plugin-server.savedobjectattribute.md) | Type definition for a Saved Object attribute value | | [SavedObjectAttributeSingle](./kibana-plugin-server.savedobjectattributesingle.md) | Don't use this type, it's simply a helper type for [SavedObjectAttribute](./kibana-plugin-server.savedobjectattribute.md) | +| [SavedObjectMigrationFn](./kibana-plugin-server.savedobjectmigrationfn.md) | A migration function defined for a [saved objects type](./kibana-plugin-server.savedobjectstype.md) used to migrate it's | +| [SavedObjectSanitizedDoc](./kibana-plugin-server.savedobjectsanitizeddoc.md) | | | [SavedObjectsClientContract](./kibana-plugin-server.savedobjectsclientcontract.md) | Saved Objects is Kibana's data persisentence mechanism allowing plugins to use Elasticsearch for storing plugin state.\#\# SavedObjectsClient errorsSince the SavedObjectsClient has its hands in everything we are a little paranoid about the way we present errors back to to application code. Ideally, all errors will be either:1. Caused by bad implementation (ie. undefined is not a function) and as such unpredictable 2. An error that has been classified and decorated appropriately by the decorators in [SavedObjectsErrorHelpers](./kibana-plugin-server.savedobjectserrorhelpers.md)Type 1 errors are inevitable, but since all expected/handle-able errors should be Type 2 the isXYZError() helpers exposed at SavedObjectsErrorHelpers should be used to understand and manage error responses from the SavedObjectsClient.Type 2 errors are decorated versions of the source error, so if the elasticsearch client threw an error it will be decorated based on its type. That means that rather than looking for error.body.error.type or doing substring checks on error.body.error.reason, just use the helpers to understand the meaning of the error:\`\`\`js if (SavedObjectsErrorHelpers.isNotFoundError(error)) { // handle 404 }if (SavedObjectsErrorHelpers.isNotAuthorizedError(error)) { // 401 handling should be automatic, but in case you wanted to know }// always rethrow the error unless you handle it throw error; \`\`\`\#\#\# 404s from missing indexFrom the perspective of application code and APIs the SavedObjectsClient is a black box that persists objects. One of the internal details that users have no control over is that we use an elasticsearch index for persistance and that index might be missing.At the time of writing we are in the process of transitioning away from the operating assumption that the SavedObjects index is always available. Part of this transition is handling errors resulting from an index missing. These used to trigger a 500 error in most cases, and in others cause 404s with different error messages.From my (Spencer) perspective, a 404 from the SavedObjectsApi is a 404; The object the request/call was targeting could not be found. This is why \#14141 takes special care to ensure that 404 errors are generic and don't distinguish between index missing or document missing.\#\#\# 503s from missing indexUnlike all other methods, create requests are supposed to succeed even when the Kibana index does not exist because it will be automatically created by elasticsearch. When that is not the case it is because Elasticsearch's action.auto_create_index setting prevents it from being created automatically so we throw a special 503 with the intention of informing the user that their Elasticsearch settings need to be updated.See [SavedObjectsClient](./kibana-plugin-server.savedobjectsclient.md) See [SavedObjectsErrorHelpers](./kibana-plugin-server.savedobjectserrorhelpers.md) | | [SavedObjectsClientFactory](./kibana-plugin-server.savedobjectsclientfactory.md) | Describes the factory used to create instances of the Saved Objects Client. | | [SavedObjectsClientFactoryProvider](./kibana-plugin-server.savedobjectsclientfactoryprovider.md) | Provider to invoke to retrieve a [SavedObjectsClientFactory](./kibana-plugin-server.savedobjectsclientfactory.md). | | [SavedObjectsClientWrapperFactory](./kibana-plugin-server.savedobjectsclientwrapperfactory.md) | Describes the factory used to create instances of Saved Objects Client Wrappers. | +| [SavedObjectsFieldMapping](./kibana-plugin-server.savedobjectsfieldmapping.md) | Describe a [saved object type mapping](./kibana-plugin-server.savedobjectstypemappingdefinition.md) field.Please refer to [elasticsearch documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-types.html) For the mapping documentation | | [ScopeableRequest](./kibana-plugin-server.scopeablerequest.md) | A user credentials container. It accommodates the necessary auth credentials to impersonate the current user.See [KibanaRequest](./kibana-plugin-server.kibanarequest.md). | | [SharedGlobalConfig](./kibana-plugin-server.sharedglobalconfig.md) | | | [StringValidation](./kibana-plugin-server.stringvalidation.md) | Allows regex objects or a regex string | diff --git a/docs/development/core/server/kibana-plugin-server.savedobjectmigrationfn.md b/docs/development/core/server/kibana-plugin-server.savedobjectmigrationfn.md new file mode 100644 index 0000000000000..629d748083737 --- /dev/null +++ b/docs/development/core/server/kibana-plugin-server.savedobjectmigrationfn.md @@ -0,0 +1,13 @@ + + +[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [SavedObjectMigrationFn](./kibana-plugin-server.savedobjectmigrationfn.md) + +## SavedObjectMigrationFn type + +A migration function defined for a [saved objects type](./kibana-plugin-server.savedobjectstype.md) used to migrate it's + +Signature: + +```typescript +export declare type SavedObjectMigrationFn = (doc: SavedObjectUnsanitizedDoc, log: SavedObjectsMigrationLogger) => SavedObjectUnsanitizedDoc; +``` diff --git a/docs/development/core/server/kibana-plugin-server.savedobjectmigrationmap.md b/docs/development/core/server/kibana-plugin-server.savedobjectmigrationmap.md new file mode 100644 index 0000000000000..1f49b44f73a72 --- /dev/null +++ b/docs/development/core/server/kibana-plugin-server.savedobjectmigrationmap.md @@ -0,0 +1,27 @@ + + +[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [SavedObjectMigrationMap](./kibana-plugin-server.savedobjectmigrationmap.md) + +## SavedObjectMigrationMap interface + +A map of [migration functions](./kibana-plugin-server.savedobjectmigrationfn.md) to be used for a given type. The map's keys must be valid semver versions. + +For a given document, only migrations with a higher version number than that of the document will be applied. Migrations are executed in order, starting from the lowest version and ending with the highest one. + +Signature: + +```typescript +export interface SavedObjectMigrationMap +``` + +## Example + + +```typescript +const migrations: SavedObjectMigrationMap = { + '1.0.0': migrateToV1, + '2.1.0': migrateToV21 +} + +``` + diff --git a/docs/development/core/server/kibana-plugin-server.savedobjectsanitizeddoc.md b/docs/development/core/server/kibana-plugin-server.savedobjectsanitizeddoc.md new file mode 100644 index 0000000000000..3c7cd591cd41b --- /dev/null +++ b/docs/development/core/server/kibana-plugin-server.savedobjectsanitizeddoc.md @@ -0,0 +1,12 @@ + + +[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [SavedObjectSanitizedDoc](./kibana-plugin-server.savedobjectsanitizeddoc.md) + +## SavedObjectSanitizedDoc type + + +Signature: + +```typescript +export declare type SavedObjectSanitizedDoc = SavedObjectDoc & Referencable; +``` diff --git a/docs/development/core/server/kibana-plugin-server.savedobjectscomplexfieldmapping.dynamic.md b/docs/development/core/server/kibana-plugin-server.savedobjectscomplexfieldmapping.dynamic.md new file mode 100644 index 0000000000000..44e548c611b02 --- /dev/null +++ b/docs/development/core/server/kibana-plugin-server.savedobjectscomplexfieldmapping.dynamic.md @@ -0,0 +1,11 @@ + + +[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [SavedObjectsComplexFieldMapping](./kibana-plugin-server.savedobjectscomplexfieldmapping.md) > [dynamic](./kibana-plugin-server.savedobjectscomplexfieldmapping.dynamic.md) + +## SavedObjectsComplexFieldMapping.dynamic property + +Signature: + +```typescript +dynamic?: string; +``` diff --git a/docs/development/core/server/kibana-plugin-server.savedobjectscomplexfieldmapping.md b/docs/development/core/server/kibana-plugin-server.savedobjectscomplexfieldmapping.md new file mode 100644 index 0000000000000..ecb5ec0e88911 --- /dev/null +++ b/docs/development/core/server/kibana-plugin-server.savedobjectscomplexfieldmapping.md @@ -0,0 +1,22 @@ + + +[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [SavedObjectsComplexFieldMapping](./kibana-plugin-server.savedobjectscomplexfieldmapping.md) + +## SavedObjectsComplexFieldMapping interface + +See [SavedObjectsFieldMapping](./kibana-plugin-server.savedobjectsfieldmapping.md) for documentation. + +Signature: + +```typescript +export interface SavedObjectsComplexFieldMapping +``` + +## Properties + +| Property | Type | Description | +| --- | --- | --- | +| [dynamic](./kibana-plugin-server.savedobjectscomplexfieldmapping.dynamic.md) | string | | +| [properties](./kibana-plugin-server.savedobjectscomplexfieldmapping.properties.md) | SavedObjectsMappingProperties | | +| [type](./kibana-plugin-server.savedobjectscomplexfieldmapping.type.md) | string | | + diff --git a/docs/development/core/server/kibana-plugin-server.savedobjectscomplexfieldmapping.properties.md b/docs/development/core/server/kibana-plugin-server.savedobjectscomplexfieldmapping.properties.md new file mode 100644 index 0000000000000..7ceb0f8ee43c5 --- /dev/null +++ b/docs/development/core/server/kibana-plugin-server.savedobjectscomplexfieldmapping.properties.md @@ -0,0 +1,11 @@ + + +[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [SavedObjectsComplexFieldMapping](./kibana-plugin-server.savedobjectscomplexfieldmapping.md) > [properties](./kibana-plugin-server.savedobjectscomplexfieldmapping.properties.md) + +## SavedObjectsComplexFieldMapping.properties property + +Signature: + +```typescript +properties: SavedObjectsMappingProperties; +``` diff --git a/docs/development/core/server/kibana-plugin-server.savedobjectscomplexfieldmapping.type.md b/docs/development/core/server/kibana-plugin-server.savedobjectscomplexfieldmapping.type.md new file mode 100644 index 0000000000000..ef0db5123c927 --- /dev/null +++ b/docs/development/core/server/kibana-plugin-server.savedobjectscomplexfieldmapping.type.md @@ -0,0 +1,11 @@ + + +[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [SavedObjectsComplexFieldMapping](./kibana-plugin-server.savedobjectscomplexfieldmapping.md) > [type](./kibana-plugin-server.savedobjectscomplexfieldmapping.type.md) + +## SavedObjectsComplexFieldMapping.type property + +Signature: + +```typescript +type?: string; +``` diff --git a/docs/development/core/server/kibana-plugin-server.savedobjectscorefieldmapping.enabled.md b/docs/development/core/server/kibana-plugin-server.savedobjectscorefieldmapping.enabled.md new file mode 100644 index 0000000000000..b7e4203977763 --- /dev/null +++ b/docs/development/core/server/kibana-plugin-server.savedobjectscorefieldmapping.enabled.md @@ -0,0 +1,11 @@ + + +[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [SavedObjectsCoreFieldMapping](./kibana-plugin-server.savedobjectscorefieldmapping.md) > [enabled](./kibana-plugin-server.savedobjectscorefieldmapping.enabled.md) + +## SavedObjectsCoreFieldMapping.enabled property + +Signature: + +```typescript +enabled?: boolean; +``` diff --git a/docs/development/core/server/kibana-plugin-server.savedobjectscorefieldmapping.fields.md b/docs/development/core/server/kibana-plugin-server.savedobjectscorefieldmapping.fields.md new file mode 100644 index 0000000000000..880ef36cd73fc --- /dev/null +++ b/docs/development/core/server/kibana-plugin-server.savedobjectscorefieldmapping.fields.md @@ -0,0 +1,15 @@ + + +[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [SavedObjectsCoreFieldMapping](./kibana-plugin-server.savedobjectscorefieldmapping.md) > [fields](./kibana-plugin-server.savedobjectscorefieldmapping.fields.md) + +## SavedObjectsCoreFieldMapping.fields property + +Signature: + +```typescript +fields?: { + [subfield: string]: { + type: string; + }; + }; +``` diff --git a/docs/development/core/server/kibana-plugin-server.savedobjectscorefieldmapping.index.md b/docs/development/core/server/kibana-plugin-server.savedobjectscorefieldmapping.index.md new file mode 100644 index 0000000000000..31ff5a1d51948 --- /dev/null +++ b/docs/development/core/server/kibana-plugin-server.savedobjectscorefieldmapping.index.md @@ -0,0 +1,11 @@ + + +[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [SavedObjectsCoreFieldMapping](./kibana-plugin-server.savedobjectscorefieldmapping.md) > [index](./kibana-plugin-server.savedobjectscorefieldmapping.index.md) + +## SavedObjectsCoreFieldMapping.index property + +Signature: + +```typescript +index?: boolean; +``` diff --git a/docs/development/core/server/kibana-plugin-server.savedobjectscorefieldmapping.md b/docs/development/core/server/kibana-plugin-server.savedobjectscorefieldmapping.md new file mode 100644 index 0000000000000..4287921997098 --- /dev/null +++ b/docs/development/core/server/kibana-plugin-server.savedobjectscorefieldmapping.md @@ -0,0 +1,23 @@ + + +[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [SavedObjectsCoreFieldMapping](./kibana-plugin-server.savedobjectscorefieldmapping.md) + +## SavedObjectsCoreFieldMapping interface + +See [SavedObjectsFieldMapping](./kibana-plugin-server.savedobjectsfieldmapping.md) for documentation. + +Signature: + +```typescript +export interface SavedObjectsCoreFieldMapping +``` + +## Properties + +| Property | Type | Description | +| --- | --- | --- | +| [enabled](./kibana-plugin-server.savedobjectscorefieldmapping.enabled.md) | boolean | | +| [fields](./kibana-plugin-server.savedobjectscorefieldmapping.fields.md) | {
[subfield: string]: {
type: string;
};
} | | +| [index](./kibana-plugin-server.savedobjectscorefieldmapping.index.md) | boolean | | +| [type](./kibana-plugin-server.savedobjectscorefieldmapping.type.md) | string | | + diff --git a/docs/development/core/server/kibana-plugin-server.savedobjectscorefieldmapping.type.md b/docs/development/core/server/kibana-plugin-server.savedobjectscorefieldmapping.type.md new file mode 100644 index 0000000000000..8071217af861f --- /dev/null +++ b/docs/development/core/server/kibana-plugin-server.savedobjectscorefieldmapping.type.md @@ -0,0 +1,11 @@ + + +[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [SavedObjectsCoreFieldMapping](./kibana-plugin-server.savedobjectscorefieldmapping.md) > [type](./kibana-plugin-server.savedobjectscorefieldmapping.type.md) + +## SavedObjectsCoreFieldMapping.type property + +Signature: + +```typescript +type: string; +``` diff --git a/docs/development/core/server/kibana-plugin-server.savedobjectsfieldmapping.md b/docs/development/core/server/kibana-plugin-server.savedobjectsfieldmapping.md new file mode 100644 index 0000000000000..f31e18e8df670 --- /dev/null +++ b/docs/development/core/server/kibana-plugin-server.savedobjectsfieldmapping.md @@ -0,0 +1,15 @@ + + +[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [SavedObjectsFieldMapping](./kibana-plugin-server.savedobjectsfieldmapping.md) + +## SavedObjectsFieldMapping type + +Describe a [saved object type mapping](./kibana-plugin-server.savedobjectstypemappingdefinition.md) field. + +Please refer to [elasticsearch documentation](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-types.html) For the mapping documentation + +Signature: + +```typescript +export declare type SavedObjectsFieldMapping = SavedObjectsCoreFieldMapping | SavedObjectsComplexFieldMapping; +``` diff --git a/docs/development/core/server/kibana-plugin-server.savedobjectsmappingproperties.md b/docs/development/core/server/kibana-plugin-server.savedobjectsmappingproperties.md new file mode 100644 index 0000000000000..23c5a83afc1f8 --- /dev/null +++ b/docs/development/core/server/kibana-plugin-server.savedobjectsmappingproperties.md @@ -0,0 +1,13 @@ + + +[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [SavedObjectsMappingProperties](./kibana-plugin-server.savedobjectsmappingproperties.md) + +## SavedObjectsMappingProperties interface + +Describe the fields of a [saved object type](./kibana-plugin-server.savedobjectstypemappingdefinition.md). + +Signature: + +```typescript +export interface SavedObjectsMappingProperties +``` diff --git a/docs/development/core/server/kibana-plugin-server.savedobjectsmigrationlogger.md b/docs/development/core/server/kibana-plugin-server.savedobjectsmigrationlogger.md index a98d88700cd55..1b8db4a6c9f8d 100644 --- a/docs/development/core/server/kibana-plugin-server.savedobjectsmigrationlogger.md +++ b/docs/development/core/server/kibana-plugin-server.savedobjectsmigrationlogger.md @@ -17,5 +17,6 @@ export interface SavedObjectsMigrationLogger | --- | --- | --- | | [debug](./kibana-plugin-server.savedobjectsmigrationlogger.debug.md) | (msg: string) => void | | | [info](./kibana-plugin-server.savedobjectsmigrationlogger.info.md) | (msg: string) => void | | +| [warn](./kibana-plugin-server.savedobjectsmigrationlogger.warn.md) | (msg: string) => void | | | [warning](./kibana-plugin-server.savedobjectsmigrationlogger.warning.md) | (msg: string) => void | | diff --git a/docs/development/core/server/kibana-plugin-server.savedobjectsmigrationlogger.warn.md b/docs/development/core/server/kibana-plugin-server.savedobjectsmigrationlogger.warn.md new file mode 100644 index 0000000000000..7ebb1dd5ee4d2 --- /dev/null +++ b/docs/development/core/server/kibana-plugin-server.savedobjectsmigrationlogger.warn.md @@ -0,0 +1,11 @@ + + +[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [SavedObjectsMigrationLogger](./kibana-plugin-server.savedobjectsmigrationlogger.md) > [warn](./kibana-plugin-server.savedobjectsmigrationlogger.warn.md) + +## SavedObjectsMigrationLogger.warn property + +Signature: + +```typescript +warn: (msg: string) => void; +``` diff --git a/docs/development/core/server/kibana-plugin-server.savedobjectsmigrationlogger.warning.md b/docs/development/core/server/kibana-plugin-server.savedobjectsmigrationlogger.warning.md index a87955d603b70..689a799515094 100644 --- a/docs/development/core/server/kibana-plugin-server.savedobjectsmigrationlogger.warning.md +++ b/docs/development/core/server/kibana-plugin-server.savedobjectsmigrationlogger.warning.md @@ -4,6 +4,11 @@ ## SavedObjectsMigrationLogger.warning property +> Warning: This API is now obsolete. +> +> Use `warn` instead. +> + Signature: ```typescript diff --git a/docs/development/core/server/kibana-plugin-server.savedobjectsrawdoc._source.md b/docs/development/core/server/kibana-plugin-server.savedobjectsrawdoc._source.md index dcf207f8120ea..332df982b23eb 100644 --- a/docs/development/core/server/kibana-plugin-server.savedobjectsrawdoc._source.md +++ b/docs/development/core/server/kibana-plugin-server.savedobjectsrawdoc._source.md @@ -7,5 +7,5 @@ Signature: ```typescript -_source: any; +_source: SavedObjectsRawDocSource; ``` diff --git a/docs/development/core/server/kibana-plugin-server.savedobjectsrawdoc.md b/docs/development/core/server/kibana-plugin-server.savedobjectsrawdoc.md index b0130df01817f..3e3ad7da5aecd 100644 --- a/docs/development/core/server/kibana-plugin-server.savedobjectsrawdoc.md +++ b/docs/development/core/server/kibana-plugin-server.savedobjectsrawdoc.md @@ -9,7 +9,7 @@ A raw document as represented directly in the saved object index. Signature: ```typescript -export interface RawDoc +export interface SavedObjectsRawDoc ``` ## Properties @@ -19,6 +19,6 @@ export interface RawDoc | [\_id](./kibana-plugin-server.savedobjectsrawdoc._id.md) | string | | | [\_primary\_term](./kibana-plugin-server.savedobjectsrawdoc._primary_term.md) | number | | | [\_seq\_no](./kibana-plugin-server.savedobjectsrawdoc._seq_no.md) | number | | -| [\_source](./kibana-plugin-server.savedobjectsrawdoc._source.md) | any | | +| [\_source](./kibana-plugin-server.savedobjectsrawdoc._source.md) | SavedObjectsRawDocSource | | | [\_type](./kibana-plugin-server.savedobjectsrawdoc._type.md) | string | | diff --git a/docs/development/core/server/kibana-plugin-server.savedobjectsserializer.generaterawid.md b/docs/development/core/server/kibana-plugin-server.savedobjectsserializer.generaterawid.md new file mode 100644 index 0000000000000..50fb4433b33c8 --- /dev/null +++ b/docs/development/core/server/kibana-plugin-server.savedobjectsserializer.generaterawid.md @@ -0,0 +1,26 @@ + + +[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [SavedObjectsSerializer](./kibana-plugin-server.savedobjectsserializer.md) > [generateRawId](./kibana-plugin-server.savedobjectsserializer.generaterawid.md) + +## SavedObjectsSerializer.generateRawId() method + +Given a saved object type and id, generates the compound id that is stored in the raw document. + +Signature: + +```typescript +generateRawId(namespace: string | undefined, type: string, id?: string): string; +``` + +## Parameters + +| Parameter | Type | Description | +| --- | --- | --- | +| namespace | string | undefined | | +| type | string | | +| id | string | | + +Returns: + +`string` + diff --git a/docs/development/core/server/kibana-plugin-server.savedobjectsserializer.israwsavedobject.md b/docs/development/core/server/kibana-plugin-server.savedobjectsserializer.israwsavedobject.md new file mode 100644 index 0000000000000..32d00ee1a9f7f --- /dev/null +++ b/docs/development/core/server/kibana-plugin-server.savedobjectsserializer.israwsavedobject.md @@ -0,0 +1,24 @@ + + +[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [SavedObjectsSerializer](./kibana-plugin-server.savedobjectsserializer.md) > [isRawSavedObject](./kibana-plugin-server.savedobjectsserializer.israwsavedobject.md) + +## SavedObjectsSerializer.isRawSavedObject() method + +Determines whether or not the raw document can be converted to a saved object. + +Signature: + +```typescript +isRawSavedObject(rawDoc: SavedObjectsRawDoc): boolean; +``` + +## Parameters + +| Parameter | Type | Description | +| --- | --- | --- | +| rawDoc | SavedObjectsRawDoc | | + +Returns: + +`boolean` + diff --git a/docs/development/core/server/kibana-plugin-server.savedobjectsserializer.md b/docs/development/core/server/kibana-plugin-server.savedobjectsserializer.md new file mode 100644 index 0000000000000..5fa84af147b40 --- /dev/null +++ b/docs/development/core/server/kibana-plugin-server.savedobjectsserializer.md @@ -0,0 +1,29 @@ + + +[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [SavedObjectsSerializer](./kibana-plugin-server.savedobjectsserializer.md) + +## SavedObjectsSerializer class + +A serializer that can be used to manually convert [raw](./kibana-plugin-server.savedobjectsrawdoc.md) or [sanitized](./kibana-plugin-server.savedobjectsanitizeddoc.md) documents to the other kind. + +Signature: + +```typescript +export declare class SavedObjectsSerializer +``` + +## Remarks + +Serializer instances should only be created and accessed by calling [SavedObjectsServiceStart.createSerializer](./kibana-plugin-server.savedobjectsservicestart.createserializer.md) + +The constructor for this class is marked as internal. Third-party code should not call the constructor directly or create subclasses that extend the `SavedObjectsSerializer` class. + +## Methods + +| Method | Modifiers | Description | +| --- | --- | --- | +| [generateRawId(namespace, type, id)](./kibana-plugin-server.savedobjectsserializer.generaterawid.md) | | Given a saved object type and id, generates the compound id that is stored in the raw document. | +| [isRawSavedObject(rawDoc)](./kibana-plugin-server.savedobjectsserializer.israwsavedobject.md) | | Determines whether or not the raw document can be converted to a saved object. | +| [rawToSavedObject(doc)](./kibana-plugin-server.savedobjectsserializer.rawtosavedobject.md) | | Converts a document from the format that is stored in elasticsearch to the saved object client format. | +| [savedObjectToRaw(savedObj)](./kibana-plugin-server.savedobjectsserializer.savedobjecttoraw.md) | | Converts a document from the saved object client format to the format that is stored in elasticsearch. | + diff --git a/docs/development/core/server/kibana-plugin-server.savedobjectsserializer.rawtosavedobject.md b/docs/development/core/server/kibana-plugin-server.savedobjectsserializer.rawtosavedobject.md new file mode 100644 index 0000000000000..6a8a006241297 --- /dev/null +++ b/docs/development/core/server/kibana-plugin-server.savedobjectsserializer.rawtosavedobject.md @@ -0,0 +1,24 @@ + + +[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [SavedObjectsSerializer](./kibana-plugin-server.savedobjectsserializer.md) > [rawToSavedObject](./kibana-plugin-server.savedobjectsserializer.rawtosavedobject.md) + +## SavedObjectsSerializer.rawToSavedObject() method + +Converts a document from the format that is stored in elasticsearch to the saved object client format. + +Signature: + +```typescript +rawToSavedObject(doc: SavedObjectsRawDoc): SavedObjectSanitizedDoc; +``` + +## Parameters + +| Parameter | Type | Description | +| --- | --- | --- | +| doc | SavedObjectsRawDoc | | + +Returns: + +`SavedObjectSanitizedDoc` + diff --git a/docs/development/core/server/kibana-plugin-server.savedobjectsserializer.savedobjecttoraw.md b/docs/development/core/server/kibana-plugin-server.savedobjectsserializer.savedobjecttoraw.md new file mode 100644 index 0000000000000..b0d2043665b54 --- /dev/null +++ b/docs/development/core/server/kibana-plugin-server.savedobjectsserializer.savedobjecttoraw.md @@ -0,0 +1,24 @@ + + +[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [SavedObjectsSerializer](./kibana-plugin-server.savedobjectsserializer.md) > [savedObjectToRaw](./kibana-plugin-server.savedobjectsserializer.savedobjecttoraw.md) + +## SavedObjectsSerializer.savedObjectToRaw() method + +Converts a document from the saved object client format to the format that is stored in elasticsearch. + +Signature: + +```typescript +savedObjectToRaw(savedObj: SavedObjectSanitizedDoc): SavedObjectsRawDoc; +``` + +## Parameters + +| Parameter | Type | Description | +| --- | --- | --- | +| savedObj | SavedObjectSanitizedDoc | | + +Returns: + +`SavedObjectsRawDoc` + diff --git a/docs/development/core/server/kibana-plugin-server.savedobjectsservicesetup.md b/docs/development/core/server/kibana-plugin-server.savedobjectsservicesetup.md index 64fb1f4a5f638..9981bfee0cb7d 100644 --- a/docs/development/core/server/kibana-plugin-server.savedobjectsservicesetup.md +++ b/docs/development/core/server/kibana-plugin-server.savedobjectsservicesetup.md @@ -20,9 +20,19 @@ When plugins access the Saved Objects client, a new client is created using the ## 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)); }) } } +export class Plugin() { + setup: (core: CoreSetup) => { + core.savedObjects.setClientFactory(({ request: KibanaRequest }) => { + return new SavedObjectsClient(core.savedObjects.scopedRepository(request)); + }) + } +} + +``` ## Properties diff --git a/docs/development/core/server/kibana-plugin-server.savedobjectsservicestart.createserializer.md b/docs/development/core/server/kibana-plugin-server.savedobjectsservicestart.createserializer.md new file mode 100644 index 0000000000000..b0e7ae1db89c1 --- /dev/null +++ b/docs/development/core/server/kibana-plugin-server.savedobjectsservicestart.createserializer.md @@ -0,0 +1,13 @@ + + +[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [SavedObjectsServiceStart](./kibana-plugin-server.savedobjectsservicestart.md) > [createSerializer](./kibana-plugin-server.savedobjectsservicestart.createserializer.md) + +## SavedObjectsServiceStart.createSerializer property + +Creates a [serializer](./kibana-plugin-server.savedobjectsserializer.md) that is aware of all registered types. + +Signature: + +```typescript +createSerializer: () => SavedObjectsSerializer; +``` diff --git a/docs/development/core/server/kibana-plugin-server.savedobjectsservicestart.md b/docs/development/core/server/kibana-plugin-server.savedobjectsservicestart.md index cf2b4f57a7461..ad34d76bb33f4 100644 --- a/docs/development/core/server/kibana-plugin-server.savedobjectsservicestart.md +++ b/docs/development/core/server/kibana-plugin-server.savedobjectsservicestart.md @@ -18,5 +18,6 @@ export interface SavedObjectsServiceStart | --- | --- | --- | | [createInternalRepository](./kibana-plugin-server.savedobjectsservicestart.createinternalrepository.md) | (extraTypes?: string[]) => ISavedObjectsRepository | Creates a [Saved Objects repository](./kibana-plugin-server.isavedobjectsrepository.md) that uses the internal Kibana user for authenticating with Elasticsearch. | | [createScopedRepository](./kibana-plugin-server.savedobjectsservicestart.createscopedrepository.md) | (req: KibanaRequest, extraTypes?: string[]) => ISavedObjectsRepository | Creates a [Saved Objects repository](./kibana-plugin-server.isavedobjectsrepository.md) that uses the credentials from the passed in request to authenticate with Elasticsearch. | +| [createSerializer](./kibana-plugin-server.savedobjectsservicestart.createserializer.md) | () => SavedObjectsSerializer | Creates a [serializer](./kibana-plugin-server.savedobjectsserializer.md) that is aware of all registered types. | | [getScopedClient](./kibana-plugin-server.savedobjectsservicestart.getscopedclient.md) | (req: KibanaRequest, options?: SavedObjectsClientProviderOptions) => SavedObjectsClientContract | Creates a [Saved Objects client](./kibana-plugin-server.savedobjectsclientcontract.md) 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 [RequestHandlerContext](./kibana-plugin-server.requesthandlercontext.md). | diff --git a/docs/development/core/server/kibana-plugin-server.savedobjectstype.converttoaliasscript.md b/docs/development/core/server/kibana-plugin-server.savedobjectstype.converttoaliasscript.md new file mode 100644 index 0000000000000..f2519b2600e2f --- /dev/null +++ b/docs/development/core/server/kibana-plugin-server.savedobjectstype.converttoaliasscript.md @@ -0,0 +1,13 @@ + + +[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [SavedObjectsType](./kibana-plugin-server.savedobjectstype.md) > [convertToAliasScript](./kibana-plugin-server.savedobjectstype.converttoaliasscript.md) + +## SavedObjectsType.convertToAliasScript property + +If defined, will be used to convert the type to an alias. + +Signature: + +```typescript +convertToAliasScript?: string; +``` diff --git a/docs/development/core/server/kibana-plugin-server.savedobjectstype.hidden.md b/docs/development/core/server/kibana-plugin-server.savedobjectstype.hidden.md new file mode 100644 index 0000000000000..9e611bc8faf27 --- /dev/null +++ b/docs/development/core/server/kibana-plugin-server.savedobjectstype.hidden.md @@ -0,0 +1,15 @@ + + +[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [SavedObjectsType](./kibana-plugin-server.savedobjectstype.md) > [hidden](./kibana-plugin-server.savedobjectstype.hidden.md) + +## SavedObjectsType.hidden property + +Is the type hidden by default. If true, repositories will not have access to this type unless explicitly declared as an `extraType` when creating the repository. + +See [createInternalRepository](./kibana-plugin-server.savedobjectsservicestart.createinternalrepository.md). + +Signature: + +```typescript +hidden: boolean; +``` diff --git a/docs/development/core/server/kibana-plugin-server.savedobjectstype.indexpattern.md b/docs/development/core/server/kibana-plugin-server.savedobjectstype.indexpattern.md new file mode 100644 index 0000000000000..18cb6fe936060 --- /dev/null +++ b/docs/development/core/server/kibana-plugin-server.savedobjectstype.indexpattern.md @@ -0,0 +1,13 @@ + + +[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [SavedObjectsType](./kibana-plugin-server.savedobjectstype.md) > [indexPattern](./kibana-plugin-server.savedobjectstype.indexpattern.md) + +## SavedObjectsType.indexPattern property + +If defined, the type instances will be stored in the given index instead of the default one. + +Signature: + +```typescript +indexPattern?: string; +``` diff --git a/docs/development/core/server/kibana-plugin-server.savedobjectstype.mappings.md b/docs/development/core/server/kibana-plugin-server.savedobjectstype.mappings.md new file mode 100644 index 0000000000000..3166a2604d728 --- /dev/null +++ b/docs/development/core/server/kibana-plugin-server.savedobjectstype.mappings.md @@ -0,0 +1,13 @@ + + +[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [SavedObjectsType](./kibana-plugin-server.savedobjectstype.md) > [mappings](./kibana-plugin-server.savedobjectstype.mappings.md) + +## SavedObjectsType.mappings property + +The [mapping definition](./kibana-plugin-server.savedobjectstypemappingdefinition.md) for the type. + +Signature: + +```typescript +mappings: SavedObjectsTypeMappingDefinition; +``` diff --git a/docs/development/core/server/kibana-plugin-server.savedobjectstype.md b/docs/development/core/server/kibana-plugin-server.savedobjectstype.md new file mode 100644 index 0000000000000..1e989652e52bf --- /dev/null +++ b/docs/development/core/server/kibana-plugin-server.savedobjectstype.md @@ -0,0 +1,28 @@ + + +[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [SavedObjectsType](./kibana-plugin-server.savedobjectstype.md) + +## SavedObjectsType interface + +Signature: + +```typescript +export interface SavedObjectsType +``` + +## Remarks + +This is only internal for now, and will only be public when we expose the registerType API + +## Properties + +| Property | Type | Description | +| --- | --- | --- | +| [convertToAliasScript](./kibana-plugin-server.savedobjectstype.converttoaliasscript.md) | string | If defined, will be used to convert the type to an alias. | +| [hidden](./kibana-plugin-server.savedobjectstype.hidden.md) | boolean | Is the type hidden by default. If true, repositories will not have access to this type unless explicitly declared as an extraType when creating the repository.See [createInternalRepository](./kibana-plugin-server.savedobjectsservicestart.createinternalrepository.md). | +| [indexPattern](./kibana-plugin-server.savedobjectstype.indexpattern.md) | string | If defined, the type instances will be stored in the given index instead of the default one. | +| [mappings](./kibana-plugin-server.savedobjectstype.mappings.md) | SavedObjectsTypeMappingDefinition | The [mapping definition](./kibana-plugin-server.savedobjectstypemappingdefinition.md) for the type. | +| [migrations](./kibana-plugin-server.savedobjectstype.migrations.md) | SavedObjectMigrationMap | An optional map of [migrations](./kibana-plugin-server.savedobjectmigrationfn.md) to be used to migrate the type. | +| [name](./kibana-plugin-server.savedobjectstype.name.md) | string | The name of the type, which is also used as the internal id. | +| [namespaceAgnostic](./kibana-plugin-server.savedobjectstype.namespaceagnostic.md) | boolean | Is the type global (true), or namespaced (false). | + diff --git a/docs/development/core/server/kibana-plugin-server.savedobjectstype.migrations.md b/docs/development/core/server/kibana-plugin-server.savedobjectstype.migrations.md new file mode 100644 index 0000000000000..a7f933ef6e3f7 --- /dev/null +++ b/docs/development/core/server/kibana-plugin-server.savedobjectstype.migrations.md @@ -0,0 +1,13 @@ + + +[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [SavedObjectsType](./kibana-plugin-server.savedobjectstype.md) > [migrations](./kibana-plugin-server.savedobjectstype.migrations.md) + +## SavedObjectsType.migrations property + +An optional map of [migrations](./kibana-plugin-server.savedobjectmigrationfn.md) to be used to migrate the type. + +Signature: + +```typescript +migrations?: SavedObjectMigrationMap; +``` diff --git a/docs/development/core/server/kibana-plugin-server.savedobjectstype.name.md b/docs/development/core/server/kibana-plugin-server.savedobjectstype.name.md new file mode 100644 index 0000000000000..444152d239cdf --- /dev/null +++ b/docs/development/core/server/kibana-plugin-server.savedobjectstype.name.md @@ -0,0 +1,13 @@ + + +[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [SavedObjectsType](./kibana-plugin-server.savedobjectstype.md) > [name](./kibana-plugin-server.savedobjectstype.name.md) + +## SavedObjectsType.name property + +The name of the type, which is also used as the internal id. + +Signature: + +```typescript +name: string; +``` diff --git a/docs/development/core/server/kibana-plugin-server.savedobjectstype.namespaceagnostic.md b/docs/development/core/server/kibana-plugin-server.savedobjectstype.namespaceagnostic.md new file mode 100644 index 0000000000000..5c68942276ec7 --- /dev/null +++ b/docs/development/core/server/kibana-plugin-server.savedobjectstype.namespaceagnostic.md @@ -0,0 +1,13 @@ + + +[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [SavedObjectsType](./kibana-plugin-server.savedobjectstype.md) > [namespaceAgnostic](./kibana-plugin-server.savedobjectstype.namespaceagnostic.md) + +## SavedObjectsType.namespaceAgnostic property + +Is the type global (true), or namespaced (false). + +Signature: + +```typescript +namespaceAgnostic: boolean; +``` diff --git a/docs/development/core/server/kibana-plugin-server.savedobjectstypemappingdefinition.md b/docs/development/core/server/kibana-plugin-server.savedobjectstypemappingdefinition.md new file mode 100644 index 0000000000000..99983d3a9f02b --- /dev/null +++ b/docs/development/core/server/kibana-plugin-server.savedobjectstypemappingdefinition.md @@ -0,0 +1,45 @@ + + +[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [SavedObjectsTypeMappingDefinition](./kibana-plugin-server.savedobjectstypemappingdefinition.md) + +## SavedObjectsTypeMappingDefinition interface + +Describe a saved object type mapping. + +Signature: + +```typescript +export interface SavedObjectsTypeMappingDefinition +``` + +## Example + + +```ts +const typeDefinition: SavedObjectsTypeMappingDefinition = { + properties: { + enabled: { + type: "boolean" + }, + sendUsageFrom: { + ignore_above: 256, + type: "keyword" + }, + lastReported: { + type: "date" + }, + lastVersionChecked: { + ignore_above: 256, + type: "keyword" + }, + } +} + +``` + +## Properties + +| Property | Type | Description | +| --- | --- | --- | +| [properties](./kibana-plugin-server.savedobjectstypemappingdefinition.properties.md) | SavedObjectsMappingProperties | | + diff --git a/docs/development/core/server/kibana-plugin-server.savedobjectstypemappingdefinition.properties.md b/docs/development/core/server/kibana-plugin-server.savedobjectstypemappingdefinition.properties.md new file mode 100644 index 0000000000000..555870c3fdd7d --- /dev/null +++ b/docs/development/core/server/kibana-plugin-server.savedobjectstypemappingdefinition.properties.md @@ -0,0 +1,11 @@ + + +[Home](./index.md) > [kibana-plugin-server](./kibana-plugin-server.md) > [SavedObjectsTypeMappingDefinition](./kibana-plugin-server.savedobjectstypemappingdefinition.md) > [properties](./kibana-plugin-server.savedobjectstypemappingdefinition.properties.md) + +## SavedObjectsTypeMappingDefinition.properties property + +Signature: + +```typescript +properties: SavedObjectsMappingProperties; +``` diff --git a/src/core/server/index.ts b/src/core/server/index.ts index c45acd7f0129a..cc838ddd1351d 100644 --- a/src/core/server/index.ts +++ b/src/core/server/index.ts @@ -197,6 +197,7 @@ export { SavedObjectsImportUnsupportedTypeError, SavedObjectsMigrationLogger, SavedObjectsRawDoc, + SavedObjectSanitizedDoc, SavedObjectsRepositoryFactory, SavedObjectsResolveImportErrorsOptions, SavedObjectsSchema, @@ -211,6 +212,15 @@ export { SavedObjectsRepository, SavedObjectsDeleteByNamespaceOptions, SavedObjectsIncrementCounterOptions, + SavedObjectsComplexFieldMapping, + SavedObjectsCoreFieldMapping, + SavedObjectsFieldMapping, + SavedObjectsTypeMappingDefinition, + SavedObjectsMappingProperties, + SavedObjectTypeRegistry, + SavedObjectsType, + SavedObjectMigrationMap, + SavedObjectMigrationFn, } from './saved_objects'; export { diff --git a/src/core/server/legacy/legacy_service.mock.ts b/src/core/server/legacy/legacy_service.mock.ts index 495141cdcb58d..44405dc391d8e 100644 --- a/src/core/server/legacy/legacy_service.mock.ts +++ b/src/core/server/legacy/legacy_service.mock.ts @@ -18,13 +18,18 @@ */ import { LegacyService } from './legacy_service'; -import { LegacyServiceDiscoverPlugins, LegacyServiceSetupDeps } from './types'; +import { LegacyConfig, LegacyServiceDiscoverPlugins, LegacyServiceSetupDeps } from './types'; type LegacyServiceMock = jest.Mocked & { legacyId: symbol }>; const createDiscoverPluginsMock = (): LegacyServiceDiscoverPlugins => ({ pluginSpecs: [], - uiExports: {} as any, + uiExports: { + savedObjectSchemas: {}, + savedObjectMappings: [], + savedObjectMigrations: {}, + savedObjectValidations: {}, + }, navLinks: [], pluginExtendedConfig: { get: jest.fn(), @@ -34,6 +39,7 @@ const createDiscoverPluginsMock = (): LegacyServiceDiscoverPlugins => ({ disabledPluginSpecs: [], settings: {}, }); + const createLegacyServiceMock = (): LegacyServiceMock => ({ legacyId: Symbol(), discoverPlugins: jest.fn().mockResolvedValue(createDiscoverPluginsMock()), @@ -42,8 +48,15 @@ const createLegacyServiceMock = (): LegacyServiceMock => ({ stop: jest.fn(), }); +const createLegacyConfigMock = (): jest.Mocked => ({ + get: jest.fn(), + has: jest.fn(), + set: jest.fn(), +}); + export const legacyServiceMock = { create: createLegacyServiceMock, createSetupContract: (deps: LegacyServiceSetupDeps) => createLegacyServiceMock().setup(deps), createDiscoverPlugins: createDiscoverPluginsMock, + createLegacyConfig: createLegacyConfigMock, }; diff --git a/src/core/server/legacy/legacy_service.test.ts b/src/core/server/legacy/legacy_service.test.ts index af4db68ee95e1..e8e20580a36db 100644 --- a/src/core/server/legacy/legacy_service.test.ts +++ b/src/core/server/legacy/legacy_service.test.ts @@ -83,7 +83,7 @@ beforeEach(() => { getAuthHeaders: () => undefined, } as any, }, - savedObjects: savedObjectsServiceMock.createSetupContract(), + savedObjects: savedObjectsServiceMock.createInternalSetupContract(), plugins: { contracts: new Map([['plugin-id', 'plugin-value']]), uiPlugins: { @@ -101,7 +101,7 @@ beforeEach(() => { startDeps = { core: { capabilities: capabilitiesServiceMock.createStartContract(), - savedObjects: savedObjectsServiceMock.createStartContract(), + savedObjects: savedObjectsServiceMock.createInternalStartContract(), uiSettings: uiSettingsServiceMock.createStartContract(), plugins: { contracts: new Map() }, }, diff --git a/src/core/server/legacy/legacy_service.ts b/src/core/server/legacy/legacy_service.ts index f9b18afadc938..b2501496d87ef 100644 --- a/src/core/server/legacy/legacy_service.ts +++ b/src/core/server/legacy/legacy_service.ts @@ -262,6 +262,7 @@ export class LegacyService implements CoreService { getScopedClient: startDeps.core.savedObjects.getScopedClient, createScopedRepository: startDeps.core.savedObjects.createScopedRepository, createInternalRepository: startDeps.core.savedObjects.createInternalRepository, + createSerializer: startDeps.core.savedObjects.createSerializer, }, uiSettings: { asScopedToClient: startDeps.core.uiSettings.asScopedToClient }, }; @@ -328,6 +329,7 @@ export class LegacyService implements CoreService { __internals: { hapiServer: setupDeps.core.http.server, kibanaMigrator: startDeps.core.savedObjects.migrator, + typeRegistry: startDeps.core.savedObjects.typeRegistry, uiPlugins: setupDeps.core.plugins.uiPlugins, elasticsearch: setupDeps.core.elasticsearch, rendering: setupDeps.core.rendering, diff --git a/src/core/server/mocks.ts b/src/core/server/mocks.ts index 97f836f8ef37d..9a7868d568ea0 100644 --- a/src/core/server/mocks.ts +++ b/src/core/server/mocks.ts @@ -38,6 +38,7 @@ export { httpServiceMock } from './http/http_service.mock'; export { loggingServiceMock } from './logging/logging_service.mock'; export { savedObjectsClientMock } from './saved_objects/service/saved_objects_client.mock'; export { savedObjectsRepositoryMock } from './saved_objects/service/lib/repository.mock'; +export { typeRegistryMock as savedObjectsTypeRegistryMock } from './saved_objects/saved_objects_type_registry.mock'; export { uiSettingsServiceMock } from './ui_settings/ui_settings_service.mock'; import { uuidServiceMock } from './uuid/uuid_service.mock'; @@ -112,12 +113,19 @@ function createCoreSetupMock() { const uiSettingsMock = { register: uiSettingsServiceMock.createSetupContract().register, }; + + const savedObjectsService = savedObjectsServiceMock.createSetupContract(); + const savedObjectMock: jest.Mocked = { + addClientWrapper: savedObjectsService.addClientWrapper, + setClientFactoryProvider: savedObjectsService.setClientFactoryProvider, + }; + const mock: CoreSetupMockType = { capabilities: capabilitiesServiceMock.createSetupContract(), context: contextServiceMock.createSetupContract(), elasticsearch: elasticsearchServiceMock.createSetup(), http: httpMock, - savedObjects: savedObjectsServiceMock.createSetupContract(), + savedObjects: savedObjectMock, uiSettings: uiSettingsMock, uuid: uuidServiceMock.createSetupContract(), getStartServices: jest @@ -145,7 +153,7 @@ function createInternalCoreSetupMock() { elasticsearch: elasticsearchServiceMock.createInternalSetup(), http: httpServiceMock.createSetupContract(), uiSettings: uiSettingsServiceMock.createSetupContract(), - savedObjects: savedObjectsServiceMock.createSetupContract(), + savedObjects: savedObjectsServiceMock.createInternalSetupContract(), uuid: uuidServiceMock.createSetupContract(), }; return setupDeps; @@ -154,7 +162,7 @@ function createInternalCoreSetupMock() { function createInternalCoreStartMock() { const startDeps: InternalCoreStart = { capabilities: capabilitiesServiceMock.createStartContract(), - savedObjects: savedObjectsServiceMock.createStartContract(), + savedObjects: savedObjectsServiceMock.createInternalStartContract(), uiSettings: uiSettingsServiceMock.createStartContract(), }; return startDeps; diff --git a/src/core/server/plugins/plugin_context.ts b/src/core/server/plugins/plugin_context.ts index 77300900e84f3..a7b555a9eba01 100644 --- a/src/core/server/plugins/plugin_context.ts +++ b/src/core/server/plugins/plugin_context.ts @@ -205,6 +205,7 @@ export function createPluginStartContext( getScopedClient: deps.savedObjects.getScopedClient, createInternalRepository: deps.savedObjects.createInternalRepository, createScopedRepository: deps.savedObjects.createScopedRepository, + createSerializer: deps.savedObjects.createSerializer, }, uiSettings: { asScopedToClient: deps.uiSettings.asScopedToClient, diff --git a/src/core/server/saved_objects/__snapshots__/utils.test.ts.snap b/src/core/server/saved_objects/__snapshots__/utils.test.ts.snap new file mode 100644 index 0000000000000..7846e7f1a802a --- /dev/null +++ b/src/core/server/saved_objects/__snapshots__/utils.test.ts.snap @@ -0,0 +1,144 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`convertLegacyTypes converts the legacy mappings using default values if no schemas are specified 1`] = ` +Array [ + Object { + "convertToAliasScript": undefined, + "hidden": false, + "indexPattern": undefined, + "mappings": Object { + "properties": Object { + "fieldA": Object { + "type": "text", + }, + }, + }, + "migrations": Object {}, + "name": "typeA", + "namespaceAgnostic": false, + }, + Object { + "convertToAliasScript": undefined, + "hidden": false, + "indexPattern": undefined, + "mappings": Object { + "properties": Object { + "fieldB": Object { + "type": "text", + }, + }, + }, + "migrations": Object {}, + "name": "typeB", + "namespaceAgnostic": false, + }, + Object { + "convertToAliasScript": undefined, + "hidden": false, + "indexPattern": undefined, + "mappings": Object { + "properties": Object { + "fieldC": Object { + "type": "text", + }, + }, + }, + "migrations": Object {}, + "name": "typeC", + "namespaceAgnostic": false, + }, +] +`; + +exports[`convertLegacyTypes merges everything when all are present 1`] = ` +Array [ + Object { + "convertToAliasScript": undefined, + "hidden": true, + "indexPattern": "myIndex", + "mappings": Object { + "properties": Object { + "fieldA": Object { + "type": "text", + }, + }, + }, + "migrations": Object { + "1.0.0": [MockFunction], + "2.0.4": [MockFunction], + }, + "name": "typeA", + "namespaceAgnostic": true, + }, + Object { + "convertToAliasScript": "some alias script", + "hidden": false, + "indexPattern": undefined, + "mappings": Object { + "properties": Object { + "anotherFieldB": Object { + "type": "boolean", + }, + "fieldB": Object { + "type": "text", + }, + }, + }, + "migrations": Object {}, + "name": "typeB", + "namespaceAgnostic": false, + }, + Object { + "convertToAliasScript": undefined, + "hidden": false, + "indexPattern": undefined, + "mappings": Object { + "properties": Object { + "fieldC": Object { + "type": "text", + }, + }, + }, + "migrations": Object { + "1.5.3": [MockFunction], + }, + "name": "typeC", + "namespaceAgnostic": false, + }, +] +`; + +exports[`convertLegacyTypes merges the mappings and the schema to create the type when schema exists for the type 1`] = ` +Array [ + Object { + "convertToAliasScript": undefined, + "hidden": true, + "indexPattern": "fooBar", + "mappings": Object { + "properties": Object { + "fieldA": Object { + "type": "text", + }, + }, + }, + "migrations": Object {}, + "name": "typeA", + "namespaceAgnostic": true, + }, + Object { + "convertToAliasScript": undefined, + "hidden": false, + "indexPattern": undefined, + "mappings": Object { + "properties": Object { + "fieldC": Object { + "type": "text", + }, + }, + }, + "migrations": Object {}, + "name": "typeC", + "namespaceAgnostic": false, + }, +] +`; diff --git a/src/core/server/saved_objects/index.ts b/src/core/server/saved_objects/index.ts index 181025d73817d..529ee9599f178 100644 --- a/src/core/server/saved_objects/index.ts +++ b/src/core/server/saved_objects/index.ts @@ -31,7 +31,12 @@ export { SavedObjectsExportResultDetails, } from './export'; -export { SavedObjectsSerializer, RawDoc as SavedObjectsRawDoc } from './serialization'; +export { + SavedObjectsSerializer, + SavedObjectsRawDoc, + SavedObjectSanitizedDoc, + SavedObjectUnsanitizedDoc, +} from './serialization'; export { SavedObjectsMigrationLogger } from './migrations/core/migration_logger'; @@ -50,4 +55,18 @@ export { SavedObjectsDeleteByNamespaceOptions, } from './service/lib/repository'; +export { + SavedObjectsCoreFieldMapping, + SavedObjectsComplexFieldMapping, + SavedObjectsFieldMapping, + SavedObjectsMappingProperties, + SavedObjectsTypeMappingDefinition, + SavedObjectsTypeMappingDefinitions, +} from './mappings'; + +export { SavedObjectMigrationMap, SavedObjectMigrationFn } from './migrations'; + +export { SavedObjectsType } from './types'; + export { config } from './saved_objects_config'; +export { SavedObjectTypeRegistry, ISavedObjectTypeRegistry } from './saved_objects_type_registry'; diff --git a/src/core/server/saved_objects/mappings/index.ts b/src/core/server/saved_objects/mappings/index.ts index 15b0736ca5f1f..e1d718ee454ce 100644 --- a/src/core/server/saved_objects/mappings/index.ts +++ b/src/core/server/saved_objects/mappings/index.ts @@ -18,9 +18,12 @@ */ export { getTypes, getProperty, getRootProperties, getRootPropertiesObjects } from './lib'; export { - FieldMapping, - MappingMeta, - MappingProperties, + SavedObjectsComplexFieldMapping, + SavedObjectsCoreFieldMapping, + SavedObjectsTypeMappingDefinition, + SavedObjectsTypeMappingDefinitions, + SavedObjectsMappingProperties, + SavedObjectsFieldMapping, + IndexMappingMeta, IndexMapping, - SavedObjectsMapping, } from './types'; diff --git a/src/core/server/saved_objects/mappings/lib/get_property.test.ts b/src/core/server/saved_objects/mappings/lib/get_property.test.ts index a85697ddd08b8..a271cf3826e5d 100644 --- a/src/core/server/saved_objects/mappings/lib/get_property.test.ts +++ b/src/core/server/saved_objects/mappings/lib/get_property.test.ts @@ -17,7 +17,7 @@ * under the License. */ -import { FieldMapping, IndexMapping } from '../types'; +import { SavedObjectsFieldMapping, IndexMapping } from '../types'; import { getProperty } from './get_property'; const MAPPINGS = { @@ -47,7 +47,7 @@ const MAPPINGS = { }, }; -function runTest(key: string | string[], mapping: IndexMapping | FieldMapping) { +function runTest(key: string | string[], mapping: IndexMapping | SavedObjectsFieldMapping) { expect(typeof key === 'string' || Array.isArray(key)).toBeTruthy(); expect(typeof mapping).toBe('object'); diff --git a/src/core/server/saved_objects/mappings/lib/get_property.ts b/src/core/server/saved_objects/mappings/lib/get_property.ts index 4e9ff10fc5d47..a31c9fe0c3ba1 100644 --- a/src/core/server/saved_objects/mappings/lib/get_property.ts +++ b/src/core/server/saved_objects/mappings/lib/get_property.ts @@ -18,15 +18,15 @@ */ import toPath from 'lodash/internal/toPath'; -import { CoreFieldMapping, FieldMapping, IndexMapping } from '../types'; +import { SavedObjectsCoreFieldMapping, SavedObjectsFieldMapping, IndexMapping } from '../types'; function getPropertyMappingFromObjectMapping( - mapping: IndexMapping | FieldMapping, + mapping: IndexMapping | SavedObjectsFieldMapping, path: string[] -): FieldMapping | undefined { +): SavedObjectsFieldMapping | undefined { const props = (mapping && (mapping as IndexMapping).properties) || - (mapping && (mapping as CoreFieldMapping).fields); + (mapping && (mapping as SavedObjectsCoreFieldMapping).fields); if (!props) { return undefined; @@ -39,6 +39,9 @@ function getPropertyMappingFromObjectMapping( } } -export function getProperty(mappings: IndexMapping | FieldMapping, path: string | string[]) { +export function getProperty( + mappings: IndexMapping | SavedObjectsFieldMapping, + path: string | string[] +) { return getPropertyMappingFromObjectMapping(mappings, toPath(path)); } diff --git a/src/core/server/saved_objects/mappings/lib/get_root_properties_objects.ts b/src/core/server/saved_objects/mappings/lib/get_root_properties_objects.ts index 61e4d752445c4..81ba1d8235561 100644 --- a/src/core/server/saved_objects/mappings/lib/get_root_properties_objects.ts +++ b/src/core/server/saved_objects/mappings/lib/get_root_properties_objects.ts @@ -17,7 +17,11 @@ * under the License. */ -import { ComplexFieldMapping, IndexMapping, MappingProperties } from '../types'; +import { + SavedObjectsComplexFieldMapping, + IndexMapping, + SavedObjectsMappingProperties, +} from '../types'; import { getRootProperties } from './get_root_properties'; /** @@ -43,10 +47,10 @@ export function getRootPropertiesObjects(mappings: IndexMapping) { // we consider the existence of the properties or type of object to designate that this is an object datatype if ( !blacklist.includes(key) && - ((value as ComplexFieldMapping).properties || value.type === 'object') + ((value as SavedObjectsComplexFieldMapping).properties || value.type === 'object') ) { acc[key] = value; } return acc; - }, {} as MappingProperties); + }, {} as SavedObjectsMappingProperties); } diff --git a/src/core/server/saved_objects/mappings/types.ts b/src/core/server/saved_objects/mappings/types.ts index 8bb1a69d2eb13..578fdcea3718e 100644 --- a/src/core/server/saved_objects/mappings/types.ts +++ b/src/core/server/saved_objects/mappings/types.ts @@ -17,47 +17,133 @@ * under the License. */ -// FieldMapping isn't 1:1 with the options available, -// modify as needed. -export interface CoreFieldMapping { - type: string; - fields?: { - [subfield: string]: { - type: string; - }; - }; +/** + * Describe a saved object type mapping. + * + * @example + * ```ts + * const typeDefinition: SavedObjectsTypeMappingDefinition = { + * properties: { + * enabled: { + * type: "boolean" + * }, + * sendUsageFrom: { + * ignore_above: 256, + * type: "keyword" + * }, + * lastReported: { + * type: "date" + * }, + * lastVersionChecked: { + * ignore_above: 256, + * type: "keyword" + * }, + * } + * } + * ``` + * + * @public + */ +export interface SavedObjectsTypeMappingDefinition { + properties: SavedObjectsMappingProperties; } -// FieldMapping isn't 1:1 with the options available, -// modify as needed. -export interface ComplexFieldMapping { - dynamic?: string; - type?: string; - properties: MappingProperties; -} +/** + * A map of {@link SavedObjectsTypeMappingDefinition | saved object type mappings} + * + * @example + * ```ts + * const mappings: SavedObjectsTypeMappingDefinitions = { + * someType: { + * properties: { + * enabled: { + * type: "boolean" + * }, + * field: { + * type: "keyword" + * }, + * }, + * }, + * anotherType: { + * properties: { + * enabled: { + * type: "boolean" + * }, + * lastReported: { + * type: "date" + * }, + * }, + * }, -export type FieldMapping = CoreFieldMapping | ComplexFieldMapping; + * } + * ``` + * @remark This is the format for the legacy `mappings.json` savedObject mapping file. + * + * @internal + */ +export interface SavedObjectsTypeMappingDefinitions { + [type: string]: SavedObjectsTypeMappingDefinition; +} -export interface MappingProperties { - [field: string]: FieldMapping; +/** + * Describe the fields of a {@link SavedObjectsTypeMappingDefinition | saved object type}. + * + * @public + */ +export interface SavedObjectsMappingProperties { + [field: string]: SavedObjectsFieldMapping; } -export interface SavedObjectsMapping { - pluginId: string; - properties: MappingProperties; +/** + * Describe a {@link SavedObjectsTypeMappingDefinition | saved object type mapping} field. + * + * Please refer to {@link https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-types.html | elasticsearch documentation} + * For the mapping documentation + * + * @public + */ +export type SavedObjectsFieldMapping = + | SavedObjectsCoreFieldMapping + | SavedObjectsComplexFieldMapping; + +/** @internal */ +export interface IndexMapping { + dynamic?: string; + properties: SavedObjectsMappingProperties; + _meta?: IndexMappingMeta; } -export interface MappingMeta { +/** @internal */ +export interface IndexMappingMeta { // A dictionary of key -> md5 hash (e.g. 'dashboard': '24234qdfa3aefa3wa') // with each key being a root-level mapping property, and each value being // the md5 hash of that mapping's value when the index was created. migrationMappingPropertyHashes?: { [k: string]: string }; } -// IndexMapping isn't 1:1 with the options available, -// modify as needed. -export interface IndexMapping { +/** + * See {@link SavedObjectsFieldMapping} for documentation. + * + * @public + */ +export interface SavedObjectsCoreFieldMapping { + type: string; + index?: boolean; + enabled?: boolean; + fields?: { + [subfield: string]: { + type: string; + }; + }; +} + +/** + * See {@link SavedObjectsFieldMapping} for documentation. + * + * @public + */ +export interface SavedObjectsComplexFieldMapping { dynamic?: string; - properties: MappingProperties; - _meta?: MappingMeta; + type?: string; + properties: SavedObjectsMappingProperties; } diff --git a/src/core/server/saved_objects/migrations/core/build_active_mappings.test.ts b/src/core/server/saved_objects/migrations/core/build_active_mappings.test.ts index 71f589f24369a..821a10353f8ec 100644 --- a/src/core/server/saved_objects/migrations/core/build_active_mappings.test.ts +++ b/src/core/server/saved_objects/migrations/core/build_active_mappings.test.ts @@ -27,21 +27,19 @@ describe('buildActiveMappings', () => { bbb: { type: 'long' }, }; - expect(buildActiveMappings({ properties })).toMatchSnapshot(); + expect(buildActiveMappings(properties)).toMatchSnapshot(); }); test('disallows duplicate mappings', () => { const properties = { type: { type: 'long' } }; - expect(() => buildActiveMappings({ properties })).toThrow( - /Cannot redefine core mapping \"type\"/ - ); + expect(() => buildActiveMappings(properties)).toThrow(/Cannot redefine core mapping \"type\"/); }); test('disallows mappings with leading underscore', () => { const properties = { _hm: { type: 'keyword' } }; - expect(() => buildActiveMappings({ properties })).toThrow( + expect(() => buildActiveMappings(properties)).toThrow( /Invalid mapping \"_hm\"\. Mappings cannot start with _/ ); }); @@ -53,7 +51,7 @@ describe('buildActiveMappings', () => { ccc: { fields: { b: { type: 'text' }, a: { type: 'text' } }, type: 'keyword' }, }; - const mappings = buildActiveMappings({ properties }); + const mappings = buildActiveMappings(properties); const hashes = mappings._meta!.migrationMappingPropertyHashes!; expect(hashes.aaa).toBeDefined(); diff --git a/src/core/server/saved_objects/migrations/core/build_active_mappings.ts b/src/core/server/saved_objects/migrations/core/build_active_mappings.ts index 2cf640cceea83..3afe8aae119d9 100644 --- a/src/core/server/saved_objects/migrations/core/build_active_mappings.ts +++ b/src/core/server/saved_objects/migrations/core/build_active_mappings.ts @@ -22,31 +22,31 @@ */ import crypto from 'crypto'; -import _ from 'lodash'; -import { IndexMapping, MappingProperties } from './../../mappings'; +import { cloneDeep, mapValues } from 'lodash'; +import { + IndexMapping, + SavedObjectsMappingProperties, + SavedObjectsTypeMappingDefinitions, +} from './../../mappings'; /** * Creates an index mapping with the core properties required by saved object * indices, as well as the specified additional properties. * - * @param {Opts} opts - * @prop {MappingDefinition} properties - The mapping's properties - * @returns {IndexMapping} + * @param typeDefinitions - the type definitions to build mapping from. */ -export function buildActiveMappings({ - properties, -}: { - properties: MappingProperties; -}): IndexMapping { +export function buildActiveMappings( + typeDefinitions: SavedObjectsTypeMappingDefinitions | SavedObjectsMappingProperties +): IndexMapping { const mapping = defaultMapping(); - properties = validateAndMerge(mapping.properties, properties); + const mergedProperties = validateAndMerge(mapping.properties, typeDefinitions); - return _.cloneDeep({ + return cloneDeep({ ...mapping, - properties, + properties: mergedProperties, _meta: { - migrationMappingPropertyHashes: md5Values(properties), + migrationMappingPropertyHashes: md5Values(mergedProperties), }, }); } @@ -113,7 +113,7 @@ function canonicalStringify(obj: any): string { // Convert an object's values to md5 hash strings function md5Values(obj: any) { - return _.mapValues(obj, md5Object); + return mapValues(obj, md5Object); } // If something exists in actual, but is missing in expected, we don't @@ -171,12 +171,14 @@ function defaultMapping(): IndexMapping { }; } -function validateAndMerge(dest: MappingProperties, source: MappingProperties) { +function validateAndMerge( + dest: SavedObjectsMappingProperties, + source: SavedObjectsTypeMappingDefinitions | SavedObjectsMappingProperties +) { Object.keys(source).forEach(k => { if (k.startsWith('_')) { throw new Error(`Invalid mapping "${k}". Mappings cannot start with _.`); } - if (dest.hasOwnProperty(k)) { throw new Error(`Cannot redefine core mapping "${k}".`); } diff --git a/src/core/server/saved_objects/migrations/core/build_index_map.test.ts b/src/core/server/saved_objects/migrations/core/build_index_map.test.ts index bdc96323238c0..44add4e977006 100644 --- a/src/core/server/saved_objects/migrations/core/build_index_map.test.ts +++ b/src/core/server/saved_objects/migrations/core/build_index_map.test.ts @@ -18,20 +18,30 @@ */ import { createIndexMap } from './build_index_map'; -import { ObjectToConfigAdapter } from '../../../config'; -import { SavedObjectsSchema } from '../../schema'; -import { LegacyConfig } from '../../../legacy'; +import { SavedObjectTypeRegistry } from '../../saved_objects_type_registry'; +import { SavedObjectsType } from '../../types'; -const config = (new ObjectToConfigAdapter({}) as unknown) as LegacyConfig; +const createRegistry = (...types: Array>) => { + const registry = new SavedObjectTypeRegistry(); + types.forEach(type => + registry.registerType({ + name: 'unknown', + namespaceAgnostic: false, + hidden: false, + mappings: { properties: {} }, + migrations: {}, + ...type, + }) + ); + return registry; +}; test('mappings without index pattern goes to default index', () => { const result = createIndexMap({ - config, kibanaIndexName: '.kibana', - schema: new SavedObjectsSchema({ - type1: { - isNamespaceAgnostic: false, - }, + registry: createRegistry({ + name: 'type1', + namespaceAgnostic: false, }), indexMap: { type1: { @@ -60,13 +70,11 @@ test('mappings without index pattern goes to default index', () => { test(`mappings with custom index pattern doesn't go to default index`, () => { const result = createIndexMap({ - config, kibanaIndexName: '.kibana', - schema: new SavedObjectsSchema({ - type1: { - isNamespaceAgnostic: false, - indexPattern: '.other_kibana', - }, + registry: createRegistry({ + name: 'type1', + namespaceAgnostic: false, + indexPattern: '.other_kibana', }), indexMap: { type1: { @@ -95,14 +103,12 @@ test(`mappings with custom index pattern doesn't go to default index`, () => { test('creating a script gets added to the index pattern', () => { const result = createIndexMap({ - config, kibanaIndexName: '.kibana', - schema: new SavedObjectsSchema({ - type1: { - isNamespaceAgnostic: false, - indexPattern: '.other_kibana', - convertToAliasScript: `ctx._id = ctx._source.type + ':' + ctx._id`, - }, + registry: createRegistry({ + name: 'type1', + namespaceAgnostic: false, + indexPattern: '.other_kibana', + convertToAliasScript: `ctx._id = ctx._source.type + ':' + ctx._id`, }), indexMap: { type1: { @@ -132,16 +138,19 @@ test('creating a script gets added to the index pattern', () => { test('throws when two scripts are defined for an index pattern', () => { const defaultIndex = '.kibana'; - const schema = new SavedObjectsSchema({ - type1: { - isNamespaceAgnostic: false, + const registry = createRegistry( + { + name: 'type1', + namespaceAgnostic: false, convertToAliasScript: `ctx._id = ctx._source.type + ':' + ctx._id`, }, - type2: { - isNamespaceAgnostic: false, + { + name: 'type2', + namespaceAgnostic: false, convertToAliasScript: `ctx._id = ctx._source.type + ':' + ctx._id`, - }, - }); + } + ); + const indexMap = { type1: { properties: { @@ -160,9 +169,8 @@ test('throws when two scripts are defined for an index pattern', () => { }; expect(() => createIndexMap({ - config, kibanaIndexName: defaultIndex, - schema, + registry, indexMap, }) ).toThrowErrorMatchingInlineSnapshot( diff --git a/src/core/server/saved_objects/migrations/core/build_index_map.ts b/src/core/server/saved_objects/migrations/core/build_index_map.ts index 914447563cc77..8f7fe2f8eac5b 100644 --- a/src/core/server/saved_objects/migrations/core/build_index_map.ts +++ b/src/core/server/saved_objects/migrations/core/build_index_map.ts @@ -17,39 +17,32 @@ * under the License. */ -import { MappingProperties } from '../../mappings'; -import { SavedObjectsSchema } from '../../schema'; -import { LegacyConfig } from '../../../legacy'; +import { SavedObjectsTypeMappingDefinitions } from '../../mappings'; +import { ISavedObjectTypeRegistry } from '../../saved_objects_type_registry'; export interface CreateIndexMapOptions { - config: LegacyConfig; kibanaIndexName: string; - schema: SavedObjectsSchema; - indexMap: MappingProperties; + registry: ISavedObjectTypeRegistry; + indexMap: SavedObjectsTypeMappingDefinitions; } export interface IndexMap { [index: string]: { - typeMappings: MappingProperties; + typeMappings: SavedObjectsTypeMappingDefinitions; script?: string; }; } /* - * This file contains logic to convert savedObjectSchemas into a dictonary of indexes and documents + * This file contains logic to convert savedObjectSchemas into a dictionary of indexes and documents */ -export function createIndexMap({ - /** @deprecated Remove once savedObjectsSchemas are exposed from Core */ - config, - kibanaIndexName, - schema, - indexMap, -}: CreateIndexMapOptions) { +export function createIndexMap({ kibanaIndexName, registry, indexMap }: CreateIndexMapOptions) { const map: IndexMap = {}; Object.keys(indexMap).forEach(type => { - const script = schema.getConvertToAliasScript(type); + const typeDef = registry.getType(type); + const script = typeDef?.convertToAliasScript; // Defaults to kibanaIndexName if indexPattern isn't defined - const indexPattern = schema.getIndexForType(config, type) || kibanaIndexName; + const indexPattern = typeDef?.indexPattern || kibanaIndexName; if (!map.hasOwnProperty(indexPattern as string)) { map[indexPattern] = { typeMappings: {} }; } diff --git a/src/core/server/saved_objects/migrations/core/document_migrator.test.ts b/src/core/server/saved_objects/migrations/core/document_migrator.test.ts index 38496a3503833..0e3a4780e12b6 100644 --- a/src/core/server/saved_objects/migrations/core/document_migrator.test.ts +++ b/src/core/server/saved_objects/migrations/core/document_migrator.test.ts @@ -18,41 +18,49 @@ */ import _ from 'lodash'; -import { RawSavedObjectDoc } from '../../serialization'; +import { SavedObjectUnsanitizedDoc } from '../../serialization'; import { DocumentMigrator } from './document_migrator'; import { loggingServiceMock } from '../../../logging/logging_service.mock'; +import { SavedObjectsType } from '../../types'; +import { SavedObjectTypeRegistry } from '../../saved_objects_type_registry'; const mockLoggerFactory = loggingServiceMock.create(); const mockLogger = mockLoggerFactory.get('mock logger'); +const createRegistry = (...types: Array>) => { + const registry = new SavedObjectTypeRegistry(); + types.forEach(type => + registry.registerType({ + name: 'unknown', + namespaceAgnostic: false, + hidden: false, + mappings: { properties: {} }, + migrations: {}, + ...type, + }) + ); + return registry; +}; + describe('DocumentMigrator', () => { function testOpts() { return { kibanaVersion: '25.2.3', - migrations: {}, + typeRegistry: createRegistry(), validateDoc: _.noop, log: mockLogger, }; } - it('validates the migration definition', () => { - const invalidDefinition: any = { - kibanaVersion: '3.2.3', - migrations: 'hello', - validateDoc: _.noop, - }; - expect(() => new DocumentMigrator(invalidDefinition)).toThrow( - /Migration definition should be an object/i - ); - }); - it('validates individual migration definitions', () => { - const invalidDefinition: any = { + const invalidDefinition = { kibanaVersion: '3.2.3', - migrations: { - foo: _.noop, - }, + typeRegistry: createRegistry({ + name: 'foo', + migrations: _.noop as any, + }), validateDoc: _.noop, + log: mockLogger, }; expect(() => new DocumentMigrator(invalidDefinition)).toThrow( /Migration for type foo should be an object/i @@ -60,14 +68,16 @@ describe('DocumentMigrator', () => { }); it('validates individual migration semvers', () => { - const invalidDefinition: any = { + const invalidDefinition = { kibanaVersion: '3.2.3', - migrations: { - foo: { - bar: _.noop, + typeRegistry: createRegistry({ + name: 'foo', + migrations: { + bar: doc => doc, }, - }, + }), validateDoc: _.noop, + log: mockLogger, }; expect(() => new DocumentMigrator(invalidDefinition)).toThrow( /Expected all properties to be semvers/i @@ -75,14 +85,16 @@ describe('DocumentMigrator', () => { }); it('validates the migration function', () => { - const invalidDefinition: any = { + const invalidDefinition = { kibanaVersion: '3.2.3', - migrations: { - foo: { - '1.2.3': 23, + typeRegistry: createRegistry({ + name: 'foo', + migrations: { + '1.2.3': 23 as any, }, - }, + }), validateDoc: _.noop, + log: mockLogger, }; expect(() => new DocumentMigrator(invalidDefinition)).toThrow( /expected a function, but got 23/i @@ -92,11 +104,12 @@ describe('DocumentMigrator', () => { it('migrates type and attributes', () => { const migrator = new DocumentMigrator({ ...testOpts(), - migrations: { - user: { + typeRegistry: createRegistry({ + name: 'user', + migrations: { '1.2.3': setAttr('attributes.name', 'Chris'), }, - }, + }), }); const actual = migrator.migrate({ id: 'me', @@ -115,14 +128,15 @@ describe('DocumentMigrator', () => { it(`doesn't mutate the original document`, () => { const migrator = new DocumentMigrator({ ...testOpts(), - migrations: { - user: { - '1.2.3': (doc: RawSavedObjectDoc) => { + typeRegistry: createRegistry({ + name: 'user', + migrations: { + '1.2.3': doc => { _.set(doc, 'attributes.name', 'Mike'); return doc; }, }, - }, + }), }); const originalDoc = { id: 'me', @@ -138,11 +152,12 @@ describe('DocumentMigrator', () => { it('migrates meta properties', () => { const migrator = new DocumentMigrator({ ...testOpts(), - migrations: { - acl: { + typeRegistry: createRegistry({ + name: 'acl', + migrations: { '2.3.5': setAttr('acl', 'admins-only,sucka!'), }, - }, + }), }); const actual = migrator.migrate({ id: 'me', @@ -163,11 +178,26 @@ describe('DocumentMigrator', () => { it('does not apply migrations to unrelated docs', () => { const migrator = new DocumentMigrator({ ...testOpts(), - migrations: { - aaa: { '1.0.0': setAttr('aaa', 'A') }, - bbb: { '1.0.0': setAttr('bbb', 'B') }, - ccc: { '1.0.0': setAttr('ccc', 'C') }, - }, + typeRegistry: createRegistry( + { + name: 'aaa', + migrations: { + '1.0.0': setAttr('aaa', 'A'), + }, + }, + { + name: 'bbb', + migrations: { + '1.0.0': setAttr('bbb', 'B'), + }, + }, + { + name: 'ccc', + migrations: { + '1.0.0': setAttr('ccc', 'C'), + }, + } + ), }); const actual = migrator.migrate({ id: 'me', @@ -185,11 +215,26 @@ describe('DocumentMigrator', () => { it('assumes documents w/ undefined migrationVersion are up to date', () => { const migrator = new DocumentMigrator({ ...testOpts(), - migrations: { - user: { '1.0.0': setAttr('aaa', 'A') }, - bbb: { '2.3.4': setAttr('bbb', 'B') }, - ccc: { '1.0.0': setAttr('ccc', 'C') }, - }, + typeRegistry: createRegistry( + { + name: 'user', + migrations: { + '1.0.0': setAttr('aaa', 'A'), + }, + }, + { + name: 'bbb', + migrations: { + '2.3.4': setAttr('bbb', 'B'), + }, + }, + { + name: 'ccc', + migrations: { + '1.0.0': setAttr('ccc', 'C'), + }, + } + ), }); const actual = migrator.migrate({ id: 'me', @@ -212,13 +257,14 @@ describe('DocumentMigrator', () => { it('only applies migrations that are more recent than the doc', () => { const migrator = new DocumentMigrator({ ...testOpts(), - migrations: { - dog: { + typeRegistry: createRegistry({ + name: 'dog', + migrations: { '1.2.3': setAttr('attributes.a', 'A'), '1.2.4': setAttr('attributes.b', 'B'), '2.0.1': setAttr('attributes.c', 'C'), }, - }, + }), }); const actual = migrator.migrate({ id: 'smelly', @@ -254,11 +300,12 @@ describe('DocumentMigrator', () => { it('rejects docs that belong to a newer plugin', () => { const migrator = new DocumentMigrator({ ...testOpts(), - migrations: { - dawg: { + typeRegistry: createRegistry({ + name: 'dawg', + migrations: { '1.2.3': setAttr('attributes.a', 'A'), }, - }, + }), }); expect(() => migrator.migrate({ @@ -276,13 +323,14 @@ describe('DocumentMigrator', () => { let count = 0; const migrator = new DocumentMigrator({ ...testOpts(), - migrations: { - dog: { + typeRegistry: createRegistry({ + name: 'dog', + migrations: { '2.2.4': setAttr('attributes.b', () => ++count), '10.0.1': setAttr('attributes.c', () => ++count), '1.2.3': setAttr('attributes.a', () => ++count), }, - }, + }), }); const actual = migrator.migrate({ id: 'smelly', @@ -301,14 +349,20 @@ describe('DocumentMigrator', () => { it('allows props to be added', () => { const migrator = new DocumentMigrator({ ...testOpts(), - migrations: { - animal: { - '1.0.0': setAttr('animal', (name: string) => `Animal: ${name}`), - }, - dog: { - '2.2.4': setAttr('animal', 'Doggie'), + typeRegistry: createRegistry( + { + name: 'animal', + migrations: { + '1.0.0': setAttr('animal', (name: string) => `Animal: ${name}`), + }, }, - }, + { + name: 'dog', + migrations: { + '2.2.4': setAttr('animal', 'Doggie'), + }, + } + ), }); const actual = migrator.migrate({ id: 'smelly', @@ -328,16 +382,22 @@ describe('DocumentMigrator', () => { it('allows props to be renamed', () => { const migrator = new DocumentMigrator({ ...testOpts(), - migrations: { - animal: { - '1.0.0': setAttr('animal', (name: string) => `Animal: ${name}`), - '3.2.1': renameAttr('animal', 'dawg'), - }, - dawg: { - '2.2.4': renameAttr('dawg', 'animal'), - '3.2.0': setAttr('dawg', (name: string) => `Dawg3.x: ${name}`), + typeRegistry: createRegistry( + { + name: 'animal', + migrations: { + '1.0.0': setAttr('animal', (name: string) => `Animal: ${name}`), + '3.2.1': renameAttr('animal', 'dawg'), + }, }, - }, + { + name: 'dawg', + migrations: { + '2.2.4': renameAttr('dawg', 'animal'), + '3.2.0': setAttr('dawg', (name: string) => `Dawg3.x: ${name}`), + }, + } + ), }); const actual = migrator.migrate({ id: 'smelly', @@ -358,14 +418,20 @@ describe('DocumentMigrator', () => { it('allows changing type', () => { const migrator = new DocumentMigrator({ ...testOpts(), - migrations: { - cat: { - '1.0.0': setAttr('attributes.name', (name: string) => `Kitty ${name}`), - }, - dog: { - '2.2.4': setAttr('type', 'cat'), + typeRegistry: createRegistry( + { + name: 'cat', + migrations: { + '1.0.0': setAttr('attributes.name', (name: string) => `Kitty ${name}`), + }, }, - }, + { + name: 'dog', + migrations: { + '2.2.4': setAttr('type', 'cat'), + }, + } + ), }); const actual = migrator.migrate({ id: 'smelly', @@ -384,11 +450,12 @@ describe('DocumentMigrator', () => { it('disallows updating a migrationVersion prop to a lower version', () => { const migrator = new DocumentMigrator({ ...testOpts(), - migrations: { - cat: { + typeRegistry: createRegistry({ + name: 'cat', + migrations: { '1.0.0': setAttr('migrationVersion.foo', '3.2.1'), }, - }, + }), }); expect(() => @@ -406,11 +473,12 @@ describe('DocumentMigrator', () => { it('disallows removing a migrationVersion prop', () => { const migrator = new DocumentMigrator({ ...testOpts(), - migrations: { - cat: { + typeRegistry: createRegistry({ + name: 'cat', + migrations: { '1.0.0': setAttr('migrationVersion', {}), }, - }, + }), }); expect(() => migrator.migrate({ @@ -427,8 +495,9 @@ describe('DocumentMigrator', () => { it('allows updating a migrationVersion prop to a later version', () => { const migrator = new DocumentMigrator({ ...testOpts(), - migrations: { - cat: { + typeRegistry: createRegistry({ + name: 'cat', + migrations: { '1.0.0': setAttr('migrationVersion.cat', '2.9.1'), '2.0.0': () => { throw new Error('POW!'); @@ -438,7 +507,7 @@ describe('DocumentMigrator', () => { }, '3.0.0': setAttr('attributes.name', 'Shiny'), }, - }, + }), }); const actual = migrator.migrate({ id: 'smelly', @@ -457,11 +526,12 @@ describe('DocumentMigrator', () => { it('allows adding props to migrationVersion', () => { const migrator = new DocumentMigrator({ ...testOpts(), - migrations: { - cat: { + typeRegistry: createRegistry({ + name: 'cat', + migrations: { '1.0.0': setAttr('migrationVersion.foo', '5.6.7'), }, - }, + }), }); const actual = migrator.migrate({ id: 'smelly', @@ -481,13 +551,14 @@ describe('DocumentMigrator', () => { const log = mockLogger; const migrator = new DocumentMigrator({ ...testOpts(), - migrations: { - dog: { + typeRegistry: createRegistry({ + name: 'dog', + migrations: { '1.2.3': () => { throw new Error('Dang diggity!'); }, }, - }, + }), log, }); const failedDoc = { @@ -511,15 +582,16 @@ describe('DocumentMigrator', () => { const logTestMsg = '...said the joker to the thief'; const migrator = new DocumentMigrator({ ...testOpts(), - migrations: { - dog: { + typeRegistry: createRegistry({ + name: 'dog', + migrations: { '1.2.3': (doc, log) => { log.info(logTestMsg); log.warning(logTestMsg); return doc; }, }, - }, + }), log: mockLogger, }); const doc = { @@ -536,17 +608,23 @@ describe('DocumentMigrator', () => { test('extracts the latest migration version info', () => { const { migrationVersion } = new DocumentMigrator({ ...testOpts(), - migrations: { - aaa: { - '1.2.3': (doc: RawSavedObjectDoc) => doc, - '10.4.0': (doc: RawSavedObjectDoc) => doc, - '2.2.1': (doc: RawSavedObjectDoc) => doc, - }, - bbb: { - '3.2.3': (doc: RawSavedObjectDoc) => doc, - '2.0.0': (doc: RawSavedObjectDoc) => doc, + typeRegistry: createRegistry( + { + name: 'aaa', + migrations: { + '1.2.3': (doc: SavedObjectUnsanitizedDoc) => doc, + '10.4.0': (doc: SavedObjectUnsanitizedDoc) => doc, + '2.2.1': (doc: SavedObjectUnsanitizedDoc) => doc, + }, }, - }, + { + name: 'bbb', + migrations: { + '3.2.3': (doc: SavedObjectUnsanitizedDoc) => doc, + '2.0.0': (doc: SavedObjectUnsanitizedDoc) => doc, + }, + } + ), }); expect(migrationVersion).toEqual({ @@ -558,11 +636,12 @@ describe('DocumentMigrator', () => { test('fails if the validate doc throws', () => { const migrator = new DocumentMigrator({ ...testOpts(), - migrations: { - aaa: { + typeRegistry: createRegistry({ + name: 'aaa', + migrations: { '2.3.4': d => _.set(d, 'attributes.counter', 42), }, - }, + }), validateDoc: d => { if ((d.attributes as any).counter === 42) { throw new Error('Meaningful!'); @@ -577,11 +656,15 @@ describe('DocumentMigrator', () => { }); function renameAttr(path: string, newPath: string) { - return (doc: RawSavedObjectDoc) => - _.omit(_.set(doc, newPath, _.get(doc, path)) as {}, path) as RawSavedObjectDoc; + return (doc: SavedObjectUnsanitizedDoc) => + _.omit(_.set(doc, newPath, _.get(doc, path)) as {}, path) as SavedObjectUnsanitizedDoc; } function setAttr(path: string, value: any) { - return (doc: RawSavedObjectDoc) => - _.set(doc, path, _.isFunction(value) ? value(_.get(doc, path)) : value) as RawSavedObjectDoc; + return (doc: SavedObjectUnsanitizedDoc) => + _.set( + doc, + path, + _.isFunction(value) ? value(_.get(doc, path)) : value + ) as SavedObjectUnsanitizedDoc; } diff --git a/src/core/server/saved_objects/migrations/core/document_migrator.ts b/src/core/server/saved_objects/migrations/core/document_migrator.ts index 563d978dcc1f1..b5019b2874bec 100644 --- a/src/core/server/saved_objects/migrations/core/document_migrator.ts +++ b/src/core/server/saved_objects/migrations/core/document_migrator.ts @@ -65,23 +65,19 @@ import _ from 'lodash'; import cloneDeep from 'lodash.clonedeep'; import Semver from 'semver'; import { Logger } from '../../../logging'; -import { RawSavedObjectDoc } from '../../serialization'; +import { SavedObjectUnsanitizedDoc } from '../../serialization'; import { SavedObjectsMigrationVersion } from '../../types'; -import { MigrationLogger, SavedObjectsMigrationLogger } from './migration_logger'; +import { MigrationLogger } from './migration_logger'; +import { ISavedObjectTypeRegistry } from '../../saved_objects_type_registry'; +import { SavedObjectMigrationFn } from '../types'; -export type TransformFn = (doc: RawSavedObjectDoc) => RawSavedObjectDoc; +export type TransformFn = (doc: SavedObjectUnsanitizedDoc) => SavedObjectUnsanitizedDoc; -type MigrationFn = (doc: RawSavedObjectDoc, log: SavedObjectsMigrationLogger) => RawSavedObjectDoc; +type ValidateDoc = (doc: SavedObjectUnsanitizedDoc) => void; -type ValidateDoc = (doc: RawSavedObjectDoc) => void; - -export interface MigrationDefinition { - [type: string]: { [version: string]: MigrationFn }; -} - -interface Opts { +interface DocumentMigratorOptions { kibanaVersion: string; - migrations: MigrationDefinition; + typeRegistry: ISavedObjectTypeRegistry; validateDoc: ValidateDoc; log: Logger; } @@ -114,22 +110,22 @@ export class DocumentMigrator implements VersionedTransformer { /** * Creates an instance of DocumentMigrator. * - * @param {Opts} opts + * @param {DocumentMigratorOptions} opts * @prop {string} kibanaVersion - The current version of Kibana - * @prop {MigrationDefinition} migrations - The migrations that will be used to migrate documents + * @prop {SavedObjectTypeRegistry} typeRegistry - The type registry to get type migrations from * @prop {ValidateDoc} validateDoc - A function which, given a document throws an error if it is * not up to date. This is used to ensure we don't let unmigrated documents slip through. * @prop {Logger} log - The migration logger * @memberof DocumentMigrator */ - constructor(opts: Opts) { - validateMigrationDefinition(opts.migrations); + constructor({ typeRegistry, kibanaVersion, log, validateDoc }: DocumentMigratorOptions) { + validateMigrationDefinition(typeRegistry); - this.migrations = buildActiveMigrations(opts.migrations, opts.log); + this.migrations = buildActiveMigrations(typeRegistry, log); this.transformDoc = buildDocumentTransform({ - kibanaVersion: opts.kibanaVersion, + kibanaVersion, migrations: this.migrations, - validateDoc: opts.validateDoc, + validateDoc, }); } @@ -147,11 +143,11 @@ export class DocumentMigrator implements VersionedTransformer { /** * Migrates a document to the latest version. * - * @param {RawSavedObjectDoc} doc - * @returns {RawSavedObjectDoc} + * @param {SavedObjectUnsanitizedDoc} doc + * @returns {SavedObjectUnsanitizedDoc} * @memberof DocumentMigrator */ - public migrate = (doc: RawSavedObjectDoc): RawSavedObjectDoc => { + public migrate = (doc: SavedObjectUnsanitizedDoc): SavedObjectUnsanitizedDoc => { // Clone the document to prevent accidental mutations on the original data // Ex: Importing sample data that is cached at import level, migrations would // execute on mutated data the second time. @@ -166,7 +162,7 @@ export class DocumentMigrator implements VersionedTransformer { * language. So, this is just to provide a little developer-friendly error messaging. Joi was * giving weird errors, so we're just doing manual validation. */ -function validateMigrationDefinition(migrations: MigrationDefinition) { +function validateMigrationDefinition(registry: ISavedObjectTypeRegistry) { function assertObject(obj: any, prefix: string) { if (!obj || typeof obj !== 'object') { throw new Error(`${prefix} Got ${obj}.`); @@ -187,17 +183,17 @@ function validateMigrationDefinition(migrations: MigrationDefinition) { } } - assertObject(migrations, 'Migration definition should be an object.'); - - Object.entries(migrations).forEach(([type, versions]: any) => { - assertObject( - versions, - `Migration for type ${type} should be an object like { '2.0.0': (doc) => doc }.` - ); - Object.entries(versions).forEach(([version, fn]) => { - assertValidSemver(version, type); - assertValidTransform(fn, version, type); - }); + registry.getAllTypes().forEach(type => { + if (type.migrations) { + assertObject( + type.migrations, + `Migration for type ${type.name} should be an object like { '2.0.0': (doc) => doc }.` + ); + Object.entries(type.migrations).forEach(([version, fn]) => { + assertValidSemver(version, type.name); + assertValidTransform(fn, version, type.name); + }); + } }); } @@ -207,19 +203,28 @@ function validateMigrationDefinition(migrations: MigrationDefinition) { * From: { type: { version: fn } } * To: { type: { latestVersion: string, transforms: [{ version: string, transform: fn }] } } */ -function buildActiveMigrations(migrations: MigrationDefinition, log: Logger): ActiveMigrations { - return _.mapValues(migrations, (versions, prop) => { - const transforms = Object.entries(versions) - .map(([version, transform]) => ({ - version, - transform: wrapWithTry(version, prop!, transform, log), - })) - .sort((a, b) => Semver.compare(a.version, b.version)); - return { - latestVersion: _.last(transforms).version, - transforms, - }; - }); +function buildActiveMigrations( + typeRegistry: ISavedObjectTypeRegistry, + log: Logger +): ActiveMigrations { + return typeRegistry + .getAllTypes() + .filter(type => type.migrations && Object.keys(type.migrations).length > 0) + .reduce((migrations, type) => { + const transforms = Object.entries(type.migrations!) + .map(([version, transform]) => ({ + version, + transform: wrapWithTry(version, type.name, transform, log), + })) + .sort((a, b) => Semver.compare(a.version, b.version)); + return { + ...migrations, + [type.name]: { + latestVersion: _.last(transforms).version, + transforms, + }, + }; + }, {} as ActiveMigrations); } /** @@ -234,7 +239,7 @@ function buildDocumentTransform({ migrations: ActiveMigrations; validateDoc: ValidateDoc; }): TransformFn { - return function transformAndValidate(doc: RawSavedObjectDoc) { + return function transformAndValidate(doc: SavedObjectUnsanitizedDoc) { const result = doc.migrationVersion ? applyMigrations(doc, migrations) : markAsUpToDate(doc, migrations); @@ -252,7 +257,7 @@ function buildDocumentTransform({ }; } -function applyMigrations(doc: RawSavedObjectDoc, migrations: ActiveMigrations) { +function applyMigrations(doc: SavedObjectUnsanitizedDoc, migrations: ActiveMigrations) { while (true) { const prop = nextUnmigratedProp(doc, migrations); if (!prop) { @@ -265,14 +270,14 @@ function applyMigrations(doc: RawSavedObjectDoc, migrations: ActiveMigrations) { /** * Gets the doc's props, handling the special case of "type". */ -function props(doc: RawSavedObjectDoc) { +function props(doc: SavedObjectUnsanitizedDoc) { return Object.keys(doc).concat(doc.type); } /** * Looks up the prop version in a saved object document or in our latest migrations. */ -function propVersion(doc: RawSavedObjectDoc | ActiveMigrations, prop: string) { +function propVersion(doc: SavedObjectUnsanitizedDoc | ActiveMigrations, prop: string) { return ( (doc[prop] && doc[prop].latestVersion) || (doc.migrationVersion && (doc as any).migrationVersion[prop]) @@ -282,7 +287,7 @@ function propVersion(doc: RawSavedObjectDoc | ActiveMigrations, prop: string) { /** * Sets the doc's migrationVersion to be the most recent version */ -function markAsUpToDate(doc: RawSavedObjectDoc, migrations: ActiveMigrations) { +function markAsUpToDate(doc: SavedObjectUnsanitizedDoc, migrations: ActiveMigrations) { return { ...doc, migrationVersion: props(doc).reduce((acc, prop) => { @@ -296,20 +301,25 @@ function markAsUpToDate(doc: RawSavedObjectDoc, migrations: ActiveMigrations) { * If a specific transform function fails, this tacks on a bit of information * about the document and transform that caused the failure. */ -function wrapWithTry(version: string, prop: string, transform: MigrationFn, log: Logger) { - return function tryTransformDoc(doc: RawSavedObjectDoc) { +function wrapWithTry( + version: string, + type: string, + migrationFn: SavedObjectMigrationFn, + log: Logger +) { + return function tryTransformDoc(doc: SavedObjectUnsanitizedDoc) { try { - const result = transform(doc, new MigrationLogger(log)); + const result = migrationFn(doc, new MigrationLogger(log)); // A basic sanity check to help migration authors detect basic errors // (e.g. forgetting to return the transformed doc) if (!result || !result.type) { - throw new Error(`Invalid saved object returned from migration ${prop}:${version}.`); + throw new Error(`Invalid saved object returned from migration ${type}:${version}.`); } return result; } catch (error) { - const failedTransform = `${prop}:${version}`; + const failedTransform = `${type}:${version}`; const failedDoc = JSON.stringify(doc); log.warn( `Failed to transform document ${doc}. Transform: ${failedTransform}\nDoc: ${failedDoc}` @@ -322,7 +332,7 @@ function wrapWithTry(version: string, prop: string, transform: MigrationFn, log: /** * Finds the first unmigrated property in the specified document. */ -function nextUnmigratedProp(doc: RawSavedObjectDoc, migrations: ActiveMigrations) { +function nextUnmigratedProp(doc: SavedObjectUnsanitizedDoc, migrations: ActiveMigrations) { return props(doc).find(p => { const latestVersion = propVersion(migrations, p); const docVersion = propVersion(doc, p); @@ -352,10 +362,10 @@ function nextUnmigratedProp(doc: RawSavedObjectDoc, migrations: ActiveMigrations * Applies any relevent migrations to the document for the specified property. */ function migrateProp( - doc: RawSavedObjectDoc, + doc: SavedObjectUnsanitizedDoc, prop: string, migrations: ActiveMigrations -): RawSavedObjectDoc { +): SavedObjectUnsanitizedDoc { const originalType = doc.type; let migrationVersion = _.clone(doc.migrationVersion) || {}; const typeChanged = () => !doc.hasOwnProperty(prop) || doc.type !== originalType; @@ -376,7 +386,11 @@ function migrateProp( /** * Retrieves any prop transforms that have not been applied to doc. */ -function applicableTransforms(migrations: ActiveMigrations, doc: RawSavedObjectDoc, prop: string) { +function applicableTransforms( + migrations: ActiveMigrations, + doc: SavedObjectUnsanitizedDoc, + prop: string +) { const minVersion = propVersion(doc, prop); const { transforms } = migrations[prop]; return minVersion @@ -389,7 +403,7 @@ function applicableTransforms(migrations: ActiveMigrations, doc: RawSavedObjectD * has not mutated migrationVersion in an unsupported way. */ function updateMigrationVersion( - doc: RawSavedObjectDoc, + doc: SavedObjectUnsanitizedDoc, migrationVersion: SavedObjectsMigrationVersion, prop: string, version: string @@ -405,7 +419,7 @@ function updateMigrationVersion( * as this could get us into an infinite loop. So, we explicitly check for that here. */ function assertNoDowngrades( - doc: RawSavedObjectDoc, + doc: SavedObjectUnsanitizedDoc, migrationVersion: SavedObjectsMigrationVersion, prop: string, version: string diff --git a/src/core/server/saved_objects/migrations/core/index_migrator.test.ts b/src/core/server/saved_objects/migrations/core/index_migrator.test.ts index 2fc65f6e475d8..a9d0a339c229f 100644 --- a/src/core/server/saved_objects/migrations/core/index_migrator.test.ts +++ b/src/core/server/saved_objects/migrations/core/index_migrator.test.ts @@ -18,8 +18,8 @@ */ import _ from 'lodash'; -import { SavedObjectsSchema } from '../../schema'; -import { RawSavedObjectDoc, SavedObjectsSerializer } from '../../serialization'; +import { SavedObjectUnsanitizedDoc, SavedObjectsSerializer } from '../../serialization'; +import { SavedObjectTypeRegistry } from '../../saved_objects_type_registry'; import { IndexMigrator } from './index_migrator'; import { loggingServiceMock } from '../../../logging/logging_service.mock'; @@ -39,7 +39,7 @@ describe('IndexMigrator', () => { migrationVersion: {}, migrate: _.identity, }, - serializer: new SavedObjectsSerializer(new SavedObjectsSchema()), + serializer: new SavedObjectsSerializer(new SavedObjectTypeRegistry()), }; }); @@ -254,7 +254,7 @@ describe('IndexMigrator', () => { test('transforms all docs from the original index', async () => { let count = 0; const { callCluster } = testOpts; - const migrateDoc = jest.fn((doc: RawSavedObjectDoc) => { + const migrateDoc = jest.fn((doc: SavedObjectUnsanitizedDoc) => { return { ...doc, attributes: { name: ++count }, diff --git a/src/core/server/saved_objects/migrations/core/migrate_raw_docs.test.ts b/src/core/server/saved_objects/migrations/core/migrate_raw_docs.test.ts index aa208a23954f0..89f3fde384848 100644 --- a/src/core/server/saved_objects/migrations/core/migrate_raw_docs.test.ts +++ b/src/core/server/saved_objects/migrations/core/migrate_raw_docs.test.ts @@ -18,17 +18,21 @@ */ import _ from 'lodash'; -import { SavedObjectsSchema } from '../../schema'; +import { SavedObjectTypeRegistry } from '../../saved_objects_type_registry'; import { SavedObjectsSerializer } from '../../serialization'; import { migrateRawDocs } from './migrate_raw_docs'; describe('migrateRawDocs', () => { test('converts raw docs to saved objects', async () => { const transform = jest.fn((doc: any) => _.set(doc, 'attributes.name', 'HOI!')); - const result = migrateRawDocs(new SavedObjectsSerializer(new SavedObjectsSchema()), transform, [ - { _id: 'a:b', _source: { type: 'a', a: { name: 'AAA' } } }, - { _id: 'c:d', _source: { type: 'c', c: { name: 'DDD' } } }, - ]); + const result = migrateRawDocs( + new SavedObjectsSerializer(new SavedObjectTypeRegistry()), + transform, + [ + { _id: 'a:b', _source: { type: 'a', a: { name: 'AAA' } } }, + { _id: 'c:d', _source: { type: 'c', c: { name: 'DDD' } } }, + ] + ); expect(result).toEqual([ { @@ -48,10 +52,14 @@ describe('migrateRawDocs', () => { const transform = jest.fn((doc: any) => _.set(_.cloneDeep(doc), 'attributes.name', 'TADA') ); - const result = migrateRawDocs(new SavedObjectsSerializer(new SavedObjectsSchema()), transform, [ - { _id: 'foo:b', _source: { type: 'a', a: { name: 'AAA' } } }, - { _id: 'c:d', _source: { type: 'c', c: { name: 'DDD' } } }, - ]); + const result = migrateRawDocs( + new SavedObjectsSerializer(new SavedObjectTypeRegistry()), + transform, + [ + { _id: 'foo:b', _source: { type: 'a', a: { name: 'AAA' } } }, + { _id: 'c:d', _source: { type: 'c', c: { name: 'DDD' } } }, + ] + ); expect(result).toEqual([ { _id: 'foo:b', _source: { type: 'a', a: { name: 'AAA' } } }, diff --git a/src/core/server/saved_objects/migrations/core/migrate_raw_docs.ts b/src/core/server/saved_objects/migrations/core/migrate_raw_docs.ts index c008b18619629..5fe15f40db8ec 100644 --- a/src/core/server/saved_objects/migrations/core/migrate_raw_docs.ts +++ b/src/core/server/saved_objects/migrations/core/migrate_raw_docs.ts @@ -21,7 +21,7 @@ * This file provides logic for migrating raw documents. */ -import { RawDoc, SavedObjectsSerializer } from '../../serialization'; +import { SavedObjectsRawDoc, SavedObjectsSerializer } from '../../serialization'; import { TransformFn } from './document_migrator'; /** @@ -29,14 +29,14 @@ import { TransformFn } from './document_migrator'; * of raw docs. Any raw docs that are not valid saved objects will simply be passed through. * * @param {TransformFn} migrateDoc - * @param {RawDoc[]} rawDocs - * @returns {RawDoc[]} + * @param {SavedObjectsRawDoc[]} rawDocs + * @returns {SavedObjectsRawDoc[]} */ export function migrateRawDocs( serializer: SavedObjectsSerializer, migrateDoc: TransformFn, - rawDocs: RawDoc[] -): RawDoc[] { + rawDocs: SavedObjectsRawDoc[] +): SavedObjectsRawDoc[] { return rawDocs.map(raw => { if (serializer.isRawSavedObject(raw)) { const savedObject = serializer.rawToSavedObject(raw); diff --git a/src/core/server/saved_objects/migrations/core/migration_context.ts b/src/core/server/saved_objects/migrations/core/migration_context.ts index d4e97ee6c5747..3a6145f5d9623 100644 --- a/src/core/server/saved_objects/migrations/core/migration_context.ts +++ b/src/core/server/saved_objects/migrations/core/migration_context.ts @@ -26,7 +26,7 @@ import { Logger } from 'src/core/server/logging'; import { SavedObjectsSerializer } from '../../serialization'; -import { MappingProperties } from '../../mappings'; +import { SavedObjectsTypeMappingDefinitions } from '../../mappings'; import { buildActiveMappings } from './build_active_mappings'; import { CallCluster } from './call_cluster'; import { VersionedTransformer } from './document_migrator'; @@ -40,7 +40,7 @@ export interface MigrationOpts { callCluster: CallCluster; index: string; log: Logger; - mappingProperties: MappingProperties; + mappingProperties: SavedObjectsTypeMappingDefinitions; documentMigrator: VersionedTransformer; serializer: SavedObjectsSerializer; convertToAliasScript?: string; @@ -107,9 +107,9 @@ function createSourceContext(source: FullIndexInfo, alias: string) { function createDestContext( source: FullIndexInfo, alias: string, - mappingProperties: MappingProperties + mappingProperties: SavedObjectsTypeMappingDefinitions ): FullIndexInfo { - const activeMappings = buildActiveMappings({ properties: mappingProperties }); + const activeMappings = buildActiveMappings(mappingProperties); return { aliases: {}, @@ -133,6 +133,5 @@ function createDestContext( function nextIndexName(indexName: string, alias: string) { const indexSuffix = (indexName.match(/[\d]+$/) || [])[0]; const indexNum = parseInt(indexSuffix, 10) || 0; - return `${alias}_${indexNum + 1}`; } diff --git a/src/core/server/saved_objects/migrations/core/migration_coordinator.test.ts b/src/core/server/saved_objects/migrations/core/migration_coordinator.test.ts index 818ca73b37189..800edaeaa5885 100644 --- a/src/core/server/saved_objects/migrations/core/migration_coordinator.test.ts +++ b/src/core/server/saved_objects/migrations/core/migration_coordinator.test.ts @@ -24,6 +24,7 @@ describe('coordinateMigration', () => { const log = { debug: jest.fn(), warning: jest.fn(), + warn: jest.fn(), info: jest.fn(), }; diff --git a/src/core/server/saved_objects/migrations/core/migration_logger.ts b/src/core/server/saved_objects/migrations/core/migration_logger.ts index 7c61d0c48d9bd..9dfb3abc8e72d 100644 --- a/src/core/server/saved_objects/migrations/core/migration_logger.ts +++ b/src/core/server/saved_objects/migrations/core/migration_logger.ts @@ -30,7 +30,11 @@ export type LogFn = (path: string[], message: string) => void; export interface SavedObjectsMigrationLogger { debug: (msg: string) => void; info: (msg: string) => void; + /** + * @deprecated Use `warn` instead. + */ warning: (msg: string) => void; + warn: (msg: string) => void; } export class MigrationLogger implements SavedObjectsMigrationLogger { @@ -43,4 +47,5 @@ export class MigrationLogger implements SavedObjectsMigrationLogger { public info = (msg: string) => this.logger.info(msg); public debug = (msg: string) => this.logger.debug(msg); public warning = (msg: string) => this.logger.warn(msg); + public warn = (msg: string) => this.logger.warn(msg); } diff --git a/src/core/server/saved_objects/migrations/index.ts b/src/core/server/saved_objects/migrations/index.ts index 6cb7ecac92ab9..e96986bd702e6 100644 --- a/src/core/server/saved_objects/migrations/index.ts +++ b/src/core/server/saved_objects/migrations/index.ts @@ -18,3 +18,4 @@ */ export { KibanaMigrator, IKibanaMigrator } from './kibana'; +export { SavedObjectMigrationFn, SavedObjectMigrationMap } from './types'; diff --git a/src/core/server/saved_objects/migrations/kibana/__snapshots__/kibana_migrator.test.ts.snap b/src/core/server/saved_objects/migrations/kibana/__snapshots__/kibana_migrator.test.ts.snap index 792b2063fc86d..37a73b11bbc48 100644 --- a/src/core/server/saved_objects/migrations/kibana/__snapshots__/kibana_migrator.test.ts.snap +++ b/src/core/server/saved_objects/migrations/kibana/__snapshots__/kibana_migrator.test.ts.snap @@ -4,8 +4,8 @@ exports[`KibanaMigrator getActiveMappings returns full index mappings w/ core pr Object { "_meta": Object { "migrationMappingPropertyHashes": Object { - "amap": "625b32086eb1d1203564cf85062dd22e", - "bmap": "625b32086eb1d1203564cf85062dd22e", + "amap": "510f1f0adb69830cf8a1c5ce2923ed82", + "bmap": "510f1f0adb69830cf8a1c5ce2923ed82", "config": "87aca8fdb053154f11383fce3dbf3edf", "migrationVersion": "4a1746014a75ade3a714e1db5763276f", "namespace": "2f4316de49999235636386fe51dc06c1", @@ -17,10 +17,18 @@ Object { "dynamic": "strict", "properties": Object { "amap": Object { - "type": "text", + "properties": Object { + "field": Object { + "type": "text", + }, + }, }, "bmap": Object { - "type": "text", + "properties": Object { + "field": Object { + "type": "text", + }, + }, }, "config": Object { "dynamic": "true", diff --git a/src/core/server/saved_objects/migrations/kibana/kibana_migrator.mock.ts b/src/core/server/saved_objects/migrations/kibana/kibana_migrator.mock.ts index c1021018538d5..2ee656721abd0 100644 --- a/src/core/server/saved_objects/migrations/kibana/kibana_migrator.mock.ts +++ b/src/core/server/saved_objects/migrations/kibana/kibana_migrator.mock.ts @@ -19,28 +19,29 @@ import { KibanaMigrator } from './kibana_migrator'; import { buildActiveMappings } from '../core'; -import { SavedObjectsMapping } from '../../mappings'; -const { mergeProperties } = jest.requireActual('./kibana_migrator'); +const { mergeTypes } = jest.requireActual('./kibana_migrator'); +import { SavedObjectsType } from '../../types'; -const defaultSavedObjectMappings = [ +const defaultSavedObjectTypes: SavedObjectsType[] = [ { - pluginId: 'testplugin', - properties: { - testtype: { - properties: { - name: { type: 'keyword' }, - }, + name: 'testtype', + hidden: false, + namespaceAgnostic: false, + mappings: { + properties: { + name: { type: 'keyword' }, }, }, + migrations: {}, }, ]; const createMigrator = ( { - savedObjectMappings, + types, }: { - savedObjectMappings: SavedObjectsMapping[]; - } = { savedObjectMappings: defaultSavedObjectMappings } + types: SavedObjectsType[]; + } = { types: defaultSavedObjectTypes } ) => { const mockMigrator: jest.Mocked> = { runMigrations: jest.fn(), @@ -48,9 +49,7 @@ const createMigrator = ( migrateDocument: jest.fn(), }; - mockMigrator.getActiveMappings.mockReturnValue( - buildActiveMappings({ properties: mergeProperties(savedObjectMappings) }) - ); + mockMigrator.getActiveMappings.mockReturnValue(buildActiveMappings(mergeTypes(types))); mockMigrator.migrateDocument.mockImplementation(doc => doc); return mockMigrator; }; diff --git a/src/core/server/saved_objects/migrations/kibana/kibana_migrator.test.ts b/src/core/server/saved_objects/migrations/kibana/kibana_migrator.test.ts index c6a72eb53d6c4..fd82bf282266e 100644 --- a/src/core/server/saved_objects/migrations/kibana/kibana_migrator.test.ts +++ b/src/core/server/saved_objects/migrations/kibana/kibana_migrator.test.ts @@ -17,45 +17,49 @@ * under the License. */ -import _ from 'lodash'; import { KibanaMigratorOptions, KibanaMigrator } from './kibana_migrator'; import { loggingServiceMock } from '../../../logging/logging_service.mock'; -import { SavedObjectsSchema } from '../../schema'; +import { SavedObjectTypeRegistry } from '../../saved_objects_type_registry'; +import { SavedObjectsType } from '../../types'; + +const createRegistry = (types: Array>) => { + const registry = new SavedObjectTypeRegistry(); + types.forEach(type => + registry.registerType({ + name: 'unknown', + namespaceAgnostic: false, + hidden: false, + mappings: { properties: {} }, + migrations: {}, + ...type, + }) + ); + return registry; +}; describe('KibanaMigrator', () => { describe('getActiveMappings', () => { it('returns full index mappings w/ core properties', () => { const options = mockOptions(); - options.savedObjectMappings = [ + options.typeRegistry = createRegistry([ { - pluginId: 'aaa', - properties: { amap: { type: 'text' } }, + name: 'amap', + mappings: { + properties: { field: { type: 'text' } }, + }, }, { - pluginId: 'bbb', - properties: { bmap: { type: 'text' } }, + name: 'bmap', + indexPattern: 'other-index', + mappings: { + properties: { field: { type: 'text' } }, + }, }, - ]; + ]); + const mappings = new KibanaMigrator(options).getActiveMappings(); expect(mappings).toMatchSnapshot(); }); - - it('Fails if duplicate mappings are defined', () => { - const options = mockOptions(); - options.savedObjectMappings = [ - { - pluginId: 'aaa', - properties: { amap: { type: 'text' } }, - }, - { - pluginId: 'bbb', - properties: { amap: { type: 'long' } }, - }, - ]; - expect(() => new KibanaMigrator(options).getActiveMappings()).toThrow( - /Plugin bbb is attempting to redefine mapping "amap"/ - ); - }); }); describe('runMigrations', () => { @@ -78,41 +82,37 @@ describe('KibanaMigrator', () => { }); }); -function mockOptions({ configValues }: { configValues?: any } = {}): KibanaMigratorOptions { +function mockOptions(): KibanaMigratorOptions { const callCluster = jest.fn(); return { logger: loggingServiceMock.create().get(), kibanaVersion: '8.2.3', savedObjectValidations: {}, - savedObjectMigrations: {}, - savedObjectMappings: [ + typeRegistry: createRegistry([ { - pluginId: 'testtype', - properties: { - testtype: { - properties: { - name: { type: 'keyword' }, - }, + name: 'testtype', + hidden: false, + namespaceAgnostic: false, + mappings: { + properties: { + name: { type: 'keyword' }, }, }, + migrations: {}, }, { - pluginId: 'testtype2', - properties: { - testtype2: { - properties: { - name: { type: 'keyword' }, - }, + name: 'testtype2', + hidden: false, + namespaceAgnostic: false, + indexPattern: 'other-index', + mappings: { + properties: { + name: { type: 'keyword' }, }, }, + migrations: {}, }, - ], - savedObjectSchemas: new SavedObjectsSchema({ - testtype2: { - isNamespaceAgnostic: false, - indexPattern: 'other-index', - }, - }), + ]), kibanaConfig: { enabled: true, index: '.my-index', @@ -123,15 +123,6 @@ function mockOptions({ configValues }: { configValues?: any } = {}): KibanaMigra scrollDuration: '10m', skip: false, }, - config: { - get: (name: string) => { - if (configValues && configValues[name]) { - return configValues[name]; - } else { - throw new Error(`Unexpected config ${name}`); - } - }, - } as KibanaMigratorOptions['config'], callCluster, }; } diff --git a/src/core/server/saved_objects/migrations/kibana/kibana_migrator.ts b/src/core/server/saved_objects/migrations/kibana/kibana_migrator.ts index 747b48a540109..9b4fe10a35100 100644 --- a/src/core/server/saved_objects/migrations/kibana/kibana_migrator.ts +++ b/src/core/server/saved_objects/migrations/kibana/kibana_migrator.ts @@ -24,30 +24,23 @@ import { Logger } from 'src/core/server/logging'; import { KibanaConfigType } from 'src/core/server/kibana_config'; -import { MappingProperties, SavedObjectsMapping, IndexMapping } from '../../mappings'; -import { SavedObjectsSchema } from '../../schema'; -import { RawSavedObjectDoc, SavedObjectsSerializer } from '../../serialization'; +import { IndexMapping, SavedObjectsTypeMappingDefinitions } from '../../mappings'; +import { SavedObjectUnsanitizedDoc, SavedObjectsSerializer } from '../../serialization'; import { docValidator, PropertyValidators } from '../../validation'; import { buildActiveMappings, CallCluster, IndexMigrator } from '../core'; -import { - DocumentMigrator, - VersionedTransformer, - MigrationDefinition, -} from '../core/document_migrator'; +import { DocumentMigrator, VersionedTransformer } from '../core/document_migrator'; import { createIndexMap } from '../core/build_index_map'; import { SavedObjectsConfigType } from '../../saved_objects_config'; -import { LegacyConfig } from '../../../legacy'; +import { ISavedObjectTypeRegistry } from '../../saved_objects_type_registry'; +import { SavedObjectsType } from '../../types'; export interface KibanaMigratorOptions { callCluster: CallCluster; - config: LegacyConfig; + typeRegistry: ISavedObjectTypeRegistry; savedObjectsConfig: SavedObjectsConfigType; kibanaConfig: KibanaConfigType; kibanaVersion: string; logger: Logger; - savedObjectMappings: SavedObjectsMapping[]; - savedObjectMigrations: MigrationDefinition; - savedObjectSchemas: SavedObjectsSchema; savedObjectValidations: PropertyValidators; } @@ -58,13 +51,12 @@ export type IKibanaMigrator = Pick; */ export class KibanaMigrator { private readonly callCluster: CallCluster; - private readonly config: LegacyConfig; private readonly savedObjectsConfig: SavedObjectsConfigType; private readonly documentMigrator: VersionedTransformer; private readonly kibanaConfig: KibanaConfigType; private readonly log: Logger; - private readonly mappingProperties: MappingProperties; - private readonly schema: SavedObjectsSchema; + private readonly mappingProperties: SavedObjectsTypeMappingDefinitions; + private readonly typeRegistry: ISavedObjectTypeRegistry; private readonly serializer: SavedObjectsSerializer; private migrationResult?: Promise>; @@ -73,27 +65,23 @@ export class KibanaMigrator { */ constructor({ callCluster, - config, + typeRegistry, kibanaConfig, savedObjectsConfig, + savedObjectValidations, kibanaVersion, logger, - savedObjectMappings, - savedObjectMigrations, - savedObjectSchemas, - savedObjectValidations, }: KibanaMigratorOptions) { - this.config = config; this.callCluster = callCluster; this.kibanaConfig = kibanaConfig; this.savedObjectsConfig = savedObjectsConfig; - this.schema = savedObjectSchemas; - this.serializer = new SavedObjectsSerializer(this.schema); - this.mappingProperties = mergeProperties(savedObjectMappings || []); + this.typeRegistry = typeRegistry; + this.serializer = new SavedObjectsSerializer(this.typeRegistry); + this.mappingProperties = mergeTypes(this.typeRegistry.getAllTypes()); this.log = logger; this.documentMigrator = new DocumentMigrator({ kibanaVersion, - migrations: savedObjectMigrations || {}, + typeRegistry, validateDoc: docValidator(savedObjectValidations || {}), log: this.log, }); @@ -118,10 +106,9 @@ export class KibanaMigrator { private runMigrationsInternal() { const kibanaIndexName = this.kibanaConfig.index; const indexMap = createIndexMap({ - config: this.config, kibanaIndexName, indexMap: this.mappingProperties, - schema: this.schema, + registry: this.typeRegistry, }); const migrators = Object.keys(indexMap).map(index => { @@ -150,7 +137,7 @@ export class KibanaMigrator { * */ public getActiveMappings(): IndexMapping { - return buildActiveMappings({ properties: this.mappingProperties }); + return buildActiveMappings(this.mappingProperties); } /** @@ -159,7 +146,7 @@ export class KibanaMigrator { * @param doc - The saved object to migrate * @returns `doc` with all registered migrations applied. */ - public migrateDocument(doc: RawSavedObjectDoc): RawSavedObjectDoc { + public migrateDocument(doc: SavedObjectUnsanitizedDoc): SavedObjectUnsanitizedDoc { return this.documentMigrator.migrate(doc); } } @@ -168,12 +155,15 @@ export class KibanaMigrator { * Merges savedObjectMappings properties into a single object, verifying that * no mappings are redefined. */ -export function mergeProperties(mappings: SavedObjectsMapping[]): MappingProperties { - return mappings.reduce((acc, { pluginId, properties }) => { - const duplicate = Object.keys(properties).find(k => acc.hasOwnProperty(k)); +export function mergeTypes(types: SavedObjectsType[]): SavedObjectsTypeMappingDefinitions { + return types.reduce((acc, { name: type, mappings }) => { + const duplicate = acc.hasOwnProperty(type); if (duplicate) { - throw new Error(`Plugin ${pluginId} is attempting to redefine mapping "${duplicate}".`); + throw new Error(`Type ${type} is already defined.`); } - return Object.assign(acc, properties); + return { + ...acc, + [type]: mappings, + }; }, {}); } diff --git a/src/core/server/saved_objects/migrations/types.ts b/src/core/server/saved_objects/migrations/types.ts new file mode 100644 index 0000000000000..01741dd2ded1a --- /dev/null +++ b/src/core/server/saved_objects/migrations/types.ts @@ -0,0 +1,51 @@ +/* + * 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 { SavedObjectUnsanitizedDoc } from '../serialization'; +import { SavedObjectsMigrationLogger } from './core/migration_logger'; + +/** + * A migration function defined for a {@link SavedObjectsType | saved objects type} + * used to migrate it's {@link SavedObjectUnsanitizedDoc | documents} + */ +export type SavedObjectMigrationFn = ( + doc: SavedObjectUnsanitizedDoc, + log: SavedObjectsMigrationLogger +) => SavedObjectUnsanitizedDoc; + +/** + * A map of {@link SavedObjectMigrationFn | migration functions} to be used for a given type. + * The map's keys must be valid semver versions. + * + * For a given document, only migrations with a higher version number than that of the document will be applied. + * Migrations are executed in order, starting from the lowest version and ending with the highest one. + * + * @example + * ```typescript + * const migrations: SavedObjectMigrationMap = { + * '1.0.0': migrateToV1, + * '2.1.0': migrateToV21 + * } + * ``` + * + * @public + */ +export interface SavedObjectMigrationMap { + [version: string]: SavedObjectMigrationFn; +} diff --git a/src/core/server/saved_objects/saved_objects_service.mock.ts b/src/core/server/saved_objects/saved_objects_service.mock.ts index a15d1f5b864b7..70f3d5a5b18e4 100644 --- a/src/core/server/saved_objects/saved_objects_service.mock.ts +++ b/src/core/server/saved_objects/saved_objects_service.mock.ts @@ -21,38 +21,60 @@ import { SavedObjectsService, InternalSavedObjectsServiceSetup, InternalSavedObjectsServiceStart, + SavedObjectsServiceSetup, + SavedObjectsServiceStart, } from './saved_objects_service'; import { mockKibanaMigrator } from './migrations/kibana/kibana_migrator.mock'; import { savedObjectsClientProviderMock } from './service/lib/scoped_client_provider.mock'; import { savedObjectsRepositoryMock } from './service/lib/repository.mock'; import { savedObjectsClientMock } from './service/saved_objects_client.mock'; +import { typeRegistryMock } from './saved_objects_type_registry.mock'; type SavedObjectsServiceContract = PublicMethodsOf; const createStartContractMock = () => { - const startContract: jest.Mocked = { - clientProvider: savedObjectsClientProviderMock.create(), + const startContrat: jest.Mocked = { getScopedClient: jest.fn(), createInternalRepository: jest.fn(), createScopedRepository: jest.fn(), - migrator: mockKibanaMigrator.create(), + createSerializer: jest.fn(), }; - startContract.getScopedClient.mockReturnValue(savedObjectsClientMock.create()); - startContract.createInternalRepository.mockReturnValue(savedObjectsRepositoryMock.create()); - startContract.createScopedRepository.mockReturnValue(savedObjectsRepositoryMock.create()); + startContrat.getScopedClient.mockReturnValue(savedObjectsClientMock.create()); + startContrat.createInternalRepository.mockReturnValue(savedObjectsRepositoryMock.create()); + startContrat.createScopedRepository.mockReturnValue(savedObjectsRepositoryMock.create()); + + return startContrat; +}; + +const createInternalStartContractMock = () => { + const internalStartContract: jest.Mocked = { + ...createStartContractMock(), + clientProvider: savedObjectsClientProviderMock.create(), + migrator: mockKibanaMigrator.create(), + typeRegistry: typeRegistryMock.create(), + }; - return startContract; + return internalStartContract; }; const createSetupContractMock = () => { - const setupContract: jest.Mocked = { + const setupContract: jest.Mocked = { setClientFactoryProvider: jest.fn(), addClientWrapper: jest.fn(), }; + return setupContract; }; +const createInternalSetupContractMock = () => { + const internalSetupContract: jest.Mocked = { + ...createSetupContractMock(), + registerType: jest.fn(), + }; + return internalSetupContract; +}; + const createSavedObjectsServiceMock = () => { const mocked: jest.Mocked = { setup: jest.fn(), @@ -60,14 +82,16 @@ const createSavedObjectsServiceMock = () => { stop: jest.fn(), }; - mocked.setup.mockResolvedValue(createSetupContractMock()); - mocked.start.mockResolvedValue(createStartContractMock()); + mocked.setup.mockResolvedValue(createInternalSetupContractMock()); + mocked.start.mockResolvedValue(createInternalStartContractMock()); mocked.stop.mockResolvedValue(); return mocked; }; export const savedObjectsServiceMock = { create: createSavedObjectsServiceMock, + createInternalSetupContract: createInternalSetupContractMock, createSetupContract: createSetupContractMock, + createInternalStartContract: createInternalStartContractMock, createStartContract: createStartContractMock, }; diff --git a/src/core/server/saved_objects/saved_objects_service.test.mocks.ts b/src/core/server/saved_objects/saved_objects_service.test.mocks.ts index a9ad0778d4757..8c1cd3411203a 100644 --- a/src/core/server/saved_objects/saved_objects_service.test.mocks.ts +++ b/src/core/server/saved_objects/saved_objects_service.test.mocks.ts @@ -19,6 +19,7 @@ import { mockKibanaMigrator } from './migrations/kibana/kibana_migrator.mock'; import { savedObjectsClientProviderMock } from './service/lib/scoped_client_provider.mock'; +import { typeRegistryMock } from './saved_objects_type_registry.mock'; export const migratorInstanceMock = mockKibanaMigrator.create(); export const KibanaMigratorMock = jest.fn().mockImplementation(() => migratorInstanceMock); @@ -30,3 +31,8 @@ export const clientProviderInstanceMock = savedObjectsClientProviderMock.create( jest.doMock('./service/lib/scoped_client_provider', () => ({ SavedObjectsClientProvider: jest.fn().mockImplementation(() => clientProviderInstanceMock), })); + +export const typeRegistryInstanceMock = typeRegistryMock.create(); +jest.doMock('./saved_objects_type_registry', () => ({ + SavedObjectTypeRegistry: jest.fn().mockImplementation(() => typeRegistryInstanceMock), +})); diff --git a/src/core/server/saved_objects/saved_objects_service.test.ts b/src/core/server/saved_objects/saved_objects_service.test.ts index 19798aa68928d..07e7d8db8b25c 100644 --- a/src/core/server/saved_objects/saved_objects_service.test.ts +++ b/src/core/server/saved_objects/saved_objects_service.test.ts @@ -21,6 +21,7 @@ import { KibanaMigratorMock, migratorInstanceMock, clientProviderInstanceMock, + typeRegistryInstanceMock, } from './saved_objects_service.test.mocks'; import { SavedObjectsService } from './saved_objects_service'; @@ -108,6 +109,25 @@ describe('SavedObjectsService', () => { ); }); }); + + describe('registerType', () => { + it('registers the type to the internal typeRegistry', async () => { + const coreContext = mockCoreContext.create(); + const soService = new SavedObjectsService(coreContext); + const setup = await soService.setup(createSetupDeps()); + + const type = { + name: 'someType', + hidden: false, + namespaceAgnostic: false, + mappings: { properties: {} }, + }; + setup.registerType(type); + + expect(typeRegistryInstanceMock.registerType).toHaveBeenCalledTimes(1); + expect(typeRegistryInstanceMock.registerType).toHaveBeenCalledWith(type); + }); + }); }); describe('#start()', () => { diff --git a/src/core/server/saved_objects/saved_objects_service.ts b/src/core/server/saved_objects/saved_objects_service.ts index 0c985c71c7e2f..2f07dbe51a09e 100644 --- a/src/core/server/saved_objects/saved_objects_service.ts +++ b/src/core/server/saved_objects/saved_objects_service.ts @@ -21,7 +21,6 @@ import { CoreService } from 'src/core/types'; import { first, filter, take } from 'rxjs/operators'; import { SavedObjectsClient, - SavedObjectsSchema, SavedObjectsClientProvider, ISavedObjectsClientProvider, SavedObjectsClientProviderOptions, @@ -34,17 +33,17 @@ import { KibanaConfigType } from '../kibana_config'; import { migrationsRetryCallCluster } from '../elasticsearch/retry_call_cluster'; import { SavedObjectsConfigType } from './saved_objects_config'; import { KibanaRequest } from '../http'; -import { SavedObjectsClientContract } from './types'; +import { SavedObjectsClientContract, SavedObjectsType } from './types'; import { ISavedObjectsRepository, SavedObjectsRepository } from './service/lib/repository'; import { SavedObjectsClientFactoryProvider, SavedObjectsClientWrapperFactory, } from './service/lib/scoped_client_provider'; import { Logger } from '../logging'; -import { SavedObjectsMapping } from './mappings'; -import { MigrationDefinition } from './migrations/core/document_migrator'; -import { SavedObjectsSchemaDefinition } from './schema'; +import { convertLegacyTypes } from './utils'; +import { SavedObjectTypeRegistry, ISavedObjectTypeRegistry } from './saved_objects_type_registry'; import { PropertyValidators } from './validation'; +import { SavedObjectsSerializer } from './serialization'; /** * Saved Objects is Kibana's data persistence mechanism allowing plugins to @@ -70,6 +69,7 @@ import { PropertyValidators } from './validation'; * constructor. * * @example + * ```ts * import { SavedObjectsClient, CoreSetup } from 'src/core/server'; * * export class Plugin() { @@ -79,6 +79,7 @@ import { PropertyValidators } from './validation'; * }) * } * } + * ``` * * @public */ @@ -102,7 +103,9 @@ export interface SavedObjectsServiceSetup { /** * @internal */ -export type InternalSavedObjectsServiceSetup = SavedObjectsServiceSetup; +export interface InternalSavedObjectsServiceSetup extends SavedObjectsServiceSetup { + registerType: (type: SavedObjectsType) => void; +} /** * Saved Objects is Kibana's data persisentence mechanism allowing plugins to @@ -146,6 +149,10 @@ export interface SavedObjectsServiceStart { * @param extraTypes - A list of additional hidden types the repository should have access to. */ createInternalRepository: (extraTypes?: string[]) => ISavedObjectsRepository; + /** + * Creates a {@link SavedObjectsSerializer | serializer} that is aware of all registered types. + */ + createSerializer: () => SavedObjectsSerializer; } export interface InternalSavedObjectsServiceStart extends SavedObjectsServiceStart { @@ -157,6 +164,10 @@ export interface InternalSavedObjectsServiceStart extends SavedObjectsServiceSta * @deprecated Exposed only for injecting into Legacy */ clientProvider: ISavedObjectsClientProvider; + /** + * @deprecated Exposed only for injecting into Legacy + */ + typeRegistry: ISavedObjectTypeRegistry; } /** @@ -207,9 +218,7 @@ export class SavedObjectsService private clientFactoryProvider?: SavedObjectsClientFactoryProvider; private clientFactoryWrappers: WrappedClientFactoryWrapper[] = []; - private mappings: SavedObjectsMapping[] = []; - private migrations: MigrationDefinition = {}; - private schemas: SavedObjectsSchemaDefinition = {}; + private typeRegistry = new SavedObjectTypeRegistry(); private validations: PropertyValidators = {}; constructor(private readonly coreContext: CoreContext) { @@ -221,16 +230,12 @@ export class SavedObjectsService this.setupDeps = setupDeps; - const { - savedObjectSchemas: savedObjectsSchemasDefinition, - savedObjectMappings, - savedObjectMigrations, - savedObjectValidations, - } = setupDeps.legacyPlugins.uiExports; - this.mappings = savedObjectMappings; - this.migrations = savedObjectMigrations; - this.schemas = savedObjectsSchemasDefinition; - this.validations = savedObjectValidations; + const legacyTypes = convertLegacyTypes( + setupDeps.legacyPlugins.uiExports, + setupDeps.legacyPlugins.pluginExtendedConfig + ); + legacyTypes.forEach(type => this.typeRegistry.registerType(type)); + this.validations = setupDeps.legacyPlugins.uiExports.savedObjectValidations || {}; return { setClientFactoryProvider: provider => { @@ -246,6 +251,9 @@ export class SavedObjectsService factory, }); }, + registerType: type => { + this.typeRegistry.registerType(type); + }, }; } @@ -303,8 +311,7 @@ export class SavedObjectsService const createRepository = (callCluster: APICaller, extraTypes: string[] = []) => { return SavedObjectsRepository.createRepository( migrator, - new SavedObjectsSchema(this.schemas), - this.setupDeps!.legacyPlugins.pluginExtendedConfig, + this.typeRegistry, kibanaConfig.index, callCluster, extraTypes @@ -335,9 +342,11 @@ export class SavedObjectsService return { migrator, clientProvider, + typeRegistry: this.typeRegistry, getScopedClient: clientProvider.getClient.bind(clientProvider), createScopedRepository: repositoryFactory.createScopedRepository, createInternalRepository: repositoryFactory.createInternalRepository, + createSerializer: () => new SavedObjectsSerializer(this.typeRegistry), }; } @@ -348,18 +357,14 @@ export class SavedObjectsService savedObjectsConfig: SavedObjectsConfigType, migrationsRetryDelay?: number ): KibanaMigrator { - const savedObjectSchemas = new SavedObjectsSchema(this.schemas); const adminClient = this.setupDeps!.elasticsearch.adminClient; return new KibanaMigrator({ - savedObjectSchemas, - savedObjectMappings: this.mappings, - savedObjectMigrations: this.migrations, - savedObjectValidations: this.validations, + typeRegistry: this.typeRegistry, logger: this.logger, kibanaVersion: this.coreContext.env.packageInfo.version, - config: this.setupDeps!.legacyPlugins.pluginExtendedConfig, savedObjectsConfig, + savedObjectValidations: this.validations, kibanaConfig, callCluster: migrationsRetryCallCluster( adminClient.callAsInternalUser, diff --git a/src/core/server/saved_objects/schema/schema.mock.ts b/src/core/server/saved_objects/saved_objects_type_registry.mock.ts similarity index 58% rename from src/core/server/saved_objects/schema/schema.mock.ts rename to src/core/server/saved_objects/saved_objects_type_registry.mock.ts index 89c318f087ab0..6e11920db6b7d 100644 --- a/src/core/server/saved_objects/schema/schema.mock.ts +++ b/src/core/server/saved_objects/saved_objects_type_registry.mock.ts @@ -17,19 +17,25 @@ * under the License. */ -import { SavedObjectsSchema } from './schema'; +import { ISavedObjectTypeRegistry } from './saved_objects_type_registry'; -type Schema = PublicMethodsOf; -const createSchemaMock = () => { - const mocked: jest.Mocked = { - getIndexForType: jest.fn().mockReturnValue('.kibana-test'), - isHiddenType: jest.fn().mockReturnValue(false), - isNamespaceAgnostic: jest.fn((type: string) => type === 'global'), - getConvertToAliasScript: jest.fn().mockReturnValue(undefined), +const createRegistryMock = (): jest.Mocked => { + const mock = { + registerType: jest.fn(), + getType: jest.fn(), + getAllTypes: jest.fn(), + isNamespaceAgnostic: jest.fn(), + isHidden: jest.fn(), + getIndex: jest.fn(), }; - return mocked; + + mock.getIndex.mockReturnValue('.kibana-test'); + mock.isHidden.mockReturnValue(false); + mock.isNamespaceAgnostic.mockImplementation((type: string) => type === 'global'); + + return mock; }; -export const schemaMock = { - create: createSchemaMock, +export const typeRegistryMock = { + create: createRegistryMock, }; diff --git a/src/core/server/saved_objects/saved_objects_type_registry.test.ts b/src/core/server/saved_objects/saved_objects_type_registry.test.ts new file mode 100644 index 0000000000000..4268ab7718f8d --- /dev/null +++ b/src/core/server/saved_objects/saved_objects_type_registry.test.ts @@ -0,0 +1,215 @@ +/* + * 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 { SavedObjectTypeRegistry } from './saved_objects_type_registry'; +import { SavedObjectsType } from './types'; + +const createType = (type: Partial): SavedObjectsType => ({ + name: 'unknown', + hidden: false, + namespaceAgnostic: false, + mappings: { properties: {} }, + migrations: {}, + ...type, +}); + +describe('SavedObjectTypeRegistry', () => { + let registry: SavedObjectTypeRegistry; + + beforeEach(() => { + registry = new SavedObjectTypeRegistry(); + }); + + it('allows to register types', () => { + registry.registerType(createType({ name: 'typeA' })); + registry.registerType(createType({ name: 'typeB' })); + registry.registerType(createType({ name: 'typeC' })); + + expect( + registry + .getAllTypes() + .map(type => type.name) + .sort() + ).toEqual(['typeA', 'typeB', 'typeC']); + }); + + it('throws when trying to register the same type twice', () => { + registry.registerType(createType({ name: 'typeA' })); + registry.registerType(createType({ name: 'typeB' })); + expect(() => { + registry.registerType(createType({ name: 'typeA' })); + }).toThrowErrorMatchingInlineSnapshot(`"Type 'typeA' is already registered"`); + }); + + describe('#getType', () => { + it(`retrieve a type by it's name`, () => { + const typeA = createType({ name: 'typeA' }); + const typeB = createType({ name: 'typeB' }); + registry.registerType(typeA); + registry.registerType(typeB); + registry.registerType(createType({ name: 'typeC' })); + + expect(registry.getType('typeA')).toEqual(typeA); + expect(registry.getType('typeB')).toEqual(typeB); + expect(registry.getType('unknownType')).toBeUndefined(); + }); + + it('forbids to mutate the registered types', () => { + registry.registerType( + createType({ + name: 'typeA', + mappings: { + properties: { + someField: { type: 'text' }, + }, + }, + }) + ); + + const typeA = registry.getType('typeA')!; + + expect(() => { + typeA.migrations = {}; + }).toThrow(); + expect(() => { + typeA.name = 'foo'; + }).toThrow(); + expect(() => { + typeA.mappings.properties = {}; + }).toThrow(); + expect(() => { + typeA.indexPattern = '.overrided'; + }).toThrow(); + }); + }); + + describe('#getAllTypes', () => { + it('returns all registered types', () => { + const typeA = createType({ name: 'typeA' }); + const typeB = createType({ name: 'typeB' }); + const typeC = createType({ name: 'typeC' }); + registry.registerType(typeA); + registry.registerType(typeB); + + const registered = registry.getAllTypes(); + expect(registered.length).toEqual(2); + expect(registered).toContainEqual(typeA); + expect(registered).toContainEqual(typeB); + expect(registered).not.toContainEqual(typeC); + }); + + it('forbids to mutate the registered types', () => { + registry.registerType( + createType({ + name: 'typeA', + mappings: { + properties: { + someField: { type: 'text' }, + }, + }, + }) + ); + registry.registerType( + createType({ + name: 'typeB', + migrations: { + '1.0.0': jest.fn(), + }, + }) + ); + + const typeA = registry.getType('typeA')!; + const typeB = registry.getType('typeB')!; + + expect(() => { + typeA.migrations = {}; + }).toThrow(); + expect(() => { + typeA.name = 'foo'; + }).toThrow(); + expect(() => { + typeB.mappings.properties = {}; + }).toThrow(); + expect(() => { + typeB.indexPattern = '.overrided'; + }).toThrow(); + }); + + it('does not mutate the registered types when altering the list', () => { + registry.registerType(createType({ name: 'typeA' })); + registry.registerType(createType({ name: 'typeB' })); + registry.registerType(createType({ name: 'typeC' })); + + const types = registry.getAllTypes(); + types.splice(0, 3); + + expect(registry.getAllTypes().length).toEqual(3); + }); + }); + + describe('#isNamespaceAgnostic', () => { + it('returns correct value for the type', () => { + registry.registerType(createType({ name: 'typeA', namespaceAgnostic: true })); + registry.registerType(createType({ name: 'typeB', namespaceAgnostic: false })); + + expect(registry.isNamespaceAgnostic('typeA')).toEqual(true); + expect(registry.isNamespaceAgnostic('typeB')).toEqual(false); + }); + it('returns false when the type is not registered', () => { + registry.registerType(createType({ name: 'typeA', namespaceAgnostic: true })); + registry.registerType(createType({ name: 'typeB', namespaceAgnostic: false })); + + expect(registry.isNamespaceAgnostic('unknownType')).toEqual(false); + }); + }); + + describe('#isHidden', () => { + it('returns correct value for the type', () => { + registry.registerType(createType({ name: 'typeA', hidden: true })); + registry.registerType(createType({ name: 'typeB', hidden: false })); + + expect(registry.isHidden('typeA')).toEqual(true); + expect(registry.isHidden('typeB')).toEqual(false); + }); + it('returns false when the type is not registered', () => { + registry.registerType(createType({ name: 'typeA', hidden: true })); + registry.registerType(createType({ name: 'typeB', hidden: false })); + + expect(registry.isHidden('unknownType')).toEqual(false); + }); + }); + + describe('#getIndex', () => { + it('returns correct value for the type', () => { + registry.registerType(createType({ name: 'typeA', indexPattern: '.custom-index' })); + registry.registerType(createType({ name: 'typeB', indexPattern: '.another-index' })); + registry.registerType(createType({ name: 'typeWithNoIndex' })); + + expect(registry.getIndex('typeA')).toEqual('.custom-index'); + expect(registry.getIndex('typeB')).toEqual('.another-index'); + expect(registry.getIndex('typeWithNoIndex')).toBeUndefined(); + }); + it('returns undefined when the type is not registered', () => { + registry.registerType(createType({ name: 'typeA', namespaceAgnostic: true })); + registry.registerType(createType({ name: 'typeB', namespaceAgnostic: false })); + + expect(registry.getIndex('unknownType')).toBeUndefined(); + }); + }); +}); diff --git a/src/core/server/saved_objects/saved_objects_type_registry.ts b/src/core/server/saved_objects/saved_objects_type_registry.ts new file mode 100644 index 0000000000000..3f26d696831fd --- /dev/null +++ b/src/core/server/saved_objects/saved_objects_type_registry.ts @@ -0,0 +1,86 @@ +/* + * 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 { deepFreeze } from '../../utils'; +import { SavedObjectsType } from './types'; + +/** + * See {@link SavedObjectTypeRegistry} for documentation. + * + * @internal + * */ +export type ISavedObjectTypeRegistry = PublicMethodsOf; + +/** + * Registry holding information about all the registered {@link SavedObjectsType | savedObject types}. + * + * @internal + */ +export class SavedObjectTypeRegistry { + private readonly types = new Map(); + + /** + * Register a {@link SavedObjectsType | type} inside the registry. + * A type can only be registered once. subsequent calls with the same type name will throw an error. + */ + public registerType(type: SavedObjectsType) { + if (this.types.has(type.name)) { + throw new Error(`Type '${type.name}' is already registered`); + } + this.types.set(type.name, deepFreeze(type)); + } + + /** + * Return the {@link SavedObjectsType | type} definition for given type name. + */ + public getType(type: string) { + return this.types.get(type); + } + + /** + * Return all {@link SavedObjectsType | types} currently registered. + */ + public getAllTypes() { + return [...this.types.values()]; + } + + /** + * Returns the `namespaceAgnostic` property for given type, or `false` if + * the type is not registered. + */ + public isNamespaceAgnostic(type: string) { + return this.types.get(type)?.namespaceAgnostic ?? false; + } + + /** + * Returns the `hidden` property for given type, or `false` if + * the type is not registered. + */ + public isHidden(type: string) { + return this.types.get(type)?.hidden ?? false; + } + + /** + * Returns the `indexPattern` property for given type, or `undefined` if + * the type is not registered. + */ + public getIndex(type: string) { + return this.types.get(type)?.indexPattern; + } +} diff --git a/src/core/server/saved_objects/schema/schema.ts b/src/core/server/saved_objects/schema/schema.ts index 00c4939233318..17ca406ea109a 100644 --- a/src/core/server/saved_objects/schema/schema.ts +++ b/src/core/server/saved_objects/schema/schema.ts @@ -19,6 +19,10 @@ import { LegacyConfig } from '../../legacy'; +/** + * @deprecated + * @internal + **/ interface SavedObjectsSchemaTypeDefinition { isNamespaceAgnostic: boolean; hidden?: boolean; @@ -26,12 +30,18 @@ interface SavedObjectsSchemaTypeDefinition { convertToAliasScript?: string; } -/** @internal */ +/** + * @deprecated + * @internal + **/ export interface SavedObjectsSchemaDefinition { - [key: string]: SavedObjectsSchemaTypeDefinition; + [type: string]: SavedObjectsSchemaTypeDefinition; } -/** @internal */ +/** + * @deprecated This is only used by the {@link SavedObjectsLegacyService | legacy savedObjects service} + * @internal + **/ export class SavedObjectsSchema { private readonly definition?: SavedObjectsSchemaDefinition; constructor(schemaDefinition?: SavedObjectsSchemaDefinition) { @@ -46,7 +56,6 @@ export class SavedObjectsSchema { return false; } - // TODO: Remove dependency on config when we move SavedObjectsSchema to NP public getIndexForType(config: LegacyConfig, type: string): string | undefined { if (this.definition != null && this.definition.hasOwnProperty(type)) { const { indexPattern } = this.definition[type]; diff --git a/src/core/server/saved_objects/serialization/index.ts b/src/core/server/saved_objects/serialization/index.ts index 217ffe7129e94..f7f4e75704341 100644 --- a/src/core/server/saved_objects/serialization/index.ts +++ b/src/core/server/saved_objects/serialization/index.ts @@ -22,167 +22,5 @@ * the raw document format as stored in ElasticSearch. */ -/* eslint-disable @typescript-eslint/camelcase */ - -import uuid from 'uuid'; -import { SavedObjectsSchema } from '../schema'; -import { decodeVersion, encodeVersion } from '../version'; -import { SavedObjectsMigrationVersion, SavedObjectReference } from '../types'; - -/** - * A raw document as represented directly in the saved object index. - */ -export interface RawDoc { - _id: string; - _source: any; - _type?: string; - _seq_no?: number; - _primary_term?: number; -} - -/** - * A saved object type definition that allows for miscellaneous, unknown - * properties, as current discussions around security, ACLs, etc indicate - * that future props are likely to be added. Migrations support this - * scenario out of the box. - */ -interface SavedObjectDoc { - attributes: object; - id?: string; // NOTE: SavedObjectDoc is used for uncreated objects where `id` is optional - type: string; - namespace?: string; - migrationVersion?: SavedObjectsMigrationVersion; - version?: string; - updated_at?: string; - - [rootProp: string]: any; -} - -interface Referencable { - references: SavedObjectReference[]; -} - -/** - * We want to have two types, one that guarantees a "references" attribute - * will exist and one that allows it to be null. Since we're not migrating - * all the saved objects to have a "references" array, we need to support - * the scenarios where it may be missing (ex migrations). - */ -export type RawSavedObjectDoc = SavedObjectDoc & Partial; -export type SanitizedSavedObjectDoc = SavedObjectDoc & Referencable; - -function assertNonEmptyString(value: string, name: string) { - if (!value || typeof value !== 'string') { - throw new TypeError(`Expected "${value}" to be a ${name}`); - } -} - -/** @internal */ -export class SavedObjectsSerializer { - private readonly schema: SavedObjectsSchema; - - constructor(schema: SavedObjectsSchema) { - this.schema = schema; - } - /** - * Determines whether or not the raw document can be converted to a saved object. - * - * @param {RawDoc} rawDoc - The raw ES document to be tested - */ - public isRawSavedObject(rawDoc: RawDoc) { - const { type, namespace } = rawDoc._source; - const namespacePrefix = - namespace && !this.schema.isNamespaceAgnostic(type) ? `${namespace}:` : ''; - return ( - type && - rawDoc._id.startsWith(`${namespacePrefix}${type}:`) && - rawDoc._source.hasOwnProperty(type) - ); - } - - /** - * Converts a document from the format that is stored in elasticsearch to the saved object client format. - * - * @param {RawDoc} rawDoc - The raw ES document to be converted to saved object format. - */ - public rawToSavedObject(doc: RawDoc): SanitizedSavedObjectDoc { - const { _id, _source, _seq_no, _primary_term } = doc; - const { type, namespace } = _source; - - const version = - _seq_no != null || _primary_term != null - ? encodeVersion(_seq_no!, _primary_term!) - : undefined; - - return { - type, - id: this.trimIdPrefix(namespace, type, _id), - ...(namespace && !this.schema.isNamespaceAgnostic(type) && { namespace }), - attributes: _source[type], - references: _source.references || [], - ...(_source.migrationVersion && { migrationVersion: _source.migrationVersion }), - ...(_source.updated_at && { updated_at: _source.updated_at }), - ...(version && { version }), - }; - } - - /** - * Converts a document from the saved object client format to the format that is stored in elasticsearch. - * - * @param {SanitizedSavedObjectDoc} savedObj - The saved object to be converted to raw ES format. - */ - public savedObjectToRaw(savedObj: SanitizedSavedObjectDoc): RawDoc { - const { - id, - type, - namespace, - attributes, - migrationVersion, - updated_at, - version, - references, - } = savedObj; - const source = { - [type]: attributes, - type, - references, - ...(namespace && !this.schema.isNamespaceAgnostic(type) && { namespace }), - ...(migrationVersion && { migrationVersion }), - ...(updated_at && { updated_at }), - }; - - return { - _id: this.generateRawId(namespace, type, id), - _source: source, - ...(version != null && decodeVersion(version)), - }; - } - - /** - * Given a saved object type and id, generates the compound id that is stored in the raw document. - * - * @param {string} namespace - The namespace of the saved object - * @param {string} type - The saved object type - * @param {string} id - The id of the saved object - */ - public generateRawId(namespace: string | undefined, type: string, id?: string) { - const namespacePrefix = - namespace && !this.schema.isNamespaceAgnostic(type) ? `${namespace}:` : ''; - return `${namespacePrefix}${type}:${id || uuid.v1()}`; - } - - private trimIdPrefix(namespace: string | undefined, type: string, id: string) { - assertNonEmptyString(id, 'document id'); - assertNonEmptyString(type, 'saved object type'); - - const namespacePrefix = - namespace && !this.schema.isNamespaceAgnostic(type) ? `${namespace}:` : ''; - const prefix = `${namespacePrefix}${type}:`; - - if (!id.startsWith(prefix)) { - return id; - } - - return id.slice(prefix.length); - } -} +export { SavedObjectUnsanitizedDoc, SavedObjectSanitizedDoc, SavedObjectsRawDoc } from './types'; +export { SavedObjectsSerializer } from './serializer'; diff --git a/src/core/server/saved_objects/serialization/serialization.test.ts b/src/core/server/saved_objects/serialization/serializer.test.ts similarity index 79% rename from src/core/server/saved_objects/serialization/serialization.test.ts rename to src/core/server/saved_objects/serialization/serializer.test.ts index e8bcdde73fcf4..8f09b25bb3908 100644 --- a/src/core/server/saved_objects/serialization/serialization.test.ts +++ b/src/core/server/saved_objects/serialization/serializer.test.ts @@ -18,14 +18,21 @@ */ import _ from 'lodash'; -import { SavedObjectsSerializer } from '.'; -import { SavedObjectsSchema } from '../schema'; +import { SavedObjectsSerializer } from './serializer'; +import { typeRegistryMock } from '../saved_objects_type_registry.mock'; import { encodeVersion } from '../version'; describe('saved object conversion', () => { + let typeRegistry: ReturnType; + + beforeEach(() => { + typeRegistry = typeRegistryMock.create(); + typeRegistry.isNamespaceAgnostic.mockReturnValue(false); + }); + describe('#rawToSavedObject', () => { test('it copies the _source.type property to type', () => { - const serializer = new SavedObjectsSerializer(new SavedObjectsSchema()); + const serializer = new SavedObjectsSerializer(typeRegistry); const actual = serializer.rawToSavedObject({ _id: 'foo:bar', _source: { @@ -36,7 +43,7 @@ describe('saved object conversion', () => { }); test('it copies the _source.references property to references', () => { - const serializer = new SavedObjectsSerializer(new SavedObjectsSchema()); + const serializer = new SavedObjectsSerializer(typeRegistry); const actual = serializer.rawToSavedObject({ _id: 'foo:bar', _source: { @@ -54,7 +61,7 @@ describe('saved object conversion', () => { }); test('if specified it copies the _source.migrationVersion property to migrationVersion', () => { - const serializer = new SavedObjectsSerializer(new SavedObjectsSchema()); + const serializer = new SavedObjectsSerializer(typeRegistry); const actual = serializer.rawToSavedObject({ _id: 'foo:bar', _source: { @@ -72,7 +79,7 @@ describe('saved object conversion', () => { }); test(`if _source.migrationVersion is unspecified it doesn't set migrationVersion`, () => { - const serializer = new SavedObjectsSerializer(new SavedObjectsSchema()); + const serializer = new SavedObjectsSerializer(typeRegistry); const actual = serializer.rawToSavedObject({ _id: 'foo:bar', _source: { @@ -83,8 +90,8 @@ describe('saved object conversion', () => { }); test('it converts the id and type properties, and retains migrationVersion', () => { - const now = new Date(); - const serializer = new SavedObjectsSerializer(new SavedObjectsSchema()); + const now = String(new Date()); + const serializer = new SavedObjectsSerializer(typeRegistry); const actual = serializer.rawToSavedObject({ _id: 'hello:world', _seq_no: 3, @@ -121,7 +128,7 @@ describe('saved object conversion', () => { }); test(`if version is unspecified it doesn't set version`, () => { - const serializer = new SavedObjectsSerializer(new SavedObjectsSchema()); + const serializer = new SavedObjectsSerializer(typeRegistry); const actual = serializer.rawToSavedObject({ _id: 'foo:bar', _source: { @@ -133,7 +140,7 @@ describe('saved object conversion', () => { }); test(`if specified it encodes _seq_no and _primary_term to version`, () => { - const serializer = new SavedObjectsSerializer(new SavedObjectsSchema()); + const serializer = new SavedObjectsSerializer(typeRegistry); const actual = serializer.rawToSavedObject({ _id: 'foo:bar', _seq_no: 4, @@ -147,7 +154,7 @@ describe('saved object conversion', () => { }); test(`if only _seq_no is specified it throws`, () => { - const serializer = new SavedObjectsSerializer(new SavedObjectsSchema()); + const serializer = new SavedObjectsSerializer(typeRegistry); expect(() => serializer.rawToSavedObject({ _id: 'foo:bar', @@ -161,7 +168,7 @@ describe('saved object conversion', () => { }); test(`if only _primary_term is throws`, () => { - const serializer = new SavedObjectsSerializer(new SavedObjectsSchema()); + const serializer = new SavedObjectsSerializer(typeRegistry); expect(() => serializer.rawToSavedObject({ _id: 'foo:bar', @@ -175,7 +182,7 @@ describe('saved object conversion', () => { }); test('if specified it copies the _source.updated_at property to updated_at', () => { - const serializer = new SavedObjectsSerializer(new SavedObjectsSchema()); + const serializer = new SavedObjectsSerializer(typeRegistry); const now = Date(); const actual = serializer.rawToSavedObject({ _id: 'foo:bar', @@ -188,7 +195,7 @@ describe('saved object conversion', () => { }); test(`if _source.updated_at is unspecified it doesn't set updated_at`, () => { - const serializer = new SavedObjectsSerializer(new SavedObjectsSchema()); + const serializer = new SavedObjectsSerializer(typeRegistry); const actual = serializer.rawToSavedObject({ _id: 'foo:bar', _source: { @@ -199,7 +206,7 @@ describe('saved object conversion', () => { }); test('it does not pass unknown properties through', () => { - const serializer = new SavedObjectsSerializer(new SavedObjectsSchema()); + const serializer = new SavedObjectsSerializer(typeRegistry); const actual = serializer.rawToSavedObject({ _id: 'universe', _source: { @@ -221,7 +228,7 @@ describe('saved object conversion', () => { }); test('it does not create attributes if [type] is missing', () => { - const serializer = new SavedObjectsSerializer(new SavedObjectsSchema()); + const serializer = new SavedObjectsSerializer(typeRegistry); const actual = serializer.rawToSavedObject({ _id: 'universe', _source: { @@ -236,7 +243,7 @@ describe('saved object conversion', () => { }); test('it fails for documents which do not specify a type', () => { - const serializer = new SavedObjectsSerializer(new SavedObjectsSchema()); + const serializer = new SavedObjectsSerializer(typeRegistry); expect(() => serializer.rawToSavedObject({ _id: 'universe', @@ -244,13 +251,13 @@ describe('saved object conversion', () => { hello: { world: 'earth', }, - }, + } as any, }) ).toThrow(/Expected "undefined" to be a saved object type/); }); test('it is complimentary with savedObjectToRaw', () => { - const serializer = new SavedObjectsSerializer(new SavedObjectsSchema()); + const serializer = new SavedObjectsSerializer(typeRegistry); const raw = { _id: 'foo-namespace:foo:bar', _primary_term: 24, @@ -266,7 +273,7 @@ describe('saved object conversion', () => { bar: '9.8.7', }, namespace: 'foo-namespace', - updated_at: new Date(), + updated_at: String(new Date()), references: [], }, }; @@ -277,7 +284,7 @@ describe('saved object conversion', () => { }); test('it handles unprefixed ids', () => { - const serializer = new SavedObjectsSerializer(new SavedObjectsSchema()); + const serializer = new SavedObjectsSerializer(typeRegistry); const actual = serializer.rawToSavedObject({ _id: 'universe', _source: { @@ -290,7 +297,7 @@ describe('saved object conversion', () => { describe('namespaced type without a namespace', () => { test(`removes type prefix from _id`, () => { - const serializer = new SavedObjectsSerializer(new SavedObjectsSchema()); + const serializer = new SavedObjectsSerializer(typeRegistry); const actual = serializer.rawToSavedObject({ _id: 'foo:bar', _source: { @@ -302,7 +309,7 @@ describe('saved object conversion', () => { }); test(`if prefixed by random prefix and type it copies _id to id`, () => { - const serializer = new SavedObjectsSerializer(new SavedObjectsSchema()); + const serializer = new SavedObjectsSerializer(typeRegistry); const actual = serializer.rawToSavedObject({ _id: 'random:foo:bar', _source: { @@ -314,7 +321,7 @@ describe('saved object conversion', () => { }); test(`doesn't specify namespace`, () => { - const serializer = new SavedObjectsSerializer(new SavedObjectsSchema()); + const serializer = new SavedObjectsSerializer(typeRegistry); const actual = serializer.rawToSavedObject({ _id: 'foo:bar', _source: { @@ -328,7 +335,7 @@ describe('saved object conversion', () => { describe('namespaced type with a namespace', () => { test(`removes type and namespace prefix from _id`, () => { - const serializer = new SavedObjectsSerializer(new SavedObjectsSchema()); + const serializer = new SavedObjectsSerializer(typeRegistry); const actual = serializer.rawToSavedObject({ _id: 'baz:foo:bar', _source: { @@ -341,7 +348,7 @@ describe('saved object conversion', () => { }); test(`if prefixed by only type it copies _id to id`, () => { - const serializer = new SavedObjectsSerializer(new SavedObjectsSchema()); + const serializer = new SavedObjectsSerializer(typeRegistry); const actual = serializer.rawToSavedObject({ _id: 'foo:bar', _source: { @@ -354,7 +361,7 @@ describe('saved object conversion', () => { }); test(`if prefixed by random prefix and type it copies _id to id`, () => { - const serializer = new SavedObjectsSerializer(new SavedObjectsSchema()); + const serializer = new SavedObjectsSerializer(typeRegistry); const actual = serializer.rawToSavedObject({ _id: 'random:foo:bar', _source: { @@ -367,7 +374,7 @@ describe('saved object conversion', () => { }); test(`copies _source.namespace to namespace`, () => { - const serializer = new SavedObjectsSerializer(new SavedObjectsSchema()); + const serializer = new SavedObjectsSerializer(typeRegistry); const actual = serializer.rawToSavedObject({ _id: 'baz:foo:bar', _source: { @@ -381,10 +388,12 @@ describe('saved object conversion', () => { }); describe('namespace agnostic type with a namespace', () => { + beforeEach(() => { + typeRegistry.isNamespaceAgnostic.mockReturnValue(true); + }); + test(`removes type prefix from _id`, () => { - const serializer = new SavedObjectsSerializer( - new SavedObjectsSchema({ foo: { isNamespaceAgnostic: true } }) - ); + const serializer = new SavedObjectsSerializer(typeRegistry); const actual = serializer.rawToSavedObject({ _id: 'foo:bar', _source: { @@ -397,9 +406,7 @@ describe('saved object conversion', () => { }); test(`if prefixed by namespace and type it copies _id to id`, () => { - const serializer = new SavedObjectsSerializer( - new SavedObjectsSchema({ foo: { isNamespaceAgnostic: true } }) - ); + const serializer = new SavedObjectsSerializer(typeRegistry); const actual = serializer.rawToSavedObject({ _id: 'baz:foo:bar', _source: { @@ -412,9 +419,7 @@ describe('saved object conversion', () => { }); test(`doesn't copy _source.namespace to namespace`, () => { - const serializer = new SavedObjectsSerializer( - new SavedObjectsSchema({ foo: { isNamespaceAgnostic: true } }) - ); + const serializer = new SavedObjectsSerializer(typeRegistry); const actual = serializer.rawToSavedObject({ _id: 'baz:foo:bar', _source: { @@ -430,7 +435,7 @@ describe('saved object conversion', () => { describe('#savedObjectToRaw', () => { test('it copies the type property to _source.type and uses the ROOT_TYPE as _type', () => { - const serializer = new SavedObjectsSerializer(new SavedObjectsSchema()); + const serializer = new SavedObjectsSerializer(typeRegistry); const actual = serializer.savedObjectToRaw({ type: 'foo', attributes: {}, @@ -440,7 +445,7 @@ describe('saved object conversion', () => { }); test('it copies the references property to _source.references', () => { - const serializer = new SavedObjectsSerializer(new SavedObjectsSchema()); + const serializer = new SavedObjectsSerializer(typeRegistry); const actual = serializer.savedObjectToRaw({ id: '1', type: 'foo', @@ -457,7 +462,7 @@ describe('saved object conversion', () => { }); test('if specified it copies the updated_at property to _source.updated_at', () => { - const serializer = new SavedObjectsSerializer(new SavedObjectsSchema()); + const serializer = new SavedObjectsSerializer(typeRegistry); const now = new Date(); const actual = serializer.savedObjectToRaw({ type: '', @@ -469,7 +474,7 @@ describe('saved object conversion', () => { }); test(`if unspecified it doesn't add updated_at property to _source`, () => { - const serializer = new SavedObjectsSerializer(new SavedObjectsSchema()); + const serializer = new SavedObjectsSerializer(typeRegistry); const actual = serializer.savedObjectToRaw({ type: '', attributes: {}, @@ -479,7 +484,7 @@ describe('saved object conversion', () => { }); test('it copies the migrationVersion property to _source.migrationVersion', () => { - const serializer = new SavedObjectsSerializer(new SavedObjectsSchema()); + const serializer = new SavedObjectsSerializer(typeRegistry); const actual = serializer.savedObjectToRaw({ type: '', attributes: {}, @@ -496,7 +501,7 @@ describe('saved object conversion', () => { }); test(`if unspecified it doesn't add migrationVersion property to _source`, () => { - const serializer = new SavedObjectsSerializer(new SavedObjectsSchema()); + const serializer = new SavedObjectsSerializer(typeRegistry); const actual = serializer.savedObjectToRaw({ type: '', attributes: {}, @@ -506,7 +511,7 @@ describe('saved object conversion', () => { }); test('it decodes the version property to _seq_no and _primary_term', () => { - const serializer = new SavedObjectsSerializer(new SavedObjectsSchema()); + const serializer = new SavedObjectsSerializer(typeRegistry); const actual = serializer.savedObjectToRaw({ type: '', attributes: {}, @@ -518,7 +523,7 @@ describe('saved object conversion', () => { }); test(`if unspecified it doesn't add _seq_no or _primary_term properties`, () => { - const serializer = new SavedObjectsSerializer(new SavedObjectsSchema()); + const serializer = new SavedObjectsSerializer(typeRegistry); const actual = serializer.savedObjectToRaw({ type: '', attributes: {}, @@ -529,7 +534,7 @@ describe('saved object conversion', () => { }); test(`if version invalid it throws`, () => { - const serializer = new SavedObjectsSerializer(new SavedObjectsSchema()); + const serializer = new SavedObjectsSerializer(typeRegistry); expect(() => serializer.savedObjectToRaw({ type: '', @@ -540,7 +545,7 @@ describe('saved object conversion', () => { }); test('it copies attributes to _source[type]', () => { - const serializer = new SavedObjectsSerializer(new SavedObjectsSchema()); + const serializer = new SavedObjectsSerializer(typeRegistry); const actual = serializer.savedObjectToRaw({ type: 'foo', attributes: { @@ -557,7 +562,7 @@ describe('saved object conversion', () => { describe('namespaced type without a namespace', () => { test('generates an id prefixed with type, if no id is specified', () => { - const serializer = new SavedObjectsSerializer(new SavedObjectsSchema()); + const serializer = new SavedObjectsSerializer(typeRegistry); const v1 = serializer.savedObjectToRaw({ type: 'foo', attributes: { @@ -577,7 +582,7 @@ describe('saved object conversion', () => { }); test(`doesn't specify _source.namespace`, () => { - const serializer = new SavedObjectsSerializer(new SavedObjectsSchema()); + const serializer = new SavedObjectsSerializer(typeRegistry); const actual = serializer.savedObjectToRaw({ type: '', attributes: {}, @@ -589,7 +594,7 @@ describe('saved object conversion', () => { describe('namespaced type with a namespace', () => { test('generates an id prefixed with namespace and type, if no id is specified', () => { - const serializer = new SavedObjectsSerializer(new SavedObjectsSchema()); + const serializer = new SavedObjectsSerializer(typeRegistry); const v1 = serializer.savedObjectToRaw({ type: 'foo', namespace: 'bar', @@ -611,7 +616,7 @@ describe('saved object conversion', () => { }); test(`it copies namespace to _source.namespace`, () => { - const serializer = new SavedObjectsSerializer(new SavedObjectsSchema()); + const serializer = new SavedObjectsSerializer(typeRegistry); const actual = serializer.savedObjectToRaw({ type: 'foo', attributes: {}, @@ -623,10 +628,12 @@ describe('saved object conversion', () => { }); describe('namespace agnostic type with a namespace', () => { + beforeEach(() => { + typeRegistry.isNamespaceAgnostic.mockReturnValue(true); + }); + test('generates an id prefixed with type, if no id is specified', () => { - const serializer = new SavedObjectsSerializer( - new SavedObjectsSchema({ foo: { isNamespaceAgnostic: true } }) - ); + const serializer = new SavedObjectsSerializer(typeRegistry); const v1 = serializer.savedObjectToRaw({ type: 'foo', namespace: 'bar', @@ -648,9 +655,7 @@ describe('saved object conversion', () => { }); test(`doesn't specify _source.namespace`, () => { - const serializer = new SavedObjectsSerializer( - new SavedObjectsSchema({ foo: { isNamespaceAgnostic: true } }) - ); + const serializer = new SavedObjectsSerializer(typeRegistry); const actual = serializer.savedObjectToRaw({ type: 'foo', namespace: 'bar', @@ -665,7 +670,7 @@ describe('saved object conversion', () => { describe('#isRawSavedObject', () => { describe('namespaced type without a namespace', () => { test('is true if the id is prefixed and the type matches', () => { - const serializer = new SavedObjectsSerializer(new SavedObjectsSchema()); + const serializer = new SavedObjectsSerializer(typeRegistry); expect( serializer.isRawSavedObject({ _id: 'hello:world', @@ -678,7 +683,7 @@ describe('saved object conversion', () => { }); test('is false if the id is not prefixed', () => { - const serializer = new SavedObjectsSerializer(new SavedObjectsSchema()); + const serializer = new SavedObjectsSerializer(typeRegistry); expect( serializer.isRawSavedObject({ _id: 'world', @@ -691,19 +696,19 @@ describe('saved object conversion', () => { }); test('is false if the type attribute is missing', () => { - const serializer = new SavedObjectsSerializer(new SavedObjectsSchema()); + const serializer = new SavedObjectsSerializer(typeRegistry); expect( serializer.isRawSavedObject({ _id: 'hello:world', _source: { hello: {}, - }, + } as any, }) ).toBeFalsy(); }); test(`is false if the type prefix omits the :`, () => { - const serializer = new SavedObjectsSerializer(new SavedObjectsSchema()); + const serializer = new SavedObjectsSerializer(typeRegistry); expect( serializer.isRawSavedObject({ _id: 'helloworld', @@ -716,7 +721,7 @@ describe('saved object conversion', () => { }); test('is false if the type attribute does not match the id', () => { - const serializer = new SavedObjectsSerializer(new SavedObjectsSchema()); + const serializer = new SavedObjectsSerializer(typeRegistry); expect( serializer.isRawSavedObject({ _id: 'hello:world', @@ -730,7 +735,7 @@ describe('saved object conversion', () => { }); test('is false if there is no [type] attribute', () => { - const serializer = new SavedObjectsSerializer(new SavedObjectsSchema()); + const serializer = new SavedObjectsSerializer(typeRegistry); expect( serializer.isRawSavedObject({ _id: 'hello:world', @@ -745,7 +750,7 @@ describe('saved object conversion', () => { describe('namespaced type with a namespace', () => { test('is true if the id is prefixed with type and namespace and the type matches', () => { - const serializer = new SavedObjectsSerializer(new SavedObjectsSchema()); + const serializer = new SavedObjectsSerializer(typeRegistry); expect( serializer.isRawSavedObject({ _id: 'foo:hello:world', @@ -759,7 +764,7 @@ describe('saved object conversion', () => { }); test('is false if the id is not prefixed by anything', () => { - const serializer = new SavedObjectsSerializer(new SavedObjectsSchema()); + const serializer = new SavedObjectsSerializer(typeRegistry); expect( serializer.isRawSavedObject({ _id: 'world', @@ -773,7 +778,7 @@ describe('saved object conversion', () => { }); test('is false if the id is prefixed only with type and the type matches', () => { - const serializer = new SavedObjectsSerializer(new SavedObjectsSchema()); + const serializer = new SavedObjectsSerializer(typeRegistry); expect( serializer.isRawSavedObject({ _id: 'hello:world', @@ -787,7 +792,7 @@ describe('saved object conversion', () => { }); test('is false if the id is prefixed only with namespace and the namespace matches', () => { - const serializer = new SavedObjectsSerializer(new SavedObjectsSchema()); + const serializer = new SavedObjectsSerializer(typeRegistry); expect( serializer.isRawSavedObject({ _id: 'foo:world', @@ -801,7 +806,7 @@ describe('saved object conversion', () => { }); test(`is false if the id prefix omits the trailing :`, () => { - const serializer = new SavedObjectsSerializer(new SavedObjectsSchema()); + const serializer = new SavedObjectsSerializer(typeRegistry); expect( serializer.isRawSavedObject({ _id: 'foo:helloworld', @@ -815,20 +820,20 @@ describe('saved object conversion', () => { }); test('is false if the type attribute is missing', () => { - const serializer = new SavedObjectsSerializer(new SavedObjectsSchema()); + const serializer = new SavedObjectsSerializer(typeRegistry); expect( serializer.isRawSavedObject({ _id: 'foo:hello:world', _source: { hello: {}, namespace: 'foo', - }, + } as any, }) ).toBeFalsy(); }); test('is false if the type attribute does not match the id', () => { - const serializer = new SavedObjectsSerializer(new SavedObjectsSchema()); + const serializer = new SavedObjectsSerializer(typeRegistry); expect( serializer.isRawSavedObject({ _id: 'foo:hello:world', @@ -843,7 +848,7 @@ describe('saved object conversion', () => { }); test('is false if the namespace attribute does not match the id', () => { - const serializer = new SavedObjectsSerializer(new SavedObjectsSchema()); + const serializer = new SavedObjectsSerializer(typeRegistry); expect( serializer.isRawSavedObject({ _id: 'bar:jam:world', @@ -858,7 +863,7 @@ describe('saved object conversion', () => { }); test('is false if there is no [type] attribute', () => { - const serializer = new SavedObjectsSerializer(new SavedObjectsSchema()); + const serializer = new SavedObjectsSerializer(typeRegistry); expect( serializer.isRawSavedObject({ _id: 'hello:world', @@ -872,11 +877,13 @@ describe('saved object conversion', () => { }); }); - describe('namespace agonstic type with a namespace', () => { + describe('namespace agnostic type with a namespace', () => { + beforeEach(() => { + typeRegistry.isNamespaceAgnostic.mockReturnValue(true); + }); + test('is true if the id is prefixed with type and the type matches', () => { - const serializer = new SavedObjectsSerializer( - new SavedObjectsSchema({ hello: { isNamespaceAgnostic: true } }) - ); + const serializer = new SavedObjectsSerializer(typeRegistry); expect( serializer.isRawSavedObject({ _id: 'hello:world', @@ -890,9 +897,7 @@ describe('saved object conversion', () => { }); test('is false if the id is not prefixed', () => { - const serializer = new SavedObjectsSerializer( - new SavedObjectsSchema({ hello: { isNamespaceAgnostic: true } }) - ); + const serializer = new SavedObjectsSerializer(typeRegistry); expect( serializer.isRawSavedObject({ _id: 'world', @@ -906,9 +911,7 @@ describe('saved object conversion', () => { }); test('is false if the id is prefixed with type and namespace', () => { - const serializer = new SavedObjectsSerializer( - new SavedObjectsSchema({ hello: { isNamespaceAgnostic: true } }) - ); + const serializer = new SavedObjectsSerializer(typeRegistry); expect( serializer.isRawSavedObject({ _id: 'foo:hello:world', @@ -922,9 +925,7 @@ describe('saved object conversion', () => { }); test(`is false if the type prefix omits the :`, () => { - const serializer = new SavedObjectsSerializer( - new SavedObjectsSchema({ hello: { isNamespaceAgnostic: true } }) - ); + const serializer = new SavedObjectsSerializer(typeRegistry); expect( serializer.isRawSavedObject({ _id: 'helloworld', @@ -938,24 +939,20 @@ describe('saved object conversion', () => { }); test('is false if the type attribute is missing', () => { - const serializer = new SavedObjectsSerializer( - new SavedObjectsSchema({ hello: { isNamespaceAgnostic: true } }) - ); + const serializer = new SavedObjectsSerializer(typeRegistry); expect( serializer.isRawSavedObject({ _id: 'hello:world', _source: { hello: {}, namespace: 'foo', - }, + } as any, }) ).toBeFalsy(); }); test('is false if the type attribute does not match the id', () => { - const serializer = new SavedObjectsSerializer( - new SavedObjectsSchema({ hello: { isNamespaceAgnostic: true } }) - ); + const serializer = new SavedObjectsSerializer(typeRegistry); expect( serializer.isRawSavedObject({ _id: 'hello:world', @@ -970,9 +967,7 @@ describe('saved object conversion', () => { }); test('is false if there is no [type] attribute', () => { - const serializer = new SavedObjectsSerializer( - new SavedObjectsSchema({ hello: { isNamespaceAgnostic: true } }) - ); + const serializer = new SavedObjectsSerializer(typeRegistry); expect( serializer.isRawSavedObject({ _id: 'hello:world', @@ -990,13 +985,13 @@ describe('saved object conversion', () => { describe('#generateRawId', () => { describe('namespaced type without a namespace', () => { test('generates an id if none is specified', () => { - const serializer = new SavedObjectsSerializer(new SavedObjectsSchema()); + const serializer = new SavedObjectsSerializer(typeRegistry); const id = serializer.generateRawId('', 'goodbye'); expect(id).toMatch(/goodbye\:[\w-]+$/); }); test('uses the id that is specified', () => { - const serializer = new SavedObjectsSerializer(new SavedObjectsSchema()); + const serializer = new SavedObjectsSerializer(typeRegistry); const id = serializer.generateRawId('', 'hello', 'world'); expect(id).toMatch('hello:world'); }); @@ -1004,13 +999,13 @@ describe('saved object conversion', () => { describe('namespaced type with a namespace', () => { test('generates an id if none is specified and prefixes namespace', () => { - const serializer = new SavedObjectsSerializer(new SavedObjectsSchema()); + const serializer = new SavedObjectsSerializer(typeRegistry); const id = serializer.generateRawId('foo', 'goodbye'); expect(id).toMatch(/foo:goodbye\:[\w-]+$/); }); test('uses the id that is specified and prefixes the namespace', () => { - const serializer = new SavedObjectsSerializer(new SavedObjectsSchema()); + const serializer = new SavedObjectsSerializer(typeRegistry); const id = serializer.generateRawId('foo', 'hello', 'world'); expect(id).toMatch('foo:hello:world'); }); @@ -1018,15 +1013,15 @@ describe('saved object conversion', () => { describe('namespace agnostic type with a namespace', () => { test(`generates an id if none is specified and doesn't prefix namespace`, () => { - const serializer = new SavedObjectsSerializer( - new SavedObjectsSchema({ foo: { isNamespaceAgnostic: true } }) - ); + typeRegistry.isNamespaceAgnostic.mockReturnValue(true); + + const serializer = new SavedObjectsSerializer(typeRegistry); const id = serializer.generateRawId('foo', 'goodbye'); expect(id).toMatch(/goodbye\:[\w-]+$/); }); test(`uses the id that is specified and doesn't prefix the namespace`, () => { - const serializer = new SavedObjectsSerializer(new SavedObjectsSchema()); + const serializer = new SavedObjectsSerializer(typeRegistry); const id = serializer.generateRawId('foo', 'hello', 'world'); expect(id).toMatch('hello:world'); }); diff --git a/src/core/server/saved_objects/serialization/serializer.ts b/src/core/server/saved_objects/serialization/serializer.ts new file mode 100644 index 0000000000000..99d6b0c6b59f9 --- /dev/null +++ b/src/core/server/saved_objects/serialization/serializer.ts @@ -0,0 +1,151 @@ +/* + * 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. + */ + +/* eslint-disable @typescript-eslint/camelcase */ + +import uuid from 'uuid'; +import { decodeVersion, encodeVersion } from '../version'; +import { ISavedObjectTypeRegistry } from '../saved_objects_type_registry'; +import { SavedObjectsRawDoc, SavedObjectSanitizedDoc } from './types'; + +/** + * A serializer that can be used to manually convert {@link SavedObjectsRawDoc | raw} or + * {@link SavedObjectSanitizedDoc | sanitized} documents to the other kind. + * + * @remarks Serializer instances should only be created and accessed by calling {@link SavedObjectsServiceStart.createSerializer} + * + * @public + */ +export class SavedObjectsSerializer { + private readonly registry: ISavedObjectTypeRegistry; + + /** + * @internal + */ + constructor(registry: ISavedObjectTypeRegistry) { + this.registry = registry; + } + /** + * Determines whether or not the raw document can be converted to a saved object. + * + * @param {SavedObjectsRawDoc} rawDoc - The raw ES document to be tested + */ + public isRawSavedObject(rawDoc: SavedObjectsRawDoc) { + const { type, namespace } = rawDoc._source; + const namespacePrefix = + namespace && !this.registry.isNamespaceAgnostic(type) ? `${namespace}:` : ''; + return Boolean( + type && + rawDoc._id.startsWith(`${namespacePrefix}${type}:`) && + rawDoc._source.hasOwnProperty(type) + ); + } + + /** + * Converts a document from the format that is stored in elasticsearch to the saved object client format. + * + * @param {SavedObjectsRawDoc} doc - The raw ES document to be converted to saved object format. + */ + public rawToSavedObject(doc: SavedObjectsRawDoc): SavedObjectSanitizedDoc { + const { _id, _source, _seq_no, _primary_term } = doc; + const { type, namespace } = _source; + + const version = + _seq_no != null || _primary_term != null + ? encodeVersion(_seq_no!, _primary_term!) + : undefined; + + return { + type, + id: this.trimIdPrefix(namespace, type, _id), + ...(namespace && !this.registry.isNamespaceAgnostic(type) && { namespace }), + attributes: _source[type], + references: _source.references || [], + ...(_source.migrationVersion && { migrationVersion: _source.migrationVersion }), + ...(_source.updated_at && { updated_at: _source.updated_at }), + ...(version && { version }), + }; + } + + /** + * Converts a document from the saved object client format to the format that is stored in elasticsearch. + * + * @param {SavedObjectSanitizedDoc} savedObj - The saved object to be converted to raw ES format. + */ + public savedObjectToRaw(savedObj: SavedObjectSanitizedDoc): SavedObjectsRawDoc { + const { + id, + type, + namespace, + attributes, + migrationVersion, + updated_at, + version, + references, + } = savedObj; + const source = { + [type]: attributes, + type, + references, + ...(namespace && !this.registry.isNamespaceAgnostic(type) && { namespace }), + ...(migrationVersion && { migrationVersion }), + ...(updated_at && { updated_at }), + }; + + return { + _id: this.generateRawId(namespace, type, id), + _source: source, + ...(version != null && decodeVersion(version)), + }; + } + + /** + * Given a saved object type and id, generates the compound id that is stored in the raw document. + * + * @param {string} namespace - The namespace of the saved object + * @param {string} type - The saved object type + * @param {string} id - The id of the saved object + */ + public generateRawId(namespace: string | undefined, type: string, id?: string) { + const namespacePrefix = + namespace && !this.registry.isNamespaceAgnostic(type) ? `${namespace}:` : ''; + return `${namespacePrefix}${type}:${id || uuid.v1()}`; + } + + private trimIdPrefix(namespace: string | undefined, type: string, id: string) { + assertNonEmptyString(id, 'document id'); + assertNonEmptyString(type, 'saved object type'); + + const namespacePrefix = + namespace && !this.registry.isNamespaceAgnostic(type) ? `${namespace}:` : ''; + const prefix = `${namespacePrefix}${type}:`; + + if (!id.startsWith(prefix)) { + return id; + } + + return id.slice(prefix.length); + } +} + +function assertNonEmptyString(value: string, name: string) { + if (!value || typeof value !== 'string') { + throw new TypeError(`Expected "${value}" to be a ${name}`); + } +} diff --git a/src/core/server/saved_objects/serialization/types.ts b/src/core/server/saved_objects/serialization/types.ts new file mode 100644 index 0000000000000..aaf6f45c244ec --- /dev/null +++ b/src/core/server/saved_objects/serialization/types.ts @@ -0,0 +1,79 @@ +/* + * 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 { SavedObjectsMigrationVersion, SavedObjectReference } from '../types'; + +/** + * A raw document as represented directly in the saved object index. + * + * @public + */ +export interface SavedObjectsRawDoc { + _id: string; + _source: SavedObjectsRawDocSource; + _type?: string; + _seq_no?: number; + _primary_term?: number; +} + +/** @public */ +export interface SavedObjectsRawDocSource { + type: string; + namespace?: string; + migrationVersion?: SavedObjectsMigrationVersion; + updated_at?: string; + references?: SavedObjectReference[]; + + [typeMapping: string]: any; +} + +/** + * A saved object type definition that allows for miscellaneous, unknown + * properties, as current discussions around security, ACLs, etc indicate + * that future props are likely to be added. Migrations support this + * scenario out of the box. + */ +interface SavedObjectDoc { + attributes: object; + id?: string; // NOTE: SavedObjectDoc is used for uncreated objects where `id` is optional + type: string; + namespace?: string; + migrationVersion?: SavedObjectsMigrationVersion; + version?: string; + updated_at?: string; + + [rootProp: string]: any; +} + +interface Referencable { + references: SavedObjectReference[]; +} + +/** + * We want to have two types, one that guarantees a "references" attribute + * will exist and one that allows it to be null. Since we're not migrating + * all the saved objects to have a "references" array, we need to support + * the scenarios where it may be missing (ex migrations). + * + * @public + */ +export type SavedObjectUnsanitizedDoc = SavedObjectDoc & Partial; + +/** @public */ +export type SavedObjectSanitizedDoc = SavedObjectDoc & Referencable; diff --git a/src/core/server/saved_objects/service/lib/repository.test.js b/src/core/server/saved_objects/service/lib/repository.test.js index 6171faa7a617c..2e5eeec04e0a8 100644 --- a/src/core/server/saved_objects/service/lib/repository.test.js +++ b/src/core/server/saved_objects/service/lib/repository.test.js @@ -21,10 +21,9 @@ import _ from 'lodash'; import { SavedObjectsRepository } from './repository'; import * as getSearchDslNS from './search_dsl/search_dsl'; import { SavedObjectsErrorHelpers } from './errors'; -import { SavedObjectsSchema } from '../../schema'; import { SavedObjectsSerializer } from '../../serialization'; -import { getRootPropertiesObjects } from '../../mappings/lib/get_root_properties_objects'; import { encodeHitVersion } from '../../version'; +import { SavedObjectTypeRegistry } from '../../saved_objects_type_registry'; jest.mock('./search_dsl/search_dsl', () => ({ getSearchDsl: jest.fn() })); @@ -35,6 +34,7 @@ describe('SavedObjectsRepository', () => { let callAdminCluster; let savedObjectsRepository; let migrator; + const mockTimestamp = '2017-08-14T15:49:14.886Z'; const mockTimestampFields = { updated_at: mockTimestamp }; const mockVersionProps = { _seq_no: 1, _primary_term: 1 }; @@ -240,12 +240,95 @@ describe('SavedObjectsRepository', () => { }, }; - const schema = new SavedObjectsSchema({ - globaltype: { isNamespaceAgnostic: true }, - foo: { isNamespaceAgnostic: true }, - bar: { isNamespaceAgnostic: true }, - baz: { indexPattern: 'beats' }, - hiddenType: { isNamespaceAgnostic: true, hidden: true }, + const typeRegistry = new SavedObjectTypeRegistry(); + typeRegistry.registerType({ + name: 'config', + hidden: false, + namespaceAgnostic: false, + mappings: { + properties: { + type: 'keyword', + }, + }, + }); + typeRegistry.registerType({ + name: 'index-pattern', + hidden: false, + namespaceAgnostic: false, + mappings: { + properties: { + someField: { + type: 'keyword', + }, + }, + }, + }); + typeRegistry.registerType({ + name: 'dashboard', + hidden: false, + namespaceAgnostic: false, + mappings: { + properties: { + otherField: { + type: 'keyword', + }, + }, + }, + }); + typeRegistry.registerType({ + name: 'globaltype', + hidden: false, + namespaceAgnostic: true, + mappings: { + properties: { + yetAnotherField: { + type: 'keyword', + }, + }, + }, + }); + typeRegistry.registerType({ + name: 'foo', + hidden: false, + namespaceAgnostic: true, + mappings: { + properties: { + type: 'keyword', + }, + }, + }); + typeRegistry.registerType({ + name: 'bar', + hidden: false, + namespaceAgnostic: true, + mappings: { + properties: { + type: 'keyword', + }, + }, + }); + typeRegistry.registerType({ + name: 'baz', + hidden: false, + namespaceAgnostic: false, + indexPattern: 'beats', + mappings: { + properties: { + type: 'keyword', + }, + }, + }); + typeRegistry.registerType({ + name: 'hiddenType', + hidden: true, + namespaceAgnostic: true, + mappings: { + properties: { + someField: { + type: 'keyword', + }, + }, + }, }); beforeEach(() => { @@ -255,16 +338,16 @@ describe('SavedObjectsRepository', () => { runMigrations: async () => ({ status: 'skipped' }), }; - const serializer = new SavedObjectsSerializer(schema); - const allTypes = Object.keys(getRootPropertiesObjects(mappings)); - const allowedTypes = [...new Set(allTypes.filter(type => !schema.isHiddenType(type)))]; + const serializer = new SavedObjectsSerializer(typeRegistry); + const allTypes = typeRegistry.getAllTypes().map(type => type.name); + const allowedTypes = [...new Set(allTypes.filter(type => !typeRegistry.isHidden(type)))]; savedObjectsRepository = new SavedObjectsRepository({ index: '.kibana-test', mappings, callCluster: callAdminCluster, migrator, - schema, + typeRegistry, serializer, allowedTypes, }); @@ -1171,7 +1254,7 @@ describe('SavedObjectsRepository', () => { expect(result).toEqual(deleteByQueryResults); expect(callAdminCluster).toHaveBeenCalledTimes(1); - expect(getSearchDslNS.getSearchDsl).toHaveBeenCalledWith(mappings, schema, { + expect(getSearchDslNS.getSearchDsl).toHaveBeenCalledWith(mappings, typeRegistry, { namespace: 'my-namespace', type: ['config', 'baz', 'index-pattern', 'dashboard'], }); @@ -1261,7 +1344,11 @@ describe('SavedObjectsRepository', () => { await savedObjectsRepository.find(relevantOpts); expect(getSearchDslNS.getSearchDsl).toHaveBeenCalledTimes(1); - expect(getSearchDslNS.getSearchDsl).toHaveBeenCalledWith(mappings, schema, relevantOpts); + expect(getSearchDslNS.getSearchDsl).toHaveBeenCalledWith( + mappings, + typeRegistry, + relevantOpts + ); }); it('accepts KQL filter and passes keuryNode to getSearchDsl', async () => { diff --git a/src/core/server/saved_objects/service/lib/repository.ts b/src/core/server/saved_objects/service/lib/repository.ts index 70b8b2878c15b..b485b8dfe398c 100644 --- a/src/core/server/saved_objects/service/lib/repository.ts +++ b/src/core/server/saved_objects/service/lib/repository.ts @@ -27,9 +27,12 @@ import { includedFields } from './included_fields'; import { decorateEsError } from './decorate_es_error'; import { SavedObjectsErrorHelpers } from './errors'; import { decodeRequestVersion, encodeVersion, encodeHitVersion } from '../../version'; -import { SavedObjectsSchema } from '../../schema'; import { KibanaMigrator } from '../../migrations'; -import { SavedObjectsSerializer, SanitizedSavedObjectDoc, RawDoc } from '../../serialization'; +import { + SavedObjectsSerializer, + SavedObjectSanitizedDoc, + SavedObjectsRawDoc, +} from '../../serialization'; import { SavedObjectsBulkCreateObject, SavedObjectsBulkGetObject, @@ -51,8 +54,8 @@ import { SavedObjectsMigrationVersion, MutatingOperationRefreshSetting, } from '../../types'; +import { SavedObjectTypeRegistry } from '../../saved_objects_type_registry'; import { validateConvertFilterToKueryNode } from './filter_utils'; -import { LegacyConfig } from '../../../legacy'; // BEWARE: The SavedObjectClient depends on the implementation details of the SavedObjectsRepository // so any breaking changes to this repository are considered breaking changes to the SavedObjectsClient. @@ -75,11 +78,9 @@ const isLeft = (either: Either): either is Left => { export interface SavedObjectsRepositoryOptions { index: string; - /** @deprecated Will be removed once SavedObjectsSchema is exposed from Core */ - config: LegacyConfig; mappings: IndexMapping; callCluster: APICaller; - schema: SavedObjectsSchema; + typeRegistry: SavedObjectTypeRegistry; serializer: SavedObjectsSerializer; migrator: KibanaMigrator; allowedTypes: string[]; @@ -118,9 +119,8 @@ export type ISavedObjectsRepository = Pick !schema.isHiddenType(type)); + const serializer = new SavedObjectsSerializer(typeRegistry); + const visibleTypes = allTypes.filter(type => !typeRegistry.isHidden(type)); const missingTypeMappings = extraTypes.filter(type => !allTypes.includes(type)); if (missingTypeMappings.length > 0) { @@ -158,10 +157,9 @@ export class SavedObjectsRepository { return new injectedConstructor({ index: indexName, - config, migrator, mappings, - schema, + typeRegistry, serializer, allowedTypes, callCluster: retryCallCluster(callCluster), @@ -171,10 +169,9 @@ export class SavedObjectsRepository { private constructor(options: SavedObjectsRepositoryOptions) { const { index, - config, mappings, callCluster, - schema, + typeRegistry, serializer, migrator, allowedTypes = [], @@ -189,9 +186,8 @@ export class SavedObjectsRepository { // to returning them. this._migrator = migrator; this._index = index; - this._config = config; this._mappings = mappings; - this._schema = schema; + this._registry = typeRegistry; if (allowedTypes.length === 0) { throw new Error('Empty or missing types for saved object repository!'); } @@ -201,7 +197,6 @@ export class SavedObjectsRepository { await migrator.runMigrations(); return callCluster(...args); }; - this._schema = schema; this._serializer = serializer; } @@ -250,7 +245,7 @@ export class SavedObjectsRepository { references, }); - const raw = this._serializer.savedObjectToRaw(migrated as SanitizedSavedObjectDoc); + const raw = this._serializer.savedObjectToRaw(migrated as SavedObjectSanitizedDoc); const response = await this._writeToCluster(method, { id: raw._id, @@ -316,7 +311,7 @@ export class SavedObjectsRepository { namespace, updated_at: time, references: object.references || [], - }) as SanitizedSavedObjectDoc + }) as SavedObjectSanitizedDoc ), }; @@ -435,7 +430,7 @@ export class SavedObjectsRepository { const allTypes = Object.keys(getRootPropertiesObjects(this._mappings)); - const typesToDelete = allTypes.filter(type => !this._schema.isNamespaceAgnostic(type)); + const typesToDelete = allTypes.filter(type => !this._registry.isNamespaceAgnostic(type)); const esOptions = { index: this.getIndicesForTypes(typesToDelete), @@ -443,7 +438,7 @@ export class SavedObjectsRepository { refresh, body: { conflicts: 'proceed', - ...getSearchDsl(this._mappings, this._schema, { + ...getSearchDsl(this._mappings, this._registry, { namespace, type: typesToDelete, }), @@ -531,7 +526,7 @@ export class SavedObjectsRepository { rest_total_hits_as_int: true, body: { seq_no_primary_term: true, - ...getSearchDsl(this._mappings, this._schema, { + ...getSearchDsl(this._mappings, this._registry, { search, defaultSearchOperator, searchFields, @@ -562,7 +557,9 @@ export class SavedObjectsRepository { page, per_page: perPage, total: response.hits.total, - saved_objects: response.hits.hits.map((hit: RawDoc) => this._rawToSavedObject(hit)), + saved_objects: response.hits.hits.map((hit: SavedObjectsRawDoc) => + this._rawToSavedObject(hit) + ), }; } @@ -890,7 +887,7 @@ export class SavedObjectsRepository { updated_at: time, }); - const raw = this._serializer.savedObjectToRaw(migrated as SanitizedSavedObjectDoc); + const raw = this._serializer.savedObjectToRaw(migrated as SavedObjectSanitizedDoc); const response = await this._writeToCluster('update', { id: this._serializer.generateRawId(namespace, type, id), @@ -952,7 +949,7 @@ export class SavedObjectsRepository { * @param type - the type */ private getIndexForType(type: string) { - return this._schema.getIndexForType(this._config, type) || this._index; + return this._registry.getIndex(type) || this._index; } /** @@ -964,7 +961,7 @@ export class SavedObjectsRepository { */ private getIndicesForTypes(types: string[]) { const unique = (array: string[]) => [...new Set(array)]; - return unique(types.map(t => this._schema.getIndexForType(this._config, t) || this._index)); + return unique(types.map(t => this.getIndexForType(t))); } private _getCurrentTime() { @@ -975,7 +972,7 @@ export class SavedObjectsRepository { // includes the namespace, and we use this for migrating documents. However, we don't // want the namespace to be returned from the repository, as the repository scopes each // method transparently to the specified namespace. - private _rawToSavedObject(raw: RawDoc): SavedObject { + private _rawToSavedObject(raw: SavedObjectsRawDoc): SavedObject { const savedObject = this._serializer.rawToSavedObject(raw); return omit(savedObject, 'namespace'); } diff --git a/src/core/server/saved_objects/service/lib/repository_create_repository.test.ts b/src/core/server/saved_objects/service/lib/repository_create_repository.test.ts index b3ea056484760..4a87bb1043ca2 100644 --- a/src/core/server/saved_objects/service/lib/repository_create_repository.test.ts +++ b/src/core/server/saved_objects/service/lib/repository_create_repository.test.ts @@ -18,43 +18,54 @@ */ import { SavedObjectsRepository } from './repository'; import { mockKibanaMigrator } from '../../migrations/kibana/kibana_migrator.mock'; -import { SavedObjectsSchema } from '../../schema'; import { KibanaMigrator } from '../../migrations'; -import { LegacyConfig } from '../../../legacy'; +import { SavedObjectTypeRegistry } from '../../saved_objects_type_registry'; + jest.mock('./repository'); const { SavedObjectsRepository: originalRepository } = jest.requireActual('./repository'); describe('SavedObjectsRepository#createRepository', () => { const callAdminCluster = jest.fn(); - const schema = new SavedObjectsSchema({ - nsAgnosticType: { isNamespaceAgnostic: true }, - nsType: { indexPattern: 'beats', isNamespaceAgnostic: false }, - hiddenType: { isNamespaceAgnostic: true, hidden: true }, + + const typeRegistry = new SavedObjectTypeRegistry(); + typeRegistry.registerType({ + name: 'nsAgnosticType', + hidden: false, + namespaceAgnostic: true, + mappings: { + properties: { + name: { type: 'keyword' }, + }, + }, + migrations: {}, + }); + + typeRegistry.registerType({ + name: 'nsType', + hidden: false, + namespaceAgnostic: false, + indexPattern: 'beats', + mappings: { + properties: { + name: { type: 'keyword' }, + }, + }, + migrations: {}, }); - const mappings = [ - { - pluginId: 'testplugin', + typeRegistry.registerType({ + name: 'hiddenType', + hidden: true, + namespaceAgnostic: true, + mappings: { properties: { - nsAgnosticType: { - properties: { - name: { type: 'keyword' }, - }, - }, - nsType: { - properties: { - name: { type: 'keyword' }, - }, - }, - hiddenType: { - properties: { - name: { type: 'keyword' }, - }, - }, + name: { type: 'keyword' }, }, }, - ]; - const migrator = mockKibanaMigrator.create({ savedObjectMappings: mappings }); + migrations: {}, + }); + + const migrator = mockKibanaMigrator.create({ types: typeRegistry.getAllTypes() }); const RepositoryConstructor = (SavedObjectsRepository as unknown) as jest.Mock< SavedObjectsRepository >; @@ -67,8 +78,7 @@ describe('SavedObjectsRepository#createRepository', () => { try { originalRepository.createRepository( (migrator as unknown) as KibanaMigrator, - schema, - {} as LegacyConfig, + typeRegistry, '.kibana-test', callAdminCluster, ['unMappedType1', 'unmappedType2'] @@ -83,8 +93,7 @@ describe('SavedObjectsRepository#createRepository', () => { it('should create a repository without hidden types', () => { const repository = originalRepository.createRepository( (migrator as unknown) as KibanaMigrator, - schema, - {} as LegacyConfig, + typeRegistry, '.kibana-test', callAdminCluster, [], @@ -103,8 +112,7 @@ describe('SavedObjectsRepository#createRepository', () => { it('should create a repository with a unique list of hidden types', () => { const repository = originalRepository.createRepository( (migrator as unknown) as KibanaMigrator, - schema, - {} as LegacyConfig, + typeRegistry, '.kibana-test', callAdminCluster, ['hiddenType', 'hiddenType', 'hiddenType'], diff --git a/src/core/server/saved_objects/service/lib/search_dsl/query_params.test.ts b/src/core/server/saved_objects/service/lib/search_dsl/query_params.test.ts index e585953109d2c..b2129765ee426 100644 --- a/src/core/server/saved_objects/service/lib/search_dsl/query_params.test.ts +++ b/src/core/server/saved_objects/service/lib/search_dsl/query_params.test.ts @@ -17,10 +17,11 @@ * under the License. */ -import { schemaMock } from '../../../schema/schema.mock'; +import { typeRegistryMock } from '../../../saved_objects_type_registry.mock'; import { getQueryParams } from './query_params'; -const SCHEMA = schemaMock.create(); +const registry = typeRegistryMock.create(); + const MAPPINGS = { properties: { type: { @@ -85,7 +86,7 @@ const createTypeClause = (type: string, namespace?: string) => { describe('searchDsl/queryParams', () => { describe('no parameters', () => { it('searches for all known types without a namespace specified', () => { - expect(getQueryParams({ mappings: MAPPINGS, schema: SCHEMA })).toEqual({ + expect(getQueryParams({ mappings: MAPPINGS, registry })).toEqual({ query: { bool: { filter: [ @@ -108,9 +109,7 @@ describe('searchDsl/queryParams', () => { describe('namespace', () => { it('filters namespaced types for namespace, and ensures namespace agnostic types have no namespace', () => { - expect( - getQueryParams({ mappings: MAPPINGS, schema: SCHEMA, namespace: 'foo-namespace' }) - ).toEqual({ + expect(getQueryParams({ mappings: MAPPINGS, registry, namespace: 'foo-namespace' })).toEqual({ query: { bool: { filter: [ @@ -134,7 +133,7 @@ describe('searchDsl/queryParams', () => { describe('type (singular, namespaced)', () => { it('includes a terms filter for type and namespace not being specified', () => { expect( - getQueryParams({ mappings: MAPPINGS, schema: SCHEMA, namespace: undefined, type: 'saved' }) + getQueryParams({ mappings: MAPPINGS, registry, namespace: undefined, type: 'saved' }) ).toEqual({ query: { bool: { @@ -155,7 +154,7 @@ describe('searchDsl/queryParams', () => { describe('type (singular, global)', () => { it('includes a terms filter for type and namespace not being specified', () => { expect( - getQueryParams({ mappings: MAPPINGS, schema: SCHEMA, namespace: undefined, type: 'global' }) + getQueryParams({ mappings: MAPPINGS, registry, namespace: undefined, type: 'global' }) ).toEqual({ query: { bool: { @@ -178,7 +177,7 @@ describe('searchDsl/queryParams', () => { expect( getQueryParams({ mappings: MAPPINGS, - schema: SCHEMA, + registry, namespace: undefined, type: ['saved', 'global'], }) @@ -204,7 +203,7 @@ describe('searchDsl/queryParams', () => { expect( getQueryParams({ mappings: MAPPINGS, - schema: SCHEMA, + registry, namespace: 'foo-namespace', type: ['saved', 'global'], }) @@ -230,7 +229,7 @@ describe('searchDsl/queryParams', () => { expect( getQueryParams({ mappings: MAPPINGS, - schema: SCHEMA, + registry, namespace: undefined, type: undefined, search: 'us*', @@ -270,7 +269,7 @@ describe('searchDsl/queryParams', () => { expect( getQueryParams({ mappings: MAPPINGS, - schema: SCHEMA, + registry, namespace: 'foo-namespace', type: undefined, search: 'us*', @@ -310,7 +309,7 @@ describe('searchDsl/queryParams', () => { expect( getQueryParams({ mappings: MAPPINGS, - schema: SCHEMA, + registry, namespace: undefined, type: ['saved', 'global'], search: 'us*', @@ -346,7 +345,7 @@ describe('searchDsl/queryParams', () => { expect( getQueryParams({ mappings: MAPPINGS, - schema: SCHEMA, + registry, namespace: 'foo-namespace', type: ['saved', 'global'], search: 'us*', @@ -382,7 +381,7 @@ describe('searchDsl/queryParams', () => { expect( getQueryParams({ mappings: MAPPINGS, - schema: SCHEMA, + registry, namespace: undefined, type: undefined, search: 'y*', @@ -419,7 +418,7 @@ describe('searchDsl/queryParams', () => { expect( getQueryParams({ mappings: MAPPINGS, - schema: SCHEMA, + registry, namespace: undefined, type: undefined, search: 'y*', @@ -456,7 +455,7 @@ describe('searchDsl/queryParams', () => { expect( getQueryParams({ mappings: MAPPINGS, - schema: SCHEMA, + registry, namespace: undefined, type: undefined, search: 'y*', @@ -503,7 +502,7 @@ describe('searchDsl/queryParams', () => { expect( getQueryParams({ mappings: MAPPINGS, - schema: SCHEMA, + registry, namespace: 'foo-namespace', type: undefined, search: 'y*', @@ -540,7 +539,7 @@ describe('searchDsl/queryParams', () => { expect( getQueryParams({ mappings: MAPPINGS, - schema: SCHEMA, + registry, namespace: 'foo-namespace', type: undefined, search: 'y*', @@ -577,7 +576,7 @@ describe('searchDsl/queryParams', () => { expect( getQueryParams({ mappings: MAPPINGS, - schema: SCHEMA, + registry, namespace: 'foo-namespace', type: undefined, search: 'y*', @@ -624,7 +623,7 @@ describe('searchDsl/queryParams', () => { expect( getQueryParams({ mappings: MAPPINGS, - schema: SCHEMA, + registry, namespace: undefined, type: ['saved', 'global'], search: 'y*', @@ -657,7 +656,7 @@ describe('searchDsl/queryParams', () => { expect( getQueryParams({ mappings: MAPPINGS, - schema: SCHEMA, + registry, namespace: undefined, type: ['saved', 'global'], search: 'y*', @@ -690,7 +689,7 @@ describe('searchDsl/queryParams', () => { expect( getQueryParams({ mappings: MAPPINGS, - schema: SCHEMA, + registry, namespace: undefined, type: ['saved', 'global'], search: 'y*', @@ -726,7 +725,7 @@ describe('searchDsl/queryParams', () => { expect( getQueryParams({ mappings: MAPPINGS, - schema: SCHEMA, + registry, namespace: 'foo-namespace', type: ['saved', 'global'], search: 'y*', @@ -759,7 +758,7 @@ describe('searchDsl/queryParams', () => { expect( getQueryParams({ mappings: MAPPINGS, - schema: SCHEMA, + registry, namespace: 'foo-namespace', type: ['saved', 'global'], search: 'y*', @@ -792,7 +791,7 @@ describe('searchDsl/queryParams', () => { expect( getQueryParams({ mappings: MAPPINGS, - schema: SCHEMA, + registry, namespace: 'foo-namespace', type: ['saved', 'global'], search: 'y*', @@ -828,7 +827,7 @@ describe('searchDsl/queryParams', () => { expect( getQueryParams({ mappings: MAPPINGS, - schema: SCHEMA, + registry, namespace: 'foo-namespace', type: ['saved', 'global'], search: 'foo', @@ -902,7 +901,7 @@ describe('searchDsl/queryParams', () => { expect( getQueryParams({ mappings: MAPPINGS, - schema: SCHEMA, + registry, namespace: 'foo-namespace', type: ['saved', 'global'], search: undefined, @@ -958,7 +957,7 @@ describe('searchDsl/queryParams', () => { expect( getQueryParams({ mappings: MAPPINGS, - schema: SCHEMA, + registry, namespace: 'foo-namespace', kueryNode: { type: 'function', @@ -1052,7 +1051,7 @@ describe('searchDsl/queryParams', () => { expect( getQueryParams({ mappings: MAPPINGS, - schema: SCHEMA, + registry, namespace: 'foo-namespace', kueryNode: { type: 'function', @@ -1189,7 +1188,7 @@ describe('searchDsl/queryParams', () => { expect( getQueryParams({ mappings: MAPPINGS, - schema: SCHEMA, + registry, namespace: 'foo-namespace', search: 'y*', searchFields: ['title'], diff --git a/src/core/server/saved_objects/service/lib/search_dsl/query_params.ts b/src/core/server/saved_objects/service/lib/search_dsl/query_params.ts index a1e3ae9620299..3fabad6af08ff 100644 --- a/src/core/server/saved_objects/service/lib/search_dsl/query_params.ts +++ b/src/core/server/saved_objects/service/lib/search_dsl/query_params.ts @@ -20,7 +20,7 @@ import { esKuery } from '../../../../../../plugins/data/server'; import { getRootPropertiesObjects, IndexMapping } from '../../../mappings'; -import { SavedObjectsSchema } from '../../../schema'; +import { ISavedObjectTypeRegistry } from '../../../saved_objects_type_registry'; /** * Gets the types based on the type. Uses mappings to support @@ -61,8 +61,12 @@ function getFieldsForTypes(types: string[], searchFields?: string[]) { * Gets the clause that will filter for the type in the namespace. * Some types are namespace agnostic, so they must be treated differently. */ -function getClauseForType(schema: SavedObjectsSchema, namespace: string | undefined, type: string) { - if (namespace && !schema.isNamespaceAgnostic(type)) { +function getClauseForType( + registry: ISavedObjectTypeRegistry, + namespace: string | undefined, + type: string +) { + if (namespace && !registry.isNamespaceAgnostic(type)) { return { bool: { must: [{ term: { type } }, { term: { namespace } }], @@ -85,7 +89,7 @@ interface HasReferenceQueryParams { interface QueryParams { mappings: IndexMapping; - schema: SavedObjectsSchema; + registry: ISavedObjectTypeRegistry; namespace?: string; type?: string | string[]; search?: string; @@ -100,7 +104,7 @@ interface QueryParams { */ export function getQueryParams({ mappings, - schema, + registry, namespace, type, search, @@ -140,7 +144,7 @@ export function getQueryParams({ }, ] : undefined, - should: types.map(shouldType => getClauseForType(schema, namespace, shouldType)), + should: types.map(shouldType => getClauseForType(registry, namespace, shouldType)), minimum_should_match: 1, }, }, diff --git a/src/core/server/saved_objects/service/lib/search_dsl/search_dsl.test.ts b/src/core/server/saved_objects/service/lib/search_dsl/search_dsl.test.ts index 97cab3e566d5e..95b7ffd117ee9 100644 --- a/src/core/server/saved_objects/service/lib/search_dsl/search_dsl.test.ts +++ b/src/core/server/saved_objects/service/lib/search_dsl/search_dsl.test.ts @@ -20,7 +20,7 @@ jest.mock('./query_params'); jest.mock('./sorting_params'); -import { schemaMock } from '../../../schema/schema.mock'; +import { typeRegistryMock } from '../../../saved_objects_type_registry.mock'; import * as queryParamsNS from './query_params'; import { getSearchDsl } from './search_dsl'; import * as sortParamsNS from './sorting_params'; @@ -28,8 +28,8 @@ import * as sortParamsNS from './sorting_params'; const getQueryParams = queryParamsNS.getQueryParams as jest.Mock; const getSortingParams = sortParamsNS.getSortingParams as jest.Mock; -const SCHEMA = schemaMock.create(); -const MAPPINGS = { properties: {} }; +const registry = typeRegistryMock.create(); +const mappings = { properties: {} }; describe('getSearchDsl', () => { afterEach(() => { @@ -40,7 +40,7 @@ describe('getSearchDsl', () => { describe('validation', () => { it('throws when type is not specified', () => { expect(() => { - getSearchDsl(MAPPINGS, SCHEMA, { + getSearchDsl(mappings, registry, { type: undefined as any, sortField: 'title', }); @@ -48,7 +48,7 @@ describe('getSearchDsl', () => { }); it('throws when sortOrder without sortField', () => { expect(() => { - getSearchDsl(MAPPINGS, SCHEMA, { + getSearchDsl(mappings, registry, { type: 'foo', sortOrder: 'desc', }); @@ -70,11 +70,11 @@ describe('getSearchDsl', () => { }, }; - getSearchDsl(MAPPINGS, SCHEMA, opts); + getSearchDsl(mappings, registry, opts); expect(getQueryParams).toHaveBeenCalledTimes(1); expect(getQueryParams).toHaveBeenCalledWith({ - mappings: MAPPINGS, - schema: SCHEMA, + mappings, + registry, namespace: opts.namespace, type: opts.type, search: opts.search, @@ -92,10 +92,10 @@ describe('getSearchDsl', () => { sortOrder: 'baz', }; - getSearchDsl(MAPPINGS, SCHEMA, opts); + getSearchDsl(mappings, registry, opts); expect(getSortingParams).toHaveBeenCalledTimes(1); expect(getSortingParams).toHaveBeenCalledWith( - MAPPINGS, + mappings, opts.type, opts.sortField, opts.sortOrder @@ -105,7 +105,7 @@ describe('getSearchDsl', () => { it('returns combination of getQueryParams and getSortingParams', () => { getQueryParams.mockReturnValue({ a: 'a' }); getSortingParams.mockReturnValue({ b: 'b' }); - expect(getSearchDsl(MAPPINGS, SCHEMA, { type: 'foo' })).toEqual({ a: 'a', b: 'b' }); + expect(getSearchDsl(mappings, registry, { type: 'foo' })).toEqual({ a: 'a', b: 'b' }); }); }); }); diff --git a/src/core/server/saved_objects/service/lib/search_dsl/search_dsl.ts b/src/core/server/saved_objects/service/lib/search_dsl/search_dsl.ts index 1b6e1361bb92a..75ab058a38be9 100644 --- a/src/core/server/saved_objects/service/lib/search_dsl/search_dsl.ts +++ b/src/core/server/saved_objects/service/lib/search_dsl/search_dsl.ts @@ -20,11 +20,11 @@ import Boom from 'boom'; import { IndexMapping } from '../../../mappings'; -import { SavedObjectsSchema } from '../../../schema'; import { getQueryParams } from './query_params'; import { getSortingParams } from './sorting_params'; // eslint-disable-next-line @kbn/eslint/no-restricted-paths import { esKuery } from '../../../../../../plugins/data/server'; +import { ISavedObjectTypeRegistry } from '../../../saved_objects_type_registry'; interface GetSearchDslOptions { type: string | string[]; @@ -43,7 +43,7 @@ interface GetSearchDslOptions { export function getSearchDsl( mappings: IndexMapping, - schema: SavedObjectsSchema, + registry: ISavedObjectTypeRegistry, options: GetSearchDslOptions ) { const { @@ -69,7 +69,7 @@ export function getSearchDsl( return { ...getQueryParams({ mappings, - schema, + registry, namespace, type, search, diff --git a/src/core/server/saved_objects/types.ts b/src/core/server/saved_objects/types.ts index a3fe2b937635b..980ba005e0eeb 100644 --- a/src/core/server/saved_objects/types.ts +++ b/src/core/server/saved_objects/types.ts @@ -18,9 +18,8 @@ */ import { SavedObjectsClient } from './service/saved_objects_client'; -import { SavedObjectsMapping } from './mappings'; -import { MigrationDefinition } from './migrations/core/document_migrator'; -import { SavedObjectsSchemaDefinition } from './schema'; +import { SavedObjectsTypeMappingDefinition, SavedObjectsTypeMappingDefinitions } from './mappings'; +import { SavedObjectMigrationMap } from './migrations'; import { PropertyValidators } from './validation'; export { @@ -34,6 +33,7 @@ export { } from './import/types'; import { SavedObjectAttributes } from '../../types'; +import { LegacyConfig } from '../legacy'; export { SavedObjectAttributes, SavedObjectAttribute, @@ -208,13 +208,88 @@ export type MutatingOperationRefreshSetting = boolean | 'wait_for'; */ export type SavedObjectsClientContract = Pick; +/** + * @remarks This is only internal for now, and will only be public when we expose the registerType API + * + * @public + */ +export interface SavedObjectsType { + /** + * The name of the type, which is also used as the internal id. + */ + name: string; + /** + * Is the type hidden by default. If true, repositories will not have access to this type unless explicitly + * declared as an `extraType` when creating the repository. + * + * See {@link SavedObjectsServiceStart.createInternalRepository | createInternalRepository}. + */ + hidden: boolean; + /** + * Is the type global (true), or namespaced (false). + */ + namespaceAgnostic: boolean; + /** + * If defined, the type instances will be stored in the given index instead of the default one. + */ + indexPattern?: string; + /** + * If defined, will be used to convert the type to an alias. + */ + convertToAliasScript?: string; + /** + * The {@link SavedObjectsTypeMappingDefinition | mapping definition} for the type. + */ + mappings: SavedObjectsTypeMappingDefinition; + /** + * An optional map of {@link SavedObjectMigrationFn | migrations} to be used to migrate the type. + */ + migrations?: SavedObjectMigrationMap; +} + /** * @internal * @deprecated */ export interface SavedObjectsLegacyUiExports { - savedObjectMappings: SavedObjectsMapping[]; - savedObjectMigrations: MigrationDefinition; - savedObjectSchemas: SavedObjectsSchemaDefinition; + savedObjectMappings: SavedObjectsLegacyMapping[]; + savedObjectMigrations: SavedObjectsLegacyMigrationDefinitions; + savedObjectSchemas: SavedObjectsLegacySchemaDefinitions; savedObjectValidations: PropertyValidators; } + +/** + * @internal + * @deprecated + */ +export interface SavedObjectsLegacyMapping { + pluginId: string; + properties: SavedObjectsTypeMappingDefinitions; +} + +/** + * @internal + * @deprecated + */ +export interface SavedObjectsLegacyMigrationDefinitions { + [type: string]: SavedObjectMigrationMap; +} + +/** + * @internal + * @deprecated + */ +interface SavedObjectsLegacyTypeSchema { + isNamespaceAgnostic?: boolean; + hidden?: boolean; + indexPattern?: ((config: LegacyConfig) => string) | string; + convertToAliasScript?: string; +} + +/** + * @internal + * @deprecated + */ +export interface SavedObjectsLegacySchemaDefinitions { + [type: string]: SavedObjectsLegacyTypeSchema; +} diff --git a/src/core/server/saved_objects/utils.test.ts b/src/core/server/saved_objects/utils.test.ts new file mode 100644 index 0000000000000..d1c15517e94a6 --- /dev/null +++ b/src/core/server/saved_objects/utils.test.ts @@ -0,0 +1,285 @@ +/* + * 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 { legacyServiceMock } from '../legacy/legacy_service.mock'; +import { convertLegacyTypes, convertTypesToLegacySchema } from './utils'; +import { SavedObjectsLegacyUiExports, SavedObjectsType } from './types'; +import { LegacyConfig } from 'kibana/server'; + +describe('convertLegacyTypes', () => { + let legacyConfig: ReturnType; + + beforeEach(() => { + legacyConfig = legacyServiceMock.createLegacyConfig(); + }); + + it('converts the legacy mappings using default values if no schemas are specified', () => { + const uiExports: SavedObjectsLegacyUiExports = { + savedObjectMappings: [ + { + pluginId: 'pluginA', + properties: { + typeA: { + properties: { + fieldA: { type: 'text' }, + }, + }, + typeB: { + properties: { + fieldB: { type: 'text' }, + }, + }, + }, + }, + { + pluginId: 'pluginB', + properties: { + typeC: { + properties: { + fieldC: { type: 'text' }, + }, + }, + }, + }, + ], + savedObjectMigrations: {}, + savedObjectSchemas: {}, + savedObjectValidations: {}, + }; + + const converted = convertLegacyTypes(uiExports, legacyConfig); + expect(converted).toMatchSnapshot(); + }); + + it('merges the mappings and the schema to create the type when schema exists for the type', () => { + const uiExports: SavedObjectsLegacyUiExports = { + savedObjectMappings: [ + { + pluginId: 'pluginA', + properties: { + typeA: { + properties: { + fieldA: { type: 'text' }, + }, + }, + }, + }, + { + pluginId: 'pluginB', + properties: { + typeC: { + properties: { + fieldC: { type: 'text' }, + }, + }, + }, + }, + ], + savedObjectMigrations: {}, + savedObjectSchemas: { + typeA: { + indexPattern: 'fooBar', + hidden: true, + isNamespaceAgnostic: true, + }, + }, + savedObjectValidations: {}, + }; + + const converted = convertLegacyTypes(uiExports, legacyConfig); + expect(converted).toMatchSnapshot(); + }); + + it('invokes indexPattern to retrieve the index when it is a function', () => { + const indexPatternAccessor: (config: LegacyConfig) => string = jest.fn(config => { + config.get('foo.bar'); + return 'myIndex'; + }); + + const uiExports: SavedObjectsLegacyUiExports = { + savedObjectMappings: [ + { + pluginId: 'pluginA', + properties: { + typeA: { + properties: { + fieldA: { type: 'text' }, + }, + }, + }, + }, + ], + savedObjectMigrations: {}, + savedObjectSchemas: { + typeA: { + indexPattern: indexPatternAccessor, + hidden: true, + isNamespaceAgnostic: true, + }, + }, + savedObjectValidations: {}, + }; + + const converted = convertLegacyTypes(uiExports, legacyConfig); + + expect(indexPatternAccessor).toHaveBeenCalledWith(legacyConfig); + expect(legacyConfig.get).toHaveBeenCalledWith('foo.bar'); + expect(converted.length).toEqual(1); + expect(converted[0].indexPattern).toEqual('myIndex'); + }); + + it('import migrations from the uiExports', () => { + const migrationsA = { + '1.0.0': jest.fn(), + '2.0.4': jest.fn(), + }; + const migrationsB = { + '1.5.3': jest.fn(), + }; + + const uiExports: SavedObjectsLegacyUiExports = { + savedObjectMappings: [ + { + pluginId: 'pluginA', + properties: { + typeA: { + properties: { + fieldA: { type: 'text' }, + }, + }, + }, + }, + { + pluginId: 'pluginB', + properties: { + typeB: { + properties: { + fieldC: { type: 'text' }, + }, + }, + }, + }, + ], + savedObjectMigrations: { + typeA: migrationsA, + typeB: migrationsB, + }, + savedObjectSchemas: {}, + savedObjectValidations: {}, + }; + + const converted = convertLegacyTypes(uiExports, legacyConfig); + expect(converted.length).toEqual(2); + expect(converted[0].migrations).toEqual(migrationsA); + expect(converted[1].migrations).toEqual(migrationsB); + }); + + it('merges everything when all are present', () => { + const uiExports: SavedObjectsLegacyUiExports = { + savedObjectMappings: [ + { + pluginId: 'pluginA', + properties: { + typeA: { + properties: { + fieldA: { type: 'text' }, + }, + }, + typeB: { + properties: { + fieldB: { type: 'text' }, + anotherFieldB: { type: 'boolean' }, + }, + }, + }, + }, + { + pluginId: 'pluginB', + properties: { + typeC: { + properties: { + fieldC: { type: 'text' }, + }, + }, + }, + }, + ], + savedObjectMigrations: { + typeA: { + '1.0.0': jest.fn(), + '2.0.4': jest.fn(), + }, + typeC: { + '1.5.3': jest.fn(), + }, + }, + savedObjectSchemas: { + typeA: { + indexPattern: jest.fn(config => { + config.get('foo.bar'); + return 'myIndex'; + }), + hidden: true, + isNamespaceAgnostic: true, + }, + typeB: { + convertToAliasScript: 'some alias script', + hidden: false, + }, + }, + savedObjectValidations: {}, + }; + + const converted = convertLegacyTypes(uiExports, legacyConfig); + expect(converted).toMatchSnapshot(); + }); +}); + +describe('convertTypesToLegacySchema', () => { + it('converts types to the legacy schema format', () => { + const types: SavedObjectsType[] = [ + { + name: 'typeA', + hidden: false, + namespaceAgnostic: true, + mappings: { properties: {} }, + convertToAliasScript: 'some script', + }, + { + name: 'typeB', + hidden: true, + namespaceAgnostic: false, + indexPattern: 'myIndex', + mappings: { properties: {} }, + }, + ]; + expect(convertTypesToLegacySchema(types)).toEqual({ + typeA: { + hidden: false, + isNamespaceAgnostic: true, + convertToAliasScript: 'some script', + }, + typeB: { + hidden: true, + isNamespaceAgnostic: false, + indexPattern: 'myIndex', + }, + }); + }); +}); diff --git a/src/core/server/saved_objects/utils.ts b/src/core/server/saved_objects/utils.ts new file mode 100644 index 0000000000000..5c4d0ccb84b25 --- /dev/null +++ b/src/core/server/saved_objects/utils.ts @@ -0,0 +1,76 @@ +/* + * 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 { LegacyConfig } from '../legacy'; +import { SavedObjectsType, SavedObjectsLegacyUiExports } from './types'; +import { SavedObjectsSchemaDefinition } from './schema'; + +/** + * Converts the legacy savedObjects mappings, schema, and migrations + * to actual {@link SavedObjectsType | saved object types} + */ +export const convertLegacyTypes = ( + { + savedObjectMappings = [], + savedObjectMigrations = {}, + savedObjectSchemas = {}, + }: SavedObjectsLegacyUiExports, + legacyConfig: LegacyConfig +): SavedObjectsType[] => { + return savedObjectMappings.reduce((types, { pluginId, properties }) => { + return [ + ...types, + ...Object.entries(properties).map(([type, mappings]) => { + const schema = savedObjectSchemas[type]; + const migrations = savedObjectMigrations[type]; + return { + name: type, + hidden: schema?.hidden ?? false, + namespaceAgnostic: schema?.isNamespaceAgnostic ?? false, + mappings, + indexPattern: + typeof schema?.indexPattern === 'function' + ? schema.indexPattern(legacyConfig) + : schema?.indexPattern, + convertToAliasScript: schema?.convertToAliasScript, + migrations: migrations ?? {}, + }; + }), + ]; + }, [] as SavedObjectsType[]); +}; + +/** + * Convert {@link SavedObjectsType | saved object types} to the legacy {@link SavedObjectsSchemaDefinition | schema} format + */ +export const convertTypesToLegacySchema = ( + types: SavedObjectsType[] +): SavedObjectsSchemaDefinition => { + return types.reduce((schema, type) => { + return { + ...schema, + [type.name]: { + isNamespaceAgnostic: type.namespaceAgnostic, + hidden: type.hidden, + indexPattern: type.indexPattern, + convertToAliasScript: type.convertToAliasScript, + }, + }; + }, {} as SavedObjectsSchemaDefinition); +}; diff --git a/src/core/server/server.api.md b/src/core/server/server.api.md index fb27fcccc2abe..0a98c44cf58f5 100644 --- a/src/core/server/server.api.md +++ b/src/core/server/server.api.md @@ -1421,6 +1421,19 @@ export interface SavedObjectAttributes { // @public export type SavedObjectAttributeSingle = string | number | boolean | null | undefined | SavedObjectAttributes; +// Warning: (ae-forgotten-export) The symbol "SavedObjectUnsanitizedDoc" needs to be exported by the entry point index.d.ts +// Warning: (ae-missing-release-tag) "SavedObjectMigrationFn" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) +// Warning: (ae-unresolved-link) The @link reference could not be resolved: The package "kibana" does not have an export "SavedObjectUnsanitizedDoc" +// +// @public +export type SavedObjectMigrationFn = (doc: SavedObjectUnsanitizedDoc, log: SavedObjectsMigrationLogger) => SavedObjectUnsanitizedDoc; + +// @public +export interface SavedObjectMigrationMap { + // (undocumented) + [version: string]: SavedObjectMigrationFn; +} + // @public export interface SavedObjectReference { // (undocumented) @@ -1431,6 +1444,12 @@ export interface SavedObjectReference { type: string; } +// Warning: (ae-forgotten-export) The symbol "SavedObjectDoc" needs to be exported by the entry point index.d.ts +// Warning: (ae-forgotten-export) The symbol "Referencable" needs to be exported by the entry point index.d.ts +// +// @public (undocumented) +export type SavedObjectSanitizedDoc = SavedObjectDoc & Referencable; + // @public (undocumented) export interface SavedObjectsBaseOptions { namespace?: string; @@ -1534,6 +1553,32 @@ export interface SavedObjectsClientWrapperOptions { request: KibanaRequest; } +// @public +export interface SavedObjectsComplexFieldMapping { + // (undocumented) + dynamic?: string; + // (undocumented) + properties: SavedObjectsMappingProperties; + // (undocumented) + type?: string; +} + +// @public +export interface SavedObjectsCoreFieldMapping { + // (undocumented) + enabled?: boolean; + // (undocumented) + fields?: { + [subfield: string]: { + type: string; + }; + }; + // (undocumented) + index?: boolean; + // (undocumented) + type: string; +} + // @public (undocumented) export interface SavedObjectsCreateOptions extends SavedObjectsBaseOptions { id?: string; @@ -1629,6 +1674,9 @@ export interface SavedObjectsExportResultDetails { }>; } +// @public +export type SavedObjectsFieldMapping = SavedObjectsCoreFieldMapping | SavedObjectsComplexFieldMapping; + // @public (undocumented) export interface SavedObjectsFindOptions extends SavedObjectsBaseOptions { // (undocumented) @@ -1793,6 +1841,12 @@ export interface SavedObjectsLegacyService { types: string[]; } +// @public +export interface SavedObjectsMappingProperties { + // (undocumented) + [field: string]: SavedObjectsFieldMapping; +} + // @public (undocumented) export interface SavedObjectsMigrationLogger { // (undocumented) @@ -1800,6 +1854,8 @@ export interface SavedObjectsMigrationLogger { // (undocumented) info: (msg: string) => void; // (undocumented) + warn: (msg: string) => void; + // @deprecated (undocumented) warning: (msg: string) => void; } @@ -1809,8 +1865,6 @@ export interface SavedObjectsMigrationVersion { [pluginName: string]: string; } -// Warning: (ae-missing-release-tag) "RawDoc" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal) -// // @public export interface SavedObjectsRawDoc { // (undocumented) @@ -1819,8 +1873,10 @@ export interface SavedObjectsRawDoc { _primary_term?: number; // (undocumented) _seq_no?: number; + // Warning: (ae-forgotten-export) The symbol "SavedObjectsRawDocSource" needs to be exported by the entry point index.d.ts + // // (undocumented) - _source: any; + _source: SavedObjectsRawDocSource; // (undocumented) _type?: string; } @@ -1834,7 +1890,7 @@ export class SavedObjectsRepository { // Warning: (ae-forgotten-export) The symbol "KibanaMigrator" needs to be exported by the entry point index.d.ts // // @internal - static createRepository(migrator: KibanaMigrator, schema: SavedObjectsSchema, config: LegacyConfig, indexName: string, callCluster: APICaller, extraTypes?: string[], injectedConstructor?: any): ISavedObjectsRepository; + static createRepository(migrator: KibanaMigrator, typeRegistry: SavedObjectTypeRegistry, indexName: string, callCluster: APICaller, extraTypes?: string[], injectedConstructor?: any): ISavedObjectsRepository; delete(type: string, id: string, options?: SavedObjectsDeleteOptions): Promise<{}>; deleteByNamespace(namespace: string, options?: SavedObjectsDeleteByNamespaceOptions): Promise; // (undocumented) @@ -1873,7 +1929,7 @@ export interface SavedObjectsResolveImportErrorsOptions { supportedTypes: string[]; } -// @internal (undocumented) +// @internal @deprecated (undocumented) export class SavedObjectsSchema { // Warning: (ae-forgotten-export) The symbol "SavedObjectsSchemaDefinition" needs to be exported by the entry point index.d.ts constructor(schemaDefinition?: SavedObjectsSchemaDefinition); @@ -1887,14 +1943,16 @@ export class SavedObjectsSchema { isNamespaceAgnostic(type: string): boolean; } -// @internal (undocumented) +// @public export class SavedObjectsSerializer { - constructor(schema: SavedObjectsSchema); + // Warning: (ae-forgotten-export) The symbol "ISavedObjectTypeRegistry" needs to be exported by the entry point index.d.ts + // + // @internal + constructor(registry: ISavedObjectTypeRegistry); generateRawId(namespace: string | undefined, type: string, id?: string): string; - isRawSavedObject(rawDoc: SavedObjectsRawDoc): any; - // Warning: (ae-forgotten-export) The symbol "SanitizedSavedObjectDoc" needs to be exported by the entry point index.d.ts - rawToSavedObject(doc: SavedObjectsRawDoc): SanitizedSavedObjectDoc; - savedObjectToRaw(savedObj: SanitizedSavedObjectDoc): SavedObjectsRawDoc; + isRawSavedObject(rawDoc: SavedObjectsRawDoc): boolean; + rawToSavedObject(doc: SavedObjectsRawDoc): SavedObjectSanitizedDoc; + savedObjectToRaw(savedObj: SavedObjectSanitizedDoc): SavedObjectsRawDoc; } // @public @@ -1907,9 +1965,27 @@ export interface SavedObjectsServiceSetup { export interface SavedObjectsServiceStart { createInternalRepository: (extraTypes?: string[]) => ISavedObjectsRepository; createScopedRepository: (req: KibanaRequest, extraTypes?: string[]) => ISavedObjectsRepository; + createSerializer: () => SavedObjectsSerializer; getScopedClient: (req: KibanaRequest, options?: SavedObjectsClientProviderOptions) => SavedObjectsClientContract; } +// @public (undocumented) +export interface SavedObjectsType { + convertToAliasScript?: string; + hidden: boolean; + indexPattern?: string; + mappings: SavedObjectsTypeMappingDefinition; + migrations?: SavedObjectMigrationMap; + name: string; + namespaceAgnostic: boolean; +} + +// @public +export interface SavedObjectsTypeMappingDefinition { + // (undocumented) + properties: SavedObjectsMappingProperties; +} + // @public (undocumented) export interface SavedObjectsUpdateOptions extends SavedObjectsBaseOptions { references?: SavedObjectReference[]; @@ -1925,6 +2001,16 @@ export interface SavedObjectsUpdateResponse = { + const configKeys: Record = { 'xpack.task_manager.index': '.kibana_task_manager', }; + const config = { get: (path: string) => configKeys[path] }; + + const savedObjectTypes = convertLegacyTypes(uiExports, config as LegacyConfig); + const typeRegistry = new SavedObjectTypeRegistry(); + savedObjectTypes.forEach(type => typeRegistry.registerType(type)); const logger = { trace: log.verbose.bind(log), @@ -117,7 +124,6 @@ export async function migrateKibanaIndex({ }; const migratorOptions = { - config: { get: (path: string) => config[path] } as any, savedObjectsConfig: { scrollDuration: '5m', batchSize: 100, @@ -129,9 +135,7 @@ export async function migrateKibanaIndex({ } as any, logger, kibanaVersion, - savedObjectSchemas: new SavedObjectsSchema(uiExports.savedObjectSchemas), - savedObjectMappings: uiExports.savedObjectMappings, - savedObjectMigrations: uiExports.savedObjectMigrations, + typeRegistry, savedObjectValidations: uiExports.savedObjectValidations, callCluster: (path: string, ...args: any[]) => (get(client, path) as Function).call(client, ...args), diff --git a/src/legacy/core_plugins/kibana/public/dashboard/migrations/migrations_730.test.ts b/src/legacy/core_plugins/kibana/public/dashboard/migrations/migrations_730.test.ts index 6404b5bdbd63b..86d399d219a26 100644 --- a/src/legacy/core_plugins/kibana/public/dashboard/migrations/migrations_730.test.ts +++ b/src/legacy/core_plugins/kibana/public/dashboard/migrations/migrations_730.test.ts @@ -28,6 +28,7 @@ import { const mockLogger = { warning: () => {}, + warn: () => {}, debug: () => {}, info: () => {}, }; diff --git a/src/legacy/server/kbn_server.d.ts b/src/legacy/server/kbn_server.d.ts index 8da1b3b05fa76..a21a05e1e012a 100644 --- a/src/legacy/server/kbn_server.d.ts +++ b/src/legacy/server/kbn_server.d.ts @@ -114,6 +114,7 @@ export interface KibanaCore { elasticsearch: LegacyServiceSetupDeps['core']['elasticsearch']; hapiServer: LegacyServiceSetupDeps['core']['http']['server']; kibanaMigrator: LegacyServiceStartDeps['core']['savedObjects']['migrator']; + typeRegistry: LegacyServiceStartDeps['core']['savedObjects']['typeRegistry']; legacy: ILegacyInternals; rendering: LegacyServiceSetupDeps['core']['rendering']; uiPlugins: LegacyServiceSetupDeps['core']['plugins']['uiPlugins']; diff --git a/src/legacy/server/saved_objects/saved_objects_mixin.js b/src/legacy/server/saved_objects/saved_objects_mixin.js index e0d8b895af838..1d2f2d0f23e17 100644 --- a/src/legacy/server/saved_objects/saved_objects_mixin.js +++ b/src/legacy/server/saved_objects/saved_objects_mixin.js @@ -20,7 +20,6 @@ // Disable lint errors for imports from src/core/server/saved_objects until SavedObjects migration is complete /* eslint-disable @kbn/eslint/no-restricted-paths */ import { SavedObjectsSchema } from '../../../core/server/saved_objects/schema'; -import { SavedObjectsSerializer } from '../../../core/server/saved_objects/serialization'; import { SavedObjectsClient, SavedObjectsRepository, @@ -29,6 +28,7 @@ import { resolveImportErrors, } from '../../../core/server/saved_objects'; import { getRootPropertiesObjects } from '../../../core/server/saved_objects/mappings'; +import { convertTypesToLegacySchema } from '../../../core/server/saved_objects/utils'; import { SavedObjectsManagement } from '../../../core/server/saved_objects/management'; import { @@ -57,9 +57,10 @@ function getImportableAndExportableTypes({ kbnServer, visibleTypes }) { export function savedObjectsMixin(kbnServer, server) { const migrator = kbnServer.newPlatform.__internals.kibanaMigrator; + const typeRegistry = kbnServer.newPlatform.__internals.typeRegistry; const mappings = migrator.getActiveMappings(); const allTypes = Object.keys(getRootPropertiesObjects(mappings)); - const schema = new SavedObjectsSchema(kbnServer.uiExports.savedObjectSchemas); + const schema = new SavedObjectsSchema(convertTypesToLegacySchema(typeRegistry.getAllTypes())); const visibleTypes = allTypes.filter(type => !schema.isHiddenType(type)); const importableAndExportableTypes = getImportableAndExportableTypes({ kbnServer, visibleTypes }); @@ -99,7 +100,7 @@ export function savedObjectsMixin(kbnServer, server) { server.route(createResolveImportErrorsRoute(prereqs, server, importableAndExportableTypes)); server.route(createLogLegacyImportRoute()); - const serializer = new SavedObjectsSerializer(schema); + const serializer = kbnServer.newPlatform.start.core.savedObjects.createSerializer(); const createRepository = (callCluster, extraTypes = []) => { if (typeof callCluster !== 'function') { @@ -118,10 +119,9 @@ export function savedObjectsMixin(kbnServer, server) { return new SavedObjectsRepository({ index: config.get('kibana.index'), - config, migrator, mappings, - schema, + typeRegistry, serializer, allowedTypes, callCluster, diff --git a/src/legacy/server/saved_objects/saved_objects_mixin.test.js b/src/legacy/server/saved_objects/saved_objects_mixin.test.js index 691878cf66d27..9ca0374b959f6 100644 --- a/src/legacy/server/saved_objects/saved_objects_mixin.test.js +++ b/src/legacy/server/saved_objects/saved_objects_mixin.test.js @@ -22,6 +22,14 @@ import { savedObjectsMixin } from './saved_objects_mixin'; import { mockKibanaMigrator } from '../../../core/server/saved_objects/migrations/kibana/kibana_migrator.mock'; // eslint-disable-next-line @kbn/eslint/no-restricted-paths import { savedObjectsClientProviderMock } from '../../../core/server/saved_objects/service/lib/scoped_client_provider.mock'; +// eslint-disable-next-line @kbn/eslint/no-restricted-paths +import { convertLegacyTypes } from '../../../core/server/saved_objects/utils'; +import { SavedObjectTypeRegistry } from '../../../core/server'; +import { coreMock } from '../../../core/server/mocks'; + +const mockConfig = { + get: jest.fn().mockReturnValue('anything'), +}; const savedObjectMappings = [ { @@ -61,7 +69,30 @@ const savedObjectMappings = [ }, ]; -const migrator = mockKibanaMigrator.create({ savedObjectMappings }); +const savedObjectSchemas = { + hiddentype: { + hidden: true, + }, + doc1: { + indexPattern: 'other-index', + }, +}; + +const savedObjectTypes = convertLegacyTypes( + { + savedObjectMappings, + savedObjectSchemas, + savedObjectMigrations: {}, + }, + mockConfig +); + +const typeRegistry = new SavedObjectTypeRegistry(); +savedObjectTypes.forEach(type => typeRegistry.registerType(type)); + +const migrator = mockKibanaMigrator.create({ + types: savedObjectTypes, +}); describe('Saved Objects Mixin', () => { let mockKbnServer; @@ -113,7 +144,17 @@ describe('Saved Objects Mixin', () => { }; mockKbnServer = { newPlatform: { - __internals: { kibanaMigrator: migrator, savedObjectsClientProvider: clientProvider }, + __internals: { + kibanaMigrator: migrator, + savedObjectsClientProvider: clientProvider, + typeRegistry, + }, + setup: { + core: coreMock.createSetup(), + }, + start: { + core: coreMock.createStart(), + }, }, server: mockServer, ready: () => {}, @@ -124,14 +165,7 @@ describe('Saved Objects Mixin', () => { }, uiExports: { savedObjectMappings, - savedObjectSchemas: { - hiddentype: { - hidden: true, - }, - doc1: { - indexPattern: 'other-index', - }, - }, + savedObjectSchemas, }, }; }); diff --git a/test/api_integration/apis/saved_objects/migrations.js b/test/api_integration/apis/saved_objects/migrations.js index 58a07fd87c0f9..bd207ccb41b20 100644 --- a/test/api_integration/apis/saved_objects/migrations.js +++ b/test/api_integration/apis/saved_objects/migrations.js @@ -27,8 +27,10 @@ import { DocumentMigrator, IndexMigrator, } from '../../../../src/core/server/saved_objects/migrations/core'; -import { SavedObjectsSerializer } from '../../../../src/core/server/saved_objects/serialization'; -import { SavedObjectsSchema } from '../../../../src/core/server/saved_objects/schema'; +import { + SavedObjectsSerializer, + SavedObjectTypeRegistry, +} from '../../../../src/core/server/saved_objects'; export default ({ getService }) => { const es = getService('legacyEs'); @@ -350,10 +352,15 @@ async function migrateIndex({ validateDoc, obsoleteIndexTemplatePattern, }) { + const typeRegistry = new SavedObjectTypeRegistry(); + const types = migrationsToTypes(migrations); + types.forEach(type => typeRegistry.registerType(type)); + const documentMigrator = new DocumentMigrator({ kibanaVersion: '99.9.9', - migrations, + typeRegistry, validateDoc: validateDoc || _.noop, + log: { info: _.noop, debug: _.noop, warn: _.noop }, }); const migrator = new IndexMigrator({ @@ -366,12 +373,22 @@ async function migrateIndex({ log: { info: _.noop, debug: _.noop, warn: _.noop }, pollInterval: 50, scrollDuration: '5m', - serializer: new SavedObjectsSerializer(new SavedObjectsSchema()), + serializer: new SavedObjectsSerializer(typeRegistry), }); return await migrator.migrate(); } +function migrationsToTypes(migrations) { + return Object.entries(migrations).map(([type, migrations]) => ({ + name: type, + hidden: false, + namespaceAgnostic: false, + mappings: { properties: {} }, + migrations: { ...migrations }, + })); +} + async function fetchDocs({ callCluster, index }) { const { hits: { hits }, diff --git a/x-pack/legacy/plugins/task_manager/server/index.ts b/x-pack/legacy/plugins/task_manager/server/index.ts index 98d67a38c20d0..ff25d8a1e0e5d 100644 --- a/x-pack/legacy/plugins/task_manager/server/index.ts +++ b/x-pack/legacy/plugins/task_manager/server/index.ts @@ -51,9 +51,7 @@ export function taskManager(kibana: any) { server.expose( createLegacyApi( getTaskManagerSetup(server)! - .registerLegacyAPI({ - savedObjectSchemas, - }) + .registerLegacyAPI({}) .then((taskManagerPlugin: TaskManager) => { // we can't tell the Kibana Platform Task Manager plugin to // to wait to `start` as that happens before legacy plugins diff --git a/x-pack/plugins/task_manager/server/create_task_manager.test.ts b/x-pack/plugins/task_manager/server/create_task_manager.test.ts index f4deeb1ea02ed..34258e15f45d1 100644 --- a/x-pack/plugins/task_manager/server/create_task_manager.test.ts +++ b/x-pack/plugins/task_manager/server/create_task_manager.test.ts @@ -6,8 +6,11 @@ import { createTaskManager, LegacyDeps } from './create_task_manager'; import { mockLogger } from './test_utils'; -import { CoreSetup, UuidServiceSetup } from 'kibana/server'; -import { savedObjectsRepositoryMock } from '../../../../src/core/server/mocks'; +import { CoreSetup, SavedObjectsSerializer, UuidServiceSetup } from '../../../../src/core/server'; +import { + savedObjectsRepositoryMock, + savedObjectsTypeRegistryMock, +} from '../../../../src/core/server/mocks'; jest.mock('./task_manager'); @@ -23,7 +26,7 @@ describe('createTaskManager', () => { const getMockLegacyDeps = (): LegacyDeps => ({ config: {}, - savedObjectSchemas: {}, + savedObjectsSerializer: new SavedObjectsSerializer(savedObjectsTypeRegistryMock.create()), elasticsearch: { callAsInternalUser: jest.fn(), }, diff --git a/x-pack/plugins/task_manager/server/create_task_manager.ts b/x-pack/plugins/task_manager/server/create_task_manager.ts index 5c66b8ba5bd58..9ff97bbcc17e6 100644 --- a/x-pack/plugins/task_manager/server/create_task_manager.ts +++ b/x-pack/plugins/task_manager/server/create_task_manager.ts @@ -7,7 +7,6 @@ import { IClusterClient, SavedObjectsSerializer, - SavedObjectsSchema, CoreSetup, ISavedObjectsRepository, } from '../../../../src/core/server'; @@ -16,9 +15,9 @@ import { Logger } from './types'; export interface LegacyDeps { config: any; - savedObjectSchemas: any; elasticsearch: Pick; savedObjectsRepository: ISavedObjectsRepository; + savedObjectsSerializer: SavedObjectsSerializer; logger: Logger; } @@ -27,19 +26,16 @@ export function createTaskManager( { logger, config, - savedObjectSchemas, elasticsearch: { callAsInternalUser }, savedObjectsRepository, + savedObjectsSerializer, }: LegacyDeps ) { - // as we use this Schema solely to interact with Tasks, we - // can initialise it with solely the Tasks schema - const serializer = new SavedObjectsSerializer(new SavedObjectsSchema(savedObjectSchemas)); return new TaskManager({ taskManagerId: core.uuid.getInstanceUuid(), config, savedObjectsRepository, - serializer, + serializer: savedObjectsSerializer, callAsInternalUser, logger, }); diff --git a/x-pack/plugins/task_manager/server/plugin.ts b/x-pack/plugins/task_manager/server/plugin.ts index 8b09f8f2b445a..f3dff2a88f53a 100644 --- a/x-pack/plugins/task_manager/server/plugin.ts +++ b/x-pack/plugins/task_manager/server/plugin.ts @@ -9,11 +9,12 @@ import { first } from 'rxjs/operators'; import { once } from 'lodash'; import { TaskDictionary, TaskDefinition } from './task'; import { TaskManager } from './task_manager'; -import { createTaskManager, LegacyDeps } from './create_task_manager'; +import { createTaskManager } from './create_task_manager'; import { TaskManagerConfig } from './config'; import { Middleware } from './lib/middleware'; -export type PluginLegacyDependencies = Pick; +// eslint-disable-next-line @typescript-eslint/no-empty-interface +export interface PluginLegacyDependencies {} export type TaskManagerSetupContract = { config$: Observable; registerLegacyAPI: (legacyDependencies: PluginLegacyDependencies) => Promise; @@ -51,7 +52,7 @@ export class TaskManagerPlugin config, elasticsearch, savedObjectsRepository, - ...__LEGACY, + savedObjectsSerializer: savedObjects.createSerializer(), }) ); this.legacyTaskManager$.complete(); diff --git a/x-pack/plugins/task_manager/server/task_manager.test.ts b/x-pack/plugins/task_manager/server/task_manager.test.ts index a65723b2e8de7..3d48ce18c9d6a 100644 --- a/x-pack/plugins/task_manager/server/task_manager.test.ts +++ b/x-pack/plugins/task_manager/server/task_manager.test.ts @@ -21,13 +21,13 @@ import { TaskLifecycleEvent, } from './task_manager'; import { savedObjectsRepositoryMock } from '../../../../src/core/server/mocks'; -import { SavedObjectsSerializer, SavedObjectsSchema } from '../../../../src/core/server'; +import { SavedObjectsSerializer, SavedObjectTypeRegistry } from '../../../../src/core/server'; import { mockLogger } from './test_utils'; import { asErr, asOk } from './lib/result_type'; import { ConcreteTaskInstance, TaskLifecycleResult, TaskStatus } from './task'; const savedObjectsClient = savedObjectsRepositoryMock.create(); -const serializer = new SavedObjectsSerializer(new SavedObjectsSchema()); +const serializer = new SavedObjectsSerializer(new SavedObjectTypeRegistry()); describe('TaskManager', () => { let clock: sinon.SinonFakeTimers; diff --git a/x-pack/plugins/task_manager/server/task_store.test.ts b/x-pack/plugins/task_manager/server/task_store.test.ts index e6cce7a664d91..6ed6ae16fa0f9 100644 --- a/x-pack/plugins/task_manager/server/task_store.test.ts +++ b/x-pack/plugins/task_manager/server/task_store.test.ts @@ -20,7 +20,7 @@ import { StoreOpts, OwnershipClaimingOpts, TaskStore, SearchOpts } from './task_ import { savedObjectsRepositoryMock } from '../../../../src/core/server/mocks'; import { SavedObjectsSerializer, - SavedObjectsSchema, + SavedObjectTypeRegistry, SavedObjectAttributes, } from '../../../../src/core/server'; import { SavedObjectsErrorHelpers } from '../../../../src/core/server/saved_objects/service/lib/errors'; @@ -46,7 +46,7 @@ const taskDefinitions: TaskDictionary = { }; const savedObjectsClient = savedObjectsRepositoryMock.create(); -const serializer = new SavedObjectsSerializer(new SavedObjectsSchema()); +const serializer = new SavedObjectsSerializer(new SavedObjectTypeRegistry()); beforeEach(() => jest.resetAllMocks());