Skip to content

Commit 2c91f18

Browse files
enhancement: accept other certificates
env var P_OTHER_CERT_PATH accepts a directory path where user can keep all the certificates intended to be accepted by the server
1 parent ef8eb34 commit 2c91f18

File tree

5 files changed

+39
-2
lines changed

5 files changed

+39
-2
lines changed

server/src/cli.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ pub struct Cli {
3434
/// The location of TLS Private Key file
3535
pub tls_key_path: Option<PathBuf>,
3636

37+
/// The location of other certificates to accept
38+
pub other_cert_path: Option<PathBuf>,
39+
3740
/// The address on which the http server will listen.
3841
pub address: String,
3942

@@ -115,6 +118,7 @@ impl Cli {
115118
// identifiers for arguments
116119
pub const TLS_CERT: &'static str = "tls-cert-path";
117120
pub const TLS_KEY: &'static str = "tls-key-path";
121+
pub const OTHER_CERT: &'static str = "other-cert-path";
118122
pub const ADDRESS: &'static str = "address";
119123
pub const DOMAIN_URI: &'static str = "origin";
120124
pub const STAGING: &'static str = "local-staging-path";
@@ -175,6 +179,14 @@ impl Cli {
175179
.value_parser(validation::file_path)
176180
.help("Local path on this device where private key file is located. Required to enable TLS"),
177181
)
182+
.arg(
183+
Arg::new(Self::OTHER_CERT)
184+
.long(Self::OTHER_CERT)
185+
.env("P_OTHER_CERT_PATH")
186+
.value_name("DIR")
187+
.value_parser(validation::canonicalize_path)
188+
.help("Local path on this device where other certificate files are located.")
189+
)
178190
.arg(
179191
Arg::new(Self::ADDRESS)
180192
.long(Self::ADDRESS)
@@ -455,6 +467,7 @@ impl FromArgMatches for Cli {
455467
self.query_cache_path = m.get_one::<PathBuf>(Self::QUERY_CACHE).cloned();
456468
self.tls_cert_path = m.get_one::<PathBuf>(Self::TLS_CERT).cloned();
457469
self.tls_key_path = m.get_one::<PathBuf>(Self::TLS_KEY).cloned();
470+
self.other_cert_path = m.get_one::<PathBuf>(Self::OTHER_CERT).cloned();
458471
self.domain_address = m.get_one::<Url>(Self::DOMAIN_URI).cloned();
459472

460473
self.address = m

server/src/handlers/http/modal/ingest_server.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ impl ParseableServer for IngestServer {
7979
let ssl = get_ssl_acceptor(
8080
&CONFIG.parseable.tls_cert_path,
8181
&CONFIG.parseable.tls_key_path,
82+
&CONFIG.parseable.other_cert_path,
8283
)?;
8384

8485
// fn that creates the app

server/src/handlers/http/modal/query_server.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ impl ParseableServer for QueryServer {
6363
let ssl = get_ssl_acceptor(
6464
&CONFIG.parseable.tls_cert_path,
6565
&CONFIG.parseable.tls_key_path,
66+
&CONFIG.parseable.other_cert_path,
6667
)?;
6768

6869
let create_app_fn = move || {

server/src/handlers/http/modal/server.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ impl ParseableServer for Server {
9494
let ssl = get_ssl_acceptor(
9595
&CONFIG.parseable.tls_cert_path,
9696
&CONFIG.parseable.tls_key_path,
97+
&CONFIG.parseable.other_cert_path,
9798
)?;
9899

99100
// concurrent workers equal to number of cores on the cpu

server/src/handlers/http/modal/ssl_acceptor.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,42 @@
1616
*
1717
*/
1818

19-
use std::{fs::File, io::BufReader, path::PathBuf};
19+
use std::{
20+
fs::{self, File},
21+
io::BufReader,
22+
path::PathBuf,
23+
};
2024

2125
use rustls::ServerConfig;
2226

2327
pub fn get_ssl_acceptor(
2428
tls_cert: &Option<PathBuf>,
2529
tls_key: &Option<PathBuf>,
30+
other_certs: &Option<PathBuf>,
2631
) -> anyhow::Result<Option<ServerConfig>> {
2732
match (tls_cert, tls_key) {
2833
(Some(cert), Some(key)) => {
2934
let server_config = ServerConfig::builder().with_no_client_auth();
3035

3136
let cert_file = &mut BufReader::new(File::open(cert)?);
3237
let key_file = &mut BufReader::new(File::open(key)?);
33-
let certs = rustls_pemfile::certs(cert_file).collect::<Result<Vec<_>, _>>()?;
38+
39+
let mut certs = rustls_pemfile::certs(cert_file).collect::<Result<Vec<_>, _>>()?;
40+
// Load CA certificates from the directory
41+
if let Some(other_cert_dir) = other_certs {
42+
if other_cert_dir.is_dir() {
43+
for entry in fs::read_dir(other_cert_dir)? {
44+
let path = entry.unwrap().path();
45+
46+
if path.is_file() {
47+
let other_cert_file = &mut BufReader::new(File::open(&path)?);
48+
let mut other_certs = rustls_pemfile::certs(other_cert_file)
49+
.collect::<Result<Vec<_>, _>>()?;
50+
certs.append(&mut other_certs);
51+
}
52+
}
53+
}
54+
}
3455
let private_key = rustls_pemfile::private_key(key_file)?
3556
.ok_or(anyhow::anyhow!("Could not parse private key."))?;
3657

0 commit comments

Comments
 (0)