Skip to content

Commit

Permalink
Invalid searchSourceJSON causes saved object migration to fail (ela…
Browse files Browse the repository at this point in the history
…stic#78535)

* Invalid `searchSourceJSON` causes saved object migration to fail

Closes: elastic#78530

* 7.8.2 -> 7.9.3

* return migration into 6.7.2
  • Loading branch information
alexwizp committed Sep 30, 2020
1 parent 4f15a84 commit fc995b0
Show file tree
Hide file tree
Showing 7 changed files with 160 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,5 @@ export const dashboardSavedObjectTypeMigrations = {
'6.7.2': flow(migrateMatchAllQuery),
'7.0.0': flow(migrations700),
'7.3.0': flow(migrations730),
'7.9.3': flow(migrateMatchAllQuery),
};
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,21 @@ describe('migrate match_all query', () => {
},
});
});

it('should return original doc if searchSourceJSON cannot be parsed', () => {
const migratedDoc = migrateMatchAllQuery(
{
attributes: {
kibanaSavedObjectMeta: 'kibanaSavedObjectMeta',
},
} as Parameters<SavedObjectMigrationFn>[0],
savedObjectMigrationContext
);

expect(migratedDoc).toEqual({
attributes: {
kibanaSavedObjectMeta: 'kibanaSavedObjectMeta',
},
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ import { SavedObjectMigrationFn } from 'kibana/server';
import { get } from 'lodash';
import { DEFAULT_QUERY_LANGUAGE } from '../../../data/common';

/**
* This migration script is related to:
* @link https://github.com/elastic/kibana/pull/62194
* @link https://github.com/elastic/kibana/pull/14644
* This is only a problem when you import an object from 5.x into 6.x but to be sure that all saved objects migrated we should execute it twice in 6.7.2 and 7.9.3
*/
export const migrateMatchAllQuery: SavedObjectMigrationFn<any, any> = (doc) => {
const searchSourceJSON = get(doc, 'attributes.kibanaSavedObjectMeta.searchSourceJSON');

Expand All @@ -31,6 +37,7 @@ export const migrateMatchAllQuery: SavedObjectMigrationFn<any, any> = (doc) => {
searchSource = JSON.parse(searchSourceJSON);
} catch (e) {
// Let it go, the data is invalid and we'll leave it as is
return doc;
}

if (searchSource.query?.match_all) {
Expand Down
85 changes: 59 additions & 26 deletions src/plugins/discover/server/saved_objects/search_migrations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,36 +22,61 @@ import { searchMigrations } from './search_migrations';

const savedObjectMigrationContext = (null as unknown) as SavedObjectMigrationContext;

const testMigrateMatchAllQuery = (migrationFn: Function) => {
it('should migrate obsolete match_all query', () => {
const migratedDoc = migrationFn(
{
type: 'search',
attributes: {
kibanaSavedObjectMeta: {
searchSourceJSON: JSON.stringify({
query: {
match_all: {},
},
}),
},
},
},
savedObjectMigrationContext
);
const migratedSearchSource = JSON.parse(
migratedDoc.attributes.kibanaSavedObjectMeta.searchSourceJSON
);

expect(migratedSearchSource).toEqual({
query: {
query: '',
language: 'kuery',
},
});
});

it('should return original doc if searchSourceJSON cannot be parsed', () => {
const migratedDoc = migrationFn(
{
type: 'search',
attributes: {
kibanaSavedObjectMeta: 'kibanaSavedObjectMeta',
},
},
savedObjectMigrationContext
);

expect(migratedDoc).toEqual({
type: 'search',
attributes: {
kibanaSavedObjectMeta: 'kibanaSavedObjectMeta',
},
});
});
};

describe('migration search', () => {
describe('6.7.2', () => {
const migrationFn = searchMigrations['6.7.2'];

it('should migrate obsolete match_all query', () => {
const migratedDoc = migrationFn(
{
type: 'search',
attributes: {
kibanaSavedObjectMeta: {
searchSourceJSON: JSON.stringify({
query: {
match_all: {},
},
}),
},
},
},
savedObjectMigrationContext
);
const migratedSearchSource = JSON.parse(
migratedDoc.attributes.kibanaSavedObjectMeta.searchSourceJSON
);

expect(migratedSearchSource).toEqual({
query: {
query: '',
language: 'kuery',
},
});
describe('migrateMatchAllQuery', () => {
testMigrateMatchAllQuery(migrationFn);
});
});

Expand Down Expand Up @@ -328,4 +353,12 @@ Object {
expect(migratedDoc).toEqual(doc);
});
});

describe('7.9.3', () => {
const migrationFn = searchMigrations['7.9.3'];

describe('migrateMatchAllQuery', () => {
testMigrateMatchAllQuery(migrationFn);
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ import { flow, get } from 'lodash';
import { SavedObjectMigrationFn } from 'kibana/server';
import { DEFAULT_QUERY_LANGUAGE } from '../../../data/common';

/**
* This migration script is related to:
* @link https://github.com/elastic/kibana/pull/62194
* @link https://github.com/elastic/kibana/pull/14644
* This is only a problem when you import an object from 5.x into 6.x but to be sure that all saved objects migrated we should execute it twice in 6.7.2 and 7.9.3
*/
const migrateMatchAllQuery: SavedObjectMigrationFn<any, any> = (doc) => {
const searchSourceJSON = get(doc, 'attributes.kibanaSavedObjectMeta.searchSourceJSON');

Expand All @@ -31,6 +37,7 @@ const migrateMatchAllQuery: SavedObjectMigrationFn<any, any> = (doc) => {
searchSource = JSON.parse(searchSourceJSON);
} catch (e) {
// Let it go, the data is invalid and we'll leave it as is
return doc;
}

if (searchSource.query?.match_all) {
Expand Down Expand Up @@ -125,4 +132,5 @@ export const searchMigrations = {
'6.7.2': flow(migrateMatchAllQuery),
'7.0.0': flow(setNewReferences),
'7.4.0': flow(migrateSearchSortToNestedArray),
'7.9.3': flow(migrateMatchAllQuery),
};
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,50 @@ import { SavedObjectMigrationContext, SavedObjectMigrationFn } from 'kibana/serv

const savedObjectMigrationContext = (null as unknown) as SavedObjectMigrationContext;

const testMigrateMatchAllQuery = (migrate: Function) => {
it('should migrate obsolete match_all query', () => {
const migratedDoc = migrate({
type: 'area',
attributes: {
kibanaSavedObjectMeta: {
searchSourceJSON: JSON.stringify({
query: {
match_all: {},
},
}),
},
},
});

const migratedSearchSource = JSON.parse(
migratedDoc.attributes.kibanaSavedObjectMeta.searchSourceJSON
);

expect(migratedSearchSource).toEqual({
query: {
query: '',
language: 'kuery',
},
});
});

it('should return original doc if searchSourceJSON cannot be parsed', () => {
const migratedDoc = migrate({
type: 'area',
attributes: {
kibanaSavedObjectMeta: 'kibanaSavedObjectMeta',
},
});

expect(migratedDoc).toEqual({
type: 'area',
attributes: {
kibanaSavedObjectMeta: 'kibanaSavedObjectMeta',
},
});
});
};

describe('migration visualization', () => {
describe('6.7.2', () => {
const migrate = (doc: any) =>
Expand All @@ -31,6 +75,10 @@ describe('migration visualization', () => {
);
let doc: any;

describe('migrateMatchAllQuery', () => {
testMigrateMatchAllQuery(migrate);
});

describe('date histogram time zone removal', () => {
beforeEach(() => {
doc = {
Expand Down Expand Up @@ -150,32 +198,6 @@ describe('migration visualization', () => {
expect(aggs[3]).not.toHaveProperty('params.customBucket.params.time_zone');
expect(aggs[2]).not.toHaveProperty('params.time_zone');
});

it('should migrate obsolete match_all query', () => {
const migratedDoc = migrate({
...doc,
attributes: {
...doc.attributes,
kibanaSavedObjectMeta: {
searchSourceJSON: JSON.stringify({
query: {
match_all: {},
},
}),
},
},
});
const migratedSearchSource = JSON.parse(
migratedDoc.attributes.kibanaSavedObjectMeta.searchSourceJSON
);

expect(migratedSearchSource).toEqual({
query: {
query: '',
language: 'kuery',
},
});
});
});
});

Expand Down Expand Up @@ -1487,6 +1509,18 @@ describe('migration visualization', () => {
});
});

describe('7.9.3', () => {
const migrate = (doc: any) =>
visualizationSavedObjectTypeMigrations['7.9.3'](
doc as Parameters<SavedObjectMigrationFn>[0],
savedObjectMigrationContext
);

describe('migrateMatchAllQuery', () => {
testMigrateMatchAllQuery(migrate);
});
});

describe('7.8.0 tsvb split_color_mode', () => {
const migrate = (doc: any) =>
visualizationSavedObjectTypeMigrations['7.8.0'](
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,12 @@ const migrateTableSplits: SavedObjectMigrationFn<any, any> = (doc) => {
}
};

/**
* This migration script is related to:
* @link https://github.com/elastic/kibana/pull/62194
* @link https://github.com/elastic/kibana/pull/14644
* This is only a problem when you import an object from 5.x into 6.x but to be sure that all saved objects migrated we should execute it twice in 6.7.2 and 7.9.3
*/
const migrateMatchAllQuery: SavedObjectMigrationFn<any, any> = (doc) => {
const searchSourceJSON = get(doc, 'attributes.kibanaSavedObjectMeta.searchSourceJSON');

Expand All @@ -665,6 +671,7 @@ const migrateMatchAllQuery: SavedObjectMigrationFn<any, any> = (doc) => {
searchSource = JSON.parse(searchSourceJSON);
} catch (e) {
// Let it go, the data is invalid and we'll leave it as is
return doc;
}

if (searchSource.query?.match_all) {
Expand Down Expand Up @@ -781,5 +788,6 @@ export const visualizationSavedObjectTypeMigrations = {
'7.4.2': flow(transformSplitFiltersStringToQueryObject),
'7.7.0': flow(migrateOperatorKeyTypo, migrateSplitByChartRow),
'7.8.0': flow(migrateTsvbDefaultColorPalettes),
'7.9.3': flow(migrateMatchAllQuery),
'7.10.0': flow(migrateFilterRatioQuery, removeTSVBSearchSource),
};

0 comments on commit fc995b0

Please sign in to comment.