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 8a6570781e87d..bc7fced1d988e 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; @@ -92,7 +93,8 @@ const app = uiModules.get('apps/discover', [ uiRoutes .defaults(/^\/discover(\/|$)/, { - requireDefaultIndex: true, + requireDefaultIndex: false, + requireIndexPatternLength: true, requireUICapability: 'discover.show', k7Breadcrumbs: ($route, $injector) => $injector.invoke( @@ -122,7 +124,7 @@ uiRoutes resolve: { ip: function (Promise, indexPatterns, config, Private) { const State = Private(StateProvider); - return indexPatterns.getCache().then((savedObjects)=> { + 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 @@ -133,17 +135,14 @@ uiRoutes * @type {State} */ const state = new State('_a', {}); - - const specified = !!state.index; - const exists = _.findIndex(savedObjects, o => o.id === state.index) > -1; - const id = exists ? state.index : config.get('defaultIndex'); + const id = getIndexPatternId(state.index, indexPatternList, config.get('defaultIndex')); state.destroy(); return Promise.props({ - list: savedObjects, + list: indexPatternList, loaded: indexPatterns.get(id), stateVal: state.index, - stateValFound: specified && 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..ab0a8d290d826 --- /dev/null +++ b/src/legacy/core_plugins/kibana/public/discover/helpers/get_index_pattern_id.ts @@ -0,0 +1,60 @@ +/* + * 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: string +): 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 ? '' : 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; +} 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..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, @@ -67,12 +67,21 @@ 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 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'); let defined = !!defaultId; const exists = _.contains(patterns, defaultId); @@ -99,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());