-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
136 lines (120 loc) · 4.04 KB
/
main.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
#! /usr/bin/env node
import ocsp from 'ocsp';
import fs from 'fs';
import pino from 'pino';
import { exit } from 'process';
import https from 'https';
'use strict';
let config = {
listenip: '',
listenport: '',
loglevel: 'info',
issuercert: '',
issuercertdata: '',
issuerkey: '',
issuerkeydata: '',
rootcert: '',
rootcertdata: ''
}
let configjson;
const logger = pino({
level: config.loglevel,
prettyPrint: {
colorize: true,
translateTime: true
}
});
// try to read configfile
try {
let configraw = fs.readFileSync('config.json');
configjson = JSON.parse(configraw);
// loglevel handling
if (typeof configjson.loglevel !== 'undefined' && configjson.loglevel !== "") {
if (configjson.loglevel === 'trace' || configjson.loglevel === 'debug' || configjson.loglevel === 'info' || configjson.loglevel === 'warn' || configjson.loglevel === 'error') {
logger.level = configjson.loglevel
} else {
logger.error(`Loglevel config misconfigured - allowed values: trace|debug|info|warn|error`)
exit(1)
}
}
// config consistency check
logger.debug(`config read: ` + JSON.stringify(configjson, null, 4))
// config listen handling
if (typeof configjson.listenip === 'undefined' || typeof configjson.listenport === 'undefined') {
logger.error(`Listen config misconfigured`)
exit(1)
}
// config issuercert handling
if (typeof configjson.issuercert === 'undefined' || typeof configjson.issuerkey === 'undefined' || configjson.issuercert === "" || configjson.issuercert === "") {
logger.error(`No issuer-cert/issuer-key parameters defined`)
exit(1)
}
// config listen handling
config = configjson;
} catch(e) {
logger.error(`Couldn't read config.json, check existence and consistence (jq)`)
exit(1)
}
function callRest(url) {
return new Promise ((resolve, reject) => {
const options = {
timeout: 3000,
rejectUnauthorized: false // TODO: move to config
};
logger.info("callRest - obtaining info from " + url)
https.get(url, options, (resp) => {
let data = '';
// A chunk of data has been received.
resp.on('data', (chunk) => {
data += chunk;
});
// The whole response has been received. Print out the result.
resp.on('end', () => {
logger.debug("callRest - obtained data from " + url);
resolve(data);
});
}).on("error", (err) => {
logger.error("callRest - error: " + err.message)
});
});
}
if(config.issuercert.includes("https://")){
logger.info("Reading issuer certificate from network")
try {
config.issuercertdata = await callRest(config.issuercert);
config.issuerkeydata = await callRest(config.issuerkey);
if(typeof configjson.rootcert !== 'undefined' && configjson.rootcert !== ""){
config.rootcertdata = await callRest(config.rootcert);
}
} catch(e) {
logger.error(`Couldn't read cert: \"` + config.issuercert + `\" (defined in config.json) or key: \"` + config.issuerkey + `\" (defined in config.json) \n` + e)
exit(1)
}
} else {
logger.info("Reading issuer certificate from disk")
try {
config.issuercertdata = fs.readFileSync(config.issuercert).toString();
config.issuerkeydata = fs.readFileSync(config.issuerkey).toString();
if(typeof configjson.rootcert !== 'undefined' && configjson.rootcert !== ""){
config.rootcertdata = fs.readFileSync(config.rootcert).toString();
}
} catch(e) {
logger.error(`Couldn't read cert: \"` + config.issuercert + `\" (defined in config.json) or key: \"` + config.issuerkey + `\" (defined in config.json)`)
exit(1)
}
}
// try to start application
var server = ocsp.Server.create({
configobj: config
});
if(config.certdb === "memory") {
logger.debug(`CERTDB is memory - loaded certs from code`)
server.addCert(43, 'good');
server.addCert(44, 'revoked', {
revocationTime: new Date(),
revocationReason: 'cACompromise'
});
}
server.listen(config.listenport, config.listenip, () => {
logger.info(`Server running at http://${config.listenip}:${config.listenport}/`);
});