@@ -81,14 +81,69 @@ impl ExportOpt {
8181 }
8282}
8383
84- #[ cfg( all( unix, not ( feature = "native_tls" ) , feature = "rust_tls" ) ) ]
85- fn get_tls_config (
86- _fluvio_config : fluvio:: config:: FluvioClusterConfig ,
87- _cert_path : Option < String > ,
88- _key_path : Option < String > ,
89- _remote_id : String ,
84+ #[ cfg( all( unix, feature = "rust_tls" , not ( feature = "native_tls" ) ) ) ]
85+ pub fn get_tls_config (
86+ fluvio_config : fluvio:: config:: FluvioClusterConfig ,
87+ cert_path : Option < String > ,
88+ key_path : Option < String > ,
89+ remote_id : String ,
9090) -> Result < Option < ClientTls > > {
91- todo ! ( "implement a pure rust version" )
91+ use fluvio:: config:: { TlsConfig , TlsPolicy } ;
92+ use fluvio_future:: rust_tls:: { load_certs} ;
93+
94+ match & fluvio_config. tls {
95+ TlsPolicy :: Verified ( config) => {
96+ let ( remote_cert, remote_key, cert_path) = match ( cert_path. clone ( ) , key_path) {
97+ ( Some ( cert) , Some ( key) ) => (
98+ std:: fs:: read_to_string ( cert. clone ( ) ) ?,
99+ std:: fs:: read_to_string ( key) ?,
100+ cert,
101+ ) ,
102+ _ => {
103+ return Err ( anyhow ! (
104+ "remote cert and key are required for a cluster using TLS"
105+ ) ) ;
106+ }
107+ } ;
108+
109+ let cert_chain =
110+ load_certs ( cert_path) . map_err ( |err| anyhow ! ( "error building cert: {}" , err) ) ?;
111+
112+ let leaf_cert = cert_chain
113+ . first ( )
114+ . ok_or_else ( || anyhow ! ( "No certificates found in path" ) ) ?;
115+
116+ // 2. Pass the byte slice (&[u8]) to the authenticator
117+ let principal = fluvio_auth:: x509:: X509Authenticator :: principal_from_raw_certificate (
118+ leaf_cert. as_ref ( ) ,
119+ )
120+ . expect ( "error getting principal from certificate" ) ;
121+
122+ if principal != remote_id {
123+ return Err ( anyhow ! (
124+ "remote_id: \" {}\" does not match the CN in the certificate: \" {}\" " ,
125+ remote_id,
126+ principal
127+ ) ) ;
128+ }
129+
130+ match config {
131+ TlsConfig :: Inline ( config) => Ok ( Some ( ClientTls {
132+ domain : config. domain . clone ( ) ,
133+ ca_cert : config. ca_cert . clone ( ) ,
134+ client_cert : remote_cert,
135+ client_key : remote_key,
136+ } ) ) ,
137+ TlsConfig :: Files ( file_config) => Ok ( Some ( ClientTls {
138+ domain : file_config. domain . clone ( ) ,
139+ ca_cert : std:: fs:: read_to_string ( & file_config. ca_cert ) ?,
140+ client_cert : remote_cert,
141+ client_key : remote_key,
142+ } ) ) ,
143+ }
144+ }
145+ _ => Ok ( None ) ,
146+ }
92147}
93148
94149#[ cfg( all( unix, feature = "native_tls" ) ) ]
0 commit comments