second-brain-cli 0.13.5

Memory layer for AI coding agents: ingest, recall, and sync across machines
mod common;
#[path = "common/devices.rs"]
mod devices;

use std::net::{TcpListener, TcpStream};
use std::os::unix::fs::PermissionsExt;
use std::path::{Path, PathBuf};
use std::process::{Child, Command, Output, Stdio};
use std::time::{Duration, Instant};

use common::Home;
use devices::Device;
use second_brain_api::sync_loop::SSH_KEY_FILE;
use second_brain_core::keys::load_device_keys;
use second_brain_core::store::Store;
use second_brain_sync::authorized_keys::granted;
use second_brain_sync::peers::PEERS_FILE;
use second_brain_sync::protocol::Hello;
use second_brain_sync::ssh::{device_alias, known_hosts_path_in};
use second_brain_sync::ssh_identity::write_identity_file;
use serde_json::json;

const SSHD: &str = "/usr/sbin/sshd";
/// The crew wrapper stubs `ssh` on PATH so tests can't reach real peers;
/// this names a real one for a run that dials only its own sshd.
const REAL_SSH: &str = "SB_TEST_REAL_SSH";
const WAIT: Duration = Duration::from_secs(90);
const NOTE: &str = "the pi reads its config from /etc/brain.toml";
const FOREIGN: &str = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIForeignKeyOfTheUser me@laptop";

/// A user-mode sshd on a free port that trusts only `authorized_keys`.
struct Sshd {
    child: Child,
    port: u16,
    dir: tempfile::TempDir,
}

impl Sshd {
    fn start(authorized_keys: &Path) -> Self {
        let dir = tempfile::tempdir().unwrap();
        let key = dir.path().join("ssh_host_ed25519_key");
        let made = Command::new("ssh-keygen")
            .args(["-q", "-t", "ed25519", "-N", "", "-f"])
            .arg(&key)
            .status()
            .unwrap();
        assert!(made.success());
        let port = TcpListener::bind("127.0.0.1:0")
            .unwrap()
            .local_addr()
            .unwrap()
            .port();
        let config = dir.path().join("sshd_config");
        std::fs::write(
            &config,
            format!(
                "Port {port}\nListenAddress 127.0.0.1\nHostKey {}\nPidFile none\nUsePAM no\n\
                 StrictModes no\nPasswordAuthentication no\nKbdInteractiveAuthentication no\n\
                 PubkeyAuthentication yes\nAuthorizedKeysFile {}\nLogLevel VERBOSE\n",
                key.display(),
                authorized_keys.display()
            ),
        )
        .unwrap();
        let child = Command::new(SSHD)
            .args(["-D", "-e", "-f"])
            .arg(&config)
            .stdin(Stdio::null())
            .stdout(Stdio::null())
            .stderr(Stdio::from(
                std::fs::File::create(dir.path().join("sshd.log")).unwrap(),
            ))
            .spawn()
            .unwrap();
        let start = Instant::now();
        while TcpStream::connect(("127.0.0.1", port)).is_err() {
            assert!(
                start.elapsed() < Duration::from_secs(10),
                "sshd never listened: {}",
                std::fs::read_to_string(dir.path().join("sshd.log")).unwrap_or_default()
            );
            std::thread::sleep(Duration::from_millis(20));
        }
        Self { child, port, dir }
    }

    fn log(&self) -> String {
        std::fs::read_to_string(self.dir.path().join("sshd.log")).unwrap_or_default()
    }
}

impl Drop for Sshd {
    fn drop(&mut self) {
        let _ = self.child.kill();
        let _ = self.child.wait();
    }
}

/// The forced command's `sb`: the binary under test, pointed at the pairing
/// device's temp HOME and daemon, since sshd starts it in the real HOME.
fn wrapper(dir: &Path, home: &Path, base: &str) -> PathBuf {
    let path = dir.join("sb-under-test");
    std::fs::write(
        &path,
        format!(
            "#!/bin/sh\nexport HOME='{}'\nexport SECOND_BRAIN_API='{base}'\nexec '{}' \"$@\"\n",
            home.display(),
            env!("CARGO_BIN_EXE_sb")
        ),
    )
    .unwrap();
    std::fs::set_permissions(&path, std::fs::Permissions::from_mode(0o755)).unwrap();
    path
}

