certificate.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119
  1. const _ = require('lodash');
  2. const fs = require('fs');
  3. const tempWrite = require('temp-write');
  4. const moment = require('moment');
  5. const logger = require('../logger').ssl;
  6. const error = require('../lib/error');
  7. const utils = require('../lib/utils');
  8. const certificateModel = require('../models/certificate');
  9. const dnsPlugins = require('../global/certbot-dns-plugins');
  10. const internalAuditLog = require('./audit-log');
  11. const internalNginx = require('./nginx');
  12. const internalHost = require('./host');
  13. const letsencryptStaging = process.env.NODE_ENV !== 'production';
  14. const letsencryptConfig = '/etc/letsencrypt.ini';
  15. const certbotCommand = 'certbot';
  16. const archiver = require('archiver');
  17. function omissions() {
  18. return ['is_deleted'];
  19. }
  20. const internalCertificate = {
  21. allowedSslFiles: ['certificate', 'certificate_key', 'intermediate_certificate'],
  22. intervalTimeout: 1000 * 60 * 60, // 1 hour
  23. interval: null,
  24. intervalProcessing: false,
  25. initTimer: () => {
  26. logger.info('Let\'s Encrypt Renewal Timer initialized');
  27. internalCertificate.interval = setInterval(internalCertificate.processExpiringHosts, internalCertificate.intervalTimeout);
  28. // And do this now as well
  29. internalCertificate.processExpiringHosts();
  30. },
  31. /**
  32. * Triggered by a timer, this will check for expiring hosts and renew their ssl certs if required
  33. */
  34. processExpiringHosts: () => {
  35. if (!internalCertificate.intervalProcessing) {
  36. internalCertificate.intervalProcessing = true;
  37. logger.info('Renewing SSL certs close to expiry...');
  38. const cmd = certbotCommand + ' renew --non-interactive --quiet ' +
  39. '--config "' + letsencryptConfig + '" ' +
  40. '--preferred-challenges "dns,http" ' +
  41. '--disable-hook-validation ' +
  42. (letsencryptStaging ? '--staging' : '');
  43. return utils.exec(cmd)
  44. .then((result) => {
  45. if (result) {
  46. logger.info('Renew Result: ' + result);
  47. }
  48. return internalNginx.reload()
  49. .then(() => {
  50. logger.info('Renew Complete');
  51. return result;
  52. });
  53. })
  54. .then(() => {
  55. // Now go and fetch all the letsencrypt certs from the db and query the files and update expiry times
  56. return certificateModel
  57. .query()
  58. .where('is_deleted', 0)
  59. .andWhere('provider', 'letsencrypt')
  60. .then((certificates) => {
  61. if (certificates && certificates.length) {
  62. let promises = [];
  63. certificates.map(function (certificate) {
  64. promises.push(
  65. internalCertificate.getCertificateInfoFromFile('/etc/letsencrypt/live/npm-' + certificate.id + '/fullchain.pem')
  66. .then((cert_info) => {
  67. return certificateModel
  68. .query()
  69. .where('id', certificate.id)
  70. .andWhere('provider', 'letsencrypt')
  71. .patch({
  72. expires_on: moment(cert_info.dates.to, 'X').format('YYYY-MM-DD HH:mm:ss')
  73. });
  74. })
  75. .catch((err) => {
  76. // Don't want to stop the train here, just log the error
  77. logger.error(err.message);
  78. })
  79. );
  80. });
  81. return Promise.all(promises);
  82. }
  83. });
  84. })
  85. .then(() => {
  86. internalCertificate.intervalProcessing = false;
  87. })
  88. .catch((err) => {
  89. logger.error(err);
  90. internalCertificate.intervalProcessing = false;
  91. });
  92. }
  93. },
  94. /**
  95. * @param {Access} access
  96. * @param {Object} data
  97. * @returns {Promise}
  98. */
  99. create: (access, data) => {
  100. return access.can('certificates:create', data)
  101. .then(() => {
  102. data.owner_user_id = access.token.getUserId(1);
  103. if (data.provider === 'letsencrypt') {
  104. data.nice_name = data.domain_names.sort().join(', ');
  105. }
  106. return certificateModel
  107. .query()
  108. .omit(omissions())
  109. .insertAndFetch(data);
  110. })
  111. .then((certificate) => {
  112. if (certificate.provider === 'letsencrypt') {
  113. // Request a new Cert from LE. Let the fun begin.
  114. // 1. Find out any hosts that are using any of the hostnames in this cert
  115. // 2. Disable them in nginx temporarily
  116. // 3. Generate the LE config
  117. // 4. Request cert
  118. // 5. Remove LE config
  119. // 6. Re-instate previously disabled hosts
  120. // 1. Find out any hosts that are using any of the hostnames in this cert
  121. return internalHost.getHostsWithDomains(certificate.domain_names)
  122. .then((in_use_result) => {
  123. // 2. Disable them in nginx temporarily
  124. return internalCertificate.disableInUseHosts(in_use_result)
  125. .then(() => {
  126. return in_use_result;
  127. });
  128. })
  129. .then((in_use_result) => {
  130. // With DNS challenge no config is needed, so skip 3 and 5.
  131. if (certificate.meta.dns_challenge) {
  132. return internalNginx.reload().then(() => {
  133. // 4. Request cert
  134. return internalCertificate.requestLetsEncryptSslWithDnsChallenge(certificate);
  135. })
  136. .then(internalNginx.reload)
  137. .then(() => {
  138. // 6. Re-instate previously disabled hosts
  139. return internalCertificate.enableInUseHosts(in_use_result);
  140. })
  141. .then(() => {
  142. return certificate;
  143. })
  144. .catch((err) => {
  145. // In the event of failure, revert things and throw err back
  146. return internalCertificate.enableInUseHosts(in_use_result)
  147. .then(internalNginx.reload)
  148. .then(() => {
  149. throw err;
  150. });
  151. });
  152. } else {
  153. // 3. Generate the LE config
  154. return internalNginx.generateLetsEncryptRequestConfig(certificate)
  155. .then(internalNginx.reload)
  156. .then(() => {
  157. // 4. Request cert
  158. return internalCertificate.requestLetsEncryptSsl(certificate);
  159. })
  160. .then(() => {
  161. // 5. Remove LE config
  162. return internalNginx.deleteLetsEncryptRequestConfig(certificate);
  163. })
  164. .then(internalNginx.reload)
  165. .then(() => {
  166. // 6. Re-instate previously disabled hosts
  167. return internalCertificate.enableInUseHosts(in_use_result);
  168. })
  169. .then(() => {
  170. return certificate;
  171. })
  172. .catch((err) => {
  173. // In the event of failure, revert things and throw err back
  174. return internalNginx.deleteLetsEncryptRequestConfig(certificate)
  175. .then(() => {
  176. return internalCertificate.enableInUseHosts(in_use_result);
  177. })
  178. .then(internalNginx.reload)
  179. .then(() => {
  180. throw err;
  181. });
  182. });
  183. }
  184. })
  185. .then(() => {
  186. // At this point, the letsencrypt cert should exist on disk.
  187. // Lets get the expiry date from the file and update the row silently
  188. return internalCertificate.getCertificateInfoFromFile('/etc/letsencrypt/live/npm-' + certificate.id + '/fullchain.pem')
  189. .then((cert_info) => {
  190. return certificateModel
  191. .query()
  192. .patchAndFetchById(certificate.id, {
  193. expires_on: moment(cert_info.dates.to, 'X').format('YYYY-MM-DD HH:mm:ss')
  194. })
  195. .then((saved_row) => {
  196. // Add cert data for audit log
  197. saved_row.meta = _.assign({}, saved_row.meta, {
  198. letsencrypt_certificate: cert_info
  199. });
  200. return saved_row;
  201. });
  202. });
  203. }).catch(async (error) => {
  204. // Delete the certificate from the database if it was not created successfully
  205. await certificateModel
  206. .query()
  207. .deleteById(certificate.id);
  208. throw error;
  209. });
  210. } else {
  211. return certificate;
  212. }
  213. }).then((certificate) => {
  214. data.meta = _.assign({}, data.meta || {}, certificate.meta);
  215. // Add to audit log
  216. return internalAuditLog.add(access, {
  217. action: 'created',
  218. object_type: 'certificate',
  219. object_id: certificate.id,
  220. meta: data
  221. })
  222. .then(() => {
  223. return certificate;
  224. });
  225. });
  226. },
  227. /**
  228. * @param {Access} access
  229. * @param {Object} data
  230. * @param {Number} data.id
  231. * @param {String} [data.email]
  232. * @param {String} [data.name]
  233. * @return {Promise}
  234. */
  235. update: (access, data) => {
  236. return access.can('certificates:update', data.id)
  237. .then((/*access_data*/) => {
  238. return internalCertificate.get(access, {id: data.id});
  239. })
  240. .then((row) => {
  241. if (row.id !== data.id) {
  242. // Sanity check that something crazy hasn't happened
  243. throw new error.InternalValidationError('Certificate could not be updated, IDs do not match: ' + row.id + ' !== ' + data.id);
  244. }
  245. return certificateModel
  246. .query()
  247. .omit(omissions())
  248. .patchAndFetchById(row.id, data)
  249. .then((saved_row) => {
  250. saved_row.meta = internalCertificate.cleanMeta(saved_row.meta);
  251. data.meta = internalCertificate.cleanMeta(data.meta);
  252. // Add row.nice_name for custom certs
  253. if (saved_row.provider === 'other') {
  254. data.nice_name = saved_row.nice_name;
  255. }
  256. // Add to audit log
  257. return internalAuditLog.add(access, {
  258. action: 'updated',
  259. object_type: 'certificate',
  260. object_id: row.id,
  261. meta: _.omit(data, ['expires_on']) // this prevents json circular reference because expires_on might be raw
  262. })
  263. .then(() => {
  264. return _.omit(saved_row, omissions());
  265. });
  266. });
  267. });
  268. },
  269. /**
  270. * @param {Access} access
  271. * @param {Object} data
  272. * @param {Number} data.id
  273. * @param {Array} [data.expand]
  274. * @param {Array} [data.omit]
  275. * @return {Promise}
  276. */
  277. get: (access, data) => {
  278. if (typeof data === 'undefined') {
  279. data = {};
  280. }
  281. return access.can('certificates:get', data.id)
  282. .then((access_data) => {
  283. let query = certificateModel
  284. .query()
  285. .where('is_deleted', 0)
  286. .andWhere('id', data.id)
  287. .allowEager('[owner]')
  288. .first();
  289. if (access_data.permission_visibility !== 'all') {
  290. query.andWhere('owner_user_id', access.token.getUserId(1));
  291. }
  292. // Custom omissions
  293. if (typeof data.omit !== 'undefined' && data.omit !== null) {
  294. query.omit(data.omit);
  295. }
  296. if (typeof data.expand !== 'undefined' && data.expand !== null) {
  297. query.eager('[' + data.expand.join(', ') + ']');
  298. }
  299. return query;
  300. })
  301. .then((row) => {
  302. if (row) {
  303. return _.omit(row, omissions());
  304. } else {
  305. throw new error.ItemNotFoundError(data.id);
  306. }
  307. });
  308. },
  309. /**
  310. * @param {Access} access
  311. * @param {Object} data
  312. * @param {Number} data.id
  313. * @returns {Promise}
  314. */
  315. download: (access, data) => {
  316. return new Promise((resolve, reject) => {
  317. access.can('certificates:get', data)
  318. .then(() => {
  319. return internalCertificate.get(access, data);
  320. })
  321. .then((certificate) => {
  322. if (certificate.provider === 'letsencrypt') {
  323. const zipDirectory = '/etc/letsencrypt/live/npm-' + data.id;
  324. if (!fs.existsSync(zipDirectory)) {
  325. throw new error.ItemNotFoundError('Certificate ' + certificate.nice_name + ' does not exists');
  326. }
  327. const downloadName = 'npm-' + data.id + '-' + `${Date.now()}.zip`;
  328. const opName = '/tmp/' + downloadName;
  329. internalCertificate.zipDirectory(zipDirectory, opName)
  330. .then(() => {
  331. logger.debug('zip completed : ', opName);
  332. const resp = {
  333. fileName: opName
  334. };
  335. resolve(resp);
  336. }).catch((err) => {
  337. reject(err);
  338. });
  339. } else {
  340. throw new error.ValidationError('Only Let\'sEncrypt certificates can be renewed');
  341. }
  342. }).catch((err) => reject(err));
  343. });
  344. },
  345. /**
  346. * @param {String} source
  347. * @param {String} out
  348. * @returns {Promise}
  349. */
  350. zipDirectory(source, out) {
  351. const archive = archiver('zip', { zlib: { level: 9 } });
  352. const stream = fs.createWriteStream(out);
  353. return new Promise((resolve, reject) => {
  354. archive
  355. .directory(source, false)
  356. .on('error', (err) => reject(err))
  357. .pipe(stream);
  358. stream.on('close', () => resolve());
  359. archive.finalize();
  360. });
  361. },
  362. /**
  363. * @param {Access} access
  364. * @param {Object} data
  365. * @param {Number} data.id
  366. * @param {String} [data.reason]
  367. * @returns {Promise}
  368. */
  369. delete: (access, data) => {
  370. return access.can('certificates:delete', data.id)
  371. .then(() => {
  372. return internalCertificate.get(access, {id: data.id});
  373. })
  374. .then((row) => {
  375. if (!row) {
  376. throw new error.ItemNotFoundError(data.id);
  377. }
  378. return certificateModel
  379. .query()
  380. .where('id', row.id)
  381. .patch({
  382. is_deleted: 1
  383. })
  384. .then(() => {
  385. // Add to audit log
  386. row.meta = internalCertificate.cleanMeta(row.meta);
  387. return internalAuditLog.add(access, {
  388. action: 'deleted',
  389. object_type: 'certificate',
  390. object_id: row.id,
  391. meta: _.omit(row, omissions())
  392. });
  393. })
  394. .then(() => {
  395. if (row.provider === 'letsencrypt') {
  396. // Revoke the cert
  397. return internalCertificate.revokeLetsEncryptSsl(row);
  398. }
  399. });
  400. })
  401. .then(() => {
  402. return true;
  403. });
  404. },
  405. /**
  406. * All Certs
  407. *
  408. * @param {Access} access
  409. * @param {Array} [expand]
  410. * @param {String} [search_query]
  411. * @returns {Promise}
  412. */
  413. getAll: (access, expand, search_query) => {
  414. return access.can('certificates:list')
  415. .then((access_data) => {
  416. let query = certificateModel
  417. .query()
  418. .where('is_deleted', 0)
  419. .groupBy('id')
  420. .omit(['is_deleted'])
  421. .allowEager('[owner]')
  422. .orderBy('nice_name', 'ASC');
  423. if (access_data.permission_visibility !== 'all') {
  424. query.andWhere('owner_user_id', access.token.getUserId(1));
  425. }
  426. // Query is used for searching
  427. if (typeof search_query === 'string') {
  428. query.where(function () {
  429. this.where('name', 'like', '%' + search_query + '%');
  430. });
  431. }
  432. if (typeof expand !== 'undefined' && expand !== null) {
  433. query.eager('[' + expand.join(', ') + ']');
  434. }
  435. return query;
  436. });
  437. },
  438. /**
  439. * Report use
  440. *
  441. * @param {Number} user_id
  442. * @param {String} visibility
  443. * @returns {Promise}
  444. */
  445. getCount: (user_id, visibility) => {
  446. let query = certificateModel
  447. .query()
  448. .count('id as count')
  449. .where('is_deleted', 0);
  450. if (visibility !== 'all') {
  451. query.andWhere('owner_user_id', user_id);
  452. }
  453. return query.first()
  454. .then((row) => {
  455. return parseInt(row.count, 10);
  456. });
  457. },
  458. /**
  459. * @param {Object} certificate
  460. * @returns {Promise}
  461. */
  462. writeCustomCert: (certificate) => {
  463. logger.info('Writing Custom Certificate:', certificate);
  464. const dir = '/data/custom_ssl/npm-' + certificate.id;
  465. return new Promise((resolve, reject) => {
  466. if (certificate.provider === 'letsencrypt') {
  467. reject(new Error('Refusing to write letsencrypt certs here'));
  468. return;
  469. }
  470. let certData = certificate.meta.certificate;
  471. if (typeof certificate.meta.intermediate_certificate !== 'undefined') {
  472. certData = certData + '\n' + certificate.meta.intermediate_certificate;
  473. }
  474. try {
  475. if (!fs.existsSync(dir)) {
  476. fs.mkdirSync(dir);
  477. }
  478. } catch (err) {
  479. reject(err);
  480. return;
  481. }
  482. fs.writeFile(dir + '/fullchain.pem', certData, function (err) {
  483. if (err) {
  484. reject(err);
  485. } else {
  486. resolve();
  487. }
  488. });
  489. })
  490. .then(() => {
  491. return new Promise((resolve, reject) => {
  492. fs.writeFile(dir + '/privkey.pem', certificate.meta.certificate_key, function (err) {
  493. if (err) {
  494. reject(err);
  495. } else {
  496. resolve();
  497. }
  498. });
  499. });
  500. });
  501. },
  502. /**
  503. * @param {Access} access
  504. * @param {Object} data
  505. * @param {Array} data.domain_names
  506. * @param {String} data.meta.letsencrypt_email
  507. * @param {Boolean} data.meta.letsencrypt_agree
  508. * @returns {Promise}
  509. */
  510. createQuickCertificate: (access, data) => {
  511. return internalCertificate.create(access, {
  512. provider: 'letsencrypt',
  513. domain_names: data.domain_names,
  514. meta: data.meta
  515. });
  516. },
  517. /**
  518. * Validates that the certs provided are good.
  519. * No access required here, nothing is changed or stored.
  520. *
  521. * @param {Object} data
  522. * @param {Object} data.files
  523. * @returns {Promise}
  524. */
  525. validate: (data) => {
  526. return new Promise((resolve) => {
  527. // Put file contents into an object
  528. let files = {};
  529. _.map(data.files, (file, name) => {
  530. if (internalCertificate.allowedSslFiles.indexOf(name) !== -1) {
  531. files[name] = file.data.toString();
  532. }
  533. });
  534. resolve(files);
  535. })
  536. .then((files) => {
  537. // For each file, create a temp file and write the contents to it
  538. // Then test it depending on the file type
  539. let promises = [];
  540. _.map(files, (content, type) => {
  541. promises.push(new Promise((resolve) => {
  542. if (type === 'certificate_key') {
  543. resolve(internalCertificate.checkPrivateKey(content));
  544. } else {
  545. // this should handle `certificate` and intermediate certificate
  546. resolve(internalCertificate.getCertificateInfo(content, true));
  547. }
  548. }).then((res) => {
  549. return {[type]: res};
  550. }));
  551. });
  552. return Promise.all(promises)
  553. .then((files) => {
  554. let data = {};
  555. _.each(files, (file) => {
  556. data = _.assign({}, data, file);
  557. });
  558. return data;
  559. });
  560. });
  561. },
  562. /**
  563. * @param {Access} access
  564. * @param {Object} data
  565. * @param {Number} data.id
  566. * @param {Object} data.files
  567. * @returns {Promise}
  568. */
  569. upload: (access, data) => {
  570. return internalCertificate.get(access, {id: data.id})
  571. .then((row) => {
  572. if (row.provider !== 'other') {
  573. throw new error.ValidationError('Cannot upload certificates for this type of provider');
  574. }
  575. return internalCertificate.validate(data)
  576. .then((validations) => {
  577. if (typeof validations.certificate === 'undefined') {
  578. throw new error.ValidationError('Certificate file was not provided');
  579. }
  580. _.map(data.files, (file, name) => {
  581. if (internalCertificate.allowedSslFiles.indexOf(name) !== -1) {
  582. row.meta[name] = file.data.toString();
  583. }
  584. });
  585. // TODO: This uses a mysql only raw function that won't translate to postgres
  586. return internalCertificate.update(access, {
  587. id: data.id,
  588. expires_on: moment(validations.certificate.dates.to, 'X').format('YYYY-MM-DD HH:mm:ss'),
  589. domain_names: [validations.certificate.cn],
  590. meta: _.clone(row.meta) // Prevent the update method from changing this value that we'll use later
  591. })
  592. .then((certificate) => {
  593. console.log('ROWMETA:', row.meta);
  594. certificate.meta = row.meta;
  595. return internalCertificate.writeCustomCert(certificate);
  596. });
  597. })
  598. .then(() => {
  599. return _.pick(row.meta, internalCertificate.allowedSslFiles);
  600. });
  601. });
  602. },
  603. /**
  604. * Uses the openssl command to validate the private key.
  605. * It will save the file to disk first, then run commands on it, then delete the file.
  606. *
  607. * @param {String} private_key This is the entire key contents as a string
  608. */
  609. checkPrivateKey: (private_key) => {
  610. return tempWrite(private_key, '/tmp')
  611. .then((filepath) => {
  612. return new Promise((resolve, reject) => {
  613. const failTimeout = setTimeout(() => {
  614. reject(new error.ValidationError('Result Validation Error: Validation timed out. This could be due to the key being passphrase-protected.'));
  615. }, 10000);
  616. utils
  617. .exec('openssl pkey -in ' + filepath + ' -check -noout 2>&1 ')
  618. .then((result) => {
  619. clearTimeout(failTimeout);
  620. if (!result.toLowerCase().includes('key is valid')) {
  621. reject(new error.ValidationError('Result Validation Error: ' + result));
  622. }
  623. fs.unlinkSync(filepath);
  624. resolve(true);
  625. })
  626. .catch((err) => {
  627. clearTimeout(failTimeout);
  628. fs.unlinkSync(filepath);
  629. reject(new error.ValidationError('Certificate Key is not valid (' + err.message + ')', err));
  630. });
  631. });
  632. });
  633. },
  634. /**
  635. * Uses the openssl command to both validate and get info out of the certificate.
  636. * It will save the file to disk first, then run commands on it, then delete the file.
  637. *
  638. * @param {String} certificate This is the entire cert contents as a string
  639. * @param {Boolean} [throw_expired] Throw when the certificate is out of date
  640. */
  641. getCertificateInfo: (certificate, throw_expired) => {
  642. return tempWrite(certificate, '/tmp')
  643. .then((filepath) => {
  644. return internalCertificate.getCertificateInfoFromFile(filepath, throw_expired)
  645. .then((certData) => {
  646. fs.unlinkSync(filepath);
  647. return certData;
  648. }).catch((err) => {
  649. fs.unlinkSync(filepath);
  650. throw err;
  651. });
  652. });
  653. },
  654. /**
  655. * Uses the openssl command to both validate and get info out of the certificate.
  656. * It will save the file to disk first, then run commands on it, then delete the file.
  657. *
  658. * @param {String} certificate_file The file location on disk
  659. * @param {Boolean} [throw_expired] Throw when the certificate is out of date
  660. */
  661. getCertificateInfoFromFile: (certificate_file, throw_expired) => {
  662. let certData = {};
  663. return utils.exec('openssl x509 -in ' + certificate_file + ' -subject -noout')
  664. .then((result) => {
  665. // subject=CN = something.example.com
  666. const regex = /(?:subject=)?[^=]+=\s+(\S+)/gim;
  667. const match = regex.exec(result);
  668. if (typeof match[1] === 'undefined') {
  669. throw new error.ValidationError('Could not determine subject from certificate: ' + result);
  670. }
  671. certData['cn'] = match[1];
  672. })
  673. .then(() => {
  674. return utils.exec('openssl x509 -in ' + certificate_file + ' -issuer -noout');
  675. })
  676. .then((result) => {
  677. // issuer=C = US, O = Let's Encrypt, CN = Let's Encrypt Authority X3
  678. const regex = /^(?:issuer=)?(.*)$/gim;
  679. const match = regex.exec(result);
  680. if (typeof match[1] === 'undefined') {
  681. throw new error.ValidationError('Could not determine issuer from certificate: ' + result);
  682. }
  683. certData['issuer'] = match[1];
  684. })
  685. .then(() => {
  686. return utils.exec('openssl x509 -in ' + certificate_file + ' -dates -noout');
  687. })
  688. .then((result) => {
  689. // notBefore=Jul 14 04:04:29 2018 GMT
  690. // notAfter=Oct 12 04:04:29 2018 GMT
  691. let validFrom = null;
  692. let validTo = null;
  693. const lines = result.split('\n');
  694. lines.map(function (str) {
  695. const regex = /^(\S+)=(.*)$/gim;
  696. const match = regex.exec(str.trim());
  697. if (match && typeof match[2] !== 'undefined') {
  698. const date = parseInt(moment(match[2], 'MMM DD HH:mm:ss YYYY z').format('X'), 10);
  699. if (match[1].toLowerCase() === 'notbefore') {
  700. validFrom = date;
  701. } else if (match[1].toLowerCase() === 'notafter') {
  702. validTo = date;
  703. }
  704. }
  705. });
  706. if (!validFrom || !validTo) {
  707. throw new error.ValidationError('Could not determine dates from certificate: ' + result);
  708. }
  709. if (throw_expired && validTo < parseInt(moment().format('X'), 10)) {
  710. throw new error.ValidationError('Certificate has expired');
  711. }
  712. certData['dates'] = {
  713. from: validFrom,
  714. to: validTo
  715. };
  716. return certData;
  717. }).catch((err) => {
  718. throw new error.ValidationError('Certificate is not valid (' + err.message + ')', err);
  719. });
  720. },
  721. /**
  722. * Cleans the ssl keys from the meta object and sets them to "true"
  723. *
  724. * @param {Object} meta
  725. * @param {Boolean} [remove]
  726. * @returns {Object}
  727. */
  728. cleanMeta: function (meta, remove) {
  729. internalCertificate.allowedSslFiles.map((key) => {
  730. if (typeof meta[key] !== 'undefined' && meta[key]) {
  731. if (remove) {
  732. delete meta[key];
  733. } else {
  734. meta[key] = true;
  735. }
  736. }
  737. });
  738. return meta;
  739. },
  740. /**
  741. * Request a certificate using the http challenge
  742. * @param {Object} certificate the certificate row
  743. * @returns {Promise}
  744. */
  745. requestLetsEncryptSsl: (certificate) => {
  746. logger.info('Requesting Let\'sEncrypt certificates for Cert #' + certificate.id + ': ' + certificate.domain_names.join(', '));
  747. const cmd = certbotCommand + ' certonly --non-interactive ' +
  748. '--config "' + letsencryptConfig + '" ' +
  749. '--cert-name "npm-' + certificate.id + '" ' +
  750. '--agree-tos ' +
  751. '--authenticator webroot ' +
  752. '--email "' + certificate.meta.letsencrypt_email + '" ' +
  753. '--preferred-challenges "dns,http" ' +
  754. '--domains "' + certificate.domain_names.join(',') + '" ' +
  755. (letsencryptStaging ? '--staging' : '');
  756. logger.info('Command:', cmd);
  757. return utils.exec(cmd)
  758. .then((result) => {
  759. logger.success(result);
  760. return result;
  761. });
  762. },
  763. /**
  764. * @param {Object} certificate the certificate row
  765. * @param {String} dns_provider the dns provider name (key used in `certbot-dns-plugins.js`)
  766. * @param {String | null} credentials the content of this providers credentials file
  767. * @param {String} propagation_seconds the cloudflare api token
  768. * @returns {Promise}
  769. */
  770. requestLetsEncryptSslWithDnsChallenge: (certificate) => {
  771. const dns_plugin = dnsPlugins[certificate.meta.dns_provider];
  772. if (!dns_plugin) {
  773. throw Error(`Unknown DNS provider '${certificate.meta.dns_provider}'`);
  774. }
  775. logger.info(`Requesting Let'sEncrypt certificates via ${dns_plugin.display_name} for Cert #${certificate.id}: ${certificate.domain_names.join(', ')}`);
  776. const credentialsLocation = '/etc/letsencrypt/credentials/credentials-' + certificate.id;
  777. const credentialsCmd = 'mkdir -p /etc/letsencrypt/credentials 2> /dev/null; echo \'' + certificate.meta.dns_provider_credentials.replace('\'', '\\\'') + '\' > \'' + credentialsLocation + '\' && chmod 600 \'' + credentialsLocation + '\'';
  778. const prepareCmd = 'pip install ' + dns_plugin.package_name + '==' + dns_plugin.package_version + ' ' + dns_plugin.dependencies;
  779. // Whether the plugin has a --<name>-credentials argument
  780. const hasConfigArg = certificate.meta.dns_provider !== 'route53';
  781. let mainCmd = certbotCommand + ' certonly --non-interactive ' +
  782. '--cert-name "npm-' + certificate.id + '" ' +
  783. '--agree-tos ' +
  784. '--email "' + certificate.meta.letsencrypt_email + '" ' +
  785. '--domains "' + certificate.domain_names.join(',') + '" ' +
  786. '--authenticator ' + dns_plugin.full_plugin_name + ' ' +
  787. (
  788. hasConfigArg
  789. ? '--' + dns_plugin.full_plugin_name + '-credentials "' + credentialsLocation + '"'
  790. : ''
  791. ) +
  792. (
  793. certificate.meta.propagation_seconds !== undefined
  794. ? ' --' + dns_plugin.full_plugin_name + '-propagation-seconds ' + certificate.meta.propagation_seconds
  795. : ''
  796. ) +
  797. (letsencryptStaging ? ' --staging' : '');
  798. // Prepend the path to the credentials file as an environment variable
  799. if (certificate.meta.dns_provider === 'route53') {
  800. mainCmd = 'AWS_CONFIG_FILE=\'' + credentialsLocation + '\' ' + mainCmd;
  801. }
  802. logger.info('Command:', `${credentialsCmd} && ${prepareCmd} && ${mainCmd}`);
  803. return utils.exec(credentialsCmd)
  804. .then(() => {
  805. return utils.exec(prepareCmd)
  806. .then(() => {
  807. return utils.exec(mainCmd)
  808. .then(async (result) => {
  809. logger.info(result);
  810. return result;
  811. });
  812. });
  813. }).catch(async (err) => {
  814. // Don't fail if file does not exist
  815. const delete_credentialsCmd = `rm -f '${credentialsLocation}' || true`;
  816. await utils.exec(delete_credentialsCmd);
  817. throw err;
  818. });
  819. },
  820. /**
  821. * @param {Access} access
  822. * @param {Object} data
  823. * @param {Number} data.id
  824. * @returns {Promise}
  825. */
  826. renew: (access, data) => {
  827. return access.can('certificates:update', data)
  828. .then(() => {
  829. return internalCertificate.get(access, data);
  830. })
  831. .then((certificate) => {
  832. if (certificate.provider === 'letsencrypt') {
  833. const renewMethod = certificate.meta.dns_challenge ? internalCertificate.renewLetsEncryptSslWithDnsChallenge : internalCertificate.renewLetsEncryptSsl;
  834. return renewMethod(certificate)
  835. .then(() => {
  836. return internalCertificate.getCertificateInfoFromFile('/etc/letsencrypt/live/npm-' + certificate.id + '/fullchain.pem');
  837. })
  838. .then((cert_info) => {
  839. return certificateModel
  840. .query()
  841. .patchAndFetchById(certificate.id, {
  842. expires_on: moment(cert_info.dates.to, 'X').format('YYYY-MM-DD HH:mm:ss')
  843. });
  844. })
  845. .then((updated_certificate) => {
  846. // Add to audit log
  847. return internalAuditLog.add(access, {
  848. action: 'renewed',
  849. object_type: 'certificate',
  850. object_id: updated_certificate.id,
  851. meta: updated_certificate
  852. })
  853. .then(() => {
  854. return updated_certificate;
  855. });
  856. });
  857. } else {
  858. throw new error.ValidationError('Only Let\'sEncrypt certificates can be renewed');
  859. }
  860. });
  861. },
  862. /**
  863. * @param {Object} certificate the certificate row
  864. * @returns {Promise}
  865. */
  866. renewLetsEncryptSsl: (certificate) => {
  867. logger.info('Renewing Let\'sEncrypt certificates for Cert #' + certificate.id + ': ' + certificate.domain_names.join(', '));
  868. const cmd = certbotCommand + ' renew --force-renewal --non-interactive ' +
  869. '--config "' + letsencryptConfig + '" ' +
  870. '--cert-name "npm-' + certificate.id + '" ' +
  871. '--preferred-challenges "dns,http" ' +
  872. '--disable-hook-validation ' +
  873. (letsencryptStaging ? '--staging' : '');
  874. logger.info('Command:', cmd);
  875. return utils.exec(cmd)
  876. .then((result) => {
  877. logger.info(result);
  878. return result;
  879. });
  880. },
  881. /**
  882. * @param {Object} certificate the certificate row
  883. * @returns {Promise}
  884. */
  885. renewLetsEncryptSslWithDnsChallenge: (certificate) => {
  886. const dns_plugin = dnsPlugins[certificate.meta.dns_provider];
  887. if (!dns_plugin) {
  888. throw Error(`Unknown DNS provider '${certificate.meta.dns_provider}'`);
  889. }
  890. logger.info(`Renewing Let'sEncrypt certificates via ${dns_plugin.display_name} for Cert #${certificate.id}: ${certificate.domain_names.join(', ')}`);
  891. let mainCmd = certbotCommand + ' renew --non-interactive ' +
  892. '--cert-name "npm-' + certificate.id + '" ' +
  893. '--disable-hook-validation' +
  894. (letsencryptStaging ? ' --staging' : '');
  895. // Prepend the path to the credentials file as an environment variable
  896. if (certificate.meta.dns_provider === 'route53') {
  897. const credentialsLocation = '/etc/letsencrypt/credentials/credentials-' + certificate.id;
  898. mainCmd = 'AWS_CONFIG_FILE=\'' + credentialsLocation + '\' ' + mainCmd;
  899. }
  900. logger.info('Command:', mainCmd);
  901. return utils.exec(mainCmd)
  902. .then(async (result) => {
  903. logger.info(result);
  904. return result;
  905. });
  906. },
  907. /**
  908. * @param {Object} certificate the certificate row
  909. * @param {Boolean} [throw_errors]
  910. * @returns {Promise}
  911. */
  912. revokeLetsEncryptSsl: (certificate, throw_errors) => {
  913. logger.info('Revoking Let\'sEncrypt certificates for Cert #' + certificate.id + ': ' + certificate.domain_names.join(', '));
  914. const mainCmd = certbotCommand + ' revoke --non-interactive ' +
  915. '--cert-path "/etc/letsencrypt/live/npm-' + certificate.id + '/fullchain.pem" ' +
  916. '--delete-after-revoke ' +
  917. (letsencryptStaging ? '--staging' : '');
  918. // Don't fail command if file does not exist
  919. const delete_credentialsCmd = `rm -f '/etc/letsencrypt/credentials/credentials-${certificate.id}' || true`;
  920. logger.info('Command:', mainCmd + '; ' + delete_credentialsCmd);
  921. return utils.exec(mainCmd)
  922. .then(async (result) => {
  923. await utils.exec(delete_credentialsCmd);
  924. logger.info(result);
  925. return result;
  926. })
  927. .catch((err) => {
  928. logger.error(err.message);
  929. if (throw_errors) {
  930. throw err;
  931. }
  932. });
  933. },
  934. /**
  935. * @param {Object} certificate
  936. * @returns {Boolean}
  937. */
  938. hasLetsEncryptSslCerts: (certificate) => {
  939. const letsencryptPath = '/etc/letsencrypt/live/npm-' + certificate.id;
  940. return fs.existsSync(letsencryptPath + '/fullchain.pem') && fs.existsSync(letsencryptPath + '/privkey.pem');
  941. },
  942. /**
  943. * @param {Object} in_use_result
  944. * @param {Number} in_use_result.total_count
  945. * @param {Array} in_use_result.proxy_hosts
  946. * @param {Array} in_use_result.redirection_hosts
  947. * @param {Array} in_use_result.dead_hosts
  948. */
  949. disableInUseHosts: (in_use_result) => {
  950. if (in_use_result.total_count) {
  951. let promises = [];
  952. if (in_use_result.proxy_hosts.length) {
  953. promises.push(internalNginx.bulkDeleteConfigs('proxy_host', in_use_result.proxy_hosts));
  954. }
  955. if (in_use_result.redirection_hosts.length) {
  956. promises.push(internalNginx.bulkDeleteConfigs('redirection_host', in_use_result.redirection_hosts));
  957. }
  958. if (in_use_result.dead_hosts.length) {
  959. promises.push(internalNginx.bulkDeleteConfigs('dead_host', in_use_result.dead_hosts));
  960. }
  961. return Promise.all(promises);
  962. } else {
  963. return Promise.resolve();
  964. }
  965. },
  966. /**
  967. * @param {Object} in_use_result
  968. * @param {Number} in_use_result.total_count
  969. * @param {Array} in_use_result.proxy_hosts
  970. * @param {Array} in_use_result.redirection_hosts
  971. * @param {Array} in_use_result.dead_hosts
  972. */
  973. enableInUseHosts: (in_use_result) => {
  974. if (in_use_result.total_count) {
  975. let promises = [];
  976. if (in_use_result.proxy_hosts.length) {
  977. promises.push(internalNginx.bulkGenerateConfigs('proxy_host', in_use_result.proxy_hosts));
  978. }
  979. if (in_use_result.redirection_hosts.length) {
  980. promises.push(internalNginx.bulkGenerateConfigs('redirection_host', in_use_result.redirection_hosts));
  981. }
  982. if (in_use_result.dead_hosts.length) {
  983. promises.push(internalNginx.bulkGenerateConfigs('dead_host', in_use_result.dead_hosts));
  984. }
  985. return Promise.all(promises);
  986. } else {
  987. return Promise.resolve();
  988. }
  989. }
  990. };
  991. module.exports = internalCertificate;