Skip to content

Commit 981d5a1

Browse files
committed
Initial work for disabling hosts
1 parent 48f2bb4 commit 981d5a1

File tree

11 files changed

+368
-31
lines changed

11 files changed

+368
-31
lines changed

src/backend/internal/proxy-host.js

Lines changed: 100 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ const internalProxyHost = {
104104
/**
105105
* @param {Access} access
106106
* @param {Object} data
107-
* @param {Integer} data.id
107+
* @param {Number} data.id
108108
* @return {Promise}
109109
*/
110110
update: (access, data) => {
@@ -192,7 +192,7 @@ const internalProxyHost = {
192192
return internalNginx.configure(proxyHostModel, 'proxy_host', row)
193193
.then(new_meta => {
194194
row.meta = new_meta;
195-
row = internalHost.cleanRowCertificateMeta(row);
195+
row = internalHost.cleanRowCertificateMeta(row);
196196
return _.omit(row, omissions());
197197
});
198198
});
@@ -202,7 +202,7 @@ const internalProxyHost = {
202202
/**
203203
* @param {Access} access
204204
* @param {Object} data
205-
* @param {Integer} data.id
205+
* @param {Number} data.id
206206
* @param {Array} [data.expand]
207207
* @param {Array} [data.omit]
208208
* @return {Promise}
@@ -249,7 +249,7 @@ const internalProxyHost = {
249249
/**
250250
* @param {Access} access
251251
* @param {Object} data
252-
* @param {Integer} data.id
252+
* @param {Number} data.id
253253
* @param {String} [data.reason]
254254
* @returns {Promise}
255255
*/
@@ -291,6 +291,101 @@ const internalProxyHost = {
291291
});
292292
},
293293

