proxy-host.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. const _ = require('lodash');
  2. const error = require('../lib/error');
  3. const utils = require('../lib/utils');
  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(() => {
  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 proxyHostModel
  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 internalProxyHost.update(access, {
  59. id: row.id,
  60. certificate_id: cert.id
  61. });
  62. })
  63. .then(() => {
  64. return row;
  65. });
  66. } else {
  67. return row;
  68. }
  69. })
  70. .then((row) => {
  71. // re-fetch with cert
  72. return internalProxyHost.get(access, {
  73. id: row.id,
  74. expand: ['certificate', 'owner', 'access_list.[clients,items]']
  75. });
  76. })
  77. .then((row) => {
  78. // Configure nginx
  79. return internalNginx.configure(proxyHostModel, 'proxy_host', row)
  80. .then(() => {
  81. return row;
  82. });
  83. })
  84. .then((row) => {
  85. // Audit log
  86. data.meta = _.assign({}, data.meta || {}, row.meta);
  87. // Add to audit log
  88. return internalAuditLog.add(access, {
  89. action: 'created',
  90. object_type: 'proxy-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('proxy_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, 'proxy', 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 internalProxyHost.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('Proxy 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 proxyHostModel
  159. .query()
  160. .where({id: data.id})
  161. .patch(data)
  162. .then(utils.omitRow(omissions()))
  163. .then((saved_row) => {
  164. // Add to audit log
  165. return internalAuditLog.add(access, {
  166. action: 'updated',
  167. object_type: 'proxy-host',
  168. object_id: row.id,
  169. meta: data
  170. })
  171. .then(() => {
  172. return saved_row;
  173. });
  174. });
  175. })
  176. .then(() => {
  177. return internalProxyHost.get(access, {
  178. id: data.id,
  179. expand: ['owner', 'certificate', 'access_list.[clients,items]']
  180. })
  181. .then((row) => {
  182. if (!row.enabled) {
  183. // No need to add nginx config if host is disabled
  184. return row;
  185. }
  186. // Configure nginx
  187. return internalNginx.configure(proxyHostModel, 'proxy_host', row)
  188. .then((new_meta) => {
  189. row.meta = new_meta;
  190. row = internalHost.cleanRowCertificateMeta(row);
  191. return _.omit(row, omissions());
  192. });
  193. });
  194. });
  195. },
  196. /**
  197. * @param {Access} access
  198. * @param {Object} data
  199. * @param {Number} data.id
  200. * @param {Array} [data.expand]
  201. * @param {Array} [data.omit]
  202. * @return {Promise}
  203. */
  204. get: (access, data) => {
  205. if (typeof data === 'undefined') {
  206. data = {};
  207. }
  208. return access.can('proxy_hosts:get', data.id)
  209. .then((access_data) => {
  210. let query = proxyHostModel
  211. .query()
  212. .where('is_deleted', 0)
  213. .andWhere('id', data.id)
  214. .allowGraph('[owner,access_list.[clients,items],certificate]')
  215. .first();
  216. if (access_data.permission_visibility !== 'all') {
  217. query.andWhere('owner_user_id', access.token.getUserId(1));
  218. }
  219. if (typeof data.expand !== 'undefined' && data.expand !== null) {
  220. query.withGraphFetched('[' + data.expand.join(', ') + ']');
  221. }
  222. return query.then(utils.omitRow(omissions()));
  223. })
  224. .then((row) => {
  225. if (!row || !row.id) {
  226. throw new error.ItemNotFoundError(data.id);
  227. }
  228. row = internalHost.cleanRowCertificateMeta(row);
  229. // Custom omissions
  230. if (typeof data.omit !== 'undefined' && data.omit !== null) {
  231. row = _.omit(row, data.omit);
  232. }
  233. return row;
  234. });
  235. },
  236. /**
  237. * @param {Access} access
  238. * @param {Object} data
  239. * @param {Number} data.id
  240. * @param {String} [data.reason]
  241. * @returns {Promise}
  242. */
  243. delete: (access, data) => {
  244. return access.can('proxy_hosts:delete', data.id)
  245. .then(() => {
  246. return internalProxyHost.get(access, {id: data.id});
  247. })
  248. .then((row) => {
  249. if (!row || !row.id) {
  250. throw new error.ItemNotFoundError(data.id);
  251. }
  252. return proxyHostModel
  253. .query()
  254. .where('id', row.id)
  255. .patch({
  256. is_deleted: 1
  257. })
  258. .then(() => {
  259. // Delete Nginx Config
  260. return internalNginx.deleteConfig('proxy_host', row)
  261. .then(() => {
  262. return internalNginx.reload();
  263. });
  264. })
  265. .then(() => {
  266. // Add to audit log
  267. return internalAuditLog.add(access, {
  268. action: 'deleted',
  269. object_type: 'proxy-host',
  270. object_id: row.id,
  271. meta: _.omit(row, omissions())
  272. });
  273. });
  274. })
  275. .then(() => {
  276. return true;
  277. });
  278. },
  279. /**
  280. * @param {Access} access
  281. * @param {Object} data
  282. * @param {Number} data.id
  283. * @param {String} [data.reason]
  284. * @returns {Promise}
  285. */
  286. enable: (access, data) => {
  287. return access.can('proxy_hosts:update', data.id)
  288. .then(() => {
  289. return internalProxyHost.get(access, {
  290. id: data.id,
  291. expand: ['certificate', 'owner', 'access_list']
  292. });
  293. })
  294. .then((row) => {
  295. if (!row || !row.id) {
  296. throw new error.ItemNotFoundError(data.id);
  297. } else if (row.enabled) {
  298. throw new error.ValidationError('Host is already enabled');
  299. }
  300. row.enabled = 1;
  301. return proxyHostModel
  302. .query()
  303. .where('id', row.id)
  304. .patch({
  305. enabled: 1
  306. })
  307. .then(() => {
  308. // Configure nginx
  309. return internalNginx.configure(proxyHostModel, 'proxy_host', row);
  310. })
  311. .then(() => {
  312. // Add to audit log
  313. return internalAuditLog.add(access, {
  314. action: 'enabled',
  315. object_type: 'proxy-host',
  316. object_id: row.id,
  317. meta: _.omit(row, omissions())
  318. });
  319. });
  320. })
  321. .then(() => {
  322. return true;
  323. });
  324. },
  325. /**
  326. * @param {Access} access
  327. * @param {Object} data
  328. * @param {Number} data.id
  329. * @param {String} [data.reason]
  330. * @returns {Promise}
  331. */
  332. disable: (access, data) => {
  333. return access.can('proxy_hosts:update', data.id)
  334. .then(() => {
  335. return internalProxyHost.get(access, {id: data.id});
  336. })
  337. .then((row) => {
  338. if (!row || !row.id) {
  339. throw new error.ItemNotFoundError(data.id);
  340. } else if (!row.enabled) {
  341. throw new error.ValidationError('Host is already disabled');
  342. }
  343. row.enabled = 0;
  344. return proxyHostModel
  345. .query()
  346. .where('id', row.id)
  347. .patch({
  348. enabled: 0
  349. })
  350. .then(() => {
  351. // Delete Nginx Config
  352. return internalNginx.deleteConfig('proxy_host', row)
  353. .then(() => {
  354. return internalNginx.reload();
  355. });
  356. })
  357. .then(() => {
  358. // Add to audit log
  359. return internalAuditLog.add(access, {
  360. action: 'disabled',
  361. object_type: 'proxy-host',
  362. object_id: row.id,
  363. meta: _.omit(row, omissions())
  364. });
  365. });
  366. })
  367. .then(() => {
  368. return true;
  369. });
  370. },
  371. /**
  372. * All Hosts
  373. *
  374. * @param {Access} access
  375. * @param {Array} [expand]
  376. * @param {String} [search_query]
  377. * @returns {Promise}
  378. */
  379. getAll: (access, expand, search_query) => {
  380. return access.can('proxy_hosts:list')
  381. .then((access_data) => {
  382. let query = proxyHostModel
  383. .query()
  384. .where('is_deleted', 0)
  385. .groupBy('id')
  386. .allowGraph('[owner,access_list,certificate]')
  387. .orderBy('domain_names', 'ASC');
  388. if (access_data.permission_visibility !== 'all') {
  389. query.andWhere('owner_user_id', access.token.getUserId(1));
  390. }
  391. // Query is used for searching
  392. if (typeof search_query === 'string') {
  393. query.where(function () {
  394. this.where('domain_names', 'like', '%' + search_query + '%');
  395. });
  396. }
  397. if (typeof expand !== 'undefined' && expand !== null) {
  398. query.withGraphFetched('[' + expand.join(', ') + ']');
  399. }
  400. return query.then(utils.omitRows(omissions()));
  401. })
  402. .then((rows) => {
  403. if (typeof expand !== 'undefined' && expand !== null && expand.indexOf('certificate') !== -1) {
  404. return internalHost.cleanAllRowsCertificateMeta(rows);
  405. }
  406. return rows;
  407. });
  408. },
  409. /**
  410. * Report use
  411. *
  412. * @param {Number} user_id
  413. * @param {String} visibility
  414. * @returns {Promise}
  415. */
  416. getCount: (user_id, visibility) => {
  417. let query = proxyHostModel
  418. .query()
  419. .count('id as count')
  420. .where('is_deleted', 0);
  421. if (visibility !== 'all') {
  422. query.andWhere('owner_user_id', user_id);
  423. }
  424. return query.first()
  425. .then((row) => {
  426. return parseInt(row.count, 10);
  427. });
  428. }
  429. };
  430. module.exports = internalProxyHost;