rustnetconf 0.10.0

An async-first NETCONF 1.0/1.1 client library for Rust
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
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
//! SSH transport implementation using `russh`.
//!
//! Connects to a NETCONF device over SSH, requests the `netconf` subsystem,
//! and provides byte-stream read/write access to the SSH channel.

use async_trait::async_trait;
use russh::client::AuthResult;
use russh::keys::{self, PrivateKeyWithHashAlg};
use russh::*;
use std::borrow::Cow;
use std::path::Path;
use std::pin::Pin;
use std::process::Stdio;
use std::sync::Arc;
use std::task::{Context, Poll};
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
use tokio::process::{Child, ChildStdin, ChildStdout};

use zeroize::Zeroizing;

use crate::error::TransportError;
use crate::transport::Transport;

/// SSH transport for NETCONF sessions.
pub struct SshTransport {
    channel: ChannelStream,
    handle: client::Handle<SshHandler>,
    /// SSH handles for each jump-host hop, kept alive for the lifetime of
    /// the target session. Dropping a jump handle would tear down the
    /// `direct-tcpip` channel that carries the next hop's transport, so
    /// these must outlive `handle`.
    _jump_handles: Vec<client::Handle<SshHandler>>,
    /// `ProxyCommand` subprocess (if any), kept alive for the lifetime of
    /// the target session. Dropping the `Child` would close its stdin/stdout
    /// pipes, tearing down the SSH transport stream that runs over them.
    _proxy_process: Option<Child>,
}

/// SSH authentication method.
///
/// Sensitive fields (passwords, passphrases) use [`Zeroizing<String>`] so
/// the memory is overwritten on drop, reducing the window for credential
/// leakage via memory dumps.
#[derive(Clone)]
pub enum SshAuth {
    /// Password authentication.
    Password(Zeroizing<String>),
    /// Key file authentication (path to private key, optional passphrase).
    KeyFile {
        path: String,
        passphrase: Option<Zeroizing<String>>,
    },
    /// SSH agent authentication.
    Agent,
}

/// SSH host key verification policy.
///
/// Controls how the client validates the device's SSH host key during
/// connection. Use this to protect against man-in-the-middle attacks.
#[derive(Clone, Debug)]
pub enum HostKeyVerification {
    /// Accept all host keys without verification (**INSECURE**).
    ///
    /// Suitable for lab environments, testing, or initial device provisioning.
    /// A warning is logged when this mode is used.
    AcceptAll,

    /// Accept only a host key matching a specific SHA-256 fingerprint.
    ///
    /// The fingerprint can be provided with or without the `SHA256:` prefix
    /// (e.g., `"SHA256:abc123..."` or just `"abc123..."`).
    ///
    /// Obtain the fingerprint with: `ssh-keygen -lf /etc/ssh/ssh_host_ed25519_key.pub`
    Fingerprint(String),

    /// Reject all host keys (useful for testing error paths).
    RejectAll,
}

/// Configuration for establishing an SSH transport.
#[derive(Clone)]
pub struct SshConfig {
    pub host: String,
    pub port: u16,
    pub username: String,
    pub auth: SshAuth,
    /// Host key verification policy.
    pub host_key_verification: HostKeyVerification,
    /// Optional ordered list of jump-host hops to tunnel through.
    ///
    /// Each hop has its own host, credentials, and host-key-verification
    /// policy (independent of the target). When empty (the default), a
    /// direct TCP connection is made to `host:port`. When set, an SSH
    /// session is established to each hop in turn, and a `direct-tcpip`
    /// channel is used to carry the next leg — equivalent to OpenSSH's
    /// `ProxyJump h1,h2,h3,...,target`.
    pub jump_hosts: Vec<JumpHostConfig>,
    /// Optional shell command to spawn whose stdin/stdout become the
    /// transport stream to the target — equivalent to OpenSSH's
    /// `ProxyCommand`. The substrings `%h` and `%p` are replaced with the
    /// target host and port respectively.
    ///
    /// Mutually exclusive with `jump_hosts`. If both are set,
    /// [`SshTransport::connect`] returns
    /// [`TransportError::Connect`]. Use one or the other to reach a host.
    ///
    /// **Security:** the command is interpreted by `sh -c` to support
    /// pipelines and shell features. The `%h` and `%p` values are
    /// shell-escaped (single-quoted) before substitution to prevent
    /// injection. The command template itself is not escaped — the
    /// caller is responsible for its safety.
    pub proxy_command: Option<String>,
}

