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

Support customized server config #313

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
28 changes: 27 additions & 1 deletion kibana-reports/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,34 @@
* permissions and limitations under the License.
*/

import { PluginInitializerContext } from '../../../src/core/server';
import { OpendistroKibanaReportsPlugin } from './plugin';
import { schema, TypeOf } from '@kbn/config-schema';
import {
PluginInitializerContext,
PluginConfigDescriptor,
HttpServerInfo,
} from '../../../src/core/server';

export const configSchema = schema.object({
access: schema.object({
port: schema.number({ defaultValue: 5601 }),
basePath: schema.string({ defaultValue: '' }),
}),
});

export type KibanaReportsPluginConfigType = TypeOf<typeof configSchema>;

export const config: PluginConfigDescriptor<KibanaReportsPluginConfigType> = {
exposeToBrowser: {
access: true,
},
schema: configSchema,
Copy link
Contributor

Choose a reason for hiding this comment

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

what does this file do besides defining KibanaReportsPluginConfigType? it seems it's adding port and basepath config to the kibana.yml file? shouldn't be necessary since we are not reading from yml

Copy link
Member Author

Choose a reason for hiding this comment

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

This is something I forget to remove when I tried the plugin config approach. Will remove

};

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

// This exports static code and TypeScript types,
// as well as, Kibana Platform `plugin()` initializer.
Expand Down
13 changes: 10 additions & 3 deletions kibana-reports/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import {
import registerRoutes from './routes';
import { pollAndExecuteJob } from './executor/executor';
import { POLL_INTERVAL } from './utils/constants';
import { AccessInfoType, KibanaReportsPluginConfigType } from 'server';

export interface ReportsPluginRequestContext {
logger: Logger;
Expand Down Expand Up @@ -63,6 +64,14 @@ export class OpendistroKibanaReportsPlugin

public setup(core: CoreSetup) {
this.logger.debug('opendistro_kibana_reports: 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 elasticsearch client as soon as https://github.com/elastic/kibana/issues/35508 done.
const esReportsClient: ILegacyClusterClient = core.elasticsearch.legacy.createClient(
Expand All @@ -78,9 +87,8 @@ export class OpendistroKibanaReportsPlugin
plugins: [notificationPlugin],
}
);

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

// put logger into route handler context, so that we don't need to pass through parameters
core.http.registerRouteHandlerContext(
Expand All @@ -101,7 +109,6 @@ export class OpendistroKibanaReportsPlugin

public start(core: CoreStart) {
this.logger.debug('opendistro_kibana_reports: Started');

const esReportsClient: ILegacyClusterClient = core.elasticsearch.legacy.createClient(
'es_reports',
{
Expand Down
9 changes: 5 additions & 4 deletions kibana-reports/server/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ import registerReportRoute from './report';
import registerReportDefinitionRoute from './reportDefinition';
import registerReportSourceRoute from './reportSource';
import registerMetricRoute from './metric';
import { IRouter } from '../../../../src/core/server';
import { HttpServerInfo, IRouter } from '../../../../src/core/server';
import { AccessInfoType, KibanaReportsPluginConfigType } from 'server';

export default function (router: IRouter) {
registerReportRoute(router);
registerReportDefinitionRoute(router);
export default function (router: IRouter, accessInfo: AccessInfoType) {
registerReportRoute(router, accessInfo);
registerReportDefinitionRoute(router, accessInfo);
registerReportSourceRoute(router);
registerMetricRoute(router);
}
12 changes: 11 additions & 1 deletion kibana-reports/server/routes/lib/createReport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ import { deliverReport } from './deliverReport';
import { updateReportState } from './updateReportState';
import { saveReport } from './saveReport';
import { SemaphoreInterface } from 'async-mutex';
import { AccessInfoType } from 'server';

export const createReport = async (
request: KibanaRequest,
context: RequestHandlerContext,
report: ReportSchemaType,
accessInfo: AccessInfoType,
savedReportId?: string
): Promise<CreateReportResultType> => {
const isScheduledTask = false;
Expand All @@ -59,6 +61,10 @@ export const createReport = async (
const esClient = context.core.elasticsearch.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 @@ -89,7 +95,10 @@ export const createReport = async (
} else {
// report source can only be one of [saved search, visualization, dashboard]
// compose url
const completeQueryUrl = `${LOCAL_HOST}${report.query_url}`;
const relativeUrl = report.query_url.startsWith(basePath)
? report.query_url
: `${basePath}${report.query_url}`;
const completeQueryUrl = `${protocol}://${hostname}:${port}${relativeUrl}`;
// Check if security is enabled. TODO: is there a better way to check?
let cookieObject: SetCookie | undefined;
if (request.headers.cookie) {
Expand All @@ -101,6 +110,7 @@ export const createReport = async (
name: cookie[0],
value: cookie[1],
url: completeQueryUrl,
path: basePath,
};
}
});
Expand Down
27 changes: 20 additions & 7 deletions kibana-reports/server/routes/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ 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) {
export default function (router: IRouter, accessInfo: AccessInfoType) {
const { basePath } = accessInfo;
// generate report (with provided metadata)
router.post(
{
Expand Down Expand Up @@ -60,7 +62,8 @@ export default function (router: IRouter) {
request.headers.origin;
report = await validateReport(
context.core.elasticsearch.legacy.client,
report
report,
basePath
);
} catch (error) {
logger.error(`Failed input validation for create report ${error}`);
Expand All @@ -69,7 +72,12 @@ export default function (router: IRouter) {
}

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

// 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 @@ -133,12 +141,13 @@ export default function (router: IRouter) {
}
);
// convert report to use UI model
report = backendToUiReport(esResp.reportInstance);
const report = backendToUiReport(esResp.reportInstance, basePath);
// generate report
const reportData = await createReport(
request,
context,
report,
accessInfo,
savedReportId
);
addToMetric('report', 'download', 'count', report);
Expand Down Expand Up @@ -198,12 +207,13 @@ export default function (router: IRouter) {
);
const reportId = esResp.reportInstance.id;
// convert report to use UI model
report = backendToUiReport(esResp.reportInstance);
const report = backendToUiReport(esResp.reportInstance, basePath);
// generate report
const reportData = await createReport(
request,
context,
report,
accessInfo,
reportId
);
addToMetric('report', 'create_from_definition', 'count', report);
Expand Down Expand Up @@ -260,7 +270,10 @@ export default function (router: IRouter) {
}
);

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

return response.ok({
body: {
Expand Down Expand Up @@ -307,7 +320,7 @@ export default function (router: IRouter) {
}
);

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

return response.ok({
body: report,
Expand Down
16 changes: 11 additions & 5 deletions kibana-reports/server/routes/reportDefinition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ 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) {
export default function (router: IRouter, accessInfo: AccessInfoType) {
const { basePath, serverInfo } = accessInfo;
// Create report Definition
router.post(
{
Expand All @@ -56,7 +58,8 @@ export default function (router: IRouter) {
request.headers.origin;
reportDefinition = await validateReportDefinition(
context.core.elasticsearch.legacy.client,
reportDefinition
reportDefinition,
basePath
);
} catch (error) {
logger.error(
Expand Down Expand Up @@ -114,7 +117,8 @@ export default function (router: IRouter) {
request.headers.origin;
reportDefinition = await validateReportDefinition(
context.core.elasticsearch.legacy.client,
reportDefinition
reportDefinition,
basePath
);
} catch (error) {
logger.error(
Expand Down Expand Up @@ -182,7 +186,8 @@ export default function (router: IRouter) {
);

const reportDefinitionsList = backendToUiReportDefinitionsList(
esResp.reportDefinitionDetailsList
esResp.reportDefinitionDetailsList,
basePath
);

return response.ok({
Expand Down Expand Up @@ -231,7 +236,8 @@ export default function (router: IRouter) {
);

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

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

export const LOCAL_HOST = 'http://localhost:5601';

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

export const SECURITY_AUTH_COOKIE_NAME = 'security_authentication';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* permissions and limitations under the License.
*/

import { KibanaReportsPluginConfigType } from 'server';
import {
BackendReportInstanceType,
BACKEND_DELIVERY_FORMAT,
Expand Down Expand Up @@ -73,6 +74,13 @@ const input: BackendReportInstanceType = {
status: BACKEND_REPORT_STATE.success,
};

const testConfig: KibanaReportsPluginConfigType = {
access: {
port: 5601,
basePath: '',
},
};

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'))",
Expand Down Expand Up @@ -125,7 +133,7 @@ const output = {

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