ic-bn-lib 0.10.2

Internet Computer Boundary Nodes shared modules
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
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
use std::{net::SocketAddr, sync::Arc, time::Instant};

use tokio::io::AsyncWriteExt;
use tokio_rustls::server::TlsStream;
use tokio_util::sync::CancellationToken;
use tracing::{debug, info};

use crate::{
    IpFamily as _,
    network::{AsyncReadWrite, tls_handshake},
    smtp::{
        Metrics,
        inbound::{
            Session, SessionConfig, SessionData, SessionError, SessionResult, SessionTlsMode,
            SessionUpgrade,
        },
    },
};

/// Manages the lifetime of a single SMTP session.
///
/// It's needed because the SMTP session can transition into TLS state
/// which requires external orchestration.
pub struct SessionManager;

impl SessionManager {
    pub async fn handle_connection<S: AsyncReadWrite>(
        stream: S,
        remote_addr: SocketAddr,
        params: Arc<SessionConfig>,
        metrics: Metrics,
        shutdown_token: CancellationToken,
    ) {
        // Convert v6-mapped address to v4
        let remote_ip = remote_addr.ip().to_canonical();
        let mut session = Session::new(remote_ip, stream, params, metrics);

        session
            .metrics
            .sessions_open
            .with_label_values(&[remote_ip.family()])
            .inc();

        match session.handle(shutdown_token.child_token()).await {
            Ok(v) => match v {
                SessionUpgrade::No => {
                    Self::notify(&session, None).await;
                    session.stream.shutdown().await.ok();
                }
                SessionUpgrade::StartTls => {
                    Self::handle_connection_tls(session, shutdown_token.child_token()).await
                }
            },

            Err(e) => {
                if !matches!(e, SessionError::Quit) {
                    info!("{session}: error: {e:#}");
                }

                if let Err(e) = session.shutdown().await {
                    debug!("{session}: error closing connection: {e:#}");
                };

                Self::notify(&session, Some(e)).await;
            }
        }
    }

    /// Converts session into TLS mode & runs it
    async fn handle_connection_tls<S: AsyncReadWrite>(
        session: Session<S>,
        shutdown_token: CancellationToken,
    ) {
        let session_name = session.to_string();
        debug!("{session}: starting TLS handshake");

        match session.into_tls().await {
            Ok(mut session) => {
                debug!("{session}: TLS handshake succeeded");

                if let Err(e) = session.handle(shutdown_token.child_token()).await {
                    if !matches!(e, SessionError::Quit) {
                        info!("{session}: error: {e:#}");
                    }

                    if let Err(e) = session.shutdown().await {
                        debug!("{session}: error closing connection: {e:#}");
                    };

                    Self::notify(&session, Some(e)).await;
                }
            }

            Err(e) => {
                info!("{session_name}: TLS handshake failed: {e:#}");
            }
        };
    }

    async fn notify<S: AsyncReadWrite>(session: &Session<S>, error: Option<SessionError>) {
        let ip_family = session.remote_ip.family();

        let tls_proto = session
            .tls_info
            .as_ref()
            .map_or("", |x| x.protocol.as_str().unwrap_or_default());
        let error_lbl: &'static str = error.as_ref().map_or("", |x| x.into());
        session
            .metrics
            .sessions_processed
            .with_label_values(&[ip_family, tls_proto, error_lbl])
            .inc();
        session
            .metrics
            .sessions_open
            .with_label_values(&[ip_family])
            .dec();
        session
            .metrics
            .session_duration
            .with_label_values(&[ip_family, tls_proto])
            .observe(
                Instant::now()
                    .duration_since(session.counters.started)
                    .as_secs_f64(),
            );

        if let Some(v) = session.cfg.notifications_handler.clone() {
            let meta = session.meta();
            tokio::spawn(async move {
                v.notify_session_finish(meta, error).await;
            });
        }
    }
}

