datum-mq 0.10.4

Kafka sources and sinks for Datum streams, backed by rdkafka
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
use std::{collections::VecDeque, fmt, sync::Arc, time::Duration};

use tokio::{
    io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt},
    net::TcpStream,
    sync::{Mutex, mpsc, oneshot},
    time,
};
use tokio_rustls::{TlsConnector, rustls::pki_types::ServerName};

use crate::native::{
    KafkaClientError, KafkaClientResult,
    protocol::{
        API_KEY_SASL_AUTHENTICATE, API_KEY_SASL_HANDSHAKE, Decoder, Encoder,
        SaslAuthenticateResponse, SaslHandshakeResponse, encode_sasl_authenticate_request,
        encode_sasl_handshake_request, request_header_version, response_header_version,
    },
    security::{NativeSecurityConfig, SaslConfig, SaslMechanism, ScramClient},
};

const DEFAULT_COMMAND_BUFFER: usize = 64;
const MAX_RESPONSE_BYTES: usize = 512 * 1024 * 1024;

#[derive(Debug, Clone)]
pub(crate) struct BrokerConnection {
    sender: mpsc::Sender<Command>,
    broker: String,
    sasl: Option<SaslConfig>,
    auth: Arc<Mutex<AuthState>>,
}

trait BrokerIo: AsyncRead + AsyncWrite + Unpin + Send {}
impl<T> BrokerIo for T where T: AsyncRead + AsyncWrite + Unpin + Send {}

#[derive(Debug, Default)]
struct AuthState {
    handshake_version: Option<i16>,
    authenticate_version: Option<i16>,
    authenticated: bool,
    reauthenticate_at: Option<time::Instant>,
}

#[derive(Debug)]
pub(crate) struct BrokerResponse {
    receiver: oneshot::Receiver<KafkaClientResult<Vec<u8>>>,
}

#[derive(Debug)]
struct Command {
    api_key: i16,
    api_version: i16,
    request_header_version: i16,
    response_header_version: i16,
    body: Vec<u8>,
    expect_response: bool,
    respond: oneshot::Sender<KafkaClientResult<Vec<u8>>>,
}

#[derive(Debug)]
struct Pending {
    correlation_id: i32,
    response_header_version: i16,
    respond: oneshot::Sender<KafkaClientResult<Vec<u8>>>,
}

impl BrokerConnection {
    pub(crate) async fn connect(
        addr: String,
        broker: String,
        client_id: String,
        max_in_flight: usize,
        request_timeout: Duration,
        security: &NativeSecurityConfig,
    ) -> KafkaClientResult<Self> {
        let stream = time::timeout(request_timeout, TcpStream::connect(&addr))
            .await
            .map_err(|_| {
                KafkaClientError::Io(std::io::Error::new(
                    std::io::ErrorKind::TimedOut,
                    format!("Kafka TCP connect to {addr} timed out"),
                ))
            })??;
        stream.set_nodelay(true)?;
        let stream: Box<dyn BrokerIo> = if let Some(tls) = &security.tls {
            let server_name =
                ServerName::try_from(broker.clone()).map_err(|error| KafkaClientError::Tls {
                    broker: broker.clone(),
                    message: format!("invalid TLS SNI name: {error}"),
                })?;
            let connector = TlsConnector::from(Arc::clone(tls));
            let tls_stream = time::timeout(request_timeout, connector.connect(server_name, stream))
                .await
                .map_err(|_| KafkaClientError::Tls {
                    broker: broker.clone(),
                    message: "TLS handshake timed out".to_owned(),
                })?
                .map_err(|error| KafkaClientError::Tls {
                    broker: broker.clone(),
                    message: error.to_string(),
                })?;
            Box::new(tls_stream)
        } else {
            Box::new(stream)
        };
        let (sender, receiver) = mpsc::channel(DEFAULT_COMMAND_BUFFER);
        tokio::spawn(connection_task(
            stream,
            receiver,
            client_id,
            max_in_flight.max(1),
            request_timeout,
        ));
        Ok(Self {
            sender,
            broker,
            sasl: security.sasl.clone(),
            auth: Arc::new(Mutex::new(AuthState::default())),
        })
    }

