Added extra checking for Let's Encrypt NodeJS version.

Ylian Saint-Hilaire committed Nov 19, 2019 at 11:18 UTC 2ab89dc8a9a926a679224f3c32b76be644c799b7
2 files changed +9 -59
letsEncrypt.js
+7 -57
@@ -25,8 +25,9 @@ module.exports.CreateLetsEncrypt = function (parent) {
25 parent.debug('cert', "Initializing Let's Encrypt support, using GreenLock v" + greenLockVersion);
26 }
27
28 - // Check the current node version
29 - if (Number(process.version.match(/^v(\d+\.\d+)/)[1]) < 8) { return null; }
28 + // Check the current node version and support for generateKeyPair
29 + if (require('crypto').generateKeyPair == null) { return null; }
30 + if (Number(process.version.match(/^v(\d+\.\d+)/)[1]) < 10) { return null; }
31
32 // Try to delete the "./ursa-optional" or "./node_modules/ursa-optional" folder if present.
33 // This is an optional module that GreenLock uses that causes issues.
@@ -217,59 +218,12 @@ module.exports.CreateLetsEncrypt = function (parent) {
218
219 // Check if we need to renew the certificate, call this every day.
220 obj.checkRenewCertificate = function () {
220 - obj.certCheckStart = Date.now();
221 -
222 - // Check if there is anything in the let's encrypt folder
223 - var somethingIsinFolder = false;
224 - try {
225 - var filesinFolder = require('fs').readdirSync(obj.runAsProduction ? obj.configPath : obj.configPathStaging);
226 - somethingIsinFolder = (filesinFolder.indexOf(obj.runAsProduction ? 'live' : 'staging') != -1);
227 - } catch (ex) { console.log(ex); }
221 + parent.debug('cert', "Checking certificate for " + obj.leDomains[0] + " (" + (obj.runAsProduction ? "Production" : "Staging") + ")");
222
223 // Setup renew options
224 + obj.certCheckStart = Date.now();
225 const xle = (obj.runAsProduction === true) ? obj.le : obj.leStaging;
226 var renewOptions = { servername: obj.leDomains[0], altnames: obj.leDomains };
232 -
233 - // Add the domains
234 - if (somethingIsinFolder == false) {
235 - try {
236 - var addOptions = { subject: obj.leDomains[0], altnames: obj.leDomains };
237 - parent.debug('cert', "Adding domains: " + JSON.stringify(addOptions));
238 - xle.add(addOptions);
239 - } catch (ex) {
240 - parent.debug('cert', "add certificate exception: (" + JSON.stringify(ex) + ")");
241 - console.log(ex);
242 - }
243 - }
244 -
245 - /*
246 - if (somethingIsinFolder == false) {
247 - parent.debug('cert', "Getting certificate for " + obj.leDomains[0] + " (" + (obj.runAsProduction ? "Production" : "Staging") + ")");
248 - xle.get({ servername: obj.leDomains[0] })
249 - .then(function (results) {
250 - if ((results == null) || (typeof results != 'object') || (results.length == 0) || (results[0].error != null)) {
251 - parent.debug('cert', "Unable to get a certificate (" + (obj.runAsProduction ? "Production" : "Staging") + ", " + (Date.now() - obj.certCheckStart) + "ms): " + JSON.stringify(results));
252 - } else {
253 - parent.debug('cert', "Get certificate completed (" + (obj.runAsProduction ? "Production" : "Staging") + ", " + (Date.now() - obj.certCheckStart) + "ms): " + JSON.stringify(results));
254 - if (obj.performRestart === true) { parent.debug('cert', "Certs changed, restarting..."); obj.parent.performServerCertUpdate(); } // Reset the server, TODO: Reset all peers
255 - else if (obj.performMoveToProduction == true) {
256 - parent.debug('cert', "Staging certificate received, moving to production...");
257 - obj.runAsProduction = true;
258 - obj.performMoveToProduction = false;
259 - obj.performRestart = true;
260 - setTimeout(obj.checkRenewCertificate, 10000); // Check the certificate in 10 seconds.
261 - }
262 - }
263 - })
264 - .catch(function (ex) {
265 - parent.debug('cert', "getCertificate exception: (" + JSON.stringify(ex) + ")");
266 - console.log(ex);
267 - });
268 - return;
269 - }
270 - */
271 -
272 - parent.debug('cert', "Checking certificate for " + obj.leDomains[0] + " (" + (obj.runAsProduction ? "Production" : "Staging") + ")");
227 try {
228 xle.renew(renewOptions)
229 .then(function (results) {
@@ -332,19 +286,15 @@ module.exports.create = function (options) {
286 //console.log('LE-DEFAULTS-Production', options);
287 if (options != null) { for (var i in options) { if (manager.parent.leDefaults[i] == null) { manager.parent.leDefaults[i] = options[i]; } } }
288 r = manager.parent.leDefaults;
335 - var mainsite = { subject: manager.parent.leDomains[0] };
336 - if (manager.parent.leDomains.length > 0) { mainsite.altnames = manager.parent.leDomains; }
289 r.subscriberEmail = manager.parent.parent.config.letsencrypt.email;
338 - r.sites = { mainsite: mainsite };
290 + r.sites = { mainsite: { subject: manager.parent.leDomains[0], altnames: manager.parent.leDomains } };
291 } else {
292 // Staging
293 //console.log('LE-DEFAULTS-Staging', options);
294 if (options != null) { for (var i in options) { if (manager.parent.leDefaultsStaging[i] == null) { manager.parent.leDefaultsStaging[i] = options[i]; } } }
295 r = manager.parent.leDefaultsStaging;
344 - var mainsite = { subject: manager.parent.leDefaultsStaging[0] };
345 - if (manager.parent.leDefaultsStaging.length > 0) { mainsite.altnames = manager.parent.leDefaultsStaging; }
296 r.subscriberEmail = manager.parent.parent.config.letsencrypt.email;
347 - r.sites = { mainsite: mainsite };
297 + r.sites = { mainsite: { subject: manager.parent.leDomains[0], altnames: manager.parent.leDomains } };
298 }
299 return r;
300 };
meshcentral.js
+2 -2
@@ -833,7 +833,7 @@ function CreateMeshCentralServer(config, args) {
833 obj.certificateOperations.GetMeshServerCertificate(obj.args, obj.config, function (certs) {
834 // Get the current node version
835 const nodeVersion = Number(process.version.match(/^v(\d+\.\d+)/)[1]);
836 - if ((nodeVersion < 8) || (obj.config.letsencrypt == null) || (obj.redirserver == null)) {
836 + if ((nodeVersion < 8) || (require('crypto').generateKeyPair == null) || (obj.config.letsencrypt == null) || (obj.redirserver == null)) {
837 obj.StartEx3(certs); // Just use the configured certificates
838 } else {
839 var le = require('./letsencrypt.js');
@@ -2040,7 +2040,7 @@ function mainStart() {
2040 if (require('os').platform() == 'win32') { modules.push('node-windows'); if (sspi == true) { modules.push('node-sspi'); } } // Add Windows modules
2041 if (ldap == true) { modules.push('ldapauth-fork'); }
2042 //if (config.letsencrypt != null) { modules.push('greenlock@2.8.8'); modules.push('le-store-certbot'); modules.push('le-challenge-fs'); modules.push('le-acme-core'); } // Add Greenlock Modules
2043 - if (config.letsencrypt != null) { if (nodeVersion < 8) { console.log("WARNING: Let's Encrypt support requires Node v8 or higher."); } else { modules.push('greenlock'); } } // Add Greenlock Module
2043 + if (config.letsencrypt != null) { if ((nodeVersion < 10) || (require('crypto').generateKeyPair == null)) { console.log("WARNING: Let's Encrypt support requires Node v10.12.0 or higher."); } else { modules.push('greenlock'); } } // Add Greenlock Module
2044 if (config.settings.mqtt != null) { modules.push('aedes'); } // Add MQTT Modules
2045 if (config.settings.mongodb != null) { modules.push('mongodb'); } // Add MongoDB, official driver.
2046 if (config.settings.vault != null) { modules.push('node-vault'); } // Add official HashiCorp's Vault module.