auxon-sdk 2.3.0

A collection of clients, servers, protocols, and general API types for interacting with the APIs that are used throughout Auxon's suite of tools
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
#![cfg_attr(not(unix), allow(unused))]

use crate::api::{AttrKey, AttrVal};
use crate::mutation_plane::protocol::{
    LeafwardsMessage, RootwardsMessage, MUTATION_PROTOCOL_VERSION,
};
use crate::mutation_plane::types::{ParticipantId, TriggerCRDT};
use minicbor_io::{AsyncReader, AsyncWriter};
use std::collections::BTreeMap;
use tokio::net::TcpStream;
use tokio::sync::{broadcast, mpsc, oneshot};
use tokio_util::compat::{TokioAsyncReadCompatExt, TokioAsyncWriteCompatExt};

pub struct AuthReq {
    pub is_direct: bool,
    pub token: Vec<u8>,
    pub participant_id: ParticipantId,
    pub response_tx: oneshot::Sender<AuthResponse>,
}

#[derive(Debug)]
pub enum AuthResponse {
    DirectAuthOk {
        connection_id: ChildConnectionId,
        message: Option<String>,
        rootwards_tx: mpsc::Sender<Rootwards>,
        leafwards_rx: mpsc::Receiver<LeafwardsMessage>,
    },
    DelegatingAuthOk {
        message: Option<String>,
    },
    NotAuth {
        message: Option<String>,
    },
}
/// Opaque, internal-only id for child connections
#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq)]
pub struct ChildConnectionId(pub uuid::Uuid);

impl std::fmt::Display for ChildConnectionId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        uuid::fmt::Hyphenated::from_uuid(self.0).fmt(f)
    }
}

/// Messages from an already-authenticated child connection.
#[derive(Debug)]
pub enum Rootwards {
    // Probably need to send ChildConnectionId along with all Rootwards messages
    MutatorAnnouncement {
        connection_id: ChildConnectionId,
        /// Id of the participant in charge of managing the mutator
        participant_id: ParticipantId,
        mutator_id: crate::mutation_plane::types::MutatorId,
        mutator_attrs: BTreeMap<AttrKey, AttrVal>,
    },
    MutatorRetirement {
        connection_id: ChildConnectionId,
        /// Id of the participant in charge of managing the mutator
        participant_id: ParticipantId,
        mutator_id: crate::mutation_plane::types::MutatorId,
    },
    UpdateTriggerState {
        connection_id: ChildConnectionId,

        mutator_id: crate::mutation_plane::types::MutatorId,
        mutation_id: crate::mutation_plane::types::MutationId,
        /// Interpret "None" as "clear your trigger state for this mutation, you don't need to
        /// track it (anymore)"
        maybe_trigger_crdt: Option<TriggerCRDT>,
    },
}
impl Rootwards {
    pub fn connection_id(&self) -> ChildConnectionId {
        match self {
            Rootwards::MutatorAnnouncement { connection_id, .. } => *connection_id,
            Rootwards::MutatorRetirement { connection_id, .. } => *connection_id,
            Rootwards::UpdateTriggerState { connection_id, .. } => *connection_id,
        }
    }
}

pub async fn mutation_protocol_child_tcp_connection(
    mut stream: TcpStream,
    shutdown: broadcast::Receiver<()>,
    auth_tx: mpsc::Sender<AuthReq>,
) -> (
    Option<ChildConnectionId>,
    Result<(), Box<dyn std::error::Error>>,
) {
    let (reader, writer) = stream.split();
    let msg_reader = AsyncReader::new(reader.compat());
    let msg_writer = AsyncWriter::new(writer.compat_write());
    mutation_protocol_child_connection(msg_reader, msg_writer, shutdown, auth_tx).await
}

/// This code LOOKS the same as tcp_connection, but none of the TYPES are common. So we get to
/// duplicate it.
#[cfg(unix)]
pub async fn mutation_protocol_child_uds_connection(
    mut stream: tokio::net::UnixStream,
    shutdown: broadcast::Receiver<()>,
    auth_tx: mpsc::Sender<AuthReq>,
) -> (
    Option<ChildConnectionId>,
    Result<(), Box<dyn std::error::Error>>,
) {
    let (reader, writer) = stream.split();
    let msg_reader = AsyncReader::new(reader.compat());
    let msg_writer = AsyncWriter::new(writer.compat_write());
    mutation_protocol_child_connection(msg_reader, msg_writer, shutdown, auth_tx).await
}

