Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Reporting] Define shims of legacy dependencies #54082

Merged
merged 33 commits into from
Jan 16, 2020
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
7315513
simplify serverfacade definition
tsullivan Jan 6, 2020
d954ef0
simplify requestfacade definition
tsullivan Jan 6, 2020
bb65245
makeRequestFacade
tsullivan Jan 6, 2020
79a9288
requestFacade
tsullivan Jan 6, 2020
c931081
use the shim
tsullivan Jan 6, 2020
62dc408
import sorting
tsullivan Jan 7, 2020
78a2ea1
originalServer
tsullivan Jan 7, 2020
3e71f57
reduce loc change
tsullivan Jan 7, 2020
956ae17
remove consolelog
tsullivan Jan 7, 2020
e4761ff
hacks to fix tests
tsullivan Jan 7, 2020
48ec88c
Merge branch 'master' into reporting/shim-plugin
elasticmachine Jan 7, 2020
44b714c
ServerFacade in index
tsullivan Jan 7, 2020
4192c34
Merge branch 'master' into reporting/shim-plugin
elasticmachine Jan 7, 2020
4e7e448
Cosmetic
tsullivan Jan 7, 2020
71bd4a5
remove field from serverfacade
tsullivan Jan 7, 2020
c64b4f7
Merge branch 'master' into reporting/shim-plugin
tsullivan Jan 8, 2020
ae8d134
add raw to the request
tsullivan Jan 8, 2020
81afb28
fix types
tsullivan Jan 8, 2020
a6e830a
add fieldFormatServiceFactory to legacy
tsullivan Jan 9, 2020
79524ef
Merge branch 'master' into reporting/shim-plugin
elasticmachine Jan 10, 2020
ec3f12b
Merge branch 'master' into reporting/shim-plugin
elasticmachine Jan 13, 2020
bdc87c9
Merge branch 'master' into reporting/shim-plugin
elasticmachine Jan 14, 2020
80e0fcc
Pass the complete request object to sec plugin
tsullivan Jan 14, 2020
17b3b10
Fix test
tsullivan Jan 14, 2020
f7609d1
fix test 2
tsullivan Jan 14, 2020
cb40fa3
getUser takes a legacy request
tsullivan Jan 14, 2020
15ab24b
add unit test for new lib
tsullivan Jan 14, 2020
37f6c3e
Merge branch 'master' into reporting/shim-plugin
elasticmachine Jan 15, 2020
ddf8fac
add getRawRequest to pass to saved objects method
tsullivan Jan 15, 2020
b4dcefb
update test snapshot
tsullivan Jan 16, 2020
9d446a1
Merge branch 'master' into reporting/shim-plugin
elasticmachine Jan 16, 2020
25e30da
leave a TODO comment for type import
tsullivan Jan 16, 2020
5378584
variable rename for legacy id
tsullivan Jan 16, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions x-pack/legacy/plugins/reporting/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ import {
getExportTypesRegistry,
runValidations,
} from './server/lib';
import { config as reportingConfig } from './config';
import { logConfiguration } from './log_configuration';
import { createBrowserDriverFactory } from './server/browsers';
import { registerReportingUsageCollector } from './server/usage';
import { ReportingConfigOptions, ReportingPluginSpecOptions, ServerFacade } from './types.d';
import { config as reportingConfig } from './config';
import { logConfiguration } from './log_configuration';
import { makeServerFacade } from './make_server_facade';

const kbToBase64Length = (kb: number) => {
return Math.floor((kb * 1024 * 8) / 6);
Expand Down Expand Up @@ -70,9 +71,8 @@ export const reporting = (kibana: any) => {
},
},

