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
use trust_dns_resolver::{
error::{ResolveError, ResolveErrorKind},
Resolver,
};
use crate::{
error::{ErrorKind, Result},
options::StreamAddress,
};
pub(crate) struct SrvResolver {
resolver: Resolver,
}
#[derive(Debug)]
pub(crate) struct ResolvedConfig {
pub(crate) hosts: Vec<StreamAddress>,
pub(crate) auth_source: Option<String>,
pub(crate) replica_set: Option<String>,
}
impl SrvResolver {
pub(crate) fn new() -> Result<Self> {
let resolver = Resolver::new(Default::default(), Default::default())?;
Ok(Self { resolver })
}
pub(crate) fn resolve_client_options(&self, hostname: &str) -> Result<ResolvedConfig> {
let hosts = self.get_srv_hosts(hostname)?;
let mut config = ResolvedConfig {
hosts,
auth_source: None,
replica_set: None,
};
self.get_txt_options(&hostname, &mut config)?;
Ok(config)
}
fn get_srv_hosts(&self, original_hostname: &str) -> Result<Vec<StreamAddress>> {
let hostname_parts: Vec<_> = original_hostname.split('.').collect();
if hostname_parts.len() < 3 {
return Err(ErrorKind::ArgumentError {
message: "a 'mongodb+srv' hostname must have at least three '.'-delimited parts"
.into(),
}
.into());
}
let domain_name = &hostname_parts[1..];
let lookup_hostname = format!("_mongodb._tcp.{}", original_hostname);
let mut srv_addresses: Vec<_> = self
.resolver
.srv_lookup(&lookup_hostname)?
.iter()
.map(|record| {
let hostname = record.target().to_utf8();
let port = Some(record.port());
StreamAddress { hostname, port }
})
.collect();
if srv_addresses.is_empty() {
return Err(ErrorKind::SrvLookupError {
message: format!("SRV lookup for {} returned no records", original_hostname),
}
.into());
}
for address in srv_addresses.iter_mut() {
let mut hostname_parts: Vec<_> = address.hostname.split('.').collect();
if hostname_parts.last().map(|s| s.is_empty()).unwrap_or(false) {
hostname_parts.pop();
}
if !&hostname_parts[1..].ends_with(domain_name) {
return Err(ErrorKind::SrvLookupError {
message: format!(
"SRV lookup for {} returned result {}, which does not match domain name {}",
original_hostname,
address,
domain_name.join(".")
),
}
.into());
}
address.hostname = hostname_parts.join(".");
}
Ok(srv_addresses)
}
fn get_txt_options(&self, original_hostname: &str, config: &mut ResolvedConfig) -> Result<()> {
let txt_records_response = match self.resolver.txt_lookup(original_hostname) {
Ok(response) => response,
Err(e) => return ignore_no_records(e),
};
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::TxtLookupError {
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::TxtLookupError {
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());
}
other => {
return Err(ErrorKind::TxtLookupError {
message: format!(
"TXT record option '{}' was returned, but only 'authSource' and \
'replicaSet' are allowed",
other
),
}
.into())
}
};
}
Ok(())
}
}
fn ignore_no_records(error: ResolveError) -> Result<()> {
match error.kind() {
ResolveErrorKind::NoRecordsFound { .. } => Ok(()),
_ => Err(error.into()),
}
}