Skip to content

Commit c543a1d

Browse files
author
Jamie Curnow
committed
Finished importer, advanced nginx config for hosts, custom certs used in nginx templates
1 parent d092d4b commit c543a1d

File tree

20 files changed

+174
-72
lines changed

20 files changed

+174
-72
lines changed

TODO.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22

33
In order of importance, somewhat..
44

5-
- v1 Importer
6-
- ssl certificates
7-
- nginx advanced config
8-
- Redirection host preserve path nginx configuration
9-
- Custom ssl certificate saving to disk and usage in nginx configs
5+
- Nginx config output:
6+
- Redirection host preserve path nginx configuration
107
- Dashboard stats are caching instead of querying
118
- UI Log tail
129
- Custom Nginx Config Editor
10+
- Enable/Disable a config
1311

1412
Testing:
1513

rootfs/etc/services.d/nginx/run

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
mkdir -p /tmp/nginx/body \
44
/var/log/nginx \
55
/data/nginx \
6+
/data/custom_ssl \
67
/data/logs \
78
/data/access \
89
/data/nginx/proxy_host \

src/backend/importer.js

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -186,18 +186,13 @@ module.exports = function () {
186186

187187
// 2. rename archive folder name
188188
new_archive_path = new_archive_path + 'npm-' + certificate.id;
189-
//logger.debug('Renaming archive folder:', full_archive_path, '->', new_archive_path);
190-
191189
fs.renameSync(full_archive_path, new_archive_path);
192190

193191
return certificate;
194192
})
195193
.then(certificate => {
196194
// 3. rename live folder name
197195
new_live_path = new_live_path + 'npm-' + certificate.id;
198-
199-
//logger.debug('Renaming live folder:', full_live_path, '->', new_live_path);
200-
201196
fs.renameSync(full_live_path, new_live_path);
202197

203198
// and also update the symlinks in this folder:
@@ -211,8 +206,6 @@ module.exports = function () {
211206
];
212207

213208
names.map(function (name) {
214-
//logger.debug('Live Link:', name);
215-
216209
// remove symlink
217210
try {
218211
fs.unlinkSync(new_live_path + '/' + name[0]);
@@ -221,7 +214,6 @@ module.exports = function () {
221214
logger.error(err);
222215
}
223216

224-
//logger.debug('Creating Link:', '../../archive/npm-' + certificate.id + '/' + name[1]);
225217
// create new symlink
226218
fs.symlinkSync('../../archive/npm-' + certificate.id + '/' + name[1], name[0]);
227219
});
@@ -356,8 +348,6 @@ module.exports = function () {
356348
certificate_id = certificate_map[host.hostname];
357349
}
358350

359-
// TODO: Advanced nginx config
360-
361351
return proxyHostModel
362352
.query()
363353
.insertAndFetch({
@@ -370,6 +360,7 @@ module.exports = function () {
370360
ssl_forced: host.force_ssl || false,
371361
caching_enabled: host.asset_caching || false,
372362
block_exploits: host.block_exploits || false,
363+
advanced_config: host.advanced || '',
373364
meta: meta
374365
})
375366
.then(row => {
@@ -405,16 +396,15 @@ module.exports = function () {
405396
certificate_id = certificate_map[host.hostname];
406397
}
407398

408-
// TODO: Advanced nginx config
409-
410399
return deadHostModel
411400
.query()
412401
.insertAndFetch({
413-
owner_user_id: 1,
414-
domain_names: [host.hostname],
415-
certificate_id: certificate_id,
416-
ssl_forced: host.force_ssl || false,
417-
meta: meta
402+
owner_user_id: 1,
403+
domain_names: [host.hostname],
404+
certificate_id: certificate_id,
405+
ssl_forced: host.force_ssl || false,
406+
advanced_config: host.advanced || '',
407+
meta: meta
418408
})
419409
.then(row => {
420410
// re-fetch with cert
@@ -449,8 +439,6 @@ module.exports = function () {
449439
certificate_id = certificate_map[host.hostname];
450440
}
451441

452-
// TODO: Advanced nginx config
453-
454442
return redirectionHostModel
455443
.query()
456444
.insertAndFetch({
@@ -460,6 +448,7 @@ module.exports = function () {
460448
block_exploits: host.block_exploits || false,
461449
certificate_id: certificate_id,
462450
ssl_forced: host.force_ssl || false,
451+
advanced_config: host.advanced || '',
463452
meta: meta
464453
})
465454
.then(row => {
@@ -483,8 +472,6 @@ module.exports = function () {
483472
const importStream = function (access, host) {
484473
logger.info('Creating Stream: ' + host.incoming_port);
485474

486-
// TODO: Advanced nginx config
487-
488475
return streamModel
489476
.query()
490477
.insertAndFetch({
@@ -537,7 +524,7 @@ module.exports = function () {
537524
})
538525
.then(() => {
539526
// Write the /config/v2-imported file so we don't import again
540-
fs.writeFile('/config/v2-imported', 'true', function(err) {
527+
fs.writeFile('/config/v2-imported', 'true', function (err) {
541528
if (err) {
542529
logger.err(err);
543530
}

src/backend/internal/certificate.js

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,10 @@ const internalCertificate = {
183183
});
184184
});
185185
} else {
186-
return certificate;
186+
return internalCertificate.writeCustomCert(certificate)
187+
.then(() => {
188+
return certificate;
189+
});
187190
}
188191
}).then(certificate => {
189192

@@ -401,6 +404,54 @@ const internalCertificate = {
401404
});
402405
},
403406

407+
/**
408+
* @param {Object} certificate
409+
* @returns {Promise}
410+
*/
411+
writeCustomCert: certificate => {
412+
return new Promise((resolve, reject) => {
413+
let dir = '/data/custom_ssl/npm-' + certificate.id;
414+
415+
if (certificate.provider === 'letsencrypt') {
416+
reject(new Error('Refusing to write letsencrypt certs here'));
417+
return;
418+
}
419+
420+
let cert_data = certificate.meta.certificate;
421+
if (typeof certificate.meta.intermediate_certificate !== 'undefined') {
422+
cert_data = cert_data + "\n" + certificate.meta.intermediate_certificate;
423+
}
424+
425+
try {
426+
if (!fs.existsSync(dir)) {
427+
fs.mkdirSync(dir);
428+
}
429+
} catch (err) {
430+
reject(err);
431+
return;
432+
}
433+
434+
fs.writeFile(dir + '/fullchain.pem', cert_data, function (err) {
435+
if (err) {
436+
reject(err);
437+
} else {
438+
resolve();
439+
}
440+
});
441+
})
442+
.then(() => {
443+
return new Promise((resolve, reject) => {
444+
fs.writeFile(dir + '/privkey.pem', certificate.meta.certificate_key, function (err) {
445+
if (err) {
446+
reject(err);
447+
} else {
448+
resolve();
449+
}
450+
});
451+
});
452+
});
453+
},
454+
404455
/**
405456
* @param {Access} access
406457
* @param {Object} data

src/backend/migrations/20180618015850_initial.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ exports.up = function (knex/*, Promise*/) {
7676
table.integer('ssl_forced').notNull().unsigned().defaultTo(0);
7777
table.integer('caching_enabled').notNull().unsigned().defaultTo(0);
7878
table.integer('block_exploits').notNull().unsigned().defaultTo(0);
79+
table.text('advanced_config').notNull().defaultTo('');
7980
table.json('meta').notNull().defaultTo('{}');
8081
});
8182
})
@@ -94,6 +95,7 @@ exports.up = function (knex/*, Promise*/) {
9495
table.integer('certificate_id').notNull().unsigned().defaultTo(0);
9596
table.integer('ssl_forced').notNull().unsigned().defaultTo(0);
9697
table.integer('block_exploits').notNull().unsigned().defaultTo(0);
98+
table.text('advanced_config').notNull().defaultTo('');
9799
table.json('meta').notNull().defaultTo('{}');
98100
});
99101
})
@@ -109,6 +111,7 @@ exports.up = function (knex/*, Promise*/) {
109111
table.json('domain_names').notNull();
110112
table.integer('certificate_id').notNull().unsigned().defaultTo(0);
111113
table.integer('ssl_forced').notNull().unsigned().defaultTo(0);
114+
table.text('advanced_config').notNull().defaultTo('');
112115
table.json('meta').notNull().defaultTo('{}');
113116
});
114117
})

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

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,11 @@
2424
"ssl_forced": {
2525
"$ref": "../definitions.json#/definitions/ssl_forced"
2626
},
27+
"advanced_config": {
28+
"type": "string"
29+
},
2730
"meta": {
28-
"type": "object",
29-
"additionalProperties": false,
30-
"properties": {
31-
"letsencrypt_email": {
32-
"type": "string",
33-
"format": "email"
34-
},
35-
"letsencrypt_agree": {
36-
"type": "boolean"
37-
}
38-
}
31+
"type": "object"
3932
}
4033
},
4134
"properties": {
@@ -57,6 +50,9 @@
5750
"ssl_forced": {
5851
"$ref": "#/definitions/ssl_forced"
5952
},
53+
"advanced_config": {
54+
"$ref": "#/definitions/advanced_config"
55+
},
6056
"meta": {
6157
"$ref": "#/definitions/meta"
6258
}
@@ -105,6 +101,9 @@
105101
"ssl_forced": {
106102
"$ref": "#/definitions/ssl_forced"
107103
},
104+
"advanced_config": {
105+
"$ref": "#/definitions/advanced_config"
106+
},
108107
"meta": {
109108
"$ref": "#/definitions/meta"
110109
}
@@ -139,6 +138,9 @@
139138
"ssl_forced": {
140139
"$ref": "#/definitions/ssl_forced"
141140
},
141+
"advanced_config": {
142+
"$ref": "#/definitions/advanced_config"
143+
},
142144
"meta": {
143145
"$ref": "#/definitions/meta"
144146
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242
"access_list_id": {
4343
"$ref": "../definitions.json#/definitions/access_list_id"
4444
},
45+
"advanced_config": {
46+
"type": "string"
47+
},
4548
"meta": {
4649
"type": "object"
4750
}
@@ -80,6 +83,9 @@
8083
"access_list_id": {
8184
"$ref": "#/definitions/access_list_id"
8285
},
86+
"advanced_config": {
87+
"$ref": "#/definitions/advanced_config"
88+
},
8389
"meta": {
8490
"$ref": "#/definitions/meta"
8591
}
@@ -145,6 +151,9 @@
145151
"access_list_id": {
146152
"$ref": "#/definitions/access_list_id"
147153
},
154+
"advanced_config": {
155+
"$ref": "#/definitions/advanced_config"
156+
},
148157
"meta": {
149158
"$ref": "#/definitions/meta"
150159
}
@@ -194,6 +203,9 @@
194203
"access_list_id": {
195204
"$ref": "#/definitions/access_list_id"
196205
},
206+
"advanced_config": {
207+
"$ref": "#/definitions/advanced_config"
208+
},
197209
"meta": {
198210
"$ref": "#/definitions/meta"
199211
}

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

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,11 @@
3535
"block_exploits": {
3636
"$ref": "../definitions.json#/definitions/block_exploits"
3737
},
38+
"advanced_config": {
39+
"type": "string"
40+
},
3841
"meta": {
39-
"type": "object",
40-
"additionalProperties": false,
41-
"properties": {
42-
"letsencrypt_email": {
43-
"type": "string",
44-
"format": "email"
45-
},
46-
"letsencrypt_agree": {
47-
"type": "boolean"
48-
}
49-
}
42+
"type": "object"
5043
}
5144
},
5245
"properties": {
@@ -77,6 +70,9 @@
7770
"block_exploits": {
7871
"$ref": "#/definitions/block_exploits"
7972
},
73+
"advanced_config": {
74+
"$ref": "#/definitions/advanced_config"
75+
},
8076
"meta": {
8177
"$ref": "#/definitions/meta"
8278
}
@@ -135,6 +131,9 @@
135131
"block_exploits": {
136132
"$ref": "#/definitions/block_exploits"
137133
},
134+
"advanced_config": {
135+
"$ref": "#/definitions/advanced_config"
136+
},
138137
"meta": {
139138
"$ref": "#/definitions/meta"
140139
}
@@ -178,6 +177,9 @@
178177
"block_exploits": {
179178
"$ref": "#/definitions/block_exploits"
180179
},
180+
"advanced_config": {
181+
"$ref": "#/definitions/advanced_config"
182+
},
181183
"meta": {
182184
"$ref": "#/definitions/meta"
183185
}

src/backend/templates/_certificates.conf

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
ssl_certificate /etc/letsencrypt/live/npm-{{ certificate_id }}/fullchain.pem;
77
ssl_certificate_key /etc/letsencrypt/live/npm-{{ certificate_id }}/privkey.pem;
88
{% endif %}
9-
# TODO: Custom SSL paths
10-
{% endif %}
9+
ssl_certificate /data/custom_ssl/npm-{{ certificate_id }}/fullchain.pem;
10+
ssl_certificate_key /data/custom_ssl/npm-{{ certificate_id }}/privkey.pem;
11+
{% endif %}

src/backend/templates/dead_host.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ server {
66

77
access_log /data/logs/dead_host-{{ id }}.log proxy;
88

9-
# TODO: Advanced config options
9+
{{ advanced_config }}
1010

1111
return 404;
1212
}

0 commit comments

Comments
 (0)