1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
//! Bridges discovery events to Iroh peer management.
//!
//! The `PeerConnector` consumes `DiscoveryEvent`s (from Kubernetes, mDNS, etc.)
//! and teaches the Iroh endpoint about discovered peers via the `MemoryLookup`.
//! This enables Iroh QUIC connections to peers whose addresses were learned
//! through non-Iroh discovery mechanisms.
use crate::discovery::DiscoveryEvent;
use crate::security::certificate::CertificateBundle;
use crate::security::FormationPeerSet;
use crate::storage::NetworkedIrohBlobStore;
use hkdf::Hkdf;
use iroh::{EndpointAddr, EndpointId, SecretKey, TransportAddr};
use sha2::Sha256;
use std::sync::{Arc, RwLock};
use tokio::sync::mpsc;
use tokio::task::JoinHandle;
use tracing::{debug, info, warn};
/// Bridges discovery events to Iroh's networking layer.
///
/// Given a formation secret, the connector derives deterministic Iroh
/// `EndpointId`s for discovered peers (using HKDF with context `"iroh:" + hostname`)
/// and registers their addresses with the blob store's `MemoryLookup`.
pub struct PeerConnector {
formation_secret: Vec<u8>,
blob_store: Arc<NetworkedIrohBlobStore>,
/// Known formation member EndpointIds. Updated as peers are
/// discovered/lost. Used by [`FormationEndpointHooks`] to gate
/// QUIC connections at the transport level.
formation_peers: FormationPeerSet,
/// Optional certificate bundle for peer validation.
/// When set, peers without valid certificates are rejected.
certificate_bundle: Option<Arc<RwLock<CertificateBundle>>>,
}
impl PeerConnector {
/// Create a new `PeerConnector`.
///
/// # Arguments
///
/// * `formation_secret` - Shared secret (raw bytes, already base64-decoded)
/// * `blob_store` - The networked blob store whose MemoryLookup and peer list to update
/// * `formation_peers` - Shared set of known formation member EndpointIds,
/// updated as peers are discovered/lost
pub fn new(
formation_secret: Vec<u8>,
blob_store: Arc<NetworkedIrohBlobStore>,
formation_peers: FormationPeerSet,
) -> Self {
Self {
formation_secret,
blob_store,
formation_peers,
certificate_bundle: None,
}
}
/// Set the certificate bundle for peer validation.
///
/// When set, peers without valid certificates are rejected.
pub fn with_certificate_bundle(mut self, bundle: Arc<RwLock<CertificateBundle>>) -> Self {
self.certificate_bundle = Some(bundle);
self
}
/// Derive the Iroh `EndpointId` for a peer given its hostname.
///
/// Uses `HKDF(formation_secret, "iroh:" + hostname)` → `SecretKey` → `.public()`.
/// This is deterministic: any node with the same formation secret can compute
/// any other node's `EndpointId` from its hostname alone.
pub fn derive_peer_endpoint_id(&self, hostname: &str) -> EndpointId {
let hk = Hkdf::<Sha256>::new(None, &self.formation_secret);
let mut okm = [0u8; 32];
let context = format!("iroh:{}", hostname);
hk.expand(context.as_bytes(), &mut okm)
.expect("HKDF expand with 32-byte output should never fail");
SecretKey::from_bytes(&okm).public()
}
/// Run the connector, consuming discovery events and updating Iroh.
///
/// Returns a `JoinHandle` for the spawned background task.
pub fn run(self, mut events: mpsc::Receiver<DiscoveryEvent>) -> JoinHandle<()> {
tokio::spawn(async move {
info!("PeerConnector started");
let my_endpoint_id = self.blob_store.endpoint_id();
while let Some(event) = events.recv().await {
match event {
DiscoveryEvent::PeerFound(peer_info) => {
let endpoint_id = self.derive_peer_endpoint_id(&peer_info.node_id);
// Don't connect to ourselves
if endpoint_id == my_endpoint_id {
debug!(
peer = %peer_info.node_id,
"Skipping self in peer discovery"
);
continue;
}
// Certificate validation (hard-reject when configured)
if let Some(ref bundle) = self.certificate_bundle {
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.expect("system clock before UNIX epoch")
.as_millis() as u64;
let bundle = bundle.read().unwrap_or_else(|e| e.into_inner());
if !bundle.validate_node_id(&peer_info.node_id, now) {
warn!(
peer = %peer_info.node_id,
"Rejecting peer: no valid certificate"
);
continue;
}
let tier = bundle.get_node_tier(&peer_info.node_id);
info!(
peer = %peer_info.node_id,
tier = ?tier,
"Peer certificate validated"
);
}
let addrs: std::collections::BTreeSet<TransportAddr> = peer_info
.addresses
.iter()
.map(|a| TransportAddr::Ip(*a))
.collect();
let endpoint_addr = EndpointAddr {
id: endpoint_id,
addrs,
};
self.blob_store
.memory_lookup()
.add_endpoint_info(endpoint_addr);
self.blob_store.add_peer(endpoint_id).await;
self.formation_peers.insert(endpoint_id);
info!(
peer = %peer_info.node_id,
endpoint_id = %endpoint_id.fmt_short(),
addresses = ?peer_info.addresses,
"Peer connected to Iroh (formation member)"
);
}
DiscoveryEvent::PeerLost(node_id) => {
let endpoint_id = self.derive_peer_endpoint_id(&node_id);
self.blob_store
.memory_lookup()
.remove_endpoint_info(endpoint_id);
self.blob_store.remove_peer(&endpoint_id).await;
self.formation_peers.remove(&endpoint_id);
info!(
peer = %node_id,
endpoint_id = %endpoint_id.fmt_short(),
"Peer removed from Iroh (formation member removed)"
);
}
DiscoveryEvent::PeerUpdated(peer_info) => {
let endpoint_id = self.derive_peer_endpoint_id(&peer_info.node_id);
if endpoint_id == my_endpoint_id {
continue;
}
// Re-validate certificate on update (hard-reject when configured)
let should_remove = if let Some(ref bundle) = self.certificate_bundle {
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.expect("system clock before UNIX epoch")
.as_millis() as u64;
let bundle = bundle.read().unwrap_or_else(|e| e.into_inner());
!bundle.validate_node_id(&peer_info.node_id, now)
} else {
false
};
if should_remove {
warn!(
peer = %peer_info.node_id,
"Removing peer on update: certificate no longer valid"
);
self.blob_store
.memory_lookup()
.remove_endpoint_info(endpoint_id);
self.blob_store.remove_peer(&endpoint_id).await;
self.formation_peers.remove(&endpoint_id);
continue;
}
let addrs: std::collections::BTreeSet<TransportAddr> = peer_info
.addresses
.iter()
.map(|a| TransportAddr::Ip(*a))
.collect();
let endpoint_addr = EndpointAddr {
id: endpoint_id,
addrs,
};
self.blob_store
.memory_lookup()
.set_endpoint_info(endpoint_addr);
debug!(
peer = %peer_info.node_id,
endpoint_id = %endpoint_id.fmt_short(),
addresses = ?peer_info.addresses,
"Peer addresses updated in Iroh"
);
}
}
}
warn!("PeerConnector event stream closed");
})
}
}
#[cfg(test)]
mod tests {
use super::*;
/// Verify that key derivation is deterministic: same inputs → same output.
#[test]
fn test_derive_peer_endpoint_id_deterministic() {
let secret = b"test-formation-secret".to_vec();
let store_secret = secret.clone();
// We can't easily build a real NetworkedIrohBlobStore in a sync test,
// so test the HKDF derivation directly.
let derive = |secret: &[u8], hostname: &str| -> EndpointId {
let hk = Hkdf::<Sha256>::new(None, secret);
let mut okm = [0u8; 32];
let context = format!("iroh:{}", hostname);
hk.expand(context.as_bytes(), &mut okm).unwrap();
SecretKey::from_bytes(&okm).public()
};
let id1 = derive(&secret, "peat-mesh-0");
let id2 = derive(&store_secret, "peat-mesh-0");
assert_eq!(
id1, id2,
"Same secret + hostname must produce same EndpointId"
);
}
/// Different hostnames must produce different EndpointIds.
#[test]
fn test_derive_peer_endpoint_id_different_hosts() {
let secret = b"test-formation-secret".to_vec();
let derive = |hostname: &str| -> EndpointId {
let hk = Hkdf::<Sha256>::new(None, &secret);
let mut okm = [0u8; 32];
let context = format!("iroh:{}", hostname);
hk.expand(context.as_bytes(), &mut okm).unwrap();
SecretKey::from_bytes(&okm).public()
};
let id_a = derive("peat-mesh-0");
let id_b = derive("peat-mesh-1");
assert_ne!(
id_a, id_b,
"Different hostnames must produce different EndpointIds"
);
}
/// Different secrets must produce different EndpointIds.
#[test]
fn test_derive_peer_endpoint_id_different_secrets() {
let derive = |secret: &[u8], hostname: &str| -> EndpointId {
let hk = Hkdf::<Sha256>::new(None, secret);
let mut okm = [0u8; 32];
let context = format!("iroh:{}", hostname);
hk.expand(context.as_bytes(), &mut okm).unwrap();
SecretKey::from_bytes(&okm).public()
};
let id_a = derive(b"secret-one", "peat-mesh-0");
let id_b = derive(b"secret-two", "peat-mesh-0");
assert_ne!(
id_a, id_b,
"Different secrets must produce different EndpointIds"
);
}
}