Skip to content

Commit 177bb2e

Browse files
author
Jamie Curnow
committed
Certificates UI for all hosts, Access Lists placeholder, audit log tweaks
1 parent 6920a61 commit 177bb2e

27 files changed

+406
-760
lines changed

src/backend/internal/audit-log.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const internalAuditLog = {
1919
let query = auditLogModel
2020
.query()
2121
.orderBy('created_on', 'DESC')
22+
.orderBy('id', 'DESC')
2223
.limit(100)
2324
.allowEager('[user]');
2425

src/backend/internal/dead-host.js

Lines changed: 83 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
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');
7-
const internalNginx = require('./nginx');
8-
const internalAuditLog = require('./audit-log');
3+
const _ = require('lodash');
4+
const error = require('../lib/error');
5+
const deadHostModel = require('../models/dead_host');
6+
const internalHost = require('./host');
7+
const internalNginx = require('./nginx');
8+
const internalAuditLog = require('./audit-log');
9+
const internalCertificate = require('./certificate');
910

1011
function omissions () {
1112
return ['is_deleted'];
@@ -19,6 +20,12 @@ const internalDeadHost = {
1920
* @returns {Promise}
2021
*/
2122
create: (access, data) => {
23+
let create_certificate = data.certificate_id === 'new';
24+
25+
if (create_certificate) {
26+
delete data.certificate_id;
27+
}
28+
2229
return access.can('dead_hosts:create', data)
2330
.then(access_data => {
2431
// Get a list of the ___domain names and check each of them against existing records
@@ -46,14 +53,40 @@ const internalDeadHost = {
4653
.omit(omissions())
4754
.insertAndFetch(data);
4855
})
56+
.then(row => {
57+
if (create_certificate) {
58+
return internalCertificate.createQuickCertificate(access, data)
59+
.then(cert => {
60+
// update host with cert id
61+
return internalDeadHost.update(access, {
62+
id: row.id,
63+
certificate_id: cert.id
64+
});
65+
})
66+
.then(() => {
67+
return row;
68+
});
69+
} else {
70+
return row;
71+
}
72+
})
73+
.then(row => {
74+
// re-fetch with cert
75+
return internalDeadHost.get(access, {
76+
id: row.id,
77+
expand: ['certificate', 'owner']
78+
});
79+
})
4980
.then(row => {
5081
// Configure nginx
5182
return internalNginx.configure(deadHostModel, 'dead_host', row)
5283
.then(() => {
53-
return internalDeadHost.get(access, {id: row.id, expand: ['owner']});
84+
return row;
5485
});
5586
})
5687
.then(row => {
88+
data.meta = _.assign({}, data.meta || {}, row.meta);
89+
5790
// Add to audit log
5891
return internalAuditLog.add(access, {
5992
action: 'created',
@@ -71,11 +104,15 @@ const internalDeadHost = {
71104
* @param {Access} access
72105
* @param {Object} data
73106
* @param {Integer} data.id
74-
* @param {String} [data.email]
75-
* @param {String} [data.name]
76107
* @return {Promise}
77108
*/
78109
update: (access, data) => {
110+
let create_certificate = data.certificate_id === 'new';
111+
112+
if (create_certificate) {
113+
delete data.certificate_id;
114+
}
115+
79116
return access.can('dead_hosts:update', data.id)
80117
.then(access_data => {
81118
// Get a list of the ___domain names and check each of them against existing records
@@ -105,13 +142,33 @@ const internalDeadHost = {
105142
throw new error.InternalValidationError('404 Host could not be updated, IDs do not match: ' + row.id + ' !== ' + data.id);
106143
}
107144

145+
if (create_certificate) {
146+
return internalCertificate.createQuickCertificate(access, {
147+
domain_names: data.domain_names || row.domain_names,
148+
meta: _.assign({}, row.meta, data.meta)
149+
})
150+
.then(cert => {
151+
// update host with cert id
152+
data.certificate_id = cert.id;
153+
})
154+
.then(() => {
155+
return row;
156+
});
157+
} else {
158+
return row;
159+
}
160+
})
161+
.then(row => {
162+
// Add domain_names to the data in case it isn't there, so that the audit log renders correctly. The order is important here.
163+
data = _.assign({}, {
164+
domain_names: row.domain_names
165+
},data);
166+
108167
return deadHostModel
109168
.query()
110-
.omit(omissions())
111-
.patchAndFetchById(row.id, data)
169+
.where({id: data.id})
170+
.patch(data)
112171
.then(saved_row => {
113-
saved_row.meta = internalHost.cleanMeta(saved_row.meta);
114-
115172
// Add to audit log
116173
return internalAuditLog.add(access, {
117174
action: 'updated',
@@ -123,6 +180,19 @@ const internalDeadHost = {
123180
return _.omit(saved_row, omissions());
124181
});
125182
});
183+
})
184+
.then(() => {
185+
return internalDeadHost.get(access, {
186+
id: data.id,
187+
expand: ['owner', 'certificate']
188+
})
189+
.then(row => {
190+
// Configure nginx
191+
return internalNginx.configure(deadHostModel, 'dead_host', row)
192+
.then(() => {
193+
return _.omit(row, omissions());
194+
});
195+
});
126196
});
127197
},
128198

@@ -165,7 +235,6 @@ const internalDeadHost = {
165235
})
166236
.then(row => {
167237
if (row) {
168-
row.meta = internalHost.cleanMeta(row.meta);
169238
return _.omit(row, omissions());
170239
} else {
171240
throw new error.ItemNotFoundError(data.id);
@@ -205,8 +274,6 @@ const internalDeadHost = {
205274
})
206275
.then(() => {
207276
// Add to audit log
208-
row.meta = internalHost.cleanMeta(row.meta);
209-
210277
return internalAuditLog.add(access, {
211278
action: 'deleted',
212279
object_type: 'dead-host',
@@ -220,40 +287,6 @@ const internalDeadHost = {
220287
});
221288
},
222289

223-
/**
224-
* @param {Access} access
225-
* @param {Object} data
226-
* @param {Integer} data.id
227-
* @param {Object} data.files
228-
* @returns {Promise}
229-
*/
230-
setCerts: (access, data) => {
231-
return internalDeadHost.get(access, {id: data.id})
232-
.then(row => {
233-
_.map(data.files, (file, name) => {
234-
if (internalHost.allowed_ssl_files.indexOf(name) !== -1) {
235-
row.meta[name] = file.data.toString();
236-
}
237-
});
238-
239-
return internalDeadHost.update(access, {
240-
id: data.id,
241-
meta: row.meta
242-
});
243-
})
244-
.then(row => {
245-
return internalAuditLog.add(access, {
246-
action: 'updated',
247-
object_type: 'dead-host',
248-
object_id: row.id,
249-
meta: data
250-
})
251-
.then(() => {
252-
return _.pick(row.meta, internalHost.allowed_ssl_files);
253-
});
254-
});
255-
},
256-
257290
/**
258291
* All Hosts
259292
*
@@ -289,13 +322,6 @@ const internalDeadHost = {
289322
}
290323

291324
return query;
292-
})
293-
.then(rows => {
294-
rows.map(row => {
295-
row.meta = internalHost.cleanMeta(row.meta);
296-
});
297-
298-
return rows;
299325
});
300326
},
301327

src/backend/internal/proxy-host.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,6 @@ const internalProxyHost = {
105105
* @param {Access} access
106106
* @param {Object} data
107107
* @param {Integer} data.id
108-
* @param {String} [data.email]
109-
* @param {String} [data.name]
110108
* @return {Promise}
111109
*/
112110
update: (access, data) => {
@@ -162,6 +160,11 @@ const internalProxyHost = {
162160
}
163161
})
164162
.then(row => {
163+
// Add domain_names to the data in case it isn't there, so that the audit log renders correctly. The order is important here.
164+
data = _.assign({}, {
165+
domain_names: row.domain_names
166+
},data);
167+
165168
return proxyHostModel
166169
.query()
167170
.where({id: data.id})
@@ -190,7 +193,7 @@ const internalProxyHost = {
190193
.then(() => {
191194
return _.omit(row, omissions());
192195
});
193-
})
196+
});
194197
});
195198
},
196199

0 commit comments

Comments
 (0)