diff options
Diffstat (limited to 'src/access')
| -rw-r--r-- | src/access/policy.rs | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/access/policy.rs b/src/access/policy.rs index 85b562a..9f18eb5 100644 --- a/src/access/policy.rs +++ b/src/access/policy.rs @@ -56,6 +56,36 @@ impl Policy { } } +/// The policy a new swarm records in its first change (`--bootstrap-policy`, its JSON), +/// checked: one this node knows; strict, only with its first users' keys +/// (`--bootstrap-keys`), or nobody could sign what decides who may do what. None +/// given: none. +pub(crate) fn first( + given: Option<&str>, + bootstrap_keys: &[String], +) -> Result<Option<JsonValue>, String> { + let Some(given) = given else { + return Ok(None); + }; + let value: JsonValue = serde_json::from_str(given) + .map_err(|error| format!("bootstrap-policy isn't JSON: {error}"))?; + let mut doc = crate::document::Document::new(); + let mut write = doc.transaction(); + store::write_path(&mut write, &path(), &value) + .map_err(|error| format!("bootstrap-policy: {error}"))?; + let policy = of(&write); + write.rollback(); + match policy { + Policy::Unknown(why) => Err(format!("bootstrap-policy: one this node doesn't know: {why}")), + Policy::Open => Err("bootstrap-policy: no policy".to_owned()), + policy if policy.strict() && bootstrap_keys.is_empty() => Err( + "bootstrap-policy: strict, but no bootstrap-keys: nobody could sign what decides who may do what" + .to_owned(), + ), + _ => Ok(Some(value)), + } +} + /// The policy `doc` holds. pub(crate) fn of(doc: &impl ReadDoc) -> Policy { let value = match store::read_plain(doc, &path()) { @@ -145,4 +175,22 @@ mod tests { )); assert!(matches!(with(json!("on")), Policy::Unknown(_))); } + + /// A new swarm's policy: one this node knows; strict, only with keys to sign. + #[test] + fn the_first_policy() { + let keys = ["alice=ssh-ed25519 AAAA".to_owned()]; + assert_eq!(first(None, &[]), Ok(None)); + assert_eq!( + first(Some(r#"{"version": 1}"#), &[]), + Ok(Some(json!({"version": 1}))) + ); + assert_eq!( + first(Some(r#"{"version": 1, "values": "signed"}"#), &keys), + Ok(Some(json!({"version": 1, "values": "signed"}))) + ); + assert!(first(Some(r#"{"version": 1, "values": "signed"}"#), &[]).is_err()); + assert!(first(Some(r#"{"version": 2}"#), &keys).is_err()); + assert!(first(Some("not json"), &keys).is_err()); + } } |
