Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Discover] Remove error when a user without default index patterns accesses #47705

Closed
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -92,7 +93,8 @@ const app = uiModules.get('apps/discover', [

uiRoutes
.defaults(/^\/discover(\/|$)/, {
requireDefaultIndex: true,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When setting this to false, kibana no longer is trying to persist the defaultIndex when none is set

requireDefaultIndex: false,
requireIndexPatternLength: true,
requireUICapability: 'discover.show',
k7Breadcrumbs: ($route, $injector) =>
$injector.invoke(
Expand Down Expand Up @@ -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
Expand All @@ -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
});
});
},
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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);
Expand All @@ -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());

Expand Down