ssl-passthrough-host.js 8.9 KB

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