feat(connection): add StarRocks TLS support
This commit is contained in:
1 parent
eaa0d7e735
commit
23b9228f82
6 files changed
+393
-19
No files matched your search
@@ -250,7 +250,8 @@ pub async fn connect_mysql_metadata_pool(
|
||||
let idle_timeout_secs = Some(db_config.idle_timeout_secs);
|
||||
let extra_setup_queries = oceanbase_mysql_setup_queries(db_config);
|
||||
if db_config.needs_bare_mysql() {
|
||||
return match db::mysql::connect_bare_with_pool_limit_and_setup(
|
||||
return match connect_bare_mysql_pool_with_setup(
|
||||
db_config,
|
||||
&url,
|
||||
connect_timeout,
|
||||
max_connections,
|
||||
@@ -265,7 +266,8 @@ pub async fn connect_mysql_metadata_pool(
|
||||
log::info!(
|
||||
"MySQL metadata connection without a default database failed ({err}); retrying with configured default database."
|
||||
);
|
||||
db::mysql::connect_bare_with_pool_limit_and_setup(
|
||||
connect_bare_mysql_pool_with_setup(
|
||||
db_config,
|
||||
&fallback_url,
|
||||
connect_timeout,
|
||||
max_connections,
|
||||
@@ -328,7 +330,8 @@ pub async fn connect_bare_metadata_pool(
|
||||
let url = connection_url_for_endpoint(db_config, host, port);
|
||||
let extra_setup_queries = oceanbase_mysql_setup_queries(db_config);
|
||||
if db_config.effective_database().is_none() {
|
||||
return db::mysql::connect_bare_with_pool_limit_and_setup(
|
||||
return connect_bare_mysql_pool_with_setup(
|
||||
db_config,
|
||||
&url,
|
||||
connect_timeout,
|
||||
max_connections,
|
||||
@@ -341,7 +344,8 @@ pub async fn connect_bare_metadata_pool(
|
||||
unscoped_config.database = None;
|
||||
let unscoped_url = connection_url_for_endpoint(&unscoped_config, host, port);
|
||||
if unscoped_url == url {
|
||||
return db::mysql::connect_bare_with_pool_limit_and_setup(
|
||||
return connect_bare_mysql_pool_with_setup(
|
||||
db_config,
|
||||
&url,
|
||||
connect_timeout,
|
||||
max_connections,
|
||||
@@ -351,8 +355,9 @@ pub async fn connect_bare_metadata_pool(
|
||||
}
|
||||
|
||||
let preferred =
|
||||
db::mysql::connect_bare_with_pool_limit_and_setup(&url, connect_timeout, max_connections, &extra_setup_queries);
|
||||
let unscoped = db::mysql::connect_bare_with_pool_limit_and_setup(
|
||||
connect_bare_mysql_pool_with_setup(db_config, &url, connect_timeout, max_connections, &extra_setup_queries);
|
||||
let unscoped = connect_bare_mysql_pool_with_setup(
|
||||
db_config,
|
||||
&unscoped_url,
|
||||
connect_timeout,
|
||||
max_connections,
|
||||
@@ -383,6 +388,30 @@ pub async fn connect_bare_metadata_pool(
|
||||
}
|
||||
}
|
||||
|
||||
async fn connect_bare_mysql_pool_with_setup(
|
||||
db_config: &ConnectionConfig,
|
||||
url: &str,
|
||||
connect_timeout: std::time::Duration,
|
||||
max_connections: usize,
|
||||
extra_setup_queries: &[String],
|
||||
) -> Result<db::mysql::MySqlPool, String> {
|
||||
if db_config.bare_mysql_uses_tls() {
|
||||
let idle_timeout_secs = Some(db_config.idle_timeout_secs);
|
||||
db::mysql::connect_with_ca_cert_pool_limit_idle_and_setup(
|
||||
url,
|
||||
Some(&db_config.ca_cert_path),
|
||||
connect_timeout,
|
||||
max_connections,
|
||||
idle_timeout_secs,
|
||||
extra_setup_queries,
|
||||
)
|
||||
.await
|
||||
} else {
|
||||
db::mysql::connect_bare_with_pool_limit_and_setup(url, connect_timeout, max_connections, extra_setup_queries)
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
fn mysql_metadata_fallback_url(
|
||||
config: &ConnectionConfig,
|
||||
db_config: &ConnectionConfig,
|
||||
@@ -751,7 +780,14 @@ impl AppState {
|
||||
connect_bare_metadata_pool(&db_config, &host, port, connect_timeout, mysql_pool_max_connections)
|
||||
.await?
|
||||
} else {
|
||||
db::mysql::connect_bare_with_pool_limit(&url, connect_timeout, mysql_pool_max_connections).await?
|
||||
connect_bare_mysql_pool_with_setup(
|
||||
&db_config,
|
||||
&url,
|
||||
connect_timeout,
|
||||
mysql_pool_max_connections,
|
||||
&oceanbase_mysql_setup_queries(&db_config),
|
||||
)
|
||||
.await?
|
||||
};
|
||||
PoolKind::Mysql(pool, MysqlMode::Bare)
|
||||
}
|
||||
|
||||
@@ -659,6 +659,25 @@ impl ConnectionConfig {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn is_starrocks(&self) -> bool {
|
||||
self.db_type == DatabaseType::StarRocks
|
||||
|| self.driver_profile.as_deref().is_some_and(|profile| profile.eq_ignore_ascii_case("starrocks"))
|
||||
}
|
||||
|
||||
pub fn bare_mysql_supports_tls(&self) -> bool {
|
||||
self.is_starrocks()
|
||||
}
|
||||
|
||||
pub fn bare_mysql_uses_tls(&self) -> bool {
|
||||
if !self.bare_mysql_supports_tls() {
|
||||
return false;
|
||||
}
|
||||
if mysql_url_params_tls_disabled(self.url_params.as_deref()) {
|
||||
return false;
|
||||
}
|
||||
self.mysql_uses_tls()
|
||||
}
|
||||
|
||||
pub fn canonicalized(&self) -> Self {
|
||||
let mut config = self.clone();
|
||||
if config.db_type == DatabaseType::Mysql
|
||||
@@ -1072,6 +1091,9 @@ impl ConnectionConfig {
|
||||
fn normalized_url_params(&self) -> String {
|
||||
let value = self.url_params.as_deref().unwrap_or("").trim();
|
||||
if self.needs_bare_mysql() {
|
||||
if self.bare_mysql_uses_tls() {
|
||||
return normalize_mysql_url_params(value, true, self.ca_cert_path.trim().is_empty());
|
||||
}
|
||||
return normalize_bare_mysql_url_params(value);
|
||||
}
|
||||
match self.db_type {
|
||||
@@ -1097,8 +1119,10 @@ impl ConnectionConfig {
|
||||
self.ssl || url_params_contains_flag(self.url_params.as_deref(), "secure", "true")
|
||||
}
|
||||
|
||||
fn mysql_uses_tls(&self) -> bool {
|
||||
self.ssl || self.host.to_ascii_lowercase().ends_with(".tidbcloud.com")
|
||||
pub fn mysql_uses_tls(&self) -> bool {
|
||||
self.ssl
|
||||
|| self.host.to_ascii_lowercase().ends_with(".tidbcloud.com")
|
||||
|| mysql_url_params_require_tls(self.url_params.as_deref())
|
||||
}
|
||||
|
||||
fn redis_tls_insecure_fragment(&self) -> &'static str {
|
||||
@@ -1131,6 +1155,50 @@ fn url_params_contains_flag(params: Option<&str>, key: &str, expected: &str) ->
|
||||
)
|
||||
}
|
||||
|
||||
fn mysql_tls_file_param_is(key: &str, target: &str) -> bool {
|
||||
let normalized = key.to_ascii_lowercase().replace(['-', '_'], "");
|
||||
normalized == format!("ssl{target}")
|
||||
}
|
||||
|
||||
fn mysql_url_params_tls_disabled(params: Option<&str>) -> bool {
|
||||
params.unwrap_or("").trim().trim_start_matches('?').split('&').any(|part| {
|
||||
let part = part.trim();
|
||||
if part.is_empty() {
|
||||
return false;
|
||||
}
|
||||
let Some((key, value)) = part.split_once('=') else {
|
||||
return false;
|
||||
};
|
||||
let key = key.trim();
|
||||
let value = value.trim();
|
||||
(key.eq_ignore_ascii_case("require_ssl") && value.eq_ignore_ascii_case("false"))
|
||||
|| ((key.eq_ignore_ascii_case("ssl-mode") || key.eq_ignore_ascii_case("sslmode"))
|
||||
&& matches!(value.to_ascii_lowercase().replace('-', "_").as_str(), "disabled" | "disable"))
|
||||
})
|
||||
}
|
||||
|
||||
fn mysql_url_params_require_tls(params: Option<&str>) -> bool {
|
||||
params.unwrap_or("").trim().trim_start_matches('?').split('&').any(|part| {
|
||||
let part = part.trim();
|
||||
if part.is_empty() {
|
||||
return false;
|
||||
}
|
||||
let Some((key, value)) = part.split_once('=') else {
|
||||
return mysql_tls_file_param_is(part, "cert") || mysql_tls_file_param_is(part, "key");
|
||||
};
|
||||
let key = key.trim();
|
||||
let value = value.trim();
|
||||
(key.eq_ignore_ascii_case("require_ssl") && value.eq_ignore_ascii_case("true"))
|
||||
|| mysql_tls_file_param_is(key, "cert")
|
||||
|| mysql_tls_file_param_is(key, "key")
|
||||
|| ((key.eq_ignore_ascii_case("ssl-mode") || key.eq_ignore_ascii_case("sslmode"))
|
||||
&& matches!(
|
||||
value.to_ascii_lowercase().replace('-', "_").as_str(),
|
||||
"required" | "require" | "verify_ca" | "verify_identity"
|
||||
))
|
||||
})
|
||||
}
|
||||
|
||||
fn normalize_bare_mysql_url_params(value: &str) -> String {
|
||||
value
|
||||
.trim_start_matches('?')
|
||||
@@ -1863,7 +1931,7 @@ mod tests {
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn starrocks_profile_omits_mysql_ssl_mode_param() {
|
||||
fn starrocks_profile_omits_mysql_ssl_mode_param_when_tls_disabled() {
|
||||
let mut config = mysql_config("root", "secret", Some("analytics"));
|
||||
config.driver_profile = Some("starrocks".to_string());
|
||||
config.url_params = Some(
|
||||
@@ -1872,9 +1940,38 @@ mod tests {
|
||||
);
|
||||
|
||||
assert!(config.needs_bare_mysql());
|
||||
assert!(!config.bare_mysql_uses_tls());
|
||||
assert_eq!(config.connection_url(), "mysql://root:secret@10.1.2.3:2883/analytics");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn starrocks_profile_preserves_mysql_tls_params_when_enabled() {
|
||||
let mut config = mysql_config("root", "secret", Some("analytics"));
|
||||
config.driver_profile = Some("starrocks".to_string());
|
||||
config.ssl = true;
|
||||
config.url_params = Some("verify_ca=true&verify_identity=false".to_string());
|
||||
|
||||
assert!(config.bare_mysql_uses_tls());
|
||||
assert_eq!(
|
||||
config.connection_url(),
|
||||
"mysql://root:secret@10.1.2.3:2883/analytics?require_ssl=true&verify_ca=true&verify_identity=false&charset=utf8mb4"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn starrocks_database_type_preserves_mysql_tls_params_when_enabled() {
|
||||
let mut config = mysql_config("root", "secret", Some("analytics"));
|
||||
config.db_type = DatabaseType::StarRocks;
|
||||
config.ssl = true;
|
||||
config.ca_cert_path = "/tmp/starrocks-ca.pem".to_string();
|
||||
|
||||
assert!(config.bare_mysql_uses_tls());
|
||||
assert_eq!(
|
||||
config.connection_url(),
|
||||
"mysql://root:secret@10.1.2.3:2883/analytics?require_ssl=true&verify_identity=false&charset=utf8mb4"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn starrocks_profile_keeps_non_mysql_tls_params() {
|
||||
let mut config = mysql_config("root", "secret", Some("analytics"));
|
||||
|
||||
Reference in new issue
Block a user