mod common;
use std::net::{IpAddr, Ipv4Addr, TcpListener};
use std::path::PathBuf;
use std::sync::Arc;
use std::time::{Duration, Instant};
use axum::http::StatusCode;
use second_brain_api::AppState;
use second_brain_api::bootstrap::Bootstrap;
use second_brain_api::clock_gate::ClockGate;
use second_brain_api::device_access::DeviceAccess;
use second_brain_api::pairing::{Pairing, PairingConfig, advertise_route};
use second_brain_api::routes;
use second_brain_core::keys::{DeviceKeys, DeviceRecord, ensure_device_keys};
use second_brain_core::sqlite_store::SqliteStore;
use second_brain_sync::authorized_keys::granted;
use second_brain_sync::dial::{Route, dials};
use serde_json::{Value, json};
use tempfile::TempDir;
use tower::ServiceExt;
use common::{json_body, post_json, state_in};
const FOREIGN: &str = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIForeignKeyOfTheUser me@laptop";
const WAIT: Duration = Duration::from_secs(30);
struct Daemon {
app: axum::Router,
host_key: String,
state: Arc<AppState>,
dir: TempDir,
_sshd: Option<TcpListener>,
}
impl Daemon {
fn new(id: &str, sshd: bool) -> Self {
let dir = tempfile::tempdir().unwrap();
let store = SqliteStore::in_memory(id.to_string()).unwrap();
ensure_device_keys(&store, dir.path()).unwrap();
let host_key = DeviceKeys::generate().unwrap().ssh_public_key();
let key_dir = dir.path().join("etc-ssh");
std::fs::create_dir(&key_dir).unwrap();
let listener = sshd.then(|| {
std::fs::write(
key_dir.join("ssh_host_ed25519_key.pub"),
format!("{host_key} root@{id}\n"),
)
.unwrap();
TcpListener::bind("127.0.0.1:0").unwrap()
});
let config = PairingConfig {
listen_ip: IpAddr::V4(Ipv4Addr::LOCALHOST),
port: 0,
ssh_host_key_dir: key_dir,
ssh_port: listener
.as_ref()
.map_or(1, |l| l.local_addr().unwrap().port()),
discovery: None,
};
let ssh_dir = dir.path().join("home-ssh");
std::fs::create_dir(&ssh_dir).unwrap();
std::fs::write(ssh_dir.join("authorized_keys"), format!("{FOREIGN}\n")).unwrap();
let base = Arc::into_inner(state_in(store, dir.path(), ClockGate::open_for_tests()))
.expect("a new state has one owner");
let state = Arc::new(AppState {
pairing: Arc::new(Pairing::new(config)),
device_access: Arc::new(DeviceAccess::with_authorized_keys(
ssh_dir.join("authorized_keys"),
PathBuf::from("/opt/second brain/sb"),
)),
bootstrap: Arc::new(Bootstrap::default()),
..base
});
Self {
app: routes::router().with_state(state.clone()),
host_key,
state,
dir,
_sshd: listener,
}
}
async fn call(&self, request: axum::http::Request<axum::body::Body>) -> (StatusCode, Value) {
let response = self.app.clone().oneshot(request).await.unwrap();
let status = response.status();
(status, json_body(response).await)
}
fn id(&self) -> String {
self.state.sqlite.machine_id().to_string()
}
fn authorized_keys(&self) -> String {
std::fs::read_to_string(self.dir.path().join("home-ssh/authorized_keys")).unwrap()
}
fn granted(&self) -> Vec<String> {
granted(&self.dir.path().join("home-ssh/authorized_keys")).unwrap()
}
fn known_hosts(&self) -> String {
std::fs::read_to_string(self.dir.path().join("known_hosts")).unwrap_or_default()
}
fn row(&self, machine_id: &str) -> DeviceRecord {
self.state
.sqlite
.device_record(machine_id)
.unwrap()
.unwrap()
.current
.record
}
async fn hello_to(&self, peer: &Daemon) {
let hello = second_brain_api::sync_hello::hello_for(
&self.state.sqlite,
&self.state.clock,
self.dir.path(),
&peer.id(),
None,
)
.unwrap();
let response = peer
.app
.clone()
.oneshot(post_json("/v1/sync/accept-hello", json!({"hello": hello})))
.await
.unwrap();
assert!(response.status().is_success(), "{}", response.status());
}
fn sshd_up(&self) -> (Pairing, TcpListener, String) {
let host_key = DeviceKeys::generate().unwrap().ssh_public_key();
let key_dir = self.dir.path().join("etc-ssh");
std::fs::write(
key_dir.join("ssh_host_ed25519_key.pub"),
format!("{host_key} root@{}\n", self.id()),
)
.unwrap();
let listener = TcpListener::bind("127.0.0.1:0").unwrap();
let pairing = Pairing::new(PairingConfig {
listen_ip: IpAddr::V4(Ipv4Addr::LOCALHOST),
port: 0,
ssh_host_key_dir: key_dir,
ssh_port: listener.local_addr().unwrap().port(),
discovery: None,
});
(pairing, listener, host_key)
}
fn dialled(&self) -> Vec<(String, Vec<String>)> {
dials(&self.state.sqlite, self.dir.path())
.unwrap()
.into_iter()
.map(|dial| (dial.key, dial.routes.iter().map(Route::describe).collect()))
.collect()
}
async fn pair(&self, joiner: &Daemon) {
let (status, started) = self.call(post_json("/v1/pair/start", json!({}))).await;
assert_eq!(status, StatusCode::OK, "{started}");
let code = started["code"].as_str().unwrap().to_string();
let port = started["port"].as_u64().unwrap();
let (status, joined) = joiner
.call(post_json(
"/v1/pair/join",
json!({"code": code, "host": format!("127.0.0.1:{port}")}),
))
.await;
assert_eq!(status, StatusCode::OK, "{joined}");
let start = Instant::now();
loop {
let (_, status) = self.call(common::get("/v1/pair/status")).await;
if status["state"] == "paired" {
break;
}
assert!(start.elapsed() < WAIT, "pairing never finished: {status}");
tokio::time::sleep(Duration::from_millis(20)).await;
}
self.call(post_json("/v1/pair/close", json!({}))).await;
}
}
fn forced_lines(text: &str) -> Vec<&str> {
text.lines()
.filter(|line| line.contains("second-brain:"))
.collect()
}
#[tokio::test(flavor = "multi_thread")]
async fn pairing_grants_exactly_one_restricted_line_each_way_and_dials_by_machine_id() {
let desk = Daemon::new("desk", true);
let laptop = Daemon::new("laptop", true);
desk.pair(&laptop).await;
let lines = desk.authorized_keys();
assert_eq!(lines.lines().next(), Some(FOREIGN), "{lines}");
let forced = forced_lines(&lines);
assert_eq!(forced.len(), 1, "{lines}");
assert!(
forced[0].starts_with(
"command=\"'/opt/second brain/sb' sync-serve --machine laptop\",restrict ssh-ed25519 "
),
"{lines}"
);
assert!(forced[0].ends_with(" second-brain:laptop"), "{lines}");
assert_eq!(laptop.granted(), vec![desk.id()]);
assert!(
desk.known_hosts()
.lines()
.any(|l| l == format!("sb-laptop {}", laptop.host_key)),
"{}",
desk.known_hosts()
);
assert!(
laptop
.known_hosts()
.lines()
.any(|l| l == format!("sb-desk {}", desk.host_key)),
"{}",
laptop.known_hosts()
);
let login = std::env::var("USER").unwrap_or_default();
let dialled = laptop.dialled();
assert_eq!(dialled.len(), 1, "{dialled:?}");
assert_eq!(dialled[0].0, "desk");
let port = desk._sshd.as_ref().unwrap().local_addr().unwrap().port();
assert_eq!(
dialled[0].1.first(),
Some(&format!("{login}@127.0.0.1:{port}"))
);
assert!(
desk.state.device_access.take_kick(),
"a pairing starts a sync"
);
assert!(laptop.state.device_access.take_kick());
desk.state
.device_access
.reconcile(&desk.state.sqlite, desk.dir.path())
.unwrap();
assert_eq!(
desk.authorized_keys(),
lines,
"a second sweep changes nothing"
);
}
#[tokio::test(flavor = "multi_thread")]
async fn an_empty_device_waits_for_its_bootstrap_and_one_with_data_does_not() {
let desk = Daemon::new("desk", true);
let laptop = Daemon::new("laptop", false);
desk.pair(&laptop).await;
assert!(laptop.state.bootstrap.is_pending());
assert!(!desk.state.bootstrap.is_pending());
let (status, body) = laptop.call(post_json("/v1/ingest", json!({}))).await;
assert_eq!(status, StatusCode::OK, "{body}");
assert_eq!(body["deferred"], true, "{body}");
let used = Daemon::new("used", false);
let (status, body) = used
.call(post_json(
"/v1/remember",
json!({"content": "a note written before pairing", "memory_type": "semantic"}),
))
.await;
assert!(status.is_success(), "{body}");
desk.pair(&used).await;
assert!(!used.state.bootstrap.is_pending());
}
#[tokio::test(flavor = "multi_thread")]
async fn a_removed_device_loses_its_line_on_the_remover_and_on_a_peer_after_a_handshake() {
let desk = Daemon::new("desk", true);
let laptop = Daemon::new("laptop", true);
let pi = Daemon::new("pi", true);
desk.pair(&laptop).await;
desk.pair(&pi).await;
assert_eq!(
pi.granted(),
vec![desk.id(), laptop.id()],
"pi learns laptop from the device list"
);
assert!(
pi.known_hosts().contains("sb-laptop "),
"{}",
pi.known_hosts()
);
let (status, removed) = desk
.call(post_json("/v1/devices/remove", json!({"device": "laptop"})))
.await;
assert_eq!(status, StatusCode::OK, "{removed}");
assert_eq!(desk.granted(), vec![pi.id()]);
assert_eq!(desk.authorized_keys().lines().next(), Some(FOREIGN));
assert!(
!desk.known_hosts().contains("sb-laptop "),
"{}",
desk.known_hosts()
);
assert!(
desk.dialled().iter().all(|(key, _)| key != "laptop"),
"{:?}",
desk.dialled()
);
let hello = second_brain_api::sync_hello::hello_for(
&desk.state.sqlite,
&desk.state.clock,
desk.dir.path(),
&pi.id(),
None,
)
.unwrap();
let response = pi
.app
.clone()
.oneshot(post_json("/v1/sync/accept-hello", json!({"hello": hello})))
.await
.unwrap();
assert!(response.status().is_success(), "{}", response.status());
assert_eq!(pi.granted(), vec![desk.id()]);
assert_eq!(pi.authorized_keys().lines().next(), Some(FOREIGN));
assert!(pi.dialled().iter().all(|(key, _)| key != "laptop"));
}
#[tokio::test(flavor = "multi_thread")]
async fn a_removal_cuts_the_ssh_line_even_when_known_hosts_cannot_be_written() {
let desk = Daemon::new("desk", true);
let laptop = Daemon::new("laptop", true);
desk.pair(&laptop).await;
let known_hosts = desk.dir.path().join("known_hosts");
std::fs::remove_file(&known_hosts).unwrap();
std::fs::create_dir(&known_hosts).unwrap();
let (status, removed) = desk
.call(post_json("/v1/devices/remove", json!({"device": "laptop"})))
.await;
assert_eq!(status, StatusCode::OK, "{removed}");
assert_eq!(desk.granted(), Vec::<String>::new());
}
#[tokio::test(flavor = "multi_thread")]
async fn pairing_completes_on_both_sides_when_authorized_keys_cannot_be_written() {
let desk = Daemon::new("desk", true);
let laptop = Daemon::new("laptop", true);
for daemon in [&desk, &laptop] {
let path = daemon.dir.path().join("home-ssh/authorized_keys");
std::fs::remove_file(&path).unwrap();
std::fs::create_dir(&path).unwrap();
}
desk.pair(&laptop).await;
assert!(laptop.state.bootstrap.is_pending());
assert!(
dials(&laptop.state.sqlite, laptop.dir.path())
.unwrap()
.iter()
.any(|dial| dial.key == desk.id())
);
}
#[tokio::test(flavor = "multi_thread")]
async fn a_failed_join_leaves_the_bootstrap_as_it_was() {
let desk = Daemon::new("desk", true);
let laptop = Daemon::new("laptop", false);
let waiting = Daemon::new("waiting", false);
waiting
.state
.bootstrap
.mark_pending(&waiting.state.sqlite)
.unwrap();
let (status, started) = desk.call(post_json("/v1/pair/start", json!({}))).await;
assert_eq!(status, StatusCode::OK, "{started}");
let port = started["port"].as_u64().unwrap();
let wrong = second_brain_sync::pairing::HostSecrets::generate()
.code()
.to_string();
for joiner in [&laptop, &waiting] {
let (status, body) = joiner
.call(post_json(
"/v1/pair/join",
json!({"code": wrong, "host": format!("127.0.0.1:{port}")}),
))
.await;
assert!(!status.is_success(), "{body}");
}
assert!(!laptop.state.bootstrap.is_pending());
assert!(waiting.state.bootstrap.is_pending());
}
#[tokio::test(flavor = "multi_thread")]
async fn founding_an_identity_ends_a_wait_to_join_one() {
let laptop = Daemon::new("laptop", false);
laptop
.state
.bootstrap
.mark_pending(&laptop.state.sqlite)
.unwrap();
let (status, started) = laptop.call(post_json("/v1/pair/start", json!({}))).await;
assert_eq!(status, StatusCode::OK, "{started}");
assert!(started["founded"].is_object(), "{started}");
assert!(!laptop.state.bootstrap.is_pending());
}
#[tokio::test(flavor = "multi_thread")]
async fn a_device_that_moves_to_another_identity_drops_the_old_ones_host_keys() {
let desk = Daemon::new("desk", true);
let laptop = Daemon::new("laptop", true);
let other = Daemon::new("other", true);
desk.pair(&laptop).await;
let desk_key = desk.host_key.split_whitespace().nth(1).unwrap().to_string();
assert!(
laptop.known_hosts().contains(&desk_key),
"{}",
laptop.known_hosts()
);
other.pair(&laptop).await;
let known_hosts = laptop.known_hosts();
assert!(!known_hosts.contains(&desk_key), "{known_hosts}");
assert!(!known_hosts.contains("sb-desk "), "{known_hosts}");
let other_key = other.host_key.split_whitespace().nth(1).unwrap();
assert!(known_hosts.contains(other_key), "{known_hosts}");
assert_eq!(laptop.granted(), vec![other.id()]);
}
#[tokio::test(flavor = "multi_thread")]
async fn a_joiner_that_cannot_write_known_hosts_still_stores_the_route_and_grants_its_host() {
let desk = Daemon::new("desk", true);
let laptop = Daemon::new("laptop", true);
std::fs::create_dir(laptop.dir.path().join("known_hosts")).unwrap();
desk.pair(&laptop).await;
assert!(
laptop.dialled().iter().any(|(key, _)| *key == desk.id()),
"{:?}",
laptop.dialled()
);
assert_eq!(laptop.granted(), vec![desk.id()]);
}
#[tokio::test(flavor = "multi_thread")]
async fn a_joiner_that_cannot_dial_its_host_does_not_wait_for_a_snapshot() {
let desk = Daemon::new("desk", false);
let laptop = Daemon::new("laptop", true);
laptop
.state
.bootstrap
.mark_pending(&laptop.state.sqlite)
.unwrap();
desk.pair(&laptop).await;
assert!(!laptop.state.bootstrap.is_pending());
assert!(laptop.dialled().iter().all(|(key, _)| *key != desk.id()));
}
#[tokio::test(flavor = "multi_thread")]
async fn a_waiting_device_stops_waiting_when_told_whether_or_not_it_joined() {
let laptop = Daemon::new("laptop", false);
laptop
.state
.bootstrap
.mark_pending(&laptop.state.sqlite)
.unwrap();
let desk = Daemon::new("desk", true);
let joined = Daemon::new("joined", false);
desk.pair(&joined).await;
assert!(joined.state.bootstrap.is_pending());
for device in [&laptop, &joined] {
let (_, health) = device.call(common::get("/health")).await;
assert_eq!(health["bootstrap_pending"], true, "{health}");
let (status, body) = device
.call(post_json("/v1/pair/stop-waiting", json!({})))
.await;
assert_eq!(status, StatusCode::OK, "{body}");
assert_eq!(body["cancelled"], true, "{body}");
assert!(!device.state.bootstrap.is_pending());
let (_, health) = device.call(common::get("/health")).await;
assert!(health.get("bootstrap_pending").is_none(), "{health}");
}
let (_, body) = laptop
.call(post_json("/v1/pair/stop-waiting", json!({})))
.await;
assert_eq!(body["cancelled"], false, "{body}");
}
#[tokio::test(flavor = "multi_thread")]
async fn a_push_from_a_paired_device_counts_as_a_sync_on_the_device_it_dials() {
let desk = Daemon::new("desk", true);
let laptop = Daemon::new("laptop", false);
desk.pair(&laptop).await;
assert!(
second_brain_sync::peers::last_success(desk.dir.path())
.unwrap()
.is_none()
);
let hello = second_brain_api::sync_hello::hello_for(
&laptop.state.sqlite,
&laptop.state.clock,
laptop.dir.path(),
&desk.id(),
None,
)
.unwrap();
let header = second_brain_sync::protocol::SyncHeader {
hello: Some(hello),
..second_brain_sync::protocol::SyncHeader::local()
};
let (status, body) = desk
.call(post_json(
"/v1/sync/import",
json!({"header": header, "lines": []}),
))
.await;
assert_eq!(status, StatusCode::OK, "{body}");
assert!(
second_brain_sync::peers::last_success(desk.dir.path())
.unwrap()
.is_some()
);
}
#[tokio::test(flavor = "multi_thread")]
async fn a_device_paired_through_another_pins_and_dials_the_founder() {
let desk = Daemon::new("desk", true);
let laptop = Daemon::new("laptop", true);
let phone = Daemon::new("phone", true);
desk.pair(&laptop).await;
laptop.pair(&phone).await;
laptop.hello_to(&phone).await;
laptop.hello_to(&desk).await;
let founder = phone.row(&desk.id());
assert_eq!(
founder.ssh_host_key.as_deref(),
Some(desk.host_key.as_str())
);
assert!(
phone
.known_hosts()
.lines()
.any(|l| l == format!("sb-desk {}", desk.host_key)),
"{}",
phone.known_hosts()
);
let login = std::env::var("USER").unwrap_or_default();
let port = desk._sshd.as_ref().unwrap().local_addr().unwrap().port();
let dialled = phone.dialled();
let to_desk = dialled
.iter()
.find(|(key, _)| *key == desk.id())
.unwrap_or_else(|| panic!("phone never dials desk: {dialled:?}"));
assert_eq!(
to_desk.1.first(),
Some(&format!("{login}@127.0.0.1:{port}"))
);
assert!(desk.granted().contains(&phone.id()), "{:?}", desk.granted());
}
#[tokio::test(flavor = "multi_thread")]
async fn a_founder_whose_row_lacks_its_ssh_fills_it_in_on_start_and_peers_take_it() {
let desk = Daemon::new("desk", false);
let laptop = Daemon::new("laptop", true);
desk.pair(&laptop).await;
let before = laptop.row(&desk.id());
assert_eq!(before.ssh_host_key, None);
assert_eq!(before.ssh_login, None);
let (pairing, sshd, host_key) = desk.sshd_up();
advertise_route(&desk.state.sqlite, &pairing, "desk", desk.dir.path());
let own = desk.row(&desk.id());
assert_eq!(own.ssh_host_key.as_deref(), Some(host_key.as_str()));
assert!(own.ssh_login.is_some(), "{own:?}");
assert_eq!(own.ssh_port, Some(sshd.local_addr().unwrap().port()));
assert_eq!(own.version, before.version + 1);
advertise_route(&desk.state.sqlite, &pairing, "desk", desk.dir.path());
assert_eq!(
desk.row(&desk.id()),
own,
"an unchanged sshd signs no new row"
);
desk.hello_to(&laptop).await;
assert_eq!(laptop.row(&desk.id()), own);
assert!(
laptop
.known_hosts()
.lines()
.any(|l| l == format!("sb-desk {host_key}")),
"{}",
laptop.known_hosts()
);
}