bssh 2.4.3

Parallel SSH command execution tool for cluster management
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
// Copyright 2025 Lablup Inc. and Jeongkyu Shin
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use super::core::SshClient;
use crate::jump::{JumpHostChain, parse_jump_hosts};
use crate::security::Password;
use crate::ssh::known_hosts::StrictHostKeyChecking;
use crate::ssh::tokio_client::{
    AuthMethod, Client, SshConnectionConfig, SshConnectionConfigResolver,
};
use anyhow::{Context, Result};
use std::path::Path;
use std::sync::Arc;
use std::time::Duration;

// SSH connection timeout design:
// - 30 seconds accommodates slow networks and SSH negotiation
// - Industry standard for SSH client connections
// - Balances user patience with reliability on poor networks
const SSH_CONNECT_TIMEOUT_SECS: u64 = 30;

/// Build the friendly, outer-context message for a failed direct SSH
/// connection attempt.
///
/// The result is meant to be used as `anyhow::Error::new(e).context(message)`
/// so the message renders as the outer layer and `e` renders as the cause.
/// Because anyhow's `{:#}` form joins layers with `": "`, a context message
/// that restates the variant's own `Display` text would make the same wording
/// appear twice in one line. So this returns `None` for variants whose
/// `Display` already says everything the context layer would, and the
/// messages it does return add remediation guidance without echoing the
/// cause and carry no trailing period (which would render as `".: "`).
/// See issue #238.
fn connect_error_message(e: &crate::ssh::tokio_client::Error) -> Option<String> {
    match e {
        crate::ssh::tokio_client::Error::KeyAuthFailed => {
            Some("The private key was rejected by the server".to_string())
        }
        crate::ssh::tokio_client::Error::ServerCheckFailed => Some(
            "Host key verification failed: the server's host key was not recognized or has changed"
                .to_string(),
        ),
        crate::ssh::tokio_client::Error::HostKeyChanged { host, port, .. } => {
            // Name the entry as known_hosts records it, so the command also
            // works for non-standard ports, and double quote it so zsh does not
            // reject the unmatched `[...]` glob. No `-f` here: this path has no
            // known_hosts path to hand, and production always uses the default
            // file, which `ssh-keygen` picks itself.
            let entry = crate::ssh::tokio_client::host_verification::known_hosts_entry_name(
                host, *port,
            );
            Some(format!(
                "Possible man-in-the-middle attack: verify the server's new key out of band, or remove the old entry with 'ssh-keygen -R \"{entry}\"' if the change is expected"
            ))
        }
        // Unlike `HostKeyChanged`, there is no `ssh-keygen -R` remediation to
        // offer: the marker was placed deliberately to blocklist this exact
        // key, and the entry it points at is the `@revoked` line itself, not
        // a stale pin to remove.
        crate::ssh::tokio_client::Error::HostKeyRevoked { .. } => Some(
            "The offered host key matches a key explicitly revoked in known_hosts; do not connect unless you can independently verify the server's identity out of band"
                .to_string(),
        ),
        crate::ssh::tokio_client::Error::SshError(russh::Error::UnknownKey) => Some(
            "The host is not in known_hosts and strict host key checking is enabled; connect once with '--strict-host-key-checking accept-new' or add the key manually"
                .to_string(),
        ),
        crate::ssh::tokio_client::Error::KeyInvalid(_) => {
            Some("Check the key file format and passphrase".to_string())
        }
        crate::ssh::tokio_client::Error::AgentConnectionFailed => {
            Some("Ensure SSH_AUTH_SOCK is set and the agent is running".to_string())
        }
        crate::ssh::tokio_client::Error::AgentNoIdentities => {
            Some("Add your key to the agent using 'ssh-add'".to_string())
        }
        // `PasswordWrong`, `AgentAuthenticationFailed` and `SshError` already
        // render the full story through their own `Display`, and any context
        // here would only repeat it.
        _ => None,
    }
}