    pub(crate) async fn initialize_sasl(
        &self,
        handshake_version: i16,
        authenticate_version: i16,
    ) -> KafkaClientResult<()> {
        let Some(sasl) = self.sasl.clone() else {
            return Ok(());
        };
        let mut auth = self.auth.lock().await;
        auth.handshake_version = Some(handshake_version);
        auth.authenticate_version = Some(authenticate_version);
        self.authenticate(&sasl, &mut auth).await
    }

    pub(crate) async fn request_before_auth(
        &self,
        api_key: i16,
        api_version: i16,
        request_header_version: i16,
        response_header_version: i16,
        body: Vec<u8>,
    ) -> KafkaClientResult<Vec<u8>> {
        self.raw_request(
            api_key,
            api_version,
            request_header_version,
            response_header_version,
            body,
        )
        .await
    }

    pub(crate) async fn request(
        &self,
        api_key: i16,
        api_version: i16,
        request_header_version: i16,
        response_header_version: i16,
        body: Vec<u8>,
    ) -> KafkaClientResult<Vec<u8>> {
        self.ensure_authenticated().await?;
        self.raw_request(
            api_key,
            api_version,
            request_header_version,
            response_header_version,
            body,
        )
        .await
    }

    /// Enqueues a request and returns before its response arrives. Kafka
    /// responses remain correlated and delivered in connection order by the
    /// single-owner carrier task.
    pub(crate) async fn begin_request(
        &self,
        api_key: i16,
        api_version: i16,
        request_header_version: i16,
        response_header_version: i16,
        body: Vec<u8>,
    ) -> KafkaClientResult<BrokerResponse> {
        self.ensure_authenticated().await?;
        self.raw_begin_request(
            api_key,
            api_version,
            request_header_version,
            response_header_version,
            body,
        )
        .await
    }

    async fn raw_begin_request(
        &self,
        api_key: i16,
        api_version: i16,
        request_header_version: i16,
        response_header_version: i16,
        body: Vec<u8>,
    ) -> KafkaClientResult<BrokerResponse> {
        let receiver = self
            .begin(
                api_key,
                api_version,
                request_header_version,
                response_header_version,
                body,
                true,
            )
            .await?;
        Ok(BrokerResponse { receiver })
    }

    /// Writes a Kafka request for an API mode that deliberately returns no
    /// response, notably Produce with `acks=0`.
    pub(crate) async fn send_one_way(
        &self,
        api_key: i16,
        api_version: i16,
        request_header_version: i16,
        body: Vec<u8>,
    ) -> KafkaClientResult<()> {
        self.ensure_authenticated().await?;
        self.begin(api_key, api_version, request_header_version, 0, body, false)
            .await?
            .await
            .map_err(|_| KafkaClientError::ChannelClosed)?
            .map(|_| ())
    }

    async fn raw_request(
        &self,
        api_key: i16,
        api_version: i16,
        request_header_version: i16,
        response_header_version: i16,
        body: Vec<u8>,
    ) -> KafkaClientResult<Vec<u8>> {
        self.raw_begin_request(
            api_key,
            api_version,
            request_header_version,
            response_header_version,
            body,
        )
        .await?
        .receive()
        .await
    }

    async fn ensure_authenticated(&self) -> KafkaClientResult<()> {
        let Some(sasl) = self.sasl.clone() else {
            return Ok(());
        };
        let mut auth = self.auth.lock().await;
        if auth.authenticated
            && auth
                .reauthenticate_at
                .is_none_or(|deadline| deadline > time::Instant::now())
        {
            return Ok(());
        }
        self.authenticate(&sasl, &mut auth).await
    }

