proksi 0.11.0

A batteries-included reverse proxy with automatic HTTPS using Cloudflare Pingora and Let's Encrypt.
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
//! `host "<name>" { location "<path>" { ... } }` blocks (ADR 0003), as written in HCL or YAML.
//! Each block converts into the model in [`crate::config::host`].

use std::collections::BTreeMap;

use serde::{Deserialize, Serialize};

use super::host::{HealthCheck, Host, LocationTimeouts};

mod headers;
mod location;
mod tcp;
mod tls;
mod upstream;

pub use headers::HeadersBlock;
pub use location::LocationBlock;
pub use tcp::TcpBlock;
pub use tls::TlsSetting;

/// `host "<name>" { ... }`
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct HostBlock {
    /// Addresses the host is served on (`ip:port`), instead of the default ones
    #[serde(default)]
    pub listen: Vec<String>,
    /// `tls = false` (plain HTTP) or `tls { ... }`
    pub tls: Option<TlsSetting>,
    /// Headers for every location of the host
    pub headers: Option<HeadersBlock>,
    /// Timeouts for every location of the host (a location can set its own)
    pub timeouts: Option<LocationTimeouts>,
    /// How the upstreams of every location and `tcp` block of the host are checked
    pub health_check: Option<HealthCheck>,
    #[serde(default)]
    pub location: BTreeMap<String, LocationBlock>,
    /// TCP proxying with the host's certificate (or its TLS forwarded, or plain TCP)
    #[serde(default)]
    pub tcp: BTreeMap<String, TcpBlock>,
}

impl HostBlock {
    /// Converts the block of host `name` into a host
    pub fn to_host(&self, name: &str) -> Result<Host, String> {
        let locations = self
            .location
            .iter()
            .map(|(path, location)| {
                location
                    .to_location(path, self)
                    .map_err(|err| format!("location \"{path}\": {err}"))
            })
            .collect::<Result<Vec<_>, _>>()?;

        Ok(Host {
            name: name.to_string(),
            listen: self.listen.clone(),
            tls: self
                .tls
                .as_ref()
                .map(TlsSetting::to_host_tls)
                .unwrap_or_default(),
            locations,
        })
    }
}

#[cfg(test)]
mod tests {
    use std::{borrow::Cow, collections::BTreeMap};

    use figment::providers::{Format, Yaml};
    use serde::Deserialize;

    use crate::config::{
        hcl::HclFormat,
        host::{Host, HostTls, Location},
        RouteHeader, RouteHeaderAdd, RouteHeaderRemove, RoutePlugin, RouteSslPath, RouteUpstream,
    };

    use super::HostBlock;

    #[derive(Deserialize)]
    struct Doc {
        host: BTreeMap<String, HostBlock>,
    }

    const HCL: &str = r#"
host "app.test" {
  tls {
    certificate {
      pem = "/certs/app.pem"
      key = "/certs/app.key"
    }
    self_signed_on_failure = true
  }

  headers {
    response {
      add    = { "x-host" = "app" }
      remove = ["Server"]
    }
  }

  location "/v1/session" {
    upstream "directory:8080" {}
  }

  location "/*" {
    headers {
      response {
        add = { "x-location" = "default" }
      }
    }
    request_id {}
    upstream "10.0.0.1:3000" {
      weight = 2
      sni    = "app.internal"
      headers {
        add = { "x-forwarded-by" = "proksi" }
      }
    }
  }
}
"#;

