Skip to content

Commit a0196b4

Browse files
authored
Use rustls instead of openssl (#133)
Use rustls instead of openssl. This involves switching out openssl for rustls in actix web. This commit introduces no additional breaking changes in how keys are registered with actix, so they should work as before. Fixes #121
1 parent 7f46eac commit a0196b4

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

server/Cargo.toml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ categories = ["olap", "analytics-store"]
1010

1111
[dependencies]
1212
actix-web-httpauth = "0.6"
13-
actix-web = { version = "4.1", features = ["openssl"] }
13+
actix-web = { version = "4.1", features = ["rustls"] }
1414
actix-cors = "0.6"
1515
actix-files = "0.6.1"
1616
anyhow = { version = "1.0.43", features = ["backtrace"] }
@@ -30,10 +30,11 @@ http = "0.2.4"
3030
lazy_static = "1.4.0"
3131
log = "0.4.14"
3232
num_cpus = "1.0.0"
33-
openssl = { version = "0.10" }
3433
os_info = "3.0.7"
3534
hostname = "0.3"
3635
rand = "0.8.4"
36+
rustls = "0.20.6"
37+
rustls-pemfile = "1.0.1"
3738
rust-flatten-json = "0.2.0"
3839
semver = "1.0.14"
3940
serde = "^1.0.8"

server/src/main.rs

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,14 @@ use chrono::{DateTime, NaiveDateTime, Timelike, Utc};
2626
use clokwerk::{AsyncScheduler, Scheduler, TimeUnits};
2727
use filetime::FileTime;
2828
use log::warn;
29-
use openssl::ssl::{SslAcceptor, SslFiletype, SslMethod};
29+
use rustls::{Certificate, PrivateKey, ServerConfig};
30+
use rustls_pemfile::{certs, pkcs8_private_keys};
3031
use thread_priority::{ThreadBuilder, ThreadPriority};
3132

3233
include!(concat!(env!("OUT_DIR"), "/generated.rs"));
3334

34-
use std::fs;
35+
use std::fs::{self, File};
36+
use std::io::BufReader;
3537
use std::panic::{catch_unwind, AssertUnwindSafe};
3638
use std::path::Path;
3739
use std::thread::{self, JoinHandle};
@@ -274,19 +276,40 @@ async fn run_http() -> anyhow::Result<()> {
274276
&CONFIG.parseable.tls_key_path,
275277
) {
276278
(Some(cert), Some(key)) => {
277-
let mut builder = SslAcceptor::mozilla_intermediate(SslMethod::tls())?;
278-
builder.set_private_key_file(key, SslFiletype::PEM)?;
279-
builder.set_certificate_chain_file(cert)?;
280-
Some(builder)
279+
// init server config builder with safe defaults
280+
let config = ServerConfig::builder()
281+
.with_safe_defaults()
282+
.with_no_client_auth();
283+
284+
// load TLS key/cert files
285+
let cert_file = &mut BufReader::new(File::open(cert)?);
286+
let key_file = &mut BufReader::new(File::open(key)?);
287+
288+
// convert files to key/cert objects
289+
let cert_chain = certs(cert_file)?.into_iter().map(Certificate).collect();
290+
291+
let mut keys: Vec<PrivateKey> = pkcs8_private_keys(key_file)?
292+
.into_iter()
293+
.map(PrivateKey)
294+
.collect();
295+
296+
// exit if no keys could be parsed
297+
if keys.is_empty() {
298+
anyhow::bail!("Could not locate PKCS 8 private keys.");
299+
}
300+
301+
let server_config = config.with_single_cert(cert_chain, keys.remove(0))?;
302+
303+
Some(server_config)
281304
}
282305
(_, _) => None,
283306
};
284307

285308
// concurrent workers equal to number of cores on the cpu
286309
let http_server = HttpServer::new(move || create_app!()).workers(num_cpus::get());
287-
if let Some(builder) = ssl_acceptor {
310+
if let Some(config) = ssl_acceptor {
288311
http_server
289-
.bind_openssl(&CONFIG.parseable.address, builder)?
312+
.bind_rustls(&CONFIG.parseable.address, config)?
290313
.run()
291314
.await?;
292315
} else {

0 commit comments

Comments
 (0)