-
Notifications
You must be signed in to change notification settings - Fork 16
[SVLS-7945] feat: Support TLS certificate for logs/proxy flusher #979
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 { | ||
|
|
@@ -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 { | ||
| 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())?; | ||
|
|
@@ -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 | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can a file contain multiple certificates? This confuses me
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A pem file can contain multiple certificates: 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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!
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
| } | ||
|
|
||
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?