sew-proto 0.11.6

Sew wire contracts: ids, addresses, errors, HTTP and gateway bodies, permissions, policies, audit.
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
//! Install inputs as JSON: `sew install --config-json <FILE|->` and
//! `sew media install --config-json <FILE|->` take their URLs, hosts and paths as one JSON
//! object (from a file, or stdin with `-`) instead of argv. K2's root helper allows argv tokens
//! of at most 64 characters from `[A-Za-z0-9-_.@=]`, which a URL or a path cannot be.
//!
//! The keys are the value flags' names (`--k2-jwks-url` → `k2_jwks_url`; repeatable flags are
//! plural arrays: `--mgmt-user` → `mgmt_users`, `--stun-server` → `stun_servers`,
//! `--turn-plain` → `turn_plain`). Mode flags (`--no-start`, `--reconfigure`, `--restart`,
//! `--now`, `--print-units`) stay on argv. Unknown keys and wrong types are refused, every
//! value is checked here ([`Validate`]), and a key given both in the JSON and as its flag is a
//! usage error (exit 2). The existing flags keep working alone. The JSON never carries key
//! material: `master_key_from` is an absolute path (not `-`).

use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::error::{Validate, ValidationError};

/// Largest `--config-json` input, bytes.
pub const CONFIG_JSON_MAX_BYTES: usize = 65_536;
/// Longest string value, bytes.
pub const INPUT_MAX_BYTES: usize = 2_048;
/// Most entries in an array value.
pub const INPUT_LIST_MAX: usize = 16;

/// `sew install --config-json`.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct InstallInputs {
    /// `--provider`: `standalone` or `k2`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub provider: Option<String>,
    /// `--k2-jwks-url`: K2's JWKS URL (with provider `k2`).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub k2_jwks_url: Option<String>,
    /// `--profile`: `dedicated` or `shared`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub profile: Option<String>,
    /// `--pg-major`: the Postgres major, 16–99.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub pg_major: Option<u32>,
    /// `--master-key-from`: an absolute path to the master key file (never `-`).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub master_key_from: Option<String>,
    /// `--cred-mode`: `encrypted` or `plain`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub cred_mode: Option<String>,
    /// `--mgmt-user` (repeatable): users to add to `sew-mgmt`, 0–16 login names.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub mgmt_users: Vec<String>,
    /// `[mgmt] allow_uids`: integrator uids allowed on the mgmt socket besides the
    /// `sew-mgmt` group; `sew install` writes them to `sew.toml` (absent: the file keeps what
    /// it has; `[]` removes the key). 0–16 distinct uids, never 0. JSON only, no flag.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub mgmt_allow_uids: Option<Vec<u32>>,
}

/// Most uids in `mgmt_allow_uids` (the server's `[mgmt] allow_uids` cap).
pub const MGMT_ALLOW_UIDS_MAX: usize = 16;