impl<S: AsyncReadWrite> Session<S> {
    /// Converts the plain-text session into a TLS one by doing a TLS handshake
    pub async fn into_tls(self) -> SessionResult<Session<TlsStream<S>>> {
        let ip_family = self.remote_ip.family();

        // SAFETY: We should end up here only if TLS is enabled.
        // It's better to panic otherwise.
        let tls_config = match &self.cfg.tls_mode {
            SessionTlsMode::Allowed(v) | SessionTlsMode::Required(v) => v.clone(),
            SessionTlsMode::Disabled => {
                unreachable!("Session::into_tls() called with TLS disabled")
            }
        };

        let meta = self.meta();
        let (stream, tls_info) = match tls_handshake(tls_config, self.stream).await {
            Ok(v) => v,
            Err(e) => {
                let error = SessionError::TlsHandshakeFailed(e.to_string());
                let error_str = e.to_string();
                let error_lbl: &'static str = (&error).into();

                // Session is partially consumed by `tls_handshake`, so we can't use `Manager::notify()`
                self.metrics
                    .sessions_processed
                    .with_label_values(&[ip_family, "", error_lbl])
                    .inc();
                self.metrics
                    .sessions_open
                    .with_label_values(&[ip_family])
                    .dec();
                self.metrics
                    .session_duration
                    .with_label_values(&[ip_family, ""])
                    .observe(
                        Instant::now()
                            .duration_since(self.counters.started)
                            .as_secs_f64(),
                    );

                if let Some(v) = self.cfg.notifications_handler.clone() {
                    tokio::spawn(async move {
                        v.notify_session_finish(
                            meta,
                            Some(SessionError::TlsHandshakeFailed(error_str)),
                        )
                        .await;
                    });
                }

                return Err(error);
            }
        };

        let tls_proto = tls_info.protocol.as_str().unwrap_or_default();

        Ok(Session {
            id: self.id,
            remote_ip: self.remote_ip,
            stream,
            state: self.state,
            // According to the RFC we need to discard all session data
            // after switching into TLS mode.
            // https://datatracker.ietf.org/doc/html/rfc3207#section-4.2
            data: SessionData::default(),
            counters: self.counters,
            cfg: self.cfg,
            tls_info: Some(tls_info),
            labels: [ip_family, tls_proto],
            metrics: self.metrics,
        })
    }
}

#[cfg(test)]
mod test {
    use std::{
        net::{IpAddr, Ipv4Addr},
        str::FromStr,
        time::Duration,
    };

    use async_trait::async_trait;
    use fqdn::FQDN;
    use prometheus::Registry;
    use rustls::{ClientConfig, ProtocolVersion, pki_types::ServerName};
    use tokio::{
        io::{AsyncReadExt, duplex},
        sync::mpsc,
    };
    use tokio_rustls::TlsConnector;

    use crate::{
        smtp::{
            EmailMessage, MessageError, ProtocolError, ReceivesSmtpNotifications, SessionMeta,
            inbound::mail_from::test::{test_config, tls_server_config},
        },
        tls::verify::NoopServerCertVerifier,
    };

    use super::*;

    const GREETING: &[u8] = b"220 test ESMTP IC SMTP Gateway\r\n";

    /// Forwards session-finish notifications into a channel so the test can await them
    #[derive(Debug)]
    struct Notifications(mpsc::UnboundedSender<(SessionMeta, Option<SessionError>)>);

    #[async_trait]
    impl ReceivesSmtpNotifications for Notifications {
        async fn notify_message(
            &self,
            _: SessionMeta,
            _: Arc<EmailMessage>,
            _: Duration,
            _: Option<MessageError>,
        ) {
        }

        async fn notify_protocol_error(&self, _: SessionMeta, _: ProtocolError) {}

        async fn notify_session_finish(&self, meta: SessionMeta, error: Option<SessionError>) {
            let _ = self.0.send((meta, error));
        }
    }

    /// An IPv4-mapped IPv6 peer must be accounted for as plain IPv4 everywhere
    #[tokio::test]
    async fn test_handle_connection_canonicalizes_v6_mapped_address() {
        let (tx, mut rx) = mpsc::unbounded_channel();
        let mut cfg = test_config();
        cfg.notifications_handler = Some(Arc::new(Notifications(tx)));

        let metrics = Metrics::new(&Registry::new());
        let (server, mut client) = duplex(4096);
        let token = CancellationToken::new();

        let task = tokio::spawn({
            let (metrics, token) = (metrics.clone(), token.clone());
            async move {
                SessionManager::handle_connection(
                    server,
                    SocketAddr::from_str("[::ffff:1.2.3.4]:25").unwrap(),
                    Arc::new(cfg),
                    metrics,
                    token,
                )
                .await;
            }
        });

        let mut buf = vec![0; 256];
        let n = client.read(&mut buf).await.unwrap();
        assert_eq!(&buf[..n], GREETING);
        assert_eq!(metrics.sessions_open.with_label_values(&["v4"]).get(), 1);
        assert_eq!(metrics.sessions_open.with_label_values(&["v6"]).get(), 0);

        // Shut the session down cleanly via the token
        token.cancel();
        task.await.unwrap();

        let (meta, error) = rx.recv().await.unwrap();
        assert_eq!(meta.remote_ip, IpAddr::V4(Ipv4Addr::new(1, 2, 3, 4)));
        assert!(error.is_none(), "{error:?}");

        // The gauge must come back to zero & the session be counted without an error label
        assert_eq!(metrics.sessions_open.with_label_values(&["v4"]).get(), 0);
        assert_eq!(
            metrics
                .sessions_processed
                .with_label_values(&["v4", "", ""])
                .get(),
            1
        );
        assert_eq!(
            metrics
                .session_duration
                .with_label_values(&["v4", ""])
                .get_sample_count(),
            1
        );
    }

