-
Notifications
You must be signed in to change notification settings - Fork 176
/
Copy pathsrv.rs
292 lines (261 loc) · 9.34 KB
/
srv.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
use std::time::Duration;
#[cfg(feature = "dns-resolver")]
use crate::error::ErrorKind;
use crate::{client::options::ResolverConfig, error::Result, options::ServerAddress};
#[derive(Debug)]
pub(crate) struct ResolvedConfig {
pub(crate) hosts: Vec<ServerAddress>,
pub(crate) min_ttl: Duration,
pub(crate) auth_source: Option<String>,
pub(crate) replica_set: Option<String>,
pub(crate) load_balanced: Option<bool>,
}
#[derive(Debug, Clone)]
pub(crate) struct LookupHosts {
pub(crate) hosts: Vec<ServerAddress>,
pub(crate) min_ttl: Duration,
}
impl LookupHosts {
#[cfg(feature = "dns-resolver")]
pub(crate) fn validate(mut self, original_hostname: &str, dm: DomainMismatch) -> Result<Self> {
let original_hostname_parts: Vec<_> = original_hostname.split('.').collect();
let original_domain_name = if original_hostname_parts.len() >= 3 {
&original_hostname_parts[1..]
} else {
&original_hostname_parts[..]
};
let mut ok_hosts = vec![];
for addr in self.hosts.drain(..) {
let host = addr.host();
let hostname_parts: Vec<_> = host.split('.').collect();
if hostname_parts[1..].ends_with(original_domain_name) {
ok_hosts.push(addr);
} else {
let message = format!(
"SRV lookup for {} returned result {}, which does not match domain name {}",
original_hostname,
host,
original_domain_name.join(".")
);
match dm {
DomainMismatch::Error => return Err(ErrorKind::DnsResolve { message }.into()),
DomainMismatch::Skip => {
#[cfg(feature = "tracing-unstable")]
{
use crate::trace::SERVER_SELECTION_TRACING_EVENT_TARGET;
if crate::trace::trace_or_log_enabled!(
target: SERVER_SELECTION_TRACING_EVENT_TARGET,
crate::trace::TracingOrLogLevel::Warn
) {
tracing::warn!(
target: SERVER_SELECTION_TRACING_EVENT_TARGET,
message,
);
}
}
continue;
}
}
}
}
self.hosts = ok_hosts;
if self.hosts.is_empty() {
return Err(ErrorKind::DnsResolve {
message: format!("SRV lookup for {} returned no records", original_hostname),
}
.into());
}
Ok(self)
}
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct OriginalSrvInfo {
pub(crate) hostname: String,
pub(crate) min_ttl: Duration,
}
pub(crate) enum DomainMismatch {
#[allow(dead_code)]
Error,
Skip,
}
#[cfg(feature = "dns-resolver")]
pub(crate) struct SrvResolver {
resolver: crate::runtime::AsyncResolver,
srv_service_name: Option<String>,
}
#[cfg(feature = "dns-resolver")]
impl SrvResolver {
pub(crate) async fn new(
config: Option<ResolverConfig>,
srv_service_name: Option<String>,
) -> Result<Self> {
let resolver = crate::runtime::AsyncResolver::new(config.map(|c| c.inner)).await?;
Ok(Self {
resolver,
srv_service_name,
})
}
pub(crate) async fn resolve_client_options(
&mut self,
hostname: &str,
) -> Result<ResolvedConfig> {
let lookup_result = self.get_srv_hosts(hostname, DomainMismatch::Error).await?;
let mut config = ResolvedConfig {
hosts: lookup_result.hosts,
min_ttl: lookup_result.min_ttl,
auth_source: None,
replica_set: None,
load_balanced: None,
};
self.get_txt_options(hostname, &mut config).await?;
Ok(config)
}
async fn get_srv_hosts_unvalidated(&self, lookup_hostname: &str) -> Result<LookupHosts> {
use hickory_proto::rr::RData;
let srv_lookup = self.resolver.srv_lookup(lookup_hostname).await?;
let mut hosts = vec![];
let mut min_ttl = u32::MAX;
for record in srv_lookup.as_lookup().record_iter() {
let srv = match record.data() {
Some(RData::SRV(s)) => s,
_ => continue,
};
let mut host = srv.target().to_utf8();
// Remove the trailing '.'
if host.ends_with('.') {
host.pop();
}
let port = Some(srv.port());
hosts.push(ServerAddress::Tcp { host, port });
min_ttl = std::cmp::min(min_ttl, record.ttl());
}
Ok(LookupHosts {
hosts,
min_ttl: Duration::from_secs(min_ttl.into()),
})
}
pub(crate) async fn get_srv_hosts(
&self,
original_hostname: &str,
dm: DomainMismatch,
) -> Result<LookupHosts> {
let lookup_hostname = format!(
"_{}._tcp.{}",
self.srv_service_name.as_deref().unwrap_or("mongodb"),
original_hostname
);
self.get_srv_hosts_unvalidated(&lookup_hostname)
.await?
.validate(original_hostname, dm)
}
async fn get_txt_options(
&self,
original_hostname: &str,
config: &mut ResolvedConfig,
) -> Result<()> {
let txt_records_response = match self.resolver.txt_lookup(original_hostname).await? {
Some(response) => response,
None => return Ok(()),
};
let mut txt_records = txt_records_response.iter();
let txt_record = match txt_records.next() {
Some(record) => record,
None => return Ok(()),
};
if txt_records.next().is_some() {
return Err(ErrorKind::DnsResolve {
message: format!(
"TXT lookup for {} returned more than one record, but more than one are not \
allowed with 'mongodb+srv'",
original_hostname,
),
}
.into());
}
let txt_data: Vec<_> = txt_record
.txt_data()
.iter()
.map(|bytes| String::from_utf8_lossy(bytes.as_ref()).into_owned())
.collect();
let txt_string = txt_data.join("");
for option_pair in txt_string.split('&') {
let parts: Vec<_> = option_pair.split('=').collect();
if parts.len() != 2 {
return Err(ErrorKind::DnsResolve {
message: format!(
"TXT record string '{}' is not a value `key=value` option pair",
option_pair
),
}
.into());
}
match &parts[0].to_lowercase()[..] {
"authsource" => {
config.auth_source = Some(parts[1].to_string());
}
"replicaset" => {
config.replica_set = Some(parts[1].into());
}
"loadbalanced" => {
let val = match parts[1] {
"true" => true,
"false" => false,
_ => {
return Err(ErrorKind::DnsResolve {
message: format!(
"TXT record option 'loadbalanced={}' was returned, only \
'true' and 'false' are allowed values.",
parts[1]
),
}
.into())
}
};
config.load_balanced = Some(val);
}
other => {
return Err(ErrorKind::DnsResolve {
message: format!(
"TXT record option '{}' was returned, but only 'authSource', \
'replicaSet', and 'loadBalanced' are allowed",
other
),
}
.into())
}
};
}
Ok(())
}
}
/// Stub implementation when dns resolution isn't enabled.
#[cfg(not(feature = "dns-resolver"))]
pub(crate) struct SrvResolver {}
#[cfg(not(feature = "dns-resolver"))]
impl SrvResolver {
pub(crate) async fn new(
_config: Option<ResolverConfig>,
_srv_service_name: Option<String>,
) -> Result<Self> {
Ok(Self {})
}
pub(crate) async fn resolve_client_options(
&mut self,
_hostname: &str,
) -> Result<ResolvedConfig> {
Err(crate::error::Error::invalid_argument(
"mongodb+srv connection strings cannot be used when the 'dns-resolver' feature is \
disabled",
))
}
pub(crate) async fn get_srv_hosts(
&self,
_original_hostname: &str,
_dm: DomainMismatch,
) -> Result<LookupHosts> {
return Err(crate::error::Error::invalid_argument(
"mongodb+srv connection strings cannot be used when the 'dns-resolver' feature is \
disabled",
));
}
}