From 6e45a3148b14f0183241fd952ccdd7856f4b254d Mon Sep 17 00:00:00 2001 From: Matthias Wilhelm Date: Wed, 9 Oct 2019 16:05:35 +0200 Subject: [PATCH 1/7] Refactor defaultIndex logic - setting uiRoutes: requireDefaultIndex to false - retrieve defaultIndex if necessary in discover route - before it was set in load_default.js --- .../public/discover/controllers/discover.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/legacy/core_plugins/kibana/public/discover/controllers/discover.js b/src/legacy/core_plugins/kibana/public/discover/controllers/discover.js index f79689ac8c733..1290f900b2ef1 100644 --- a/src/legacy/core_plugins/kibana/public/discover/controllers/discover.js +++ b/src/legacy/core_plugins/kibana/public/discover/controllers/discover.js @@ -91,7 +91,7 @@ const app = uiModules.get('apps/discover', [ uiRoutes .defaults(/^\/discover(\/|$)/, { - requireDefaultIndex: true, + requireDefaultIndex: false, requireUICapability: 'discover.show', k7Breadcrumbs: ($route, $injector) => $injector.invoke( @@ -132,17 +132,26 @@ uiRoutes * @type {State} */ const state = new State('_a', {}); + /** + * returns the id of the default index, if none is configured + * the value of the first available indexpattern/saved object is used + */ + const getDefaultIndexId = () => { + if(config.get('defaultIndex')) { + return config.get('defaultIndex'); + } + return !savedObjects.length ? '' : savedObjects[0].id; + }; - const specified = !!state.index; const exists = _.findIndex(savedObjects, o => o.id === state.index) > -1; - const id = exists ? state.index : config.get('defaultIndex'); + const id = exists ? state.index : getDefaultIndexId(); state.destroy(); return Promise.props({ list: savedObjects, loaded: indexPatterns.get(id), stateVal: state.index, - stateValFound: specified && exists + stateValFound: !!state.index && exists }); }); }, From 431b2bd624f507202bc9e4da8b3bdf364a8197d1 Mon Sep 17 00:00:00 2001 From: Matthias Wilhelm Date: Thu, 10 Oct 2019 18:15:01 +0200 Subject: [PATCH 2/7] Take care of case when no index patterns are available --- .../kibana/public/discover/controllers/discover.js | 1 + .../kibana/public/management/route_setup/load_default.js | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/legacy/core_plugins/kibana/public/discover/controllers/discover.js b/src/legacy/core_plugins/kibana/public/discover/controllers/discover.js index 1290f900b2ef1..0d743811fddc5 100644 --- a/src/legacy/core_plugins/kibana/public/discover/controllers/discover.js +++ b/src/legacy/core_plugins/kibana/public/discover/controllers/discover.js @@ -92,6 +92,7 @@ const app = uiModules.get('apps/discover', [ uiRoutes .defaults(/^\/discover(\/|$)/, { requireDefaultIndex: false, + requireIndexPatternLength: true, requireUICapability: 'discover.show', k7Breadcrumbs: ($route, $injector) => $injector.invoke( diff --git a/src/legacy/core_plugins/kibana/public/management/route_setup/load_default.js b/src/legacy/core_plugins/kibana/public/management/route_setup/load_default.js index f797acbe8888e..42c87af6cd77c 100644 --- a/src/legacy/core_plugins/kibana/public/management/route_setup/load_default.js +++ b/src/legacy/core_plugins/kibana/public/management/route_setup/load_default.js @@ -67,12 +67,16 @@ export default function (opts) { .addSetupWork(function loadDefaultIndexPattern(Promise, $route, config, indexPatterns) { const route = _.get($route, 'current.$$route'); - if (!route.requireDefaultIndex) { + if (!route.requireDefaultIndex && !route.requireIndexPatternLength) { return; } return indexPatterns.getIds() .then(function (patterns) { + if (route.requireIndexPatternLength && !patterns.length) { + throw new NoDefaultIndexPattern(); + } + let defaultId = config.get('defaultIndex'); let defined = !!defaultId; const exists = _.contains(patterns, defaultId); From af36902ad6486ff2dcc5ca806ef0f356abbc204f Mon Sep 17 00:00:00 2001 From: Matthias Wilhelm Date: Fri, 11 Oct 2019 14:23:52 +0200 Subject: [PATCH 3/7] Improve logic --- .../public/management/route_setup/load_default.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/legacy/core_plugins/kibana/public/management/route_setup/load_default.js b/src/legacy/core_plugins/kibana/public/management/route_setup/load_default.js index 42c87af6cd77c..730bc70f50ddf 100644 --- a/src/legacy/core_plugins/kibana/public/management/route_setup/load_default.js +++ b/src/legacy/core_plugins/kibana/public/management/route_setup/load_default.js @@ -20,7 +20,7 @@ import _ from 'lodash'; import React from 'react'; import { banners } from 'ui/notify'; -import { NoDefaultIndexPattern } from 'ui/index_patterns'; +import { NoDefaultIndexPattern, NoDefinedIndexPatterns } from 'ui/index_patterns'; import uiRoutes from 'ui/routes'; import { EuiCallOut, @@ -74,7 +74,12 @@ export default function (opts) { return indexPatterns.getIds() .then(function (patterns) { if (route.requireIndexPatternLength && !patterns.length) { - throw new NoDefaultIndexPattern(); + throw new NoDefinedIndexPatterns(); + } + if(route.requireIndexPatternLength && !route.requireDefaultIndex) { + // iIn this case e.g. Discover takes care of the defaultIndex logic + // Users with lack of permissions would see an error when a defaultIndex is persisted + return; } let defaultId = config.get('defaultIndex'); @@ -103,8 +108,8 @@ export default function (opts) { // failure function (err, kbnUrl) { - const hasDefault = !(err instanceof NoDefaultIndexPattern); - if (hasDefault || !whenMissingRedirectTo) throw err; // rethrow + const isKnowException = (err instanceof NoDefaultIndexPattern) || (err instanceof NoDefinedIndexPatterns); + if (!isKnowException || !whenMissingRedirectTo) throw err; // rethrow kbnUrl.change(whenMissingRedirectTo()); From aff471d96318e3789b040eba9f63367eb3dedf72 Mon Sep 17 00:00:00 2001 From: Matthias Wilhelm Date: Fri, 11 Oct 2019 23:31:36 +0200 Subject: [PATCH 4/7] Consider condition when the configured defaultIndex is wrong --- .../kibana/public/discover/controllers/discover.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/legacy/core_plugins/kibana/public/discover/controllers/discover.js b/src/legacy/core_plugins/kibana/public/discover/controllers/discover.js index dec0f47b1243f..499e5f2a97c49 100644 --- a/src/legacy/core_plugins/kibana/public/discover/controllers/discover.js +++ b/src/legacy/core_plugins/kibana/public/discover/controllers/discover.js @@ -134,18 +134,21 @@ uiRoutes * @type {State} */ const state = new State('_a', {}); + const findIndexById = (id) => savedObjects.find(o => o.id === id); + /** * returns the id of the default index, if none is configured * the value of the first available indexpattern/saved object is used */ const getDefaultIndexId = () => { - if(config.get('defaultIndex')) { - return config.get('defaultIndex'); + const defaultIndex = config.get('defaultIndex'); + if(defaultIndex && findIndexById(defaultIndex)) { + return defaultIndex; } return !savedObjects.length ? '' : savedObjects[0].id; }; - const exists = _.findIndex(savedObjects, o => o.id === state.index) > -1; + const exists = state.index && findIndexById(state.index); const id = exists ? state.index : getDefaultIndexId(); state.destroy(); From 941b9705abe3429fdee5788560e4b8722eda30e5 Mon Sep 17 00:00:00 2001 From: Matthias Wilhelm Date: Sun, 13 Oct 2019 09:45:58 +0200 Subject: [PATCH 5/7] Refactor functionionality to seperate file --- .../public/discover/controllers/discover.js | 24 ++------ .../discover/helpers/get_index_pattern_id.ts | 57 +++++++++++++++++++ 2 files changed, 62 insertions(+), 19 deletions(-) create mode 100644 src/legacy/core_plugins/kibana/public/discover/helpers/get_index_pattern_id.ts diff --git a/src/legacy/core_plugins/kibana/public/discover/controllers/discover.js b/src/legacy/core_plugins/kibana/public/discover/controllers/discover.js index 499e5f2a97c49..b4732b90380cd 100644 --- a/src/legacy/core_plugins/kibana/public/discover/controllers/discover.js +++ b/src/legacy/core_plugins/kibana/public/discover/controllers/discover.js @@ -75,6 +75,7 @@ import { addHelpMenuToAppChrome } from '../components/help_menu/help_menu_util'; import { extractTimeFilter, changeTimeFilter } from '../../../../data/public'; import { start as data } from '../../../../data/public/legacy'; import { npStart } from 'ui/new_platform'; +import { getIndexPatternId } from '../helpers/get_index_pattern_id'; const { savedQueryService } = data.search.services; @@ -123,7 +124,7 @@ uiRoutes resolve: { ip: function (Promise, indexPatterns, config, Private) { const State = Private(StateProvider); - return indexPatterns.getCache().then((savedObjects)=> { + return indexPatterns.getCache().then((indexPatterns)=> { /** * In making the indexPattern modifiable it was placed in appState. Unfortunately, * the load order of AppState conflicts with the load order of many other things @@ -134,29 +135,14 @@ uiRoutes * @type {State} */ const state = new State('_a', {}); - const findIndexById = (id) => savedObjects.find(o => o.id === id); - - /** - * returns the id of the default index, if none is configured - * the value of the first available indexpattern/saved object is used - */ - const getDefaultIndexId = () => { - const defaultIndex = config.get('defaultIndex'); - if(defaultIndex && findIndexById(defaultIndex)) { - return defaultIndex; - } - return !savedObjects.length ? '' : savedObjects[0].id; - }; - - const exists = state.index && findIndexById(state.index); - const id = exists ? state.index : getDefaultIndexId(); + const id = getIndexPatternId(state.index, indexPatterns, config.get('defaultIndex')); state.destroy(); return Promise.props({ - list: savedObjects, + list: indexPatterns, loaded: indexPatterns.get(id), stateVal: state.index, - stateValFound: !!state.index && exists + stateValFound: !!state.index && id === state.index }); }); }, diff --git a/src/legacy/core_plugins/kibana/public/discover/helpers/get_index_pattern_id.ts b/src/legacy/core_plugins/kibana/public/discover/helpers/get_index_pattern_id.ts new file mode 100644 index 0000000000000..a220aa7ca71f9 --- /dev/null +++ b/src/legacy/core_plugins/kibana/public/discover/helpers/get_index_pattern_id.ts @@ -0,0 +1,57 @@ +/* + * 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 { IndexPattern } from '../../../../data/public/index_patterns'; + +export function findIndexPatternById(indexPatterns: IndexPattern[], id): IndexPattern | undefined { + if (!Array.isArray(indexPatterns) || !id) { + return; + } + return indexPatterns.find(o => o.id === id); +} + +/** + * Checks if the given defaultIndex exists and returns + * the first available index pattern id if not + */ +export function getFallbackIndexPatternId( + indexPatterns: IndexPattern[], + defaultIndex: string = '' +): string { + if (defaultIndex && findIndexPatternById(indexPatterns, defaultIndex)) { + return defaultIndex; + } + return !indexPatterns || !indexPatterns.length ? '' : indexPatterns[0].id; +} + +/** + * A given index pattern id is checked for existence and a fallback is provided if it doesn't exist + * The provided defaultIndex is usually configured in Advanced Setting, if it's also invalid + * the first entry of the given list of Indexpatterns is used + */ +export function getIndexPatternId( + id: string = '', + indexPatterns: IndexPattern[], + defaultIndex: string = '' +): string { + if (!id || !findIndexPatternById(indexPatterns, id)) { + return getFallbackIndexPatternId(indexPatterns, defaultIndex); + } + return id; +} From b446b929b7c907cc76623db14ae6654c51dfa9c0 Mon Sep 17 00:00:00 2001 From: Matthias Wilhelm Date: Mon, 14 Oct 2019 10:49:05 +0200 Subject: [PATCH 6/7] Fix naming indexpatterns collision --- .../kibana/public/discover/controllers/discover.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/legacy/core_plugins/kibana/public/discover/controllers/discover.js b/src/legacy/core_plugins/kibana/public/discover/controllers/discover.js index 426c622163585..bc7fced1d988e 100644 --- a/src/legacy/core_plugins/kibana/public/discover/controllers/discover.js +++ b/src/legacy/core_plugins/kibana/public/discover/controllers/discover.js @@ -124,7 +124,7 @@ uiRoutes resolve: { ip: function (Promise, indexPatterns, config, Private) { const State = Private(StateProvider); - return indexPatterns.getCache().then((indexPatterns)=> { + return indexPatterns.getCache().then((indexPatternList)=> { /** * In making the indexPattern modifiable it was placed in appState. Unfortunately, * the load order of AppState conflicts with the load order of many other things @@ -135,11 +135,11 @@ uiRoutes * @type {State} */ const state = new State('_a', {}); - const id = getIndexPatternId(state.index, indexPatterns, config.get('defaultIndex')); + const id = getIndexPatternId(state.index, indexPatternList, config.get('defaultIndex')); state.destroy(); return Promise.props({ - list: indexPatterns, + list: indexPatternList, loaded: indexPatterns.get(id), stateVal: state.index, stateValFound: !!state.index && id === state.index From d740444b8d68fe854ec8f5522d57b2064195d60e Mon Sep 17 00:00:00 2001 From: Matthias Wilhelm Date: Mon, 14 Oct 2019 16:09:01 +0200 Subject: [PATCH 7/7] Fix types --- .../kibana/public/discover/helpers/get_index_pattern_id.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/legacy/core_plugins/kibana/public/discover/helpers/get_index_pattern_id.ts b/src/legacy/core_plugins/kibana/public/discover/helpers/get_index_pattern_id.ts index a220aa7ca71f9..ab0a8d290d826 100644 --- a/src/legacy/core_plugins/kibana/public/discover/helpers/get_index_pattern_id.ts +++ b/src/legacy/core_plugins/kibana/public/discover/helpers/get_index_pattern_id.ts @@ -19,7 +19,10 @@ import { IndexPattern } from '../../../../data/public/index_patterns'; -export function findIndexPatternById(indexPatterns: IndexPattern[], id): IndexPattern | undefined { +export function findIndexPatternById( + indexPatterns: IndexPattern[], + id: string +): IndexPattern | undefined { if (!Array.isArray(indexPatterns) || !id) { return; } @@ -37,7 +40,7 @@ export function getFallbackIndexPatternId( if (defaultIndex && findIndexPatternById(indexPatterns, defaultIndex)) { return defaultIndex; } - return !indexPatterns || !indexPatterns.length ? '' : indexPatterns[0].id; + return !indexPatterns || !indexPatterns.length || !indexPatterns[0].id ? '' : indexPatterns[0].id; } /**