Your browser sucks, JavaScript disabled! Gunnar's console needs JavaScript and WebAssembly. Git over SSH on port 2222 does not.

nordisk/gunnar

crates/gunnar-server/tests/become_twin_said_once.rs

//! **L1 (estate-box check 3, release-3 recharge): `Bootstrap.BecomeTwin` on a box that
//! ALREADY is the twin of that primary is said once, at INFO.**
//!
//! The front's pairing door certifies the twin by asking `BecomeTwin` first (idempotent: the
//! same key back) and retries every 5–30 s until the certification takes. After a recharge it
//! retried for minutes, and EVERY answer was logged as `THIS BOX IS NOW A TWIN: it gave up its
//! own log…` plus `its GIT LISTENER is still bound…` at WARN — 7/min on the twin (MEASURED,
//! estate-box 2026-09-30), though nothing changed after the first. The change is said at WARN
//! when it happens; an unchanged answer is an INFO line.

use std::net::SocketAddr;
use std::sync::Arc;
use std::time::{Duration, Instant};

use gunnar_core::ssh_key::PrivateKey;
use gunnar_grpc::{EventBus, ServerConfig, ServiceContext};
use gunnar_proto::bootstrap_client::BootstrapClient;
use gunnar_server::stores::ServerStores;
use gunnar_server::twin;
use gunnar_server::CoreControlPlane;

fn free_addr() -> SocketAddr {
    let l = std::net::TcpListener::bind("127.0.0.1:0").expect("bind");
    l.local_addr().expect("addr")
}

fn wait<T>(what: &str, within: Duration, mut probe: impl FnMut() -> Option<T>) -> T {
    let t0 = Instant::now();
    loop {
        if let Some(v) = probe() {
            return v;
        }
        assert!(t0.elapsed() < within, "{what}: not within {within:?}");
        std::thread::sleep(Duration::from_millis(25));
    }
}

struct Served {
    rt: tokio::runtime::Runtime,
    addr: SocketAddr,
}

/// The fresh box's control plane as `serve` serves it: the real router,
/// authentication REQUIRED (the default), Bootstrap outside the gate.
fn serve(stores: &Arc<ServerStores>) -> Served {
    let plane: Arc<dyn gunnar_grpc::ControlPlane> =
        Arc::new(CoreControlPlane::new(Arc::clone(stores)));
    let ctx = ServiceContext::new(plane, Arc::new(EventBus::new(16)), ServerConfig::default());
    let rt = tokio::runtime::Builder::new_multi_thread()
        .worker_threads(2)
        .enable_all()
        .build()
        .expect("rt");
    let addr = free_addr();
    rt.spawn(async move {
        let _ = gunnar_grpc::router(ctx, || 0).serve(addr).await;
    });
    wait("the box listens", Duration::from_secs(10), || {
        std::net::TcpStream::connect(addr).ok()
    });
    Served { rt, addr }
}

fn with_credential<T>(message: T, credential: Option<String>) -> tonic::Request<T> {
    let mut request = tonic::Request::new(message);
    if let Some(value) = credential {
        request.metadata_mut().insert(
            gunnar_grpc::CREDENTIAL_METADATA_KEY,
            value.parse().expect("a legal header"),
        );
    }
    request
}

impl Served {
    fn client(&self) -> BootstrapClient<tonic::transport::Channel> {
        // A lazy channel spawns onto the runtime it is made in.
        let _in = self.rt.enter();
        let channel = tonic::transport::Channel::from_shared(format!("http://{}", self.addr))
            .expect("url")
            .connect_lazy();
        BootstrapClient::new(channel)
    }

    fn become_twin(
        &self,
        primary: &str,
        credential: Option<String>,
    ) -> Result<gunnar_proto::BecomeTwinResponse, tonic::Status> {
        let mut c = self.client();
        let req = with_credential(
            gunnar_proto::BecomeTwinRequest {
                primary: primary.to_string(),
            },
            credential,
        );
        self.rt
            .block_on(async move { c.become_twin(req).await.map(|r| r.into_inner()) })
    }
}