impl SshClient {
    /// Determine the authentication method based on provided parameters.
    ///
    /// The `pre_collected_password` argument carries the password the dispatcher
    /// collected once up-front (via `--password`). When `use_password` is `true`
    /// and a pre-collected value is provided, `AuthContext::password_auth()`
    /// consumes it directly instead of prompting in the per-node task.
    pub(super) async fn determine_auth_method(
        &self,
        key_path: Option<&Path>,
        use_agent: bool,
        use_password: bool,
        #[cfg(target_os = "macos")] use_keychain: bool,
        pre_collected_password: Option<Arc<Password>>,
    ) -> Result<AuthMethod> {
        // Use centralized authentication logic from auth module
        let mut auth_ctx =
            crate::ssh::auth::AuthContext::new(self.username.clone(), self.host.clone())
                .with_context(|| {
                    format!("Invalid credentials for {}@{}", self.username, self.host)
                })?;

        // Set key path if provided
        if let Some(path) = key_path {
            auth_ctx = auth_ctx
                .with_key_path(Some(path.to_path_buf()))
                .with_context(|| format!("Invalid SSH key path: {path:?}"))?;
        }

        auth_ctx = auth_ctx
            .with_agent(use_agent)
            .with_password(use_password)
            .with_pre_collected_password(pre_collected_password);

        #[cfg(target_os = "macos")]
        {
            auth_ctx = auth_ctx.with_keychain(use_keychain);
        }

        auth_ctx.determine_method().await
    }

    /// Create a direct SSH connection (no jump hosts)
    pub(super) async fn connect_direct(
        &self,
        auth_method: &AuthMethod,
        strict_mode: StrictHostKeyChecking,
        connect_timeout_seconds: Option<u64>,
        ssh_connection_config: Option<&SshConnectionConfig>,
    ) -> Result<Client> {
        // SECURITY: Add rate limiting before connection attempts
        const RATE_LIMIT_DELAY: Duration = Duration::from_millis(100);
        tokio::time::sleep(RATE_LIMIT_DELAY).await;

        // SECURITY: Capture start time for timing attack mitigation
        let start_time = std::time::Instant::now();

        let addr = (self.host.as_str(), self.port);
        let check_method = crate::ssh::known_hosts::get_check_method(strict_mode);

        let connect_timeout =
            Duration::from_secs(connect_timeout_seconds.unwrap_or(SSH_CONNECT_TIMEOUT_SECS));

        let default_conn_cfg;
        let conn_cfg = match ssh_connection_config {
            Some(c) => c,
            None => {
                default_conn_cfg = SshConnectionConfig::default();
                &default_conn_cfg
            }
        };

        let result = match tokio::time::timeout(
            connect_timeout,
            Client::connect_with_ssh_config(
                addr,
                &self.username,
                auth_method.clone(),
                check_method,
                conn_cfg,
            ),
        )
        .await
        {
            Ok(Ok(client)) => Ok(client),
            Ok(Err(e)) => {
                // Specific error from the SSH connection attempt.
                // The friendly message is the outer context and `e` is the
                // cause, so `{:#}` renders "<friendly message>: <cause>"
                // instead of the reverse. Variants whose own `Display` is
                // already sufficient get no extra layer, so the same wording
                // is not printed twice; see issue #238.
                match connect_error_message(&e) {
                    Some(error_msg) => Err(anyhow::Error::new(e).context(error_msg)),
                    None => Err(anyhow::Error::new(e)),
                }
            }
            Err(_) => Err(anyhow::anyhow!(
                "Connection timeout after {} seconds. \
                     Please check if the host is reachable and SSH service is running.",
                connect_timeout.as_secs()
            )),
        };

        // SECURITY: Normalize timing to prevent timing attacks
        // Ensure all authentication attempts take at least 500ms to complete
        const MIN_AUTH_DURATION: Duration = Duration::from_millis(500);
        let elapsed = start_time.elapsed();
        if elapsed < MIN_AUTH_DURATION {
            tokio::time::sleep(MIN_AUTH_DURATION - elapsed).await;
        }

        result
    }

