-
Notifications
You must be signed in to change notification settings - Fork 17
/
server.js
190 lines (151 loc) · 5.55 KB
/
server.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
/*
* NodePKI
* ... a NodeJS-based OpenSSL PKI management server.
* Originally developed by Thomas Leister for ADITO GmbH.
* NodePKI is published under MIT License.
*
* NodePKI startup file
* Loads config, prepares CertDB database, starts OCSP server, initializes and starts HTTP server and API.
*/
var exec = require('child_process').exec;
var util = require('util');
var fs = require('fs-extra');
var yaml = require('js-yaml');
var log = require('fancy-log');
var express = require('express');
var figlet = require('figlet');
var commandExists = require('command-exists').sync;
var http = require('http');
var bodyparser = require('body-parser');
var api = require('./api.js');
var publicDl = require('./publicDl.js');
var certdb = require('./certdb.js');
var ocsp = require('./ocsp-server.js');
var crl = require('./crl.js');
var fingerprint = require('./cert_fingerprint.js');
var genpki = require('./genpki.js');
var app = express();
/***************
* Start server *
***************/
log.info("NodePKI is starting up ...");
console.log(figlet.textSync('NodePKI', {}));
console.log(" By ADITO Software GmbH\n\n");
// Base Base path of the application
global.paths = {
basepath: __dirname + "/",
datapath: __dirname + '/data/',
pkipath: __dirname + "/data/mypki/",
tempdir: __dirname + "/tmp/"
};
new Promise(function(resolve, reject) {
// Checks environment
/*
* Make sure there is a config file config.yml
*/
if(fs.existsSync(global.paths.datapath + 'config/config.yml')) {
log.info("Reading config file data/config/config.yml ...");
global.config = yaml.safeLoad(fs.readFileSync(global.paths.datapath + 'config/config.yml', 'utf8'));
/*
* Check if the openssl command is available
*/
if(commandExists('openssl') === false) {
log("openssl command is not available. Please install openssl.")
reject()
} else {
/*
* Check if there is a PKI directory with all the OpenSSL content.
*/
fs.ensureDir(global.paths.pkipath);
if(fs.existsSync(global.paths.pkipath + 'created') === false) {
log("There is no PKI available. Creating PKI ...");
genpki.create().then(function() {
log(">>>>>> CA has successfully been created! :-) <<<<<<")
resolve()
})
.catch(function(err) {
reject(err)
})
} else {
resolve()
}
}
} else {
// There is no config file yet. Create one from config.yml.default and quit server.
log("No custom config file 'data/config/config.yml' found.");
fs.ensureDirSync(global.paths.datapath +'config');
fs.copySync(__dirname + '/config.default.yml', global.paths.datapath + 'config/config.yml');
log("Default config file was copied to data/config/config.yml.");
console.log("\
**********************************************************************\n\
*** Please customize data/config/config.yml according to your ***\n\
*** environment and restart script. ***\n\
**********************************************************************");
log("Server will now quit.");
reject()
}
})
.then(function() {
// Ensure tmp dir
fs.ensureDir('tmp');
// Make sure DB file exists ...
fs.ensureFileSync('data/user.db');
// Re-index cert database
certdb.reindex().then(function(){
/*
* Start HTTP server
*/
app.use('/api', bodyparser.json()); // JSON body parser for /api/ paths
var server = app.listen(global.config.server.http.port, global.config.server.ip, function() {
var host = server.address().address;
var port = server.address().port;
log.info(">>>>>> HTTP server is listening on " + host + ":" + port + " <<<<<<");
});
log.info("Registering API endpoints");
api.initAPI(app)
publicDl.initPublicDl(app)
}).catch(function(error){
log.error("Could not initialize CertDB index: " + error);
});
// Start OCSP server
ocsp.startServer()
.then(function(){
log.info("OCSP-Server is running");
})
.catch(function(error){
log.error("Could not start OCSP server: " + error);
});
// Show Root Cert fingerprint
fingerprint.getFingerprint(global.paths.pkipath + 'root/root.cert.pem').then(function(fingerprint_out) {
log(">>>>>> Root CA Fingerprint: " + fingerprint_out);
})
.catch(function(err) {
log.error("Could not get Root CA fingerprint!")
});
/*
* CRL renewal cronjob
*/
var crlrenewint = 1000 * 60 * 60 * 24; // 24h
setInterval(crl.createCRL, crlrenewint);
log("Server started.")
})
.catch(function(err) {
log("Server not started.")
if(err != undefined) {
log("Error: " + err)
}
process.exit()
})
/*********************************
* Server stop routine and events *
*********************************/
var stopServer = function() {
log("Received termination signal.");
log("Stopping OCSP server ...");
ocsp.stopServer();
log("Bye!");
process.exit();
};
process.on('SIGINT', stopServer);
process.on('SIGHUP', stopServer);
process.on('SIGQUIT', stopServer);