pub async fn mutation_protocol_child_connection<R, W>(
    mut msg_reader: AsyncReader<R>,
    mut msg_writer: AsyncWriter<W>,
    mut shutdown_rx: broadcast::Receiver<()>,
    auth_tx: mpsc::Sender<AuthReq>,
) -> (
    Option<ChildConnectionId>,
    Result<(), Box<dyn std::error::Error>>,
)
where
    R: futures::AsyncRead + Unpin,
    W: futures::AsyncWrite + Unpin,
{
    let mut unauth_state = UnauthenticatedConnectionState { auth_tx };
    let mut ready_state = loop {
        tokio::select! {
            msg = msg_reader.read::<RootwardsMessage>() => {
                let msg = match msg {
                    Ok(Some(msg)) => msg,
                    Ok(None) => return (None, Ok(())),
                    Err(minicbor_io::Error::Decode(e)) => {
                        tracing::error!(
                            error = &e as &dyn std::error::Error,
                            "Dropping invalid message during unauth state"
                        );
                        continue;
                    }
                    Err(e) => return (None, Err(e.into())),
                };

                match unauth_state.handle_rootwards_message(msg).await {
                    UnauthenticatedMessageOutcome::Proceed { state, reply } => {
                        if let Err(e) = msg_writer.write(reply).await {
                            return (Some(state.connection_id), Err(e.into()));
                        }
                        break state;
                    }
                    UnauthenticatedMessageOutcome::StayPut { state, reply } => {
                        if let Err(e) = msg_writer.write(reply).await {
                            return (None, Err(e.into()));
                        }
                        unauth_state = state;
                    }
                }
            },
            _ = shutdown_rx.recv() => {
                tracing::info!("Mutation protocol child connection received shutdown request while still unauthenticated.");
                return (None, Ok(()))
            }
        }
    };
    tracing::trace!("Mutation protocol client authenticated");

    loop {
        tokio::select! {
            // Pull from the channel coming from parent
            maybe_leafwards = ready_state.leafwards_rx.recv() => {
                match maybe_leafwards {
                    Some(leafwards) => {
                        if let Err(e) = msg_writer.write(leafwards).await {
                            return (Some(ready_state.connection_id), Err(e.into()));
                        }
                    },
                    None => {
                        tracing::warn!("Internal leafwards channel closed early unexpectedly for mutation protocol child connection.");
                        return (Some(ready_state.connection_id), Ok(()));
                    }
                }
            },
            // Pull from the network coming from child
            maybe_rootwards_result = msg_reader.read::<RootwardsMessage>() => {
                let msg: RootwardsMessage = match maybe_rootwards_result {
                    Ok(Some(msg)) => msg,
                    Ok(None) => return (Some(ready_state.connection_id), Ok(())),
                    Err(minicbor_io::Error::Decode(e)) => {
                        tracing::error!(error = &e as &dyn std::error::Error, "Dropping invalid message during ready state.");
                        continue;
                    }
                    Err(e) => return (Some(ready_state.connection_id), Err(e.into())),
                };
                let ReadyMessageOutcome {
                    reply_to_child, send_to_root
                } = ready_state.handle_rootwards_message(msg).await;
                if let Some(reply) = reply_to_child {
                    if let Err(e) = msg_writer.write(reply).await {
                        return (Some(ready_state.connection_id), Err(e.into()));
                    }
                }
                if let Some(rootwards) = send_to_root {
                    if let Err(e) = ready_state.rootwards_tx.send(rootwards).await {
                        tracing::error!(error = &e as &dyn std::error::Error, "Could not send rootwards message from child connection over internal channel.");
                    }
                }
            },
            _ = shutdown_rx.recv() => {
                tracing::info!("Mutation protocol child connection received shutdown request while in the ready state");
                return (Some(ready_state.connection_id), Ok(()))
            }
        }
    }
}
/// After handling a message, a connection can stay unauthenticated, or can move forward to 'ready'.
enum UnauthenticatedMessageOutcome {
    Proceed {
        state: ReadyConnectionState,
        reply: LeafwardsMessage,
    },
    StayPut {
        state: UnauthenticatedConnectionState,
        reply: LeafwardsMessage,
    },
}
struct UnauthenticatedConnectionState {
    auth_tx: tokio::sync::mpsc::Sender<AuthReq>,
}

