Skip to content

Commit

Permalink
Improve TLS loader (fix after merge from master)
Browse files Browse the repository at this point in the history
  • Loading branch information
madadam committed Jan 28, 2025
1 parent 224f889 commit 2912b7d
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions service/src/tls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub(crate) async fn make_server_config(
let key = keys.into_iter().next().ok_or_else(|| {
tracing::error!(
"failed to load TLS key from {}: no keys found",
cert_path.display()
key_path.display()
);

Error::TlsKeysNotFound
Expand Down Expand Up @@ -125,7 +125,7 @@ async fn load_certificates_from_dir(dir: &Path) -> io::Result<Vec<CertificateDer
async fn load_certificates_from_file(
path: impl AsRef<Path>,
) -> io::Result<Vec<CertificateDer<'static>>> {
load_pems(path.as_ref(), "CERTIFICATE")
load_pems(path.as_ref(), &["CERTIFICATE"])
.await
.map(|pems| pems.map(|content| content.into()).collect())
}
Expand All @@ -134,23 +134,28 @@ async fn load_certificates_from_file(
pub async fn load_keys_from_file(
path: impl AsRef<Path>,
) -> io::Result<Vec<PrivateKeyDer<'static>>> {
load_pems(path.as_ref(), "PRIVATE KEY").await.map(|pems| {
load_pems(
path.as_ref(),
&["PRIVATE KEY", "EC PRIVATE KEY", "RSA PRIVATE KEY"],
)
.await
.map(|pems| {
pems.map(|content| PrivatePkcs8KeyDer::from(content).into())
.collect()
})
}

async fn load_pems<'a>(
path: &Path,
tag: &'a str,
tags: &'a [&str],
) -> io::Result<impl Iterator<Item = Vec<u8>> + 'a> {
let content = fs::read(path).await?;

pem::parse_many(content)
.map_err(|error| io::Error::new(io::ErrorKind::InvalidInput, error))
.map(move |pems| {
pems.into_iter()
.filter(move |pem| pem.tag() == tag)
.filter(move |pem| tags.contains(&pem.tag()))
.map(|pem| pem.into_contents())
})
}

0 comments on commit 2912b7d

Please sign in to comment.