-
Notifications
You must be signed in to change notification settings - Fork 255
Expand file tree
/
Copy pathserver.js
More file actions
441 lines (402 loc) · 15.4 KB
/
server.js
File metadata and controls
441 lines (402 loc) · 15.4 KB
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
const http = require('http');
const https = require('https');
const cluster = require('cluster');
const { series } = require('async');
const arsenal = require('arsenal');
const { RedisClient, StatsClient } = arsenal.metrics;
const monitoringClient = require('./utilities/monitoringHandler');
const logger = require('./utilities/logger');
const { internalHandlers } = require('./utilities/internalHandlers');
const { clientCheck, healthcheckHandler } = require('./utilities/healthcheckHandler');
const _config = require('./Config').config;
const { blacklistedPrefixes } = require('../constants');
const api = require('./api/api');
const dataWrapper = require('./data/wrapper');
const kms = require('./kms/wrapper');
const locationStorageCheck =
require('./api/apiUtils/object/locationStorageCheck');
const vault = require('./auth/vault');
const metadata = require('./metadata/wrapper');
const { initManagement } = require('./management');
const {
initManagementClient,
isManagementAgentUsed,
} = require('./management/agentClient');
const HttpAgent = require('agentkeepalive');
const QuotaService = require('./quotas/quotas');
const { parseLC, MultipleBackendGateway } = arsenal.storage.data;
const websiteEndpoints = _config.websiteEndpoints;
let client = dataWrapper.client;
const implName = dataWrapper.implName;
let allEndpoints;
function updateAllEndpoints() {
allEndpoints = Object.keys(_config.restEndpoints);
}
_config.on('rest-endpoints-update', updateAllEndpoints);
updateAllEndpoints();
_config.on('location-constraints-update', () => {
if (implName === 'multipleBackends') {
const clients = parseLC(_config, vault);
client = new MultipleBackendGateway(
clients, metadata, locationStorageCheck);
}
});
// redis client
let localCacheClient;
if (_config.localCache) {
localCacheClient = new RedisClient(_config.localCache, logger);
}
// stats client
const STATS_INTERVAL = 5; // 5 seconds
const STATS_EXPIRY = 30; // 30 seconds
const statsClient = new StatsClient(localCacheClient, STATS_INTERVAL,
STATS_EXPIRY);
const enableRemoteManagement = true;
class S3Server {
/**
* This represents our S3 connector.
* @constructor
* @param {Object} config - Configuration object
* @param {Worker} [worker=null] - Track the worker when using cluster
*/
constructor(config, worker) {
this.config = config;
this.worker = worker;
this.cluster = config.isCluster;
this.servers = [];
http.globalAgent = new HttpAgent({
keepAlive: true,
freeSocketTimeout: arsenal.constants.httpClientFreeSocketTimeout,
});
process.on('SIGINT', this.cleanUp.bind(this));
process.on('SIGHUP', this.cleanUp.bind(this));
process.on('SIGQUIT', this.cleanUp.bind(this));
process.on('SIGTERM', this.cleanUp.bind(this));
process.on('SIGPIPE', () => { });
// This will pick up exceptions up the stack
process.on('uncaughtException', err => {
// If just send the error object results in empty
// object on server log.
logger.fatal('caught error', {
error: err.message,
stack: err.stack,
workerId: this.worker ? this.worker.id : undefined,
workerPid: this.worker ? this.worker.process.pid : undefined,
});
this.caughtExceptionShutdown();
});
this.started = false;
}
/**
* Route requests on 'internal s3' port
* Same as routeRequest, but ignoring user's bucket policy. This should be used only for
* backbeat and other internal/system processes that must not be affected by user's bucket
* policy.
* Note that this is not a temporary measure: eventually this should be improved to better
* identify and isolate internal/system calls vs user calls, so that "system" bucket policies
* may be applied to system requests, and the existing "user" bucket policies apply only to
* user requests.
* @param {http.IncomingMessage} req - http request object
* @param {http.ServerResponse} res - http response object
* @returns {undefined}
*/
internalRouteRequest(req, res) {
req.bypassUserBucketPolicies = true; // eslint-disable-line no-param-reassign
return this.routeRequest(req, res);
}
/**
* Route requests on 's3' port
* @param {http.IncomingMessage} req - http request object
* @param {http.ServerResponse} res - http response object
* @returns {undefined}
*/
routeRequest(req, res) {
monitoringClient.httpActiveRequests.inc();
const requestStartTime = process.hrtime.bigint();
// disable nagle algorithm
req.socket.setNoDelay();
res.on('close', () => {
// this is tested by retrieveData
// eslint-disable-next-line no-param-reassign
res.isclosed = true;
});
const monitorEndOfRequest = () => {
const responseTimeInNs = Number(process.hrtime.bigint() - requestStartTime);
const labels = {
method: req.method,
code: res.statusCode,
};
if (req.apiMethod) {
labels.action = req.apiMethod;
}
monitoringClient.httpRequestsTotal.labels(labels).inc();
monitoringClient.httpRequestDurationSeconds
.labels(labels)
.observe(responseTimeInNs / 1e9);
monitoringClient.httpActiveRequests.dec();
};
res.on('close', monitorEndOfRequest);
// use proxied hostname if needed
if (req.headers['x-target-host']) {
// eslint-disable-next-line no-param-reassign
req.headers.host = req.headers['x-target-host'];
}
const params = {
api,
internalHandlers,
statsClient,
allEndpoints,
websiteEndpoints,
blacklistedPrefixes,
dataRetrievalParams: {
client,
implName,
config: this.config,
kms,
metadata,
locStorageCheckFn: locationStorageCheck,
vault,
},
};
arsenal.s3routes.routes(req, res, params, logger, this.config);
}
/**
* Route requests on 'admin' port
* @param {http.IncomingMessage} req - http request object
* @param {http.ServerResponse} res - http response object
* @returns {undefined}
*/
routeAdminRequest(req, res) {
// use proxied hostname if needed
if (req.headers['x-target-host']) {
// eslint-disable-next-line no-param-reassign
req.headers.host = req.headers['x-target-host'];
}
const clientInfo = {
clientIP: req.socket.remoteAddress,
clientPort: req.socket.remotePort,
httpMethod: req.method,
httpURL: req.url,
endpoint: req.endpoint,
};
let reqUids = req.headers['x-scal-request-uids'];
if (reqUids !== undefined && !/*isValidReqUids*/(reqUids.length < 128)) {
// simply ignore invalid id (any user can provide an
// invalid request ID through a crafted header)
reqUids = undefined;
}
const log = (reqUids !== undefined ?
logger.newRequestLoggerFromSerializedUids(reqUids) :
logger.newRequestLogger());
log.end().addDefaultFields(clientInfo);
log.debug('received admin request', clientInfo);
switch (req.url) {
case '/live':
healthcheckHandler(clientInfo.clientIP, req, res, log, statsClient, false);
break;
case '/ready':
healthcheckHandler(clientInfo.clientIP, req, res, log, statsClient, true);
break;
default:
monitoringClient.monitoringHandler(clientInfo.clientIP, req, res, log);
break;
}
}
/**
* This starts the http server.
* @param {http.RequestListener} listener - Callback to handle requests
* @param {number} port - Port to listen on
* @param {string | undefined} ipAddress - IpAddress to listen on
* @returns {undefined}
*/
_startServer(listener, port, ipAddress) {
// Todo: http.globalAgent.maxSockets, http.globalAgent.maxFreeSockets
let server;
if (this.config.https) {
server = https.createServer({
cert: this.config.https.cert,
key: this.config.https.key,
ca: this.config.https.ca,
ciphers: arsenal.https.ciphers.ciphers,
dhparam: arsenal.https.dhparam.dhparam,
rejectUnauthorized: true,
});
logger.info('Https server configuration', {
https: true,
});
} else {
server = http.createServer();
logger.info('Http server configuration', {
https: false,
});
}
// Starting NodeJS v18, the default timeout, when `undefined`, is
// 5 minutes. We must set the value to zero to allow for long
// upload durations.
server.requestTimeout = 0; // disabling request timeout
server.on('connection', socket => {
socket.on('error', err => logger.info('request rejected',
{ error: err }));
});
// https://nodejs.org/dist/latest-v6.x/
// docs/api/http.html#http_event_checkexpectation
server.on('checkExpectation', listener);
server.on('request', listener);
server.on('checkContinue', listener);
server.on('listening', () => {
const addr = server.address() || {
address: ipAddress || '[::]',
port,
};
const { address } = addr;
logger.info('server started', {
address, port,
pid: process.pid, serverIP: address, serverPort: port
});
});
if (ipAddress !== undefined) {
server.listen(port, ipAddress);
} else {
server.listen(port);
}
this.servers.push(server);
}
/*
* This exits the running process properly.
*/
cleanUp() {
logger.info('server shutting down');
Promise.all(this.servers.map(server =>
new Promise(resolve => server.close(resolve))
)).then(() => process.exit(0));
}
caughtExceptionShutdown() {
if (!this.cluster) {
process.exit(1);
}
logger.error('shutdown of worker due to exception', {
workerId: this.worker ? this.worker.id : undefined,
workerPid: this.worker ? this.worker.process.pid : undefined,
});
// Will close all servers, cause disconnect event on primary and kill
// worker process with 'SIGTERM'.
if (this.worker) {
this.worker.kill();
}
}
startServer(listenOn, port, routeRequest) {
if (listenOn.length > 0) {
listenOn.forEach(item => {
this._startServer(routeRequest.bind(this), item.port, item.ip);
});
} else if (port) {
this._startServer(routeRequest.bind(this), port);
}
}
initiateStartup(log) {
series([
next => metadata.setup(next),
next => clientCheck(true, log, next),
], (err, results) => {
if (err) {
log.warn('initial health check failed, delaying startup', {
error: err,
healthStatus: results,
});
setTimeout(() => this.initiateStartup(log), 2000);
return;
}
log.debug('initial health check succeeded');
if (this.started) {
return;
}
// Start API server(s)
this.startServer(this.config.listenOn, this.config.port, this.routeRequest);
// Start internal API server(s)
this.startServer(this.config.internalListenOn, this.config.internalPort, this.internalRouteRequest);
// Start metrics server(s) only if not cluster mode worker
if (!this.cluster && !this.worker) {
this.startServer(this.config.metricsListenOn, this.config.metricsPort, this.routeAdminRequest);
}
// Start quota service health checks
if (QuotaService.enabled) {
QuotaService?.setup(log);
}
// TODO this should wait for metadata healthcheck to be ok
// TODO only do this in cluster master
if (enableRemoteManagement) {
if (!isManagementAgentUsed()) {
setTimeout(() => {
initManagement(logger.newRequestLogger());
}, 5000);
} else {
initManagementClient();
}
}
this.started = true;
});
}
}
function main() {
const workers = _config.clusters;
if (!_config.isCluster) {
process.env.REPORT_TOKEN = _config.reportToken;
const server = new S3Server(_config);
server.initiateStartup(logger.newRequestLogger());
}
if (_config.isCluster && cluster.isPrimary) {
for (let n = 0; n < workers; n++) {
const worker = cluster.fork();
logger.info('new worker forked', {
workerId: worker.id,
workerPid: worker.process.pid,
});
}
setInterval(() => {
const len = Object.keys(cluster.workers).length;
if (len < workers) {
for (let i = len; i < workers; i++) {
const newWorker = cluster.fork();
logger.info('new worker forked', {
workerId: newWorker.id,
workerPid: newWorker.process.pid,
});
}
}
}, 1000);
cluster.on('disconnect', worker => {
logger.error('worker disconnected. making sure exits', {
workerId: worker.id,
workerPid: worker.process.pid,
});
setTimeout(() => {
if (!worker.isDead() && !worker.exitedAfterDisconnect) {
logger.error('worker not exiting. killing it', {
workerId: worker.id,
workerPid: worker.pid,
});
worker.process.kill('SIGKILL');
}
}, 2000);
});
cluster.on('exit', worker => {
logger.error('worker exited.', {
workerId: worker.id,
workerPid: worker.process.pid,
});
});
const metricServer = new S3Server(_config);
metricServer.startServer(_config.metricsListenOn,
_config.metricsPort, metricServer.routeAdminRequest);
}
if (_config.isCluster && cluster.isWorker) {
const server = new S3Server(_config, cluster.worker);
server.initiateStartup(logger.newRequestLogger());
}
// Avoid default metrics on cluster primary as it only aggregate workers
if (!_config.isCluster || cluster.isWorker) {
monitoringClient.collectDefaultMetrics({ timeout: 5000 });
}
}
module.exports = main;
module.exports.S3Server = S3Server;