@@ -8,8 +8,8 @@ use bytes::Bytes;
88use core:: time:: Duration ;
99use datadog_fips:: reqwest_adapter:: create_reqwest_client_builder;
1010use std:: sync:: Arc ;
11- use std:: { collections:: HashMap , error:: Error } ;
12- use tracing:: error;
11+ use std:: { collections:: HashMap , error:: Error , fs :: File , io :: BufReader } ;
12+ use tracing:: { debug , error} ;
1313
1414#[ must_use]
1515pub 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
4747 . http2_keep_alive_timeout ( Duration :: from_secs ( 1000 ) ) ;
4848 }
4949
50+ // Load custom TLS certificate if configured
51+ if let Some ( cert_path) = & config. tls_cert_file {
52+ match load_custom_cert ( cert_path) {
53+ Ok ( certs) => {
54+ let cert_count = certs. len ( ) ;
55+ for cert in certs {
56+ client = client. add_root_certificate ( cert) ;
57+ }
58+ debug ! (
59+ "HTTP | Added {} root certificate(s) from {}" ,
60+ cert_count, cert_path
61+ ) ;
62+ }
63+ Err ( e) => {
64+ error ! (
65+ "Failed to load TLS certificate from {}: {}, continuing without custom cert" ,
66+ cert_path, e
67+ ) ;
68+ }
69+ }
70+ }
71+
5072 // This covers DD_PROXY_HTTPS and HTTPS_PROXY
5173 if let Some ( https_uri) = & config. proxy_https {
5274 let proxy = reqwest:: Proxy :: https ( https_uri. clone ( ) ) ?;
@@ -56,6 +78,26 @@ fn build_client(config: &Arc<config::Config>) -> Result<reqwest::Client, Box<dyn
5678 }
5779}
5880
81+ fn load_custom_cert ( cert_path : & str ) -> Result < Vec < reqwest:: Certificate > , Box < dyn Error > > {
82+ let file = File :: open ( cert_path) ?;
83+ let mut reader = BufReader :: new ( file) ;
84+
85+ // Parse PEM certificates
86+ let certs = rustls_pemfile:: certs ( & mut reader) . collect :: < Result < Vec < _ > , _ > > ( ) ?;
87+
88+ if certs. is_empty ( ) {
89+ return Err ( "No certificates found in file" . into ( ) ) ;
90+ }
91+
92+ // Convert all certificates found in the file
93+ let mut reqwest_certs = Vec :: new ( ) ;
94+ for cert in certs {
95+ reqwest_certs. push ( reqwest:: Certificate :: from_der ( & cert) ?) ;
96+ }
97+
98+ Ok ( reqwest_certs)
99+ }
100+
59101pub async fn handler_not_found ( ) -> Response {
60102 ( StatusCode :: NOT_FOUND , "Not Found" ) . into_response ( )
61103}
0 commit comments