fix: add TLS connection settings
This commit is contained in:
1 parent
94487384e1
commit
f9b58993b4
11 files changed
+324
-87
No files matched your search
@@ -494,26 +494,7 @@ impl ConnectionConfig {
|
||||
};
|
||||
}
|
||||
match self.db_type {
|
||||
DatabaseType::Mysql => {
|
||||
let base = "ssl-mode=preferred&charset=utf8mb4";
|
||||
if value.is_empty() {
|
||||
base.to_string()
|
||||
} else if value.contains("ssl-mode=") {
|
||||
let v = value.trim_start_matches('?');
|
||||
if v.contains("charset=") {
|
||||
v.to_string()
|
||||
} else {
|
||||
format!("{v}&charset=utf8mb4")
|
||||
}
|
||||
} else {
|
||||
let v = value.trim_start_matches('?');
|
||||
if v.contains("charset=") {
|
||||
format!("ssl-mode=preferred&{v}")
|
||||
} else {
|
||||
format!("{base}&{v}")
|
||||
}
|
||||
}
|
||||
}
|
||||
DatabaseType::Mysql => normalize_mysql_url_params(value, self.ssl, self.ca_cert_path.trim().is_empty()),
|
||||
DatabaseType::Doris | DatabaseType::StarRocks => {
|
||||
let v = value.trim_start_matches('?');
|
||||
let filtered: Vec<&str> = v
|
||||
@@ -526,7 +507,7 @@ impl ConnectionConfig {
|
||||
format!("ssl-mode=disabled&{}", filtered.join("&"))
|
||||
}
|
||||
}
|
||||
DatabaseType::Postgres | DatabaseType::Redshift => normalize_postgres_url_params(value),
|
||||
DatabaseType::Postgres | DatabaseType::Redshift => normalize_postgres_url_params(value, self.ssl),
|
||||
DatabaseType::MongoDb => value.trim_start_matches('?').to_string(),
|
||||
_ => value.trim_start_matches('?').to_string(),
|
||||
}
|
||||
@@ -543,12 +524,37 @@ fn url_params_contains_flag(params: Option<&str>, key: &str, expected: &str) ->
|
||||
)
|
||||
}
|
||||
|
||||
fn normalize_postgres_url_params(value: &str) -> String {
|
||||
fn normalize_mysql_url_params(value: &str, force_tls: bool, accept_invalid_certs: bool) -> String {
|
||||
let value = value.trim_start_matches('?');
|
||||
if value.is_empty() {
|
||||
return String::new();
|
||||
let mut parts: Vec<String> = value.split('&').filter(|part| !part.is_empty()).map(str::to_string).collect();
|
||||
|
||||
if force_tls {
|
||||
parts.retain(|part| !url_param_key_is(part, "ssl-mode") && !url_param_key_is(part, "sslmode"));
|
||||
if !parts.iter().any(|part| url_param_key_is(part, "require_ssl")) {
|
||||
parts.insert(0, "require_ssl=true".to_string());
|
||||
}
|
||||
if accept_invalid_certs && !parts.iter().any(|part| url_param_key_is(part, "verify_ca")) {
|
||||
parts.push("verify_ca=false".to_string());
|
||||
}
|
||||
if !parts.iter().any(|part| url_param_key_is(part, "verify_identity")) {
|
||||
parts.push("verify_identity=false".to_string());
|
||||
}
|
||||
} else if !parts.iter().any(|part| {
|
||||
url_param_key_is(part, "ssl-mode") || url_param_key_is(part, "sslmode") || url_param_key_is(part, "require_ssl")
|
||||
}) {
|
||||
parts.insert(0, "ssl-mode=preferred".to_string());
|
||||
}
|
||||
|
||||
if !parts.iter().any(|part| url_param_key_is(part, "charset")) {
|
||||
parts.push("charset=utf8mb4".to_string());
|
||||
}
|
||||
|
||||
parts.join("&")
|
||||
}
|
||||
|
||||
fn normalize_postgres_url_params(value: &str, force_tls: bool) -> String {
|
||||
let value = value.trim_start_matches('?');
|
||||
|
||||
let mut timezone: Option<String> = None;
|
||||
let mut parts: Vec<String> = Vec::new();
|
||||
|
||||
@@ -566,6 +572,9 @@ fn normalize_postgres_url_params(value: &str) -> String {
|
||||
}
|
||||
|
||||
let Some(timezone) = timezone else {
|
||||
if force_tls && !parts.iter().any(|part| url_param_key_is(part, "sslmode")) {
|
||||
parts.insert(0, "sslmode=require".to_string());
|
||||
}
|
||||
return parts.join("&");
|
||||
};
|
||||
|
||||
@@ -585,9 +594,18 @@ fn normalize_postgres_url_params(value: &str) -> String {
|
||||
parts.push(format!("options={}", encode_url_part(&timezone_option)));
|
||||
}
|
||||
|
||||
if force_tls && !parts.iter().any(|part| url_param_key_is(part, "sslmode")) {
|
||||
parts.insert(0, "sslmode=require".to_string());
|
||||
}
|
||||
|
||||
parts.join("&")
|
||||
}
|
||||
|
||||
fn url_param_key_is(part: &str, expected: &str) -> bool {
|
||||
let key = part.split_once('=').map(|(key, _)| key).unwrap_or(part);
|
||||
percent_decode_str(key).decode_utf8_lossy().eq_ignore_ascii_case(expected)
|
||||
}
|
||||
|
||||
fn clickhouse_http_url(config: &ConnectionConfig, host: &str, port: u16) -> String {
|
||||
let trimmed = host.trim();
|
||||
if let Some(rest) = trimmed.strip_prefix("https://") {
|
||||
@@ -985,6 +1003,29 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mysql_tls_switch_requires_ssl_without_strict_certificate_checks_by_default() {
|
||||
let mut config = mysql_config("root", "secret", Some("test"));
|
||||
config.ssl = true;
|
||||
|
||||
assert_eq!(
|
||||
config.connection_url(),
|
||||
"mysql://root:secret@10.1.2.3:2883/test?require_ssl=true&verify_ca=false&verify_identity=false&charset=utf8mb4"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mysql_tls_switch_uses_ca_cert_for_ca_validation() {
|
||||
let mut config = mysql_config("root", "secret", Some("test"));
|
||||
config.ssl = true;
|
||||
config.ca_cert_path = "/tmp/tidb-ca.pem".to_string();
|
||||
|
||||
assert_eq!(
|
||||
config.connection_url(),
|
||||
"mysql://root:secret@10.1.2.3:2883/test?require_ssl=true&verify_identity=false&charset=utf8mb4"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn postgres_url_appends_custom_params() {
|
||||
let mut config = mysql_config("postgres", "secret", Some("test"));
|
||||
@@ -994,6 +1035,15 @@ mod tests {
|
||||
assert_eq!(config.connection_url(), "postgres://postgres:secret@10.1.2.3:2883/test?sslmode=disable");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn postgres_tls_switch_adds_require_sslmode() {
|
||||
let mut config = mysql_config("postgres", "secret", Some("test"));
|
||||
config.db_type = DatabaseType::Postgres;
|
||||
config.ssl = true;
|
||||
|
||||
assert_eq!(config.connection_url(), "postgres://postgres:secret@10.1.2.3:2883/test?sslmode=require");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn postgres_url_normalizes_timezone_param_into_options() {
|
||||
let mut config = mysql_config("postgres", "secret", Some("test"));
|
||||
|
||||
Reference in new issue
Block a user