Skip to content

Commit

Permalink
reporting.setSpaceToRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
tsullivan committed Sep 9, 2020
1 parent 611cf56 commit dd19128
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 22 deletions.
30 changes: 25 additions & 5 deletions x-pack/plugins/reporting/server/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { first, map, take } from 'rxjs/operators';
import {
BasePath,
ElasticsearchServiceSetup,
HttpServiceSetup,
IRouter,
KibanaRequest,
SavedObjectsClientContract,
Expand All @@ -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 {
Expand All @@ -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
}

/*
Expand Down Expand Up @@ -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}`);
}
}
}
26 changes: 14 additions & 12 deletions x-pack/plugins/reporting/server/export_types/csv/execute_job.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,30 +32,31 @@ const getRequest = async (
)
);
}
return await crypto.decrypt(headers);
return (await crypto.decrypt(headers)) as Record<string, string>;
} 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<RunTaskFn<
Expand All @@ -72,6 +73,7 @@ export const runTaskFnFactory: RunTaskFnFactory<RunTaskFn<

const { headers, spaceId } = job;
const fakeRequest = await getRequest(headers, spaceId, crypto, logger);
reporting.setSpaceToRequest(spaceId, fakeRequest);

const { callAsCurrentUser } = elasticsearch.legacy.client.asScoped(fakeRequest);
const callEndpoint = (endpoint: string, clientParams = {}, options = {}) =>
Expand Down
6 changes: 1 addition & 5 deletions x-pack/plugins/reporting/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand Down

0 comments on commit dd19128

Please sign in to comment.