access-list.js 14 KB

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