    async fn authenticate(&self, sasl: &SaslConfig, auth: &mut AuthState) -> KafkaClientResult<()> {
        let handshake_version = auth.handshake_version.ok_or_else(|| {
            self.sasl_error(sasl.mechanism, "SASL API versions were not negotiated")
        })?;
        let authenticate_version = auth.authenticate_version.ok_or_else(|| {
            self.sasl_error(sasl.mechanism, "SASL API versions were not negotiated")
        })?;
        let body = encode_sasl_handshake_request(sasl.mechanism.name())
            .map_err(|error| self.sasl_error(sasl.mechanism, error))?;
        let response = self
            .raw_request(
                API_KEY_SASL_HANDSHAKE,
                handshake_version,
                request_header_version(handshake_version),
                response_header_version(API_KEY_SASL_HANDSHAKE, handshake_version),
                body,
            )
            .await
            .map_err(|error| self.sasl_error(sasl.mechanism, error))?;
        let handshake = SaslHandshakeResponse::decode(handshake_version, &response)
            .map_err(|error| self.sasl_error(sasl.mechanism, error))?;
        if handshake.error_code != 0 {
            return Err(self.sasl_error(
                sasl.mechanism,
                format!(
                    "SaslHandshake returned broker code {} (advertised: {})",
                    handshake.error_code,
                    handshake.mechanisms.join(", ")
                ),
            ));
        }
        if !handshake
            .mechanisms
            .iter()
            .any(|mechanism| mechanism == sasl.mechanism.name())
        {
            return Err(self.sasl_error(
                sasl.mechanism,
                format!(
                    "broker did not advertise the selected mechanism (advertised: {})",
                    handshake.mechanisms.join(", ")
                ),
            ));
        }

        let session_lifetime_ms = match sasl.mechanism {
            SaslMechanism::Plain => {
                if sasl.username.contains('\0') || sasl.password.contains('\0') {
                    return Err(self.sasl_error(
                        sasl.mechanism,
                        "PLAIN credentials must not contain NUL bytes",
                    ));
                }
                let mut token = Vec::with_capacity(sasl.username.len() + sasl.password.len() + 2);
                token.push(0);
                token.extend_from_slice(sasl.username.as_bytes());
                token.push(0);
                token.extend_from_slice(sasl.password.as_bytes());
                self.authenticate_token(sasl.mechanism, authenticate_version, &token)
                    .await?
                    .session_lifetime_ms
            }
            SaslMechanism::ScramSha256 | SaslMechanism::ScramSha512 => {
                let client = ScramClient::new(sasl)
                    .map_err(|error| self.sasl_error(sasl.mechanism, error))?;
                let first = self
                    .authenticate_token(
                        sasl.mechanism,
                        authenticate_version,
                        client.first_message().as_bytes(),
                    )
                    .await?;
                let server_first = std::str::from_utf8(&first.auth_bytes).map_err(|_| {
                    self.sasl_error(sasl.mechanism, "SCRAM server-first message was not UTF-8")
                })?;
                let (client_final, verifier) = client
                    .handle_server_first(server_first)
                    .map_err(|error| self.sasl_error(sasl.mechanism, error))?;
                let final_response = self
                    .authenticate_token(
                        sasl.mechanism,
                        authenticate_version,
                        client_final.as_bytes(),
                    )
                    .await?;
                let server_final =
                    std::str::from_utf8(&final_response.auth_bytes).map_err(|_| {
                        self.sasl_error(sasl.mechanism, "SCRAM server-final message was not UTF-8")
                    })?;
                verifier
                    .verify(server_final)
                    .map_err(|error| self.sasl_error(sasl.mechanism, error))?;
                final_response.session_lifetime_ms
            }
        };
        auth.authenticated = true;
        auth.reauthenticate_at = reauthentication_deadline(session_lifetime_ms);
        Ok(())
    }