/// Configuration for one jump-host hop in a `ProxyJump` chain.
///
/// Each hop carries its own credentials and host-key policy because in
/// the real world the bastion frequently has different access rules than
/// the device behind it (different user, key, fingerprint).
#[derive(Clone)]
pub struct JumpHostConfig {
    pub host: String,
    pub port: u16,
    pub username: String,
    pub auth: SshAuth,
    pub host_key_verification: HostKeyVerification,
}

/// Internal SSH client handler for russh callbacks.
struct SshHandler {
    host_key_verification: HostKeyVerification,
}

impl client::Handler for SshHandler {
    type Error = russh::Error;

    fn check_server_key(
        &mut self,
        server_public_key: &keys::PublicKey,
    ) -> impl std::future::Future<Output = Result<bool, Self::Error>> + Send {
        let result = match &self.host_key_verification {
            HostKeyVerification::AcceptAll => {
                tracing::warn!(
                    "accepting SSH host key without verification — \
                     set host_key_verification() for production use"
                );
                Ok(true)
            }
            HostKeyVerification::Fingerprint(expected) => {
                let fingerprint = server_public_key.fingerprint(keys::HashAlg::Sha256);
                let actual = fingerprint.to_string();
                // Allow comparison with or without "SHA256:" prefix
                let matches = actual == *expected
                    || actual
                        .strip_prefix("SHA256:")
                        .is_some_and(|stripped| stripped == expected);
                if matches {
                    tracing::debug!("SSH host key fingerprint verified");
                    Ok(true)
                } else {
                    tracing::error!(
                        expected = %expected,
                        actual = %actual,
                        "SSH host key fingerprint mismatch — possible MITM attack"
                    );
                    Ok(false)
                }
            }
            HostKeyVerification::RejectAll => {
                tracing::error!("SSH host key rejected (RejectAll policy)");
                Ok(false)
            }
        };
        std::future::ready(result)
    }
}

/// Channel stream wrapper that provides read/write on the SSH channel.
struct ChannelStream {
    channel: Channel<client::Msg>,
    /// Buffered data from the channel that hasn't been consumed yet.
    read_buffer: Vec<u8>,
}

/// Build the russh client config used for every hop and the target.
///
/// Network devices (Juniper, Cisco) often only support ECDH NIST or DH
/// group key exchange — not Curve25519 which is russh's default
/// preference, so we extend the preferred list.
fn build_russh_config() -> client::Config {
    let preferred = Preferred {
        kex: Cow::Borrowed(&[
            kex::CURVE25519,
            kex::CURVE25519_PRE_RFC_8731,
            kex::ECDH_SHA2_NISTP256,
            kex::ECDH_SHA2_NISTP384,
            kex::ECDH_SHA2_NISTP521,
            kex::DH_G16_SHA512,
            kex::DH_G14_SHA256,
            kex::EXTENSION_SUPPORT_AS_CLIENT,
            kex::EXTENSION_OPENSSH_STRICT_KEX_AS_CLIENT,
        ]),
        ..Preferred::default()
    };
    client::Config {
        preferred,
        ..Default::default()
    }
}

/// Shell-escape a string by wrapping it in single quotes and escaping
/// any embedded single quotes (`'` → `'\''`).
///
/// This is the POSIX-standard way to produce a literal string safe for
/// `sh -c` consumption: `'foo'\''bar'` passes `foo'bar` to the program.
fn shell_escape(s: &str) -> String {
    let mut out = String::with_capacity(s.len() + 2);
    out.push('\'');
    for ch in s.chars() {
        if ch == '\'' {
            out.push_str("'\\''");
        } else {
            out.push(ch);
        }
    }
    out.push('\'');
    out
}

/// Substitute OpenSSH `ProxyCommand` tokens in `command`.
///
/// Replaces `%h` with the target host and `%p` with the target port,
/// matching `man 5 ssh_config`. Both values are shell-escaped before
/// substitution to prevent shell injection when the result is passed to
/// `sh -c`.
fn expand_proxy_command(command: &str, host: &str, port: u16) -> String {
    command
        .replace("%h", &shell_escape(host))
        .replace("%p", &shell_escape(&port.to_string()))
}

