diff options
Diffstat (limited to 'src/access')
| -rw-r--r-- | src/access/bans.rs | 7 | ||||
| -rw-r--r-- | src/access/challenge.rs | 12 | ||||
| -rw-r--r-- | src/access/envelope.rs | 27 | ||||
| -rw-r--r-- | src/access/policy.rs | 20 | ||||
| -rw-r--r-- | src/access/registry.rs | 39 | ||||
| -rw-r--r-- | src/access/rules.rs | 58 | ||||
| -rw-r--r-- | src/access/sealed.rs | 31 | ||||
| -rw-r--r-- | src/access/strict.rs | 20 | ||||
| -rw-r--r-- | src/access/token.rs | 41 |
9 files changed, 135 insertions, 120 deletions
diff --git a/src/access/bans.rs b/src/access/bans.rs index a314f49..999f610 100644 --- a/src/access/bans.rs +++ b/src/access/bans.rs @@ -5,7 +5,8 @@ //! (`access::rules`, judged as the ban stood when they were made), and the tokens it //! issued (`access::token`). Deleting the entry lifts the ban. -use crate::document::{ReadDoc, meta, store}; +use crate::document::meta; +use crate::document::view::{self, Read}; /// Where `node`'s ban is. pub(crate) fn path(node: &str) -> Vec<String> { @@ -13,6 +14,6 @@ pub(crate) fn path(node: &str) -> Vec<String> { } /// Whether `doc` bans `node`. -pub(crate) fn banned(doc: &impl ReadDoc, node: &str) -> bool { - store::read_plain(doc, &path(node)).ok().flatten().is_some() +pub(crate) fn banned(doc: &impl Read, node: &str) -> bool { + view::read_plain(doc, &path(node)).ok().flatten().is_some() } diff --git a/src/access/challenge.rs b/src/access/challenge.rs index 8df0f08..4dd8dfb 100644 --- a/src/access/challenge.rs +++ b/src/access/challenge.rs @@ -16,7 +16,7 @@ use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD as B64URL}; use serde::{Deserialize, Serialize}; use super::{registry, sshsig}; -use crate::document::ReadDoc; +use crate::document::view::Read; use crate::identity::NodeKey; /// What a user's signature over a challenge is for (`sshsig`). @@ -70,7 +70,7 @@ pub(crate) struct Used(HashMap<String, (u64, String)>); /// `per_user` of its own still remembered. Why not, if not. #[allow(clippy::too_many_arguments)] pub(crate) fn take( - doc: &impl ReadDoc, + doc: &impl Read, key: &NodeKey, challenge: &str, signature: &str, @@ -150,8 +150,8 @@ pub(crate) fn take_with( mod tests { use super::*; use crate::document::Document; - use crate::document::store; use crate::document::testing::change; + use crate::document::view; use ed25519_dalek::SigningKey; use serde_json::json; @@ -163,12 +163,12 @@ mod tests { change(&mut doc, |doc| { let mut alice = registry::entry("root", 1, &[]); alice["keys"] = json!({ "laptop": line }); - store::write_path(doc, ®istry::user_path("alice"), &alice).unwrap(); + view::write_path(doc, ®istry::user_path("alice"), &alice).unwrap(); let mut carol = registry::entry("root", 1, &[]); carol["keys"] = json!({ "laptop": line }); carol["disabled"] = json!(true); - store::write_path(doc, ®istry::user_path("carol"), &carol).unwrap(); - store::write_path( + view::write_path(doc, ®istry::user_path("carol"), &carol).unwrap(); + view::write_path( doc, ®istry::user_path("bob"), ®istry::entry("root", 1, &[]), diff --git a/src/access/envelope.rs b/src/access/envelope.rs index f697809..d592bff 100644 --- a/src/access/envelope.rs +++ b/src/access/envelope.rs @@ -25,7 +25,8 @@ use sha2::{Digest, Sha256}; use super::{Right, agree, registry, sshsig}; pub(crate) use crate::document::signed::Payload; -use crate::document::{ReadDoc, meta, signed, store}; +use crate::document::view::{self, Read}; +use crate::document::{meta, signed}; /// What a signature over a payload is for (`sshsig`). pub(crate) const NAMESPACE: &str = "continuum-value"; @@ -38,8 +39,8 @@ pub(crate) fn listed_at() -> Vec<String> { /// The paths `doc` lists as user-signed; an entry that isn't a list of strings is left /// out. -pub(crate) fn listed(doc: &impl ReadDoc) -> Vec<Vec<String>> { - let Ok(Some(JsonValue::Array(entries))) = store::read_plain(doc, &listed_at()) else { +pub(crate) fn listed(doc: &impl Read) -> Vec<Vec<String>> { + let Ok(Some(JsonValue::Array(entries))) = view::read_plain(doc, &listed_at()) else { return Vec::new(); }; entries @@ -66,7 +67,7 @@ fn deciding() -> Vec<Vec<String>> { /// The paths whose values their users sign under a strict policy: what decides who may /// do what (`deciding`), and those `doc` lists (`listed`). -pub(crate) fn signed_paths(doc: &impl ReadDoc) -> Vec<Vec<String>> { +pub(crate) fn signed_paths(doc: &impl Read) -> Vec<Vec<String>> { let mut paths = deciding(); paths.extend(listed(doc)); paths @@ -150,7 +151,7 @@ pub(crate) fn read(envelope: &str) -> Result<Payload, String> { /// its swarm, that path, naming what it replaces and numbered after it, signed by one of /// its user's keys, a user who may write there. pub(crate) fn check( - doc: &impl ReadDoc, + doc: &impl Read, envelope: &str, path: &[String], previous: Option<&str>, @@ -160,7 +161,7 @@ pub(crate) fn check( payload, signature, } = signed::open(envelope)?; - let swarm = store::read_path( + let swarm = view::read_path( doc, &crate::document::meta::path(&[crate::document::meta::SWARM]), ) @@ -206,7 +207,7 @@ pub(crate) fn check( /// with only its keys changed from the one at `path` in `doc`, one left at least: a user /// manages its own keys, whatever else it may do. fn own_keys_only( - doc: &impl ReadDoc, + doc: &impl Read, payload: &Payload, user: ®istry::User, path: &[String], @@ -214,7 +215,7 @@ fn own_keys_only( if user.disabled || path != registry::user_path(&payload.user) { return false; } - let Ok(Some(mut before)) = store::read_plain(doc, path) else { + let Ok(Some(mut before)) = view::read_plain(doc, path) else { return false; }; // One key left at least: a user can't sign, nor log in by key, without one. @@ -256,13 +257,13 @@ mod tests { let line = sshsig::PublicKey(key.verifying_key().to_bytes()).line(); let mut doc = Document::new(); change(&mut doc, |doc| { - store::write_path(doc, &path("_continuum/swarm"), &json!("s")).unwrap(); + view::write_path(doc, &path("_continuum/swarm"), &json!("s")).unwrap(); let mut alice = registry::entry("root", 1, &[(path("config"), &[Right::Write][..])]); alice["keys"] = json!({ "laptop": line }); - store::write_path(doc, ®istry::user_path("alice"), &alice).unwrap(); + view::write_path(doc, ®istry::user_path("alice"), &alice).unwrap(); let mut bob = registry::entry("root", 1, &[]); bob["keys"] = json!({ "laptop": line }); - store::write_path(doc, ®istry::user_path("bob"), &bob).unwrap(); + view::write_path(doc, ®istry::user_path("bob"), &bob).unwrap(); }); doc } @@ -363,7 +364,7 @@ mod tests { fn users_sign_their_own_keys() { let key = SigningKey::from_bytes(&[5; 32]); let doc = registry_with(&key); - let entry = store::read_plain(&doc, ®istry::user_path("alice")) + let entry = view::read_plain(&doc, ®istry::user_path("alice")) .unwrap() .unwrap(); let line = @@ -388,7 +389,7 @@ mod tests { fn listed_paths() { let mut doc = Document::new(); change(&mut doc, |doc| { - store::write_path(doc, &listed_at(), &json!([["config"], ["a", "b"], "junk"])).unwrap(); + view::write_path(doc, &listed_at(), &json!([["config"], ["a", "b"], "junk"])).unwrap(); }); assert_eq!(listed(&doc), vec![path("config"), path("a/b")]); let signed = signed_paths(&doc); diff --git a/src/access/policy.rs b/src/access/policy.rs index 1e28731..e4027b8 100644 --- a/src/access/policy.rs +++ b/src/access/policy.rs @@ -20,7 +20,8 @@ use serde_json::Value as JsonValue; -use crate::document::{ReadDoc, meta, store}; +use crate::document::meta; +use crate::document::view::{self, Read}; /// The policy versions this node knows: rules by path, and a swarm holding a store. pub(crate) const VERSION: u64 = 1; @@ -108,7 +109,7 @@ pub(crate) fn first( pub(crate) fn as_written(value: &JsonValue) -> Result<Policy, String> { let mut doc = crate::document::Document::new(); let mut write = doc.transaction(); - store::write_path(&mut write, &path(), value).map_err(|error| error.to_string())?; + view::write_path(&mut write, &path(), value).map_err(|error| error.to_string())?; let policy = of(&write); write.rollback(); Ok(policy) @@ -118,8 +119,8 @@ pub(crate) fn as_written(value: &JsonValue) -> Result<Policy, String> { /// where its `store` is, as continuum writes them: an Automerge text there, or one holding /// the policy, reads as a string in plain JSON, so a policy that would read so only /// through a text is one nobody knows, never what its characters spell. -pub(crate) fn of(doc: &impl ReadDoc) -> Policy { - match store::read_plain(doc, &path()) { +pub(crate) fn of(doc: &impl Read) -> Policy { + match view::read_plain(doc, &path()) { Ok(Some(value)) => match read(&value) { policy if policy.strict() && !a_string(doc, "values") => { Policy::Unknown("values not a string".to_owned()) @@ -137,13 +138,9 @@ pub(crate) fn of(doc: &impl ReadDoc) -> Policy { /// Whether the policy `doc` holds, read in typed JSON (where a text is tagged, and an /// envelope reads as its payload), has a string for its `key`. -fn a_string(doc: &impl ReadDoc, key: &str) -> bool { - let typed = crate::document::typed::JsonForm { - typed: true, - ..Default::default() - }; +fn a_string(doc: &impl Read, key: &str) -> bool { matches!( - store::read_plain_as(doc, &path(), typed), + doc.read_typed(&path()), Ok(Some(value)) if value.get(key).is_some_and(JsonValue::is_string) ) } @@ -211,13 +208,14 @@ fn store_of(value: &JsonValue) -> Policy { mod tests { use super::*; use crate::document::Document; + use crate::document::store; use crate::document::testing::change; use serde_json::json; fn with(policy: JsonValue) -> Policy { let mut doc = Document::new(); change(&mut doc, |doc| { - store::write_path(doc, &path(), &policy).unwrap(); + view::write_path(doc, &path(), &policy).unwrap(); }); of(&doc) } diff --git a/src/access/registry.rs b/src/access/registry.rs index e806d0c..5a58e1f 100644 --- a/src/access/registry.rs +++ b/src/access/registry.rs @@ -18,7 +18,8 @@ use std::collections::BTreeMap; use serde_json::{Value as JsonValue, json}; use super::{Capability, Caps, Right}; -use crate::document::{ReadDoc, meta, store}; +use crate::document::meta; +use crate::document::view::{self, Read}; /// `_continuum/users`. pub(crate) fn users_dir() -> Vec<String> { @@ -34,13 +35,13 @@ pub(crate) fn user_path(name: &str) -> Vec<String> { /// Whether the swarm has a registry: until it has, every user a node lets in may do /// everything but write protocol data (`docs/access.md`, A swarm without a registry). -pub(crate) fn exists(doc: &impl ReadDoc) -> bool { - matches!(store::keys_at(doc, &users_dir()), Ok(Some(_))) +pub(crate) fn exists(doc: &impl Read) -> bool { + matches!(view::keys_at(doc, &users_dir()), Ok(Some(_))) } /// The registered users' names, sorted. -pub(crate) fn names(doc: &impl ReadDoc) -> Vec<String> { - let mut names = store::keys_at(doc, &users_dir()) +pub(crate) fn names(doc: &impl Read) -> Vec<String> { + let mut names = view::keys_at(doc, &users_dir()) .ok() .flatten() .unwrap_or_default(); @@ -127,11 +128,11 @@ impl User { } /// The entry of user `name`, if it has one. -pub(crate) fn user(doc: &impl ReadDoc, name: &str) -> Option<User> { - let value = store::read_plain(doc, &user_path(name)).ok()??; +pub(crate) fn user(doc: &impl Read, name: &str) -> Option<User> { + let value = view::read_plain(doc, &user_path(name)).ok()??; let mut user = value.is_object().then(|| User::from_json(&value))?; // A damaged one revokes every token. - user.tokens_not_before = match store::read_plain(doc, &logouts_path(name)) { + user.tokens_not_before = match view::read_plain(doc, &logouts_path(name)) { Ok(None) => 0, Ok(Some(at)) => at.as_u64().unwrap_or(u64::MAX), Err(_) => u64::MAX, @@ -155,15 +156,15 @@ pub(crate) fn session_path(name: &str, session: &str) -> Vec<String> { } /// Whether user `name`'s session `session` was ended (`session_path`). -pub(crate) fn session_ended(doc: &impl ReadDoc, name: &str, session: &str) -> bool { - store::read_path(doc, &session_path(name, session)) +pub(crate) fn session_ended(doc: &impl Read, name: &str, session: &str) -> bool { + view::read_path(doc, &session_path(name, session)) .ok() .flatten() .is_some() } /// The users who may manage users: enabled, with `write` on `_continuum/users`. -pub(crate) fn managers(doc: &impl ReadDoc) -> Vec<String> { +pub(crate) fn managers(doc: &impl Read) -> Vec<String> { names(doc) .into_iter() .filter(|name| caps_of(doc, name).allows(Right::Write, &users_dir())) @@ -171,7 +172,7 @@ pub(crate) fn managers(doc: &impl ReadDoc) -> Vec<String> { } /// What user `name` may do: none if it isn't registered (or is disabled). -pub(crate) fn caps_of(doc: &impl ReadDoc, name: &str) -> Caps { +pub(crate) fn caps_of(doc: &impl Read, name: &str) -> Caps { user(doc, name).map(|user| user.caps()).unwrap_or_default() } @@ -289,7 +290,7 @@ mod tests { ], ); change(&mut doc, |tx| { - store::write_path(tx, &user_path("alice"), &entry).unwrap() + view::write_path(tx, &user_path("alice"), &entry).unwrap() }); assert!(exists(&doc)); assert_eq!(super::names(&doc), vec!["alice"]); @@ -308,7 +309,7 @@ mod tests { let mut entry = entry("x", 0, &[(vec![], &[Right::Read])]); entry["disabled"] = json!(true); change(&mut doc, |tx| { - store::write_path(tx, &user_path("carol"), &entry).unwrap() + view::write_path(tx, &user_path("carol"), &entry).unwrap() }); assert!(user(&doc, "carol").is_some()); assert!(caps_of(&doc, "carol").is_empty()); @@ -319,7 +320,7 @@ mod tests { "disabled": "no" }); change(&mut doc, |tx| { - store::write_path(tx, &user_path("dave"), &damaged).unwrap() + view::write_path(tx, &user_path("dave"), &damaged).unwrap() }); let dave = user(&doc, "dave").unwrap(); assert_eq!(dave.caps.len(), 1); @@ -374,14 +375,14 @@ mod tests { for (name, caps) in [("alice", all_caps()), ("bob", vec![(Vec::new(), DATA)])] { let entry = entry("x", 0, &caps); change(&mut doc, |tx| { - store::write_path(tx, &user_path(name), &entry).unwrap() + view::write_path(tx, &user_path(name), &entry).unwrap() }); } assert_eq!(managers(&doc), vec!["alice"]); let mut disabled = user_path("alice"); disabled.push("disabled".to_owned()); change(&mut doc, |tx| { - store::write_path(tx, &disabled, &json!(true)).unwrap() + view::write_path(tx, &disabled, &json!(true)).unwrap() }); assert!(managers(&doc).is_empty()); } @@ -391,14 +392,14 @@ mod tests { let mut ours = Document::new(); let entry = entry("x", 0, &[]); change(&mut ours, |tx| { - store::write_path(tx, &user_path("erin"), &entry).unwrap() + view::write_path(tx, &user_path("erin"), &entry).unwrap() }); let mut theirs = ours.fork(); for (doc, at) in [(&mut ours, "notes"), (&mut theirs, "logs")] { let mut cap_path = user_path("erin"); cap_path.extend(["caps".to_owned(), new_id()]); let cap = capability("x", 0, &path(at), &[Right::Read]); - change(doc, |tx| store::write_path(tx, &cap_path, &cap).unwrap()); + change(doc, |tx| view::write_path(tx, &cap_path, &cap).unwrap()); } ours.merge(&mut theirs).unwrap(); let caps = caps_of(&ours, "erin"); diff --git a/src/access/rules.rs b/src/access/rules.rs index 520ff0f..93cef63 100644 --- a/src/access/rules.rs +++ b/src/access/rules.rs @@ -44,7 +44,8 @@ use crate::document::at::At; use crate::document::inspect::{Action, Op, Path}; use crate::document::meta; use crate::document::replica::Epoch; -use crate::document::{ChangeHash, Document, ReadDoc, store}; +use crate::document::view::{self, Read}; +use crate::document::{ChangeHash, Document, ReadDoc}; /// A change to judge, as its bytes say. #[derive(Debug, Clone)] @@ -195,18 +196,37 @@ pub(crate) fn judge_reading( }; } let at = At::new(doc, change.seen).ok_or(Refusal::Unready)?; - judge_seen(&at, epoch, &change.unit(), reads.at(change.seen)).map_err(Refusal::Rule) + let unit = change.unit(); + // A swarm holding a store is judged by what a change writes of an Automerge document + // (`access::holding`). + let holding = |fold: &str| { + #[cfg(feature = "federation")] + { + super::holding::judge(&at, epoch, &unit, fold) + } + #[cfg(not(feature = "federation"))] + { + let _ = fold; + Ok(()) + } + }; + judge_seen(&at, epoch, &unit, reads.at(change.seen), holding).map_err(Refusal::Rule) } /// Judges `unit` by the rules, reading `at`, the document (of `epoch`) as its writer saw /// it, with what was read of it before (`read`): why they refuse it, where they do. Not /// an epoch's root, which writes what its document starts with and is judged by none. +/// In a swarm holding a store of some fold, as `holding` judges it there, which is its +/// kind of document's to say. pub(crate) fn judge_seen( - at: &impl ReadDoc, + at: &impl Read, epoch: Epoch, unit: &Unit<'_>, read: &mut Seen, + holding: impl FnOnce(&str) -> Result<(), String>, ) -> Result<(), String> { + #[cfg(not(feature = "federation"))] + let _ = &holding; // Written while the document had no policy (or one this node doesn't know): judged // as it was then, by none. History from before the policy stays as it is. let policy = read @@ -223,7 +243,7 @@ pub(crate) fn judge_seen( // its genesis change, or not at all. #[cfg(feature = "federation")] match policy { - Policy::Store(fold) => return super::holding::judge(at, epoch, unit, fold), + Policy::Store(fold) => return holding(fold), _ if super::holding::turns_store(unit.ops) => { return Err("a swarm holds a store from its genesis change only".to_owned()); } @@ -247,7 +267,7 @@ pub(crate) fn judge_seen( let member = *read .member .entry(node.to_owned()) - .or_insert_with(|| store::read_path(at, &own_entry).ok().flatten().is_some()); + .or_insert_with(|| view::read_path(at, &own_entry).ok().flatten().is_some()); let registering = unit .paths .iter() @@ -287,7 +307,7 @@ pub(crate) fn judge_seen( .paths .iter() .any(|other| other.len() > path.len() && other.starts_with(path)) - && store::read_path(at, path).ok().flatten().is_none() + && view::read_path(at, path).ok().flatten().is_none() }) .collect(); // Whether the change only makes a map at `path`, where there is none. @@ -296,7 +316,7 @@ pub(crate) fn judge_seen( .iter() .filter(|op| op.path == *path) .all(|op| op.action == Action::MakeMap) - && store::read_path(at, path).ok().flatten().is_none() + && view::read_path(at, path).ok().flatten().is_none() }; for path in unit.paths { if on_the_way.contains(path) { @@ -371,7 +391,7 @@ pub(crate) fn judge_seen( unit.paths .iter() .any(|other| other == &meta::path(&[meta::REMAKES, id, "proposal"])) - && store::read_path(at, &meta::path(&[meta::REMAKES, id])) + && view::read_path(at, &meta::path(&[meta::REMAKES, id])) .ok() .flatten() .is_none() @@ -383,7 +403,7 @@ pub(crate) fn judge_seen( // one who may write them. None => made(path) || user_may(path), Some("proposal") => { - store::read_path(at, &meta::path(&[meta::REMAKES, id, "proposal"])) + view::read_path(at, &meta::path(&[meta::REMAKES, id, "proposal"])) .ok() .flatten() .is_none() @@ -482,7 +502,7 @@ pub(crate) fn judge_held( /// string put there that is one (or deleting what is there); nothing within one; nothing /// in an object nowhere to be found, which might be one. fn sealed_values( - at: &impl ReadDoc, + at: &impl Read, declared: &[super::sealed::Declared], ops: &[Op], ) -> Result<(), String> { @@ -514,7 +534,7 @@ fn sealed_values( /// as a write replacing it does); nothing within one; above one, /// nothing replacing or deleting what holds one (a map made where there is none yet, /// what is inside a list or text, which holds none). -fn signed_values(at: &impl ReadDoc, ops: &[Op]) -> Result<(), String> { +fn signed_values(at: &impl Read, ops: &[Op]) -> Result<(), String> { let signed = super::envelope::signed_paths(at); for op in ops { let path = &op.path; @@ -533,7 +553,7 @@ fn signed_values(at: &impl ReadDoc, ops: &[Op]) -> Result<(), String> { Action::Delete if replaced() => continue, _ => return Err(format!("{} holds a value its user signs", shown(path))), }; - let previous = match store::read_path(at, path) { + let previous = match view::read_path(at, path) { Ok(Some(serde_json::Value::String(text))) if super::envelope::read(&text).is_ok() => { @@ -569,7 +589,7 @@ fn signed_values(at: &impl ReadDoc, ops: &[Op]) -> Result<(), String> { /// Whether what is at `path` in `at` holds a value at a place for one its user signs, /// of `signed` (`envelope::signed_paths`), signed or from before the policy; one that /// doesn't read, as if it did. -fn holds_signed(at: &impl ReadDoc, path: &[String], signed: &[Vec<String>]) -> bool { +fn holds_signed(at: &impl Read, path: &[String], signed: &[Vec<String>]) -> bool { fn within(value: &serde_json::Value, place: &mut Vec<String>, signed: &[Vec<String>]) -> bool { if super::envelope::is_signed(signed, place) { return true; @@ -586,7 +606,7 @@ fn holds_signed(at: &impl ReadDoc, path: &[String], signed: &[Vec<String>]) -> b _ => false, } } - match store::read_path(at, path) { + match view::read_path(at, path) { Ok(Some(value)) => within(&value, &mut path.to_vec(), signed), Ok(None) => false, Err(_) => true, @@ -608,12 +628,12 @@ fn shown(path: &Path) -> String { /// Whether remake `id`, coordinated by `node`, made `epoch`: as its snapshot's metadata /// says (`remake::snapshot`), at `at`. Never genesis. -pub(super) fn made_the_epoch(at: &impl ReadDoc, epoch: Epoch, id: &str, node: &str) -> bool { +pub(super) fn made_the_epoch(at: &impl Read, epoch: Epoch, id: &str, node: &str) -> bool { if epoch.generation == 0 { return false; } let generation = epoch.generation.to_string(); - let Some(metadata) = store::read_path(at, &meta::path(&[meta::EPOCHS, &generation])) + let Some(metadata) = view::read_path(at, &meta::path(&[meta::EPOCHS, &generation])) .ok() .flatten() else { @@ -627,8 +647,8 @@ pub(super) fn made_the_epoch(at: &impl ReadDoc, epoch: Epoch, id: &str, node: &s } /// The coordinator remake `id`'s proposal names, at `at`. -pub(super) fn coordinator(at: &impl ReadDoc, id: &str) -> Option<String> { - let proposal = store::read_path(at, &meta::path(&[meta::REMAKES, id, "proposal"])) +pub(super) fn coordinator(at: &impl Read, id: &str) -> Option<String> { + let proposal = view::read_path(at, &meta::path(&[meta::REMAKES, id, "proposal"])) .ok() .flatten()?; proposal @@ -659,7 +679,7 @@ mod tests { }; let at = path(at); transact(doc, UnixMillis(1), &writer, |doc| { - store::write_path(doc, &at, &value) + view::write_path(doc, &at, &value) }) .unwrap(); } diff --git a/src/access/sealed.rs b/src/access/sealed.rs index 2b8085a..c30bea7 100644 --- a/src/access/sealed.rs +++ b/src/access/sealed.rs @@ -25,7 +25,8 @@ use serde_json::Value as JsonValue; use sha2::{Digest, Sha256}; use super::{agree, envelope, registry, sshsig}; -use crate::document::{ReadDoc, meta, store}; +use crate::document::meta; +use crate::document::view::{self, Read}; /// What every sealed value starts with. const PREFIX: &str = "cs1."; @@ -47,8 +48,8 @@ pub(crate) struct Declared { } /// What `doc` declares sealed; an entry that doesn't read as one is left out. -pub(crate) fn declared(doc: &impl ReadDoc) -> Vec<Declared> { - let Ok(Some(JsonValue::Array(entries))) = store::read_plain(doc, &declared_at()) else { +pub(crate) fn declared(doc: &impl Read) -> Vec<Declared> { + let Ok(Some(JsonValue::Array(entries))) = view::read_plain(doc, &declared_at()) else { return Vec::new(); }; entries @@ -140,11 +141,7 @@ pub(crate) fn recipients(sealed: &str) -> Result<BTreeSet<String>, String> { /// (as its writer saw it): a sealed value, or, signed by its user (`access::envelope`), /// one holding a sealed value or `null` (deleted); with readers declared, sealed for /// exactly their keys. -pub(crate) fn check( - doc: &impl ReadDoc, - declared: &Declared, - value: &JsonValue, -) -> Result<(), String> { +pub(crate) fn check(doc: &impl Read, declared: &Declared, value: &JsonValue) -> Result<(), String> { let JsonValue::String(text) = value else { return Err("a sealed value is a string (cs1.<age file>)".to_owned()); }; @@ -187,7 +184,7 @@ pub(crate) fn check( /// Checks what is at `path` in `doc` after a write there: nothing within a declared /// place but its value, and every value at one (at `path` or below it) as `check` says. /// Why not, if not. -pub(crate) fn kept(doc: &impl ReadDoc, path: &[String]) -> Result<(), String> { +pub(crate) fn kept(doc: &impl Read, path: &[String]) -> Result<(), String> { let declared = declared(doc); if declared.is_empty() || !reaches(&declared, path) { return Ok(()); @@ -196,7 +193,7 @@ pub(crate) fn kept(doc: &impl ReadDoc, path: &[String]) -> Result<(), String> { return Err(format!("/{} is within a sealed value", path.join("/"))); } fn walk( - doc: &impl ReadDoc, + doc: &impl Read, declared: &[Declared], value: &JsonValue, at: &mut Vec<String>, @@ -215,7 +212,7 @@ pub(crate) fn kept(doc: &impl ReadDoc, path: &[String]) -> Result<(), String> { } Ok(()) } - match store::read_path(doc, path) { + match view::read_path(doc, path) { Ok(Some(value)) => walk(doc, &declared, &value, &mut path.to_vec()), Ok(None) => Ok(()), Err(error) => Err(error.to_string()), @@ -270,11 +267,11 @@ mod tests { change(&mut doc, |doc| { let mut alice = registry::entry("root", 1, &[]); alice["keys"] = json!({ "laptop": key(1).line(), "phone": key(2).line() }); - store::write_path(doc, ®istry::user_path("alice"), &alice).unwrap(); + view::write_path(doc, ®istry::user_path("alice"), &alice).unwrap(); let mut bob = registry::entry("root", 1, &[]); bob["keys"] = json!({ "laptop": key(3).line() }); - store::write_path(doc, ®istry::user_path("bob"), &bob).unwrap(); - store::write_path( + view::write_path(doc, ®istry::user_path("bob"), &bob).unwrap(); + view::write_path( doc, &declared_at(), &json!([ @@ -360,13 +357,13 @@ mod tests { let mut doc = swarm(); let all = sealed_for(&[key(1), key(2), key(3)]); change(&mut doc, |doc| { - store::write_path(doc, &path("secrets"), &json!({"db": all, "other": all})).unwrap(); - store::write_path(doc, &path("elsewhere"), &json!("plain")).unwrap(); + view::write_path(doc, &path("secrets"), &json!({"db": all, "other": all})).unwrap(); + view::write_path(doc, &path("elsewhere"), &json!("plain")).unwrap(); }); assert!(kept(&doc, &path("secrets")).is_ok()); assert!(kept(&doc, &path("elsewhere")).is_ok()); change(&mut doc, |doc| { - store::write_path(doc, &path("secrets/web"), &json!("plain")).unwrap(); + view::write_path(doc, &path("secrets/web"), &json!("plain")).unwrap(); }); assert!(kept(&doc, &path("secrets")).is_err()); assert!(kept(&doc, &path("secrets/web")).is_err()); diff --git a/src/access/strict.rs b/src/access/strict.rs index 0b294f8..9857bed 100644 --- a/src/access/strict.rs +++ b/src/access/strict.rs @@ -16,7 +16,7 @@ use serde_json::Value as JsonValue; use sha2::{Digest, Sha256}; use super::{envelope, sshsig}; -use crate::document::{ReadDoc, store}; +use crate::document::view::{self, Read}; /// What a manifest's signature is for (`sshsig`). pub(crate) const NAMESPACE: &str = "continuum-strict"; @@ -95,21 +95,21 @@ pub(crate) fn digest(value: &JsonValue) -> String { /// The places values are signed in `doc` (`envelope::signed_paths`, `*` as `doc` has /// them) that hold one, with it, as the store reads it. -pub(crate) fn held(doc: &impl ReadDoc) -> Vec<(Vec<String>, JsonValue)> { +pub(crate) fn held(doc: &impl Read) -> Vec<(Vec<String>, JsonValue)> { fn expand( - doc: &impl ReadDoc, + doc: &impl Read, pattern: &[String], at: Vec<String>, out: &mut Vec<(Vec<String>, JsonValue)>, ) { match pattern.split_first() { None => { - if let Ok(Some(value)) = store::read_path(doc, &at) { + if let Ok(Some(value)) = view::read_path(doc, &at) { out.push((at, value)); } } Some((segment, rest)) if segment == "*" => { - for key in store::keys_at(doc, &at).ok().flatten().unwrap_or_default() { + for key in view::keys_at(doc, &at).ok().flatten().unwrap_or_default() { let mut next = at.clone(); next.push(key); expand(doc, rest, next, out); @@ -135,7 +135,7 @@ pub(crate) fn held(doc: &impl ReadDoc) -> Vec<(Vec<String>, JsonValue)> { /// held at a signed place now, the policy `policy`, for `user` to look over and sign at /// `at`. Its exact line. pub(crate) fn manifest_of( - doc: &impl ReadDoc, + doc: &impl Read, swarm: &str, epoch: crate::document::replica::EpochRecord, base: &[crate::document::ChangeHash], @@ -225,15 +225,15 @@ mod tests { fn a_manifest_signed_and_checked() { let mut doc = Document::new(); change(&mut doc, |doc| { - store::write_path(doc, &path("_continuum/users/alice"), &json!({"caps": {}})).unwrap(); - store::write_path( + view::write_path(doc, &path("_continuum/users/alice"), &json!({"caps": {}})).unwrap(); + view::write_path( doc, &path("_continuum/access/userSigned"), &json!([["config"]]), ) .unwrap(); - store::write_path(doc, &path("config"), &json!({"mode": "fast"})).unwrap(); - store::write_path(doc, &path("notes"), &json!(1)).unwrap(); + view::write_path(doc, &path("config"), &json!({"mode": "fast"})).unwrap(); + view::write_path(doc, &path("notes"), &json!(1)).unwrap(); }); let policy = json!({"version": 1, "values": "signed"}); let epoch = diff --git a/src/access/token.rs b/src/access/token.rs index 650a5c4..073d1e4 100644 --- a/src/access/token.rs +++ b/src/access/token.rs @@ -17,7 +17,8 @@ use base64::{Engine, engine::general_purpose::URL_SAFE_NO_PAD as BASE64URL}; use serde::{Deserialize, Serialize}; use super::{Capability, registry}; -use crate::document::{ReadDoc, meta, store}; +use crate::document::meta; +use crate::document::view::{self, Read}; use crate::identity::{self, NodeKey}; const PREFIX: &str = "ct1."; @@ -117,7 +118,7 @@ pub(crate) enum Refused { /// (`registry::User::tokens_not_before`), of a session not ended /// (`registry::session_ended`). pub(crate) fn verify( - doc: &impl ReadDoc, + doc: &impl Read, token: &str, swarm: &str, now: u64, @@ -161,11 +162,7 @@ pub(crate) fn verify( /// store, only one it issued itself (`docs/store.md`, Members), as a node there is /// whoever its operator lets in, and lets in nobody's but its own. #[cfg(feature = "federation")] -pub(crate) fn issued_here( - doc: &impl ReadDoc, - local: &str, - claims: Claims, -) -> Result<Claims, Refused> { +pub(crate) fn issued_here(doc: &impl Read, local: &str, claims: Claims) -> Result<Claims, Refused> { match super::policy::of(doc) { super::policy::Policy::Store(_) if claims.issuer != local => Err(Refused::ElsewhereIssued), _ => Ok(claims), @@ -176,14 +173,14 @@ pub(crate) fn issued_here( /// its issuer a member and not banned, its user registered, enabled, its tokens not /// revoked since and its session not ended. What a stream opened with it is judged by /// as the document changes (`api::login::streams_end`). -pub(crate) fn still_taken(doc: &impl ReadDoc, claims: &Claims) -> Result<(), Refused> { +pub(crate) fn still_taken(doc: &impl Read, claims: &Claims) -> Result<(), Refused> { issuer_counts(doc, claims)?; user_counts(doc, claims) } /// Its issuer a member of the swarm, not banned. -fn issuer_counts(doc: &impl ReadDoc, claims: &Claims) -> Result<(), Refused> { - let member = store::read_path(doc, &meta::path(&[meta::MEMBERS, &claims.issuer])) +fn issuer_counts(doc: &impl Read, claims: &Claims) -> Result<(), Refused> { + let member = view::read_path(doc, &meta::path(&[meta::MEMBERS, &claims.issuer])) .ok() .flatten() .is_some(); @@ -198,7 +195,7 @@ fn issuer_counts(doc: &impl ReadDoc, claims: &Claims) -> Result<(), Refused> { /// In a swarm with a registry: its user registered, enabled, its tokens not revoked /// since it was issued, its session not ended. -fn user_counts(doc: &impl ReadDoc, claims: &Claims) -> Result<(), Refused> { +fn user_counts(doc: &impl Read, claims: &Claims) -> Result<(), Refused> { user_taken(doc, &claims.user)?; if let Some(user) = registry::user(doc, &claims.user) { if claims.iat < user.tokens_not_before { @@ -214,7 +211,7 @@ fn user_counts(doc: &impl ReadDoc, claims: &Claims) -> Result<(), Refused> { /// In a swarm with a registry: user `name` registered and enabled. What a stream opened /// by password or a proxy's header is judged by as the document changes /// (`api::login::streams_end`), as one opened with a token is by `still_taken`. -pub(crate) fn user_taken(doc: &impl ReadDoc, name: &str) -> Result<(), Refused> { +pub(crate) fn user_taken(doc: &impl Read, name: &str) -> Result<(), Refused> { if registry::exists(doc) { let user = registry::user(doc, name).ok_or(Refused::UnknownUser)?; if user.disabled { @@ -256,7 +253,7 @@ mod tests { fn member(doc: &mut Document, key: &NodeKey) { let path = meta::path(&[meta::MEMBERS, &key.id()]); - change(doc, |tx| store::write_path(tx, &path, &json!({})).unwrap()); + change(doc, |tx| view::write_path(tx, &path, &json!({})).unwrap()); } /// A party's session names the key it logged in with, which a token of a user's @@ -374,7 +371,7 @@ mod tests { let ban = crate::access::bans::path(&key.id()); let mut banned = doc.fork(); change(&mut banned, |tx| { - store::write_path(tx, &ban, &json!({"reason": "lost"})).unwrap() + view::write_path(tx, &ban, &json!({"reason": "lost"})).unwrap() }); assert_eq!( verify(&banned, &token, "s", 1_500), @@ -405,24 +402,24 @@ mod tests { assert!(verify(&doc, &token, "s", 1_500).is_ok()); let bob = registry::entry("x", 0, ®istry::all_caps()); change(&mut doc, |tx| { - store::write_path(tx, ®istry::user_path("bob"), &bob).unwrap() + view::write_path(tx, ®istry::user_path("bob"), &bob).unwrap() }); assert_eq!(verify(&doc, &token, "s", 1_500), Err(Refused::UnknownUser)); let mut alice = registry::entry("x", 0, &[(Vec::new(), &[Right::Read][..])]); change(&mut doc, |tx| { - store::write_path(tx, ®istry::user_path("alice"), &alice).unwrap(); - store::write_path(tx, ®istry::logouts_path("alice"), &json!(1_000)).unwrap() + view::write_path(tx, ®istry::user_path("alice"), &alice).unwrap(); + view::write_path(tx, ®istry::logouts_path("alice"), &json!(1_000)).unwrap() }); assert!(verify(&doc, &token, "s", 1_500).is_ok()); change(&mut doc, |tx| { - store::write_path(tx, ®istry::logouts_path("alice"), &json!(1_001)).unwrap() + view::write_path(tx, ®istry::logouts_path("alice"), &json!(1_001)).unwrap() }); assert_eq!(verify(&doc, &token, "s", 1_500), Err(Refused::Revoked)); // One session ended: its tokens refused, another's taken. let session = claims_of(&token).session; change(&mut doc, |tx| { - store::write_path(tx, ®istry::logouts_path("alice"), &json!(0)).unwrap(); - store::write_path( + view::write_path(tx, ®istry::logouts_path("alice"), &json!(0)).unwrap(); + view::write_path( tx, ®istry::session_path("alice", &session), &json!(10_000), @@ -433,8 +430,8 @@ mod tests { assert!(verify(&doc, &issue(&key, &claims(&key)), "s", 1_500).is_ok()); alice["disabled"] = json!(true); change(&mut doc, |tx| { - store::write_path(tx, ®istry::logouts_path("alice"), &json!(0)).unwrap(); - store::write_path(tx, ®istry::user_path("alice"), &alice).unwrap() + view::write_path(tx, ®istry::logouts_path("alice"), &json!(0)).unwrap(); + view::write_path(tx, ®istry::user_path("alice"), &alice).unwrap() }); assert_eq!(verify(&doc, &token, "s", 1_500), Err(Refused::Disabled)); } |