fn key() -> (PrivateKey, String) {
    let pem = gunnar_core::mint_ed25519().expect("mint");
    let key = PrivateKey::from_openssh(std::str::from_utf8(&pem).unwrap()).expect("parse");
    let line = key.public_key().to_openssh().expect("public line");
    (key, line)
}

fn credential(key: &PrivateKey, method: &str) -> String {
    use std::sync::atomic::{AtomicU64, Ordering};
    static N: AtomicU64 = AtomicU64::new(0);
    let now = twin::now_ms();
    let nonce = format!(
        "bootstrap-credential-test-{}-{}-{now}",
        std::process::id(),
        N.fetch_add(1, Ordering::SeqCst)
    );
    gunnar_grpc::sign_credential_for(key, now, &nonce, method)
        .expect("sign")
        .to_header_value()
}

/// What the installer's door leaves on a box it fed: `admin_pubkey` is the operator.
fn feed(root: &std::path::Path, admin: &str) {
    std::fs::create_dir_all(root.join("pairing")).unwrap();
    let text = format!(
        "estate = \"bootstrap-credential-test\"\nrole = \"twin\"\npeers = []\n\n[me]\nname = \"twin\"\noverlay = \"10.13.0.3\"\n\n\
         [feed]\nadmin_pubkey = \"{admin}\"\n"
    );
    std::fs::write(root.join("pairing/joined.toml"), text).unwrap();
}

fn fresh_fed() -> (tempfile::TempDir, Arc<ServerStores>, PrivateKey) {
    let fd = tempfile::tempdir().unwrap();
    let stores = Arc::new(ServerStores::open(fd.path()).unwrap());
    let (operator, line) = key();
    feed(fd.path(), &line);
    (fd, stores, operator)
}

#[derive(Clone, Default)]
struct Heard(Arc<std::sync::Mutex<Vec<u8>>>);
impl std::io::Write for Heard {
    fn write(&mut self, b: &[u8]) -> std::io::Result<usize> {
        self.0.lock().unwrap().extend_from_slice(b);
        Ok(b.len())
    }
    fn flush(&mut self) -> std::io::Result<()> {
        Ok(())
    }
}

#[test]
fn become_twin_asked_again_by_the_door_is_said_once_at_warn_then_at_info() {
    let heard = Heard::default();
    let h = heard.clone();
    tracing::subscriber::set_global_default(
        tracing_subscriber::fmt()
            .with_writer(move || h.clone())
            .with_ansi(false)
            .finish(),
    )
    .expect("the only subscriber in this binary");
    let (fd, stores, operator) = fresh_fed();
    let served = serve(&stores);
    let first = served
        .become_twin(
            "10.13.0.2:50051",
            Some(credential(&operator, "Bootstrap.BecomeTwin")),
        )
        .expect("the operator makes the twin");
    for _ in 0..5 {
        let again = served
            .become_twin(
                "10.13.0.2:50051",
                Some(credential(&operator, "Bootstrap.BecomeTwin")),
            )
            .expect("asked again: the same answer");
        assert_eq!(
            again.twin_public_key, first.twin_public_key,
            "idempotent: the same key"
        );
    }
    assert!(twin::is_twin_root(fd.path()));
    let said = String::from_utf8(heard.0.lock().unwrap().clone()).unwrap();
    let warns = said
        .lines()
        .filter(|l| l.contains(" WARN ") && l.contains("BecomeTwin"))
        .count();
    let now_a_twin = said
        .lines()
        .filter(|l| l.contains("THIS BOX IS NOW A TWIN"))
        .count();
    assert_eq!(now_a_twin, 1, "the change is said once:\n{said}");
    assert!(
        warns <= 2,
        "the WARNs are the change's own (and the listener's, once): {warns}\n{said}"
    );
    let infos = said
        .lines()
        .filter(|l| l.contains(" INFO ") && l.contains("already the twin of 10.13.0.2:50051"))
        .count();
    assert_eq!(infos, 5, "every unchanged answer is an INFO line:\n{said}");
}