    /// A client QUIT is an error from the session's point of view & gets its own label
    #[tokio::test]
    async fn test_handle_connection_reports_quit() {
        let (tx, mut rx) = mpsc::unbounded_channel();
        let mut cfg = test_config();
        cfg.notifications_handler = Some(Arc::new(Notifications(tx)));

        let metrics = Metrics::new(&Registry::new());
        let (server, mut client) = duplex(4096);

        let task = tokio::spawn({
            let metrics = metrics.clone();
            async move {
                SessionManager::handle_connection(
                    server,
                    SocketAddr::from_str("1.2.3.4:25").unwrap(),
                    Arc::new(cfg),
                    metrics,
                    CancellationToken::new(),
                )
                .await;
            }
        });

        let mut buf = vec![0; 256];
        let n = client.read(&mut buf).await.unwrap();
        assert_eq!(&buf[..n], GREETING);

        client.write_all(b"QUIT\r\n").await.unwrap();
        let n = client.read(&mut buf).await.unwrap();
        assert_eq!(&buf[..n], b"221 2.0.0 Bye.\r\n");
        task.await.unwrap();

        let (meta, error) = rx.recv().await.unwrap();
        assert_eq!(meta.remote_ip, IpAddr::V4(Ipv4Addr::new(1, 2, 3, 4)));
        assert!(matches!(error, Some(SessionError::Quit)));

        assert_eq!(metrics.sessions_open.with_label_values(&["v4"]).get(), 0);
        assert_eq!(
            metrics
                .sessions_processed
                .with_label_values(&["v4", "", "quit"])
                .get(),
            1
        );
    }

    /// RFC 3207 4.2: everything learned before STARTTLS has to be thrown away,
    /// while the connection-level bookkeeping survives
    #[tokio::test]
    async fn test_into_tls_discards_session_data() {
        let mut cfg = test_config();
        cfg.tls_mode = SessionTlsMode::Allowed(tls_server_config());

        let (server, client) = duplex(64 * 1024);
        let remote_ip = IpAddr::from_str("1.2.3.4").unwrap();
        let mut session = Session::new(
            remote_ip,
            server,
            Arc::new(cfg),
            Metrics::new(&Registry::new()),
        );

        // Pretend we're mid-transaction
        session.data.ehlo_hostname = Some(FQDN::from_str("foo.bar").unwrap());
        session.data.mail_from = Some("a@example.com".try_into().unwrap());
        session
            .data
            .rcpt_to
            .push("b@example.com".try_into().unwrap());
        session.data.message.extend_from_slice(b"partial");
        session.counters.bytes_tx = 42;
        session.counters.commands = 7;
        let id = session.id;

        let client_task = tokio::spawn(async move {
            let tls_cfg = ClientConfig::builder()
                .dangerous()
                .with_custom_certificate_verifier(Arc::new(NoopServerCertVerifier::default()))
                .with_no_client_auth();

            TlsConnector::from(Arc::new(tls_cfg))
                .connect(ServerName::try_from("foo").unwrap(), client)
                .await
                .unwrap()
        });

        let session = session.into_tls().await.unwrap();
        let _client = client_task.await.unwrap();

        assert!(session.data.ehlo_hostname.is_none());
        assert!(session.data.mail_from.is_none());
        assert!(session.data.rcpt_to.is_empty());
        assert!(session.data.message.is_empty());

        assert_eq!(session.id, id);
        assert_eq!(session.remote_ip, remote_ip);
        assert_eq!(session.counters.bytes_tx, 42);
        assert_eq!(session.counters.commands, 7);

        let tls_info = session.tls_info.as_ref().expect("TLS info must be set");
        assert_eq!(tls_info.protocol, ProtocolVersion::TLSv1_3);
        // The metric labels get the negotiated protocol appended
        assert_eq!(session.labels, ["v4", "TLSv1_3"]);
    }

