ssl-passthrough-host.js 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. const _ = require('lodash');
  2. const error = require('../lib/error');
  3. const passthroughHostModel = require('../models/ssl_passthrough_host');
  4. const internalHost = require('./host');
  5. const internalNginx = require('./nginx');
  6. const internalAuditLog = require('./audit-log');
  7. function omissions () {
  8. return ['is_deleted'];
  9. }
  10. const internalPassthroughHost = {
  11. /**
  12. * @param {Access} access
  13. * @param {Object} data
  14. * @returns {Promise}
  15. */
  16. create: (access, data) => {
  17. return access.can('ssl_passthrough_hosts:create', data)
  18. .then(() => {
  19. // Get a list of the domain names and check each of them against existing records
  20. let domain_name_check_promises = [];
  21. data.domain_names.map(function (domain_name) {
  22. domain_name_check_promises.push(internalHost.isHostnameTaken(domain_name));
  23. });
  24. return Promise.all(domain_name_check_promises)
  25. .then((check_results) => {
  26. check_results.map(function (result) {
  27. if (result.is_taken) {
  28. throw new error.ValidationError(result.hostname + ' is already in use');
  29. }
  30. });
  31. });
  32. }).then((/*access_data*/) => {
  33. data.owner_user_id = access.token.getUserId(1);
  34. if (typeof data.meta === 'undefined') {
  35. data.meta = {};
  36. }
  37. return passthroughHostModel
  38. .query()
  39. .omit(omissions())
  40. .insertAndFetch(data);
  41. })
  42. .then((row) => {
  43. // Configure nginx
  44. return internalNginx.configure(passthroughHostModel, 'ssl_passthrough_host', {})
  45. .then(() => {
  46. return internalPassthroughHost.get(access, {id: row.id, expand: ['owner']});
  47. });
  48. })
  49. .then((row) => {
  50. // Add to audit log
  51. return internalAuditLog.add(access, {
  52. action: 'created',
  53. object_type: 'ssl_passthrough_host',
  54. object_id: row.id,
  55. meta: data
  56. })
  57. .then(() => {
  58. return row;
  59. });
  60. });
  61. },
  62. /**
  63. * @param {Access} access
  64. * @param {Object} data
  65. * @param {Number} data.id
  66. * @return {Promise}
  67. */
  68. update: (access, data) => {
  69. return access.can('ssl_passthrough_hosts:update', data.id)
  70. .then((/*access_data*/) => {
  71. // Get a list of the domain names and check each of them against existing records
  72. let domain_name_check_promises = [];
  73. if (typeof data.domain_names !== 'undefined') {
  74. data.domain_names.map(function (domain_name) {
  75. domain_name_check_promises.push(internalHost.isHostnameTaken(domain_name, 'ssl_passthrough', data.id));
  76. });
  77. return Promise.all(domain_name_check_promises)
  78. .then((check_results) => {
  79. check_results.map(function (result) {
  80. if (result.is_taken) {
  81. throw new error.ValidationError(result.hostname + ' is already in use');
  82. }
  83. });
  84. });
  85. }
  86. }).then((/*access_data*/) => {
  87. return internalPassthroughHost.get(access, {id: data.id});
  88. })
  89. .then((row) => {
  90. if (row.id !== data.id) {
  91. // Sanity check that something crazy hasn't happened
  92. throw new error.InternalValidationError('SSL Passthrough Host could not be updated, IDs do not match: ' + row.id + ' !== ' + data.id);
  93. }
  94. return passthroughHostModel
  95. .query()
  96. .omit(omissions())
  97. .patchAndFetchById(row.id, data)
  98. .then(() => {
  99. return internalNginx.configure(passthroughHostModel, 'ssl_passthrough_host', {})
  100. .then(() => {
  101. return internalPassthroughHost.get(access, {id: row.id, expand: ['owner']});
  102. });
  103. })
  104. .then((saved_row) => {
  105. // Add to audit log
  106. return internalAuditLog.add(access, {
  107. action: 'updated',
  108. object_type: 'ssl_passthrough_host',
  109. object_id: row.id,
  110. meta: data
  111. })
  112. .then(() => {
  113. return _.omit(saved_row, omissions());
  114. });
  115. });
  116. });
  117. },
  118. /**
  119. * @param {Access} access
  120. * @param {Object} data
  121. * @param {Number} data.id
  122. * @param {Array} [data.expand]
  123. * @param {Array} [data.omit]
  124. * @return {Promise}
  125. */
  126. get: (access, data) => {
  127. if (typeof data === 'undefined') {
  128. data = {};
  129. }
  130. return access.can('ssl_passthrough_hosts:get', data.id)
  131. .then((access_data) => {
  132. let query = passthroughHostModel
  133. .query()
  134. .where('is_deleted', 0)
  135. .andWhere('id', data.id)
  136. .allowEager('[owner]')
  137. .first();
  138. if (access_data.permission_visibility !== 'all') {
  139. query.andWhere('owner_user_id', access.token.getUserId(1));
  140. }
  141. // Custom omissions
  142. if (typeof data.omit !== 'undefined' && data.omit !== null) {
  143. query.omit(data.omit);
  144. }
  145. if (typeof data.expand !== 'undefined' && data.expand !== null) {
  146. query.eager('[' + data.expand.join(', ') + ']');
  147. }
  148. return query;
  149. })
  150. .then((row) => {
  151. if (row) {
  152. return _.omit(row, omissions());
  153. } else {
  154. throw new error.ItemNotFoundError(data.id);
  155. }
  156. });
  157. },
  158. /**
  159. * @param {Access} access
  160. * @param {Object} data
  161. * @param {Number} data.id
  162. * @param {String} [data.reason]
  163. * @returns {Promise}
  164. */
  165. delete: (access, data) => {
  166. return access.can('ssl_passthrough_hosts:delete', data.id)
  167. .then(() => {
  168. return internalPassthroughHost.get(access, {id: data.id});
  169. })
  170. .then((row) => {
  171. if (!row) {
  172. throw new error.ItemNotFoundError(data.id);
  173. }
  174. return passthroughHostModel
  175. .query()
  176. .where('id', row.id)
  177. .patch({
  178. is_deleted: 1
  179. })
  180. .then(() => {
  181. // Update Nginx Config
  182. return internalNginx.configure(passthroughHostModel, 'ssl_passthrough_host', {})
  183. .then(() => {
  184. return internalNginx.reload();
  185. });
  186. })
  187. .then(() => {
  188. // Add to audit log
  189. return internalAuditLog.add(access, {
  190. action: 'deleted',
  191. object_type: 'ssl_passthrough_host',
  192. object_id: row.id,
  193. meta: _.omit(row, omissions())
  194. });
  195. });
  196. })
  197. .then(() => {
  198. return true;
  199. });
  200. },
  201. /**
  202. * @param {Access} access
  203. * @param {Object} data
  204. * @param {Number} data.id
  205. * @param {String} [data.reason]
  206. * @returns {Promise}
  207. */
  208. enable: (access, data) => {
  209. return access.can('ssl_passthrough_hosts:update', data.id)
  210. .then(() => {
  211. return internalPassthroughHost.get(access, {
  212. id: data.id,
  213. expand: ['owner']
  214. });
  215. })
  216. .then((row) => {
  217. if (!row) {
  218. throw new error.ItemNotFoundError(data.id);
  219. } else if (row.enabled) {
  220. throw new error.ValidationError('Host is already enabled');
  221. }
  222. row.enabled = 1;
  223. return passthroughHostModel
  224. .query()
  225. .where('id', row.id)
  226. .patch({
  227. enabled: 1
  228. })
  229. .then(() => {
  230. // Configure nginx
  231. return internalNginx.configure(passthroughHostModel, 'ssl_passthrough_host', {});
  232. })
  233. .then(() => {
  234. // Add to audit log
  235. return internalAuditLog.add(access, {
  236. action: 'enabled',
  237. object_type: 'ssl_passthrough_host',
  238. object_id: row.id,
  239. meta: _.omit(row, omissions())
  240. });
  241. });
  242. })
  243. .then(() => {
  244. return true;
  245. });
  246. },
  247. /**
  248. * @param {Access} access
  249. * @param {Object} data
  250. * @param {Number} data.id
  251. * @param {String} [data.reason]
  252. * @returns {Promise}
  253. */
  254. disable: (access, data) => {
  255. return access.can('ssl_passthrough_hosts:update', data.id)
  256. .then(() => {
  257. return internalPassthroughHost.get(access, {id: data.id});
  258. })
  259. .then((row) => {
  260. if (!row) {
  261. throw new error.ItemNotFoundError(data.id);
  262. } else if (!row.enabled) {
  263. throw new error.ValidationError('Host is already disabled');
  264. }
  265. row.enabled = 0;
  266. return passthroughHostModel
  267. .query()
  268. .where('id', row.id)
  269. .patch({
  270. enabled: 0
  271. })
  272. .then(() => {
  273. // Update Nginx Config
  274. return internalNginx.configure(passthroughHostModel, 'ssl_passthrough_host', {})
  275. .then(() => {
  276. return internalNginx.reload();
  277. });
  278. })
  279. .then(() => {
  280. // Add to audit log
  281. return internalAuditLog.add(access, {
  282. action: 'disabled',
  283. object_type: 'ssl_passthrough_host',
  284. object_id: row.id,
  285. meta: _.omit(row, omissions())
  286. });
  287. });
  288. })
  289. .then(() => {
  290. return true;
  291. });
  292. },
  293. /**
  294. * All SSL Passthrough Hosts
  295. *
  296. * @param {Access} access
  297. * @param {Array} [expand]
  298. * @param {String} [search_query]
  299. * @returns {Promise}
  300. */
  301. getAll: (access, expand, search_query) => {
  302. return access.can('ssl_passthrough_hosts:list')
  303. .then((access_data) => {
  304. let query = passthroughHostModel
  305. .query()
  306. .where('is_deleted', 0)
  307. .groupBy('id')
  308. .omit(['is_deleted'])
  309. .allowEager('[owner]')
  310. .orderBy('domain_name', 'ASC');
  311. if (access_data.permission_visibility !== 'all') {
  312. query.andWhere('owner_user_id', access.token.getUserId(1));
  313. }
  314. // Query is used for searching
  315. if (typeof search_query === 'string') {
  316. query.where(function () {
  317. this.where('domain_name', 'like', '%' + search_query + '%');
  318. });
  319. }
  320. if (typeof expand !== 'undefined' && expand !== null) {
  321. query.eager('[' + expand.join(', ') + ']');
  322. }
  323. return query;
  324. });
  325. },
  326. /**
  327. * Report use
  328. *
  329. * @param {Number} user_id
  330. * @param {String} visibility
  331. * @returns {Promise}
  332. */
  333. getCount: (user_id, visibility) => {
  334. let query = passthroughHostModel
  335. .query()
  336. .count('id as count')
  337. .where('is_deleted', 0);
  338. if (visibility !== 'all') {
  339. query.andWhere('owner_user_id', user_id);
  340. }
  341. return query.first()
  342. .then((row) => {
  343. return parseInt(row.count, 10);
  344. });
  345. }
  346. };
  347. module.exports = internalPassthroughHost;