Skip to content

Commit 66e25e3

Browse files
author
Jamie Curnow
committed
Audit Log items, backend stuff, help pages
1 parent a43c2d7 commit 66e25e3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+935
-133
lines changed

rootfs/etc/nginx/nginx.conf

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ http {
5151
access_log /data/logs/default.log proxy;
5252

5353
include /etc/nginx/conf.d/*.conf;
54-
include /data/nginx/*.conf;
54+
include /data/nginx/proxy_host/*.conf;
55+
include /data/nginx/redirection_host/*.conf;
56+
include /data/nginx/dead_host/*.conf;
5557
}
5658

5759
stream {

rootfs/etc/services.d/nginx/run

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
mkdir -p /tmp/nginx \
44
/data/{nginx,logs,access} \
5-
/data/nginx/stream \
5+
/data/nginx/{proxy_host,redirection_host,stream,dead_host} \
66
/var/lib/nginx/cache/{public,private}
77

88
chown root /tmp/nginx

src/backend/internal/audit-log.js

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,10 @@
11
'use strict';
22

3+
const error = require('../lib/error');
34
const auditLogModel = require('../models/audit-log');
45

56
const internalAuditLog = {
67

7-
/**
8-
* Internal use only
9-
*
10-
* @param {Object} data
11-
* @returns {Promise}
12-
*/
13-
create: data => {
14-
// TODO
15-
},
16-
178
/**
189
* All logs
1910
*
@@ -28,16 +19,14 @@ const internalAuditLog = {
2819
let query = auditLogModel
2920
.query()
3021
.orderBy('created_on', 'DESC')
31-
.limit(100);
22+
.limit(100)
23+
.allowEager('[user]');
3224

3325
// Query is used for searching
3426
if (typeof search_query === 'string') {
35-
/*
3627
query.where(function () {
37-
this.where('name', 'like', '%' + search_query + '%')
38-
.orWhere('email', 'like', '%' + search_query + '%');
28+
this.where('meta', 'like', '%' + search_query + '%');
3929
});
40-
*/
4130
}
4231

4332
if (typeof expand !== 'undefined' && expand !== null) {
@@ -46,6 +35,44 @@ const internalAuditLog = {
4635

4736
return query;
4837
});
38+
},
39+
40+
/**
41+
* This method should not be publicly used, it doesn't check certain things. It will be assumed
42+
* that permission to add to audit log is already considered, however the access token is used for
43+
* default user id determination.
44+
*
45+
* @param {Access} access
46+
* @param {Object} data
47+
* @param {String} data.action
48+
* @param {Integer} [data.user_id]
49+
* @param {Integer} [data.object_id]
50+
* @param {Integer} [data.object_type]
51+
* @param {Object} [data.meta]
52+
* @returns {Promise}
53+
*/
54+
add: (access, data) => {
55+
return new Promise((resolve, reject) => {
56+
// Default the user id
57+
if (typeof data.user_id === 'undefined' || !data.user_id) {
58+
data.user_id = access.token.get('attrs').id;
59+
}
60+
61+
if (typeof data.action === 'undefined' || !data.action) {
62+
reject(new error.InternalValidationError('Audit log entry must contain an Action'));
63+
} else {
64+
// Make sure at least 1 of the IDs are set and action
65+
resolve(auditLogModel
66+
.query()
67+
.insert({
68+
user_id: data.user_id,
69+
action: data.action,
70+
object_type: data.object_type || '',
71+
object_id: data.object_id || 0,
72+
meta: data.meta || {}
73+
}));
74+
}
75+
});
4976
}
5077
};
5178

src/backend/internal/dead-host.js

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
'use strict';
22

3-
const _ = require('lodash');
4-
const error = require('../lib/error');
5-
const deadHostModel = require('../models/dead_host');
6-
const internalHost = require('./host');
3+
const _ = require('lodash');
4+
const error = require('../lib/error');
5+
const deadHostModel = require('../models/dead_host');
6+
const internalHost = require('./host');
7+
const internalAuditLog = require('./audit-log');
78

