From f4d645bb6884267149e060cd0b1435af2cfc8f67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rodolphe=20Br=C3=A9ard?= Date: Wed, 15 Feb 2023 11:44:18 +0100 Subject: [PATCH] Remove the useless CertificateManager struct --- acmed/src/certificate_manager.rs | 65 -------------------------------- acmed/src/main.rs | 1 - acmed/src/main_event_loop.rs | 62 +++++++++++++++++++++++------- 3 files changed, 49 insertions(+), 79 deletions(-) delete mode 100644 acmed/src/certificate_manager.rs diff --git a/acmed/src/certificate_manager.rs b/acmed/src/certificate_manager.rs deleted file mode 100644 index c095c75..0000000 --- a/acmed/src/certificate_manager.rs +++ /dev/null @@ -1,65 +0,0 @@ -use crate::acme_proto::request_certificate; -use crate::certificate::Certificate; -use crate::logs::HasLogger; -use crate::{AccountSync, EndpointSync}; -use std::time::Duration; -use tokio::time::sleep; - -#[derive(Clone, Debug)] -pub struct CertificateManager { - cert: Certificate, -} - -impl CertificateManager { - pub fn new(cert: Certificate) -> Self { - Self { cert } - } - - pub fn get_id(&self) -> String { - self.cert.get_id() - } - - pub fn get_account_name(&self) -> String { - self.cert.account_name.clone() - } - - pub fn get_endpoint_name(&self) -> String { - self.cert.endpoint_name.clone() - } - - pub async fn renew( - &mut self, - account_s: AccountSync, - endpoint_s: EndpointSync, - ) -> (&mut Self, AccountSync, EndpointSync) { - loop { - match self.cert.should_renew() { - Ok(true) => break, - Ok(false) => {} - Err(e) => { - self.cert.warn(&e.message); - } - } - sleep(Duration::from_secs(crate::DEFAULT_SLEEP_TIME)).await; - } - let mut account = account_s.write().await; - let mut endpoint = endpoint_s.write().await; - let (status, is_success) = - match request_certificate(&self.cert, &mut endpoint, &mut account) { - Ok(_) => ("success".to_string(), true), - Err(e) => { - let e = e.prefix("unable to renew the certificate"); - self.cert.warn(&e.message); - (e.message, false) - } - }; - match self.cert.call_post_operation_hooks(&status, is_success) { - Ok(_) => {} - Err(e) => { - let e = e.prefix("post-operation hook error"); - self.cert.warn(&e.message); - } - }; - (self, account_s.clone(), endpoint_s.clone()) - } -} diff --git a/acmed/src/main.rs b/acmed/src/main.rs index d8dbd37..8931a63 100644 --- a/acmed/src/main.rs +++ b/acmed/src/main.rs @@ -13,7 +13,6 @@ use tokio::runtime::Builder; mod account; mod acme_proto; mod certificate; -mod certificate_manager; mod config; mod duration; mod endpoint; diff --git a/acmed/src/main_event_loop.rs b/acmed/src/main_event_loop.rs index bea398e..96163c9 100644 --- a/acmed/src/main_event_loop.rs +++ b/acmed/src/main_event_loop.rs @@ -1,9 +1,10 @@ use crate::account::Account; +use crate::acme_proto::request_certificate; use crate::certificate::Certificate; -use crate::certificate_manager::CertificateManager; use crate::config; use crate::endpoint::Endpoint; use crate::hooks::HookType; +use crate::logs::HasLogger; use crate::storage::FileManager; use crate::{AccountSync, EndpointSync}; use acme_common::error::Error; @@ -12,9 +13,11 @@ use futures::stream::FuturesUnordered; use futures::StreamExt; use std::collections::HashMap; use std::sync::Arc; +use std::time::Duration; +use tokio::time::sleep; pub struct MainEventLoop { - cert_managers: HashMap, + certificates: HashMap, accounts: HashMap, endpoints: HashMap, } @@ -70,10 +73,8 @@ impl MainEventLoop { accounts.insert(name, account); } - // TODO: Virer la création de l'endpoint de ce bloc - // TODO: Construire une liste de CertificateManager et non de Certificate let mut endpoints: HashMap = HashMap::new(); - let mut cert_managers: HashMap = HashMap::new(); + let mut certificates: HashMap = HashMap::new(); for crt in cnf.certificate.iter() { let endpoint = crt.get_endpoint(&cnf, root_certs)?; let endpoint_name = endpoint.name.clone(); @@ -119,7 +120,7 @@ impl MainEventLoop { file_manager: fm, }; let crt_id = cert.get_id(); - if cert_managers.contains_key(&crt_id) { + if certificates.contains_key(&crt_id) { let msg = format!("{crt_id}: duplicate certificate id"); return Err(msg.into()); } @@ -133,11 +134,11 @@ impl MainEventLoop { if !endpoints.contains_key(&endpoint.name) { endpoints.insert(endpoint.name.clone(), endpoint); } - cert_managers.insert(crt_id, CertificateManager::new(cert)); + certificates.insert(crt_id, cert); } Ok(MainEventLoop { - cert_managers, + certificates, accounts: accounts .iter() .map(|(k, v)| (k.to_owned(), Arc::new(RwLock::new(v.to_owned())))) @@ -151,11 +152,11 @@ impl MainEventLoop { pub async fn run(&mut self) { let mut renewals = FuturesUnordered::new(); - for (_, crt) in self.cert_managers.iter_mut() { + for (_, crt) in self.certificates.iter_mut() { log::trace!("Adding certificate: {}", crt.get_id()); - if let Some(acc) = self.accounts.get(&crt.get_account_name()) { - if let Some(ept) = self.endpoints.get(&crt.get_endpoint_name()) { - renewals.push(crt.renew(acc.clone(), ept.clone())); + if let Some(acc) = self.accounts.get(&crt.account_name) { + if let Some(ept) = self.endpoints.get(&crt.endpoint_name) { + renewals.push(renew_certificate(crt, acc.clone(), ept.clone())); } else { } } else { @@ -167,8 +168,43 @@ impl MainEventLoop { return; } if let Some((crt, acc, ept)) = renewals.next().await { - renewals.push(crt.renew(acc, ept)); + renewals.push(renew_certificate(crt, acc, ept)); } } } } + +async fn renew_certificate( + certificate: &mut Certificate, + account_s: AccountSync, + endpoint_s: EndpointSync, +) -> (&mut Certificate, AccountSync, EndpointSync) { + loop { + match certificate.should_renew() { + Ok(true) => break, + Ok(false) => {} + Err(e) => { + certificate.warn(&e.message); + } + } + sleep(Duration::from_secs(crate::DEFAULT_SLEEP_TIME)).await; + } + let mut account = account_s.write().await; + let mut endpoint = endpoint_s.write().await; + let (status, is_success) = match request_certificate(certificate, &mut endpoint, &mut account) { + Ok(_) => ("success".to_string(), true), + Err(e) => { + let e = e.prefix("unable to renew the certificate"); + certificate.warn(&e.message); + (e.message, false) + } + }; + match certificate.call_post_operation_hooks(&status, is_success) { + Ok(_) => {} + Err(e) => { + let e = e.prefix("post-operation hook error"); + certificate.warn(&e.message); + } + }; + (certificate, account_s.clone(), endpoint_s.clone()) +}