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/NP] Migration of Reporting Security dependency #56046

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
3 changes: 2 additions & 1 deletion x-pack/legacy/plugins/reporting/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import { resolve } from 'path';
import { i18n } from '@kbn/i18n';
import { Legacy } from 'kibana';
import { PluginSetupContract as SecurityPluginSetup } from '../../../plugins/security/server';
import { PLUGIN_ID, UI_SETTINGS_CUSTOM_PDF_LOGO } from './common/constants';
import { ReportingConfigOptions, ReportingPluginSpecOptions } from './types.d';
import { config as reportingConfig } from './config';
Expand Down Expand Up @@ -68,6 +69,7 @@ export const reporting = (kibana: any) => {
async init(server: Legacy.Server) {
const coreSetup = server.newPlatform.setup.core;
const pluginsSetup: ReportingSetupDeps = {
security: server.newPlatform.setup.plugins.security as SecurityPluginSetup,
usageCollection: server.newPlatform.setup.plugins.usageCollection,
};
const __LEGACY: LegacySetup = {
Expand All @@ -77,7 +79,6 @@ export const reporting = (kibana: any) => {
plugins: {
elasticsearch: server.plugins.elasticsearch,
xpack_main: server.plugins.xpack_main,
security: server.plugins.security,
},
savedObjects: server.savedObjects,
uiSettingsServiceFactory: server.uiSettingsServiceFactory,
Expand Down
8 changes: 5 additions & 3 deletions x-pack/legacy/plugins/reporting/server/lib/get_user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@
*/

import { Legacy } from 'kibana';
import { KibanaRequest } from '../../../../../../src/core/server';
import { Logger, ServerFacade } from '../../types';
import { ReportingSetupDeps } from '../plugin';

export function getUserFactory(server: ServerFacade, logger: Logger) {
export function getUserFactory(server: ServerFacade, plugins: ReportingSetupDeps, logger: Logger) {
/*
* Legacy.Request because this is called from routing middleware
*/
return async (request: Legacy.Request) => {
if (!server.plugins.security) {
if (!plugins.security) {
return null;
}

try {
return await server.plugins.security.getUser(request);
return await plugins.security.authc.getCurrentUser(KibanaRequest.from(request));
} catch (err) {
logger.error(err, ['getUser']);
return null;
Expand Down
5 changes: 3 additions & 2 deletions x-pack/legacy/plugins/reporting/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Legacy } from 'kibana';
import { CoreSetup, CoreStart, Plugin, LoggerFactory } from 'src/core/server';
import { IUiSettingsClient } from 'src/core/server';
import { UsageCollectionSetup } from 'src/plugins/usage_collection/server';
import { PluginSetupContract as SecurityPluginSetup } from '../../../../plugins/security/server';
import { XPackMainPlugin } from '../../xpack_main/server/xpack_main';
// @ts-ignore
import { mirrorPluginStatus } from '../../../server/lib/mirror_plugin_status';
Expand All @@ -29,6 +30,7 @@ export type ReportingStart = object;

export interface ReportingSetupDeps {
usageCollection: UsageCollectionSetup;
security: SecurityPluginSetup;
}
export type ReportingStartDeps = object;

Expand All @@ -39,7 +41,6 @@ export interface LegacySetup {
info: Legacy.Server['info'];
plugins: {
elasticsearch: LegacyPlugins['elasticsearch'];
security: LegacyPlugins['security'];
xpack_main: XPackMainPlugin & {
status?: any;
};
Expand Down Expand Up @@ -105,7 +106,7 @@ export function reportingPluginFactory(
isCollectorReady = true;

// Reporting routes
registerRoutes(__LEGACY, exportTypesRegistry, browserDriverFactory, logger);
registerRoutes(__LEGACY, plugins, exportTypesRegistry, browserDriverFactory, logger);

return {};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,34 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { Legacy } from 'kibana';
import boom from 'boom';
import Joi from 'joi';
import { Legacy } from 'kibana';
import rison from 'rison-node';
import { API_BASE_URL } from '../../common/constants';
import { ServerFacade, ReportingResponseToolkit, Logger } from '../../types';
import { Logger, ReportingResponseToolkit, ServerFacade } from '../../types';
import { ReportingSetupDeps } from '../plugin';
import { makeRequestFacade } from './lib/make_request_facade';
import {
getRouteConfigFactoryReportingPre,
GetRouteConfigFactoryFn,
getRouteConfigFactoryReportingPre,
RouteConfigFactory,
} from './lib/route_config_factories';
import { makeRequestFacade } from './lib/make_request_facade';
Copy link
Member Author

Choose a reason for hiding this comment

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

more automated re-organization of the imports

import { HandlerErrorFunction, HandlerFunction } from './types';

const BASE_GENERATE = `${API_BASE_URL}/generate`;

export function registerGenerateFromJobParams(
server: ServerFacade,
plugins: ReportingSetupDeps,
handler: HandlerFunction,
handleError: HandlerErrorFunction,
logger: Logger
) {
const getRouteConfig = () => {
const getOriginalRouteConfig: GetRouteConfigFactoryFn = getRouteConfigFactoryReportingPre(
server,
plugins,
logger
);
const routeConfigFactory: RouteConfigFactory = getOriginalRouteConfig(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
import { Legacy } from 'kibana';
import { get } from 'lodash';
import { API_BASE_GENERATE_V1, CSV_FROM_SAVEDOBJECT_JOB_TYPE } from '../../common/constants';
import { ServerFacade, ReportingResponseToolkit, Logger } 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';
import { Logger, ReportingResponseToolkit, ServerFacade } from '../../types';
import { ReportingSetupDeps } from '../plugin';
import { makeRequestFacade } from './lib/make_request_facade';
import { getRouteOptionsCsv } from './lib/route_config_factories';
import { HandlerErrorFunction, HandlerFunction, QueuedJobPayload } from './types';

/*
* This function registers API Endpoints for queuing Reporting jobs. The API inputs are:
Expand All @@ -24,11 +25,12 @@ import { getJobParamsFromRequest } from '../../export_types/csv_from_savedobject
*/
export function registerGenerateCsvFromSavedObject(
server: ServerFacade,
plugins: ReportingSetupDeps,
handleRoute: HandlerFunction,
handleRouteError: HandlerErrorFunction,
logger: Logger
) {
const routeOptions = getRouteOptionsCsv(server, logger);
const routeOptions = getRouteOptionsCsv(server, plugins, logger);

server.route({
path: `${API_BASE_GENERATE_V1}/csv/saved-object/{savedObjectType}:{savedObjectId}`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@
import { Legacy } from 'kibana';
import { API_BASE_GENERATE_V1 } from '../../common/constants';
import { createJobFactory, executeJobFactory } from '../../export_types/csv_from_savedobject';
import { getJobParamsFromRequest } from '../../export_types/csv_from_savedobject/server/lib/get_job_params_from_request';
import { JobDocPayloadPanelCsv } from '../../export_types/csv_from_savedobject/types';
import {
ServerFacade,
ResponseFacade,
HeadlessChromiumDriverFactory,
ReportingResponseToolkit,
Logger,
JobDocOutput,
Logger,
ReportingResponseToolkit,
ResponseFacade,
ServerFacade,
} from '../../types';
import { JobDocPayloadPanelCsv } from '../../export_types/csv_from_savedobject/types';
import { getJobParamsFromRequest } from '../../export_types/csv_from_savedobject/server/lib/get_job_params_from_request';
import { getRouteOptionsCsv } from './lib/route_config_factories';
import { ReportingSetupDeps } from '../plugin';
import { makeRequestFacade } from './lib/make_request_facade';
import { getRouteOptionsCsv } from './lib/route_config_factories';

/*
* This function registers API Endpoints for immediate Reporting jobs. The API inputs are:
Expand All @@ -31,9 +32,10 @@ import { makeRequestFacade } from './lib/make_request_facade';
*/
export function registerGenerateCsvFromSavedObjectImmediate(
server: ServerFacade,
plugins: ReportingSetupDeps,
parentLogger: Logger
) {
const routeOptions = getRouteOptionsCsv(server, parentLogger);
const routeOptions = getRouteOptionsCsv(server, plugins, parentLogger);

/*
* CSV export with the `immediate` option does not queue a job with Reporting's ESQueue to run the job async. Instead, this does:
Expand Down
14 changes: 8 additions & 6 deletions x-pack/legacy/plugins/reporting/server/routes/generation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,22 @@ import boom from 'boom';
import { Legacy } from 'kibana';
import { API_BASE_URL } from '../../common/constants';
import {
ServerFacade,
ExportTypesRegistry,
HeadlessChromiumDriverFactory,
ReportingResponseToolkit,
Logger,
ReportingResponseToolkit,
ServerFacade,
} from '../../types';
import { createQueueFactory, enqueueJobFactory } from '../lib';
import { ReportingSetupDeps } from '../plugin';
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,
plugins: ReportingSetupDeps,
exportTypesRegistry: ExportTypesRegistry,
browserDriverFactory: HeadlessChromiumDriverFactory,
logger: Logger
Expand Down Expand Up @@ -73,11 +75,11 @@ export function registerJobGenerationRoutes(
return err;
}

registerGenerateFromJobParams(server, handler, handleError, logger);
registerGenerateFromJobParams(server, plugins, handler, handleError, logger);

// Register beta panel-action download-related API's
if (config.get('xpack.reporting.csv.enablePanelActionDownload')) {
registerGenerateCsvFromSavedObject(server, handler, handleError, logger);
registerGenerateCsvFromSavedObjectImmediate(server, logger);
registerGenerateCsvFromSavedObject(server, plugins, handler, handleError, logger);
registerGenerateCsvFromSavedObjectImmediate(server, plugins, logger);
}
}
8 changes: 5 additions & 3 deletions x-pack/legacy/plugins/reporting/server/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@
*/

import {
ServerFacade,
ExportTypesRegistry,
HeadlessChromiumDriverFactory,
Logger,
ServerFacade,
} from '../../types';
import { ReportingSetupDeps } from '../plugin';
import { registerJobGenerationRoutes } from './generation';
import { registerJobInfoRoutes } from './jobs';

export function registerRoutes(
server: ServerFacade,
plugins: ReportingSetupDeps,
exportTypesRegistry: ExportTypesRegistry,
browserDriverFactory: HeadlessChromiumDriverFactory,
logger: Logger
) {
registerJobGenerationRoutes(server, exportTypesRegistry, browserDriverFactory, logger);
registerJobInfoRoutes(server, exportTypesRegistry, logger);
registerJobGenerationRoutes(server, plugins, exportTypesRegistry, browserDriverFactory, logger);
registerJobInfoRoutes(server, plugins, exportTypesRegistry, logger);
}
14 changes: 9 additions & 5 deletions x-pack/legacy/plugins/reporting/server/routes/jobs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ beforeEach(() => {
};
});

const mockPlugins = {
security: null,
};

const getHits = (...sources) => {
return {
hits: {
Expand All @@ -67,7 +71,7 @@ test(`returns 404 if job not found`, async () => {
.getCluster('admin')
.callWithInternalUser.mockReturnValue(Promise.resolve(getHits()));

registerJobInfoRoutes(mockServer, exportTypesRegistry, mockLogger);
registerJobInfoRoutes(mockServer, mockPlugins, exportTypesRegistry, mockLogger);

const request = {
method: 'GET',
Expand All @@ -84,7 +88,7 @@ test(`returns 401 if not valid job type`, async () => {
.getCluster('admin')
.callWithInternalUser.mockReturnValue(Promise.resolve(getHits({ jobtype: 'invalidJobType' })));

registerJobInfoRoutes(mockServer, exportTypesRegistry, mockLogger);
registerJobInfoRoutes(mockServer, mockPlugins, exportTypesRegistry, mockLogger);

const request = {
method: 'GET',
Expand All @@ -103,7 +107,7 @@ describe(`when job is incomplete`, () => {
Promise.resolve(getHits({ jobtype: 'unencodedJobType', status: 'pending' }))
);

registerJobInfoRoutes(mockServer, exportTypesRegistry, mockLogger);
registerJobInfoRoutes(mockServer, mockPlugins, exportTypesRegistry, mockLogger);

const request = {
method: 'GET',
Expand Down Expand Up @@ -145,7 +149,7 @@ describe(`when job is failed`, () => {
.getCluster('admin')
.callWithInternalUser.mockReturnValue(Promise.resolve(hits));

registerJobInfoRoutes(mockServer, exportTypesRegistry, mockLogger);
registerJobInfoRoutes(mockServer, mockPlugins, exportTypesRegistry, mockLogger);

const request = {
method: 'GET',
Expand Down Expand Up @@ -190,7 +194,7 @@ describe(`when job is completed`, () => {
.getCluster('admin')
.callWithInternalUser.mockReturnValue(Promise.resolve(hits));

registerJobInfoRoutes(mockServer, exportTypesRegistry, mockLogger);
registerJobInfoRoutes(mockServer, mockPlugins, exportTypesRegistry, mockLogger);

const request = {
method: 'GET',
Expand Down
16 changes: 9 additions & 7 deletions x-pack/legacy/plugins/reporting/server/routes/jobs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,26 @@
*/

import Boom from 'boom';
import { Legacy } from 'kibana';
import { ResponseObject } from 'hapi';
import { Legacy } from 'kibana';
import { API_BASE_URL } from '../../common/constants';
import {
ServerFacade,
ExportTypesRegistry,
Logger,
ReportingResponseToolkit,
JobDocOutput,
JobSource,
ListQuery,
Logger,
ReportingResponseToolkit,
ServerFacade,
} from '../../types';
import { jobsQueryFactory } from '../lib/jobs_query';
import { ReportingSetupDeps } from '../plugin';
import { jobResponseHandlerFactory } from './lib/job_response_handler';
import { makeRequestFacade } from './lib/make_request_facade';
import {
getRouteConfigFactoryDownloadPre,
getRouteConfigFactoryManagementPre,
} from './lib/route_config_factories';
import { makeRequestFacade } from './lib/make_request_facade';

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

Expand All @@ -33,12 +34,13 @@ function isResponse(response: Boom<null> | ResponseObject): response is Response

export function registerJobInfoRoutes(
server: ServerFacade,
plugins: ReportingSetupDeps,
exportTypesRegistry: ExportTypesRegistry,
logger: Logger
) {
const jobsQuery = jobsQueryFactory(server);
const getRouteConfig = getRouteConfigFactoryManagementPre(server, logger);
const getRouteConfigDownload = getRouteConfigFactoryDownloadPre(server, logger);
const getRouteConfig = getRouteConfigFactoryManagementPre(server, plugins, logger);
const getRouteConfigDownload = getRouteConfigFactoryDownloadPre(server, plugins, logger);

// list jobs in the queue, paginated
server.route({
Expand Down
Loading