Nginx templates
This commit is contained in:
@ -18,7 +18,43 @@ function omissions () {
|
||||
|
||||
const internalCertificate = {
|
||||
|
||||
allowed_ssl_files: ['certificate', 'certificate_key', 'intermediate_certificate'],
|
||||
allowed_ssl_files: ['certificate', 'certificate_key', 'intermediate_certificate'],
|
||||
interval_timeout: 1000 * 60 * 60 * 12, // 12 hours
|
||||
interval: null,
|
||||
interval_processing: false,
|
||||
|
||||
initTimer: () => {
|
||||
logger.info('Let\'s Encrypt Renewal Timer initialized');
|
||||
internalCertificate.interval = setInterval(internalCertificate.processExpiringHosts, internalCertificate.interval_timeout);
|
||||
},
|
||||
|
||||
/**
|
||||
* Triggered by a timer, this will check for expiring hosts and renew their ssl certs if required
|
||||
*/
|
||||
processExpiringHosts: () => {
|
||||
let internalNginx = require('./nginx');
|
||||
|
||||
if (!internalCertificate.interval_processing) {
|
||||
internalCertificate.interval_processing = true;
|
||||
logger.info('Renewing SSL certs close to expiry...');
|
||||
|
||||
return utils.exec(certbot_command + ' renew -q ' + (debug_mode ? '--staging' : ''))
|
||||
.then(result => {
|
||||
logger.info(result);
|
||||
internalCertificate.interval_processing = false;
|
||||
|
||||
return internalNginx.reload()
|
||||
.then(() => {
|
||||
logger.info('Renew Complete');
|
||||
return result;
|
||||
});
|
||||
})
|
||||
.catch(err => {
|
||||
logger.error(err);
|
||||
internalCertificate.interval_processing = false;
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* @param {Access} access
|
||||
@ -493,7 +529,7 @@ const internalCertificate = {
|
||||
* @returns {Promise}
|
||||
*/
|
||||
requestLetsEncryptSsl: certificate => {
|
||||
logger.info('Requesting Let\'sEncrypt certificates for Cert #' + certificate.id + ': ' + certificate.domain_names.join(', '));
|
||||
logger.info('Requesting Let\'sEncrypt certificates for Cert #' + certificate.id + ': ' + certificate.domain_names.join(', '));
|
||||
|
||||
return utils.exec(certbot_command + ' certonly --cert-name "npm-' + certificate.id + '" --agree-tos ' +
|
||||
'--email "' + certificate.meta.letsencrypt_email + '" ' +
|
||||
@ -511,7 +547,7 @@ const internalCertificate = {
|
||||
* @returns {Promise}
|
||||
*/
|
||||
renewLetsEncryptSsl: certificate => {
|
||||
logger.info('Renewing Let\'sEncrypt certificates for Cert #' + certificate.id + ': ' + certificate.domain_names.join(', '));
|
||||
logger.info('Renewing Let\'sEncrypt certificates for Cert #' + certificate.id + ': ' + certificate.domain_names.join(', '));
|
||||
|
||||
return utils.exec(certbot_command + ' renew -n --force-renewal --disable-hook-validation --cert-name "npm-' + certificate.id + '" ' + (debug_mode ? '--staging' : ''))
|
||||
.then(result => {
|
||||
@ -519,6 +555,16 @@ const internalCertificate = {
|
||||
return result;
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* @param {Object} certificate
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
hasLetsEncryptSslCerts: certificate => {
|
||||
let le_path = '/etc/letsencrypt/live/npm-' + certificate.id;
|
||||
|
||||
return fs.existsSync(le_path + '/fullchain.pem') && fs.existsSync(le_path + '/privkey.pem');
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = internalCertificate;
|
||||
|
@ -1,13 +1,13 @@
|
||||
'use strict';
|
||||
|
||||
const _ = require('lodash');
|
||||
const fs = require('fs');
|
||||
const Liquid = require('liquidjs');
|
||||
const logger = require('../logger').nginx;
|
||||
const utils = require('../lib/utils');
|
||||
const error = require('../lib/error');
|
||||
const internalSsl = require('./ssl');
|
||||
const debug_mode = process.env.NODE_ENV !== 'production';
|
||||
const _ = require('lodash');
|
||||
const fs = require('fs');
|
||||
const Liquid = require('liquidjs');
|
||||
const logger = require('../logger').nginx;
|
||||
const utils = require('../lib/utils');
|
||||
const error = require('../lib/error');
|
||||
const internalCertificate = require('./certificate');
|
||||
const debug_mode = process.env.NODE_ENV !== 'production';
|
||||
|
||||
const internalNginx = {
|
||||
|
||||
@ -32,11 +32,6 @@ const internalNginx = {
|
||||
// We're deleting this config regardless.
|
||||
return internalNginx.deleteConfig(host_type, host); // Don't throw errors, as the file may not exist at all
|
||||
})
|
||||
.then(() => {
|
||||
if (host.ssl && !internalSsl.hasValidSslCerts(host_type, host)) {
|
||||
return internalSsl.configureSsl(host_type, host);
|
||||
}
|
||||
})
|
||||
.then(() => {
|
||||
return internalNginx.generateConfig(host_type, host);
|
||||
})
|
||||
@ -56,7 +51,6 @@ const internalNginx = {
|
||||
});
|
||||
})
|
||||
.catch(err => {
|
||||
|
||||
if (debug_mode) {
|
||||
logger.error('Nginx test failed:', err.message);
|
||||
}
|
||||
|
@ -1,164 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
const fs = require('fs');
|
||||
const Liquid = require('liquidjs');
|
||||
const timestamp = require('unix-timestamp');
|
||||
const internalNginx = require('./nginx');
|
||||
const logger = require('../logger').ssl;
|
||||
const utils = require('../lib/utils');
|
||||
const error = require('../lib/error');
|
||||
|
||||
timestamp.round = true;
|
||||
|
||||
const internalSsl = {
|
||||
|
||||
interval_timeout: 1000 * 60 * 60 * 12, // 12 hours
|
||||
interval: null,
|
||||
interval_processing: false,
|
||||
|
||||
initTimer: () => {
|
||||
logger.info('Let\'s Encrypt Renewal Timer initialized');
|
||||
internalSsl.interval = setInterval(internalSsl.processExpiringHosts, internalSsl.interval_timeout);
|
||||
},
|
||||
|
||||
/**
|
||||
* Triggered by a timer, this will check for expiring hosts and renew their ssl certs if required
|
||||
*/
|
||||
processExpiringHosts: () => {
|
||||
if (!internalSsl.interval_processing) {
|
||||
logger.info('Renewing SSL certs close to expiry...');
|
||||
return utils.exec('/usr/bin/certbot renew -q')
|
||||
.then(result => {
|
||||
logger.info(result);
|
||||
internalSsl.interval_processing = false;
|
||||
|
||||
return internalNginx.reload()
|
||||
.then(() => {
|
||||
logger.info('Renew Complete');
|
||||
return result;
|
||||
});
|
||||
})
|
||||
.catch(err => {
|
||||
logger.error(err);
|
||||
internalSsl.interval_processing = false;
|
||||
});
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* @param {String} host_type
|
||||
* @param {Object} host
|
||||
* @returns {Boolean}
|
||||
*/
|
||||
hasValidSslCerts: (host_type, host) => {
|
||||
host_type = host_type.replace(new RegExp('-', 'g'), '_');
|
||||
let le_path = '/etc/letsencrypt/live/' + host_type + '-' + host.id;
|
||||
|
||||
return fs.existsSync(le_path + '/fullchain.pem') && fs.existsSync(le_path + '/privkey.pem');
|
||||
},
|
||||
|
||||
/**
|
||||
* @param {String} host_type
|
||||
* @param {Object} host
|
||||
* @returns {Promise}
|
||||
*/
|
||||
requestSsl: (host_type, host) => {
|
||||
logger.info('Requesting SSL certificates for ' + host_type + ' #' + host.id);
|
||||
|
||||
// TODO
|
||||
|
||||
return utils.exec('/usr/bin/letsencrypt certonly --agree-tos --email "' + host.letsencrypt_email + '" -n -a webroot -d "' + host.hostname + '"')
|
||||
.then(result => {
|
||||
logger.info(result);
|
||||
return result;
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* @param {String} host_type
|
||||
* @param {Object} host
|
||||
* @returns {Promise}
|
||||
*/
|
||||
renewSsl: (host_type, host) => {
|
||||
logger.info('Renewing SSL certificates for ' + host_type + ' #' + host.id);
|
||||
|
||||
// TODO
|
||||
|
||||
return utils.exec('/usr/bin/certbot renew --force-renewal --disable-hook-validation --cert-name "' + host.hostname + '"')
|
||||
.then(result => {
|
||||
logger.info(result);
|
||||
return result;
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* @param {String} host_type
|
||||
* @param {Object} host
|
||||
* @returns {Promise}
|
||||
*/
|
||||
deleteCerts: (host_type, host) => {
|
||||
logger.info('Deleting SSL certificates for ' + host_type + ' #' + host.id);
|
||||
|
||||
// TODO
|
||||
|
||||
return utils.exec('/usr/bin/certbot delete -n --cert-name "' + host.hostname + '"')
|
||||
.then(result => {
|
||||
logger.info(result);
|
||||
})
|
||||
.catch(err => {
|
||||
logger.error(err);
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* @param {String} host_type
|
||||
* @param {Object} host
|
||||
* @returns {Promise}
|
||||
*/
|
||||
generateSslSetupConfig: (host_type, host) => {
|
||||
host_type = host_type.replace(new RegExp('-', 'g'), '_');
|
||||
|
||||
let renderEngine = Liquid();
|
||||
let template = null;
|
||||
let filename = internalNginx.getConfigName(host_type, host);
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
try {
|
||||
template = fs.readFileSync(__dirname + '/../templates/letsencrypt.conf', {encoding: 'utf8'});
|
||||
} catch (err) {
|
||||
reject(new error.ConfigurationError(err.message));
|
||||
return;
|
||||
}
|
||||
|
||||
return renderEngine
|
||||
.parseAndRender(template, host)
|
||||
.then(config_text => {
|
||||
fs.writeFileSync(filename, config_text, {encoding: 'utf8'});
|
||||
return template_data;
|
||||
})
|
||||
.catch(err => {
|
||||
throw new error.ConfigurationError(err.message);
|
||||
});
|
||||
});
|
||||
},
|
||||
|
||||
/**
|
||||
* @param {String} host_type
|
||||
* @param {Object} host
|
||||
* @returns {Promise}
|
||||
*/
|
||||
configureSsl: (host_type, host) => {
|
||||
|
||||
// TODO
|
||||
|
||||
return internalSsl.generateSslSetupConfig(host)
|
||||
.then(data => {
|
||||
return internalNginx.reload()
|
||||
.then(() => {
|
||||
return internalSsl.requestSsl(data);
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = internalSsl;
|
Reference in New Issue
Block a user