nginx-config-mod 0.2.9

A graphql command-line tool
Documentation
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
547
548
549
550
551
552
553
554
555
556
use std::collections::HashMap;
use std::mem;
use std::path::{PathBuf, Path};
use std::fs::read_to_string;
use std::str::FromStr;

use failure::{Error, ResultExt, err_msg};
use nginx_config;
use nginx_config::ast::{self, Listen, Value, Directive, Item};
use nginx_config::{parse_directives, Pos};
use nginx_config::visitors::{replace_vars, visit_mutable};
use regex::Regex;

use nginx_config_mod::{Config, EntryPoint};
use nginx_config_mod::checks;

#[derive(StructOpt)]
pub struct Modify {

    #[structopt(parse(from_os_str))]
    file: PathBuf,

    #[structopt(short="s", long="subst-variable", name="var=value",
                help="replace variable in the config to specified value")]
    set_var: Vec<String>,

    #[structopt(long="subst-server-name", name="orig.domain=dest.domain",
                help="replace orig.domain and all names starting with it \
                      to a dest.domain (keeping prefix if needed)")]
    server_name_mapping: Vec<String>,

    #[structopt(long="subst-proxy-pass-host", name="orig.host=dest.host",
                help="replace orig.host and all names starting with it \
                      to a dest.host (keeping prefix and port). \
                      These replaces take place *before* regex replacement.")]
    proxy_pass_mapping: Vec<String>,

    #[structopt(long="regex-subst-proxy-pass", name="REGEX=SUBST",
                help="replace REGEX to a destination substitution. \
                      Destination substitution may contain capture groups \
                      '$0', '$1'...
                      Regex replace takes place *after* host replacement")]
    proxy_pass_regexes: Vec<String>,

    #[structopt(long="regex-subst-if", name="IF_REGEX=SUBST",
                help="replace REGEX in 'if' conditions (only literal value, \
                      and only in `=` or `!=` conditions). \
                      Destination substitution may contain capture groups \
                      '$0', '$1'... ")]
    if_regexes: Vec<String>,

    #[structopt(long="regex-subst-rewrite-host", name="HOST_REGEX=SUBST",
                help="replace REGEX in host for rewrite directives. \
                      This work only for `http://host` or `https://host`,
                      or `$scheme://host` rewrites not local ones.
                      Destination substitution may contain capture groups \
                      '$0', '$1'... ")]
    rewrite_host_regexes: Vec<String>,

    #[structopt(long="check-proxy-pass-hostnames", help="\
        Also check that all hostnames in proxy_pass directives can be \
        resolved. This is needed because nginx refuses to start if can't \
        resolve IP addresses. \
        Note: this might be slow. \
        ")]
    check_proxy_pass_hostnames: bool,

    #[structopt(long="regex-proxy-pass-exclude", help="\
        Exclude following hostnames for the check-proxy-pass-hostnames. \
        This regex should match all upstreams, because it's usually \
        useless to resolve them. \
        (Note: currently, we don't check upstreams present in config, \
        but we may add the feature later). \
        ")]
    proxy_pass_exclude: Vec<String>,

    #[structopt(long="expand-local-includes", help="\
        Expand non-absolute includes to their contents. \
        Include path is treated relative to the configuration file path. \
        Including works before all other directives so included contents is \
        processed normally.")]
    expand_local_includes: bool,

    #[structopt(long="allow-includes", help="\
        Add a path prefix to a list of allowed include directive prefixes. \
        Note: these includes aren't read and checked so may possibly \
        contain disallowed things if not checked on its own.",
        parse(from_os_str))]
    allow_includes: Vec<PathBuf>,

    #[structopt(long="listen", name="LISTEN",
                help="replace all listen directives to this value. \
                If used multiple times. This number of listen directives will \
                be present in each block",
                parse(try_from_str="parse_listen"))]
    listen: Vec<Listen>,

    #[structopt(long="replace-by-name", name="DIR=VALUE", help="\
        Replace any occurrence of directve named DIR with another directive, \
        specified in VALUE. For example ``expires=add_header Expires never``. \
        VALUE must be a single and fully valid directive, excluding \
        semicolon. This replacement works *after* all other replacements \
        took place. Only one rule is applied to each directive.",
        parse(try_from_str="parse_replacement"))]
    replace_by_name: Vec<(String, Item)>,
}

fn parse_listen(s: &str) -> Result<Listen, Error> {
    let text = format!("listen {};", s);
    let mut dirs = parse_directives(&text)?;
    if dirs.len() > 1 {
        bail!("unexpected semicolon");
    }
    match dirs.pop().map(|d| d.item) {
        Some(ast::Item::Listen(lst)) => Ok(lst),
        _ => bail!("Internal error when parsing listen directive"),
    }
}

