From 68226434eccbf03a8cdd12bcd57f102515da6bb4 Mon Sep 17 00:00:00 2001 From: Tim Roes Date: Thu, 26 Nov 2020 10:47:58 +0100 Subject: [PATCH 1/3] Remove deprecated Graph licensing methods --- x-pack/plugins/graph/public/application.ts | 4 ++-- x-pack/plugins/graph/public/plugin.ts | 22 ++++++---------------- 2 files changed, 8 insertions(+), 18 deletions(-) diff --git a/x-pack/plugins/graph/public/application.ts b/x-pack/plugins/graph/public/application.ts index 90e87ff4ec85e..c7cf2554c5c60 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 eb0089feaa072..b1efdeacacc3e 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) { From 775a72ad2f4d672efb8fc6ca0465b5609828532d Mon Sep 17 00:00:00 2001 From: Tim Roes Date: Thu, 26 Nov 2020 10:48:20 +0100 Subject: [PATCH 2/3] Use new elasticsearch client --- x-pack/plugins/graph/server/routes/explore.ts | 57 ++++++++++--------- x-pack/plugins/graph/server/routes/search.ts | 21 ++++--- 2 files changed, 39 insertions(+), 39 deletions(-) diff --git a/x-pack/plugins/graph/server/routes/explore.ts b/x-pack/plugins/graph/server/routes/explore.ts index a1e24695413c9..762186d69d819 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 e1d430eeb311a..4357c4509fe06 100644 --- a/x-pack/plugins/graph/server/routes/search.ts +++ b/x-pack/plugins/graph/server/routes/search.ts @@ -4,6 +4,7 @@ * 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 { LicenseState, verifyApiAccess } from '../lib/license_state'; @@ -31,11 +32,7 @@ export function registerSearchRoute({ { core: { uiSettings: { client: uiSettings }, - elasticsearch: { - legacy: { - client: { callAsCurrentUser: callCluster }, - }, - }, + elasticsearch: { client: esClient }, }, }, request, @@ -47,12 +44,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) { From 0b7cb52485ece6ac6edb3e7da5fc077ed61584d0 Mon Sep 17 00:00:00 2001 From: Tim Roes Date: Thu, 26 Nov 2020 16:05:25 +0100 Subject: [PATCH 3/3] fix typing error --- x-pack/plugins/graph/server/routes/search.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/x-pack/plugins/graph/server/routes/search.ts b/x-pack/plugins/graph/server/routes/search.ts index 4357c4509fe06..1bd2861687281 100644 --- a/x-pack/plugins/graph/server/routes/search.ts +++ b/x-pack/plugins/graph/server/routes/search.ts @@ -4,7 +4,6 @@ * 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 { LicenseState, verifyApiAccess } from '../lib/license_state';