erbridge-core 0.3.0

TCP/UDP forwarding and reverse tunnels over Noise, with a multi-tenant SNI-routed server, as a library
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
//! Tenants and enrollment codes for a multi-tenant `serve`, and the snapshot
//! type the handshake and router consult. Nothing here touches a socket.
//!
//! A tenant is identified during the Noise handshake by which of the known
//! PSKs verifies (see [`crate::noise::accept_any`]); its `sni` is the routing
//! key for inbound TLS connections. Only the PSK (a hash of the token) is
//! stored, never the token itself.

use std::collections::HashSet;
use std::path::Path;

use anyhow::{Context, Result, bail};
use serde::{Deserialize, Serialize};

use crate::noise::Psk;

pub const DEFAULT_MAX_CONNS: usize = 64;

fn default_max_conns() -> usize {
    DEFAULT_MAX_CONNS
}

/// Trims a host, lowercases it and drops one trailing dot: the form used for
/// matching.
pub fn normalize_host(host: &str) -> String {
    let host = host.trim();
    host.strip_suffix('.').unwrap_or(host).to_ascii_lowercase()
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Tenant {
    pub name: String,
    pub psk: Psk,
    pub sni: String,
    #[serde(default = "default_max_conns")]
    pub max_conns: usize,
    #[serde(default)]
    pub disabled: bool,
}

/// A single-use PSK that can be redeemed for a new tenant.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct EnrollCode {
    pub psk: Psk,
    pub sni: String,
    #[serde(default = "default_max_conns")]
    pub max_conns: usize,
    /// Unix seconds after which the code no longer works.
    #[serde(default)]
    pub expires_at: Option<u64>,
}

/// What a successful PSK match turned out to be.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Identity {
    Tenant(Tenant),
    Code(EnrollCode),
}

/// The PSKs to try for one handshake, and what each one stands for.
#[derive(Debug, Clone, Default)]
pub struct Candidates {
    pub psks: Vec<Psk>,
    ids: Vec<Identity>,
}

impl Candidates {
    pub fn identity(&self, index: usize) -> &Identity {
        &self.ids[index]
    }
}

/// An immutable, validated snapshot of every tenant and outstanding code.
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct TenantSet {
    tenants: Vec<Tenant>,
    codes: Vec<EnrollCode>,
}

impl TenantSet {
    /// Builds a snapshot, rejecting anything that would make identification or
    /// routing ambiguous: duplicate names, duplicate hosts, a PSK shared by two
    /// entries, an empty host, or a zero connection limit.
    pub fn new(tenants: Vec<Tenant>, codes: Vec<EnrollCode>) -> Result<Self> {
        let mut names = HashSet::new();
        let mut hosts = HashSet::new();
        let mut psks = HashSet::new();
        for t in &tenants {
            if t.name.is_empty() {
                bail!("tenant has an empty name");
            }
            if !names.insert(t.name.clone()) {
                bail!("duplicate tenant name '{}'", t.name);
            }
            check_entry(&t.sni, t.max_conns, &t.name)?;
            if !hosts.insert(normalize_host(&t.sni)) {
                bail!("duplicate sni '{}' (tenant '{}')", t.sni, t.name);
            }
            if !psks.insert(t.psk) {
                bail!("tenant '{}' shares a PSK with another entry", t.name);
            }
        }
        for c in &codes {
            check_entry(&c.sni, c.max_conns, "enrollment code")?;
            if !psks.insert(c.psk) {
                bail!("an enrollment code shares a PSK with another entry");
            }
        }
        Ok(Self { tenants, codes })
    }

    pub fn tenants(&self) -> &[Tenant] {
        &self.tenants
    }

    pub fn codes(&self) -> &[EnrollCode] {
        &self.codes
    }

    pub fn tenant(&self, name: &str) -> Option<&Tenant> {
        self.tenants.iter().find(|t| t.name == name)
    }

    /// The enabled tenant routed by `sni`, if any.
    pub fn tenant_by_sni(&self, sni: &str) -> Option<&Tenant> {
        let wanted = normalize_host(sni);
        self.tenants
            .iter()
            .find(|t| !t.disabled && normalize_host(&t.sni) == wanted)
    }

