dead-host.js 11 KB

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