access-list.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. const _ = require('lodash');
  2. const fs = require('fs');
  3. const batchflow = require('batchflow');
  4. const logger = require('../logger').access;
  5. const error = require('../lib/error');
  6. const accessListModel = require('../models/access_list');
  7. const accessListAuthModel = require('../models/access_list_auth');
  8. const accessListClientModel = require('../models/access_list_client');
  9. const proxyHostModel = require('../models/proxy_host');
  10. const internalAuditLog = require('./audit-log');
  11. const internalNginx = require('./nginx');
  12. const utils = require('../lib/utils');
  13. function omissions () {
  14. return ['is_deleted'];
  15. }
  16. const internalAccessList = {
  17. /**
  18. * @param {Access} access
  19. * @param {Object} data
  20. * @returns {Promise}
  21. */
  22. create: (access, data) => {
  23. return access.can('access_lists:create', data)
  24. .then((/*access_data*/) => {
  25. return accessListModel
  26. .query()
  27. .omit(omissions())
  28. .insertAndFetch({
  29. name: data.name,
  30. satisfy_any: data.satisfy_any,
  31. owner_user_id: access.token.getUserId(1)
  32. });
  33. })
  34. .then((row) => {
  35. data.id = row.id;
  36. let promises = [];
  37. // Now add the items
  38. data.items.map((item) => {
  39. promises.push(accessListAuthModel
  40. .query()
  41. .insert({
  42. access_list_id: row.id,
  43. username: item.username,
  44. password: item.password
  45. })
  46. );
  47. });
  48. // Now add the clients
  49. if (typeof data.clients !== 'undefined' && data.clients) {
  50. data.clients.map((client) => {
  51. promises.push(accessListClientModel
  52. .query()
  53. .insert({
  54. access_list_id: row.id,
  55. address: client.address,
  56. directive: client.directive
  57. })
  58. );
  59. });
  60. }
  61. return Promise.all(promises);
  62. })
  63. .then(() => {
  64. // re-fetch with expansions
  65. return internalAccessList.get(access, {
  66. id: data.id,
  67. expand: ['owner', 'items', 'clients', 'proxy_hosts.access_list.[clients,items]']
  68. }, true /* <- skip masking */);
  69. })
  70. .then((row) => {
  71. // Audit log
  72. data.meta = _.assign({}, data.meta || {}, row.meta);
  73. return internalAccessList.build(row)
  74. .then(() => {
  75. if (row.proxy_host_count) {
  76. return internalNginx.bulkGenerateConfigs('proxy_host', row.proxy_hosts);
  77. }
  78. })
  79. .then(() => {
  80. // Add to audit log
  81. return internalAuditLog.add(access, {
  82. action: 'created',
  83. object_type: 'access-list',
  84. object_id: row.id,
  85. meta: internalAccessList.maskItems(data)
  86. });
  87. })
  88. .then(() => {
  89. return internalAccessList.maskItems(row);
  90. });
  91. });
  92. },
  93. /**
  94. * @param {Access} access
  95. * @param {Object} data
  96. * @param {Integer} data.id
  97. * @param {String} [data.name]
  98. * @param {String} [data.items]
  99. * @return {Promise}
  100. */
  101. update: (access, data) => {
  102. return access.can('access_lists:update', data.id)
  103. .then((/*access_data*/) => {
  104. return internalAccessList.get(access, {id: data.id});
  105. })
  106. .then((row) => {
  107. if (row.id !== data.id) {
  108. // Sanity check that something crazy hasn't happened
  109. throw new error.InternalValidationError('Access List could not be updated, IDs do not match: ' + row.id + ' !== ' + data.id);
  110. }
  111. })
  112. .then(() => {
  113. // patch name if specified
  114. if (typeof data.name !== 'undefined' && data.name) {
  115. return accessListModel
  116. .query()
  117. .where({id: data.id})
  118. .patch({
  119. name: data.name,
  120. satisfy_any: data.satisfy_any,
  121. });
  122. }
  123. })
  124. .then(() => {
  125. // Check for items and add/update/remove them
  126. if (typeof data.items !== 'undefined' && data.items) {
  127. let promises = [];
  128. let items_to_keep = [];
  129. data.items.map(function (item) {
  130. if (item.password) {
  131. promises.push(accessListAuthModel
  132. .query()
  133. .insert({
  134. access_list_id: data.id,
  135. username: item.username,
  136. password: item.password
  137. })
  138. );
  139. } else {
  140. // This was supplied with an empty password, which means keep it but don't change the password
  141. items_to_keep.push(item.username);
  142. }
  143. });
  144. let query = accessListAuthModel
  145. .query()
  146. .delete()
  147. .where('access_list_id', data.id);
  148. if (items_to_keep.length) {
  149. query.andWhere('username', 'NOT IN', items_to_keep);
  150. }
  151. return query
  152. .then(() => {
  153. // Add new items
  154. if (promises.length) {
  155. return Promise.all(promises);
  156. }
  157. });
  158. }
  159. })
  160. .then(() => {
  161. // Check for clients and add/update/remove them
  162. if (typeof data.clients !== 'undefined' && data.clients) {
  163. let promises = [];
  164. data.clients.map(function (client) {
  165. if (client.address) {
  166. promises.push(accessListClientModel
  167. .query()
  168. .insert({
  169. access_list_id: data.id,
  170. address: client.address,
  171. directive: client.directive
  172. })
  173. );
  174. }
  175. });
  176. let query = accessListClientModel
  177. .query()
  178. .delete()
  179. .where('access_list_id', data.id);
  180. return query
  181. .then(() => {
  182. // Add new items
  183. if (promises.length) {
  184. return Promise.all(promises);
  185. }
  186. });
  187. }
  188. })
  189. .then(() => {
  190. // Add to audit log
  191. return internalAuditLog.add(access, {
  192. action: 'updated',
  193. object_type: 'access-list',
  194. object_id: data.id,
  195. meta: internalAccessList.maskItems(data)
  196. });
  197. })
  198. .then(() => {
  199. // re-fetch with expansions
  200. return internalAccessList.get(access, {
  201. id: data.id,
  202. expand: ['owner', 'items', 'clients', 'proxy_hosts.access_list.[clients,items]']
  203. }, true /* <- skip masking */);
  204. })
  205. .then((row) => {
  206. return internalAccessList.build(row)
  207. .then(() => {
  208. if (row.proxy_host_count) {
  209. return internalNginx.bulkGenerateConfigs('proxy_host', row.proxy_hosts);
  210. }
  211. })
  212. .then(() => {
  213. return internalAccessList.maskItems(row);
  214. });
  215. });
  216. },
  217. /**
  218. * @param {Access} access
  219. * @param {Object} data
  220. * @param {Integer} data.id
  221. * @param {Array} [data.expand]
  222. * @param {Array} [data.omit]
  223. * @param {Boolean} [skip_masking]
  224. * @return {Promise}
  225. */
  226. get: (access, data, skip_masking) => {
  227. if (typeof data === 'undefined') {
  228. data = {};
  229. }
  230. return access.can('access_lists:get', data.id)
  231. .then((access_data) => {
  232. let query = accessListModel
  233. .query()
  234. .select('access_list.*', accessListModel.raw('COUNT(proxy_host.id) as proxy_host_count'))
  235. .joinRaw('LEFT JOIN `proxy_host` ON `proxy_host`.`access_list_id` = `access_list`.`id` AND `proxy_host`.`is_deleted` = 0')
  236. .where('access_list.is_deleted', 0)
  237. .andWhere('access_list.id', data.id)
  238. .allowEager('[owner,items,clients,proxy_hosts.[*, access_list.[clients,items]]]')
  239. .omit(['access_list.is_deleted'])
  240. .first();
  241. if (access_data.permission_visibility !== 'all') {
  242. query.andWhere('access_list.owner_user_id', access.token.getUserId(1));
  243. }
  244. // Custom omissions
  245. if (typeof data.omit !== 'undefined' && data.omit !== null) {
  246. query.omit(data.omit);
  247. }
  248. if (typeof data.expand !== 'undefined' && data.expand !== null) {
  249. query.eager('[' + data.expand.join(', ') + ']');
  250. }
  251. return query;
  252. })
  253. .then((row) => {
  254. if (row) {
  255. if (!skip_masking && typeof row.items !== 'undefined' && row.items) {
  256. row = internalAccessList.maskItems(row);
  257. }
  258. return _.omit(row, omissions());
  259. } else {
  260. throw new error.ItemNotFoundError(data.id);
  261. }
  262. });
  263. },
  264. /**
  265. * @param {Access} access
  266. * @param {Object} data
  267. * @param {Integer} data.id
  268. * @param {String} [data.reason]
  269. * @returns {Promise}
  270. */
  271. delete: (access, data) => {
  272. return access.can('access_lists:delete', data.id)
  273. .then(() => {
  274. return internalAccessList.get(access, {id: data.id, expand: ['proxy_hosts', 'items', 'clients']});
  275. })
  276. .then((row) => {
  277. if (!row) {
  278. throw new error.ItemNotFoundError(data.id);
  279. }
  280. // 1. update row to be deleted
  281. // 2. update any proxy hosts that were using it (ignoring permissions)
  282. // 3. reconfigure those hosts
  283. // 4. audit log
  284. // 1. update row to be deleted
  285. return accessListModel
  286. .query()
  287. .where('id', row.id)
  288. .patch({
  289. is_deleted: 1
  290. })
  291. .then(() => {
  292. // 2. update any proxy hosts that were using it (ignoring permissions)
  293. if (row.proxy_hosts) {
  294. return proxyHostModel
  295. .query()
  296. .where('access_list_id', '=', row.id)
  297. .patch({access_list_id: 0})
  298. .then(() => {
  299. // 3. reconfigure those hosts, then reload nginx
  300. // set the access_list_id to zero for these items
  301. row.proxy_hosts.map(function (val, idx) {
  302. row.proxy_hosts[idx].access_list_id = 0;
  303. });
  304. return internalNginx.bulkGenerateConfigs('proxy_host', row.proxy_hosts);
  305. })
  306. .then(() => {
  307. return internalNginx.reload();
  308. });
  309. }
  310. })
  311. .then(() => {
  312. // delete the htpasswd file
  313. let htpasswd_file = internalAccessList.getFilename(row);
  314. try {
  315. fs.unlinkSync(htpasswd_file);
  316. } catch (err) {
  317. // do nothing
  318. }
  319. })
  320. .then(() => {
  321. // 4. audit log
  322. return internalAuditLog.add(access, {
  323. action: 'deleted',
  324. object_type: 'access-list',
  325. object_id: row.id,
  326. meta: _.omit(internalAccessList.maskItems(row), ['is_deleted', 'proxy_hosts'])
  327. });
  328. });
  329. })
  330. .then(() => {
  331. return true;
  332. });
  333. },
  334. /**
  335. * All Lists
  336. *
  337. * @param {Access} access
  338. * @param {Array} [expand]
  339. * @param {String} [search_query]
  340. * @returns {Promise}
  341. */
  342. getAll: (access, expand, search_query) => {
  343. return access.can('access_lists:list')
  344. .then((access_data) => {
  345. let query = accessListModel
  346. .query()
  347. .select('access_list.*', accessListModel.raw('COUNT(proxy_host.id) as proxy_host_count'))
  348. .joinRaw('LEFT JOIN `proxy_host` ON `proxy_host`.`access_list_id` = `access_list`.`id` AND `proxy_host`.`is_deleted` = 0')
  349. .where('access_list.is_deleted', 0)
  350. .groupBy('access_list.id')
  351. .omit(['access_list.is_deleted'])
  352. .allowEager('[owner,items,clients]')
  353. .orderBy('access_list.name', 'ASC');
  354. if (access_data.permission_visibility !== 'all') {
  355. query.andWhere('access_list.owner_user_id', access.token.getUserId(1));
  356. }
  357. // Query is used for searching
  358. if (typeof search_query === 'string') {
  359. query.where(function () {
  360. this.where('name', 'like', '%' + search_query + '%');
  361. });
  362. }
  363. if (typeof expand !== 'undefined' && expand !== null) {
  364. query.eager('[' + expand.join(', ') + ']');
  365. }
  366. return query;
  367. })
  368. .then((rows) => {
  369. if (rows) {
  370. rows.map(function (row, idx) {
  371. if (typeof row.items !== 'undefined' && row.items) {
  372. rows[idx] = internalAccessList.maskItems(row);
  373. }
  374. });
  375. }
  376. return rows;
  377. });
  378. },
  379. /**
  380. * Report use
  381. *
  382. * @param {Integer} user_id
  383. * @param {String} visibility
  384. * @returns {Promise}
  385. */
  386. getCount: (user_id, visibility) => {
  387. let query = accessListModel
  388. .query()
  389. .count('id as count')
  390. .where('is_deleted', 0);
  391. if (visibility !== 'all') {
  392. query.andWhere('owner_user_id', user_id);
  393. }
  394. return query.first()
  395. .then((row) => {
  396. return parseInt(row.count, 10);
  397. });
  398. },
  399. /**
  400. * @param {Object} list
  401. * @returns {Object}
  402. */
  403. maskItems: (list) => {
  404. if (list && typeof list.items !== 'undefined') {
  405. list.items.map(function (val, idx) {
  406. let repeat_for = 8;
  407. let first_char = '*';
  408. if (typeof val.password !== 'undefined' && val.password) {
  409. repeat_for = val.password.length - 1;
  410. first_char = val.password.charAt(0);
  411. }
  412. list.items[idx].hint = first_char + ('*').repeat(repeat_for);
  413. list.items[idx].password = '';
  414. });
  415. }
  416. return list;
  417. },
  418. /**
  419. * @param {Object} list
  420. * @param {Integer} list.id
  421. * @returns {String}
  422. */
  423. getFilename: (list) => {
  424. return '/data/access/' + list.id;
  425. },
  426. /**
  427. * @param {Object} list
  428. * @param {Integer} list.id
  429. * @param {String} list.name
  430. * @param {Array} list.items
  431. * @returns {Promise}
  432. */
  433. build: (list) => {
  434. logger.info('Building Access file #' + list.id + ' for: ' + list.name);
  435. return new Promise((resolve, reject) => {
  436. let htpasswd_file = internalAccessList.getFilename(list);
  437. // 1. remove any existing access file
  438. try {
  439. fs.unlinkSync(htpasswd_file);
  440. } catch (err) {
  441. // do nothing
  442. }
  443. // 2. create empty access file
  444. try {
  445. fs.writeFileSync(htpasswd_file, '', {encoding: 'utf8'});
  446. resolve(htpasswd_file);
  447. } catch (err) {
  448. reject(err);
  449. }
  450. })
  451. .then((htpasswd_file) => {
  452. // 3. generate password for each user
  453. if (list.items.length) {
  454. return new Promise((resolve, reject) => {
  455. batchflow(list.items).sequential()
  456. .each((i, item, next) => {
  457. if (typeof item.password !== 'undefined' && item.password.length) {
  458. logger.info('Adding: ' + item.username);
  459. utils.exec('/usr/bin/htpasswd -b "' + htpasswd_file + '" "' + item.username + '" "' + item.password + '"')
  460. .then((/*result*/) => {
  461. next();
  462. })
  463. .catch((err) => {
  464. logger.error(err);
  465. next(err);
  466. });
  467. }
  468. })
  469. .error((err) => {
  470. logger.error(err);
  471. reject(err);
  472. })
  473. .end((results) => {
  474. logger.success('Built Access file #' + list.id + ' for: ' + list.name);
  475. resolve(results);
  476. });
  477. });
  478. }
  479. });
  480. }
  481. };
  482. module.exports = internalAccessList;