feat(mongodb): add TLS connection options in connection tab (#2515)
Add MongoDB TLS options (`tlsAllowInvalidCertificates`, `retryWrites`, `tlsCAFile`) to the connection form with backend URL normalization and DocumentDB/SSH tunnel guidance, while preserving legacy `tlsCAFile` in `url_params` for existing DocumentDB users, hardening auth failure hint matching, and clarifying TLS bypass security labels.
This commit is contained in:
1 parent
f826cb5d38
commit
b60f8b2039
11 files changed
+270
-6
No files matched your search
@@ -1110,7 +1110,9 @@ impl ConnectionConfig {
|
||||
}
|
||||
DatabaseType::Databend => normalize_bare_mysql_url_params(value),
|
||||
DatabaseType::Postgres | DatabaseType::Redshift => normalize_postgres_url_params(value, self.ssl),
|
||||
DatabaseType::MongoDb => normalize_mongo_url_params(value, self.ssl, !self.username.trim().is_empty()),
|
||||
DatabaseType::MongoDb => {
|
||||
normalize_mongo_url_params(value, self.ssl, !self.username.trim().is_empty(), self.ca_cert_path.trim())
|
||||
}
|
||||
_ => value.trim_start_matches('?').to_string(),
|
||||
}
|
||||
}
|
||||
@@ -1270,7 +1272,7 @@ fn normalize_mysql_url_params(value: &str, force_tls: bool, accept_invalid_certs
|
||||
parts.join("&")
|
||||
}
|
||||
|
||||
fn normalize_mongo_url_params(value: &str, force_tls: bool, default_auth_source: bool) -> String {
|
||||
fn normalize_mongo_url_params(value: &str, force_tls: bool, default_auth_source: bool, ca_cert_path: &str) -> String {
|
||||
let value = value.trim_start_matches('?');
|
||||
let mut parts: Vec<String> = value.split('&').filter(|part| !part.is_empty()).map(str::to_string).collect();
|
||||
|
||||
@@ -1279,6 +1281,29 @@ fn normalize_mongo_url_params(value: &str, force_tls: bool, default_auth_source:
|
||||
parts.insert(0, "tls=true".to_string());
|
||||
}
|
||||
|
||||
normalize_mongo_tls_compat_params(&mut parts);
|
||||
|
||||
if !force_tls {
|
||||
parts.retain(|part| {
|
||||
!url_param_key_is(part, "tlsAllowInvalidCertificates") && !url_param_key_is(part, "tlsCAFile")
|
||||
});
|
||||
}
|
||||
|
||||
let existing_tls_ca_file = if ca_cert_path.is_empty() {
|
||||
parts.iter().find(|part| url_param_key_is(part, "tlsCAFile")).cloned()
|
||||
} else {
|
||||
None
|
||||
};
|
||||
parts.retain(|part| !url_param_key_is(part, "tlsCAFile"));
|
||||
|
||||
if force_tls {
|
||||
if !ca_cert_path.is_empty() {
|
||||
parts.push(format!("tlsCAFile={}", encode_mongo_tls_file_path(ca_cert_path)));
|
||||
} else if let Some(part) = existing_tls_ca_file {
|
||||
parts.push(part);
|
||||
}
|
||||
}
|
||||
|
||||
if default_auth_source && !parts.iter().any(|part| url_param_key_is(part, "authSource")) {
|
||||
parts.push("authSource=admin".to_string());
|
||||
}
|
||||
@@ -1286,6 +1311,29 @@ fn normalize_mongo_url_params(value: &str, force_tls: bool, default_auth_source:
|
||||
parts.join("&")
|
||||
}
|
||||
|
||||
/// The Rust MongoDB driver uses rustls by default, which does not accept
|
||||
/// `tlsAllowInvalidHostnames` in the connection string. Map it to the
|
||||
/// supported `tlsAllowInvalidCertificates` option instead.
|
||||
fn normalize_mongo_tls_compat_params(parts: &mut Vec<String>) {
|
||||
let allow_invalid_hostnames =
|
||||
parts.iter().any(|part| url_param_key_is(part, "tlsAllowInvalidHostnames") && mongo_url_param_is_truthy(part));
|
||||
parts.retain(|part| !url_param_key_is(part, "tlsAllowInvalidHostnames"));
|
||||
if allow_invalid_hostnames && !parts.iter().any(|part| url_param_key_is(part, "tlsAllowInvalidCertificates")) {
|
||||
parts.push("tlsAllowInvalidCertificates=true".to_string());
|
||||
}
|
||||
}
|
||||
|
||||
fn mongo_url_param_is_truthy(part: &str) -> bool {
|
||||
let Some((_, value)) = part.split_once('=') else {
|
||||
return true;
|
||||
};
|
||||
matches!(percent_decode_str(value).decode_utf8_lossy().trim().to_ascii_lowercase().as_str(), "true" | "1" | "yes")
|
||||
}
|
||||
|
||||
fn encode_mongo_tls_file_path(path: &str) -> String {
|
||||
utf8_percent_encode(path, NON_ALPHANUMERIC).to_string()
|
||||
}
|
||||
|
||||
fn mongo_uri_db_part_for_suffix<'a>(db_part: &'a str, suffix: &str) -> &'a str {
|
||||
if db_part.is_empty() && !suffix.is_empty() {
|
||||
"/"
|
||||
@@ -2495,6 +2543,56 @@ mod tests {
|
||||
assert_eq!(config.connection_url(), "mongodb://root:secret@10.1.2.3:17000/admin?tls=true&authSource=admin");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mongodb_form_tls_ca_cert_adds_tls_ca_file_param() {
|
||||
let mut config = mongodb_config("root", "secret", Some("admin"));
|
||||
config.ssl = true;
|
||||
config.ca_cert_path = "/tmp/mongo-ca.pem".to_string();
|
||||
|
||||
assert_eq!(
|
||||
config.connection_url(),
|
||||
"mongodb://root:secret@10.1.2.3:17000/admin?tls=true&tlsCAFile=%2Ftmp%2Fmongo%2Dca%2Epem&authSource=admin"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mongodb_form_tls_cert_params_replace_existing_url_values() {
|
||||
let mut config = mongodb_config("root", "secret", Some("admin"));
|
||||
config.ssl = true;
|
||||
config.ca_cert_path = "/tmp/new-ca.pem".to_string();
|
||||
config.url_params = Some("tlsCAFile=%2Fold-ca.pem&authSource=admin".to_string());
|
||||
|
||||
assert_eq!(
|
||||
config.connection_url(),
|
||||
"mongodb://root:secret@10.1.2.3:17000/admin?tls=true&authSource=admin&tlsCAFile=%2Ftmp%2Fnew%2Dca%2Epem"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mongodb_form_tls_preserves_legacy_tls_ca_file_in_url_params() {
|
||||
let mut config = mongodb_config("root", "secret", Some("admin"));
|
||||
config.ssl = true;
|
||||
config.url_params = Some("tlsCAFile=%2Ftmp%2Flegacy-ca.pem&authSource=admin".to_string());
|
||||
|
||||
assert_eq!(
|
||||
config.connection_url(),
|
||||
"mongodb://root:secret@10.1.2.3:17000/admin?tls=true&authSource=admin&tlsCAFile=%2Ftmp%2Flegacy-ca.pem"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mongodb_form_tls_allow_invalid_hostnames_maps_to_allow_invalid_certificates() {
|
||||
let mut config = mongodb_config("root", "secret", Some("admin"));
|
||||
config.ssl = true;
|
||||
config.url_params = Some("replicaSet=rs0&tlsAllowInvalidHostnames=true".to_string());
|
||||
|
||||
assert_eq!(
|
||||
config.connection_url(),
|
||||
"mongodb://root:secret@10.1.2.3:17000/admin?tls=true&replicaSet=rs0&tlsAllowInvalidCertificates=true&authSource=admin"
|
||||
);
|
||||
assert!(!config.connection_url().contains("tlsAllowInvalidHostnames"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_mongo_first_host_replica_set() {
|
||||
let uri = "mongodb://user:pass@host1:27017,host2:27017,host3:27017/admin?replicaSet=rs0";
|
||||
|
||||
Reference in new issue
Block a user