/// Combined `AsyncRead`/`AsyncWrite` over a child process's stdio,
/// used to give russh a duplex stream for `ProxyCommand` mode.
///
/// The `Child` is held alive by the same struct because dropping it
/// would close the stdio pipes and tear down the SSH transport.
struct ProxyCommandStream {
    stdin: ChildStdin,
    stdout: ChildStdout,
}

impl AsyncRead for ProxyCommandStream {
    fn poll_read(
        mut self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        buf: &mut ReadBuf<'_>,
    ) -> Poll<std::io::Result<()>> {
        Pin::new(&mut self.stdout).poll_read(cx, buf)
    }
}

impl AsyncWrite for ProxyCommandStream {
    fn poll_write(
        mut self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        data: &[u8],
    ) -> Poll<std::io::Result<usize>> {
        Pin::new(&mut self.stdin).poll_write(cx, data)
    }

    fn poll_flush(
        mut self: Pin<&mut Self>,
        cx: &mut Context<'_>,
    ) -> Poll<std::io::Result<()>> {
        Pin::new(&mut self.stdin).poll_flush(cx)
    }

    fn poll_shutdown(
        mut self: Pin<&mut Self>,
        cx: &mut Context<'_>,
    ) -> Poll<std::io::Result<()>> {
        Pin::new(&mut self.stdin).poll_shutdown(cx)
    }
}

/// Spawn the `ProxyCommand` shell and return a duplex stream over its
/// stdio plus the `Child` handle (which the caller must keep alive).
fn spawn_proxy_command(
    command: &str,
    host: &str,
    port: u16,
) -> Result<(ProxyCommandStream, Child), TransportError> {
    let expanded = expand_proxy_command(command, host, port);
    let mut child = tokio::process::Command::new("sh")
        .arg("-c")
        .arg(&expanded)
        .stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::inherit())
        .kill_on_drop(true)
        .spawn()
        .map_err(|e| {
            TransportError::Connect(format!(
                "failed to spawn ProxyCommand `{expanded}`: {e}"
            ))
        })?;

    let stdin = child.stdin.take().ok_or_else(|| {
        TransportError::Connect("ProxyCommand stdin not captured".to_string())
    })?;
    let stdout = child.stdout.take().ok_or_else(|| {
        TransportError::Connect("ProxyCommand stdout not captured".to_string())
    })?;

    Ok((ProxyCommandStream { stdin, stdout }, child))
}

/// Authenticate an open SSH session against the given username/auth method.
///
/// Used uniformly for jump hosts and the final target.
async fn authenticate(
    handle: &mut client::Handle<SshHandler>,
    username: &str,
    auth: &SshAuth,
) -> Result<(), TransportError> {
    let auth_result = match auth {
        SshAuth::Password(password) => handle
            .authenticate_password(username, password.as_str())
            .await
            .map_err(|e| TransportError::Auth(format!("password auth failed: {e}")))?,
        SshAuth::KeyFile { path, passphrase } => {
            let key_path = Path::new(path);
            let key_contents = tokio::fs::read_to_string(key_path).await.map_err(|e| {
                tracing::debug!(path, %e, "failed to read key file");
                TransportError::Auth("failed to read SSH key file".to_string())
            })?;
            let passphrase_str = passphrase.as_ref().map(|p| p.as_str());
            let key_pair = keys::decode_secret_key(&key_contents, passphrase_str)
                .map_err(|e| {
                    tracing::debug!(%e, "failed to decode key");
                    TransportError::Auth("failed to decode SSH key".to_string())
                })?;
            let hash_alg = handle
                .best_supported_rsa_hash()
                .await
                .unwrap_or(None)
                .flatten();
            let key_with_hash = PrivateKeyWithHashAlg::new(Arc::new(key_pair), hash_alg);
            handle
                .authenticate_publickey(username, key_with_hash)
                .await
                .map_err(|e| TransportError::Auth(format!("key auth failed: {e}")))?
        }
        SshAuth::Agent => {
            let mut agent = keys::agent::client::AgentClient::connect_env()
                .await
                .map_err(|e| TransportError::Auth(format!("SSH agent connect failed: {e}")))?;
            let identities = agent.request_identities().await.map_err(|e| {
                TransportError::Auth(format!("SSH agent identities failed: {e}"))
            })?;

            let mut auth_success = false;
            for identity in identities {
                match handle
                    .authenticate_publickey_with(
                        username,
                        identity.public_key().into_owned(),
                        None,
                        &mut agent,
                    )
                    .await
                {
                    Ok(AuthResult::Success) => {
                        auth_success = true;
                        break;
                    }
                    _ => continue,
                }
            }
            if auth_success {
                AuthResult::Success
            } else {
                AuthResult::Failure {
                    remaining_methods: russh::MethodSet::empty(),
                    partial_success: false,
                }
            }
        }
    };

    if !matches!(auth_result, AuthResult::Success) {
        return Err(TransportError::Auth(format!(
            "authentication failed for user '{username}'"
        )));
    }
    Ok(())
}

