diff options
Diffstat (limited to 'src/sshkeys.rs')
-rw-r--r-- | src/sshkeys.rs | 49 |
1 files changed, 37 insertions, 12 deletions
diff --git a/src/sshkeys.rs b/src/sshkeys.rs index 1425cb3..349cabe 100644 --- a/src/sshkeys.rs +++ b/src/sshkeys.rs @@ -3,7 +3,7 @@ use std::fs::{read, File, Permissions}; use std::io::Write; use std::os::unix::fs::PermissionsExt; -use std::path::Path; +use std::path::{Path, PathBuf}; use std::process::Command; use tempfile::tempdir; @@ -18,9 +18,29 @@ pub enum KeyError { #[error("ssh-keygen failed to certify a key: {0}")] CertError(String), - /// I/O error. - #[error(transparent)] - IoError(#[from] std::io::Error), + /// Error creating a temporary directory. + #[error("Couldn't create temporary directory")] + TempDir(#[source] std::io::Error), + + /// Error running ssh-keygen. + #[error("Couldn't run ssh-keygen")] + Run(#[source] std::io::Error), + + /// Error reading a file. + #[error("Couldn't read file {0}")] + Read(PathBuf, #[source] std::io::Error), + + /// Error writing a file. + #[error("Couldn't write file {0}")] + Write(PathBuf, #[source] std::io::Error), + + /// Error creating a file. + #[error("Couldn't create file {0}")] + Create(PathBuf, #[source] std::io::Error), + + /// Error setting file permissions. + #[error("Couldn't set permissions for file {0}")] + SetPerm(PathBuf, #[source] std::io::Error), /// Error parsing a string as UTF8. #[error(transparent)] @@ -90,7 +110,7 @@ impl KeyPair { /// Generate a new key pair of the desired kind. pub fn generate(kind: KeyKind) -> Result<Self, KeyError> { - let dirname = tempdir()?; + let dirname = tempdir().map_err(|err| KeyError::TempDir(err))?; let private_key = dirname.path().join("key"); let output = Command::new("ssh-keygen") .arg("-f") @@ -101,7 +121,8 @@ impl KeyPair { .arg(format!("{}", kind.bits())) .arg("-N") .arg("") - .output()?; + .output() + .map_err(|err| KeyError::Run(err))?; if !output.status.success() { let stderr = String::from_utf8_lossy(&output.stderr).into_owned(); @@ -128,7 +149,7 @@ impl KeyPair { } fn read_string(filename: &Path) -> Result<String, KeyError> { - let bytes = read(filename)?; + let bytes = read(filename).map_err(|err| KeyError::Read(filename.to_path_buf(), err))?; Ok(String::from_utf8(bytes)?) } @@ -157,7 +178,7 @@ impl CaKey { /// /// Return as a string. pub fn certify_host(&self, host_key: &KeyPair, hostname: &str) -> Result<String, KeyError> { - let dirname = tempdir()?; + let dirname = tempdir().map_err(|err| KeyError::TempDir(err))?; let ca_key = dirname.path().join("ca"); let host_key_pub = dirname.path().join("host.pub"); let cert = dirname.path().join("host-cert.pub"); @@ -174,7 +195,8 @@ impl CaKey { .arg("-I") .arg(format!("host key for {}", hostname)) .arg(&host_key_pub) - .output()?; + .output() + .map_err(|err| KeyError::Run(err))?; if !output.status.success() { let stderr = String::from_utf8_lossy(&output.stderr).into_owned(); @@ -186,10 +208,13 @@ impl CaKey { } fn write_string(filename: &Path, s: &str) -> Result<(), KeyError> { - let mut file = File::create(filename)?; + let mut file = + File::create(filename).map_err(|err| KeyError::Create(filename.to_path_buf(), err))?; let ro_user = Permissions::from_mode(0o600); - file.set_permissions(ro_user)?; - file.write_all(s.as_bytes())?; + file.set_permissions(ro_user) + .map_err(|err| KeyError::SetPerm(filename.to_path_buf(), err))?; + file.write_all(s.as_bytes()) + .map_err(|err| KeyError::Write(filename.to_path_buf(), err))?; Ok(()) } |