Day 2: roads, the compiler, explain, CBOR control messages, the via command #1

Merged
mnovak merged 25 commits from feature/roads into main 2026-11-26 05:24:39 +01:00
11 changed files with 141 additions and 32 deletions
Showing only changes of commit acb98a6a1c - Show all commits
+4
View File
@@ -56,10 +56,12 @@ pub const DIRECTORY_ID: &str = "directory.id";
pub const DIRECTORY_DUPLICATE: &str = "directory.duplicate";
pub const DIRECTORY_NAME: &str = "directory.name";
pub const DIRECTORY_MEMBER: &str = "directory.member";
pub const DIRECTORY_MEMBER_UNKNOWN: &str = "directory.member-unknown";
pub const DEVICES_KEY: &str = "devices.key";
pub const DEVICES_DUPLICATE: &str = "devices.duplicate";
pub const DEVICES_NAME: &str = "devices.name";
pub const DEVICES_OWNER: &str = "devices.owner";
pub const DEVICES_OWNER_UNKNOWN: &str = "devices.owner-unknown";
pub const DEVICES_ADDRESS: &str = "devices.address";
pub const DEVICES_ROUTE: &str = "devices.route";
pub const DEVICES_OVERLAP: &str = "devices.overlap";
@@ -117,10 +119,12 @@ pub const ALL: &[&str] = &[
DIRECTORY_DUPLICATE,
DIRECTORY_NAME,
DIRECTORY_MEMBER,
DIRECTORY_MEMBER_UNKNOWN,
DEVICES_KEY,
DEVICES_DUPLICATE,
DEVICES_NAME,
DEVICES_OWNER,
DEVICES_OWNER_UNKNOWN,
DEVICES_ADDRESS,
DEVICES_ROUTE,
DEVICES_OVERLAP,
+55 -23
View File
@@ -1,5 +1,11 @@
//! The two snapshots the compiler reads besides the roads: `directory.json` and `devices.json`
//! (docs/FORMATS.md). Both are strict: unknown fields are errors, and any error means no plan is produced.
//! (docs/FORMATS.md). Both are strict: unknown fields are errors, and a *structural* error (bad JSON, a wrong
//! format, a malformed or duplicate id, key or address) means no plan is produced.
//!
//! A *dangling reference* is different (QA F-04): a group member or a device owner that is a well-formed id
//! but no longer a person in the directory. That is what deleting a person upstream leaves behind. It is
//! still an error, and reported, but it only removes that reference: the member leaves the group, the device
//! becomes ownerless (reached only by `device:` roads). Everybody else keeps their plans.
use crate::ids::{
DeviceKey, GroupId, Hostname, PersonId, clip, is_device_key, is_hostname, is_opaque_id,
@@ -173,11 +179,17 @@ pub fn is_hidden_char(c: char) -> bool {
|| matches!(c, '\u{2028}' | '\u{2029}' | '\u{200b}'..='\u{200f}' | '\u{202a}'..='\u{202e}' | '\u{2060}'..='\u{2069}' | '\u{feff}')
}
/// A parsed snapshot and the dangling references dropped from it (errors that do not stop plans).
pub type Parsed<T> = Result<(T, Vec<Problem>), Vec<Problem>>;
impl Directory {
pub fn parse(bytes: &[u8]) -> Result<Directory, Vec<Problem>> {
/// `Err`: a structural error, no directory. `Ok((dir, dangling))`: `dangling` lists the members left
/// out because they are not people in the directory.
pub fn parse(bytes: &[u8]) -> Parsed<Directory> {
let path = DIRECTORY_FILE;
let raw: RawDirectory = read_json(path, bytes)?;
let mut problems = Vec::new();
let mut dangling = Vec::new();
let mut e = |field: String, code, msg: String| {
problems.push(Problem::error(path, Some(&field), code, msg))
};
@@ -247,8 +259,8 @@ impl Directory {
}
let mut members = BTreeSet::new();
for (j, m) in g.members.iter().enumerate() {
match PersonId::new(m).filter(|m| dir.people.contains_key(m)) {
Some(m) => {
match PersonId::new(m) {
Some(m) if dir.people.contains_key(&m) => {
if !members.insert(m.clone()) {
e(
format!("groups[{i}].members[{j}]"),
@@ -257,10 +269,21 @@ impl Directory {
);
}
}
Some(m) => dangling.push(Problem::error(
path,
Some(&format!("groups[{i}].members[{j}]")),
r::DIRECTORY_MEMBER_UNKNOWN,
format!(
"`{m}` is not a person in the directory (groups are flat: members are people); left out of the group"
),
)),
None => e(
format!("groups[{i}].members[{j}]"),
r::DIRECTORY_MEMBER,
format!("`{}` is not a person in the directory", clip(m)),
format!(
"`{}` is not a person id: 1-64 of A-Z a-z 0-9 _ -",
clip(m)
),
),
}
}
@@ -284,7 +307,7 @@ impl Directory {
}
}
if problems.is_empty() {
Ok(dir)
Ok((dir, dangling))
} else {
Err(problems)
}
@@ -292,10 +315,13 @@ impl Directory {
}
impl Devices {
pub fn parse(bytes: &[u8], directory: Option<&Directory>) -> Result<Devices, Vec<Problem>> {
/// `Err`: a structural error, no device list. `Ok((devices, dangling))`: `dangling` lists the owners
/// dropped because they are not people in `directory`; those devices are ownerless.
pub fn parse(bytes: &[u8], directory: Option<&Directory>) -> Parsed<Devices> {
let path = DEVICES_FILE;
let raw: RawDevices = read_json(path, bytes)?;
let mut problems = Vec::new();
let mut dangling = Vec::new();
let mut e = |field: String, code, msg: String| {
problems.push(Problem::error(path, Some(&field), code, msg))
};
@@ -345,22 +371,28 @@ impl Devices {
}
let owner = match &d.owner {
None => None,
Some(o) => {
let known = directory.is_none_or(|dir| {
PersonId::new(o).is_some_and(|p| dir.people.contains_key(&p))
});
match PersonId::new(o).filter(|_| is_opaque_id(o) && known) {
Some(p) => Some(p),
None => {
e(
f("owner"),
r::DEVICES_OWNER,
format!("`{}` is not a person in the directory", clip(o)),
);
None
}
Some(o) => match PersonId::new(o).filter(|_| is_opaque_id(o)) {
Some(p) if directory.is_none_or(|dir| dir.people.contains_key(&p)) => Some(p),
Some(p) => {
dangling.push(Problem::error(
path,
Some(&f("owner")),
r::DEVICES_OWNER_UNKNOWN,
format!(
"`{p}` is not a person in the directory; the device is ownerless until it gets an owner, and only `device:` roads reach it"
),
));
None
}
}
None => {
e(
f("owner"),
r::DEVICES_OWNER,
format!("`{}` is not a person id: 1-64 of A-Z a-z 0-9 _ -", clip(o)),
);
None
}
},
};
let address = match d.address.parse::<std::net::IpAddr>() {
Ok(a)
@@ -506,7 +538,7 @@ impl Devices {
}
}
if problems.is_empty() {
Ok(out)
Ok((out, dangling))
} else {
Err(problems)
}
+8 -2
View File
@@ -36,7 +36,10 @@ impl Store {
"missing",
)),
Some(b) => match Directory::parse(b) {
Ok(d) => store.directory = Some(d),
Ok((d, dangling)) => {
store.directory = Some(d);
store.problems.extend(dangling);
}
Err(p) => store.problems.extend(p),
},
}
@@ -48,7 +51,10 @@ impl Store {
"missing",
)),
Some(b) => match Devices::parse(b, store.directory.as_ref()) {
Ok(d) => store.devices = Some(d),
Ok((d, dangling)) => {
store.devices = Some(d);
store.problems.extend(dangling);
}
Err(p) => store.problems.extend(p),
},
}
+6 -3
View File
@@ -52,13 +52,16 @@ directory bad-person-id.directory.json directory.id people[3].id
directory duplicate-person.directory.json directory.duplicate people[3].id
directory name-newline.directory.json directory.name people[3].name
directory name-bidi.directory.json directory.name people[3].name
directory member-unknown.directory.json directory.member groups[0].members[1]
directory member-group.directory.json directory.member groups[0].members[1]
directory member-unknown.directory.json directory.member-unknown groups[0].members[1]
directory member-group.directory.json directory.member-unknown groups[0].members[1]
directory member-bad-id.directory.json directory.member groups[0].members[1]
directory member-twice.directory.json directory.member groups[0].members[1]
devices unknown-field.devices.json snapshot.json -
devices bad-key.devices.json devices.key devices[0].key
devices duplicate-key.devices.json devices.duplicate devices[6].key
devices name-newline.devices.json devices.name devices[0].name
devices owner-unknown.devices.json devices.owner devices[6].owner
devices owner-unknown.devices.json devices.owner-unknown devices[6].owner
devices owner-bad-id.devices.json devices.owner devices[6].owner
devices address-twice.devices.json devices.address devices[6].address
devices address-in-route.devices.json devices.address devices.petr-phone.address
devices address-loopback.devices.json devices.address devices[6].address
@@ -0,0 +1,12 @@
{
"format": 1,
"people": [
{ "id": "mark", "name": "Mark" },
{ "id": "jana", "name": "Jana" },
{ "id": "petr", "name": "Petr" },
{ "id": "eva", "name": "Eva" }
],
"groups": [
{ "id": "household", "name": "Household", "members": ["mark", "no body"] }
]
}
@@ -0,0 +1,12 @@
{
"format": 1,
"people": [
{ "id": "mark", "name": "Mark" },
{ "id": "jana", "name": "Jana" },
{ "id": "petr", "name": "Petr" },
{ "id": "eva", "name": "Eva" }
],
"groups": [
{ "id": "household", "name": "Household", "members": ["mark", "mark"] }
]
}
@@ -0,0 +1,17 @@
{
"format": 1,
"devices": [
{ "key": "AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQE", "name": "mark-pc", "owner": "mark", "address": "100.96.0.1" },
{ "key": "AgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgI", "name": "home-server", "owner": "mark", "address": "100.96.0.2", "exit": true, "relay": true,
"routes": ["192.168.100.0/24"],
"services": [
{ "host": "photos.home.arpa", "ports": ["tcp/443"] },
{ "host": "git.home.arpa", "ports": ["tcp/443", "tcp/22"] }
] },
{ "key": "AwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwM", "name": "jana-phone", "owner": "jana", "address": "100.96.0.3" },
{ "key": "BAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQ", "name": "jana-laptop", "owner": "jana", "address": "100.96.0.4" },
{ "key": "BQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQUFBQU", "name": "router", "address": "100.96.0.5", "classic": true },
{ "key": "BgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgYGBgY", "name": "friend-node", "owner": "petr", "address": "100.96.0.6", "exit": true },
{ "key": "BwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwcHBwc", "name": "petr-phone", "owner": "no body", "address": "100.96.0.7" }
]
}
-1
View File
@@ -119,7 +119,6 @@ fn f03_a_kill_switch_whose_exit_goes_away_fails_closed() {
/// a dangling `owner`, which is a snapshot error, which means no plan for anybody. Day 3 then has to choose
/// between the household offline and the deleted person keeping their access.
#[test]
#[ignore = "QA F-04 (major): one dangling owner means no plans for anybody"]
fn f04_deleting_a_person_revokes_them_and_nobody_else() {
let parts =
Parts::home().edit_directory(|d| d.replace(r#" { "id": "petr", "name": "Petr" },"#, ""));
+2 -1
View File
@@ -182,7 +182,8 @@ fn removing_a_person_from_a_group_removes_the_group_road_only() {
#[test]
fn a_person_deleted_from_the_directory_leaves_their_roads_reported_and_void() {
// QA R-20: the road naming petr is an error, and the device that petr owned fails the snapshot.
// QA R-20: the road naming petr is an error. The device petr owned is edited to have no owner here; QA
// F-04 (`qa_findings.rs`) covers the upstream-only edit, where the dangling owner leaves it ownerless.
let parts = Parts::home()
.edit_directory(|d| d.replace(r#" { "id": "petr", "name": "Petr" },"#, ""))
.edit_devices(|d| d.replace(r#""owner": "petr", "#, ""));
+16
View File
@@ -46,6 +46,15 @@ fn inactive_code(code: &str) -> bool {
.contains(&code)
}
/// Snapshot errors that are a dangling reference, not a structural fault: reported, but plans are produced.
fn dangling_code(code: &str) -> bool {
[
rules::DEVICES_OWNER_UNKNOWN,
rules::DIRECTORY_MEMBER_UNKNOWN,
]
.contains(&code)
}
/// Runs one case; returns the codes it produced.
fn run(case: &Case, baseline: &[u8]) -> BTreeSet<String> {
let bytes = std::fs::read(
@@ -122,6 +131,13 @@ fn run(case: &Case, baseline: &[u8]) -> BTreeSet<String> {
"{}: a road with an error changed a plan",
case.file
);
} else if dangling_code(&case.code) {
// QA F-04: a dangling reference drops only itself; everybody else keeps a plan.
assert!(
c.has_plans(),
"{}: one dangling reference stopped every plan",
case.file
);
} else {
assert!(
c.plans.is_empty(),
+9 -2
View File
@@ -144,10 +144,17 @@ Valid roads that grant nothing right now, reported with their reason (not errors
| `people[].name` | 1–100 characters, no leading or trailing space, and none of: control characters (CR, LF, U+0085 included), U+2028, U+2029, zero-width characters U+200B–U+200F and U+2060–U+2069, bidi controls U+202A–U+202E, U+FEFF (`directory.name`) — so a name can never carry a line into an exported config |
| `groups[].id` | a group id, unique among groups |
| `groups[].name` | as a person's name |
| `groups[].members` | person ids that exist, no repeats (`directory.member`) |
| `groups[].members` | person ids, no repeats (`directory.member`); a well-formed id that is not a person in `people` is left out of the group (`directory.member-unknown`) |
Unknown fields, bad JSON and a missing file are `snapshot.json`.
**Structural errors and dangling references (QA F-04).** Any error in either snapshot means no plan at all,
with two exceptions: a group member and a device owner that are well-formed ids but not people in the
directory. That is what deleting a person upstream leaves behind, before anybody edits `devices.json`. Each
is still an error and is reported, but it removes only itself: the member leaves the group, and the device
becomes ownerless, so only `device:` roads reach it. Everybody else keeps their plans. "No plans" is kept for
what makes the snapshot unreadable: bad JSON, a wrong format, a malformed or duplicate id, key or address.
## `devices.json` — the device list
```json
@@ -167,7 +174,7 @@ Unknown fields, bad JSON and a missing file are `snapshot.json`.
| `format` | — | `1` |
| `key` | — | a device key (`devices.key`), unique (`devices.duplicate`) |
| `name` | — | 1–64 characters of `[a-z0-9-]`, unique (`devices.name`) |
| `owner` | none | a person id in the directory (`devices.owner`); a device with no owner is reached only by `device:` roads |
| `owner` | none | a person id (`devices.owner`). One that is not in the directory leaves the device ownerless (`devices.owner-unknown`, reported). A device with no owner is reached only by `device:` roads |
| `address` | — | the device's mesh address, one IPv4 or IPv6 address, unique, not inside any device's routes, not loopback, link-local, multicast or IPv4-embedding IPv6 (`devices.address`) |
| `classic` | `false` | a classic WireGuard peer, **not post-quantum** |
| `exit` | `false` | offers itself as an exit |