Skip to content

Commit 3a5319f

Browse files
committed
tls support improvements, added trust to hostname and ca certificate
1 parent 1c3a803 commit 3a5319f

File tree

3 files changed

+62
-36
lines changed

3 files changed

+62
-36
lines changed

src/client/mod.rs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -406,10 +406,20 @@ impl Client {
406406
let stream = if broker.tls.enabled() {
407407
let stream = TcpStream::connect((broker.host.as_str(), broker.port)).await?;
408408

409-
let mut tls_builder = tokio_native_tls::native_tls::TlsConnector::builder();
410-
tls_builder
411-
.danger_accept_invalid_certs(true)
412-
.danger_accept_invalid_hostnames(true);
409+
let mut tls_builder: tokio_native_tls::native_tls::TlsConnectorBuilder = tokio_native_tls::native_tls::TlsConnector::builder();
410+
411+
412+
if broker.tls.trust_hostname_enabled() {
413+
tls_builder.danger_accept_invalid_hostnames(true);
414+
}
415+
if broker.tls.trust_certificate_enabled() {
416+
tls_builder.danger_accept_invalid_certs(true);
417+
} else {
418+
if let Some(cert)=broker.tls.get_root_certificate() {
419+
print!("Hello, World!");
420+
tls_builder.add_root_certificate(cert.clone());
421+
}
422+
}
413423

414424
let conn = tokio_native_tls::TlsConnector::from(tls_builder.build()?);
415425

src/client/options.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,17 @@ impl Default for ClientOptions {
4242
collector: Arc::new(NopMetricsCollector {}),
4343
tls: TlsConfiguration {
4444
enabled: false,
45-
hostname_verification: false,
46-
trust_everything: false,
45+
trust_hostname: false,
46+
trust_certificate: false,
47+
certificate: None,
4748
},
4849
}
4950
}
5051
}
5152

5253
impl ClientOptions {
5354
pub fn get_tls(&self) -> TlsConfiguration {
54-
self.tls
55+
self.tls.clone()
5556
}
5657

5758
pub fn enable_tls(&mut self) {

src/environment.rs

Lines changed: 44 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -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)]
1821
pub 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)]
146141
pub 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

152148
impl 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

162159
impl 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

188195
pub struct TlsConfigurationBuilder(TlsConfiguration);
189196

190197
impl 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

Comments
 (0)