isb-core 1.9.0

isb's foundation: the incus client, the sandbox spec and planner, compose projects, stacks, orgs, the registry and ingress. Use the `isb` crate.
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
//! The incus side of a sandbox's egress policy.
//!
//! Each sandbox with an `egress:` policy gets a bridge network of its own
//! (`isbbrx<hash>`) and a network ACL, and its NIC is attached to both:
//!
//! - the bridge does not NAT or route, so the host forwards nothing for it;
//! - the ACL's default egress action is `drop`, and its one allow rule is
//!   TCP to the bridge's own address on the ports the policy uses, where the
//!   egress proxy listens;
//! - the bridge's dnsmasq has no upstream and answers only the allowed
//!   names (with the bridge address, so the guest connects to the proxy);
//!   with `egress: none` it does no DNS at all.
//!
//! Nothing here needs root: incusd owns the bridge, the ACL and dnsmasq.

use std::collections::BTreeMap;

use serde_json::{Value, json};

use super::policy::Policy;
use crate::client::{Client, encode_segment};
use crate::error::{Error, Result};

/// Egress bridges are named `isbbrx<8 hex>`, under the `isbbr+` pattern
/// `isb host setup` opens DHCP and DNS for, and the `isbbrx+` pattern it
/// opens the proxy's ports on.
pub const NET_PREFIX: &str = "isbbrx";
/// Marks the bridge, ACL and instance as one sandbox's egress: `project/instance`.
pub const KEY_FOR: &str = "user.isb.egress-for";
/// The policy as JSON, on the bridge (what the proxy enforces) and the instance.
pub const KEY_POLICY: &str = "user.isb.egress";
/// When the bridge was made (unix seconds): garbage collection leaves a young one alone.
pub const KEY_CREATED: &str = "user.isb.egress-created";
/// The fingerprint of the CA the guest trusts, once it does.
pub const KEY_CA_INSTALLED: &str = "user.isb.egress-ca";

pub type Props = BTreeMap<String, String>;

fn digest8(project: &str, instance: &str) -> String {
    let d = ring::digest::digest(
        &ring::digest::SHA256,
        format!("isb-egress\0{project}\0{instance}").as_bytes(),
    );
    d.as_ref()[..4].iter().map(|b| format!("{b:02x}")).collect()
}

/// The bridge for sandbox `instance` in incus project `project`.
pub fn network_name(project: &str, instance: &str) -> String {
    format!("{NET_PREFIX}{}", digest8(project, instance))
}

/// The ACL for the same sandbox.
pub fn acl_name(project: &str, instance: &str) -> String {
    format!("isbx-{}", digest8(project, instance))
}

/// Everything about one sandbox's egress plumbing.
#[derive(Debug, Clone, PartialEq)]
pub struct Plumbing {
    pub project: String,
    pub instance: String,
    pub network: String,
    pub acl: String,
    pub policy: Policy,
}

impl Plumbing {
    pub fn new(project: &str, instance: &str, policy: Policy) -> Plumbing {
        Plumbing {
            project: project.to_string(),
            instance: instance.to_string(),
            network: network_name(project, instance),
            acl: acl_name(project, instance),
            policy,
        }
    }

    /// `project/instance`, the value of [`KEY_FOR`].
    pub fn owner(&self) -> String {
        format!("{}/{}", self.project, self.instance)
    }

    /// The instance's NIC: the egress bridge, behind the ACL.
    pub fn nic(&self) -> Props {
        [
            ("type", "nic"),
            ("name", "eth0"),
            ("network", self.network.as_str()),
            ("security.acls", self.acl.as_str()),
            ("security.acls.default.egress.action", "drop"),
            ("security.acls.default.ingress.action", "allow"),
        ]
        .into_iter()
        .map(|(k, v)| (k.to_string(), v.to_string()))
        .collect()
    }

    /// The policy as stored in `user.isb.egress`.
    pub fn policy_json(&self) -> String {
        let mut v = serde_json::to_value(&self.policy).expect("a policy serializes");
        v["project"] = json!(self.project);
        v["instance"] = json!(self.instance);
        v.to_string()
    }

    /// dnsmasq's extra configuration: no upstream, no hosts file, and an
    /// answer (the bridge address) only for the allowed names. Everything
    /// else is NXDOMAIN, and nothing is ever forwarded.
    pub fn dnsmasq(&self, ip: &str) -> String {
        if self.policy.is_none() {
            return String::new();
        }
        let mut lines = vec![
            "no-resolv".to_string(),
            "no-hosts".into(),
            "address=/#/".into(),
        ];
        lines.extend(
            self.policy
                .dns_names()
                .into_iter()
                .map(|n| format!("address=/{n}/{ip}")),
        );
        lines.join("\n")
    }