    async fn authenticate_token(
        &self,
        mechanism: SaslMechanism,
        version: i16,
        token: &[u8],
    ) -> KafkaClientResult<SaslAuthenticateResponse> {
        let body = encode_sasl_authenticate_request(token)
            .map_err(|error| self.sasl_error(mechanism, error))?;
        let response = self
            .raw_request(
                API_KEY_SASL_AUTHENTICATE,
                version,
                request_header_version(version),
                response_header_version(API_KEY_SASL_AUTHENTICATE, version),
                body,
            )
            .await
            .map_err(|error| self.sasl_error(mechanism, error))?;
        let response = SaslAuthenticateResponse::decode(version, &response)
            .map_err(|error| self.sasl_error(mechanism, error))?;
        if response.error_code != 0 {
            return Err(self.sasl_error(
                mechanism,
                response.error_message.as_deref().map_or_else(
                    || {
                        format!(
                            "SaslAuthenticate returned broker code {}",
                            response.error_code
                        )
                    },
                    |message| {
                        format!(
                            "SaslAuthenticate returned broker code {}: {message}",
                            response.error_code
                        )
                    },
                ),
            ));
        }
        Ok(response)
    }

    fn sasl_error(&self, mechanism: SaslMechanism, message: impl fmt::Display) -> KafkaClientError {
        KafkaClientError::Sasl {
            broker: self.broker.clone(),
            mechanism: mechanism.name().to_owned(),
            message: message.to_string(),
        }
    }

    async fn begin(
        &self,
        api_key: i16,
        api_version: i16,
        request_header_version: i16,
        response_header_version: i16,
        body: Vec<u8>,
        expect_response: bool,
    ) -> KafkaClientResult<oneshot::Receiver<KafkaClientResult<Vec<u8>>>> {
        let (respond, receiver) = oneshot::channel();
        let command = Command {
            api_key,
            api_version,
            request_header_version,
            response_header_version,
            body,
            expect_response,
            respond,
        };
        self.sender
            .send(command)
            .await
            .map_err(|_| KafkaClientError::ChannelClosed)?;
        Ok(receiver)
    }
}

fn reauthentication_deadline(session_lifetime_ms: i64) -> Option<time::Instant> {
    let lifetime_ms = u64::try_from(session_lifetime_ms)
        .ok()
        .filter(|value| *value > 0)?;
    let margin_ms = (lifetime_ms / 10).clamp(1, 30_000);
    Some(time::Instant::now() + Duration::from_millis(lifetime_ms.saturating_sub(margin_ms)))
}

impl BrokerResponse {
    pub(crate) async fn receive(self) -> KafkaClientResult<Vec<u8>> {
        self.receiver
            .await
            .map_err(|_| KafkaClientError::ChannelClosed)?
    }
}

async fn connection_task(
    mut stream: Box<dyn BrokerIo>,
    mut receiver: mpsc::Receiver<Command>,
    client_id: String,
    max_in_flight: usize,
    request_timeout: Duration,
) {
    let mut correlation_id = 1_i32;
    let mut pending = VecDeque::<Pending>::new();
    let mut receiving_commands = true;
    loop {
        if !receiving_commands {
            if pending.is_empty() {
                break;
            }
            match read_response(&mut stream, &mut pending, request_timeout).await {
                Ok(()) => continue,
                Err(error) => {
                    fail_all(&mut pending, error);
                    break;
                }
            }
        }
        if pending.len() >= max_in_flight {
            match read_response(&mut stream, &mut pending, request_timeout).await {
                Ok(()) => continue,
                Err(error) => {
                    fail_all(&mut pending, error);
                    break;
                }
            }
        }

        tokio::select! {
            command = receiver.recv(), if receiving_commands => {
                let Some(command) = command else {
                    receiving_commands = false;
                    continue;
                };
                let request_correlation = correlation_id;
                correlation_id = if correlation_id == i32::MAX { 1 } else { correlation_id + 1 };
                match write_request(&mut stream, &client_id, request_correlation, &command).await {
                    Ok(()) => {
                        if command.expect_response {
                            pending.push_back(Pending {
                                correlation_id: request_correlation,
                                response_header_version: command.response_header_version,
                                respond: command.respond,
                            });
                        } else {
                            let _ = command.respond.send(Ok(Vec::new()));
                        }
                    }
                    Err(error) => {
                        let _ = command.respond.send(Err(error));
                        fail_all(&mut pending, KafkaClientError::ChannelClosed);
                        break;
                    }
                }
            }
            result = read_response(&mut stream, &mut pending, request_timeout), if !pending.is_empty() => {
                if let Err(error) = result {
                    fail_all(&mut pending, error);
                    break;
                }
            }
        }
    }
}

