From dd19128572e8456f251d765ed73d087cad884ed4 Mon Sep 17 00:00:00 2001 From: Timothy Sullivan Date: Tue, 8 Sep 2020 17:16:43 -0700 Subject: [PATCH] reporting.setSpaceToRequest --- x-pack/plugins/reporting/server/core.ts | 30 +++++++++++++++---- .../server/export_types/csv/execute_job.ts | 26 ++++++++-------- x-pack/plugins/reporting/server/plugin.ts | 6 +--- 3 files changed, 40 insertions(+), 22 deletions(-) diff --git a/x-pack/plugins/reporting/server/core.ts b/x-pack/plugins/reporting/server/core.ts index c9ff1d8f34fb335..dbfc174c32e766c 100644 --- a/x-pack/plugins/reporting/server/core.ts +++ b/x-pack/plugins/reporting/server/core.ts @@ -9,6 +9,7 @@ import { first, map, take } from 'rxjs/operators'; import { BasePath, ElasticsearchServiceSetup, + HttpServiceSetup, IRouter, KibanaRequest, SavedObjectsClientContract, @@ -25,13 +26,17 @@ import { ESQueueInstance } from './lib/create_queue'; import { screenshotsObservableFactory, ScreenshotsObservableFn } from './lib/screenshots'; import { ReportingStore } from './lib/store'; -export interface ReportingInternalSetup { +interface PluginSetupOpts { elasticsearch: ElasticsearchServiceSetup; licensing: LicensingPluginSetup; - basePath: BasePath['get']; - router: IRouter; security?: SecurityPluginSetup; spaces?: SpacesPluginSetup; + http: HttpServiceSetup; +} + +export interface ReportingInternalSetup extends PluginSetupOpts { + basePath: BasePath['get']; + router: IRouter; } export interface ReportingInternalStart { @@ -55,9 +60,18 @@ export class ReportingCore { /* * Register setupDeps */ - public pluginSetup(setupDeps: ReportingInternalSetup) { + public pluginSetup({ http, ...opts }: PluginSetupOpts) { + const router = http.createRouter(); + const basePath = http.basePath.get; + + const internalSetup: ReportingInternalSetup = { + router, + basePath, + http, + ...opts, + }; this.pluginSetup$.next(true); // trigger the observer - this.pluginSetupDeps = setupDeps; // cache + this.pluginSetupDeps = internalSetup; // cache } /* @@ -174,4 +188,10 @@ export class ReportingCore { public getSpaceId(request: KibanaRequest) { return this.getPluginSetupDeps().spaces?.spacesService?.getSpaceId(request); } + + public setSpaceToRequest(spaceId: string | undefined, request: KibanaRequest) { + if (spaceId) { + this.getPluginSetupDeps().http.basePath.set(request, `/s/${spaceId}`); + } + } } diff --git a/x-pack/plugins/reporting/server/export_types/csv/execute_job.ts b/x-pack/plugins/reporting/server/export_types/csv/execute_job.ts index bf97e192f1fabfa..c5214969d404e75 100644 --- a/x-pack/plugins/reporting/server/export_types/csv/execute_job.ts +++ b/x-pack/plugins/reporting/server/export_types/csv/execute_job.ts @@ -32,30 +32,31 @@ const getRequest = async ( ) ); } - return await crypto.decrypt(headers); + return (await crypto.decrypt(headers)) as Record; } catch (err) { logger.error(err); throw new Error( - i18n.translate( - 'xpack.reporting.exportTypes.csv.executeJob.failedToDecryptReportJobDataErrorMessage', - { - defaultMessage: 'Failed to decrypt report job data. Please ensure that {encryptionKey} is set and re-generate this report. {err}', - values: { encryptionKey: 'xpack.reporting.encryptionKey', err: err.toString() }, - } - ) - ); // prettier-ignore + i18n.translate( + 'xpack.reporting.exportTypes.csv.executeJob.failedToDecryptReportJobDataErrorMessage', + { + defaultMessage: + 'Failed to decrypt report job data. Please ensure that {encryptionKey} is set and re-generate this report. {err}', + values: { encryptionKey: 'xpack.reporting.encryptionKey', err: err.toString() }, + } + ) + ); } }; - return KibanaRequest.from({ + const fakeRequest = KibanaRequest.from({ headers: await decryptHeaders(), - // Provide the space to scope UI settings properly - path: `/s/${spaceId}`, + path: '/', route: { settings: {} }, url: { href: '/' }, app: {}, raw: { req: { url: '/' } }, } as Hapi.Request); + return fakeRequest; }; export const runTaskFnFactory: RunTaskFnFactory diff --git a/x-pack/plugins/reporting/server/plugin.ts b/x-pack/plugins/reporting/server/plugin.ts index 6fc6408ff2e5e20..a144612e74e1636 100644 --- a/x-pack/plugins/reporting/server/plugin.ts +++ b/x-pack/plugins/reporting/server/plugin.ts @@ -73,14 +73,10 @@ export class ReportingPlugin const { licensing, security, spaces } = plugins; const { initializerContext: initContext, reportingCore } = this; - const router = http.createRouter(); - const basePath = http.basePath.get; - reportingCore.pluginSetup({ + http, elasticsearch, licensing, - basePath, - router, security, spaces, });