fn parse_replacement(s: &str) -> Result<(String, Item), Error> {
    let mut pair = s.splitn(2, "=");
    let name = pair.next().unwrap();
    if name.len() == 0 {
        bail!("directive name must not be empty");
    }
    let dirs = match pair.next() {
        Some(x) => {
            let text = format!("{};", x);
            parse_directives(&text)?
        }
        None => bail!("no replacement directive specified"),
    };
    if dirs.len() == 0 {
        bail!("no replacement directive specified");
    }
    if dirs.len() > 1 {
        bail!("Only single replacement directive may be specified \
               (consider removing semicolon from argument)");
    }
    Ok((name.to_string(), {dirs}.pop().unwrap().item))
}

// TODO(tailhook) temporary, until we expose Value::parse
fn parse_proxy(s: &str) -> Result<Value, Error> {
    let text = format!("proxy_pass {};", s);
    let mut dirs = parse_directives(&text)?;
    if dirs.len() > 1 {
        bail!("Only single proxy_pass directive may be specified \
               (consider removing semicolon from argument)");
    }
    match dirs.pop().map(|d| d.item) {
        Some(ast::Item::ProxyPass(value)) => Ok(value),
        _ => bail!("Internal error when parsing proxy_pass directive"),
    }
}

fn relative<'x>(name: &'x str, anchor: &str) -> Option<&'x str> {
    if name.ends_with(anchor) {
        if anchor.len() == name.len() {
            return Some("");
        } else if name[..name.len() - anchor.len()].ends_with(".") {
            return Some(&name[..name.len() - anchor.len()])
        } else {
            return None
        }
    } else {
        None
    }
}

fn proxy_subst<'x>(name: &'x str, anchor: &str) -> Option<(&'x str, &'x str)> {
    let mut cur = name;
    let mut prefix = 0;
    let mut suffix = 0;
    if name.starts_with("http://") {
        cur = &cur["http://".len()..];
        prefix += "http://".len();
    } else if name.starts_with("https://") {
        cur = &cur["https://".len()..];
        prefix += "https://".len();
    } else {
        return None;
    }
    if let Some(suf) = cur.find('/') {
        suffix += cur.len() - suf;
        cur = &cur[..suf];
    }
    if let Some(suf) = cur.find(':') {
        suffix += cur.len() - suf;
        cur = &cur[..suf];
    }
    if cur.ends_with(anchor) {
        // TODO(tailhook) check for variables ?
        if anchor.len() == cur.len() {
            return Some((&name[..prefix], &name[name.len() - suffix..]));
        } else if cur[..cur.len() - anchor.len()].ends_with(".") {
            prefix += cur.len() - anchor.len();
            return Some((&name[..prefix], &name[name.len() - suffix..]));
        } else {
            return None
        }
    } else {
        None
    }
}


fn server_names(cfg: &mut Config, names: &Vec<String>)
    -> Result<(), Error>
{
    use nginx_config::ast::ServerName::*;
    let mut snames = HashMap::new();
    for item in names {
        let mut pair = item.splitn(2, '=');
        let orig = pair.next().expect("first item always exists");
        if let Some(dest) = pair.next() {
            snames.insert(orig, dest);
        } else {
            bail!("server name {:?} doesn't include substitution target \
                   (format is `orig.name=dest.example.org`)");
        }
    }
    visit_mutable(cfg.directives_mut(), |dir| {
        match dir.item {
            ast::Item::ServerName(ref mut names) => {
                for name in names {
                    match *name {
                        Exact(ref mut n) | Suffix(ref mut n) |
                        StarSuffix(ref mut n)
                        => {
                            for (orig, new) in &snames {
                                *n = if let Some(prefix) = relative(n, orig) {
                                    format!("{}{}", prefix, new)
                                } else {
                                    continue;
                                }
                            }
                        }
                        StarPrefix(_) => {}  // ingoring, warn? forbid?
                        Regex(_) => {}  // ingoring, warn? forbid?
                    }
                }
            }
            _ => {}
        }
    });
    Ok(())
}

