@@ -18,7 +18,43 @@ function omissions () {
1818
1919const internalCertificate = {
2020
21- allowed_ssl_files : [ 'certificate' , 'certificate_key' , 'intermediate_certificate' ] ,
21+ allowed_ssl_files : [ 'certificate' , 'certificate_key' , 'intermediate_certificate' ] ,
22+ interval_timeout : 1000 * 60 * 60 * 12 , // 12 hours
23+ interval : null ,
24+ interval_processing : false ,
25+
26+ initTimer : ( ) => {
27+ logger . info ( 'Let\'s Encrypt Renewal Timer initialized' ) ;
28+ internalCertificate . interval = setInterval ( internalCertificate . processExpiringHosts , internalCertificate . interval_timeout ) ;
29+ } ,
30+
31+ /**
32+ * Triggered by a timer, this will check for expiring hosts and renew their ssl certs if required
33+ */
34+ processExpiringHosts : ( ) => {
35+ let internalNginx = require ( './nginx' ) ;
36+
37+ if ( ! internalCertificate . interval_processing ) {
38+ internalCertificate . interval_processing = true ;
39+ logger . info ( 'Renewing SSL certs close to expiry...' ) ;
40+
41+ return utils . exec ( certbot_command + ' renew -q ' + ( debug_mode ? '--staging' : '' ) )
42+ . then ( result => {
43+ logger . info ( result ) ;
44+ internalCertificate . interval_processing = false ;
45+
46+ return internalNginx . reload ( )
47+ . then ( ( ) => {
48+ logger . info ( 'Renew Complete' ) ;
49+ return result ;
50+ } ) ;
51+ } )
52+ . catch ( err => {
53+ logger . error ( err ) ;
54+ internalCertificate . interval_processing = false ;
55+ } ) ;
56+ }
57+ } ,
2258
2359 /**
2460 * @param {Access } access
@@ -493,7 +529,7 @@ const internalCertificate = {
493529 * @returns {Promise }
494530 */
495531 requestLetsEncryptSsl : certificate => {
496- logger . info ( 'Requesting Let\'sEncrypt certificates for Cert #' + certificate . id + ': ' + certificate . domain_names . join ( ', ' ) ) ;
532+ logger . info ( 'Requesting Let\'sEncrypt certificates for Cert #' + certificate . id + ': ' + certificate . domain_names . join ( ', ' ) ) ;
497533
498534 return utils . exec ( certbot_command + ' certonly --cert-name "npm-' + certificate . id + '" --agree-tos ' +
499535 '--email "' + certificate . meta . letsencrypt_email + '" ' +
@@ -511,14 +547,24 @@ const internalCertificate = {
511547 * @returns {Promise }
512548 */
513549 renewLetsEncryptSsl : certificate => {
514- logger . info ( 'Renewing Let\'sEncrypt certificates for Cert #' + certificate . id + ': ' + certificate . domain_names . join ( ', ' ) ) ;
550+ logger . info ( 'Renewing Let\'sEncrypt certificates for Cert #' + certificate . id + ': ' + certificate . domain_names . join ( ', ' ) ) ;
515551
516552 return utils . exec ( certbot_command + ' renew -n --force-renewal --disable-hook-validation --cert-name "npm-' + certificate . id + '" ' + ( debug_mode ? '--staging' : '' ) )
517553 . then ( result => {
518554 logger . info ( result ) ;
519555 return result ;
520556 } ) ;
521557 } ,
558+
559+ /**
560+ * @param {Object } certificate
561+ * @returns {Boolean }
562+ */
563+ hasLetsEncryptSslCerts : certificate => {
564+ let le_path = '/etc/letsencrypt/live/npm-' + certificate . id ;
565+
566+ return fs . existsSync ( le_path + '/fullchain.pem' ) && fs . existsSync ( le_path + '/privkey.pem' ) ;
567+ }
522568} ;
523569
524570module . exports = internalCertificate ;
0 commit comments