Skip to content
This repository has been archived by the owner on Aug 9, 2022. It is now read-only.

use session cookie for puppeteer to access url of security-enabled domain #129

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
97 changes: 97 additions & 0 deletions kibana-reports/server/executor/createScheduledReport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

import {
REPORT_TYPE,
REPORT_STATE,
LOCAL_HOST,
} from '../routes/utils/constants';
import { updateReportState, saveReport } from '../routes/utils/helpers';
import { ILegacyClusterClient, Logger } from '../../../../src/core/server';
import { createSavedSearchReport } from '../routes/utils/savedSearchReportHelper';
import { ReportSchemaType } from '../model';
import { CreateReportResultType } from '../routes/utils/types';
import { createVisualReport } from '../routes/utils/visualReportHelper';
import { deliverReport } from '../routes/lib/deliverReport';

export const createScheduledReport = async (
report: ReportSchemaType,
esClient: ILegacyClusterClient,
notificationClient: ILegacyClusterClient,
logger: Logger
): Promise<CreateReportResultType> => {
const isScheduledTask = true;
let createReportResult: CreateReportResultType;
let reportId;
// create new report instance and set report state to "pending"

const esResp = await saveReport(isScheduledTask, report, esClient);
reportId = esResp._id;

const reportDefinition = report.report_definition;
const reportParams = reportDefinition.report_params;
const reportSource = reportParams.report_source;

// compose url
const queryUrl = `${LOCAL_HOST}${report.query_url}`;
try {
// generate report
if (reportSource === REPORT_TYPE.savedSearch) {
createReportResult = await createSavedSearchReport(
report,
esClient,
isScheduledTask
);
} else {
// report source can only be one of [saved search, visualization, dashboard]
createReportResult = await createVisualReport(
reportParams,
queryUrl,
logger
);
}

await updateReportState(
isScheduledTask,
reportId,
esClient,
REPORT_STATE.created,
createReportResult
);

// deliver report
createReportResult = await deliverReport(
report,
createReportResult,
notificationClient,
esClient,
reportId,
isScheduledTask
);
} catch (error) {
// update report instance with "error" state
//TODO: save error detail and display on UI

await updateReportState(
isScheduledTask,
reportId,
esClient,
REPORT_STATE.error
);
throw error;
}

return createReportResult;
};
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
*/