fn wait_for(what: &str, mut done: impl FnMut() -> bool, log: impl Fn() -> String) {
    let start = Instant::now();
    while !done() {
        assert!(start.elapsed() < WAIT, "{what} never happened\n{}", log());
        std::thread::sleep(Duration::from_millis(200));
    }
}

fn write_device_ssh_key(device: &Device, path: &Path) {
    let keys = load_device_keys(device.data_dir()).unwrap().unwrap();
    write_identity_file(path, keys.ssh_signing_key(), "second-brain:test").unwrap();
}

fn files_holding_a_private_key(dir: &Path) -> Vec<PathBuf> {
    let label = ["OPENSSH ", "PRIVATE KEY"].concat();
    let mut found = Vec::new();
    let mut dirs = vec![dir.to_path_buf()];
    while let Some(d) = dirs.pop() {
        for entry in std::fs::read_dir(&d).unwrap().flatten() {
            let path = entry.path();
            if path.is_dir() {
                dirs.push(path);
            } else if std::fs::read(&path)
                .unwrap_or_default()
                .windows(label.len())
                .any(|w| w == label.as_bytes())
            {
                found.push(path);
            }
        }
    }
    found
}

/// Runs a command over ssh as the joined device does, with its own key.
fn ssh_as(joiner: &Device, host: &Device, sshd: &Sshd, command: &[&str]) -> Output {
    let login = std::env::var("USER").unwrap();
    let key_dir = tempfile::tempdir().unwrap();
    let key = key_dir.path().join("id");
    write_device_ssh_key(joiner, &key);
    Command::new("ssh")
        .args(["-p", &sshd.port.to_string(), "-i"])
        .arg(&key)
        .args(["-o", "IdentitiesOnly=yes", "-o", "BatchMode=yes"])
        .arg("-o")
        .arg(format!(
            "UserKnownHostsFile={}",
            known_hosts_path_in(joiner.data_dir()).display()
        ))
        .args(["-o", "StrictHostKeyChecking=yes"])
        .arg("-o")
        .arg(format!(
            "HostKeyAlias={}",
            device_alias(host.state.sqlite.machine_id())
        ))
        .arg(format!("{login}@127.0.0.1"))
        .args(command)
        .output()
        .unwrap()
}

fn hello_naming(machine_id: &str) -> String {
    Hello {
        machine_id: machine_id.to_string(),
        wall_micros: 0,
        synced: false,
        cursor: None,
        held_origin_seq: None,
        keys: None,
        peer_in: None,
        log_floor: 0,
        head: 0,
        applied: None,
        merging: None,
    }
    .to_arg()
}

