* New PairAttachVerifier (crates/maestro-daemon/src/jwt.rs): checks signature, aud == self, exp > now, and sub ∈ allow_inbound. Default deny — empty allow_inbound rejects every cross-daemon attach. * /sessions/:id/stream tries the legacy verifier first, falls back to the pair verifier on mismatch. Compat with existing direct-attach. * New BrokerHandle (crates/maestro-daemon/src/broker.rs): shared between the registry-client task and HTTP handlers; calls ResolveAttach over the control link and correlates the reply via a pending map. * RegistryClient now owns a BrokerHandle, drains its outbound mpsc alongside session events / heartbeats, and routes ResolveAttachResult back into the pending map. First heartbeat fires immediately so the registry has the daemon's perimeter info before the first ResolveAttach. * /api/attach/🆔 self-owned → local PTY; peer-owned (per catalog mirror) → ResolveAttach + T2 dial + bidirectional frame proxy. Returns structured errors for missing local perimeter, peer on a different perimeter, etc. T1/T3 stay stubbed. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
104 lines
3.7 KiB
Rust
104 lines
3.7 KiB
Rust
//! Home-daemon broker handle (NAP-247).
|
|
//!
|
|
//! The HTTP `WS /api/attach/:id` handler uses this to ask the registry to
|
|
//! resolve a peer-owned session into an [`AttachCandidates`] (a pair-scoped
|
|
//! JWT + the target daemon's T2 perimeter info). The actual control-link
|
|
//! send and the reply correlation live here so the handler stays thin.
|
|
|
|
use std::collections::HashMap;
|
|
use std::sync::Arc;
|
|
use std::time::Duration;
|
|
|
|
use maestro_protocol::{AttachCandidates, DaemonOutbound, ReplyTo, RpcResult};
|
|
use tokio::sync::{mpsc, oneshot, Mutex};
|
|
use uuid::Uuid;
|
|
|
|
/// How long to wait for a `ResolveAttachResult` after the request goes out.
|
|
/// Matches the registry's RPC timeout — a healthy registry replies in ms.
|
|
pub const RESOLVE_TIMEOUT: Duration = Duration::from_secs(10);
|
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
pub enum BrokerError {
|
|
#[error("registry control link unavailable")]
|
|
NoControlLink,
|
|
#[error("registry reply channel closed before response")]
|
|
ReplyClosed,
|
|
#[error("registry did not reply within {0:?}")]
|
|
Timeout(Duration),
|
|
#[error("registry rejected resolve: {0}: {1}")]
|
|
Registry(String, String),
|
|
}
|
|
|
|
/// Shared between the registry-client task (producer of `ResolveAttachResult`)
|
|
/// and the broker HTTP handler (consumer). Clone freely — all fields are
|
|
/// behind `Arc`s.
|
|
#[derive(Debug, Clone)]
|
|
pub struct BrokerHandle {
|
|
out_tx: mpsc::Sender<DaemonOutbound>,
|
|
pending: Arc<Mutex<HashMap<ReplyTo, oneshot::Sender<RpcResult<AttachCandidates>>>>>,
|
|
}
|
|
|
|
impl BrokerHandle {
|
|
/// `(handle, receiver)` — the registry-client task owns the receiver and
|
|
/// drains it into the control-link WS sink. The handle is shared with
|
|
/// HTTP handlers and is what they use to issue requests.
|
|
pub fn new() -> (Self, mpsc::Receiver<DaemonOutbound>) {
|
|
let (out_tx, out_rx) = mpsc::channel(64);
|
|
let handle = Self {
|
|
out_tx,
|
|
pending: Arc::new(Mutex::new(HashMap::new())),
|
|
};
|
|
(handle, out_rx)
|
|
}
|
|
|
|
/// Resolve a peer session: ask the registry for an [`AttachCandidates`].
|
|
pub async fn resolve_attach(&self, session_id: Uuid) -> Result<AttachCandidates, BrokerError> {
|
|
let reply_to = Uuid::new_v4();
|
|
let (tx, rx) = oneshot::channel();
|
|
self.pending.lock().await.insert(reply_to, tx);
|
|
|
|
if self
|
|
.out_tx
|
|
.send(DaemonOutbound::ResolveAttach {
|
|
reply_to,
|
|
session_id,
|
|
})
|
|
.await
|
|
.is_err()
|
|
{
|
|
self.pending.lock().await.remove(&reply_to);
|
|
return Err(BrokerError::NoControlLink);
|
|
}
|
|
|
|
match tokio::time::timeout(RESOLVE_TIMEOUT, rx).await {
|
|
Ok(Ok(RpcResult::Ok { value })) => Ok(value),
|
|
Ok(Ok(RpcResult::Err { error })) => {
|
|
Err(BrokerError::Registry(error.code, error.message))
|
|
}
|
|
Ok(Err(_)) => Err(BrokerError::ReplyClosed),
|
|
Err(_) => {
|
|
self.pending.lock().await.remove(&reply_to);
|
|
Err(BrokerError::Timeout(RESOLVE_TIMEOUT))
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Called by the registry-client task when a `ResolveAttachResult` frame
|
|
/// comes back from the registry. Resolves the waiting `resolve_attach`
|
|
/// future if one's still alive.
|
|
pub async fn complete_resolve(
|
|
&self,
|
|
reply_to: ReplyTo,
|
|
result: RpcResult<AttachCandidates>,
|
|
) -> bool {
|
|
let mut pending = self.pending.lock().await;
|
|
match pending.remove(&reply_to) {
|
|
Some(tx) => tx.send(result).is_ok(),
|
|
None => {
|
|
tracing::debug!(?reply_to, "no waiter for ResolveAttachResult");
|
|
false
|
|
}
|
|
}
|
|
}
|
|
}
|