Skip to content

Commit

Permalink
[7.x] [Reporting] Prepare export type definitions for Task Manager (#…
Browse files Browse the repository at this point in the history
…65213) (#69346)

* [Reporting] Prepare export type definitions for Task Manager (#65213)

# Conflicts:
#	x-pack/plugins/reporting/server/export_types/common/execute_job/get_full_urls.ts
#	x-pack/plugins/reporting/server/export_types/printable_pdf/server/create_job/index.ts
#	x-pack/plugins/reporting/server/export_types/printable_pdf/server/execute_job/index.test.ts

* fix unintentional change of data access
  • Loading branch information
tsullivan authored Jun 18, 2020
1 parent 1b5c8c2 commit 4aa3b7d
Show file tree
Hide file tree
Showing 38 changed files with 421 additions and 468 deletions.
2 changes: 0 additions & 2 deletions x-pack/plugins/reporting/public/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,6 @@ export class ReportingPublicPlugin implements Plugin<void, void> {
);
}

// FIXME: only perform these actions for authenticated routes
// Depends on https://github.com/elastic/kibana/pull/39477
public start(core: CoreStart) {
const { http, notifications } = core;
const apiClient = new ReportingAPIClient(http);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ interface HasEncryptedHeaders {
// TODO merge functionality with CSV execute job
export const decryptJobHeaders = async <
JobParamsType,
JobDocPayloadType extends HasEncryptedHeaders
ScheduledTaskParamsType extends HasEncryptedHeaders
>({
encryptionKey,
job,
logger,
}: {
encryptionKey?: string;
job: JobDocPayloadType;
job: ScheduledTaskParamsType;
logger: LevelLogger;
}): Promise<Record<string, string>> => {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import sinon from 'sinon';
import { ReportingConfig } from '../../../';
import { ReportingCore } from '../../../core';
import { createMockReportingCore } from '../../../test_helpers';
import { JobDocPayload } from '../../../types';
import { JobDocPayloadPDF } from '../../printable_pdf/types';
import { ScheduledTaskParams } from '../../../types';
import { ScheduledTaskParamsPDF } from '../../printable_pdf/types';
import { getConditionalHeaders, getCustomLogo } from './index';

let mockConfig: ReportingConfig;
Expand Down Expand Up @@ -37,7 +37,7 @@ describe('conditions', () => {
};

const conditionalHeaders = await getConditionalHeaders({
job: {} as JobDocPayload<any>,
job: {} as ScheduledTaskParams<any>,
filteredHeaders: permittedHeaders,
config: mockConfig,
});
Expand All @@ -64,14 +64,14 @@ test('uses basePath from job when creating saved object service', async () => {
baz: 'quix',
};
const conditionalHeaders = await getConditionalHeaders({
job: {} as JobDocPayload<any>,
job: {} as ScheduledTaskParams<any>,
filteredHeaders: permittedHeaders,
config: mockConfig,
});
const jobBasePath = '/sbp/s/marketing';
await getCustomLogo({
reporting: mockReportingPlugin,
job: { basePath: jobBasePath } as JobDocPayloadPDF,
job: { basePath: jobBasePath } as ScheduledTaskParamsPDF,
conditionalHeaders,
config: mockConfig,
});
Expand All @@ -94,14 +94,14 @@ test(`uses basePath from server if job doesn't have a basePath when creating sav
baz: 'quix',
};
const conditionalHeaders = await getConditionalHeaders({
job: {} as JobDocPayload<any>,
job: {} as ScheduledTaskParams<any>,
filteredHeaders: permittedHeaders,
config: mockConfig,
});

await getCustomLogo({
reporting: mockReportingPlugin,
job: {} as JobDocPayloadPDF,
job: {} as ScheduledTaskParamsPDF,
conditionalHeaders,
config: mockConfig,
});
Expand Down Expand Up @@ -139,7 +139,7 @@ describe('config formatting', () => {
mockConfig = getMockConfig(mockConfigGet);

const conditionalHeaders = await getConditionalHeaders({
job: {} as JobDocPayload<any>,
job: {} as ScheduledTaskParams<any>,
filteredHeaders: {},
config: mockConfig,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
import { ReportingConfig } from '../../../';
import { ConditionalHeaders } from '../../../types';

export const getConditionalHeaders = <JobDocPayloadType>({
export const getConditionalHeaders = <ScheduledTaskParamsType>({
config,
job,
filteredHeaders,
}: {
config: ReportingConfig;
job: JobDocPayloadType;
job: ScheduledTaskParamsType;
filteredHeaders: Record<string, string>;
}) => {
const { kbnConfig } = config;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import { ReportingCore } from '../../../core';
import { createMockReportingCore } from '../../../test_helpers';
import { JobDocPayloadPDF } from '../../printable_pdf/types';
import { ScheduledTaskParamsPDF } from '../../printable_pdf/types';
import { getConditionalHeaders, getCustomLogo } from './index';

const mockConfigGet = jest.fn().mockImplementation((key: string) => {
Expand Down Expand Up @@ -37,15 +37,15 @@ test(`gets logo from uiSettings`, async () => {
});

const conditionalHeaders = await getConditionalHeaders({
job: {} as JobDocPayloadPDF,
job: {} as ScheduledTaskParamsPDF,
filteredHeaders: permittedHeaders,
config: mockConfig,
});

const { logo } = await getCustomLogo({
reporting: mockReportingPlugin,
config: mockConfig,
job: {} as JobDocPayloadPDF,
job: {} as ScheduledTaskParamsPDF,
conditionalHeaders,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import { ReportingConfig, ReportingCore } from '../../../';
import { UI_SETTINGS_CUSTOM_PDF_LOGO } from '../../../../common/constants';
import { ConditionalHeaders } from '../../../types';
import { JobDocPayloadPDF } from '../../printable_pdf/types'; // Logo is PDF only
import { ScheduledTaskParamsPDF } from '../../printable_pdf/types'; // Logo is PDF only

export const getCustomLogo = async ({
reporting,
Expand All @@ -17,7 +17,7 @@ export const getCustomLogo = async ({
}: {
reporting: ReportingCore;
config: ReportingConfig;
job: JobDocPayloadPDF;
job: ScheduledTaskParamsPDF;
conditionalHeaders: ConditionalHeaders;
}) => {
const serverBasePath: string = config.kbnConfig.get('server', 'basePath');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
*/

import { ReportingConfig } from '../../../';
import { JobDocPayloadPNG } from '../../png/types';
import { JobDocPayloadPDF } from '../../printable_pdf/types';
import { ScheduledTaskParamsPNG } from '../../png/types';
import { ScheduledTaskParamsPDF } from '../../printable_pdf/types';
import { getFullUrls } from './get_full_urls';

interface FullUrlsOpts {
job: JobDocPayloadPNG & JobDocPayloadPDF;
job: ScheduledTaskParamsPNG & ScheduledTaskParamsPDF;
config: ReportingConfig;
}

Expand All @@ -35,7 +35,7 @@ beforeEach(() => {
mockConfig = getMockConfig(mockConfigGet);
});

const getMockJob = (base: object) => base as JobDocPayloadPNG & JobDocPayloadPDF;
const getMockJob = (base: object) => base as ScheduledTaskParamsPNG & ScheduledTaskParamsPDF;

test(`fails if no URL is passed`, async () => {
const fn = () => getFullUrls({ job: getMockJob({}), config: mockConfig } as FullUrlsOpts);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,26 @@ import {
import { ReportingConfig } from '../../..';
import { getAbsoluteUrlFactory } from '../../../../common/get_absolute_url';
import { validateUrls } from '../../../../common/validate_urls';
import { JobDocPayloadPNG } from '../../png/types';
import { JobDocPayloadPDF } from '../../printable_pdf/types';
import { ScheduledTaskParamsPNG } from '../../png/types';
import { ScheduledTaskParamsPDF } from '../../printable_pdf/types';

function isPngJob(job: JobDocPayloadPNG | JobDocPayloadPDF): job is JobDocPayloadPNG {
return (job as JobDocPayloadPNG).relativeUrl !== undefined;
function isPngJob(
job: ScheduledTaskParamsPNG | ScheduledTaskParamsPDF
): job is ScheduledTaskParamsPNG {
return (job as ScheduledTaskParamsPNG).relativeUrl !== undefined;
}
function isPdfJob(job: JobDocPayloadPNG | JobDocPayloadPDF): job is JobDocPayloadPDF {
return (job as JobDocPayloadPDF).objects !== undefined;
function isPdfJob(
job: ScheduledTaskParamsPNG | ScheduledTaskParamsPDF
): job is ScheduledTaskParamsPDF {
return (job as ScheduledTaskParamsPDF).objects !== undefined;
}

export function getFullUrls<JobDocPayloadType>({
export function getFullUrls<ScheduledTaskParamsType>({
config,
job,
}: {
config: ReportingConfig;
job: JobDocPayloadPDF | JobDocPayloadPNG;
job: ScheduledTaskParamsPDF | ScheduledTaskParamsPNG;
}) {
const [basePath, protocol, hostname, port] = [
config.kbnConfig.get('server', 'basePath'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import {
KBN_SCREENSHOT_HEADER_BLACKLIST_STARTS_WITH_PATTERN,
} from '../../../../common/constants';

export const omitBlacklistedHeaders = <JobDocPayloadType>({
export const omitBlacklistedHeaders = <ScheduledTaskParamsType>({
job,
decryptedHeaders,
}: {
job: JobDocPayloadType;
job: ScheduledTaskParamsType;
decryptedHeaders: Record<string, string>;
}) => {
const filteredHeaders: Record<string, string> = omit(
Expand Down
14 changes: 7 additions & 7 deletions x-pack/plugins/reporting/server/export_types/csv/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,21 @@ import {
import { CSV_JOB_TYPE as jobType } from '../../../constants';
import { ESQueueCreateJobFn, ESQueueWorkerExecuteFn, ExportTypeDefinition } from '../../types';
import { metadata } from './metadata';
import { createJobFactory } from './server/create_job';
import { executeJobFactory } from './server/execute_job';
import { JobDocPayloadDiscoverCsv, JobParamsDiscoverCsv } from './types';
import { scheduleTaskFnFactory } from './server/create_job';
import { runTaskFnFactory } from './server/execute_job';
import { JobParamsDiscoverCsv, ScheduledTaskParamsCSV } from './types';

export const getExportType = (): ExportTypeDefinition<
JobParamsDiscoverCsv,
ESQueueCreateJobFn<JobParamsDiscoverCsv>,
JobDocPayloadDiscoverCsv,
ESQueueWorkerExecuteFn<JobDocPayloadDiscoverCsv>
ScheduledTaskParamsCSV,
ESQueueWorkerExecuteFn<ScheduledTaskParamsCSV>
> => ({
...metadata,
jobType,
jobContentExtension: 'csv',
createJobFactory,
executeJobFactory,
scheduleTaskFnFactory,
runTaskFnFactory,
validLicenses: [
LICENSE_TYPE_TRIAL,
LICENSE_TYPE_BASIC,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,18 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { KibanaRequest, RequestHandlerContext } from 'src/core/server';
import { ReportingCore } from '../../../';
import { cryptoFactory } from '../../../lib';
import { CreateJobFactory, ESQueueCreateJobFn } from '../../../types';
import { ESQueueCreateJobFn, ScheduleTaskFnFactory } from '../../../types';
import { JobParamsDiscoverCsv } from '../types';

export const createJobFactory: CreateJobFactory<ESQueueCreateJobFn<
export const scheduleTaskFnFactory: ScheduleTaskFnFactory<ESQueueCreateJobFn<
JobParamsDiscoverCsv
>> = function createJobFactoryFn(reporting: ReportingCore) {
>> = function createJobFactoryFn(reporting) {
const config = reporting.getConfig();
const crypto = cryptoFactory(config.get('encryptionKey'));
const setupDeps = reporting.getPluginSetupDeps();

return async function createJob(
jobParams: JobParamsDiscoverCsv,
context: RequestHandlerContext,
request: KibanaRequest
) {
return async function scheduleTask(jobParams, context, request) {
const serializedEncryptedHeaders = await crypto.encrypt(request.headers);

const savedObjectsClient = context.core.savedObjects.client;
Expand Down
Loading

0 comments on commit 4aa3b7d

Please sign in to comment.