-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathopenca-ng.js
executable file
·508 lines (423 loc) · 16.1 KB
/
openca-ng.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
#! /usr/bin/env node
// =============================================================================
// OpenCA Next Generation Front End - v0.0.1
// (c) 2020 by Massimiliano Pala and OpenCA Labs
// All Rights Reserved
// =============================================================================
// Loads the Package Configuration
OCApkg = require(__dirname + "/package");
// Basic Requirements
const fs = require('fs');
const prg = require('commander');
const http = require('http');
const https = require('https');
const helmet = require('helmet');
const path = require('path');
// Front End Interface
OCAprg = null;
// Some Information
banner();
// Let's Parse the arguments
prg.version(OCApkg.version)
.option('-f, --frontend', 'Runs the Front End (Client Facing) Server.')
.option('-b, --backend', 'Runs the Back End (Order Processing) Server.')
.option('-v, --verbose', 'Prints additional information while running.')
.option('-d, --debug', 'Runs in debugging mode (lots of information).')
.parse(process.argv);
// Let's Run in one of the two modes (frontend or backend)
if (prg.frontend == true && prg.backend == true) {
console.error("ERROR: Only one of 'frontend' or 'backend' options allowed.\n");
return(1);
}
// Sets the Frontend Prefix
if (prg.frontend == true) {
OCAprg = "frontend";
}
// Sets the Backend Prefix
if (prg.backend == true) {
OCAprg = "backend";
}
// Checks we have a prefix
if (OCAprg == null) {
console.error("ERROR: One of '-frontend' (-f) or '-backend' (-b) options required.\n");
return(1);
}
// Some Verbose Output
console.log("[=== Begin Server Initialization: ===]\n");
console.log("* Pre-Requisites Checking:");
// Global: Load Main Config and Package Info
OCAcnf = require(__dirname + "/conf/" + OCAprg + "/" + OCAprg + "-cnf");
// Sets the Verbose and Debug Option
if (prg.verbose == true) OCAcnf.verbose = true;
if (prg.debug == true) OCAcnf.debug = true;
// Global: Constant for OpenCA Base URL
BASE_URL = OCAcnf.baseUrl;
// Global: Generic URLs
Urls = OCAcnf.urls;
// Prepends the URLs with the baseUrl
for (i in Urls) {
Urls[i] = OCAcnf.baseUrl + Urls[i];
}
// Directories For Frontend
if (OCAprg == "frontend") {
OCApath = {
"base" : path.normalize(__dirname),
"lib" : path.normalize(__dirname + "/lib/" + OCAprg),
"mods" : path.normalize(__dirname + "/mods/" + OCAprg),
"data" : path.normalize(__dirname + "/data/" + OCAprg),
"audit" : path.normalize(__dirname + "/data/" + OCAprg + "/audit.d"),
"order" : path.normalize(__dirname + "/data/" + OCAprg + "/order.d"),
"type" : path.normalize(__dirname + "/data/" + OCAprg + "/type.d"),
"org" : path.normalize(__dirname + "/data/" + OCAprg + "/org.d"),
"pki" : path.normalize(__dirname + "/data/" + OCAprg + "/pki.d"),
"backend": path.normalize(__dirname + "/conf/" + OCAprg + "/backend.d"),
"sync" : path.normalize(__dirname + "/conf/" + OCAprg + "/sync.d"),
"user" : path.normalize(__dirname + "/conf/" + OCAprg + "/user.d"),
"queue" : path.normalize(__dirname + "/conf/" + OCAprg + "/queue.d")
};
} else {
// Directories For Backend
OCApath = {
"base" : path.normalize(__dirname),
"lib" : path.normalize(__dirname + "/lib/" + OCAprg),
"mods" : path.normalize(__dirname + "/mods/" + OCAprg),
"data" : path.normalize(__dirname + "/data/" + OCAprg),
"audit" : path.normalize(__dirname + "/data/" + OCAprg + "/audit.d"),
"order" : path.normalize(__dirname + "/data/" + OCAprg + "/order.d"),
"type" : path.normalize(__dirname + "/data/" + OCAprg + "/type.d"),
"ca" : path.normalize(__dirname + "/data/" + OCAprg + "/ca.d"),
"job" : path.normalize(__dirname + "/data/" + OCAprg + "/job.d"),
"csp" : path.normalize(__dirname + "/conf/" + OCAprg + "/csp.d"),
"user" : path.normalize(__dirname + "/conf/" + OCAprg + "/user.d")
};
}
// Checks the existance of the required paths
for (var dir in OCApath) {
// Checks if the directory still exists
if (!fs.existsSync(OCApath[dir])) {
// If we have a missing directory, we create it
fs.mkdir(OCApath[dir], '0700', function $mail$createDirs (err) {
// Checks if we have an error code
if (err != null && err.code != 'EEXIST') {
// Non recoverable error, we must abort.
console.error("ERROR: Cannot create dir [ id: %s, path: %s, err: %s]", OCApath[dir], OCApath[dir], err);
process.exit(1);
}
})
}
}
// Global: Require Options
const reqOptions = {
"recurse" : true,
"extensions" : [ '.js', '.json' ],
"filter" : function (path) {
return (path.match(/._.*/) ? false : true); }
};
// Global: Common Modules
common = require("require-dir")( __dirname + "/lib/common", reqOptions);
console.log(" - Loaded common libraries .....: OK");
// Global: Library Modules
lib = require("require-dir")( __dirname + "/lib/" + OCAprg, reqOptions);
console.log(" - Loaded local libraries ......: OK");
// Global: Modules Shortcut
OCAengine = common.engine;
OCAtools = common.tools;
OCAauth = common.auth;
// OCAsec = lib.sec;
// OCAdata = lib.data;
// Global: Engine Constructors
OCAMsg = common.engine.OCAMsg;
OCAErr = common.engine.OCAErr;
// Logging and Debugging
OCAlog = common.engine.OCAlog;
OCAverb = common.engine.OCAverb;
OCAdebug = common.engine.OCAdebug;
// Network Queries
OCAquery = common.engine.OCAquery;
// API Error
ApiError = common.engine.ApiError
// ==============================
// Instantiate the Global Objects
// ==============================
// Available CAs
OCAcas = {
"queue" : { }
};
if (OCAprg == "backend") {
// This Global Object is used for managing the
// synchronization between the Frontend and the
// Backend (that can be online or offline)
OCAfeq = lib.feq.OCAfeq;
// Orders Submitted
OCAorders = {
"queue" : { }
}; // Frontend Orders
// Jobs Created from Orders
OCAjobs = {
"queue" : { },
"last" : null,
"first" : null
}; // Backend Jobs
// Availabe Crypto Service Providers (populated by ca.js)
OCAcsps = {
"queue" : { }
}; // CSPs List
// Front-End Queue
fequeue = new OCAfeq(OCAcnf.frontend,
{ "localId" : OCAcnf.id }
);
} else {
// This Global Object is used for managing the
// synchronization between the Frontend and the
// Backend (that can be online or offline)
OCAbesync = lib.besync.OCAbesync;
// This Global Object is used for Synchronizing
// across different Frontends (e.g., users, orgs,
// products, etc.)
OCAfesync = lib.fesync.OCAfesync;
// This Global Object is used for Synchronizing
// across different Frontends (e.g., users, orgs,
// products, etc.)
OCAbeq = lib.beq.OCAbeq;
// Products Profiles
OCAproducts = {
"queue" : { }
}; // Available Products
// Available Organizations
OCAorgs = {
"queue" : { }
};
// Available PKIs
OCApkis = {
"queue" : { }
};
// Synchronization Object for Frontends
OCAlog('\n* Setting up Front-End Synchronization Objects (configs: %s)',
OCApath.queue);
OCAsync = {
"backend" : new OCAbesync(OCApath.queue, { "localId" : OCAcnf.id }),
"frontend" : new OCAfesync(OCApath.sync, { "localId" : OCAcnf.id })
};
}
// Make sure that all the needed paths are
// present in the directory structure
OCAlog(" - Checking local directories ..: OK");
// Start the Audit Facility
OCAaudit = {
"init" : common.audit.OCAaudit,
"cnf" : OCAcnf.audit
}
// Setup the General Audit
OCAaudit.server = new OCAaudit.init(OCApath.audit, OCAcnf.audit);
// Global: Session inactivity and lifetime (express in DB format)
SessionsConfig = {
"maxInactivity" : OCAcnf.cookies.maxInactivity,
"maxLifespan" : OCAcnf.cookies.maxLifespan,
"cookieName" : OCAcnf.cookies.name,
"cookieDomain": OCAcnf.cookies.domain, // ".openca.org",
"cookieMaxAge": OCAcnf.cookies.maxAge, // 1800000, // Expires in 30 mins
"maxUpdateAge": OCAcnf.cookies.MaxUpdateAge, // 3600000 // Expires in 1 hour
};
// Some Verbose Output
OCAlog("\n* Netowork Server Initialization (Mode: %s):", OCAprg);
// Initializes the Auth Layer
OCAauth.init();
// Local: Packages
var Express = require('express'); // call express
var BodyParser = require('body-parser');
var CookieParser = require('cookie-parser');
// Promise Engine
Promise = require('bluebird');
// define our app using express
var app = Express();
// configure app to use bodyParser()
// this will let us get the data from a POST
app.use(BodyParser.urlencoded({ extended: true }));
app.use(BodyParser.json());
app.use(CookieParser());
app.use(helmet());
// Port Number
const port = OCAcnf.listen.port || process.env.PORT; // set our port
// ROUTES FOR OUR API
// =============================================================================
var router = Express.Router(); // get an instance of the express Router
// REGISTER OUR ROUTES -------------------------------
if (OCAcnf.listen.pathPrefix != null) {
// all of our routes will be prefixed with the version of the API
app.use(OCAcnf.listen.pathPrefix, router);
OCAlog(" - Setting pathPrefix ..........: %s", OCAcnf.listen.pathPrefix);
}
// test route to make sure everything is working
// (accessed at GET http://localhost:PORT/api)
router.get('/', function(req, res) {
// Wrong Usage of the API, let's redirect somewhere else
res.redirect("https://www.openca.org");
});
// Routing Functions
mods = require("require-dir")( __dirname + "/mods/" + OCAprg, reqOptions);
// Registers all the handlers
OCAlog("\n* Registered Modules (from 'mods/' dir):");
// Cycles through the mods and registers all the routes
// for each modulus that was deployed in the mods/ directory
for (var i in mods) {
// If we have a viable handlers array, let's go through them and
// register the handlers with the right paths
if (mods[i] && mods[i].handlers != null) {
// Some information about the module being imported
OCAlog(" - Registering Module [" + i + "] with the following methods:");
// If the module has handlers, let's register the module
OCAtools.registerHandlers(router, mods[i].handlers);
} else {
// The module does not have valid handlers
if (typeof(mods[i]) !== "function") OCAlog(" - " + i + " ... Skipped");
}
}
// Default Handler For Wrong Path
const defaultHandler = OCAtools.getHandler({
method: "all", path: /.*/, func: function $Main$defaultHandler ( req, res, ctx ) {
// Builds the error message
ctx.msg.err(new OCAErr(404, "Requested Resource Not Found"));
// Returns the error
OCAtools.returnError(req, res, ctx);
// All Done
}
}, { login: false, certAuth: false, reqAdmin: false, reqAudit: false }, true);
// Registers the Default Handler
OCAtools.registerHandlers(router, [ defaultHandler ]);
// General Server Handler
OCAserver = null;
// Listen Server
OCAlog('\n* Setting up the Listening Server (%s:%d) (TLS: %s)',
OCAcnf.listen.host, OCAcnf.listen.port, (OCAcnf.listen.auth == null ? "No" : "Yes"));
// Checks if we want an HTTP or HTTPS server
if (OCAcnf.listen.auth == null) {
// No HTTPS to setup
OCAserver = http.createServer(app);
} else {
// Let's setup the HTTPS
try {
var keyFile = OCAcnf.listen.auth.privKey;
var certFile = OCAcnf.listen.auth.certAndChain;
var trustedFile = OCAcnf.listen.auth.trustedCas;
// Checks for the Key
OCAlog(" - [ load: key file, path:%s ]", keyFile);
if (keyFile != "" && !fs.existsSync(keyFile)) {
OCAlog("\nERROR: Cannot find the private key (auth > privKey) file ['%s'], aborting\n", keyFile);
process.exit(1);
}
// Checks for the Certificate / Certificate Chain
OCAlog(" - [ load: chain file, path:%s ]", certFile);
if (certFile != "" && !fs.existsSync(certFile)) {
OCAlog("\nERROR: Cannot find the cert (auth > certChain option) file ['%s'], aborting.\n", certFile);
process.exit(1);
}
// Checks for the Trusted CAs
OCAlog(" - [ load: trusted CAs, path:%s ]", trustedFile);
if (trustedFile != null && !fs.existsSync(trustedFile)) {
OCAlog("\nERROR: Cannot find the cert (auth > trustedCas option) file ['%s'], aborting.\n", trustedFile);
process.exit(1);
}
const httpsOptions = {
// Secure Context Options
maxVersion : 'TLSv1.3',
minVersion : 'TLSv1.2',
// Private keys in PEM format
key : fs.readFileSync(keyFile, 'utf8'),
// Server's Certificate in PEM format
cert : fs.readFileSync(certFile, 'utf8'),
// Overrides the default Mozilla's CAs
ca : (trustedFile != null ? fs.readFileSync(trustedFile, 'utf8') : undefined),
// Loads the most recent CRL
// crl : fs.readFileSync(crlFile, 'utf8'),
// Requires Client Authentication
requestCert : (OCAcnf.listen.auth.requestClientCert == true ? true : false),
// Rejects Certificates not from the Infrastructure PKI
rejectUnauthorized : (OCAcnf.listen.auth.allowUntrustedClientCert == false ? true : false)
};
// Setup the HTTPS server
OCAserver = https.createServer(httpsOptions, app);
if (OCAserver == null) {
throw new ApiError("Cannot create a new HTTPS server, aborting.");
process.exit(1);
}
} catch (e) {
// Error detected, aborting.
OCAlog("Cannot Setup TLS (Server Side), aborting. (%s)", JSON.stringify(e));
// All Done
process.exit(1);
}
}
// START Listening
OCAserver.listen(port);
// All Done
OCAlog('\n* Services from \'%s\' are available on port %d\n', OCApkg.name, port);
OCAlog("[=== End Server Initialization: ===]\n");
//
// SETUP the default handler for exceptions
//
process.on("uncaughtException", function OCA$uncaughtExceptionHandler(ex) {
OCAdebug(new ApiError("Exception: " + ex, { "Exception" : ex }));
})
// Terminating the Process gracefully
var exitFunction = function main$exitFunction(code) {
// Adds the exit to the general Audit logs
OCAaudit.server.add("Process ({0}) Exiting, closing logs [Pid: %d].".format(OCAprg, process.pid));
// For each of the configured CAs, let's add the exit line
for (var id in OCAcas.queue) {
// Gets the CA definition
var myCA = OCAcas.queue[id];
// Writes into the CAs logs
myCA.log.add("Process ({0}) Exiting, closing logs [Pid: {1}].".format(OCAprg, process.pid));
}
};
process.on('SIGINT', function() {
OCAdebug("SIGINT detected, calling cleanup functions (exit).");
// Invoke the Exit Handler
process.exit();
});
process.on('exit', (code) => {
// Debugging Info
OCAdebug("Process (%s) Exiting [Pid: %d]", OCAprg, process.pid);
// Creates the log object
var logObj = {
"date" : new Date(),
"text" : "Process ({0}) Exiting, closing logs [Pid: %d].".format(OCAprg, process.pid)
};
// Appends the log data to the main audit file
fs.appendFileSync(OCAaudit.server.filename, JSON.stringify(logObj) + "\n");
// If the OCAcas is defined
if (typeof OCAcas !== "undefined" && OCAcas.queue != null) {
// Appends the log data to each of the CA files
for (var id in OCAcas.queue) {
// Retrives the CA object
var myCA = OCAcas.queue[id];
// Sets the text in the logObj
logObj.text = "Process ({0}) Exiting, closing logs [Pid: {1}].".format(OCAprg, process.pid);
// Appends the log line to the file
fs.appendFileSync(myCA.log.filename, JSON.stringify(logObj) + "\n");
}
}
// If the OCAorders is defined
if (typeof OCAorders !== "undefined" && OCAorders.queue != null) {
// Appends the log data for each of the Order
for (var id in OCAorders.queue) {
// Retrives the CA object
var myOrd = OCAorders.queue[id];
// Sets the text in the logObj
logObj.text = "Process ({0}) Exiting, closing log [Pid: {1}].".format(OCAprg, process.pid);
// Appends the log line to the file
fs.appendFileSync(myOrd.log.filename, JSON.stringify(logObj) + "\n");
}
}
// All Done
OCAdebug("All Done, Glad to Serve You Master!");
});
// Server is listening
return 0;
function banner() {
// Some Banner Information
console.log("");
console.log("// %s - v%s", OCApkg.description, OCApkg.version);
console.log("// (C) 2020 by %s <%s>", OCApkg.author.name, OCApkg.author.email);
console.log("// All Rights Reserved\n");
}