ci / rust (push) Failing after 6s
Registry-side primitives for the daemon↔registry conversation. No client-facing HTTP yet (that's NAP-215). - `state::Registry`: in-memory store of connected daemons + a session catalog keyed by (daemon_id, session_id). Tracks pending CreateSession / KillSession RPCs so a single daemon connection can multiplex them. - `handler::handle_socket`: per-connection loop that authenticates the Hello, applies snapshot/diff/heartbeat frames, and resolves matching *Result frames. Heartbeat watchdog is a `tokio::time::timeout` around the read side — no separate janitor task. - `server::router_with_timeout`: axum router mounting `WS /daemon`. The timeout knob lets tests verify the 30s drop path in milliseconds. - `token::ensure_token`: first-run bootstrap that generates a 256-bit hex token, prints it once to stderr, and persists it to `~/.config/maestro/registry.toml`. - Outbound `create_session` / `kill_session` exposed as the internal API NAP-215's HTTP handlers will call. Integration tests use a real axum server bound to `:0` and drive it via `tokio-tungstenite`, covering: bad-token reject, snapshot accept, diff apply (add/update/remove), heartbeat timeout dropping the daemon, re-registration replacing the catalog, and a full CreateSession RPC round-trip. Closes NAP-213.
101 lines
3.1 KiB
Rust
101 lines
3.1 KiB
Rust
//! First-run bootstrap of the shared registry/daemon token.
|
|
//!
|
|
//! On startup the registry looks for `~/.config/maestro/registry.toml`:
|
|
//!
|
|
//! - If present, loads `token` from it.
|
|
//! - If absent, generates a 256-bit random token, prints it once to stderr,
|
|
//! and persists it. Subsequent runs are silent.
|
|
|
|
use std::path::{Path, PathBuf};
|
|
|
|
use rand::RngCore;
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
pub enum TokenError {
|
|
#[error("io: {0}")]
|
|
Io(#[from] std::io::Error),
|
|
#[error("toml decode: {0}")]
|
|
TomlDecode(#[from] toml::de::Error),
|
|
#[error("toml encode: {0}")]
|
|
TomlEncode(#[from] toml::ser::Error),
|
|
#[error("could not determine config directory (XDG_CONFIG_HOME / $HOME unset?)")]
|
|
NoConfigDir,
|
|
}
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
|
pub struct RegistryConfig {
|
|
pub token: String,
|
|
}
|
|
|
|
/// Load or create the registry config file. On creation, prints the generated
|
|
/// token to stderr so the operator can copy it to daemons.
|
|
pub fn ensure_token(config_path: &Path) -> Result<RegistryConfig, TokenError> {
|
|
if let Some(parent) = config_path.parent() {
|
|
std::fs::create_dir_all(parent)?;
|
|
}
|
|
if config_path.exists() {
|
|
let bytes = std::fs::read(config_path)?;
|
|
let cfg: RegistryConfig = toml::from_str(std::str::from_utf8(&bytes).unwrap_or(""))?;
|
|
return Ok(cfg);
|
|
}
|
|
let token = generate_token();
|
|
let cfg = RegistryConfig {
|
|
token: token.clone(),
|
|
};
|
|
let body = toml::to_string_pretty(&cfg)?;
|
|
std::fs::write(config_path, body)?;
|
|
eprintln!(
|
|
"maestro-registry: generated new MAESTRO_TOKEN; deploy this to every daemon:\n {token}"
|
|
);
|
|
Ok(cfg)
|
|
}
|
|
|
|
/// Default config path: `~/.config/maestro/registry.toml`. Falls back to
|
|
/// `TokenError::NoConfigDir` if neither `XDG_CONFIG_HOME` nor `$HOME` are set.
|
|
pub fn default_config_path() -> Result<PathBuf, TokenError> {
|
|
let base = dirs::config_dir().ok_or(TokenError::NoConfigDir)?;
|
|
Ok(base.join("maestro").join("registry.toml"))
|
|
}
|
|
|
|
fn generate_token() -> String {
|
|
let mut bytes = [0u8; 32];
|
|
rand::thread_rng().fill_bytes(&mut bytes);
|
|
hex_encode(&bytes)
|
|
}
|
|
|
|
fn hex_encode(bytes: &[u8]) -> String {
|
|
const HEX: &[u8; 16] = b"0123456789abcdef";
|
|
let mut out = String::with_capacity(bytes.len() * 2);
|
|
for &b in bytes {
|
|
out.push(HEX[(b >> 4) as usize] as char);
|
|
out.push(HEX[(b & 0xf) as usize] as char);
|
|
}
|
|
out
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn first_run_generates_persistent_token() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let path = dir.path().join("registry.toml");
|
|
let cfg = ensure_token(&path).unwrap();
|
|
assert_eq!(cfg.token.len(), 64); // 32 bytes hex-encoded
|
|
|
|
let again = ensure_token(&path).unwrap();
|
|
assert_eq!(cfg, again, "second call must return same token");
|
|
}
|
|
|
|
#[test]
|
|
fn creates_parent_directory() {
|
|
let dir = tempfile::tempdir().unwrap();
|
|
let path = dir.path().join("nested/here/registry.toml");
|
|
let cfg = ensure_token(&path).unwrap();
|
|
assert!(path.exists());
|
|
assert_eq!(cfg.token.len(), 64);
|
|
}
|
|
}
|