From 3fc51a70cdc76b5d3c80ea9ce5111188cceb1b91 Mon Sep 17 00:00:00 2001 From: Florian Uekermann Date: Sun, 10 Nov 2024 16:38:56 +0100 Subject: [PATCH] clippy --- .github/workflows/rust.yml | 1 + Cargo.toml | 2 +- src/acme.rs | 12 ++++++------ src/caches/dir.rs | 10 +++++----- src/caches/no.rs | 6 +++--- src/caches/test.rs | 7 +++++-- src/config.rs | 10 +++++----- src/lib.rs | 6 +++--- src/state.rs | 9 ++++++--- 9 files changed, 35 insertions(+), 28 deletions(-) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index cdf914e..9073d36 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -22,3 +22,4 @@ jobs: - run: rustup update ${{ matrix.toolchain }} && rustup default ${{ matrix.toolchain }} - run: cargo build --verbose --no-default-features --features ${{ matrix.crypto }},${{ matrix.tokio }} - run: cargo test --verbose --no-default-features --features ${{ matrix.crypto }},${{ matrix.tokio }} + - run: cargo clippy --tests --no-default-features --features ${{ matrix.crypto }},${{ matrix.tokio }} \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index 3c84f2f..cec88c5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rustls-acme" -version = "0.11.1" +version = "0.12.0" authors = ["Florian Uekermann "] edition = "2018" description = "TLS certificate management and serving using rustls" diff --git a/src/acme.rs b/src/acme.rs index 39a3e4d..0b611d9 100644 --- a/src/acme.rs +++ b/src/acme.rs @@ -27,7 +27,7 @@ pub struct Account { pub kid: String, } -static ALG: &'static EcdsaSigningAlgorithm = &ECDSA_P256_SHA256_FIXED_SIGNING; +static ALG: &EcdsaSigningAlgorithm = &ECDSA_P256_SHA256_FIXED_SIGNING; impl Account { pub fn generate_key_pair() -> Vec { @@ -41,7 +41,7 @@ impl Account { I: IntoIterator, { let key_pair = Self::generate_key_pair(); - Ok(Self::create_with_keypair(client_config, directory, contact, &key_pair).await?) + Self::create_with_keypair(client_config, directory, contact, &key_pair).await } pub async fn create_with_keypair<'a, S, I>( client_config: &Arc, @@ -86,7 +86,7 @@ impl Account { Ok((location, body)) } pub async fn new_order(&self, client_config: &Arc, domains: Vec) -> Result<(String, Order), AcmeError> { - let domains: Vec = domains.into_iter().map(|d| Identifier::Dns(d)).collect(); + let domains: Vec = domains.into_iter().map(Identifier::Dns).collect(); let payload = format!("{{\"identifiers\":{}}}", serde_json::to_string(&domains)?); let response = self.request(client_config, &self.directory.new_order, &payload).await?; let url = response.0.ok_or(AcmeError::MissingHeader("Location"))?; @@ -114,14 +114,14 @@ impl Account { pub async fn certificate(&self, client_config: &Arc, url: impl AsRef) -> Result { Ok(self.request(client_config, &url, "").await?.1) } - pub fn tls_alpn_01<'a>(&self, challenges: &'a Vec, domain: String) -> Result<(&'a Challenge, CertifiedKey), AcmeError> { - let challenge = challenges.iter().filter(|c| c.typ == ChallengeType::TlsAlpn01).next(); + pub fn tls_alpn_01<'a>(&self, challenges: &'a [Challenge], domain: String) -> Result<(&'a Challenge, CertifiedKey), AcmeError> { + let challenge = challenges.iter().find(|c| c.typ == ChallengeType::TlsAlpn01); let challenge = match challenge { Some(challenge) => challenge, None => return Err(AcmeError::NoTlsAlpn01Challenge), }; let mut params = rcgen::CertificateParams::new(vec![domain])?; - let key_auth = key_authorization_sha256(&self.key_pair, &*challenge.token)?; + let key_auth = key_authorization_sha256(&self.key_pair, &challenge.token)?; params.custom_extensions = vec![CustomExtension::new_acme_identifier(key_auth.as_ref())]; let key_pair = KeyPair::generate_for(&PKCS_ECDSA_P256_SHA256)?; let cert = params.self_signed(&key_pair)?; diff --git a/src/caches/dir.rs b/src/caches/dir.rs index 18add36..4dbe52d 100644 --- a/src/caches/dir.rs +++ b/src/caches/dir.rs @@ -20,7 +20,7 @@ impl + Send + Sync> DirCache

{ Ok(content) => Ok(Some(content)), Err(err) => match err.kind() { ErrorKind::NotFound => Ok(None), - _ => Err(err.into()), + _ => Err(err), }, } } @@ -57,11 +57,11 @@ impl + Send + Sync> DirCache

{ impl + Send + Sync> CertCache for DirCache

{ type EC = std::io::Error; async fn load_cert(&self, domains: &[String], directory_url: &str) -> Result>, Self::EC> { - let file_name = Self::cached_cert_file_name(&domains, directory_url); + let file_name = Self::cached_cert_file_name(domains, directory_url); self.read_if_exist(file_name).await } async fn store_cert(&self, domains: &[String], directory_url: &str, cert: &[u8]) -> Result<(), Self::EC> { - let file_name = Self::cached_cert_file_name(&domains, directory_url); + let file_name = Self::cached_cert_file_name(domains, directory_url); self.write(file_name, cert).await } } @@ -70,12 +70,12 @@ impl + Send + Sync> CertCache for DirCache

{ impl + Send + Sync> AccountCache for DirCache

{ type EA = std::io::Error; async fn load_account(&self, contact: &[String], directory_url: &str) -> Result>, Self::EA> { - let file_name = Self::cached_account_file_name(&contact, directory_url); + let file_name = Self::cached_account_file_name(contact, directory_url); self.read_if_exist(file_name).await } async fn store_account(&self, contact: &[String], directory_url: &str, account: &[u8]) -> Result<(), Self::EA> { - let file_name = Self::cached_account_file_name(&contact, directory_url); + let file_name = Self::cached_account_file_name(contact, directory_url); self.write(file_name, account).await } } diff --git a/src/caches/no.rs b/src/caches/no.rs index d2322ae..83346bd 100644 --- a/src/caches/no.rs +++ b/src/caches/no.rs @@ -10,7 +10,7 @@ use std::sync::atomic::AtomicPtr; /// # use rustls_acme::caches::NoCache; /// # type EC = std::io::Error; /// # type EA = EC; -/// let no_cache = NoCache::::new(); +/// let no_cache = NoCache::::default(); /// ``` #[derive(Copy, Clone)] pub struct NoCache { @@ -18,8 +18,8 @@ pub struct NoCache { _account_error: PhantomData>>, } -impl NoCache { - pub fn new() -> Self { +impl Default for NoCache { + fn default() -> Self { Self { _cert_error: Default::default(), _account_error: Default::default(), diff --git a/src/caches/test.rs b/src/caches/test.rs index 3f373b4..731f5b7 100644 --- a/src/caches/test.rs +++ b/src/caches/test.rs @@ -26,8 +26,8 @@ pub struct TestCache { _account_error: PhantomData>>, } -impl TestCache { - pub fn new() -> Self { +impl Default for TestCache { + fn default() -> Self { let mut params = CertificateParams::default(); let mut distinguished_name = DistinguishedName::new(); distinguished_name.push(DnType::CountryName, "US"); @@ -49,6 +49,9 @@ impl TestCache { _account_error: Default::default(), } } +} + +impl TestCache { pub fn ca_pem(&self) -> &str { &self.ca_pem } diff --git a/src/config.rs b/src/config.rs index 4b32c38..3f8d2a0 100644 --- a/src/config.rs +++ b/src/config.rs @@ -60,9 +60,9 @@ impl AcmeConfig { root_store.extend(TLS_SERVER_ROOTS.iter().map(|ta| { let ta = ta.to_owned(); TrustAnchor { - subject: ta.subject.into(), - subject_public_key_info: ta.subject_public_key_info.into(), - name_constraints: ta.name_constraints.map(Into::into), + subject: ta.subject, + subject_public_key_info: ta.subject_public_key_info, + name_constraints: ta.name_constraints, } })); let client_config = Arc::new( @@ -77,7 +77,7 @@ impl AcmeConfig { directory_url: LETS_ENCRYPT_STAGING_DIRECTORY.into(), domains: domains.into_iter().map(|s| s.as_ref().into()).collect(), contact: vec![], - cache: Box::new(NoCache::new()), + cache: Box::new(NoCache::default()), } } } @@ -143,7 +143,7 @@ impl AcmeConfig { pub fn cache_option(self, cache: Option) -> AcmeConfig { match cache { Some(cache) => self.cache(cache), - None => self.cache(NoCache::::new()), + None => self.cache(NoCache::::default()), } } pub fn state(self) -> AcmeState { diff --git a/src/lib.rs b/src/lib.rs index 748bcdb..6b77e4c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -105,7 +105,7 @@ //! //! Thanks to [Josh Triplett](https://github.com/joshtriplett) for contributions and feedback. -#![cfg_attr(doc_auto_cfg, feature(doc_auto_cfg))] +#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))] mod acceptor; pub mod acme; @@ -147,9 +147,9 @@ pub(crate) fn any_ecdsa_type( der: &futures_rustls::pki_types::PrivateKeyDer, ) -> Result, futures_rustls::rustls::Error> { #[cfg(all(feature = "ring", not(feature = "aws-lc-rs")))] - return futures_rustls::rustls::crypto::ring::sign::any_ecdsa_type(&der); + return futures_rustls::rustls::crypto::ring::sign::any_ecdsa_type(der); #[cfg(feature = "aws-lc-rs")] - return futures_rustls::rustls::crypto::aws_lc_rs::sign::any_ecdsa_type(&der); + return futures_rustls::rustls::crypto::aws_lc_rs::sign::any_ecdsa_type(der); } #[cfg(any(feature = "ring", feature = "aws-lc-rs"))] diff --git a/src/state.rs b/src/state.rs index b3be325..a69c30a 100644 --- a/src/state.rs +++ b/src/state.rs @@ -22,6 +22,7 @@ use std::time::Duration; use thiserror::Error; use x509_parser::parse_x509_certificate; +#[allow(clippy::type_complexity)] pub struct AcmeState { config: Arc>, resolver: Arc, @@ -152,7 +153,7 @@ impl AcmeState { .with_no_client_auth() .with_cert_resolver(self.resolver()); rustls_config.alpn_protocols.push(ACME_TLS_ALPN_NAME.to_vec()); - return Arc::new(rustls_config); + Arc::new(rustls_config) } /// Creates a default [rustls::ServerConfig] for accepting regular tls connections. Use this if [crate::is_tls_alpn_challenge] returns `false`. /// If you need a [rustls::ServerConfig], which uses the certificates acquired by this [AcmeState], @@ -168,7 +169,7 @@ impl AcmeState { .unwrap() .with_no_client_auth() .with_cert_resolver(self.resolver()); - return Arc::new(rustls_config); + Arc::new(rustls_config) } pub fn new(config: AcmeConfig) -> Self { let config = Arc::new(config); @@ -191,7 +192,7 @@ impl AcmeState { } } fn parse_cert(pem: &[u8]) -> Result<(CertifiedKey, [DateTime; 2]), CertParseError> { - let mut pems = pem::parse_many(&pem)?; + let mut pems = pem::parse_many(pem)?; if pems.len() < 2 { return Err(CertParseError::TooFewPem(pems.len())); } @@ -210,6 +211,8 @@ impl AcmeState { let cert = CertifiedKey::new(cert_chain, pk); Ok((cert, validity)) } + + #[allow(clippy::result_large_err)] fn process_cert(&mut self, pem: Vec, cached: bool) -> Event { let (cert, validity) = match (Self::parse_cert(&pem), cached) { (Ok(r), _) => r,