From c48428ed22a213275d19a17dbc0bac256ad722c3 Mon Sep 17 00:00:00 2001 From: Tim Roes Date: Mon, 30 Nov 2020 18:08:58 +0100 Subject: [PATCH] [Graph] Use new ES client and change license API (#84398) * Remove deprecated Graph licensing methods * Use new elasticsearch client * fix typing error Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com> --- x-pack/plugins/graph/public/application.ts | 4 +- x-pack/plugins/graph/public/plugin.ts | 22 ++----- x-pack/plugins/graph/server/routes/explore.ts | 57 ++++++++++--------- x-pack/plugins/graph/server/routes/search.ts | 20 +++---- 4 files changed, 46 insertions(+), 57 deletions(-) diff --git a/x-pack/plugins/graph/public/application.ts b/x-pack/plugins/graph/public/application.ts index 90e87ff4ec85e3..c7cf2554c5c606 100644 --- a/x-pack/plugins/graph/public/application.ts +++ b/x-pack/plugins/graph/public/application.ts @@ -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'; @@ -60,7 +60,7 @@ export interface GraphDependencies { capabilities: Record>; coreStart: AppMountContext['core']; navigation: NavigationStart; - licensing: LicensingPluginSetup; + licensing: LicensingPluginStart; chrome: ChromeStart; toastNotifications: ToastsStart; indexPatterns: IndexPatternsContract; diff --git a/x-pack/plugins/graph/public/plugin.ts b/x-pack/plugins/graph/public/plugin.ts index eb0089feaa072c..b1efdeacacc3e1 100644 --- a/x-pack/plugins/graph/public/plugin.ts +++ b/x-pack/plugins/graph/public/plugin.ts @@ -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, @@ -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; @@ -44,16 +44,9 @@ export interface GraphPluginStartDependencies { export class GraphPlugin implements Plugin { - private licensing: LicensingPluginSetup | null = null; - constructor(private initializerContext: PluginInitializerContext) {} - setup( - core: CoreSetup, - { licensing, home }: GraphPluginSetupDependencies - ) { - this.licensing = licensing; - + setup(core: CoreSetup, { home }: GraphPluginSetupDependencies) { if (home) { home.featureCatalogue.register({ id: 'graph', @@ -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, @@ -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) { diff --git a/x-pack/plugins/graph/server/routes/explore.ts b/x-pack/plugins/graph/server/routes/explore.ts index a1e24695413c96..762186d69d8191 100644 --- a/x-pack/plugins/graph/server/routes/explore.ts +++ b/x-pack/plugins/graph/server/routes/explore.ts @@ -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, @@ -31,11 +37,7 @@ export function registerExploreRoute({ async ( { core: { - elasticsearch: { - legacy: { - client: { callAsCurrentUser: callCluster }, - }, - }, + elasticsearch: { client: esClient }, }, }, request, @@ -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({ diff --git a/x-pack/plugins/graph/server/routes/search.ts b/x-pack/plugins/graph/server/routes/search.ts index e1d430eeb311a5..1bd28616872816 100644 --- a/x-pack/plugins/graph/server/routes/search.ts +++ b/x-pack/plugins/graph/server/routes/search.ts @@ -31,11 +31,7 @@ export function registerSearchRoute({ { core: { uiSettings: { client: uiSettings }, - elasticsearch: { - legacy: { - client: { callAsCurrentUser: callCluster }, - }, - }, + elasticsearch: { client: esClient }, }, }, request, @@ -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) {