-
Notifications
You must be signed in to change notification settings - Fork 8.2k
/
http_server.ts
541 lines (483 loc) · 18.7 KB
/
http_server.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
/*
* 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 and the Server Side Public License, v 1; you may not use this file except
* in compliance with, at your election, the Elastic License 2.0 or the Server
* Side Public License, v 1.
*/
import { Server, Request } from '@hapi/hapi';
import HapiStaticFiles from '@hapi/inert';
import url from 'url';
import uuid from 'uuid';
import {
createServer,
getListenerOptions,
getServerOptions,
getRequestId,
} from '@kbn/server-http-tools';
import agent from 'elastic-apm-node';
import type { Duration } from 'moment';
import { Observable } from 'rxjs';
import { take } from 'rxjs/operators';
import { Logger, LoggerFactory } from '../logging';
import { HttpConfig } from './http_config';
import type { InternalExecutionContextSetup } from '../execution_context';
import { adoptToHapiAuthFormat, AuthenticationHandler } from './lifecycle/auth';
import { adoptToHapiOnPreAuth, OnPreAuthHandler } from './lifecycle/on_pre_auth';
import { adoptToHapiOnPostAuthFormat, OnPostAuthHandler } from './lifecycle/on_post_auth';
import { adoptToHapiOnRequest, OnPreRoutingHandler } from './lifecycle/on_pre_routing';
import { adoptToHapiOnPreResponseFormat, OnPreResponseHandler } from './lifecycle/on_pre_response';
import {
IRouter,
RouteConfigOptions,
KibanaRouteOptions,
KibanaRequestState,
isSafeMethod,
RouterRoute,
} from './router';
import {
SessionStorageCookieOptions,
createCookieSessionStorageFactory,
} from './cookie_session_storage';
import { AuthStateStorage } from './auth_state_storage';
import { AuthHeadersStorage, GetAuthHeaders, SetAuthHeaders } from './auth_headers_storage';
import { BasePath } from './base_path_service';
import { getEcsResponseLog } from './logging';
import { HttpServiceSetup, HttpServerInfo, HttpAuth } from './types';
/** @internal */
export interface HttpServerSetup {
server: Server;
/**
* Add all the routes registered with `router` to HTTP server request listeners.
* @param router {@link IRouter} - a router with registered route handlers.
*/
registerRouter: (router: IRouter) => void;
/**
* Add all the routes registered with `router` to HTTP server request listeners.
* Unlike `registerRouter`, this function allows routes to be registered even after the server
* has started listening for requests.
* @param router {@link IRouter} - a router with registered route handlers.
*/
registerRouterAfterListening: (router: IRouter) => void;
registerStaticDir: (path: string, dirPath: string) => void;
basePath: HttpServiceSetup['basePath'];
csp: HttpServiceSetup['csp'];
createCookieSessionStorageFactory: HttpServiceSetup['createCookieSessionStorageFactory'];
registerOnPreRouting: HttpServiceSetup['registerOnPreRouting'];
registerOnPreAuth: HttpServiceSetup['registerOnPreAuth'];
registerAuth: HttpServiceSetup['registerAuth'];
registerOnPostAuth: HttpServiceSetup['registerOnPostAuth'];
registerOnPreResponse: HttpServiceSetup['registerOnPreResponse'];
getAuthHeaders: GetAuthHeaders;
setAuthHeaders: SetAuthHeaders;
auth: HttpAuth;
getServerInfo: () => HttpServerInfo;
}
/** @internal */
export type LifecycleRegistrar = Pick<
HttpServerSetup,
| 'registerOnPreRouting'
| 'registerOnPreAuth'
| 'registerAuth'
| 'registerOnPostAuth'
| 'registerOnPreResponse'
>;
export class HttpServer {
private server?: Server;
private config?: HttpConfig;
private registeredRouters = new Set<IRouter>();
private authRegistered = false;
private cookieSessionStorageCreated = false;
private handleServerResponseEvent?: (req: Request) => void;
private stopping = false;
private stopped = false;
private readonly log: Logger;
private readonly authState: AuthStateStorage;
private readonly authRequestHeaders: AuthHeadersStorage;
private readonly authResponseHeaders: AuthHeadersStorage;
constructor(
private readonly logger: LoggerFactory,
private readonly name: string,
private readonly shutdownTimeout$: Observable<Duration>
) {
this.authState = new AuthStateStorage(() => this.authRegistered);
this.authRequestHeaders = new AuthHeadersStorage();
this.authResponseHeaders = new AuthHeadersStorage();
this.log = logger.get('http', 'server', name);
}
public isListening() {
return this.server !== undefined && this.server.listener.listening;
}
private registerRouter(router: IRouter) {
if (this.isListening()) {
throw new Error('Routers can be registered only when HTTP server is stopped.');
}
this.registeredRouters.add(router);
}
private registerRouterAfterListening(router: IRouter) {
if (this.isListening()) {
for (const route of router.getRoutes()) {
this.configureRoute(route);
}
} else {
// Not listening yet, add to set of registeredRouters so that it can be added after listening has started.
this.registeredRouters.add(router);
}
}
public async setup(
config: HttpConfig,
executionContext?: InternalExecutionContextSetup
): Promise<HttpServerSetup> {
const serverOptions = getServerOptions(config);
const listenerOptions = getListenerOptions(config);
this.server = createServer(serverOptions, listenerOptions);
await this.server.register([HapiStaticFiles]);
this.config = config;
// It's important to have setupRequestStateAssignment call the very first, otherwise context passing will be broken.
// That's the only reason why context initialization exists in this method.
this.setupRequestStateAssignment(config, executionContext);
const basePathService = new BasePath(config.basePath, config.publicBaseUrl);
this.setupBasePathRewrite(config, basePathService);
this.setupConditionalCompression(config);
this.setupResponseLogging();
this.setupGracefulShutdownHandlers();
return {
registerRouter: this.registerRouter.bind(this),
registerRouterAfterListening: this.registerRouterAfterListening.bind(this),
registerStaticDir: this.registerStaticDir.bind(this),
registerOnPreRouting: this.registerOnPreRouting.bind(this),
registerOnPreAuth: this.registerOnPreAuth.bind(this),
registerAuth: this.registerAuth.bind(this),
registerOnPostAuth: this.registerOnPostAuth.bind(this),
registerOnPreResponse: this.registerOnPreResponse.bind(this),
createCookieSessionStorageFactory: <T>(cookieOptions: SessionStorageCookieOptions<T>) =>
this.createCookieSessionStorageFactory(cookieOptions, config.basePath),
basePath: basePathService,
csp: config.csp,
auth: {
get: this.authState.get,
isAuthenticated: this.authState.isAuthenticated,
},
getAuthHeaders: this.authRequestHeaders.get,
setAuthHeaders: this.authRequestHeaders.set,
getServerInfo: () => ({
name: config.name,
hostname: config.host,
port: config.port,
protocol: this.server!.info.protocol,
}),
// Return server instance with the connection options so that we can properly
// bridge core and the "legacy" Kibana internally. Once this bridge isn't
// needed anymore we shouldn't return the instance from this method.
server: this.server,
};
}
public async start() {
if (this.server === undefined) {
throw new Error('Http server is not setup up yet');
}
if (this.stopping || this.stopped) {
this.log.warn(`start called after stop`);
return;
}
this.log.debug('starting http server');
for (const router of this.registeredRouters) {
for (const route of router.getRoutes()) {
this.configureRoute(route);
}
}
await this.server.start();
const serverPath =
this.config && this.config.rewriteBasePath && this.config.basePath !== undefined
? this.config.basePath
: '';
this.log.info(`http server running at ${this.server.info.uri}${serverPath}`);
}
public async stop() {
this.stopping = true;
if (this.server === undefined) {
this.stopping = false;
this.stopped = true;
return;
}
const hasStarted = this.server.info.started > 0;
if (hasStarted) {
this.log.debug('stopping http server');
const shutdownTimeout = await this.shutdownTimeout$.pipe(take(1)).toPromise();
await this.server.stop({ timeout: shutdownTimeout.asMilliseconds() });
this.log.debug(`http server stopped`);
// Removing the listener after stopping so we don't leave any pending requests unhandled
if (this.handleServerResponseEvent) {
this.server.events.removeListener('response', this.handleServerResponseEvent);
}
}
this.stopping = false;
this.stopped = true;
}
private getAuthOption(
authRequired: RouteConfigOptions<any>['authRequired'] = true
): undefined | false | { mode: 'required' | 'try' } {
if (this.authRegistered === false) return undefined;
if (authRequired === true) {
return { mode: 'required' };
}
if (authRequired === 'optional') {
// we want to use HAPI `try` mode and not `optional` to not throw unauthorized errors when the user
// has invalid or expired credentials
return { mode: 'try' };
}
if (authRequired === false) {
return false;
}
}
private setupGracefulShutdownHandlers() {
this.registerOnPreRouting((request, response, toolkit) => {
if (this.stopping || this.stopped) {
return response.customError({
statusCode: 503,
body: { message: 'Kibana is shutting down and not accepting new incoming requests' },
});
}
return toolkit.next();
});
}
private setupBasePathRewrite(config: HttpConfig, basePathService: BasePath) {
if (config.basePath === undefined || !config.rewriteBasePath) {
return;
}
this.registerOnPreRouting((request, response, toolkit) => {
const oldUrl = request.url.pathname + request.url.search;
const newURL = basePathService.remove(oldUrl);
const shouldRedirect = newURL !== oldUrl;
if (shouldRedirect) {
return toolkit.rewriteUrl(newURL);
}
return response.notFound();
});
}
private setupConditionalCompression(config: HttpConfig) {
if (this.server === undefined) {
throw new Error('Server is not created yet');
}
if (this.stopping || this.stopped) {
this.log.warn(`setupConditionalCompression called after stop`);
}
const { enabled, referrerWhitelist: list } = config.compression;
if (!enabled) {
this.log.debug('HTTP compression is disabled');
this.server.ext('onRequest', (request, h) => {
request.info.acceptEncoding = '';
return h.continue;
});
} else if (list) {
this.log.debug(`HTTP compression is only enabled for any referrer in the following: ${list}`);
this.server.ext('onRequest', (request, h) => {
const { referrer } = request.info;
if (referrer !== '') {
const { hostname } = url.parse(referrer);
if (!hostname || !list.includes(hostname)) {
request.info.acceptEncoding = '';
}
}
return h.continue;
});
}
}
private setupResponseLogging() {
if (this.server === undefined) {
throw new Error('Server is not created yet');
}
if (this.stopping || this.stopped) {
this.log.warn(`setupResponseLogging called after stop`);
}
const log = this.logger.get('http', 'server', 'response');
this.handleServerResponseEvent = (request) => {
const { message, meta } = getEcsResponseLog(request, this.log);
log.debug(message!, meta);
};
this.server.events.on('response', this.handleServerResponseEvent);
}
private setupRequestStateAssignment(
config: HttpConfig,
executionContext?: InternalExecutionContextSetup
) {
this.server!.ext('onRequest', (request, responseToolkit) => {
const requestId = getRequestId(request, config.requestId);
const parentContext = executionContext?.getParentContextFrom(request.headers);
if (parentContext) executionContext?.set(parentContext);
executionContext?.setRequestId(requestId);
request.app = {
...(request.app ?? {}),
requestId,
requestUuid: uuid.v4(),
// Kibana stores trace.id until https://github.com/elastic/apm-agent-nodejs/issues/2353 is resolved
// The current implementation of the APM agent ends a request transaction before "response" log is emitted.
traceId: agent.currentTraceIds['trace.id'],
} as KibanaRequestState;
return responseToolkit.continue;
});
}
private registerOnPreAuth(fn: OnPreAuthHandler) {
if (this.server === undefined) {
throw new Error('Server is not created yet');
}
if (this.stopping || this.stopped) {
this.log.warn(`registerOnPreAuth called after stop`);
}
this.server.ext('onPreAuth', adoptToHapiOnPreAuth(fn, this.log));
}
private registerOnPostAuth(fn: OnPostAuthHandler) {
if (this.server === undefined) {
throw new Error('Server is not created yet');
}
if (this.stopping || this.stopped) {
this.log.warn(`registerOnPostAuth called after stop`);
}
this.server.ext('onPostAuth', adoptToHapiOnPostAuthFormat(fn, this.log));
}
private registerOnPreRouting(fn: OnPreRoutingHandler) {
if (this.server === undefined) {
throw new Error('Server is not created yet');
}
if (this.stopping || this.stopped) {
this.log.warn(`registerOnPreRouting called after stop`);
}
this.server.ext('onRequest', adoptToHapiOnRequest(fn, this.log));
}
private registerOnPreResponse(fn: OnPreResponseHandler) {
if (this.server === undefined) {
throw new Error('Server is not created yet');
}
if (this.stopping || this.stopped) {
this.log.warn(`registerOnPreResponse called after stop`);
}
this.server.ext('onPreResponse', adoptToHapiOnPreResponseFormat(fn, this.log));
}
private async createCookieSessionStorageFactory<T>(
cookieOptions: SessionStorageCookieOptions<T>,
basePath?: string
) {
if (this.server === undefined) {
throw new Error('Server is not created yet');
}
if (this.stopping || this.stopped) {
this.log.warn(`createCookieSessionStorageFactory called after stop`);
}
if (this.cookieSessionStorageCreated) {
throw new Error('A cookieSessionStorageFactory was already created');
}
this.cookieSessionStorageCreated = true;
const sessionStorageFactory = await createCookieSessionStorageFactory<T>(
this.logger.get('http', 'server', this.name, 'cookie-session-storage'),
this.server,
cookieOptions,
basePath
);
return sessionStorageFactory;
}
private registerAuth<T>(fn: AuthenticationHandler) {
if (this.server === undefined) {
throw new Error('Server is not created yet');
}
if (this.stopping || this.stopped) {
this.log.warn(`registerAuth called after stop`);
}
if (this.authRegistered) {
throw new Error('Auth interceptor was already registered');
}
this.authRegistered = true;
this.server.auth.scheme('login', () => ({
authenticate: adoptToHapiAuthFormat(
fn,
this.log,
(req, { state, requestHeaders, responseHeaders }) => {
this.authState.set(req, state);
if (responseHeaders) {
this.authResponseHeaders.set(req, responseHeaders);
}
if (requestHeaders) {
this.authRequestHeaders.set(req, requestHeaders);
// we mutate headers only for the backward compatibility with the legacy platform.
// where some plugin read directly from headers to identify whether a user is authenticated.
Object.assign(req.headers, requestHeaders);
}
}
),
}));
this.server.auth.strategy('session', 'login');
// The default means that the `session` strategy that is based on `login` schema defined above will be
// automatically assigned to all routes that don't contain an auth config.
// should be applied for all routes if they don't specify auth strategy in route declaration
// https://github.com/hapijs/hapi/blob/master/API.md#-serverauthdefaultoptions
this.server.auth.default('session');
this.registerOnPreResponse((request, preResponseInfo, t) => {
const authResponseHeaders = this.authResponseHeaders.get(request);
return t.next({ headers: authResponseHeaders });
});
}
private registerStaticDir(path: string, dirPath: string) {
if (this.server === undefined) {
throw new Error('Http server is not setup up yet');
}
if (this.stopping || this.stopped) {
this.log.warn(`registerStaticDir called after stop`);
}
this.server.route({
path,
method: 'GET',
handler: {
directory: {
path: dirPath,
listing: false,
lookupCompressed: true,
},
},
options: {
auth: false,
cache: {
privacy: 'public',
otherwise: 'must-revalidate',
},
},
});
}
private configureRoute(route: RouterRoute) {
this.log.debug(`registering route handler for [${route.path}]`);
// Hapi does not allow payload validation to be specified for 'head' or 'get' requests
const validate = isSafeMethod(route.method) ? undefined : { payload: true };
const { authRequired, tags, body = {}, timeout } = route.options;
const { accepts: allow, maxBytes, output, parse } = body;
const kibanaRouteOptions: KibanaRouteOptions = {
xsrfRequired: route.options.xsrfRequired ?? !isSafeMethod(route.method),
};
this.server!.route({
handler: route.handler,
method: route.method,
path: route.path,
options: {
auth: this.getAuthOption(authRequired),
app: kibanaRouteOptions,
tags: tags ? Array.from(tags) : undefined,
// TODO: This 'validate' section can be removed once the legacy platform is completely removed.
// We are telling Hapi that NP routes can accept any payload, so that it can bypass the default
// validation applied in ./http_tools#getServerOptions
// (All NP routes are already required to specify their own validation in order to access the payload)
validate,
// @ts-expect-error Types are outdated and doesn't allow `payload.multipart` to be `true`
payload: [allow, maxBytes, output, parse, timeout?.payload].some((x) => x !== undefined)
? {
allow,
maxBytes,
output,
parse,
timeout: timeout?.payload,
multipart: true,
}
: undefined,
timeout: {
socket: timeout?.idleSocket ?? this.config!.socketTimeout,
},
},
});
}
}