proxy-host.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. 'use strict';
  2. const _ = require('lodash');
  3. const error = require('../lib/error');
  4. const proxyHostModel = require('../models/proxy_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 internalProxyHost = {
  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('proxy_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.get('attrs').id;
  42. return proxyHostModel
  43. .query()
  44. .omit(omissions())
  45. .insertAndFetch(data);
  46. })
  47. .then(row => {
  48. if (create_certificate) {
  49. return internalCertificate.createQuickCertificate(access, data)
  50. .then(cert => {
  51. // update host with cert id
  52. return internalProxyHost.update(access, {
  53. id: row.id,
  54. certificate_id: cert.id
  55. });
  56. })
  57. .then(() => {
  58. return row;
  59. });
  60. } else {
  61. return row;
  62. }
  63. })
  64. .then(row => {
  65. // re-fetch with cert
  66. return internalProxyHost.get(access, {
  67. id: row.id,
  68. expand: ['certificate', 'owner']
  69. });
  70. })
  71. .then(row => {
  72. // Configure nginx
  73. return internalNginx.configure(proxyHostModel, 'proxy_host', row)
  74. .then(() => {
  75. return row;
  76. });
  77. })
  78. .then(row => {
  79. // Audit log
  80. data.meta = _.assign({}, data.meta || {}, row.meta);
  81. // Add to audit log
  82. return internalAuditLog.add(access, {
  83. action: 'created',
  84. object_type: 'proxy-host',
  85. object_id: row.id,
  86. meta: data
  87. })
  88. .then(() => {
  89. return row;
  90. });
  91. });
  92. },
  93. /**
  94. * @param {Access} access
  95. * @param {Object} data
  96. * @param {Integer} data.id
  97. * @return {Promise}
  98. */
  99. update: (access, data) => {
  100. let create_certificate = data.certificate_id === 'new';
  101. if (create_certificate) {
  102. delete data.certificate_id;
  103. }
  104. return access.can('proxy_hosts:update', data.id)
  105. .then(access_data => {
  106. // Get a list of the domain names and check each of them against existing records
  107. let domain_name_check_promises = [];
  108. if (typeof data.domain_names !== 'undefined') {
  109. data.domain_names.map(function (domain_name) {
  110. domain_name_check_promises.push(internalHost.isHostnameTaken(domain_name, 'proxy', data.id));
  111. });
  112. return Promise.all(domain_name_check_promises)
  113. .then(check_results => {
  114. check_results.map(function (result) {
  115. if (result.is_taken) {
  116. throw new error.ValidationError(result.hostname + ' is already in use');
  117. }
  118. });
  119. });
  120. }
  121. })
  122. .then(() => {
  123. return internalProxyHost.get(access, {id: data.id});
  124. })
  125. .then(row => {
  126. if (row.id !== data.id) {
  127. // Sanity check that something crazy hasn't happened
  128. throw new error.InternalValidationError('Proxy Host could not be updated, IDs do not match: ' + row.id + ' !== ' + data.id);
  129. }
  130. if (create_certificate) {
  131. return internalCertificate.createQuickCertificate(access, {
  132. domain_names: data.domain_names || row.domain_names,
  133. meta: _.assign({}, row.meta, data.meta)
  134. })
  135. .then(cert => {
  136. // update host with cert id
  137. data.certificate_id = cert.id;
  138. })
  139. .then(() => {
  140. return row;
  141. });
  142. } else {
  143. return row;
  144. }
  145. })
  146. .then(row => {
  147. // Add domain_names to the data in case it isn't there, so that the audit log renders correctly. The order is important here.
  148. data = _.assign({}, {
  149. domain_names: row.domain_names
  150. },data);
  151. return proxyHostModel
  152. .query()
  153. .where({id: data.id})
  154. .patch(data)
  155. .then(saved_row => {
  156. // Add to audit log
  157. return internalAuditLog.add(access, {
  158. action: 'updated',
  159. object_type: 'proxy-host',
  160. object_id: row.id,
  161. meta: data
  162. })
  163. .then(() => {
  164. return _.omit(saved_row, omissions());
  165. });
  166. });
  167. })
  168. .then(() => {
  169. return internalProxyHost.get(access, {
  170. id: data.id,
  171. expand: ['owner', 'certificate']
  172. })
  173. .then(row => {
  174. // Configure nginx
  175. return internalNginx.configure(proxyHostModel, 'proxy_host', row)
  176. .then(() => {
  177. return _.omit(row, omissions());
  178. });
  179. });
  180. });
  181. },
  182. /**
  183. * @param {Access} access
  184. * @param {Object} data
  185. * @param {Integer} data.id
  186. * @param {Array} [data.expand]
  187. * @param {Array} [data.omit]
  188. * @return {Promise}
  189. */
  190. get: (access, data) => {
  191. if (typeof data === 'undefined') {
  192. data = {};
  193. }
  194. return access.can('proxy_hosts:get', data.id)
  195. .then(access_data => {
  196. let query = proxyHostModel
  197. .query()
  198. .where('is_deleted', 0)
  199. .andWhere('id', data.id)
  200. .allowEager('[owner,access_list,certificate]')
  201. .first();
  202. if (access_data.permission_visibility !== 'all') {
  203. query.andWhere('owner_user_id', access.token.get('attrs').id);
  204. }
  205. // Custom omissions
  206. if (typeof data.omit !== 'undefined' && data.omit !== null) {
  207. query.omit(data.omit);
  208. }
  209. if (typeof data.expand !== 'undefined' && data.expand !== null) {
  210. query.eager('[' + data.expand.join(', ') + ']');
  211. }
  212. return query;
  213. })
  214. .then(row => {
  215. if (row) {
  216. return _.omit(row, omissions());
  217. } else {
  218. throw new error.ItemNotFoundError(data.id);
  219. }
  220. });
  221. },
  222. /**
  223. * @param {Access} access
  224. * @param {Object} data
  225. * @param {Integer} data.id
  226. * @param {String} [data.reason]
  227. * @returns {Promise}
  228. */
  229. delete: (access, data) => {
  230. return access.can('proxy_hosts:delete', data.id)
  231. .then(() => {
  232. return internalProxyHost.get(access, {id: data.id});
  233. })
  234. .then(row => {
  235. if (!row) {
  236. throw new error.ItemNotFoundError(data.id);
  237. }
  238. return proxyHostModel
  239. .query()
  240. .where('id', row.id)
  241. .patch({
  242. is_deleted: 1
  243. })
  244. .then(() => {
  245. // Delete Nginx Config
  246. return internalNginx.deleteConfig('proxy_host', row)
  247. .then(() => {
  248. return internalNginx.reload();
  249. });
  250. })
  251. .then(() => {
  252. // Add to audit log
  253. return internalAuditLog.add(access, {
  254. action: 'deleted',
  255. object_type: 'proxy-host',
  256. object_id: row.id,
  257. meta: _.omit(row, omissions())
  258. });
  259. });
  260. })
  261. .then(() => {
  262. return true;
  263. });
  264. },
  265. /**
  266. * All Hosts
  267. *
  268. * @param {Access} access
  269. * @param {Array} [expand]
  270. * @param {String} [search_query]
  271. * @returns {Promise}
  272. */
  273. getAll: (access, expand, search_query) => {
  274. return access.can('proxy_hosts:list')
  275. .then(access_data => {
  276. let query = proxyHostModel
  277. .query()
  278. .where('is_deleted', 0)
  279. .groupBy('id')
  280. .omit(['is_deleted'])
  281. .allowEager('[owner,access_list,certificate]')
  282. .orderBy('domain_names', 'ASC');
  283. if (access_data.permission_visibility !== 'all') {
  284. query.andWhere('owner_user_id', access.token.get('attrs').id);
  285. }
  286. // Query is used for searching
  287. if (typeof search_query === 'string') {
  288. query.where(function () {
  289. this.where('domain_names', 'like', '%' + search_query + '%');
  290. });
  291. }
  292. if (typeof expand !== 'undefined' && expand !== null) {
  293. query.eager('[' + expand.join(', ') + ']');
  294. }
  295. return query;
  296. });
  297. },
  298. /**
  299. * Report use
  300. *
  301. * @param {Integer} user_id
  302. * @param {String} visibility
  303. * @returns {Promise}
  304. */
  305. getCount: (user_id, visibility) => {
  306. let query = proxyHostModel
  307. .query()
  308. .count('id as count')
  309. .where('is_deleted', 0);
  310. if (visibility !== 'all') {
  311. query.andWhere('owner_user_id', user_id);
  312. }
  313. return query.first()
  314. .then(row => {
  315. return parseInt(row.count, 10);
  316. });
  317. }
  318. };
  319. module.exports = internalProxyHost;