/// `sew media install --config-json`.
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(deny_unknown_fields)]
pub struct MediaInstallInputs {
    /// `--tarball`: an absolute path to the pinned sew-media tarball.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub tarball: Option<String>,
    /// `--public-url`: the signalling URL clients use (`wss://voice.<host>`).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub public_url: Option<String>,
    /// `--node-ip`: public `IPv4[,IPv6]` the engine advertises, or `stun`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub node_ip: Option<String>,
    /// `--stun-server` (repeatable): STUN `host:port` for node IP `stun`.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub stun_servers: Vec<String>,
    /// `--allow-private-node-ip`: allow loopback and private node IPs.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub allow_private_node_ip: Option<bool>,
    /// `--udp-port`: the UDP media port, 1–65535.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub udp_port: Option<u16>,
    /// `--tcp-port`: the ICE-TCP port, 1–65535.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub tcp_port: Option<u16>,
    /// `--bind-v4`: the IPv4 bind address (`""` turns IPv4 off).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub bind_v4: Option<String>,
    /// `--bind-v6`: the IPv6 bind address (`""` turns IPv6 off).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub bind_v6: Option<String>,
    /// `--turn-tls`: `direct`, `external`, `passthrough`, `plain` or `off`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub turn_tls: Option<String>,
    /// `--turn-domain`: the TURN DNS name (`turn.<host>`).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub turn_domain: Option<String>,
    /// `--turn-cert`: an absolute path to the TURN TLS certificate chain (PEM).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub turn_cert: Option<String>,
    /// `--turn-key`: an absolute path to the TURN TLS private key (PEM, root-only file).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub turn_key: Option<String>,
    /// `--turn-advertise-port`: the advertised TURN TLS port (0 = the listener's).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub turn_advertise_port: Option<u16>,
    /// `--turn-proxy-protocol`: `v1` or `v2` (with TURN TLS `external`).
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub turn_proxy_protocol: Option<String>,
    /// `--turn-plain`: plain TURN listeners, each `udp` or `tcp`.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub turn_plain: Vec<String>,
    /// `--profile`: `dedicated` or `shared`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub profile: Option<String>,
    /// `--cred-mode`: `encrypted` or `plain`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub cred_mode: Option<String>,
    /// `--sysctl`: `apply` or `skip`.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub sysctl: Option<String>,
    /// `--relay-only`: `voice.ops.ice_relay_only`. `true` with the tunnel
    /// tier (`passthrough`), `false` in direct mode; absent keeps the config's value.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub relay_only: Option<bool>,
    /// `--turn-trusted-cidr` (repeatable): `voice.ops.turn_trusted_cidrs`, the sources
    /// whose PROXY header the TURN `external` and `passthrough` listeners trust (1 to 16
    /// CIDRs). K2 sends `["127.0.0.1/32", "::1/128"]` for the tunnel tier (0.7.1+); empty or
    /// absent keeps the config's value.
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub turn_trusted_cidrs: Vec<String>,
}

/// `addr/prefix` with an IPv4 (prefix 0 to 32) or IPv6 (0 to 128) address.
pub fn is_cidr(s: &str) -> bool {
    let Some((ip, len)) = s.split_once('/') else {
        return false;
    };
    if len.is_empty() || !len.bytes().all(|b| b.is_ascii_digit()) {
        return false;
    }
    let Ok(len) = len.parse::<u8>() else {
        return false;
    };
    match ip.parse::<std::net::IpAddr>() {
        Ok(std::net::IpAddr::V4(_)) => len <= 32,
        Ok(std::net::IpAddr::V6(_)) => len <= 128,
        Err(_) => false,
    }
}

fn text(field: &str, v: &str, allow_empty: bool) -> Result<(), ValidationError> {
    if v.is_empty() && !allow_empty {
        return Err(ValidationError::invalid(field, "must not be empty"));
    }
    if v.len() > INPUT_MAX_BYTES {
        return Err(ValidationError::invalid(
            field,
            format!("at most {INPUT_MAX_BYTES} bytes"),
        ));
    }
    if v.chars().any(char::is_control) {
        return Err(ValidationError::invalid(
            field,
            "control characters are not allowed",
        ));
    }
    Ok(())
}

fn opt_text(field: &str, v: &Option<String>) -> Result<(), ValidationError> {
    v.as_deref().map_or(Ok(()), |s| text(field, s, false))
}

fn one_of(field: &str, v: &Option<String>, allowed: &[&str]) -> Result<(), ValidationError> {
    match v.as_deref() {
        Some(s) if !allowed.contains(&s) => Err(ValidationError::invalid(
            field,
            format!("must be one of {}", allowed.join(", ")),
        )),
        _ => Ok(()),
    }
}

