From 54ed931e80140798ac3b880f78cb99a76c5b8ab5 Mon Sep 17 00:00:00 2001 From: Olivier Guittonneau Date: Thu, 3 Apr 2025 18:12:07 +0200 Subject: [PATCH] Simplify http-to-https example --- Cargo.lock | 2 -- Cargo.toml | 1 - middleware/http-to-https/Cargo.toml | 1 - middleware/http-to-https/src/main.rs | 23 +++++++++++------------ 4 files changed, 11 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 53f52178..d5d91557 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2047,7 +2047,6 @@ dependencies = [ "log", "notify 6.1.1", "rustls 0.23.25", - "rustls-pemfile 2.2.0", "tokio", ] @@ -5094,7 +5093,6 @@ dependencies = [ "futures-util", "log", "rustls 0.23.25", - "rustls-pemfile 2.2.0", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index d6b22773..8eeb0c9d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -112,7 +112,6 @@ rand = "0.9" redis = { version = "0.27" } reqwest = { version = "0.12", features = ["json", "stream"] } rustls = "0.23" -rustls-pemfile = "2" serde = { version = "1", features = ["derive"] } serde_json = "1" time = "0.3" diff --git a/middleware/http-to-https/Cargo.toml b/middleware/http-to-https/Cargo.toml index 47c6c75b..d1a92b8f 100644 --- a/middleware/http-to-https/Cargo.toml +++ b/middleware/http-to-https/Cargo.toml @@ -9,4 +9,3 @@ env_logger.workspace = true futures-util.workspace = true log.workspace = true rustls.workspace = true -rustls-pemfile.workspace = true diff --git a/middleware/http-to-https/src/main.rs b/middleware/http-to-https/src/main.rs index 284e1562..df182900 100644 --- a/middleware/http-to-https/src/main.rs +++ b/middleware/http-to-https/src/main.rs @@ -1,9 +1,9 @@ -use std::{fs::File, io::BufReader}; - use actix_web::{App, HttpResponse, HttpServer, dev::Service, get, http}; use futures_util::future::{self, Either, FutureExt}; -use rustls::{ServerConfig, pki_types::PrivateKeyDer}; -use rustls_pemfile::{certs, pkcs8_private_keys}; +use rustls::{ + ServerConfig, + pki_types::{CertificateDer, PrivateKeyDer, pem::PemObject}, +}; #[get("/")] async fn index() -> String { @@ -18,18 +18,17 @@ async fn main() -> std::io::Result<()> { .install_default() .unwrap(); - let cert_file = &mut BufReader::new(File::open("cert.pem").unwrap()); - let key_file = &mut BufReader::new(File::open("key.pem").unwrap()); + let cert_chain = CertificateDer::pem_file_iter("cert.pem") + .unwrap() + .flatten() + .collect(); - let cert_chain = certs(cert_file).collect::, _>>().unwrap(); - let mut keys = pkcs8_private_keys(key_file) - .map(|key| key.map(PrivateKeyDer::Pkcs8)) - .collect::, _>>() - .unwrap(); + let key_der = + PrivateKeyDer::from_pem_file("key.pem").expect("Could not locate PKCS 8 private keys."); let config = ServerConfig::builder() .with_no_client_auth() - .with_single_cert(cert_chain, keys.remove(0)) + .with_single_cert(cert_chain, key_der) .unwrap(); log::info!(