forked from Juniper/contrail-web-core
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebServerStart.js
653 lines (596 loc) · 20.7 KB
/
webServerStart.js
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
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
/*
* Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
*/
var assert = require('assert');
var clusterUtils = require('./src/serverroot/utils/cluster.utils');
var args = process.argv.slice(2);
var argsCnt = args.length;
var configFile = null;
for (var i = 0; i < argsCnt; i++) {
if (('--c' == args[i]) || ('--conf_file' == args[i])) {
if (null == args[i + 1]) {
console.error('Config file not provided');
assert(0);
} else {
configFile = args[i + 1];
try {
var tmpConfig = require(configFile);
if ((null == tmpConfig) || (typeof tmpConfig !== 'object')) {
console.error('Config file ' + configFile + ' is not valid');
assert(0);
}
break;
} catch(e) {
console.error('Config file ' + configFile + ' not found');
assert(0);
}
}
}
}
/* Set corePath before loading any other module */
var corePath = process.cwd();
var config =
require('./src/serverroot/common/config.utils').compareAndMergeDefaultConfig(configFile);
exports.corePath = corePath;
exports.config = config;
var redisUtils = require('./src/serverroot/utils/redis.utils');
var global = require('./src/serverroot/common/global');
var server_port = (config.redis_server_port) ?
config.redis_server_port : global.DFLT_REDIS_SERVER_PORT;
var server_ip = (config.redis_server_ip) ?
config.redis_server_ip : global.DFLT_REDIS_SERVER_IP;
var express = require('express')
, path = require('path')
, fs = require("fs")
, http = require('http')
, https = require("https")
, underscore = require('underscore')
, cluster = require('cluster')
, axon = require('axon')
, producerSock = axon.socket('push')
, global = require('./src/serverroot/common/global')
, redis = require("redis")
, eventEmitter = require('events').EventEmitter
, async = require('async')
, os = require('os')
, commonUtils = require('./src/serverroot/utils/common.utils')
, discClient = require('./src/serverroot/common/discoveryclient.api')
, assert = require('assert')
, jsonPath = require('JSONPath').eval
, helmet = require('helmet')
, logutils = require('./src/serverroot/utils/log.utils')
;
var pkgList = commonUtils.mergeAllPackageList(global.service.MAINSEREVR);
assert(pkgList);
var nodeWorkerCount = config.node_worker_count;
var server_port = (config.redis_server_port) ?
config.redis_server_port : global.DFLT_REDIS_SERVER_PORT;
var server_ip = (config.redis_server_ip) ?
config.redis_server_ip : global.DFLT_REDIS_SERVER_IP;
var RedisStore = require('connect-redis')(express);
var store;
var myIdentity = global.service.MAINSEREVR;
var discServEnable = ((null != config.discoveryService) &&
(null != config.discoveryService.enable)) ?
config.discoveryService.enable : true;
var sessEvent = new eventEmitter();
var csrfInvalidEvent = new eventEmitter();
/* Recommended Cipheres */
var defCiphers =
'ECDHE-RSA-AES256-SHA384:AES256-SHA256:RC4-SHA:RC4:HIGH:!MD5:!aNULL:!EDH:!AESGCM';
var serCiphers = ((null != config.server_options) &&
(null != config.server_options.ciphers)) ?
config.server_options.ciphers : defCiphers;
var keyFile = './keys/cs-key.pem';
var certFile = './keys/cs-cert.pem';
if (config.server_options) {
keyFile = config.server_options.key_file;
if (null != keyFile) {
keyFile = path.normalize(keyFile);
if (false == fs.existsSync(keyFile)) {
keyFile = './keys/cs-key.pem';
}
} else {
keyFile = './keys/cs-key.pem';
}
certFile = config.server_options.cert_file;
if (null != certFile) {
certFile = path.normalize(certFile);
if (false == fs.existsSync(certFile)) {
certFile = './keys/cs-cert.pem';
}
} else {
certFile = './keys/cs-cert.pem';
}
}
var options = {
key:fs.readFileSync(keyFile),
cert:fs.readFileSync(certFile),
/* From https://github.com/nodejs/node-v0.x-archive/issues/2727
https://github.com/nodejs/node-v0.x-archive/pull/2732/files
*/
ciphers: serCiphers,
honorCipherOrder: true
};
var insecureAccessFlag = false;
if (config.insecure_access && (config.insecure_access == true)) {
insecureAccessFlag = true;
}
var httpsApp = express(),
httpApp = express(),
httpPort = config.http_port,
httpsPort = config.https_port,
redisPort = (config.redis_server_port) ?
config.redis_server_port : global.DFLT_REDIS_SERVER_PORT,
redisIP = (config.redis_server_ip) ?
config.redis_server_ip : global.DFLT_REDIS_SERVER_IP;
function initializeAppConfig (appObj)
{
var app = appObj.app;
var port = appObj.port;
var secretKey =
'enterasupbK3xg8qescJK.dUbdgfVq0D70UaLTMGTzO4yx5vVJral2zIhVersecretkey';
if ((null != config.session) && (null != config.session.secret_key)) {
secretKey = config.session.secret_key;
}
app.set('port', process.env.PORT || port);
app.use(express.cookieParser());
store = new RedisStore({host:redisIP, port:redisPort,
db:global.WEBUI_SESSION_REDIS_DB,
prefix:global.STR_REDIS_STORE_SESSION_ID_PREFIX,
eventEmitter:sessEvent});
// Implement X-XSS-Protection
app.use(helmet.xssFilter());
// Implement X-Frame: Deny
app.use(helmet.xframe('deny'));
// Implement Strict-Transport-Security
var maxAgeTime =
((null != config.session) && (null != config.session.timeout)) ?
config.session.timeout : global.MAX_AGE_SESSION_ID;
var compressOptions = {
filter: function(req, res) {
//To enable gzip compression for xml,tmpl,...files
return /json|text|xml|javascript|tmpl/.test(res.getHeader('Content-Type'))
}
};
app.use(express.compress(compressOptions));
express.static.mime.define({'text/tmpl': ['tmpl']});
registerStaticFiles(app);
app.use(helmet.hsts({
maxAge: maxAgeTime,
includeSubdomains: true
}));
var cookieObj = {maxAge: maxAgeTime, httpOnly: true};
if (false == insecureAccessFlag) {
cookieObj['secure'] = true;
}
app.use(express.session({ store:store,
secret: secretKey,
cookie: cookieObj
}));
app.use(express.methodOverride());
app.use(express.bodyParser());
app.use(app.router);
// Catch-all error handler
app.use(function (err, req, res, next) {
logutils.logger.error(err.stack);
res.send(500, 'An unexpected error occurred!');
});
}
function loadStaticFiles (app, pkgDir)
{
var dirPath = path.join(pkgDir, 'webroot');
var fsStat = fs.statSync(dirPath);
if ((null != fsStat) && (true == fsStat.isDirectory())) {
logutils.logger.debug("Registering Static Directory: " + dirPath);
app.use(express.static(dirPath, {maxAge: 3600*24*3*1000}));
}
}
function registerStaticFiles (app)
{
var pkgDir;
var staticFileDirLists = [];
var pkgNameListsLen = pkgList.length;
for (var i = 0; i < pkgNameListsLen; i++) {
pkgDir = commonUtils.getPkgPathByPkgName(pkgList[i]['pkgName']);
loadStaticFiles(app, pkgDir);
}
}
function initAppConfig ()
{
if (false == insecureAccessFlag) {
httpsApp.configure(function () {
initializeAppConfig({app:httpsApp, port:httpsPort});
});
} else {
httpApp.configure(function () {
initializeAppConfig({app:httpApp, port:httpPort});
});
}
}
getSessionIdByRedisSessionStore = function(redisStoreSessId) {
var sessIdPrefix = global.STR_REDIS_STORE_SESSION_ID_PREFIX;
var pos = redisStoreSessId.indexOf(sessIdPrefix);
if (pos != -1) {
return redisStoreSessId.slice(pos + sessIdPrefix.length);
}
return redisStoreSessId;
}
/* Set max listeners to 0 */
//store.eventEmitter.setMaxListeners(0);
function registerSessionDeleteEvent ()
{
store.eventEmitter.on('sessionDeleted', function (sid) {
/* Handle session delete cases here */
logutils.logger.debug("Session got expired: " + sid);
});
}
function loadAllFeatureURLs (myApp)
{
var pkgDir;
var pkgListLen = pkgList.length;
for (var i = 0; i < pkgListLen; i++) {
if (pkgList[i]['parseURL.xml']) {
pkgDir = commonUtils.getPkgPathByPkgName(pkgList[i]['pkgName']);
var urlListLen = pkgList[i]['parseURL.xml'].length;
for (var j = 0; j < urlListLen; j++) {
logutils.logger.debug("Registered URLs to APP :" +
pkgDir + pkgList[i]['parseURL.xml'][j]);
require(pkgDir +
pkgList[i]['parseURL.xml'][j]).registerURLsToApp(myApp);
}
}
}
return;
}
function registerReqToApp ()
{
var myApp = httpsApp;
if (true == insecureAccessFlag) {
myApp = httpApp;
}
var csrfOptions = {eventEmitter: csrfInvalidEvent};
var csrf = express.csrf(csrfOptions);
//Populate the CSRF token in req.session on login request
myApp.get('/', csrf);
myApp.get('/vcenter', csrf);
//Enable CSRF token check for all URLs starting with "/api"
myApp.post('/api/*', csrf);
loadAllFeatureURLs(myApp);
var handler = require('./src/serverroot/web/routes/handler')
handler.addAppReqToAllowedList(myApp.routes);
csrfInvalidEvent.on('csrfInvalidated', function(req, res) {
logutils.logger.debug('_csrf token got invalidated');
commonUtils.redirectToLogout(req, res);
});
}
function bindProducerSocket ()
{
var hostName = config.jobServer.server_ip
, port = config.jobServer.server_port
;
var connectURL = 'tcp://' + hostName + ":" + port;
/* Master of this nodeJS Server should connect to the worker
Server of other nodeJS server
*/
producerSock.bind(connectURL);
logutils.logger.debug('Web Server bound to port ' + port +
' to Job Server');
return;
}
function sendRequestToJobServer (msg)
{
var timer = setInterval(function () {
//logutils.logger.debug("SENDING to jobServer:" + msg.reqData);
//console.log("Getting producerSock as:", producerSock);
producerSock.send(msg.reqData);
clearTimeout(timer);
}, 1000);
}
function addProducerSockListener ()
{
producerSock.on('message', function (msg) {
console.log("Got A message, [%s]", msg);
});
}
function messageHandler (msg)
{
console.log("Got from worker process:", msg);
if ((null != msg) && (null != msg.cmd)) {
switch (msg.cmd) {
case global.STR_SEND_TO_JOB_SERVER:
sendRequestToJobServer(msg);
break;
default:
logutils.logger.error('Unknown cmd: ' + msg.cmd);
break;
}
}
}
var workers = [];
function registerFeatureLists ()
{
var pkgDir;
var pkgListLen = pkgList.length;
for (var i = 0; i < pkgListLen; i++) {
if (pkgList[i]['featureList.xml']) {
pkgDir = commonUtils.getPkgPathByPkgName(pkgList[i]['pkgName']);
var featureLen = pkgList[i]['featureList.xml'].length;
for (var j = 0; j < featureLen; j++) {
logutils.logger.debug("Registering feature Lists: " +
pkgDir +
pkgList[i]['featureList.xml'][j]);
require(pkgDir +
pkgList[i]['featureList.xml'][j]).registerFeature();
}
}
}
return;
}
function checkAndDeleteRedisRDB (callback)
{
var redisRdb = config.redis_dump_file;
if (null == redisRdb) {
redisRdb = '/var/lib/redis/dump-webui.rdb';
}
fs.stat(redisRdb, function(err, stats) {
if ((null == err) && (null != stats) && (0 === stats.size)) {
logutils.logger.debug('dump-webui.rdb size is 0, trying to ' +
'delete');
fs.unlink(redisRdb, function(err) {
if (null != err) {
logutils.logger.error('Delete of zero sized ' +
'dump-webui.rdb failed!!!');
} else {
logutils.logger.debug('zero sized dump-webui.rdb ' +
'deleted successfully');
}
callback();
});
} else {
callback();
}
});
}
function startWebCluster ()
{
if (cluster.isMaster) {
clusterMasterInit(function(err) {
logutils.logger.info("Starting Contrail UI in clustered mode.");
bindProducerSocket();
addProducerSockListener();
for (var i = 0; i < nodeWorkerCount; i += 1) {
var worker = cluster.fork();
workers[i] = worker;
}
clusterUtils.addClusterEventListener(messageHandler);
// Trick by Ian Young to make cluster and supervisor play nicely together.
// https://github.com/isaacs/node-supervisor/issues/40#issuecomment-4330946
if (process.env.NODE_HOT_RELOAD === 1) {
var signals = ['SIGINT', 'SIGTERM', 'SIGQUIT'];
underscore.each(signals, function (signal) {
process.on(signal, function () {
underscore.each(cluster.workers, function (worker) {
worker.destroy();
});
});
});
}
});
doPreStartServer(false);
} else {
clusterWorkerInit(function(error) {
initAppConfig();
var jsonDiff = require('./src/serverroot/common/jsondiff');
var redisSub = require('./src/serverroot/web/core/redisSub');
jsonDiff.doFeatureJsonDiffParamsInit();
registerSessionDeleteEvent();
registerReqToApp();
/* Set maxListener to unlimited */
process.setMaxListeners(0);
registerFeatureLists();
redisSub.createRedisClientAndSubscribeMsg(function() {
discClient.sendWebServerReadyMessage();
});
/* All the config should be set before this line */
startServer();
});
}
}
function doPreStartServer (isRetry)
{
generateLogoFile(isRetry);
generateFaviconFile(isRetry);
}
function generateLogoFile(isRetry) {
var rootPath = path.join(__dirname, 'webroot');
var defLogoFile = '/img/opencontrail-logo.png';
var srcLogoFile = rootPath + defLogoFile;
if ((null != config.logo_file) && (false == isRetry)) {
srcLogoFile = config.logo_file;
}
var destLogoFile = rootPath + '/img/sdn-logo.png';
var cmd = 'cp -f ' + srcLogoFile + " " + destLogoFile;
commonUtils.executeShellCommand(cmd, function(error, stdout, stderr) {
if (error) {
logutils.logger.error("Error occurred while copying logo file:" +
srcLogoFile + ' to ' + destLogoFile +
' ['+ error + ']');
if (false == isRetry) {
logutils.logger.error("Retrying Copying default logo");
generateLogoFile(true);
} else {
/* Default logo is also missing !!! */
}
}
});
}
function generateFaviconFile(isRetry) {
var rootPath = path.join(__dirname, 'webroot');
var defFaviconFile = '/img/opencontrail-favicon.ico';
var srcFaviconFile = rootPath + defFaviconFile;
if ((null != config.favicon_file) && (false == isRetry)) {
srcFaviconFile = config.favicon_file;
}
var destFaviconFile = rootPath + '/img/sdn-favicon.ico';
var cmdFavicon = 'cp -f ' + srcFaviconFile + " " + destFaviconFile;
commonUtils.executeShellCommand(cmdFavicon, function(error, stdout, stderr) {
if (error) {
logutils.logger.error("Error occurred while copying favicon file:" +
srcFaviconFile + ' to ' + destFaviconFile +
' ['+ error + ']');
if (false == isRetry) {
logutils.logger.error("Retrying Copying default favicon");
generateFaviconFile(true);
} else {
/* Default favicon is also missing !!! */
}
}
});
}
function startWebUIService (webUIIP, callback)
{
httpsServer = https.createServer(options, httpsApp);
httpsServer.listen(httpsPort, webUIIP, function () {
logutils.logger.info("Contrail UI HTTPS server listening on host:" +
webUIIP + " Port:" + httpsPort);
});
httpServer = http.createServer(httpApp);
httpServer.listen(httpPort, webUIIP, function () {
logutils.logger.info("Contrail UI HTTP server listening on host:" +
webUIIP + " Port:" + httpPort);
});
httpServer.on('clientError', function(exception, socket) {
logutils.logger.error("httpServer Exception: on clientError:" +
exception);
});
httpsServer.on('clientError', function(exception, socket) {
logutils.logger.error("httpsServer Exception: on clientError:" +
exception);
});
if (false == insecureAccessFlag) {
httpApp.get("*", function (req, res) {
var reqHost = req.headers.host;
var index = reqHost.indexOf(':');
if (-1 == index) {
/* here, http port: 80, in case of http port 80, req.headers.host
* does not include port, so add it
*/
reqHost = reqHost + ':80';
}
var redirectURL = global.HTTPS_URL +
reqHost.replace(httpPort, httpsPort) + req.url;
res.redirect(redirectURL);
});
}
callback(null, null);
}
function checkIfIpAddrAny (ipList)
{
if (null == ipList) {
return true;
}
var ipCnt = ipList.length;
for (var i = 0; i < ipCnt; i++) {
if (ipList[i] == global.IPADDR_ANY) {
return true;
}
}
return false;
}
function checkIpInConfig (ip)
{
var ipList = config.webui_addresses;
if (null == ipList) {
return false;
}
var ipCnt = ipList.length;
for (var i = 0; i < ipCnt; i++) {
if (ipList[i] == ip) {
return true;
}
}
return false;
}
function startServer ()
{
var ipList = config.webui_addresses;
var uiIpList = [];
var k = 0;
if (null == ipList) {
/* IPADDR_ANY */
ipList = [global.IPADDR_ANY];
}
var ipCnt = ipList.length;
var ifaces = os.networkInterfaces();
if (checkIfIpAddrAny(ipList) || (null == ifaces)) {
startWebUIService(global.IPADDR_ANY, function(err, data) {
logutils.logger.debug("**** Contrail-WebUI Server ****");
});
return;
}
for (var dev in ifaces) {
ifaces[dev].forEach(function(details) {
if (checkIpInConfig(details['address'])) {
uiIpList[k++] = details['address'];
}
});
}
if (uiIpList.length == 0) {
uiIpList = [global.IPADDR_ANY];
}
async.mapSeries(uiIpList, startWebUIService, function(err, data) {
logutils.logger.debug("**** Contrail-WebUI Server ****");
});
}
/* Function: clusterMasterInit
Initialization call for Master
*/
function clusterMasterInit (callback)
{
var parseXMLList = require('./src/tools/parseXMLList');
var mergePath = path.join(__dirname, 'webroot');
async.parallel([
function(CB) {
commonUtils.mergeAllMenuXMLFiles(pkgList, mergePath, function() {
CB(null, null);
});
},
function(CB) {
parseXMLList.readAndParseRoleListFile(function() {
CB(null, null);
});
},
function(CB) {
checkAndDeleteRedisRDB(function() {
CB(null, null);
});
},
function(CB) {
var regionJs = require('./src/tools/parseRegion');
regionJs.createRegionFile(function() {
CB(null, null);
});
}
],
function(error, results) {
callback();
});
}
/* Function: clusterWorkerInit
Initialization call for Worker
*/
function clusterWorkerInit (callback)
{
redisUtils.createRedisClientAndWait(server_port, server_ip,
global.WEBUI_DFLT_REDIS_DB,
function(redisClient) {
exports.redisClient = redisClient;
callback();
});
}
/* Start Main Server */
startWebCluster();
exports.myIdentity = myIdentity;
exports.discServEnable = discServEnable;
exports.pkgList = pkgList;