    /// Create an SSH connection through jump hosts
    #[allow(clippy::too_many_arguments)]
    pub(super) async fn connect_via_jump_hosts(
        &self,
        jump_hosts: &[crate::jump::parser::JumpHost],
        auth_method: &AuthMethod,
        strict_mode: StrictHostKeyChecking,
        key_path: Option<&Path>,
        use_agent: bool,
        use_password: bool,
        connect_timeout_seconds: Option<u64>,
        ssh_connection_config: Option<&SshConnectionConfig>,
        ssh_connection_config_resolver: Option<&SshConnectionConfigResolver>,
        pre_collected_password: Option<Arc<Password>>,
    ) -> Result<Client> {
        // Create jump host chain with user-specified or default connect timeout
        let connect_timeout =
            Duration::from_secs(connect_timeout_seconds.unwrap_or(SSH_CONNECT_TIMEOUT_SECS));
        let mut chain = JumpHostChain::new(jump_hosts.to_vec())
            .with_connect_timeout(connect_timeout)
            .with_command_timeout(Duration::from_secs(300))
            .with_ssh_password(pre_collected_password);
        if let Some(cfg) = ssh_connection_config {
            chain = chain.with_ssh_connection_config(cfg.clone());
        }
        if let Some(resolver) = ssh_connection_config_resolver {
            chain = chain.with_ssh_connection_config_resolver(resolver.clone());
        }

        // Connect through the chain
        let connection = chain
            .connect(
                &self.host,
                self.port,
                &self.username,
                auth_method.clone(),
                key_path,
                Some(strict_mode),
                use_agent,
                use_password,
            )
            .await
            .with_context(|| {
                format!(
                    "Failed to establish jump host connection to {}:{}",
                    self.host, self.port
                )
            })?;

        tracing::info!(
            "Jump host connection established: {}",
            connection.jump_info.path_description()
        );

        Ok(connection.client)
    }