    /// PSKs worth trying at `now_unix` seconds: enabled tenants and codes that
    /// have not expired.
    pub fn candidates(&self, now_unix: u64) -> Candidates {
        let mut out = Candidates::default();
        for t in self.tenants.iter().filter(|t| !t.disabled) {
            out.psks.push(t.psk);
            out.ids.push(Identity::Tenant(t.clone()));
        }
        for c in self
            .codes
            .iter()
            .filter(|c| c.expires_at.is_none_or(|at| now_unix < at))
        {
            out.psks.push(c.psk);
            out.ids.push(Identity::Code(c.clone()));
        }
        out
    }
}

fn check_entry(sni: &str, max_conns: usize, what: &str) -> Result<()> {
    if normalize_host(sni).is_empty() {
        bail!("{what} has an empty sni");
    }
    if max_conns == 0 {
        bail!("{what} has max_conns = 0");
    }
    Ok(())
}

/// `bytes` random bytes from the OS-seeded CSPRNG, as lowercase hex.
fn random_hex(bytes: usize) -> String {
    let mut buf = vec![0u8; bytes];
    rand::fill(&mut buf[..]);
    buf.iter().map(|b| format!("{b:02x}")).collect()
}

/// A fresh tenant name, `t-` and 12 hex characters. Chosen by the server, never
/// by a client.
pub fn new_tenant_name() -> String {
    format!("t-{}", random_hex(6))
}

/// A fresh long-term tenant token: 256 random bits as hex.
pub fn new_token() -> String {
    random_hex(32)
}

/// A fresh enrollment code (128 random bits as hex) and the entry that
/// redeems it, bound to `sni`. The code itself is shown to whoever enrolls and
/// is not stored.
pub fn new_enroll_code(
    sni: &str,
    max_conns: usize,
    expires_at: Option<u64>,
) -> (String, EnrollCode) {
    let code = random_hex(16);
    let entry = EnrollCode {
        psk: crate::noise::derive_psk(&code),
        sni: sni.to_string(),
        max_conns,
        expires_at,
    };
    (code, entry)
}

/// Edits. Each returns a new validated set and leaves `self` untouched.
impl TenantSet {
    pub fn with_tenant(&self, tenant: Tenant) -> Result<Self> {
        let mut tenants = self.tenants.clone();
        tenants.push(tenant);
        Self::new(tenants, self.codes.clone())
    }

    pub fn with_code(&self, code: EnrollCode) -> Result<Self> {
        // A code for a host a tenant already holds could never be redeemed.
        let host = normalize_host(&code.sni);
        if let Some(t) = self.tenants.iter().find(|t| normalize_host(&t.sni) == host) {
            bail!("sni '{}' already belongs to tenant '{}'", code.sni, t.name);
        }
        let mut codes = self.codes.clone();
        codes.push(code);
        Self::new(self.tenants.clone(), codes)
    }

    pub fn without_tenant(&self, name: &str) -> Result<Self> {
        if self.tenant(name).is_none() {
            bail!("no tenant named '{name}'");
        }
        let tenants = self
            .tenants
            .iter()
            .filter(|t| t.name != name)
            .cloned()
            .collect();
        Self::new(tenants, self.codes.clone())
    }

    pub fn with_disabled(&self, name: &str, disabled: bool) -> Result<Self> {
        if self.tenant(name).is_none() {
            bail!("no tenant named '{name}'");
        }
        let mut tenants = self.tenants.clone();
        for t in tenants.iter_mut().filter(|t| t.name == name) {
            t.disabled = disabled;
        }
        Self::new(tenants, self.codes.clone())
    }

    /// Burns the code whose PSK is `code_psk` and creates a tenant from it
    /// under `name` with PSK `psk`. Fails, changing nothing, if there is no such
    /// code or it has expired at `now_unix`.
    pub fn redeem(
        &self,
        code_psk: &Psk,
        now_unix: u64,
        name: String,
        psk: Psk,
    ) -> Result<(Self, Tenant)> {
        let code = self
            .codes
            .iter()
            .find(|c| c.psk == *code_psk && c.expires_at.is_none_or(|at| now_unix < at))
            .context("unknown, used or expired enrollment code")?;
        let tenant = Tenant {
            name,
            psk,
            sni: code.sni.clone(),
            max_conns: code.max_conns,
            disabled: false,
        };
        let codes = self
            .codes
            .iter()
            // Expired codes can never be redeemed; drop them while here.
            .filter(|c| c.psk != *code_psk && c.expires_at.is_none_or(|at| now_unix < at))
            .cloned()
            .collect();
        let mut tenants = self.tenants.clone();
        tenants.push(tenant.clone());
        Ok((Self::new(tenants, codes)?, tenant))
    }
}