294+
/**
295+
* @param {Access} access
296+
* @param {Object} data
297+
* @param {Number} data.id
298+
* @param {String} [data.reason]
299+
* @returns {Promise}
300+
*/
301+
enable: (access, data) => {
302+
return access.can('proxy_hosts:update', data.id)
303+
.then(() => {
304+
return internalProxyHost.get(access, {id: data.id});
305+
})
306+
.then(row => {
307+
if (!row) {
308+
throw new error.ItemNotFoundError(data.id);
309+
} else if (row.enabled) {
310+
throw new error.ValidationError('Host is already enabled');
311+
}
312+
313+
row.enabled = 1;
314+
315+
return proxyHostModel
316+
.query()
317+
.where('id', row.id)
318+
.patch({
319+
enabled: 1
320+
})
321+
.then(() => {
322+
// Configure nginx
323+
return internalNginx.configure(proxyHostModel, 'proxy_host', row);
324+
})
325+
.then(() => {
326+
// Add to audit log
327+
return internalAuditLog.add(access, {
328+
action: 'enabled',
329+
object_type: 'proxy-host',
330+
object_id: row.id,
331+
meta: _.omit(row, omissions())
332+
});
333+
});
334+
})
335+
.then(() => {
336+
return true;
337+
});
338+
},
339+
340+
/**
341+
* @param {Access} access
342+
* @param {Object} data
343+
* @param {Number} data.id
344+
* @param {String} [data.reason]
345+
* @returns {Promise}
346+
*/
347+
disable: (access, data) => {
348+
return access.can('proxy_hosts:update', data.id)
349+
.then(() => {
350+
return internalProxyHost.get(access, {id: data.id});
351+
})
352+
.then(row => {
353+
if (!row) {
354+
throw new error.ItemNotFoundError(data.id);
355+
} else if (!row.enabled) {
356+
throw new error.ValidationError('Host is already disabled');
357+
}
358+
359+
row.enabled = 0;
360+
361+
return proxyHostModel
362+
.query()
363+
.where('id', row.id)
364+
.patch({
365+
enabled: 0
366+
})
367+
.then(() => {
368+
// Delete Nginx Config
369+
return internalNginx.deleteConfig('proxy_host', row)
370+
.then(() => {
371+
return internalNginx.reload();
372+
});
373+
})
374+
.then(() => {
375+
// Add to audit log
376+
return internalAuditLog.add(access, {
377+
action: 'disabled',
378+
object_type: 'proxy-host',
379+
object_id: row.id,
380+
meta: _.omit(row, omissions())
381+
});
382+
});
383+
})
384+
.then(() => {
385+
return true;
386+
});
387+
},
388+
294389
/**
295390
* All Hosts
296391
*
@@ -339,7 +434,7 @@ const internalProxyHost = {
339434
/**
340435
* Report use
341436
*
342-
* @param {Integer} user_id
437+
* @param {Number} user_id
343438
* @param {String} visibility
344439
* @returns {Promise}
345440
*/
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
'use strict';
2+
3+
const migrate_name = 'disabled';
4+
const logger = require('../logger').migrate;
5+
6+
/**
7+
* Migrate
8+
*
9+
* @see http://knexjs.org/#Schema
10+
*
11+
* @param {Object} knex
12+
* @param {Promise} Promise
13+
* @returns {Promise}
14+
*/
15+
exports.up = function (knex/*, Promise*/) {
16+
logger.info('[' + migrate_name + '] Migrating Up...');
17+
18+
return knex.schema.table('proxy_host', function (proxy_host) {
19+
proxy_host.integer('enabled').notNull().unsigned().defaultTo(1);
20+
})
21+
.then(() => {
22+
logger.info('[' + migrate_name + '] proxy_host Table altered');
23+
24+
return knex.schema.table('redirection_host', function (redirection_host) {
25+
redirection_host.integer('enabled').notNull().unsigned().defaultTo(1);
26+
});
27+
})
28+
.then(() => {
29+
logger.info('[' + migrate_name + '] redirection_host Table altered');
30+
31+
return knex.schema.table('dead_host', function (dead_host) {
32+
dead_host.integer('enabled').notNull().unsigned().defaultTo(1);
33+
});
34+
})
35+
.then(() => {
36+
logger.info('[' + migrate_name + '] dead_host Table altered');
37+
38+
return knex.schema.table('stream', function (stream) {
39+
stream.integer('enabled').notNull().unsigned().defaultTo(1);
40+
});
41+
})
42+
.then(() => {
43+
logger.info('[' + migrate_name + '] stream Table altered');
44+
});
45+
};
46+
47+
/**
48+
* Undo Migrate
49+
*
50+
* @param {Object} knex
51+
* @param {Promise} Promise
52+
* @returns {Promise}
53+
*/
54+
exports.down = function (knex, Promise) {
55+
logger.warn('[' + migrate_name + '] You can\'t migrate down this one.');
56+
return Promise.resolve(true);
57+
};

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

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ router
2020
.options((req, res) => {
2121
res.sendStatus(204);
2222
})
23-
.all(jwtdecode()) // preferred so it doesn't apply to nonexistent routes
23+
.all(jwtdecode())
2424

2525
/**
2626
* GET /api/nginx/proxy-hosts
@@ -79,7 +79,7 @@ router
7979
.options((req, res) => {
8080
res.sendStatus(204);
8181
})
82-
.all(jwtdecode()) // preferred so it doesn't apply to nonexistent routes
82+
.all(jwtdecode())
8383

8484
/**
8585
* GET /api/nginx/proxy-hosts/123
@@ -147,4 +147,52 @@ router
147147
.catch(next);
148148
});
149149

150+
/**
151+
* Enable proxy-host
152+
*
153+
* /api/nginx/proxy-hosts/123/enable
154+
*/
155+
router
156+
.route('/:host_id/enable')
157+
.options((req, res) => {
158+
res.sendStatus(204);
159+
})
160+
.all(jwtdecode())
161+
162+
/**
163+
* POST /api/nginx/proxy-hosts/123/enable
164+
*/
165+
.post((req, res, next) => {
166+
internalProxyHost.enable(res.locals.access, {id: parseInt(req.params.host_id, 10)})
167+
.then(result => {
168+
res.status(200)
169+
.send(result);
170+
})
171+
.catch(next);
172+
});
173+
174+
/**
175+
* Disable proxy-host
176+
*
177+
* /api/nginx/proxy-hosts/123/disable
178+
*/
179+
router
180+
.route('/:host_id/disable')
181+
.options((req, res) => {
182+
res.sendStatus(204);
183+
})
184+
.all(jwtdecode())
185+
186+
/**
187+
* POST /api/nginx/proxy-hosts/123/disable
188+
*/
189+
.post((req, res, next) => {
190+
internalProxyHost.disable(res.locals.access, {id: parseInt(req.params.host_id, 10)})
191+
.then(result => {
192+
res.status(200)
193+
.send(result);
194+
})
195+
.catch(next);
196+
});
197+
150198
module.exports = router;