impl UnauthenticatedConnectionState {
    async fn handle_rootwards_message(
        self,
        msg: RootwardsMessage,
    ) -> UnauthenticatedMessageOutcome {
        match msg {
            RootwardsMessage::ChildAuthAttempt {
                child_participant_id,
                version,
                token,
            } => {
                tracing::debug!(version = version, participant_id = %child_participant_id, "Auth attempt from unauthorized child connection");
                let (response_tx, response_rx) = tokio::sync::oneshot::channel();
                if self
                    .auth_tx
                    .send(AuthReq {
                        // We are trying to auth the child participant
                        // sitting at the top of the participant tree directly
                        // on the other side of this network connection
                        is_direct: true,
                        token,
                        participant_id: child_participant_id,
                        response_tx,
                    })
                    .await
                    .is_err()
                {
                    UnauthenticatedMessageOutcome::StayPut {
                        state: self,
                        reply: LeafwardsMessage::ChildAuthOutcome {
                            child_participant_id,
                            version: MUTATION_PROTOCOL_VERSION,
                            ok: false,
                            message: Some(
                                "Could not send auth request over internal channel".to_owned(),
                            ),
                        },
                    }
                } else {
                    match response_rx.await {
                        Ok(resp) => {
                            match resp {
                                AuthResponse::DirectAuthOk { connection_id, message, rootwards_tx, leafwards_rx } => {
                                    UnauthenticatedMessageOutcome::Proceed {
                                        state: ReadyConnectionState {
                                            connection_id,
                                            auth_tx: self.auth_tx,
                                            leafwards_rx,
                                            rootwards_tx
                                        },
                                        reply: LeafwardsMessage::ChildAuthOutcome {
                                            child_participant_id,
                                            version: MUTATION_PROTOCOL_VERSION,
                                            ok: true,
                                            message
                                        }
                                    }
                                }
                                AuthResponse::DelegatingAuthOk { message } => {
                                    UnauthenticatedMessageOutcome::StayPut {
                                        state: self,
                                        reply: LeafwardsMessage::ChildAuthOutcome {
                                            child_participant_id,
                                            version: MUTATION_PROTOCOL_VERSION,
                                            ok: true,
                                            message
                                        }
                                    }
                                }
                                AuthResponse::NotAuth { message } => {
                                    UnauthenticatedMessageOutcome::StayPut { state: self, reply: LeafwardsMessage::ChildAuthOutcome {
                                        child_participant_id,
                                        version: MUTATION_PROTOCOL_VERSION,
                                        ok: false,
                                        message
                                    } }
                                }
                            }
                        },
                        Err(_recv_err) => {
                            UnauthenticatedMessageOutcome::StayPut { state: self, reply: LeafwardsMessage::ChildAuthOutcome {
                                child_participant_id,
                                version: MUTATION_PROTOCOL_VERSION,
                                ok: false,
                                message:Some("Mutation plane child connection could not receive auth request over internal channel.".to_owned())
                            } }
                        }
                    }
                }
            }
            _ => UnauthenticatedMessageOutcome::StayPut {
                state: self,
                reply: LeafwardsMessage::UnauthenticatedResponse {},
            },
        }
    }
}

struct ReadyConnectionState {
    connection_id: ChildConnectionId,
    auth_tx: tokio::sync::mpsc::Sender<AuthReq>,
    leafwards_rx: tokio::sync::mpsc::Receiver<LeafwardsMessage>,
    rootwards_tx: tokio::sync::mpsc::Sender<Rootwards>,
}

struct ReadyMessageOutcome {
    reply_to_child: Option<LeafwardsMessage>,
    send_to_root: Option<Rootwards>, // Maybe upwards things? Maybe deal with it all inline?
}