fn abs_path(field: &str, v: &Option<String>) -> Result<(), ValidationError> {
    match v.as_deref() {
        None => Ok(()),
        Some(s) => {
            text(field, s, false)?;
            if s.starts_with('/') {
                Ok(())
            } else {
                Err(ValidationError::invalid(field, "must be an absolute path"))
            }
        }
    }
}

fn url(field: &str, v: &Option<String>, schemes: &[&str]) -> Result<(), ValidationError> {
    match v.as_deref() {
        None => Ok(()),
        Some(s) => {
            text(field, s, false)?;
            if schemes.iter().any(|p| s.starts_with(p)) && !s.contains(char::is_whitespace) {
                Ok(())
            } else {
                Err(ValidationError::invalid(
                    field,
                    format!("must be a URL starting with {}", schemes.join(" or ")),
                ))
            }
        }
    }
}

fn list(
    field: &str,
    v: &[String],
    each: impl Fn(&str) -> bool,
    what: &str,
) -> Result<(), ValidationError> {
    if v.len() > INPUT_LIST_MAX {
        return Err(ValidationError::invalid(
            field,
            format!("at most {INPUT_LIST_MAX} entries"),
        ));
    }
    for (i, s) in v.iter().enumerate() {
        let f = format!("{field}.{i}");
        text(&f, s, false)?;
        if !each(s) {
            return Err(ValidationError::invalid(f, what));
        }
    }
    let mut seen: Vec<&String> = Vec::new();
    for s in v {
        if seen.contains(&s) {
            return Err(ValidationError::invalid(
                field,
                format!("{s:?} is listed twice"),
            ));
        }
        seen.push(s);
    }
    Ok(())
}

fn login_name(s: &str) -> bool {
    let b = s.as_bytes();
    (1..=32).contains(&b.len())
        && (b[0].is_ascii_lowercase() || b[0] == b'_')
        && b.iter()
            .all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || *c == b'_' || *c == b'-')
}

impl Validate for InstallInputs {
    fn validate(&self) -> Result<(), ValidationError> {
        one_of("provider", &self.provider, &["standalone", "k2"])?;
        url("k2_jwks_url", &self.k2_jwks_url, &["http://", "https://"])?;
        one_of("profile", &self.profile, &["dedicated", "shared"])?;
        if let Some(m) = self.pg_major
            && !(16..=99).contains(&m)
        {
            return Err(ValidationError::invalid("pg_major", "must be 16 to 99"));
        }
        abs_path("master_key_from", &self.master_key_from)?;
        one_of("cred_mode", &self.cred_mode, &["encrypted", "plain"])?;
        list(
            "mgmt_users",
            &self.mgmt_users,
            login_name,
            "must be a login name ([a-z_][a-z0-9_-]{0,31})",
        )?;
        if let Some(uids) = &self.mgmt_allow_uids {
            if uids.len() > MGMT_ALLOW_UIDS_MAX {
                return Err(ValidationError::invalid(
                    "mgmt_allow_uids",
                    format!("at most {MGMT_ALLOW_UIDS_MAX} uids"),
                ));
            }
            for (i, u) in uids.iter().enumerate() {
                if *u == 0 {
                    return Err(ValidationError::invalid(
                        format!("mgmt_allow_uids.{i}"),
                        "root (uid 0) is always allowed; do not list it",
                    ));
                }
                if uids[..i].contains(u) {
                    return Err(ValidationError::invalid(
                        format!("mgmt_allow_uids.{i}"),
                        "listed twice",
                    ));
                }
            }
        }
        Ok(())
    }
}