import { ILegacyClusterClient, Logger } from '../../../../src/core/server';
import { createReport } from '../routes/utils/reportHelper';
import { POLL_INTERVAL } from './constants';
import { createScheduledReport } from './createScheduledReport';
import { POLL_INTERVAL } from '../utils/constants';
import {
ReportSchemaType,
DataReportSchemaType,
Expand Down Expand Up @@ -98,15 +98,14 @@ async function executeScheduledJob(
reportDefinition,
triggeredTime
);
// create report and return report data
const reportData = await createReport(
true,

const reportData = await createScheduledReport(
reportMetaData,
esClient,
logger,
notificationClient,
undefined
logger
);

logger.info(`new scheduled report created: ${reportData.fileName}`);
} catch (error) {
logger.error(
Expand Down
2 changes: 1 addition & 1 deletion kibana-reports/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
OpendistroKibanaReportsPluginStart,
} from './types';
import registerRoutes from './routes';
import { pollAndExecuteJob } from './utils/executor';
import { pollAndExecuteJob } from './executor/executor';
import { POLL_INTERVAL } from './utils/constants';

export interface ReportsPluginRequestContext {
Expand Down
136 changes: 136 additions & 0 deletions kibana-reports/server/routes/lib/createReport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

import {
REPORT_TYPE,
REPORT_STATE,
LOCAL_HOST,
SECURITY_AUTH_COOKIE_NAME,
} from '../utils/constants';
import { updateReportState, saveReport } from '../utils/helpers';
import {
ILegacyScopedClusterClient,
KibanaRequest,
Logger,
RequestHandlerContext,
} from '../../../../../src/core/server';
import { createSavedSearchReport } from '../utils/savedSearchReportHelper';
import { ReportSchemaType } from '../../model';
import { CreateReportResultType } from '../utils/types';
import { createVisualReport } from '../utils/visualReportHelper';
import { SetCookie } from 'puppeteer';
import { deliverReport } from './deliverReport';

export const createReport = async (
request: KibanaRequest,
context: RequestHandlerContext,
report: ReportSchemaType,
savedReportId?: string
): Promise<CreateReportResultType> => {
const isScheduledTask = false;
//@ts-ignore
const logger: Logger = context.reporting_plugin.logger;
// @ts-ignore
const notificationClient: ILegacyScopedClusterClient = context.reporting_plugin.notificationClient.asScoped(
request
);
const esClient = context.core.elasticsearch.legacy.client;

let createReportResult: CreateReportResultType;
let reportId;
// create new report instance and set report state to "pending"
if (savedReportId) {
reportId = savedReportId;
} else {
const esResp = await saveReport(isScheduledTask, report, esClient);
reportId = esResp._id;
}

const reportDefinition = report.report_definition;
const reportParams = reportDefinition.report_params;
const reportSource = reportParams.report_source;

// compose url
const queryUrl = `${LOCAL_HOST}${report.query_url}`;
try {
// generate report
if (reportSource === REPORT_TYPE.savedSearch) {
createReportResult = await createSavedSearchReport(
report,
esClient,
isScheduledTask
);
} else {
// report source can only be one of [saved search, visualization, dashboard]
let cookieObject: SetCookie | undefined;
if (request.headers.cookie) {
const cookies = request.headers.cookie.split(';');
cookies.map((item: string) => {
const cookie = item.trim().split('=');
if (cookie[0] === SECURITY_AUTH_COOKIE_NAME) {
cookieObject = {
name: cookie[0],
value: cookie[1],
url: queryUrl,
};
}
});
}

createReportResult = await createVisualReport(
reportParams,
queryUrl,
logger,
cookieObject
);
}
// update report state to "created"
if (!savedReportId) {
await updateReportState(
isScheduledTask,
reportId,
esClient,
REPORT_STATE.created,
createReportResult
);
}

// deliver report
if (!savedReportId) {
createReportResult = await deliverReport(
report,
createReportResult,
notificationClient,
esClient,
reportId,
isScheduledTask
);
}
} catch (error) {
// update report instance with "error" state
//TODO: save error detail and display on UI
if (!savedReportId) {
await updateReportState(
isScheduledTask,
reportId,
esClient,
REPORT_STATE.error
);
}
throw error;
}

return createReportResult;
};
70 changes: 70 additions & 0 deletions kibana-reports/server/routes/lib/createSchedule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Copyright 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

import { ReportDefinitionSchemaType } from 'server/model';
import {
KibanaRequest,
RequestHandlerContext,
} from '../../../../../src/core/server';
import { TRIGGER_TYPE } from '../utils/constants';

export const createSchedule = async (
request: KibanaRequest,
reportDefinitionId: string,
context: RequestHandlerContext
) => {
const reportDefinition: ReportDefinitionSchemaType = request.body;
const trigger = reportDefinition.trigger;
const triggerType = trigger.trigger_type;
const triggerParams = trigger.trigger_params;

// @ts-ignore
const schedulerClient: ILegacyScopedClusterClient = context.reporting_plugin.schedulerClient.asScoped(
request
);

if (triggerType === TRIGGER_TYPE.schedule) {
const schedule = triggerParams.schedule;

// compose the request body
const scheduledJob = {
schedule: schedule,
name: `${reportDefinition.report_params.report_name}_schedule`,
enabled: triggerParams.enabled,
report_definition_id: reportDefinitionId,
enabled_time: triggerParams.enabled_time,
};
// send to reports-scheduler to create a scheduled job
const res = await schedulerClient.callAsCurrentUser(
'reports_scheduler.createSchedule',
{
jobId: reportDefinitionId,
body: scheduledJob,
}
);

return res;
} else if (triggerType == TRIGGER_TYPE.onDemand) {
/*
* TODO: return nothing for on Demand report, because currently on-demand report is handled by client side,
Copy link
Contributor

Choose a reason for hiding this comment

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

What's the TODO here? It looks like we already return nothing for on-demand report?

* by hitting the create report http endpoint with data to get a report downloaded. Server side only saves
* that on-demand report definition into the index. Need further discussion on what behavior we want
* await createReport(reportDefinition, esClient);
*/
return;
}
// else if (triggerType == TRIGGER_TYPE.alerting) {
Copy link
Contributor

Choose a reason for hiding this comment

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

np: I think we can get rid of the commented-out else if block since there is a TODO statement

Copy link
Member Author

Choose a reason for hiding this comment

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

yes will do

//TODO: add alert-based scheduling logic [enhancement feature]
};
Loading