Skip to content

Commit fec3683

Browse files
committedMar 17, 2023
- Updated objection, knex, liquidjs, signale and sqlite3 packages
- Changes for objection migration - Moved common access template code to an include - Fixed access rules configuration generation
1 parent 00aeef7 commit fec3683

30 files changed

+819
-316
lines changed
 

‎backend/internal/access-list.js

+18-23
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ const fs = require('fs');
33
const batchflow = require('batchflow');
44
const logger = require('../logger').access;
55
const error = require('../lib/error');
6+
const utils = require('../lib/utils');
67
const accessListModel = require('../models/access_list');
78
const accessListAuthModel = require('../models/access_list_auth');
89
const accessListClientModel = require('../models/access_list_client');
910
const proxyHostModel = require('../models/proxy_host');
1011
const internalAuditLog = require('./audit-log');
1112
const internalNginx = require('./nginx');
12-
const utils = require('../lib/utils');
1313

1414
function omissions () {
1515
return ['is_deleted'];
@@ -27,13 +27,13 @@ const internalAccessList = {
2727
.then((/*access_data*/) => {
2828
return accessListModel
2929
.query()
30-
.omit(omissions())
3130
.insertAndFetch({
3231
name: data.name,
3332
satisfy_any: data.satisfy_any,
3433
pass_auth: data.pass_auth,
3534
owner_user_id: access.token.getUserId(1)
36-
});
35+
})
36+
.then(utils.omitRow(omissions()));
3737
})
3838
.then((row) => {
3939
data.id = row.id;
@@ -256,35 +256,31 @@ const internalAccessList = {
256256
.joinRaw('LEFT JOIN `proxy_host` ON `proxy_host`.`access_list_id` = `access_list`.`id` AND `proxy_host`.`is_deleted` = 0')
257257
.where('access_list.is_deleted', 0)
258258
.andWhere('access_list.id', data.id)
259-
.allowEager('[owner,items,clients,proxy_hosts.[certificate,access_list.[clients,items]]]')
260-
.omit(['access_list.is_deleted'])
259+
.allowGraph('[owner,items,clients,proxy_hosts.[certificate,access_list.[clients,items]]]')
261260
.first();
262261

263262
if (access_data.permission_visibility !== 'all') {
264263
query.andWhere('access_list.owner_user_id', access.token.getUserId(1));
265264
}
266265

267-
// Custom omissions
268-
if (typeof data.omit !== 'undefined' && data.omit !== null) {
269-
query.omit(data.omit);
270-
}
271-
272266
if (typeof data.expand !== 'undefined' && data.expand !== null) {
273-
query.eager('[' + data.expand.join(', ') + ']');
267+
query.withGraphFetched('[' + data.expand.join(', ') + ']');
274268
}
275269

276-
return query;
270+
return query.then(utils.omitRow(omissions()));
277271
})
278272
.then((row) => {
279-
if (row) {
280-
if (!skip_masking && typeof row.items !== 'undefined' && row.items) {
281-
row = internalAccessList.maskItems(row);
282-
}
283-
284-
return _.omit(row, omissions());
285-
} else {
273+
if (!row) {
286274
throw new error.ItemNotFoundError(data.id);
287275
}
276+
if (!skip_masking && typeof row.items !== 'undefined' && row.items) {
277+
row = internalAccessList.maskItems(row);
278+
}
279+
// Custom omissions
280+
if (typeof data.omit !== 'undefined' && data.omit !== null) {
281+
row = _.omit(row, data.omit);
282+
}
283+
return row;
288284
});
289285
},
290286

@@ -381,8 +377,7 @@ const internalAccessList = {
381377
.joinRaw('LEFT JOIN `proxy_host` ON `proxy_host`.`access_list_id` = `access_list`.`id` AND `proxy_host`.`is_deleted` = 0')
382378
.where('access_list.is_deleted', 0)
383379
.groupBy('access_list.id')
384-
.omit(['access_list.is_deleted'])
385-
.allowEager('[owner,items,clients]')
380+
.allowGraph('[owner,items,clients]')
386381
.orderBy('access_list.name', 'ASC');
387382

388383
if (access_data.permission_visibility !== 'all') {
@@ -397,10 +392,10 @@ const internalAccessList = {
397392
}
398393

399394
if (typeof expand !== 'undefined' && expand !== null) {
400-
query.eager('[' + expand.join(', ') + ']');
395+
query.withGraphFetched('[' + expand.join(', ') + ']');
401396
}
402397

403-
return query;
398+
return query.then(utils.omitRows(omissions()));
404399
})
405400
.then((rows) => {
406401
if (rows) {

‎backend/internal/audit-log.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const internalAuditLog = {
1919
.orderBy('created_on', 'DESC')
2020
.orderBy('id', 'DESC')
2121
.limit(100)
22-
.allowEager('[user]');
22+
.allowGraph('[user]');
2323

2424
// Query is used for searching
2525
if (typeof search_query === 'string') {
@@ -29,7 +29,7 @@ const internalAuditLog = {
2929
}
3030

3131
if (typeof expand !== 'undefined' && expand !== null) {
32-
query.eager('[' + expand.join(', ') + ']');
32+
query.withGraphFetched('[' + expand.join(', ') + ']');
3333
}
3434

3535
return query;

‎backend/internal/certificate.js

+16-20
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ const internalCertificate = {
121121

122122
return certificateModel
123123
.query()
124-
.omit(omissions())
125-
.insertAndFetch(data);
124+
.insertAndFetch(data)
125+
.then(utils.omitRow(omissions()));
126126
})
127127
.then((certificate) => {
128128
if (certificate.provider === 'letsencrypt') {
@@ -269,8 +269,8 @@ const internalCertificate = {
269269

270270
return certificateModel
271271
.query()
272-
.omit(omissions())
273272
.patchAndFetchById(row.id, data)
273+
.then(utils.omitRow(omissions()))
274274
.then((saved_row) => {
275275
saved_row.meta = internalCertificate.cleanMeta(saved_row.meta);
276276
data.meta = internalCertificate.cleanMeta(data.meta);
@@ -288,7 +288,7 @@ const internalCertificate = {
288288
meta: _.omit(data, ['expires_on']) // this prevents json circular reference because expires_on might be raw
289289
})
290290
.then(() => {
291-
return _.omit(saved_row, omissions());
291+
return saved_row;
292292
});
293293
});
294294
});
@@ -313,30 +313,28 @@ const internalCertificate = {
313313
.query()
314314
.where('is_deleted', 0)
315315
.andWhere('id', data.id)
316-
.allowEager('[owner]')
316+
.allowGraph('[owner]')
317317
.first();
318318

319319
if (access_data.permission_visibility !== 'all') {
320320
query.andWhere('owner_user_id', access.token.getUserId(1));
321321
}
322322

323-
// Custom omissions
324-
if (typeof data.omit !== 'undefined' && data.omit !== null) {
325-
query.omit(data.omit);
326-
}
327-
328323
if (typeof data.expand !== 'undefined' && data.expand !== null) {
329-
query.eager('[' + data.expand.join(', ') + ']');
324+
query.withGraphFetched('[' + data.expand.join(', ') + ']');
330325
}
331326

332-
return query;
327+
return query.then(utils.omitRow(omissions()));
333328
})
334329
.then((row) => {
335-
if (row) {
336-
return _.omit(row, omissions());
337-
} else {
330+
if (!row) {
338331
throw new error.ItemNotFoundError(data.id);
339332
}
333+
// Custom omissions
334+
if (typeof data.omit !== 'undefined' && data.omit !== null) {
335+
row = _.omit(row, data.omit);
336+
}
337+
return row;
340338
});
341339
},
342340

@@ -466,8 +464,7 @@ const internalCertificate = {
466464
.query()
467465
.where('is_deleted', 0)
468466
.groupBy('id')
469-
.omit(['is_deleted'])
470-
.allowEager('[owner]')
467+
.allowGraph('[owner]')
471468
.orderBy('nice_name', 'ASC');
472469

473470
if (access_data.permission_visibility !== 'all') {
@@ -482,10 +479,10 @@ const internalCertificate = {
482479
}
483480

484481
if (typeof expand !== 'undefined' && expand !== null) {
485-
query.eager('[' + expand.join(', ') + ']');
482+
query.withGraphFetched('[' + expand.join(', ') + ']');
486483
}
487484

488-
return query;
485+
return query.then(utils.omitRows(omissions()));
489486
});
490487
},
491488

@@ -662,7 +659,6 @@ const internalCertificate = {
662659
meta: _.clone(row.meta) // Prevent the update method from changing this value that we'll use later
663660
})
664661
.then((certificate) => {
665-
console.log('ROWMETA:', row.meta);
666662
certificate.meta = row.meta;
667663
return internalCertificate.writeCustomCert(certificate);
668664
});

‎backend/internal/dead-host.js

+15-18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const _ = require('lodash');
22
const error = require('../lib/error');
3+
const utils = require('../lib/utils');
34
const deadHostModel = require('../models/dead_host');
45
const internalHost = require('./host');
56
const internalNginx = require('./nginx');
@@ -49,8 +50,8 @@ const internalDeadHost = {
4950

5051
return deadHostModel
5152
.query()
52-
.omit(omissions())
53-
.insertAndFetch(data);
53+
.insertAndFetch(data)
54+
.then(utils.omitRow(omissions()));
5455
})
5556
.then((row) => {
5657
if (create_certificate) {
@@ -218,31 +219,28 @@ const internalDeadHost = {
218219
.query()
219220
.where('is_deleted', 0)
220221
.andWhere('id', data.id)
221-
.allowEager('[owner,certificate]')
222+
.allowGraph('[owner,certificate]')
222223
.first();
223224

224225
if (access_data.permission_visibility !== 'all') {
225226
query.andWhere('owner_user_id', access.token.getUserId(1));
226227
}
227228

228-
// Custom omissions
229-
if (typeof data.omit !== 'undefined' && data.omit !== null) {
230-
query.omit(data.omit);
231-
}
232-
233229
if (typeof data.expand !== 'undefined' && data.expand !== null) {
234-
query.eager('[' + data.expand.join(', ') + ']');
230+
query.withGraphFetched('[' + data.expand.join(', ') + ']');
235231
}
236232

237-
return query;
233+
return query.then(utils.omitRow(omissions()));
238234
})
239235
.then((row) => {
240-
if (row) {
241-
row = internalHost.cleanRowCertificateMeta(row);
242-
return _.omit(row, omissions());
243-
} else {
236+
if (!row) {
244237
throw new error.ItemNotFoundError(data.id);
245238
}
239+
// Custom omissions
240+
if (typeof data.omit !== 'undefined' && data.omit !== null) {
241+
row = _.omit(row, data.omit);
242+
}
243+
return row;
246244
});
247245
},
248246

@@ -404,8 +402,7 @@ const internalDeadHost = {
404402
.query()
405403
.where('is_deleted', 0)
406404
.groupBy('id')
407-
.omit(['is_deleted'])
408-
.allowEager('[owner,certificate]')
405+
.allowGraph('[owner,certificate]')
409406
.orderBy('domain_names', 'ASC');
410407

411408
if (access_data.permission_visibility !== 'all') {
@@ -420,10 +417,10 @@ const internalDeadHost = {
420417
}
421418

422419
if (typeof expand !== 'undefined' && expand !== null) {
423-
query.eager('[' + expand.join(', ') + ']');
420+
query.withGraphFetched('[' + expand.join(', ') + ']');
424421
}
425422

426-
return query;
423+
return query.then(utils.omitRows(omissions()));
427424
})
428425
.then((rows) => {
429426
if (typeof expand !== 'undefined' && expand !== null && expand.indexOf('certificate') !== -1) {

‎backend/internal/ip_ranges.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ const https = require('https');
22
const fs = require('fs');
33
const logger = require('../logger').ip_ranges;
44
const error = require('../lib/error');
5+
const utils = require('../lib/utils');
56
const internalNginx = require('./nginx');
6-
const { Liquid } = require('liquidjs');
77

88
const CLOUDFRONT_URL = 'https://ip-ranges.amazonaws.com/ip-ranges.json';
99
const CLOUDFARE_V4_URL = 'https://www.cloudflare.com/ips-v4';
@@ -119,10 +119,7 @@ const internalIpRanges = {
119119
* @returns {Promise}
120120
*/
121121
generateConfig: (ip_ranges) => {
122-
let renderEngine = new Liquid({
123-
root: __dirname + '/../templates/'
124-
});
125-
122+
const renderEngine = utils.getRenderEngine();
126123
return new Promise((resolve, reject) => {
127124
let template = null;
128125
let filename = '/etc/nginx/conf.d/include/ip_ranges.conf';

‎backend/internal/nginx.js

+4-17
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ const fs = require('fs');
33
const logger = require('../logger').nginx;
44
const utils = require('../lib/utils');
55
const error = require('../lib/error');
6-
const { Liquid } = require('liquidjs');
76
const debug_mode = process.env.NODE_ENV !== 'production' || !!process.env.DEBUG;
87

98
const internalNginx = {
@@ -138,8 +137,6 @@ const internalNginx = {
138137
* @returns {Promise}
139138
*/
140139
renderLocations: (host) => {
141-
142-
//logger.info('host = ' + JSON.stringify(host, null, 2));
143140
return new Promise((resolve, reject) => {
144141
let template;
145142

@@ -150,9 +147,7 @@ const internalNginx = {
150147
return;
151148
}
152149

153-
let renderer = new Liquid({
154-
root: __dirname + '/../templates/'
155-
});
150+
const renderEngine = utils.getRenderEngine();
156151
let renderedLocations = '';
157152

158153
const locationRendering = async () => {
@@ -170,10 +165,8 @@ const internalNginx = {
170165
locationCopy.forward_path = `/${splitted.join('/')}`;
171166
}
172167

173-
//logger.info('locationCopy = ' + JSON.stringify(locationCopy, null, 2));
174-
175168
// eslint-disable-next-line
176-
renderedLocations += await renderer.parseAndRender(template, locationCopy);
169+
renderedLocations += await renderEngine.parseAndRender(template, locationCopy);
177170
}
178171

179172
};
@@ -195,11 +188,7 @@ const internalNginx = {
195188
logger.info('Generating ' + nice_host_type + ' Config:', JSON.stringify(host, null, 2));
196189
}
197190

198-
// logger.info('host = ' + JSON.stringify(host, null, 2));
199-
200-
let renderEngine = new Liquid({
201-
root: __dirname + '/../templates/'
202-
});
191+
const renderEngine = utils.getRenderEngine();
203192

204193
return new Promise((resolve, reject) => {
205194
let template = null;
@@ -283,9 +272,7 @@ const internalNginx = {
283272
logger.info('Generating LetsEncrypt Request Config:', certificate);
284273
}
285274

286-
let renderEngine = new Liquid({
287-
root: __dirname + '/../templates/'
288-
});
275+
const renderEngine = utils.getRenderEngine();
289276

290277
return new Promise((resolve, reject) => {
291278
let template = null;

0 commit comments

Comments
 (0)