@@ -13,8 +13,11 @@ use crate::{
1313 stream_creator:: StreamCreator ,
1414 RabbitMQStreamResult ,
1515} ;
16+
17+ use tokio_native_tls:: native_tls:: Certificate ;
18+
1619/// Main access point to a node
17- #[ derive( Clone ) ]
20+ #[ derive( Clone ) ]
1821pub struct Environment {
1922 pub ( crate ) options : EnvironmentOptions ,
2023}
@@ -108,18 +111,10 @@ impl EnvironmentBuilder {
108111 }
109112
110113 pub fn tls ( mut self , tls_configuration : TlsConfiguration ) -> EnvironmentBuilder {
114+
111115 self . 0
112116 . client_options
113- . tls
114- . trust_everything ( tls_configuration. trust_everything_enabled ( ) ) ;
115- self . 0
116- . client_options
117- . tls
118- . hostname_verification_enable ( tls_configuration. hostname_verification_enabled ( ) ) ;
119- self . 0
120- . client_options
121- . tls
122- . enable ( tls_configuration. enabled ( ) ) ;
117+ . tls = tls_configuration;
123118
124119 self
125120 }
@@ -142,27 +137,26 @@ pub struct EnvironmentOptions {
142137}
143138
144139/** Helper for tls configuration */
145- #[ derive( Clone , Copy ) ]
140+ #[ derive( Clone ) ]
146141pub struct TlsConfiguration {
147142 pub ( crate ) enabled : bool ,
148- pub ( crate ) hostname_verification : bool ,
149- pub ( crate ) trust_everything : bool ,
143+ pub ( crate ) trust_hostname : bool ,
144+ pub ( crate ) trust_certificate : bool ,
145+ pub ( crate ) certificate : Option < Certificate > ,
150146}
151147
152148impl Default for TlsConfiguration {
153149 fn default ( ) -> TlsConfiguration {
154150 TlsConfiguration {
155151 enabled : true ,
156- trust_everything : false ,
157- hostname_verification : true ,
152+ trust_certificate : false ,
153+ trust_hostname : false ,
154+ certificate : None ,
158155 }
159156 }
160157}
161158
162159impl TlsConfiguration {
163- pub fn trust_everything ( & mut self , trust_everything : bool ) {
164- self . trust_everything = trust_everything
165- }
166160
167161 pub fn enable ( & mut self , enabled : bool ) {
168162 self . enabled = enabled
@@ -172,24 +166,37 @@ impl TlsConfiguration {
172166 self . enabled
173167 }
174168
175- pub fn hostname_verification_enable ( & mut self , hostname_verification : bool ) {
176- self . hostname_verification = hostname_verification
169+ pub fn get_root_certificate ( & self ) -> Option < & Certificate > {
170+ self . certificate . as_ref ( )
171+ }
172+
173+ pub fn add_root_certificate ( & mut self , certificate : Certificate ) {
174+ self . certificate = Some ( certificate)
175+ }
176+
177+ pub fn trust_hostname ( & mut self , trust_hostname : bool ) {
178+ self . trust_hostname = trust_hostname
177179 }
178180
179- pub fn hostname_verification_enabled ( & self ) -> bool {
180- self . hostname_verification
181+ pub fn trust_hostname_enabled ( & self ) -> bool {
182+ self . trust_hostname
181183 }
182184
183- pub fn trust_everything_enabled ( & self ) -> bool {
184- self . trust_everything
185+ pub fn trust_certificate ( & mut self , trust_certificate : bool ) {
186+ self . trust_certificate = trust_certificate
185187 }
188+
189+ pub fn trust_certificate_enabled ( & self ) -> bool {
190+ self . trust_certificate
191+ }
192+
186193}
187194
188195pub struct TlsConfigurationBuilder ( TlsConfiguration ) ;
189196
190197impl TlsConfigurationBuilder {
191- pub fn trust_everything ( mut self , trust_everything : bool ) -> TlsConfigurationBuilder {
192- self . 0 . trust_everything = trust_everything ;
198+ pub fn trust_certificate ( mut self , trust_certificate : bool ) -> TlsConfigurationBuilder {
199+ self . 0 . trust_certificate = trust_certificate ;
193200 self
194201 }
195202
@@ -198,11 +205,19 @@ impl TlsConfigurationBuilder {
198205 self
199206 }
200207
201- pub fn hostname_verification_enable (
208+ pub fn trust_hostname (
202209 mut self ,
203210 hostname_verification : bool ,
204211 ) -> TlsConfigurationBuilder {
205- self . 0 . hostname_verification = hostname_verification;
212+ self . 0 . trust_hostname = hostname_verification;
213+ self
214+ }
215+
216+ pub fn add_root_certificate (
217+ mut self ,
218+ certificate : Certificate ,
219+ ) -> TlsConfigurationBuilder {
220+ self . 0 . certificate = Some ( certificate) ;
206221 self
207222 }
208223
0 commit comments