access-list.js 13 KB

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