netweft 0.1.0

Deterministic network planning and configuration generation for portable infrastructure.
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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
use std::collections::BTreeMap;
use std::fmt;
use std::net::{IpAddr, Ipv4Addr};

use ipnet::{Ipv4Net, Ipv6Net};
use serde::Deserialize;

use crate::validate::{ValidationReport, validate_bundle};

pub const SCHEMA_VERSION: u32 = 1;

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct SettingsFile {
    pub schema_version: u32,
    pub active_location: String,
    #[serde(default)]
    pub paths: PathSettings,
    #[serde(default)]
    pub render: RenderSettings,
    #[serde(default)]
    pub validation: ValidationSettings,
}

#[derive(Debug, Default, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct PathSettings {
    pub generated_root: Option<String>,
    pub state_root: Option<String>,
    pub cache_root: Option<String>,
}

#[derive(Debug, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct RenderSettings {
    pub atomic: bool,
    pub stable_order: bool,
    pub generated_headers: bool,
}

impl Default for RenderSettings {
    fn default() -> Self {
        Self {
            atomic: true,
            stable_order: true,
            generated_headers: true,
        }
    }
}

#[derive(Debug, Default, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct ValidationSettings {
    pub warn_dynamic_ipv6: bool,
    pub warn_dropbox_runtime: bool,
    pub warn_latest_images: bool,
    pub deny_warnings: bool,
}

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct InventoryFile {
    pub schema_version: u32,
    pub domains: Domains,
    pub hosts: BTreeMap<String, Host>,
}

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Domains {
    pub primary: String,
    #[serde(default)]
    pub additional: Vec<String>,
}

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Host {
    pub kind: HostKind,
    #[serde(default)]
    pub roles: Vec<String>,
    pub parent: Option<String>,
    pub runtime_root: Option<String>,
    pub ssh_user: Option<String>,
    #[serde(default = "default_true")]
    pub enabled: bool,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum HostKind {
    Physical,
    Vm,
    Laptop,
    Workstation,
    Nas,
}

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct NetworksFile {
    pub schema_version: u32,
    pub networks: BTreeMap<String, LogicalNetwork>,
}

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct LogicalNetwork {
    pub kind: NetworkKind,
    pub owner: Option<String>,
    pub docker_name: Option<String>,
    pub ipv4_cidr: Option<Ipv4Net>,
    pub ipv4_gateway: Option<Ipv4Addr>,
    pub allocation_key: String,
    pub preferred_display_id: Option<u16>,
    #[serde(default)]
    pub ula_enabled: bool,
    #[serde(default)]
    pub reverse_dns: bool,
    #[serde(default = "default_true")]
    pub dns_clients: bool,
    pub routing: Option<NetworkRouting>,
}

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct NetworkRouting {
    pub from: Option<String>,
    pub mode: RoutingMode,
    pub via: Option<String>,
}

#[derive(Debug, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "kebab-case")]
pub enum RoutingMode {
    Direct,
    ViaHost,
    HostPrivate,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum NetworkKind {
    Lan,
    Docker,
}

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ServicesFile {
    pub schema_version: u32,
    pub services: BTreeMap<String, Service>,
}

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Service {
    pub kind: ServiceKind,
    pub host: String,
    pub network: String,
    #[serde(default = "default_true")]
    pub enabled: bool,
    pub address: Option<ServiceAddress>,
    #[serde(default)]
    pub ports: Vec<PortMapping>,
    pub runtime: Option<BTreeMap<String, String>>,
    pub ingress: Option<ServiceIngress>,
    pub ssh: Option<SshService>,
    pub web: Option<WebService>,
}

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ServiceIngress {
    pub mode: IngressMode,
    pub interface: String,
}

#[derive(Debug, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "kebab-case")]
pub enum IngressMode {
    HostPort,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum ServiceKind {
    Dns,
    ReverseProxy,
    DevelopmentContainer,
    Web,
    Database,
    Generic,
}

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ServiceAddress {
    pub ipv4: Option<Ipv4Addr>,
    pub ipv6_interface_id: Option<String>,
}

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct PortMapping {
    pub host: u16,
    pub container: u16,
    pub protocol: TransportProtocol,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum TransportProtocol {
    Tcp,
    Udp,
}

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct SshService {
    pub user: String,
    pub host_port: u16,
    pub container_port: u16,
    pub route: String,
}

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct WebService {
    pub container_port: u16,
    pub domain: String,
    pub access: String,
    pub proxy: String,
    #[serde(default)]
    pub tls: bool,
}

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct DnsFile {
    pub schema_version: u32,
    pub dns: DnsSettings,
    #[serde(default)]
    pub zones: Vec<DnsZone>,
    #[serde(default)]
    pub records: Vec<DnsRecord>,
}

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct DnsSettings {
    pub provider: String,
    pub service: String,
    pub default_ttl: u32,
    pub negative_ttl: u32,
    pub soa: SoaSettings,
    pub recursion: RecursionSettings,
    pub forwarders: DnsForwarders,
}

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct SoaSettings {
    pub primary_nameserver: String,
    pub responsible_mailbox: String,
    pub refresh: u32,
    pub retry: u32,
    pub expire: u32,
}

#[derive(Debug, Deserialize)]
#[serde(default, deny_unknown_fields)]
pub struct RecursionSettings {
    pub enabled: bool,
    pub include_location_segments: bool,
    pub include_tailscale: bool,
    pub include_ula: bool,
    pub include_docker_networks: bool,
}

impl Default for RecursionSettings {
    fn default() -> Self {
        Self {
            enabled: true,
            include_location_segments: true,
            include_tailscale: true,
            include_ula: true,
            include_docker_networks: true,
        }
    }
}

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct DnsForwarders {
    #[serde(default)]
    pub ipv4: Vec<Ipv4Addr>,
    #[serde(default)]
    pub ipv6: Vec<std::net::Ipv6Addr>,
}

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct DnsZone {
    pub name: String,
    pub visibility: DnsVisibility,
    #[serde(default)]
    pub authoritative: bool,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum DnsVisibility {
    Internal,
    Public,
    Both,
}

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct DnsRecord {
    pub name: String,
    pub kind: DnsRecordKind,
    pub target: Option<String>,
    pub interface: Option<String>,
    pub address_scope: Option<AddressScope>,
    #[serde(default = "default_ipv4_families")]
    pub families: Vec<AddressFamily>,
    #[serde(default)]
    pub reverse: bool,
}

#[derive(Debug, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "kebab-case")]
pub enum DnsRecordKind {
    Host,
    Service,
    Proxy,
    Cname,
    SegmentGateway,
}

#[derive(Debug, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "kebab-case")]
pub enum AddressScope {
    Container,
    Ingress,
}

#[derive(Debug, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "lowercase")]
pub enum AddressFamily {
    Ipv4,
    Ipv6,
}

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct AllocationsFile {
    pub schema_version: u32,
    pub ula: Option<UlaAllocations>,
}

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct UlaAllocations {
    pub prefix: Ipv6Net,
    #[serde(default)]
    pub segments: BTreeMap<String, u16>,
    #[serde(default)]
    pub networks: BTreeMap<String, u16>,
}

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct LocationFile {
    pub schema_version: u32,
    pub name: String,
    pub description: Option<String>,
    pub router: Router,
    pub ipv6: LocationIpv6,
    pub segments: BTreeMap<String, Segment>,
    #[serde(default)]
    pub hosts: BTreeMap<String, LocationHost>,
    #[serde(default)]
    pub tailscale: LocationTailscale,
}

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Router {
    pub kind: String,
    pub managed: bool,
    pub supports_vlans: bool,
}

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct LocationIpv6 {
    pub mode: Ipv6Mode,
    pub prefix: Option<Ipv6Net>,
    pub subnet_prefix_length: Option<u8>,
    pub stability: PrefixStability,
    #[serde(default)]
    pub publish_public_aaaa: bool,
}

#[derive(Debug, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "kebab-case")]
pub enum Ipv6Mode {
    Disabled,
    RouterAdvertised,
    Delegated,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum PrefixStability {
    Dynamic,
    Stable,
}

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Segment {
    pub kind: SegmentKind,
    pub ipv4_cidr: Ipv4Net,
    pub ipv4_gateway: Ipv4Addr,
    pub vlan_id: Option<u16>,
    pub public_ipv6_allocation: Option<u32>,
    #[serde(default = "default_true")]
    pub dns_clients: bool,
    #[serde(default = "default_true")]
    pub reverse_dns: bool,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum SegmentKind {
    Lan,
    Vlan,
}

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct LocationHost {
    #[serde(default)]
    pub interfaces: BTreeMap<String, LocationInterface>,
}

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct LocationInterface {
    pub segment: String,
    pub ipv4: Option<Ipv4Addr>,
    pub ipv6_mode: Option<InterfaceIpv6Mode>,
    pub ula_interface_id: Option<String>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum InterfaceIpv6Mode {
    Slaac,
    Static,
    Disabled,
}

#[derive(Debug, Default, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct LocationTailscale {
    #[serde(default)]
    pub enabled: bool,
    pub strategy: Option<TailscaleStrategy>,
    pub primary_router: Option<String>,
    #[serde(default)]
    pub routers: BTreeMap<String, TailscaleRouterSettings>,
}

#[derive(Debug, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "kebab-case")]
pub enum TailscaleStrategy {
    SubnetRouter,
    HaSubnetRouter,
    DirectNodes,
}

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct TailscaleRouterSettings {
    #[serde(default = "default_true")]
    pub enabled: bool,
    #[serde(default = "default_true")]
    pub snat_subnet_routes: bool,
    #[serde(default)]
    pub accept_routes: bool,
    #[serde(default)]
    pub exit_node: bool,
    #[serde(default)]
    pub advertise: Vec<String>,
}

#[derive(Debug)]
pub struct ConfigBundle {
    pub settings: SettingsFile,
    pub inventory: InventoryFile,
    pub networks: NetworksFile,
    pub services: ServicesFile,
    pub dns: DnsFile,
    pub allocations: AllocationsFile,
    pub location: LocationFile,
}

impl ConfigBundle {
    pub fn validate(&self) -> anyhow::Result<ValidationReport> {
        validate_bundle(self)
    }

    pub fn print_summary(&self) {
        println!("Location: {}", self.location.name);
        println!("Router: {}", self.location.router.kind);
        println!("IPv6 mode: {}", self.location.ipv6.mode);
        println!("Hosts: {}", self.inventory.hosts.len());
        println!("Networks: {}", self.networks.networks.len());
        println!("Services: {}", self.services.services.len());
        println!("DNS zones: {}", self.dns.zones.len());
    }

    pub fn print_hosts(&self) {
        for (name, host) in &self.inventory.hosts {
            println!(
                "{name}\t{:?}\t{}\t{}",
                host.kind,
                if host.enabled { "enabled" } else { "disabled" },
                host.roles.join(",")
            );
        }
    }

    pub fn print_networks(&self) {
        for (name, network) in &self.networks.networks {
            println!(
                "{name}\t{:?}\t{}",
                network.kind,
                network
                    .ipv4_cidr
                    .map(|value| value.to_string())
                    .unwrap_or_else(|| "-".to_owned())
            );
        }
    }

    pub fn print_services(&self) {
        for (name, service) in &self.services.services {
            println!(
                "{name}\t{:?}\thost={}\tnetwork={}",
                service.kind, service.host, service.network
            );
        }
    }
}

impl fmt::Display for Ipv6Mode {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let value = match self {
            Self::Disabled => "disabled",
            Self::RouterAdvertised => "router-advertised",
            Self::Delegated => "delegated",
        };
        f.write_str(value)
    }
}

pub fn parse_ip(value: &str) -> Result<IpAddr, std::net::AddrParseError> {
    value.parse()
}

fn default_true() -> bool {
    true
}

fn default_ipv4_families() -> Vec<AddressFamily> {
    vec![AddressFamily::Ipv4]
}