[roads] The road model, validator, compiler and explain; proto's versioned CBOR messages; the via command
Road files in Pensum's strict header subset (unknown keys are errors, random ids), the directory and device snapshots, deny-by-default compilation to one plan per device, explain from the compiler's own grants, CBOR envelope v1 with version-first refusals. Tests: the six mandatory ones (validation fixtures for every rule, revocation, the least-privilege property against an independent oracle, determinism with golden plans, explain, custom round-trip), QA's attack cases, the protocol and the CLI.
This commit is contained in:
@@ -0,0 +1,164 @@
|
||||
//! The compiled plan: what one device needs (docs/FORMATS.md, "The plan"). Field order is the wire order.
|
||||
|
||||
use crate::id::RoadId;
|
||||
use crate::ids::{AppId, DeviceKey, Hostname};
|
||||
use crate::net::{Addr, Net};
|
||||
use crate::ports::Proto;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
pub const FORMAT: u32 = 1;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct Plan {
|
||||
pub format: u32,
|
||||
pub device: DeviceKey,
|
||||
pub name: String,
|
||||
pub address: Addr,
|
||||
/// This device is a classic WireGuard peer: not post-quantum.
|
||||
pub classic: bool,
|
||||
/// The earliest `until` of the roads this plan was built from, UTC: compile again by then.
|
||||
pub expires: Option<String>,
|
||||
pub peers: Vec<Peer>,
|
||||
pub firewall: Firewall,
|
||||
pub dns: Vec<Dns>,
|
||||
pub exit: Option<Exit>,
|
||||
pub kill_switch: Option<KillSwitch>,
|
||||
pub direct: Vec<Direct>,
|
||||
pub custom: Vec<Custom>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct Peer {
|
||||
pub key: DeviceKey,
|
||||
pub name: String,
|
||||
pub address: Addr,
|
||||
/// A classic WireGuard peer: **not post-quantum**.
|
||||
pub classic: bool,
|
||||
pub allowed_ips: Vec<Net>,
|
||||
/// A path hint (a relay or an exit) when every road to this peer names the same one.
|
||||
pub via: Option<DeviceKey>,
|
||||
pub roads: Vec<RoadId>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct Firewall {
|
||||
/// Always `deny`: only the rules below let traffic in or through.
|
||||
pub default: Action,
|
||||
pub rules: Vec<Rule>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "kebab-case")]
|
||||
pub enum Action {
|
||||
Deny,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "kebab-case")]
|
||||
pub enum Direction {
|
||||
/// To this device's own address.
|
||||
In,
|
||||
/// Through this device: to a subnet it routes, or to the internet as an exit.
|
||||
Forward,
|
||||
}
|
||||
|
||||
/// Where a rule lets traffic go.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub enum Dst {
|
||||
Net(Net),
|
||||
/// Every address except the private ranges (`net::NOT_INTERNET`).
|
||||
Internet,
|
||||
}
|
||||
|
||||
impl Serialize for Dst {
|
||||
fn serialize<S: serde::Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
|
||||
match self {
|
||||
Dst::Net(n) => n.serialize(s),
|
||||
Dst::Internet => s.serialize_str("internet"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Deserialize<'de> for Dst {
|
||||
fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
|
||||
let s = String::deserialize(d)?;
|
||||
if s == "internet" {
|
||||
return Ok(Dst::Internet);
|
||||
}
|
||||
Net::parse_canonical(&s)
|
||||
.map(Dst::Net)
|
||||
.map_err(|_| serde::de::Error::custom(format!("`{s}` is not a network or `internet`")))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct Rule {
|
||||
pub direction: Direction,
|
||||
pub src: Net,
|
||||
pub dst: Dst,
|
||||
pub proto: Proto,
|
||||
pub ports: Option<[u16; 2]>,
|
||||
pub roads: Vec<RoadId>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct Dns {
|
||||
pub host: Hostname,
|
||||
pub address: Addr,
|
||||
pub roads: Vec<RoadId>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct Exit {
|
||||
pub through: DeviceKey,
|
||||
pub roads: Vec<RoadId>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct KillSwitch {
|
||||
/// `None` when kill-switch roads disagree: nothing leaves but the mesh (fails closed).
|
||||
pub through: Option<DeviceKey>,
|
||||
pub roads: Vec<RoadId>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct Direct {
|
||||
/// `None`: all traffic to `subnet`; `Some`: only that app's.
|
||||
pub app: Option<AppId>,
|
||||
pub subnet: Net,
|
||||
pub roads: Vec<RoadId>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(deny_unknown_fields)]
|
||||
pub struct Custom {
|
||||
pub road: RoadId,
|
||||
/// The road said this is for a classic peer: not post-quantum.
|
||||
pub classic: bool,
|
||||
/// The raw block, byte for byte. **Never checked by the compiler.**
|
||||
pub raw: String,
|
||||
}
|
||||
|
||||
impl Plan {
|
||||
/// Every allowed IP of every peer, with the peer.
|
||||
pub fn allowed_ips(&self) -> impl Iterator<Item = (&DeviceKey, &Net)> {
|
||||
self.peers
|
||||
.iter()
|
||||
.flat_map(|p| p.allowed_ips.iter().map(move |n| (&p.key, n)))
|
||||
}
|
||||
|
||||
/// Pretty JSON, two spaces, fields in wire order, a final newline: the same input, the same bytes.
|
||||
pub fn to_json(&self) -> String {
|
||||
let mut s = serde_json::to_string_pretty(self).expect("a plan always serialises");
|
||||
s.push('\n');
|
||||
s
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user