// TODO: Decouple Hapi: Build a server facade object based on the server to
// pass through to the libs. Do not pass server directly
async init(server: ServerFacade) {
async init(originalServer: ServerFacade) {
const server = makeServerFacade(originalServer);
const exportTypesRegistry = getExportTypesRegistry();

let isCollectorReady = false;
Expand Down
24 changes: 24 additions & 0 deletions x-pack/legacy/plugins/reporting/make_server_facade.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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 { ServerFacade } from './types';

export function makeServerFacade(server: ServerFacade): ServerFacade {
return {
config: server.config,
info: server.info,
route: server.route.bind(server),
newPlatform: server.newPlatform,
plugins: {
elasticsearch: server.plugins.elasticsearch,
xpack_main: server.plugins.xpack_main,
security: server.plugins.security,
},
savedObjects: server.savedObjects,
uiSettingsServiceFactory: server.uiSettingsServiceFactory,
log: server.log.bind(server),
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
* you may not use this file except in compliance with the Elastic License.
*/

export function getUserFactory(server) {
return async request => {
import { RequestFacade, ServerFacade } from '../../types';

export function getUserFactory(server: ServerFacade) {
return async (request: RequestFacade) => {
if (!server.plugins.security) {
return null;
}
Expand Down
8 changes: 4 additions & 4 deletions x-pack/legacy/plugins/reporting/server/lib/level_logger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* 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) => {
return typeof toTrim === 'string' ? toTrim.trim() : toTrim;
Expand All @@ -16,12 +16,12 @@ export class LevelLogger {

public warn: (msg: string, tags?: string[]) => void;

static createForServer(server: any, tags: string[]) {
const serverLog: ServerLog = (tgs: string[], msg: string) => server.log(tgs, msg);
static createForServer(server: ServerFacade, tags: string[]) {
const serverLog: ServerFacade['log'] = (tgs: string[], msg: string) => server.log(tgs, msg);
return new LevelLogger(serverLog, tags);
}

constructor(logger: ServerLog, tags: string[]) {
constructor(logger: ServerFacade['log'], tags: string[]) {
this._logger = logger;
this._tags = tags;

Expand Down
4 changes: 0 additions & 4 deletions x-pack/legacy/plugins/reporting/server/lib/once_per_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ export function oncePerServer(fn: ServerFn) {
throw new TypeError('This function expects to be called with a single argument');
}

if (!server || typeof server.expose !== 'function') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yesssss

throw new TypeError('This function expects to be passed the server');
}

// @ts-ignore
return fn.call(this, server);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
GetRouteConfigFactoryFn,
RouteConfigFactory,
} from './lib/route_config_factories';
import { makeRequestFacade } from './lib/make_request_facade';
import { HandlerErrorFunction, HandlerFunction } from './types';

const BASE_GENERATE = `${API_BASE_URL}/generate`;
Expand Down Expand Up @@ -54,7 +55,8 @@ export function registerGenerateFromJobParams(
path: `${BASE_GENERATE}/{exportType}`,
method: 'POST',
options: getRouteConfig(),
handler: async (request: RequestFacade, h: ReportingResponseToolkit) => {
handler: async (originalRequest: RequestFacade, h: ReportingResponseToolkit) => {
const request = makeRequestFacade(originalRequest);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to keep with the legacy naming convention here? originalRequest to legacyRequest?

let jobParamsRison: string | null;

if (request.payload) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { API_BASE_GENERATE_V1, CSV_FROM_SAVEDOBJECT_JOB_TYPE } from '../../commo
import { ServerFacade, RequestFacade, ReportingResponseToolkit } from '../../types';
import { HandlerErrorFunction, HandlerFunction, QueuedJobPayload } from './types';
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';

/*
Expand All @@ -31,7 +32,8 @@ export function registerGenerateCsvFromSavedObject(
path: `${API_BASE_GENERATE_V1}/csv/saved-object/{savedObjectType}:{savedObjectId}`,
method: 'POST',
options: routeOptions,
handler: async (request: RequestFacade, h: ReportingResponseToolkit) => {
handler: async (originalRequest: RequestFacade, h: ReportingResponseToolkit) => {
const request = makeRequestFacade(originalRequest);
/*
* 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ import {
JobDocOutputExecuted,
} from '../../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 { 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:
Expand All @@ -43,7 +44,8 @@ export function registerGenerateCsvFromSavedObjectImmediate(
path: `${API_BASE_GENERATE_V1}/immediate/csv/saved-object/{savedObjectType}:{savedObjectId}`,
method: 'POST',
options: routeOptions,
handler: async (request: RequestFacade, h: ReportingResponseToolkit) => {
handler: async (originalRequest: RequestFacade, h: ReportingResponseToolkit) => {
const request = makeRequestFacade(originalRequest);
const logger = parentLogger.clone(['savedobject-csv']);
const jobParams = getJobParamsFromRequest(request, { isImmediate: true });

Expand Down
4 changes: 3 additions & 1 deletion x-pack/legacy/plugins/reporting/server/routes/generation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { registerGenerateFromJobParams } from './generate_from_jobparams';
import { registerGenerateCsvFromSavedObject } from './generate_from_savedobject';
import { registerGenerateCsvFromSavedObjectImmediate } from './generate_from_savedobject_immediate';
import { createQueueFactory, enqueueJobFactory } from '../lib';
import { makeRequestFacade } from './lib/make_request_facade';

export function registerJobGenerationRoutes(
server: ServerFacade,
Expand All @@ -39,9 +40,10 @@ export function registerJobGenerationRoutes(
async function handler(
exportTypeId: string,
jobParams: object,
request: RequestFacade,
originalRequest: RequestFacade,
h: ReportingResponseToolkit
) {
const request = makeRequestFacade(originalRequest);
const user = request.pre.user;
const headers = request.headers;

Expand Down
16 changes: 11 additions & 5 deletions x-pack/legacy/plugins/reporting/server/routes/jobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
getRouteConfigFactoryDownloadPre,
getRouteConfigFactoryManagementPre,
} from './lib/route_config_factories';
import { makeRequestFacade } from './lib/make_request_facade';

const MAIN_ENTRY = `${API_BASE_URL}/jobs`;

Expand All @@ -40,7 +41,8 @@ export function registerJobInfoRoutes(
path: `${MAIN_ENTRY}/list`,
method: 'GET',
options: getRouteConfig(),
handler: (request: RequestFacade) => {
handler: (originalRequest: RequestFacade) => {
const request = makeRequestFacade(originalRequest);
const { page: queryPage, size: querySize, ids: queryIds } = request.query;
const page = parseInt(queryPage, 10) || 0;
const size = Math.min(100, parseInt(querySize, 10) || 10);
Expand All @@ -62,7 +64,8 @@ export function registerJobInfoRoutes(
path: `${MAIN_ENTRY}/count`,
method: 'GET',
options: getRouteConfig(),
handler: (request: RequestFacade) => {
handler: (originalRequest: RequestFacade) => {
const request = makeRequestFacade(originalRequest);
const results = jobsQuery.count(request.pre.management.jobTypes, request.pre.user);
return results;
},
Expand All @@ -73,7 +76,8 @@ export function registerJobInfoRoutes(
path: `${MAIN_ENTRY}/output/{docId}`,
method: 'GET',
options: getRouteConfig(),
handler: (request: RequestFacade) => {
handler: (originalRequest: RequestFacade) => {
const request = makeRequestFacade(originalRequest);
const { docId } = request.params;

return jobsQuery.get(request.pre.user, docId, { includeContent: true }).then(
Expand All @@ -98,7 +102,8 @@ export function registerJobInfoRoutes(
path: `${MAIN_ENTRY}/info/{docId}`,
method: 'GET',
options: getRouteConfig(),
handler: (request: RequestFacade) => {
handler: (originalRequest: RequestFacade) => {
const request = makeRequestFacade(originalRequest);
const { docId } = request.params;

return jobsQuery
Expand Down Expand Up @@ -130,7 +135,8 @@ export function registerJobInfoRoutes(
path: `${MAIN_ENTRY}/download/{docId}`,
method: 'GET',
options: getRouteConfigDownload(),
handler: async (request: RequestFacade, h: ReportingResponseToolkit) => {
handler: async (originalRequest: RequestFacade, h: ReportingResponseToolkit) => {
const request = makeRequestFacade(originalRequest);
const { docId } = request.params;

let response = await jobResponseHandler(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export const authorizedUserPreRoutingFactory = function authorizedUserPreRouting
const config = server.config();

return async function authorizedUserPreRouting(request) {
console.log('how are you');
const xpackInfo = server.plugins.xpack_main.info;

if (!xpackInfo || !xpackInfo.isAvailable()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* 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 { RequestFacade } from '../../../types';

export function makeRequestFacade(request: RequestFacade): RequestFacade {
return {
headers: request.headers,
auth: request.auth, // for getUser
params: request.params,
payload: request.payload,
query: request.query,
pre: request.pre,
getBasePath: request.getBasePath,
getSavedObjectsClient: request.getSavedObjectsClient.bind(request),
route: request.route,
};
}
47 changes: 29 additions & 18 deletions x-pack/legacy/plugins/reporting/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,34 +64,30 @@ interface GenerateQuery {
interface GenerateExportTypePayload {
jobParams: string;
}
interface DownloadParams {
docId: string;
}

/*
* Legacy System
*/

export type ReportingPluginSpecOptions = Legacy.PluginSpecOptions;

export type ServerFacade = Legacy.Server & {
type LegacyPlugins = Legacy.Server['plugins'];

export interface ServerFacade {
config: Legacy.Server['config'];
info: Legacy.Server['info'];
log: Legacy.Server['log'];
newPlatform: Legacy.Server['newPlatform'];
plugins: {
xpack_main?: XPackMainPlugin & {
elasticsearch: LegacyPlugins['elasticsearch'];
security: LegacyPlugins['security'];
xpack_main: XPackMainPlugin & {
status?: any;
};
};
};

interface ReportingRequest {
query: ListQuery & GenerateQuery;
params: DownloadParams;
payload: GenerateExportTypePayload;
pre: {
management: {
jobTypes: any;
};
user: any;
};
route: Legacy.Server['route'];
savedObjects: Legacy.Server['savedObjects'];
uiSettingsServiceFactory: Legacy.Server['uiSettingsServiceFactory'];
}

export type EnqueueJobFn = <JobParamsType>(
Expand All @@ -103,7 +99,22 @@ export type EnqueueJobFn = <JobParamsType>(
request: RequestFacade
) => Promise<Job>;

export type RequestFacade = ReportingRequest & Legacy.Request;
export interface RequestFacade {
getBasePath: Legacy.Request['getBasePath'];
getSavedObjectsClient: Legacy.Request['getSavedObjectsClient'];
auth: Legacy.Request['auth'];
headers: Legacy.Request['headers'];
params: Legacy.Request['params'];
payload: JobParamPostPayload | GenerateExportTypePayload;
query: ListQuery & GenerateQuery;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure why this isn't ListQuery | GenerateQuery

route: Legacy.Request['route'];
pre: {
management: {
jobTypes: any;
};
user: any;
};
}

export type ResponseFacade = ResponseObject & {
isBoom: boolean;
Expand Down