impl Validate for MediaInstallInputs {
    fn validate(&self) -> Result<(), ValidationError> {
        abs_path("tarball", &self.tarball)?;
        url("public_url", &self.public_url, &["wss://", "ws://"])?;
        opt_text("node_ip", &self.node_ip)?;
        list(
            "stun_servers",
            &self.stun_servers,
            |s| {
                s.rsplit_once(':')
                    .is_some_and(|(h, p)| !h.is_empty() && p.parse::<u16>().is_ok_and(|p| p > 0))
            },
            "must be host:port",
        )?;
        for (field, v) in [("udp_port", self.udp_port), ("tcp_port", self.tcp_port)] {
            if v == Some(0) {
                return Err(ValidationError::invalid(field, "must be 1 to 65535"));
            }
        }
        for (field, v) in [("bind_v4", &self.bind_v4), ("bind_v6", &self.bind_v6)] {
            if let Some(s) = v {
                text(field, s, true)?;
            }
        }
        one_of(
            "turn_tls",
            &self.turn_tls,
            &["direct", "external", "passthrough", "plain", "off"],
        )?;
        opt_text("turn_domain", &self.turn_domain)?;
        abs_path("turn_cert", &self.turn_cert)?;
        abs_path("turn_key", &self.turn_key)?;
        one_of(
            "turn_proxy_protocol",
            &self.turn_proxy_protocol,
            &["v1", "v2"],
        )?;
        list(
            "turn_plain",
            &self.turn_plain,
            |s| s == "udp" || s == "tcp",
            "must be udp or tcp",
        )?;
        one_of("profile", &self.profile, &["dedicated", "shared"])?;
        one_of("cred_mode", &self.cred_mode, &["encrypted", "plain"])?;
        one_of("sysctl", &self.sysctl, &["apply", "skip"])?;
        list(
            "turn_trusted_cidrs",
            &self.turn_trusted_cidrs,
            is_cidr,
            "must be a CIDR (address/prefix, e.g. 127.0.0.1/32)",
        )
    }
}

#[cfg(test)]
// The package has no clippy.toml (Sew's allows unwrap in tests); the module says it itself.
#[allow(clippy::unwrap_used)]
mod tests {
    use serde_json::json;

    use super::*;

    fn install(v: serde_json::Value) -> Result<InstallInputs, String> {
        let i: InstallInputs = serde_json::from_value(v).map_err(|e| e.to_string())?;
        i.validate().map_err(|e| e.field)?;
        Ok(i)
    }

    fn media(v: serde_json::Value) -> Result<MediaInstallInputs, String> {
        let i: MediaInstallInputs = serde_json::from_value(v).map_err(|e| e.to_string())?;
        i.validate().map_err(|e| e.field)?;
        Ok(i)
    }

    #[test]
    fn install_inputs_rules() {
        let ok = install(json!({
            "provider": "k2",
            "k2_jwks_url": "http://127.0.0.1:7300/v1/chat/jwks",
            "profile": "shared",
            "pg_major": 16,
            "master_key_from": "/run/k2/sew-master.key",
            "cred_mode": "encrypted",
            "mgmt_users": ["k2", "_k2-daemon"],
            "mgmt_allow_uids": [988, 1001]
        }));
        assert_eq!(
            ok.map(|i| (i.mgmt_users.len(), i.mgmt_allow_uids)),
            Ok((2, Some(vec![988, 1001])))
        );
        assert_eq!(
            install(json!({"mgmt_allow_uids": []})).map(|i| i.mgmt_allow_uids),
            Ok(Some(vec![]))
        );
        assert!(install(json!({"mgmt_allow_uids": [-1]})).is_err());
        assert!(install(json!({"mgmt_allow_uids": "988"})).is_err());
        assert_eq!(install(json!({})), Ok(InstallInputs::default()));
        let unknown = install(json!({"provider": "k2", "jwks": "x"})).unwrap_err();
        assert!(unknown.contains("unknown field `jwks`"), "{unknown}");
        assert!(install(json!({"pg_major": "16"})).is_err());
        assert!(install(json!({"mgmt_users": "k2"})).is_err());
        for (v, field) in [
            (json!({"provider": "oidc"}), "provider"),
            (json!({"k2_jwks_url": "ftp://x/j"}), "k2_jwks_url"),
            (json!({"k2_jwks_url": "http://x/\nj"}), "k2_jwks_url"),
            (
                json!({"k2_jwks_url": format!("https://{}", "a".repeat(2_048))}),
                "k2_jwks_url",
            ),
            (json!({"profile": "huge"}), "profile"),
            (json!({"pg_major": 15}), "pg_major"),
            (json!({"master_key_from": "-"}), "master_key_from"),
            (json!({"master_key_from": "key.bin"}), "master_key_from"),
            (json!({"cred_mode": "clear"}), "cred_mode"),
            (json!({"mgmt_users": ["Root"]}), "mgmt_users.0"),
            (json!({"mgmt_users": ["k2", "k2"]}), "mgmt_users"),
            (json!({"mgmt_allow_uids": [0]}), "mgmt_allow_uids.0"),
            (json!({"mgmt_allow_uids": [988, 988]}), "mgmt_allow_uids.1"),
            (
                json!({"mgmt_allow_uids": (1..=17).collect::<Vec<u32>>()}),
                "mgmt_allow_uids",
            ),
        ] {
            assert_eq!(install(v.clone()).err().as_deref(), Some(field), "{v}");
        }
    }

