use serde::{Deserialize, Serialize};
use crate::serve::api::ApiError;
use crate::serve::fleet::identity::{PublicKey, Signer};
use crate::serve::fleet::member::{CONTROL_PLANE_SELF, ControlPlane, check_member_name};
use crate::serve::fleet::membership::{self, Compare, Log, Op, Stored};
use crate::serve::fleet::membership_api::{Own, adopt_into, can_sign, lists, sign_into};
pub struct This<'a> {
pub name: &'a str,
pub control_plane: &'a ControlPlane,
pub known: &'a [String],
}
pub struct Target<'a> {
pub label: &'a str,
pub name: Option<&'a str>,
pub control_plane: &'a ControlPlane,
pub known: &'a [String],
}
#[derive(Debug, PartialEq, Eq)]
pub struct Plan {
pub name: String,
pub target_set: Option<TargetSet>,
pub fleet: Fleet,
}
#[derive(Debug, PartialEq, Eq)]
pub struct TargetSet {
pub name: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Fleet {
pub control_plane: String,
pub joined: Joined,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Joined {
Target,
This,
New,
Same,
}
fn resolve(control_plane: &ControlPlane, own: Option<&str>) -> Option<String> {
match control_plane {
ControlPlane::None => None,
ControlPlane::This => Some(own.unwrap_or(CONTROL_PLANE_SELF).to_string()),
ControlPlane::Other(name) => Some(name.clone()),
}
}
pub fn plan(this: &This, target: &Target, asked: Option<&str>, target_control_plane: bool) -> Result<Plan, ApiError> {
let label = target.label;
if let Some(asked) = asked {
check_member_name(asked).map_err(|e| ApiError::invalid(format!("--name: {e:#}")))?;
}
if matches!(target.control_plane, ControlPlane::This) && target.name.is_none() {
return Err(ApiError::failed(format!("{label} is a control plane with no member name; set `name` in its member.toml")));
}
let this_fleet = resolve(this.control_plane, Some(this.name));
let target_fleet = resolve(target.control_plane, target.name);
if target_control_plane && !matches!(target.control_plane, ControlPlane::This) {
if let Some(k) = this_fleet.as_ref().or(target_fleet.as_ref()) {
let at = if this_fleet.is_some() {
format!("this host is in `{k}`'s fleet and {label} isn't its control plane")
} else {
format!("{label} is in `{k}`'s fleet and isn't its control plane")
};
return Err(ApiError::denied(format!(
"--control-plane: {at}; move the control-plane role with `devsandbox machine control-plane <m>` instead"
)));
}
}
match (this_fleet, target_fleet) {
(Some(k), Some(l)) if k != l => Err(ApiError::denied(format!(
"this host is in `{k}`'s fleet, {label} in `{l}`'s: a machine is in one fleet only"
))),
(Some(k), Some(_)) => {
let name = fixed_name(target, asked, &k)?;
not_this(this, label, &name, false)?;
Ok(Plan { name, target_set: None, fleet: Fleet { control_plane: k, joined: Joined::Same } })
}
(Some(k), None) => {
let name = joining_name(target, asked)?;
not_this(this, label, &name, true)?;
new_to(&name, &k, this.known, label, true)?;
let target_set = TargetSet { name: name.clone() };
Ok(Plan { name, target_set: Some(target_set), fleet: Fleet { control_plane: k, joined: Joined::Target } })
}
(None, Some(k)) => {
let name = fixed_name(target, asked, &k)?;
not_this(this, label, &name, false)?;
if this.name == k || target.known.iter().any(|n| n == this.name) {
return Err(ApiError::invalid(format!(
"this host's member name `{}` is already in `{k}`'s fleet: rename this host (`name` in its member.toml)",
this.name
)));
}
Ok(Plan { name, target_set: None, fleet: Fleet { control_plane: k, joined: Joined::This } })
}
(None, None) => {
let name = joining_name(target, asked)?;
not_this(this, label, &name, true)?;
new_to(&name, this.name, this.known, label, true)?;
let control_plane = if target_control_plane { name.clone() } else { this.name.to_string() };
Ok(Plan { target_set: Some(TargetSet { name: name.clone() }), fleet: Fleet { control_plane, joined: Joined::New }, name })
}
}
}
pub struct Side<'a> {
pub name: &'a str,
pub key: PublicKey,
pub stored: &'a Stored,
}
impl Side<'_> {
fn own(&self) -> Own<'_> {
Own { name: self.name, key: self.key, next: None }
}
}
#[derive(Debug, PartialEq, Eq)]
pub enum Logs {
Both(Stored),
Signed,
Exchange,
}
fn no_log(who: &str, control_plane: &str) -> ApiError {
ApiError::failed(format!(
"{who} is in `{control_plane}`'s fleet but holds no membership log (a fleet made before member keys): make the fleet again \
(`devsandbox machine rm` each member, then `machine add`)"
))
}
pub fn logs(fleet: &Fleet, this: &Side, signer: &impl Signer, target: &Side, label: &str) -> Result<Logs, ApiError> {
let both = |next: Stored| -> Result<Logs, ApiError> {
adopt_into(this.stored, &next, &this.own())?;
adopt_into(target.stored, &next, &target.own()).map_err(|e| on(label, e))?;
Ok(Logs::Both(next))
};
match fleet.joined {
Joined::New => {
let invalid = |e: anyhow::Error| ApiError::invalid(format!("{e:#}"));
let mut log = Log::default();
log.append(Op::genesis(this.name, this.key, &fleet.control_plane).map_err(ApiError::internal)?, this.name, signer).map_err(invalid)?;
log.append(Op::Add { name: target.name.to_string(), key: target.key }, this.name, signer).map_err(invalid)?;
both(Stored { log, introductions: vec![] })
}
Joined::Target => {
if this.stored.log.membership().is_none() {
return Err(no_log("this host", &fleet.control_plane));
}
both(sign_into(this.stored, &this.own(), signer, target.name, &target.key)?)
}
Joined::This => {
if target.stored.log.membership().is_none() {
return Err(no_log(label, &fleet.control_plane));
}
can_sign(target.stored, &target.own(), this.name, &this.key).map_err(|e| on(label, e))?;
if this.stored.log.membership().is_some()
&& let fork @ Compare::Fork { .. } = membership::compare(&this.stored.log, target.stored.log.head())
{
return Err(ApiError::denied(format!("{label}: {fork}")));
}
Ok(Logs::Signed)
}
Joined::Same => {
for (who, side) in [("this host", this), (label, target)] {
if side.stored.log.membership().is_none() {
return Err(no_log(who, &fleet.control_plane));
}
}
let here = adopt_into(this.stored, target.stored, &this.own())?;
adopt_into(target.stored, this.stored, &target.own()).map_err(|e| on(label, e))?;
if !lists(&here, target.name, &target.key) {
return Err(ApiError::denied(format!(
"{label} (`{}`, {}) isn't in `{}`'s membership log nor introduced: it isn't this fleet's member",
target.name,
target.key.fingerprint(),
fleet.control_plane
)));
}
Ok(Logs::Exchange)
}
}
}
fn on(label: &str, mut e: ApiError) -> ApiError {
e.message = format!("{label}: {}", e.message);
e
}
fn fixed_name(target: &Target, asked: Option<&str>, k: &str) -> Result<String, ApiError> {
let label = target.label;
let Some(name) = target.name else {
return Err(ApiError::failed(format!("{label} is in `{k}`'s fleet with no member name; set `name` in its member.toml")));
};
match asked {
Some(asked) if asked != name => Err(ApiError::invalid(format!(
"{label} is in `{k}`'s fleet as `{name}`: pass --name {name}, or no --name"
))),
_ => Ok(name.to_string()),
}
}
fn joining_name(target: &Target, asked: Option<&str>) -> Result<String, ApiError> {
match (asked, target.name) {
(Some(asked), _) => Ok(asked.to_string()),
(None, Some(name)) => Ok(name.to_string()),
(None, None) => Err(ApiError::invalid(format!(
"{} has no member name (its hostname isn't a valid one): pass --name",
target.label
))),
}
}
fn not_this(this: &This, label: &str, name: &str, renamable: bool) -> Result<(), ApiError> {
if name != this.name {
return Ok(());
}
let hint = if renamable { "pass --name" } else { "rename one of them (`name` in member.toml)" };
Err(ApiError::invalid(format!("{label}'s member name `{name}` is this host's too: {hint}")))
}
fn new_to(name: &str, k: &str, known: &[String], label: &str, renamable: bool) -> Result<(), ApiError> {
if name != k && !known.iter().any(|n| n == name) {
return Ok(());
}
let hint = if renamable { ": pass --name" } else { "" };
Err(ApiError::invalid(format!("`{name}` ({label}) is already a member of `{k}`'s fleet{hint}")))
}
#[cfg(test)]
mod tests {
use super::*;
fn names(n: &[&str]) -> Vec<String> {
n.iter().map(|s| s.to_string()).collect()
}
fn other(k: &str) -> ControlPlane {
ControlPlane::Other(k.into())
}
fn run(this_control_plane: ControlPlane, target_name: Option<&str>, target_control_plane: ControlPlane, asked: Option<&str>) -> Result<Plan, ApiError> {
run_control_plane(this_control_plane, target_name, target_control_plane, asked, false)
}
fn run_control_plane(this_control_plane: ControlPlane, target_name: Option<&str>, target_control_plane: ControlPlane, asked: Option<&str>, make_control_plane: bool) -> Result<Plan, ApiError> {
let (this_known, target_known) = (names(&["old"]), names(&["peer"]));
let this = This { name: "lap", control_plane: &this_control_plane, known: &this_known };
let target = Target { label: "me@box", name: target_name, control_plane: &target_control_plane, known: &target_known };
plan(&this, &target, asked, make_control_plane)
}
fn message(r: Result<Plan, ApiError>) -> String {
r.unwrap_err().message
}
#[test]
fn the_machine_joins_this_hosts_fleet() {
for (control_plane, k) in [(ControlPlane::This, "lap"), (other("k"), "k")] {
let p = run(control_plane, Some("box"), ControlPlane::None, None).unwrap();
assert_eq!(
p,
Plan {
name: "box".into(),
target_set: Some(TargetSet { name: "box".into() }),
fleet: Fleet { control_plane: k.into(), joined: Joined::Target },
}
);
}
for own in [Some("box"), None] {
let p = run(other("k"), own, ControlPlane::None, Some("b2")).unwrap();
assert_eq!(p.name, "b2");
assert_eq!(p.target_set, Some(TargetSet { name: "b2".into() }));
}
let p = run(other("k"), Some("box"), ControlPlane::None, Some("box")).unwrap();
assert_eq!(p.target_set.unwrap().name, "box", "its own name, pinned");
}
#[test]
fn this_host_joins_the_machines_fleet() {
let p = run(ControlPlane::None, Some("box"), ControlPlane::This, None).unwrap();
assert_eq!(p, Plan { name: "box".into(), target_set: None, fleet: Fleet { control_plane: "box".into(), joined: Joined::This } });
let p = run(ControlPlane::None, Some("box"), other("k"), Some("box")).unwrap();
assert_eq!((p.target_set, p.fleet), (None, Fleet { control_plane: "k".into(), joined: Joined::This }));
}
#[test]
fn neither_in_a_fleet_makes_one_of_two() {
let p = run(ControlPlane::None, Some("box"), ControlPlane::None, None).unwrap();
assert_eq!(
p,
Plan {
name: "box".into(),
target_set: Some(TargetSet { name: "box".into() }),
fleet: Fleet { control_plane: "lap".into(), joined: Joined::New },
}
);
assert_eq!(run(ControlPlane::None, None, ControlPlane::None, Some("b")).unwrap().target_set.unwrap().name, "b");
}
#[test]
fn control_plane_makes_the_machine_the_control_plane_of_a_new_fleet() {
let p = run_control_plane(ControlPlane::None, Some("box"), ControlPlane::None, None, true).unwrap();
assert_eq!(
p,
Plan {
name: "box".into(),
target_set: Some(TargetSet { name: "box".into() }),
fleet: Fleet { control_plane: "box".into(), joined: Joined::New },
}
);
let p = run_control_plane(ControlPlane::None, None, ControlPlane::None, Some("srv"), true).unwrap();
assert_eq!((p.target_set.unwrap().name, p.fleet.control_plane), ("srv".into(), "srv".into()));
assert!(message(run_control_plane(ControlPlane::None, None, ControlPlane::None, None, true)).ends_with("pass --name"));
assert!(message(run_control_plane(ControlPlane::None, Some("lap"), ControlPlane::None, None, true)).contains("`lap` is this host's too"));
assert!(message(run_control_plane(ControlPlane::None, Some("old"), ControlPlane::None, None, true)).contains("`old` (me@box) is already a member"));
}
#[test]
fn control_plane_in_a_fleet_needs_the_machine_to_be_its_control_plane() {
for (this, target, m) in [
(ControlPlane::This, ControlPlane::None, "--control-plane: this host is in `lap`'s fleet and me@box isn't its control plane"),
(other("k"), ControlPlane::None, "--control-plane: this host is in `k`'s fleet and me@box isn't its control plane"),
(other("k"), other("k"), "--control-plane: this host is in `k`'s fleet and me@box isn't its control plane"),
(ControlPlane::None, other("k"), "--control-plane: me@box is in `k`'s fleet and isn't its control plane"),
] {
let e = run_control_plane(this, Some("box"), target, None, true).unwrap_err();
assert_eq!(e.code, "denied");
assert_eq!(e.message, format!("{m}; move the control-plane role with `devsandbox machine control-plane <m>` instead"));
}
for this in [ControlPlane::None, other("box")] {
assert_eq!(run_control_plane(this.clone(), Some("box"), ControlPlane::This, None, true).unwrap(), run(this, Some("box"), ControlPlane::This, None).unwrap());
}
assert_eq!(run_control_plane(other("k"), Some("box"), ControlPlane::This, None, true).unwrap_err().message, message(run(other("k"), Some("box"), ControlPlane::This, None)));
}
#[test]
fn the_same_control_plane_is_an_extra_link() {
for (this, target) in [(other("k"), other("k")), (ControlPlane::This, other("lap")), (other("box"), ControlPlane::This)] {
let p = run(this, Some("box"), target, None).unwrap();
assert_eq!((p.target_set, p.fleet.joined), (None, Joined::Same), "nothing pinned");
}
}
#[test]
fn two_control_planes_are_refused_naming_both() {
let e = run(other("k"), Some("box"), other("l"), None).unwrap_err();
assert_eq!(e.code, "denied");
assert_eq!(e.message, "this host is in `k`'s fleet, me@box in `l`'s: a machine is in one fleet only");
assert!(message(run(ControlPlane::This, Some("box"), ControlPlane::This, None)).contains("`lap`'s fleet, me@box in `box`'s"));
}
#[test]
fn names_are_checked() {
let m = message(run(ControlPlane::None, Some("box"), other("k"), Some("b2")));
assert_eq!(m, "me@box is in `k`'s fleet as `box`: pass --name box, or no --name");
assert!(message(run(other("k"), Some("box"), other("k"), Some("b2"))).contains("as `box`"));
assert!(message(run(ControlPlane::None, None, other("k"), None)).contains("no member name"));
assert!(message(run(ControlPlane::None, None, ControlPlane::This, None)).contains("no member name"));
assert!(message(run(ControlPlane::None, None, ControlPlane::None, None)).ends_with("pass --name"));
assert_eq!(run(ControlPlane::None, Some("box"), ControlPlane::None, Some("self")).unwrap_err().code, "invalid");
assert_eq!(run(ControlPlane::None, Some("box"), ControlPlane::None, Some("a/b")).unwrap_err().code, "invalid");
for (this, target) in [(ControlPlane::None, ControlPlane::None), (ControlPlane::This, ControlPlane::None), (ControlPlane::None, other("k")), (other("k"), other("k"))] {
assert!(message(run(this, Some("lap"), target, None)).contains("`lap` is this host's too"));
}
for (this, known) in [(ControlPlane::None, "old"), (other("k"), "old"), (other("k"), "k")] {
let m = message(run(this, Some(known), ControlPlane::None, None));
assert!(m.contains(&format!("`{known}` (me@box) is already a member")) && m.ends_with("pass --name"), "{m}");
}
assert!(run(ControlPlane::None, Some("box"), ControlPlane::None, Some("old")).is_err(), "--name too");
let this_known = names(&["old"]);
let target_known = names(&["peer", "lap"]);
let this = This { name: "lap", control_plane: &ControlPlane::None, known: &this_known };
let target = Target { label: "me@box", name: Some("box"), control_plane: &ControlPlane::This, known: &target_known };
assert!(plan(&this, &target, None, false).unwrap_err().message.starts_with("this host's member name `lap` is already in `box`'s fleet"));
let this = This { name: "k", control_plane: &ControlPlane::None, known: &this_known };
let target = Target { label: "me@box", name: Some("box"), control_plane: &other("k"), known: &target_known };
assert!(plan(&this, &target, None, false).is_err(), "this host named as the control plane");
}
mod logs {
use super::super::*;
use crate::serve::fleet::identity::SigningKey;
use crate::serve::fleet::membership_api::test_support::fleet as made;
fn key(n: u8) -> SigningKey {
SigningKey::from_seed([n; 32])
}
fn pk(n: u8) -> PublicKey {
key(n).public_key()
}
fn capped(name: &str, n: u8) -> Stored {
let s = made("k", &key(7), "k", &[("m", pk(8))]);
sign_into(&s, &Own { name: "m", key: pk(8), next: None }, &key(8), name, &pk(n)).unwrap()
}
fn f(control_plane: &str, joined: Joined) -> Fleet {
Fleet { control_plane: control_plane.into(), joined }
}
fn run(fleet: Fleet, this: &Stored, target: &Stored) -> Result<Logs, ApiError> {
let this_side = Side { name: "lap", key: pk(1), stored: this };
let target_side = Side { name: "box", key: pk(2), stored: target };
logs(&fleet, &this_side, &key(1), &target_side, "me@box")
}
fn both(r: Result<Logs, ApiError>) -> Stored {
match r.unwrap() {
Logs::Both(s) => s,
other => panic!("{other:?}"),
}
}
#[test]
fn a_new_fleet_is_signed_here_whichever_end_is_the_control_plane() {
let none = Stored::default();
for control_plane in ["lap", "box"] {
let s = both(run(f(control_plane, Joined::New), &none, &none));
let m = s.log.membership().unwrap();
assert_eq!((&m.authority, m.control_plane.as_str(), m.head.len), (&("lap".to_string(), pk(1)), control_plane, 2));
assert_eq!(m.key_of("box"), Some(pk(2)));
}
let stale = made("x", &key(9), "x", &[("lap", pk(1)), ("box", pk(2))]);
assert_eq!(run(f("lap", Joined::New), &stale, &none).unwrap_err().code, "denied");
let e = run(f("lap", Joined::New), &none, &stale).unwrap_err();
assert!(e.code == "denied" && e.message.starts_with("me@box: "), "{}", e.message);
}
#[test]
fn the_machine_joining_is_added_or_introduced_here() {
let none = Stored::default();
let ours = made("lap", &key(1), "lap", &[]);
let s = both(run(f("lap", Joined::Target), &ours, &none));
assert_eq!((s.log.head().len, s.log.membership().unwrap().key_of("box")), (2, Some(pk(2))));
let ours = made("k", &key(7), "k", &[("lap", pk(1))]);
let s = both(run(f("k", Joined::Target), &ours, &none));
assert_eq!(s.log, ours.log);
assert_eq!((s.introductions[0].name.as_str(), s.introductions[0].introducer.as_str()), ("box", "lap"));
assert_eq!(run(f("k", Joined::Target), &capped("lap", 1), &none).unwrap_err().code, "denied");
let e = run(f("k", Joined::Target), &none, &none).unwrap_err();
assert!(e.code == "failed" && e.message.contains("no membership log"), "{}", e.message);
}
#[test]
fn this_host_joining_is_checked_against_the_machines_log() {
let none = Stored::default();
let theirs = made("box", &key(2), "box", &[]);
assert_eq!(run(f("box", Joined::This), &none, &theirs), Ok(Logs::Signed));
let e = run(f("box", Joined::This), &none, &none).unwrap_err();
assert!(e.code == "failed" && e.message.starts_with("me@box is in `box`'s fleet but holds no membership log"), "{}", e.message);
let e = run(f("k", Joined::This), &none, &capped("box", 2)).unwrap_err();
assert!(e.code == "denied" && e.message.starts_with("me@box: "), "{}", e.message);
let taken = made("box", &key(2), "box", &[("lap", pk(5))]);
assert_eq!(run(f("box", Joined::This), &none, &taken).unwrap_err().code, "invalid");
let stale = made("x", &key(9), "x", &[("lap", pk(1))]);
assert_eq!(run(f("box", Joined::This), &stale, &theirs).unwrap_err().code, "denied");
}
#[test]
fn an_extra_link_exchanges_logs_both_known() {
let s = made("lap", &key(1), "lap", &[("box", pk(2))]);
assert_eq!(run(f("lap", Joined::Same), &s, &s), Ok(Logs::Exchange));
let longer = sign_into(&s, &Own { name: "lap", key: pk(1), next: None }, &key(1), "pi", &pk(3)).unwrap();
assert_eq!(run(f("lap", Joined::Same), &s, &longer), Ok(Logs::Exchange));
assert_eq!(run(f("lap", Joined::Same), &longer, &s), Ok(Logs::Exchange));
let other = made("lap", &key(1), "lap", &[("box", pk(2))]);
let e = run(f("lap", Joined::Same), &s, &other).unwrap_err();
assert!(e.code == "denied" && e.message.contains("another fleet"), "{}", e.message);
let fork = sign_into(&s, &Own { name: "lap", key: pk(1), next: None }, &key(1), "pi", &pk(4)).unwrap();
assert_eq!(run(f("lap", Joined::Same), &longer, &fork).unwrap_err().code, "denied");
let alone = made("lap", &key(1), "lap", &[]);
assert_eq!(run(f("lap", Joined::Same), &alone, &alone).unwrap_err().code, "denied");
let e = run(f("lap", Joined::Same), &Stored::default(), &s).unwrap_err();
assert!(e.code == "failed" && e.message.starts_with("this host is in"), "{}", e.message);
}
}
#[test]
fn the_wire() {
let f = Fleet { control_plane: "k".into(), joined: Joined::Target };
assert_eq!(serde_json::to_value(&f).unwrap(), serde_json::json!({"control_plane": "k", "joined": "target"}));
}
}