#[test]
fn two_devices_pair_and_sync_over_ssh_and_a_removed_one_is_refused_at_ssh() {
    let Some(ssh) = std::env::var_os(REAL_SSH).map(PathBuf::from) else {
        eprintln!("skipping: set {REAL_SSH} to a real ssh to run this test");
        return;
    };
    if !Path::new(SSHD).exists() || Command::new("ssh-keygen").arg("-?").output().is_err() {
        eprintln!("skipping: no sshd or ssh-keygen on this host");
        return;
    }
    let bin = tempfile::tempdir().unwrap();
    std::os::unix::fs::symlink(&ssh, bin.path().join("ssh")).unwrap();
    let path = std::env::join_paths(std::iter::once(bin.path().to_path_buf()).chain(
        std::env::split_paths(&std::env::var_os("PATH").unwrap_or_default()),
    ))
    .unwrap();
    // SAFETY: this binary runs this one test, which sets PATH before any
    // thread of its own starts.
    unsafe { std::env::set_var("PATH", path) };
    let _ = tracing_subscriber::fmt().with_test_writer().try_init();
    let scratch = tempfile::tempdir().unwrap();
    let desk_home = Home::new();
    let authorized_keys = desk_home.path().join(".ssh/authorized_keys");
    std::fs::create_dir_all(authorized_keys.parent().unwrap()).unwrap();
    std::fs::write(&authorized_keys, format!("{FOREIGN}\n")).unwrap();
    let sshd = Sshd::start(&authorized_keys);
    let desk = Device::new(
        "desk",
        desk_home,
        Some((sshd.dir.path(), sshd.port)),
        |home, base| wrapper(scratch.path(), home, base),
    );
    let laptop = Device::new("laptop", Home::new(), None, |_, _| PathBuf::from("sb"));

    desk.post(
        "/v1/remember",
        json!({"content": NOTE, "memory_type": "semantic"}),
    );

    let noted = desk.state.sqlite.all_memory_ids().unwrap();
    assert_eq!(noted.len(), 1);

    let started = desk.post("/v1/pair/start", json!({}));
    let code = started["code"].as_str().unwrap().to_string();
    let port = started["port"].as_u64().unwrap();
    let joined = laptop.post(
        "/v1/pair/join",
        json!({"code": code, "host": format!("127.0.0.1:{port}")}),
    );
    assert_eq!(joined["host"]["machine_id"], "desk", "{joined}");
    assert!(laptop.state.bootstrap.is_pending());
    let deferred = laptop.post("/v1/ingest", json!({}));
    assert_eq!(deferred["deferred"], true, "{deferred}");

    assert_eq!(
        granted(&authorized_keys).unwrap(),
        vec!["laptop".to_string()]
    );
    let text = std::fs::read_to_string(&authorized_keys).unwrap();
    assert!(text.starts_with(&format!("{FOREIGN}\n")), "{text}");

    let legacy = laptop.data_dir().join(SSH_KEY_FILE);
    write_device_ssh_key(&laptop, &legacy);
    laptop.sync_loop().spawn();
    wait_for(
        "the laptop's bootstrap",
        || {
            !laptop.state.bootstrap.is_pending()
                && noted
                    .iter()
                    .all(|id| laptop.state.sqlite.get_memory(*id).unwrap().is_some())
        },
        || sshd.log(),
    );
    assert!(!laptop.data_dir().join(PEERS_FILE).exists());
    assert!(!desk.data_dir().join(PEERS_FILE).exists());
    assert!(!legacy.exists());
    assert_eq!(
        files_holding_a_private_key(laptop.data_dir()),
        Vec::<PathBuf>::new()
    );
    assert_eq!(
        files_holding_a_private_key(desk.data_dir()),
        Vec::<PathBuf>::new()
    );

    let other = ssh_as(
        &laptop,
        &desk,
        &sshd,
        &[
            "sb",
            "sync-export",
            "--after-seq",
            "0",
            "--hello",
            &hello_naming("desk"),
        ],
    );
    assert!(!other.status.success(), "a hello naming another id passed");
    let refused = ssh_as(&laptop, &desk, &sshd, &["sb", "sync-status"]);
    assert!(!refused.status.success(), "sync-serve ran sync-status");
    let whoami = ssh_as(&laptop, &desk, &sshd, &["sb", "whoami"]);
    assert_eq!(
        String::from_utf8_lossy(&whoami.stdout).trim(),
        "desk",
        "{}",
        String::from_utf8_lossy(&whoami.stderr)
    );

    let removed = desk.post("/v1/devices/remove", json!({"device": "laptop"}));
    assert!(removed["access_error"].is_null(), "{removed}");
    assert!(granted(&authorized_keys).unwrap().is_empty());
    let cut = ssh_as(&laptop, &desk, &sshd, &["sb", "whoami"]);
    assert_eq!(cut.status.code(), Some(255), "{cut:?}");
    assert!(
        String::from_utf8_lossy(&cut.stderr).contains("Permission denied"),
        "{}",
        String::from_utf8_lossy(&cut.stderr)
    );
    let listed = desk.get("/v1/devices");
    assert!(
        listed["devices"]
            .as_array()
            .unwrap()
            .iter()
            .any(|d| d["machine_id"] == "laptop" && d["revoked"] == true),
        "{listed}"
    );
}