From 321ebc263612e60c788d08c43322f0ac5add82e4 Mon Sep 17 00:00:00 2001 From: t8y2 <1156263951@qq.com> Date: Wed, 13 May 2026 14:07:01 +0800 Subject: [PATCH] fix: support HTTPS and self-signed certs for Elasticsearch connections ES 8.x enables HTTPS with self-signed certs by default. When SSL is toggled on, use https:// scheme and accept invalid certificates. Closes #234 --- crates/dbx-core/src/connection.rs | 9 +++++++-- crates/dbx-core/src/db/elasticsearch_driver.rs | 9 ++++++--- crates/dbx-core/src/models/connection.rs | 10 ++++++++-- src-tauri/src/commands/connection.rs | 16 ++++++++++++---- 4 files changed, 33 insertions(+), 11 deletions(-) diff --git a/crates/dbx-core/src/connection.rs b/crates/dbx-core/src/connection.rs index d8be2197f..5f39e49e1 100644 --- a/crates/dbx-core/src/connection.rs +++ b/crates/dbx-core/src/connection.rs @@ -204,8 +204,13 @@ impl AppState { PoolKind::SqlServer(Arc::new(tokio::sync::Mutex::new(client))) } DatabaseType::Elasticsearch => { - let client = - db::elasticsearch_driver::EsClient::new(&url, Some(&db_config.username), Some(&db_config.password)); + let accept_invalid_certs = db_config.ssl; + let client = db::elasticsearch_driver::EsClient::new( + &url, + Some(&db_config.username), + Some(&db_config.password), + accept_invalid_certs, + ); db::elasticsearch_driver::test_connection(&client).await?; PoolKind::Elasticsearch(client) } diff --git a/crates/dbx-core/src/db/elasticsearch_driver.rs b/crates/dbx-core/src/db/elasticsearch_driver.rs index 1b7cf4748..3a7ccaa82 100644 --- a/crates/dbx-core/src/db/elasticsearch_driver.rs +++ b/crates/dbx-core/src/db/elasticsearch_driver.rs @@ -11,13 +11,16 @@ pub struct EsClient { } impl EsClient { - pub fn new(url: &str, username: Option<&str>, password: Option<&str>) -> Self { + pub fn new(url: &str, username: Option<&str>, password: Option<&str>, accept_invalid_certs: bool) -> Self { let auth = match (username, password) { (Some(u), Some(p)) if !u.is_empty() => Some((u.to_string(), p.to_string())), _ => None, }; - let http = - HttpClient::builder().connect_timeout(connection_timeout()).build().unwrap_or_else(|_| HttpClient::new()); + let http = HttpClient::builder() + .connect_timeout(connection_timeout()) + .danger_accept_invalid_certs(accept_invalid_certs) + .build() + .unwrap_or_else(|_| HttpClient::new()); Self { http, base_url: url.trim_end_matches('/').to_string(), auth } } diff --git a/crates/dbx-core/src/models/connection.rs b/crates/dbx-core/src/models/connection.rs index ab5efb08a..479e1e01c 100644 --- a/crates/dbx-core/src/models/connection.rs +++ b/crates/dbx-core/src/models/connection.rs @@ -225,7 +225,10 @@ impl ConnectionConfig { format!("mongodb://{host}:{port}{db_part}{suffix}") } DatabaseType::Oracle => format!("oracle://{host}:{port}{db_part}"), - DatabaseType::Elasticsearch => format!("http://{host}:{port}"), + DatabaseType::Elasticsearch => { + let scheme = if self.ssl { "https" } else { "http" }; + format!("{scheme}://{host}:{port}") + } DatabaseType::Dameng => format!("dm://{host}:{port}{db_part}"), DatabaseType::Kingbase => format!("kingbase://{host}:{port}{db_part}"), DatabaseType::Vastbase => format!("vastbase://{host}:{port}{db_part}"), @@ -306,7 +309,10 @@ impl ConnectionConfig { DatabaseType::Oracle => { format!("oracle://{}:{}@{host}:{port}{db_part}", username, password) } - DatabaseType::Elasticsearch => format!("http://{host}:{port}"), + DatabaseType::Elasticsearch => { + let scheme = if self.ssl { "https" } else { "http" }; + format!("{scheme}://{host}:{port}") + } DatabaseType::Dameng => { format!("dm://{}:{}@{host}:{port}{db_part}", username, password) } diff --git a/src-tauri/src/commands/connection.rs b/src-tauri/src/commands/connection.rs index d4766ef13..7d70efbc9 100644 --- a/src-tauri/src/commands/connection.rs +++ b/src-tauri/src/commands/connection.rs @@ -98,8 +98,12 @@ pub async fn test_connection(state: State<'_, Arc>, config: Connection .map(|_| "Connection successful".to_string()) } DatabaseType::Elasticsearch => { - let client = - db::elasticsearch_driver::EsClient::new(&url, Some(&config.username), Some(&config.password)); + let client = db::elasticsearch_driver::EsClient::new( + &url, + Some(&config.username), + Some(&config.password), + config.ssl, + ); db::elasticsearch_driver::test_connection(&client).await.map(|_| "Connection successful".to_string()) } DatabaseType::Dameng @@ -208,8 +212,12 @@ pub async fn connect_db(state: State<'_, Arc>, config: ConnectionConfi PoolKind::SqlServer(std::sync::Arc::new(tokio::sync::Mutex::new(client))) } DatabaseType::Elasticsearch => { - let client = - db::elasticsearch_driver::EsClient::new(&url, Some(&db_config.username), Some(&db_config.password)); + let client = db::elasticsearch_driver::EsClient::new( + &url, + Some(&db_config.username), + Some(&db_config.password), + db_config.ssl, + ); db::elasticsearch_driver::test_connection(&client).await?; PoolKind::Elasticsearch(client) }