    #[test]
    fn media_install_inputs_rules() {
        let ok = media(json!({
            "tarball": "/var/lib/sew/staging/sew-media.tar.zst",
            "public_url": "wss://voice.example.com",
            "node_ip": "203.0.113.20",
            "stun_servers": ["stun.example.net:3478"],
            "allow_private_node_ip": false,
            "udp_port": 7882,
            "tcp_port": 7881,
            "bind_v4": "0.0.0.0",
            "bind_v6": "",
            "turn_tls": "direct",
            "turn_domain": "turn.example.com",
            "turn_cert": "/var/lib/sew/turn/fullchain.pem",
            "turn_key": "/var/lib/sew/turn/key.pem",
            "turn_advertise_port": 5349,
            "turn_proxy_protocol": "v2",
            "turn_plain": ["udp", "tcp"],
            "profile": "shared",
            "cred_mode": "encrypted",
            "sysctl": "apply",
            "turn_trusted_cidrs": ["127.0.0.1/32", "::1/128"]
        }));
        assert!(ok.is_ok(), "{ok:?}");
        // TURN-PT: every tier K2's helper may send, `passthrough` included.
        for tier in ["direct", "external", "passthrough", "plain", "off"] {
            let v = media(json!({"turn_tls": tier}));
            assert_eq!(v.map(|m| m.turn_tls), Ok(Some(tier.to_owned())), "{tier}");
        }
        assert_eq!(media(json!({})), Ok(MediaInstallInputs::default()));
        assert!(media(json!({"publicurl": "wss://x"})).is_err());
        assert!(media(json!({"udp_port": 70000})).is_err());
        assert!(media(json!({"udp_port": -1})).is_err());
        for (v, field) in [
            (json!({"tarball": "sew-media.tar"}), "tarball"),
            (json!({"public_url": "https://voice.x"}), "public_url"),
            (json!({"stun_servers": ["stun.x"]}), "stun_servers.0"),
            (json!({"udp_port": 0}), "udp_port"),
            (json!({"turn_tls": "on"}), "turn_tls"),
            (json!({"turn_key": "key.pem"}), "turn_key"),
            (json!({"turn_proxy_protocol": "v3"}), "turn_proxy_protocol"),
            (json!({"turn_plain": ["sctp"]}), "turn_plain.0"),
            (json!({"sysctl": "yes"}), "sysctl"),
            (json!({"node_ip": ""}), "node_ip"),
            (
                json!({"turn_trusted_cidrs": ["127.0.0.1"]}),
                "turn_trusted_cidrs.0",
            ),
            (
                json!({"turn_trusted_cidrs": ["::1/128", "10.0.0.0/33"]}),
                "turn_trusted_cidrs.1",
            ),
        ] {
            assert_eq!(media(v.clone()).err().as_deref(), Some(field), "{v}");
        }
    }
}