Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 57e70bb

Browse files
committedDec 15, 2023
Make auto-renew use built-in renew function
1 parent e08a4d4 commit 57e70bb

File tree

1 file changed

+33
-49
lines changed

1 file changed

+33
-49
lines changed
 

‎backend/internal/certificate.js

+33-49
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const internalCertificate = {
3030
intervalTimeout: 1000 * 60 * 60, // 1 hour
3131
interval: null,
3232
intervalProcessing: false,
33+
renewBeforeExpirationBy: [7, 'days'],
3334

3435
initTimer: () => {
3536
logger.info('Let\'s Encrypt Renewal Timer initialized');
@@ -46,58 +47,41 @@ const internalCertificate = {
4647
internalCertificate.intervalProcessing = true;
4748
logger.info('Renewing SSL certs close to expiry...');
4849

49-
const cmd = certbotCommand + ' renew --non-interactive --quiet ' +
50-
'--config "' + letsencryptConfig + '" ' +
51-
'--work-dir "/tmp/letsencrypt-lib" ' +
52-
'--logs-dir "/tmp/letsencrypt-log" ' +
53-
'--preferred-challenges "dns,http" ' +
54-
'--disable-hook-validation ' +
55-
(letsencryptStaging ? '--staging' : '');
56-
57-
return utils.exec(cmd)
58-
.then((result) => {
59-
if (result) {
60-
logger.info('Renew Result: ' + result);
50+
const expirationThreshold = moment().add(internalCertificate.renewBeforeExpirationBy[0], internalCertificate.renewBeforeExpirationBy[1]).format('YYYY-MM-DD HH:mm:ss');
51+
52+
// Fetch all the letsencrypt certs from the db that will expire within 7 days
53+
certificateModel
54+
.query()
55+
.where('is_deleted', 0)
56+
.andWhere('provider', 'letsencrypt')
57+
.andWhere('expires_on', '<', expirationThreshold)
58+
.then((certificates) => {
59+
if (!certificates || !certificates.length) {
60+
return null;
6161
}
6262

63-
return internalNginx.reload()
64-
.then(() => {
65-
logger.info('Renew Complete');
66-
return result;
67-
});
68-
})
69-
.then(() => {
70-
// Now go and fetch all the letsencrypt certs from the db and query the files and update expiry times
71-
return certificateModel
72-
.query()
73-
.where('is_deleted', 0)
74-
.andWhere('provider', 'letsencrypt')
75-
.then((certificates) => {
76-
if (certificates && certificates.length) {
77-
let promises = [];
78-
79-
certificates.map(function (certificate) {
80-
promises.push(
81-
internalCertificate.getCertificateInfoFromFile('/etc/letsencrypt/live/npm-' + certificate.id + '/fullchain.pem')
82-
.then((cert_info) => {
83-
return certificateModel
84-
.query()
85-
.where('id', certificate.id)
86-
.andWhere('provider', 'letsencrypt')
87-
.patch({
88-
expires_on: moment(cert_info.dates.to, 'X').format('YYYY-MM-DD HH:mm:ss')
89-
});
90-
})
91-
.catch((err) => {
92-
// Don't want to stop the train here, just log the error
93-
logger.error(err.message);
94-
})
95-
);
96-
});
63+
let promises = [];
64+
65+
certificates.forEach(function (certificate) {
66+
const promise = internalCertificate
67+
.renew(
68+
{
69+
can: () =>
70+
Promise.resolve({
71+
permission_visibility: 'all',
72+
}),
73+
},
74+
{ id: certificate.id },
75+
)
76+
.catch((err) => {
77+
// Don't want to stop the train here, just log the error
78+
logger.error(err.message);
79+
});
9780

98-
return Promise.all(promises);
99-
}
100-
});
81+
promises.push(promise);
82+
});
83+
84+
return Promise.all(promises);
10185
})
10286
.then(() => {
10387
internalCertificate.intervalProcessing = false;

0 commit comments

Comments
 (0)
Please sign in to comment.