From 7742bf2215f7f5d5dc7b4e6818df5f06ce6aefb6 Mon Sep 17 00:00:00 2001 From: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> Date: Wed, 27 Sep 2023 06:01:03 -0400 Subject: [PATCH] [8.10] [Saved Objects] Fix document migrator to prevent losing namespaces on import (#164848) (#167372) # Backport This will backport the following commits from `main` to `8.10`: - [[Saved Objects] Fix document migrator to prevent losing namespaces on import (#164848)](https://github.com/elastic/kibana/pull/164848) ### Questions ? Please refer to the [Backport tool documentation](https://github.com/sqren/backport) Co-authored-by: Michael Dokolin --- .../document_migrator.test.ts | 30 +++++++++++++++++++ .../document_migrator/internal_transforms.ts | 7 +++++ 2 files changed, 37 insertions(+) diff --git a/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/document_migrator/document_migrator.test.ts b/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/document_migrator/document_migrator.test.ts index 2e3a99b4b463e..43cf127a9ab19 100644 --- a/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/document_migrator/document_migrator.test.ts +++ b/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/document_migrator/document_migrator.test.ts @@ -713,6 +713,36 @@ describe('DocumentMigrator', () => { ]); }); + it('does not lose namespaces in documents with undefined namespace and defined namespaces property', () => { + const migrator = new DocumentMigrator({ + ...testOpts(), + typeRegistry: createRegistry( + { name: 'dog', namespaceType: 'multiple', convertToMultiNamespaceTypeVersion: '1.0.0' } + // no migration transforms are defined, the typeMigrationVersion will be derived from 'convertToMultiNamespaceTypeVersion' + ), + }); + migrator.prepareMigrations(); + const obj = { + id: 'mischievous', + type: 'dog', + attributes: { name: 'Ann' }, + coreMigrationVersion: kibanaVersion, + typeMigrationVersion: '0.1.0', + namespaces: ['something'], + } as SavedObjectUnsanitizedDoc; + const actual = migrator.migrateAndConvert(obj); + expect(actual).toEqual([ + { + id: 'mischievous', + type: 'dog', + attributes: { name: 'Ann' }, + coreMigrationVersion: kibanaVersion, + typeMigrationVersion: '1.0.0', + namespaces: ['something'], + }, + ]); + }); + it('does not fail when encountering documents with coreMigrationVersion higher than the latest known', () => { const migrator = new DocumentMigrator({ ...testOpts(), diff --git a/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/document_migrator/internal_transforms.ts b/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/document_migrator/internal_transforms.ts index 762fa20342c49..11090badd9173 100644 --- a/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/document_migrator/internal_transforms.ts +++ b/packages/core/saved-objects/core-saved-objects-migration-server-internal/src/document_migrator/internal_transforms.ts @@ -105,6 +105,13 @@ function convertNamespaceType(doc: SavedObjectUnsanitizedDoc) { const { namespace, ...otherAttrs } = doc; const additionalDocs: SavedObjectUnsanitizedDoc[] = []; + if (namespace == null && otherAttrs.namespaces) { + return { + additionalDocs, + transformedDoc: otherAttrs, + }; + } + // If this object exists in the default namespace, return it with the appropriate `namespaces` field without changing its ID. if (namespace === undefined) { return {