    /// The bridge's config. `ip` is the address incus picked for it. With
    /// no hosts allowed the bridge has no address at all: nothing for the
    /// guest to reach, not even a DNS port.
    pub fn network_config(&self, ip: Option<&str>, created: u64) -> Props {
        let mut c: Props = [
            (
                "ipv4.address",
                if self.policy.is_none() {
                    "none"
                } else {
                    "auto"
                },
            ),
            ("ipv4.nat", "false"),
            ("ipv4.routing", "false"),
            ("ipv6.address", "none"),
        ]
        .into_iter()
        .map(|(k, v)| (k.to_string(), v.to_string()))
        .collect();
        c.insert(KEY_FOR.into(), self.owner());
        c.insert(KEY_POLICY.into(), self.policy_json());
        c.insert(KEY_CREATED.into(), created.to_string());
        if let (false, Some(ip)) = (self.policy.is_none(), ip) {
            c.insert("raw.dnsmasq".into(), self.dnsmasq(ip));
        }
        c
    }

    /// The ACL body: TCP to the bridge address on the policy's ports, and
    /// nothing else (the default action drops the rest).
    pub fn acl_body(&self, ip: &str) -> Value {
        let ports: Vec<String> = self.policy.ports().iter().map(u16::to_string).collect();
        let egress = if ports.is_empty() {
            json!([])
        } else {
            json!([{
                "action": "allow",
                "state": "enabled",
                "protocol": "tcp",
                "destination": format!("{ip}/32"),
                "destination_port": ports.join(","),
                "description": "the egress proxy",
            }])
        };
        json!({
            "description": format!("isb egress for {}", self.owner()),
            "egress": egress,
            "ingress": [],
            "config": {KEY_FOR: self.owner()},
        })
    }
}

fn host(base: &Client) -> Client {
    base.clone().project("default")
}

/// The address of a bridge, from its `ipv4.address` (`10.1.2.1/24`).
pub fn bridge_ip(net: &Value) -> Option<String> {
    net["config"]["ipv4.address"]
        .as_str()
        .and_then(|a| a.split('/').next())
        .filter(|a| a.parse::<std::net::Ipv4Addr>().is_ok())
        .map(String::from)
}

fn now() -> u64 {
    std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .map(|d| d.as_secs())
        .unwrap_or(0)
}

/// Make the bridge and the ACL, or bring existing ones to `p`. Idempotent;
/// the instance's NIC can refer to them once this returns. Returns the
/// bridge address the proxy listens on.
pub fn prepare(client: &Client, p: &Plumbing, report: &mut dyn FnMut(&str)) -> Result<String> {
    let h = host(client);
    let t = h.get_timeouts().other;
    let net_path = format!("/1.0/networks/{}", encode_segment(&p.network));
    let mut net = h.get_opt(&net_path)?;
    if let Some(n) = &net {
        let owner = n["config"][KEY_FOR].as_str().unwrap_or_default();
        if owner != p.owner() {
            return Err(Error::invalid(format!(
                "network {} exists and is not the egress network of {}",
                p.network,
                p.owner()
            )));
        }
    } else {
        report(&format!(
            "{}: creating egress network {}",
            p.instance, p.network
        ));
        let body = json!({
            "name": p.network,
            "type": "bridge",
            "description": format!("isb egress for {}", p.owner()),
            "config": p.network_config(None, now()),
        });
        h.mutate(
            "POST",
            "/1.0/networks",
            Some(&body),
            &format!("create network {}", p.network),
            t,
        )?;
        net = Some(h.get(&net_path)?);
    }
    let mut net = net.expect("set above");
    let none = p.policy.is_none();
    if !none && bridge_ip(&net).is_none() {
        // It was `egress: none`, with no address: give the bridge one.
        h.mutate(
            "PATCH",
            &net_path,
            Some(&json!({"config": {"ipv4.address": "auto"}})),
            &format!("give network {} an address", p.network),
            t,
        )?;
        net = h.get(&net_path)?;
    }
    let ip = match bridge_ip(&net) {
        Some(ip) => ip,
        None if none => String::new(),
        None => {
            return Err(Error::invalid(format!(
                "network {} has no IPv4 address",
                p.network
            )));
        }
    };
    // Bring the config to the policy: only keys that differ are written.
    let mut want = p.network_config(Some(&ip), now());
    want.remove(KEY_CREATED);
    if !none {
        want.remove("ipv4.address");
    }
    let have = &net["config"];
    let mut cfg = have.clone();
    let mut changed = false;
    for (k, v) in &want {
        if have[k].as_str() != Some(v.as_str()) {
            cfg[k] = json!(v);
            changed = true;
        }
    }
    if !want.contains_key("raw.dnsmasq") && have.get("raw.dnsmasq").is_some() {
        cfg.as_object_mut()
            .expect("config is a map")
            .remove("raw.dnsmasq");
        changed = true;
    }
    if changed {
        report(&format!(
            "{}: updating egress network {}",
            p.instance, p.network
        ));
        h.mutate(
            "PUT",
            &net_path,
            Some(&json!({"description": net["description"], "config": cfg})),
            &format!("update network {}", p.network),
            t,
        )?;
    }
    put_acl(&h, p, &ip)?;
    allow_in_project(client, &p.network, true)?;
    super::ca::kick();
    Ok(ip)
}

