From 43c9eee202c82ddfd4e435410a39f28b032634d7 Mon Sep 17 00:00:00 2001 From: Rodolphe Breard Date: Mon, 24 Aug 2020 16:25:22 +0200 Subject: [PATCH] Remove a few unwrap --- acmed/src/config.rs | 15 ++++++++++----- acmed/src/http.rs | 2 +- acmed/src/main_event_loop.rs | 3 +-- acmed/src/storage.rs | 13 ++++++++++--- 4 files changed, 22 insertions(+), 11 deletions(-) diff --git a/acmed/src/config.rs b/acmed/src/config.rs index f490e27..58baa6e 100644 --- a/acmed/src/config.rs +++ b/acmed/src/config.rs @@ -338,12 +338,17 @@ impl Certificate { } } - pub fn get_crt_name(&self) -> String { - match &self.name { + pub fn get_crt_name(&self) -> Result { + let name = match &self.name { Some(n) => n.to_string(), - None => self.domains.first().unwrap().dns.to_owned(), - } - .replace("*", "_") + None => self + .domains + .first() + .ok_or_else(|| Error::from("Certificate has no domain names."))? + .dns + .to_owned(), + }; + Ok(name.replace("*", "_")) } pub fn get_crt_name_format(&self) -> String { diff --git a/acmed/src/http.rs b/acmed/src/http.rs index 6fb1e33..c3a5f47 100644 --- a/acmed/src/http.rs +++ b/acmed/src/http.rs @@ -109,7 +109,7 @@ where let _ = new_nonce(endpoint, root_certs); } for _ in 0..crate::DEFAULT_HTTP_FAIL_NB_RETRY { - let nonce = &endpoint.nonce.clone().unwrap(); + let nonce = &endpoint.nonce.clone().unwrap_or_default(); let body = data_builder(&nonce, url)?; rate_limit(endpoint); let response = session.post(url).text(&body).send()?; diff --git a/acmed/src/main_event_loop.rs b/acmed/src/main_event_loop.rs index 0f5e7ba..b5af93c 100644 --- a/acmed/src/main_event_loop.rs +++ b/acmed/src/main_event_loop.rs @@ -53,7 +53,7 @@ impl MainEventLoop { hooks: crt.get_hooks(&cnf)?, account_directory: cnf.get_account_dir(), crt_directory: crt.get_crt_dir(&cnf), - crt_name: crt.get_crt_name(), + crt_name: crt.get_crt_name()?, crt_name_format: crt.get_crt_name_format(), cert_file_mode: cnf.get_cert_file_mode(), cert_file_owner: cnf.get_cert_file_user(), @@ -109,7 +109,6 @@ impl MainEventLoop { let handle = thread::spawn(move || { let mut endpoint = lock.write().unwrap(); for crt in certs_to_renew { - //let root_certs = rc.clone(); renew_certificate(&crt, &rc, &mut endpoint); } }); diff --git a/acmed/src/storage.rs b/acmed/src/storage.rs index 67c2f39..5567cc8 100644 --- a/acmed/src/storage.rs +++ b/acmed/src/storage.rs @@ -93,7 +93,9 @@ fn set_owner(cert: &Certificate, path: &PathBuf, file_type: FileType) -> Result< let uid = match uid { Some(u) => { if u.bytes().all(|b| b.is_ascii_digit()) { - let raw_uid = u.parse::().unwrap(); + let raw_uid = u + .parse::() + .map_err(|_| Error::from("Unable to parse the UID"))?; let nix_uid = nix::unistd::Uid::from_raw(raw_uid); Some(nix_uid) } else { @@ -106,7 +108,9 @@ fn set_owner(cert: &Certificate, path: &PathBuf, file_type: FileType) -> Result< let gid = match gid { Some(g) => { if g.bytes().all(|b| b.is_ascii_digit()) { - let raw_gid = g.parse::().unwrap(); + let raw_gid = g + .parse::() + .map_err(|_| Error::from("Unable to parse the GID"))?; let nix_gid = nix::unistd::Gid::from_raw(raw_gid); Some(nix_gid) } else { @@ -219,7 +223,10 @@ fn check_files(cert: &Certificate, file_types: &[FileType]) -> bool { return false; } }; - cert.trace(&format!("Testing file path: {}", path.to_str().unwrap())); + cert.trace(&format!( + "Testing file path: {}", + path.to_str().unwrap_or_default() + )); if !path.is_file() { return false; }