aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/federation/registry.rs427
1 files changed, 424 insertions, 3 deletions
diff --git a/src/federation/registry.rs b/src/federation/registry.rs
index 9daf2e8..804ca1e 100644
--- a/src/federation/registry.rs
+++ b/src/federation/registry.rs
@@ -1,11 +1,13 @@
//! 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`).
+//! places of its own (`name`, `address`, `asks`); the rules for krebs, made from kartei
+//! (`rules`); kartei's files read and written (`read`, `write`); the writes signing them
+//! in (`writes`); and what kartei's consumers read, written out from what counts (`out`).
use std::collections::{BTreeMap, BTreeSet};
use std::net::{Ipv4Addr, Ipv6Addr};
+use std::path::Path;
use super::jcs::Json;
use super::version::{self, Version};
@@ -305,10 +307,171 @@ pub(crate) fn with_rules(version: &Version, given: &[(String, String)]) -> Json
}
}
+/// The files under `dir`, by their paths below it, as kartei's are read: what begins with
+/// `.` left out, a link read as the file it names, each file text (UTF-8).
+pub(crate) fn read(dir: &Path) -> Result<Files, String> {
+ let mut files = Files::new();
+ let mut dirs = vec![(dir.to_path_buf(), String::new())];
+ while let Some((dir, at)) = dirs.pop() {
+ let entries = std::fs::read_dir(&dir).map_err(|why| format!("{}: {why}", dir.display()))?;
+ for entry in entries {
+ let entry = entry.map_err(|why| format!("{}: {why}", dir.display()))?;
+ let path = entry.path();
+ let Some(name) = entry.file_name().to_str().map(str::to_owned) else {
+ return Err(format!("{}: a name that isn't UTF-8", path.display()));
+ };
+ if name.starts_with('.') {
+ continue;
+ }
+ let below = if at.is_empty() {
+ name
+ } else {
+ format!("{at}/{name}")
+ };
+ let kind = entry
+ .file_type()
+ .map_err(|why| format!("{}: {why}", path.display()))?;
+ if kind.is_dir() {
+ dirs.push((path, below));
+ } else {
+ let text = std::fs::read_to_string(&path)
+ .map_err(|why| format!("{}: {why}", path.display()))?;
+ files.insert(below, text);
+ }
+ }
+ }
+ Ok(files)
+}
+
+/// Writes `files` under `dir`, which it makes: nothing may be there yet. Each path is a
+/// path below it: no segment empty, nor beginning with `.`.
+pub(crate) fn write(dir: &Path, files: &Files) -> Result<(), String> {
+ if let Some(path) = files.keys().find(|path| {
+ path.split('/')
+ .any(|segment| segment.is_empty() || segment.starts_with('.'))
+ }) {
+ return Err(format!("{path:?} is no path below a directory"));
+ }
+ std::fs::create_dir(dir).map_err(|why| format!("{}: {why}", dir.display()))?;
+ for (path, text) in files {
+ let file = dir.join(path);
+ if let Some(parent) = file.parent() {
+ std::fs::create_dir_all(parent)
+ .map_err(|why| format!("{}: {why}", parent.display()))?;
+ }
+ std::fs::write(&file, text).map_err(|why| format!("{}: {why}", file.display()))?;
+ }
+ Ok(())
+}
+
+/// The writes making `party`'s namespace hold `files`, the files of its kartei namespaces,
+/// where `values` are what counts: each file at its place, where that doesn't hold the
+/// file's text, and `null` at each place of the namespace that no file is at.
+pub(crate) fn writes(
+ party: &str,
+ files: &Files,
+ values: &BTreeMap<String, Json>,
+) -> Vec<(String, Json)> {
+ let home = format!("home/{party}/");
+ let mut writes: Vec<(String, Json)> = files
+ .iter()
+ .map(|(path, text)| (format!("{home}{path}"), Json::Str(text.clone())))
+ .filter(|(place, text)| values.get(place.as_str()) != Some(text))
+ .collect();
+ writes.extend(
+ values
+ .keys()
+ .filter(|place| {
+ place
+ .strip_prefix(home.as_str())
+ .is_some_and(|path| !files.contains_key(path))
+ })
+ .map(|place| (place.clone(), Json::Null)),
+ );
+ writes
+}
+
+/// What kartei's consumers read, written out from what counts, `values`, under the
+/// current version `version`: each party's namespace as its files (`{party}/…`), the text
+/// each place of it holds, but for what asks for a place the version doesn't give the
+/// party: a file whose own places it doesn't hold left out, and each line whose place it
+/// doesn't; a file left with blank lines alone left out too. What isn't text, what is at
+/// a path with a segment beginning with `.`, and what is at the way to another file is no
+/// file.
+pub(crate) fn out(version: &Version, values: &BTreeMap<String, Json>) -> Files {
+ let mut files = Files::new();
+ for (place, value) in values {
+ let Some((party, path)) = place
+ .strip_prefix("home/")
+ .and_then(|rest| rest.split_once('/'))
+ else {
+ continue;
+ };
+ let Some(text) = value.as_str() else {
+ continue;
+ };
+ if std::iter::once(party)
+ .chain(path.split('/'))
+ .any(|segment| segment.starts_with('.'))
+ {
+ continue;
+ }
+ let holds = |place: &str| {
+ version::path(place).is_some_and(|segments| version.writer(&segments) == Some(party))
+ };
+ if !holds(place.as_str()) {
+ continue;
+ }
+ if let Some(text) = kept(&asks(path, text), text, &holds) {
+ files.insert(format!("{party}/{path}"), text);
+ }
+ }
+ let ways: BTreeSet<String> = files
+ .keys()
+ .flat_map(|path| {
+ path.match_indices('/')
+ .map(move |(at, _)| path[..at].to_owned())
+ })
+ .collect();
+ files.retain(|path, _| !ways.contains(path));
+ files
+}
+
+/// What is written out of a file holding `text`, which asks `asks`, where `holds` says
+/// which places its party holds: nothing where it doesn't hold the file's own places, or
+/// where no line but blank ones is left; else its lines but those whose place it doesn't
+/// hold, the text as it is where that is none.
+fn kept(asks: &Asks<'_>, text: &str, holds: &impl Fn(&str) -> bool) -> Option<String> {
+ if !asks.places.iter().all(|place| holds(place.as_str())) {
+ return None;
+ }
+ let Some(lines) = &asks.lines else {
+ return Some(text.to_owned());
+ };
+ let kept: Vec<&str> = lines
+ .iter()
+ .filter_map(|line| match line {
+ Line::Free(line) => Some(*line),
+ Line::Holds(line, place) if holds(place.as_str()) => Some(*line),
+ Line::Holds(..) | Line::Unspelled => None,
+ })
+ .collect();
+ if kept.iter().all(|line| line.trim().is_empty()) {
+ return None;
+ }
+ if kept.len() == lines.len() {
+ return Some(text.to_owned());
+ }
+ Some(kept.iter().map(|line| format!("{line}\n")).collect())
+}
+
#[cfg(test)]
mod tests {
use super::*;
- use crate::federation::harness::{Party, Spec};
+ use crate::federation::fold::Federation;
+ use crate::federation::harness::{self, Party, Spec};
+ use crate::federation::store::Id;
+ use crate::federation::store::memory::{Offered, Store};
fn files(entries: &[(&str, &str)]) -> Files {
entries
@@ -511,4 +674,262 @@ mod tests {
Err("berber is no party of the version".to_owned())
);
}
+
+ /// The files of `namespace` in `kartei`, by their paths in it.
+ fn namespace(kartei: &Files, namespace: &str) -> Files {
+ let prefix = format!("{namespace}/");
+ kartei
+ .iter()
+ .filter_map(|(path, text)| Some((path.strip_prefix(&prefix)?.to_owned(), text.clone())))
+ .collect()
+ }
+
+ /// The first version with krebs's rules, made from `kartei()`: tv, lass, and krebs.
+ fn krebs() -> (Party, Party, Party, Spec) {
+ let (tv, lass, krebs, mut first) = parties();
+ let given = rules(&kartei(), &version(&first), Some("krebs"))
+ .expect("rules")
+ .given;
+ let json = with_rules(&version(&first), &given);
+ first.rules = given
+ .iter()
+ .map(|(place, party)| (place.clone(), harness::str(party)))
+ .chain(
+ BY_NAME
+ .iter()
+ .map(|path| ((*path).to_owned(), harness::str("{party}"))),
+ )
+ .collect();
+ assert_eq!(first.json(), json);
+ (tv, lass, krebs, first)
+ }
+
+ /// A federation's store, kartei signed in.
+ struct Registry {
+ store: Store<Federation>,
+ }
+
+ impl Registry {
+ /// From the first version `first`, each party signing in its namespaces: tv and
+ /// lass their own, the group krebs berber's.
+ fn new(first: &Spec, tv: &Party, lass: &Party, krebs: &Party) -> Registry {
+ let mut registry = Registry {
+ store: Store::new(first.root(), Vec::new()).expect("a root"),
+ };
+ let kartei = kartei();
+ registry.sign_in(tv, &namespace(&kartei, "tv"));
+ registry.sign_in(lass, &namespace(&kartei, "lass"));
+ registry.sign_in(krebs, &namespace(&kartei, "berber"));
+ registry
+ }
+
+ fn take(&mut self, bytes: Vec<u8>) -> Id {
+ match self.store.offer(bytes) {
+ Offered::Taken(ids) => ids[0],
+ other => panic!("not taken: {other:?}"),
+ }
+ }
+
+ /// `party` signs in its namespace to hold `files`.
+ fn sign_in(&mut self, party: &Party, files: &Files) {
+ let values = self.store.state().values();
+ for (place, value) in writes(&party.name, files, &values) {
+ let heads = self.store.heads();
+ self.take(party.write(&heads, &place, value));
+ }
+ }
+
+ /// `proposer` proposes `next`, changing the version of hash `on`, and `voters`
+ /// back it: a version event writes it.
+ fn pass(&mut self, proposer: &Party, next: &Spec, on: Id, voters: &[&Party]) {
+ let heads = self.store.heads();
+ let proposal = self.take(proposer.propose(&heads, &next.json()));
+ let votes: Vec<Id> = voters
+ .iter()
+ .map(|voter| self.take(voter.vote(&[proposal], on, next.hash(), true)))
+ .collect();
+ self.take(harness::version_event(&votes, next.hash()));
+ assert_eq!(self.store.state().current().hash, next.hash());
+ }
+
+ fn out(&self) -> Files {
+ let state = self.store.state();
+ out(state.current(), &state.values())
+ }
+ }
+
+ #[test]
+ fn kartei_signed_in_is_written_out_as_it_was() {
+ let (tv, lass, krebs, first) = krebs();
+ let mut registry = Registry::new(&first, &tv, &lass, &krebs);
+ let kartei = kartei();
+ let mut was: Files = ["tv", "lass"]
+ .iter()
+ .flat_map(|party| {
+ namespace(&kartei, party)
+ .into_iter()
+ .map(move |(path, text)| (format!("{party}/{path}"), text))
+ })
+ .collect();
+ was.extend(
+ namespace(&kartei, "berber")
+ .into_iter()
+ .map(|(path, text)| (format!("krebs/{path}"), text)),
+ );
+ assert_eq!(registry.out(), was);
+ // Signed in again, the namespace holds them already.
+ let values = registry.store.state().values();
+ assert!(writes("tv", &namespace(&kartei, "tv"), &values).is_empty());
+ // A file gone is emptied, and written out no more.
+ let mut gone = namespace(&kartei, "tv");
+ gone.remove("users/vv/mail");
+ registry.sign_in(&tv, &gone);
+ assert!(!registry.out().contains_key("tv/users/vv/mail"));
+ }
+
+ #[test]
+ fn what_asks_for_a_place_its_party_doesnt_hold_is_left_out() {
+ let (tv, lass, krebs, first) = krebs();
+ let mut registry = Registry::new(&first, &tv, &lass, &krebs);
+ let mut files = namespace(&kartei(), "lass");
+ for (path, text) in [
+ // tv's name, tv's host, tv's address, a block nobody's, tv's user.
+ (
+ "hosts/yellow/retiolum/aliases",
+ "yellow.r\nwiki.r\ncgit.ne.r\n",
+ ),
+ ("hosts/ne/retiolum/ip4", "10.243.0.9\n"),
+ ("hosts/yellow/retiolum/tinc.subnets", "10.243.0.5\n"),
+ (
+ "hosts/yellow/retiolum/tinc.extra",
+ "Subnet = 10.243.0.0/24\nSPTPSCipher = aes-256-gcm\n",
+ ),
+ ("users/tv/mail", "lass@example.org\n"),
+ ] {
+ files.insert(path.to_owned(), text.to_owned());
+ }
+ registry.sign_in(&lass, &files);
+ let out = registry.out();
+ assert_eq!(
+ out["lass/hosts/yellow/retiolum/aliases"],
+ "yellow.r\nwiki.r\n"
+ );
+ assert!(!out.contains_key("lass/hosts/ne/retiolum/ip4"));
+ assert!(!out.contains_key("lass/hosts/yellow/retiolum/tinc.subnets"));
+ assert_eq!(
+ out["lass/hosts/yellow/retiolum/tinc.extra"],
+ "SPTPSCipher = aes-256-gcm\n"
+ );
+ assert!(!out.contains_key("lass/users/tv/mail"));
+ assert_eq!(out["tv/hosts/ne/retiolum/ip4"], "10.243.0.5\n");
+ assert_eq!(out["tv/users/tv/mail"], "tv@example.org\n");
+ }
+
+ #[test]
+ fn a_name_a_version_gives_another_party_is_that_partys_in_kartei() {
+ let (tv, lass, krebs, first) = krebs();
+ let mut registry = Registry::new(&first, &tv, &lass, &krebs);
+ let mut files = namespace(&kartei(), "tv");
+ files.insert(
+ "hosts/ne/retiolum/aliases".to_owned(),
+ "ne.r\ncgit.ne.r\nne.tv.r\nwiki.r\n".to_owned(),
+ );
+ registry.sign_in(&tv, &files);
+ let out = registry.out();
+ assert_eq!(
+ out["tv/hosts/ne/retiolum/aliases"],
+ "ne.r\ncgit.ne.r\nne.tv.r\n"
+ );
+ assert_eq!(
+ out["lass/hosts/yellow/retiolum/aliases"],
+ "yellow.r\nwiki.r\n"
+ );
+ let mut next = first.after(first.hash());
+ next.rules
+ .insert(0, ("names/r/wiki".to_owned(), harness::str("tv")));
+ registry.pass(&tv, &next, first.hash(), &[&tv, &lass, &krebs]);
+ let out = registry.out();
+ assert_eq!(
+ out["tv/hosts/ne/retiolum/aliases"],
+ "ne.r\ncgit.ne.r\nne.tv.r\nwiki.r\n"
+ );
+ assert_eq!(out["lass/hosts/yellow/retiolum/aliases"], "yellow.r\n");
+ }
+
+ #[test]
+ fn signing_in_writes_what_changed_and_empties_what_went() {
+ let values: BTreeMap<String, Json> = [
+ ("home/tv/a", "1"),
+ ("home/tv/b", "2"),
+ ("home/tv/e", "5"),
+ ("home/lass/c", "3"),
+ ]
+ .iter()
+ .map(|(place, text)| ((*place).to_owned(), harness::str(text)))
+ .collect();
+ assert_eq!(
+ writes(
+ "tv",
+ &files(&[("a", "1"), ("b", "two"), ("d", "4")]),
+ &values
+ ),
+ [
+ ("home/tv/b".to_owned(), harness::str("two")),
+ ("home/tv/d".to_owned(), harness::str("4")),
+ ("home/tv/e".to_owned(), Json::Null),
+ ]
+ );
+ }
+
+ #[test]
+ fn what_no_file_can_be_is_left_out() {
+ let (_, _, _, mut first) = parties();
+ first.rules = BY_NAME
+ .iter()
+ .map(|path| ((*path).to_owned(), harness::str("{party}")))
+ .collect();
+ let values: BTreeMap<String, Json> = [
+ ("home/tv/ssh/tv.pub", harness::str("ssh-ed25519 AAAA tv\n")),
+ // The way to another file, at a dot, no text, of no party.
+ ("home/tv/ssh", harness::str("x")),
+ ("home/tv/.git/config", harness::str("x")),
+ ("home/tv/ssh/../x", harness::str("x")),
+ ("home/tv/n", Json::Int(1)),
+ ("home/nobody/x", harness::str("x")),
+ ]
+ .into_iter()
+ .map(|(place, value)| (place.to_owned(), value))
+ .collect();
+ assert_eq!(
+ out(&version(&first), &values),
+ files(&[("tv/ssh/tv.pub", "ssh-ed25519 AAAA tv\n")])
+ );
+ }
+
+ #[cfg(unix)]
+ #[test]
+ fn kartei_is_read_from_its_files_and_written_to_new_ones() {
+ let dir = crate::files::scratch_dir("registry");
+ let tv = dir.join("tv");
+ std::fs::create_dir_all(tv.join("ssh")).unwrap();
+ std::fs::create_dir_all(tv.join("users/tv")).unwrap();
+ std::fs::write(tv.join("ssh/tv.pub"), "ssh-ed25519 AAAA tv\n").unwrap();
+ std::os::unix::fs::symlink("../../ssh/tv.pub", tv.join("users/tv/ssh.pub")).unwrap();
+ std::fs::write(tv.join(".hidden"), "x").unwrap();
+ let held = read(&tv).expect("read");
+ assert_eq!(
+ held,
+ files(&[
+ ("ssh/tv.pub", "ssh-ed25519 AAAA tv\n"),
+ ("users/tv/ssh.pub", "ssh-ed25519 AAAA tv\n"),
+ ])
+ );
+ let written = dir.join("out");
+ write(&written, &held).expect("written");
+ assert_eq!(read(&written), Ok(held.clone()));
+ // Nothing may be there yet, and no path may leave it.
+ assert!(write(&written, &held).is_err());
+ assert!(write(&dir.join("other"), &files(&[("../x", "1")])).is_err());
+ assert!(!dir.join("other").exists());
+ }
}