gunnar-server 3.0.0

The gunnar git server: SSH and smart-HTTP git transports and the gRPC control plane. The appliance half, extracted from gunnar-cli so the server code lives in its own crate.
Documentation
//! **DEV-NARRATE: this box's boot log, sealed, to the installer — before the
//! link exists** (feature `dev-narrate` ONLY; Skidbladnir `nordisk-narrate`,
//! owner 2026-09-30).
//!
//! The installer reads a box's log through the mutual-TLS log door over its
//! WireGuard link ([`crate::installer_listener`]); whatever this box says
//! before that link is up — and everything a box that never gets that far
//! says — reached nobody but the console. In a DEV build this module streams
//! the SAME log ([`crate::installer_listener::BOOT_LOG`]: the ESP's
//! `BOOTLOG.TXT` with the install phase and korp-start, then every line of
//! this process, cleaned by the same redaction) as sealed UDP datagrams on
//! udp/[`nordisk_narrate::PORT`], to the address an authenticated installer
//! hello came from, from line 0 — and adds the kernel's own records
//! ([`nordisk_narrate::kmsg`]), which no log carried past the hand-off's tail.
//!
//! Called first thing in `main` (gunnar-server and gunnar-front), right after
//! the boot log's capture. The socket binds `0.0.0.0` at once — before any NIC
//! has an address — so the first hello that CAN arrive is answered and the
//! whole buffer is flushed. Nothing is ever fatal: this is diagnostics.

use std::net::SocketAddr;
use std::path::{Path, PathBuf};
use std::time::Duration;

fn console(line: &str) {
    gunnar_common::logqueue::console_write(&format!("{line}\n"));
}

fn data_root() -> PathBuf {
    std::env::var("GUNNAR_DATA_ROOT")
        .ok()
        .filter(|v| !v.trim().is_empty())
        .map(PathBuf::from)
        .unwrap_or_else(|| PathBuf::from("/data"))
}

/// **Narrate, as `product`**, on its own thread; returns at once. The shared
/// start (Skidbladnir `nordisk_narrate::boxside`, the same call holger makes):
/// korp-start's RAM hand-off when the ESP was unreadable, the kernel follower,
/// the seed's installer key and the sender over [`crate::installer_listener::BOOT_LOG`].
pub fn start(product: &'static str) {
    nordisk_narrate::boxside::start(
        &crate::installer_listener::BOOT_LOG,
        data_root(),
        product,
        console,
    );
}

/// [`start`]'s narrating half over this process's boot log (the tests' door).
pub fn start_at(
    root: &Path,
    product: &str,
    bind: SocketAddr,
    wait: Duration,
) -> Result<SocketAddr, String> {
    nordisk_narrate::boxside::start_at(
        &crate::installer_listener::BOOT_LOG,
        root,
        product,
        bind,
        wait,
    )
}

#[cfg(test)]
mod tests {
    use super::*;
    use nordisk_narrate::seedkey;
    use std::net::UdpSocket;

    /// **A DEV box answers a knock with a WHO sealed to the SEED's installer
    /// key** — and only that key opens it; a data root with no seed narrates
    /// nothing and says why.
    #[test]
    fn a_dev_box_narrates_to_the_installer_key_its_seed_names() {
        let installer = nordisk_narrate::wire::generate();
        let root = std::env::temp_dir().join(format!("gunnar-dev-narrate-{}", std::process::id()));
        std::fs::create_dir_all(root.join("pairing")).unwrap();
        std::fs::write(
            root.join(seedkey::SEED_REL),
            format!("estate = \"gunnar\"\nkind = \"gunnar\"\n[installer_link]\ninstaller_pubkey = \"{}\"\nvm_private = \"broken\"\n", seedkey::b64_of_key(installer.public_key().as_bytes())),
        )
        .unwrap();
        crate::installer_listener::BOOT_LOG.push("gunnar: STAGE exec +0ms dev-narrate test");
        let at = start_at(
            &root,
            "gunnar-server",
            "127.0.0.1:0".parse().unwrap(),
            Duration::from_secs(1),
        )
        .unwrap();
        let probe = UdpSocket::bind("127.0.0.1:0").unwrap();
        probe
            .set_read_timeout(Some(Duration::from_secs(2)))
            .unwrap();
        probe
            .send_to(&nordisk_narrate::wire::knock([5; 16]), at)
            .unwrap();
        let mut buf = [0u8; 2048];
        let (n, _) = probe.recv_from(&mut buf).unwrap();
        let (_, up) = nordisk_narrate::wire::open_up(&installer, &buf[..n])
            .expect("sealed to the seed's installer key");
        let nordisk_narrate::wire::Up::Who { id, echo } = up else {
            panic!("{up:?}")
        };
        assert_eq!((id.product.as_str(), echo), ("gunnar-server", [5; 16]));
        assert!(
            nordisk_narrate::wire::open_up(&nordisk_narrate::wire::generate(), &buf[..n]).is_err(),
            "another key opens it"
        );
        let none =
            std::env::temp_dir().join(format!("gunnar-dev-narrate-none-{}", std::process::id()));
        let e = start_at(
            &none,
            "gunnar-server",
            "127.0.0.1:0".parse().unwrap(),
            Duration::from_millis(10),
        )
        .unwrap_err();
        assert!(e.contains("no seed at"), "{e}");
        let _ = std::fs::remove_dir_all(&root);
    }
}