forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest_handler.ts
250 lines (209 loc) · 7.58 KB
/
request_handler.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import Boom from '@hapi/boom';
import moment from 'moment';
import { schema, TypeOf } from '@kbn/config-schema';
import type { KibanaRequest, KibanaResponseFactory, Logger } from '@kbn/core/server';
import { i18n } from '@kbn/i18n';
import type { BaseParams } from '@kbn/reporting-common/types';
import { cryptoFactory } from '@kbn/reporting-server';
import rison from '@kbn/rison';
import { Counters, getCounters } from '..';
import type { ReportingCore } from '../../..';
import { PUBLIC_ROUTES } from '../../../../common/constants';
import { checkParamsVersion } from '../../../lib';
import { Report } from '../../../lib/store';
import type {
ReportingJobResponse,
ReportingRequestHandlerContext,
ReportingUser,
} from '../../../types';
export const handleUnavailable = (res: KibanaResponseFactory) => {
return res.custom({ statusCode: 503, body: 'Not Available' });
};
const validation = {
params: schema.object({ exportType: schema.string({ minLength: 2 }) }),
body: schema.nullable(schema.object({ jobParams: schema.maybe(schema.string()) })),
query: schema.nullable(schema.object({ jobParams: schema.string({ defaultValue: '' }) })),
};
/**
* Handles the common parts of requests to generate a report
*/
export class RequestHandler {
constructor(
private reporting: ReportingCore,
private user: ReportingUser,
private context: ReportingRequestHandlerContext,
private path: string,
private req: KibanaRequest<
TypeOf<typeof validation['params']>,
TypeOf<typeof validation['query']>,
TypeOf<typeof validation['body']>
>,
private res: KibanaResponseFactory,
private logger: Logger
) {}
private async encryptHeaders() {
const { encryptionKey } = this.reporting.getConfig();
const crypto = cryptoFactory(encryptionKey);
return await crypto.encrypt(this.req.headers);
}
public async enqueueJob(exportTypeId: string, jobParams: BaseParams) {
const { reporting, logger, context, req, user } = this;
const exportType = reporting.getExportTypesRegistry().getById(exportTypeId);
if (exportType == null) {
throw new Error(`Export type ${exportTypeId} does not exist in the registry!`);
}
const store = await reporting.getStore();
if (!exportType.createJob) {
throw new Error(`Export type ${exportTypeId} is not a valid instance!`);
}
// 1. Ensure the incoming params have a version field (should be set by the UI)
jobParams.version = checkParamsVersion(jobParams, logger);
// 2. Encrypt request headers to store for the running report job to authenticate itself with Kibana
const headers = await this.encryptHeaders();
// 3. Create a payload object by calling exportType.createJob(), and adding some automatic parameters
const job = await exportType.createJob(jobParams, context, req);
const payload = {
...job,
headers,
title: job.title,
objectType: jobParams.objectType,
browserTimezone: jobParams.browserTimezone,
version: jobParams.version,
spaceId: reporting.getSpaceId(req, logger),
};
// 4. Add the report to ReportingStore to show as pending
const report = await store.addReport(
new Report({
jobtype: exportType.jobType,
created_by: user ? user.username : false,
payload,
migration_version: jobParams.version,
meta: {
// telemetry fields
objectType: jobParams.objectType,
layout: jobParams.layout?.id,
isDeprecated: job.isDeprecated,
},
})
);
logger.debug(`Successfully stored pending job: ${report._index}/${report._id}`);
// 5. Schedule the report with Task Manager
const task = await reporting.scheduleTask(report.toReportTaskJSON());
logger.info(
`Scheduled ${exportType.name} reporting task. Task ID: task:${task.id}. Report ID: ${report._id}`
);
// 6. Log the action with event log
reporting.getEventLogger(report, task).logScheduleTask();
return report;
}
public getJobParams(): BaseParams {
let jobParamsRison: null | string = null;
const req = this.req;
const res = this.res;
if (req.body) {
const { jobParams: jobParamsPayload } = req.body;
jobParamsRison = jobParamsPayload ? jobParamsPayload : null;
} else if (req.query?.jobParams) {
const { jobParams: queryJobParams } = req.query;
if (queryJobParams) {
jobParamsRison = queryJobParams;
} else {
jobParamsRison = null;
}
}
if (!jobParamsRison) {
throw res.customError({
statusCode: 400,
body: 'A jobParams RISON string is required in the querystring or POST body',
});
}
let jobParams;
try {
jobParams = rison.decode(jobParamsRison) as BaseParams | null;
if (!jobParams) {
throw res.customError({
statusCode: 400,
body: 'Missing jobParams!',
});
}
} catch (err) {
throw res.customError({
statusCode: 400,
body: `invalid rison: ${jobParamsRison}`,
});
}
return jobParams;
}
public static getValidation() {
return validation;
}
public async handleGenerateRequest(exportTypeId: string, jobParams: BaseParams) {
const req = this.req;
const reporting = this.reporting;
const counters = getCounters(
req.route.method,
this.path.replace(/{exportType}/, exportTypeId),
reporting.getUsageCounter()
);
// ensure the async dependencies are loaded
if (!this.context.reporting) {
return handleUnavailable(this.res);
}
const licenseInfo = await this.reporting.getLicenseInfo();
const licenseResults = licenseInfo[exportTypeId];
if (!licenseResults) {
return this.res.badRequest({ body: `Invalid export-type of ${exportTypeId}` });
}
if (!licenseResults.enableLinks) {
return this.res.forbidden({ body: licenseResults.message });
}
if (jobParams.browserTimezone && !moment.tz.zone(jobParams.browserTimezone)) {
return this.res.badRequest({
body: `Invalid timezone "${jobParams.browserTimezone ?? ''}".`,
});
}
let report: Report | undefined;
try {
report = await this.enqueueJob(exportTypeId, jobParams);
const { basePath } = this.reporting.getServerInfo();
const publicDownloadPath = basePath + PUBLIC_ROUTES.JOBS.DOWNLOAD_PREFIX;
// return task manager's task information and the download URL
counters.usageCounter();
return this.res.ok<ReportingJobResponse>({
headers: { 'content-type': 'application/json' },
body: {
path: `${publicDownloadPath}/${report._id}`,
job: report.toApiJSON(),
},
});
} catch (err) {
return this.handleError(err, counters, report?.jobtype);
}
}
private handleError(err: Error | Boom.Boom, counters: Counters, jobtype?: string) {
this.logger.error(err);
if (err instanceof Boom.Boom) {
const statusCode = err.output.statusCode;
counters?.errorCounter(jobtype, statusCode);
return this.res.customError({
statusCode,
body: err.output.payload.message,
});
}
counters?.errorCounter(jobtype, 500);
return this.res.customError({
statusCode: 500,
body:
err?.message ||
i18n.translate('xpack.reporting.errorHandler.unknownError', {
defaultMessage: 'Unknown error',
}),
});
}
}