89
function omissions () {
910
return ['is_deleted'];
@@ -49,7 +50,16 @@ const internalDeadHost = {
4950
.insertAndFetch(data);
5051
})
5152
.then(row => {
52-
return _.omit(row, omissions());
53+
// Add to audit log
54+
return internalAuditLog.add(access, {
55+
action: 'created',
56+
object_type: 'dead-host',
57+
object_id: row.id,
58+
meta: data
59+
})
60+
.then(() => {
61+
return _.omit(row, omissions());
62+
});
5363
});
5464
},
5565

@@ -97,7 +107,17 @@ const internalDeadHost = {
97107
.patchAndFetchById(row.id, data)
98108
.then(saved_row => {
99109
saved_row.meta = internalHost.cleanMeta(saved_row.meta);
100-
return _.omit(saved_row, omissions());
110+
111+
// Add to audit log
112+
return internalAuditLog.add(access, {
113+
action: 'updated',
114+
object_type: 'dead-host',
115+
object_id: row.id,
116+
meta: data
117+
})
118+
.then(() => {
119+
return _.omit(saved_row, omissions());
120+
});
101121
});
102122
});
103123
},
@@ -171,6 +191,17 @@ const internalDeadHost = {
171191
.where('id', row.id)
172192
.patch({
173193
is_deleted: 1
194+
})
195+
.then(() => {
196+
// Add to audit log
197+
row.meta = internalHost.cleanMeta(row.meta);
198+
199+
return internalAuditLog.add(access, {
200+
action: 'deleted',
201+
object_type: 'dead-host',
202+
object_id: row.id,
203+
meta: _.omit(row, omissions())
204+
});
174205
});
175206
})
176207
.then(() => {
@@ -200,7 +231,15 @@ const internalDeadHost = {
200231
});
201232
})
202233
.then(row => {
203-
return _.pick(row.meta, internalHost.allowed_ssl_files);
234+
return internalAuditLog.add(access, {
235+
action: 'updated',
236+
object_type: 'dead-host',
237+
object_id: row.id,
238+
meta: data
239+
})
240+
.then(() => {
241+
return _.pick(row.meta, internalHost.allowed_ssl_files);
242+
});
204243
});
205244
},
206245

src/backend/internal/nginx.js

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
'use strict';
2+
3+
const fs = require('fs');
4+
const Liquid = require('liquidjs');
5+
const logger = require('../logger').nginx;
6+
const utils = require('../lib/utils');
7+
const error = require('../lib/error');
8+
9+
const internalNginx = {
10+
11+
/**
12+
* @returns {Promise}
13+
*/
14+
test: () => {
15+
logger.info('Testing Nginx configuration');
16+
return utils.exec('/usr/sbin/nginx -t');
17+
},
18+
19+
/**
20+
* @returns {Promise}
21+
*/
22+
reload: () => {
23+
return internalNginx.test()
24+
.then(() => {
25+
logger.info('Reloading Nginx');
26+
return utils.exec('/usr/sbin/nginx -s reload');
27+
});
28+
},
29+
30+
/**
31+
* @param {String} host_type
32+
* @param {Integer} host_id
33+
* @returns {String}
34+
*/
35+
getConfigName: (host_type, host_id) => {
36+
host_type = host_type.replace(new RegExp('-', 'g'), '_');
37+
return '/data/nginx/' + host_type + '/' + host_id + '.conf';
38+
},
39+
40+
/**
41+
* @param {String} host_type
42+
* @param {Object} host
43+
* @returns {Promise}
44+
*/
45+
generateConfig: (host_type, host) => {
46+
let renderEngine = Liquid();
47+
host_type = host_type.replace(new RegExp('-', 'g'), '_');
48+
49+
return new Promise((resolve, reject) => {
50+
let template = null;
51+
let filename = internalNginx.getConfigName(host_type, host.id);
52+
try {
53+
template = fs.readFileSync(__dirname + '/../templates/' + host_type + '.conf', {encoding: 'utf8'});
54+
} catch (err) {
55+
reject(new error.ConfigurationError(err.message));
56+
return;
57+
}
58+
59+
return renderEngine
60+
.parseAndRender(template, host)
61+
.then(config_text => {
62+
fs.writeFileSync(filename, config_text, {encoding: 'utf8'});
63+
return true;
64+
})
65+
.catch(err => {
66+
throw new error.ConfigurationError(err.message);
67+
});
68+
});
69+
},
70+
71+
/**
72+
* @param {String} host_type
73+
* @param {Object} host
74+
* @param {Boolean} [throw_errors]
75+
* @returns {Promise}
76+
*/
77+
deleteConfig: (host_type, host, throw_errors) => {
78+
return new Promise((resolve, reject) => {
79+
try {
80+
fs.unlinkSync(internalNginx.getConfigName(host_type, host.id));
81+
} catch (err) {
82+
if (throw_errors) {
83+
reject(err);
84+
}
85+
}
86+
87+
resolve();
88+
});
89+
}
90+
};
91+
92+
module.exports = internalNginx;

