russh-extra-core 0.1.2

Core types shared by russh-extra crates.
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
//! Error and result types used across the workspace.

use std::{borrow::Cow, error::Error as StdError, fmt};

use crate::CommandExit;

/// Boxed error source preserved for diagnostics.
pub type BoxError = Box<dyn StdError + Send + Sync + 'static>;

/// Result type used by `russh-extra`.
pub type Result<T, E = Error> = std::result::Result<T, E>;

/// Shared category details for typed error variants.
pub struct CategoryError<K> {
    kind: K,
    message: Cow<'static, str>,
    source: Option<BoxError>,
}

impl<K> CategoryError<K> {
    /// Creates a category error without a lower-level source.
    pub fn new(kind: K, message: impl Into<Cow<'static, str>>) -> Self {
        Self {
            kind,
            message: message.into(),
            source: None,
        }
    }

    /// Creates a category error with a lower-level source.
    pub fn with_source<E>(kind: K, message: impl Into<Cow<'static, str>>, source: E) -> Self
    where
        E: StdError + Send + Sync + 'static,
    {
        Self::with_boxed_source(kind, message, Box::new(source))
    }

    /// Creates a category error with an already boxed lower-level source.
    pub fn with_boxed_source(
        kind: K,
        message: impl Into<Cow<'static, str>>,
        source: BoxError,
    ) -> Self {
        Self {
            kind,
            message: message.into(),
            source: Some(source),
        }
    }

    /// Returns the stable subcategory.
    pub fn kind(&self) -> K
    where
        K: Copy,
    {
        self.kind
    }

    /// Returns the user-facing message.
    pub fn message(&self) -> &str {
        &self.message
    }

    /// Returns whether a lower-level source is preserved.
    pub fn has_source(&self) -> bool {
        self.source.is_some()
    }
}

impl<K> fmt::Debug for CategoryError<K>
where
    K: fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("CategoryError")
            .field("kind", &self.kind)
            .field("message", &self.message)
            .field("has_source", &self.source.is_some())
            .finish()
    }
}

impl<K> fmt::Display for CategoryError<K> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.message)
    }
}

