certificate.js 33 KB

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