Skip to content

Commit a8d63d0

Browse files
author
Jamie Curnow
committed
SSL certificate upload support
1 parent 291cb96 commit a8d63d0

File tree

9 files changed

+347
-24
lines changed

9 files changed

+347
-24
lines changed

src/backend/app.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
const path = require('path');
44
const express = require('express');
55
const bodyParser = require('body-parser');
6+
const fileUpload = require('express-fileupload');
67
const compression = require('compression');
78
const log = require('./logger').express;
89

910
/**
1011
* App
1112
*/
1213
const app = express();
14+
app.use(fileUpload());
1315
app.use(bodyParser.json());
1416
app.use(bodyParser.urlencoded({extended: true}));
1517

src/backend/internal/host.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ const deadHostModel = require('../models/dead_host');
88

99
const internalHost = {
1010

11+
allowed_ssl_files: ['other_certificate', 'other_certificate_key'],
12+
1113
/**
1214
* Internal use only, checks to see if the ___domain is already taken by any other record
1315
*
@@ -64,6 +66,21 @@ const internalHost = {
6466
});
6567
},
6668

69+
/**
70+
* Cleans the ssl keys from the meta object and sets them to "true"
71+
*
72+
* @param {Object} meta
73+
* @returns {*}
74+
*/
75+
cleanMeta: function (meta) {
76+
internalHost.allowed_ssl_files.map(key => {
77+
if (typeof meta[key] !== 'undefined' && meta[key]) {
78+
meta[key] = true;
79+
}
80+
});
81+
return meta;
82+
},
83+
6784
/**
6885
* Private call only
6986
*

src/backend/internal/proxy-host.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ const internalProxyHost = {
9696
.omit(omissions())
9797
.patchAndFetchById(row.id, data)
9898
.then(saved_row => {
99+
saved_row.meta = internalHost.cleanMeta(saved_row.meta);
99100
return _.omit(saved_row, omissions());
100101
});
101102
});
@@ -144,6 +145,7 @@ const internalProxyHost = {
144145
})
145146
.then(row => {
146147
if (row) {
148+
row.meta = internalHost.cleanMeta(row.meta);
147149
return _.omit(row, omissions());
148150
} else {
149151
throw new error.ItemNotFoundError(data.id);
@@ -180,6 +182,32 @@ const internalProxyHost = {
180182
});
181183
},
182184

185+
/**
186+
* @param {Access} access
187+
* @param {Object} data
188+
* @param {Integer} data.id
189+
* @param {Object} data.files
190+
* @returns {Promise}
191+
*/
192+
setCerts: (access, data) => {
193+
return internalProxyHost.get(access, {id: data.id})
194+
.then(row => {
195+
_.map(data.files, (file, name) => {
196+
if (internalHost.allowed_ssl_files.indexOf(name) !== -1) {
197+
row.meta[name] = file.data.toString();
198+
}
199+
});
200+
201+
return internalProxyHost.update(access, {
202+
id: data.id,
203+
meta: row.meta
204+
});
205+
})
206+
.then(row => {
207+
return _.pick(row.meta, internalHost.allowed_ssl_files);
208+
});
209+
},
210+
183211
/**
184212
* All Hosts
185213
*
@@ -215,6 +243,13 @@ const internalProxyHost = {
215243
}
216244

217245
return query;
246+
})
247+
.then(rows => {
248+
rows.map(row => {
249+
row.meta = internalHost.cleanMeta(row.meta);
250+
});
251+
252+
return rows;
218253
});
219254
},
220255

src/backend/lib/access.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,8 @@ module.exports = function (token_string) {
234234
});
235235
},
236236

237+
reloadObjects: this.loadObjects,
238+
237239
/**
238240
*
239241
* @param {String} permission
@@ -248,7 +250,6 @@ module.exports = function (token_string) {
248250
return this.init()
249251
.then(() => {
250252
// Initialised, token decoded ok
251-
252253
return this.getObjectSchema(permission)
253254
.then(objectSchema => {
254255
let data_schema = {
@@ -275,9 +276,9 @@ module.exports = function (token_string) {
275276

276277
permissionSchema.properties[permission] = require('./access/' + permission.replace(/:/gim, '-') + '.json');
277278

278-
//logger.debug('objectSchema:', JSON.stringify(objectSchema, null, 2));
279-
//logger.debug('permissionSchema:', JSON.stringify(permissionSchema, null, 2));
280-
//logger.debug('data_schema:', JSON.stringify(data_schema, null, 2));
279+
// logger.info('objectSchema', JSON.stringify(objectSchema, null, 2));
280+
// logger.info('permissionSchema', JSON.stringify(permissionSchema, null, 2));
281+
// logger.info('data_schema', JSON.stringify(data_schema, null, 2));
281282

282283
let ajv = validator({
283284
verbose: true,
@@ -301,8 +302,9 @@ module.exports = function (token_string) {
301302
});
302303
})
303304
.catch(err => {
304-
logger.error(err.message);
305-
logger.error(err.errors);
305+
err.permission = permission;
306+
err.permission_data = data;
307+
logger.error(permission, data, err.message);
306308

307309
throw new error.PermissionError('Permission Denied', err);
308310
});

src/backend/routes/api/nginx/proxy_hosts.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,38 @@ router
147147
.catch(next);
148148
});
149149

150+
/**
151+
* Specific proxy-host Certificates
152+
*
153+
* /api/nginx/proxy-hosts/123/certificates
154+
*/
155+
router
156+
.route('/:host_id/certificates')
157+
.options((req, res) => {
158+
res.sendStatus(204);
159+
})
160+
.all(jwtdecode()) // preferred so it doesn't apply to nonexistent routes
161+
162+
/**
163+
* POST /api/nginx/proxy-hosts/123/certificates
164+
*
165+
* Upload certifications
166+
*/
167+
.post((req, res, next) => {
168+
if (!req.files) {
169+
res.status(400)
170+
.send({error: 'No files were uploaded'});
171+
} else {
172+
internalProxyHost.setCerts(res.locals.access, {
173+
id: parseInt(req.params.host_id, 10),
174+
files: req.files
175+
})
176+
.then(result => {
177+
res.status(200)
178+
.send(result);
179+
})
180+
.catch(next);
181+
}
182+
});
183+
150184
module.exports = router;

0 commit comments

Comments
 (0)