Skip to content

Commit

Permalink
[Reporting/NP] Migration of Reporting Security dependency (elastic#56046
Browse files Browse the repository at this point in the history
)

* first pass at new security integration

* fix tests

* cosmetic

* specialize security parameter for get user factory
  • Loading branch information
tsullivan committed Jan 28, 2020
1 parent 029e37b commit 1609708
Show file tree
Hide file tree
Showing 17 changed files with 205 additions and 96 deletions.
12 changes: 6 additions & 6 deletions x-pack/legacy/plugins/reporting/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { resolve } from 'path';
import { i18n } from '@kbn/i18n';
import { Legacy } from 'kibana';
import { IUiSettingsClient } from 'kibana/server';
import { resolve } from 'path';
import { PluginStart as DataPluginStart } from '../../../../src/plugins/data/server';
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';
import {
LegacySetup,
ReportingPlugin,
ReportingSetupDeps,
reportingPluginFactory,
ReportingSetupDeps,
} from './server/plugin';

import { PluginStart as DataPluginStart } from '../../../../src/plugins/data/server';
import { ReportingConfigOptions, ReportingPluginSpecOptions } from './types.d';

const kbToBase64Length = (kb: number) => {
return Math.floor((kb * 1024 * 8) / 6);
Expand Down Expand Up @@ -75,6 +75,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,
};

Expand All @@ -92,7 +93,6 @@ export const reporting = (kibana: any) => {
plugins: {
elasticsearch: server.plugins.elasticsearch,
xpack_main: server.plugins.xpack_main,
security: server.plugins.security,
},
savedObjects: server.savedObjects,
fieldFormatServiceFactory,
Expand Down
12 changes: 9 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,25 @@
*/

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,
security: ReportingSetupDeps['security'],
logger: Logger
) {
/*
* Legacy.Request because this is called from routing middleware
*/
return async (request: Legacy.Request) => {
if (!server.plugins.security) {
if (!security) {
return null;
}

try {
return await server.plugins.security.getUser(request);
return await 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 @@ -7,6 +7,7 @@
import { Legacy } from 'kibana';
import { CoreSetup, CoreStart, Plugin, LoggerFactory } 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';
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
16 changes: 9 additions & 7 deletions x-pack/legacy/plugins/reporting/server/routes/generation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,23 @@ 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 { registerLegacy } from './legacy';
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 @@ -74,12 +76,12 @@ export function registerJobGenerationRoutes(
return err;
}

registerGenerateFromJobParams(server, handler, handleError, logger);
registerLegacy(server, handler, handleError, logger); // 7.x only
registerGenerateFromJobParams(server, plugins, handler, handleError, logger);
registerLegacy(server, plugins, handler, handleError, logger); // 7.x only

// 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

0 comments on commit 1609708

Please sign in to comment.