async fn write_request<S>(
    stream: &mut S,
    client_id: &str,
    correlation_id: i32,
    command: &Command,
) -> KafkaClientResult<()>
where
    S: AsyncWrite + Unpin + ?Sized,
{
    let mut frame = Encoder::with_capacity(command.body.len() + 64);
    frame.put_i16(command.api_key);
    frame.put_i16(command.api_version);
    frame.put_i32(correlation_id);
    frame.put_nullable_string(Some(client_id))?;
    if command.request_header_version >= 2 {
        frame.put_empty_tags();
    }
    frame.put_raw(&command.body);
    let frame = frame.into_inner();
    let len = i32::try_from(frame.len())
        .map_err(|_| KafkaClientError::protocol("Kafka request frame exceeds int32 length"))?;
    stream.write_all(&len.to_be_bytes()).await?;
    stream.write_all(&frame).await?;
    Ok(())
}

async fn read_response<S>(
    stream: &mut S,
    pending: &mut VecDeque<Pending>,
    request_timeout: Duration,
) -> KafkaClientResult<()>
where
    S: AsyncRead + Unpin + ?Sized,
{
    let mut len_buf = [0_u8; 4];
    time::timeout(request_timeout, stream.read_exact(&mut len_buf))
        .await
        .map_err(|_| KafkaClientError::protocol("Kafka response read timed out"))??;
    let len = i32::from_be_bytes(len_buf);
    if len < 4 {
        return Err(KafkaClientError::protocol(format!(
            "invalid Kafka response frame length {len}"
        )));
    }
    let len = usize::try_from(len)
        .map_err(|_| KafkaClientError::protocol("Kafka response length conversion failed"))?;
    if len > MAX_RESPONSE_BYTES {
        return Err(KafkaClientError::protocol(format!(
            "Kafka response frame too large: {len} bytes"
        )));
    }
    let mut frame = vec![0_u8; len];
    time::timeout(request_timeout, stream.read_exact(&mut frame))
        .await
        .map_err(|_| KafkaClientError::protocol("Kafka response body read timed out"))??;
    let Some(pending_request) = pending.pop_front() else {
        return Err(KafkaClientError::protocol(
            "Kafka response arrived with no pending request",
        ));
    };
    let mut decoder = Decoder::new(&frame);
    let correlation_id = decoder.get_i32()?;
    if pending_request.response_header_version >= 1 {
        decoder.skip_tags()?;
    }
    if correlation_id != pending_request.correlation_id {
        let _ = pending_request
            .respond
            .send(Err(KafkaClientError::protocol(format!(
                "Kafka response correlation mismatch: expected {}, got {correlation_id}",
                pending_request.correlation_id
            ))));
        return Err(KafkaClientError::protocol(
            "Kafka connection correlation-id mismatch",
        ));
    }
    let body = decoder.take(decoder.remaining())?.to_vec();
    let _ = pending_request.respond.send(Ok(body));
    Ok(())
}

fn fail_all(pending: &mut VecDeque<Pending>, error: KafkaClientError) {
    let retriable = error.retriable();
    let message = error.to_string();
    while let Some(pending) = pending.pop_front() {
        let error = if retriable {
            KafkaClientError::ChannelClosed
        } else {
            KafkaClientError::protocol(message.clone())
        };
        let _ = pending.respond.send(Err(error));
    }
}