src/backend/internal/proxy-host.js

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
'use strict';
22

3-
const _ = require('lodash');
4-
const error = require('../lib/error');
5-
const proxyHostModel = require('../models/proxy_host');
6-
const internalHost = require('./host');
3+
const _ = require('lodash');
4+
const error = require('../lib/error');
5+
const proxyHostModel = require('../models/proxy_host');
6+
const internalHost = require('./host');
7+
const internalAuditLog = require('./audit-log');
78

89
function omissions () {
910
return ['is_deleted'];
@@ -49,7 +50,16 @@ const internalProxyHost = {
4950
.insertAndFetch(data);
5051
})
5152
.then(row => {
52-
return _.omit(row, omissions());
53+
// Add to audit log
54+
return internalAuditLog.add(access, {
55+
action: 'created',
56+
object_type: 'proxy-host',
57+
object_id: row.id,
58+
meta: data
59+
})
60+
.then(() => {
61+
return _.omit(row, omissions());
62+
});
5363
});
5464
},
5565

@@ -97,7 +107,17 @@ const internalProxyHost = {
97107
.patchAndFetchById(row.id, data)
98108
.then(saved_row => {
99109
saved_row.meta = internalHost.cleanMeta(saved_row.meta);
100-
return _.omit(saved_row, omissions());
110+
111+
// Add to audit log
112+
return internalAuditLog.add(access, {
113+
action: 'updated',
114+
object_type: 'proxy-host',
115+
object_id: row.id,
116+
meta: data
117+
})
118+
.then(() => {
119+
return _.omit(saved_row, omissions());
120+
});
101121
});
102122
});
103123
},
@@ -171,6 +191,17 @@ const internalProxyHost = {
171191
.where('id', row.id)
172192
.patch({
173193
is_deleted: 1
194+
})
195+
.then(() => {
196+
// Add to audit log
197+
row.meta = internalHost.cleanMeta(row.meta);
198+
199+
return internalAuditLog.add(access, {
200+
action: 'deleted',
201+
object_type: 'proxy-host',
202+
object_id: row.id,
203+
meta: _.omit(row, omissions())
204+
});
174205
});
175206
})
176207
.then(() => {
@@ -200,7 +231,15 @@ const internalProxyHost = {
200231
});
201232
})
202233
.then(row => {
203-
return _.pick(row.meta, internalHost.allowed_ssl_files);
234+
return internalAuditLog.add(access, {
235+
action: 'updated',
236+
object_type: 'proxy-host',
237+
object_id: row.id,
238+
meta: data
239+
})
240+
.then(() => {
241+
return _.pick(row.meta, internalHost.allowed_ssl_files);
242+
});
204243
});
205244
},
206245

0 commit comments

Comments
 (0)