refactor(workspace): split SQL and database driver crates

This commit is contained in:
t8y2 committed 2026-09-25 15:51:58 +08:00
1 parent b99c60aff6
commit e0da2d09d3
180 files changed
+1422 -538

No files matched your search

-149
View File
@@ -1,149 +0,0 @@
use std::collections::HashSet;
use crate::database_manifest;
use crate::models::connection::DatabaseType;
pub fn agent_key(db_type: &DatabaseType, driver_profile: Option<&str>) -> Option<&'static str> {
let entry = database_manifest::entry(db_type)?;
if let Some(driver_profile) = driver_profile {
if let Some(profile) =
entry.driver_profiles.iter().find(|profile| profile.profile.eq_ignore_ascii_case(driver_profile))
{
return Some(profile.agent_key.as_str());
}
}
entry.agent_key.as_deref()
}
pub fn is_agent_type(db_type: &DatabaseType) -> bool {
database_manifest::is_agent_runtime(db_type)
}
pub fn driver_store_entries() -> impl Iterator<Item = (&'static str, &'static str)> {
let mut entries = database_manifest::entries()
.iter()
.flat_map(|entry| {
let base = entry.driver_store_visible.then(|| {
entry
.agent_key
.as_deref()
.map(|key| (entry.driver_store_order.unwrap_or(u32::MAX), key, entry.label.as_str()))
});
let profiles = entry.driver_profiles.iter().filter(|profile| profile.store_visible).map(|profile| {
(
profile.store_order.unwrap_or(u32::MAX),
profile.package_key.as_deref().unwrap_or(profile.agent_key.as_str()),
profile.label.as_str(),
)
});
let managed = entry
.managed_drivers
.iter()
.filter(|driver| driver.store_visible)
.map(|driver| (driver.store_order.unwrap_or(u32::MAX), driver.key.as_str(), driver.label.as_str()));
base.flatten().into_iter().chain(profiles).chain(managed)
})
.collect::<Vec<_>>();
entries.sort_by_key(|(order, _, _)| *order);
let mut seen = HashSet::new();
entries.into_iter().filter(move |(_, key, _)| seen.insert(*key)).map(|(_, key, label)| (key, label))
}
pub fn label_for_key(agent_key: &str) -> Option<&'static str> {
for entry in database_manifest::entries() {
if entry.agent_key.as_deref() == Some(agent_key) {
return Some(entry.label.as_str());
}
if let Some(profile) = entry
.driver_profiles
.iter()
.find(|profile| profile.package_key.as_deref() == Some(agent_key) || profile.agent_key == agent_key)
{
return Some(profile.label.as_str());
}
if let Some(driver) = entry.managed_drivers.iter().find(|driver| driver.key == agent_key) {
return Some(driver.label.as_str());
}
}
None
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn h2_profiles_share_the_same_agent() {
assert_eq!(agent_key(&DatabaseType::H2, None), Some("h2"));
assert_eq!(agent_key(&DatabaseType::H2, Some("h2")), Some("h2"));
assert_eq!(agent_key(&DatabaseType::H2, Some("h2-legacy")), Some("h2"));
assert_eq!(agent_key(&DatabaseType::H2, Some("h2-v1")), Some("h2"));
assert_eq!(agent_key(&DatabaseType::H2, Some("h2-v2")), Some("h2"));
assert_eq!(agent_key(&DatabaseType::H2, Some("h2-v3")), Some("h2"));
assert_eq!(agent_key(&DatabaseType::H2, Some("h2-custom")), Some("h2"));
assert_eq!(label_for_key("h2-legacy"), Some("H2 2.1 Legacy"));
assert!(!driver_store_entries().any(|(key, _)| key == "h2-legacy"));
}
#[test]
fn etcd_v2_profile_uses_dedicated_agent() {
assert_eq!(agent_key(&DatabaseType::Etcd, None), Some("etcd"));
assert_eq!(agent_key(&DatabaseType::Etcd, Some("etcd")), Some("etcd"));
assert_eq!(agent_key(&DatabaseType::Etcd, Some("etcd-v2")), Some("etcd2"));
assert_eq!(agent_key(&DatabaseType::Etcd, Some("etcd-custom")), Some("etcd"));
assert_eq!(label_for_key("etcd2"), Some("etcd 2.x (v2 API)"));
// etcd2 ships its own binary, version, and registry entry, so it must
// stay visible in the driver store for install/upgrade/uninstall.
assert!(driver_store_entries().any(|(key, label)| key == "etcd2" && label == "etcd 2.x (v2 API)"));
}
#[test]
fn duckdb_is_available_in_driver_store_without_using_agent_runtime() {
assert!(driver_store_entries().any(|(key, label)| key == "duckdb" && label == "DuckDB"));
assert_eq!(label_for_key("duckdb"), Some("DuckDB"));
assert!(!is_agent_type(&DatabaseType::DuckDb));
}
#[test]
fn sqlite_ssh_worker_is_available_in_driver_store_without_using_agent_runtime() {
assert!(driver_store_entries().any(|(key, label)| key == "sqlite-worker" && label == "SQLite SSH Worker"));
assert_eq!(label_for_key("sqlite-worker"), Some("SQLite SSH Worker"));
assert!(!is_agent_type(&DatabaseType::Sqlite));
}
#[test]
fn impala_reuses_hive_agent_without_duplicate_store_entry() {
assert_eq!(agent_key(&DatabaseType::Impala, None), Some("hive"));
assert_eq!(driver_store_entries().filter(|(key, _)| *key == "hive").count(), 1);
}
#[test]
fn kyuubi_reuses_hive_agent_without_duplicate_store_entry() {
assert_eq!(agent_key(&DatabaseType::Kyuubi, None), Some("hive"));
assert_eq!(driver_store_entries().filter(|(key, _)| *key == "hive").count(), 1);
}
#[test]
fn cache_profile_uses_dedicated_agent_under_iris() {
assert_eq!(agent_key(&DatabaseType::Iris, None), Some("iris"));
assert_eq!(agent_key(&DatabaseType::Iris, Some("iris")), Some("iris"));
assert_eq!(agent_key(&DatabaseType::Iris, Some("cache")), Some("cache"));
assert_eq!(label_for_key("cache"), Some("InterSystems Caché"));
// The Caché agent ships its own shaded CacheDB driver, so it needs its
// own driver store entry for install/upgrade/uninstall.
assert!(driver_store_entries().any(|(key, label)| key == "cache" && label == "InterSystems Caché"));
}
#[test]
fn manifest_agent_keys_match_catalog_defaults() {
for entry in database_manifest::entries().iter().filter(|entry| entry.agent_key.is_some()) {
assert_eq!(
agent_key(&entry.db_type, None),
entry.agent_key.as_deref(),
"agent key drift for {:?}",
entry.db_type
);
}
}
}
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
-68
View File
@@ -1,68 +0,0 @@
//! Recovery decisions derived from typed JDBC Agent failures.
use crate::db::agent_driver::{AgentCallError, AgentErrorCategory, AgentSessionDisposition};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RecoveryScope {
UserOperation,
ReadOnlyMetadata { retried: bool },
Keepalive,
ConnectionOpen,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RecoveryDecision {
KeepSession,
RetryReadOnlyMetadata,
QuarantineSession,
ReplaceRuntime,
}
impl RecoveryDecision {
pub fn discards_session(self) -> bool {
!matches!(self, Self::KeepSession)
}
pub fn replaces_runtime(self) -> bool {
matches!(self, Self::ReplaceRuntime)
}
}
pub struct RecoveryPolicy;
impl RecoveryPolicy {
pub fn decide(error: &AgentCallError, scope: RecoveryScope) -> RecoveryDecision {
match error {
AgentCallError::ContractViolation { .. } => RecoveryDecision::QuarantineSession,
AgentCallError::Transport { .. } => RecoveryDecision::ReplaceRuntime,
AgentCallError::Timeout { .. } | AgentCallError::Canceled { .. } => RecoveryDecision::QuarantineSession,
AgentCallError::Structured { context, .. } => {
if matches!(context.category, AgentErrorCategory::Timeout | AgentErrorCategory::Canceled) {
return RecoveryDecision::QuarantineSession;
}
Self::from_disposition(Some(context.session_disposition), Some(context.category), scope)
}
AgentCallError::Legacy { hints, .. } => {
Self::from_disposition(hints.session_disposition, hints.category, scope)
}
}
}
fn from_disposition(
disposition: Option<AgentSessionDisposition>,
category: Option<AgentErrorCategory>,
scope: RecoveryScope,
) -> RecoveryDecision {
match disposition {
Some(AgentSessionDisposition::ReplaceRuntime) => RecoveryDecision::ReplaceRuntime,
Some(AgentSessionDisposition::Quarantine)
if category == Some(AgentErrorCategory::Connection)
&& matches!(scope, RecoveryScope::ReadOnlyMetadata { retried: false }) =>
{
RecoveryDecision::RetryReadOnlyMetadata
}
Some(AgentSessionDisposition::Quarantine) => RecoveryDecision::QuarantineSession,
Some(AgentSessionDisposition::Keep) | None => RecoveryDecision::KeepSession,
}
}
}
-469
View File
@@ -1,469 +0,0 @@
use serde::de::DeserializeOwned;
use std::time::Duration;
use crate::agent_manager::{AgentManager, DEFAULT_JRE_KEY};
use crate::agent_recovery::{RecoveryPolicy, RecoveryScope};
use crate::database_capabilities;
use crate::db::agent_driver::{AgentCallError, AgentDriverClient, AgentMethod, AgentRuntimeClient};
use crate::models::connection::DatabaseType;
pub struct SharedConnectionOpenError {
pub message: String,
pub runtime: Option<std::sync::Arc<AgentRuntimeClient>>,
}
impl From<String> for SharedConnectionOpenError {
fn from(message: String) -> Self {
Self { message, runtime: None }
}
}
pub fn db_type_to_agent_key(db_type: &DatabaseType, driver_profile: Option<&str>) -> Option<&'static str> {
database_capabilities::agent_key(db_type, driver_profile)
}
pub fn is_agent_type(db_type: &DatabaseType) -> bool {
database_capabilities::is_agent_type(db_type)
}
pub async fn stop_daemons(manager: &AgentManager) {
manager.daemons.lock().await.clear();
let runtimes = std::mem::take(&mut *manager.connection_runtimes.lock().await);
for runtime in runtimes.into_values().filter_map(|cell| cell.get().cloned()) {
runtime.kill_and_wait().await;
}
}
pub async fn stop_daemon_by_key(manager: &AgentManager, agent_key: &str) {
manager.daemons.lock().await.remove(agent_key);
let runtimes = {
let mut runtimes = manager.connection_runtimes.lock().await;
let matching =
runtimes.keys().filter(|key| key.starts_with(&format!("{agent_key}|"))).cloned().collect::<Vec<_>>();
matching
.into_iter()
.filter_map(|key| runtimes.remove(&key).and_then(|cell| cell.get().cloned()))
.collect::<Vec<_>>()
};
for runtime in runtimes {
runtime.kill_and_wait().await;
}
}
pub async fn restart_daemon_by_key(manager: &AgentManager, agent_key: &str) -> Result<(), String> {
manager.daemons.lock().await.remove(agent_key);
let client = spawn_client_for_key(manager, agent_key, &[]).await?;
manager.daemons.lock().await.insert(agent_key.to_string(), client);
Ok(())
}
pub async fn spawn_connection_client(
manager: &AgentManager,
db_type: &DatabaseType,
driver_profile: Option<&str>,
extra_java_args: &[String],
) -> Result<AgentDriverClient, String> {
let keys = runtime_agent_key_candidates(db_type, driver_profile)
.ok_or_else(|| format!("{:?} is not an agent-driven database type", db_type))?;
spawn_first_available_client(manager, &keys, extra_java_args).await
}
pub async fn spawn_shared_connection_client(
manager: &AgentManager,
db_type: &DatabaseType,
driver_profile: Option<&str>,
extra_java_args: &[String],
agent_session_id: String,
connect_params: serde_json::Value,
connect_timeout: Duration,
) -> Result<AgentDriverClient, SharedConnectionOpenError> {
let keys = runtime_agent_key_candidates(db_type, driver_profile)
.ok_or_else(|| format!("{:?} is not an agent-driven database type", db_type))?;
let key = first_installed_agent_key(manager, &keys).unwrap_or(keys[0]);
let state = manager.load_state();
let jre_key = state.installed_drivers.get(key).map(|driver| driver.jre.as_str()).unwrap_or(DEFAULT_JRE_KEY);
let launch = manager.resolve_agent_launch_spec_with_extra_args(&state, key, jre_key, extra_java_args)?;
let runtime_key = shared_runtime_key(key, &launch);
let mut session_params = connect_params;
session_params
.as_object_mut()
.ok_or_else(|| "Agent connect parameters must be an object".to_string())?
.insert("agentSessionId".to_string(), serde_json::Value::String(agent_session_id.clone()));
let (runtime_cell, runtime) = loop {
let runtime_cell = {
let mut runtimes = manager.connection_runtimes.lock().await;
if runtimes.get(&runtime_key).and_then(|cell| cell.get()).is_some_and(|runtime| runtime.is_failed()) {
runtimes.remove(&runtime_key);
}
runtimes
.entry(runtime_key.clone())
.or_insert_with(|| std::sync::Arc::new(tokio::sync::OnceCell::new()))
.clone()
};
let runtime = runtime_cell
.get_or_try_init(|| AgentRuntimeClient::spawn(launch.clone(), manager.agent_app_version()))
.await?
.clone();
let mut runtimes = manager.connection_runtimes.lock().await;
if reserve_runtime_locked(&mut runtimes, &runtime_key, &runtime_cell, &runtime) {
break (runtime_cell, runtime);
}
};
if let Err(err) = runtime
.call_typed::<serde_json::Value>(AgentMethod::OpenSession.as_str(), session_params, Some(connect_timeout), None)
.await
{
let open_error = shared_connection_open_error(err, runtime.clone());
forget_unused_runtime_after_failed_open(manager, &runtime_key, &runtime_cell, &runtime).await;
return Err(open_error);
}
Ok(AgentDriverClient::shared_session(runtime, agent_session_id))
}
fn shared_connection_open_error(
error: AgentCallError,
runtime: std::sync::Arc<AgentRuntimeClient>,
) -> SharedConnectionOpenError {
let runtime = RecoveryPolicy::decide(&error, RecoveryScope::ConnectionOpen).replaces_runtime().then_some(runtime);
SharedConnectionOpenError { message: error.into_legacy_string(), runtime }
}
async fn forget_unused_runtime_after_failed_open(
manager: &AgentManager,
runtime_key: &str,
runtime_cell: &std::sync::Arc<tokio::sync::OnceCell<std::sync::Arc<AgentRuntimeClient>>>,
runtime: &std::sync::Arc<AgentRuntimeClient>,
) {
if AgentRuntimeClient::decrement_session_count(runtime) != 0 {
return;
}
remove_unused_runtime_if_current(manager, runtime_key, runtime_cell, runtime).await;
}
async fn remove_unused_runtime_if_current(
manager: &AgentManager,
runtime_key: &str,
runtime_cell: &std::sync::Arc<tokio::sync::OnceCell<std::sync::Arc<AgentRuntimeClient>>>,
runtime: &std::sync::Arc<AgentRuntimeClient>,
) {
// Keep reservation and map-entry validation under the same lock as openers.
let mut runtimes = manager.connection_runtimes.lock().await;
if runtime.active_session_count() == 0
&& runtimes.get(runtime_key).is_some_and(|current| std::sync::Arc::ptr_eq(current, runtime_cell))
{
runtimes.remove(runtime_key);
}
}
fn reserve_runtime_locked(
runtimes: &mut std::collections::HashMap<
String,
std::sync::Arc<tokio::sync::OnceCell<std::sync::Arc<AgentRuntimeClient>>>,
>,
runtime_key: &str,
runtime_cell: &std::sync::Arc<tokio::sync::OnceCell<std::sync::Arc<AgentRuntimeClient>>>,
runtime: &std::sync::Arc<AgentRuntimeClient>,
) -> bool {
if runtimes.get(runtime_key).is_some_and(|current| std::sync::Arc::ptr_eq(current, runtime_cell))
&& !runtime.is_failed()
{
runtime.increment_session_count();
true
} else {
false
}
}
fn shared_runtime_key(agent_key: &str, launch: &crate::db::agent_driver::AgentLaunchSpec) -> String {
format!(
"{}|{}|{}|{}",
agent_key,
launch.program.display(),
launch.args.join("\u{1f}"),
launch.working_dir.as_ref().map(|path| path.display().to_string()).unwrap_or_default()
)
}
pub async fn call_daemon<T: DeserializeOwned + Send + 'static>(
manager: &AgentManager,
db_type: &DatabaseType,
driver_profile: Option<&str>,
method: &str,
params: serde_json::Value,
) -> Result<T, String> {
let keys = runtime_agent_key_candidates(db_type, driver_profile)
.ok_or_else(|| format!("{:?} is not an agent-driven database type", db_type))?;
let key = first_installed_agent_key(manager, &keys).unwrap_or(keys[0]).to_string();
let mut daemons = manager.daemons.lock().await;
if !daemons.contains_key(&key) {
let client = spawn_client_for_key(manager, &key, &[]).await?;
daemons.insert(key.clone(), client);
}
let client = daemons.get_mut(&key).unwrap();
match client.call::<T>(method, params.clone()).await {
Ok(result) => Ok(result),
Err(err) => {
log::warn!("[agent] daemon call failed, respawning: {err}");
daemons.remove(&key);
let mut new_client = spawn_client_for_key(manager, &key, &[]).await?;
let result = new_client.call::<T>(method, params).await?;
daemons.insert(key, new_client);
Ok(result)
}
}
}
pub async fn call_daemon_with_timeout<T: DeserializeOwned + Send + 'static>(
manager: &AgentManager,
db_type: &DatabaseType,
driver_profile: Option<&str>,
method: &str,
params: serde_json::Value,
timeout_duration: Option<Duration>,
) -> Result<T, String> {
let keys = runtime_agent_key_candidates(db_type, driver_profile)
.ok_or_else(|| format!("{:?} is not an agent-driven database type", db_type))?;
let key = first_installed_agent_key(manager, &keys).unwrap_or(keys[0]).to_string();
let mut daemons = manager.daemons.lock().await;
if !daemons.contains_key(&key) {
let client = spawn_client_for_key(manager, &key, &[]).await?;
daemons.insert(key.clone(), client);
}
let client = daemons.get_mut(&key).unwrap();
match client.call_with_timeout::<T>(method, params.clone(), timeout_duration).await {
Ok(result) => Ok(result),
Err(err) => {
log::warn!("[agent] daemon call failed, respawning: {err}");
daemons.remove(&key);
let mut new_client = spawn_client_for_key(manager, &key, &[]).await?;
let result = new_client.call_with_timeout::<T>(method, params, timeout_duration).await?;
daemons.insert(key, new_client);
Ok(result)
}
}
}
pub async fn call_daemon_method<T: DeserializeOwned + Send + 'static>(
manager: &AgentManager,
db_type: &DatabaseType,
driver_profile: Option<&str>,
method: AgentMethod,
params: serde_json::Value,
) -> Result<T, String> {
call_daemon(manager, db_type, driver_profile, method.as_str(), params).await
}
pub async fn call_daemon_method_with_timeout<T: DeserializeOwned + Send + 'static>(
manager: &AgentManager,
db_type: &DatabaseType,
driver_profile: Option<&str>,
method: AgentMethod,
params: serde_json::Value,
timeout_duration: Option<Duration>,
) -> Result<T, String> {
call_daemon_with_timeout(manager, db_type, driver_profile, method.as_str(), params, timeout_duration).await
}
fn runtime_agent_key_candidates(db_type: &DatabaseType, driver_profile: Option<&str>) -> Option<Vec<&'static str>> {
let primary = db_type_to_agent_key(db_type, driver_profile)?;
Some(vec![primary])
}
fn first_installed_agent_key<'a>(manager: &AgentManager, keys: &'a [&'static str]) -> Option<&'a str> {
keys.iter().copied().find(|key| manager.is_driver_installed(key))
}
async fn spawn_first_available_client(
manager: &AgentManager,
keys: &[&'static str],
extra_java_args: &[String],
) -> Result<AgentDriverClient, String> {
let mut last_error = None;
for key in keys {
match spawn_client_for_key(manager, key, extra_java_args).await {
Ok(client) => return Ok(client),
Err(err) => last_error = Some(err),
}
}
Err(last_error.unwrap_or_else(|| "No agent driver candidates available".to_string()))
}
async fn spawn_client_for_key(
manager: &AgentManager,
key: &str,
extra_java_args: &[String],
) -> Result<AgentDriverClient, String> {
let state = manager.load_state();
let jre_key = state.installed_drivers.get(key).map(|driver| driver.jre.as_str()).unwrap_or(DEFAULT_JRE_KEY);
let launch = manager.resolve_agent_launch_spec_with_extra_args(&state, key, jre_key, extra_java_args)?;
let mut client = AgentDriverClient::spawn(launch).await?;
client.try_optional_handshake(manager.agent_app_version()).await;
Ok(client)
}
#[cfg(test)]
mod tests {
use super::*;
use std::path::PathBuf;
async fn test_shared_runtime(
name: &str,
) -> (
AgentManager,
std::sync::Arc<tokio::sync::OnceCell<std::sync::Arc<AgentRuntimeClient>>>,
std::sync::Arc<AgentRuntimeClient>,
PathBuf,
) {
let test_id = uuid::Uuid::new_v4();
let script_path = std::env::temp_dir().join(format!("dbx-agent-runtime-{name}-{test_id}.py"));
std::fs::write(
&script_path,
r#"import json, sys
print(json.dumps({'ready': True}), flush=True)
for line in sys.stdin:
req = json.loads(line)
result = {'protocolVersion': 2, 'agentProtocolVersion': 2, 'capabilities': ['multi_session']} if req['method'] == 'handshake' else {'ok': True}
print(json.dumps({'jsonrpc': '2.0', 'id': req['id'], 'result': result}), flush=True)
"#,
)
.unwrap();
let python = if cfg!(windows) { "python" } else { "python3" };
let runtime = AgentRuntimeClient::spawn(
crate::db::agent_driver::AgentLaunchSpec::new(python)
.with_args([script_path.to_string_lossy().to_string()]),
"test",
)
.await
.unwrap();
let cell = std::sync::Arc::new(tokio::sync::OnceCell::new());
assert!(cell.set(runtime.clone()).is_ok());
let manager_dir = std::env::temp_dir().join(format!("dbx-agent-manager-{name}-{test_id}"));
let manager = AgentManager::new_with_base_dir_and_app_version(manager_dir, "test");
(manager, cell, runtime, script_path)
}
#[test]
fn prestosql_does_not_use_agent_driver() {
assert_eq!(runtime_agent_key_candidates(&DatabaseType::PrestoSql, None), None);
}
#[test]
fn trino_uses_only_trino_agent_driver() {
assert_eq!(runtime_agent_key_candidates(&DatabaseType::Trino, None).unwrap(), vec!["trino"]);
}
#[test]
fn shared_runtime_key_includes_launch_fingerprint() {
let base = crate::db::agent_driver::AgentLaunchSpec::new(PathBuf::from("oracle-agent"))
.with_args(["--mode", "stdio"])
.with_working_dir(PathBuf::from("/tmp/oracle"));
let different_args = crate::db::agent_driver::AgentLaunchSpec::new(PathBuf::from("oracle-agent"))
.with_args(["--mode", "debug"])
.with_working_dir(PathBuf::from("/tmp/oracle"));
assert_eq!(shared_runtime_key("oracle", &base), shared_runtime_key("oracle", &base));
assert_ne!(shared_runtime_key("oracle", &base), shared_runtime_key("oracle", &different_args));
}
#[tokio::test]
async fn failed_open_forgets_runtime_when_no_other_session_uses_it() {
let (manager, cell, runtime, script_path) = test_shared_runtime("failed-open-unused").await;
let runtime_key = "kingbase|test";
manager.connection_runtimes.lock().await.insert(runtime_key.to_string(), cell.clone());
runtime.increment_session_count();
forget_unused_runtime_after_failed_open(&manager, runtime_key, &cell, &runtime).await;
assert!(!manager.connection_runtimes.lock().await.contains_key(runtime_key));
assert_eq!(runtime.active_session_count(), 0);
runtime.kill();
let _ = std::fs::remove_file(script_path);
}
#[tokio::test]
async fn failed_open_keeps_runtime_while_another_session_is_reserved() {
let (manager, cell, runtime, script_path) = test_shared_runtime("failed-open-in-use").await;
let runtime_key = "oracle|test";
manager.connection_runtimes.lock().await.insert(runtime_key.to_string(), cell.clone());
runtime.increment_session_count();
runtime.increment_session_count();
forget_unused_runtime_after_failed_open(&manager, runtime_key, &cell, &runtime).await;
assert!(manager.connection_runtimes.lock().await.contains_key(runtime_key));
assert_eq!(runtime.active_session_count(), 1);
AgentRuntimeClient::decrement_session_count(&runtime);
runtime.kill();
let _ = std::fs::remove_file(script_path);
}
#[tokio::test]
async fn replace_runtime_open_error_reports_runtime_without_killing_it_directly() {
let (manager, _cell, runtime, script_path) = test_shared_runtime("replace-runtime-open-error").await;
let error = AgentCallError::Legacy {
rpc_code: Some(-1),
message: "capacity exhausted".to_string(),
hints: crate::db::agent_driver::LegacyAgentHints {
category: Some(crate::db::agent_driver::AgentErrorCategory::Resource),
session_disposition: Some(crate::db::agent_driver::AgentSessionDisposition::ReplaceRuntime),
..Default::default()
},
};
let open_error = shared_connection_open_error(error, runtime.clone());
assert!(open_error.runtime.as_ref().is_some_and(|failed| std::sync::Arc::ptr_eq(failed, &runtime)));
assert!(!runtime.is_failed());
runtime.kill();
drop(manager);
let _ = std::fs::remove_file(script_path);
}
#[tokio::test]
async fn failed_open_cleanup_cannot_remove_runtime_after_reservation() {
let (manager, cell, runtime, script_path) = test_shared_runtime("failed-open-race").await;
let runtime_key = "oracle|test";
manager.connection_runtimes.lock().await.insert(runtime_key.to_string(), cell.clone());
runtime.increment_session_count();
assert_eq!(AgentRuntimeClient::decrement_session_count(&runtime), 0);
let mut runtimes = manager.connection_runtimes.lock().await;
assert!(reserve_runtime_locked(&mut runtimes, runtime_key, &cell, &runtime));
drop(runtimes);
remove_unused_runtime_if_current(&manager, runtime_key, &cell, &runtime).await;
assert!(manager.connection_runtimes.lock().await.contains_key(runtime_key));
assert_eq!(runtime.active_session_count(), 1);
AgentRuntimeClient::decrement_session_count(&runtime);
runtime.kill();
let _ = std::fs::remove_file(script_path);
}
#[tokio::test]
async fn stopping_driver_runtime_removes_and_terminates_shared_process() {
let (manager, cell, runtime, script_path) = test_shared_runtime("stop-driver-runtime").await;
let runtime_key = "dameng|test";
manager.connection_runtimes.lock().await.insert(runtime_key.to_string(), cell);
stop_daemon_by_key(&manager, "dameng").await;
assert!(!manager.connection_runtimes.lock().await.contains_key(runtime_key));
assert!(runtime.is_failed());
let call_result =
runtime.call::<serde_json::Value>("ping", serde_json::json!({}), Some(Duration::from_secs(1)), None).await;
assert_eq!(call_result.unwrap_err(), "Agent runtime is unavailable");
let _ = std::fs::remove_file(script_path);
}
}
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
@@ -1,84 +0,0 @@
use crate::agent_catalog;
use crate::database_manifest;
use crate::models::connection::DatabaseType;
pub fn agent_key(db_type: &DatabaseType, driver_profile: Option<&str>) -> Option<&'static str> {
agent_catalog::agent_key(db_type, driver_profile)
}
pub fn is_agent_type(db_type: &DatabaseType) -> bool {
database_manifest::is_agent_runtime(db_type)
}
pub fn is_single_connection_pool(db_type: &DatabaseType) -> bool {
database_manifest::entry(db_type).is_some_and(|entry| entry.single_connection_pool)
}
pub fn is_metadata_connection_scoped(db_type: &DatabaseType) -> bool {
database_manifest::entry(db_type).is_some_and(|entry| entry.metadata_connection_scoped)
}
pub fn skips_tcp_probe(db_type: &DatabaseType) -> bool {
database_manifest::entry(db_type).is_some_and(|entry| entry.skip_tcp_probe) || is_agent_type(db_type)
}
/// Database types whose connection backs onto a single local file (or may, in the
/// case of H2 file mode). Used to decide whether to expose a "reveal in file
/// manager" affordance. Whether the H2 connection is actually in file mode must
/// be determined separately by parsing the JDBC URL.
pub fn is_local_file_db_type(db_type: &DatabaseType) -> bool {
database_manifest::entry(db_type).is_some_and(|entry| entry.local_file)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn local_file_db_types_match_expected_set() {
assert!(is_local_file_db_type(&DatabaseType::Sqlite));
assert!(is_local_file_db_type(&DatabaseType::DuckDb));
assert!(is_local_file_db_type(&DatabaseType::Access));
assert!(is_local_file_db_type(&DatabaseType::H2));
}
#[test]
fn non_local_file_db_types_rejected() {
assert!(!is_local_file_db_type(&DatabaseType::Mysql));
assert!(!is_local_file_db_type(&DatabaseType::Postgres));
assert!(!is_local_file_db_type(&DatabaseType::Redis));
assert!(!is_local_file_db_type(&DatabaseType::MongoDb));
assert!(!is_local_file_db_type(&DatabaseType::Turso));
assert!(!is_local_file_db_type(&DatabaseType::CloudflareD1));
assert!(!is_local_file_db_type(&DatabaseType::Rqlite));
}
#[test]
fn cloudflare_d1_uses_a_single_http_pool_without_tcp_probe() {
assert!(is_single_connection_pool(&DatabaseType::CloudflareD1));
assert!(skips_tcp_probe(&DatabaseType::CloudflareD1));
}
#[test]
fn manifest_connection_capabilities_are_used_for_every_registered_driver() {
for entry in database_manifest::entries() {
assert_eq!(
is_single_connection_pool(&entry.db_type),
entry.single_connection_pool,
"single connection pool drift for {:?}",
entry.db_type
);
assert_eq!(
is_metadata_connection_scoped(&entry.db_type),
entry.metadata_connection_scoped,
"metadata scope drift for {:?}",
entry.db_type
);
assert!(
skips_tcp_probe(&entry.db_type) || !is_agent_type(&entry.db_type),
"agent driver {:?} must skip the TCP probe",
entry.db_type
);
}
}
}
File diff suppressed because it is too large. Load diff
-223
View File
@@ -1,223 +0,0 @@
//! Structural, quote-and-parenthesis-aware scanning helpers for
//! post-processing already-rendered `CREATE TABLE` DDL text.
//!
//! A naive `str::find` for a literal marker (e.g. `" SERVER \""` or a bare
//! `;`) can be fooled by that same text appearing inside a quoted string
//! literal, a quoted identifier, or a nested parenthesized sub-expression —
//! a column's `DEFAULT`/`CHECK` expression can legally contain any of those.
//! These helpers track quote and parenthesis depth so callers only ever
//! match at the top level.
use std::ops::Range;
fn char_at(sql: &str, pos: usize) -> Option<char> {
sql.get(pos..)?.chars().next()
}
/// Skips a `'...'` string literal (`''`-escaped, with backslash-escape
/// tolerance) or a `"..."` quoted identifier (`""`-escaped) starting at
/// `pos`, which must point at the opening `quote` character. Returns the
/// index just past the closing quote (or `sql.len()` if unterminated).
fn skip_quoted(sql: &str, pos: usize, quote: char) -> usize {
let mut i = pos + quote.len_utf8();
while i < sql.len() {
let Some(ch) = char_at(sql, i) else { break };
let next = char_at(sql, i + ch.len_utf8());
if ch == quote {
if next == Some(quote) {
i += ch.len_utf8() + quote.len_utf8();
continue;
}
return i + ch.len_utf8();
}
if quote == '\'' && ch == '\\' {
i += ch.len_utf8();
if let Some(escaped) = char_at(sql, i) {
i += escaped.len_utf8();
}
continue;
}
i += ch.len_utf8();
}
sql.len()
}
/// Skips a PostgreSQL `$tag$...$tag$` dollar-quoted body starting at `pos`
/// (pointing at the first `$`). Returns `None` if `pos` isn't actually the
/// start of a valid dollar-quote tag (e.g. a stray `$` in an expression), in
/// which case the caller should treat `$` as an ordinary character.
fn skip_dollar_quoted(sql: &str, pos: usize) -> Option<usize> {
let tag_end_offset = sql.get(pos + 1..)?.find('$')?;
let tag_end = pos + 1 + tag_end_offset;
let tag = &sql[pos + 1..tag_end];
let valid_tag = tag.is_empty()
|| (tag.chars().next().is_some_and(|ch| ch.is_ascii_alphabetic() || ch == '_')
&& tag.chars().all(|ch| ch.is_ascii_alphanumeric() || ch == '_'));
if !valid_tag {
return None;
}
let delimiter = &sql[pos..=tag_end];
let content_start = tag_end + 1;
sql.get(content_start..)?.find(delimiter).map(|offset| content_start + offset + delimiter.len())
}
/// Finds the byte index just past the first top-level (parenthesis-depth-0)
/// closing paren at or after `from` — the end of the outermost parenthesized
/// group that starts somewhere at or after `from` — treating quoted or
/// dollar-quoted content as opaque so parens or keywords inside a
/// `DEFAULT`/`CHECK` expression can't confuse the count. Returns `None` if no
/// top-level `(` is ever opened, or it's never closed.
pub fn find_top_level_paren_close(sql: &str, from: usize) -> Option<usize> {
let mut i = from;
let mut depth: usize = 0;
let mut opened = false;
while i < sql.len() {
let ch = char_at(sql, i)?;
match ch {
'\'' | '"' => {
i = skip_quoted(sql, i, ch);
continue;
}
'$' => {
if let Some(end) = skip_dollar_quoted(sql, i) {
i = end;
continue;
}
}
'(' => {
depth += 1;
opened = true;
}
')' => {
depth = depth.saturating_sub(1);
i += ch.len_utf8();
if opened && depth == 0 {
return Some(i);
}
continue;
}
_ => {}
}
i += ch.len_utf8();
}
None
}
/// Finds the byte index of the next top-level (depth-0, outside any quoted
/// or dollar-quoted content) `;` at or after `from`.
fn find_top_level_semicolon(sql: &str, from: usize) -> Option<usize> {
let mut i = from;
let mut depth: usize = 0;
while i < sql.len() {
let ch = char_at(sql, i)?;
match ch {
'\'' | '"' => {
i = skip_quoted(sql, i, ch);
continue;
}
'$' => {
if let Some(end) = skip_dollar_quoted(sql, i) {
i = end;
continue;
}
}
'(' => depth += 1,
')' => depth = depth.saturating_sub(1),
';' if depth == 0 => return Some(i),
_ => {}
}
i += ch.len_utf8();
}
None
}
/// Splits `sql` into consecutive top-level statement ranges, each ending
/// just past its own top-level `;`. The last statement may lack a trailing
/// `;`, in which case its range runs to the end of the string.
pub fn top_level_statement_ranges(sql: &str) -> Vec<Range<usize>> {
let mut ranges = Vec::new();
let mut start = 0;
while start < sql.len() {
match find_top_level_semicolon(sql, start) {
Some(semi) => {
ranges.push(start..semi + 1);
start = semi + 1;
}
None => {
ranges.push(start..sql.len());
break;
}
}
}
ranges
}
/// If `stmt` (ignoring leading whitespace) begins with `CREATE TABLE
/// "schema"."table"` or `CREATE FOREIGN TABLE "schema"."table"`, returns the
/// unescaped `(schema, table)` pair.
pub fn parse_create_table_relation(stmt: &str) -> Option<(String, String)> {
let trimmed = stmt.trim_start();
let rest =
trimmed.strip_prefix("CREATE FOREIGN TABLE ").or_else(|| trimmed.strip_prefix("CREATE TABLE "))?.trim_start();
let (schema, rest) = unquote_ident(rest)?;
let (table, _) = unquote_ident(rest.strip_prefix('.')?)?;
Some((schema, table))
}
fn unquote_ident(s: &str) -> Option<(String, &str)> {
if !s.starts_with('"') {
return None;
}
let end = skip_quoted(s, 0, '"');
if end < 2 {
return None;
}
let raw = &s[1..end - 1];
Some((raw.replace("\"\"", "\""), &s[end..]))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn paren_close_skips_nested_parens_and_quotes() {
let ddl =
"\"public\".\"t\" (\n \"a\" text DEFAULT 'x(y)'::text,\n \"b\" text CHECK ((b <> 'z'))\n) SERVER \"s\"";
let close = find_top_level_paren_close(ddl, 0).unwrap();
assert_eq!(
&ddl[..close],
"\"public\".\"t\" (\n \"a\" text DEFAULT 'x(y)'::text,\n \"b\" text CHECK ((b <> 'z'))\n)"
);
}
#[test]
fn paren_close_ignores_literal_server_text_inside_quotes() {
let ddl = "\"public\".\"t\" (\n \"note\" text DEFAULT 'contact SERVER \"admin\"'\n) SERVER \"real_server\"";
let close = find_top_level_paren_close(ddl, 0).unwrap();
assert!(ddl[close..].starts_with(" SERVER \"real_server\""));
}
#[test]
fn top_level_statement_ranges_split_on_depth_zero_semicolons() {
let ddl = "CREATE TABLE \"s\".\"t\" (\n \"a\" text DEFAULT 'x;y'\n);\nALTER TABLE \"s\".\"t\" ADD CONSTRAINT c CHECK (a <> '');";
let ranges = top_level_statement_ranges(ddl);
assert_eq!(ranges.len(), 2);
assert!(ddl[ranges[0].clone()].starts_with("CREATE TABLE"));
assert!(ddl[ranges[0].clone()].ends_with(";"));
assert!(ddl[ranges[1].clone()].trim_start().starts_with("ALTER TABLE"));
}
#[test]
fn parse_create_table_relation_unescapes_doubled_quotes() {
let (schema, table) =
parse_create_table_relation("CREATE TABLE \"pub\"\"lic\".\"t\" (\n \"a\" int\n);").unwrap();
assert_eq!(schema, "pub\"lic");
assert_eq!(table, "t");
}
#[test]
fn parse_create_table_relation_rejects_non_create_table() {
assert!(parse_create_table_relation("ALTER TABLE \"s\".\"t\" ADD CONSTRAINT c CHECK (true);").is_none());
}
}
@@ -1,69 +0,0 @@
use serde::{Deserialize, Serialize};
/// Common result shape returned by document-store queries.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DocumentQueryResult {
pub documents: Vec<serde_json::Value>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub raw_documents: Option<Vec<String>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub extended_documents: Option<Vec<serde_json::Value>>,
pub total: u64,
// Older document stores always return an exact total. Keep that wire shape
// unchanged and only send the flag when Elasticsearch reports a lower bound.
#[serde(default = "default_total_is_exact", skip_serializing_if = "is_true")]
pub total_is_exact: bool,
/// Opaque document-store cursor for APIs such as DynamoDB that do not
/// support offset pagination.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub next_cursor: Option<String>,
}
fn default_total_is_exact() -> bool {
true
}
fn is_true(value: &bool) -> bool {
*value
}
#[cfg(test)]
mod tests {
use super::DocumentQueryResult;
#[test]
fn exact_results_keep_the_existing_wire_shape() {
let result = DocumentQueryResult {
documents: Vec::new(),
raw_documents: None,
extended_documents: None,
total: 1,
total_is_exact: true,
next_cursor: None,
};
let serialized = serde_json::to_value(result).unwrap();
assert!(serialized.get("total_is_exact").is_none());
let deserialized: DocumentQueryResult = serde_json::from_value(serde_json::json!({
"documents": [],
"total": 1,
}))
.unwrap();
assert!(deserialized.total_is_exact);
}
#[test]
fn inexact_results_preserve_the_explicit_flag() {
let deserialized: DocumentQueryResult = serde_json::from_value(serde_json::json!({
"documents": [],
"total": 10_000_000,
"total_is_exact": false,
}))
.unwrap();
assert!(!deserialized.total_is_exact);
let serialized = serde_json::to_value(deserialized).unwrap();
assert_eq!(serialized.get("total_is_exact"), Some(&serde_json::json!(false)));
}
}
-236
View File
@@ -1,236 +0,0 @@
use mysql_async::prelude::Queryable;
use std::collections::HashSet;
use crate::models::connection::{ConnectionConfig, DatabaseType};
use crate::types::{DatabaseInfo, TableInfo};
use super::mysql::{get_conn_with_timeout, list_table_names_show_filtered_with_conn, quote_identifier, MySqlPool};
const ENABLE_BRANCH_DATABASES_SQL: &str = "SET @@dolt_show_branch_databases = 1";
// 与设置对称地复位 session 变量:连接从池中取出后是单条会被复用的 session 连接,
// 若不在归还前复位,后续无关的 SHOW DATABASES(其他元数据路径复用同一条连接时)
// 会意外看到 branch 数据库。复位是 best-effort,失败不应让 list_databases 整体失败。
const DISABLE_BRANCH_DATABASES_SQL: &str = "SET @@dolt_show_branch_databases = 0";
const SHOW_DATABASES_SQL: &str = "SHOW DATABASES";
const ENABLE_SYSTEM_TABLES_SQL: &str = "SET @@dolt_show_system_tables = 1";
const DISABLE_SYSTEM_TABLES_SQL: &str = "SET @@dolt_show_system_tables = 0";
const SHOW_SYSTEM_TABLES_CONFIG_KEY: &str = "doltShowSystemTables";
const SYSTEM_TABLE_PREFIX: &str = "dolt";
pub fn is_config(config: &ConnectionConfig) -> bool {
is_profile(&config.db_type, config.driver_profile.as_deref())
}
pub fn system_tables_visible(config: &ConnectionConfig) -> bool {
is_config(config)
&& config
.external_config
.as_ref()
.and_then(|value| value.get(SHOW_SYSTEM_TABLES_CONFIG_KEY))
.and_then(serde_json::Value::as_bool)
.unwrap_or(false)
}
pub fn requests_system_tables(include_patterns: Option<&[String]>) -> bool {
include_patterns.is_some_and(|patterns| patterns.iter().any(|pattern| pattern.trim().eq_ignore_ascii_case("dolt%")))
}
fn is_profile(db_type: &DatabaseType, driver_profile: Option<&str>) -> bool {
*db_type == DatabaseType::Mysql && driver_profile.is_some_and(|profile| profile.eq_ignore_ascii_case("dolt"))
}
pub async fn list_databases(pool: &MySqlPool) -> Result<Vec<DatabaseInfo>, String> {
let mut conn = get_conn_with_timeout(pool, super::connection_timeout()).await?;
if let Err(error) = conn.query_drop(ENABLE_BRANCH_DATABASES_SQL).await {
log::debug!("Dolt branch database system variable is unavailable; falling back to dolt_branches: {error}");
return list_databases_from_branches(&mut conn).await;
}
let databases = query_database_names(&mut conn).await?;
// 主路径成功读完 SHOW DATABASES 后,归还连接前复位 session 变量,
// 避免该连接被复用于无关元数据查询时仍残留 branch 数据库可见性。
if let Err(error) = conn.query_drop(DISABLE_BRANCH_DATABASES_SQL).await {
log::warn!("Failed to reset Dolt branch database session variable before returning connection: {error}");
}
Ok(database_infos(databases))
}
pub async fn list_system_tables(
pool: &MySqlPool,
database: &str,
filter: Option<&str>,
) -> Result<Vec<TableInfo>, String> {
// The visibility flag is session-scoped, so setup, metadata lookup, and
// cleanup must use the same pooled connection.
let mut conn = get_conn_with_timeout(pool, super::connection_timeout()).await?;
conn.query_drop(ENABLE_SYSTEM_TABLES_SQL).await.map_err(|error| error.to_string())?;
let server_filter = filter.map(str::trim).filter(|filter| !filter.is_empty()).unwrap_or(SYSTEM_TABLE_PREFIX);
let tables = list_table_names_show_filtered_with_conn(&mut conn, database, Some(server_filter), &[])
.await
.map(retain_system_tables);
if let Err(error) = conn.query_drop(DISABLE_SYSTEM_TABLES_SQL).await {
log::warn!("Failed to reset Dolt system table session variable before returning connection: {error}");
}
tables
}
fn retain_system_tables(mut tables: Vec<TableInfo>) -> Vec<TableInfo> {
tables.retain(|table| table.name.to_ascii_lowercase().starts_with(SYSTEM_TABLE_PREFIX));
tables
}
async fn list_databases_from_branches(conn: &mut mysql_async::Conn) -> Result<Vec<DatabaseInfo>, String> {
let base_names = query_database_names(conn).await?;
let discovery_names: Vec<String> =
base_names.iter().filter(|name| is_branch_discovery_database(name)).cloned().collect();
let mut seen: HashSet<String> = base_names.iter().cloned().collect();
let mut names = base_names;
for database in discovery_names {
let branches: Vec<String> = match conn.query(list_branches_sql(&database)).await {
Ok(branches) => branches,
Err(error) => {
// fallback 进入这里说明实例不支持 dolt_show_branch_databases 系统变量。
// 首个非系统库的 dolt_branches 查询报错即可判定该实例并非 Dolt
// (可能是同一实例上混用的纯 MySQL 库),逐库试探只会对每个普通库
// 产生一次无效报错噪音。首次失败即整体降级为纯 SHOW DATABASES 结果,
// 不再继续试探剩余库。注意区分「查询报错(非 Dolt)」与「查询成功但为空
// (是 Dolt 但无 branch)」:只有前者触发降级,后者继续遍历后续库。
log::debug!(
"Dolt branch discovery failed on `{database}`, treating instance as non-Dolt and falling back to plain SHOW DATABASES: {error}"
);
return Ok(database_infos(names));
}
};
for branch in branches {
let revision = format!("{database}/{}", branch.trim());
if !branch.trim().is_empty() && seen.insert(revision.clone()) {
names.push(revision);
}
}
}
Ok(database_infos(names))
}
async fn query_database_names(conn: &mut mysql_async::Conn) -> Result<Vec<String>, String> {
conn.query(SHOW_DATABASES_SQL).await.map_err(|error| error.to_string())
}
fn database_infos(names: Vec<String>) -> Vec<DatabaseInfo> {
names
.into_iter()
.map(|name| name.trim().to_string())
.filter(|name| !name.is_empty())
.map(|name| DatabaseInfo { name, ..Default::default() })
.collect()
}
fn is_branch_discovery_database(name: &str) -> bool {
let normalized = name.trim().to_ascii_lowercase();
!normalized.is_empty()
&& !normalized.contains('/')
&& !matches!(normalized.as_str(), "information_schema" | "mysql" | "performance_schema" | "sys")
}
fn list_branches_sql(database: &str) -> String {
format!("SELECT name FROM {}.dolt_branches ORDER BY name", quote_identifier(database))
}
#[cfg(test)]
mod tests {
use super::*;
fn dolt_config() -> ConnectionConfig {
serde_json::from_value(serde_json::json!({
"id": "dolt",
"name": "Dolt",
"db_type": "mysql",
"driver_profile": "dolt",
"host": "127.0.0.1",
"port": 3306,
"username": "root",
"password": "",
"database": "app"
}))
.unwrap()
}
#[test]
fn profile_is_isolated_from_standard_mysql() {
assert!(is_profile(&DatabaseType::Mysql, Some("Dolt")));
assert!(!is_profile(&DatabaseType::Mysql, None));
assert!(!is_profile(&DatabaseType::Postgres, Some("dolt")));
}
#[test]
fn system_table_visibility_uses_dolt_profile_config_only() {
let mut config = dolt_config();
assert!(!system_tables_visible(&config));
config.external_config = Some(serde_json::json!({ "doltShowSystemTables": true }));
assert!(system_tables_visible(&config));
config.driver_profile = Some("mysql".to_string());
assert!(!system_tables_visible(&config));
}
#[test]
fn system_table_requests_require_the_dedicated_include_pattern() {
assert!(requests_system_tables(Some(&["dolt%".to_string()])));
assert!(requests_system_tables(Some(&[" DOLT% ".to_string()])));
assert!(!requests_system_tables(Some(&["%dolt%".to_string()])));
assert!(!requests_system_tables(Some(&[])));
assert!(!requests_system_tables(None));
}
#[test]
fn system_table_results_exclude_ordinary_tables_after_show_fallbacks() {
let tables = vec![
TableInfo {
name: "dolt_log".to_string(),
table_type: "BASE TABLE".to_string(),
valid: None,
comment: None,
parent_schema: None,
parent_name: None,
},
TableInfo {
name: "DOLT_BRANCHES".to_string(),
table_type: "BASE TABLE".to_string(),
valid: None,
comment: None,
parent_schema: None,
parent_name: None,
},
TableInfo {
name: "orders".to_string(),
table_type: "BASE TABLE".to_string(),
valid: None,
comment: Some("ordinary".to_string()),
parent_schema: None,
parent_name: None,
},
];
assert_eq!(
retain_system_tables(tables).into_iter().map(|table| table.name).collect::<Vec<_>>(),
vec!["dolt_log", "DOLT_BRANCHES"]
);
}
#[test]
fn fallback_only_discovers_base_user_databases() {
assert!(is_branch_discovery_database("inventory"));
assert!(!is_branch_discovery_database("inventory/main"));
assert!(!is_branch_discovery_database("information_schema"));
assert!(!is_branch_discovery_database("mysql"));
}
#[test]
fn fallback_quotes_database_identifier() {
assert_eq!(
list_branches_sql("inventory`archive"),
"SELECT name FROM `inventory``archive`.dolt_branches ORDER BY name"
);
}
}
-32
View File
@@ -1,32 +0,0 @@
use crate::models::connection::{ConnectionConfig, DatabaseType};
pub use super::mysql_compatible::{
get_columns_show_from as get_catalog_columns, list_catalog_indexes, list_catalogs,
list_databases_show_from as list_catalog_databases, list_indexes_with_ddl_fallback as list_indexes,
list_tables_show_from as list_catalog_tables, show_create_table_ddl_from as get_catalog_table_ddl,
};
pub fn is_config(config: &ConnectionConfig) -> bool {
is_profile(&config.db_type, config.driver_profile.as_deref())
}
pub fn is_profile(db_type: &DatabaseType, driver_profile: Option<&str>) -> bool {
*db_type == DatabaseType::Doris
|| (*db_type == DatabaseType::Mysql
&& driver_profile
.is_some_and(|profile| matches!(profile.to_ascii_lowercase().as_str(), "doris" | "selectdb")))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn profile_matches_doris_and_selectdb_only() {
assert!(is_profile(&DatabaseType::Doris, None));
assert!(is_profile(&DatabaseType::Mysql, Some("DORIS")));
assert!(is_profile(&DatabaseType::Mysql, Some("selectdb")));
assert!(!is_profile(&DatabaseType::Mysql, Some("starrocks")));
assert!(!is_profile(&DatabaseType::Postgres, Some("doris")));
}
}
File diff suppressed because it is too large. Load diff
@@ -1,426 +0,0 @@
// SQL → Elasticsearch `_search` DSL translator.
//
// ES SQL's `_sql` endpoint refuses several common shapes — `LIKE` on a `text`
// field with no `.keyword` sub-field, `SELECT *` returning an array field
// like `host.ip`, ... — even though the same intent is easy to express with
// the raw query DSL. So instead of delegating to ES SQL (or its
// `_sql/translate` cousin, which lives in the same engine), we parse the
// SQL ourselves with `sqlparser-rs` and emit a `_search` body.
use serde_json::{json, Map, Value as Json};
use sqlparser::ast::{
BinaryOperator, Expr, OrderByExpr, OrderByKind, Query, Select, SelectItem, SetExpr, Statement, TableFactor,
UnaryOperator, Value as SqlValue,
};
use sqlparser::dialect::GenericDialect;
use sqlparser::parser::Parser;
const AUTO_PAGED_SELECT_STAR_SIZE: usize = 100;
pub struct TranslatedSelectStar {
pub index: String,
pub body: Json,
/// User wrote `LIMIT N` (no OFFSET) — explicit row cap.
pub user_limited: bool,
/// SQL had OFFSET — signals this came from the pagination plan.
pub from_plan_pagination: bool,
}
/// Try to translate a `SELECT * FROM <index> [WHERE …] [ORDER BY …]
/// [LIMIT N] [OFFSET M]` statement into a `_search` body.
///
/// Returns `Ok(None)` if the statement isn't a recognised `SELECT *` shape
/// (caller should fall back to a different path). Returns `Err` if it IS a
/// `SELECT *` but a clause is unsupported (so the caller surfaces a clear
/// error rather than silently downgrading).
pub fn translate_select_star(sql: &str) -> Result<Option<TranslatedSelectStar>, String> {
let dialect = GenericDialect {};
let statements = Parser::parse_sql(&dialect, sql).map_err(|e| format!("SQL parse error: {e}"))?;
let Some(stmt) = statements.first() else {
return Ok(None);
};
let Statement::Query(query) = stmt else {
return Ok(None);
};
let SetExpr::Select(select) = query.body.as_ref() else {
return Ok(None);
};
if !is_select_star(select) {
return Ok(None);
}
let Some(source) = single_table_source(select)? else {
return Ok(None);
};
let mut body: Map<String, Json> = Map::new();
if let Some(where_expr) = &select.selection {
body.insert("query".to_string(), translate_predicate(where_expr, source.alias.as_deref())?);
}
if let Some(order_by) = &query.order_by {
if let OrderByKind::Expressions(items) = &order_by.kind {
if let Some(sort) = translate_order_by(items, source.alias.as_deref())? {
body.insert("sort".to_string(), sort);
}
}
}
let (limit, offset) = extract_limit_offset(query)?;
let user_limited = limit.is_some() && offset.is_none();
let from_plan_pagination = offset.is_some();
let effective_size = limit.unwrap_or(AUTO_PAGED_SELECT_STAR_SIZE);
let effective_from = offset.unwrap_or(0);
body.insert("size".to_string(), json!(effective_size));
if effective_from > 0 {
body.insert("from".to_string(), json!(effective_from));
}
Ok(Some(TranslatedSelectStar { index: source.index, body: Json::Object(body), user_limited, from_plan_pagination }))
}
fn is_select_star(select: &Select) -> bool {
select.projection.len() == 1 && matches!(select.projection.first(), Some(SelectItem::Wildcard(_)))
}
struct SelectSource {
index: String,
alias: Option<String>,
}
fn single_table_source(select: &Select) -> Result<Option<SelectSource>, String> {
if select.from.len() != 1 {
return Ok(None);
}
let table = &select.from[0];
if !table.joins.is_empty() {
return Err("JOINs are not supported in Elasticsearch SQL translation".to_string());
}
let TableFactor::Table { name, alias, .. } = &table.relation else {
return Ok(None);
};
let joined = name
.0
.iter()
.map(|part| match part {
sqlparser::ast::ObjectNamePart::Identifier(ident) => ident.value.clone(),
other => format!("{other}"),
})
.collect::<Vec<_>>()
.join(".");
Ok(Some(SelectSource { index: joined, alias: alias.as_ref().map(|alias| alias.name.value.clone()) }))
}
fn translate_predicate(expr: &Expr, table_alias: Option<&str>) -> Result<Json, String> {
match expr {
Expr::Nested(inner) => translate_predicate(inner, table_alias),
Expr::BinaryOp { left, op, right } => translate_binary_op(left, op, right, table_alias),
Expr::Like { negated, expr, pattern, .. } | Expr::ILike { negated, expr, pattern, .. } => {
translate_like(*negated, expr, pattern, table_alias)
}
Expr::InList { expr, list, negated } => translate_in_list(expr, list, *negated, table_alias),
Expr::IsNull(inner) => translate_is_null(inner, false, table_alias),
Expr::IsNotNull(inner) => translate_is_null(inner, true, table_alias),
Expr::Between { expr, negated, low, high } => translate_between(expr, *negated, low, high, table_alias),
Expr::UnaryOp { op: UnaryOperator::Not, expr } => {
let inner = translate_predicate(expr, table_alias)?;
Ok(json!({ "bool": { "must_not": [inner] } }))
}
other => Err(format!("Unsupported WHERE expression: {other}")),
}
}
fn translate_binary_op(
left: &Expr,
op: &BinaryOperator,
right: &Expr,
table_alias: Option<&str>,
) -> Result<Json, String> {
match op {
BinaryOperator::And => {
let mut must = Vec::new();
collect_and(left, &mut must, table_alias)?;
collect_and(right, &mut must, table_alias)?;
Ok(json!({ "bool": { "must": must } }))
}
BinaryOperator::Or => {
let mut should = Vec::new();
collect_or(left, &mut should, table_alias)?;
collect_or(right, &mut should, table_alias)?;
Ok(json!({ "bool": { "should": should, "minimum_should_match": 1 } }))
}
BinaryOperator::Eq => term_clause(left, right, table_alias),
BinaryOperator::NotEq => {
let inner = term_clause(left, right, table_alias)?;
Ok(json!({ "bool": { "must_not": [inner] } }))
}
BinaryOperator::Gt => range_clause(left, right, "gt", table_alias),
BinaryOperator::Lt => range_clause(left, right, "lt", table_alias),
BinaryOperator::GtEq => range_clause(left, right, "gte", table_alias),
BinaryOperator::LtEq => range_clause(left, right, "lte", table_alias),
other => Err(format!("Unsupported operator: {other}")),
}
}
fn collect_and(expr: &Expr, out: &mut Vec<Json>, table_alias: Option<&str>) -> Result<(), String> {
if let Expr::BinaryOp { left, op: BinaryOperator::And, right } = expr {
collect_and(left, out, table_alias)?;
collect_and(right, out, table_alias)?;
} else {
out.push(translate_predicate(expr, table_alias)?);
}
Ok(())
}
fn collect_or(expr: &Expr, out: &mut Vec<Json>, table_alias: Option<&str>) -> Result<(), String> {
if let Expr::BinaryOp { left, op: BinaryOperator::Or, right } = expr {
collect_or(left, out, table_alias)?;
collect_or(right, out, table_alias)?;
} else {
out.push(translate_predicate(expr, table_alias)?);
}
Ok(())
}
fn term_clause(left: &Expr, right: &Expr, table_alias: Option<&str>) -> Result<Json, String> {
let field = field_path(left, table_alias)?;
let value = literal_to_json(right)?;
Ok(json!({ "term": { field: value } }))
}
fn range_clause(left: &Expr, right: &Expr, key: &str, table_alias: Option<&str>) -> Result<Json, String> {
let field = field_path(left, table_alias)?;
let value = literal_to_json(right)?;
Ok(json!({ "range": { field: { key: value } } }))
}
fn translate_like(negated: bool, expr: &Expr, pattern: &Expr, table_alias: Option<&str>) -> Result<Json, String> {
let field = field_path(expr, table_alias)?;
let pattern_str = match literal_string(pattern)? {
Some(s) => s,
None => return Err("LIKE pattern must be a string literal".to_string()),
};
// Optimise the common `prefix%` shape into a `prefix` query — cheaper than
// `wildcard` and supported on any analyzed text field.
let is_prefix = !pattern_str.starts_with('%')
&& pattern_str.ends_with('%')
&& !pattern_str[..pattern_str.len() - 1].contains(['%', '_']);
let query = if is_prefix {
let prefix = &pattern_str[..pattern_str.len() - 1];
json!({ "prefix": { field: prefix } })
} else {
let es_pattern = sql_like_to_wildcard(&pattern_str);
json!({
"wildcard": {
field: { "value": es_pattern, "case_insensitive": true }
}
})
};
if negated {
Ok(json!({ "bool": { "must_not": [query] } }))
} else {
Ok(query)
}
}
fn translate_in_list(expr: &Expr, list: &[Expr], negated: bool, table_alias: Option<&str>) -> Result<Json, String> {
let field = field_path(expr, table_alias)?;
let values: Vec<Json> = list.iter().map(literal_to_json).collect::<Result<_, _>>()?;
let query = json!({ "terms": { field: values } });
if negated {
Ok(json!({ "bool": { "must_not": [query] } }))
} else {
Ok(query)
}
}
fn translate_is_null(expr: &Expr, negated: bool, table_alias: Option<&str>) -> Result<Json, String> {
let field = field_path(expr, table_alias)?;
let exists = json!({ "exists": { "field": field } });
if negated {
// IS NOT NULL → field exists
Ok(exists)
} else {
Ok(json!({ "bool": { "must_not": [exists] } }))
}
}
fn translate_between(
expr: &Expr,
negated: bool,
low: &Expr,
high: &Expr,
table_alias: Option<&str>,
) -> Result<Json, String> {
let field = field_path(expr, table_alias)?;
let lo = literal_to_json(low)?;
let hi = literal_to_json(high)?;
let query = json!({ "range": { field: { "gte": lo, "lte": hi } } });
if negated {
Ok(json!({ "bool": { "must_not": [query] } }))
} else {
Ok(query)
}
}
fn translate_order_by(items: &[OrderByExpr], table_alias: Option<&str>) -> Result<Option<Json>, String> {
let mut sort = Vec::new();
for item in items {
let field = field_path(&item.expr, table_alias)?;
let order = if item.options.asc.unwrap_or(true) { "asc" } else { "desc" };
sort.push(json!({ field: { "order": order } }));
}
if sort.is_empty() {
Ok(None)
} else {
Ok(Some(Json::Array(sort)))
}
}
fn extract_limit_offset(query: &Query) -> Result<(Option<usize>, Option<usize>), String> {
let limit = match &query.limit_clause {
Some(sqlparser::ast::LimitClause::LimitOffset { limit, .. }) => {
limit.as_ref().map(usize_literal).transpose()?
}
_ => None,
};
let offset = match &query.limit_clause {
Some(sqlparser::ast::LimitClause::LimitOffset { offset: Some(off), .. }) => Some(usize_literal(&off.value)?),
_ => None,
};
Ok((limit, offset))
}
fn usize_literal(expr: &Expr) -> Result<usize, String> {
match expr {
Expr::Value(v) => match &v.value {
SqlValue::Number(n, _) => n.parse::<usize>().map_err(|e| format!("Invalid number: {e}")),
other => Err(format!("Expected integer literal, got {other:?}")),
},
other => Err(format!("Expected integer literal, got {other}")),
}
}
fn field_path(expr: &Expr, table_alias: Option<&str>) -> Result<String, String> {
match expr {
Expr::Identifier(ident) => Ok(ident.value.clone()),
Expr::CompoundIdentifier(parts) => {
let mut field_parts = parts.iter().map(|p| p.value.clone()).collect::<Vec<_>>();
if let (Some(alias), Some(first)) = (table_alias, field_parts.first()) {
// Dotted Elasticsearch fields are valid document paths; only
// strip a known SQL table alias such as `ass.agencyCode`.
if first.eq_ignore_ascii_case(alias) {
field_parts.remove(0);
}
}
Ok(field_parts.join("."))
}
other => Err(format!("Expected column name, got {other}")),
}
}
fn literal_to_json(expr: &Expr) -> Result<Json, String> {
let Expr::Value(v) = expr else {
return Err(format!("Expected literal value, got {expr}"));
};
match &v.value {
SqlValue::SingleQuotedString(s) | SqlValue::DoubleQuotedString(s) => Ok(Json::String(s.clone())),
SqlValue::Number(n, _) => serde_json::from_str::<Json>(n).map_err(|e| format!("Invalid number {n}: {e}")),
SqlValue::Boolean(b) => Ok(Json::Bool(*b)),
SqlValue::Null => Ok(Json::Null),
other => Err(format!("Unsupported literal: {other:?}")),
}
}
fn literal_string(expr: &Expr) -> Result<Option<String>, String> {
let Expr::Value(v) = expr else {
return Err(format!("Expected string literal, got {expr}"));
};
Ok(match &v.value {
SqlValue::SingleQuotedString(s) | SqlValue::DoubleQuotedString(s) => Some(s.clone()),
_ => None,
})
}
/// Convert SQL LIKE pattern to ES wildcard pattern.
/// SQL: `%` = any chars, `_` = single char. Backslash escapes both.
/// ES wildcard: `*` = any chars, `?` = single char. `\` escapes `*` / `?` / `\`.
fn sql_like_to_wildcard(pattern: &str) -> String {
let mut out = String::with_capacity(pattern.len());
let mut chars = pattern.chars().peekable();
while let Some(ch) = chars.next() {
match ch {
'\\' => {
if let Some(&next) = chars.peek() {
out.push(next);
chars.next();
}
}
'%' => out.push('*'),
'_' => out.push('?'),
'*' | '?' => {
// Escape ES wildcard metacharacters that the user wrote literally.
out.push('\\');
out.push(ch);
}
_ => out.push(ch),
}
}
out
}
#[cfg(test)]
mod tests {
use super::translate_select_star;
use serde_json::json;
#[test]
fn translate_select_star_strips_table_alias_from_where_fields() {
let translated = translate_select_star(
"SELECT * FROM assets002 AS ass WHERE ass.agencyCode = '035021' AND ass.mofDivCode = '420108000'",
)
.unwrap()
.unwrap();
assert_eq!(translated.index, "assets002");
assert_eq!(
translated.body["query"],
json!({
"bool": {
"must": [
{ "term": { "agencyCode": "035021" } },
{ "term": { "mofDivCode": "420108000" } }
]
}
})
);
}
#[test]
fn translate_select_star_strips_table_alias_before_nested_field_path() {
let translated =
translate_select_star("SELECT * FROM assets002 ass WHERE ass.department.code = 'A01'").unwrap().unwrap();
assert_eq!(translated.body["query"], json!({ "term": { "department.code": "A01" } }));
}
#[test]
fn translate_select_star_preserves_unaliased_dotted_document_fields() {
let translated = translate_select_star("SELECT * FROM logs WHERE host.ip = '127.0.0.1'").unwrap().unwrap();
assert_eq!(translated.body["query"], json!({ "term": { "host.ip": "127.0.0.1" } }));
}
#[test]
fn translate_select_star_strips_alias_in_like_and_order_by() {
let translated = translate_select_star(
"SELECT * FROM assets002 ass WHERE ass.assetName LIKE 'car%' ORDER BY ass.createdAt DESC LIMIT 10",
)
.unwrap()
.unwrap();
assert_eq!(translated.body["query"], json!({ "prefix": { "assetName": "car" } }));
assert_eq!(translated.body["sort"], json!([{ "createdAt": { "order": "desc" } }]));
assert_eq!(translated.body["size"], json!(10));
}
}
-130
View File
@@ -1,130 +0,0 @@
use std::path::Path;
use crate::path_utils::expand_tilde;
/// Validates a file path for database connections.
///
/// Performs comprehensive checks including:
/// - Empty path validation
/// - Null character detection
/// - File existence (for local paths)
/// - File type validation (must be a file, not directory)
/// - Network path detection (skips validation for network paths)
///
/// # Arguments
/// * `path` - The file path to validate
/// * `is_network_path` - Closure to determine if path is a network path
///
/// # Returns
/// * `Ok(())` if validation passes
/// * `Err(String)` with descriptive error message if validation fails
pub fn validate_file_path<F>(path: &str, is_network_path: F) -> Result<(), String>
where
F: Fn(&str) -> bool,
{
// Check if path is empty
if path.is_empty() {
return Err("File path cannot be empty".to_string());
}
// Check if path contains invalid characters
if path.contains('\0') {
return Err("File path contains null characters".to_string());
}
let expanded = expand_tilde(path);
let path_obj = Path::new(&expanded);
// For non-network paths, perform file system checks
if !is_network_path(path) {
if !path_obj.exists() {
return Err(format!("File does not exist: {}", path));
}
// Check if path is actually a file, not a directory
if path_obj.is_dir() {
return Err(format!("File path is a directory, not a file: {}", path));
}
// Check if path is a valid file
if !path_obj.is_file() {
return Err(format!("File path is not a valid file: {}", path));
}
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
fn is_network_path_test(path: &str) -> bool {
path.starts_with("\\\\") || path.starts_with("//")
}
#[test]
fn test_empty_path() {
let result = validate_file_path("", is_network_path_test);
assert!(result.is_err());
assert!(result.unwrap_err().contains("empty"));
}
#[test]
fn test_null_character() {
let result = validate_file_path("path\0invalid", is_network_path_test);
assert!(result.is_err());
assert!(result.unwrap_err().contains("null"));
}
#[test]
fn test_network_path_skips_validation() {
let result = validate_file_path("//network/path/nonexistent.db", is_network_path_test);
assert!(result.is_ok());
}
#[test]
fn test_nonexistent_local_file() {
let result = validate_file_path("/nonexistent/path/to/file.db", is_network_path_test);
assert!(result.is_err());
assert!(result.unwrap_err().contains("does not exist"));
}
#[test]
fn test_tilde_expands_to_home() {
let home = std::env::var("HOME").unwrap_or_default();
if home.is_empty() {
return; // skip if no HOME
}
let result = validate_file_path(&format!("{}/.bashrc", home), is_network_path_test);
// $HOME/.bashrc may or may not exist, but the path shouldn't contain literal ~
// We just verify no error mentioning tilde
if let Err(e) = &result {
assert!(!e.contains('~'), "Error should not contain literal tilde: {e}");
}
}
#[test]
fn test_tilde_only() {
let home = std::env::var("HOME").unwrap_or_default();
if home.is_empty() {
return;
}
let result = validate_file_path("~", |_| true); // treat as network to skip fs check
assert!(result.is_ok());
}
#[test]
fn test_no_tilde_unchanged() {
let result = validate_file_path("/absolute/path", is_network_path_test);
assert!(result.is_err());
assert!(result.unwrap_err().contains("/absolute/path"));
}
#[test]
fn test_unknown_named_user_path_remains_literal() {
let result = validate_file_path("~dbx-user-that-must-not-exist-7088", is_network_path_test);
assert!(result.is_err());
assert!(result.unwrap_err().contains('~'));
}
}
-441
View File
@@ -1,441 +0,0 @@
use bytes::Bytes;
use reqwest::header::{HeaderValue, AUTHORIZATION, CONTENT_TYPE};
use reqwest::{Client, StatusCode, Url};
use std::collections::HashMap;
use std::sync::Arc;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::{TcpListener, TcpStream};
use tokio::sync::Mutex;
use tokio::task::JoinHandle;
use tokio::time::{timeout, Duration};
use uuid::Uuid;
const DEFAULT_CONNECT_TIMEOUT: Duration = Duration::from_secs(10);
const TCP_READ_BUFFER_SIZE: usize = 16 * 1024;
const HTTP_READ_WAIT_MS: u64 = 1_000;
const HTTP_REQUEST_TIMEOUT: Duration = Duration::from_secs(15);
#[derive(Default)]
pub struct HttpTunnelManager {
tunnels: Mutex<HashMap<String, (JoinHandle<()>, u16)>>,
}
impl HttpTunnelManager {
pub fn new() -> Self {
Self { tunnels: Mutex::new(HashMap::new()) }
}
pub async fn start_tunnel(
&self,
connection_id: &str,
tunnel_url: &str,
token: &str,
connect_timeout_secs: u64,
remote_host: &str,
remote_port: u16,
) -> Result<u16, String> {
if let Some(local_port) = self.local_port(connection_id).await {
return Ok(local_port);
}
let local_port = portpicker::pick_unused_port().ok_or("No available port")?;
let listener = TcpListener::bind(("127.0.0.1", local_port))
.await
.map_err(|e| format!("Failed to bind HTTP tunnel local port: {e}"))?;
let endpoint =
Arc::new(HttpTunnelEndpoint::new(tunnel_url, token, connect_timeout_secs, remote_host, remote_port)?);
let handle = tokio::spawn(http_tunnel_forward_loop(listener, endpoint));
let mut tunnels = self.tunnels.lock().await;
if let Some((_, existing_port)) = tunnels.get(connection_id) {
handle.abort();
return Ok(*existing_port);
}
tunnels.insert(connection_id.to_string(), (handle, local_port));
Ok(local_port)
}
pub async fn local_port(&self, connection_id: &str) -> Option<u16> {
self.tunnels.lock().await.get(connection_id).map(|(_, port)| *port)
}
pub async fn stop_tunnel(&self, connection_id: &str) {
if let Some((handle, _)) = self.tunnels.lock().await.remove(connection_id) {
handle.abort();
}
}
pub async fn stop_tunnels_with_prefix(&self, connection_id_prefix: &str) {
let mut tunnels = self.tunnels.lock().await;
let keys: Vec<String> = tunnels.keys().filter(|key| key.starts_with(connection_id_prefix)).cloned().collect();
for key in keys {
if let Some((handle, _)) = tunnels.remove(&key) {
handle.abort();
}
}
}
pub async fn stop_all_tunnels(&self) {
let tunnels = std::mem::take(&mut *self.tunnels.lock().await);
for (handle, _) in tunnels.into_values() {
handle.abort();
}
}
}
struct HttpTunnelEndpoint {
url: String,
token: String,
connect_timeout: Duration,
target_host: String,
target_port: u16,
client: Client,
}
impl HttpTunnelEndpoint {
fn new(
tunnel_url: &str,
token: &str,
connect_timeout_secs: u64,
remote_host: &str,
remote_port: u16,
) -> Result<Self, String> {
validate_script_url(tunnel_url)?;
Ok(Self {
url: tunnel_url.trim().to_string(),
token: token.trim().to_string(),
connect_timeout: effective_connect_timeout(connect_timeout_secs),
target_host: remote_host.to_string(),
target_port: remote_port,
client: Client::new(),
})
}
}
async fn http_tunnel_forward_loop(listener: TcpListener, endpoint: Arc<HttpTunnelEndpoint>) {
loop {
let (inbound, _) = match listener.accept().await {
Ok(pair) => pair,
Err(_) => break,
};
let endpoint = endpoint.clone();
tokio::spawn(async move {
let _ = bridge_tcp_to_http_script(inbound, endpoint).await;
});
}
}
async fn bridge_tcp_to_http_script(mut inbound: TcpStream, endpoint: Arc<HttpTunnelEndpoint>) -> Result<(), String> {
let session = Uuid::new_v4().simple().to_string();
open_session(&endpoint, &session).await?;
let (mut tcp_reader, mut tcp_writer) = inbound.split();
let tcp_to_http = async {
let mut buf = vec![0_u8; TCP_READ_BUFFER_SIZE];
loop {
let n = tcp_reader.read(&mut buf).await.map_err(|e| format!("Failed to read local TCP stream: {e}"))?;
if n == 0 {
return Ok::<(), String>(());
}
write_session(&endpoint, &session, Bytes::copy_from_slice(&buf[..n])).await?;
}
};
let http_to_tcp = async {
loop {
let bytes = read_session(&endpoint, &session).await?;
if !bytes.is_empty() {
tcp_writer.write_all(&bytes).await.map_err(|e| format!("Failed to write local TCP stream: {e}"))?;
}
}
};
let result = tokio::select! {
result = tcp_to_http => result,
result = http_to_tcp => result,
};
let _ = close_session(&endpoint, &session).await;
result
}
async fn open_session(endpoint: &HttpTunnelEndpoint, session: &str) -> Result<(), String> {
let url = script_url(
&endpoint.url,
"open",
session,
Some((&endpoint.target_host, endpoint.target_port)),
Some(endpoint.connect_timeout.as_secs()),
None,
)?;
let response = timeout(
endpoint.connect_timeout + HTTP_REQUEST_TIMEOUT,
authorized_request(endpoint, endpoint.client.post(url)).send(),
)
.await
.map_err(|_| "HTTP tunnel script open timed out".to_string())?
.map_err(|e| format!("Failed to open HTTP tunnel script session: {e}"))?;
ensure_success(response, "open HTTP tunnel script session").await
}
async fn write_session(endpoint: &HttpTunnelEndpoint, session: &str, data: Bytes) -> Result<(), String> {
let url = script_url(&endpoint.url, "write", session, None, None, None)?;
let response = timeout(
HTTP_REQUEST_TIMEOUT,
authorized_request(endpoint, endpoint.client.post(url))
.header(CONTENT_TYPE, "application/octet-stream")
.body(data)
.send(),
)
.await
.map_err(|_| "HTTP tunnel script write timed out".to_string())?
.map_err(|e| format!("Failed to write HTTP tunnel script session: {e}"))?;
ensure_success(response, "write HTTP tunnel script session").await
}
async fn read_session(endpoint: &HttpTunnelEndpoint, session: &str) -> Result<Bytes, String> {
let url = script_url(&endpoint.url, "read", session, None, None, Some(HTTP_READ_WAIT_MS))?;
let response = timeout(HTTP_REQUEST_TIMEOUT, authorized_request(endpoint, endpoint.client.post(url)).send())
.await
.map_err(|_| "HTTP tunnel script read timed out".to_string())?
.map_err(|e| format!("Failed to read HTTP tunnel script session: {e}"))?;
let status = response.status();
if status == StatusCode::NO_CONTENT {
return Ok(Bytes::new());
}
if status == StatusCode::OK {
return response.bytes().await.map_err(|e| format!("Failed to read HTTP tunnel script response: {e}"));
}
if status == StatusCode::GONE {
return Err("HTTP tunnel script session closed".to_string());
}
Err(error_response_message(response, "read HTTP tunnel script session").await)
}
async fn close_session(endpoint: &HttpTunnelEndpoint, session: &str) -> Result<(), String> {
let url = script_url(&endpoint.url, "close", session, None, None, None)?;
let response = timeout(HTTP_REQUEST_TIMEOUT, authorized_request(endpoint, endpoint.client.post(url)).send())
.await
.map_err(|_| "HTTP tunnel script close timed out".to_string())?
.map_err(|e| format!("Failed to close HTTP tunnel script session: {e}"))?;
ensure_success(response, "close HTTP tunnel script session").await
}
fn authorized_request(endpoint: &HttpTunnelEndpoint, builder: reqwest::RequestBuilder) -> reqwest::RequestBuilder {
if endpoint.token.is_empty() {
return builder;
}
match HeaderValue::from_str(&endpoint.token) {
Ok(token) => builder
.header("X-DBX-Tunnel-Token", token.clone())
.header(AUTHORIZATION, format!("Bearer {}", endpoint.token)),
Err(_) => builder,
}
}
async fn ensure_success(response: reqwest::Response, action: &str) -> Result<(), String> {
if response.status().is_success() {
Ok(())
} else {
Err(error_response_message(response, action).await)
}
}
async fn error_response_message(response: reqwest::Response, action: &str) -> String {
let status = response.status();
let body = response.text().await.unwrap_or_default();
if body.trim().is_empty() {
format!("Failed to {action}: HTTP {status}")
} else {
format!("Failed to {action}: HTTP {status}: {}", body.trim())
}
}
fn script_url(
tunnel_url: &str,
action: &str,
session: &str,
target: Option<(&str, u16)>,
connect_timeout_secs: Option<u64>,
wait_ms: Option<u64>,
) -> Result<Url, String> {
let mut url = validate_script_url(tunnel_url)?;
url.query_pairs_mut().append_pair("dbx_action", action).append_pair("dbx_session", session);
if let Some((host, port)) = target {
url.query_pairs_mut().append_pair("dbx_target_host", host).append_pair("dbx_target_port", &port.to_string());
}
if let Some(connect_timeout_secs) = connect_timeout_secs {
url.query_pairs_mut()
.append_pair("dbx_connect_timeout", &effective_connect_timeout_secs(connect_timeout_secs).to_string());
}
if let Some(wait_ms) = wait_ms {
url.query_pairs_mut().append_pair("dbx_wait_ms", &wait_ms.to_string());
}
Ok(url)
}
fn validate_script_url(tunnel_url: &str) -> Result<Url, String> {
let trimmed = tunnel_url.trim();
if trimmed.is_empty() {
return Err("HTTP tunnel script URL is required".to_string());
}
let url = Url::parse(trimmed).map_err(|e| format!("HTTP tunnel script URL is invalid: {e}"))?;
match url.scheme() {
"http" | "https" => Ok(url),
other => Err(format!("Unsupported HTTP tunnel script URL scheme: {other}")),
}
}
fn effective_connect_timeout(value: u64) -> Duration {
Duration::from_secs(effective_connect_timeout_secs(value))
}
fn effective_connect_timeout_secs(value: u64) -> u64 {
if value == 0 {
DEFAULT_CONNECT_TIMEOUT.as_secs()
} else {
value.clamp(1, 300)
}
}
#[cfg(test)]
mod tests {
use super::{script_url, validate_script_url, HttpTunnelManager};
use std::sync::Arc;
use tokio::io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader};
use tokio::net::{TcpListener, TcpStream};
use tokio::sync::{Mutex, Notify};
use tokio::time::{timeout, Duration};
#[test]
fn script_url_preserves_existing_query_and_appends_action() {
let url = script_url(
"https://gateway.example.com/dbx_tunnel.php?keep=1",
"open",
"session-1",
Some(("mysql.internal", 3306)),
Some(10),
None,
)
.unwrap();
assert_eq!(
url.as_str(),
"https://gateway.example.com/dbx_tunnel.php?keep=1&dbx_action=open&dbx_session=session-1&dbx_target_host=mysql.internal&dbx_target_port=3306&dbx_connect_timeout=10"
);
}
#[test]
fn script_url_rejects_non_http_schemes() {
let err = validate_script_url("file:///tmp/tunnel.php").unwrap_err();
assert!(err.contains("Unsupported HTTP tunnel script URL scheme"));
}
#[tokio::test]
async fn manager_forwards_sequential_round_trips_and_closes_session() {
let script = MockScript::start().await;
let manager = HttpTunnelManager::new();
let local_port = manager.start_tunnel("test", &script.url, "secret", 5, "mysql.internal", 3306).await.unwrap();
let mut client = TcpStream::connect(("127.0.0.1", local_port)).await.unwrap();
let close_notification = script.closed.notified();
timeout(Duration::from_secs(10), async {
for index in 0..100_u32 {
let payload = format!("request-{index:03}-{}", "x".repeat((index % 17) as usize));
client.write_all(payload.as_bytes()).await.unwrap();
let mut response = vec![0_u8; payload.len()];
client.read_exact(&mut response).await.unwrap();
assert_eq!(response, payload.as_bytes());
}
})
.await
.expect("100 sequential HTTP tunnel round trips timed out");
drop(client);
timeout(Duration::from_secs(2), close_notification).await.expect("HTTP tunnel session was not closed");
manager.stop_tunnel("test").await;
script.handle.abort();
}
struct MockScript {
url: String,
closed: Arc<Notify>,
handle: tokio::task::JoinHandle<()>,
}
impl MockScript {
async fn start() -> Self {
let listener = TcpListener::bind(("127.0.0.1", 0)).await.unwrap();
let addr = listener.local_addr().unwrap();
let buffered = Arc::new(Mutex::new(Vec::<u8>::new()));
let closed = Arc::new(Notify::new());
let handle = {
let buffered = buffered.clone();
let closed = closed.clone();
tokio::spawn(async move {
loop {
let Ok((stream, _)) = listener.accept().await else {
break;
};
let buffered = buffered.clone();
let closed = closed.clone();
tokio::spawn(async move {
handle_mock_http_request(stream, buffered, closed).await;
});
}
})
};
Self { url: format!("http://{addr}/dbx_tunnel.php"), closed, handle }
}
}
async fn handle_mock_http_request(stream: TcpStream, buffered: Arc<Mutex<Vec<u8>>>, closed: Arc<Notify>) {
let mut reader = BufReader::new(stream);
let mut request_line = String::new();
reader.read_line(&mut request_line).await.unwrap();
let mut content_length = 0_usize;
loop {
let mut line = String::new();
reader.read_line(&mut line).await.unwrap();
let trimmed = line.trim_end();
if trimmed.is_empty() {
break;
}
let lower = trimmed.to_ascii_lowercase();
if let Some(value) = lower.strip_prefix("content-length:") {
content_length = value.trim().parse().unwrap();
}
}
let mut body = vec![0_u8; content_length];
reader.read_exact(&mut body).await.unwrap();
let mut stream = reader.into_inner();
if request_line.contains("dbx_action=write") {
buffered.lock().await.extend_from_slice(&body);
write_http_response(&mut stream, "200 OK", b"OK").await;
} else if request_line.contains("dbx_action=read") {
let bytes = {
let mut buffered = buffered.lock().await;
std::mem::take(&mut *buffered)
};
if bytes.is_empty() {
write_http_response(&mut stream, "204 No Content", b"").await;
} else {
write_http_response(&mut stream, "200 OK", &bytes).await;
}
} else if request_line.contains("dbx_action=close") {
write_http_response(&mut stream, "200 OK", b"OK").await;
closed.notify_one();
} else {
write_http_response(&mut stream, "200 OK", b"OK").await;
}
}
async fn write_http_response(stream: &mut TcpStream, status: &str, body: &[u8]) {
let header = format!("HTTP/1.1 {status}\r\nContent-Length: {}\r\nConnection: close\r\n\r\n", body.len());
stream.write_all(header.as_bytes()).await.unwrap();
stream.write_all(body).await.unwrap();
}
}
@@ -1,270 +0,0 @@
use mysql_async::prelude::*;
use std::collections::HashMap;
use crate::models::connection::{ConnectionConfig, DatabaseType};
use crate::types::{ColumnInfo, IndexInfo, ObjectInfo};
use super::mysql::{self, MySqlPool};
pub fn is_config(config: &ConnectionConfig) -> bool {
is_profile(&config.db_type, config.driver_profile.as_deref())
}
pub fn is_profile(db_type: &DatabaseType, driver_profile: Option<&str>) -> bool {
*db_type == DatabaseType::ManticoreSearch
|| (*db_type == DatabaseType::Mysql
&& driver_profile.is_some_and(|profile| profile.eq_ignore_ascii_case("manticoresearch")))
}
fn quote_identifier(value: &str) -> String {
format!("`{}`", value.replace('`', "``"))
}
fn row_get<T, I>(row: &mysql_async::Row, index: I) -> Option<T>
where
T: mysql_async::prelude::FromValue,
I: mysql_async::prelude::ColumnIndex,
{
row.get_opt::<T, I>(index).and_then(|result| result.ok())
}
fn get_str_by_name(row: &mysql_async::Row, name: &str) -> String {
row_get::<String, _>(row, name)
.or_else(|| row_get::<Vec<u8>, _>(row, name).map(|bytes| String::from_utf8_lossy(&bytes).to_string()))
.unwrap_or_default()
}
fn get_opt_str(row: &mysql_async::Row, name: &str) -> Option<String> {
row_get::<String, _>(row, name)
.or_else(|| row_get::<Vec<u8>, _>(row, name).map(|bytes| String::from_utf8_lossy(&bytes).to_string()))
}
fn preferred_metadata(primary: Option<String>, fallback: Option<String>) -> Option<String> {
primary.filter(|value| !value.trim().is_empty()).or_else(|| fallback.filter(|value| !value.trim().is_empty()))
}
pub async fn list_objects(pool: &MySqlPool, database: &str) -> Result<Vec<ObjectInfo>, String> {
let (tables, functions) = tokio::join!(mysql::list_tables_show(pool, database), list_udf_objects(pool, database));
let mut objects: Vec<ObjectInfo> = tables?
.into_iter()
.map(|table| ObjectInfo {
name: table.name,
object_type: "TABLE".to_string(),
schema: Some(database.to_string()),
valid: None,
signature: None,
custom_type_kind: None,
has_members: None,
comment: table.comment,
created_at: None,
updated_at: None,
parent_schema: table.parent_schema,
parent_name: table.parent_name,
trigger: None,
xugu_type_members_expandable: None,
})
.collect();
match functions {
Ok(functions) => objects.extend(functions),
Err(err) => log::warn!("Skipping UDFs for Manticore Search database `{}` in object browser: {}", database, err),
}
Ok(objects)
}
async fn list_udf_objects(pool: &MySqlPool, database: &str) -> Result<Vec<ObjectInfo>, String> {
let mut conn = pool.get_conn().await.map_err(|err| err.to_string())?;
let result = conn.query_iter("SHOW PLUGINS").await.map_err(|err| err.to_string())?;
let rows: Vec<mysql_async::Row> = result.collect_and_drop().await.map_err(|err| err.to_string())?;
Ok(rows
.iter()
.filter_map(|row| {
plugin_object(
&get_str_by_name(row, "Type"),
&get_str_by_name(row, "Name"),
get_opt_str(row, "Library").as_deref(),
get_opt_str(row, "Extra").as_deref(),
database,
)
})
.collect())
}
fn plugin_object(
plugin_type: &str,
name: &str,
library: Option<&str>,
extra: Option<&str>,
database: &str,
) -> Option<ObjectInfo> {
if !plugin_type.eq_ignore_ascii_case("udf") {
return None;
}
let name = name.trim();
if name.is_empty() {
return None;
}
let mut comment_parts = Vec::new();
if let Some(library) = library.map(str::trim).filter(|value| !value.is_empty()) {
comment_parts.push(format!("SONAME {library}"));
}
if let Some(return_type) = extra.map(str::trim).filter(|value| !value.is_empty()) {
comment_parts.push(format!("RETURNS {return_type}"));
}
Some(ObjectInfo {
name: name.to_string(),
object_type: "FUNCTION".to_string(),
schema: Some(database.to_string()),
valid: None,
signature: None,
custom_type_kind: None,
has_members: None,
comment: if comment_parts.is_empty() { None } else { Some(comment_parts.join(", ")) },
created_at: None,
updated_at: None,
parent_schema: None,
parent_name: None,
trigger: None,
xugu_type_members_expandable: None,
})
}
pub async fn get_columns(pool: &MySqlPool, database: &str, table: &str) -> Result<Vec<ColumnInfo>, String> {
let mut columns = mysql::get_columns_show(pool, database, table).await?;
let properties = column_properties(pool, database, table).await.unwrap_or_default();
for column in &mut columns {
if let Some(property) = properties.get(&column.name.to_lowercase()) {
column.extra = preferred_metadata(column.extra.take(), Some(property.clone()));
}
}
Ok(columns)
}
async fn column_properties(pool: &MySqlPool, database: &str, table: &str) -> Result<HashMap<String, String>, String> {
let sql = show_columns_sql(database, table, true);
let mut conn = pool.get_conn().await.map_err(|err| err.to_string())?;
let rows: Vec<mysql_async::Row> = match conn.query_iter(&sql).await {
Ok(result) => result.collect_and_drop().await.map_err(|err| err.to_string())?,
Err(_) => {
let sql = show_columns_sql(database, table, false);
let result = conn.query_iter(&sql).await.map_err(|err| err.to_string())?;
result.collect_and_drop().await.map_err(|err| err.to_string())?
}
};
Ok(rows
.iter()
.filter_map(|row| {
let name = get_str_by_name(row, "Field").trim().to_string();
if name.is_empty() {
return None;
}
get_opt_str(row, "Properties")
.filter(|value| !value.trim().is_empty())
.map(|properties| (name.to_lowercase(), properties))
})
.collect())
}
fn show_columns_sql(database: &str, table: &str, full: bool) -> String {
let prefix = if full { "SHOW FULL COLUMNS FROM" } else { "SHOW COLUMNS FROM" };
if database.trim().is_empty() {
format!("{prefix} {}", quote_identifier(table))
} else {
format!("{prefix} {}.{}", quote_identifier(database), quote_identifier(table))
}
}
pub async fn list_indexes(pool: &MySqlPool, table: &str) -> Result<Vec<IndexInfo>, String> {
let sql = list_indexes_sql(table);
let mut conn = pool.get_conn().await.map_err(|err| err.to_string())?;
let result = conn.query_iter(&sql).await.map_err(|err| err.to_string())?;
let rows: Vec<mysql_async::Row> = result.collect_and_drop().await.map_err(|err| err.to_string())?;
Ok(rows.iter().map(index_info_from_row).collect())
}
fn list_indexes_sql(table: &str) -> String {
format!("SHOW TABLE INDEXES FROM {}", quote_identifier(table))
}
fn index_info_from_row(row: &mysql_async::Row) -> IndexInfo {
let name = get_str_by_name(row, "Name");
let enabled = get_str_by_name(row, "Enabled");
let percent = get_str_by_name(row, "Percent");
let comment_parts: Vec<String> = [
(!enabled.is_empty()).then(|| format!("Enabled: {enabled}")),
(!percent.is_empty()).then(|| format!("Percent: {percent}")),
]
.into_iter()
.flatten()
.collect();
IndexInfo {
name: name.clone(),
columns: vec![name],
is_unique: false,
is_primary: false,
filter: None,
index_type: Some(get_str_by_name(row, "Type")),
included_columns: None,
comment: (!comment_parts.is_empty()).then(|| comment_parts.join(", ")),
key_is_expression: Vec::new(),
column_opclasses: vec![],
key_options: Vec::new(),
constraint_backed: false,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn profile_matches_native_and_mysql_profile_connections() {
assert!(is_profile(&DatabaseType::ManticoreSearch, None));
assert!(is_profile(&DatabaseType::Mysql, Some("MANTICORESEARCH")));
assert!(!is_profile(&DatabaseType::Mysql, None));
assert!(!is_profile(&DatabaseType::Postgres, Some("manticoresearch")));
}
#[test]
fn show_metadata_uses_unqualified_table_names_when_database_is_empty() {
assert_eq!(show_columns_sql("", "idx", true), "SHOW FULL COLUMNS FROM `idx`");
}
#[test]
fn column_extra_falls_back_to_properties() {
assert_eq!(
preferred_metadata(Some("auto_increment".to_string()), Some("indexed attribute".to_string())),
Some("auto_increment".to_string())
);
assert_eq!(
preferred_metadata(Some(" ".to_string()), Some("indexed attribute".to_string())),
Some("indexed attribute".to_string())
);
assert_eq!(preferred_metadata(None, Some(" ".to_string())), None);
}
#[test]
fn indexes_sql_uses_show_table_indexes() {
assert_eq!(list_indexes_sql("materials"), "SHOW TABLE INDEXES FROM `materials`");
assert_eq!(list_indexes_sql("odd`name"), "SHOW TABLE INDEXES FROM `odd``name`");
}
#[test]
fn udf_plugin_maps_to_function_object() {
let object = plugin_object("udf", "sequence", Some("udfexample.dll"), Some("INT"), "app").expect("udf plugin");
assert_eq!(object.name, "sequence");
assert_eq!(object.object_type, "FUNCTION");
assert_eq!(object.schema.as_deref(), Some("app"));
assert_eq!(object.comment.as_deref(), Some("SONAME udfexample.dll, RETURNS INT"));
}
#[test]
fn non_udf_plugins_are_not_sidebar_functions() {
assert!(plugin_object("ranker", "bm25", Some("ranker.so"), None, "app").is_none());
assert!(plugin_object("udf", " ", Some("udf.so"), Some("INT"), "app").is_none());
}
}
+18 -341
View File
@@ -1,12 +1,10 @@
pub mod agent_driver;
pub use dbx_driver_agent::agent_driver;
pub mod clickhouse_driver;
pub mod cloudberry;
pub mod cloudflare_d1;
pub use cloudflare_d1 as cloudflare_d1_driver;
pub mod ddl_scan;
pub mod document_result;
pub mod dolt;
pub mod doris;
pub use dbx_driver_mysql::{dolt, doris};
pub use dbx_driver_support::{ddl_scan, document_result};
pub mod duckdb_sql;
#[cfg(feature = "duckdb-sidecar")]
pub mod duckdb_worker_process;
@@ -19,359 +17,38 @@ pub mod dynamodb_driver;
#[path = "dynamodb_driver_disabled.rs"]
pub mod dynamodb_driver;
pub mod easysearch_driver;
pub mod elasticsearch_driver;
pub mod elasticsearch_sql;
pub mod file_validator;
pub use dbx_driver_elasticsearch as elasticsearch_driver;
pub use dbx_driver_support::{elasticsearch_sql, file_validator};
pub mod hbase_driver;
pub mod http_tunnel;
pub use dbx_driver_support::http_tunnel;
pub mod influxdb3_driver;
pub mod influxdb_driver;
pub mod manticoresearch;
pub use dbx_driver_mysql::manticoresearch;
pub mod meilisearch_driver;
pub mod mongo_driver;
pub mod mysql;
pub mod mysql_compatible;
pub mod ob_oracle;
pub mod oceanbase_mysql;
pub use dbx_driver_mongodb::mongo_driver;
pub use dbx_driver_mysql::{mysql, mysql_compatible, ob_oracle, oceanbase_mysql};
pub mod opentenbase;
pub mod postgres;
pub use dbx_driver_postgres as postgres;
pub mod proxy_tunnel;
pub mod questdb;
pub mod redis_driver;
pub use dbx_driver_redis as redis_driver;
pub mod rqlite_driver;
pub mod salesforce_driver;
pub mod solr_driver;
pub mod sqlite;
pub mod sqlite_worker;
pub mod sqlserver;
pub mod ssh_host_key;
pub use dbx_driver_sqlserver as sqlserver;
pub use dbx_driver_support::ssh_host_key;
pub use dbx_platform::ssh_prompt;
pub mod ssh_tunnel;
pub mod starrocks;
pub use dbx_sql::tdsql_mysql;
pub mod tidb;
pub use dbx_driver_mysql::starrocks;
pub use dbx_driver_mysql::tidb;
pub use dbx_sql_core::tdsql_mysql;
pub mod transport_layer_tunnel;
pub mod turso_driver;
pub mod vector_driver;
pub mod victoriametrics_driver;
pub mod wkb;
pub use dbx_driver_support::wkb;
use crate::path_utils::expand_tilde;
use reqwest::ClientBuilder;
use reqwest::{Certificate, Identity};
use std::fmt;
use std::future::Future;
use std::time::Duration;
// Re-export types so that `db::QueryResult` etc. work within dbx-core
pub use crate::mysql_event_sql::MysqlEventInfo;
pub use crate::types::*;
pub use file_validator::validate_file_path;
pub const CONNECTION_TIMEOUT_SECS: u64 = 5;
pub const TCP_PROBE_TIMEOUT_SECS: u64 = 3;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PoolCheckoutStage {
Wait,
Create,
Recycle,
Unknown,
}
impl PoolCheckoutStage {
pub fn as_str(self) -> &'static str {
match self {
Self::Wait => "wait",
Self::Create => "create",
Self::Recycle => "recycle",
Self::Unknown => "unknown",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PoolCheckoutError {
Timeout { database: &'static str, stage: PoolCheckoutStage, timeout: Duration },
Failed { database: &'static str, stage: PoolCheckoutStage, detail: String },
Canceled,
}
impl PoolCheckoutError {
pub fn stage(&self) -> Option<PoolCheckoutStage> {
match self {
Self::Timeout { stage, .. } | Self::Failed { stage, .. } => Some(*stage),
Self::Canceled => None,
}
}
pub fn is_pool_saturation(&self) -> bool {
matches!(self, Self::Timeout { stage: PoolCheckoutStage::Wait, .. })
}
}
impl fmt::Display for PoolCheckoutError {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Timeout { database, stage, timeout } => write!(
formatter,
"{database} connection pool checkout timed out [stage={}, timeout_ms={}]",
stage.as_str(),
timeout.as_millis()
),
Self::Failed { database, stage, detail } => {
write!(formatter, "{database} connection pool checkout failed [stage={}]: {detail}", stage.as_str())
}
Self::Canceled => formatter.write_str(crate::execution::QUERY_CANCELED),
}
}
}
pub fn connection_timeout() -> Duration {
Duration::from_secs(CONNECTION_TIMEOUT_SECS)
}
pub fn http_client_builder(timeout: Duration) -> ClientBuilder {
reqwest::Client::builder().connect_timeout(timeout).no_proxy()
}
/// Augments an HTTP client builder with custom TLS material for drivers that
/// need to trust a non-system CA or present a client certificate (mTLS).
///
/// * `ca_cert_path` adds an extra root certificate (PEM bundle) alongside the
/// system roots, so self-signed server certificates are trusted.
/// * `client_cert_path` + `client_key_path` (required together) are concatenated
/// into a single PEM identity, as expected by reqwest's rustls backend.
///
/// Returns the augmented builder, or a descriptive error if a configured
/// certificate/key file cannot be read or parsed. Callers surface that error
/// instead of silently falling back to the system trust store, so a typo in a
/// cert path fails the connection loudly rather than being ignored.
pub fn apply_tls_certificates(
mut builder: ClientBuilder,
ca_cert_path: Option<&str>,
client_cert_path: Option<&str>,
client_key_path: Option<&str>,
) -> Result<ClientBuilder, String> {
// The desktop build unifies reqwest with both `default-tls` and `rustls-tls`,
// and `TlsBackend` defaults to native-tls there, which rejects the PEM identity
// below with `incompatible TLS identity type`. Pin rustls like the Consul and
// etcd-metrics clients so custom CA roots and PEM client identities work in
// every target.
builder = builder.use_rustls_tls();
if let Some(ca) = ca_cert_path.filter(|path| !path.is_empty()) {
let ca = expand_tilde(ca);
let contents = std::fs::read(&ca)
.map_err(|error| format!("Failed to read Elasticsearch CA certificate at {ca}: {error}"))?;
let certificates = Certificate::from_pem_bundle(&contents)
.map_err(|error| format!("Failed to parse Elasticsearch CA certificate at {ca}: {error}"))?;
for certificate in certificates {
builder = builder.add_root_certificate(certificate);
}
}
if let (Some(cert_path), Some(key_path)) =
(client_cert_path.filter(|path| !path.is_empty()), client_key_path.filter(|path| !path.is_empty()))
{
let cert_path = expand_tilde(cert_path);
let key_path = expand_tilde(key_path);
let mut pem = std::fs::read(&cert_path)
.map_err(|error| format!("Failed to read Elasticsearch client certificate at {cert_path}: {error}"))?;
// reqwest's rustls backend concatenates the cert and key as two PEM
// blocks; ensure they are newline-separated so parsing succeeds even
// when the cert file does not end with a trailing newline.
if !pem.ends_with(b"\n") {
pem.push(b'\n');
}
pem.extend(
std::fs::read(&key_path)
.map_err(|error| format!("Failed to read Elasticsearch client key at {key_path}: {error}"))?,
);
let identity = Identity::from_pem(&pem)
.map_err(|error| format!("Failed to parse Elasticsearch client identity: {error}"))?;
builder = builder.identity(identity);
}
Ok(builder)
}
pub use dbx_types::json::JS_MAX_SAFE_INTEGER;
pub use dbx_types::json::{json_value_for_js, safe_i64_to_json, safe_u64_to_json};
pub fn hex_encode(bytes: &[u8]) -> String {
const HEX: &[u8; 16] = b"0123456789abcdef";
let mut out = String::with_capacity(bytes.len() * 2);
for &byte in bytes {
out.push(HEX[(byte >> 4) as usize] as char);
out.push(HEX[(byte & 0x0f) as usize] as char);
}
out
}
pub fn binary_value_to_json(bytes: &[u8]) -> serde_json::Value {
serde_json::Value::String(format!("0x{}", hex_encode(bytes)))
}
pub fn tcp_probe_timeout() -> Duration {
Duration::from_secs(TCP_PROBE_TIMEOUT_SECS)
}
pub fn parse_connect_timeout(url: &str) -> Duration {
parse_connect_timeout_with_fallback(url, connection_timeout())
}
pub fn parse_connect_timeout_with_fallback(url: &str, fallback: Duration) -> Duration {
let Some(query) = url.split('?').nth(1) else {
return fallback;
};
for param in query.split('&') {
let trimmed = param.trim();
if trimmed.is_empty() {
continue;
}
let (key, value) = match trimmed.split_once('=') {
Some(pair) => pair,
None => continue,
};
if key.eq_ignore_ascii_case("connect_timeout")
|| key.eq_ignore_ascii_case("connectTimeout")
|| key.eq_ignore_ascii_case("connection_timeout")
|| key.eq_ignore_ascii_case("connectionTimeout")
{
if let Ok(v) = value.parse::<u64>() {
if (1..=300).contains(&v) {
return Duration::from_secs(v);
}
}
}
}
fallback
}
pub async fn with_connection_timeout<T, F>(label: &str, timeout: Duration, future: F) -> Result<T, String>
where
F: Future<Output = Result<T, String>>,
{
tokio::time::timeout(timeout, future)
.await
.map_err(|_| format!("{label} connection timed out ({}s)", timeout.as_secs()))?
}
pub async fn probe_tcp_endpoint(label: &str, host: &str, port: u16, timeout: Duration) -> Result<(), String> {
tokio::time::timeout(timeout, tokio::net::TcpStream::connect((host, port)))
.await
.map_err(|_| format!("{label} TCP connection timed out ({}s)", timeout.as_secs()))?
.map(|_| ())
.map_err(|e| format!("{label} TCP connection failed: {e}"))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn binary_values_are_displayed_as_prefixed_hex() {
assert_eq!(binary_value_to_json(&[0x00, 0x01, 0xab, 0xff]), serde_json::json!("0x0001abff"));
}
}
#[cfg(test)]
mod tls_tests {
use super::{apply_tls_certificates, http_client_builder};
use std::path::Path;
use std::process::Command;
use std::time::Duration;
/// Generates a throwaway CA + client certificate/key (PEM) via openssl and
/// returns their paths. If openssl is unavailable the files stay empty, which
/// makes `apply_tls_certificates` surface a read error instead of degrading.
fn gen_certs(dir: &Path) -> (std::path::PathBuf, std::path::PathBuf, std::path::PathBuf) {
let ca_key = dir.join("ca.key");
let ca_pem = dir.join("ca.pem");
let csr = dir.join("client.csr");
let client_key = dir.join("client.key");
let client_crt = dir.join("client.crt");
// Extensions force an X.509 v3 client cert; some toolchains (e.g.
// OpenSSL 3.0 on CI) emit v1 without one, which rustls rejects.
let client_ext = dir.join("client.ext");
let _ = std::fs::write(&client_ext, "basicConstraints=critical,CA:FALSE\n");
let _ = Command::new("openssl")
.args([
"req",
"-x509",
"-newkey",
"rsa:2048",
"-nodes",
"-keyout",
ca_key.to_str().unwrap(),
"-out",
ca_pem.to_str().unwrap(),
"-days",
"1",
"-subj",
"/CN=dbx-test-ca",
])
.output();
let _ = Command::new("openssl")
.args([
"req",
"-newkey",
"rsa:2048",
"-nodes",
"-keyout",
client_key.to_str().unwrap(),
"-out",
csr.to_str().unwrap(),
"-subj",
"/CN=dbx-test-client",
])
.output();
let _ = Command::new("openssl")
.args([
"x509",
"-req",
"-in",
csr.to_str().unwrap(),
"-CA",
ca_pem.to_str().unwrap(),
"-CAkey",
ca_key.to_str().unwrap(),
"-CAcreateserial",
"-out",
client_crt.to_str().unwrap(),
"-days",
"1",
"-extfile",
client_ext.to_str().unwrap(),
])
.output();
(ca_pem, client_crt, client_key)
}
#[test]
fn apply_tls_certificates_builds_with_ca_and_client_cert() {
let dir = std::env::temp_dir().join(format!("dbx-tls-test-{}", std::process::id()));
std::fs::create_dir_all(&dir).unwrap();
let (ca, client_crt, client_key) = gen_certs(&dir);
let built = apply_tls_certificates(
http_client_builder(Duration::from_secs(5)),
ca.to_str(),
client_crt.to_str(),
client_key.to_str(),
)
.expect("client should build with CA + client cert")
.build();
assert!(built.is_ok(), "built client should be usable with CA + client cert: {:?}", built.err());
// A misconfigured CA path must now surface a descriptive error instead of
// silently degrading to the system trust store.
let missing = apply_tls_certificates(
http_client_builder(Duration::from_secs(5)),
Some("/nonexistent/ca.pem"),
None,
None,
);
assert!(missing.is_err(), "missing CA path must surface a read error");
let _ = std::fs::remove_dir_all(&dir);
}
}
pub use dbx_driver_support::db::*;
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
@@ -1,646 +0,0 @@
use mysql_async::prelude::*;
use std::collections::HashSet;
use crate::models::connection::ConnectionConfig;
use crate::types::{ColumnInfo, DatabaseInfo, IndexInfo, TableInfo};
use super::mysql::{
bytes_to_string_lossy, database_infos_from_names, first_nonempty_str_by_name, fix_potential_double_encoding,
get_conn_with_health_check, get_conn_with_timeout, get_opt_str, get_str, get_str_by_name, is_mysql_identifier_byte,
list_indexes, mysql_keyword_at, quote_identifier, show_create_table_ddl, skip_mysql_quoted, MySqlPool,
};
pub fn uses_show_metadata(config: &ConnectionConfig) -> bool {
super::doris::is_config(config) || super::starrocks::is_config(config) || super::manticoresearch::is_config(config)
}
pub fn supports_external_catalogs(config: &ConnectionConfig) -> bool {
super::doris::is_config(config) || super::starrocks::is_config(config)
}
// Doris and StarRocks reuse the MySQL wire protocol, but catalog addressing and DDL/index
// metadata semantics are MySQL-compatible distributed behavior and stay isolated in this module.
pub async fn list_indexes_with_ddl_fallback(
pool: &MySqlPool,
database: &str,
table: &str,
) -> Result<Vec<IndexInfo>, String> {
let statistics_result = list_indexes(pool, database, table).await;
let mut indexes = match &statistics_result {
Ok(indexes) => indexes.clone(),
Err(err) => {
log::debug!(
"Falling back to SHOW CREATE TABLE for MySQL-compatible distributed indexes on `{database}`.`{table}` after information_schema.STATISTICS failed: {err}"
);
Vec::new()
}
};
match show_create_table_ddl(pool, database, table).await {
Ok(ddl) => {
merge_index_infos(&mut indexes, indexes_from_create_table_ddl(&ddl));
Ok(indexes)
}
Err(ddl_err) => {
if indexes.is_empty() {
if let Err(statistics_err) = statistics_result {
return Err(format!(
"{statistics_err}; SHOW CREATE TABLE fallback failed for MySQL-compatible distributed indexes: {ddl_err}"
));
}
}
Ok(indexes)
}
}
}
// ---------------------------------------------------------------------------
// Doris / StarRocks multi-catalog support.
//
// These engines expose external catalogs (iceberg, hive, jdbc, ...) alongside
// the native `internal` catalog via `SHOW CATALOGS`. The functions below address
// objects in a specific catalog using 3-part qualified names
// (`<catalog>.<database>.<table>`), which the engines accept directly without
// needing to `SWITCH` the session catalog.
// ---------------------------------------------------------------------------
/// Build a 2-part qualified identifier `` `<catalog>`.`<database>` ``.
fn catalog_database_ref(catalog: &str, database: &str) -> String {
format!("{}.{}", quote_identifier(catalog), quote_identifier(database))
}
/// Build a 3-part qualified identifier `` `<catalog>`.`<database>`.`<table>` ``.
fn catalog_table_ref(catalog: &str, database: &str, table: &str) -> String {
format!("{}.{}.{}", quote_identifier(catalog), quote_identifier(database), quote_identifier(table))
}
/// `SHOW CATALOGS` → list of catalogs visible to the current user.
///
/// Column layouts differ between engines: Doris exposes `CatalogName` (with
/// `CatalogId`/`IsCurrent`/`CreateTime`/`LastUpdateTime`), while StarRocks
/// exposes `Catalog` (only `Type`/`Comment`, no `IsCurrent`). The name is read
/// from either column; missing trailing columns degrade gracefully to
/// empty/None. The built-in catalog is named `internal` in Doris and
/// `default_catalog` in StarRocks (both with `Type=internal`); detection is
/// type-based (see `CatalogInfo::is_internal`), not name-based.
pub async fn list_catalogs(pool: &MySqlPool) -> Result<Vec<crate::db::CatalogInfo>, String> {
let mut conn = get_conn_with_timeout(pool, super::connection_timeout()).await?;
let result = conn.query_iter("SHOW CATALOGS").await.map_err(|e| e.to_string())?;
let rows: Vec<mysql_async::Row> = result.collect_and_drop().await.map_err(|e| e.to_string())?;
let catalogs: Vec<crate::db::CatalogInfo> = rows
.iter()
.filter_map(|row| {
// Doris column is `CatalogName`; StarRocks column is `Catalog`.
let name = first_nonempty_str_by_name(row, &["CatalogName", "Catalog"]).trim().to_string();
if name.is_empty() {
return None;
}
let catalog_type = get_str_by_name(row, "Type").trim().to_string();
let is_current = {
let value = get_str_by_name(row, "IsCurrent").trim().to_ascii_lowercase();
!value.is_empty() && value != "no" && value != "false" && value != "0"
};
let comment = get_opt_str(row, "Comment").map(|s| s.trim().to_string()).filter(|s| !s.is_empty());
Some(crate::db::CatalogInfo { name, catalog_type, is_current, comment })
})
.collect();
Ok(normalize_catalogs(catalogs))
}
/// Sort with the built-in catalog first, then the rest alphabetically by name.
/// The built-in catalog is identified by `CatalogInfo::is_internal` (type-based)
/// rather than by name, so StarRocks `default_catalog` sorts first just like
/// Doris `internal`. No synthetic catalog is injected: `SHOW CATALOGS` always
/// lists the built-in catalog on both engines, and a single-catalog result is
/// handled by the flat-sidebar fallback in the caller.
fn normalize_catalogs(mut catalogs: Vec<crate::db::CatalogInfo>) -> Vec<crate::db::CatalogInfo> {
catalogs.sort_by(|a, b| match (a.is_internal(), b.is_internal()) {
(true, false) => std::cmp::Ordering::Less,
(false, true) => std::cmp::Ordering::Greater,
_ => a.name.cmp(&b.name),
});
catalogs
}
/// `SHOW DATABASES FROM <catalog>` → databases in the given catalog.
pub async fn list_databases_show_from(pool: &MySqlPool, catalog: &str) -> Result<Vec<DatabaseInfo>, String> {
let mut conn = get_conn_with_timeout(pool, super::connection_timeout()).await?;
let sql = format!("SHOW DATABASES FROM {}", quote_identifier(catalog));
let result = conn.query_iter(&sql).await.map_err(|e| e.to_string())?;
let rows: Vec<mysql_async::Row> = result.collect_and_drop().await.map_err(|e| e.to_string())?;
Ok(database_infos_from_names(rows.iter().map(|row| get_str(row, 0)), false))
}
/// `SHOW TABLES FROM <catalog>.<database>` → tables in an external catalog.
///
/// External catalogs do not support `SHOW TABLE STATUS`, so comments/status are
/// not fetched (the caller only needs names + types for browsing).
pub async fn list_tables_show_from(pool: &MySqlPool, catalog: &str, database: &str) -> Result<Vec<TableInfo>, String> {
let sql = format!("SHOW TABLES FROM {}", catalog_database_ref(catalog, database));
let mut conn = get_conn_with_timeout(pool, super::connection_timeout()).await?;
let result = conn.query_iter(&sql).await.map_err(|e| e.to_string())?;
let rows: Vec<mysql_async::Row> = result.collect_and_drop().await.map_err(|e| e.to_string())?;
let mut tables: Vec<TableInfo> = rows
.iter()
.filter_map(|row| {
let name = get_str(row, 0).trim().to_string();
if name.is_empty() {
return None;
}
// SHOW FULL TABLES exposes a type column; plain SHOW TABLES does not.
let table_type = get_str(row, 1);
Some(TableInfo {
name,
table_type: if table_type.trim().is_empty() { "TABLE".to_string() } else { table_type },
valid: None,
comment: None,
parent_schema: None,
parent_name: None,
})
})
.collect();
tables.sort_by(|a, b| a.name.cmp(&b.name));
Ok(tables)
}
/// `SHOW COLUMNS FROM <catalog>.<database>.<table>` → columns of an external
/// catalog table. Falls back to `DESCRIBE` if `SHOW COLUMNS` is rejected.
pub async fn get_columns_show_from(
pool: &MySqlPool,
catalog: &str,
database: &str,
table: &str,
) -> Result<Vec<ColumnInfo>, String> {
let qualified = catalog_table_ref(catalog, database, table);
let full_sql = format!("SHOW FULL COLUMNS FROM {qualified}");
let plain_sql = format!("SHOW COLUMNS FROM {qualified}");
let describe_sql = format!("DESCRIBE {qualified}");
let mut conn = get_conn_with_health_check(pool).await?;
let rows: Vec<mysql_async::Row> = match conn.query_iter(&full_sql).await {
Ok(result) => result.collect_and_drop().await.map_err(|e| e.to_string())?,
Err(_) => match conn.query_iter(&plain_sql).await {
Ok(result) => result.collect_and_drop().await.map_err(|e| e.to_string())?,
Err(_) => {
let result = conn.query_iter(&describe_sql).await.map_err(|e| e.to_string())?;
result.collect_and_drop().await.map_err(|e| e.to_string())?
}
},
};
Ok(rows
.iter()
.filter_map(|row| {
// MySQL allows leading/trailing spaces in column names, so keep the name verbatim
// and only drop rows whose name is blank, matching `mysql_column_name` in mysql.rs.
let name = get_str_by_name(row, "Field");
if name.trim().is_empty() {
return None;
}
let key = get_str_by_name(row, "Key");
let collation = get_opt_str(row, "Collation").filter(|s| !s.is_empty());
Some(ColumnInfo {
name,
data_type: get_str_by_name(row, "Type"),
resolved_schema: None,
is_nullable: get_str_by_name(row, "Null").eq_ignore_ascii_case("YES"),
column_default: get_opt_str(row, "Default"),
is_primary_key: key.eq_ignore_ascii_case("PRI"),
is_unique: key.eq_ignore_ascii_case("UNI"),
extra: get_opt_str(row, "Extra"),
comment: get_opt_str(row, "Comment")
.map(|s| fix_potential_double_encoding(&s))
.filter(|s| !s.is_empty()),
numeric_precision: None,
numeric_scale: None,
character_maximum_length: None,
enum_values: None,
character_set: collation
.as_deref()
.and_then(|c| c.split_once('_').map(|(charset, _)| charset.to_string()))
.filter(|s| !s.is_empty()),
collation,
metadata_capabilities: None,
})
})
.collect())
}
/// `SHOW CREATE TABLE <catalog>.<database>.<table>` → DDL for an external
/// catalog table.
pub async fn show_create_table_ddl_from(
pool: &MySqlPool,
catalog: &str,
database: &str,
table: &str,
) -> Result<String, String> {
let sql = format!("SHOW CREATE TABLE {}", catalog_table_ref(catalog, database, table));
let mut conn = get_conn_with_health_check(pool).await?;
let result = conn.query_iter(&sql).await.map_err(|e| e.to_string())?;
let rows: Vec<mysql_async::Row> = result.collect_and_drop().await.map_err(|e| e.to_string())?;
let row = rows.first().ok_or("DDL not found")?;
row.get_opt::<String, usize>(1)
.and_then(|result| result.ok())
.or_else(|| row.get_opt::<Vec<u8>, usize>(1).and_then(|result| result.ok()).map(bytes_to_string_lossy))
.ok_or_else(|| "Failed to read DDL".to_string())
}
/// Best-effort index listing for an external catalog table. External catalogs
/// generally do not expose MySQL-style index metadata via `information_schema`
/// (that view is scoped to the internal catalog), so indexes are derived from
/// `SHOW CREATE TABLE` parsing. Returns empty on failure (graceful degradation
/// — indexes are informational for external tables).
pub async fn list_catalog_indexes(
pool: &MySqlPool,
catalog: &str,
database: &str,
table: &str,
) -> Result<Vec<IndexInfo>, String> {
let ddl = show_create_table_ddl_from(pool, catalog, database, table).await?;
Ok(indexes_from_create_table_ddl(&ddl))
}
fn merge_index_infos(target: &mut Vec<IndexInfo>, parsed: Vec<IndexInfo>) {
let mut seen_names: HashSet<String> = target.iter().map(|index| index.name.to_ascii_lowercase()).collect();
for index in parsed {
if index.columns.is_empty() {
continue;
}
if seen_names.contains(&index.name.to_ascii_lowercase())
|| target.iter().any(|existing| {
existing.is_unique == index.is_unique
&& existing.is_primary == index.is_primary
&& existing.columns == index.columns
})
{
continue;
}
seen_names.insert(index.name.to_ascii_lowercase());
target.push(index);
}
}
fn indexes_from_create_table_ddl(ddl: &str) -> Vec<IndexInfo> {
let mut indexes = Vec::new();
for raw_line in ddl.lines() {
let line = trim_ddl_definition_line(raw_line);
if line.is_empty() {
continue;
}
let upper = line.to_ascii_uppercase();
if upper.starts_with("PRIMARY KEY") {
if let Some(index) = table_key_index("PRIMARY", line, true, true, "PRIMARY KEY") {
indexes.push(index);
}
} else if upper.starts_with("UNIQUE KEY") {
if let Some(index) = table_key_index("UNIQUE KEY", line, true, false, "UNIQUE KEY") {
indexes.push(index);
}
} else if upper.starts_with("INDEX ") {
if let Some(index) = secondary_index(line) {
indexes.push(index);
}
}
}
indexes
}
fn trim_ddl_definition_line(line: &str) -> &str {
let mut trimmed = line.trim();
if let Some(rest) = trimmed.strip_prefix(',') {
trimmed = rest.trim_start();
}
while let Some(rest) = trimmed.strip_suffix(',') {
trimmed = rest.trim_end();
}
trimmed
}
fn table_key_index(name: &str, line: &str, is_unique: bool, is_primary: bool, index_type: &str) -> Option<IndexInfo> {
let columns = parse_mysql_index_columns(first_parenthesized_content(line)?);
if columns.is_empty() {
return None;
}
Some(IndexInfo {
name: name.to_string(),
columns,
is_unique,
is_primary,
filter: None,
index_type: Some(index_type.to_string()),
included_columns: None,
comment: None,
key_is_expression: Vec::new(),
column_opclasses: vec![],
key_options: Vec::new(),
constraint_backed: false,
})
}
fn secondary_index(line: &str) -> Option<IndexInfo> {
let (_, rest) = split_keyword_prefix(line, "INDEX")?;
let (name, after_name) = read_mysql_identifier(rest.trim_start())?;
let columns = parse_mysql_index_columns(first_parenthesized_content(after_name)?);
if columns.is_empty() {
return None;
}
Some(IndexInfo {
name,
columns,
is_unique: false,
is_primary: false,
filter: None,
index_type: mysql_keyword_argument(after_name, "USING").or_else(|| Some("INDEX".to_string())),
included_columns: None,
comment: mysql_quoted_string_argument(after_name, "COMMENT"),
key_is_expression: Vec::new(),
column_opclasses: vec![],
key_options: Vec::new(),
constraint_backed: false,
})
}
fn split_keyword_prefix<'a>(line: &'a str, keyword: &str) -> Option<(&'a str, &'a str)> {
if line.len() < keyword.len() || !line[..keyword.len()].eq_ignore_ascii_case(keyword) {
return None;
}
let rest = &line[keyword.len()..];
if !rest.is_empty() && is_mysql_identifier_byte(rest.as_bytes()[0]) {
return None;
}
Some((&line[..keyword.len()], rest))
}
fn read_mysql_identifier(input: &str) -> Option<(String, &str)> {
let input = input.trim_start();
if input.is_empty() {
return None;
}
let bytes = input.as_bytes();
if bytes[0] == b'`' {
let mut i = 1;
let mut value = String::new();
while i < bytes.len() {
if bytes[i] == b'`' {
if i + 1 < bytes.len() && bytes[i + 1] == b'`' {
value.push('`');
i += 2;
continue;
}
return Some((value, &input[i + 1..]));
}
let ch = input[i..].chars().next()?;
value.push(ch);
i += ch.len_utf8();
}
return None;
}
let end = input.find(|ch: char| ch.is_whitespace() || matches!(ch, '(' | ')' | ',')).unwrap_or(input.len());
if end == 0 {
return None;
}
Some((input[..end].to_string(), &input[end..]))
}
fn first_parenthesized_content(input: &str) -> Option<&str> {
let bytes = input.as_bytes();
let mut depth = 0usize;
let mut start = None;
let mut i = 0usize;
while i < bytes.len() {
match bytes[i] {
b'\'' | b'"' | b'`' => {
i = skip_mysql_quoted(input, i, bytes[i]);
continue;
}
b'(' => {
if depth == 0 {
start = Some(i + 1);
}
depth += 1;
}
b')' if depth > 0 => {
depth -= 1;
if depth == 0 {
return start.map(|start| &input[start..i]);
}
}
_ => {}
}
i += 1;
}
None
}
fn split_top_level_csv(input: &str) -> Vec<&str> {
let bytes = input.as_bytes();
let mut parts = Vec::new();
let mut depth = 0usize;
let mut start = 0usize;
let mut i = 0usize;
while i < bytes.len() {
match bytes[i] {
b'\'' | b'"' | b'`' => {
i = skip_mysql_quoted(input, i, bytes[i]);
continue;
}
b'(' => depth += 1,
b')' if depth > 0 => depth -= 1,
b',' if depth == 0 => {
parts.push(input[start..i].trim());
start = i + 1;
}
_ => {}
}
i += 1;
}
parts.push(input[start..].trim());
parts
}
fn parse_mysql_index_columns(input: &str) -> Vec<String> {
split_top_level_csv(input)
.into_iter()
.filter_map(|part| read_mysql_identifier(part).map(|(column, _)| column))
.filter(|column| !column.is_empty())
.collect()
}
fn mysql_keyword_argument(input: &str, keyword: &str) -> Option<String> {
let bytes = input.as_bytes();
let mut i = 0usize;
while i < bytes.len() {
match bytes[i] {
b'\'' | b'"' | b'`' => {
i = skip_mysql_quoted(input, i, bytes[i]);
continue;
}
_ if mysql_keyword_at(input, i, keyword) => {
return read_mysql_identifier(&input[i + keyword.len()..]).map(|(value, _)| value);
}
_ => i += 1,
}
}
None
}
fn mysql_quoted_string_argument(input: &str, keyword: &str) -> Option<String> {
let bytes = input.as_bytes();
let mut i = 0usize;
while i < bytes.len() {
match bytes[i] {
b'\'' | b'"' | b'`' => {
i = skip_mysql_quoted(input, i, bytes[i]);
continue;
}
_ if mysql_keyword_at(input, i, keyword) => {
let rest = input[i + keyword.len()..].trim_start();
if rest.as_bytes().first().copied() != Some(b'\'') {
return None;
}
let end = skip_mysql_quoted(rest, 0, b'\'');
if end <= 1 || end > rest.len() {
return None;
}
return Some(rest[1..end - 1].replace("\\'", "'").replace("''", "'"));
}
_ => i += 1,
}
}
None
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn doris_create_table_ddl_indexes_include_unique_key_and_inverted_indexes() {
let ddl = r#"
CREATE TABLE `bfm_org` (
`org_id` bigint NULL,
`ORG_CODE` varchar(255) NULL,
`ORG_NAME` varchar(255) NULL,
INDEX org_id_idx (`org_id`) USING INVERTED,
INDEX org_code_idx (`ORG_CODE`) USING INVERTED,
INDEX org_name_idx (`ORG_NAME`) USING INVERTED
) ENGINE=OLAP
UNIQUE KEY(`org_id`)
COMMENT '部门信息表'
DISTRIBUTED BY HASH(`org_id`) BUCKETS 4
"#;
let indexes = indexes_from_create_table_ddl(ddl);
assert_eq!(indexes.len(), 4);
assert_eq!(indexes[0].name, "org_id_idx");
assert_eq!(indexes[0].columns, vec!["org_id"]);
assert!(!indexes[0].is_unique);
assert_eq!(indexes[0].index_type.as_deref(), Some("INVERTED"));
assert_eq!(indexes[3].name, "UNIQUE KEY");
assert_eq!(indexes[3].columns, vec!["org_id"]);
assert!(indexes[3].is_unique);
assert!(!indexes[3].is_primary);
assert_eq!(indexes[3].index_type.as_deref(), Some("UNIQUE KEY"));
}
#[test]
fn doris_create_table_ddl_index_parser_handles_quoted_names_and_comments() {
let ddl = r#"
CREATE TABLE `search_test` (
`name``part` varchar(64) NULL,
INDEX `idx``name` (`name``part`) USING NGRAM_BF COMMENT 'name''s index'
) ENGINE=OLAP
UNIQUE KEY(`tenant_id`, `name``part`)
"#;
let indexes = indexes_from_create_table_ddl(ddl);
assert_eq!(indexes.len(), 2);
assert_eq!(indexes[0].name, "idx`name");
assert_eq!(indexes[0].columns, vec!["name`part"]);
assert_eq!(indexes[0].index_type.as_deref(), Some("NGRAM_BF"));
assert_eq!(indexes[0].comment.as_deref(), Some("name's index"));
assert_eq!(indexes[1].columns, vec!["tenant_id", "name`part"]);
assert!(indexes[1].is_unique);
}
fn catalog_info(name: &str, catalog_type: &str, is_current: bool) -> crate::db::CatalogInfo {
crate::db::CatalogInfo {
name: name.to_string(),
catalog_type: catalog_type.to_string(),
is_current,
comment: None,
}
}
#[test]
fn catalog_database_ref_backtick_qualifies_two_parts() {
assert_eq!(catalog_database_ref("iceberg_catalog", "sales"), "`iceberg_catalog`.`sales`");
}
#[test]
fn catalog_table_ref_backtick_qualifies_three_parts() {
assert_eq!(catalog_table_ref("iceberg_catalog", "sales", "orders"), "`iceberg_catalog`.`sales`.`orders`");
}
#[test]
fn doris_catalog_refs_escape_embedded_backticks() {
assert_eq!(catalog_database_ref("a`b", "c`d"), "`a``b`.`c``d`");
assert_eq!(catalog_table_ref("a`b", "c`d", "e`f"), "`a``b`.`c``d`.`e``f`");
}
#[test]
fn normalize_catalogs_does_not_inject_missing_internal() {
// SHOW CATALOGS always lists the built-in catalog on both engines, so a
// missing internal catalog is not synthesized — the caller's flat-sidebar
// fallback handles a single/empty result instead.
let catalogs =
vec![catalog_info("iceberg_catalog", "iceberg", true), catalog_info("hive_catalog", "hive", false)];
let normalized = normalize_catalogs(catalogs);
let names: Vec<&str> = normalized.iter().map(|c| c.name.as_str()).collect();
assert_eq!(names, vec!["hive_catalog", "iceberg_catalog"]);
assert!(!normalized.iter().any(|c| c.is_internal()));
}
#[test]
fn normalize_catalogs_keeps_existing_internal_first() {
let catalogs = vec![
catalog_info("iceberg_catalog", "iceberg", false),
catalog_info("internal", "internal", true),
catalog_info("hive_catalog", "hive", false),
];
let normalized = normalize_catalogs(catalogs);
let names: Vec<&str> = normalized.iter().map(|c| c.name.as_str()).collect();
assert_eq!(names, vec!["internal", "hive_catalog", "iceberg_catalog"]);
}
#[test]
fn normalize_catalogs_sorts_starrocks_default_catalog_first() {
// StarRocks names its built-in catalog `default_catalog` (Type=Internal);
// detection is type-based, so it sorts first just like Doris `internal`.
let catalogs =
vec![catalog_info("hive_catalog", "hive", false), catalog_info("default_catalog", "Internal", true)];
let normalized = normalize_catalogs(catalogs);
let names: Vec<&str> = normalized.iter().map(|c| c.name.as_str()).collect();
assert_eq!(names, vec!["default_catalog", "hive_catalog"]);
assert!(normalized[0].is_internal());
assert!(!normalized[1].is_internal());
}
#[test]
fn normalize_catalogs_detects_internal_by_type_not_name() {
// A catalog literally named `internal` but with an external type is NOT
// the built-in catalog and must not sort first.
let catalogs = vec![catalog_info("internal", "iceberg", false), catalog_info("hive_catalog", "hive", false)];
let normalized = normalize_catalogs(catalogs);
let names: Vec<&str> = normalized.iter().map(|c| c.name.as_str()).collect();
assert_eq!(names, vec!["hive_catalog", "internal"]);
assert!(!normalized.iter().any(|c| c.is_internal()));
}
#[test]
fn normalize_catalogs_handles_empty_input() {
let normalized = normalize_catalogs(Vec::new());
assert!(normalized.is_empty());
}
}
-401
View File
@@ -1,401 +0,0 @@
use mysql_async::prelude::*;
use crate::types::{ColumnInfo, DatabaseInfo, ForeignKeyInfo, IndexInfo, ObjectInfo, TableInfo, TriggerInfo};
const CURRENT_SCHEMA_EXPRESSION: &str = "SYS_CONTEXT('USERENV', 'CURRENT_SCHEMA')";
fn row_get<T, I>(row: &mysql_async::Row, index: I) -> Option<T>
where
T: mysql_async::prelude::FromValue,
I: mysql_async::prelude::ColumnIndex,
{
row.get_opt::<T, I>(index).and_then(|result| result.ok())
}
fn quote_value(s: &str) -> String {
format!("'{}'", s.replace('\\', "\\\\").replace('\'', "\\'"))
}
fn metadata_owner_sql(schema: &str) -> String {
if schema.trim().is_empty() {
// Unqualified Oracle objects resolve in the session's current schema. Keep metadata lookup on the same session
// semantics without an extra round trip that could observe a different pooled connection.
CURRENT_SCHEMA_EXPRESSION.to_string()
} else {
quote_value(schema)
}
}
fn get_str(row: &mysql_async::Row, idx: usize) -> String {
row_get::<String, _>(row, idx)
.or_else(|| row_get::<Vec<u8>, _>(row, idx).map(|b| String::from_utf8_lossy(&b).to_string()))
.unwrap_or_default()
}
fn get_opt_str(row: &mysql_async::Row, idx: usize) -> Option<String> {
row_get::<String, _>(row, idx)
.or_else(|| row_get::<Vec<u8>, _>(row, idx).map(|b| String::from_utf8_lossy(&b).to_string()))
}
fn get_opt_i32(row: &mysql_async::Row, idx: usize) -> Option<i32> {
row_get::<i32, _>(row, idx).or_else(|| row_get::<i64, _>(row, idx).and_then(|v| i32::try_from(v).ok()))
}
fn list_user_schemas_sql() -> &'static str {
"SELECT USERNAME FROM ALL_USERS \
WHERE USERNAME NOT IN ('SYS','LBACSYS','__public') \
OR USERNAME = SYS_CONTEXT('USERENV', 'CURRENT_SCHEMA') \
ORDER BY USERNAME"
}
pub async fn list_databases(pool: &mysql_async::Pool) -> Result<Vec<DatabaseInfo>, String> {
let mut conn = pool.get_conn().await.map_err(|e| e.to_string())?;
let result = conn.query_iter(list_user_schemas_sql()).await.map_err(|e| e.to_string())?;
let rows: Vec<mysql_async::Row> = result.collect_and_drop().await.map_err(|e| e.to_string())?;
Ok(rows.iter().map(|row| DatabaseInfo { name: get_str(row, 0), ..Default::default() }).collect())
}
pub async fn list_schemas(pool: &mysql_async::Pool) -> Result<Vec<String>, String> {
list_databases(pool).await.map(|databases| databases.into_iter().map(|database| database.name).collect())
}
fn list_tables_sql(schema: &str) -> String {
format!(
"SELECT TABLE_NAME, 'TABLE' AS TABLE_TYPE FROM ALL_TABLES WHERE OWNER = {s} \
UNION ALL \
SELECT VIEW_NAME, 'VIEW' AS TABLE_TYPE FROM ALL_VIEWS WHERE OWNER = {s} \
ORDER BY 1",
s = metadata_owner_sql(schema),
)
}
pub async fn list_tables(pool: &mysql_async::Pool, schema: &str) -> Result<Vec<TableInfo>, String> {
let sql = list_tables_sql(schema);
let mut conn = pool.get_conn().await.map_err(|e| e.to_string())?;
let result = conn.query_iter(&sql).await.map_err(|e| e.to_string())?;
let rows: Vec<mysql_async::Row> = result.collect_and_drop().await.map_err(|e| e.to_string())?;
Ok(rows
.iter()
.map(|row| TableInfo {
name: get_str(row, 0),
table_type: get_str(row, 1),
valid: None,
comment: None,
parent_schema: None,
parent_name: None,
})
.collect())
}
fn list_objects_sql(schema: &str) -> String {
format!(
"SELECT TABLE_NAME AS OBJECT_NAME, 'TABLE' AS OBJECT_TYPE, OWNER AS OBJECT_SCHEMA, 0 AS SORT_ORDER \
FROM ALL_TABLES WHERE OWNER = {s} \
UNION ALL \
SELECT VIEW_NAME AS OBJECT_NAME, 'VIEW' AS OBJECT_TYPE, OWNER AS OBJECT_SCHEMA, 1 AS SORT_ORDER \
FROM ALL_VIEWS WHERE OWNER = {s} \
UNION ALL \
SELECT OBJECT_NAME, OBJECT_TYPE, OWNER AS OBJECT_SCHEMA, \
CASE WHEN OBJECT_TYPE = 'PROCEDURE' THEN 2 ELSE 3 END AS SORT_ORDER \
FROM ALL_PROCEDURES \
WHERE OWNER = {s} AND OBJECT_TYPE IN ('PROCEDURE', 'FUNCTION') AND PROCEDURE_NAME IS NULL \
UNION ALL \
SELECT OBJECT_NAME, CASE OBJECT_TYPE WHEN 'PACKAGE BODY' THEN 'PACKAGE_BODY' ELSE OBJECT_TYPE END AS OBJECT_TYPE, \
OWNER AS OBJECT_SCHEMA, CASE WHEN OBJECT_TYPE = 'PACKAGE' THEN 4 ELSE 5 END AS SORT_ORDER \
FROM ALL_OBJECTS \
WHERE OWNER = {s} AND OBJECT_TYPE IN ('PACKAGE', 'PACKAGE BODY') \
ORDER BY SORT_ORDER, OBJECT_NAME",
s = metadata_owner_sql(schema),
)
}
pub async fn list_objects(pool: &mysql_async::Pool, schema: &str) -> Result<Vec<ObjectInfo>, String> {
let sql = list_objects_sql(schema);
let mut conn = pool.get_conn().await.map_err(|e| e.to_string())?;
let result = conn.query_iter(&sql).await.map_err(|e| e.to_string())?;
let rows: Vec<mysql_async::Row> = result.collect_and_drop().await.map_err(|e| e.to_string())?;
Ok(rows
.iter()
.map(|row| ObjectInfo {
name: get_str(row, 0),
object_type: get_str(row, 1),
schema: get_opt_str(row, 2)
.filter(|schema| !schema.is_empty())
.or_else(|| (!schema.trim().is_empty()).then(|| schema.to_string())),
valid: None,
signature: None,
custom_type_kind: None,
has_members: None,
comment: None,
created_at: None,
updated_at: None,
parent_schema: None,
parent_name: None,
trigger: None,
xugu_type_members_expandable: None,
})
.collect())
}
fn get_columns_sql(schema: &str, table: &str) -> String {
format!(
"SELECT c.COLUMN_NAME, c.DATA_TYPE, c.NULLABLE, c.DATA_DEFAULT, \
c.DATA_LENGTH, c.DATA_PRECISION, c.DATA_SCALE, c.COLUMN_ID, \
CASE WHEN cc.COLUMN_NAME IS NOT NULL THEN 1 ELSE 0 END AS IS_PK \
FROM ALL_TAB_COLUMNS c \
LEFT JOIN ( \
SELECT cols.OWNER, cols.TABLE_NAME, cols.COLUMN_NAME \
FROM ALL_CONS_COLUMNS cols \
JOIN ALL_CONSTRAINTS con ON con.CONSTRAINT_NAME = cols.CONSTRAINT_NAME AND con.OWNER = cols.OWNER \
WHERE con.CONSTRAINT_TYPE = 'P' \
) cc ON cc.OWNER = c.OWNER AND cc.TABLE_NAME = c.TABLE_NAME AND cc.COLUMN_NAME = c.COLUMN_NAME \
WHERE c.OWNER = {s} AND c.TABLE_NAME = {t} \
ORDER BY c.COLUMN_ID",
s = metadata_owner_sql(schema),
t = quote_value(table),
)
}
pub async fn get_columns(pool: &mysql_async::Pool, schema: &str, table: &str) -> Result<Vec<ColumnInfo>, String> {
let sql = get_columns_sql(schema, table);
let mut conn = pool.get_conn().await.map_err(|e| e.to_string())?;
let result = conn.query_iter(&sql).await.map_err(|e| e.to_string())?;
let rows: Vec<mysql_async::Row> = result.collect_and_drop().await.map_err(|e| e.to_string())?;
Ok(rows
.iter()
.map(|row| {
let data_type = get_str(row, 1);
let precision = get_opt_i32(row, 5);
let scale = get_opt_i32(row, 6);
let length = get_opt_i32(row, 4);
let display_type = format_oracle_type(&data_type, precision, scale, length);
ColumnInfo {
name: get_str(row, 0),
data_type: display_type,
is_nullable: get_str(row, 2) == "Y",
column_default: get_opt_str(row, 3).map(|s| s.trim().to_string()).filter(|s| !s.is_empty()),
is_primary_key: row_get::<i32, _>(row, 8).unwrap_or(0) == 1,
extra: None,
comment: None,
numeric_precision: precision,
numeric_scale: scale,
character_maximum_length: length,
enum_values: None,
..Default::default()
}
})
.collect())
}
fn format_oracle_type(data_type: &str, precision: Option<i32>, scale: Option<i32>, length: Option<i32>) -> String {
match data_type.to_uppercase().as_str() {
"NUMBER" => match (precision, scale) {
(Some(p), Some(s)) if s > 0 => format!("NUMBER({p},{s})"),
(Some(p), _) => format!("NUMBER({p})"),
_ => "NUMBER".to_string(),
},
"VARCHAR2" | "NVARCHAR2" | "CHAR" | "NCHAR" | "RAW" => match length {
Some(l) => format!("{data_type}({l})"),
None => data_type.to_string(),
},
_ => data_type.to_string(),
}
}
fn list_indexes_sql(schema: &str, table: &str) -> String {
format!(
"SELECT ai.INDEX_NAME, \
LISTAGG(aic.COLUMN_NAME, ',') WITHIN GROUP (ORDER BY aic.COLUMN_POSITION) AS COLUMNS, \
ai.UNIQUENESS, \
CASE WHEN ac.CONSTRAINT_TYPE = 'P' THEN 1 ELSE 0 END AS IS_PRIMARY \
FROM ALL_INDEXES ai \
JOIN ALL_IND_COLUMNS aic ON ai.INDEX_NAME = aic.INDEX_NAME AND ai.TABLE_OWNER = aic.TABLE_OWNER \
LEFT JOIN ALL_CONSTRAINTS ac ON ac.INDEX_NAME = ai.INDEX_NAME AND ac.OWNER = ai.TABLE_OWNER AND ac.CONSTRAINT_TYPE = 'P' \
WHERE ai.TABLE_OWNER = {s} AND ai.TABLE_NAME = {t} \
GROUP BY ai.INDEX_NAME, ai.UNIQUENESS, ac.CONSTRAINT_TYPE \
ORDER BY ai.INDEX_NAME",
s = metadata_owner_sql(schema),
t = quote_value(table),
)
}
pub async fn list_indexes(pool: &mysql_async::Pool, schema: &str, table: &str) -> Result<Vec<IndexInfo>, String> {
let sql = list_indexes_sql(schema, table);
let mut conn = pool.get_conn().await.map_err(|e| e.to_string())?;
let result = conn.query_iter(&sql).await.map_err(|e| e.to_string())?;
let rows: Vec<mysql_async::Row> = result.collect_and_drop().await.map_err(|e| e.to_string())?;
Ok(rows
.iter()
.map(|row| {
let cols_str = get_str(row, 1);
IndexInfo {
name: get_str(row, 0),
columns: cols_str.split(',').filter(|s| !s.is_empty()).map(|s| s.to_string()).collect(),
is_unique: get_str(row, 2) == "UNIQUE",
is_primary: row_get::<i32, _>(row, 3).unwrap_or(0) == 1,
filter: None,
index_type: None,
included_columns: None,
comment: None,
key_is_expression: Vec::new(),
column_opclasses: vec![],
key_options: Vec::new(),
constraint_backed: false,
}
})
.collect())
}
pub async fn list_foreign_keys(
pool: &mysql_async::Pool,
schema: &str,
table: &str,
) -> Result<Vec<ForeignKeyInfo>, String> {
let sql = list_foreign_keys_sql(schema, table);
let mut conn = pool.get_conn().await.map_err(|e| e.to_string())?;
let result = conn.query_iter(&sql).await.map_err(|e| e.to_string())?;
let rows: Vec<mysql_async::Row> = result.collect_and_drop().await.map_err(|e| e.to_string())?;
Ok(rows
.iter()
.map(|row| ForeignKeyInfo {
name: get_str(row, 0),
column: get_str(row, 1),
ref_schema: Some(get_str(row, 2)),
ref_table: get_str(row, 3),
ref_column: get_str(row, 4),
on_update: None,
on_delete: None,
})
.collect())
}
fn list_foreign_keys_sql(schema: &str, table: &str) -> String {
format!(
"SELECT ac.CONSTRAINT_NAME, acc.COLUMN_NAME, \
ac2.OWNER AS R_OWNER, ac2.TABLE_NAME AS R_TABLE, acc2.COLUMN_NAME AS R_COLUMN \
FROM ALL_CONSTRAINTS ac \
JOIN ALL_CONS_COLUMNS acc ON ac.CONSTRAINT_NAME = acc.CONSTRAINT_NAME AND ac.OWNER = acc.OWNER \
JOIN ALL_CONSTRAINTS ac2 ON ac.R_CONSTRAINT_NAME = ac2.CONSTRAINT_NAME AND ac.R_OWNER = ac2.OWNER \
JOIN ALL_CONS_COLUMNS acc2 ON ac2.CONSTRAINT_NAME = acc2.CONSTRAINT_NAME AND ac2.OWNER = acc2.OWNER \
AND acc.POSITION = acc2.POSITION \
WHERE ac.CONSTRAINT_TYPE = 'R' AND ac.OWNER = {s} AND ac.TABLE_NAME = {t} \
ORDER BY ac.CONSTRAINT_NAME, acc.POSITION",
s = metadata_owner_sql(schema),
t = quote_value(table),
)
}
fn list_triggers_sql(schema: &str, table: &str) -> String {
format!(
"SELECT TRIGGER_NAME, TRIGGERING_EVENT, TRIGGER_TYPE \
FROM ALL_TRIGGERS \
WHERE TABLE_OWNER = {s} AND TABLE_NAME = {t} \
ORDER BY TRIGGER_NAME",
s = metadata_owner_sql(schema),
t = quote_value(table),
)
}
pub async fn list_triggers(pool: &mysql_async::Pool, schema: &str, table: &str) -> Result<Vec<TriggerInfo>, String> {
let sql = list_triggers_sql(schema, table);
let mut conn = pool.get_conn().await.map_err(|e| e.to_string())?;
let result = conn.query_iter(&sql).await.map_err(|e| e.to_string())?;
let rows: Vec<mysql_async::Row> = result.collect_and_drop().await.map_err(|e| e.to_string())?;
Ok(rows
.iter()
.map(|row| {
let trigger_type = get_str(row, 2);
let timing = if trigger_type.contains("BEFORE") {
"BEFORE"
} else if trigger_type.contains("AFTER") {
"AFTER"
} else {
"INSTEAD OF"
};
TriggerInfo {
name: get_str(row, 0),
event: get_str(row, 1),
timing: timing.to_string(),
level: None,
condition: None,
language: None,
enabled: None,
valid: None,
comment: None,
created_at: None,
statement: None,
}
})
.collect())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn ob_oracle_list_objects_sql_includes_routines() {
let sql = list_objects_sql("DLJPM");
assert!(sql.contains("ALL_TABLES"));
assert!(sql.contains("ALL_VIEWS"));
assert!(sql.contains("ALL_PROCEDURES"));
assert!(sql.contains("'PROCEDURE'"));
assert!(sql.contains("'FUNCTION'"));
assert!(sql.contains("ALL_OBJECTS"));
assert!(sql.contains("'PACKAGE'"));
assert!(sql.contains("'PACKAGE BODY'"));
assert!(sql.contains("OWNER AS OBJECT_SCHEMA"));
}
#[test]
fn ob_oracle_user_schema_sql_filters_system_schemas_but_keeps_current_schema() {
let sql = list_user_schemas_sql();
assert!(sql.contains("USERNAME NOT IN ('SYS','LBACSYS','__public')"));
assert!(sql.contains("OR USERNAME = SYS_CONTEXT('USERENV', 'CURRENT_SCHEMA')"));
assert!(!sql.contains("ORAAUDITOR"));
}
#[test]
fn ob_oracle_empty_schema_uses_current_schema_for_all_metadata_sql() {
let statements = [
list_tables_sql(""),
list_objects_sql(""),
get_columns_sql("", "PERF_PROV_CONFIG"),
list_indexes_sql("", "PERF_PROV_CONFIG"),
list_foreign_keys_sql("", "PERF_PROV_CONFIG"),
list_triggers_sql("", "PERF_PROV_CONFIG"),
];
for sql in statements {
assert!(sql.contains(CURRENT_SCHEMA_EXPRESSION), "missing current schema fallback in: {sql}");
assert!(!sql.contains("OWNER = ''"), "empty owner remained in: {sql}");
assert!(!sql.contains("TABLE_OWNER = ''"), "empty table owner remained in: {sql}");
}
}
#[test]
fn ob_oracle_explicit_schema_does_not_query_current_schema() {
let statements = [
list_tables_sql("SYS"),
list_objects_sql("SYS"),
get_columns_sql("SYS", "PERF_PROV_CONFIG"),
list_indexes_sql("SYS", "PERF_PROV_CONFIG"),
list_foreign_keys_sql("SYS", "PERF_PROV_CONFIG"),
list_triggers_sql("SYS", "PERF_PROV_CONFIG"),
];
for sql in statements {
assert!(sql.contains("'SYS'"), "missing explicit schema in: {sql}");
assert!(!sql.contains(CURRENT_SCHEMA_EXPRESSION), "unexpected current schema lookup in: {sql}");
}
}
}
@@ -1,237 +0,0 @@
use mysql_async::prelude::*;
use std::collections::{HashMap, HashSet};
use crate::models::connection::{ConnectionConfig, DatabaseType};
use crate::types::{ObjectInfo, TableInfo};
use super::mysql::{
get_conn_with_timeout, get_str_by_name, list_routine_objects, list_tables_show_with_status, quote_value,
table_infos_to_objects, MySqlPool, TableStatusMeta,
};
pub const DRIVER_PROFILE: &str = "oceanbase";
fn materialized_views_sql(database: &str) -> String {
format!("SELECT MVIEW_NAME FROM oceanbase.DBA_MVIEWS WHERE OWNER = {}", quote_value(database))
}
async fn list_materialized_view_names(pool: &MySqlPool, database: &str) -> Result<HashSet<String>, String> {
let mut conn = get_conn_with_timeout(pool, super::connection_timeout()).await?;
let result = conn.query_iter(materialized_views_sql(database)).await.map_err(|error| error.to_string())?;
let rows: Vec<mysql_async::Row> = result.collect_and_drop().await.map_err(|error| error.to_string())?;
Ok(rows
.iter()
.filter_map(|row| {
let name = get_str_by_name(row, "MVIEW_NAME").trim().to_string();
(!name.is_empty()).then_some(name)
})
.collect())
}
fn is_materialized_view_type(table_type: &str) -> bool {
let normalized = table_type.to_ascii_uppercase().replace('_', " ");
normalized.contains("MATERIALIZED") && normalized.contains("VIEW")
}
fn merge_materialized_views(
tables: &mut Vec<TableInfo>,
materialized_view_names: Result<HashSet<String>, String>,
database: &str,
) {
let materialized_view_names = match materialized_view_names {
Ok(names) => names,
Err(error) => {
log::warn!("Skipping materialized view classification for OceanBase database `{database}`: {error}");
HashSet::new()
}
};
for table in tables.iter_mut() {
if is_materialized_view_type(&table.table_type)
|| materialized_view_names.iter().any(|name| name.eq_ignore_ascii_case(&table.name))
{
table.table_type = "VIEW".to_string();
}
}
let mut known_names: HashSet<String> = tables.iter().map(|table| table.name.to_ascii_lowercase()).collect();
let mut missing_names = materialized_view_names.into_iter().collect::<Vec<_>>();
missing_names.sort();
for name in missing_names {
if known_names.insert(name.to_ascii_lowercase()) {
tables.push(TableInfo {
name,
table_type: "VIEW".to_string(),
valid: None,
comment: None,
parent_schema: None,
parent_name: None,
});
}
}
tables.sort_by(|left, right| left.name.cmp(&right.name));
}
async fn list_tables_with_status(
pool: &MySqlPool,
database: &str,
) -> Result<(Vec<TableInfo>, HashMap<String, TableStatusMeta>), String> {
let (tables, materialized_view_names) =
tokio::join!(list_tables_show_with_status(pool, database), list_materialized_view_names(pool, database));
let (mut tables, status) = tables?;
merge_materialized_views(&mut tables, materialized_view_names, database);
Ok((tables, status))
}
pub async fn list_tables(pool: &MySqlPool, database: &str) -> Result<Vec<TableInfo>, String> {
list_tables_with_status(pool, database).await.map(|(tables, _)| tables)
}
pub async fn list_objects(pool: &MySqlPool, database: &str) -> Result<Vec<ObjectInfo>, String> {
let (tables, routines) =
tokio::join!(list_tables_with_status(pool, database), list_routine_objects(pool, database));
let (tables, status) = tables?;
let mut objects = table_infos_to_objects(tables, &status, database);
match routines {
Ok(routines) => objects.extend(routines),
Err(error) => log::warn!("Skipping routines for OceanBase database `{database}` in object browser: {error}"),
}
Ok(objects)
}
pub fn is_config(config: &ConnectionConfig) -> bool {
is_profile(&config.db_type, config.driver_profile.as_deref())
}
pub fn is_profile(db_type: &DatabaseType, driver_profile: Option<&str>) -> bool {
*db_type == DatabaseType::Mysql
&& driver_profile.is_some_and(|profile| profile.eq_ignore_ascii_case(DRIVER_PROFILE))
}
pub fn query_timeout_sql(config: &ConnectionConfig, timeout_secs: u64) -> Option<String> {
if !is_config(config) || timeout_secs == 0 {
return None;
}
Some(format!("SET ob_query_timeout = {}", timeout_secs.saturating_mul(1_000_000)))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn profile_is_isolated_from_oracle_mode_and_standard_mysql() {
assert!(is_profile(&DatabaseType::Mysql, Some("OceanBase")));
assert!(!is_profile(&DatabaseType::Mysql, None));
assert!(!is_profile(&DatabaseType::OceanbaseOracle, Some("oceanbase")));
}
#[test]
fn query_timeout_uses_microseconds_and_saturates() {
let mut config = test_config();
assert_eq!(query_timeout_sql(&config, 30), Some("SET ob_query_timeout = 30000000".to_string()));
assert_eq!(query_timeout_sql(&config, u64::MAX), Some(format!("SET ob_query_timeout = {}", u64::MAX)));
config.driver_profile = None;
assert_eq!(query_timeout_sql(&config, 30), None);
}
#[test]
fn materialized_views_are_visible_as_mysql_views_without_retyping_regular_objects() {
let mut tables =
vec![table("orders", "BASE TABLE"), table("orders_view", "VIEW"), table("orders_mv", "BASE TABLE")];
merge_materialized_views(
&mut tables,
Ok(HashSet::from(["orders_mv".to_string(), "daily_orders_mv".to_string()])),
"analytics",
);
assert_eq!(
tables.iter().map(|table| (table.name.as_str(), table.table_type.as_str())).collect::<Vec<_>>(),
vec![("daily_orders_mv", "VIEW"), ("orders", "BASE TABLE"), ("orders_mv", "VIEW"), ("orders_view", "VIEW"),]
);
let objects = table_infos_to_objects(tables, &HashMap::new(), "analytics");
assert_eq!(
objects.iter().map(|object| (object.name.as_str(), object.object_type.as_str())).collect::<Vec<_>>(),
vec![("daily_orders_mv", "VIEW"), ("orders", "TABLE"), ("orders_mv", "VIEW"), ("orders_view", "VIEW"),]
);
}
#[test]
fn materialized_view_names_are_case_insensitive_and_not_duplicated() {
let mut tables = vec![table("Orders_MV", "BASE TABLE")];
merge_materialized_views(
&mut tables,
Ok(HashSet::from(["orders_mv".to_string(), "daily_mv".to_string()])),
"analytics",
);
assert_eq!(
tables.iter().map(|table| (table.name.as_str(), table.table_type.as_str())).collect::<Vec<_>>(),
vec![("Orders_MV", "VIEW"), ("daily_mv", "VIEW")]
);
}
#[test]
fn materialized_view_catalog_failures_preserve_show_rows() {
let mut tables = vec![table("orders", "BASE TABLE"), table("orders_mv", "MATERIALIZED VIEW")];
merge_materialized_views(&mut tables, Err("permission denied".to_string()), "analytics");
assert_eq!(
tables.iter().map(|table| (table.name.as_str(), table.table_type.as_str())).collect::<Vec<_>>(),
vec![("orders", "BASE TABLE"), ("orders_mv", "VIEW")]
);
}
#[test]
fn empty_materialized_view_metadata_does_not_retype_regular_objects() {
let mut tables = vec![table("orders", "BASE TABLE"), table("orders_view", "VIEW")];
merge_materialized_views(&mut tables, Ok(HashSet::new()), "analytics");
assert_eq!(
tables.iter().map(|table| (table.name.as_str(), table.table_type.as_str())).collect::<Vec<_>>(),
vec![("orders", "BASE TABLE"), ("orders_view", "VIEW")]
);
}
#[test]
fn materialized_view_catalog_sql_escapes_database_names() {
assert_eq!(
materialized_views_sql("tenant's analytics"),
"SELECT MVIEW_NAME FROM oceanbase.DBA_MVIEWS WHERE OWNER = 'tenant\\'s analytics'"
);
}
fn test_config() -> ConnectionConfig {
serde_json::from_value(serde_json::json!({
"id": "oceanbase",
"name": "OceanBase",
"db_type": "mysql",
"driver_profile": "oceanbase",
"host": "127.0.0.1",
"port": 2883,
"username": "root",
"password": "",
"database": "test"
}))
.unwrap()
}
fn table(name: &str, table_type: &str) -> TableInfo {
TableInfo {
name: name.to_string(),
table_type: table_type.to_string(),
valid: None,
comment: None,
parent_schema: None,
parent_name: None,
}
}
}
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
File diff suppressed because it is too large. Load diff
-186
View File
@@ -1,186 +0,0 @@
//! Host-key verification for the SSH tunnel client.
//!
//! Before any credential is sent, russh invokes
//! [`russh::client::Handler::check_server_key`]. The original implementation
//! accepted *every* server key, which let a man-in-the-middle endpoint reject
//! the public key and trick the client into sending the password instead.
//! This module makes that callback actually verify the server identity.
//!
//! Design
//! ------
//! Verification is layered and **fail-closed**, reusing russh's own OpenSSH-
//! format `known_hosts` module instead of a hand-rolled store:
//!
//! 1. The user's system `~/.ssh/known_hosts` is checked **read-only**. Reusing
//! it means a host already trusted in a terminal is also trusted here, and a
//! key change recorded there is detected here too. dbx never *writes* to it.
//! 2. dbx's own store at `<data_dir>/known_hosts` (OpenSSH format, managed by
//! russh's `known_hosts` module) is checked. This is where dbx-accepted host
//! keys live — NOT `~/.ssh` — so we never pollute the user's SSH config.
//! 3. An unknown host is reported as [`HostKeyState::Unknown`]; the caller
//! (the `check_server_key` handler) then performs an **explicit TOFU** flow:
//! it asks the UI to confirm the key and, on acceptance, records it via
//! [`HostKeyVerifier::learn`].
//! 4. A *changed* key in the dbx store is [`HostKeyState::Changed`]. The caller
//! prompts the user to compare fingerprints and, if they choose to update,
//! replaces the old entry via [`HostKeyVerifier::replace`]. A changed key
//! in `~/.ssh/known_hosts` stays a hard reject — dbx does not edit that file.
//!
//! Hardening rules:
//! * A changed key is never trusted silently. Credentials are sent only after
//! the user explicitly continues (session-only) or updates the saved key.
//! * If an unknown host's key **cannot be persisted** (e.g. no write permission
//! on `<data_dir>`), `learn` fails — but that failure is surfaced by the
//! caller, which still refuses to trust the host silently.
use std::io;
use std::path::{Path, PathBuf};
use russh::keys::known_hosts::{
check_known_hosts, check_known_hosts_path, known_host_keys_path, learn_known_hosts_path,
};
use russh::keys::parse_public_key_base64;
use russh::keys::ssh_key::{HashAlg, PublicKey};
/// Outcome of checking a server key against the known-hosts stores.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum HostKeyState {
/// The key matches a previously trusted entry (system or dbx store).
Trusted,
/// The key is not present in either store (candidate for explicit TOFU).
Unknown,
/// The dbx store has a different key of the same algorithm. The caller
/// should prompt before replacing it; `previous_fingerprint` is the saved
/// SHA256 fingerprint (or a fallback locator if the line could not be parsed).
Changed { previous_fingerprint: String },
}
/// Host-key verifier backed by russh's OpenSSH-format known-hosts store.
///
/// `known_hosts_path` is the dbx-managed store (typically
/// `<data_dir>/known_hosts`). The system `~/.ssh/known_hosts` is also consulted
/// read-only via [`check_known_hosts`].
pub struct HostKeyVerifier {
known_hosts_path: PathBuf,
}
impl HostKeyVerifier {
/// Creates a verifier that records/reads accepted host keys at `known_hosts_path`.
pub fn new(known_hosts_path: PathBuf) -> Self {
Self { known_hosts_path }
}
/// Layered, fail-closed check. A *changed* key in `~/.ssh` is an `Err`. A
/// changed key in the dbx store is [`HostKeyState::Changed`] so the UI can
/// offer a one-click replace.
pub fn check(&self, host: &str, port: u16, key: &PublicKey) -> Result<HostKeyState, io::Error> {
// 1. System known_hosts (read-only). A changed key there is a hard reject.
match check_known_hosts(host, port, key) {
Ok(true) => return Ok(HostKeyState::Trusted),
Err(russh::keys::Error::KeyChanged { line }) => {
return Err(host_key_changed_error(host, port, line, "~/.ssh/known_hosts"));
}
// Missing system file, no home dir, or an unreadable system file:
// don't fail the whole connection on it; fall through to the dbx
// store / TOFU, which still protects against MITM.
_ => {}
}
// 2. dbx-managed known_hosts.
match check_known_hosts_path(host, port, key, &self.known_hosts_path) {
Ok(true) => return Ok(HostKeyState::Trusted),
Err(russh::keys::Error::KeyChanged { line }) => {
let previous_fingerprint = previous_fingerprint_for(&self.known_hosts_path, host, port, key)
.unwrap_or_else(|| format!("(known_hosts line {line})"));
return Ok(HostKeyState::Changed { previous_fingerprint });
}
// Unknown (or an unreadable dbx store): report as a candidate for TOFU.
_ => {}
}
Ok(HostKeyState::Unknown)
}
/// Records a host key into the dbx store (TOFU persistence). Called by the
/// caller only after the user explicitly accepts the key. A write failure
/// is reported (so the caller knows persistence did not happen) but does
/// not by itself abort the session — the host may simply be trusted for
/// this session only.
pub fn learn(&self, host: &str, port: u16, key: &PublicKey) -> Result<(), io::Error> {
learn_known_hosts_path(host, port, key, &self.known_hosts_path).map_err(|e| {
io::Error::new(
io::ErrorKind::PermissionDenied,
format!(
"Failed to persist host key for {host}:{port} to {} ({e}). \
The host is trusted for this session only.",
self.known_hosts_path.display()
),
)
})
}
/// Drops the previous same-algorithm key for this host in the dbx store,
/// then records `key`. Used after the user confirms a fingerprint change.
pub fn replace(&self, host: &str, port: u16, key: &PublicKey) -> Result<(), io::Error> {
self.forget_matching_algorithm(host, port, key)?;
self.learn(host, port, key)
}
fn forget_matching_algorithm(&self, host: &str, port: u16, key: &PublicKey) -> Result<(), io::Error> {
let recorded = match known_host_keys_path(host, port, &self.known_hosts_path) {
Ok(keys) => keys,
Err(_) => return Ok(()),
};
let drop_keys: Vec<PublicKey> = recorded
.into_iter()
.filter(|(_, recorded)| recorded.algorithm() == key.algorithm())
.map(|(_, recorded)| recorded)
.collect();
if drop_keys.is_empty() {
return Ok(());
}
let contents = match std::fs::read_to_string(&self.known_hosts_path) {
Ok(contents) => contents,
Err(error) if error.kind() == io::ErrorKind::NotFound => return Ok(()),
Err(error) => return Err(error),
};
let mut kept = Vec::new();
for line in contents.lines() {
let trimmed = line.trim();
if trimmed.is_empty() || trimmed.starts_with('#') {
kept.push(line);
continue;
}
let should_drop = trimmed
.split_whitespace()
.nth(2)
.and_then(|encoded| parse_public_key_base64(encoded).ok())
.map(|parsed| drop_keys.iter().any(|recorded| recorded == &parsed))
.unwrap_or(false);
if !should_drop {
kept.push(line);
}
}
let mut out = kept.join("\n");
if !out.is_empty() {
out.push('\n');
}
std::fs::write(&self.known_hosts_path, out)
}
}
fn previous_fingerprint_for(path: &Path, host: &str, port: u16, key: &PublicKey) -> Option<String> {
let recorded = known_host_keys_path(host, port, path).ok()?;
recorded
.into_iter()
.find(|(_, recorded)| recorded.algorithm() == key.algorithm() && recorded != key)
.map(|(_, recorded)| recorded.fingerprint(HashAlg::Sha256).to_string())
}
fn host_key_changed_error(host: &str, port: u16, line: usize, store: &str) -> io::Error {
io::Error::other(format!(
"Host key for {host}:{port} changed (recorded at {store}, line {line}). \
This may indicate a man-in-the-middle attack. Remove the old entry and reconnect only if you expect this change."
))
}
-255
View File
@@ -1,255 +0,0 @@
use mysql_async::prelude::*;
use std::collections::HashSet;
use crate::models::connection::{ConnectionConfig, DatabaseType};
use crate::types::{ObjectInfo, TableInfo};
use super::mysql::{
get_conn_with_timeout, get_str_by_name, list_routine_objects, list_tables_show_with_status, quote_value,
table_infos_to_objects, MySqlPool, TableStatusMeta,
};
pub use super::mysql_compatible::{
get_columns_show_from as get_catalog_columns, list_catalog_indexes, list_catalogs,
list_databases_show_from as list_catalog_databases, list_indexes_with_ddl_fallback as list_indexes,
show_create_table_ddl_from as get_catalog_table_ddl,
};
pub async fn list_catalog_tables(pool: &MySqlPool, catalog: &str, database: &str) -> Result<Vec<TableInfo>, String> {
match super::mysql_compatible::list_tables_show_from(pool, catalog, database).await {
Ok(tables) => Ok(tables),
Err(error) if should_retry_catalog_table_listing(&error) => {
match super::mysql_compatible::list_databases_show_from(pool, catalog).await {
Ok(databases) if catalog_database_exists(&databases, database) => {
log::debug!(
"Treating StarRocks external database `{catalog}`.`{database}` as empty after its qualified table lookup failed: {error}"
);
Ok(Vec::new())
}
_ => Err(error),
}
}
Err(error) => Err(error),
}
}
fn should_retry_catalog_table_listing(error: &str) -> bool {
error.to_ascii_lowercase().contains("unknown database")
}
fn catalog_database_exists(databases: &[crate::types::DatabaseInfo], database: &str) -> bool {
databases.iter().any(|candidate| candidate.name == database)
}
pub fn is_config(config: &ConnectionConfig) -> bool {
is_profile(&config.db_type, config.driver_profile.as_deref())
}
pub fn is_profile(db_type: &DatabaseType, driver_profile: Option<&str>) -> bool {
*db_type == DatabaseType::StarRocks
|| (*db_type == DatabaseType::Mysql
&& driver_profile.is_some_and(|profile| profile.eq_ignore_ascii_case("starrocks")))
}
fn materialized_views_sql(database: &str) -> String {
format!(
"SELECT TABLE_NAME FROM information_schema.materialized_views WHERE TABLE_SCHEMA = {}",
quote_value(database)
)
}
pub fn materialized_view_definition_sql(database: &str, name: &str) -> String {
format!(
"SELECT MATERIALIZED_VIEW_DEFINITION \
FROM information_schema.materialized_views \
WHERE TABLE_SCHEMA = {} AND TABLE_NAME = {} \
LIMIT 1",
quote_value(database),
quote_value(name)
)
}
async fn list_materialized_view_names(pool: &MySqlPool, database: &str) -> Result<HashSet<String>, String> {
let sql = materialized_views_sql(database);
let mut conn = get_conn_with_timeout(pool, super::connection_timeout()).await?;
let result = conn.query_iter(&sql).await.map_err(|error| error.to_string())?;
let rows: Vec<mysql_async::Row> = result.collect_and_drop().await.map_err(|error| error.to_string())?;
Ok(rows
.iter()
.filter_map(|row| {
let name = get_str_by_name(row, "TABLE_NAME").trim().to_string();
(!name.is_empty()).then_some(name)
})
.collect())
}
fn merge_materialized_views(
tables: &mut Vec<TableInfo>,
materialized_view_names: Result<HashSet<String>, String>,
database: &str,
) {
let materialized_view_names = match materialized_view_names {
Ok(names) => names,
Err(error) => {
log::warn!("Skipping materialized view classification for StarRocks database `{database}`: {error}");
return;
}
};
let known_names: HashSet<String> = tables.iter().map(|table| table.name.clone()).collect();
for table in tables.iter_mut() {
if materialized_view_names.contains(&table.name) {
table.table_type = "MATERIALIZED_VIEW".to_string();
}
}
let mut sorted_names: Vec<&String> = materialized_view_names.iter().collect();
sorted_names.sort();
for name in sorted_names {
if !known_names.contains(name.as_str()) {
tables.push(TableInfo {
name: name.clone(),
table_type: "MATERIALIZED_VIEW".to_string(),
valid: None,
comment: None,
parent_schema: None,
parent_name: None,
});
}
}
}
async fn list_tables_with_status(
pool: &MySqlPool,
database: &str,
) -> Result<(Vec<TableInfo>, std::collections::HashMap<String, TableStatusMeta>), String> {
let (tables, materialized_view_names) =
tokio::join!(list_tables_show_with_status(pool, database), list_materialized_view_names(pool, database));
let (mut tables, status) = tables?;
merge_materialized_views(&mut tables, materialized_view_names, database);
Ok((tables, status))
}
pub async fn list_tables(pool: &MySqlPool, database: &str) -> Result<Vec<TableInfo>, String> {
list_tables_with_status(pool, database).await.map(|(tables, _)| tables)
}
pub async fn list_table_objects(pool: &MySqlPool, database: &str) -> Result<Vec<ObjectInfo>, String> {
let (tables, routines) =
tokio::join!(list_tables_with_status(pool, database), list_routine_objects(pool, database));
let (tables, status) = tables?;
let mut objects = table_infos_to_objects(tables, &status, database);
match routines {
Ok(routines) => objects.extend(routines),
Err(error) => log::warn!("Skipping routines for database `{database}` in object browser: {error}"),
}
Ok(objects)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn profile_matches_starrocks_only() {
assert!(is_profile(&DatabaseType::StarRocks, None));
assert!(is_profile(&DatabaseType::Mysql, Some("STARROCKS")));
assert!(!is_profile(&DatabaseType::Mysql, Some("doris")));
assert!(!is_profile(&DatabaseType::Postgres, Some("starrocks")));
}
#[test]
fn catalog_table_listing_retries_only_unknown_database_errors() {
assert!(should_retry_catalog_table_listing(
"ERROR 5501 (3F000): Getting analyzing error. Detail message: Unknown database 'edw_dwt'."
));
assert!(!should_retry_catalog_table_listing("Access denied on database edw_dwt"));
assert!(!should_retry_catalog_table_listing("Connection timed out"));
}
#[test]
fn catalog_database_existence_requires_an_exact_name_match() {
let databases = vec![crate::types::DatabaseInfo { name: "edw_dwt".to_string(), ..Default::default() }];
assert!(catalog_database_exists(&databases, "edw_dwt"));
assert!(!catalog_database_exists(&databases, "EDW_DWT"));
assert!(!catalog_database_exists(&databases, "missing"));
}
#[test]
fn materialized_views_are_classified_without_duplicates() {
let mut tables = vec![table("orders", "BASE TABLE"), table("orders_view", "VIEW"), table("orders_mv", "VIEW")];
merge_materialized_views(&mut tables, Ok(HashSet::from(["orders_mv".to_string()])), "analytics");
assert_eq!(
tables.iter().map(|table| (table.name.as_str(), table.table_type.as_str())).collect::<Vec<_>>(),
vec![("orders", "BASE TABLE"), ("orders_view", "VIEW"), ("orders_mv", "MATERIALIZED_VIEW")]
);
}
#[test]
fn missing_materialized_views_are_appended_deterministically() {
let mut tables = vec![table("orders", "BASE TABLE")];
let names = HashSet::from(["orders_mv".to_string(), "daily_orders_mv".to_string()]);
merge_materialized_views(&mut tables, Ok(names), "analytics");
assert_eq!(
tables.iter().map(|table| (table.name.as_str(), table.table_type.as_str())).collect::<Vec<_>>(),
vec![
("orders", "BASE TABLE"),
("daily_orders_mv", "MATERIALIZED_VIEW"),
("orders_mv", "MATERIALIZED_VIEW"),
]
);
}
#[test]
fn lookup_failure_preserves_base_table_types() {
let mut tables = vec![table("orders_mv", "VIEW")];
merge_materialized_views(&mut tables, Err("permission denied".to_string()), "analytics");
assert_eq!(tables[0].table_type, "VIEW");
}
#[test]
fn materialized_view_queries_escape_database_and_name() {
assert_eq!(
materialized_views_sql("tenant's analytics"),
"SELECT TABLE_NAME FROM information_schema.materialized_views WHERE TABLE_SCHEMA = 'tenant\\'s analytics'"
);
assert_eq!(
materialized_view_definition_sql("tenant's analytics", "weird'name"),
"SELECT MATERIALIZED_VIEW_DEFINITION FROM information_schema.materialized_views WHERE TABLE_SCHEMA = 'tenant\\'s analytics' AND TABLE_NAME = 'weird\\'name' LIMIT 1"
);
}
#[test]
fn object_conversion_preserves_materialized_view_type() {
let objects = table_infos_to_objects(
vec![table("orders", "BASE TABLE"), table("orders_view", "VIEW"), table("orders_mv", "MATERIALIZED_VIEW")],
&std::collections::HashMap::new(),
"analytics",
);
assert_eq!(
objects.iter().map(|object| (object.name.as_str(), object.object_type.as_str())).collect::<Vec<_>>(),
vec![("orders", "TABLE"), ("orders_view", "VIEW"), ("orders_mv", "MATERIALIZED_VIEW")]
);
}
fn table(name: &str, table_type: &str) -> TableInfo {
TableInfo {
name: name.to_string(),
table_type: table_type.to_string(),
valid: None,
comment: None,
parent_schema: None,
parent_name: None,
}
}
}
-24
View File
@@ -1,24 +0,0 @@
use crate::models::connection::{ConnectionConfig, DatabaseType};
pub const DRIVER_PROFILE: &str = "tidb";
pub fn is_config(config: &ConnectionConfig) -> bool {
is_profile(&config.db_type, config.driver_profile.as_deref())
}
pub fn is_profile(db_type: &DatabaseType, driver_profile: Option<&str>) -> bool {
*db_type == DatabaseType::Mysql
&& driver_profile.is_some_and(|profile| profile.eq_ignore_ascii_case(DRIVER_PROFILE))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn profile_is_isolated_from_standard_mysql() {
assert!(is_profile(&DatabaseType::Mysql, Some("TiDB")));
assert!(!is_profile(&DatabaseType::Mysql, None));
assert!(!is_profile(&DatabaseType::Postgres, Some("tidb")));
}
}
-375
View File
@@ -1,375 +0,0 @@
/// Shared WKB (Well-Known Binary) geometry parsing shared by PostgreSQL (EWKB)
/// and MySQL (standard WKB). Parses WKB bytes into WKT while retaining the
/// top-level EWKB SRID when present.
///
/// Supports: POINT, LINESTRING, POLYGON, MULTIPOINT, MULTILINESTRING,
/// MULTIPOLYGON, GEOMETRYCOLLECTION, and their Z/M/ZM variants.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
struct WkbDimensions {
has_z: bool,
has_m: bool,
}
impl WkbDimensions {
fn suffix(self) -> &'static str {
match (self.has_z, self.has_m) {
(false, false) => "",
(true, false) => " Z",
(false, true) => " M",
(true, true) => " ZM",
}
}
fn coordinate_len(self) -> usize {
2 + usize::from(self.has_z) + usize::from(self.has_m)
}
}
#[derive(Debug, Clone, PartialEq)]
enum WkbGeometry {
Point { dims: WkbDimensions, coords: Option<Vec<f64>> },
LineString { dims: WkbDimensions, points: Vec<Vec<f64>> },
Polygon { dims: WkbDimensions, rings: Vec<Vec<Vec<f64>>> },
MultiPoint { dims: WkbDimensions, points: Vec<Option<Vec<f64>>> },
MultiLineString { dims: WkbDimensions, lines: Vec<Vec<Vec<f64>>> },
MultiPolygon { dims: WkbDimensions, polygons: Vec<Vec<Vec<Vec<f64>>>> },
GeometryCollection { dims: WkbDimensions, geometries: Vec<WkbGeometry> },
}
#[derive(Debug, Clone, PartialEq)]
pub struct DecodedGeometry {
pub wkt: String,
pub srid: Option<u32>,
}
impl WkbGeometry {
fn to_wkt(&self) -> String {
match self {
Self::Point { dims, coords } => match coords {
Some(coords) => format!("POINT{}({})", dims.suffix(), format_wkb_coordinate(coords)),
None => format!("POINT{} EMPTY", dims.suffix()),
},
Self::LineString { dims, points } => {
if points.is_empty() {
format!("LINESTRING{} EMPTY", dims.suffix())
} else {
format!("LINESTRING{}({})", dims.suffix(), format_wkb_coordinate_sequence(points))
}
}
Self::Polygon { dims, rings } => {
if rings.is_empty() {
format!("POLYGON{} EMPTY", dims.suffix())
} else {
format!(
"POLYGON{}({})",
dims.suffix(),
rings
.iter()
.map(|ring| format!("({})", format_wkb_coordinate_sequence(ring)))
.collect::<Vec<_>>()
.join(",")
)
}
}
Self::MultiPoint { dims, points } => {
if points.is_empty() {
format!("MULTIPOINT{} EMPTY", dims.suffix())
} else {
format!(
"MULTIPOINT{}({})",
dims.suffix(),
points
.iter()
.map(|point| match point {
Some(coords) => format!("({})", format_wkb_coordinate(coords)),
None => "EMPTY".to_string(),
})
.collect::<Vec<_>>()
.join(",")
)
}
}
Self::MultiLineString { dims, lines } => {
if lines.is_empty() {
format!("MULTILINESTRING{} EMPTY", dims.suffix())
} else {
format!(
"MULTILINESTRING{}({})",
dims.suffix(),
lines
.iter()
.map(|line| format!("({})", format_wkb_coordinate_sequence(line)))
.collect::<Vec<_>>()
.join(",")
)
}
}
Self::MultiPolygon { dims, polygons } => {
if polygons.is_empty() {
format!("MULTIPOLYGON{} EMPTY", dims.suffix())
} else {
format!(
"MULTIPOLYGON{}({})",
dims.suffix(),
polygons
.iter()
.map(|polygon| {
format!(
"({})",
polygon
.iter()
.map(|ring| format!("({})", format_wkb_coordinate_sequence(ring)))
.collect::<Vec<_>>()
.join(",")
)
})
.collect::<Vec<_>>()
.join(",")
)
}
}
Self::GeometryCollection { dims, geometries } => {
if geometries.is_empty() {
format!("GEOMETRYCOLLECTION{} EMPTY", dims.suffix())
} else {
format!(
"GEOMETRYCOLLECTION{}({})",
dims.suffix(),
geometries.iter().map(WkbGeometry::to_wkt).collect::<Vec<_>>().join(",")
)
}
}
}
}
}
fn format_wkb_coordinate(coords: &[f64]) -> String {
coords.iter().map(|value| value.to_string()).collect::<Vec<_>>().join(" ")
}
fn format_wkb_coordinate_sequence(points: &[Vec<f64>]) -> String {
points.iter().map(|point| format_wkb_coordinate(point)).collect::<Vec<_>>().join(",")
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum WkbEndian {
Big,
Little,
}
struct WkbReader<'a> {
raw: &'a [u8],
pos: usize,
}
impl<'a> WkbReader<'a> {
fn new(raw: &'a [u8]) -> Self {
Self { raw, pos: 0 }
}
fn read_u8(&mut self) -> Option<u8> {
let value = *self.raw.get(self.pos)?;
self.pos += 1;
Some(value)
}
fn read_array<const N: usize>(&mut self) -> Option<[u8; N]> {
let end = self.pos.checked_add(N)?;
let bytes: [u8; N] = self.raw.get(self.pos..end)?.try_into().ok()?;
self.pos = end;
Some(bytes)
}
fn read_u32(&mut self, endian: WkbEndian) -> Option<u32> {
let bytes = self.read_array::<4>()?;
Some(match endian {
WkbEndian::Big => u32::from_be_bytes(bytes),
WkbEndian::Little => u32::from_le_bytes(bytes),
})
}
fn read_f64(&mut self, endian: WkbEndian) -> Option<f64> {
let bytes = self.read_array::<8>()?;
Some(match endian {
WkbEndian::Big => f64::from_be_bytes(bytes),
WkbEndian::Little => f64::from_le_bytes(bytes),
})
}
}
fn parse_wkb_dimensions(type_word: u32) -> (u32, WkbDimensions, bool) {
let mut base_type = type_word & 0x1FFF_FFFF;
let mut dims = WkbDimensions { has_z: (type_word & 0x8000_0000) != 0, has_m: (type_word & 0x4000_0000) != 0 };
let has_srid = (type_word & 0x2000_0000) != 0;
if base_type >= 3000 {
dims.has_z = true;
dims.has_m = true;
base_type -= 3000;
} else if base_type >= 2000 {
dims.has_m = true;
base_type -= 2000;
} else if base_type >= 1000 {
dims.has_z = true;
base_type -= 1000;
}
(base_type, dims, has_srid)
}
fn read_wkb_point_coords(reader: &mut WkbReader<'_>, dims: WkbDimensions) -> Option<Option<Vec<f64>>> {
let endian = match reader.read_u8()? {
0 => WkbEndian::Big,
1 => WkbEndian::Little,
_ => return None,
};
let type_word = reader.read_u32(endian)?;
let (base_type, parsed_dims, has_srid) = parse_wkb_dimensions(type_word);
if base_type != 1 || parsed_dims != dims {
return None;
}
if has_srid {
reader.read_u32(endian)?;
}
let coords = (0..parsed_dims.coordinate_len()).map(|_| reader.read_f64(endian)).collect::<Option<Vec<_>>>()?;
Some((!coords.iter().all(|value| value.is_nan())).then_some(coords))
}
fn parse_wkb_points(reader: &mut WkbReader<'_>, endian: WkbEndian, dims: WkbDimensions) -> Option<Vec<Vec<f64>>> {
let count = usize::try_from(reader.read_u32(endian)?).ok()?;
(0..count)
.map(|_| (0..dims.coordinate_len()).map(|_| reader.read_f64(endian)).collect::<Option<Vec<_>>>())
.collect::<Option<Vec<_>>>()
}
fn parse_wkb_geometry(reader: &mut WkbReader<'_>) -> Option<(WkbGeometry, Option<u32>)> {
let endian = match reader.read_u8()? {
0 => WkbEndian::Big,
1 => WkbEndian::Little,
_ => return None,
};
let type_word = reader.read_u32(endian)?;
let (base_type, dims, has_srid) = parse_wkb_dimensions(type_word);
let srid = has_srid.then(|| reader.read_u32(endian)).flatten().filter(|value| *value != 0);
let geometry = match base_type {
1 => {
let coords = (0..dims.coordinate_len()).map(|_| reader.read_f64(endian)).collect::<Option<Vec<_>>>()?;
let coords = if coords.iter().all(|value| value.is_nan()) { None } else { Some(coords) };
Some(WkbGeometry::Point { dims, coords })
}
2 => Some(WkbGeometry::LineString { dims, points: parse_wkb_points(reader, endian, dims)? }),
3 => {
let ring_count = usize::try_from(reader.read_u32(endian)?).ok()?;
let rings = (0..ring_count).map(|_| parse_wkb_points(reader, endian, dims)).collect::<Option<Vec<_>>>()?;
Some(WkbGeometry::Polygon { dims, rings })
}
4 => {
let count = usize::try_from(reader.read_u32(endian)?).ok()?;
let points = (0..count).map(|_| read_wkb_point_coords(reader, dims)).collect::<Option<Vec<_>>>()?;
Some(WkbGeometry::MultiPoint { dims, points })
}
5 => {
let count = usize::try_from(reader.read_u32(endian)?).ok()?;
let lines = (0..count)
.map(|_| match parse_wkb_geometry(reader)?.0 {
WkbGeometry::LineString { points, .. } => Some(points),
_ => None,
})
.collect::<Option<Vec<_>>>()?;
Some(WkbGeometry::MultiLineString { dims, lines })
}
6 => {
let count = usize::try_from(reader.read_u32(endian)?).ok()?;
let polygons = (0..count)
.map(|_| match parse_wkb_geometry(reader)?.0 {
WkbGeometry::Polygon { rings, .. } => Some(rings),
_ => None,
})
.collect::<Option<Vec<_>>>()?;
Some(WkbGeometry::MultiPolygon { dims, polygons })
}
7 => {
let count = usize::try_from(reader.read_u32(endian)?).ok()?;
let geometries =
(0..count).map(|_| parse_wkb_geometry(reader).map(|parsed| parsed.0)).collect::<Option<Vec<_>>>()?;
Some(WkbGeometry::GeometryCollection { dims, geometries })
}
_ => None,
}?;
Some((geometry, srid))
}
pub fn decode_wkb_geometry(raw: &[u8]) -> Option<DecodedGeometry> {
let mut reader = WkbReader::new(raw);
let (geometry, srid) = parse_wkb_geometry(&mut reader)?;
if reader.pos != raw.len() {
return None;
}
Some(DecodedGeometry { wkt: geometry.to_wkt(), srid })
}
/// Parse WKB/EWKB bytes into a WKT (Well-Known Text) string.
///
/// Supports both standard WKB (MySQL) and Extended WKB (PostgreSQL/PostGIS).
/// Returns `None` if the bytes cannot be parsed as valid WKB.
pub fn wkb_to_wkt(raw: &[u8]) -> Option<String> {
decode_wkb_geometry(raw).map(|geometry| geometry.wkt)
}
#[cfg(test)]
mod tests {
use super::*;
fn decode_hex(value: &str) -> Vec<u8> {
value
.as_bytes()
.chunks_exact(2)
.map(|pair| {
let text = std::str::from_utf8(pair).unwrap();
u8::from_str_radix(text, 16).unwrap()
})
.collect()
}
#[test]
fn ewkb_decoder_retains_top_level_srid() {
let decoded = decode_wkb_geometry(&decode_hex("0101000020E6100000C520B07268195D404E62105839F44340")).unwrap();
assert_eq!(decoded.wkt, "POINT(116.397 39.908)");
assert_eq!(decoded.srid, Some(4326));
}
#[test]
fn standard_wkb_has_no_srid() {
let decoded = decode_wkb_geometry(&decode_hex("0101000000000000000000F03F0000000000000040")).unwrap();
assert_eq!(decoded.wkt, "POINT(1 2)");
assert_eq!(decoded.srid, None);
}
#[test]
fn big_endian_ewkb_retains_srid() {
let decoded = decode_wkb_geometry(&decode_hex("002000000100000F113FF00000000000004000000000000000")).unwrap();
assert_eq!(decoded.wkt, "POINT(1 2)");
assert_eq!(decoded.srid, Some(3857));
}
#[test]
fn ewkb_srid_zero_is_unknown() {
let decoded = decode_wkb_geometry(&decode_hex("010100002000000000000000000000F03F0000000000000040")).unwrap();
assert_eq!(decoded.wkt, "POINT(1 2)");
assert_eq!(decoded.srid, None);
}
#[test]
fn multipoint_preserves_valid_empty_points() {
let decoded =
decode_wkb_geometry(&decode_hex("0104000000010000000101000000000000000000F87F000000000000F87F")).unwrap();
assert_eq!(decoded.wkt, "MULTIPOINT(EMPTY)");
}
#[test]
fn multipoint_rejects_truncated_child_points() {
assert!(decode_wkb_geometry(&decode_hex("0104000000010000000101000000")).is_none());
}
}
-340
View File
@@ -1,340 +0,0 @@
use std::future::Future;
use std::sync::{
atomic::{AtomicU64, Ordering},
Arc,
};
use std::time::Duration;
use tokio::time::timeout;
use tokio_util::sync::CancellationToken;
use crate::db;
use crate::models::connection::ConnectionConfig;
pub const QUERY_TIMEOUT: Duration = Duration::from_secs(30);
pub const MAX_ROWS: usize = 10000;
pub const QUERY_CANCELED: &str = "Query canceled";
/// Unified database operation execution budget.
/// query_timeout = None only means SQL execution has no upper limit;
/// checkout/connect/recycle/cancel/cleanup always have hard upper limits and cannot be disabled.
#[derive(Debug, Clone)]
pub struct DbOperationBudget {
pub checkout_timeout: Duration,
pub connect_timeout: Duration,
pub recycle_timeout: Duration,
pub query_timeout: Option<Duration>,
pub cancel_timeout: Duration,
pub cleanup_timeout: Duration,
}
impl DbOperationBudget {
/// Build an execution budget from connection config.
/// checkout/connect/recycle use connect_timeout_secs (clamped to 1s minimum, 300s maximum).
/// query_timeout follows resolve_query_timeout semantics (Some(0) -> None).
/// cancel/cleanup are fixed values and cannot be disabled.
pub fn from_config(connect_timeout_secs: u64, query_timeout_secs: Option<u64>) -> Self {
let infra_timeout = Duration::from_secs(connect_timeout_secs.clamp(1, 300));
Self {
checkout_timeout: infra_timeout,
connect_timeout: infra_timeout,
recycle_timeout: infra_timeout,
query_timeout: resolve_query_timeout(query_timeout_secs),
cancel_timeout: Duration::from_secs(5),
cleanup_timeout: Duration::from_secs(3),
}
}
pub fn from_connection_config(config: &ConnectionConfig) -> Self {
// effective_query_timeout_secs, not the raw field: it preserves 0 ("no limit")
// and applies the per-database-type floor Cloud Spanner needs for DDL.
Self::from_config(config.effective_connect_timeout_secs(), Some(config.effective_query_timeout_secs()))
}
/// Use global default values (when no connection config is available).
pub fn with_defaults() -> Self {
let default_infra = db::connection_timeout();
Self {
checkout_timeout: default_infra,
connect_timeout: default_infra,
recycle_timeout: default_infra,
query_timeout: Some(QUERY_TIMEOUT),
cancel_timeout: Duration::from_secs(5),
cleanup_timeout: Duration::from_secs(3),
}
}
}
pub struct StreamProgressClock {
started_at: tokio::time::Instant,
last_progress_ms: AtomicU64,
#[cfg(any(test, feature = "test-support"))]
marked: std::sync::atomic::AtomicBool,
}
impl Default for StreamProgressClock {
fn default() -> Self {
Self::new()
}
}
impl StreamProgressClock {
pub fn new() -> Self {
Self {
started_at: tokio::time::Instant::now(),
last_progress_ms: AtomicU64::new(0),
#[cfg(any(test, feature = "test-support"))]
marked: std::sync::atomic::AtomicBool::new(false),
}
}
pub fn mark(&self) {
self.last_progress_ms.store(self.started_at.elapsed().as_millis() as u64, Ordering::Relaxed);
#[cfg(any(test, feature = "test-support"))]
self.marked.store(true, Ordering::Relaxed);
}
/// Whether any progress has been recorded yet. Test-only: production code
/// only needs the derived inactivity window. A row read within the first
/// millisecond records a zero timestamp, so this cannot be derived from
/// `last_progress_ms`.
#[cfg(any(test, feature = "test-support"))]
pub fn marked(&self) -> bool {
self.marked.load(Ordering::Relaxed)
}
fn elapsed_since_progress(&self) -> Duration {
let last_progress_ms = self.last_progress_ms.load(Ordering::Relaxed);
let elapsed_ms = self.started_at.elapsed().as_millis() as u64;
Duration::from_millis(elapsed_ms.saturating_sub(last_progress_ms))
}
}
pub fn timeout_error() -> String {
timeout_error_for(QUERY_TIMEOUT)
}
pub fn timeout_error_for(timeout_duration: Duration) -> String {
let seconds = timeout_duration.as_secs().max(1);
format!("Query timed out after {seconds} seconds")
}
pub fn canceled_error() -> String {
QUERY_CANCELED.to_string()
}
pub async fn await_stream_with_progress_timeout<F, T>(
stream_future: F,
timeout: Option<Duration>,
progress_clock: Arc<StreamProgressClock>,
cancel_token: Option<&CancellationToken>,
timeout_message: String,
) -> Result<T, String>
where
F: Future<Output = Result<T, String>>,
{
let Some(timeout) = timeout else {
return match cancel_token {
Some(token) => {
tokio::select! {
biased;
_ = token.cancelled() => Err(canceled_error()),
result = stream_future => result,
}
}
None => stream_future.await,
};
};
tokio::pin!(stream_future);
loop {
// Query timeout is an inactivity budget, not a cap on total stream duration.
let remaining = timeout.saturating_sub(progress_clock.elapsed_since_progress());
if remaining.is_zero() {
return Err(timeout_message.clone());
}
let sleep = tokio::time::sleep(remaining);
tokio::pin!(sleep);
match cancel_token {
Some(token) => {
tokio::select! {
biased;
_ = token.cancelled() => return Err(canceled_error()),
result = &mut stream_future => return result,
_ = &mut sleep => {},
}
}
None => {
tokio::select! {
biased;
result = &mut stream_future => return result,
_ = &mut sleep => {},
}
}
}
if progress_clock.elapsed_since_progress() >= timeout {
return Err(timeout_message);
}
}
}
pub fn is_canceled(cancel_token: &Option<CancellationToken>) -> bool {
cancel_token.as_ref().map(|token| token.is_cancelled()).unwrap_or(false)
}
pub async fn wait_for_query<F>(cancel_token: Option<CancellationToken>, future: F) -> Result<db::QueryResult, String>
where
F: Future<Output = Result<db::QueryResult, String>>,
{
wait_for_query_with_timeout(cancel_token, QUERY_TIMEOUT, future).await
}
pub async fn wait_for_query_with_timeout<F>(
cancel_token: Option<CancellationToken>,
timeout_duration: Duration,
future: F,
) -> Result<db::QueryResult, String>
where
F: Future<Output = Result<db::QueryResult, String>>,
{
wait_for_result_with_timeout(cancel_token, timeout_duration, future).await
}
pub async fn wait_for_result_with_timeout<T, F>(
cancel_token: Option<CancellationToken>,
timeout_duration: Duration,
future: F,
) -> Result<T, String>
where
F: Future<Output = Result<T, String>>,
{
if let Some(token) = cancel_token {
tokio::select! {
biased;
_ = token.cancelled() => Err(canceled_error()),
result = timeout(timeout_duration, future) => result.map_err(|_| timeout_error_for(timeout_duration))?,
}
} else {
timeout(timeout_duration, future).await.map_err(|_| timeout_error_for(timeout_duration))?
}
}
/// Like `wait_for_query_with_timeout` but with an optional timeout.
/// `None` means no timeout (only cancellation can stop the query).
pub async fn wait_for_query_opt<F>(
cancel_token: Option<CancellationToken>,
timeout_duration: Option<Duration>,
future: F,
) -> Result<db::QueryResult, String>
where
F: Future<Output = Result<db::QueryResult, String>>,
{
wait_for_result_opt(cancel_token, timeout_duration, future).await
}
pub async fn wait_for_result_opt<T, F>(
cancel_token: Option<CancellationToken>,
timeout_duration: Option<Duration>,
future: F,
) -> Result<T, String>
where
F: Future<Output = Result<T, String>>,
{
match timeout_duration {
Some(d) => wait_for_result_with_timeout(cancel_token, d, future).await,
None => match cancel_token {
Some(token) => {
tokio::select! {
biased;
_ = token.cancelled() => Err(canceled_error()),
result = future => result,
}
}
None => future.await,
},
}
}
pub async fn wait_for_value_opt<T, F>(
cancel_token: Option<CancellationToken>,
timeout_duration: Option<Duration>,
future: F,
) -> Result<T, String>
where
F: Future<Output = T>,
{
match timeout_duration {
Some(timeout_duration) => {
if let Some(token) = cancel_token {
tokio::select! {
biased;
_ = token.cancelled() => Err(canceled_error()),
result = timeout(timeout_duration, future) => result.map_err(|_| timeout_error_for(timeout_duration)),
}
} else {
timeout(timeout_duration, future).await.map_err(|_| timeout_error_for(timeout_duration))
}
}
None => match cancel_token {
Some(token) => {
tokio::select! {
biased;
_ = token.cancelled() => Err(canceled_error()),
result = future => Ok(result),
}
}
None => Ok(future.await),
},
}
}
/// Locks a mutex-guarded shared connection (e.g. SQL Server's single connection
/// per pool key) and reports how long the caller waited for the lock alongside
/// the guard. Callers should fold the returned wait time into any execution-time
/// metric they report, since a driver-level timer that only starts once the lock
/// is held cannot see time spent queued behind another operation on the same
/// connection.
pub async fn lock_shared_client_with_wait<'a, T>(
client: &'a Arc<tokio::sync::Mutex<T>>,
cancel_token: Option<CancellationToken>,
timeout_duration: Option<Duration>,
) -> Result<(tokio::sync::MutexGuard<'a, T>, u128), String> {
let started_at = std::time::Instant::now();
let guard = wait_for_value_opt(cancel_token, timeout_duration, client.lock()).await?;
Ok((guard, started_at.elapsed().as_millis()))
}
pub fn query_timeout_duration(timeout_secs: Option<u64>) -> Option<Duration> {
match timeout_secs {
Some(0) => None,
Some(n) => Some(Duration::from_secs(n)),
None => Some(QUERY_TIMEOUT),
}
}
pub fn resolve_query_timeout(timeout_secs: Option<u64>) -> Option<Duration> {
query_timeout_duration(timeout_secs)
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test(start_paused = true)]
async fn default_progress_clock_tracks_inactivity_from_creation_and_marks() {
let clock = StreamProgressClock::default();
assert_eq!(clock.elapsed_since_progress(), Duration::ZERO);
assert!(!clock.marked());
tokio::time::advance(Duration::from_secs(2)).await;
assert_eq!(clock.elapsed_since_progress(), Duration::from_secs(2));
clock.mark();
assert!(clock.marked());
assert_eq!(clock.elapsed_since_progress(), Duration::ZERO);
tokio::time::advance(Duration::from_secs(1)).await;
assert_eq!(clock.elapsed_since_progress(), Duration::from_secs(1));
}
}
+10 -16
View File
@@ -1,26 +1,20 @@
#![recursion_limit = "256"]
pub use dbx_sql::query_result_sql;
pub use dbx_sql_data::query_result_sql;
pub use dbx_driver_support::{runtime_config, ssh_config};
pub use dbx_platform::download::DownloadSource;
pub use dbx_platform::{path_utils, process};
pub use dbx_sql::{mysql_ddl_normalize, mysql_event_sql, sql, sql_dialect, sql_error_position, sqlserver_temporal};
pub use dbx_sql_core::{mysql_ddl_normalize, mysql_event_sql, sql, sql_error_position, sqlserver_temporal};
pub use dbx_sql_dialect::sql_dialect;
pub use dbx_types::{database_manifest, models, types};
pub mod agent_catalog;
pub mod agent_connection;
pub mod agent_manager;
pub mod agent_offline_export;
pub mod agent_recovery;
pub mod agent_runtime;
pub mod agent_service;
pub mod backend_error;
pub mod database_capabilities;
pub use dbx_driver_agent::{
agent_catalog, agent_connection, agent_manager, agent_offline_export, agent_recovery, agent_runtime, agent_service,
backend_error, database_capabilities,
};
pub mod db;
pub mod execution;
pub use dbx_driver_support::execution;
pub mod metadata;
pub mod mongo_oidc;
pub mod mongo_shell;
pub mod runtime_config;
pub use dbx_driver_mongodb::{mongo_oidc, mongo_shell};
pub mod salesforce_oauth;
pub mod ssh_config;
-728
View File
@@ -1,728 +0,0 @@
use std::{
sync::{Arc, OnceLock},
time::{Duration, Instant},
};
use base64::{engine::general_purpose::URL_SAFE_NO_PAD, Engine as _};
use futures::FutureExt;
use mongodb::{
error::Error as MongoError,
options::oidc::{Callback, CallbackContext, IdpServerResponse},
};
use reqwest::Url;
use serde::Deserialize;
use sha2::{Digest, Sha256};
use tokio::{
io::{AsyncReadExt, AsyncWriteExt},
net::TcpListener,
sync::Mutex,
};
pub const OIDC_REDIRECT_URI: &str = "http://localhost:27097/redirect";
const OIDC_CALLBACK_ADDRESS: &str = "127.0.0.1:27097";
const DEFAULT_CALLBACK_TIMEOUT: Duration = Duration::from_secs(5 * 60);
pub const OIDC_BROWSER_AUTH_TIMEOUT: Duration = DEFAULT_CALLBACK_TIMEOUT;
const HTTP_TIMEOUT: Duration = Duration::from_secs(15);
const MAX_CALLBACK_REQUEST_BYTES: usize = 16 * 1024;
pub type MongoOidcBrowserOpener = Arc<dyn Fn(&str) -> Result<(), String> + Send + Sync>;
#[derive(Debug, Deserialize)]
struct OidcDiscoveryDocument {
issuer: String,
authorization_endpoint: String,
token_endpoint: String,
#[serde(default)]
scopes_supported: Option<Vec<String>>,
}
#[derive(Debug, Deserialize)]
struct OidcTokenResponse {
access_token: String,
#[serde(default)]
refresh_token: Option<String>,
#[serde(default)]
expires_in: Option<u64>,
}
#[derive(Debug)]
struct AuthorizationCallback {
code: String,
state: String,
}
#[derive(Clone, Copy)]
enum OidcEndpointPolicy {
HttpsOnly,
#[cfg(test)]
AllowLoopbackHttp,
}
impl OidcEndpointPolicy {
fn parse_url(self, value: &str, description: &str) -> Result<Url, MongoError> {
let url = Url::parse(value).map_err(|err| oidc_error(format!("invalid {description}: {err}")))?;
if url.scheme() == "https" {
return Ok(url);
}
#[cfg(test)]
if matches!(self, Self::AllowLoopbackHttp)
&& url.scheme() == "http"
&& url.host_str().is_some_and(|host| {
host == "localhost" || host.parse::<std::net::IpAddr>().is_ok_and(|ip| ip.is_loopback())
})
{
return Ok(url);
}
Err(oidc_error(format!("{description} must use HTTPS")))
}
}
fn browser_flow_lock() -> &'static Mutex<()> {
static LOCK: OnceLock<Mutex<()>> = OnceLock::new();
LOCK.get_or_init(|| Mutex::new(()))
}
fn oidc_error(message: impl Into<String>) -> MongoError {
MongoError::custom(format!("MongoDB OIDC authentication failed: {}", message.into()))
}
fn discovery_url_with_policy(issuer: &str, endpoint_policy: OidcEndpointPolicy) -> Result<Url, MongoError> {
let mut url = endpoint_policy.parse_url(issuer, "issuer URL")?;
if url.query().is_some() || url.fragment().is_some() {
return Err(oidc_error("issuer URL must not contain a query or fragment"));
}
let path = format!("{}/.well-known/openid-configuration", url.path().trim_end_matches('/'));
url.set_path(&path);
Ok(url)
}
fn validate_discovery_document(
issuer: &str,
document: OidcDiscoveryDocument,
endpoint_policy: OidcEndpointPolicy,
) -> Result<OidcDiscoveryDocument, MongoError> {
if document.issuer != issuer {
return Err(oidc_error("provider metadata issuer does not exactly match the MongoDB server response"));
}
endpoint_policy.parse_url(&document.issuer, "provider metadata issuer")?;
endpoint_policy.parse_url(&document.authorization_endpoint, "authorization endpoint")?;
endpoint_policy.parse_url(&document.token_endpoint, "token endpoint")?;
Ok(document)
}
async fn discover_provider(
issuer: &str,
endpoint_policy: OidcEndpointPolicy,
) -> Result<OidcDiscoveryDocument, MongoError> {
let client = reqwest::Client::builder()
.timeout(HTTP_TIMEOUT)
.build()
.map_err(|err| oidc_error(format!("failed to create HTTP client: {err}")))?;
let response = client
.get(discovery_url_with_policy(issuer, endpoint_policy)?)
.send()
.await
.map_err(|err| oidc_error(format!("failed to load provider metadata: {err}")))?;
let status = response.status();
if !status.is_success() {
return Err(oidc_error(format!("provider metadata endpoint returned HTTP {status}")));
}
let document = response
.json::<OidcDiscoveryDocument>()
.await
.map_err(|err| oidc_error(format!("invalid provider metadata: {err}")))?;
validate_discovery_document(issuer, document, endpoint_policy)
}
fn random_url_safe_value() -> String {
let first = uuid::Uuid::new_v4();
let second = uuid::Uuid::new_v4();
let mut bytes = [0_u8; 32];
bytes[..16].copy_from_slice(first.as_bytes());
bytes[16..].copy_from_slice(second.as_bytes());
URL_SAFE_NO_PAD.encode(bytes)
}
fn requested_scopes(scopes: Option<Vec<String>>, supported_scopes: Option<&[String]>) -> String {
let mut scopes = scopes.unwrap_or_default();
let supports =
|candidate: &str| supported_scopes.is_none_or(|supported| supported.iter().any(|scope| scope == candidate));
if supports("openid") && !scopes.iter().any(|scope| scope == "openid") {
scopes.insert(0, "openid".to_string());
}
if supports("offline_access") && !scopes.iter().any(|scope| scope == "offline_access") {
scopes.push("offline_access".to_string());
}
scopes.join(" ")
}
fn authorization_url_with_policy(
endpoint: &str,
client_id: &str,
scopes: &str,
state: &str,
code_challenge: &str,
endpoint_policy: OidcEndpointPolicy,
) -> Result<Url, MongoError> {
let mut url = endpoint_policy.parse_url(endpoint, "authorization endpoint")?;
url.query_pairs_mut()
.append_pair("response_type", "code")
.append_pair("client_id", client_id)
.append_pair("redirect_uri", OIDC_REDIRECT_URI)
.append_pair("scope", scopes)
.append_pair("state", state)
.append_pair("code_challenge", code_challenge)
.append_pair("code_challenge_method", "S256");
Ok(url)
}
async fn write_callback_response(stream: &mut tokio::net::TcpStream, success: bool) {
let (status, title, body) = if success {
(
"200 OK",
"Authorization received",
"DBX received the authorization response. Return to DBX while it completes the connection.",
)
} else {
(
"400 Bad Request",
"Authentication failed",
"DBX could not complete authentication. Return to DBX for details.",
)
};
let html = format!(
"<!doctype html><html><head><meta charset=\"utf-8\"><meta name=\"viewport\" content=\"width=device-width\"><title>{title}</title><style>body{{font-family:system-ui,sans-serif;margin:48px;color:#202124}}h1{{font-size:22px}}</style></head><body><h1>{title}</h1><p>{body}</p></body></html>"
);
let response = format!(
"HTTP/1.1 {status}\r\nContent-Type: text/html; charset=utf-8\r\nContent-Security-Policy: default-src 'none'; style-src 'unsafe-inline'\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{html}",
html.len()
);
let _ = stream.write_all(response.as_bytes()).await;
let _ = stream.shutdown().await;
}
fn parse_callback_target(target: &str) -> Result<AuthorizationCallback, String> {
let url = Url::parse(&format!("http://localhost{target}")).map_err(|err| format!("invalid callback URL: {err}"))?;
if url.path() != "/redirect" {
return Err("unexpected callback path".to_string());
}
let mut code = None;
let mut state = None;
let mut provider_error = None;
let mut provider_error_description = None;
for (key, value) in url.query_pairs() {
match key.as_ref() {
"code" => code = Some(value.into_owned()),
"state" => state = Some(value.into_owned()),
"error" => provider_error = Some(value.into_owned()),
"error_description" => provider_error_description = Some(value.into_owned()),
_ => {}
}
}
if let Some(error) = provider_error {
let detail = provider_error_description.map(|description| format!(": {description}")).unwrap_or_default();
return Err(format!("identity provider returned {error}{detail}"));
}
Ok(AuthorizationCallback {
code: code.ok_or_else(|| "callback did not include an authorization code".to_string())?,
state: state.ok_or_else(|| "callback did not include state".to_string())?,
})
}
async fn wait_for_callback(listener: TcpListener, expected_state: &str) -> Result<String, MongoError> {
'requests: loop {
let (mut stream, _) =
listener.accept().await.map_err(|err| oidc_error(format!("callback listener failed: {err}")))?;
let mut buffer = Vec::with_capacity(1024);
loop {
if buffer.len() == MAX_CALLBACK_REQUEST_BYTES {
write_callback_response(&mut stream, false).await;
continue 'requests;
}
let mut chunk = [0_u8; 1024];
let remaining = MAX_CALLBACK_REQUEST_BYTES - buffer.len();
let read_len = remaining.min(chunk.len());
let bytes_read = match stream.read(&mut chunk[..read_len]).await {
Ok(bytes_read) => bytes_read,
Err(_) => {
write_callback_response(&mut stream, false).await;
continue 'requests;
}
};
if bytes_read == 0 {
break;
}
buffer.extend_from_slice(&chunk[..bytes_read]);
if buffer.windows(4).any(|window| window == b"\r\n\r\n") {
break;
}
}
let request = String::from_utf8_lossy(&buffer);
let target = request.lines().next().and_then(|line| line.split_whitespace().nth(1)).map(str::to_owned);
let Some(target) = target else {
write_callback_response(&mut stream, false).await;
continue;
};
let callback = match parse_callback_target(&target) {
Ok(callback) => callback,
Err(_) => {
write_callback_response(&mut stream, false).await;
continue;
}
};
if callback.state != expected_state {
write_callback_response(&mut stream, false).await;
continue;
}
write_callback_response(&mut stream, true).await;
return Ok(callback.code);
}
}
async fn request_token(
token_endpoint: &str,
parameters: &[(&str, &str)],
endpoint_policy: OidcEndpointPolicy,
) -> Result<OidcTokenResponse, MongoError> {
let token_endpoint = endpoint_policy.parse_url(token_endpoint, "token endpoint")?;
let client = reqwest::Client::builder()
.timeout(HTTP_TIMEOUT)
.build()
.map_err(|err| oidc_error(format!("failed to create HTTP client: {err}")))?;
let response = client
.post(token_endpoint)
.form(parameters)
.send()
.await
.map_err(|err| oidc_error(format!("token request failed: {err}")))?;
let status = response.status();
if !status.is_success() {
let body = response.text().await.unwrap_or_default();
let detail = serde_json::from_str::<serde_json::Value>(&body)
.ok()
.and_then(|value| {
value
.get("error_description")
.or_else(|| value.get("error"))
.and_then(serde_json::Value::as_str)
.map(str::to_string)
})
.unwrap_or_else(|| body.chars().take(300).collect());
return Err(oidc_error(format!("token endpoint returned HTTP {status}: {detail}")));
}
let token = response
.json::<OidcTokenResponse>()
.await
.map_err(|err| oidc_error(format!("invalid token response: {err}")))?;
if token.access_token.trim().is_empty() {
return Err(oidc_error("token response did not include an access token"));
}
Ok(token)
}
fn callback_deadline(timeout: Option<Instant>) -> Instant {
timeout.unwrap_or_else(|| Instant::now() + DEFAULT_CALLBACK_TIMEOUT)
}
fn remaining_callback_timeout(deadline: Instant) -> Result<Duration, MongoError> {
let remaining = deadline.saturating_duration_since(Instant::now());
if remaining.is_zero() {
return Err(oidc_error("browser authentication timed out"));
}
Ok(remaining)
}
async fn lock_browser_flow_until(deadline: Instant) -> Result<tokio::sync::MutexGuard<'static, ()>, MongoError> {
tokio::time::timeout(remaining_callback_timeout(deadline)?, browser_flow_lock().lock())
.await
.map_err(|_| oidc_error("browser authentication timed out"))
}
fn driver_token_response(token: OidcTokenResponse, previous_refresh_token: Option<String>) -> IdpServerResponse {
let expires = token.expires_in.and_then(|seconds| Instant::now().checked_add(Duration::from_secs(seconds)));
IdpServerResponse::builder()
.access_token(token.access_token)
.expires(expires)
.refresh_token(token.refresh_token.or(previous_refresh_token))
.build()
}
async fn authenticate(
context: CallbackContext,
opener: MongoOidcBrowserOpener,
) -> Result<IdpServerResponse, MongoError> {
authenticate_with_policy(context, opener, OidcEndpointPolicy::HttpsOnly).await
}
async fn authenticate_with_policy(
context: CallbackContext,
opener: MongoOidcBrowserOpener,
endpoint_policy: OidcEndpointPolicy,
) -> Result<IdpServerResponse, MongoError> {
let deadline = callback_deadline(context.timeout);
let info =
context.idp_info.ok_or_else(|| oidc_error("MongoDB server did not provide identity provider details"))?;
let client_id = info.client_id.ok_or_else(|| oidc_error("MongoDB server did not provide an OIDC client ID"))?;
let discovery = discover_provider(&info.issuer, endpoint_policy).await?;
if let Some(refresh_token) = context.refresh_token.clone() {
if let Ok(token) = request_token(
&discovery.token_endpoint,
&[
("grant_type", "refresh_token"),
("client_id", client_id.as_str()),
("refresh_token", refresh_token.as_str()),
],
endpoint_policy,
)
.await
{
return Ok(driver_token_response(token, Some(refresh_token)));
}
}
let _browser_guard = lock_browser_flow_until(deadline).await?;
let listener = TcpListener::bind(OIDC_CALLBACK_ADDRESS)
.await
.map_err(|err| oidc_error(format!("cannot listen on {OIDC_REDIRECT_URI}: {err}")))?;
let state = random_url_safe_value();
let verifier = random_url_safe_value();
let challenge = URL_SAFE_NO_PAD.encode(Sha256::digest(verifier.as_bytes()));
let scopes = requested_scopes(info.request_scopes, discovery.scopes_supported.as_deref());
let authorization_url = authorization_url_with_policy(
&discovery.authorization_endpoint,
&client_id,
&scopes,
&state,
&challenge,
endpoint_policy,
)?;
remaining_callback_timeout(deadline)?;
opener(authorization_url.as_str()).map_err(oidc_error)?;
let timeout = remaining_callback_timeout(deadline)?;
let code = tokio::time::timeout(timeout, wait_for_callback(listener, &state))
.await
.map_err(|_| oidc_error("browser authentication timed out"))??;
let token = request_token(
&discovery.token_endpoint,
&[
("grant_type", "authorization_code"),
("client_id", client_id.as_str()),
("code", code.as_str()),
("redirect_uri", OIDC_REDIRECT_URI),
("code_verifier", verifier.as_str()),
],
endpoint_policy,
)
.await?;
Ok(driver_token_response(token, None))
}
pub fn human_callback(opener: MongoOidcBrowserOpener) -> Callback {
Callback::human(move |context| {
let opener = opener.clone();
async move { authenticate(context, opener).await }.boxed()
})
}
#[cfg(test)]
mod tests {
use std::{
io::{Read, Write},
sync::{Arc, Mutex as StdMutex},
time::{Duration, Instant},
};
use mongodb::options::oidc::{CallbackContext, IdpServerInfo};
use tokio::{
io::{AsyncReadExt, AsyncWriteExt},
net::{TcpListener, TcpStream},
task::JoinHandle,
};
use super::{
authenticate_with_policy, authorization_url_with_policy, discovery_url_with_policy, parse_callback_target,
request_token, requested_scopes, validate_discovery_document, wait_for_callback, MongoOidcBrowserOpener,
OidcDiscoveryDocument, OidcEndpointPolicy, OIDC_REDIRECT_URI,
};
async fn read_http_request(stream: &mut tokio::net::TcpStream) -> String {
let mut request = Vec::new();
loop {
let mut chunk = [0_u8; 1024];
let read = tokio::io::AsyncReadExt::read(stream, &mut chunk).await.unwrap();
if read == 0 {
break;
}
request.extend_from_slice(&chunk[..read]);
let Some(header_end) = request.windows(4).position(|window| window == b"\r\n\r\n") else {
continue;
};
let headers = String::from_utf8_lossy(&request[..header_end]);
let content_length = headers
.lines()
.filter_map(|line| line.split_once(':'))
.find(|(name, _)| name.eq_ignore_ascii_case("content-length"))
.and_then(|(_, value)| value.trim().parse::<usize>().ok())
.unwrap_or(0);
if request.len() >= header_end + 4 + content_length {
break;
}
}
String::from_utf8(request).unwrap()
}
async fn write_json_response(stream: &mut tokio::net::TcpStream, body: &str) {
let response = format!(
"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}",
body.len()
);
stream.write_all(response.as_bytes()).await.unwrap();
}
async fn start_mock_oidc_server(expected_requests: usize) -> (String, Arc<StdMutex<Vec<String>>>, JoinHandle<()>) {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let issuer = format!("http://{}", listener.local_addr().unwrap());
let server_issuer = issuer.clone();
let requests = Arc::new(StdMutex::new(Vec::new()));
let server_requests = requests.clone();
let task = tokio::spawn(async move {
for _ in 0..expected_requests {
let (mut stream, _) = listener.accept().await.unwrap();
let request = read_http_request(&mut stream).await;
let request_line = request.lines().next().unwrap_or_default().to_string();
server_requests.lock().unwrap().push(request.clone());
if request_line.contains("/.well-known/openid-configuration") {
let body = serde_json::json!({
"issuer": server_issuer,
"authorization_endpoint": format!("{server_issuer}/authorize"),
"token_endpoint": format!("{server_issuer}/token"),
"scopes_supported": ["openid", "profile", "offline_access"]
})
.to_string();
write_json_response(&mut stream, &body).await;
} else if request.contains("grant_type=refresh_token") {
write_json_response(&mut stream, r#"{"access_token":"refreshed-token","expires_in":120}"#).await;
} else {
write_json_response(
&mut stream,
r#"{"access_token":"authorization-token","refresh_token":"refresh-token","expires_in":120}"#,
)
.await;
}
}
});
(issuer, requests, task)
}
fn callback_context(issuer: String, refresh_token: Option<String>) -> CallbackContext {
CallbackContext::builder()
.timeout(Some(Instant::now() + Duration::from_secs(5)))
.refresh_token(refresh_token)
.idp_info(Some(
IdpServerInfo::builder()
.issuer(issuer)
.client_id(Some("dbx-client".to_string()))
.request_scopes(Some(vec!["profile".to_string()]))
.build(),
))
.build()
}
#[test]
fn oidc_endpoints_require_https_and_exact_issuer_match() {
assert!(discovery_url_with_policy("http://idp.example", OidcEndpointPolicy::HttpsOnly).is_err());
assert!(authorization_url_with_policy(
"http://idp.example/authorize",
"dbx-client",
"openid",
"state",
"challenge",
OidcEndpointPolicy::HttpsOnly,
)
.is_err());
let issuer = "https://idp.example/realms/dbx";
let document = OidcDiscoveryDocument {
issuer: issuer.to_string(),
authorization_endpoint: "https://idp.example/authorize".to_string(),
token_endpoint: "https://idp.example/token".to_string(),
scopes_supported: None,
};
assert!(validate_discovery_document(issuer, document, OidcEndpointPolicy::HttpsOnly).is_ok());
let trailing_slash = OidcDiscoveryDocument {
issuer: format!("{issuer}/"),
authorization_endpoint: "https://idp.example/authorize".to_string(),
token_endpoint: "https://idp.example/token".to_string(),
scopes_supported: None,
};
assert!(validate_discovery_document(issuer, trailing_slash, OidcEndpointPolicy::HttpsOnly).is_err());
let insecure_endpoint = OidcDiscoveryDocument {
issuer: issuer.to_string(),
authorization_endpoint: "http://idp.example/authorize".to_string(),
token_endpoint: "https://idp.example/token".to_string(),
scopes_supported: None,
};
assert!(validate_discovery_document(issuer, insecure_endpoint, OidcEndpointPolicy::HttpsOnly).is_err());
}
#[tokio::test]
async fn token_endpoint_requires_https_before_network_request() {
let error = request_token("http://idp.example/token", &[], OidcEndpointPolicy::HttpsOnly)
.await
.unwrap_err()
.to_string();
assert!(error.contains("token endpoint must use HTTPS"));
}
#[tokio::test]
async fn callback_ignores_invalid_requests_until_matching_state() {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let address = listener.local_addr().unwrap();
let callback = tokio::spawn(wait_for_callback(listener, "expected-state"));
for target in ["/redirect?code=wrong&state=wrong-state", "/redirect?error=access_denied"] {
let mut stream = TcpStream::connect(address).await.unwrap();
stream
.write_all(format!("GET {target} HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n").as_bytes())
.await
.unwrap();
let mut response = String::new();
stream.read_to_string(&mut response).await.unwrap();
assert!(response.starts_with("HTTP/1.1 400 Bad Request"));
}
let mut stream = TcpStream::connect(address).await.unwrap();
stream
.write_all(b"GET /redirect?code=valid-code&state=expected-state HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n")
.await
.unwrap();
let mut response = String::new();
stream.read_to_string(&mut response).await.unwrap();
assert!(response.starts_with("HTTP/1.1 200 OK"));
assert_eq!(callback.await.unwrap().unwrap(), "valid-code");
}
#[tokio::test]
async fn browser_lock_timeout_includes_time_waiting_for_lock() {
let _guard = super::browser_flow_lock().lock().await;
let error =
super::lock_browser_flow_until(Instant::now() + Duration::from_millis(20)).await.unwrap_err().to_string();
assert!(error.contains("browser authentication timed out"));
}
#[test]
fn discovery_url_preserves_issuer_path() {
assert_eq!(
discovery_url_with_policy("https://idp.example/realms/dbx", OidcEndpointPolicy::HttpsOnly)
.unwrap()
.as_str(),
"https://idp.example/realms/dbx/.well-known/openid-configuration"
);
}
#[test]
fn authorization_request_uses_pkce_and_registered_redirect() {
let url = authorization_url_with_policy(
"https://idp.example/authorize",
"dbx-client",
"openid profile offline_access",
"expected-state",
"challenge",
OidcEndpointPolicy::HttpsOnly,
)
.unwrap();
let params = url.query_pairs().collect::<std::collections::HashMap<_, _>>();
assert_eq!(params.get("response_type").map(|value| value.as_ref()), Some("code"));
assert_eq!(params.get("redirect_uri").map(|value| value.as_ref()), Some(OIDC_REDIRECT_URI));
assert_eq!(params.get("code_challenge_method").map(|value| value.as_ref()), Some("S256"));
assert_eq!(params.get("state").map(|value| value.as_ref()), Some("expected-state"));
}
#[test]
fn required_scopes_are_added_without_duplicates() {
assert_eq!(requested_scopes(Some(vec!["profile".to_string()]), None), "openid profile offline_access");
assert_eq!(
requested_scopes(Some(vec!["openid".to_string(), "offline_access".to_string()]), None),
"openid offline_access"
);
assert_eq!(
requested_scopes(Some(vec!["profile".to_string()]), Some(&["openid".to_string(), "profile".to_string()])),
"openid profile"
);
}
#[test]
fn callback_requires_code_and_state() {
let callback = parse_callback_target("/redirect?code=abc&state=xyz").unwrap();
assert_eq!(callback.code, "abc");
assert_eq!(callback.state, "xyz");
assert!(parse_callback_target("/redirect?error=access_denied&error_description=cancelled")
.unwrap_err()
.contains("access_denied: cancelled"));
}
#[tokio::test]
async fn authorization_code_flow_exchanges_pkce_code() {
let (issuer, requests, server) = start_mock_oidc_server(2).await;
let opened_url = Arc::new(StdMutex::new(None));
let captured_url = opened_url.clone();
let opener: MongoOidcBrowserOpener = Arc::new(move |url| {
let parsed = reqwest::Url::parse(url).unwrap();
let state =
parsed.query_pairs().find_map(|(key, value)| (key == "state").then(|| value.into_owned())).unwrap();
*captured_url.lock().unwrap() = Some(parsed);
std::thread::spawn(move || {
let mut stream = std::net::TcpStream::connect("127.0.0.1:27097").unwrap();
write!(stream, "GET /redirect?code=authorization-code&state={state}").unwrap();
std::thread::sleep(Duration::from_millis(10));
write!(stream, " HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n").unwrap();
let mut response = String::new();
stream.read_to_string(&mut response).unwrap();
assert!(response.starts_with("HTTP/1.1 200 OK"));
});
Ok(())
});
let token =
authenticate_with_policy(callback_context(issuer, None), opener, OidcEndpointPolicy::AllowLoopbackHttp)
.await
.unwrap();
server.await.unwrap();
assert_eq!(token.access_token, "authorization-token");
assert_eq!(token.refresh_token.as_deref(), Some("refresh-token"));
assert!(token.expires.is_some_and(|expires| expires > Instant::now()));
let authorization_url = opened_url.lock().unwrap().clone().unwrap();
assert_eq!(
authorization_url.query_pairs().find(|(key, _)| key == "scope").unwrap().1,
"openid profile offline_access"
);
assert!(authorization_url.query_pairs().any(|(key, value)| key == "code_challenge" && !value.is_empty()));
let requests = requests.lock().unwrap();
assert!(requests[1].contains("grant_type=authorization_code"));
assert!(requests[1].contains("code=authorization-code"));
assert!(requests[1].contains("code_verifier="));
}
#[tokio::test]
async fn refresh_token_avoids_browser_and_preserves_refresh_token() {
let (issuer, requests, server) = start_mock_oidc_server(2).await;
let opener: MongoOidcBrowserOpener = Arc::new(|_| Err("browser should not be opened".to_string()));
let token = authenticate_with_policy(
callback_context(issuer, Some("cached-refresh-token".to_string())),
opener,
OidcEndpointPolicy::AllowLoopbackHttp,
)
.await
.unwrap();
server.await.unwrap();
assert_eq!(token.access_token, "refreshed-token");
assert_eq!(token.refresh_token.as_deref(), Some("cached-refresh-token"));
let requests = requests.lock().unwrap();
assert!(requests[1].contains("grant_type=refresh_token"));
assert!(requests[1].contains("refresh_token=cached-refresh-token"));
}
}
File diff suppressed because it is too large. Load diff
-7
View File
@@ -1,7 +0,0 @@
pub const DUCKDB_WORKER_MAX_PROCESSES_MIN: usize = 1;
pub const DUCKDB_WORKER_MAX_PROCESSES_MAX: usize = 16;
pub const DUCKDB_WORKER_MAX_PROCESSES_DEFAULT: usize = 4;
pub fn normalize_duckdb_worker_max_processes(value: usize) -> usize {
value.clamp(DUCKDB_WORKER_MAX_PROCESSES_MIN, DUCKDB_WORKER_MAX_PROCESSES_MAX)
}
-498
View File
@@ -1,498 +0,0 @@
use serde::Serialize;
use crate::models::connection::SshTunnelConfig;
use crate::path_utils::expand_tilde;
/// Legacy sentinel values used by connection payloads that predate the
/// explicit SSH authentication method field. Current connection forms always
/// set `auth_method`, so their user and port values must be treated as
/// explicit, including the valid defaults `root` and `22`.
const DEFAULT_USER_SENTINEL: &str = "root";
const DEFAULT_PORT_SENTINEL: u16 = 22;
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct SshConfigHostEntry {
pub alias: String,
pub host_name: Option<String>,
pub port: Option<u16>,
pub user: Option<String>,
pub identity_file: Option<String>,
/// The `ProxyJump` alias, if any. Only a single hop is parsed (OpenSSH's
/// comma-separated multi-jump form is not supported); the jump itself
/// may declare its own `ProxyJump`, which `resolve_ssh_tunnel_chain`
/// follows recursively.
pub proxy_jump: Option<String>,
}
/// Reads and parses `~/.ssh/config`. Returns an empty list (not an error) if
/// the file does not exist, since that's a normal state for users without
/// an SSH config.
pub fn list_hosts() -> Result<Vec<SshConfigHostEntry>, String> {
let path = expand_tilde("~/.ssh/config");
match std::fs::read_to_string(&path) {
Ok(content) => Ok(parse_ssh_config(&content)),
Err(err) if err.kind() == std::io::ErrorKind::NotFound => Ok(Vec::new()),
Err(err) => Err(format!("Failed to read {path}: {err}")),
}
}
pub fn find_host(alias: &str) -> Option<SshConfigHostEntry> {
list_hosts().ok()?.into_iter().find(|entry| entry.alias == alias)
}
/// Fills in `host`, `user`, `port`, and `key_path` from a matching `~/.ssh/config`
/// `Host` block, without overwriting values the user has explicitly set.
///
/// Only `ssh.host` is matched against config aliases; `user`/`port`/`key_path`
/// are filled in from that same matched entry. Older payloads without an
/// `auth_method` retain the legacy default-filling behavior so imported
/// connections continue to resolve their aliases. Payloads produced by the
/// current connection form keep all user and port values as entered.
pub fn resolve_ssh_tunnel_config(ssh: &SshTunnelConfig) -> SshTunnelConfig {
match find_host(&ssh.host) {
Some(entry) => apply_host_entry(ssh, entry),
None => ssh.clone(),
}
}
/// Resolves `ssh.host` plus, when its `~/.ssh/config` entry (or an ancestor
/// reached by following `ProxyJump`) declares `ProxyJump`, every jump host
/// leading to it. The returned list is in connection order — outermost jump
/// first, `ssh`'s own resolved config last — and always has at least one
/// element. A chain of length 1 means there is no `ProxyJump` to honor, and
/// behaves exactly like [`resolve_ssh_tunnel_config`].
///
/// Jump hosts have no dedicated credentials field in DBX's connection form,
/// so each one inherits `ssh`'s password/key/agent settings; only the
/// host/port/user/identity file come from its own `~/.ssh/config` entry.
pub fn resolve_ssh_tunnel_chain(ssh: &SshTunnelConfig) -> Vec<SshTunnelConfig> {
resolve_chain_from_entries(ssh, &list_hosts().unwrap_or_default())
}
fn resolve_chain_from_entries(ssh: &SshTunnelConfig, entries: &[SshConfigHostEntry]) -> Vec<SshTunnelConfig> {
let find = |alias: &str| entries.iter().find(|entry| entry.alias == alias).cloned();
let Some(target_entry) = find(&ssh.host) else {
return vec![ssh.clone()];
};
let resolved_target = apply_host_entry(ssh, target_entry.clone());
let mut jump_hops = Vec::new();
let mut seen_aliases = std::collections::HashSet::new();
seen_aliases.insert(ssh.host.clone());
let mut next_jump = target_entry.proxy_jump.clone();
while let Some(alias) = next_jump {
// Guard against a cycle in `~/.ssh/config` (e.g. two hosts naming
// each other as ProxyJump) rather than looping forever.
if !seen_aliases.insert(alias.clone()) {
break;
}
let Some(entry) = find(&alias) else { break };
next_jump = entry.proxy_jump.clone();
jump_hops.push(jump_hop_from_entry(ssh, entry));
}
jump_hops.reverse();
jump_hops.push(resolved_target);
jump_hops
}
/// Builds a synthetic hop for a `ProxyJump` host. Unlike the leaf host in
/// `ssh`, there's no user-filled form for this host, so its identity comes
/// entirely from `~/.ssh/config` (falling back to the leaf's own settings
/// for anything the config entry doesn't specify).
fn jump_hop_from_entry(leaf: &SshTunnelConfig, entry: SshConfigHostEntry) -> SshTunnelConfig {
SshTunnelConfig {
id: format!("{}::proxyjump::{}", leaf.id, entry.alias),
name: entry.alias.clone(),
enabled: true,
host: entry.host_name.unwrap_or_else(|| entry.alias.clone()),
port: entry.port.unwrap_or(DEFAULT_PORT_SENTINEL),
user: entry.user.unwrap_or_else(|| leaf.user.clone()),
password: leaf.password.clone(),
key_path: entry.identity_file.unwrap_or_else(|| leaf.key_path.clone()),
key_passphrase: leaf.key_passphrase.clone(),
connect_timeout_secs: leaf.connect_timeout_secs,
expose_lan: false,
use_ssh_agent: leaf.use_ssh_agent,
ssh_agent_sock_path: leaf.ssh_agent_sock_path.clone(),
auth_method: leaf.auth_method.clone(),
allow_exec_channel_proxy: leaf.allow_exec_channel_proxy,
profile_id: String::new(),
}
}
/// Applies a resolved `~/.ssh/config` entry onto `ssh`, without overwriting
/// values the user has explicitly set. The empty `auth_method` identifies
/// legacy payloads where default values cannot be distinguished from fields
/// that were left blank in the old form.
fn apply_host_entry(ssh: &SshTunnelConfig, entry: SshConfigHostEntry) -> SshTunnelConfig {
let mut resolved = ssh.clone();
let is_legacy_payload = ssh.auth_method.is_empty();
if let Some(host_name) = entry.host_name {
resolved.host = host_name;
}
if is_legacy_payload && resolved.user == DEFAULT_USER_SENTINEL {
if let Some(user) = entry.user {
resolved.user = user;
}
}
if is_legacy_payload && resolved.port == DEFAULT_PORT_SENTINEL {
if let Some(port) = entry.port {
resolved.port = port;
}
}
if resolved.key_path.is_empty() {
if let Some(identity_file) = entry.identity_file {
resolved.key_path = identity_file;
// If the SSH config supplied the only usable credential, make the
// backend use it even when an older/default UI payload still says
// "password" with an empty password.
if resolved.auth_method.is_empty() || (resolved.auth_method == "password" && resolved.password.is_empty()) {
resolved.auth_method = "key".to_string();
}
}
}
resolved
}
/// Parses a minimal subset of OpenSSH client config syntax: `Host`, `HostName`,
/// `Port`, `User`, `IdentityFile`, `ProxyJump`. Wildcard host patterns
/// (containing `*` or `?`) are skipped since they aren't usable as a literal
/// alias in the host field. `Include` and other directives are not
/// supported, and `ProxyJump`'s comma-separated multi-hop form is read as a
/// single alias (its first hop).
fn parse_ssh_config(content: &str) -> Vec<SshConfigHostEntry> {
let mut entries: Vec<SshConfigHostEntry> = Vec::new();
let mut current_aliases: Vec<String> = Vec::new();
for raw_line in content.lines() {
let line = strip_comment(raw_line).trim();
if line.is_empty() {
continue;
}
let Some((keyword, value)) = split_directive(line) else {
continue;
};
match keyword.to_ascii_lowercase().as_str() {
"host" => {
current_aliases = value
.split_whitespace()
.filter(|alias| !alias.contains('*') && !alias.contains('?'))
.map(str::to_string)
.collect();
for alias in &current_aliases {
entries.push(SshConfigHostEntry {
alias: alias.clone(),
host_name: None,
port: None,
user: None,
identity_file: None,
proxy_jump: None,
});
}
}
"hostname" => set_current_field(&mut entries, &current_aliases, |entry| {
entry.host_name = Some(value.to_string());
}),
"port" => {
if let Ok(port) = value.parse::<u16>() {
set_current_field(&mut entries, &current_aliases, |entry| {
entry.port = Some(port);
});
}
}
"user" => set_current_field(&mut entries, &current_aliases, |entry| {
entry.user = Some(value.to_string());
}),
"identityfile" => set_current_field(&mut entries, &current_aliases, |entry| {
entry.identity_file = Some(value.to_string());
}),
"proxyjump" => {
if let Some(first_hop) = value.split(',').next().map(str::trim).filter(|hop| !hop.is_empty()) {
// A `user@host:port` jump target names an alias-incompatible
// literal host; only a plain alias is resolvable here.
if !first_hop.contains('@') && !first_hop.contains(':') {
let first_hop = first_hop.to_string();
set_current_field(&mut entries, &current_aliases, |entry| {
entry.proxy_jump = Some(first_hop.clone());
});
}
}
}
_ => {}
}
}
entries
}
fn set_current_field(
entries: &mut [SshConfigHostEntry],
current_aliases: &[String],
apply: impl Fn(&mut SshConfigHostEntry),
) {
for entry in entries.iter_mut() {
if current_aliases.contains(&entry.alias) {
apply(entry);
}
}
}
fn strip_comment(line: &str) -> &str {
match line.find('#') {
Some(index) => &line[..index],
None => line,
}
}
/// Splits a config line into `(keyword, value)`. OpenSSH allows the keyword
/// and value to be separated by whitespace or a single `=`.
fn split_directive(line: &str) -> Option<(&str, &str)> {
let line = line.trim();
let split_index = line.find(|c: char| c.is_whitespace() || c == '=')?;
let keyword = &line[..split_index];
let value = line[split_index..].trim_start_matches(|c: char| c.is_whitespace() || c == '=').trim();
if keyword.is_empty() || value.is_empty() {
return None;
}
Some((keyword, value))
}
#[cfg(test)]
mod tests {
use super::*;
fn config(host: &str) -> SshTunnelConfig {
SshTunnelConfig {
profile_id: String::new(),
id: "1".to_string(),
name: String::new(),
enabled: true,
host: host.to_string(),
port: DEFAULT_PORT_SENTINEL,
user: DEFAULT_USER_SENTINEL.to_string(),
password: String::new(),
key_path: String::new(),
key_passphrase: String::new(),
connect_timeout_secs: 5,
expose_lan: false,
use_ssh_agent: false,
ssh_agent_sock_path: String::new(),
auth_method: String::new(),
allow_exec_channel_proxy: false,
}
}
#[test]
fn parses_basic_host_block() {
let entries = parse_ssh_config(
"Host myserver\n HostName 10.0.0.5\n Port 2222\n User deploy\n IdentityFile ~/.ssh/id_ed25519\n",
);
assert_eq!(entries.len(), 1);
let entry = &entries[0];
assert_eq!(entry.alias, "myserver");
assert_eq!(entry.host_name, Some("10.0.0.5".to_string()));
assert_eq!(entry.port, Some(2222));
assert_eq!(entry.user, Some("deploy".to_string()));
assert_eq!(entry.identity_file, Some("~/.ssh/id_ed25519".to_string()));
}
#[test]
fn one_line_can_declare_multiple_aliases() {
let entries = parse_ssh_config("Host prod prod-alias\n HostName 10.0.0.9\n");
assert_eq!(entries.len(), 2);
assert!(entries.iter().all(|entry| entry.host_name == Some("10.0.0.9".to_string())));
assert_eq!(entries[0].alias, "prod");
assert_eq!(entries[1].alias, "prod-alias");
}
#[test]
fn skips_wildcard_host_patterns() {
let entries = parse_ssh_config("Host *.example.com\n User git\nHost real\n User deploy\n");
assert_eq!(entries.len(), 1);
assert_eq!(entries[0].alias, "real");
}
#[test]
fn ignores_comments_and_blank_lines() {
let entries = parse_ssh_config("# a comment\n\nHost myserver # inline comment\n User deploy\n");
assert_eq!(entries.len(), 1);
assert_eq!(entries[0].user, Some("deploy".to_string()));
}
#[test]
fn parses_proxy_jump() {
let entries = parse_ssh_config("Host target\n HostName 1.1.1.2\n ProxyJump gateway\n");
assert_eq!(entries[0].proxy_jump, Some("gateway".to_string()));
}
#[test]
fn proxy_jump_multi_hop_form_reads_only_the_first_hop() {
let entries = parse_ssh_config("Host target\n ProxyJump gw1,gw2\n");
assert_eq!(entries[0].proxy_jump, Some("gw1".to_string()));
}
#[test]
fn proxy_jump_user_at_host_form_is_not_resolvable_as_an_alias() {
let entries = parse_ssh_config("Host target\n ProxyJump jumper@1.1.1.1\n");
assert_eq!(entries[0].proxy_jump, None);
}
fn entry(alias: &str) -> SshConfigHostEntry {
SshConfigHostEntry {
alias: alias.to_string(),
host_name: Some("10.0.0.5".to_string()),
port: Some(2222),
user: Some("deploy".to_string()),
identity_file: Some("~/.ssh/id_ed25519".to_string()),
proxy_jump: None,
}
}
#[test]
fn resolve_fills_unset_fields_from_matching_alias() {
let ssh = config("myserver");
let resolved = apply_host_entry(&ssh, entry("myserver"));
assert_eq!(resolved.host, "10.0.0.5");
assert_eq!(resolved.port, 2222);
assert_eq!(resolved.user, "deploy");
assert_eq!(resolved.key_path, "~/.ssh/id_ed25519");
assert_eq!(resolved.auth_method, "key");
}
#[test]
fn resolve_keeps_password_auth_when_password_is_present() {
let mut ssh = config("myserver");
ssh.password = "secret".to_string();
ssh.auth_method = "password".to_string();
let resolved = apply_host_entry(&ssh, entry("myserver"));
assert_eq!(resolved.key_path, "~/.ssh/id_ed25519");
assert_eq!(resolved.auth_method, "password");
}
#[test]
fn resolve_keeps_default_user_and_port_for_current_payloads() {
let mut ssh = config("myserver");
ssh.auth_method = "password".to_string();
let resolved = apply_host_entry(&ssh, entry("myserver"));
assert_eq!(resolved.host, "10.0.0.5");
assert_eq!(resolved.user, DEFAULT_USER_SENTINEL);
assert_eq!(resolved.port, DEFAULT_PORT_SENTINEL);
}
#[test]
fn resolve_preserves_key_plus_password_method() {
let mut ssh = config("myserver");
ssh.auth_method = "key+password".to_string();
ssh.password = "secret".to_string();
let resolved = apply_host_entry(&ssh, entry("myserver"));
assert_eq!(resolved.key_path, "~/.ssh/id_ed25519");
assert_eq!(resolved.auth_method, "key+password");
}
#[test]
fn resolve_does_not_override_explicit_values() {
let mut ssh = config("myserver");
ssh.user = "alice".to_string();
ssh.port = 9999;
ssh.key_path = "/explicit/key".to_string();
let resolved = apply_host_entry(&ssh, entry("myserver"));
assert_eq!(resolved.host, "10.0.0.5");
assert_eq!(resolved.user, "alice");
assert_eq!(resolved.port, 9999);
assert_eq!(resolved.key_path, "/explicit/key");
}
#[test]
fn resolve_is_noop_when_host_does_not_match_any_alias() {
// `resolve_ssh_tunnel_config` looks up the real `~/.ssh/config`; an
// alias this unlikely to exist on a test machine exercises the
// "no match found" branch without needing to mock the filesystem.
let ssh = config("dbx-test-alias-that-should-never-exist-anywhere");
let resolved = resolve_ssh_tunnel_config(&ssh);
assert_eq!(resolved, ssh);
}
fn jump_entry(alias: &str, host_name: &str, proxy_jump: Option<&str>) -> SshConfigHostEntry {
SshConfigHostEntry {
alias: alias.to_string(),
host_name: Some(host_name.to_string()),
port: None,
user: None,
identity_file: None,
proxy_jump: proxy_jump.map(str::to_string),
}
}
#[test]
fn chain_is_single_hop_without_proxy_jump() {
let ssh = config("myserver");
let chain = resolve_chain_from_entries(&ssh, std::slice::from_ref(&entry("myserver")));
assert_eq!(chain.len(), 1);
assert_eq!(chain[0].host, "10.0.0.5");
}
#[test]
fn chain_prepends_the_proxy_jump_hop_in_connection_order() {
// Mirrors issue #7706: `target` (ProxyJump gateway) resolves to
// [gateway, target], gateway dialed first.
let ssh = config("target");
let entries = [jump_entry("gateway", "1.1.1.1", None), jump_entry("target", "1.1.1.2", Some("gateway"))];
let chain = resolve_chain_from_entries(&ssh, &entries);
assert_eq!(chain.len(), 2);
assert_eq!(chain[0].host, "1.1.1.1");
assert_eq!(chain[0].name, "gateway");
assert_eq!(chain[1].host, "1.1.1.2");
}
#[test]
fn chain_jump_hop_inherits_leaf_credentials_when_config_has_no_identity_file() {
let mut ssh = config("target");
ssh.auth_method = "key".to_string();
ssh.key_path = "/home/user/.ssh/id_rsa".to_string();
let entries = [jump_entry("gateway", "1.1.1.1", None), jump_entry("target", "1.1.1.2", Some("gateway"))];
let chain = resolve_chain_from_entries(&ssh, &entries);
assert_eq!(chain[0].auth_method, "key");
assert_eq!(chain[0].key_path, "/home/user/.ssh/id_rsa");
}
#[test]
fn chain_follows_multiple_proxy_jump_hops_recursively() {
let ssh = config("target");
let entries = [
jump_entry("gateway-outer", "1.1.1.1", None),
jump_entry("gateway-inner", "1.1.1.2", Some("gateway-outer")),
jump_entry("target", "1.1.1.3", Some("gateway-inner")),
];
let chain = resolve_chain_from_entries(&ssh, &entries);
assert_eq!(
chain.iter().map(|hop| hop.host.as_str()).collect::<Vec<_>>(),
vec!["1.1.1.1", "1.1.1.2", "1.1.1.3"]
);
}
#[test]
fn chain_breaks_a_proxy_jump_cycle_instead_of_looping_forever() {
let ssh = config("a");
let entries = [jump_entry("a", "1.1.1.1", Some("b")), jump_entry("b", "1.1.1.2", Some("a"))];
let chain = resolve_chain_from_entries(&ssh, &entries);
// Must terminate; the exact truncation point is an implementation
// detail of cycle detection, not a behavior callers rely on.
assert!(chain.len() <= entries.len());
assert_eq!(chain.last().unwrap().host, "1.1.1.1");
}
}