/// On-disk shape of the tenant file.
#[derive(Debug, Default, Serialize, Deserialize)]
struct TenantFile {
    #[serde(default)]
    tenant: Vec<Tenant>,
    #[serde(default)]
    code: Vec<EnrollCode>,
}

impl TenantSet {
    pub fn from_toml(text: &str) -> Result<Self> {
        let file: TenantFile = toml::from_str(text).context("parsing tenant file")?;
        Self::new(file.tenant, file.code)
    }

    pub fn to_toml(&self) -> Result<String> {
        let file = TenantFile {
            tenant: self.tenants.clone(),
            code: self.codes.clone(),
        };
        toml::to_string_pretty(&file).context("serialising tenant file")
    }

    /// Reads a tenant file. A missing file is an empty set, so a fresh
    /// deployment can start `serve` before issuing anything.
    pub fn load(path: &Path) -> Result<Self> {
        match std::fs::read_to_string(path) {
            Ok(text) => Self::from_toml(&text)
                .with_context(|| format!("loading tenant file {}", path.display())),
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => Ok(Self::default()),
            Err(e) => Err(e).with_context(|| format!("reading {}", path.display())),
        }
    }
}

/// Serde adapter storing a 32-byte PSK as 64 hex characters.
#[cfg(test)]
mod tests {
    use super::*;
    use crate::noise::derive_psk;

    fn tenant(name: &str, token: &str, sni: &str) -> Tenant {
        Tenant {
            name: name.into(),
            psk: derive_psk(token),
            sni: sni.into(),
            max_conns: 8,
            disabled: false,
        }
    }

    fn code(token: &str, sni: &str, expires_at: Option<u64>) -> EnrollCode {
        EnrollCode {
            psk: derive_psk(token),
            sni: sni.into(),
            max_conns: 8,
            expires_at,
        }
    }

    #[test]
    fn sni_lookup_ignores_case_and_trailing_dot() {
        let set = TenantSet::new(vec![tenant("a", "ta", "Abc.Example.com")], vec![]).unwrap();
        assert_eq!(set.tenant_by_sni("abc.example.COM.").unwrap().name, "a");
        assert!(set.tenant_by_sni("other.example.com").is_none());
    }

    #[test]
    fn disabled_tenants_are_neither_routed_nor_candidates() {
        let mut t = tenant("a", "ta", "a.example.com");
        t.disabled = true;
        let set = TenantSet::new(vec![t], vec![]).unwrap();
        assert!(set.tenant_by_sni("a.example.com").is_none());
        assert!(set.candidates(0).psks.is_empty());
    }

    #[test]
    fn expired_codes_are_not_candidates() {
        let set = TenantSet::new(
            vec![],
            vec![
                code("fresh", "a.x", Some(200)),
                code("stale", "b.x", Some(100)),
            ],
        )
        .unwrap();
        let c = set.candidates(150);
        assert_eq!(c.psks, vec![derive_psk("fresh")]);
        assert!(matches!(c.identity(0), Identity::Code(_)));
        assert!(set.candidates(100).psks.len() == 1, "expiry is exclusive");
    }

    #[test]
    fn candidates_map_back_to_their_identity() {
        let set = TenantSet::new(
            vec![tenant("a", "ta", "a.x"), tenant("b", "tb", "b.x")],
            vec![code("c1", "c.x", None)],
        )
        .unwrap();
        let c = set.candidates(0);
        let at = |token: &str| c.psks.iter().position(|p| *p == derive_psk(token)).unwrap();
        assert!(matches!(c.identity(at("tb")), Identity::Tenant(t) if t.name == "b"));
        assert!(matches!(c.identity(at("c1")), Identity::Code(_)));
    }

