Make auto-renew use built-in renew function

This commit is contained in:
Stephen Crosby 2023-12-14 16:21:08 -08:00
parent e08a4d4490
commit fe4bd9fed6

View File

@ -26,10 +26,11 @@ function omissions() {
const internalCertificate = { const internalCertificate = {
allowedSslFiles: ['certificate', 'certificate_key', 'intermediate_certificate'], allowedSslFiles: ['certificate', 'certificate_key', 'intermediate_certificate'],
intervalTimeout: 1000 * 60 * 60, // 1 hour intervalTimeout: 1000 * 60 * 60, // 1 hour
interval: null, interval: null,
intervalProcessing: false, intervalProcessing: false,
renewBeforeExpirationBy: [7, 'days'],
initTimer: () => { initTimer: () => {
logger.info('Let\'s Encrypt Renewal Timer initialized'); logger.info('Let\'s Encrypt Renewal Timer initialized');
@ -46,58 +47,41 @@ const internalCertificate = {
internalCertificate.intervalProcessing = true; internalCertificate.intervalProcessing = true;
logger.info('Renewing SSL certs close to expiry...'); logger.info('Renewing SSL certs close to expiry...');
const cmd = certbotCommand + ' renew --non-interactive --quiet ' + const expirationThreshold = moment().add(internalCertificate.renewBeforeExpirationBy[0], internalCertificate.renewBeforeExpirationBy[1]).format('YYYY-MM-DD HH:mm:ss');
'--config "' + letsencryptConfig + '" ' +
'--work-dir "/tmp/letsencrypt-lib" ' +
'--logs-dir "/tmp/letsencrypt-log" ' +
'--preferred-challenges "dns,http" ' +
'--disable-hook-validation ' +
(letsencryptStaging ? '--staging' : '');
return utils.exec(cmd) // Fetch all the letsencrypt certs from the db that will expire within 7 days
.then((result) => { certificateModel
if (result) { .query()
logger.info('Renew Result: ' + result); .where('is_deleted', 0)
.andWhere('provider', 'letsencrypt')
.andWhere('expires_on', '<', expirationThreshold)
.then((certificates) => {
if (!certificates || !certificates.length) {
return null;
} }
return internalNginx.reload() let promises = [];
.then(() => {
logger.info('Renew Complete');
return result;
});
})
.then(() => {
// Now go and fetch all the letsencrypt certs from the db and query the files and update expiry times
return certificateModel
.query()
.where('is_deleted', 0)
.andWhere('provider', 'letsencrypt')
.then((certificates) => {
if (certificates && certificates.length) {
let promises = [];
certificates.map(function (certificate) { certificates.forEach(function (certificate) {
promises.push( const promise = internalCertificate
internalCertificate.getCertificateInfoFromFile('/etc/letsencrypt/live/npm-' + certificate.id + '/fullchain.pem') .renew(
.then((cert_info) => { {
return certificateModel can: () =>
.query() Promise.resolve({
.where('id', certificate.id) permission_visibility: 'all',
.andWhere('provider', 'letsencrypt') }),
.patch({ },
expires_on: moment(cert_info.dates.to, 'X').format('YYYY-MM-DD HH:mm:ss') { id: certificate.id },
}); )
}) .catch((err) => {
.catch((err) => { // Don't want to stop the train here, just log the error
// Don't want to stop the train here, just log the error logger.error(err.message);
logger.error(err.message); });
})
);
});
return Promise.all(promises); promises.push(promise);
} });
});
return Promise.all(promises);
}) })
.then(() => { .then(() => {
internalCertificate.intervalProcessing = false; internalCertificate.intervalProcessing = false;