impl<K> StdError for CategoryError<K>
where
    K: fmt::Debug + 'static,
{
    fn source(&self) -> Option<&(dyn StdError + 'static)> {
        self.source
            .as_ref()
            .map(|source| source.as_ref() as &(dyn StdError + 'static))
    }
}

/// Transport failure category.
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum TransportErrorKind {
    /// DNS resolution failed.
    Dns,
    /// TCP connection failed.
    TcpConnect,
    /// SSH negotiation failed.
    Negotiation,
    /// Keepalive failed.
    Keepalive,
    /// Encryption or MAC handling failed.
    Encryption,
    /// Transport I/O failed.
    Io,
    /// Other transport failure.
    Other,
}

/// Host-key verification failure category.
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum HostKeyErrorKind {
    /// Host key is unknown to the configured policy.
    Unknown,
    /// Host key changed from a previously trusted value.
    Changed,
    /// Host key was rejected by policy.
    Rejected,
    /// Host key algorithm or format is unsupported.
    Unsupported,
    /// Host key was unavailable when required.
    Unavailable,
}

/// Authentication failure category.
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum AuthenticationErrorKind {
    /// Credentials were rejected.
    Rejected,
    /// All configured credentials were exhausted.
    Exhausted,
    /// Authentication partially succeeded but did not complete.
    Partial,
    /// Requested authentication method is unsupported.
    UnsupportedMethod,
    /// Authentication could not be attempted.
    Unavailable,
}

/// Channel failure category.
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum ChannelErrorKind {
    /// Channel open failed.
    Open,
    /// Channel request failed.
    Request,
    /// Channel read failed.
    Read,
    /// Channel write failed.
    Write,
    /// Unexpected EOF.
    Eof,
    /// Channel close failed or arrived unexpectedly.
    Close,
    /// Protocol ordering or framing was invalid for the high-level API.
    Protocol,
}

/// SFTP failure category.
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum SftpErrorKind {
    /// Remote SFTP status response indicated failure (SSH_FX_* code).
    RemoteStatus,
    /// Packet was malformed or protocol framing is invalid.
    Protocol,
    /// SSH channel read/write failure.
    ChannelIo,
    /// Response request ID did not match an in-flight request.
    UnexpectedResponse,
    /// Protocol version is unsupported by the server.
    UnsupportedVersion,
    /// Feature not yet implemented.
    Unsupported,
    /// File or directory not found (SSH_FX_NO_SUCH_FILE).
    NoSuchFile,
    /// Insufficient permissions (SSH_FX_PERMISSION_DENIED).
    PermissionDenied,
}

/// Forwarding failure category.
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum ForwardingErrorKind {
    /// Local or remote bind failed.
    Bind,
    /// Listener setup failed.
    Listen,
    /// Accepting a forwarded connection failed.
    Accept,
    /// Connecting to a forwarding target failed.
    Connect,
    /// SSH global forwarding request failed.
    GlobalRequest,
    /// Opening a forwarding channel failed.
    ChannelOpen,
    /// Bidirectional stream copy failed.
    StreamCopy,
    /// Forwarding cancellation failed.
    Cancel,
    /// Forwarding shutdown failed.
    Shutdown,
}

/// High-level operation category used by timeout, cancellation, and disconnects.
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum Operation {
    /// Establishing a client connection.
    Connect,
    /// Authenticating a client or server session.
    Authentication,
    /// Opening a channel.
    ChannelOpen,
    /// Running a remote command.
    Command,
    /// Running an interactive shell.
    Shell,
    /// Running SFTP.
    Sftp,
    /// Running forwarding or tunnel work.
    Forwarding,
    /// Running server work.
    Server,
    /// Shutting down an operation.
    Shutdown,
    /// Other operation.
    Other,
}

/// Lower-level SSH failure category.
#[non_exhaustive]
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum SshErrorKind {
    /// Error came from `russh`.
    Russh,
    /// Other lower-level SSH error.
    Other,
}

/// Transport failure details.
pub type TransportError = CategoryError<TransportErrorKind>;
/// Host-key verification failure details.
pub type HostKeyError = CategoryError<HostKeyErrorKind>;
/// Authentication failure details.
pub type AuthenticationError = CategoryError<AuthenticationErrorKind>;
/// Channel failure details.
pub type ChannelError = CategoryError<ChannelErrorKind>;
/// SFTP failure details.
pub type SftpError = CategoryError<SftpErrorKind>;
/// Forwarding failure details.
pub type ForwardingError = CategoryError<ForwardingErrorKind>;
/// Timeout failure details.
pub type TimeoutError = CategoryError<Operation>;
/// Cancellation failure details.
pub type CancelledError = CategoryError<Operation>;
/// Remote disconnect failure details.
pub type DisconnectedError = CategoryError<Operation>;
/// Lower-level SSH failure details.
pub type SshError = CategoryError<SshErrorKind>;

/// Error type used by `russh-extra`.
#[non_exhaustive]
#[derive(Debug, thiserror::Error)]
pub enum Error {
    /// A builder or parser received invalid configuration.
    #[error("invalid configuration: {0}")]
    InvalidConfig(Cow<'static, str>),

    /// SSH transport failed.
    #[error("transport error: {0}")]
    Transport(#[source] TransportError),

    /// Host-key verification failed.
    #[error("host key verification failed: {0}")]
    HostKey(#[source] HostKeyError),

    /// Authentication was rejected or could not be attempted.
    #[error("authentication failed: {0}")]
    Authentication(#[source] AuthenticationError),

    /// A channel could not be opened or used.
    #[error("channel error: {0}")]
    Channel(#[source] ChannelError),

    /// A remote command exited unsuccessfully.
    #[error("remote command exited unsuccessfully: {exit:?}")]
    CommandExit {
        /// Reported remote command exit.
        exit: CommandExit,
    },

    /// SFTP operation failed.
    #[error("sftp error: {0}")]
    Sftp(#[source] SftpError),

    /// Forwarding operation failed.
    #[error("forwarding error: {0}")]
    Forwarding(#[source] ForwardingError),

    /// Operation timed out.
    #[error("operation timed out: {0}")]
    Timeout(#[source] TimeoutError),

    /// Operation was cancelled.
    #[error("operation cancelled: {0}")]
    Cancelled(#[source] CancelledError),

    /// Remote peer disconnected.
    #[error("remote disconnected: {0}")]
    Disconnected(#[source] DisconnectedError),

    /// A requested operation is not implemented or not supported.
    #[error("unsupported operation: {0}")]
    Unsupported(Cow<'static, str>),

    /// Local I/O failed.
    #[error(transparent)]
    Io(#[from] std::io::Error),

    /// An unclassified lower-level SSH error occurred.
    #[error("ssh error: {0}")]
    Ssh(#[source] SshError),
}

impl Error {
    /// Creates an invalid configuration error.
    pub fn invalid_config(message: impl Into<Cow<'static, str>>) -> Self {
        Self::InvalidConfig(message.into())
    }

    /// Creates a transport error.
    pub fn transport(kind: TransportErrorKind, message: impl Into<Cow<'static, str>>) -> Self {
        Self::Transport(TransportError::new(kind, message))
    }

    /// Creates a transport error with a lower-level source.
    pub fn transport_with_source<E>(
        kind: TransportErrorKind,
        message: impl Into<Cow<'static, str>>,
        source: E,
    ) -> Self
    where
        E: StdError + Send + Sync + 'static,
    {
        Self::Transport(TransportError::with_source(kind, message, source))
    }

    /// Creates a host-key verification error.
    pub fn host_key(kind: HostKeyErrorKind, message: impl Into<Cow<'static, str>>) -> Self {
        Self::HostKey(HostKeyError::new(kind, message))
    }

    /// Creates a host-key verification error with a lower-level source.
    pub fn host_key_with_source<E>(
        kind: HostKeyErrorKind,
        message: impl Into<Cow<'static, str>>,
        source: E,
    ) -> Self
    where
        E: StdError + Send + Sync + 'static,
    {
        Self::HostKey(HostKeyError::with_source(kind, message, source))
    }

    /// Creates an authentication error.
    pub fn authentication(message: impl Into<Cow<'static, str>>) -> Self {
        Self::Authentication(AuthenticationError::new(
            AuthenticationErrorKind::Rejected,
            message,
        ))
    }

    /// Creates an authentication error with a specific category.
    pub fn authentication_kind(
        kind: AuthenticationErrorKind,
        message: impl Into<Cow<'static, str>>,
    ) -> Self {
        Self::Authentication(AuthenticationError::new(kind, message))
    }

    /// Creates an authentication error with a lower-level source.
    pub fn authentication_with_source<E>(
        kind: AuthenticationErrorKind,
        message: impl Into<Cow<'static, str>>,
        source: E,
    ) -> Self
    where
        E: StdError + Send + Sync + 'static,
    {
        Self::Authentication(AuthenticationError::with_source(kind, message, source))
    }

    /// Creates a channel error.
    pub fn channel(message: impl Into<Cow<'static, str>>) -> Self {
        Self::Channel(ChannelError::new(ChannelErrorKind::Protocol, message))
    }

    /// Creates a channel error with a specific category.
    pub fn channel_kind(kind: ChannelErrorKind, message: impl Into<Cow<'static, str>>) -> Self {
        Self::Channel(ChannelError::new(kind, message))
    }

    /// Creates a channel error with a lower-level source.
    pub fn channel_with_source<E>(
        kind: ChannelErrorKind,
        message: impl Into<Cow<'static, str>>,
        source: E,
    ) -> Self
    where
        E: StdError + Send + Sync + 'static,
    {
        Self::Channel(ChannelError::with_source(kind, message, source))
    }

    /// Creates a remote command exit error.
    pub fn command_exit(exit: CommandExit) -> Self {
        Self::CommandExit { exit }
    }

    /// Creates an SFTP error.
    pub fn sftp(kind: SftpErrorKind, message: impl Into<Cow<'static, str>>) -> Self {
        Self::Sftp(SftpError::new(kind, message))
    }

    /// Creates an SFTP error with a lower-level source.
    pub fn sftp_with_source<E>(
        kind: SftpErrorKind,
        message: impl Into<Cow<'static, str>>,
        source: E,
    ) -> Self
    where
        E: StdError + Send + Sync + 'static,
    {
        Self::Sftp(SftpError::with_source(kind, message, source))
    }

    /// Creates a forwarding error.
    pub fn forwarding(kind: ForwardingErrorKind, message: impl Into<Cow<'static, str>>) -> Self {
        Self::Forwarding(ForwardingError::new(kind, message))
    }

    /// Creates a timeout error.
    pub fn timeout(operation: Operation, message: impl Into<Cow<'static, str>>) -> Self {
        Self::Timeout(TimeoutError::new(operation, message))
    }

    /// Creates a cancellation error.
    pub fn cancelled(operation: Operation, message: impl Into<Cow<'static, str>>) -> Self {
        Self::Cancelled(CancelledError::new(operation, message))
    }

    /// Creates a remote disconnect error.
    pub fn disconnected(operation: Operation, message: impl Into<Cow<'static, str>>) -> Self {
        Self::Disconnected(DisconnectedError::new(operation, message))
    }

    /// Creates an unsupported operation error.
    pub fn unsupported(message: impl Into<Cow<'static, str>>) -> Self {
        Self::Unsupported(message.into())
    }

    /// Creates an unclassified lower-level SSH error with a source.
    pub fn ssh_with_source<E>(message: impl Into<Cow<'static, str>>, source: E) -> Self
    where
        E: StdError + Send + Sync + 'static,
    {
        Self::Ssh(SshError::with_source(SshErrorKind::Other, message, source))
    }

    /// Returns whether this error is a timeout.
    pub fn is_timeout(&self) -> bool {
        matches!(self, Self::Timeout(_))
            || matches!(self, Self::Io(error) if error.kind() == std::io::ErrorKind::TimedOut)
    }

    /// Returns whether this error is a cancellation.
    pub fn is_cancelled(&self) -> bool {
        matches!(self, Self::Cancelled(_))
    }

    /// Returns whether this error is a remote disconnect.
    pub fn is_disconnected(&self) -> bool {
        matches!(self, Self::Disconnected(_))
    }
}

impl From<BoxError> for Error {
    fn from(source: BoxError) -> Self {
        Self::Ssh(SshError::with_boxed_source(
            SshErrorKind::Other,
            "lower-level SSH error",
            source,
        ))
    }
}

#[cfg(test)]
mod tests {
    use std::{error::Error as StdError, fmt};

    use crate::{
        AuthenticationErrorKind, Error, HostKeyError, HostKeyErrorKind, Operation, SshErrorKind,
        TransportError, TransportErrorKind,
    };

    #[derive(Debug)]
    struct SourceError;

    impl fmt::Display for SourceError {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            f.write_str("source display")
        }
    }

    impl StdError for SourceError {}

    #[derive(Debug)]
    struct SecretSource;

    impl fmt::Display for SecretSource {
        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
            f.write_str("secret-source-display")
        }
    }

    impl StdError for SecretSource {}

    #[test]
    fn category_errors_preserve_source_without_debugging_it() {
        let error = TransportError::with_source(
            TransportErrorKind::Dns,
            "failed to resolve host",
            SecretSource,
        );

        assert_eq!(error.kind(), TransportErrorKind::Dns);
        assert_eq!(error.message(), "failed to resolve host");
        assert!(error.has_source());
        assert!(StdError::source(&error).is_some());

        let debug = format!("{error:?}");
        assert!(debug.contains("Dns"));
        assert!(debug.contains("has_source: true"));
        assert!(!debug.contains("secret-source-display"));
    }

    #[test]
    fn top_level_errors_preserve_category_sources() {
        let error = Error::transport_with_source(
            TransportErrorKind::TcpConnect,
            "tcp connect failed",
            SourceError,
        );

        let category = StdError::source(&error).expect("category source");
        assert_eq!(category.to_string(), "tcp connect failed");
        assert!(category.source().is_some());
    }

    #[test]
    fn helper_predicates_classify_common_control_flow() {
        assert!(Error::timeout(Operation::Connect, "connect timed out").is_timeout());
        assert!(Error::from(std::io::Error::new(std::io::ErrorKind::TimedOut, "io")).is_timeout());
        assert!(Error::cancelled(Operation::Command, "cancelled").is_cancelled());
        assert!(Error::disconnected(Operation::Sftp, "disconnect").is_disconnected());
        assert!(!Error::unsupported("not yet").is_timeout());
    }

    #[test]
    fn typed_variants_expose_stable_kinds() {
        let auth = Error::authentication_kind(AuthenticationErrorKind::Exhausted, "no credentials");
        let Error::Authentication(auth) = auth else {
            panic!("expected authentication error");
        };
        assert_eq!(auth.kind(), AuthenticationErrorKind::Exhausted);

        let host_key = Error::HostKey(HostKeyError::new(
            HostKeyErrorKind::Changed,
            "host key changed",
        ));
        let Error::HostKey(host_key) = host_key else {
            panic!("expected host key error");
        };
        assert_eq!(host_key.kind(), HostKeyErrorKind::Changed);
    }

    #[test]
    fn boxed_sources_convert_to_unclassified_ssh_errors() {
        let error = Error::from(Box::new(SourceError) as crate::BoxError);
        let Error::Ssh(ssh) = error else {
            panic!("expected ssh error");
        };

        assert_eq!(ssh.kind(), SshErrorKind::Other);
        assert!(ssh.has_source());
    }
}