fn proxy_pass_regexes(cfg: &mut Config, items: &Vec<String>)
    -> Result<(), Error>
{
    let mut regexes = Vec::new();
    for item in items {
        let mut pair = item.splitn(2, '=');
        let orig = pair.next().expect("first item always exists");
        if let Some(dest) = pair.next() {
            let regex = Regex::new(orig).context(orig.to_string())?;
            regexes.push((regex, dest));
        } else {
            bail!("proxy pass regex {:?} doesn't include substitution \
                target (format is `REGEX=SUBST`)");
        }
    }
    let mut err = None;
    visit_mutable(cfg.directives_mut(), |dir| {
        match dir.item {
            ast::Item::ProxyPass(ref mut value) => {
                let mut s = value.to_string();
                for (regex, repl) in &regexes {
                    s = regex.replace(s.as_ref(), *repl).to_string();
                }
                match parse_proxy(&s) {
                    Ok(x) => *value = x,
                    Err(e) => err = Some(e),
                }
            }
            _ => {}
        }
    });
    if let Some(e) = err {
        return Err(e);
    }
    Ok(())
}

fn if_regexes(cfg: &mut Config, items: &Vec<String>)
    -> Result<(), Error>
{
    use nginx_config::ast::IfCondition::*;
    let mut regexes = Vec::new();
    for item in items {
        let mut pair = item.splitn(2, '=');
        let orig = pair.next().expect("first item always exists");
        if let Some(dest) = pair.next() {
            let regex = Regex::new(orig).context(orig.to_string())?;
            regexes.push((regex, dest));
        } else {
            bail!("if regex {:?} doesn't include substitution \
                target (format is `REGEX=SUBST`)");
        }
    }
    visit_mutable(cfg.directives_mut(), |dir| {
        match dir.item {
            | ast::Item::If(ast::If { condition: Eq(_, ref mut value), .. })
            | ast::Item::If(ast::If { condition: Neq(_, ref mut value), .. })
            => {
                let mut s = value.to_string();
                for (regex, repl) in &regexes {
                    s = regex.replace(s.as_ref(), *repl).to_string();
                }
                *value = s;
            }
            _ => {}
        }
    });
    Ok(())
}

fn rewrite_host_regexes(cfg: &mut Config, items: &Vec<String>)
    -> Result<(), Error>
{
    let mut regexes = Vec::new();
    for item in items {
        let mut pair = item.splitn(2, '=');
        let orig = pair.next().expect("first item always exists");
        if let Some(dest) = pair.next() {
            let regex = Regex::new(orig).context(orig.to_string())?;
            regexes.push((regex, dest));
        } else {
            bail!("rewrite host regex {:?} doesn't include substitution \
                target (format is `REGEX=SUBST`)");
        }
    }
    let mut err = None;
    visit_mutable(cfg.directives_mut(), |dir| {
        match dir.item {
            | ast::Item::Rewrite(ast::Rewrite { ref mut replacement, .. })
            => {
                let mut s = replacement.to_string();
                if !(s.starts_with("http://") ||
                     s.starts_with("https://") ||
                     s.starts_with("$scheme://"))
                {
                    return;
                }
                let (prefix, tmp) = s.split_at(s.find('/').unwrap() + 2);
                let (host, suffix) = if let Some(end) = tmp.find('/') {
                    tmp.split_at(end)
                } else {
                    (tmp, "")
                };
                let mut host = host.to_string();
                for (regex, repl) in &regexes {
                    host = regex.replace(host.as_ref(), *repl).to_string();
                }
                let mut s = String::with_capacity(
                    prefix.len() + suffix.len() + host.len());
                s.push_str(prefix);
                s.push_str(&host);
                s.push_str(suffix);
                match Value::from_str(&s) {
                    Ok(v) => *replacement = v,
                    Err(e) => err = Some(e),
                }
            }
            _ => {}
        }
    });
    if let Some(e) = err {
        return Err(err_msg(e));
    }
    Ok(())
}

fn proxy_pass_mapping(cfg: &mut Config, names: &Vec<String>)
    -> Result<(), Error>
{
    let mut pnames = HashMap::new();
    for item in names {
        let mut pair = item.splitn(2, '=');
        let orig = pair.next().expect("first item always exists");
        if let Some(dest) = pair.next() {
            pnames.insert(orig, dest);
        } else {
            bail!("proxy pass host {:?} doesn't include substitution \
                target (format is `orig.host=dest.example.org`)");
        }
    }
    let mut err = None;
    visit_mutable(cfg.directives_mut(), |dir| {
        match dir.item {
            ast::Item::ProxyPass(ref mut value) => {
                let mut s = value.to_string();
                for (orig, new) in &pnames {
                    s = if let Some((pre, suf)) = proxy_subst(&s, orig) {
                        format!("{}{}{}", pre, new, suf)
                    } else {
                        continue;
                    }
                }
                match parse_proxy(&s) {
                    Ok(x) => *value = x,
                    Err(e) => err = Some(e),
                }
            }
            _ => {}
        }
    });
    if let Some(e) = err {
        return Err(e);
    }
    Ok(())
}

