redirection-host.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. const _ = require('lodash');
  2. const error = require('../lib/error');
  3. const utils = require('../lib/utils');
  4. const redirectionHostModel = require('../models/redirection_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 internalRedirectionHost = {
  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('redirection_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 redirectionHostModel
  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 internalRedirectionHost.update(access, {
  59. id: row.id,
  60. certificate_id: cert.id
  61. });
  62. })
  63. .then(() => {
  64. return row;
  65. });
  66. }
  67. return row;
  68. })
  69. .then((row) => {
  70. // re-fetch with cert
  71. return internalRedirectionHost.get(access, {
  72. id: row.id,
  73. expand: ['certificate', 'owner']
  74. });
  75. })
  76. .then((row) => {
  77. // Configure nginx
  78. return internalNginx.configure(redirectionHostModel, 'redirection_host', row)
  79. .then(() => {
  80. return row;
  81. });
  82. })
  83. .then((row) => {
  84. data.meta = _.assign({}, data.meta || {}, row.meta);
  85. // Add to audit log
  86. return internalAuditLog.add(access, {
  87. action: 'created',
  88. object_type: 'redirection-host',
  89. object_id: row.id,
  90. meta: data
  91. })
  92. .then(() => {
  93. return row;
  94. });
  95. });
  96. },
  97. /**
  98. * @param {Access} access
  99. * @param {Object} data
  100. * @param {Number} data.id
  101. * @return {Promise}
  102. */
  103. update: (access, data) => {
  104. let create_certificate = data.certificate_id === 'new';
  105. if (create_certificate) {
  106. delete data.certificate_id;
  107. }
  108. return access.can('redirection_hosts:update', data.id)
  109. .then((/*access_data*/) => {
  110. // Get a list of the domain names and check each of them against existing records
  111. let domain_name_check_promises = [];
  112. if (typeof data.domain_names !== 'undefined') {
  113. data.domain_names.map(function (domain_name) {
  114. domain_name_check_promises.push(internalHost.isHostnameTaken(domain_name, 'redirection', data.id));
  115. });
  116. return Promise.all(domain_name_check_promises)
  117. .then((check_results) => {
  118. check_results.map(function (result) {
  119. if (result.is_taken) {
  120. throw new error.ValidationError(result.hostname + ' is already in use');
  121. }
  122. });
  123. });
  124. }
  125. })
  126. .then(() => {
  127. return internalRedirectionHost.get(access, {id: data.id});
  128. })
  129. .then((row) => {
  130. if (row.id !== data.id) {
  131. // Sanity check that something crazy hasn't happened
  132. throw new error.InternalValidationError('Redirection Host could not be updated, IDs do not match: ' + row.id + ' !== ' + data.id);
  133. }
  134. if (create_certificate) {
  135. return internalCertificate.createQuickCertificate(access, {
  136. domain_names: data.domain_names || row.domain_names,
  137. meta: _.assign({}, row.meta, data.meta)
  138. })
  139. .then((cert) => {
  140. // update host with cert id
  141. data.certificate_id = cert.id;
  142. })
  143. .then(() => {
  144. return row;
  145. });
  146. } else {
  147. return row;
  148. }
  149. })
  150. .then((row) => {
  151. // Add domain_names to the data in case it isn't there, so that the audit log renders correctly. The order is important here.
  152. data = _.assign({}, {
  153. domain_names: row.domain_names
  154. }, data);
  155. data = internalHost.cleanSslHstsData(data, row);
  156. return redirectionHostModel
  157. .query()
  158. .where({id: data.id})
  159. .patch(data)
  160. .then((saved_row) => {
  161. // Add to audit log
  162. return internalAuditLog.add(access, {
  163. action: 'updated',
  164. object_type: 'redirection-host',
  165. object_id: row.id,
  166. meta: data
  167. })
  168. .then(() => {
  169. return _.omit(saved_row, omissions());
  170. });
  171. });
  172. })
  173. .then(() => {
  174. return internalRedirectionHost.get(access, {
  175. id: data.id,
  176. expand: ['owner', 'certificate']
  177. })
  178. .then((row) => {
  179. // Configure nginx
  180. return internalNginx.configure(redirectionHostModel, 'redirection_host', row)
  181. .then((new_meta) => {
  182. row.meta = new_meta;
  183. row = internalHost.cleanRowCertificateMeta(row);
  184. return _.omit(row, omissions());
  185. });
  186. });
  187. });
  188. },
  189. /**
  190. * @param {Access} access
  191. * @param {Object} data
  192. * @param {Number} data.id
  193. * @param {Array} [data.expand]
  194. * @param {Array} [data.omit]
  195. * @return {Promise}
  196. */
  197. get: (access, data) => {
  198. if (typeof data === 'undefined') {
  199. data = {};
  200. }
  201. return access.can('redirection_hosts:get', data.id)
  202. .then((access_data) => {
  203. let query = redirectionHostModel
  204. .query()
  205. .where('is_deleted', 0)
  206. .andWhere('id', data.id)
  207. .allowGraph('[owner,certificate]')
  208. .first();
  209. if (access_data.permission_visibility !== 'all') {
  210. query.andWhere('owner_user_id', access.token.getUserId(1));
  211. }
  212. if (typeof data.expand !== 'undefined' && data.expand !== null) {
  213. query.withGraphFetched('[' + data.expand.join(', ') + ']');
  214. }
  215. return query.then(utils.omitRow(omissions()));
  216. })
  217. .then((row) => {
  218. if (!row || !row.id) {
  219. throw new error.ItemNotFoundError(data.id);
  220. }
  221. row = internalHost.cleanRowCertificateMeta(row);
  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('redirection_hosts:delete', data.id)
  238. .then(() => {
  239. return internalRedirectionHost.get(access, {id: data.id});
  240. })
  241. .then((row) => {
  242. if (!row || !row.id) {
  243. throw new error.ItemNotFoundError(data.id);
  244. }
  245. return redirectionHostModel
  246. .query()
  247. .where('id', row.id)
  248. .patch({
  249. is_deleted: 1
  250. })
  251. .then(() => {
  252. // Delete Nginx Config
  253. return internalNginx.deleteConfig('redirection_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: 'redirection-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('redirection_hosts:update', data.id)
  281. .then(() => {
  282. return internalRedirectionHost.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 redirectionHostModel
  295. .query()
  296. .where('id', row.id)
  297. .patch({
  298. enabled: 1
  299. })
  300. .then(() => {
  301. // Configure nginx
  302. return internalNginx.configure(redirectionHostModel, 'redirection_host', row);
  303. })
  304. .then(() => {
  305. // Add to audit log
  306. return internalAuditLog.add(access, {
  307. action: 'enabled',
  308. object_type: 'redirection-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('redirection_hosts:update', data.id)
  327. .then(() => {
  328. return internalRedirectionHost.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 redirectionHostModel
  338. .query()
  339. .where('id', row.id)
  340. .patch({
  341. enabled: 0
  342. })
  343. .then(() => {
  344. // Delete Nginx Config
  345. return internalNginx.deleteConfig('redirection_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: 'redirection-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('redirection_hosts:list')
  374. .then((access_data) => {
  375. let query = redirectionHostModel
  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 = redirectionHostModel
  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 = internalRedirectionHost;