-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #245 from umccr/refactor/swap-hyper
refactor: swap the hyper client for reqwest
- Loading branch information
Showing
16 changed files
with
731 additions
and
632 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
//! TLS configuration related to HTTP clients. | ||
//! | ||
|
||
use crate::error::Error; | ||
use crate::error::Error::IoError; | ||
use crate::tls::RootCertStorePair; | ||
use crate::tls::{load_certs, read_bytes}; | ||
use serde::Deserialize; | ||
|
||
/// A certificate and key pair used for TLS. Serialization is not implemented because there | ||
/// is no way to convert back to a `PathBuf`. | ||
#[derive(Deserialize, Debug, Clone, Default)] | ||
#[serde(try_from = "RootCertStorePair")] | ||
pub struct TlsClientConfig { | ||
cert: Option<Vec<reqwest::Certificate>>, | ||
identity: Option<reqwest::Identity>, | ||
} | ||
|
||
impl TlsClientConfig { | ||
/// Create a new TlsClientConfig. | ||
pub fn new(cert: Option<Vec<reqwest::Certificate>>, identity: Option<reqwest::Identity>) -> Self { | ||
Self { cert, identity } | ||
} | ||
|
||
/// Get the inner client config. | ||
pub fn into_inner(self) -> (Option<Vec<reqwest::Certificate>>, Option<reqwest::Identity>) { | ||
(self.cert, self.identity) | ||
} | ||
} | ||
|
||
impl TryFrom<RootCertStorePair> for TlsClientConfig { | ||
type Error = Error; | ||
|
||
fn try_from(root_store_pair: RootCertStorePair) -> crate::error::Result<Self> { | ||
let (key_pair, root_store) = root_store_pair.into_inner(); | ||
|
||
let cert = root_store | ||
.clone() | ||
.map(|cert_path| { | ||
let certs = load_certs(cert_path)?; | ||
|
||
certs | ||
.into_iter() | ||
.map(|cert| { | ||
reqwest::Certificate::from_der(&cert.0) | ||
.map_err(|err| IoError(format!("failed to read certificate from pem: {}", err))) | ||
}) | ||
.collect::<crate::error::Result<Vec<_>>>() | ||
}) | ||
.transpose()?; | ||
|
||
let identity = key_pair | ||
.clone() | ||
.map(|pair| { | ||
let key = read_bytes(pair.key)?; | ||
let certs = read_bytes(pair.cert)?; | ||
|
||
reqwest::Identity::from_pem(&[certs, key].concat()) | ||
.map_err(|err| IoError(format!("failed to pkcs8 pem identity: {}", err))) | ||
}) | ||
.transpose()?; | ||
|
||
Ok(Self::new(cert, identity)) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
pub(crate) mod tests { | ||
use crate::tls::tests::with_test_certificates; | ||
use crate::tls::{CertificateKeyPairPath, RootCertStorePair}; | ||
use std::path::Path; | ||
|
||
use super::*; | ||
|
||
#[tokio::test] | ||
async fn test_tls_client_config() { | ||
with_test_certificates(|path, _, _| { | ||
let client_config = client_config_from_path(path); | ||
let (certs, identity) = client_config.into_inner(); | ||
|
||
assert_eq!(certs.unwrap().len(), 1); | ||
assert!(identity.is_some()); | ||
}); | ||
} | ||
|
||
pub(crate) fn client_config_from_path(path: &Path) -> TlsClientConfig { | ||
TlsClientConfig::try_from(RootCertStorePair::new( | ||
Some(CertificateKeyPairPath::new( | ||
path.join("cert.pem"), | ||
path.join("key.pem"), | ||
)), | ||
Some(path.join("cert.pem")), | ||
)) | ||
.unwrap() | ||
} | ||
} |
Oops, something went wrong.