Skip to content

Commit

Permalink
Update validation in the document migrator
Browse files Browse the repository at this point in the history
Added validation to ensure that documents with a newer
coreMigrationVersion will result in an error. Also renamed some
existing tests to be more accurate.
  • Loading branch information
jportner committed Jan 15, 2021
1 parent b88b376 commit c486690
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -368,10 +368,9 @@ describe('DocumentMigrator', () => {
});
});

it('rejects docs that belong to a newer Kibana instance', () => {
it('rejects docs with a migrationVersion[type] for a type that does not have any migrations defined', () => {
const migrator = new DocumentMigrator({
...testOpts(),
kibanaVersion: '8.0.1',
});
expect(() =>
migrator.migrate({
Expand All @@ -385,7 +384,7 @@ describe('DocumentMigrator', () => {
);
});

it('rejects docs that belong to a newer plugin', () => {
it('rejects docs with a migrationVersion[type] for a type that does not have a migration >= that version defined', () => {
const migrator = new DocumentMigrator({
...testOpts(),
typeRegistry: createRegistry({
Expand All @@ -407,6 +406,40 @@ describe('DocumentMigrator', () => {
);
});

it('rejects docs that have an invalid coreMigrationVersion', () => {
const migrator = new DocumentMigrator({
...testOpts(),
kibanaVersion: '8.0.1',
});
expect(() =>
migrator.migrate({
id: 'happy',
type: 'dog',
attributes: { name: 'Callie' },
coreMigrationVersion: 'not-a-semver',
})
).toThrowErrorMatchingInlineSnapshot(
`"Document \\"happy\\" has an invalid \\"coreMigrationVersion\\" [not-a-semver]. This must be a semver value."`
);
});

it('rejects docs that have a coreMigrationVersion higher than the current Kibana version', () => {
const migrator = new DocumentMigrator({
...testOpts(),
kibanaVersion: '8.0.1',
});
expect(() =>
migrator.migrate({
id: 'wet',
type: 'dog',
attributes: { name: 'Callie' },
coreMigrationVersion: '8.0.2',
})
).toThrowErrorMatchingInlineSnapshot(
`"Document \\"wet\\" has a \\"coreMigrationVersion\\" which belongs to a more recent version of Kibana [8.0.2]. The current version is [8.0.1]."`
);
});

it('applies migrations in order', () => {
let count = 0;
const migrator = new DocumentMigrator({
Expand Down
27 changes: 27 additions & 0 deletions src/core/server/saved_objects/migrations/core/document_migrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,8 @@ function buildDocumentTransform({
doc: SavedObjectUnsanitizedDoc,
options: TransformOptions = {}
) {
validateCoreMigrationVersion(doc, kibanaVersion);

const { convertNamespaceTypes = false } = options;
let transformedDoc: SavedObjectUnsanitizedDoc;
let additionalDocs: SavedObjectUnsanitizedDoc[] = [];
Expand All @@ -393,6 +395,31 @@ function buildDocumentTransform({
};
}

function validateCoreMigrationVersion(doc: SavedObjectUnsanitizedDoc, kibanaVersion: string) {
const { id, coreMigrationVersion: docVersion } = doc;
if (!docVersion) {
return;
}

// We verify that the object's coreMigrationVersion is valid, and that it is not greater than the version supported by Kibana.
// If we have a coreMigrationVersion and the kibanaVersion is smaller than it or does not exist, we are dealing with a document that
// belongs to a future Kibana / plugin version.
if (!Semver.valid(docVersion)) {
throw Boom.badData(
`Document "${id}" has an invalid "coreMigrationVersion" [${docVersion}]. This must be a semver value.`,
doc
);
}

if (doc.coreMigrationVersion && Semver.gt(docVersion, kibanaVersion)) {
throw Boom.badData(
`Document "${id}" has a "coreMigrationVersion" which belongs to a more recent version` +
` of Kibana [${docVersion}]. The current version is [${kibanaVersion}].`,
doc
);
}
}

function applyMigrations(
doc: SavedObjectUnsanitizedDoc,
migrations: ActiveMigrations,
Expand Down

0 comments on commit c486690

Please sign in to comment.