|
2 | 2 |
|
3 | 3 | const fs = require('fs');
|
4 | 4 | const _ = require('lodash');
|
| 5 | +const logger = require('../logger').ssl; |
5 | 6 | const error = require('../lib/error');
|
6 | 7 | const certificateModel = require('../models/certificate');
|
7 | 8 | const internalAuditLog = require('./audit-log');
|
8 | 9 | const tempWrite = require('temp-write');
|
9 | 10 | const utils = require('../lib/utils');
|
10 | 11 | const moment = require('moment');
|
| 12 | +const debug_mode = process.env.NODE_ENV !== 'production'; |
| 13 | +const certbot_command = '/usr/bin/certbot'; |
11 | 14 |
|
12 | 15 | function omissions () {
|
13 | 16 | return ['is_deleted'];
|
@@ -483,7 +486,39 @@ const internalCertificate = {
|
483 | 486 | }
|
484 | 487 | });
|
485 | 488 | return meta;
|
486 |
| - } |
| 489 | + }, |
| 490 | + |
| 491 | + /** |
| 492 | + * @param {Object} certificate the certificate row |
| 493 | + * @returns {Promise} |
| 494 | + */ |
| 495 | + requestLetsEncryptSsl: certificate => { |
| 496 | + logger.info('Requesting Let\'sEncrypt certificates for Cert #' + certificate.id + ': ' + certificate.domain_names.join(', ')); |
| 497 | + |
| 498 | + return utils.exec(certbot_command + ' certonly --cert-name "npm-' + certificate.id + '" --agree-tos ' + |
| 499 | + '--email "' + certificate.meta.letsencrypt_email + '" ' + |
| 500 | + '--preferred-challenges "http" ' + |
| 501 | + '-n -a webroot -d "' + certificate.domain_names.join(',') + '" ' + |
| 502 | + (debug_mode ? '--staging' : '')) |
| 503 | + .then(result => { |
| 504 | + logger.info(result); |
| 505 | + return result; |
| 506 | + }); |
| 507 | + }, |
| 508 | + |
| 509 | + /** |
| 510 | + * @param {Object} certificate the certificate row |
| 511 | + * @returns {Promise} |
| 512 | + */ |
| 513 | + renewLetsEncryptSsl: certificate => { |
| 514 | + logger.info('Renewing Let\'sEncrypt certificates for Cert #' + certificate.id + ': ' + certificate.domain_names.join(', ')); |
| 515 | + |
| 516 | + return utils.exec(certbot_command + ' renew -n --force-renewal --disable-hook-validation --cert-name "npm-' + certificate.id + '" ' + (debug_mode ? '--staging' : '')) |
| 517 | + .then(result => { |
| 518 | + logger.info(result); |
| 519 | + return result; |
| 520 | + }); |
| 521 | + }, |
487 | 522 | };
|
488 | 523 |
|
489 | 524 | module.exports = internalCertificate;
|
0 commit comments