impl SshTransport {
    /// Connect to a NETCONF device over SSH and open the `netconf` subsystem.
    ///
    /// If `config.jump_hosts` is non-empty, an SSH session is established to
    /// each hop in turn and a `direct-tcpip` channel is used to carry the
    /// next leg, equivalent to OpenSSH's `ProxyJump h1,h2,...,target`.
    ///
    /// If `config.proxy_command` is set, the given shell command is spawned
    /// and its stdin/stdout become the transport stream to the target,
    /// equivalent to OpenSSH's `ProxyCommand`. `proxy_command` and
    /// `jump_hosts` are mutually exclusive — setting both returns
    /// [`TransportError::Connect`].
    pub async fn connect(config: SshConfig) -> Result<Self, TransportError> {
        if config.proxy_command.is_some() && !config.jump_hosts.is_empty() {
            return Err(TransportError::Connect(
                "proxy_command and jump_hosts are mutually exclusive — \
                 use one or the other to reach the target"
                    .to_string(),
            ));
        }

        let russh_config = Arc::new(build_russh_config());

        // Establish each jump-host hop in sequence. After the loop, `prev`
        // holds the Handle of the last hop (or None for direct connection).
        // We must keep all jump handles alive for the lifetime of the
        // target session — dropping them would tear down the tunneled
        // channels that carry the target session's transport.
        let mut jump_handles: Vec<client::Handle<SshHandler>> = Vec::new();
        let mut prev: Option<&client::Handle<SshHandler>> = None;

        for (idx, hop) in config.jump_hosts.iter().enumerate() {
            let label = format!("jump-host {} ({}:{})", idx, hop.host, hop.port);
            let handle = match prev {
                None => {
                    // First hop: direct TCP connection.
                    let handler = SshHandler {
                        host_key_verification: hop.host_key_verification.clone(),
                    };
                    client::connect(russh_config.clone(), (&*hop.host, hop.port), handler)
                        .await
                        .map_err(|e| {
                            TransportError::Connect(format!("SSH connect to {label} failed: {e}"))
                        })?
                }
                Some(parent) => {
                    // Subsequent hop: tunneled via the previous hop's session.
                    let channel = parent
                        .channel_open_direct_tcpip(&*hop.host, hop.port as u32, "0.0.0.0", 0)
                        .await
                        .map_err(|e| {
                            TransportError::Connect(format!(
                                "direct-tcpip to {label} failed: {e}"
                            ))
                        })?;
                    let stream = channel.into_stream();
                    let handler = SshHandler {
                        host_key_verification: hop.host_key_verification.clone(),
                    };
                    client::connect_stream(russh_config.clone(), stream, handler)
                        .await
                        .map_err(|e| {
                            TransportError::Connect(format!(
                                "SSH handshake with {label} failed: {e}"
                            ))
                        })?
                }
            };

            jump_handles.push(handle);
            prev = Some(jump_handles.last().expect("just pushed"));
        }

        // Authenticate to each jump host in order. Done after the loop above
        // so the borrow of `prev` is released.
        for (idx, hop) in config.jump_hosts.iter().enumerate() {
            let handle = &mut jump_handles[idx];
            authenticate(handle, &hop.username, &hop.auth)
                .await
                .map_err(|e| match e {
                    TransportError::Auth(msg) => TransportError::Auth(format!(
                        "jump-host {} ({}:{}): {msg}",
                        idx, hop.host, hop.port
                    )),
                    other => other,
                })?;
        }

        // Open the final hop to the target. Three modes (in priority order):
        //   1. proxy_command set → spawn a subprocess and run SSH over its stdio
        //   2. jump_hosts non-empty → direct-tcpip tunnel through the last hop
        //   3. otherwise → direct TCP connection to the target
        let target_label = format!("{}:{}", config.host, config.port);
        let mut proxy_process: Option<Child> = None;
        let mut handle = if let Some(cmd) = config.proxy_command.as_deref() {
            let (stream, child) = spawn_proxy_command(cmd, &config.host, config.port)?;
            proxy_process = Some(child);
            let handler = SshHandler {
                host_key_verification: config.host_key_verification.clone(),
            };
            client::connect_stream(russh_config.clone(), stream, handler)
                .await
                .map_err(|e| {
                    TransportError::Connect(format!(
                        "SSH handshake with target {target_label} (via ProxyCommand) failed: {e}"
                    ))
                })?
        } else if let Some(parent) = jump_handles.last() {
            let channel = parent
                .channel_open_direct_tcpip(&*config.host, config.port as u32, "0.0.0.0", 0)
                .await
                .map_err(|e| {
                    TransportError::Connect(format!(
                        "direct-tcpip to target {target_label} failed: {e}"
                    ))
                })?;
            let stream = channel.into_stream();
            let handler = SshHandler {
                host_key_verification: config.host_key_verification.clone(),
            };
            client::connect_stream(russh_config.clone(), stream, handler)
                .await
                .map_err(|e| {
                    TransportError::Connect(format!(
                        "SSH handshake with target {target_label} failed: {e}"
                    ))
                })?
        } else {
            let handler = SshHandler {
                host_key_verification: config.host_key_verification.clone(),
            };
            client::connect(russh_config.clone(), (&*config.host, config.port), handler)
                .await
                .map_err(|e| {
                    TransportError::Connect(format!("SSH connect to {target_label} failed: {e}"))
                })?
        };

        // Authenticate to the target.
        authenticate(&mut handle, &config.username, &config.auth).await?;

        // Open a session channel and request the netconf subsystem
        let mut channel = handle
            .channel_open_session()
            .await
            .map_err(|e| TransportError::Channel(format!("failed to open SSH channel: {e}")))?;

        channel
            .request_subsystem(true, "netconf")
            .await
            .map_err(|e| TransportError::Channel(format!("failed to request netconf subsystem: {e}")))?;

        // Wait for the subsystem confirmation from the server.
        // When `want_reply` is true, the server sends Success or Failure.
        // We must consume channel messages (e.g., WindowAdjusted) until
        // we see the confirmation, otherwise these messages interfere
        // with the first data read (the hello exchange).
        loop {
            match channel.wait().await {
                Some(ChannelMsg::Success) => break,
                Some(ChannelMsg::Failure) => {
                    return Err(TransportError::Channel(
                        "server rejected netconf subsystem request".to_string(),
                    ));
                }
                Some(ChannelMsg::WindowAdjusted { .. }) => {
                    // Expected — the server adjusts the window; keep waiting.
                    continue;
                }
                Some(ChannelMsg::Eof) | Some(ChannelMsg::Close) | None => {
                    return Err(TransportError::ChannelClosed(
                        "channel closed before subsystem confirmation".to_string(),
                    ));
                }
                Some(_other) => {
                    // Other messages (e.g., ExtendedData) — skip.
                    continue;
                }
            }
        }

        let channel_stream = ChannelStream {
            channel,
            read_buffer: Vec::new(),
        };

        Ok(Self {
            channel: channel_stream,
            handle,
            _jump_handles: jump_handles,
            _proxy_process: proxy_process,
        })
    }
}