fn put_acl(h: &Client, p: &Plumbing, ip: &str) -> Result<()> {
    let t = h.get_timeouts().other;
    let path = format!("/1.0/network-acls/{}", encode_segment(&p.acl));
    let body = p.acl_body(ip);
    if h.get_opt(&path)?.is_some() {
        h.mutate(
            "PUT",
            &path,
            Some(&body),
            &format!("update ACL {}", p.acl),
            t,
        )?;
    } else {
        let mut b = body;
        b["name"] = json!(p.acl);
        h.mutate(
            "POST",
            "/1.0/network-acls",
            Some(&b),
            &format!("create ACL {}", p.acl),
            t,
        )?;
    }
    Ok(())
}

/// A restricted project (an org's) lists the networks its instances may
/// use; the egress bridge has to be on that list. A project that is not
/// restricted needs nothing.
fn allow_in_project(client: &Client, network: &str, add: bool) -> Result<()> {
    let h = host(client);
    let path = format!("/1.0/projects/{}", encode_segment(client.project_name()));
    let Some(p) = h.get_opt(&path)? else {
        return Ok(());
    };
    let cfg = &p["config"];
    if cfg["restricted"].as_str() != Some("true") {
        return Ok(());
    }
    let list = cfg["restricted.networks.access"]
        .as_str()
        .unwrap_or_default();
    let mut names: Vec<&str> = list
        .split(',')
        .map(str::trim)
        .filter(|s| !s.is_empty())
        .collect();
    if add == names.contains(&network) {
        return Ok(());
    }
    if add {
        names.push(network);
    } else {
        names.retain(|n| *n != network);
    }
    let mut cfg = cfg.clone();
    cfg["restricted.networks.access"] = json!(names.join(","));
    h.mutate(
        "PUT",
        &path,
        Some(&json!({"description": p["description"], "config": cfg})),
        &format!(
            "allow network {network} in project {}",
            client.project_name()
        ),
        h.get_timeouts().other,
    )?;
    Ok(())
}

/// Delete a sandbox's egress bridge, ACL and CA, once its instance is gone.
/// Missing pieces are fine.
pub fn teardown(client: &Client, instance: &str) -> Result<()> {
    let project = client.project_name();
    let h = host(client);
    let t = h.get_timeouts().other;
    let (net, acl) = (network_name(project, instance), acl_name(project, instance));
    let net_path = format!("/1.0/networks/{}", encode_segment(&net));
    if let Some(n) = h.get_opt(&net_path)? {
        // Never delete what is not this sandbox's.
        if n["config"][KEY_FOR].as_str() != Some(format!("{project}/{instance}").as_str()) {
            return Ok(());
        }
        allow_in_project(client, &net, false)?;
        h.mutate(
            "DELETE",
            &net_path,
            None,
            &format!("delete network {net}"),
            t,
        )?;
    }
    let acl_path = format!("/1.0/network-acls/{}", encode_segment(&acl));
    if h.get_opt(&acl_path)?.is_some() {
        h.mutate("DELETE", &acl_path, None, &format!("delete ACL {acl}"), t)?;
    }
    super::ca::forget(&net);
    Ok(())
}