src/backend/schema/definitions.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,11 @@
172172
"pattern": "^(?:\\*\\.)?(?:[^.*]+\\.?)+[^.]$"
173173
}
174174
},
175+
"enabled": {
176+
"description": "Is Enabled",
177+
"example": true,
178+
"type": "boolean"
179+
},
175180
"ssl_enabled": {
176181
"description": "Is SSL Enabled",
177182
"example": true,

src/backend/schema/endpoints/proxy-hosts.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@
5858
"advanced_config": {
5959
"type": "string"
6060
},
61+
"enabled": {
62+
"$ref": "../definitions.json#/definitions/enabled"
63+
},
6164
"meta": {
6265
"type": "object"
6366
}
@@ -108,6 +111,9 @@
108111
"advanced_config": {
109112
"$ref": "#/definitions/advanced_config"
110113
},
114+
"enabled": {
115+
"$ref": "#/definitions/enabled"
116+
},
111117
"meta": {
112118
"$ref": "#/definitions/meta"
113119
}
@@ -186,6 +192,9 @@
186192
"advanced_config": {
187193
"$ref": "#/definitions/advanced_config"
188194
},
195+
"enabled": {
196+
"$ref": "#/definitions/enabled"
197+
},
189198
"meta": {
190199
"$ref": "#/definitions/meta"
191200
}
@@ -247,6 +256,9 @@
247256
"advanced_config": {
248257
"$ref": "#/definitions/advanced_config"
249258
},
259+
"enabled": {
260+
"$ref": "#/definitions/enabled"
261+
},
250262
"meta": {
251263
"$ref": "#/definitions/meta"
252264
}
@@ -271,6 +283,34 @@
271283
"targetSchema": {
272284
"type": "boolean"
273285
}
286+
},
287+
{
288+
"title": "Enable",
289+
"description": "Enables a existing Proxy Host",
290+
"href": "/nginx/proxy-hosts/{definitions.identity.example}/enable",
291+
"access": "private",
292+
"method": "POST",
293+
"rel": "update",
294+
"http_header": {
295+
"$ref": "../examples.json#/definitions/auth_header"
296+
},
297+
"targetSchema": {
298+
"type": "boolean"
299+
}
300+
},
301+
{
302+
"title": "Disable",
303+
"description": "Disables a existing Proxy Host",
304+
"href": "/nginx/proxy-hosts/{definitions.identity.example}/disable",
305+
"access": "private",
306+
"method": "POST",
307+
"rel": "update",
308+
"http_header": {
309+
"$ref": "../examples.json#/definitions/auth_header"
310+
},
311+
"targetSchema": {
312+
"type": "boolean"
313+
}
274314
}
275315
]
276316
}

src/backend/templates/proxy_host.conf

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{% include "_header_comment.conf" %}
22

3+
{% if enabled %}
34
server {
45
set $forward_scheme {{ forward_scheme }};
56
set $server "{{ forward_host }}";
@@ -33,3 +34,4 @@ server {
3334
include conf.d/include/proxy.conf;
3435
}
3536
}
37+
{% endif %}

0 commit comments

Comments
 (0)