Skip to content

Commit

Permalink
apply changes for localhost access (opensearch-project#188)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhongnansu authored Oct 25, 2021
1 parent 8df7942 commit e985041
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 133 deletions.
6 changes: 0 additions & 6 deletions dashboards-reports/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ import {
PluginInitializerContext,
} from '../../../src/core/server';
import { ReportsDashboardsPlugin } from './plugin';

export type AccessInfoType = {
basePath: string;
serverInfo: HttpServerInfo;
};

// This exports static code and TypeScript types,
// as well as, OpenSearch Dashboards Platform `plugin()` initializer.

Expand Down
13 changes: 7 additions & 6 deletions dashboards-reports/server/model/backendModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
REPORT_STATE,
REPORT_TYPE,
TRIGGER_TYPE,
BASE_PATH,
} from '../routes/utils/constants';

export type BackendReportInstanceType = {
Expand Down Expand Up @@ -108,7 +109,7 @@ export enum BACKEND_REPORT_SOURCE {
dashboard = 'Dashboard',
visualization = 'Visualization',
savedSearch = 'SavedSearch',
notebook = 'Notebook'
notebook = 'Notebook',
}

export enum BACKEND_REPORT_STATE {
Expand Down Expand Up @@ -142,7 +143,7 @@ export const REPORT_SOURCE_DICT = {
[REPORT_TYPE.dashboard]: BACKEND_REPORT_SOURCE.dashboard,
[REPORT_TYPE.visualization]: BACKEND_REPORT_SOURCE.visualization,
[REPORT_TYPE.savedSearch]: BACKEND_REPORT_SOURCE.savedSearch,
[REPORT_TYPE.notebook]: BACKEND_REPORT_SOURCE.notebook
[REPORT_TYPE.notebook]: BACKEND_REPORT_SOURCE.notebook,
};

export const REPORT_FORMAT_DICT = {
Expand All @@ -163,8 +164,8 @@ export const TRIGGER_TYPE_DICT = {
};

export const URL_PREFIX_DICT = {
[BACKEND_REPORT_SOURCE.dashboard]: '/app/dashboards#/view/',
[BACKEND_REPORT_SOURCE.savedSearch]: '/app/discover#/view/',
[BACKEND_REPORT_SOURCE.visualization]: '/app/visualize#/edit/',
[BACKEND_REPORT_SOURCE.notebook]: '/app/notebooks-dashboards?view=output_only#/'
[BACKEND_REPORT_SOURCE.dashboard]: `${BASE_PATH}/app/dashboards#/view/`,
[BACKEND_REPORT_SOURCE.savedSearch]: `${BASE_PATH}/app/discover#/view/`,
[BACKEND_REPORT_SOURCE.visualization]: `${BASE_PATH}/app/visualize#/edit/`,
[BACKEND_REPORT_SOURCE.notebook]: `${BASE_PATH}/app/notebooks-dashboards?view=output_only#/`,
};
11 changes: 1 addition & 10 deletions dashboards-reports/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ import {
import registerRoutes from './routes';
import { pollAndExecuteJob } from './executor/executor';
import { POLL_INTERVAL } from './utils/constants';
import { AccessInfoType } from 'server';

export interface ReportsPluginRequestContext {
logger: Logger;
Expand Down Expand Up @@ -71,14 +70,6 @@ export class ReportsDashboardsPlugin

public setup(core: CoreSetup) {
this.logger.debug('reports-dashboards: Setup');

const config = core.http.getServerInfo();
const serverBasePath = core.http.basePath.serverBasePath;
const accessInfo: AccessInfoType = {
basePath: serverBasePath,
serverInfo: config,
};

const router = core.http.createRouter();
// Deprecated API. Switch to the new opensearch client as soon as https://github.com/elastic/kibana/issues/35508 done.
const opensearchReportsClient: ILegacyClusterClient = core.opensearch.legacy.createClient(
Expand All @@ -89,7 +80,7 @@ export class ReportsDashboardsPlugin
);

// Register server side APIs
registerRoutes(router, accessInfo);
registerRoutes(router);

// put logger into route handler context, so that we don't need to pass through parameters
core.http.registerRouteHandlerContext(
Expand Down
7 changes: 3 additions & 4 deletions dashboards-reports/server/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,10 @@ import registerReportDefinitionRoute from './reportDefinition';
import registerReportSourceRoute from './reportSource';
import registerMetricRoute from './metric';
import { IRouter } from '../../../../src/core/server';
import { AccessInfoType } from 'server';

export default function (router: IRouter, accessInfo: AccessInfoType) {
registerReportRoute(router, accessInfo);
registerReportDefinitionRoute(router, accessInfo);
export default function (router: IRouter) {
registerReportRoute(router);
registerReportDefinitionRoute(router);
registerReportSourceRoute(router);
registerMetricRoute(router);
}
24 changes: 9 additions & 15 deletions dashboards-reports/server/routes/lib/createReport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
REPORT_STATE,
DELIVERY_TYPE,
SECURITY_CONSTANTS,
LOCAL_HOST,
} from '../utils/constants';

import {
Expand All @@ -45,13 +46,11 @@ import { SetCookie, Headers } from 'puppeteer-core';
import { updateReportState } from './updateReportState';
import { saveReport } from './saveReport';
import { SemaphoreInterface } from 'async-mutex';
import { AccessInfoType } from 'server';

export const createReport = async (
request: OpenSearchDashboardsRequest,
context: RequestHandlerContext,
report: ReportSchemaType,
accessInfo: AccessInfoType,
savedReportId?: string
): Promise<CreateReportResultType> => {
const isScheduledTask = false;
Expand All @@ -66,10 +65,6 @@ export const createReport = async (
const opensearchClient = context.core.opensearch.legacy.client;
// @ts-ignore
const timezone = request.query.timezone;
const {
basePath,
serverInfo: { protocol, port, hostname },
} = accessInfo;

let createReportResult: CreateReportResultType;
let reportId;
Expand Down Expand Up @@ -99,10 +94,7 @@ export const createReport = async (
} else {
// report source can only be one of [saved search, visualization, dashboard, notebook]
// compose url
const relativeUrl = report.query_url.startsWith(basePath)
? report.query_url
: `${basePath}${report.query_url}`;
const completeQueryUrl = `${protocol}://${hostname}:${port}${relativeUrl}`;
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) {
Expand All @@ -114,19 +106,21 @@ export const createReport = async (
name: cookie[0],
value: cookie[1],
url: completeQueryUrl,
path: basePath,
};
}
});
}
// If header exists assuming that it needs forwarding
let additionalHeaders: Headers | undefined;
if (request.headers[SECURITY_CONSTANTS.PROXY_AUTH_USER_HEADER]) {
additionalHeaders = {}
additionalHeaders[SECURITY_CONSTANTS.PROXY_AUTH_USER_HEADER] = request.headers[SECURITY_CONSTANTS.PROXY_AUTH_USER_HEADER];
additionalHeaders[SECURITY_CONSTANTS.PROXY_AUTH_IP_HEADER] = request.headers[SECURITY_CONSTANTS.PROXY_AUTH_IP_HEADER];
additionalHeaders = {};
additionalHeaders[SECURITY_CONSTANTS.PROXY_AUTH_USER_HEADER] =
request.headers[SECURITY_CONSTANTS.PROXY_AUTH_USER_HEADER];
additionalHeaders[SECURITY_CONSTANTS.PROXY_AUTH_IP_HEADER] =
request.headers[SECURITY_CONSTANTS.PROXY_AUTH_IP_HEADER];
if (request.headers[SECURITY_CONSTANTS.PROXY_AUTH_ROLES_HEADER]) {
additionalHeaders[SECURITY_CONSTANTS.PROXY_AUTH_ROLES_HEADER] = request.headers[SECURITY_CONSTANTS.PROXY_AUTH_ROLES_HEADER]
additionalHeaders[SECURITY_CONSTANTS.PROXY_AUTH_ROLES_HEADER] =
request.headers[SECURITY_CONSTANTS.PROXY_AUTH_ROLES_HEADER];
}
}
const [value, release] = await semaphore.acquire();
Expand Down
25 changes: 7 additions & 18 deletions dashboards-reports/server/routes/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,8 @@ import {
} from './utils/converters/backendToUi';
import { addToMetric } from './utils/metricHelper';
import { validateReport } from '../../server/utils/validationHelper';
import { AccessInfoType } from 'server';

export default function (router: IRouter, accessInfo: AccessInfoType) {
const { basePath } = accessInfo;
export default function (router: IRouter) {
// generate report (with provided metadata)
router.post(
{
Expand Down Expand Up @@ -73,8 +71,7 @@ export default function (router: IRouter, accessInfo: AccessInfoType) {
request.headers.origin;
report = await validateReport(
context.core.opensearch.legacy.client,
report,
basePath
report
);
} catch (error) {
logger.error(`Failed input validation for create report ${error}`);
Expand All @@ -83,12 +80,7 @@ export default function (router: IRouter, accessInfo: AccessInfoType) {
}

try {
const reportData = await createReport(
request,
context,
report,
accessInfo
);
const reportData = await createReport(request, context, report);

// if not deliver to user himself , no need to send actual file data to client
const delivery = report.report_definition.delivery;
Expand Down Expand Up @@ -145,13 +137,12 @@ export default function (router: IRouter, accessInfo: AccessInfoType) {
}
);
// convert report to use UI model
const report = backendToUiReport(opensearchResp.reportInstance, basePath);
const report = backendToUiReport(opensearchResp.reportInstance);
// generate report
const reportData = await createReport(
request,
context,
report,
accessInfo,
savedReportId
);
addToMetric('report', 'download', 'count', report);
Expand Down Expand Up @@ -211,13 +202,12 @@ export default function (router: IRouter, accessInfo: AccessInfoType) {
);
const reportId = opensearchResp.reportInstance.id;
// convert report to use UI model
const report = backendToUiReport(opensearchResp.reportInstance, basePath);
const report = backendToUiReport(opensearchResp.reportInstance);
// generate report
const reportData = await createReport(
request,
context,
report,
accessInfo,
reportId
);
addToMetric('report', 'create_from_definition', 'count', report);
Expand Down Expand Up @@ -275,8 +265,7 @@ export default function (router: IRouter, accessInfo: AccessInfoType) {
);

const reportsList = backendToUiReportsList(
opensearchResp.reportInstanceList,
basePath
opensearchResp.reportInstanceList
);

return response.ok({
Expand Down Expand Up @@ -324,7 +313,7 @@ export default function (router: IRouter, accessInfo: AccessInfoType) {
}
);

const report = backendToUiReport(opensearchResp.reportInstance, basePath);
const report = backendToUiReport(opensearchResp.reportInstance);

return response.ok({
body: report,
Expand Down
16 changes: 5 additions & 11 deletions dashboards-reports/server/routes/reportDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,8 @@ import { updateReportDefinition } from './lib/updateReportDefinition';
import { DEFAULT_MAX_SIZE } from './utils/constants';
import { addToMetric } from './utils/metricHelper';
import { validateReportDefinition } from '../../server/utils/validationHelper';
import { AccessInfoType } from 'server';

export default function (router: IRouter, accessInfo: AccessInfoType) {
const { basePath, serverInfo } = accessInfo;
export default function (router: IRouter) {
// Create report Definition
router.post(
{
Expand All @@ -69,8 +67,7 @@ export default function (router: IRouter, accessInfo: AccessInfoType) {
request.headers.origin;
reportDefinition = await validateReportDefinition(
context.core.opensearch.legacy.client,
reportDefinition,
basePath
reportDefinition
);
} catch (error) {
logger.error(
Expand Down Expand Up @@ -128,8 +125,7 @@ export default function (router: IRouter, accessInfo: AccessInfoType) {
request.headers.origin;
reportDefinition = await validateReportDefinition(
context.core.opensearch.legacy.client,
reportDefinition,
basePath
reportDefinition
);
} catch (error) {
logger.error(
Expand Down Expand Up @@ -194,8 +190,7 @@ export default function (router: IRouter, accessInfo: AccessInfoType) {
}
);
const reportDefinitionsList = backendToUiReportDefinitionsList(
opensearchResp.reportDefinitionDetailsList,
basePath
opensearchResp.reportDefinitionDetailsList
);
return response.ok({
body: {
Expand Down Expand Up @@ -243,8 +238,7 @@ export default function (router: IRouter, accessInfo: AccessInfoType) {
);

const reportDefinition = backendToUiReportDefinition(
opensearchResp.reportDefinitionDetails,
basePath
opensearchResp.reportDefinitionDetails
);

return response.ok({
Expand Down
5 changes: 5 additions & 0 deletions dashboards-reports/server/routes/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ export enum SELECTOR {
// https://www.elastic.co/guide/en/elasticsearch/reference/6.8/search-request-from-size.html
export const DEFAULT_MAX_SIZE = 10000;

// Managed service uses 9200, with server.basePath = '/_plugin/kibana'
export const LOCAL_HOST = 'http://localhost:9200';
// Managed service specific server base path
export const BASE_PATH = '/_dashboards';

export const DEFAULT_REPORT_HEADER = '<h1>OpenSearch Dashboards Reports</h1>';

export const SECURITY_CONSTANTS = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
* permissions and limitations under the License.
*/

import { AccessInfoType } from 'server';
import {
BackendReportInstanceType,
BACKEND_DELIVERY_FORMAT,
Expand All @@ -33,6 +32,7 @@ import {
BACKEND_REPORT_STATE,
BACKEND_TRIGGER_TYPE,
} from '../../../../model/backendModel';
import { BASE_PATH } from '../../constants';
import { backendToUiReport } from '../backendToUi';

const input: BackendReportInstanceType = {
Expand Down Expand Up @@ -83,19 +83,8 @@ const input: BackendReportInstanceType = {
status: BACKEND_REPORT_STATE.success,
};

const testAccessInfo: AccessInfoType = {
basePath: '',
serverInfo: {
name: '',
hostname: 'localhost',
port: 5601,
protocol: 'http',
},
};

const output = {
query_url:
"/app/dashboards#/view/722b74f0-b882-11e8-a6d9-e546fe2bba5f?_g=(time:(from:'2020-11-11T00:32:00.000Z',to:'2020-11-11T01:02:00.000Z'))",
query_url: `${BASE_PATH}/app/dashboards#/view/722b74f0-b882-11e8-a6d9-e546fe2bba5f?_g=(time:(from:'2020-11-11T00:32:00.000Z',to:'2020-11-11T01:02:00.000Z'))`,
time_from: 1605054720000,
time_to: 1605056520000,
last_updated: 1605056644321,
Expand All @@ -107,7 +96,7 @@ const output = {
report_source: 'Dashboard',
description: 'some random',
core_params: {
base_url: '/app/dashboards#/view/722b74f0-b882-11e8-a6d9-e546fe2bba5f',
base_url: `${BASE_PATH}/app/dashboards#/view/722b74f0-b882-11e8-a6d9-e546fe2bba5f`,
report_format: 'pdf',
header: '<p>test header</p>',
footer: '<p>fake footer</p>',
Expand Down Expand Up @@ -141,7 +130,7 @@ const output = {

describe('test backend to ui model conversion', () => {
test('convert backend to ui report', async () => {
const res = backendToUiReport(input, testAccessInfo.basePath);
const res = backendToUiReport(input);
expect(res).toEqual(output);
}, 20000);
});
Loading

0 comments on commit e985041

Please sign in to comment.