-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathrun-nginx.cpp
More file actions
546 lines (492 loc) · 18.3 KB
/
Copy pathrun-nginx.cpp
File metadata and controls
546 lines (492 loc) · 18.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
/**
run-nginx -- headless supervisor and configuration generator for the
reverse-proxy image. There is no shell in the container, so everything the old
start.sh / nginx-configure.sh did in shell is done here in C++:
1. Render /etc/nginx from the /etc/nginx.template with environment variable
substitution (the envwrap mechanism), so nginx can be configured at
instantiation time -- something nginx cannot do on its own.
2. Generate the per-virtual-host server blocks from the forward / redirect
rules, taken from the environment variables FORWARD and REDIRECT and/or
from the file /config/reverse-proxy.conf. Wire per-domain basic-auth.
3. Start nginx and keep it running.
4. Watch the certificates in /etc/letsencrypt/live and the configuration in
/config; on any change, regenerate and reload nginx gracefully.
5. Forward termination signals to nginx for a clean shutdown (PID 1 duty).
Configuration format (the `--` prefix of the old parameter form is dropped):
FORWARD / REDIRECT environment variables, one rule per line:
<source> <target>
/config/reverse-proxy.conf, one rule per line, verb as first word:
forward <source> <target>
redirect <source> <target>
Environment and file are merged; on a conflict (same source) the file wins.
*/
#include <sys/inotify.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <signal.h>
#include <cctype>
#include <cerrno>
#include <cstring>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
namespace fs = std::filesystem;
extern char **environ;
static const char *TEMPLATE_DIR = "/etc/nginx.template";
static const char *TARGET_DIR = "/etc/nginx";
static const char *SERVER_DIR = "/etc/nginx/server.d";
static const char *BASIC_AUTH_DIR = "/etc/nginx/basic-auth";
static const char *CONFIG_FILE = "/config/reverse-proxy.conf";
static const char *WATCH_CERTS = "/etc/letsencrypt/live";
static const char *WATCH_CONFIG = "/config";
static pid_t nginxPid = 0;
// --------------------------------------------------------------- helpers ---
static string env(const string &key, const string &def = "") {
const char *v = getenv(key.c_str());
return v ? string(v) : def;
}
// Split on any whitespace, dropping empty tokens.
static vector<string> tokens(const string &s) {
vector<string> out;
istringstream in(s);
string t;
while (in >> t)
out.push_back(t);
return out;
}
// ------------------------------------------------ template substitution ---
// Same behaviour as envwrap: substitute ${VAR} from the environment in every
// file below the template and write the result into the target, replacing the
// target's previous content. Runs shell-free so it works in the scratch image.
static string substitute(const string &input) {
string out = input;
for (char **p = environ; *p; ++p) {
string line(*p);
auto eq = line.find('=');
if (eq == string::npos)
continue;
const string needle = "${" + line.substr(0, eq) + "}";
const string value = line.substr(eq + 1);
size_t pos = 0;
while ((pos = out.find(needle, pos)) != string::npos) {
out.replace(pos, needle.size(), value);
pos += value.size();
}
}
return out;
}
static void renderTemplate() {
if (!fs::exists(TEMPLATE_DIR))
throw runtime_error(string("template not found: ") + TEMPLATE_DIR);
// Overwrite in place rather than wiping the target: mounted data such as
// /etc/nginx/basic-auth lives under the target and must not be removed. Stale
// generated server blocks are cleared in generateServers().
fs::create_directories(TARGET_DIR);
for (const auto &entry : fs::recursive_directory_iterator(TEMPLATE_DIR)) {
const auto rel = fs::relative(entry.path(), TEMPLATE_DIR);
const fs::path dst = fs::path(TARGET_DIR) / rel;
if (entry.is_directory()) {
fs::create_directories(dst);
continue;
}
fs::create_directories(dst.parent_path());
ifstream in(entry.path(), ios::binary);
string content((istreambuf_iterator<char>(in)),
istreambuf_iterator<char>());
ofstream out(dst, ios::binary);
out << substitute(content);
}
}
// ------------------------------------------------------ configuration ------
struct Rule {
bool forward; // true = forward, false = redirect
string source;
string target;
};
// Rule tokens are rendered verbatim into the generated nginx configuration:
// restrict them to a safe character set so a malformed or hostile token can
// neither break the configuration nor inject directives — one bad rule must
// never take all virtual hosts down.
static bool safeToken(const string &s) {
if (s.empty() || s.front() == '/')
return false;
for (unsigned char c : s)
if (!isalnum(c) && c != '.' && c != '_' && c != ':' && c != '/' && c != '-')
return false;
return true;
}
// Append a rule after validation; invalid rules are skipped with a warning.
static void addRule(bool forward, const string &source, const string &target,
vector<Rule> &out) {
if (!safeToken(source) || !safeToken(target)) {
cerr << "**** WARNING: ignoring invalid rule: " << source << ' ' << target
<< endl;
return;
}
out.push_back({forward, source, target});
}
// Parse "<source> <target>" from a single line; ignores blank / comment lines.
static bool parsePair(const string &line, string &source, string &target) {
string trimmed = line;
auto hash = trimmed.find('#');
if (hash != string::npos)
trimmed = trimmed.substr(0, hash);
auto t = tokens(trimmed);
if (t.size() < 2)
return false;
source = t[0];
target = t[1];
return true;
}
static void appendRules(const string &block, bool forward, vector<Rule> &out) {
istringstream in(block);
string line;
while (getline(in, line)) {
string source, target;
if (parsePair(line, source, target))
addRule(forward, source, target, out);
}
}
// Environment FORWARD / REDIRECT plus the file, merged. File wins on conflict
// (same source), so the file's rules replace any environment rule sharing the
// source. Environment rules are read first, then filtered, then the file rules
// are appended.
static vector<Rule> collectRules() {
vector<Rule> envRules;
appendRules(env("FORWARD"), true, envRules);
appendRules(env("REDIRECT"), false, envRules);
vector<Rule> fileRules;
if (fs::exists(CONFIG_FILE)) {
ifstream in(CONFIG_FILE);
string line;
while (getline(in, line)) {
auto t = tokens(line.substr(0, line.find('#')));
if (t.size() < 3)
continue;
if (t[0] == "forward")
addRule(true, t[1], t[2], fileRules);
else if (t[0] == "redirect")
addRule(false, t[1], t[2], fileRules);
}
}
vector<Rule> rules;
for (const auto &e : envRules) {
bool overridden = false;
for (const auto &f : fileRules)
if (f.source == e.source) {
overridden = true;
break;
}
if (!overridden)
rules.push_back(e);
}
for (const auto &f : fileRules)
rules.push_back(f);
return rules;
}
// -------------------------------------------------- server generation ------
static string basicAuth(const string &fromurl, const string &frombase) {
string realm = env("BASIC_AUTH_REALM");
string base = string(BASIC_AUTH_DIR) + "/" + fromurl + "/" + frombase + ".htpasswd";
if (fs::exists(base)) {
string r = realm.empty() ? fromurl + "/" + frombase : realm;
return " auth_basic \"" + r + "\";\n"
" auth_basic_user_file " + base + ";\n";
}
string flat = string(BASIC_AUTH_DIR) + "/" + fromurl + ".htpasswd";
if (fs::exists(flat)) {
string r = realm.empty() ? fromurl : realm;
return " auth_basic \"" + r + "\";\n"
" auth_basic_user_file " + flat + ";\n";
}
return "";
}
// True if PROXY_REDIRECT_OFF (whitespace separated list) contains this host+base.
static bool redirectOff(const string &key) {
for (const auto &t : tokens(env("PROXY_REDIRECT_OFF")))
if (t == key)
return true;
return false;
}
// Port of nginx-configure.sh forward(): proxy <source> to <target>.
static string forwardLocation(const string &fromurl, const string &frombase,
const string &source, const string &target) {
string toscheme = "http://";
string tgt = target;
if (tgt.rfind("http://", 0) == 0 || tgt.rfind("https://", 0) == 0) {
toscheme = tgt.substr(0, tgt.find("://")) + "://";
tgt = tgt.substr(tgt.find("://") + 3);
}
string tobase, toport, tourl = tgt;
if (auto s = tgt.find('/'); s != string::npos) {
tobase = tgt.substr(s);
if (!tobase.empty() && tobase.back() == '/')
tobase.pop_back();
tourl = tgt.substr(0, s);
}
if (auto c = tourl.find(':'); c != string::npos) {
toport = ":" + tourl.substr(c + 1);
tourl = tourl.substr(0, c);
}
string fromport = ":$port"; // source port defaults to the listen port
ostringstream o;
o << " location " << frombase << "/ {\n";
o << basicAuth(fromurl, frombase);
o << " include proxy.conf;\n";
o << " resolver 127.0.0.11:53 valid=30s;\n";
o << " set $tourl " << tourl << ";\n";
o << " if ($request_method ~ ^COPY$) {\n";
o << " rewrite " << tobase << "/(.*) " << frombase << "/$1 break;\n";
o << " }\n";
o << " proxy_cookie_domain " << tourl << " " << fromurl << ";\n";
if (tobase + "/" != frombase + "/")
o << " proxy_cookie_path " << tobase << "/ " << frombase << "/;\n";
o << " proxy_pass " << toscheme << "$tourl" << toport << tobase << ";\n";
if (redirectOff(fromurl + frombase))
o << " proxy_redirect off;\n";
else
o << " proxy_redirect " << toscheme << "$tourl" << toport << tobase
<< "/ $scheme://" << fromurl << fromport << frombase << "/;\n";
o << " }\n";
return o.str();
}
// Port of nginx-configure.sh redirect(): permanently redirect <source> to <target>.
static string redirectRewrite(const string &source, const string &target) {
string tgt = target;
if (!tgt.empty() && tgt.back() == '/')
tgt.pop_back();
auto slash = source.find('/');
if (slash != string::npos) {
string path = source.substr(slash + 1);
return " rewrite ^/" + path + "(/.*)?$ $scheme://" + tgt + "$1 permanent;\n";
}
return " rewrite ^/$ $scheme://" + tgt + "/ permanent;\n";
}
// Split a source into virtual host (fromurl) and optional base path (frombase,
// leading slash, no trailing slash). Also strips an optional source port.
static void splitSource(const string &source, string &fromurl, string &frombase) {
frombase.clear();
fromurl = source;
if (auto s = source.find('/'); s != string::npos) {
frombase = source.substr(s);
if (!frombase.empty() && frombase.back() == '/')
frombase.pop_back();
fromurl = source.substr(0, s);
}
if (auto c = fromurl.find(':'); c != string::npos)
fromurl = fromurl.substr(0, c);
}
static string writeHTTP(const string &server, const string &content) {
ostringstream o;
o << "server { # redirect www to non-www\n"
" listen 8080;\n"
" server_name www." << server << ";\n"
" location /.well-known {\n"
" alias /acme/.well-known;\n"
" }\n"
" location / {\n"
" resolver 127.0.0.11:53 valid=30s;\n"
" return 302 http://" << server << "$request_uri;\n"
" }\n"
"}\n"
"server {\n"
" listen 8080;\n"
" server_name " << server << ";\n"
" set $port 8080;\n"
" error_page 502 /502.html;\n"
" error_page 504 /504.html;\n"
" error_page 404 /404.html;\n"
" location ~ ^/(502|504|404)\\.html$ {\n"
" root /etc/nginx/error/$lang;\n"
" }\n"
" location ~ ^/(502|504|404)\\.jpg$ {\n"
" root /etc/nginx/error;\n"
" }\n"
<< content
<< " location /.well-known {\n"
" alias /acme/.well-known;\n"
" }\n"
"}\n";
return o.str();
}
static vector<string> g_generated; // server.d files written by the previous run
// HTTPS variant: served when a certificate for the domain exists and SSL is not
// disabled. http (and www) is redirected to https. Falls back to writeHTTP until
// the certificate appears, so a domain works before Let's Encrypt has issued it.
static string writeHTTPS(const string &server, const string &content) {
const string live = "/etc/letsencrypt/live/" + server;
ostringstream o;
o << "server { # redirect http to https\n"
" listen 8080;\n"
" server_name " << server << " www." << server << ";\n"
" location /.well-known {\n"
" alias /acme/.well-known;\n"
" }\n"
" location / {\n"
" resolver 127.0.0.11:53 valid=30s;\n"
" return 302 https://" << server << "$request_uri;\n"
" }\n"
"}\n"
"server {\n"
" listen 8443 ssl;\n"
" http2 on;\n"
" server_name " << server << ";\n"
" set $port 8443;\n"
// One year — keep in sync with the map in conf/conf.d/hsts.conf.
" add_header Strict-Transport-Security max-age=31536000 always;\n"
" ssl_certificate " << live << "/fullchain.pem;\n"
" ssl_certificate_key " << live << "/privkey.pem;\n"
" error_page 502 /502.html;\n"
" error_page 504 /504.html;\n"
" error_page 404 /404.html;\n"
" location ~ ^/(502|504|404)\\.html$ {\n"
" root /etc/nginx/error/$lang;\n"
" }\n"
" location ~ ^/(502|504|404)\\.jpg$ {\n"
" root /etc/nginx/error;\n"
" }\n"
<< content
<< " location /.well-known {\n"
" alias /acme/.well-known;\n"
" }\n"
"}\n";
return o.str();
}
static bool sslEnabled() { return env("SSL") != "off"; }
static bool hasCertificate(const string &server) {
return fs::exists("/etc/letsencrypt/live/" + server + "/fullchain.pem") &&
fs::exists("/etc/letsencrypt/live/" + server + "/privkey.pem");
}
static void generateServers() {
fs::create_directories(SERVER_DIR);
// Remove the server blocks generated last time (e.g. for a domain that has
// since been removed from the configuration); template files are left alone.
for (const auto &name : g_generated)
fs::remove(fs::path(SERVER_DIR) / name);
g_generated.clear();
vector<string> order; // server names in first-seen order
unordered_map<string, string> content; // server name -> accumulated content
for (const auto &rule : collectRules()) {
string fromurl, frombase;
splitSource(rule.source, fromurl, frombase);
if (content.find(fromurl) == content.end())
order.push_back(fromurl);
if (rule.forward)
content[fromurl] += forwardLocation(fromurl, frombase, rule.source, rule.target);
else
content[fromurl] += redirectRewrite(rule.source, rule.target);
}
for (const auto &server : order) {
const string name = server + ".conf";
ofstream out(fs::path(SERVER_DIR) / name, ios::binary);
if (sslEnabled() && hasCertificate(server))
out << writeHTTPS(server, content[server]);
else
out << writeHTTP(server, content[server]);
g_generated.push_back(name);
}
}
// ---------------------------------------------------------- lifecycle ------
static void runOpenssl(const string &out, const string &bits) {
pid_t p = fork();
if (p == -1)
return;
if (p == 0) {
execl("/usr/bin/openssl", "openssl", "dhparam", "-out", out.c_str(),
bits.c_str(), nullptr);
_exit(1);
}
int status = 0;
waitpid(p, &status, 0);
}
// Ensure /etc/nginx/dhparam.pem exists -- the base ssl.conf requires it. The DH
// parameters are generated at start (not baked into the image) with DHPARAM bits
// (default 4096) and kept on the persistent DHPARAM_FILE, so they are generated
// only once. A pre-generated file (e.g. mounted) is used as-is.
static void ensureDhparam() {
const string persistent = env("DHPARAM_FILE", "/etc/letsencrypt/dhparam.pem");
const string target = string(TARGET_DIR) + "/dhparam.pem";
const string bits = env("DHPARAM", "4096");
if (!fs::exists(persistent))
runOpenssl(persistent, bits); // no-op if the path is not writable
error_code ec;
if (fs::exists(persistent))
fs::copy_file(persistent, target, fs::copy_options::overwrite_existing, ec);
else if (!fs::exists(target))
runOpenssl(target, bits);
}
static void rebuild() {
renderTemplate();
ensureDhparam();
generateServers();
}
static void startNginx() {
nginxPid = fork();
if (nginxPid == -1)
exit(EXIT_FAILURE);
if (nginxPid != 0)
return;
cout << "---- STARTING PROCESS NGINX, PID=" << getpid() << endl;
execl("/usr/sbin/nginx", "/usr/sbin/nginx", nullptr);
perror("execl nginx");
exit(EXIT_FAILURE);
}
// Forward termination to nginx for a graceful shutdown, then leave. As PID 1 the
// kernel applies no default action, so without this handler `docker stop` would
// be ignored until SIGKILL and nginx would die abruptly.
static void onTerminate(int) {
if (nginxPid > 0)
kill(nginxPid, SIGQUIT); // graceful stop
int status = 0;
if (nginxPid > 0)
waitpid(nginxPid, &status, 0);
_exit(0);
}
int main() try {
signal(SIGTERM, onTerminate);
signal(SIGINT, onTerminate);
rebuild();
startNginx();
while (true) {
pid_t watcher = fork();
if (watcher == -1)
exit(EXIT_FAILURE);
if (watcher == 0) {
cout << "---- WATCHING CERTIFICATES AND CONFIGURATION, PID=" << getpid()
<< endl;
execl("/usr/bin/inotifywait", "/usr/bin/inotifywait", "-q", "-r", "-e",
"close_write", "-e", "create", "-e", "moved_to", "-e", "delete",
WATCH_CERTS, WATCH_CONFIG, nullptr);
perror("execl inotifywait");
exit(EXIT_FAILURE);
}
int status = 0;
pid_t done = wait(&status);
if (done == nginxPid) {
cerr << "**** ERROR: NGINX TERMINATED, STATUS=" << WEXITSTATUS(status)
<< endl;
exit(EXIT_FAILURE);
}
if (done == watcher) {
if (WIFEXITED(status) && WEXITSTATUS(status) != 0) {
cerr << "**** ERROR: WATCHING FAILED, STATUS=" << WEXITSTATUS(status)
<< endl;
exit(EXIT_FAILURE);
}
cout << "**** configuration or certificate changed, reloading" << endl;
rebuild();
if (nginxPid > 0)
kill(nginxPid, SIGHUP); // graceful reload
}
}
return 0;
} catch (const exception &e) {
cerr << "EXCEPTION: " << e.what() << endl;
return 1;
}