//! The hub end to end: one-time links, pairing, roles enforced by the hub, secrets refused (P3). use lune_core::hub::{self, HubLink}; use lune_core::vault::{self, Vault}; use lune_core::{remote, secrets, state}; use std::collections::HashSet; use std::io::{BufRead, BufReader}; use std::process::{Child, Command, Stdio}; struct HubProcess(Child); impl Drop for HubProcess { fn drop(&mut self) { let _ = self.0.kill(); } } fn start_hub(data: &std::path::Path) -> (HubProcess, String, String) { let port = std::net::TcpListener::bind("127.0.0.1:0").unwrap().local_addr().unwrap().port(); let url = format!("http://127.0.0.1:{port}"); let mut child = Command::new(env!("CARGO_BIN_EXE_lunehub")) .env("LUNE_HUB_DATA", data) .env("LUNE_HUB_URL", &url) .env("LUNE_HUB_LISTEN", format!("127.0.0.1:{port}")) .env("LUNE_OWNER_NAME", "Sen") .stdout(Stdio::piped()) .spawn() .unwrap(); let mut link = String::new(); for line in BufReader::new(child.stdout.take().unwrap()).lines() { let line = line.unwrap(); if let Some(i) = line.find("http").filter(|_| line.contains("/join/")) { link = line[i..].split_whitespace().next().unwrap().to_string(); } if line.contains("listening") { break; } } (HubProcess(child), url, link) } fn as_device(url: &str, w: &hub::Welcome) -> HubLink { let link = HubLink { url: url.into(), device_id: w.device_id.clone(), key: w.key.clone() }; remote::set_hub(link.clone()); link } #[test] fn hub_lifecycle() { let tmp = tempfile::TempDir::new().unwrap(); let (_hub, url, owner_link) = start_hub(&tmp.path().join("data")); // The owner link works once. let (_, sen) = hub::join(&owner_link, "Codex").unwrap(); assert_eq!(sen.person, "Sen"); assert!(hub::join(&owner_link, "Again").is_err()); let codex = as_device(&url, &sen); let devops = codex.create_vault("DevOps").unwrap(); // Pairing: a code works once, and the new device has its own key. let code = codex.pair().unwrap(); let hermes_w = hub::login(&url, &code, "Hermes").unwrap(); assert!(hub::login(&url, &code, "Again").is_err()); assert_ne!(hermes_w.key, sen.key); // Codex shares a note; Hermes receives it labeled Codex. as_device(&url, &sen); let (cfg, _, err) = vault::add("DevOps", &tmp.path().join("codex"), &devops.git_url, "Codex").unwrap(); assert_eq!(err, None); let a = Vault::open(&cfg, "Codex").unwrap(); std::fs::write(a.root.join("vm-list.md"), "web-1\n").unwrap(); a.save_changes(None, &HashSet::new()).unwrap(); a.sync().unwrap(); let hermes = as_device(&url, &hermes_w); assert_eq!(hermes.me().unwrap().vaults[0].role, "manage"); let (cfg_h, _, _) = vault::add("DevOps", &tmp.path().join("hermes"), &devops.git_url, "Hermes").unwrap(); let h = Vault::open(&cfg_h, "Hermes").unwrap(); assert_eq!(std::fs::read(h.root.join("vm-list.md")).unwrap(), b"web-1\n"); // Bob has Read: the hub refuses his changes even from a client that ignores its role. as_device(&url, &sen); let bob_link = codex.invite("Bob", &[("DevOps".into(), "read".into())]).unwrap(); let (_, bob_w) = hub::join(&bob_link, "Bob pc").unwrap(); as_device(&url, &bob_w); let (cfg_b, _, _) = vault::add("DevOps", &tmp.path().join("bob"), &devops.git_url, "Bob pc").unwrap(); let b = Vault::open(&cfg_b, "Bob pc").unwrap(); std::fs::write(b.root.join("bob.md"), "bob\n").unwrap(); b.save_changes(None, &HashSet::new()).unwrap(); assert!(b.sync().is_err(), "read access must not share"); state::write_role(&b.git_dir, "read").unwrap(); assert!(b.sync().unwrap().read_only); // A line this device calls "not a secret" is still refused by the hub until the hub hears it too. as_device(&url, &sen); std::fs::write(a.root.join("aws.md"), "aws_access_key_id = AKIAIOSFODNN7EXAMPLE\n").unwrap(); let hit = a.secret_holds().unwrap()[0].hits[0].clone(); secrets::allow(&a.git_dir, &hit, "aws.md").unwrap(); a.save_changes(None, &HashSet::new()).unwrap(); let err = format!("{:#}", a.sync().unwrap_err()); assert!(err.contains("aws.md line 1: possible AWS access key"), "{err}"); codex.not_a_secret(devops.id, &hit.line_hash).unwrap(); a.sync().unwrap(); let log: Vec = codex.log(50).unwrap().into_iter().map(|e| e.action).collect(); for action in ["joined", "device added", "vault created", "invite created", "change refused", "not a secret"] { assert!(log.iter().any(|a| a == action), "missing {action} in {log:?}"); } }