dead-host.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. const _ = require('lodash');
  2. const error = require('../lib/error');
  3. const utils = require('../lib/utils');
  4. const deadHostModel = require('../models/dead_host');
  5. const internalHost = require('./host');
  6. const internalNginx = require('./nginx');
  7. const internalAuditLog = require('./audit-log');
  8. const internalCertificate = require('./certificate');
  9. function omissions () {
  10. return ['is_deleted'];
  11. }
  12. const internalDeadHost = {
  13. /**
  14. * @param {Access} access
  15. * @param {Object} data
  16. * @returns {Promise}
  17. */
  18. create: (access, data) => {
  19. let create_certificate = data.certificate_id === 'new';
  20. if (create_certificate) {
  21. delete data.certificate_id;
  22. }
  23. return access.can('dead_hosts:create', data)
  24. .then((/*access_data*/) => {
  25. // Get a list of the domain names and check each of them against existing records
  26. let domain_name_check_promises = [];
  27. data.domain_names.map(function (domain_name) {
  28. domain_name_check_promises.push(internalHost.isHostnameTaken(domain_name));
  29. });
  30. return Promise.all(domain_name_check_promises)
  31. .then((check_results) => {
  32. check_results.map(function (result) {
  33. if (result.is_taken) {
  34. throw new error.ValidationError(result.hostname + ' is already in use');
  35. }
  36. });
  37. });
  38. })
  39. .then(() => {
  40. // At this point the domains should have been checked
  41. data.owner_user_id = access.token.getUserId(1);
  42. data = internalHost.cleanSslHstsData(data);
  43. // Fix for db field not having a default value
  44. // for this optional field.
  45. if (typeof data.advanced_config === 'undefined') {
  46. data.advanced_config = '';
  47. }
  48. return deadHostModel
  49. .query()
  50. .insertAndFetch(data)
  51. .then(utils.omitRow(omissions()));
  52. })
  53. .then((row) => {
  54. if (create_certificate) {
  55. return internalCertificate.createQuickCertificate(access, data)
  56. .then((cert) => {
  57. // update host with cert id
  58. return internalDeadHost.update(access, {
  59. id: row.id,
  60. certificate_id: cert.id
  61. });
  62. })
  63. .then(() => {
  64. return row;
  65. });
  66. } else {
  67. return row;
  68. }
  69. })
  70. .then((row) => {
  71. // re-fetch with cert
  72. return internalDeadHost.get(access, {
  73. id: row.id,
  74. expand: ['certificate', 'owner']
  75. });
  76. })
  77. .then((row) => {
  78. // Configure nginx
  79. return internalNginx.configure(deadHostModel, 'dead_host', row)
  80. .then(() => {
  81. return row;
  82. });
  83. })
  84. .then((row) => {
  85. data.meta = _.assign({}, data.meta || {}, row.meta);
  86. // Add to audit log
  87. return internalAuditLog.add(access, {
  88. action: 'created',
  89. object_type: 'dead-host',
  90. object_id: row.id,
  91. meta: data
  92. })
  93. .then(() => {
  94. return row;
  95. });
  96. });
  97. },
  98. /**
  99. * @param {Access} access
  100. * @param {Object} data
  101. * @param {Number} data.id
  102. * @return {Promise}
  103. */
  104. update: (access, data) => {
  105. let create_certificate = data.certificate_id === 'new';
  106. if (create_certificate) {
  107. delete data.certificate_id;
  108. }
  109. return access.can('dead_hosts:update', data.id)
  110. .then((/*access_data*/) => {
  111. // Get a list of the domain names and check each of them against existing records
  112. let domain_name_check_promises = [];
  113. if (typeof data.domain_names !== 'undefined') {
  114. data.domain_names.map(function (domain_name) {
  115. domain_name_check_promises.push(internalHost.isHostnameTaken(domain_name, 'dead', data.id));
  116. });
  117. return Promise.all(domain_name_check_promises)
  118. .then((check_results) => {
  119. check_results.map(function (result) {
  120. if (result.is_taken) {
  121. throw new error.ValidationError(result.hostname + ' is already in use');
  122. }
  123. });
  124. });
  125. }
  126. })
  127. .then(() => {
  128. return internalDeadHost.get(access, {id: data.id});
  129. })
  130. .then((row) => {
  131. if (row.id !== data.id) {
  132. // Sanity check that something crazy hasn't happened
  133. throw new error.InternalValidationError('404 Host could not be updated, IDs do not match: ' + row.id + ' !== ' + data.id);
  134. }
  135. if (create_certificate) {
  136. return internalCertificate.createQuickCertificate(access, {
  137. domain_names: data.domain_names || row.domain_names,
  138. meta: _.assign({}, row.meta, data.meta)
  139. })
  140. .then((cert) => {
  141. // update host with cert id
  142. data.certificate_id = cert.id;
  143. })
  144. .then(() => {
  145. return row;
  146. });
  147. } else {
  148. return row;
  149. }
  150. })
  151. .then((row) => {
  152. // Add domain_names to the data in case it isn't there, so that the audit log renders correctly. The order is important here.
  153. data = _.assign({}, {
  154. domain_names: row.domain_names
  155. }, data);
  156. data = internalHost.cleanSslHstsData(data, row);
  157. return deadHostModel
  158. .query()
  159. .where({id: data.id})
  160. .patch(data)
  161. .then((saved_row) => {
  162. // Add to audit log
  163. return internalAuditLog.add(access, {
  164. action: 'updated',
  165. object_type: 'dead-host',
  166. object_id: row.id,
  167. meta: data
  168. })
  169. .then(() => {
  170. return _.omit(saved_row, omissions());
  171. });
  172. });
  173. })
  174. .then(() => {
  175. return internalDeadHost.get(access, {
  176. id: data.id,
  177. expand: ['owner', 'certificate']
  178. })
  179. .then((row) => {
  180. // Configure nginx
  181. return internalNginx.configure(deadHostModel, 'dead_host', row)
  182. .then((new_meta) => {
  183. row.meta = new_meta;
  184. row = internalHost.cleanRowCertificateMeta(row);
  185. return _.omit(row, omissions());
  186. });
  187. });
  188. });
  189. },
  190. /**
  191. * @param {Access} access
  192. * @param {Object} data
  193. * @param {Number} data.id
  194. * @param {Array} [data.expand]
  195. * @param {Array} [data.omit]
  196. * @return {Promise}
  197. */
  198. get: (access, data) => {
  199. if (typeof data === 'undefined') {
  200. data = {};
  201. }
  202. return access.can('dead_hosts:get', data.id)
  203. .then((access_data) => {
  204. let query = deadHostModel
  205. .query()
  206. .where('is_deleted', 0)
  207. .andWhere('id', data.id)
  208. .allowGraph('[owner,certificate]')
  209. .first();
  210. if (access_data.permission_visibility !== 'all') {
  211. query.andWhere('owner_user_id', access.token.getUserId(1));
  212. }
  213. if (typeof data.expand !== 'undefined' && data.expand !== null) {
  214. query.withGraphFetched('[' + data.expand.join(', ') + ']');
  215. }
  216. return query.then(utils.omitRow(omissions()));
  217. })
  218. .then((row) => {
  219. if (!row || !row.id) {
  220. throw new error.ItemNotFoundError(data.id);
  221. }
  222. // Custom omissions
  223. if (typeof data.omit !== 'undefined' && data.omit !== null) {
  224. row = _.omit(row, data.omit);
  225. }
  226. return row;
  227. });
  228. },
  229. /**
  230. * @param {Access} access
  231. * @param {Object} data
  232. * @param {Number} data.id
  233. * @param {String} [data.reason]
  234. * @returns {Promise}
  235. */
  236. delete: (access, data) => {
  237. return access.can('dead_hosts:delete', data.id)
  238. .then(() => {
  239. return internalDeadHost.get(access, {id: data.id});
  240. })
  241. .then((row) => {
  242. if (!row || !row.id) {
  243. throw new error.ItemNotFoundError(data.id);
  244. }
  245. return deadHostModel
  246. .query()
  247. .where('id', row.id)
  248. .patch({
  249. is_deleted: 1
  250. })
  251. .then(() => {
  252. // Delete Nginx Config
  253. return internalNginx.deleteConfig('dead_host', row)
  254. .then(() => {
  255. return internalNginx.reload();
  256. });
  257. })
  258. .then(() => {
  259. // Add to audit log
  260. return internalAuditLog.add(access, {
  261. action: 'deleted',
  262. object_type: 'dead-host',
  263. object_id: row.id,
  264. meta: _.omit(row, omissions())
  265. });
  266. });
  267. })
  268. .then(() => {
  269. return true;
  270. });
  271. },
  272. /**
  273. * @param {Access} access
  274. * @param {Object} data
  275. * @param {Number} data.id
  276. * @param {String} [data.reason]
  277. * @returns {Promise}
  278. */
  279. enable: (access, data) => {
  280. return access.can('dead_hosts:update', data.id)
  281. .then(() => {
  282. return internalDeadHost.get(access, {
  283. id: data.id,
  284. expand: ['certificate', 'owner']
  285. });
  286. })
  287. .then((row) => {
  288. if (!row || !row.id) {
  289. throw new error.ItemNotFoundError(data.id);
  290. } else if (row.enabled) {
  291. throw new error.ValidationError('Host is already enabled');
  292. }
  293. row.enabled = 1;
  294. return deadHostModel
  295. .query()
  296. .where('id', row.id)
  297. .patch({
  298. enabled: 1
  299. })
  300. .then(() => {
  301. // Configure nginx
  302. return internalNginx.configure(deadHostModel, 'dead_host', row);
  303. })
  304. .then(() => {
  305. // Add to audit log
  306. return internalAuditLog.add(access, {
  307. action: 'enabled',
  308. object_type: 'dead-host',
  309. object_id: row.id,
  310. meta: _.omit(row, omissions())
  311. });
  312. });
  313. })
  314. .then(() => {
  315. return true;
  316. });
  317. },
  318. /**
  319. * @param {Access} access
  320. * @param {Object} data
  321. * @param {Number} data.id
  322. * @param {String} [data.reason]
  323. * @returns {Promise}
  324. */
  325. disable: (access, data) => {
  326. return access.can('dead_hosts:update', data.id)
  327. .then(() => {
  328. return internalDeadHost.get(access, {id: data.id});
  329. })
  330. .then((row) => {
  331. if (!row || !row.id) {
  332. throw new error.ItemNotFoundError(data.id);
  333. } else if (!row.enabled) {
  334. throw new error.ValidationError('Host is already disabled');
  335. }
  336. row.enabled = 0;
  337. return deadHostModel
  338. .query()
  339. .where('id', row.id)
  340. .patch({
  341. enabled: 0
  342. })
  343. .then(() => {
  344. // Delete Nginx Config
  345. return internalNginx.deleteConfig('dead_host', row)
  346. .then(() => {
  347. return internalNginx.reload();
  348. });
  349. })
  350. .then(() => {
  351. // Add to audit log
  352. return internalAuditLog.add(access, {
  353. action: 'disabled',
  354. object_type: 'dead-host',
  355. object_id: row.id,
  356. meta: _.omit(row, omissions())
  357. });
  358. });
  359. })
  360. .then(() => {
  361. return true;
  362. });
  363. },
  364. /**
  365. * All Hosts
  366. *
  367. * @param {Access} access
  368. * @param {Array} [expand]
  369. * @param {String} [search_query]
  370. * @returns {Promise}
  371. */
  372. getAll: (access, expand, search_query) => {
  373. return access.can('dead_hosts:list')
  374. .then((access_data) => {
  375. let query = deadHostModel
  376. .query()
  377. .where('is_deleted', 0)
  378. .groupBy('id')
  379. .allowGraph('[owner,certificate]')
  380. .orderBy('domain_names', 'ASC');
  381. if (access_data.permission_visibility !== 'all') {
  382. query.andWhere('owner_user_id', access.token.getUserId(1));
  383. }
  384. // Query is used for searching
  385. if (typeof search_query === 'string') {
  386. query.where(function () {
  387. this.where('domain_names', 'like', '%' + search_query + '%');
  388. });
  389. }
  390. if (typeof expand !== 'undefined' && expand !== null) {
  391. query.withGraphFetched('[' + expand.join(', ') + ']');
  392. }
  393. return query.then(utils.omitRows(omissions()));
  394. })
  395. .then((rows) => {
  396. if (typeof expand !== 'undefined' && expand !== null && expand.indexOf('certificate') !== -1) {
  397. return internalHost.cleanAllRowsCertificateMeta(rows);
  398. }
  399. return rows;
  400. });
  401. },
  402. /**
  403. * Report use
  404. *
  405. * @param {Number} user_id
  406. * @param {String} visibility
  407. * @returns {Promise}
  408. */
  409. getCount: (user_id, visibility) => {
  410. let query = deadHostModel
  411. .query()
  412. .count('id as count')
  413. .where('is_deleted', 0);
  414. if (visibility !== 'all') {
  415. query.andWhere('owner_user_id', user_id);
  416. }
  417. return query.first()
  418. .then((row) => {
  419. return parseInt(row.count, 10);
  420. });
  421. }
  422. };
  423. module.exports = internalDeadHost;