/// The egress bridges incus holds, each with its policy: what the proxy
/// manager reconciles against.
pub fn list(client: &Client) -> Result<Vec<Value>> {
    let v = host(client).get("/1.0/networks?recursion=1")?;
    Ok(v.as_array()
        .map(|a| {
            a.iter()
                .filter(|n| n["config"][KEY_FOR].is_string())
                .cloned()
                .collect()
        })
        .unwrap_or_default())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::egress::policy::EgressSpec;

    fn plumbing(spec: &EgressSpec) -> Plumbing {
        let n = network_name("default", "plugin");
        Plumbing::new("default", "plugin", Policy::from_spec(spec, &n).unwrap())
    }

    #[test]
    fn names_are_stable_short_and_per_sandbox() {
        let n = network_name("default", "plugin");
        assert!(n.starts_with("isbbrx") && n.len() == 14 && n.len() <= 15);
        assert_eq!(n, network_name("default", "plugin"));
        assert_ne!(n, network_name("isb-acme", "plugin"));
        assert_ne!(n, network_name("default", "other"));
        assert!(acl_name("default", "plugin").starts_with("isbx-"));
    }

    #[test]
    fn dnsmasq_answers_only_allowed_names() {
        let p = plumbing(&EgressSpec::allow(["api.example.com", "*.cdn.net:8443"]));
        assert_eq!(
            p.dnsmasq("10.9.8.1"),
            "no-resolv\nno-hosts\naddress=/#/\naddress=/api.example.com/10.9.8.1\naddress=/cdn.net/10.9.8.1"
        );
        assert_eq!(plumbing(&EgressSpec::none()).dnsmasq("10.9.8.1"), "");
    }

    #[test]
    fn none_leaves_the_bridge_without_an_address_and_a_list_turns_dns_on() {
        let none = plumbing(&EgressSpec::none()).network_config(None, 1);
        assert_eq!(none["ipv4.address"], "none");
        assert!(!none.contains_key("raw.dnsmasq"));
        assert!(!none.contains_key("dns.mode"));
        let some =
            plumbing(&EgressSpec::allow(["a.example.com"])).network_config(Some("10.9.8.1"), 1);
        assert!(some["raw.dnsmasq"].contains("address=/a.example.com/10.9.8.1"));
        assert_eq!(some["ipv4.address"], "auto");
        for c in [&none, &some] {
            assert_eq!(c["ipv4.nat"], "false");
            assert_eq!(c["ipv4.routing"], "false");
            assert_eq!(c["ipv6.address"], "none");
            assert_eq!(c[KEY_FOR], "default/plugin");
        }
    }

    #[test]
    fn the_acl_allows_only_the_proxy_ports_on_the_bridge_address() {
        let p = plumbing(&EgressSpec::allow([
            "a.example.com",
            "b.example.com:8443",
            "c.example.com:80",
        ]));
        let acl = p.acl_body("10.9.8.1");
        let rules = acl["egress"].as_array().unwrap();
        assert_eq!(rules.len(), 1);
        assert_eq!(rules[0]["action"], "allow");
        assert_eq!(rules[0]["protocol"], "tcp");
        assert_eq!(rules[0]["destination"], "10.9.8.1/32");
        assert_eq!(rules[0]["destination_port"], "80,443,8443");
        assert_eq!(acl["ingress"].as_array().unwrap().len(), 0);
        let none = plumbing(&EgressSpec::none()).acl_body("10.9.8.1");
        assert_eq!(none["egress"].as_array().unwrap().len(), 0);
    }

    #[test]
    fn the_nic_drops_by_default() {
        let nic = plumbing(&EgressSpec::none()).nic();
        assert_eq!(nic["type"], "nic");
        assert_eq!(nic["network"], network_name("default", "plugin"));
        assert_eq!(nic["security.acls.default.egress.action"], "drop");
        assert_eq!(nic["name"], "eth0");
    }

    #[test]
    fn the_stored_policy_names_the_sandbox() {
        let p = plumbing(&EgressSpec::allow(["a.example.com"]));
        let v: Value = serde_json::from_str(&p.policy_json()).unwrap();
        assert_eq!(v["project"], "default");
        assert_eq!(v["instance"], "plugin");
        assert_eq!(v["entries"][0], "a.example.com:443");
    }

    #[test]
    fn a_bridge_address_is_read_without_its_prefix() {
        let n = json!({"config": {"ipv4.address": "10.41.184.1/24"}});
        assert_eq!(bridge_ip(&n).as_deref(), Some("10.41.184.1"));
        assert_eq!(
            bridge_ip(&json!({"config": {"ipv4.address": "none"}})),
            None
        );
    }
}