This repository has been archived by the owner on Aug 9, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
use session cookie for puppeteer to access url of security-enabled domain #129
Merged
zhongnansu
merged 6 commits into
opendistro-for-elasticsearch:dev
from
zhongnansu:session-cookie
Oct 22, 2020
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
d204e9c
improve input validation
zhongnansu 77a4a11
clean up
zhongnansu 751d61d
validate report name and email address
zhongnansu f5f3389
fix
zhongnansu 98bac62
use session cookie to access security-enabled domain
zhongnansu 5b534e9
Merge remote-tracking branch 'upstream/dev' into session-cookie
zhongnansu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
* 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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. np: I think we can get rid of the commented-out There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes will do |
||
//TODO: add alert-based scheduling logic [enhancement feature] | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?