certificate.js 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103
  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 {Object} data
  311. * @param {Number} data.id
  312. * @returns {Promise}
  313. */
  314. download: (data) => {
  315. const downloadName = "npm-" + data.id + "-" + `${Date.now()}.zip`;
  316. const opName = '/tmp/' + downloadName;
  317. const zipDirectory = "/etc/letsencrypt/live/npm-" + data.id
  318. return new Promise((resolve, reject) => {
  319. internalCertificate.zipDirectory(zipDirectory, opName)
  320. .then(() => {
  321. logger.debug("zip completed : ", opName)
  322. const resp = {
  323. fileName: opName
  324. }
  325. resolve(resp)
  326. }).catch(err => {
  327. reject(err)
  328. })
  329. });
  330. },
  331. /**
  332. * @param {String} source
  333. * @param {String} out
  334. * @returns {Promise}
  335. */
  336. zipDirectory(source, out) {
  337. const archive = archiver('zip', { zlib: { level: 9 } });
  338. const stream = fs.createWriteStream(out);
  339. return new Promise((resolve, reject) => {
  340. archive
  341. .directory(source, false)
  342. .on('error', err => reject(err))
  343. .pipe(stream);
  344. stream.on('close', () => resolve());
  345. archive.finalize();
  346. });
  347. },
  348. /**
  349. * @param {Access} access
  350. * @param {Object} data
  351. * @param {Number} data.id
  352. * @param {String} [data.reason]
  353. * @returns {Promise}
  354. */
  355. delete: (access, data) => {
  356. return access.can('certificates:delete', data.id)
  357. .then(() => {
  358. return internalCertificate.get(access, {id: data.id});
  359. })
  360. .then((row) => {
  361. if (!row) {
  362. throw new error.ItemNotFoundError(data.id);
  363. }
  364. return certificateModel
  365. .query()
  366. .where('id', row.id)
  367. .patch({
  368. is_deleted: 1
  369. })
  370. .then(() => {
  371. // Add to audit log
  372. row.meta = internalCertificate.cleanMeta(row.meta);
  373. return internalAuditLog.add(access, {
  374. action: 'deleted',
  375. object_type: 'certificate',
  376. object_id: row.id,
  377. meta: _.omit(row, omissions())
  378. });
  379. })
  380. .then(() => {
  381. if (row.provider === 'letsencrypt') {
  382. // Revoke the cert
  383. return internalCertificate.revokeLetsEncryptSsl(row);
  384. }
  385. });
  386. })
  387. .then(() => {
  388. return true;
  389. });
  390. },
  391. /**
  392. * All Certs
  393. *
  394. * @param {Access} access
  395. * @param {Array} [expand]
  396. * @param {String} [search_query]
  397. * @returns {Promise}
  398. */
  399. getAll: (access, expand, search_query) => {
  400. return access.can('certificates:list')
  401. .then((access_data) => {
  402. let query = certificateModel
  403. .query()
  404. .where('is_deleted', 0)
  405. .groupBy('id')
  406. .omit(['is_deleted'])
  407. .allowEager('[owner]')
  408. .orderBy('nice_name', 'ASC');
  409. if (access_data.permission_visibility !== 'all') {
  410. query.andWhere('owner_user_id', access.token.getUserId(1));
  411. }
  412. // Query is used for searching
  413. if (typeof search_query === 'string') {
  414. query.where(function () {
  415. this.where('name', 'like', '%' + search_query + '%');
  416. });
  417. }
  418. if (typeof expand !== 'undefined' && expand !== null) {
  419. query.eager('[' + expand.join(', ') + ']');
  420. }
  421. return query;
  422. });
  423. },
  424. /**
  425. * Report use
  426. *
  427. * @param {Number} user_id
  428. * @param {String} visibility
  429. * @returns {Promise}
  430. */
  431. getCount: (user_id, visibility) => {
  432. let query = certificateModel
  433. .query()
  434. .count('id as count')
  435. .where('is_deleted', 0);
  436. if (visibility !== 'all') {
  437. query.andWhere('owner_user_id', user_id);
  438. }
  439. return query.first()
  440. .then((row) => {
  441. return parseInt(row.count, 10);
  442. });
  443. },
  444. /**
  445. * @param {Object} certificate
  446. * @returns {Promise}
  447. */
  448. writeCustomCert: (certificate) => {
  449. logger.info('Writing Custom Certificate:', certificate);
  450. const dir = '/data/custom_ssl/npm-' + certificate.id;
  451. return new Promise((resolve, reject) => {
  452. if (certificate.provider === 'letsencrypt') {
  453. reject(new Error('Refusing to write letsencrypt certs here'));
  454. return;
  455. }
  456. let certData = certificate.meta.certificate;
  457. if (typeof certificate.meta.intermediate_certificate !== 'undefined') {
  458. certData = certData + '\n' + certificate.meta.intermediate_certificate;
  459. }
  460. try {
  461. if (!fs.existsSync(dir)) {
  462. fs.mkdirSync(dir);
  463. }
  464. } catch (err) {
  465. reject(err);
  466. return;
  467. }
  468. fs.writeFile(dir + '/fullchain.pem', certData, function (err) {
  469. if (err) {
  470. reject(err);
  471. } else {
  472. resolve();
  473. }
  474. });
  475. })
  476. .then(() => {
  477. return new Promise((resolve, reject) => {
  478. fs.writeFile(dir + '/privkey.pem', certificate.meta.certificate_key, function (err) {
  479. if (err) {
  480. reject(err);
  481. } else {
  482. resolve();
  483. }
  484. });
  485. });
  486. });
  487. },
  488. /**
  489. * @param {Access} access
  490. * @param {Object} data
  491. * @param {Array} data.domain_names
  492. * @param {String} data.meta.letsencrypt_email
  493. * @param {Boolean} data.meta.letsencrypt_agree
  494. * @returns {Promise}
  495. */
  496. createQuickCertificate: (access, data) => {
  497. return internalCertificate.create(access, {
  498. provider: 'letsencrypt',
  499. domain_names: data.domain_names,
  500. meta: data.meta
  501. });
  502. },
  503. /**
  504. * Validates that the certs provided are good.
  505. * No access required here, nothing is changed or stored.
  506. *
  507. * @param {Object} data
  508. * @param {Object} data.files
  509. * @returns {Promise}
  510. */
  511. validate: (data) => {
  512. return new Promise((resolve) => {
  513. // Put file contents into an object
  514. let files = {};
  515. _.map(data.files, (file, name) => {
  516. if (internalCertificate.allowedSslFiles.indexOf(name) !== -1) {
  517. files[name] = file.data.toString();
  518. }
  519. });
  520. resolve(files);
  521. })
  522. .then((files) => {
  523. // For each file, create a temp file and write the contents to it
  524. // Then test it depending on the file type
  525. let promises = [];
  526. _.map(files, (content, type) => {
  527. promises.push(new Promise((resolve) => {
  528. if (type === 'certificate_key') {
  529. resolve(internalCertificate.checkPrivateKey(content));
  530. } else {
  531. // this should handle `certificate` and intermediate certificate
  532. resolve(internalCertificate.getCertificateInfo(content, true));
  533. }
  534. }).then((res) => {
  535. return {[type]: res};
  536. }));
  537. });
  538. return Promise.all(promises)
  539. .then((files) => {
  540. let data = {};
  541. _.each(files, (file) => {
  542. data = _.assign({}, data, file);
  543. });
  544. return data;
  545. });
  546. });
  547. },
  548. /**
  549. * @param {Access} access
  550. * @param {Object} data
  551. * @param {Number} data.id
  552. * @param {Object} data.files
  553. * @returns {Promise}
  554. */
  555. upload: (access, data) => {
  556. return internalCertificate.get(access, {id: data.id})
  557. .then((row) => {
  558. if (row.provider !== 'other') {
  559. throw new error.ValidationError('Cannot upload certificates for this type of provider');
  560. }
  561. return internalCertificate.validate(data)
  562. .then((validations) => {
  563. if (typeof validations.certificate === 'undefined') {
  564. throw new error.ValidationError('Certificate file was not provided');
  565. }
  566. _.map(data.files, (file, name) => {
  567. if (internalCertificate.allowedSslFiles.indexOf(name) !== -1) {
  568. row.meta[name] = file.data.toString();
  569. }
  570. });
  571. // TODO: This uses a mysql only raw function that won't translate to postgres
  572. return internalCertificate.update(access, {
  573. id: data.id,
  574. expires_on: moment(validations.certificate.dates.to, 'X').format('YYYY-MM-DD HH:mm:ss'),
  575. domain_names: [validations.certificate.cn],
  576. meta: _.clone(row.meta) // Prevent the update method from changing this value that we'll use later
  577. })
  578. .then((certificate) => {
  579. console.log('ROWMETA:', row.meta);
  580. certificate.meta = row.meta;
  581. return internalCertificate.writeCustomCert(certificate);
  582. });
  583. })
  584. .then(() => {
  585. return _.pick(row.meta, internalCertificate.allowedSslFiles);
  586. });
  587. });
  588. },
  589. /**
  590. * Uses the openssl command to validate the private key.
  591. * It will save the file to disk first, then run commands on it, then delete the file.
  592. *
  593. * @param {String} private_key This is the entire key contents as a string
  594. */
  595. checkPrivateKey: (private_key) => {
  596. return tempWrite(private_key, '/tmp')
  597. .then((filepath) => {
  598. return new Promise((resolve, reject) => {
  599. const failTimeout = setTimeout(() => {
  600. reject(new error.ValidationError('Result Validation Error: Validation timed out. This could be due to the key being passphrase-protected.'));
  601. }, 10000);
  602. utils
  603. .exec('openssl pkey -in ' + filepath + ' -check -noout 2>&1 ')
  604. .then((result) => {
  605. clearTimeout(failTimeout);
  606. if (!result.toLowerCase().includes('key is valid')) {
  607. reject(new error.ValidationError('Result Validation Error: ' + result));
  608. }
  609. fs.unlinkSync(filepath);
  610. resolve(true);
  611. })
  612. .catch((err) => {
  613. clearTimeout(failTimeout);
  614. fs.unlinkSync(filepath);
  615. reject(new error.ValidationError('Certificate Key is not valid (' + err.message + ')', err));
  616. });
  617. });
  618. });
  619. },
  620. /**
  621. * Uses the openssl command to both validate and get info out of the certificate.
  622. * It will save the file to disk first, then run commands on it, then delete the file.
  623. *
  624. * @param {String} certificate This is the entire cert contents as a string
  625. * @param {Boolean} [throw_expired] Throw when the certificate is out of date
  626. */
  627. getCertificateInfo: (certificate, throw_expired) => {
  628. return tempWrite(certificate, '/tmp')
  629. .then((filepath) => {
  630. return internalCertificate.getCertificateInfoFromFile(filepath, throw_expired)
  631. .then((certData) => {
  632. fs.unlinkSync(filepath);
  633. return certData;
  634. }).catch((err) => {
  635. fs.unlinkSync(filepath);
  636. throw err;
  637. });
  638. });
  639. },
  640. /**
  641. * Uses the openssl command to both validate and get info out of the certificate.
  642. * It will save the file to disk first, then run commands on it, then delete the file.
  643. *
  644. * @param {String} certificate_file The file location on disk
  645. * @param {Boolean} [throw_expired] Throw when the certificate is out of date
  646. */
  647. getCertificateInfoFromFile: (certificate_file, throw_expired) => {
  648. let certData = {};
  649. return utils.exec('openssl x509 -in ' + certificate_file + ' -subject -noout')
  650. .then((result) => {
  651. // subject=CN = something.example.com
  652. const regex = /(?:subject=)?[^=]+=\s+(\S+)/gim;
  653. const match = regex.exec(result);
  654. if (typeof match[1] === 'undefined') {
  655. throw new error.ValidationError('Could not determine subject from certificate: ' + result);
  656. }
  657. certData['cn'] = match[1];
  658. })
  659. .then(() => {
  660. return utils.exec('openssl x509 -in ' + certificate_file + ' -issuer -noout');
  661. })
  662. .then((result) => {
  663. // issuer=C = US, O = Let's Encrypt, CN = Let's Encrypt Authority X3
  664. const regex = /^(?:issuer=)?(.*)$/gim;
  665. const match = regex.exec(result);
  666. if (typeof match[1] === 'undefined') {
  667. throw new error.ValidationError('Could not determine issuer from certificate: ' + result);
  668. }
  669. certData['issuer'] = match[1];
  670. })
  671. .then(() => {
  672. return utils.exec('openssl x509 -in ' + certificate_file + ' -dates -noout');
  673. })
  674. .then((result) => {
  675. // notBefore=Jul 14 04:04:29 2018 GMT
  676. // notAfter=Oct 12 04:04:29 2018 GMT
  677. let validFrom = null;
  678. let validTo = null;
  679. const lines = result.split('\n');
  680. lines.map(function (str) {
  681. const regex = /^(\S+)=(.*)$/gim;
  682. const match = regex.exec(str.trim());
  683. if (match && typeof match[2] !== 'undefined') {
  684. const date = parseInt(moment(match[2], 'MMM DD HH:mm:ss YYYY z').format('X'), 10);
  685. if (match[1].toLowerCase() === 'notbefore') {
  686. validFrom = date;
  687. } else if (match[1].toLowerCase() === 'notafter') {
  688. validTo = date;
  689. }
  690. }
  691. });
  692. if (!validFrom || !validTo) {
  693. throw new error.ValidationError('Could not determine dates from certificate: ' + result);
  694. }
  695. if (throw_expired && validTo < parseInt(moment().format('X'), 10)) {
  696. throw new error.ValidationError('Certificate has expired');
  697. }
  698. certData['dates'] = {
  699. from: validFrom,
  700. to: validTo
  701. };
  702. return certData;
  703. }).catch((err) => {
  704. throw new error.ValidationError('Certificate is not valid (' + err.message + ')', err);
  705. });
  706. },
  707. /**
  708. * Cleans the ssl keys from the meta object and sets them to "true"
  709. *
  710. * @param {Object} meta
  711. * @param {Boolean} [remove]
  712. * @returns {Object}
  713. */
  714. cleanMeta: function (meta, remove) {
  715. internalCertificate.allowedSslFiles.map((key) => {
  716. if (typeof meta[key] !== 'undefined' && meta[key]) {
  717. if (remove) {
  718. delete meta[key];
  719. } else {
  720. meta[key] = true;
  721. }
  722. }
  723. });
  724. return meta;
  725. },
  726. /**
  727. * Request a certificate using the http challenge
  728. * @param {Object} certificate the certificate row
  729. * @returns {Promise}
  730. */
  731. requestLetsEncryptSsl: (certificate) => {
  732. logger.info('Requesting Let\'sEncrypt certificates for Cert #' + certificate.id + ': ' + certificate.domain_names.join(', '));
  733. const cmd = certbotCommand + ' certonly --non-interactive ' +
  734. '--config "' + letsencryptConfig + '" ' +
  735. '--cert-name "npm-' + certificate.id + '" ' +
  736. '--agree-tos ' +
  737. '--authenticator webroot ' +
  738. '--email "' + certificate.meta.letsencrypt_email + '" ' +
  739. '--preferred-challenges "dns,http" ' +
  740. '--domains "' + certificate.domain_names.join(',') + '" ' +
  741. (letsencryptStaging ? '--staging' : '');
  742. logger.info('Command:', cmd);
  743. return utils.exec(cmd)
  744. .then((result) => {
  745. logger.success(result);
  746. return result;
  747. });
  748. },
  749. /**
  750. * @param {Object} certificate the certificate row
  751. * @param {String} dns_provider the dns provider name (key used in `certbot-dns-plugins.js`)
  752. * @param {String | null} credentials the content of this providers credentials file
  753. * @param {String} propagation_seconds the cloudflare api token
  754. * @returns {Promise}
  755. */
  756. requestLetsEncryptSslWithDnsChallenge: (certificate) => {
  757. const dns_plugin = dnsPlugins[certificate.meta.dns_provider];
  758. if (!dns_plugin) {
  759. throw Error(`Unknown DNS provider '${certificate.meta.dns_provider}'`);
  760. }
  761. logger.info(`Requesting Let'sEncrypt certificates via ${dns_plugin.display_name} for Cert #${certificate.id}: ${certificate.domain_names.join(', ')}`);
  762. const credentialsLocation = '/etc/letsencrypt/credentials/credentials-' + certificate.id;
  763. const credentialsCmd = 'mkdir -p /etc/letsencrypt/credentials 2> /dev/null; echo \'' + certificate.meta.dns_provider_credentials.replace('\'', '\\\'') + '\' > \'' + credentialsLocation + '\' && chmod 600 \'' + credentialsLocation + '\'';
  764. const prepareCmd = 'pip install ' + dns_plugin.package_name + '==' + dns_plugin.package_version + ' ' + dns_plugin.dependencies;
  765. // Whether the plugin has a --<name>-credentials argument
  766. const hasConfigArg = certificate.meta.dns_provider !== 'route53';
  767. let mainCmd = certbotCommand + ' certonly --non-interactive ' +
  768. '--cert-name "npm-' + certificate.id + '" ' +
  769. '--agree-tos ' +
  770. '--email "' + certificate.meta.letsencrypt_email + '" ' +
  771. '--domains "' + certificate.domain_names.join(',') + '" ' +
  772. '--authenticator ' + dns_plugin.full_plugin_name + ' ' +
  773. (
  774. hasConfigArg
  775. ? '--' + dns_plugin.full_plugin_name + '-credentials "' + credentialsLocation + '"'
  776. : ''
  777. ) +
  778. (
  779. certificate.meta.propagation_seconds !== undefined
  780. ? ' --' + dns_plugin.full_plugin_name + '-propagation-seconds ' + certificate.meta.propagation_seconds
  781. : ''
  782. ) +
  783. (letsencryptStaging ? ' --staging' : '');
  784. // Prepend the path to the credentials file as an environment variable
  785. if (certificate.meta.dns_provider === 'route53') {
  786. mainCmd = 'AWS_CONFIG_FILE=\'' + credentialsLocation + '\' ' + mainCmd;
  787. }
  788. logger.info('Command:', `${credentialsCmd} && ${prepareCmd} && ${mainCmd}`);
  789. return utils.exec(credentialsCmd)
  790. .then(() => {
  791. return utils.exec(prepareCmd)
  792. .then(() => {
  793. return utils.exec(mainCmd)
  794. .then(async (result) => {
  795. logger.info(result);
  796. return result;
  797. });
  798. });
  799. }).catch(async (err) => {
  800. // Don't fail if file does not exist
  801. const delete_credentialsCmd = `rm -f '${credentialsLocation}' || true`;
  802. await utils.exec(delete_credentialsCmd);
  803. throw err;
  804. });
  805. },
  806. /**
  807. * @param {Access} access
  808. * @param {Object} data
  809. * @param {Number} data.id
  810. * @returns {Promise}
  811. */
  812. renew: (access, data) => {
  813. return access.can('certificates:update', data)
  814. .then(() => {
  815. return internalCertificate.get(access, data);
  816. })
  817. .then((certificate) => {
  818. if (certificate.provider === 'letsencrypt') {
  819. const renewMethod = certificate.meta.dns_challenge ? internalCertificate.renewLetsEncryptSslWithDnsChallenge : internalCertificate.renewLetsEncryptSsl;
  820. return renewMethod(certificate)
  821. .then(() => {
  822. return internalCertificate.getCertificateInfoFromFile('/etc/letsencrypt/live/npm-' + certificate.id + '/fullchain.pem');
  823. })
  824. .then((cert_info) => {
  825. return certificateModel
  826. .query()
  827. .patchAndFetchById(certificate.id, {
  828. expires_on: moment(cert_info.dates.to, 'X').format('YYYY-MM-DD HH:mm:ss')
  829. });
  830. })
  831. .then((updated_certificate) => {
  832. // Add to audit log
  833. return internalAuditLog.add(access, {
  834. action: 'renewed',
  835. object_type: 'certificate',
  836. object_id: updated_certificate.id,
  837. meta: updated_certificate
  838. })
  839. .then(() => {
  840. return updated_certificate;
  841. });
  842. });
  843. } else {
  844. throw new error.ValidationError('Only Let\'sEncrypt certificates can be renewed');
  845. }
  846. });
  847. },
  848. /**
  849. * @param {Object} certificate the certificate row
  850. * @returns {Promise}
  851. */
  852. renewLetsEncryptSsl: (certificate) => {
  853. logger.info('Renewing Let\'sEncrypt certificates for Cert #' + certificate.id + ': ' + certificate.domain_names.join(', '));
  854. const cmd = certbotCommand + ' renew --force-renewal --non-interactive ' +
  855. '--config "' + letsencryptConfig + '" ' +
  856. '--cert-name "npm-' + certificate.id + '" ' +
  857. '--preferred-challenges "dns,http" ' +
  858. '--disable-hook-validation ' +
  859. (letsencryptStaging ? '--staging' : '');
  860. logger.info('Command:', cmd);
  861. return utils.exec(cmd)
  862. .then((result) => {
  863. logger.info(result);
  864. return result;
  865. });
  866. },
  867. /**
  868. * @param {Object} certificate the certificate row
  869. * @returns {Promise}
  870. */
  871. renewLetsEncryptSslWithDnsChallenge: (certificate) => {
  872. const dns_plugin = dnsPlugins[certificate.meta.dns_provider];
  873. if (!dns_plugin) {
  874. throw Error(`Unknown DNS provider '${certificate.meta.dns_provider}'`);
  875. }
  876. logger.info(`Renewing Let'sEncrypt certificates via ${dns_plugin.display_name} for Cert #${certificate.id}: ${certificate.domain_names.join(', ')}`);
  877. let mainCmd = certbotCommand + ' renew --non-interactive ' +
  878. '--cert-name "npm-' + certificate.id + '" ' +
  879. '--disable-hook-validation' +
  880. (letsencryptStaging ? ' --staging' : '');
  881. // Prepend the path to the credentials file as an environment variable
  882. if (certificate.meta.dns_provider === 'route53') {
  883. const credentialsLocation = '/etc/letsencrypt/credentials/credentials-' + certificate.id;
  884. mainCmd = 'AWS_CONFIG_FILE=\'' + credentialsLocation + '\' ' + mainCmd;
  885. }
  886. logger.info('Command:', mainCmd);
  887. return utils.exec(mainCmd)
  888. .then(async (result) => {
  889. logger.info(result);
  890. return result;
  891. });
  892. },
  893. /**
  894. * @param {Object} certificate the certificate row
  895. * @param {Boolean} [throw_errors]
  896. * @returns {Promise}
  897. */
  898. revokeLetsEncryptSsl: (certificate, throw_errors) => {
  899. logger.info('Revoking Let\'sEncrypt certificates for Cert #' + certificate.id + ': ' + certificate.domain_names.join(', '));
  900. const mainCmd = certbotCommand + ' revoke --non-interactive ' +
  901. '--cert-path "/etc/letsencrypt/live/npm-' + certificate.id + '/fullchain.pem" ' +
  902. '--delete-after-revoke ' +
  903. (letsencryptStaging ? '--staging' : '');
  904. // Don't fail command if file does not exist
  905. const delete_credentialsCmd = `rm -f '/etc/letsencrypt/credentials/credentials-${certificate.id}' || true`;
  906. logger.info('Command:', mainCmd + '; ' + delete_credentialsCmd);
  907. return utils.exec(mainCmd)
  908. .then(async (result) => {
  909. await utils.exec(delete_credentialsCmd);
  910. logger.info(result);
  911. return result;
  912. })
  913. .catch((err) => {
  914. logger.error(err.message);
  915. if (throw_errors) {
  916. throw err;
  917. }
  918. });
  919. },
  920. /**
  921. * @param {Object} certificate
  922. * @returns {Boolean}
  923. */
  924. hasLetsEncryptSslCerts: (certificate) => {
  925. const letsencryptPath = '/etc/letsencrypt/live/npm-' + certificate.id;
  926. return fs.existsSync(letsencryptPath + '/fullchain.pem') && fs.existsSync(letsencryptPath + '/privkey.pem');
  927. },
  928. /**
  929. * @param {Object} in_use_result
  930. * @param {Number} in_use_result.total_count
  931. * @param {Array} in_use_result.proxy_hosts
  932. * @param {Array} in_use_result.redirection_hosts
  933. * @param {Array} in_use_result.dead_hosts
  934. */
  935. disableInUseHosts: (in_use_result) => {
  936. if (in_use_result.total_count) {
  937. let promises = [];
  938. if (in_use_result.proxy_hosts.length) {
  939. promises.push(internalNginx.bulkDeleteConfigs('proxy_host', in_use_result.proxy_hosts));
  940. }
  941. if (in_use_result.redirection_hosts.length) {
  942. promises.push(internalNginx.bulkDeleteConfigs('redirection_host', in_use_result.redirection_hosts));
  943. }
  944. if (in_use_result.dead_hosts.length) {
  945. promises.push(internalNginx.bulkDeleteConfigs('dead_host', in_use_result.dead_hosts));
  946. }
  947. return Promise.all(promises);
  948. } else {
  949. return Promise.resolve();
  950. }
  951. },
  952. /**
  953. * @param {Object} in_use_result
  954. * @param {Number} in_use_result.total_count
  955. * @param {Array} in_use_result.proxy_hosts
  956. * @param {Array} in_use_result.redirection_hosts
  957. * @param {Array} in_use_result.dead_hosts
  958. */
  959. enableInUseHosts: (in_use_result) => {
  960. if (in_use_result.total_count) {
  961. let promises = [];
  962. if (in_use_result.proxy_hosts.length) {
  963. promises.push(internalNginx.bulkGenerateConfigs('proxy_host', in_use_result.proxy_hosts));
  964. }
  965. if (in_use_result.redirection_hosts.length) {
  966. promises.push(internalNginx.bulkGenerateConfigs('redirection_host', in_use_result.redirection_hosts));
  967. }
  968. if (in_use_result.dead_hosts.length) {
  969. promises.push(internalNginx.bulkGenerateConfigs('dead_host', in_use_result.dead_hosts));
  970. }
  971. return Promise.all(promises);
  972. } else {
  973. return Promise.resolve();
  974. }
  975. }
  976. };
  977. module.exports = internalCertificate;