    #[tokio::test]
    async fn test_into_tls_handshake_failure() {
        let (tx, mut rx) = mpsc::unbounded_channel();
        let mut cfg = test_config();
        cfg.tls_mode = SessionTlsMode::Required(tls_server_config());
        cfg.notifications_handler = Some(Arc::new(Notifications(tx)));

        let (server, mut client) = duplex(4096);
        let metrics = Metrics::new(&Registry::new());
        let remote_ip = IpAddr::from_str("1.2.3.4").unwrap();
        let session = Session::new(remote_ip, server, Arc::new(cfg), metrics.clone());

        // `handle_connection` is the one that increments this, so do it by hand
        // to show that the failure path still balances it out
        metrics.sessions_open.with_label_values(&["v4"]).inc();

        let client_task = tokio::spawn(async move {
            client
                .write_all(b"definitely not a TLS ClientHello\r\n")
                .await
                .ok();
            client
        });

        let Err(error) = session.into_tls().await else {
            panic!("the TLS handshake must fail");
        };
        assert!(
            matches!(error, SessionError::TlsHandshakeFailed(_)),
            "{error:?}"
        );
        let _client = client_task.await.unwrap();

        let (meta, error) = rx.recv().await.unwrap();
        assert_eq!(meta.remote_ip, remote_ip);
        assert!(meta.tls_info.is_none());
        assert!(matches!(error, Some(SessionError::TlsHandshakeFailed(_))));

        assert_eq!(metrics.sessions_open.with_label_values(&["v4"]).get(), 0);
        assert_eq!(
            metrics
                .sessions_processed
                .with_label_values(&["v4", "", "tls_handshake_failed"])
                .get(),
            1
        );
        assert_eq!(
            metrics
                .session_duration
                .with_label_values(&["v4", ""])
                .get_sample_count(),
            1
        );
    }

    /// Reaching `into_tls()` without TLS configured is a programming error
    #[tokio::test]
    #[should_panic(expected = "Session::into_tls() called with TLS disabled")]
    async fn test_into_tls_panics_when_tls_is_disabled() {
        let (server, _client) = duplex(64);
        let session = Session::new(
            IpAddr::from_str("1.2.3.4").unwrap(),
            server,
            // `SessionTlsMode::Disabled` by default
            Arc::new(test_config()),
            Metrics::new(&Registry::new()),
        );

        let _ = session.into_tls().await;
    }

    /// A plain IPv6 peer keeps its address & gets the `v6` metric label
    #[tokio::test]
    async fn test_handle_connection_labels_ipv6_peers() {
        let (tx, mut rx) = mpsc::unbounded_channel();
        let mut cfg = test_config();
        cfg.notifications_handler = Some(Arc::new(Notifications(tx)));

        let metrics = Metrics::new(&Registry::new());
        let (server, mut client) = duplex(4096);

        let task = tokio::spawn({
            let metrics = metrics.clone();
            async move {
                SessionManager::handle_connection(
                    server,
                    SocketAddr::from_str("[2001:db8::1]:25").unwrap(),
                    Arc::new(cfg),
                    metrics,
                    CancellationToken::new(),
                )
                .await;
            }
        });

        let mut buf = vec![0; 256];
        let n = client.read(&mut buf).await.unwrap();
        assert_eq!(&buf[..n], GREETING);
        assert_eq!(metrics.sessions_open.with_label_values(&["v6"]).get(), 1);
        assert_eq!(metrics.sessions_open.with_label_values(&["v4"]).get(), 0);

        client.write_all(b"QUIT\r\n").await.unwrap();
        let n = client.read(&mut buf).await.unwrap();
        assert_eq!(&buf[..n], b"221 2.0.0 Bye.\r\n");
        task.await.unwrap();

        let (meta, error) = rx.recv().await.unwrap();
        assert_eq!(meta.remote_ip, IpAddr::from_str("2001:db8::1").unwrap());
        assert!(matches!(error, Some(SessionError::Quit)));

        assert_eq!(metrics.sessions_open.with_label_values(&["v6"]).get(), 0);
        assert_eq!(
            metrics
                .sessions_processed
                .with_label_values(&["v6", "", "quit"])
                .get(),
            1
        );
    }

