* [Reporting] Define shims of legacy dependencies (#54082) * simplify serverfacade definition * simplify requestfacade definition * use the shim * makeRequestFacade * requestFacade * import sorting * originalServer * reduce loc change * remove consolelog * hacks to fix tests * ServerFacade in index * Cosmetic * remove field from serverfacade * add raw to the request * fix types * add fieldFormatServiceFactory to legacy * Pass the complete request object to sec plugin * Fix test * fix test 2 * getUser takes a legacy request * add unit test for new lib * add getRawRequest to pass to saved objects method * update test snapshot * leave a TODO comment for type import * variable rename for legacy id Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com> * update legacy to fix ts errors Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com>
This commit is contained in:
parent
a8ac40b191
commit
1cb96c2136
|
@ -86,7 +86,6 @@ export const executeJobFactory: ExecuteJobFactory<ESQueueWorkerExecuteFn<
|
||||||
|
|
||||||
const [formatsMap, uiSettings] = await Promise.all([
|
const [formatsMap, uiSettings] = await Promise.all([
|
||||||
(async () => {
|
(async () => {
|
||||||
// @ts-ignore fieldFormatServiceFactory' does not exist on type 'ServerFacade TODO
|
|
||||||
const fieldFormats = await server.fieldFormatServiceFactory(uiConfig);
|
const fieldFormats = await server.fieldFormatServiceFactory(uiConfig);
|
||||||
return fieldFormatMapFactory(indexPatternSavedObject, fieldFormats);
|
return fieldFormatMapFactory(indexPatternSavedObject, fieldFormats);
|
||||||
})(),
|
})(),
|
||||||
|
|
|
@ -57,7 +57,7 @@ export async function generateCsvSearch(
|
||||||
jobParams: JobParamsDiscoverCsv
|
jobParams: JobParamsDiscoverCsv
|
||||||
): Promise<CsvResultFromSearch> {
|
): Promise<CsvResultFromSearch> {
|
||||||
const { savedObjects, uiSettingsServiceFactory } = server;
|
const { savedObjects, uiSettingsServiceFactory } = server;
|
||||||
const savedObjectsClient = savedObjects.getScopedSavedObjectsClient(req);
|
const savedObjectsClient = savedObjects.getScopedSavedObjectsClient(req.getRawRequest());
|
||||||
const { indexPatternSavedObjectId, timerange } = searchPanel;
|
const { indexPatternSavedObjectId, timerange } = searchPanel;
|
||||||
const savedSearchObjectAttr = searchPanel.attributes as SavedSearchObjectAttributes;
|
const savedSearchObjectAttr = searchPanel.attributes as SavedSearchObjectAttributes;
|
||||||
const { indexPatternSavedObject } = await getDataSource(
|
const { indexPatternSavedObject } = await getDataSource(
|
||||||
|
|
|
@ -6,6 +6,9 @@
|
||||||
|
|
||||||
import { resolve } from 'path';
|
import { resolve } from 'path';
|
||||||
import { i18n } from '@kbn/i18n';
|
import { i18n } from '@kbn/i18n';
|
||||||
|
import { Legacy } from 'kibana';
|
||||||
|
import { IUiSettingsClient } from 'src/core/server';
|
||||||
|
import { XPackMainPlugin } from '../xpack_main/server/xpack_main';
|
||||||
import { PLUGIN_ID, UI_SETTINGS_CUSTOM_PDF_LOGO } from './common/constants';
|
import { PLUGIN_ID, UI_SETTINGS_CUSTOM_PDF_LOGO } from './common/constants';
|
||||||
// @ts-ignore untyped module defintition
|
// @ts-ignore untyped module defintition
|
||||||
import { mirrorPluginStatus } from '../../server/lib/mirror_plugin_status';
|
import { mirrorPluginStatus } from '../../server/lib/mirror_plugin_status';
|
||||||
|
@ -16,16 +19,35 @@ import {
|
||||||
getExportTypesRegistry,
|
getExportTypesRegistry,
|
||||||
runValidations,
|
runValidations,
|
||||||
} from './server/lib';
|
} from './server/lib';
|
||||||
import { config as reportingConfig } from './config';
|
|
||||||
import { logConfiguration } from './log_configuration';
|
|
||||||
import { createBrowserDriverFactory } from './server/browsers';
|
import { createBrowserDriverFactory } from './server/browsers';
|
||||||
import { registerReportingUsageCollector } from './server/usage';
|
import { registerReportingUsageCollector } from './server/usage';
|
||||||
import { ReportingConfigOptions, ReportingPluginSpecOptions, ServerFacade } from './types.d';
|
import { ReportingConfigOptions, ReportingPluginSpecOptions } from './types.d';
|
||||||
|
import { config as reportingConfig } from './config';
|
||||||
|
import { logConfiguration } from './log_configuration';
|
||||||
|
|
||||||
const kbToBase64Length = (kb: number) => {
|
const kbToBase64Length = (kb: number) => {
|
||||||
return Math.floor((kb * 1024 * 8) / 6);
|
return Math.floor((kb * 1024 * 8) / 6);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
type LegacyPlugins = Legacy.Server['plugins'];
|
||||||
|
|
||||||
|
export interface ServerFacade {
|
||||||
|
config: Legacy.Server['config'];
|
||||||
|
info: Legacy.Server['info'];
|
||||||
|
log: Legacy.Server['log'];
|
||||||
|
plugins: {
|
||||||
|
elasticsearch: LegacyPlugins['elasticsearch'];
|
||||||
|
security: LegacyPlugins['security'];
|
||||||
|
xpack_main: XPackMainPlugin & {
|
||||||
|
status?: any;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
route: Legacy.Server['route'];
|
||||||
|
savedObjects: Legacy.Server['savedObjects'];
|
||||||
|
uiSettingsServiceFactory: Legacy.Server['uiSettingsServiceFactory'];
|
||||||
|
fieldFormatServiceFactory: (uiConfig: IUiSettingsClient) => unknown;
|
||||||
|
}
|
||||||
|
|
||||||
export const reporting = (kibana: any) => {
|
export const reporting = (kibana: any) => {
|
||||||
return new kibana.Plugin({
|
return new kibana.Plugin({
|
||||||
id: PLUGIN_ID,
|
id: PLUGIN_ID,
|
||||||
|
@ -42,7 +64,7 @@ export const reporting = (kibana: any) => {
|
||||||
embeddableActions: ['plugins/reporting/panel_actions/get_csv_panel_action'],
|
embeddableActions: ['plugins/reporting/panel_actions/get_csv_panel_action'],
|
||||||
home: ['plugins/reporting/register_feature'],
|
home: ['plugins/reporting/register_feature'],
|
||||||
managementSections: ['plugins/reporting/views/management'],
|
managementSections: ['plugins/reporting/views/management'],
|
||||||
injectDefaultVars(server: ServerFacade, options?: ReportingConfigOptions) {
|
injectDefaultVars(server: Legacy.Server, options?: ReportingConfigOptions) {
|
||||||
const config = server.config();
|
const config = server.config();
|
||||||
return {
|
return {
|
||||||
reportingPollConfig: options ? options.poll : {},
|
reportingPollConfig: options ? options.poll : {},
|
||||||
|
@ -70,9 +92,22 @@ export const reporting = (kibana: any) => {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
// TODO: Decouple Hapi: Build a server facade object based on the server to
|
async init(server: Legacy.Server) {
|
||||||
// pass through to the libs. Do not pass server directly
|
const serverFacade: ServerFacade = {
|
||||||
async init(server: ServerFacade) {
|
config: server.config,
|
||||||
|
info: server.info,
|
||||||
|
route: server.route.bind(server),
|
||||||
|
plugins: {
|
||||||
|
elasticsearch: server.plugins.elasticsearch,
|
||||||
|
xpack_main: server.plugins.xpack_main,
|
||||||
|
security: server.plugins.security,
|
||||||
|
},
|
||||||
|
savedObjects: server.savedObjects,
|
||||||
|
uiSettingsServiceFactory: server.uiSettingsServiceFactory,
|
||||||
|
// @ts-ignore Property 'fieldFormatServiceFactory' does not exist on type 'Server'.
|
||||||
|
fieldFormatServiceFactory: server.fieldFormatServiceFactory,
|
||||||
|
log: server.log.bind(server),
|
||||||
|
};
|
||||||
const exportTypesRegistry = getExportTypesRegistry();
|
const exportTypesRegistry = getExportTypesRegistry();
|
||||||
|
|
||||||
let isCollectorReady = false;
|
let isCollectorReady = false;
|
||||||
|
@ -80,18 +115,18 @@ export const reporting = (kibana: any) => {
|
||||||
const { usageCollection } = server.newPlatform.setup.plugins;
|
const { usageCollection } = server.newPlatform.setup.plugins;
|
||||||
registerReportingUsageCollector(
|
registerReportingUsageCollector(
|
||||||
usageCollection,
|
usageCollection,
|
||||||
server,
|
serverFacade,
|
||||||
() => isCollectorReady,
|
() => isCollectorReady,
|
||||||
exportTypesRegistry
|
exportTypesRegistry
|
||||||
);
|
);
|
||||||
|
|
||||||
const logger = LevelLogger.createForServer(server, [PLUGIN_ID]);
|
const logger = LevelLogger.createForServer(serverFacade, [PLUGIN_ID]);
|
||||||
const browserDriverFactory = await createBrowserDriverFactory(server);
|
const browserDriverFactory = await createBrowserDriverFactory(serverFacade);
|
||||||
|
|
||||||
logConfiguration(server, logger);
|
logConfiguration(serverFacade, logger);
|
||||||
runValidations(server, logger, browserDriverFactory);
|
runValidations(serverFacade, logger, browserDriverFactory);
|
||||||
|
|
||||||
const { xpack_main: xpackMainPlugin } = server.plugins;
|
const { xpack_main: xpackMainPlugin } = serverFacade.plugins;
|
||||||
mirrorPluginStatus(xpackMainPlugin, this);
|
mirrorPluginStatus(xpackMainPlugin, this);
|
||||||
const checkLicense = checkLicenseFactory(exportTypesRegistry);
|
const checkLicense = checkLicenseFactory(exportTypesRegistry);
|
||||||
(xpackMainPlugin as any).status.once('green', () => {
|
(xpackMainPlugin as any).status.once('green', () => {
|
||||||
|
@ -104,7 +139,7 @@ export const reporting = (kibana: any) => {
|
||||||
isCollectorReady = true;
|
isCollectorReady = true;
|
||||||
|
|
||||||
// Reporting routes
|
// Reporting routes
|
||||||
registerRoutes(server, exportTypesRegistry, browserDriverFactory, logger);
|
registerRoutes(serverFacade, exportTypesRegistry, browserDriverFactory, logger);
|
||||||
},
|
},
|
||||||
|
|
||||||
deprecations({ unused }: any) {
|
deprecations({ unused }: any) {
|
||||||
|
|
|
@ -4,8 +4,14 @@
|
||||||
* you may not use this file except in compliance with the Elastic License.
|
* you may not use this file except in compliance with the Elastic License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export function getUserFactory(server) {
|
import { Legacy } from 'kibana';
|
||||||
return async request => {
|
import { ServerFacade } from '../../types';
|
||||||
|
|
||||||
|
export function getUserFactory(server: ServerFacade) {
|
||||||
|
/*
|
||||||
|
* Legacy.Request because this is called from routing middleware
|
||||||
|
*/
|
||||||
|
return async (request: Legacy.Request) => {
|
||||||
if (!server.plugins.security) {
|
if (!server.plugins.security) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
@ -13,7 +19,7 @@ export function getUserFactory(server) {
|
||||||
try {
|
try {
|
||||||
return await server.plugins.security.getUser(request);
|
return await server.plugins.security.getUser(request);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
server.log(['reporting', 'getUser', 'debug'], err);
|
server.log(['reporting', 'getUser', 'error'], err);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
};
|
};
|
|
@ -4,7 +4,7 @@
|
||||||
* you may not use this file except in compliance with the Elastic License.
|
* you may not use this file except in compliance with the Elastic License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
type ServerLog = (tags: string[], msg: string) => void;
|
import { ServerFacade } from '../../types';
|
||||||
|
|
||||||
const trimStr = (toTrim: string) => {
|
const trimStr = (toTrim: string) => {
|
||||||
return typeof toTrim === 'string' ? toTrim.trim() : toTrim;
|
return typeof toTrim === 'string' ? toTrim.trim() : toTrim;
|
||||||
|
@ -16,12 +16,12 @@ export class LevelLogger {
|
||||||
|
|
||||||
public warn: (msg: string, tags?: string[]) => void;
|
public warn: (msg: string, tags?: string[]) => void;
|
||||||
|
|
||||||
static createForServer(server: any, tags: string[]) {
|
static createForServer(server: ServerFacade, tags: string[]) {
|
||||||
const serverLog: ServerLog = (tgs: string[], msg: string) => server.log(tgs, msg);
|
const serverLog: ServerFacade['log'] = (tgs: string[], msg: string) => server.log(tgs, msg);
|
||||||
return new LevelLogger(serverLog, tags);
|
return new LevelLogger(serverLog, tags);
|
||||||
}
|
}
|
||||||
|
|
||||||
constructor(logger: ServerLog, tags: string[]) {
|
constructor(logger: ServerFacade['log'], tags: string[]) {
|
||||||
this._logger = logger;
|
this._logger = logger;
|
||||||
this._tags = tags;
|
this._tags = tags;
|
||||||
|
|
||||||
|
|
|
@ -27,10 +27,6 @@ export function oncePerServer(fn: ServerFn) {
|
||||||
throw new TypeError('This function expects to be called with a single argument');
|
throw new TypeError('This function expects to be called with a single argument');
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!server || typeof server.expose !== 'function') {
|
|
||||||
throw new TypeError('This function expects to be passed the server');
|
|
||||||
}
|
|
||||||
|
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
return fn.call(this, server);
|
return fn.call(this, server);
|
||||||
});
|
});
|
||||||
|
|
|
@ -4,16 +4,18 @@
|
||||||
* you may not use this file except in compliance with the Elastic License.
|
* you may not use this file except in compliance with the Elastic License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import { Legacy } from 'kibana';
|
||||||
import boom from 'boom';
|
import boom from 'boom';
|
||||||
import Joi from 'joi';
|
import Joi from 'joi';
|
||||||
import rison from 'rison-node';
|
import rison from 'rison-node';
|
||||||
import { API_BASE_URL } from '../../common/constants';
|
import { API_BASE_URL } from '../../common/constants';
|
||||||
import { ServerFacade, RequestFacade, ReportingResponseToolkit } from '../../types';
|
import { ServerFacade, ReportingResponseToolkit } from '../../types';
|
||||||
import {
|
import {
|
||||||
getRouteConfigFactoryReportingPre,
|
getRouteConfigFactoryReportingPre,
|
||||||
GetRouteConfigFactoryFn,
|
GetRouteConfigFactoryFn,
|
||||||
RouteConfigFactory,
|
RouteConfigFactory,
|
||||||
} from './lib/route_config_factories';
|
} from './lib/route_config_factories';
|
||||||
|
import { makeRequestFacade } from './lib/make_request_facade';
|
||||||
import { HandlerErrorFunction, HandlerFunction } from './types';
|
import { HandlerErrorFunction, HandlerFunction } from './types';
|
||||||
|
|
||||||
const BASE_GENERATE = `${API_BASE_URL}/generate`;
|
const BASE_GENERATE = `${API_BASE_URL}/generate`;
|
||||||
|
@ -54,7 +56,8 @@ export function registerGenerateFromJobParams(
|
||||||
path: `${BASE_GENERATE}/{exportType}`,
|
path: `${BASE_GENERATE}/{exportType}`,
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
options: getRouteConfig(),
|
options: getRouteConfig(),
|
||||||
handler: async (request: RequestFacade, h: ReportingResponseToolkit) => {
|
handler: async (legacyRequest: Legacy.Request, h: ReportingResponseToolkit) => {
|
||||||
|
const request = makeRequestFacade(legacyRequest);
|
||||||
let jobParamsRison: string | null;
|
let jobParamsRison: string | null;
|
||||||
|
|
||||||
if (request.payload) {
|
if (request.payload) {
|
||||||
|
@ -80,7 +83,7 @@ export function registerGenerateFromJobParams(
|
||||||
if (!jobParams) {
|
if (!jobParams) {
|
||||||
throw new Error('missing jobParams!');
|
throw new Error('missing jobParams!');
|
||||||
}
|
}
|
||||||
response = await handler(exportType, jobParams, request, h);
|
response = await handler(exportType, jobParams, legacyRequest, h);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw boom.badRequest(`invalid rison: ${jobParamsRison}`);
|
throw boom.badRequest(`invalid rison: ${jobParamsRison}`);
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,11 +4,13 @@
|
||||||
* you may not use this file except in compliance with the Elastic License.
|
* you may not use this file except in compliance with the Elastic License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import { Legacy } from 'kibana';
|
||||||
import { get } from 'lodash';
|
import { get } from 'lodash';
|
||||||
import { API_BASE_GENERATE_V1, CSV_FROM_SAVEDOBJECT_JOB_TYPE } from '../../common/constants';
|
import { API_BASE_GENERATE_V1, CSV_FROM_SAVEDOBJECT_JOB_TYPE } from '../../common/constants';
|
||||||
import { ServerFacade, RequestFacade, ReportingResponseToolkit } from '../../types';
|
import { ServerFacade, ReportingResponseToolkit } from '../../types';
|
||||||
import { HandlerErrorFunction, HandlerFunction, QueuedJobPayload } from './types';
|
import { HandlerErrorFunction, HandlerFunction, QueuedJobPayload } from './types';
|
||||||
import { getRouteOptionsCsv } from './lib/route_config_factories';
|
import { getRouteOptionsCsv } from './lib/route_config_factories';
|
||||||
|
import { makeRequestFacade } from './lib/make_request_facade';
|
||||||
import { getJobParamsFromRequest } from '../../export_types/csv_from_savedobject/server/lib/get_job_params_from_request';
|
import { getJobParamsFromRequest } from '../../export_types/csv_from_savedobject/server/lib/get_job_params_from_request';
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -31,17 +33,18 @@ export function registerGenerateCsvFromSavedObject(
|
||||||
path: `${API_BASE_GENERATE_V1}/csv/saved-object/{savedObjectType}:{savedObjectId}`,
|
path: `${API_BASE_GENERATE_V1}/csv/saved-object/{savedObjectType}:{savedObjectId}`,
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
options: routeOptions,
|
options: routeOptions,
|
||||||
handler: async (request: RequestFacade, h: ReportingResponseToolkit) => {
|
handler: async (legacyRequest: Legacy.Request, h: ReportingResponseToolkit) => {
|
||||||
|
const requestFacade = makeRequestFacade(legacyRequest);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* 1. Build `jobParams` object: job data that execution will need to reference in various parts of the lifecycle
|
* 1. Build `jobParams` object: job data that execution will need to reference in various parts of the lifecycle
|
||||||
* 2. Pass the jobParams and other common params to `handleRoute`, a shared function to enqueue the job with the params
|
* 2. Pass the jobParams and other common params to `handleRoute`, a shared function to enqueue the job with the params
|
||||||
* 3. Ensure that details for a queued job were returned
|
* 3. Ensure that details for a queued job were returned
|
||||||
*/
|
*/
|
||||||
|
|
||||||
let result: QueuedJobPayload<any>;
|
let result: QueuedJobPayload<any>;
|
||||||
try {
|
try {
|
||||||
const jobParams = getJobParamsFromRequest(request, { isImmediate: false });
|
const jobParams = getJobParamsFromRequest(requestFacade, { isImmediate: false });
|
||||||
result = await handleRoute(CSV_FROM_SAVEDOBJECT_JOB_TYPE, jobParams, request, h);
|
result = await handleRoute(CSV_FROM_SAVEDOBJECT_JOB_TYPE, jobParams, legacyRequest, h); // pass the original request because the handler will make the request facade on its own
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
throw handleRouteError(CSV_FROM_SAVEDOBJECT_JOB_TYPE, err);
|
throw handleRouteError(CSV_FROM_SAVEDOBJECT_JOB_TYPE, err);
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,11 +4,11 @@
|
||||||
* you may not use this file except in compliance with the Elastic License.
|
* you may not use this file except in compliance with the Elastic License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import { Legacy } from 'kibana';
|
||||||
import { API_BASE_GENERATE_V1 } from '../../common/constants';
|
import { API_BASE_GENERATE_V1 } from '../../common/constants';
|
||||||
import { createJobFactory, executeJobFactory } from '../../export_types/csv_from_savedobject';
|
import { createJobFactory, executeJobFactory } from '../../export_types/csv_from_savedobject';
|
||||||
import {
|
import {
|
||||||
ServerFacade,
|
ServerFacade,
|
||||||
RequestFacade,
|
|
||||||
ResponseFacade,
|
ResponseFacade,
|
||||||
HeadlessChromiumDriverFactory,
|
HeadlessChromiumDriverFactory,
|
||||||
ReportingResponseToolkit,
|
ReportingResponseToolkit,
|
||||||
|
@ -16,8 +16,9 @@ import {
|
||||||
JobDocOutputExecuted,
|
JobDocOutputExecuted,
|
||||||
} from '../../types';
|
} from '../../types';
|
||||||
import { JobDocPayloadPanelCsv } from '../../export_types/csv_from_savedobject/types';
|
import { JobDocPayloadPanelCsv } from '../../export_types/csv_from_savedobject/types';
|
||||||
import { getRouteOptionsCsv } from './lib/route_config_factories';
|
|
||||||
import { getJobParamsFromRequest } from '../../export_types/csv_from_savedobject/server/lib/get_job_params_from_request';
|
import { getJobParamsFromRequest } from '../../export_types/csv_from_savedobject/server/lib/get_job_params_from_request';
|
||||||
|
import { getRouteOptionsCsv } from './lib/route_config_factories';
|
||||||
|
import { makeRequestFacade } from './lib/make_request_facade';
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* This function registers API Endpoints for immediate Reporting jobs. The API inputs are:
|
* This function registers API Endpoints for immediate Reporting jobs. The API inputs are:
|
||||||
|
@ -43,7 +44,8 @@ export function registerGenerateCsvFromSavedObjectImmediate(
|
||||||
path: `${API_BASE_GENERATE_V1}/immediate/csv/saved-object/{savedObjectType}:{savedObjectId}`,
|
path: `${API_BASE_GENERATE_V1}/immediate/csv/saved-object/{savedObjectType}:{savedObjectId}`,
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
options: routeOptions,
|
options: routeOptions,
|
||||||
handler: async (request: RequestFacade, h: ReportingResponseToolkit) => {
|
handler: async (legacyRequest: Legacy.Request, h: ReportingResponseToolkit) => {
|
||||||
|
const request = makeRequestFacade(legacyRequest);
|
||||||
const logger = parentLogger.clone(['savedobject-csv']);
|
const logger = parentLogger.clone(['savedobject-csv']);
|
||||||
const jobParams = getJobParamsFromRequest(request, { isImmediate: true });
|
const jobParams = getJobParamsFromRequest(request, { isImmediate: true });
|
||||||
|
|
||||||
|
|
|
@ -5,12 +5,12 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import boom from 'boom';
|
import boom from 'boom';
|
||||||
|
import { Legacy } from 'kibana';
|
||||||
import { API_BASE_URL } from '../../common/constants';
|
import { API_BASE_URL } from '../../common/constants';
|
||||||
import {
|
import {
|
||||||
ServerFacade,
|
ServerFacade,
|
||||||
ExportTypesRegistry,
|
ExportTypesRegistry,
|
||||||
HeadlessChromiumDriverFactory,
|
HeadlessChromiumDriverFactory,
|
||||||
RequestFacade,
|
|
||||||
ReportingResponseToolkit,
|
ReportingResponseToolkit,
|
||||||
Logger,
|
Logger,
|
||||||
} from '../../types';
|
} from '../../types';
|
||||||
|
@ -19,6 +19,7 @@ import { registerGenerateCsvFromSavedObject } from './generate_from_savedobject'
|
||||||
import { registerGenerateCsvFromSavedObjectImmediate } from './generate_from_savedobject_immediate';
|
import { registerGenerateCsvFromSavedObjectImmediate } from './generate_from_savedobject_immediate';
|
||||||
import { registerLegacy } from './legacy';
|
import { registerLegacy } from './legacy';
|
||||||
import { createQueueFactory, enqueueJobFactory } from '../lib';
|
import { createQueueFactory, enqueueJobFactory } from '../lib';
|
||||||
|
import { makeRequestFacade } from './lib/make_request_facade';
|
||||||
|
|
||||||
export function registerJobGenerationRoutes(
|
export function registerJobGenerationRoutes(
|
||||||
server: ServerFacade,
|
server: ServerFacade,
|
||||||
|
@ -40,9 +41,10 @@ export function registerJobGenerationRoutes(
|
||||||
async function handler(
|
async function handler(
|
||||||
exportTypeId: string,
|
exportTypeId: string,
|
||||||
jobParams: object,
|
jobParams: object,
|
||||||
request: RequestFacade,
|
legacyRequest: Legacy.Request,
|
||||||
h: ReportingResponseToolkit
|
h: ReportingResponseToolkit
|
||||||
) {
|
) {
|
||||||
|
const request = makeRequestFacade(legacyRequest);
|
||||||
const user = request.pre.user;
|
const user = request.pre.user;
|
||||||
const headers = request.headers;
|
const headers = request.headers;
|
||||||
|
|
||||||
|
|
|
@ -4,16 +4,17 @@
|
||||||
* you may not use this file except in compliance with the Elastic License.
|
* you may not use this file except in compliance with the Elastic License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import { Legacy } from 'kibana';
|
||||||
import boom from 'boom';
|
import boom from 'boom';
|
||||||
import { API_BASE_URL } from '../../common/constants';
|
import { API_BASE_URL } from '../../common/constants';
|
||||||
import {
|
import {
|
||||||
ServerFacade,
|
ServerFacade,
|
||||||
ExportTypesRegistry,
|
ExportTypesRegistry,
|
||||||
Logger,
|
Logger,
|
||||||
RequestFacade,
|
|
||||||
ReportingResponseToolkit,
|
ReportingResponseToolkit,
|
||||||
JobDocOutput,
|
JobDocOutput,
|
||||||
JobSource,
|
JobSource,
|
||||||
|
ListQuery,
|
||||||
} from '../../types';
|
} from '../../types';
|
||||||
// @ts-ignore
|
// @ts-ignore
|
||||||
import { jobsQueryFactory } from '../lib/jobs_query';
|
import { jobsQueryFactory } from '../lib/jobs_query';
|
||||||
|
@ -23,6 +24,7 @@ import {
|
||||||
getRouteConfigFactoryDownloadPre,
|
getRouteConfigFactoryDownloadPre,
|
||||||
getRouteConfigFactoryManagementPre,
|
getRouteConfigFactoryManagementPre,
|
||||||
} from './lib/route_config_factories';
|
} from './lib/route_config_factories';
|
||||||
|
import { makeRequestFacade } from './lib/make_request_facade';
|
||||||
|
|
||||||
const MAIN_ENTRY = `${API_BASE_URL}/jobs`;
|
const MAIN_ENTRY = `${API_BASE_URL}/jobs`;
|
||||||
|
|
||||||
|
@ -40,8 +42,9 @@ export function registerJobInfoRoutes(
|
||||||
path: `${MAIN_ENTRY}/list`,
|
path: `${MAIN_ENTRY}/list`,
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
options: getRouteConfig(),
|
options: getRouteConfig(),
|
||||||
handler: (request: RequestFacade) => {
|
handler: (legacyRequest: Legacy.Request) => {
|
||||||
const { page: queryPage, size: querySize, ids: queryIds } = request.query;
|
const request = makeRequestFacade(legacyRequest);
|
||||||
|
const { page: queryPage, size: querySize, ids: queryIds } = request.query as ListQuery;
|
||||||
const page = parseInt(queryPage, 10) || 0;
|
const page = parseInt(queryPage, 10) || 0;
|
||||||
const size = Math.min(100, parseInt(querySize, 10) || 10);
|
const size = Math.min(100, parseInt(querySize, 10) || 10);
|
||||||
const jobIds = queryIds ? queryIds.split(',') : null;
|
const jobIds = queryIds ? queryIds.split(',') : null;
|
||||||
|
@ -62,7 +65,8 @@ export function registerJobInfoRoutes(
|
||||||
path: `${MAIN_ENTRY}/count`,
|
path: `${MAIN_ENTRY}/count`,
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
options: getRouteConfig(),
|
options: getRouteConfig(),
|
||||||
handler: (request: RequestFacade) => {
|
handler: (legacyRequest: Legacy.Request) => {
|
||||||
|
const request = makeRequestFacade(legacyRequest);
|
||||||
const results = jobsQuery.count(request.pre.management.jobTypes, request.pre.user);
|
const results = jobsQuery.count(request.pre.management.jobTypes, request.pre.user);
|
||||||
return results;
|
return results;
|
||||||
},
|
},
|
||||||
|
@ -73,7 +77,8 @@ export function registerJobInfoRoutes(
|
||||||
path: `${MAIN_ENTRY}/output/{docId}`,
|
path: `${MAIN_ENTRY}/output/{docId}`,
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
options: getRouteConfig(),
|
options: getRouteConfig(),
|
||||||
handler: (request: RequestFacade) => {
|
handler: (legacyRequest: Legacy.Request) => {
|
||||||
|
const request = makeRequestFacade(legacyRequest);
|
||||||
const { docId } = request.params;
|
const { docId } = request.params;
|
||||||
|
|
||||||
return jobsQuery.get(request.pre.user, docId, { includeContent: true }).then(
|
return jobsQuery.get(request.pre.user, docId, { includeContent: true }).then(
|
||||||
|
@ -98,7 +103,8 @@ export function registerJobInfoRoutes(
|
||||||
path: `${MAIN_ENTRY}/info/{docId}`,
|
path: `${MAIN_ENTRY}/info/{docId}`,
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
options: getRouteConfig(),
|
options: getRouteConfig(),
|
||||||
handler: (request: RequestFacade) => {
|
handler: (legacyRequest: Legacy.Request) => {
|
||||||
|
const request = makeRequestFacade(legacyRequest);
|
||||||
const { docId } = request.params;
|
const { docId } = request.params;
|
||||||
|
|
||||||
return jobsQuery
|
return jobsQuery
|
||||||
|
@ -130,7 +136,8 @@ export function registerJobInfoRoutes(
|
||||||
path: `${MAIN_ENTRY}/download/{docId}`,
|
path: `${MAIN_ENTRY}/download/{docId}`,
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
options: getRouteConfigDownload(),
|
options: getRouteConfigDownload(),
|
||||||
handler: async (request: RequestFacade, h: ReportingResponseToolkit) => {
|
handler: async (legacyRequest: Legacy.Request, h: ReportingResponseToolkit) => {
|
||||||
|
const request = makeRequestFacade(legacyRequest);
|
||||||
const { docId } = request.params;
|
const { docId } = request.params;
|
||||||
|
|
||||||
let response = await jobResponseHandler(
|
let response = await jobResponseHandler(
|
||||||
|
|
|
@ -4,9 +4,10 @@
|
||||||
* you may not use this file except in compliance with the Elastic License.
|
* you may not use this file except in compliance with the Elastic License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import { Legacy } from 'kibana';
|
||||||
import querystring from 'querystring';
|
import querystring from 'querystring';
|
||||||
import { API_BASE_URL } from '../../common/constants';
|
import { API_BASE_URL } from '../../common/constants';
|
||||||
import { ServerFacade, RequestFacade, ReportingResponseToolkit } from '../../types';
|
import { ServerFacade, ReportingResponseToolkit } from '../../types';
|
||||||
import {
|
import {
|
||||||
getRouteConfigFactoryReportingPre,
|
getRouteConfigFactoryReportingPre,
|
||||||
GetRouteConfigFactoryFn,
|
GetRouteConfigFactoryFn,
|
||||||
|
@ -31,7 +32,7 @@ export function registerLegacy(
|
||||||
path,
|
path,
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
options: getStaticFeatureConfig(getRouteConfig, exportTypeId),
|
options: getStaticFeatureConfig(getRouteConfig, exportTypeId),
|
||||||
handler: async (request: RequestFacade, h: ReportingResponseToolkit) => {
|
handler: async (request: Legacy.Request, h: ReportingResponseToolkit) => {
|
||||||
const message = `The following URL is deprecated and will stop working in the next major version: ${request.url.path}`;
|
const message = `The following URL is deprecated and will stop working in the next major version: ${request.url.path}`;
|
||||||
server.log(['warning', 'reporting', 'deprecation'], message);
|
server.log(['warning', 'reporting', 'deprecation'], message);
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,62 @@
|
||||||
|
/*
|
||||||
|
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||||
|
* or more contributor license agreements. Licensed under the Elastic License;
|
||||||
|
* you may not use this file except in compliance with the Elastic License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { Legacy } from 'kibana';
|
||||||
|
import { makeRequestFacade } from './make_request_facade';
|
||||||
|
|
||||||
|
describe('makeRequestFacade', () => {
|
||||||
|
test('creates a default object', () => {
|
||||||
|
const legacyRequest = ({
|
||||||
|
getBasePath: () => 'basebase',
|
||||||
|
params: {
|
||||||
|
param1: 123,
|
||||||
|
},
|
||||||
|
payload: {
|
||||||
|
payload1: 123,
|
||||||
|
},
|
||||||
|
headers: {
|
||||||
|
user: 123,
|
||||||
|
},
|
||||||
|
} as unknown) as Legacy.Request;
|
||||||
|
|
||||||
|
expect(makeRequestFacade(legacyRequest)).toMatchInlineSnapshot(`
|
||||||
|
Object {
|
||||||
|
"getBasePath": [Function],
|
||||||
|
"getRawRequest": [Function],
|
||||||
|
"getSavedObjectsClient": undefined,
|
||||||
|
"headers": Object {
|
||||||
|
"user": 123,
|
||||||
|
},
|
||||||
|
"params": Object {
|
||||||
|
"param1": 123,
|
||||||
|
},
|
||||||
|
"payload": Object {
|
||||||
|
"payload1": 123,
|
||||||
|
},
|
||||||
|
"pre": undefined,
|
||||||
|
"query": undefined,
|
||||||
|
"route": undefined,
|
||||||
|
}
|
||||||
|
`);
|
||||||
|
});
|
||||||
|
|
||||||
|
test('getRawRequest', () => {
|
||||||
|
const legacyRequest = ({
|
||||||
|
getBasePath: () => 'basebase',
|
||||||
|
params: {
|
||||||
|
param1: 123,
|
||||||
|
},
|
||||||
|
payload: {
|
||||||
|
payload1: 123,
|
||||||
|
},
|
||||||
|
headers: {
|
||||||
|
user: 123,
|
||||||
|
},
|
||||||
|
} as unknown) as Legacy.Request;
|
||||||
|
|
||||||
|
expect(makeRequestFacade(legacyRequest).getRawRequest()).toBe(legacyRequest);
|
||||||
|
});
|
||||||
|
});
|
|
@ -0,0 +1,32 @@
|
||||||
|
/*
|
||||||
|
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
||||||
|
* or more contributor license agreements. Licensed under the Elastic License;
|
||||||
|
* you may not use this file except in compliance with the Elastic License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { RequestQuery } from 'hapi';
|
||||||
|
import { Legacy } from 'kibana';
|
||||||
|
import {
|
||||||
|
RequestFacade,
|
||||||
|
ReportingRequestPayload,
|
||||||
|
ReportingRequestPre,
|
||||||
|
ReportingRequestQuery,
|
||||||
|
} from '../../../types';
|
||||||
|
|
||||||
|
export function makeRequestFacade(request: Legacy.Request): RequestFacade {
|
||||||
|
// This condition is for unit tests
|
||||||
|
const getSavedObjectsClient = request.getSavedObjectsClient
|
||||||
|
? request.getSavedObjectsClient.bind(request)
|
||||||
|
: request.getSavedObjectsClient;
|
||||||
|
return {
|
||||||
|
getSavedObjectsClient,
|
||||||
|
headers: request.headers,
|
||||||
|
params: request.params,
|
||||||
|
payload: (request.payload as object) as ReportingRequestPayload,
|
||||||
|
query: ((request.query as RequestQuery) as object) as ReportingRequestQuery,
|
||||||
|
pre: (request.pre as Record<string, any>) as ReportingRequestPre,
|
||||||
|
getBasePath: request.getBasePath,
|
||||||
|
route: request.route,
|
||||||
|
getRawRequest: () => request,
|
||||||
|
};
|
||||||
|
}
|
|
@ -4,12 +4,13 @@
|
||||||
* you may not use this file except in compliance with the Elastic License.
|
* you may not use this file except in compliance with the Elastic License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import { Legacy } from 'kibana';
|
||||||
import { RequestFacade, ReportingResponseToolkit, JobDocPayload } from '../../types';
|
import { RequestFacade, ReportingResponseToolkit, JobDocPayload } from '../../types';
|
||||||
|
|
||||||
export type HandlerFunction = (
|
export type HandlerFunction = (
|
||||||
exportType: string,
|
exportType: string,
|
||||||
jobParams: object,
|
jobParams: object,
|
||||||
request: RequestFacade,
|
request: Legacy.Request,
|
||||||
h: ReportingResponseToolkit
|
h: ReportingResponseToolkit
|
||||||
) => any;
|
) => any;
|
||||||
|
|
||||||
|
|
51
x-pack/legacy/plugins/reporting/types.d.ts
vendored
51
x-pack/legacy/plugins/reporting/types.d.ts
vendored
|
@ -7,7 +7,6 @@
|
||||||
import { ResponseObject } from 'hapi';
|
import { ResponseObject } from 'hapi';
|
||||||
import { EventEmitter } from 'events';
|
import { EventEmitter } from 'events';
|
||||||
import { Legacy } from 'kibana';
|
import { Legacy } from 'kibana';
|
||||||
import { XPackMainPlugin } from '../xpack_main/server/xpack_main';
|
|
||||||
import {
|
import {
|
||||||
ElasticsearchPlugin,
|
ElasticsearchPlugin,
|
||||||
CallCluster,
|
CallCluster,
|
||||||
|
@ -16,6 +15,7 @@ import { CancellationToken } from './common/cancellation_token';
|
||||||
import { LevelLogger } from './server/lib/level_logger';
|
import { LevelLogger } from './server/lib/level_logger';
|
||||||
import { HeadlessChromiumDriverFactory } from './server/browsers/chromium/driver_factory';
|
import { HeadlessChromiumDriverFactory } from './server/browsers/chromium/driver_factory';
|
||||||
import { BrowserType } from './server/browsers/types';
|
import { BrowserType } from './server/browsers/types';
|
||||||
|
import { ServerFacade } from './index';
|
||||||
|
|
||||||
export type ReportingPlugin = object; // For Plugin contract
|
export type ReportingPlugin = object; // For Plugin contract
|
||||||
|
|
||||||
|
@ -53,7 +53,7 @@ export interface NetworkPolicy {
|
||||||
rules: NetworkPolicyRule[];
|
rules: NetworkPolicyRule[];
|
||||||
}
|
}
|
||||||
|
|
||||||
interface ListQuery {
|
export interface ListQuery {
|
||||||
page: string;
|
page: string;
|
||||||
size: string;
|
size: string;
|
||||||
ids?: string; // optional field forbids us from extending RequestQuery
|
ids?: string; // optional field forbids us from extending RequestQuery
|
||||||
|
@ -64,9 +64,6 @@ interface GenerateQuery {
|
||||||
interface GenerateExportTypePayload {
|
interface GenerateExportTypePayload {
|
||||||
jobParams: string;
|
jobParams: string;
|
||||||
}
|
}
|
||||||
interface DownloadParams {
|
|
||||||
docId: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Legacy System
|
* Legacy System
|
||||||
|
@ -74,26 +71,6 @@ interface DownloadParams {
|
||||||
|
|
||||||
export type ReportingPluginSpecOptions = Legacy.PluginSpecOptions;
|
export type ReportingPluginSpecOptions = Legacy.PluginSpecOptions;
|
||||||
|
|
||||||
export type ServerFacade = Legacy.Server & {
|
|
||||||
plugins: {
|
|
||||||
xpack_main?: XPackMainPlugin & {
|
|
||||||
status?: any;
|
|
||||||
};
|
|
||||||
};
|
|
||||||
};
|
|
||||||
|
|
||||||
interface ReportingRequest {
|
|
||||||
query: ListQuery & GenerateQuery;
|
|
||||||
params: DownloadParams;
|
|
||||||
payload: GenerateExportTypePayload;
|
|
||||||
pre: {
|
|
||||||
management: {
|
|
||||||
jobTypes: any;
|
|
||||||
};
|
|
||||||
user: any;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export type EnqueueJobFn = <JobParamsType>(
|
export type EnqueueJobFn = <JobParamsType>(
|
||||||
parentLogger: LevelLogger,
|
parentLogger: LevelLogger,
|
||||||
exportTypeId: string,
|
exportTypeId: string,
|
||||||
|
@ -103,7 +80,27 @@ export type EnqueueJobFn = <JobParamsType>(
|
||||||
request: RequestFacade
|
request: RequestFacade
|
||||||
) => Promise<Job>;
|
) => Promise<Job>;
|
||||||
|
|
||||||
export type RequestFacade = ReportingRequest & Legacy.Request;
|
export type ReportingRequestPayload = GenerateExportTypePayload | JobParamPostPayload;
|
||||||
|
export type ReportingRequestQuery = ListQuery | GenerateQuery;
|
||||||
|
|
||||||
|
export interface ReportingRequestPre {
|
||||||
|
management: {
|
||||||
|
jobTypes: any;
|
||||||
|
};
|
||||||
|
user: any; // TODO import AuthenticatedUser from security/server
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface RequestFacade {
|
||||||
|
getBasePath: Legacy.Request['getBasePath'];
|
||||||
|
getSavedObjectsClient: Legacy.Request['getSavedObjectsClient'];
|
||||||
|
headers: Legacy.Request['headers'];
|
||||||
|
params: Legacy.Request['params'];
|
||||||
|
payload: JobParamPostPayload | GenerateExportTypePayload;
|
||||||
|
query: ReportingRequestQuery;
|
||||||
|
route: Legacy.Request['route'];
|
||||||
|
pre: ReportingRequestPre;
|
||||||
|
getRawRequest: () => Legacy.Request;
|
||||||
|
}
|
||||||
|
|
||||||
export type ResponseFacade = ResponseObject & {
|
export type ResponseFacade = ResponseObject & {
|
||||||
isBoom: boolean;
|
isBoom: boolean;
|
||||||
|
@ -355,3 +352,5 @@ export interface InterceptedRequest {
|
||||||
frameId: string;
|
frameId: string;
|
||||||
resourceType: string;
|
resourceType: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export { ServerFacade };
|
||||||
|
|
Loading…
Reference in a new issue