aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authortv <tv@krebsco.de>2026-11-17 12:25:13 +0000
committertv <tv@krebsco.de>2026-11-17 13:57:19 +0100
commit3edf37e157a51278f63037a2420c5afe975bf922 (patch)
tree33301a2b6b38be555673c2a3046dcf83bae9a786 /src
parent3d6197103820e38864e721b8850d502bacdb2dac (diff)
registry: kartei's identifiers at places of their own, and the rules for krebs
`federation::registry` spells what kartei's consumers read as one for all as places: a host's name in `.r` (and in `.i` where it is in `internet`), a user's, the names a host goes by, and the addresses, blocks and subnets of every net but `internet`, a `Subnet` in `tinc.extra` too (`name`, `address`, `asks`). The rules for krebs (`rules`) give a party its namespace, its names in `.r` and `.w`, and its user by its name, and each place kartei gives it beyond them by a rule of its own, made from kartei; what two parties ask for, or the rules give another, is named and given by no rule.
Diffstat (limited to 'src')
-rw-r--r--src/federation/mod.rs5
-rw-r--r--src/federation/registry.rs514
2 files changed, 517 insertions, 2 deletions
diff --git a/src/federation/mod.rs b/src/federation/mod.rs
index f7853e7..2f2efa9 100644
--- a/src/federation/mod.rs
+++ b/src/federation/mod.rs
@@ -1,7 +1,7 @@
//! A federation of parties that don't trust each other (`docs/federation.md`), on a store
//! of events generic over what they read as (`docs/store.md`): what a node of a swarm
-//! holding one reads from its document (`held`), forks made and checked (`fork`), and a
-//! party's client (`client`, `party`).
+//! holding one reads from its document (`held`), forks made and checked (`fork`), a
+//! party's client (`client`, `party`), and the krebs registry on it (`registry`).
//! Built stage by stage behind the `federation` feature.
// Built stage by stage: parts of it are used by nothing yet, or by tests alone.
@@ -23,6 +23,7 @@ mod model;
pub(crate) mod party;
#[cfg(test)]
mod properties;
+pub(crate) mod registry;
pub(crate) mod store;
#[cfg(test)]
mod vectors;
diff --git a/src/federation/registry.rs b/src/federation/registry.rs
new file mode 100644
index 0000000..9daf2e8
--- /dev/null
+++ b/src/federation/registry.rs
@@ -0,0 +1,514 @@
+//! The krebs registry on a federation (`docs/federation.md`, The registry on it): kartei's
+//! files ([krebs/kartei](https://github.com/krebs/kartei)), a namespace each, in its
+//! party's namespace, a place each file; what kartei's consumers read as one for all, at
+//! places of its own (`name`, `address`, `asks`); and the rules for krebs, made from
+//! kartei (`rules`).
+
+use std::collections::{BTreeMap, BTreeSet};
+use std::net::{Ipv4Addr, Ipv6Addr};
+
+use super::jcs::Json;
+use super::version::{self, Version};
+
+/// Files by their paths, `/` between segments, and their text.
+pub(crate) type Files = BTreeMap<String, String>;
+
+/// The rules each party has by its name: its namespace, its names in `.r` and `.w` and
+/// those under them, and its user.
+pub(crate) const BY_NAME: &[&str] = &[
+ "home/{party}/**",
+ "names/r/{party}",
+ "names/r/{party}/**",
+ "names/w/{party}",
+ "names/w/{party}/**",
+ "users/{party}",
+];
+
+/// The place of a name, at its labels, last first: `names/r/tv/cgit` for `cgit.tv.r`.
+pub(crate) fn name(name: &str) -> Option<String> {
+ let labels: Vec<&str> = name.split('.').rev().collect();
+ if labels
+ .iter()
+ .any(|label| label.is_empty() || label.contains('/'))
+ {
+ return None;
+ }
+ Some(format!("names/{}", labels.join("/")))
+}
+
+/// The place of an address or a block, by its parts: `ip4/10/243/7/3`, `ip6/42/1/a46e` for
+/// `42:1:a46e::/48`. A block spells one only on the bounds of its parts, with no bits set
+/// beyond its length; a tinc subnet's weight (`#10`) changes nothing.
+pub(crate) fn address(text: &str) -> Option<String> {
+ let text = match text.split_once('#') {
+ Some((text, weight))
+ if !weight.is_empty() && weight.bytes().all(|byte| byte.is_ascii_digit()) =>
+ {
+ text
+ }
+ Some(_) => return None,
+ None => text,
+ };
+ let (address, length) = match text.split_once('/') {
+ Some((address, length)) => (address, Some(length.parse::<usize>().ok()?)),
+ None => (text, None),
+ };
+ let (kind, parts, bits) = if let Ok(ip) = address.parse::<Ipv4Addr>() {
+ let parts: Vec<String> = ip.octets().iter().map(u8::to_string).collect();
+ ("ip4", parts, 8)
+ } else if let Ok(ip) = address.parse::<Ipv6Addr>() {
+ let parts: Vec<String> = ip
+ .segments()
+ .iter()
+ .map(|part| format!("{part:x}"))
+ .collect();
+ ("ip6", parts, 16)
+ } else {
+ return None;
+ };
+ let length = length.unwrap_or(parts.len() * bits);
+ if length == 0 || length > parts.len() * bits || length % bits != 0 {
+ return None;
+ }
+ let (kept, beyond) = parts.split_at(length / bits);
+ if beyond.iter().any(|part| part != "0") {
+ return None;
+ }
+ Some(format!("{kind}/{}", kept.join("/")))
+}
+
+/// What a line of a file asks of its party.
+#[derive(Debug, PartialEq, Eq)]
+pub(crate) enum Line<'a> {
+ /// Nothing.
+ Free(&'a str),
+ /// That it holds the place of the line's name, address or block.
+ Holds(&'a str, String),
+ /// A name, an address or a block that spells no place.
+ Unspelled,
+}
+
+/// What a file of a namespace asks of its party: the places it must hold for the file to
+/// be read at all, and, for a file of names, addresses or blocks, what each line asks.
+#[derive(Debug, PartialEq, Eq)]
+pub(crate) struct Asks<'a> {
+ pub(crate) places: Vec<String>,
+ pub(crate) lines: Option<Vec<Line<'a>>>,
+}
+
+impl Asks<'_> {
+ /// Every place it asks for, its own and its lines'.
+ fn every(&self) -> impl Iterator<Item = &str> {
+ let lines = self.lines.iter().flatten().filter_map(|line| match line {
+ Line::Holds(_, place) => Some(place.as_str()),
+ _ => None,
+ });
+ self.places.iter().map(String::as_str).chain(lines)
+ }
+}
+
+/// What the file at `path` of a namespace, holding `text`, asks of its party. A host
+/// (`hosts/{host}/…`) asks for its name in `.r`, and in `.i` where it is in the net
+/// `internet`; a user (`users/{user}/…`) for its name; a net's `aliases` for each name
+/// a line holds; in each net but `internet`, its addresses, blocks and subnets for each a
+/// line holds, and its `tinc.extra` for each `Subnet` a line sets. Any other file asks
+/// nothing.
+pub(crate) fn asks<'a>(path: &str, text: &'a str) -> Asks<'a> {
+ let segments: Vec<&str> = path.split('/').collect();
+ match segments.as_slice() {
+ ["hosts", host, rest @ ..] => {
+ let mut places = vec![format!("names/r/{host}")];
+ if rest.first() == Some(&"internet") {
+ places.push(format!("names/i/{host}"));
+ }
+ let lines = match rest {
+ [_, "aliases"] => Some(each(text, name)),
+ ["internet", _] => None,
+ [
+ _,
+ "ip4" | "ip6" | "addrs" | "tinc.subnets" | "wireguard.subnets",
+ ] => Some(each(text, address)),
+ [_, "tinc.extra"] => Some(
+ text.lines()
+ .map(|line| match subnet(line) {
+ Some(value) => spelled(line, address(value)),
+ None => Line::Free(line),
+ })
+ .collect(),
+ ),
+ _ => None,
+ };
+ Asks { places, lines }
+ }
+ ["users", user, ..] => Asks {
+ places: vec![format!("users/{user}")],
+ lines: None,
+ },
+ _ => Asks {
+ places: Vec::new(),
+ lines: None,
+ },
+ }
+}
+
+/// Each line of `text`, but blank ones, asking for the place `spell` gives what it holds.
+fn each(text: &str, spell: impl Fn(&str) -> Option<String>) -> Vec<Line<'_>> {
+ text.lines()
+ .map(|line| match line.trim() {
+ "" => Line::Free(line),
+ held => spelled(line, spell(held)),
+ })
+ .collect()
+}
+
+fn spelled(line: &str, place: Option<String>) -> Line<'_> {
+ match place {
+ Some(place) => Line::Holds(line, place),
+ None => Line::Unspelled,
+ }
+}
+
+/// The value a line of tinc's configuration sets `Subnet` to, where it sets it: its name
+/// in any case, then spaces or `=`.
+fn subnet(line: &str) -> Option<&str> {
+ let line = line.trim_start();
+ let end = line
+ .find(|c: char| c.is_whitespace() || c == '=')
+ .unwrap_or(line.len());
+ let (name, value) = line.split_at(end);
+ name.eq_ignore_ascii_case("subnet")
+ .then(|| value.trim_start().trim_start_matches('=').trim())
+}
+
+/// The rules for krebs, made from kartei.
+#[derive(Debug, PartialEq, Eq)]
+pub(crate) struct Krebs {
+ /// Each place kartei gives a party that neither the version's own rules nor those by
+ /// name give, with the party: a rule each.
+ pub(crate) given: Vec<(String, String)>,
+ /// Each place kartei gives a party that the rules don't give it: asked for by two
+ /// parties, given to another, or spelling no rule; with who asks, and who writes
+ /// there.
+ pub(crate) not_given: Vec<String>,
+}
+
+/// The rules for krebs, made from kartei's files `kartei` (`{namespace}/…`, from its
+/// checkout) for the version `version`: a namespace named as a party of it is that
+/// party's, any other `group`'s. Each place a namespace's files ask for that neither the
+/// version's own rules nor those by name give is given to its party by a rule of its own,
+/// unless another party asks for it too.
+pub(crate) fn rules(
+ kartei: &Files,
+ version: &Version,
+ group: Option<&str>,
+) -> Result<Krebs, String> {
+ if let Some(group) = group.filter(|group| !version.parties.contains_key(*group)) {
+ return Err(format!("{group} is no party of the version"));
+ }
+ let by_name = Version::read(&with_rules(version, &[]))?;
+ let mut asked: BTreeMap<String, BTreeSet<&str>> = BTreeMap::new();
+ let mut of_nobody = BTreeSet::new();
+ for (path, text) in kartei {
+ let Some((namespace, path)) = path.split_once('/') else {
+ continue;
+ };
+ // kartei reads no namespace of its template.
+ if namespace == "template" {
+ continue;
+ }
+ let file = asks(path, text);
+ if file.every().next().is_none() {
+ continue;
+ }
+ let party = match (version.parties.get_key_value(namespace), group) {
+ (Some((party, _)), _) => party.as_str(),
+ (None, Some(group)) => group,
+ (None, None) => {
+ of_nobody.insert(namespace);
+ continue;
+ }
+ };
+ for place in file.every() {
+ asked.entry(place.to_owned()).or_default().insert(party);
+ }
+ }
+ if !of_nobody.is_empty() {
+ let namespaces: Vec<&str> = of_nobody.into_iter().collect();
+ return Err(format!(
+ "namespaces of no party, and no group: {}",
+ namespaces.join(", ")
+ ));
+ }
+ let mut krebs = Krebs {
+ given: Vec::new(),
+ not_given: Vec::new(),
+ };
+ for (place, parties) in asked {
+ let segments = version::path(&place).filter(|segments| {
+ segments
+ .iter()
+ .all(|segment| !matches!(segment.as_str(), "{party}" | "*" | "**"))
+ });
+ let writer = segments
+ .as_ref()
+ .and_then(|segments| by_name.writer(segments));
+ let asking: Vec<&str> = parties.iter().copied().collect();
+ match (asking.as_slice(), writer, &segments) {
+ ([party], Some(writer), _) if *party == writer => {}
+ ([party], None, Some(_)) => krebs.given.push((place, (*party).to_owned())),
+ _ => krebs.not_given.push(format!(
+ "{place}: asked for by {}; {} writes there",
+ asking.join(", "),
+ writer.unwrap_or("nobody")
+ )),
+ }
+ }
+ Ok(krebs)
+}
+
+/// The version `version` with its own rules, then the rules `given`, each a place and the
+/// party writing there, then those by name.
+pub(crate) fn with_rules(version: &Version, given: &[(String, String)]) -> Json {
+ let rule = |path: &str, writer: &str| {
+ Json::object(vec![
+ ("path".to_owned(), Json::Str(path.to_owned())),
+ ("writer".to_owned(), Json::Str(writer.to_owned())),
+ ])
+ .expect("names once each")
+ };
+ let own = version
+ .json
+ .get("rules")
+ .and_then(Json::as_array)
+ .unwrap_or_default();
+ let rules: Vec<Json> = own
+ .iter()
+ .cloned()
+ .chain(
+ given
+ .iter()
+ .map(|(path, party)| rule(path.as_str(), party.as_str())),
+ )
+ .chain(BY_NAME.iter().map(|path| rule(*path, "{party}")))
+ .collect();
+ match &version.json {
+ Json::Object(members) => Json::Object(
+ members
+ .iter()
+ .map(|(name, value)| match name.as_str() {
+ "rules" => (name.clone(), Json::Array(rules.clone())),
+ _ => (name.clone(), value.clone()),
+ })
+ .collect(),
+ ),
+ other => other.clone(),
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crate::federation::harness::{Party, Spec};
+
+ fn files(entries: &[(&str, &str)]) -> Files {
+ entries
+ .iter()
+ .map(|(path, text)| ((*path).to_owned(), (*text).to_owned()))
+ .collect()
+ }
+
+ /// A kartei of three namespaces, tv's and lass's, who are parties, and berber's, who
+ /// isn't, and the files of kartei's own.
+ fn kartei() -> Files {
+ files(&[
+ ("tv/hosts/ne/retiolum/aliases", "ne.r\ncgit.ne.r\nne.tv.r\n"),
+ ("tv/hosts/ne/retiolum/ip4", "10.243.0.5\n"),
+ ("tv/hosts/ne/retiolum/ip6", "42:0:a46e::1\n"),
+ ("tv/hosts/ne/retiolum/ed25519.key", "AAAA\n"),
+ (
+ "tv/hosts/ne/wiregrill/wireguard.subnets",
+ "42:1:a46e::/48\n",
+ ),
+ ("tv/hosts/ne/internet/aliases", "ne.i\n"),
+ ("tv/hosts/ne/internet/ip4", "203.0.113.5\n"),
+ ("tv/users/tv/mail", "tv@example.org\n"),
+ ("tv/users/vv/mail", "vv@example.org\n"),
+ ("tv/ssh/tv.pub", "ssh-ed25519 AAAA tv\n"),
+ ("lass/hosts/yellow/retiolum/aliases", "yellow.r\nwiki.r\n"),
+ ("lass/hosts/yellow/retiolum/ip4", "10.243.0.6\n"),
+ (
+ "lass/hosts/yellow/retiolum/tinc.extra",
+ "Subnet = 10.243.6.0/24\nSPTPSCipher = aes-256-gcm\n",
+ ),
+ (
+ "berber/hosts/schlepptop/retiolum/aliases",
+ "schlepptop.berber.r\n",
+ ),
+ ("berber/users/berber/mail", "berber@example.org\n"),
+ ("template/hosts/DUMMYHOST/retiolum/aliases", "DUMMYHOST.r\n"),
+ ("lib/default.nix", "{ }\n"),
+ ])
+ }
+
+ /// tv and lass, and krebs, the group holding the others' namespaces: the first
+ /// version, with no rules of its own.
+ fn parties() -> (Party, Party, Party, Spec) {
+ let (tv, lass, krebs) = (
+ Party::new("tv", 1),
+ Party::new("lass", 2),
+ Party::new("krebs", 3),
+ );
+ let mut first = Spec::first(&[&tv, &lass, &krebs], &[]);
+ first.rules.clear();
+ (tv, lass, krebs, first)
+ }
+
+ fn version(spec: &Spec) -> Version {
+ Version::read(&spec.json()).expect("a version")
+ }
+
+ fn given(given: &[(&str, &str)]) -> Vec<(String, String)> {
+ given
+ .iter()
+ .map(|(place, party)| ((*place).to_owned(), (*party).to_owned()))
+ .collect()
+ }
+
+ #[test]
+ fn names_and_addresses_are_spelled_by_their_parts() {
+ assert_eq!(name("cgit.tv.r").as_deref(), Some("names/r/tv/cgit"));
+ assert_eq!(name("tv.r").as_deref(), Some("names/r/tv"));
+ assert_eq!(name("a..r"), None);
+ assert_eq!(name("a/b.r"), None);
+ assert_eq!(address("10.243.7.3").as_deref(), Some("ip4/10/243/7/3"));
+ assert_eq!(
+ address("10.244.245.0/24").as_deref(),
+ Some("ip4/10/244/245")
+ );
+ assert_eq!(address("10.243.0.1#10").as_deref(), Some("ip4/10/243/0/1"));
+ assert_eq!(
+ address("42:0:a46e::1").as_deref(),
+ Some("ip6/42/0/a46e/0/0/0/0/1")
+ );
+ assert_eq!(address("42:1:a46e::/48").as_deref(), Some("ip6/42/1/a46e"));
+ assert_eq!(
+ address("42:0000:002b:1605:3::/80").as_deref(),
+ Some("ip6/42/0/2b/1605/3")
+ );
+ // Off the bounds of its parts, bits set beyond its length, or no address.
+ assert_eq!(address("10.243.0.0/20"), None);
+ assert_eq!(address("10.244.245.1/24"), None);
+ assert_eq!(address("10.243.0.1#x"), None);
+ assert_eq!(address("bernie.thalheim.io"), None);
+ }
+
+ #[test]
+ fn a_file_asks_for_the_names_and_addresses_it_holds() {
+ let asks_for = |path: &str, text: &str| -> Vec<String> {
+ asks(path, text).every().map(str::to_owned).collect()
+ };
+ assert_eq!(
+ asks_for("hosts/ne/retiolum/aliases", "ne.r\n\ncgit.ne.r\n"),
+ ["names/r/ne", "names/r/ne", "names/r/ne/cgit"]
+ );
+ assert_eq!(
+ asks_for("hosts/ne/internet/ip4", "203.0.113.5\n"),
+ ["names/r/ne", "names/i/ne"]
+ );
+ assert_eq!(
+ asks_for(
+ "hosts/ne/retiolum/tinc.extra",
+ "subnet=10.243.6.0/24\nPort = 655\n"
+ ),
+ ["names/r/ne", "ip4/10/243/6"]
+ );
+ assert_eq!(asks_for("users/vv/mail", "vv@example.org\n"), ["users/vv"]);
+ assert!(asks_for("ssh/tv.pub", "ssh-ed25519 AAAA tv\n").is_empty());
+ assert_eq!(
+ asks("hosts/ne/retiolum/ip4", "10.243.0.0/20\n").lines,
+ Some(vec![Line::Unspelled])
+ );
+ }
+
+ #[test]
+ fn krebs_rules_give_each_party_what_kartei_gives_it() {
+ let (_, _, _, first) = parties();
+ let krebs = rules(&kartei(), &version(&first), Some("krebs")).expect("rules");
+ assert_eq!(
+ krebs,
+ Krebs {
+ given: given(&[
+ ("ip4/10/243/0/5", "tv"),
+ ("ip4/10/243/0/6", "lass"),
+ ("ip4/10/243/6", "lass"),
+ ("ip6/42/0/a46e/0/0/0/0/1", "tv"),
+ ("ip6/42/1/a46e", "tv"),
+ ("names/i/ne", "tv"),
+ ("names/r/berber/schlepptop", "krebs"),
+ ("names/r/ne", "tv"),
+ ("names/r/ne/cgit", "tv"),
+ ("names/r/schlepptop", "krebs"),
+ ("names/r/wiki", "lass"),
+ ("names/r/yellow", "lass"),
+ ("users/berber", "krebs"),
+ ("users/vv", "tv"),
+ ]),
+ not_given: Vec::new(),
+ }
+ );
+ // The version with them: its own rules, those given, those by name.
+ let with = Version::read(&with_rules(&version(&first), &krebs.given)).expect("one");
+ let writer = |place: &str| with.writer(&version::path(place).expect("a path"));
+ assert_eq!(writer("names/r/tv/ne"), Some("tv"));
+ assert_eq!(writer("names/r/schlepptop"), Some("krebs"));
+ assert_eq!(writer("home/lass/hosts/yellow/retiolum/ip4"), Some("lass"));
+ assert_eq!(writer("names/r/toaster"), None);
+ }
+
+ #[test]
+ fn what_two_parties_ask_for_nobody_is_given() {
+ let (_, _, _, first) = parties();
+ let mut kartei = kartei();
+ kartei.insert(
+ "lass/hosts/yellow/retiolum/aliases".to_owned(),
+ "yellow.r\nwiki.r\ncgit.ne.r\n".to_owned(),
+ );
+ // A host named as a party is that party's name.
+ kartei.insert(
+ "berber/hosts/tv/retiolum/ip4".to_owned(),
+ "10.243.0.7\n".to_owned(),
+ );
+ let krebs = rules(&kartei, &version(&first), Some("krebs")).expect("rules");
+ assert!(
+ !krebs
+ .given
+ .iter()
+ .any(|(place, _)| place == "names/r/ne/cgit")
+ );
+ assert_eq!(
+ krebs.not_given,
+ [
+ "names/r/ne/cgit: asked for by lass, tv; nobody writes there",
+ "names/r/tv: asked for by krebs; tv writes there",
+ ]
+ );
+ assert!(
+ krebs
+ .given
+ .contains(&("ip4/10/243/0/7".to_owned(), "krebs".to_owned()))
+ );
+ }
+
+ #[test]
+ fn a_namespace_of_no_party_needs_a_group() {
+ let (_, _, _, first) = parties();
+ assert_eq!(
+ rules(&kartei(), &version(&first), None),
+ Err("namespaces of no party, and no group: berber".to_owned())
+ );
+ assert_eq!(
+ rules(&kartei(), &version(&first), Some("berber")),
+ Err("berber is no party of the version".to_owned())
+ );
+ }
+}