Skip to content

Commit

Permalink
[Graph] Use new ES client and change license API (#84398)
Browse files Browse the repository at this point in the history
* Remove deprecated Graph licensing methods

* Use new elasticsearch client

* fix typing error

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
2 people authored and timroes committed Nov 30, 2020
1 parent 9bc8d30 commit 43e5276
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 57 deletions.
4 changes: 2 additions & 2 deletions x-pack/plugins/graph/public/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import {
// @ts-ignore
import { initGraphApp } from './app';
import { Plugin as DataPlugin, IndexPatternsContract } from '../../../../src/plugins/data/public';
import { LicensingPluginSetup } from '../../licensing/public';
import { LicensingPluginStart } from '../../licensing/public';
import { checkLicense } from '../common/check_license';
import { NavigationPublicPluginStart as NavigationStart } from '../../../../src/plugins/navigation/public';
import { Storage } from '../../../../src/plugins/kibana_utils/public';
Expand Down Expand Up @@ -60,7 +60,7 @@ export interface GraphDependencies {
capabilities: Record<string, boolean | Record<string, boolean>>;
coreStart: AppMountContext['core'];
navigation: NavigationStart;
licensing: LicensingPluginSetup;
licensing: LicensingPluginStart;
chrome: ChromeStart;
toastNotifications: ToastsStart;
indexPatterns: IndexPatternsContract;
Expand Down
22 changes: 6 additions & 16 deletions x-pack/plugins/graph/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { NavigationPublicPluginStart as NavigationStart } from '../../../../src/
import { DataPublicPluginStart } from '../../../../src/plugins/data/public';

import { toggleNavLink } from './services/toggle_nav_link';
import { LicensingPluginSetup } from '../../licensing/public';
import { LicensingPluginStart } from '../../licensing/public';
import { checkLicense } from '../common/check_license';
import {
FeatureCatalogueCategory,
Expand All @@ -30,12 +30,12 @@ import { ConfigSchema } from '../config';
import { SavedObjectsStart } from '../../../../src/plugins/saved_objects/public';

export interface GraphPluginSetupDependencies {
licensing: LicensingPluginSetup;
home?: HomePublicPluginSetup;
}

export interface GraphPluginStartDependencies {
navigation: NavigationStart;
licensing: LicensingPluginStart;
data: DataPublicPluginStart;
savedObjects: SavedObjectsStart;
kibanaLegacy: KibanaLegacyStart;
Expand All @@ -44,16 +44,9 @@ export interface GraphPluginStartDependencies {

export class GraphPlugin
implements Plugin<void, void, GraphPluginSetupDependencies, GraphPluginStartDependencies> {
private licensing: LicensingPluginSetup | null = null;

constructor(private initializerContext: PluginInitializerContext<ConfigSchema>) {}

setup(
core: CoreSetup<GraphPluginStartDependencies>,
{ licensing, home }: GraphPluginSetupDependencies
) {
this.licensing = licensing;

setup(core: CoreSetup<GraphPluginStartDependencies>, { home }: GraphPluginSetupDependencies) {
if (home) {
home.featureCatalogue.register({
id: 'graph',
Expand Down Expand Up @@ -92,7 +85,7 @@ export class GraphPlugin
return renderApp({
...params,
pluginInitializerContext: this.initializerContext,
licensing,
licensing: pluginsStart.licensing,
core: coreStart,
navigation: pluginsStart.navigation,
data: pluginsStart.data,
Expand All @@ -115,11 +108,8 @@ export class GraphPlugin
});
}

start(core: CoreStart, { home }: GraphPluginStartDependencies) {
if (this.licensing === null) {
throw new Error('Start called before setup');
}
this.licensing.license$.subscribe((license) => {
start(core: CoreStart, { home, licensing }: GraphPluginStartDependencies) {
licensing.license$.subscribe((license) => {
const licenseInformation = checkLicense(license);
toggleNavLink(licenseInformation, core.chrome.navLinks);
if (home && !licenseInformation.enableAppLink) {
Expand Down
57 changes: 29 additions & 28 deletions x-pack/plugins/graph/server/routes/explore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,18 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { errors } from '@elastic/elasticsearch';
import { IRouter } from 'kibana/server';
import { schema } from '@kbn/config-schema';
import Boom from '@hapi/boom';
import { get } from 'lodash';
import { LicenseState, verifyApiAccess } from '../lib/license_state';

interface ErrorResponse {
error?: {
root_cause?: Array<{ type: string; reason: string }>;
};
}

export function registerExploreRoute({
router,
licenseState,
Expand All @@ -31,11 +37,7 @@ export function registerExploreRoute({
async (
{
core: {
elasticsearch: {
legacy: {
client: { callAsCurrentUser: callCluster },
},
},
elasticsearch: { client: esClient },
},
},
request,
Expand All @@ -46,32 +48,31 @@ export function registerExploreRoute({
try {
return response.ok({
body: {
resp: await callCluster('transport.request', {
path: '/' + encodeURIComponent(request.body.index) + '/_graph/explore',
body: request.body.query,
method: 'POST',
query: {},
}),
resp: (
await esClient.asCurrentUser.transport.request({
path: '/' + encodeURIComponent(request.body.index) + '/_graph/explore',
body: request.body.query,
method: 'POST',
})
).body,
},
});
} catch (error) {
// Extract known reasons for bad choice of field
const relevantCause = get(
error,
'body.error.root_cause',
[] as Array<{ type: string; reason: string }>
).find((cause: { type: string; reason: string }) => {
return (
cause.reason.includes('Fielddata is disabled on text fields') ||
cause.reason.includes('No support for examining floating point') ||
cause.reason.includes('Sample diversifying key must be a single valued-field') ||
cause.reason.includes('Failed to parse query') ||
cause.type === 'parsing_exception'
);
});
if (error instanceof errors.ResponseError) {
const errorBody: ErrorResponse = error.body;
const relevantCause = (errorBody.error?.root_cause ?? []).find((cause) => {
return (
cause.reason.includes('Fielddata is disabled on text fields') ||
cause.reason.includes('No support for examining floating point') ||
cause.reason.includes('Sample diversifying key must be a single valued-field') ||
cause.reason.includes('Failed to parse query') ||
cause.type === 'parsing_exception'
);
});

if (relevantCause) {
throw Boom.badRequest(relevantCause.reason);
if (relevantCause) {
throw Boom.badRequest(relevantCause.reason);
}
}

return response.internalError({
Expand Down
20 changes: 9 additions & 11 deletions x-pack/plugins/graph/server/routes/search.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@ export function registerSearchRoute({
{
core: {
uiSettings: { client: uiSettings },
elasticsearch: {
legacy: {
client: { callAsCurrentUser: callCluster },
},
},
elasticsearch: { client: esClient },
},
},
request,
Expand All @@ -47,12 +43,14 @@ export function registerSearchRoute({
try {
return response.ok({
body: {
resp: await callCluster('search', {
index: request.body.index,
body: request.body.body,
rest_total_hits_as_int: true,
ignore_throttled: !includeFrozen,
}),
resp: (
await esClient.asCurrentUser.search({
index: request.body.index,
body: request.body.body,
rest_total_hits_as_int: true,
ignore_throttled: !includeFrozen,
})
).body,
},
});
} catch (error) {
Expand Down

0 comments on commit 43e5276

Please sign in to comment.