#[async_trait]
impl Transport for SshTransport {
    async fn write_all(&mut self, data: &[u8]) -> Result<(), TransportError> {
        self.channel
            .channel
            .data(data)
            .await
            .map_err(|e| TransportError::Io(std::io::Error::other(e.to_string())))?;
        Ok(())
    }

    async fn read(&mut self, buf: &mut [u8]) -> Result<usize, TransportError> {
        // First, drain any buffered data
        if !self.channel.read_buffer.is_empty() {
            let to_read = std::cmp::min(buf.len(), self.channel.read_buffer.len());
            buf[..to_read].copy_from_slice(&self.channel.read_buffer[..to_read]);
            self.channel.read_buffer.drain(..to_read);
            return Ok(to_read);
        }

        // Read from the SSH channel, skipping non-data messages.
        // Messages like WindowAdjusted, Success, ExtendedData (stderr)
        // can arrive at any time and must not be treated as EOF.
        loop {
            match self.channel.channel.wait().await {
                Some(ChannelMsg::Data { data: channel_data }) => {
                    let bytes = &channel_data[..];
                    let to_copy = std::cmp::min(buf.len(), bytes.len());
                    buf[..to_copy].copy_from_slice(&bytes[..to_copy]);
                    if bytes.len() > to_copy {
                        self.channel.read_buffer.extend_from_slice(&bytes[to_copy..]);
                    }
                    return Ok(to_copy);
                }
                Some(ChannelMsg::Eof) | Some(ChannelMsg::Close) | None => {
                    return Ok(0);
                }
                Some(_other) => {
                    // WindowAdjusted, Success, ExtendedData, etc. — skip and keep waiting.
                    continue;
                }
            }
        }
    }