    const YAML: &str = r#"
host:
  app.test:
    tls:
      certificate: { pem: /certs/app.pem, key: /certs/app.key }
      self_signed_on_failure: true
    headers:
      response: { add: { x-host: app }, remove: [Server] }
    location:
      /v1/session:
        upstream: { "directory:8080": {} }
      /*:
        headers: { response: { add: { x-location: default } } }
        request_id: {}
        upstream:
          "10.0.0.1:3000":
            weight: 2
            sni: app.internal
            headers: { add: { x-forwarded-by: proksi } }
"#;

    fn header(name: &str, value: &str) -> RouteHeaderAdd {
        RouteHeaderAdd {
            name: Cow::Owned(name.to_string()),
            value: Cow::Owned(value.to_string()),
        }
    }

    fn host_headers(extra: &[RouteHeaderAdd]) -> Option<RouteHeader> {
        let mut add = vec![header("x-host", "app")];
        add.extend_from_slice(extra);
        Some(RouteHeader {
            add: Some(add),
            remove: Some(vec![RouteHeaderRemove {
                name: Cow::Owned("Server".to_string()),
            }]),
            priority: None,
        })
    }

    fn expected() -> Host {
        Host {
            name: "app.test".to_string(),
            listen: vec![],
            tls: HostTls {
                enabled: true,
                certificate: Some(RouteSslPath {
                    pem: "/certs/app.pem".into(),
                    key: "/certs/app.key".into(),
                }),
                self_signed_on_failure: true,
            },
            // Sorted by path
            locations: vec![
                Location {
                    path: "/*".to_string(),
                    exact: false,
                    upstreams: vec![RouteUpstream {
                        ip: Cow::Owned("10.0.0.1".to_string()),
                        port: 3000,
                        network: None,
                        weight: Some(2),
                        sni: Some("app.internal".to_string()),
                        headers: Some(RouteHeader {
                            add: Some(vec![header("x-forwarded-by", "proksi")]),
                            remove: None,
                            priority: None,
                        }),
                    }],
                    headers: host_headers(&[header("x-location", "default")]),
                    plugins: Some(vec![RoutePlugin {
                        name: Cow::Owned("request_id".to_string()),
                        config: Some(std::collections::HashMap::new()),
                    }]),
                    cache: None,
                    timeouts: None,
                    rewrite: None,
                    health_check: None,
                },
                Location {
                    path: "/v1/session".to_string(),
                    exact: false,
                    upstreams: vec![RouteUpstream {
                        ip: Cow::Owned("directory".to_string()),
                        port: 8080,
                        network: None,
                        weight: None,
                        sni: None,
                        headers: None,
                    }],
                    headers: host_headers(&[]),
                    plugins: Some(vec![]),
                    cache: None,
                    timeouts: None,
                    rewrite: None,
                    health_check: None,
                },
            ],
        }
    }

    fn to_host(doc: &Doc) -> Result<Host, String> {
        doc.host.get("app.test").unwrap().to_host("app.test")
    }

    #[test]
    fn test_hcl_host_block() {
        let doc = HclFormat::from_str::<Doc>(HCL).unwrap();
        assert_eq!(to_host(&doc), Ok(expected()));
    }

    #[test]
    fn test_yaml_host_block() {
        let doc = Yaml::from_str::<Doc>(YAML).unwrap();
        assert_eq!(to_host(&doc), Ok(expected()));
    }

    #[test]
    fn test_unknown_attributes_are_rejected() {
        let err = HclFormat::from_str::<Doc>("host \"app.test\" {\n  locaton \"/\" {\n  }\n}\n")
            .err()
            .unwrap();

        assert!(err.to_string().contains("unknown field `locaton`"), "{err}");
    }

    #[test]
    fn test_invalid_upstream_label() {
        let doc = HclFormat::from_str::<Doc>(
            "host \"app.test\" {\n  location \"/\" {\n    upstream \"10.0.0.1\" {}\n  }\n}\n",
        )
        .unwrap();

        assert_eq!(
            to_host(&doc),
            Err("location \"/\": upstream \"10.0.0.1\" needs a port (host:port)".to_string())
        );
    }

    #[test]
    fn test_catch_all_host_without_tls() {
        let doc = HclFormat::from_str::<Doc>(
            "host \"*\" {\n  tls = false\n  location \"/\" {\n    upstream \"app:8080\" {}\n  }\n}\n",
        )
        .unwrap();

        let host = doc.host.get("*").unwrap().to_host("*").unwrap();

        assert_eq!(host.name, "*");
        assert!(!host.tls.enabled);
        assert_eq!(crate::routing::validate::validate_host(&host), Ok(()));
    }

    #[test]
    fn test_rate_limit_block() {
        let doc = HclFormat::from_str::<Doc>(
            "host \"a.test\" {\n  location \"/\" {\n    rate_limit {\n      requests = 10\n      per = \"1m\"\n    }\n  }\n}\n",
        )
        .unwrap();

        let host = doc.host.get("a.test").unwrap().to_host("a.test").unwrap();
        let plugins = host.locations[0].plugins.clone().unwrap();

        assert_eq!(plugins[0].name, "rate_limit");
        let config = plugins[0].config.as_ref().unwrap();
        assert_eq!(config["requests"], 10);
        assert_eq!(config["per"], "1m");
    }

    #[test]
    fn test_body_limit_block() {
        let doc = HclFormat::from_str::<Doc>(
            "host \"a.test\" {\n  location \"/\" {\n    body_limit {\n      max = \"1MiB\"\n    }\n  }\n}\n",
        )
        .unwrap();

        let host = doc.host.get("a.test").unwrap().to_host("a.test").unwrap();
        let plugins = host.locations[0].plugins.clone().unwrap();

        assert_eq!(plugins[0].name, "body_limit");
        assert_eq!(plugins[0].config.as_ref().unwrap()["max"], "1MiB");
    }

    #[test]
    fn test_host_listen_addresses() {
        let doc = HclFormat::from_str::<Doc>(
            "host \"a.test\" {\n  listen = [\"0.0.0.0:8080\", \"[::]:8080\"]\n}\n",
        )
        .unwrap();

        let host = doc.host.get("a.test").unwrap().to_host("a.test").unwrap();

        assert_eq!(host.listen, vec!["0.0.0.0:8080", "[::]:8080"]);
    }

    #[test]
    fn test_locations_inherit_the_timeouts_of_their_host() {
        let doc = HclFormat::from_str::<Doc>(
            r#"
host "app.test" {
  timeouts {
    read_secs  = 10
    write_secs = 20
  }
  location "/upload" {
    timeouts {
      read_secs = 300
    }
  }
  location "/" {}
}
"#,
        )
        .unwrap();
        let host = doc.host["app.test"].to_host("app.test").unwrap();
        let timeouts = |path: &str| {
            host.locations
                .iter()
                .find(|location| location.path == path)
                .and_then(|location| location.timeouts)
                .map(|t| (t.read_secs, t.write_secs))
        };

        assert_eq!(timeouts("/upload"), Some((Some(300), Some(20))));
        assert_eq!(timeouts("/"), Some((Some(10), Some(20))));
    }

    #[test]
    fn test_locations_inherit_the_upstream_timeouts_of_their_host() {
        let doc = HclFormat::from_str::<Doc>(
            r#"
host "app.test" {
  timeouts {
    upstream {
      connect_secs = 2
      read_secs    = 30
    }
  }
  location "/reports" {
    timeouts {
      read_secs = 300
      upstream {
        read_secs = 600
        idle_secs = 5
      }
    }
  }
  location "/" {}
}
"#,
        )
        .unwrap();
        let host = doc.host["app.test"].to_host("app.test").unwrap();
        let upstream = |path: &str| {
            host.locations
                .iter()
                .find(|location| location.path == path)
                .and_then(|location| location.timeouts)
                .and_then(|timeouts| timeouts.upstream)
                .map(|t| (t.connect_secs, t.tls_secs, t.read_secs, t.write_secs, t.idle_secs))
        };

        assert_eq!(upstream("/reports"), Some((Some(2), None, Some(600), None, Some(5))));
        assert_eq!(upstream("/"), Some((Some(2), None, Some(30), None, None)));
    }

    #[test]
    fn test_locations_inherit_the_health_check_of_their_host() {
        let doc = HclFormat::from_str::<Doc>(
            r#"
host "app.test" {
  health_check {
    interval_secs = 5
  }
  location "/game" {
    health_check {
      enabled = false
    }
  }
  location "/" {}
}
"#,
        )
        .unwrap();
        let host = doc.host["app.test"].to_host("app.test").unwrap();
        let health_check = |path: &str| {
            host.locations
                .iter()
                .find(|location| location.path == path)
                .and_then(|location| location.health_check)
                .map(|check| (check.enabled, check.interval_secs))
        };

        assert_eq!(health_check("/game"), Some((Some(false), Some(5))));
        assert_eq!(health_check("/"), Some((None, Some(5))));
    }

    #[test]
    fn test_idle_timeouts_are_only_for_the_server() {
        let err = HclFormat::from_str::<Doc>(
            "host \"app.test\" {\n  timeouts {\n    idle_secs = 5\n  }\n}\n",
        )
        .err()
        .unwrap();
        assert!(err.to_string().contains("idle_secs"), "{err}");
    }
}