    /// Establish a connection based on configuration (direct or via jump hosts)
    #[allow(clippy::too_many_arguments)]
    pub(super) async fn establish_connection(
        &self,
        auth_method: &AuthMethod,
        strict_mode: StrictHostKeyChecking,
        jump_hosts_spec: Option<&str>,
        key_path: Option<&Path>,
        use_agent: bool,
        use_password: bool,
        connect_timeout_seconds: Option<u64>,
        ssh_connection_config: Option<&SshConnectionConfig>,
        ssh_connection_config_resolver: Option<&SshConnectionConfigResolver>,
        pre_collected_password: Option<Arc<Password>>,
    ) -> Result<Client> {
        if let Some(jump_spec) = jump_hosts_spec {
            // Parse jump hosts
            let jump_hosts = parse_jump_hosts(jump_spec).with_context(|| {
                format!("Failed to parse jump host specification: '{jump_spec}'")
            })?;

            if jump_hosts.is_empty() {
                tracing::debug!("No valid jump hosts found, using direct connection");
                self.connect_direct(
                    auth_method,
                    strict_mode,
                    connect_timeout_seconds,
                    ssh_connection_config,
                )
                .await
            } else {
                tracing::info!(
                    "Connecting to {}:{} via {} jump host(s): {}",
                    self.host,
                    self.port,
                    jump_hosts.len(),
                    jump_hosts
                        .iter()
                        .map(|j| j.to_string())
                        .collect::<Vec<_>>()
                        .join(" -> ")
                );

                self.connect_via_jump_hosts(
                    &jump_hosts,
                    auth_method,
                    strict_mode,
                    key_path,
                    use_agent,
                    use_password,
                    connect_timeout_seconds,
                    ssh_connection_config,
                    ssh_connection_config_resolver,
                    pre_collected_password,
                )
                .await
            }
        } else {
            // Direct connection
            tracing::debug!("Using direct connection (no jump hosts)");
            self.connect_direct(
                auth_method,
                strict_mode,
                connect_timeout_seconds,
                ssh_connection_config,
            )
            .await
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::test_helpers::EnvGuard;
    use serial_test::serial;
    use tempfile::TempDir;

    #[tokio::test]
    async fn test_determine_auth_method_with_key() {
        let temp_dir = TempDir::new().unwrap();
        let key_path = temp_dir.path().join("test_key");
        std::fs::write(&key_path, "fake key content").unwrap();

        let client = SshClient::new("test.com".to_string(), 22, "user".to_string());
        let auth = client
            .determine_auth_method(
                Some(&key_path),
                false,
                false,
                #[cfg(target_os = "macos")]
                false,
                None,
            )
            .await
            .unwrap();

        match auth {
            AuthMethod::PrivateKeyFile { key_file_path, .. } => {
                // Path should be canonicalized now
                assert!(key_file_path.is_absolute());
            }
            _ => panic!("Expected PrivateKeyFile auth method"),
        }
    }

    #[cfg(target_os = "macos")]
    #[tokio::test]
    #[serial]
    async fn test_determine_auth_method_with_agent() {
        use std::os::unix::net::UnixListener;

        // Create a temporary directory for the socket and SSH keys
        let temp_dir = TempDir::new().unwrap();
        let socket_path = temp_dir.path().join("ssh-agent.sock");

        // Create a real Unix domain socket (required on macOS)
        // Note: This is a fake socket that doesn't implement SSH agent protocol
        let _listener = UnixListener::bind(&socket_path).unwrap();

        // Create a fake SSH key for fallback (since our fake agent has no identities)
        let ssh_dir = temp_dir.path().join(".ssh");
        std::fs::create_dir_all(&ssh_dir).unwrap();
        let key_content =
            "-----BEGIN PRIVATE KEY-----\nfake key content\n-----END PRIVATE KEY-----";
        std::fs::write(ssh_dir.join("id_rsa"), key_content).unwrap();

        // Guards restore prior values on drop.
        let _sock = EnvGuard::set("SSH_AUTH_SOCK", socket_path.to_str().unwrap());
        let _home = EnvGuard::set("HOME", temp_dir.path());

        let client = SshClient::new("test.com".to_string(), 22, "user".to_string());
        let auth = client
            .determine_auth_method(
                None,
                true,
                false,
                #[cfg(target_os = "macos")]
                false,
                None,
            )
            .await
            .unwrap();

        // With the agent identity check, if the agent has no identities (our fake socket),
        // it will fall back to key file authentication. Accept either outcome.
        match auth {
            AuthMethod::Agent => {
                // Real SSH agent with identities was found
            }
            AuthMethod::PrivateKeyFile { .. } => {
                // Fallback to key file (expected with fake socket)
            }
            _ => panic!("Expected Agent or PrivateKeyFile auth method"),
        }
    }

    #[cfg(target_os = "linux")]
    #[tokio::test]
    #[serial]
    async fn test_determine_auth_method_with_agent() {
        use std::os::unix::net::UnixListener;

        // Create a temporary directory for the socket and SSH keys
        let temp_dir = TempDir::new().unwrap();
        let socket_path = temp_dir.path().join("ssh-agent.sock");

        // Create a real Unix domain socket (required on Linux)
        // Note: This is a fake socket that doesn't implement SSH agent protocol
        let _listener = UnixListener::bind(&socket_path).unwrap();

        // Create a fake SSH key for fallback (since our fake agent has no identities)
        let ssh_dir = temp_dir.path().join(".ssh");
        std::fs::create_dir_all(&ssh_dir).unwrap();
        let key_content =
            "-----BEGIN PRIVATE KEY-----\nfake key content\n-----END PRIVATE KEY-----";
        std::fs::write(ssh_dir.join("id_rsa"), key_content).unwrap();

        // Guards restore prior values on drop.
        let _sock = EnvGuard::set("SSH_AUTH_SOCK", socket_path.to_str().unwrap());
        let _home = EnvGuard::set("HOME", temp_dir.path());

        let client = SshClient::new("test.com".to_string(), 22, "user".to_string());
        let auth = client
            .determine_auth_method(None, true, false, None)
            .await
            .unwrap();

        // With the agent identity check, if the agent has no identities (our fake socket),
        // it will fall back to key file authentication. Accept either outcome.
        match auth {
            AuthMethod::Agent => {
                // Real SSH agent with identities was found
            }
            AuthMethod::PrivateKeyFile { .. } => {
                // Fallback to key file (expected with fake socket)
            }
            _ => panic!("Expected Agent or PrivateKeyFile auth method"),
        }
    }

    #[test]
    fn test_determine_auth_method_with_password() {
        let _client = SshClient::new("test.com".to_string(), 22, "user".to_string());

        // Note: We can't actually test password prompt in unit tests
        // as it requires terminal input. This would need integration testing.
        // For now, we just verify the function compiles with the new parameter.
    }

    #[tokio::test]
    #[serial]
    async fn test_determine_auth_method_fallback_to_default() {
        // Create a fake home directory with default key
        let temp_dir = TempDir::new().unwrap();
        let ssh_dir = temp_dir.path().join(".ssh");
        std::fs::create_dir_all(&ssh_dir).unwrap();
        let default_key = ssh_dir.join("id_rsa");
        std::fs::write(&default_key, "fake key").unwrap();

        // Guards restore prior values on drop.
        let _home = EnvGuard::set("HOME", temp_dir.path().to_str().unwrap());
        let _sock = EnvGuard::remove("SSH_AUTH_SOCK");

        let client = SshClient::new("test.com".to_string(), 22, "user".to_string());
        let auth = client
            .determine_auth_method(
                None,
                false,
                false,
                #[cfg(target_os = "macos")]
                false,
                None,
            )
            .await
            .unwrap();

        match auth {
            AuthMethod::PrivateKeyFile { key_file_path, .. } => {
                // Path should be canonicalized now
                assert!(key_file_path.is_absolute());
            }
            _ => panic!("Expected PrivateKeyFile auth method"),
        }
    }

    #[test]
    fn test_connect_error_ordering_puts_friendly_message_first() {
        // Regression test for issue #238's readability defect: the friendly
        // message must be the OUTER context so `{:#}` renders it first,
        // followed by the underlying cause, instead of the reverse.
        let e = crate::ssh::tokio_client::Error::KeyAuthFailed;
        let cause_text = e.to_string();
        let error_msg = connect_error_message(&e).expect("KeyAuthFailed adds guidance");
        let err = anyhow::Error::new(e).context(error_msg);

        let rendered = format!("{err:#}");
        assert_eq!(
            rendered, "The private key was rejected by the server: Key authentication failed",
            "expected friendly message first, then the cause"
        );
        // The underlying cause must appear after the friendly message, not
        // before it, and must not be swallowed.
        let friendly_end = rendered
            .find("The private key was rejected by the server")
            .unwrap()
            + "The private key was rejected by the server".len();
        assert!(
            rendered[friendly_end..].contains(&cause_text),
            "expected the cause to appear after the friendly message, got: {rendered}"
        );
    }

    #[test]
    fn test_connect_error_message_omits_context_that_would_repeat_cause() {
        // Variants whose own `Display` already says everything get no extra
        // context layer, so the same wording is not printed twice. Before this
        // fix, `PasswordWrong` rendered as
        // "Password authentication failed.: Password authentication failed".
        for e in [
            crate::ssh::tokio_client::Error::PasswordWrong,
            crate::ssh::tokio_client::Error::AgentAuthenticationFailed,
            crate::ssh::tokio_client::Error::CommandDidntExit,
        ] {
            let cause_text = e.to_string();
            assert!(
                connect_error_message(&e).is_none(),
                "{cause_text} should not get a context layer"
            );

            let rendered = format!("{:#}", anyhow::Error::new(e));
            assert_eq!(rendered, cause_text);
            assert_eq!(
                rendered.matches(cause_text.as_str()).count(),
                1,
                "cause text should appear exactly once, got: {rendered}"
            );
        }
    }

    #[test]
    fn test_connect_error_message_does_not_duplicate_inner_key_error() {
        // `KeyInvalid`'s own `Display` already interpolates the underlying
        // key error, so the context layer must not interpolate it again.
        let key_err = russh::keys::Error::KeyIsCorrupt;
        let inner_text = key_err.to_string();
        let e = crate::ssh::tokio_client::Error::KeyInvalid(key_err);

        let error_msg = connect_error_message(&e).expect("KeyInvalid adds guidance");
        assert!(
            !error_msg.contains(&inner_text),
            "context must not echo the inner key error, got: {error_msg}"
        );

        let rendered = format!("{:#}", anyhow::Error::new(e).context(error_msg));
        assert_eq!(
            rendered.matches(inner_text.as_str()).count(),
            1,
            "inner key error should appear exactly once, got: {rendered}"
        );
    }

    #[test]
    fn test_connect_error_messages_have_no_trailing_period() {
        // `{:#}` joins layers with ": ", so a trailing period would render as
        // the awkward sequence ".: " in the middle of a line.
        for e in [
            crate::ssh::tokio_client::Error::KeyAuthFailed,
            crate::ssh::tokio_client::Error::ServerCheckFailed,
            crate::ssh::tokio_client::Error::AgentConnectionFailed,
            crate::ssh::tokio_client::Error::AgentNoIdentities,
            crate::ssh::tokio_client::Error::HostKeyChanged {
                host: "node1.example.com".to_string(),
                port: 22,
                line: 3,
            },
            crate::ssh::tokio_client::Error::HostKeyChanged {
                host: "node1.example.com".to_string(),
                port: 2222,
                line: 3,
            },
            crate::ssh::tokio_client::Error::HostKeyRevoked {
                host: "node1.example.com".to_string(),
                port: 22,
                line: 3,
            },
            crate::ssh::tokio_client::Error::SshError(russh::Error::UnknownKey),
        ] {
            let message = connect_error_message(&e).expect("variant adds guidance");
            assert!(
                !message.ends_with('.'),
                "context message must not end with a period, got: {message}"
            );
        }
    }

    #[test]
    fn test_connect_error_message_host_key_changed_adds_guidance_without_echo() {
        // The changed-key context layer must add remediation guidance (which
        // host entry to remove) without restating the cause's own wording,
        // which would render twice through anyhow's `{:#}` form (#239, #238).
        let e = crate::ssh::tokio_client::Error::HostKeyChanged {
            host: "node1.example.com".to_string(),
            port: 22,
            line: 7,
        };
        let cause_text = e.to_string();
        let message = connect_error_message(&e).expect("HostKeyChanged adds guidance");
        assert!(
            message.contains("ssh-keygen -R \"node1.example.com\""),
            "guidance must include the removal command, got: {message}"
        );
        assert!(
            !message.contains("has changed and no longer matches"),
            "context must not restate the cause, got: {message}"
        );

        let rendered = format!("{:#}", anyhow::Error::new(e).context(message));
        assert_eq!(
            rendered.matches(cause_text.as_str()).count(),
            1,
            "cause text should appear exactly once, got: {rendered}"
        );
        assert!(
            rendered.contains("line 7"),
            "the conflicting line number must survive into the chain, got: {rendered}"
        );
    }

    #[test]
    fn test_connect_error_message_host_key_changed_names_port_qualified_entry() {
        // known_hosts records a non-standard port as `[host]:port`, so
        // `ssh-keygen -R host` would remove nothing and leave the user stuck.
        // The guidance must name the entry that actually exists, quoted so the
        // unmatched `[...]` glob does not make zsh reject the command.
        let e = crate::ssh::tokio_client::Error::HostKeyChanged {
            host: "node1.example.com".to_string(),
            port: 2222,
            line: 7,
        };
        let message = connect_error_message(&e).expect("HostKeyChanged adds guidance");
        assert!(
            message.contains("ssh-keygen -R \"[node1.example.com]:2222\""),
            "guidance must name the port-qualified entry, got: {message}"
        );
        assert!(
            !message.contains("has changed and no longer matches"),
            "context must not restate the cause, got: {message}"
        );
    }

    #[test]
    fn test_connect_error_message_host_key_revoked_adds_guidance_without_echo() {
        // Same invariant as HostKeyChanged: the context layer adds guidance
        // without restating the cause's own wording, which would otherwise
        // render twice through anyhow's `{:#}` form (#239, #238).
        let e = crate::ssh::tokio_client::Error::HostKeyRevoked {
            host: "node1.example.com".to_string(),
            port: 22,
            line: 7,
        };
        let cause_text = e.to_string();
        let message = connect_error_message(&e).expect("HostKeyRevoked adds guidance");
        assert!(
            message.contains("explicitly revoked"),
            "guidance must warn about revocation, got: {message}"
        );
        assert!(
            !message.contains("is explicitly revoked by the known_hosts entry at line"),
            "context must not restate the cause, got: {message}"
        );

        let rendered = format!("{:#}", anyhow::Error::new(e).context(message));
        assert_eq!(
            rendered.matches(cause_text.as_str()).count(),
            1,
            "cause text should appear exactly once, got: {rendered}"
        );
    }
}