impl ReadyConnectionState {
    async fn handle_rootwards_message(&mut self, msg: RootwardsMessage) -> ReadyMessageOutcome {
        match msg {
            RootwardsMessage::ChildAuthAttempt {
                child_participant_id,
                version,
                token,
            } => {
                tracing::debug!(version = version, participant_id = %child_participant_id, "Auth attempt from already-authorized child connection");
                let (response_tx, response_rx) = tokio::sync::oneshot::channel();
                if self
                    .auth_tx
                    .send(AuthReq {
                        // We are passing along an auth request from a further descendant in the
                        // participant tree.
                        is_direct: false,
                        token,
                        participant_id: child_participant_id,
                        response_tx,
                    })
                    .await
                    .is_err()
                {
                    ReadyMessageOutcome {
                        reply_to_child: Some(LeafwardsMessage::ChildAuthOutcome {
                            child_participant_id,
                            version: MUTATION_PROTOCOL_VERSION,
                            ok: false,
                            message: Some(
                                "Could not send auth request over internal channel".to_owned(),
                            ),
                        }),
                        send_to_root: None,
                    }
                } else {
                    match response_rx.await {
                        Ok(resp) => {
                            match resp {
                                AuthResponse::DirectAuthOk { connection_id: _, message, rootwards_tx: _, leafwards_rx : _} => {
                                    ReadyMessageOutcome {
                                        reply_to_child: Some(LeafwardsMessage::ChildAuthOutcome {
                                            child_participant_id,
                                            version: MUTATION_PROTOCOL_VERSION,
                                            ok: true,
                                            message
                                        }),
                                        send_to_root: None
                                    }
                                }
                                AuthResponse::DelegatingAuthOk { message } => {
                                    ReadyMessageOutcome {
                                        reply_to_child: Some(LeafwardsMessage::ChildAuthOutcome {
                                            child_participant_id,
                                            version: MUTATION_PROTOCOL_VERSION,
                                            ok: true,
                                            message
                                        }),
                                        send_to_root: None
                                    }
                                }
                                AuthResponse::NotAuth { message } => {
                                    ReadyMessageOutcome { reply_to_child: Some(LeafwardsMessage::ChildAuthOutcome {
                                        child_participant_id,
                                        version: MUTATION_PROTOCOL_VERSION,
                                        ok: false,
                                        message
                                    }),
                                        send_to_root: None
                                    }
                                }
                            }
                        },
                        Err(_recv_err) => {
                            ReadyMessageOutcome { reply_to_child: Some(LeafwardsMessage::ChildAuthOutcome {
                                child_participant_id,
                                version: MUTATION_PROTOCOL_VERSION,
                                ok: false,
                                message:Some("Mutation plane child connection could not receive auth request over internal channel.".to_owned())
                            }),
                                send_to_root: None
                            }
                        }
                    }
                }
            }
            RootwardsMessage::MutatorAnnouncement {
                participant_id,
                mutator_id,
                mutator_attrs,
            } => ReadyMessageOutcome {
                reply_to_child: None,
                send_to_root: Some(Rootwards::MutatorAnnouncement {
                    connection_id: self.connection_id,
                    participant_id,
                    mutator_id,
                    mutator_attrs: mutator_attrs
                        .0
                        .into_iter()
                        .map(|kv| (AttrKey::from(kv.key), kv.value))
                        .collect(),
                }),
            },
            RootwardsMessage::MutatorRetirement {
                participant_id,
                mutator_id,
            } => ReadyMessageOutcome {
                reply_to_child: None,
                send_to_root: Some(Rootwards::MutatorRetirement {
                    connection_id: self.connection_id,
                    participant_id,
                    mutator_id,
                }),
            },
            RootwardsMessage::UpdateTriggerState {
                mutator_id,
                mutation_id,
                maybe_trigger_crdt,
            } => ReadyMessageOutcome {
                reply_to_child: None,
                send_to_root: Some(Rootwards::UpdateTriggerState {
                    connection_id: self.connection_id,
                    mutator_id,
                    mutation_id,
                    maybe_trigger_crdt,
                }),
            },
        }
    }
}