use passalong_core::store::StoreError;
#[derive(Debug, thiserror::Error)]
pub enum SshError {
#[error("invalid server.ssh.host_key: {0}")]
InvalidHostKey(String),
#[error(
"host key mismatch for {host}: the server presented {actual}, but server.ssh.host_key is {expected}. \
If the server's key really changed, get the new key with `ssh-keyscan -t ed25519 {host}` \
and update server.ssh.host_key"
)]
HostKeyMismatch {
host: String,
expected: String,
actual: String,
},
#[error("cannot load the SSH key {path}: {message}")]
KeyLoad {
path: String,
message: String,
},
#[error("cannot connect to {address}: {message}")]
Connect {
address: String,
message: String,
},
#[error("connecting to {address} timed out after {secs} s")]
Timeout {
address: String,
secs: u64,
},
#[error("the server at {address} rejected the key {path} for user `{user}`")]
AuthenticationFailed {
address: String,
user: String,
path: String,
},
#[error(
"{what}: RSA keys need passalong built with the rsa feature \
(cargo install passalong --features rsa); Ed25519 keys work in every build"
)]
RsaUnsupported {
what: String,
},
#[error("cannot start SFTP on {address}: {message}")]
Sftp {
address: String,
message: String,
},
}
impl From<SshError> for StoreError {
fn from(err: SshError) -> Self {
StoreError::Backend(err.to_string())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn mismatch_message_explains_how_to_fix_it() {
let err = SshError::HostKeyMismatch {
host: "nas".into(),
expected: "SHA256:aaa".into(),
actual: "SHA256:bbb".into(),
};
let msg = err.to_string();
assert!(msg.starts_with("host key mismatch for nas"), "{msg}");
assert!(
msg.contains("SHA256:bbb") && msg.contains("ssh-keyscan -t ed25519 nas"),
"{msg}"
);
match StoreError::from(err) {
StoreError::Backend(text) => assert_eq!(text, msg),
other => panic!("unexpected {other:?}"),
}
}
}