    /// An error other than QUIT goes down the same path: reply, close, notify
    #[tokio::test]
    async fn test_handle_connection_reports_an_idle_timeout() {
        let (tx, mut rx) = mpsc::unbounded_channel();
        let mut cfg = test_config();
        cfg.timeout = Duration::from_millis(200);
        cfg.notifications_handler = Some(Arc::new(Notifications(tx)));

        let metrics = Metrics::new(&Registry::new());
        let (server, mut client) = duplex(4096);

        let task = tokio::spawn({
            let metrics = metrics.clone();
            async move {
                SessionManager::handle_connection(
                    server,
                    SocketAddr::from_str("1.2.3.4:25").unwrap(),
                    Arc::new(cfg),
                    metrics,
                    CancellationToken::new(),
                )
                .await;
            }
        });

        // Say nothing at all & wait to be kicked out
        let mut buf = vec![0; 256];
        let n = client.read(&mut buf).await.unwrap();
        assert_eq!(&buf[..n], GREETING);
        let n = client.read(&mut buf).await.unwrap();
        assert_eq!(&buf[..n], b"221 2.0.0 Disconnecting due to inactivity.\r\n");
        task.await.unwrap();

        let (_, error) = rx.recv().await.unwrap();
        assert!(matches!(error, Some(SessionError::Timeout)), "{error:?}");

        assert_eq!(metrics.sessions_open.with_label_values(&["v4"]).get(), 0);
        assert_eq!(
            metrics
                .sessions_processed
                .with_label_values(&["v4", "", "timeout"])
                .get(),
            1
        );
    }

    /// Everything still has to balance out without a notifications handler
    #[tokio::test]
    async fn test_handle_connection_without_a_notifications_handler() {
        let cfg = test_config();
        assert!(cfg.notifications_handler.is_none());

        let metrics = Metrics::new(&Registry::new());
        let (server, mut client) = duplex(4096);

        let task = tokio::spawn({
            let metrics = metrics.clone();
            async move {
                SessionManager::handle_connection(
                    server,
                    SocketAddr::from_str("1.2.3.4:25").unwrap(),
                    Arc::new(cfg),
                    metrics,
                    CancellationToken::new(),
                )
                .await;
            }
        });

        let mut buf = vec![0; 256];
        let n = client.read(&mut buf).await.unwrap();
        assert_eq!(&buf[..n], GREETING);
        client.write_all(b"QUIT\r\n").await.unwrap();
        let n = client.read(&mut buf).await.unwrap();
        assert_eq!(&buf[..n], b"221 2.0.0 Bye.\r\n");
        task.await.unwrap();

        assert_eq!(metrics.sessions_open.with_label_values(&["v4"]).get(), 0);
        assert_eq!(
            metrics
                .sessions_processed
                .with_label_values(&["v4", "", "quit"])
                .get(),
            1
        );
    }

    /// The whole STARTTLS round trip through the manager: the session that finishes
    /// is the TLS one, so the negotiated protocol ends up in the labels & the meta
    #[tokio::test]
    async fn test_handle_connection_starttls_reports_the_negotiated_protocol() {
        let (tx, mut rx) = mpsc::unbounded_channel();
        let mut cfg = test_config();
        cfg.tls_mode = SessionTlsMode::Allowed(tls_server_config());
        cfg.notifications_handler = Some(Arc::new(Notifications(tx)));

        let metrics = Metrics::new(&Registry::new());
        let (server, mut client) = duplex(64 * 1024);

        let task = tokio::spawn({
            let metrics = metrics.clone();
            async move {
                SessionManager::handle_connection(
                    server,
                    SocketAddr::from_str("1.2.3.4:25").unwrap(),
                    Arc::new(cfg),
                    metrics,
                    CancellationToken::new(),
                )
                .await;
            }
        });

        let mut buf = vec![0; 4096];
        let n = client.read(&mut buf).await.unwrap();
        assert_eq!(&buf[..n], GREETING);

        client.write_all(b"STARTTLS\r\n").await.unwrap();
        let n = client.read(&mut buf).await.unwrap();
        assert_eq!(&buf[..n], b"220 2.0.0 Ready to start TLS.\r\n");

        let tls_cfg = ClientConfig::builder()
            .dangerous()
            .with_custom_certificate_verifier(Arc::new(NoopServerCertVerifier::default()))
            .with_no_client_auth();
        let mut client = TlsConnector::from(Arc::new(tls_cfg))
            .connect(ServerName::try_from("foo").unwrap(), client)
            .await
            .unwrap();

        client.write_all(b"QUIT\r\n").await.unwrap();
        let n = client.read(&mut buf).await.unwrap();
        assert_eq!(&buf[..n], b"221 2.0.0 Bye.\r\n");
        task.await.unwrap();

        let (meta, error) = rx.recv().await.unwrap();
        assert!(matches!(error, Some(SessionError::Quit)));
        assert_eq!(
            meta.tls_info.as_ref().unwrap().protocol,
            ProtocolVersion::TLSv1_3
        );

        // The gauge was incremented once & decremented once, both with the plain
        // family label, while the processed counter carries the TLS protocol
        assert_eq!(metrics.sessions_open.with_label_values(&["v4"]).get(), 0);
        assert_eq!(
            metrics
                .sessions_processed
                .with_label_values(&["v4", "TLSv1_3", "quit"])
                .get(),
            1
        );
        assert_eq!(
            metrics
                .sessions_processed
                .with_label_values(&["v4", "", "quit"])
                .get(),
            0
        );
        assert_eq!(
            metrics
                .session_duration
                .with_label_values(&["v4", "TLSv1_3"])
                .get_sample_count(),
            1
        );
    }