    #[test]
    fn ambiguous_sets_are_rejected() {
        let dup_name = TenantSet::new(
            vec![tenant("a", "t1", "a.x"), tenant("a", "t2", "b.x")],
            vec![],
        );
        let dup_host = TenantSet::new(
            vec![tenant("a", "t1", "A.x"), tenant("b", "t2", "a.X")],
            vec![],
        );
        let dup_psk = TenantSet::new(
            vec![tenant("a", "same", "a.x"), tenant("b", "same", "b.x")],
            vec![],
        );
        let code_psk = TenantSet::new(
            vec![tenant("a", "same", "a.x")],
            vec![code("same", "z.x", None)],
        );
        let empty_sni = TenantSet::new(vec![tenant("a", "t", " ")], vec![]);
        let zero = {
            let mut t = tenant("a", "t", "a.x");
            t.max_conns = 0;
            TenantSet::new(vec![t], vec![])
        };
        for (what, r) in [
            ("duplicate name", dup_name),
            ("duplicate host", dup_host),
            ("duplicate psk", dup_psk),
            ("code psk clash", code_psk),
            ("empty sni", empty_sni),
            ("zero max_conns", zero),
        ] {
            assert!(r.is_err(), "{what} should be rejected");
        }
    }

    #[test]
    fn toml_round_trip_stores_the_psk_as_hex() {
        #[derive(Serialize, Deserialize, PartialEq, Debug)]
        struct File {
            tenant: Vec<Tenant>,
        }
        let original = File {
            tenant: vec![tenant("a", "ta", "a.x")],
        };
        let text = toml::to_string(&original).unwrap();
        assert!(text.contains(&format!(
            "psk = \"{}\"",
            hex_of(derive_psk("ta").as_bytes())
        )));
        assert!(!text.contains("\"ta\""), "the token must never be stored");
        assert_eq!(toml::from_str::<File>(&text).unwrap(), original);
    }

    #[test]
    fn generated_names_tokens_and_codes_are_well_formed_and_unique() {
        let mut seen = HashSet::new();
        for _ in 0..1000 {
            let name = new_tenant_name();
            assert!(name.starts_with("t-") && name.len() == 14, "{name}");
            let token = new_token();
            assert_eq!(token.len(), 64);
            let (code, entry) = new_enroll_code("a.x", 4, None);
            assert_eq!(code.len(), 32);
            assert_eq!(entry.psk, derive_psk(&code));
            assert!(seen.insert(name) && seen.insert(token) && seen.insert(code));
        }
    }

    #[test]
    fn malformed_psk_is_rejected_on_load() {
        let bad = "name = \"a\"\npsk = \"zz\"\nsni = \"a.x\"\n";
        assert!(toml::from_str::<Tenant>(bad).is_err());
    }

    fn hex_of(b: &[u8; 32]) -> String {
        b.iter().map(|x| format!("{x:02x}")).collect()
    }
}

#[cfg(test)]
mod redaction_tests {
    use super::*;

    #[test]
    fn debug_output_never_contains_the_psk() {
        let tenant = Tenant {
            name: "t".into(),
            psk: Psk::from_bytes([0xab; 32]),
            sni: "a.example.com".into(),
            max_conns: 1,
            disabled: false,
        };
        let code = EnrollCode {
            psk: Psk::from_bytes([0xcd; 32]),
            sni: "a.example.com".into(),
            max_conns: 1,
            expires_at: None,
        };
        let shown = format!("{tenant:?} {code:?}");
        assert!(shown.contains("<redacted>"));
        assert!(!shown.contains("171") && !shown.contains("205"), "{shown}");
    }
}

#[cfg(test)]
mod prune_tests {
    use super::*;

    #[test]
    fn redeeming_drops_other_expired_codes() {
        let code = |b: u8, expires_at| EnrollCode {
            psk: Psk::from_bytes([b; 32]),
            sni: format!("h{b}.example.com"),
            max_conns: 1,
            expires_at,
        };
        let set = TenantSet::new(
            vec![],
            vec![code(1, None), code(2, Some(50)), code(3, Some(500))],
        )
        .unwrap();
        let (next, _) = set
            .redeem(
                &Psk::from_bytes([1; 32]),
                100,
                "t-x".into(),
                Psk::from_bytes([9; 32]),
            )
            .unwrap();
        let left: Vec<u8> = next.codes().iter().map(|c| c.psk.as_bytes()[0]).collect();
        assert_eq!(left, vec![3]);
    }
}