    async fn close(&mut self) -> Result<(), TransportError> {
        self.channel
            .channel
            .eof()
            .await
            .map_err(|e| TransportError::Io(std::io::Error::other(e.to_string())))?;
        self.handle
            .disconnect(Disconnect::ByApplication, "closing session", "en")
            .await
            .map_err(|e| TransportError::Io(std::io::Error::other(e.to_string())))?;
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn ssh_config_default_jump_hosts_is_empty() {
        // Direct connection (no jump hosts) is the default; verify the field
        // exists and constructs cleanly.
        let cfg = SshConfig {
            host: "10.0.0.1".to_string(),
            port: 830,
            username: "u".to_string(),
            auth: SshAuth::Password(Zeroizing::new("p".to_string())),
            host_key_verification: HostKeyVerification::AcceptAll,
            jump_hosts: Vec::new(),
            proxy_command: None,
        };
        assert!(cfg.jump_hosts.is_empty());
        assert!(cfg.proxy_command.is_none());
    }

    #[test]
    fn jump_host_config_constructs_with_independent_creds() {
        // Bastion frequently has different access rules than the device.
        // Verify each hop carries its own credentials and host-key policy.
        let hop = JumpHostConfig {
            host: "bastion.example.com".to_string(),
            port: 22,
            username: "jump-user".to_string(),
            auth: SshAuth::KeyFile {
                path: "/home/me/.ssh/jump_key".to_string(),
                passphrase: None,
            },
            host_key_verification: HostKeyVerification::Fingerprint(
                "SHA256:abc123".to_string(),
            ),
        };
        assert_eq!(hop.host, "bastion.example.com");
        assert_eq!(hop.port, 22);
        assert_eq!(hop.username, "jump-user");
        assert!(matches!(hop.auth, SshAuth::KeyFile { .. }));
        assert!(matches!(
            hop.host_key_verification,
            HostKeyVerification::Fingerprint(_)
        ));
    }

    #[test]
    fn jump_host_config_is_clone() {
        // SshConfig derives Clone (used by ClientBuilder for reconnect),
        // so JumpHostConfig must also be Clone.
        let hop = JumpHostConfig {
            host: "h".to_string(),
            port: 22,
            username: "u".to_string(),
            auth: SshAuth::Agent,
            host_key_verification: HostKeyVerification::AcceptAll,
        };
        let _cloned = hop.clone();
    }

    #[test]
    fn ssh_config_with_multi_hop_chain_clones() {
        // Reconnect support requires SshConfig: Clone, including a chain.
        let hops = vec![
            JumpHostConfig {
                host: "h1".to_string(),
                port: 22,
                username: "u1".to_string(),
                auth: SshAuth::Agent,
                host_key_verification: HostKeyVerification::AcceptAll,
            },
            JumpHostConfig {
                host: "h2".to_string(),
                port: 22,
                username: "u2".to_string(),
                auth: SshAuth::Agent,
                host_key_verification: HostKeyVerification::AcceptAll,
            },
        ];
        let cfg = SshConfig {
            host: "target".to_string(),
            port: 830,
            username: "u".to_string(),
            auth: SshAuth::Agent,
            host_key_verification: HostKeyVerification::AcceptAll,
            jump_hosts: hops,
            proxy_command: None,
        };
        let cloned = cfg.clone();
        assert_eq!(cloned.jump_hosts.len(), 2);
        assert_eq!(cloned.jump_hosts[0].host, "h1");
        assert_eq!(cloned.jump_hosts[1].host, "h2");
    }

    #[test]
    fn shell_escape_plain_string() {
        assert_eq!(shell_escape("hello"), "'hello'");
    }

    #[test]
    fn shell_escape_with_single_quote() {
        assert_eq!(shell_escape("it's"), "'it'\\''s'");
    }

    #[test]
    fn shell_escape_with_metacharacters() {
        assert_eq!(shell_escape("a;rm -rf /"), "'a;rm -rf /'");
    }

    #[test]
    fn expand_proxy_command_replaces_h_and_p() {
        let out = expand_proxy_command("ssh -W %h:%p bastion", "10.1.2.3", 830);
        assert_eq!(out, "ssh -W '10.1.2.3':'830' bastion");
    }

    #[test]
    fn expand_proxy_command_replaces_multiple_occurrences() {
        // %h and %p can appear more than once.
        let out = expand_proxy_command("nc %h %p; echo %h:%p", "host", 22);
        assert_eq!(out, "nc 'host' '22'; echo 'host':'22'");
    }

    #[test]
    fn expand_proxy_command_no_tokens_passthrough() {
        // No %h/%p → passthrough unchanged.
        let out = expand_proxy_command("nc bastion 22", "ignored", 0);
        assert_eq!(out, "nc bastion 22");
    }

    #[test]
    fn expand_proxy_command_escapes_shell_metacharacters() {
        // Shell metacharacters in host are escaped via single-quoting,
        // preventing shell injection when the result is passed to `sh -c`.
        let out = expand_proxy_command("nc %h %p", "host;rm -rf /", 22);
        assert_eq!(out, "nc 'host;rm -rf /' '22'");
    }

    #[tokio::test]
    async fn proxy_command_and_jump_hosts_mutually_exclusive() {
        // Both set → connect rejects without spawning anything or
        // attempting any TCP connection.
        let cfg = SshConfig {
            host: "10.0.0.1".to_string(),
            port: 830,
            username: "u".to_string(),
            auth: SshAuth::Password(Zeroizing::new("p".to_string())),
            host_key_verification: HostKeyVerification::AcceptAll,
            jump_hosts: vec![JumpHostConfig {
                host: "bastion".to_string(),
                port: 22,
                username: "u".to_string(),
                auth: SshAuth::Agent,
                host_key_verification: HostKeyVerification::AcceptAll,
            }],
            proxy_command: Some("nc %h %p".to_string()),
        };
        let err = match SshTransport::connect(cfg).await {
            Err(e) => e,
            Ok(_) => panic!("expected connect to fail"),
        };
        match err {
            TransportError::Connect(msg) => {
                assert!(msg.contains("mutually exclusive"), "unexpected msg: {msg}");
            }
            other => panic!("expected Connect, got {other:?}"),
        }
    }

    #[tokio::test]
    async fn proxy_command_spawn_failure_propagates_as_connect_error() {
        // ssh handshake will fail because `cat` echoes our SSH banner back at
        // us instead of speaking SSH — but the spawn itself succeeds and the
        // error path is exercised. We just want to confirm the proxy_command
        // branch is taken and surfaces a Connect error (not a panic).
        let cfg = SshConfig {
            host: "ignored".to_string(),
            port: 0,
            username: "u".to_string(),
            auth: SshAuth::Password(Zeroizing::new("p".to_string())),
            host_key_verification: HostKeyVerification::AcceptAll,
            jump_hosts: Vec::new(),
            // `false` exits 1 immediately, so stdin/stdout EOF and the SSH
            // handshake errors out — proves the proxy branch is wired.
            proxy_command: Some("false".to_string()),
        };
        let err = match SshTransport::connect(cfg).await {
            Err(e) => e,
            Ok(_) => panic!("expected connect to fail"),
        };
        assert!(matches!(err, TransportError::Connect(_)), "got {err:?}");
    }
}