Skip to main content

ts_runtime/peer_tracker/
mod.rs

1//! Peer delta update tracking.
2
3use std::{
4    collections::{HashMap, HashSet},
5    net::IpAddr,
6    sync::Arc,
7};
8
9use kameo::{
10    actor::ActorRef,
11    message::{Context, Message},
12    reply::ReplySender,
13};
14use tokio::sync::watch;
15use ts_control::{Node, UserId, UserProfile};
16use ts_transport::PeerId;
17
18use crate::{Error, env::Env, status::StatusNode};
19
20mod peer_db;
21
22pub use peer_db::PeerDb;
23
24/// Actor that tracks peer delta updates and emits new states.
25pub struct PeerTracker {
26    peer_db: PeerDb,
27    seen_state_update: bool,
28    pending_requests: Vec<Pending>,
29    /// Latest peer snapshot, published on every netmap update so embedders can watch for peer
30    /// changes ([`WatchNetmap`]).
31    peer_watch: watch::Sender<Vec<StatusNode>>,
32    /// Accumulated netmap user profiles (`MapResponse.UserProfiles`), keyed by user id, joined
33    /// against a node's [`Node::user_id`](ts_control::Node::user_id) to resolve the owning user's
34    /// login/display name for a [`WhoIs`](crate::status::WhoIs). Control sends these incrementally
35    /// (only new/changed profiles per response), so this map **accumulates** across updates rather
36    /// than being replaced — a peer upserted in one response may reference a profile delivered in an
37    /// earlier one.
38    user_profiles: HashMap<UserId, UserProfile>,
39    /// Tailnet-Lock (TKA) authority used to verify each peer's `key_signature` at the peer-trust
40    /// chokepoint. When `Some`, enforcement is **active**: every upserted peer must present a
41    /// signature this authority authorizes, or it is rejected (fail-closed). When `None` (always,
42    /// this wave) enforcement is **inactive** and every peer is upserted — identical to pre-TKA
43    /// behavior. There is no live `Authority` source yet: building one requires the
44    /// `/machine/tka/sync` Noise RPC + AUM-chain replayer (deferred, see SECURITY.md). The
45    /// enforcement path below is wired and unit-tested, and flips on the instant an authority is
46    /// supplied; it is explicitly gated, not a silent no-op.
47    tka_authority: Option<ts_tka::Authority>,
48    env: Env,
49}
50
51impl PeerTracker {
52    fn peer_by_name_opt(&self, name: &str) -> Option<&Node> {
53        // Canonicalization (case + trailing dot) is handled inside the name index lookup.
54        self.peer_db.get(&name).map(|(_id, node)| node)
55    }
56
57    fn peer_by_tailnet_ip_opt(&self, ip: IpAddr) -> Option<&Node> {
58        self.peer_db.get(&ip).map(|(_id, node)| node)
59    }
60
61    /// Build the peer entries for a [`Status`](crate::Status) snapshot from the current peer db.
62    fn status_peers(&self) -> Vec<StatusNode> {
63        self.peer_db
64            .peers()
65            .values()
66            .map(StatusNode::from_node)
67            .collect()
68    }
69
70    fn whois_opt(&self, addr: std::net::SocketAddr) -> Option<crate::status::WhoIs> {
71        let ip = crate::status::whois_addr(addr);
72        let node = self.peer_by_tailnet_ip_opt(ip).cloned()?;
73        // Join the node's owning user id against the accumulated UserProfiles table to resolve a
74        // login/display name. `None` when control sent no profile for that user (e.g. tagged nodes
75        // with no human owner, or a profile not yet delivered).
76        let user = self.resolve_user(node.user_id);
77        Some(crate::status::WhoIs::from_node_with_user(node, user))
78    }
79
80    /// Resolve a user id to its best display label from the accumulated profile table.
81    fn resolve_user(&self, user_id: UserId) -> Option<String> {
82        self.user_profiles
83            .get(&user_id)
84            .and_then(UserProfile::best_label)
85    }
86
87    /// Whether `node` may be admitted to the peer db under the current Tailnet-Lock posture.
88    ///
89    /// Fail-closed and gated:
90    /// - No [`tka_authority`](Self::tka_authority) ⇒ enforcement inactive ⇒ always admit (today's
91    ///   behavior; this is the always-taken branch this wave).
92    /// - Authority present + peer carries a `key_signature` that the authority authorizes for the
93    ///   peer's node key ⇒ admit.
94    /// - Authority present + signature missing or unauthorized/invalid ⇒ **reject** (Go denies
95    ///   network access to unsigned peers under tailnet lock; we do not upsert them).
96    fn tka_admits(&self, node: &Node) -> bool {
97        let Some(auth) = &self.tka_authority else {
98            return true;
99        };
100
101        if node.key_signature.is_empty() {
102            // TKA active but peer presented no signature: reject (Go denies network access to
103            // unsigned peers under tailnet lock, unless UnsignedPeerAPIOnly — out of scope here).
104            tracing::warn!(
105                stable_id = ?node.stable_id,
106                "TKA: rejecting unsigned peer under tailnet lock"
107            );
108            return false;
109        }
110
111        if let Err(e) = auth.node_key_authorized(&node.node_key.to_bytes(), &node.key_signature) {
112            tracing::warn!(
113                stable_id = ?node.stable_id,
114                error = %e,
115                "TKA: rejecting peer with unauthorized node key"
116            );
117            return false;
118        }
119
120        true
121    }
122}
123
124impl kameo::Actor for PeerTracker {
125    type Args = Env;
126    type Error = Error;
127
128    async fn on_start(env: Self::Args, slf: ActorRef<Self>) -> Result<Self, Self::Error> {
129        env.subscribe::<Arc<ts_control::StateUpdate>>(&slf).await?;
130
131        let (peer_watch, _) = watch::channel(Vec::new());
132
133        Ok(Self {
134            peer_db: PeerDb::default(),
135            pending_requests: Default::default(),
136            seen_state_update: false,
137            peer_watch,
138            user_profiles: HashMap::new(),
139            // No live TKA authority source this wave (the `/machine/tka/sync` RPC + AUM replayer are
140            // deferred); enforcement stays inactive until one is supplied. See `tka_authority`.
141            tka_authority: None,
142            env,
143        })
144    }
145}
146
147enum Pending {
148    PeerByName(PeerByName, ReplySender<Option<Node>>),
149    AcceptedRoute(PeerByAcceptedRoute, ReplySender<Vec<Node>>),
150    TailnetIp(PeerByTailnetIp, ReplySender<Option<Node>>),
151    Status(ReplySender<Vec<StatusNode>>),
152    WhoIs(Whois, ReplySender<Option<crate::status::WhoIs>>),
153}
154
155// For messages with arguments, a struct is generated with the args as fields. They aren't
156// documented, and we can't apply attributes directly to the fields. Hence, wrap in a module where
157// docs are turned off everywhere.
158#[allow(missing_docs)]
159mod msg_impl {
160    use std::net::IpAddr;
161
162    use kameo::prelude::DelegatedReply;
163
164    use super::*;
165
166    #[kameo::messages]
167    impl PeerTracker {
168        /// Lookup a peer by name.
169        ///
170        /// Waits until we've received at least one peer update from control.
171        #[message(ctx)]
172        pub async fn peer_by_name(
173            &mut self,
174            ctx: &mut Context<Self, DelegatedReply<Option<Node>>>,
175            name: String,
176        ) -> DelegatedReply<Option<Node>> {
177            let (deleg, sender) = ctx.reply_sender();
178            let Some(sender) = sender else { return deleg };
179
180            if !self.seen_state_update {
181                tracing::debug!(query = name, "no peer state seen yet, queueing request");
182
183                self.pending_requests
184                    .push(Pending::PeerByName(PeerByName { name }, sender));
185
186                return deleg;
187            }
188
189            sender.send(self.peer_by_name_opt(&name).cloned());
190
191            deleg
192        }
193
194        /// Lookup all peers that accept packets addressed to the given IP.
195        ///
196        /// This includes the peer's tailnet address and any subnet routes it provides. Only
197        /// the peers with the most specific subnet route match that covers `ip` will be
198        /// returned.
199        ///
200        /// E.g., suppose:
201        ///
202        /// - We're querying for `10.1.2.3`
203        /// - `PeerA` and `PeerB` have accepted routes for `10.1.2.0/24`
204        /// - `PeerC` has an accepted route for `10.1.0.0/16`
205        ///
206        /// Only `PeerA` and `PeerB` will be returned, since they have the most specific
207        /// prefix match.
208        #[message(ctx)]
209        pub fn peer_by_accepted_route(
210            &mut self,
211            ctx: &mut Context<Self, DelegatedReply<Vec<Node>>>,
212            ip: IpAddr,
213        ) -> DelegatedReply<Vec<Node>> {
214            let (deleg, sender) = ctx.reply_sender();
215            let Some(sender) = sender else { return deleg };
216
217            if !self.seen_state_update {
218                tracing::debug!(query = %ip, "no peer state seen yet, queueing request");
219
220                self.pending_requests
221                    .push(Pending::AcceptedRoute(PeerByAcceptedRoute { ip }, sender));
222
223                return deleg;
224            }
225
226            sender.send(
227                self.peer_db
228                    .get_route(ip.into())
229                    .map(|(_id, node)| node.clone())
230                    .collect(),
231            );
232
233            deleg
234        }
235
236        /// Lookup the peer that has the given tailnet IP address.
237        #[message(ctx)]
238        pub fn peer_by_tailnet_ip(
239            &mut self,
240            ctx: &mut Context<Self, DelegatedReply<Option<Node>>>,
241            ip: IpAddr,
242        ) -> DelegatedReply<Option<Node>> {
243            let (deleg, sender) = ctx.reply_sender();
244            let Some(sender) = sender else { return deleg };
245
246            if !self.seen_state_update {
247                tracing::debug!(query = %ip, "no peer state seen yet, queueing request");
248
249                self.pending_requests
250                    .push(Pending::TailnetIp(PeerByTailnetIp { ip }, sender));
251
252                return deleg;
253            }
254
255            sender.send(self.peer_by_tailnet_ip_opt(ip).cloned());
256
257            deleg
258        }
259
260        /// Build the peer entries of a [`Status`](crate::Status) snapshot.
261        ///
262        /// Returns one [`StatusNode`] per known peer. The self node is *not* included here (it
263        /// lives in the control runner); [`Runtime::status`](crate::Runtime::status) combines both.
264        ///
265        /// Waits until we've received at least one peer update from control.
266        #[message(ctx)]
267        pub fn get_status(
268            &mut self,
269            ctx: &mut Context<Self, DelegatedReply<Vec<StatusNode>>>,
270        ) -> DelegatedReply<Vec<StatusNode>> {
271            let (deleg, sender) = ctx.reply_sender();
272            let Some(sender) = sender else { return deleg };
273
274            if !self.seen_state_update {
275                tracing::debug!("no peer state seen yet, queueing status request");
276                self.pending_requests.push(Pending::Status(sender));
277                return deleg;
278            }
279
280            sender.send(self.status_peers());
281
282            deleg
283        }
284
285        /// Resolve which node owns a tailnet source address.
286        ///
287        /// Maps the source IP of `addr` to the owning node via the tailnet-IP index, returning a
288        /// [`WhoIs`](crate::WhoIs). The port is ignored (a tailnet IP uniquely identifies a node).
289        ///
290        /// The resulting [`WhoIs`](crate::WhoIs) carries no user/login or capability data: this
291        /// fork's domain [`Node`](ts_control::Node) does not retain those wire fields. See the
292        /// [`status`](crate::status) module docs for the gap.
293        ///
294        /// Waits until we've received at least one peer update from control.
295        #[message(ctx)]
296        pub fn whois(
297            &mut self,
298            ctx: &mut Context<Self, DelegatedReply<Option<crate::status::WhoIs>>>,
299            addr: std::net::SocketAddr,
300        ) -> DelegatedReply<Option<crate::status::WhoIs>> {
301            let (deleg, sender) = ctx.reply_sender();
302            let Some(sender) = sender else { return deleg };
303
304            if !self.seen_state_update {
305                tracing::debug!(query = %addr, "no peer state seen yet, queueing whois request");
306                self.pending_requests
307                    .push(Pending::WhoIs(Whois { addr }, sender));
308                return deleg;
309            }
310
311            sender.send(self.whois_opt(addr));
312
313            deleg
314        }
315
316        /// Subscribe to netmap peer-change events.
317        ///
318        /// Returns a [`watch::Receiver`] whose value is the current set of peer
319        /// [`StatusNode`]s, updated on every netmap state update from control. Embedders can await
320        /// changes via [`watch::Receiver::changed`] to react to peers joining, leaving, or changing.
321        ///
322        /// The receiver's initial value is the peer set at subscription time (empty before the
323        /// first netmap update). This is a peer-only view; combine with the self node from
324        /// [`Runtime::status`](crate::Runtime::status) when a full snapshot is needed.
325        #[message(derive(Clone))]
326        pub fn watch_netmap(&self) -> watch::Receiver<Vec<StatusNode>> {
327            self.peer_watch.subscribe()
328        }
329    }
330}
331
332pub use msg_impl::*;
333
334#[derive(Debug, Clone)]
335pub(crate) struct PeerState {
336    #[allow(unused)]
337    pub deletions: HashSet<PeerId>,
338    #[allow(unused)]
339    pub upserts: HashSet<PeerId>,
340    pub peers: Arc<PeerDb>,
341}
342
343impl Message<Arc<ts_control::StateUpdate>> for PeerTracker {
344    type Reply = ();
345
346    async fn handle(
347        &mut self,
348        msg: Arc<ts_control::StateUpdate>,
349        _ctx: &mut Context<Self, Self::Reply>,
350    ) {
351        // Accumulate user profiles first — control sends them incrementally and a response may
352        // carry profiles with no peer delta (or peers that reference a profile from an earlier
353        // response), so this must happen before the no-peer-update early return below.
354        for profile in &msg.user_profiles {
355            self.user_profiles.insert(profile.id, profile.clone());
356        }
357
358        let Some(peer_update) = &msg.peer_update else {
359            return;
360        };
361
362        let (upserts, deletions) = self.apply_peer_update(peer_update);
363
364        tracing::debug!(
365            n_upsert = upserts.len(),
366            n_delete = deletions.len(),
367            peer_count = self.peer_db.peers().len(),
368            "new peer state"
369        );
370
371        self.service_pending_requests();
372
373        // Publish the latest peer snapshot to netmap watchers. `send_replace` keeps the receiver's
374        // value current even when there are no subscribers, so a late subscriber sees fresh state.
375        self.peer_watch.send_replace(self.status_peers());
376
377        if let Err(e) = self
378            .env
379            .publish(Arc::new(PeerState {
380                upserts,
381                deletions,
382                peers: Arc::new(self.peer_db.clone()),
383            }))
384            .await
385        {
386            tracing::error!(error = %e, "publishing peer state update");
387        }
388    }
389}
390
391/// Ask the peer tracker to re-broadcast its current peer snapshot on the bus, without any peer
392/// change. `Device::set_exit_node` sends this after changing the exit-node selector so the route
393/// updater and source filter (both `Arc<PeerState>` subscribers) re-resolve the new selector
394/// immediately, rather than waiting for the next netmap update.
395#[derive(Debug, Clone, Copy)]
396pub struct RepublishState;
397
398impl Message<RepublishState> for PeerTracker {
399    type Reply = ();
400
401    async fn handle(&mut self, _msg: RepublishState, _ctx: &mut Context<Self, Self::Reply>) {
402        // An empty upsert/deletion set: this is a re-broadcast of the unchanged peer set, not a
403        // delta. Subscribers recompute their routes/filters against the current peers and the
404        // (just-updated) exit-node selector.
405        if let Err(e) = self
406            .env
407            .publish(Arc::new(PeerState {
408                upserts: HashSet::default(),
409                deletions: HashSet::default(),
410                peers: Arc::new(self.peer_db.clone()),
411            }))
412            .await
413        {
414            tracing::error!(error = %e, "re-publishing peer state after exit-node change");
415        }
416    }
417}
418
419impl PeerTracker {
420    /// Apply a single [`PeerUpdate`](ts_control::PeerUpdate) to the peer db, enforcing the
421    /// Tailnet-Lock peer-trust chokepoint ([`tka_admits`](Self::tka_admits)) at every upsert site.
422    ///
423    /// This is the **single source of truth** for the peer-trust enforcement loop: the actor's
424    /// netmap [`handle`](Message::handle) calls it, and so do the TKA enforcement tests, so the two
425    /// real upsert sites (`Full` and `Delta { upsert }`) cannot diverge from what is tested.
426    ///
427    /// Returns `(upserts, deletions)` — the [`PeerId`]s touched — for downstream bookkeeping.
428    fn apply_peer_update(
429        &mut self,
430        peer_update: &ts_control::PeerUpdate,
431    ) -> (HashSet<PeerId>, HashSet<PeerId>) {
432        let mut upserts = HashSet::default();
433        let mut deletions = HashSet::default();
434
435        match peer_update {
436            ts_control::PeerUpdate::Full(new_nodes) => {
437                tracing::trace!("full peer update");
438
439                // Only stable_ids that PASS the Tailnet-Lock gate survive a full re-sync. This makes
440                // revocation evict: if a peer is re-included with a now-invalid (or missing)
441                // signature under an active authority, it is excluded from `retained_ids`, so
442                // `retain` drops the stale (previously-admitted) entry rather than leaving it in the
443                // db unverified. With no authority, `tka_admits` is always `true`, so `retained_ids`
444                // is exactly the set of re-included stable_ids — the inactive path is byte-for-byte
445                // the pre-TKA behavior (no regression).
446                let retained_ids = new_nodes
447                    .iter()
448                    .filter(|node| self.tka_admits(node))
449                    .map(|x| &x.stable_id)
450                    .collect::<HashSet<_>>();
451
452                self.peer_db.retain(|id, peer| {
453                    let retain = retained_ids.contains(&peer.stable_id);
454
455                    if !retain {
456                        deletions.insert(id);
457                    }
458
459                    retain
460                });
461
462                for node in new_nodes {
463                    if !self.tka_admits(node) {
464                        continue; // fail-CLOSED: do not upsert a peer rejected by tailnet lock
465                    }
466                    let peer_id = self.peer_db.upsert(node);
467                    upserts.insert(peer_id);
468                }
469            }
470
471            ts_control::PeerUpdate::Delta { remove, upsert } => {
472                tracing::trace!("delta peer update");
473
474                for peer in upsert {
475                    if !self.tka_admits(peer) {
476                        continue; // fail-CLOSED: do not upsert a peer rejected by tailnet lock
477                    }
478                    let id = self.peer_db.upsert(peer);
479
480                    upserts.insert(id);
481                }
482
483                for peer in remove {
484                    let Some((id, _node)) = self.peer_db.remove(peer) else {
485                        tracing::error!(control_node_id = peer, "removed peer was unknown");
486                        continue;
487                    };
488
489                    deletions.insert(id);
490                }
491            }
492        }
493
494        (upserts, deletions)
495    }
496
497    /// Test-only constructor: build a [`PeerTracker`] with a chosen [`tka_authority`](Self::tka_authority)
498    /// without going through the actor `on_start` path. Used by the TKA enforcement unit tests to
499    /// exercise the peer-trust chokepoint ([`tka_admits`](Self::tka_admits)) directly.
500    #[cfg(test)]
501    fn for_test(env: Env, tka_authority: Option<ts_tka::Authority>) -> Self {
502        let (peer_watch, _) = watch::channel(Vec::new());
503        Self {
504            peer_db: PeerDb::default(),
505            seen_state_update: false,
506            pending_requests: Vec::new(),
507            peer_watch,
508            user_profiles: HashMap::new(),
509            tka_authority,
510            env,
511        }
512    }
513
514    fn service_pending_requests(&mut self) {
515        if self.seen_state_update {
516            return;
517        }
518
519        self.seen_state_update = true;
520
521        if !self.pending_requests.is_empty() {
522            tracing::debug!(
523                n_pending = self.pending_requests.len(),
524                "state update received, servicing pending requests"
525            );
526        }
527
528        for req in core::mem::take(&mut self.pending_requests) {
529            match req {
530                Pending::PeerByName(PeerByName { name }, reply) => {
531                    reply.send(self.peer_by_name_opt(&name).cloned());
532                }
533                Pending::TailnetIp(PeerByTailnetIp { ip }, reply) => {
534                    reply.send(self.peer_by_tailnet_ip_opt(ip).cloned());
535                }
536                Pending::AcceptedRoute(PeerByAcceptedRoute { ip }, reply) => {
537                    reply.send(
538                        self.peer_db
539                            .get_route(ip.into())
540                            .map(|(_id, node)| node.clone())
541                            .collect(),
542                    );
543                }
544                Pending::Status(reply) => {
545                    reply.send(self.status_peers());
546                }
547                Pending::WhoIs(Whois { addr }, reply) => {
548                    reply.send(self.whois_opt(addr));
549                }
550            }
551        }
552    }
553}
554
555#[cfg(test)]
556mod tka_tests {
557    //! Tailnet-Lock (TKA) enforcement tests for the peer-trust chokepoint.
558    //!
559    //! These exercise [`PeerTracker::tka_admits`] and the `tka_admits ⇒ upsert` loop the netmap
560    //! handler runs. The test [`ts_tka::Authority`] is built with [`ts_tka::Authority::from_state`]
561    //! over a known Ed25519 trusted key, and the signed node-key signature CBOR is produced through
562    //! `ts_tka`'s public `cbor` encoder + `aum_hash` (the exact same canonical bytes `ts_tka`'s own
563    //! `direct_signature_verifies_end_to_end` test signs, with no new crypto vectors invented and no
564    //! private `ts_tka` API used).
565
566    use ed25519_dalek::{Signer, SigningKey};
567    use ts_control::{Node, StableNodeId, TailnetAddress};
568    use ts_tka::{
569        AumHash, Authority, Key, KeyKind, State,
570        cbor::{self, Value},
571    };
572
573    use super::*;
574
575    /// `SigKind::Direct` wire value (Go `SigKind`; `ts_tka::SigKind::Direct = 1`).
576    const SIG_KIND_DIRECT: u64 = 1;
577
578    /// The 32-byte node key used across the signed-peer fixtures.
579    const NODE_KEY_BYTES: [u8; 32] = [7u8; 32];
580
581    /// Build a real [`Env`] for the tracker. Only the bus/keys/shutdown plumbing matters here; the
582    /// TKA gate reads neither, so the forwarding preferences are all benign defaults.
583    fn test_env() -> Env {
584        let (_shutdown_tx, shutdown_rx) = watch::channel(false);
585        Env::new(
586            ts_keys::NodeState::generate(),
587            shutdown_rx,
588            crate::env::ForwarderConfig {
589                accept_routes: false,
590                exit_node: None,
591                forward_routes: Vec::new(),
592                forward_tcp_ports: Vec::new(),
593                forward_udp_ports: Vec::new(),
594                forward_all_ports: false,
595                forward_exit_egress: false,
596                exit_proxy: None,
597                peerapi_port: None,
598                taildrop_dir: None,
599                enable_ipv6: false,
600                ingress_active: std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false)),
601            },
602        )
603    }
604
605    /// A minimal peer [`Node`] carrying `node_key` and the given `key_signature`.
606    fn peer_node(stable_id: &str, node_key: [u8; 32], key_signature: Vec<u8>) -> Node {
607        Node {
608            id: 1,
609            stable_id: StableNodeId(stable_id.to_string()),
610            hostname: stable_id.to_string(),
611            user_id: 0,
612            tailnet: Some("ts.net".to_string()),
613            tags: Vec::new(),
614            tailnet_address: TailnetAddress {
615                ipv4: "100.64.0.1/32".parse().unwrap(),
616                ipv6: "fd7a:115c:a1e0::1/128".parse().unwrap(),
617            },
618            node_key: node_key.into(),
619            node_key_expiry: None,
620            key_signature,
621            machine_key: None,
622            disco_key: None,
623            accepted_routes: Vec::new(),
624            underlay_addresses: Vec::new(),
625            derp_region: None,
626            cap: Default::default(),
627            cap_map: Default::default(),
628            peerapi_port: None,
629            peerapi_dns_proxy: false,
630            is_wireguard_only: false,
631            exit_node_dns_resolvers: Vec::new(),
632            peer_relay: false,
633            service_vips: Default::default(),
634        }
635    }
636
637    /// Encode a `Direct` [`ts_tka::NodeKeySignature`] CBOR exactly as `ts_tka`'s private `to_cbor`
638    /// does (int-map keys: 1=kind, 2=pubkey, 3=key_id, 4=signature; empty byte fields omitted),
639    /// using only the crate's *public* `cbor` encoder. `signature` of `None` produces the
640    /// signing-digest preimage (the `SigHash` form).
641    fn direct_sig_cbor(node_key: &[u8], key_id: &[u8], signature: Option<&[u8]>) -> Vec<u8> {
642        let mut pairs = alloc_pairs(node_key, key_id);
643        if let Some(sig) = signature {
644            pairs.push((4, Some(Value::Bytes(sig.to_vec()))));
645        }
646        cbor::int_map(pairs).to_vec()
647    }
648
649    fn alloc_pairs(node_key: &[u8], key_id: &[u8]) -> Vec<(u64, Option<Value>)> {
650        vec![
651            (1, Some(Value::Uint(SIG_KIND_DIRECT))),
652            (2, Some(Value::Bytes(node_key.to_vec()))),
653            (3, Some(Value::Bytes(key_id.to_vec()))),
654        ]
655    }
656
657    /// Build a TKA [`Authority`] that trusts `signing.verifying_key()`, plus a valid `Direct`
658    /// node-key signature CBOR authorizing [`NODE_KEY_BYTES`] under it.
659    fn authority_and_valid_sig() -> (Authority, Vec<u8>) {
660        // A fixed, known Ed25519 trusted key (mirrors ts_tka's own end-to-end test seed).
661        let signing = SigningKey::from_bytes(&[42u8; 32]);
662        let trusted_pub = signing.verifying_key().to_bytes().to_vec();
663
664        let authority = Authority::from_state(
665            AumHash([0; 32]),
666            State {
667                keys: vec![Key {
668                    kind: KeyKind::Ed25519,
669                    votes: 1,
670                    public: trusted_pub.clone(),
671                }],
672            },
673        );
674
675        // SigHash preimage = canonical CBOR with the signature field omitted; sign its blake2s hash.
676        let preimage = direct_sig_cbor(&NODE_KEY_BYTES, &trusted_pub, None);
677        let sig_hash = ts_tka::aum_hash(&preimage).0;
678        let signature = signing.sign(&sig_hash).to_bytes().to_vec();
679
680        let signed_cbor = direct_sig_cbor(&NODE_KEY_BYTES, &trusted_pub, Some(&signature));
681        // Sanity: the authority accepts the signature we just built (same path the gate uses).
682        assert!(
683            authority
684                .node_key_authorized(&NODE_KEY_BYTES, &signed_cbor)
685                .is_ok()
686        );
687
688        (authority, signed_cbor)
689    }
690
691    #[tokio::test]
692    async fn tka_inactive_upserts_all_peers() {
693        // No authority ⇒ enforcement inactive ⇒ both a signed and an unsigned peer are admitted.
694        let mut tracker = PeerTracker::for_test(test_env(), None);
695
696        let signed = peer_node("signed", [1u8; 32], vec![0xde, 0xad, 0xbe, 0xef]);
697        let unsigned = peer_node("unsigned", [2u8; 32], vec![]);
698
699        assert!(tracker.tka_admits(&signed));
700        assert!(tracker.tka_admits(&unsigned));
701
702        tracker.peer_db.upsert(&signed);
703        tracker.peer_db.upsert(&unsigned);
704        assert_eq!(tracker.peer_db.peers().len(), 2);
705    }
706
707    #[tokio::test]
708    async fn tka_active_rejects_unsigned_peer() {
709        // Authority present + peer presents no signature ⇒ rejected (fail-closed), not in peer_db.
710        let (authority, _sig) = authority_and_valid_sig();
711        let mut tracker = PeerTracker::for_test(test_env(), Some(authority));
712
713        let unsigned = peer_node("unsigned", NODE_KEY_BYTES, vec![]);
714        assert!(!tracker.tka_admits(&unsigned));
715
716        // Mirror the handler's `if !tka_admits { continue }` loop.
717        if tracker.tka_admits(&unsigned) {
718            tracker.peer_db.upsert(&unsigned);
719        }
720        assert_eq!(tracker.peer_db.peers().len(), 0);
721        assert!(tracker.peer_db.get(&unsigned.node_key).is_none());
722    }
723
724    #[tokio::test]
725    async fn tka_active_rejects_bad_signature() {
726        // Authority present + a signature that fails to verify ⇒ rejected, not in peer_db.
727        let (authority, mut sig) = authority_and_valid_sig();
728        // Tamper the last byte (the trailing signature byte) so verification fails.
729        let last = sig.len() - 1;
730        sig[last] ^= 0xff;
731
732        let mut tracker = PeerTracker::for_test(test_env(), Some(authority));
733        let bad = peer_node("bad", NODE_KEY_BYTES, sig);
734        assert!(!tracker.tka_admits(&bad));
735
736        if tracker.tka_admits(&bad) {
737            tracker.peer_db.upsert(&bad);
738        }
739        assert_eq!(tracker.peer_db.peers().len(), 0);
740    }
741
742    #[tokio::test]
743    async fn tka_active_admits_authorized_peer() {
744        // Authority present + correctly-signed node key ⇒ admitted and upserted.
745        let (authority, sig) = authority_and_valid_sig();
746        let mut tracker = PeerTracker::for_test(test_env(), Some(authority));
747
748        let good = peer_node("good", NODE_KEY_BYTES, sig);
749        assert!(tracker.tka_admits(&good));
750
751        if tracker.tka_admits(&good) {
752            tracker.peer_db.upsert(&good);
753        }
754        assert_eq!(tracker.peer_db.peers().len(), 1);
755        assert!(tracker.peer_db.get(&good.node_key).is_some());
756    }
757
758    // ---------------------------------------------------------------------------------------------
759    // Tests that drive REAL `PeerUpdate`s through the shared handler body
760    // ([`PeerTracker::apply_peer_update`], the single source of truth the actor's netmap `handle`
761    // also calls), so the two real upsert sites (`Full` and `Delta { upsert }`) are exercised via
762    // the actual enforcement path — not by hand-mirroring `if !tka_admits { continue }`.
763    // ---------------------------------------------------------------------------------------------
764
765    #[tokio::test]
766    async fn tka_active_delta_upsert_rejects_unauthorized() {
767        // Drive a real `Delta { upsert }` whose peer carries no signature. The Delta upsert site
768        // must reject it under an active authority ⇒ not present in peer_db after the handler runs.
769        let (authority, _sig) = authority_and_valid_sig();
770        let mut tracker = PeerTracker::for_test(test_env(), Some(authority));
771
772        let unsigned = peer_node("unsigned", NODE_KEY_BYTES, vec![]);
773        let update = ts_control::PeerUpdate::Delta {
774            upsert: vec![unsigned.clone()],
775            remove: Vec::new(),
776        };
777
778        tracker.apply_peer_update(&update);
779
780        assert_eq!(tracker.peer_db.peers().len(), 0);
781        assert!(tracker.peer_db.get(&unsigned.node_key).is_none());
782    }
783
784    #[tokio::test]
785    async fn tka_active_delta_upsert_admits_authorized() {
786        // Drive a real `Delta { upsert }` with a correctly-signed peer ⇒ present in peer_db.
787        let (authority, sig) = authority_and_valid_sig();
788        let mut tracker = PeerTracker::for_test(test_env(), Some(authority));
789
790        let good = peer_node("good", NODE_KEY_BYTES, sig);
791        let update = ts_control::PeerUpdate::Delta {
792            upsert: vec![good.clone()],
793            remove: Vec::new(),
794        };
795
796        tracker.apply_peer_update(&update);
797
798        assert_eq!(tracker.peer_db.peers().len(), 1);
799        assert!(tracker.peer_db.get(&good.node_key).is_some());
800    }
801
802    #[tokio::test]
803    async fn tka_active_full_admits_only_authorized_in_mixed_batch() {
804        // Drive a real `Full` carrying a MIX of authorized + unauthorized peers. Only the
805        // correctly-signed peer survives the Full upsert site; the unsigned and bad-sig peers are
806        // dropped fail-closed.
807        let (authority, sig) = authority_and_valid_sig();
808        // A bad-sig variant of the same authorized signature (tamper the trailing byte).
809        let mut bad_sig = sig.clone();
810        let last = bad_sig.len() - 1;
811        bad_sig[last] ^= 0xff;
812
813        let mut tracker = PeerTracker::for_test(test_env(), Some(authority));
814
815        // Only the authorized peer carries NODE_KEY_BYTES (the key the authority signed); the
816        // rejected peers use distinct node keys so the survivor is unambiguous.
817        let good = peer_node("good", NODE_KEY_BYTES, sig);
818        let unsigned = peer_node("unsigned", [8u8; 32], vec![]);
819        let bad = peer_node("bad", [9u8; 32], bad_sig);
820
821        let update =
822            ts_control::PeerUpdate::Full(vec![good.clone(), unsigned.clone(), bad.clone()]);
823
824        tracker.apply_peer_update(&update);
825
826        assert_eq!(tracker.peer_db.peers().len(), 1);
827        assert!(tracker.peer_db.get(&good.node_key).is_some());
828        assert!(tracker.peer_db.get(&unsigned.node_key).is_none());
829        assert!(tracker.peer_db.get(&bad.node_key).is_none());
830    }
831
832    #[tokio::test]
833    async fn tka_full_resync_revocation_behavior() {
834        // Revocation-on-resync: admit a peer, then re-include the SAME stable_id in a `Full` with a
835        // now-invalid signature. Per the Logic review finding, the pre-fix `retain` kept the stale
836        // (previously-admitted) entry because membership was decided purely by stable_id.
837        //
838        // FIXED (not merely documented): the `Full` `retain` now keys on `tka_admits`-passing
839        // stable_ids, so a peer whose re-included signature no longer verifies under the active
840        // authority is EVICTED. This test asserts eviction. The inactive (authority=None) path is
841        // provably unchanged — `tka_admits` always returns `true` there, so the retained set equals
842        // the set of re-included stable_ids exactly (see `tka_inactive_full_resync_keeps_*`).
843        let (authority, sig) = authority_and_valid_sig();
844        let mut tracker = PeerTracker::for_test(test_env(), Some(authority));
845
846        // 1) Admit the peer with a valid signature via a real `Full`.
847        let good = peer_node("revoked", NODE_KEY_BYTES, sig.clone());
848        tracker.apply_peer_update(&ts_control::PeerUpdate::Full(vec![good.clone()]));
849        assert_eq!(tracker.peer_db.peers().len(), 1);
850        assert!(tracker.peer_db.get(&good.node_key).is_some());
851
852        // 2) Re-sync the SAME stable_id, but with a now-invalid signature (tamper trailing byte).
853        let mut bad_sig = sig;
854        let last = bad_sig.len() - 1;
855        bad_sig[last] ^= 0xff;
856        let revoked = peer_node("revoked", NODE_KEY_BYTES, bad_sig);
857        tracker.apply_peer_update(&ts_control::PeerUpdate::Full(vec![revoked.clone()]));
858
859        // Eviction: the stale entry is dropped because its re-included signature fails the gate.
860        assert_eq!(tracker.peer_db.peers().len(), 0);
861        assert!(tracker.peer_db.get(&revoked.node_key).is_none());
862    }
863
864    #[tokio::test]
865    async fn tka_inactive_full_resync_keeps_reincluded_peer() {
866        // Guard the inactive (authority=None) path against the revocation fix: with no authority,
867        // a peer re-included in a `Full` survives regardless of its signature bytes — byte-for-byte
868        // pre-TKA behavior, proving the `Full` `retain` change does not regress the always-taken
869        // branch this wave.
870        let mut tracker = PeerTracker::for_test(test_env(), None);
871
872        let peer = peer_node("p", NODE_KEY_BYTES, vec![0xde, 0xad]);
873        tracker.apply_peer_update(&ts_control::PeerUpdate::Full(vec![peer.clone()]));
874        assert_eq!(tracker.peer_db.peers().len(), 1);
875
876        // Re-sync the same stable_id with garbage signature bytes; inactive enforcement keeps it.
877        let resynced = peer_node("p", NODE_KEY_BYTES, vec![0x00]);
878        tracker.apply_peer_update(&ts_control::PeerUpdate::Full(vec![resynced.clone()]));
879        assert_eq!(tracker.peer_db.peers().len(), 1);
880        assert!(tracker.peer_db.get(&resynced.node_key).is_some());
881    }
882
883    /// A node's `user_id` joins against the accumulated UserProfiles table to resolve the owning
884    /// user's login name in `WhoIs.user`. With no matching profile, `user` is `None` (the
885    /// pre-existing behavior); once a profile arrives, the same node resolves to its login. This
886    /// proves the accumulate-then-join path the netmap handler builds.
887    fn profile(id: ts_control::UserId, login: &str) -> ts_control::UserProfile {
888        ts_control::UserProfile {
889            id,
890            login_name: login.to_string(),
891            display_name: None,
892        }
893    }
894
895    #[tokio::test]
896    async fn whois_resolves_user_from_accumulated_profiles() {
897        let mut tracker = PeerTracker::for_test(test_env(), None);
898
899        // A peer owned by user id 42 at 100.64.0.1 (the peer_node fixture's address).
900        let mut peer = peer_node("p", NODE_KEY_BYTES, Vec::new());
901        peer.user_id = 42;
902        tracker.apply_peer_update(&ts_control::PeerUpdate::Full(vec![peer]));
903        let addr = "100.64.0.1:0".parse().unwrap();
904
905        // No profile yet: the node resolves but its owner is unknown.
906        let who = tracker.whois_opt(addr).expect("peer is known");
907        assert_eq!(who.user, None);
908
909        // Profile for a DIFFERENT user must not match.
910        tracker
911            .user_profiles
912            .insert(7, profile(7, "someone-else@example.com"));
913        assert_eq!(tracker.whois_opt(addr).unwrap().user, None);
914
915        // The owning user's profile arrives (as the netmap handler would accumulate it): now the
916        // login resolves.
917        tracker
918            .user_profiles
919            .insert(42, profile(42, "alice@example.com"));
920        assert_eq!(
921            tracker.whois_opt(addr).unwrap().user,
922            Some("alice@example.com".to_string())
923        );
924    }
925
926    /// `UserProfile::best_label` prefers the login name, falling back to display name, else `None`.
927    #[test]
928    fn user_profile_best_label_prefers_login() {
929        assert_eq!(
930            profile(1, "alice@example.com").best_label(),
931            Some("alice@example.com".to_string())
932        );
933        let display_only = ts_control::UserProfile {
934            id: 2,
935            login_name: String::new(),
936            display_name: Some("Bob".to_string()),
937        };
938        assert_eq!(display_only.best_label(), Some("Bob".to_string()));
939        let empty = ts_control::UserProfile {
940            id: 3,
941            login_name: String::new(),
942            display_name: None,
943        };
944        assert_eq!(empty.best_label(), None);
945    }
946}