fn replace_listen(dirs: &mut Vec<Directive>, lst: &Vec<Listen>) {
    let ref is_listen = |x: &Directive| matches!(x.item, ast::Item::Listen(..));
    if let Some(pos) = dirs.iter().position(is_listen) {
        dirs.retain(|x| !is_listen(x));
        for (idx, item) in lst.iter().enumerate() {
            dirs.insert(pos+idx, Directive {
                position: Pos { line: 0, column: 0 },
                item: ast::Item::Listen(item.clone()),
            });
        }
    }
}

fn expand_local_includes(dest: &mut Vec<Directive>, path: &Path)
    -> Result<(), Error>
{
    use nginx_config::ast::Item::Include;

    let orig_len = dest.len();
    let source = mem::replace(dest, Vec::with_capacity(orig_len));
    for mut dir in source {
        let inc_path = match dir.item {
            Include(ref inc_path) => {
                let inc_path = inc_path.to_string();
                let inc_path = Path::new(&inc_path);
                if !inc_path.is_absolute() {
                    Some(path.parent()
                        .expect("file path always has parent")
                        .join(inc_path))
                } else {
                    None
                }
            }
            _ => {
                if let Some(list) = dir.item.children_mut() {
                    expand_local_includes(list, path)?;
                }
                None
            }
        };
        let inc_path = if let Some(inc_path) = inc_path  {
            inc_path
        } else {
            dest.push(dir);
            continue;
        };
        let contents = read_to_string(&inc_path)
            .map_err(|e| format_err!("error reading {:?}: {}", inc_path, e))?;
        let inc_dirs = nginx_config::parse_directives(&contents)?;
        dest.extend(inc_dirs);
    }
    Ok(())
}

pub fn run(modify: Modify) -> Result<(), Error> {
    let mut cfg = Config::partial_file(EntryPoint::Main, &modify.file)?;

    if modify.expand_local_includes {
        expand_local_includes(cfg.directives_mut(), &modify.file)?;
    }

    // vars
    let mut vars = HashMap::new();
    for item in &modify.set_var {
        let mut pair = item.splitn(2, '=');
        vars.insert(pair.next().expect("first item always exists"),
            pair.next().unwrap_or(""));
    }
    replace_vars(cfg.directives_mut(), |name| vars.get(name).map(|x| *x));

    // listen
    if modify.listen.len() > 0 {
        replace_listen(cfg.directives_mut(), &modify.listen);
        visit_mutable(cfg.directives_mut(), |dir| {
            if let Some(children) = dir.item.children_mut() {
                replace_listen(children, &modify.listen);
            }
        });
    }

    if modify.server_name_mapping.len() > 0 {
        server_names(&mut cfg, &modify.server_name_mapping)?;
    }

    if modify.proxy_pass_mapping.len() > 0 {
        proxy_pass_mapping(&mut cfg, &modify.proxy_pass_mapping)?;
    }

    if modify.proxy_pass_regexes.len() > 0 {
        proxy_pass_regexes(&mut cfg, &modify.proxy_pass_regexes)?;
    }
    if modify.if_regexes.len() > 0 {
        if_regexes(&mut cfg, &modify.if_regexes)?;
    }
    if modify.rewrite_host_regexes.len() > 0 {
        rewrite_host_regexes(&mut cfg, &modify.rewrite_host_regexes)?;
    }

    for dir in cfg.all_directives() {
        match &dir.item {
            Item::Include(path) => {
                let path = path.to_string();
                let path = Path::new(&path);
                if !modify.allow_includes.iter().any(|p| path.starts_with(p)) {
                    bail!("invalid include path {:?}, allowed: {:?}",
                        path, &modify.allow_includes);
                }
            }
            _ => {}
        }
    }

    if modify.replace_by_name.len() > 0 {
        visit_mutable(cfg.directives_mut(), |dir| {
            for (name, value) in &modify.replace_by_name {
                if dir.item.directive_name() == name {
                    dir.item = value.clone();
                    break;
                }
            }
        })
    }

    if modify.check_proxy_pass_hostnames {
        let regexes = modify.proxy_pass_exclude.iter()
            .map(|r| Regex::new(&r))
            .collect::<Result<Vec<_>, _>>()?;
        let result = checks::proxy_pass::check_selected_hostnames(&cfg, |dom| {
            !regexes.iter().any(|x| x.is_match(dom))
        });
        if let Err(errs) = result {
            for e in errs {
                error!("{}", e);
            }
            return Err(err_msg("failed to resolve some hostnames"));
        }
    }

    print!("{}", cfg);
    Ok(())
}