liminal-server 0.23.1

Standalone server for the liminal messaging bus
Documentation
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
//! Carriers move sealed link bytes without opening the Noise transport.

use crate::config::LimitsConfig;
use liminal::protocol::{Frame, FrameHeader, ProtocolError, codec};
use std::io::{Read, Write};
use std::net::TcpStream;

pub mod choice;
mod reader;

/// The route a keyed link's bytes take.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Carrier {
    /// The peer's TCP wire listener.
    Wire,
    /// A direct datagram path.
    Direct,
    /// A relay carrying opaque bytes.
    Relay,
}

/// The next sealed message, or the carrier's named end.
#[derive(Debug, PartialEq, Eq)]
pub enum CarrierReceive {
    /// Bytes still sealed by the keyed link.
    Sealed { subject: String, sealed: Vec<u8> },
    /// The importing end acknowledged the message on this link's stream.
    Acknowledged {
        message_id: u64,
        durable_position: Option<u64>,
    },
    /// The importing end refused the message on this link's stream.
    Refused {
        reason_code: u16,
        reason: Option<String>,
    },
    /// The peer or byte stream ended the carrier.
    End { reason: String },
}

/// Why a carrier could not move a frame.
#[derive(Debug, thiserror::Error)]
pub enum CarrierError {
    /// The carrier's owned lifecycle or reader could not continue.
    #[error("wire link lifecycle failed: {0}")]
    Lifecycle(String),
    /// A socket operation failed; credential and message bytes are excluded.
    #[error("wire I/O failed: {0}")]
    Io(#[from] std::io::Error),
    /// The canonical codec refused a frame.
    #[error("wire framing refused: {0}")]
    Protocol(#[from] ProtocolError),
    /// The subject is not a placed name.
    #[error(transparent)]
    Subject(#[from] crate::auth_lys::SubjectBindingError),
    /// A peer's declared frame exceeded the allocation bound.
    #[error("wire frame of {bytes} bytes exceeds bound {bound}")]
    TooLarge { bytes: usize, bound: usize },
    /// The peer answered with a different frame or stream.
    #[error("unexpected frame on wire link")]
    UnexpectedFrame,
    /// Sending after an observed carrier end was refused.
    #[error("wire carrier has ended")]
    Ended,
}

/// A carrier transports sealed bytes and reports its end by name.
pub trait LinkCarrier: Send + std::fmt::Debug {
    /// Which carrier holds this path.
    fn carrier(&self) -> Carrier;
    /// # Errors
    /// A parked reader observed an unsolicited frame or a carrier end.
    fn refuse_unsolicited(&self) -> Result<(), CarrierError> {
        Ok(())
    }
    /// Sends one subject and its sealed bytes.
    ///
    /// # Errors
    /// The frame was invalid or the carrier could not send it.
    fn send(&mut self, subject: &str, sealed: &[u8]) -> Result<(), CarrierError>;
    /// Receives the next sealed bytes, or the carrier's named end.
    ///
    /// # Errors
    /// An I/O error, malformed frame, or unexpected frame/stream.
    fn receive(&mut self) -> Result<CarrierReceive, CarrierError>;
}

/// One application stream on the peer's TCP wire connection.
#[derive(Debug)]
pub struct WireCarrier {
    socket: TcpStream,
    stream_id: u32,
    ended: Option<String>,
    life: Option<crate::link::LinkLife>,
    reader: Option<reader::Reader>,
}

impl WireCarrier {
    pub(crate) fn refuse_unsolicited(&self) -> Result<(), CarrierError> {
        if let Some(reader) = &self.reader
            && let Some(answer) = reader.try_receive()?
        {
            let error = match answer {
                Ok(Some(_)) => CarrierError::UnexpectedFrame,
                Ok(None) => CarrierError::Ended,
                Err(error) => error,
            };
            self.failed(&error);
            return Err(error);
        }
        Ok(())
    }
    pub(crate) fn install_life(&mut self, life: crate::link::LinkLife) -> Result<(), CarrierError> {
        if self.life.is_some() {
            return Err(CarrierError::Lifecycle(
                "lifecycle already installed".to_owned(),
            ));
        }
        self.life = Some(life.clone());
        match reader::Reader::start(&self.socket, self.stream_id, life) {
            Ok(reader) => self.reader = Some(reader),
            Err(error) => {
                self.end_life(crate::link::LinkEnd::Refused(error.to_string()));
                return Err(error);
            }
        }
        Ok(())
    }

    pub(crate) const fn life(&self) -> Option<&crate::link::LinkLife> {
        self.life.as_ref()
    }

    fn end_life(&self, reason: crate::link::LinkEnd) {
        if let Some(life) = &self.life
            && let Err(error) = life.end(reason)
        {
            tracing::error!(%error, "wire link end observation failed");
        }
    }

    fn failed(&self, error: &CarrierError) {
        self.end_life(
            if matches!(error, CarrierError::Io(_) | CarrierError::Ended) {
                crate::link::LinkEnd::PeerGone
            } else {
                crate::link::LinkEnd::Refused(error.to_string())
            },
        );
    }
    /// Duplicates the carrier socket solely to interrupt its blocking receive at stop.
    ///
    /// # Errors
    /// The socket cannot be duplicated.
    pub fn interrupt_handle(&self) -> Result<TcpStream, CarrierError> {
        Ok(self.socket.try_clone()?)
    }
    /// Takes ownership of a connection whose link handshake has finished.
    ///
    /// # Errors
    /// Stream zero is reserved for connection control.
    pub fn new(socket: TcpStream, stream_id: u32) -> Result<Self, CarrierError> {
        liminal::protocol::validate_stream(liminal::protocol::FrameType::LinkMessage, stream_id)?;
        Ok(Self {
            socket,
            stream_id,
            ended: None,
            life: None,
            reader: None,
        })
    }
}

impl LinkCarrier for WireCarrier {
    fn refuse_unsolicited(&self) -> Result<(), CarrierError> {
        Self::refuse_unsolicited(self)
    }
    fn carrier(&self) -> Carrier {
        Carrier::Wire
    }

    fn send(&mut self, subject: &str, sealed: &[u8]) -> Result<(), CarrierError> {
        if self.ended.is_some() {
            return Err(CarrierError::Ended);
        }
        if let Some(life) = &self.life
            && life.reason().map_err(CarrierError::Lifecycle)?.is_some()
        {
            return Err(CarrierError::Ended);
        }
        crate::auth_lys::SubjectBinding::new(
            "wire".to_owned(),
            subject.to_owned(),
            subject.to_owned(),
        )?;
        let result = write_frame(
            &mut self.socket,
            &Frame::LinkMessage {
                flags: 0,
                stream_id: self.stream_id,
                subject: subject.to_owned(),
                sealed: sealed.to_vec(),
            },
        );
        if let Err(error) = &result {
            self.failed(error);
        }
        result
    }

    fn receive(&mut self) -> Result<CarrierReceive, CarrierError> {
        if let Some(reason) = &self.ended {
            return Ok(CarrierReceive::End {
                reason: reason.clone(),
            });
        }
        let answer = if let Some(reader) = &self.reader {
            reader.receive()
        } else {
            read_frame(&mut self.socket)
        };
        let frame = answer.inspect_err(|error| self.failed(error))?;
        let reason = match frame {
            Some(Frame::PublishAck {
                stream_id,
                message_id,
                durable_position,
                ..
            }) if stream_id == self.stream_id => {
                return Ok(CarrierReceive::Acknowledged {
                    message_id,
                    durable_position,
                });
            }
            Some(Frame::PublishError {
                stream_id,
                reason_code,
                message,
                ..
            }) if stream_id == self.stream_id => {
                return Ok(CarrierReceive::Refused {
                    reason_code,
                    reason: message,
                });
            }
            Some(Frame::LinkMessage {
                stream_id,
                subject,
                sealed,
                ..
            }) if stream_id == self.stream_id => {
                crate::auth_lys::SubjectBinding::new(
                    "wire".to_owned(),
                    subject.clone(),
                    subject.clone(),
                )?;
                return Ok(CarrierReceive::Sealed { subject, sealed });
            }
            Some(Frame::DisconnectReason { reason, .. }) => reason.name().to_owned(),
            Some(Frame::Disconnect { .. }) => "peer_disconnect".to_owned(),
            None => "wire_eof".to_owned(),
            Some(_) => {
                self.failed(&CarrierError::UnexpectedFrame);
                return Err(CarrierError::UnexpectedFrame);
            }
        };
        self.end_life(
            if reason == "grant_revoked" || reason == "revocation_log_reset" {
                crate::link::LinkEnd::Revoked
            } else {
                crate::link::LinkEnd::PeerGone
            },
        );
        self.ended = Some(reason.clone());
        Ok(CarrierReceive::End { reason })
    }
}

impl Drop for WireCarrier {
    fn drop(&mut self) {
        self.end_life(crate::link::LinkEnd::Shutdown);
        if let Some(reader) = self.reader.take() {
            reader.close(&self.socket);
        }
    }
}

/// An ordinary wire connection to the relay on its admitted relay stream.
#[derive(Debug)]
pub struct RelayCarrier {
    wire: WireCarrier,
    relay: crate::link_handshake::MachineIdentity,
}

impl RelayCarrier {
    /// Takes a connection whose relay stream has been granted and marked.
    ///
    /// # Errors
    /// Stream zero is reserved for connection control.
    pub fn new(
        socket: TcpStream,
        stream_id: u32,
        relay: crate::link_handshake::MachineIdentity,
    ) -> Result<Self, CarrierError> {
        Ok(Self {
            wire: WireCarrier::new(socket, stream_id)?,
            relay,
        })
    }

    /// Interrupts a blocking receive through the same owned socket.
    ///
    /// # Errors
    /// The socket cannot be duplicated.
    pub fn interrupt_handle(&self) -> Result<TcpStream, CarrierError> {
        self.wire.interrupt_handle()
    }

    /// Answers an imported frame; the relay carries the full receipt back.
    ///
    /// # Errors
    /// The carrier has ended or the ordinary connection cannot send the ack.
    pub fn acknowledge(
        &mut self,
        message_id: u64,
        durable_position: Option<u64>,
    ) -> Result<(), CarrierError> {
        self.reply(&Frame::PublishAck {
            flags: 0,
            stream_id: self.wire.stream_id,
            message_id,
            durable_position,
        })
    }

    /// Answers a named endpoint refusal on the relay stream.
    ///
    /// # Errors
    /// The carrier has ended or the ordinary connection cannot send the refusal.
    pub fn refuse(&mut self, reason_code: u16, reason: &str) -> Result<(), CarrierError> {
        self.reply(&Frame::PublishError {
            flags: 0,
            stream_id: self.wire.stream_id,
            reason_code,
            message: Some(reason.to_owned()),
        })
    }

    /// The topology record for the endpoint's observed relayed link.
    #[must_use]
    pub fn topology(
        &self,
        local: crate::link_handshake::MachineIdentity,
        peer: crate::link_handshake::MachineIdentity,
        counters: crate::console::records::LinkCounters,
    ) -> crate::console::records::Link {
        crate::console::records::Link::federated(
            &hex::encode(local.ed25519_public()),
            &hex::encode(peer.ed25519_public()),
            Some(crate::console::records::Carrier::Relay(hex::encode(
                self.relay.ed25519_public(),
            ))),
            crate::console::records::LinkState::Up,
            None,
            counters,
        )
    }

    fn reply(&mut self, frame: &Frame) -> Result<(), CarrierError> {
        if self.wire.ended.is_some() {
            return Err(CarrierError::Ended);
        }
        write_frame(&mut self.wire.socket, frame)
    }
}

impl LinkCarrier for RelayCarrier {
    fn refuse_unsolicited(&self) -> Result<(), CarrierError> {
        self.wire.refuse_unsolicited()
    }
    fn carrier(&self) -> Carrier {
        Carrier::Relay
    }
    fn send(&mut self, subject: &str, sealed: &[u8]) -> Result<(), CarrierError> {
        self.wire.send(subject, sealed)
    }
    fn receive(&mut self) -> Result<CarrierReceive, CarrierError> {
        self.wire.receive()
    }
}

/// The same bound is enforced before encoding and before allocating a peer's body.
pub(crate) const WIRE_FRAME_BOUND: usize = LimitsConfig::DEFAULT_MAX_CONNECTION_INBOX_BYTES;

pub(crate) fn write_frame(socket: &mut TcpStream, frame: &Frame) -> Result<(), CarrierError> {
    let bytes = codec::encoded_len(frame)?;
    if bytes > WIRE_FRAME_BOUND {
        return Err(CarrierError::TooLarge {
            bytes,
            bound: WIRE_FRAME_BOUND,
        });
    }
    let mut encoded = vec![0; bytes];
    let length = codec::encode(frame, &mut encoded)?;
    socket.write_all(&encoded[..length])?;
    Ok(())
}

pub(crate) fn read_frame(socket: &mut TcpStream) -> Result<Option<Frame>, CarrierError> {
    let mut header = [0; FrameHeader::WIRE_LEN];
    // Only EOF before a header is a clean end; an incomplete header/body is an error.
    loop {
        match socket.read(&mut header[..1]) {
            Ok(0) => return Ok(None),
            Ok(_) => break,
            Err(error) if error.kind() == std::io::ErrorKind::Interrupted => {}
            Err(error) => return Err(error.into()),
        }
    }
    socket.read_exact(&mut header[1..])?;
    let payload = u32::from_be_bytes([header[6], header[7], header[8], header[9]]);
    let bytes = usize::try_from(payload)
        .ok()
        .and_then(|length| length.checked_add(header.len()))
        .ok_or(CarrierError::TooLarge {
            bytes: usize::MAX,
            bound: WIRE_FRAME_BOUND,
        })?;
    if bytes > WIRE_FRAME_BOUND {
        return Err(CarrierError::TooLarge {
            bytes,
            bound: WIRE_FRAME_BOUND,
        });
    }
    let mut encoded = vec![0; bytes];
    encoded[..header.len()].copy_from_slice(&header);
    socket.read_exact(&mut encoded[header.len()..])?;
    let (frame, _) = codec::decode(&encoded)?;
    Ok(Some(frame))
}