    /// If the client bails out after we agreed to STARTTLS the accounting still has
    /// to balance, and the failure is reported once
    #[tokio::test]
    async fn test_handle_connection_starttls_handshake_failure() {
        let (tx, mut rx) = mpsc::unbounded_channel();
        let mut cfg = test_config();
        cfg.tls_mode = SessionTlsMode::Allowed(tls_server_config());
        cfg.notifications_handler = Some(Arc::new(Notifications(tx)));

        let metrics = Metrics::new(&Registry::new());
        let (server, mut client) = duplex(4096);

        let task = tokio::spawn({
            let metrics = metrics.clone();
            async move {
                SessionManager::handle_connection(
                    server,
                    SocketAddr::from_str("1.2.3.4:25").unwrap(),
                    Arc::new(cfg),
                    metrics,
                    CancellationToken::new(),
                )
                .await;
            }
        });

        let mut buf = vec![0; 4096];
        let n = client.read(&mut buf).await.unwrap();
        assert_eq!(&buf[..n], GREETING);

        client.write_all(b"STARTTLS\r\n").await.unwrap();
        let n = client.read(&mut buf).await.unwrap();
        assert_eq!(&buf[..n], b"220 2.0.0 Ready to start TLS.\r\n");

        // Not a ClientHello
        client
            .write_all(b"definitely not a TLS ClientHello\r\n")
            .await
            .unwrap();
        task.await.unwrap();

        let (meta, error) = rx.recv().await.unwrap();
        assert!(
            matches!(error, Some(SessionError::TlsHandshakeFailed(_))),
            "{error:?}"
        );
        assert!(meta.tls_info.is_none());
        // Only the one notification - the handshake failure isn't reported twice
        assert!(rx.try_recv().is_err());

        assert_eq!(metrics.sessions_open.with_label_values(&["v4"]).get(), 0);
        assert_eq!(
            metrics
                .sessions_processed
                .with_label_values(&["v4", "", "tls_handshake_failed"])
                .get(),
            1
        );
    }

    /// The protocol state machine is carried over into the TLS session - only the
    /// transaction data is dropped
    #[tokio::test]
    async fn test_into_tls_preserves_the_protocol_state() {
        let mut cfg = test_config();
        cfg.tls_mode = SessionTlsMode::Allowed(tls_server_config());
        cfg.max_recipients = 17;

        let (server, client) = duplex(64 * 1024);
        let session = Session::new(
            IpAddr::from_str("1.2.3.4").unwrap(),
            server,
            Arc::new(cfg),
            Metrics::new(&Registry::new()),
        );
        // A fresh session still owes the client a greeting
        assert_eq!(session.state.to_string(), "Greeting");

        let client_task = tokio::spawn(async move {
            let tls_cfg = ClientConfig::builder()
                .dangerous()
                .with_custom_certificate_verifier(Arc::new(NoopServerCertVerifier::default()))
                .with_no_client_auth();

            TlsConnector::from(Arc::new(tls_cfg))
                .connect(ServerName::try_from("foo").unwrap(), client)
                .await
                .unwrap()
        });

        let session = session.into_tls().await.unwrap();
        let _client = client_task.await.unwrap();

        assert_eq!(session.state.to_string(), "Greeting");
        // The config is carried over rather than rebuilt
        assert_eq!(session.cfg.max_recipients, 17);
    }
}