Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 42 additions & 2 deletions bottlecap/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ use bytes::Bytes;
use core::time::Duration;
use datadog_fips::reqwest_adapter::create_reqwest_client_builder;
use std::sync::Arc;
use std::{collections::HashMap, error::Error};
use tracing::error;
use std::{collections::HashMap, error::Error, fs::File, io::BufReader};
use tracing::{debug, error};

#[must_use]
pub fn get_client(config: &Arc<config::Config>) -> reqwest::Client {
Expand Down Expand Up @@ -47,6 +47,28 @@ fn build_client(config: &Arc<config::Config>) -> Result<reqwest::Client, Box<dyn
.http2_keep_alive_timeout(Duration::from_secs(1000));
}

// Load custom TLS certificate if configured
if let Some(cert_path) = &config.tls_cert_file {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

file and path are confusing, can a customer load more than 1 certificate or define a folder?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nvm, ignore this, a path can be to a file, but again, my question persist, can a customer load more than 1 cert?

match load_custom_cert(cert_path) {
Ok(certs) => {
let cert_count = certs.len();
for cert in certs {
client = client.add_root_certificate(cert);
}
debug!(
"HTTP | Added {} root certificate(s) from {}",
cert_count, cert_path
);
}
Err(e) => {
error!(
"Failed to load TLS certificate from {}: {}, continuing without custom cert",
cert_path, e
);
}
}
}

// This covers DD_PROXY_HTTPS and HTTPS_PROXY
if let Some(https_uri) = &config.proxy_https {
let proxy = reqwest::Proxy::https(https_uri.clone())?;
Expand All @@ -56,6 +78,24 @@ fn build_client(config: &Arc<config::Config>) -> Result<reqwest::Client, Box<dyn
}
}

fn load_custom_cert(cert_path: &str) -> Result<Vec<reqwest::Certificate>, Box<dyn Error>> {
let file = File::open(cert_path)?;
let mut reader = BufReader::new(file);

// Parse PEM certificates
let certs = rustls_pemfile::certs(&mut reader).collect::<Result<Vec<_>, _>>()?;

if certs.is_empty() {
return Err("No certificates found in file".into());
}

// Convert all certificates found in the file
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can a file contain multiple certificates? This confuses me

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A pem file can contain multiple certificates:
https://stackoverflow.com/questions/68340665/pem-file-has-two-certificates-what-does-it-mean

And we supported multiple certs for trace and stats flusher: https://github.com/DataDog/datadog-lambda-extension/blob/main/bottlecap/src/traces/trace_flusher.rs#L247-L250

Not sure if there's a real use case, but since it's not hard, I'm supporting it in this PR.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi! Appreciate the fix, our customer reached out in the support ticket that they do have many CA bundled in one .crt so the use case is valid. They also share that the PR has fixed the issue for them so really appreciate it, thanks a lot!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yues7 Nice!

certs
.into_iter()
.map(|cert| reqwest::Certificate::from_der(&cert).map_err(Into::into))
.collect()
}

pub async fn handler_not_found() -> Response {
(StatusCode::NOT_FOUND, "Not Found").into_response()
}
Expand Down
Loading