Skip to content

Commit

Permalink
Forward extra headers while using headless chromium (opensearch-proje…
Browse files Browse the repository at this point in the history
  • Loading branch information
zhongnansu committed Oct 28, 2021
1 parent 988db2a commit bc5d9a1
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 22 deletions.
21 changes: 5 additions & 16 deletions kibana-reports/server/routes/lib/createReport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
LOCAL_HOST,
SECURITY_AUTH_COOKIE_NAME,
DELIVERY_TYPE,
EXTRA_HEADERS,
} from '../utils/constants';

import {
Expand All @@ -36,6 +37,8 @@ import { deliverReport } from './deliverReport';
import { updateReportState } from './updateReportState';
import { saveReport } from './saveReport';
import { SemaphoreInterface } from 'async-mutex';
import { AccessInfoType } from 'server';
import _ from 'lodash';

export const createReport = async (
request: KibanaRequest,
Expand Down Expand Up @@ -90,28 +93,14 @@ export const createReport = async (
// report source can only be one of [saved search, visualization, dashboard]
// compose url
const completeQueryUrl = `${LOCAL_HOST}${report.query_url}`;
// Check if security is enabled. TODO: is there a better way to check?
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: completeQueryUrl,
};
}
});
}
const extraHeaders = _.pick(request.headers, EXTRA_HEADERS);
const [value, release] = await semaphore.acquire();
try {
createReportResult = await createVisualReport(
reportParams,
completeQueryUrl,
logger,
cookieObject,
extraHeaders,
timezone
);
} finally {
Expand Down
13 changes: 13 additions & 0 deletions kibana-reports/server/routes/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,20 @@ export const BASE_PATH = '/_plugin/kibana';

export const DEFAULT_REPORT_HEADER = '<h1>Open Distro Kibana Reports</h1>';

<<<<<<< HEAD:kibana-reports/server/routes/utils/constants.ts
export const SECURITY_AUTH_COOKIE_NAME = 'security_authentication';
=======
export const SECURITY_CONSTANTS = {
TENANT_LOCAL_STORAGE_KEY: 'opendistro::security::tenant::show_popup',
};
>>>>>>> 1b96740... Forward extra headers while using headless chromium (#194):dashboards-reports/server/routes/utils/constants.ts

export const EXTRA_HEADERS = [
'cookie',
'x-proxy-user',
'x-proxy-roles',
'x-forwarded-for',
];

export const CHROMIUM_PATH = `${__dirname}/../../../.chromium/headless_shell`;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* permissions and limitations under the License.
*/

import puppeteer, { ElementHandle, SetCookie } from 'puppeteer-core';
import puppeteer, { Headers } from 'puppeteer-core';
import createDOMPurify from 'dompurify';
import { JSDOM } from 'jsdom';
import { Logger } from '../../../../../../src/core/server';
Expand All @@ -29,12 +29,13 @@ import { CreateReportResultType } from '../types';
import { ReportParamsSchemaType, VisualReportSchemaType } from 'server/model';
import fs from 'fs';
import cheerio from 'cheerio';
import _ from 'lodash';

export const createVisualReport = async (
reportParams: ReportParamsSchemaType,
queryUrl: string,
logger: Logger,
cookie?: SetCookie,
extraHeaders: Headers,
timezone?: string
): Promise<CreateReportResultType> => {
const {
Expand Down Expand Up @@ -94,7 +95,13 @@ export const createVisualReport = async (
* TODO: temp fix to disable sandbox when launching chromium on Linux instance
* https://github.com/puppeteer/puppeteer/blob/main/docs/troubleshooting.md#setting-up-chrome-linux-sandbox
*/
args: ['--no-sandbox', '--disable-setuid-sandbox', '--disable-gpu', '--no-zygote', '--single-process'],
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
'--disable-gpu',
'--no-zygote',
'--single-process',
],
executablePath: CHROMIUM_PATH,
env: {
TZ: timezone || 'UTC',
Expand All @@ -103,9 +110,9 @@ export const createVisualReport = async (
const page = await browser.newPage();
page.setDefaultNavigationTimeout(0);
page.setDefaultTimeout(100000); // use 100s timeout instead of default 30s
if (cookie) {
logger.info('domain enables security, use session cookie to access');
await page.setCookie(cookie);
// Set extra headers that are needed
if (!_.isEmpty(extraHeaders)) {
await page.setExtraHTTPHeaders(extraHeaders);
}
logger.info(`original queryUrl ${queryUrl}`);
await page.goto(queryUrl, { waitUntil: 'networkidle0' });
Expand Down

0 comments on commit bc5d9a1

Please sign in to comment.