fix(sqlite): validate ssh backup and query errors

This commit is contained in:
skyler authored and GitHub committed 2026-08-30 13:30:00 +08:00
1 parent e6f8b2187a
commit 9190554e50
58 files changed
+2815 -149

No files matched your search

+7
View File
@@ -93,6 +93,13 @@ mod tests {
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"));
+22 -2
View File
@@ -14,6 +14,8 @@ use crate::db::agent_driver::{
use crate::models::connection::DatabaseType;
pub const DEFAULT_JRE_KEY: &str = "21";
pub const SQLITE_WORKER_DRIVER_KEY: &str = "sqlite-worker";
pub const SQLITE_WORKER_NATIVE_PLATFORMS: &[&str] = &["linux-x64", "linux-aarch64"];
pub const DOWNLOAD_CACHE_DIR_NAME: &str = "download-cache";
pub const DOWNLOAD_CACHE_MAX_AGE_DAYS: u64 = 7;
@@ -777,6 +779,24 @@ impl AgentManager {
self.driver_dir(db_type).join(executable_name)
}
pub fn driver_native_platform_path(&self, db_type: &str, platform: &str) -> PathBuf {
self.driver_dir(db_type).join(platform)
}
pub fn is_sqlite_worker_driver(db_type: &str) -> bool {
db_type == SQLITE_WORKER_DRIVER_KEY
}
pub fn driver_native_installed(&self, db_type: &str) -> bool {
if Self::is_sqlite_worker_driver(db_type) {
SQLITE_WORKER_NATIVE_PLATFORMS
.iter()
.all(|platform| self.driver_native_platform_path(db_type, platform).is_file())
} else {
self.driver_native_path(db_type).exists()
}
}
pub fn driver_launch_config_path(&self, db_type: &str) -> PathBuf {
self.driver_dir(db_type).join("agent-launch.json")
}
@@ -813,7 +833,7 @@ impl AgentManager {
pub fn is_driver_installed(&self, db_type: &str) -> bool {
self.is_driver_jar_valid(db_type)
|| self.driver_native_path(db_type).exists()
|| self.driver_native_installed(db_type)
|| self.driver_launch_config_path(db_type).exists()
}
@@ -823,7 +843,7 @@ impl AgentManager {
pub fn driver_requires_java_runtime(&self, db_type: &str) -> bool {
self.is_driver_jar_valid(db_type)
&& !self.driver_native_path(db_type).exists()
&& !self.driver_native_installed(db_type)
&& !self.driver_launch_config_path(db_type).exists()
}
@@ -123,6 +123,9 @@ fn preview_agent_offline_export_unlocked(am: &AgentManager) -> AgentOfflineExpor
let mut candidates = Vec::new();
for (db_type, label) in agent_catalog::driver_store_entries() {
if db_type == crate::agent_manager::SQLITE_WORKER_DRIVER_KEY {
continue;
}
let has_artifact = am.driver_launch_config_path(db_type).exists()
|| am.driver_native_path(db_type).exists()
|| am.driver_jar_path(db_type).exists();
+278 -45
View File
@@ -10,7 +10,8 @@ use sha2::{Digest, Sha256};
use crate::agent_catalog;
use crate::agent_manager::{
AgentDriverInfo, AgentInstallCancellation, AgentManager, AgentRegistry, ArtifactFormat, ArtifactInfo,
InstalledDriver, JavaRuntimeMode, OperationLockHandle, DEFAULT_JRE_KEY,
InstalledDriver, JavaRuntimeMode, OperationLockHandle, DEFAULT_JRE_KEY, SQLITE_WORKER_DRIVER_KEY,
SQLITE_WORKER_NATIVE_PLATFORMS,
};
use crate::DownloadSource;
@@ -213,7 +214,7 @@ pub fn build_agent_list(am: &AgentManager, registry: Option<&AgentRegistry>) ->
agent_catalog::driver_store_entries()
.map(|(key, label)| {
let jar_valid = am.is_driver_jar_valid(key);
let native_installed = am.driver_native_path(key).exists();
let native_installed = am.driver_native_installed(key);
let launch_config_installed = am.driver_launch_config_path(key).exists();
let installed = jar_valid || native_installed || launch_config_installed;
let local = local_state.installed_drivers.get(key);
@@ -239,7 +240,7 @@ pub fn build_agent_list(am: &AgentManager, registry: Option<&AgentRegistry>) ->
db_type: key.to_string(),
label: label.to_string(),
version: remote.map(|r| r.version.clone()).unwrap_or_default(),
size: remote.and_then(driver_download_artifact).map(|artifact| artifact.size).unwrap_or(0),
size: remote.map(|driver| driver_download_size(key, driver)).unwrap_or(0),
installed,
installed_version: local.map(|l| l.version.clone()),
update_available: match (local, remote) {
@@ -254,12 +255,27 @@ pub fn build_agent_list(am: &AgentManager, registry: Option<&AgentRegistry>) ->
.collect()
}
fn usable_driver_jar(driver: &crate::agent_manager::DriverInfo) -> Option<&crate::agent_manager::ArtifactInfo> {
driver.jar.as_ref().filter(|artifact| artifact.size > 0)
}
fn driver_download_artifact(driver: &crate::agent_manager::DriverInfo) -> Option<&crate::agent_manager::ArtifactInfo> {
driver.native.get(AgentManager::current_platform()).or(driver.jar.as_ref())
driver.native.get(AgentManager::current_platform()).or_else(|| usable_driver_jar(driver))
}
fn driver_download_size(db_type: &str, driver: &crate::agent_manager::DriverInfo) -> u64 {
if AgentManager::is_sqlite_worker_driver(db_type) {
return SQLITE_WORKER_NATIVE_PLATFORMS
.iter()
.filter_map(|platform| driver.native.get(*platform))
.map(|artifact| artifact.size)
.sum();
}
driver_download_artifact(driver).map(|artifact| artifact.size).unwrap_or(0)
}
fn remote_driver_requires_java_runtime(driver: &crate::agent_manager::DriverInfo) -> bool {
driver.jar.is_some() && !driver.native.contains_key(AgentManager::current_platform())
usable_driver_jar(driver).is_some() && !driver.native.contains_key(AgentManager::current_platform())
}
fn installed_jre_version<'a>(state: &'a crate::agent_manager::AgentState, jre_key: &str) -> Option<&'a String> {
@@ -489,6 +505,22 @@ pub async fn install_agent_driver(
install_agent_driver_from(am, db_type, DownloadSource::Official, progress).await
}
/// Ensure both Linux worker binaries are available for remote SQLite over SSH.
///
/// Unlike a regular native Agent, this driver is selected by the remote SSH
/// host's architecture rather than by the desktop application's platform.
pub async fn ensure_sqlite_worker_driver_ready(am: &AgentManager) -> Result<(), String> {
if am.driver_native_installed(SQLITE_WORKER_DRIVER_KEY) {
return Ok(());
}
install_agent_driver(am, SQLITE_WORKER_DRIVER_KEY, |_| {}).await?;
if am.driver_native_installed(SQLITE_WORKER_DRIVER_KEY) {
Ok(())
} else {
Err("SQLite SSH worker installation completed without both Linux binaries".to_string())
}
}
/// Like `install_agent_driver`, but using a command-scoped cancellation token
/// that was registered before any awaitable setup (blocker check, lock wait,
/// registry fetch) so a cancel fired during that window is observed.
@@ -1325,6 +1357,81 @@ async fn install_local_agent_with_registry_jre(
Ok(())
}
async fn install_sqlite_worker_from_registry(
am: &AgentManager,
source: DownloadSource,
db_type: &str,
driver: &crate::agent_manager::DriverInfo,
progress: &impl Fn(AgentProgressEvent),
current: Option<u32>,
total_drivers: Option<u32>,
cancellations: &[&AgentInstallCancellation],
) -> Result<(), String> {
let jre_key = &driver.jre;
std::fs::create_dir_all(am.driver_dir(db_type))
.map_err(|err| format!("Failed to create driver directory: {err}"))?;
for platform in SQLITE_WORKER_NATIVE_PLATFORMS {
let artifact = driver
.native
.get(*platform)
.ok_or_else(|| format!("SQLite SSH worker registry is missing the {platform} native package"))?;
let target_path = am.driver_native_platform_path(db_type, platform);
let download_path = driver_artifact_download_path(&target_path, artifact.format);
progress(AgentProgressEvent::transfer("driver", 0, artifact.size).with_batch(
Some(db_type),
current,
total_drivers,
));
download_with_progress(
am,
progress,
"driver",
source,
&artifact.url,
&r2_path_with_cache_buster(&github_url_to_r2_path(&artifact.url, "driver"), &driver.version),
&download_path,
artifact.size,
artifact.sha256.as_deref(),
Some(CacheIdentity::Driver { db_type, version: &driver.version }),
Some(db_type),
current,
total_drivers,
cancellations,
)
.await?;
if cancellations.iter().any(|token| token.is_cancelled()) {
std::fs::remove_file(&download_path).ok();
return Err(AGENT_DOWNLOAD_CANCELED_ERROR.to_string());
}
install_downloaded_driver_artifact(
&download_path,
&target_path,
artifact.format,
DriverArtifactKind::Native,
db_type,
&driver.version,
Some(platform),
)?;
mark_executable(&target_path)?;
}
std::fs::remove_file(am.driver_jar_path(db_type)).ok();
std::fs::remove_file(am.driver_native_path(db_type)).ok();
am.mutate_state(|state| {
state.installed_drivers.insert(
db_type.to_string(),
InstalledDriver {
version: driver.version.clone(),
installed_at: chrono::Utc::now().to_rfc3339(),
jre: jre_key.clone(),
},
);
})?;
am.stop_daemon_by_key(db_type).await;
cleanup_driver_download_cache_after_success(am, db_type);
progress(AgentProgressEvent::step("done").with_batch(Some(db_type), current, total_drivers));
Ok(())
}
async fn install_agent_driver_from_registry(
am: &AgentManager,
registry: &AgentRegistry,
@@ -1353,9 +1460,22 @@ async fn install_agent_driver_from_registry(
}
return Err(format!("Unknown driver type: {db_type}"));
};
if AgentManager::is_sqlite_worker_driver(db_type) {
return install_sqlite_worker_from_registry(
am,
source,
db_type,
driver,
progress,
current,
total_drivers,
cancellations,
)
.await;
}
let jre_key = &driver.jre;
let native_artifact = driver.native.get(AgentManager::current_platform());
let jar_artifact = driver.jar.as_ref();
let jar_artifact = usable_driver_jar(driver);
let requires_java_runtime = native_artifact.is_none();
let needs_jre = requires_java_runtime && jre_needs_install(am, registry, jre_key);
@@ -1421,6 +1541,7 @@ async fn install_agent_driver_from_registry(
artifact_kind,
db_type,
&driver.version,
is_native_artifact.then_some(AgentManager::current_platform()),
)?;
// Some drivers publish both a native agent and a legacy JAR fallback. Only
// validate the artifact type that was actually installed.
@@ -1479,12 +1600,18 @@ fn install_downloaded_driver_artifact(
artifact_kind: DriverArtifactKind,
db_type: &str,
expected_version: &str,
native_platform: Option<&str>,
) -> Result<(), String> {
let result = match format {
None => replace_download(download_path, target_path),
Some(ArtifactFormat::TarZstd) => {
install_driver_from_tar_zstd_package(download_path, target_path, artifact_kind, db_type, expected_version)
}
Some(ArtifactFormat::TarZstd) => install_driver_from_tar_zstd_package(
download_path,
target_path,
artifact_kind,
db_type,
expected_version,
native_platform,
),
};
if result.is_ok() {
std::fs::remove_file(download_path).ok();
@@ -1498,6 +1625,7 @@ fn install_driver_from_tar_zstd_package(
expected_kind: DriverArtifactKind,
db_type: &str,
expected_version: &str,
native_platform: Option<&str>,
) -> Result<(), String> {
let info = tar_zstd_driver_package_info(package_path)?;
if info.db_type != db_type {
@@ -1525,7 +1653,10 @@ fn install_driver_from_tar_zstd_package(
return Err(format!("Packaged driver jar is invalid or corrupt: {}", info.entry_name));
}
DriverArtifactKind::Native => {
validate_native_agent_binary(&staging_path)?;
let platform = native_platform
.or(info.native_platform.as_deref())
.unwrap_or_else(|| AgentManager::current_platform());
validate_native_agent_binary_for_platform(&staging_path, platform)?;
mark_executable(&staging_path)?;
}
DriverArtifactKind::Jar => {}
@@ -2276,6 +2407,7 @@ struct TarZstdDriverPackageInfo {
version: String,
jre: String,
kind: DriverArtifactKind,
native_platform: Option<String>,
entry_name: String,
size: u64,
}
@@ -2313,9 +2445,23 @@ async fn import_tar_zstd_driver_package(
});
let target_path = match info.kind {
DriverArtifactKind::Jar => am.driver_jar_path(&info.db_type),
DriverArtifactKind::Native if AgentManager::is_sqlite_worker_driver(&info.db_type) => {
let platform = info
.native_platform
.as_deref()
.ok_or_else(|| "SQLite SSH worker package is missing its Linux platform".to_string())?;
am.driver_native_platform_path(&info.db_type, platform)
}
DriverArtifactKind::Native => am.driver_native_path(&info.db_type),
};
install_driver_from_tar_zstd_package(package_path, &target_path, info.kind, &info.db_type, &info.version)?;
install_driver_from_tar_zstd_package(
package_path,
&target_path,
info.kind,
&info.db_type,
&info.version,
info.native_platform.as_deref(),
)?;
match info.kind {
DriverArtifactKind::Jar => {
std::fs::remove_file(am.driver_native_path(&info.db_type)).ok();
@@ -2324,16 +2470,20 @@ async fn import_tar_zstd_driver_package(
std::fs::remove_file(am.driver_jar_path(&info.db_type)).ok();
}
}
am.mutate_state(|state| {
state.installed_drivers.insert(
info.db_type.clone(),
InstalledDriver {
version: info.version.clone(),
installed_at: chrono::Utc::now().to_rfc3339(),
jre: info.jre.clone(),
},
);
})?;
let record_install =
!AgentManager::is_sqlite_worker_driver(&info.db_type) || am.driver_native_installed(&info.db_type);
if record_install {
am.mutate_state(|state| {
state.installed_drivers.insert(
info.db_type.clone(),
InstalledDriver {
version: info.version.clone(),
installed_at: chrono::Utc::now().to_rfc3339(),
jre: info.jre.clone(),
},
);
})?;
}
am.stop_daemon_by_key(&info.db_type).await;
result.drivers_installed.push(info.db_type);
Ok(result)
@@ -2346,9 +2496,16 @@ fn tar_zstd_driver_package_info(package_path: &Path) -> Result<TarZstdDriverPack
}
let (db_type, driver) = registry.drivers.iter().next().expect("checked one driver");
validate_offline_driver_key(db_type)?;
let platform = AgentManager::current_platform();
let native_artifact = driver.native.get(platform);
let jar_artifact = driver.jar.as_ref();
let current_platform = AgentManager::current_platform();
let (native_platform, native_artifact) = if let Some(artifact) = driver.native.get(current_platform) {
(Some(current_platform.to_string()), Some(artifact))
} else if driver.native.len() == 1 {
let (platform, artifact) = driver.native.iter().next().expect("checked one native platform");
(Some(platform.clone()), Some(artifact))
} else {
(None, None)
};
let jar_artifact = usable_driver_jar(driver);
let (kind, artifact) = match (native_artifact, jar_artifact) {
(Some(_), Some(_)) => {
return Err("A tar.zst driver package must contain exactly one driver artifact".to_string());
@@ -2356,7 +2513,7 @@ fn tar_zstd_driver_package_info(package_path: &Path) -> Result<TarZstdDriverPack
(Some(artifact), None) => (DriverArtifactKind::Native, artifact),
(None, Some(artifact)) => (DriverArtifactKind::Jar, artifact),
(None, None) if !driver.native.is_empty() => {
return Err(format!("Driver package does not support platform: {platform}"));
return Err(format!("Driver package does not support platform: {current_platform}"));
}
(None, None) => return Err("A tar.zst driver package contains no driver artifact".to_string()),
};
@@ -2376,6 +2533,7 @@ fn tar_zstd_driver_package_info(package_path: &Path) -> Result<TarZstdDriverPack
version: driver.version.clone(),
jre: driver.jre.clone(),
kind,
native_platform: native_platform.filter(|_| kind == DriverArtifactKind::Native),
entry_name,
size: artifact.size,
})
@@ -3089,26 +3247,30 @@ fn jre_dir_contains_java(path: &Path) -> bool {
}
pub(crate) fn validate_native_agent_binary(path: &Path) -> Result<(), String> {
validate_native_agent_binary_for_platform(path, AgentManager::current_platform())
}
pub(crate) fn validate_native_agent_binary_for_platform(path: &Path, platform: &str) -> Result<(), String> {
let mut file = std::fs::File::open(path).map_err(|e| format!("Failed to read native agent: {e}"))?;
let mut magic = [0_u8; 4];
file.read_exact(&mut magic).map_err(|e| format!("Failed to read native agent header: {e}"))?;
let valid = if cfg!(target_os = "windows") {
is_windows_binary_for_current_arch(&mut file, &magic)
} else if cfg!(target_os = "linux") {
is_elf_binary_for_current_arch(&mut file, &magic)
} else if cfg!(target_os = "macos") {
is_macho_binary_for_current_arch(&mut file, &magic)
} else {
false
let valid = match platform {
"linux-x64" => is_elf_binary_for_machine(&mut file, &magic, 62),
"linux-aarch64" => is_elf_binary_for_machine(&mut file, &magic, 183),
"macos-x64" => is_macho_binary_for_cpu(&mut file, &magic, 0x0100_0007),
"macos-aarch64" => is_macho_binary_for_cpu(&mut file, &magic, 0x0100_000c),
"windows-x64" => is_windows_binary_for_machine(&mut file, &magic, 0x8664),
"windows-aarch64" => is_windows_binary_for_machine(&mut file, &magic, 0xaa64),
_ => false,
};
if valid {
Ok(())
} else {
Err(format!("The selected file is not a {} native agent for this platform", AgentManager::current_platform()))
Err(format!("The selected file is not a {platform} native agent"))
}
}
fn is_elf_binary_for_current_arch(file: &mut std::fs::File, magic: &[u8; 4]) -> bool {
fn is_elf_binary_for_machine(file: &mut std::fs::File, magic: &[u8; 4], expected_machine: u16) -> bool {
if magic != b"\x7fELF" || file.seek(SeekFrom::Start(4)).is_err() {
return false;
}
@@ -3121,14 +3283,10 @@ fn is_elf_binary_for_current_arch(file: &mut std::fs::File, magic: &[u8; 4]) ->
2 => u16::from_be_bytes([header[14], header[15]]),
_ => return false,
};
(cfg!(target_arch = "x86_64") && machine == 62) || (cfg!(target_arch = "aarch64") && machine == 183)
machine == expected_machine
}
fn is_macho_binary_for_current_arch(file: &mut std::fs::File, magic: &[u8; 4]) -> bool {
const CPU_TYPE_X86_64: u32 = 0x0100_0007;
const CPU_TYPE_ARM64: u32 = 0x0100_000c;
let expected = if cfg!(target_arch = "aarch64") { CPU_TYPE_ARM64 } else { CPU_TYPE_X86_64 };
fn is_macho_binary_for_cpu(file: &mut std::fs::File, magic: &[u8; 4], expected: u32) -> bool {
let thin_endian = match magic {
[0xce, 0xfa, 0xed, 0xfe] | [0xcf, 0xfa, 0xed, 0xfe] => Some(true),
[0xfe, 0xed, 0xfa, 0xce] | [0xfe, 0xed, 0xfa, 0xcf] => Some(false),
@@ -3182,7 +3340,7 @@ fn is_macho_binary_for_current_arch(file: &mut std::fs::File, magic: &[u8; 4]) -
false
}
fn is_windows_binary_for_current_arch(file: &mut std::fs::File, magic: &[u8; 4]) -> bool {
fn is_windows_binary_for_machine(file: &mut std::fs::File, magic: &[u8; 4], expected_machine: u16) -> bool {
if &magic[..2] != b"MZ" || file.seek(SeekFrom::Start(0x3c)).is_err() {
return false;
}
@@ -3197,7 +3355,7 @@ fn is_windows_binary_for_current_arch(file: &mut std::fs::File, magic: &[u8; 4])
return false;
}
let machine = u16::from_le_bytes([pe_header[4], pe_header[5]]);
(cfg!(target_arch = "x86_64") && machine == 0x8664) || (cfg!(target_arch = "aarch64") && machine == 0xaa64)
machine == expected_machine
}
// ──────────── Tests ────────────
@@ -3236,11 +3394,11 @@ mod agent_download_url_tests {
std::fs::write(&path, test_pe_binary(expected_machine)).unwrap();
let mut file = std::fs::File::open(&path).unwrap();
assert!(is_windows_binary_for_current_arch(&mut file, b"MZ\0\0"));
assert!(is_windows_binary_for_machine(&mut file, b"MZ\0\0", expected_machine));
std::fs::write(&path, test_pe_binary(wrong_machine)).unwrap();
let mut file = std::fs::File::open(&path).unwrap();
assert!(!is_windows_binary_for_current_arch(&mut file, b"MZ\0\0"));
assert!(!is_windows_binary_for_machine(&mut file, b"MZ\0\0", expected_machine));
std::fs::remove_file(path).ok();
}
@@ -3921,6 +4079,81 @@ mod agent_registry_install_tests {
.any(|event| event.step == "done" && event.db_type.as_deref() == Some(db_type)));
}
#[tokio::test]
async fn registry_install_sqlite_worker_downloads_both_linux_platforms() {
let manager = test_manager("sqlite-worker-both-linux-platforms");
let db_type = "sqlite-worker";
let version = "0.1.0";
let x64_url = "https://example.com/dbx-agent-sqlite-worker-linux-x64";
let arm_url = "https://example.com/dbx-agent-sqlite-worker-linux-aarch64";
let x64_bytes = b"sqlite-worker-linux-x64";
let arm_bytes = b"sqlite-worker-linux-aarch64";
let mut native = std::collections::HashMap::new();
native.insert(
"linux-x64".to_string(),
ArtifactInfo { url: x64_url.to_string(), sha256: None, size: x64_bytes.len() as u64, format: None },
);
native.insert(
"linux-aarch64".to_string(),
ArtifactInfo { url: arm_url.to_string(), sha256: None, size: arm_bytes.len() as u64, format: None },
);
let mut drivers = std::collections::HashMap::new();
drivers.insert(
db_type.to_string(),
DriverInfo {
version: version.to_string(),
label: "SQLite SSH Worker".to_string(),
min_app_version: "0.1.0".to_string(),
jre: DEFAULT_JRE_KEY.to_string(),
jar: Some(ArtifactInfo {
url: "https://example.com/dbx-agent-sqlite-worker-legacy-placeholder.jar".to_string(),
sha256: None,
size: 0,
format: None,
}),
native,
},
);
let registry = AgentRegistry { jre: None, jres: std::collections::HashMap::new(), drivers };
let x64_path = manager.driver_native_platform_path(db_type, "linux-x64");
let arm_path = manager.driver_native_platform_path(db_type, "linux-aarch64");
std::fs::create_dir_all(manager.driver_dir(db_type)).unwrap();
write_cached_driver_download(&manager, db_type, version, x64_url, &x64_path, x64_bytes);
write_cached_driver_download(&manager, db_type, version, arm_url, &arm_path, arm_bytes);
let events = std::sync::Mutex::new(Vec::new());
let progress = |event| events.lock().unwrap().push(event);
install_agent_driver_from_registry(
&manager,
&registry,
DownloadSource::Official,
db_type,
&progress,
None,
None,
&[],
)
.await
.unwrap();
assert_eq!(std::fs::read(&x64_path).unwrap(), x64_bytes);
assert_eq!(std::fs::read(&arm_path).unwrap(), arm_bytes);
assert!(manager.driver_native_installed(db_type));
assert!(!manager.driver_native_path(db_type).exists());
assert!(!manager.driver_jar_path(db_type).exists());
assert!(!remote_driver_requires_java_runtime(registry.drivers.get(db_type).unwrap()));
assert_eq!(
driver_download_size(db_type, registry.drivers.get(db_type).unwrap()),
(x64_bytes.len() + arm_bytes.len()) as u64
);
assert_eq!(manager.load_state().installed_drivers.get(db_type).unwrap().version, version);
assert!(events
.lock()
.unwrap()
.iter()
.any(|event| event.step == "done" && event.db_type.as_deref() == Some(db_type)));
}
#[tokio::test]
async fn registry_install_cancel_after_cached_download_preserves_existing_driver() {
let manager = test_manager("cancel-after-cached-driver-download");
+43 -24
View File
@@ -2128,29 +2128,44 @@ impl AppState {
PoolKind::Postgres(pg_pool)
}
DatabaseType::Sqlite => {
let sqlite_path = expand_tilde(&db_config.host);
db::sqlite::validate_persistent_attachments(
&sqlite_path,
&db_config.password,
!db_config.attached_databases.is_empty(),
)?;
let extensions = db::sqlite::sqlite_extension_specs_from_url_params(db_config.url_params.as_deref())
.into_iter()
.map(|mut extension| {
extension.path = expand_tilde(&extension.path);
extension
})
.collect();
let pool = db::sqlite::connect_path_with_cipher_key_and_extensions(
&sqlite_path,
&db_config.password,
extensions,
)
.await?;
for attached in &db_config.attached_databases {
db::sqlite::attach_database(&pool, &attached.name, &expand_tilde(&attached.path))?;
if db::sqlite_worker::sqlite_ssh_worker_requested(&db_config) {
let transport_layers = self.resolved_transport_layers(&db_config).await?;
let worker = db::sqlite_worker::connect_sqlite_worker(
&self.tunnels,
&self.agent_manager,
self.storage.data_dir(),
connection_id,
&db_config,
&transport_layers,
)
.await?;
PoolKind::Sqlite(db::sqlite::SqliteHandle::from_worker(worker))
} else {
let sqlite_path = expand_tilde(&db_config.host);
db::sqlite::validate_persistent_attachments(
&sqlite_path,
&db_config.password,
!db_config.attached_databases.is_empty(),
)?;
let extensions =
db::sqlite::sqlite_extension_specs_from_url_params(db_config.url_params.as_deref())
.into_iter()
.map(|mut extension| {
extension.path = expand_tilde(&extension.path);
extension
})
.collect();
let pool = db::sqlite::connect_path_with_cipher_key_and_extensions(
&sqlite_path,
&db_config.password,
extensions,
)
.await?;
for attached in &db_config.attached_databases {
db::sqlite::attach_database(&pool, &attached.name, &expand_tilde(&attached.path))?;
}
PoolKind::Sqlite(pool)
}
PoolKind::Sqlite(pool)
}
DatabaseType::Rqlite => {
let client = db::rqlite_driver::RqliteClient::new(
@@ -2817,7 +2832,7 @@ impl AppState {
config: &ConnectionConfig,
) -> Result<(String, u16), String> {
let transport_layers = self.resolved_transport_layers(config).await?;
if transport_layers.is_empty() {
if transport_layers.is_empty() || db::sqlite_worker::sqlite_ssh_worker_requested(config) {
return Ok((config.host.clone(), config.port));
}
if config.uses_oracle_tns() {
@@ -4439,6 +4454,8 @@ impl AppState {
self.tunnels.stop_tunnels_with_prefix(&redis_sentinel_prefix).await;
self.proxy_tunnels.stop_tunnels_with_prefix(&redis_sentinel_prefix).await;
self.http_tunnels.stop_tunnels_with_prefix(&redis_sentinel_prefix).await;
let sqlite_worker_prefix = db::sqlite_worker::sqlite_worker_chain_id(connection_id);
self.tunnels.stop_tunnels_with_prefix(&sqlite_worker_prefix).await;
db::transport_layer_tunnel::stop_transport_layers(
connection_id,
layer_count,
@@ -5471,7 +5488,9 @@ async fn close_pool_kind(pool: PoolKind) -> Result<(), String> {
let _ = p.disconnect().await;
}
PoolKind::Postgres(p) => p.close(),
PoolKind::Sqlite(_) => {}
PoolKind::Sqlite(pool) => {
pool.shutdown().await;
}
PoolKind::Rqlite(_) => {}
PoolKind::Turso(_) => {}
PoolKind::CloudflareD1(_) => {}
+1
View File
@@ -39,6 +39,7 @@ pub mod questdb;
pub mod redis_driver;
pub mod rqlite_driver;
pub mod sqlite;
pub mod sqlite_worker;
pub mod sqlserver;
pub mod ssh_host_key;
pub mod ssh_prompt;
+222 -4
View File
@@ -30,7 +30,8 @@ const SQLITE_LEGACY_PAGE_SIZES: &[i64] = &[4096, 1024, 512, 2048, 8192, 16384, 3
#[derive(Clone)]
pub struct SqliteHandle {
conn: Arc<Mutex<Connection>>,
conn: Option<Arc<Mutex<Connection>>>,
worker: Option<Arc<super::sqlite_worker::SqliteWorkerClient>>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -40,11 +41,33 @@ pub struct SqliteExtensionSpec {
}
impl SqliteHandle {
pub fn from_worker(worker: Arc<super::sqlite_worker::SqliteWorkerClient>) -> Self {
Self { conn: None, worker: Some(worker) }
}
pub fn is_remote(&self) -> bool {
self.worker.is_some()
}
pub fn worker(&self) -> Option<Arc<super::sqlite_worker::SqliteWorkerClient>> {
self.worker.clone()
}
pub async fn shutdown(&self) {
if let Some(worker) = &self.worker {
worker.shutdown().await;
}
}
pub fn with_connection<T, F>(&self, f: F) -> Result<T, String>
where
F: FnOnce(&mut Connection) -> Result<T, String>,
{
let mut conn = self.conn.lock().map_err(|e| e.to_string())?;
let conn = self
.conn
.as_ref()
.ok_or_else(|| "This operation is not available for a remote SQLite file opened over SSH".to_string())?;
let mut conn = conn.lock().map_err(|e| e.to_string())?;
f(&mut conn)
}
}
@@ -186,7 +209,7 @@ fn open_sqlite_handle(
load_sqlite_extensions(&conn, &extensions)?;
register_sqlite_compat_functions(&conn)?;
return Ok(SqliteHandle { conn: Arc::new(Mutex::new(conn)) });
return Ok(SqliteHandle { conn: Some(Arc::new(Mutex::new(conn))), worker: None });
}
Err(unlock_error.unwrap_or_else(|| "Encrypted SQLite database unlock failed.".to_string()))
@@ -1395,7 +1418,50 @@ mod tests {
}
}
fn json_cell_text(row: &[serde_json::Value], index: usize) -> String {
match row.get(index) {
Some(serde_json::Value::String(value)) => value.clone(),
Some(serde_json::Value::Null) | None => String::new(),
Some(value) => value.to_string().trim_matches('"').to_string(),
}
}
fn json_cell_i32(row: &[serde_json::Value], index: usize) -> i32 {
match row.get(index) {
Some(serde_json::Value::Number(value)) => value.as_i64().unwrap_or(0) as i32,
Some(serde_json::Value::String(value)) => value.parse().unwrap_or(0),
_ => 0,
}
}
fn remote_schema_name(schema: &str) -> String {
let trimmed = schema.trim();
if trimmed.is_empty() {
"main".to_string()
} else {
trimmed.to_string()
}
}
async fn query_remote_rows(pool: &SqliteHandle, sql: &str) -> Result<Vec<Vec<serde_json::Value>>, String> {
Ok(execute_query_with_max_rows(pool, sql, None).await?.rows)
}
pub async fn list_databases(pool: &SqliteHandle) -> Result<Vec<DatabaseInfo>, String> {
if pool.is_remote() {
return Ok(query_remote_rows(pool, "PRAGMA database_list")
.await?
.into_iter()
.filter_map(|row| {
let name = json_cell_text(&row, 1);
if name.eq_ignore_ascii_case("temp") {
None
} else {
Some(DatabaseInfo { name, ..Default::default() })
}
})
.collect());
}
let pool = pool.clone();
tokio::task::spawn_blocking(move || {
pool.with_connection(|conn| {
@@ -1415,6 +1481,27 @@ pub async fn list_databases(pool: &SqliteHandle) -> Result<Vec<DatabaseInfo>, St
}
pub async fn list_tables(pool: &SqliteHandle, schema: &str) -> Result<Vec<TableInfo>, String> {
if pool.is_remote() {
let schema = remote_schema_name(schema);
let sql = format!(
"SELECT name, type FROM {}.sqlite_master WHERE type IN ('table', 'view') AND name NOT LIKE 'sqlite_%' ORDER BY name",
sqlite_quote_ident(&schema)
);
return Ok(query_remote_rows(pool, &sql)
.await?
.into_iter()
.map(|row| {
let table_type = json_cell_text(&row, 1);
TableInfo {
name: json_cell_text(&row, 0),
table_type: if table_type == "view" { "VIEW".to_string() } else { "BASE TABLE".to_string() },
comment: None,
parent_schema: None,
parent_name: None,
}
})
.collect());
}
let pool = pool.clone();
let requested_schema = schema.to_string();
tokio::task::spawn_blocking(move || {
@@ -1446,6 +1533,39 @@ pub async fn list_tables(pool: &SqliteHandle, schema: &str) -> Result<Vec<TableI
}
pub async fn get_columns(pool: &SqliteHandle, schema: &str, table: &str) -> Result<Vec<ColumnInfo>, String> {
if pool.is_remote() {
let schema = remote_schema_name(schema);
let sql = format!("PRAGMA {}.table_info({})", sqlite_quote_ident(&schema), sqlite_quote_string(table));
return Ok(query_remote_rows(pool, &sql)
.await?
.into_iter()
.map(|row| {
let name = json_cell_text(&row, 1);
let is_pk = json_cell_i32(&row, 5) > 0;
ColumnInfo {
name,
data_type: json_cell_text(&row, 2),
is_nullable: json_cell_i32(&row, 3) == 0,
column_default: {
let value = json_cell_text(&row, 4);
if value.is_empty() {
None
} else {
Some(value)
}
},
is_primary_key: is_pk,
extra: None,
comment: None,
numeric_precision: None,
numeric_scale: None,
character_maximum_length: None,
enum_values: None,
..Default::default()
}
})
.collect());
}
let pool = pool.clone();
let requested_schema = schema.to_string();
let table = table.to_string();
@@ -2342,6 +2462,41 @@ fn is_sql_keyword(value: &str) -> bool {
}
pub async fn list_indexes(pool: &SqliteHandle, schema: &str, table: &str) -> Result<Vec<IndexInfo>, String> {
if pool.is_remote() {
let schema = remote_schema_name(schema);
let list_sql = format!("PRAGMA {}.index_list({})", sqlite_quote_ident(&schema), sqlite_quote_string(table));
let mut indexes = Vec::new();
for row in query_remote_rows(pool, &list_sql).await? {
let name = json_cell_text(&row, 1);
let is_unique = json_cell_i32(&row, 2) != 0;
let origin = json_cell_text(&row, 3);
let info_sql = format!("PRAGMA {}.index_info({})", sqlite_quote_ident(&schema), sqlite_quote_string(&name));
let columns = query_remote_rows(pool, &info_sql)
.await?
.into_iter()
.filter_map(|info| {
let column = json_cell_text(&info, 2);
if column.is_empty() {
None
} else {
Some(column)
}
})
.collect();
indexes.push(IndexInfo {
name,
columns,
is_unique,
is_primary: origin == "pk",
filter: None,
index_type: None,
included_columns: None,
comment: None,
key_is_expression: Vec::new(),
});
}
return Ok(indexes);
}
let pool = pool.clone();
let requested_schema = schema.to_string();
let table = table.to_string();
@@ -2401,6 +2556,23 @@ pub async fn list_indexes(pool: &SqliteHandle, schema: &str, table: &str) -> Res
}
pub async fn list_foreign_keys(pool: &SqliteHandle, schema: &str, table: &str) -> Result<Vec<ForeignKeyInfo>, String> {
if pool.is_remote() {
let schema = remote_schema_name(schema);
let sql = format!("PRAGMA {}.foreign_key_list({})", sqlite_quote_ident(&schema), sqlite_quote_string(table));
return Ok(query_remote_rows(pool, &sql)
.await?
.into_iter()
.map(|row| ForeignKeyInfo {
name: format!("fk_{}", json_cell_i32(&row, 0)),
column: json_cell_text(&row, 3),
ref_schema: None,
ref_table: json_cell_text(&row, 2),
ref_column: json_cell_text(&row, 4),
on_update: None,
on_delete: None,
})
.collect());
}
let pool = pool.clone();
let requested_schema = schema.to_string();
let table = table.to_string();
@@ -2431,6 +2603,49 @@ pub async fn list_foreign_keys(pool: &SqliteHandle, schema: &str, table: &str) -
}
pub async fn list_triggers(pool: &SqliteHandle, schema: &str, table: &str) -> Result<Vec<TriggerInfo>, String> {
if pool.is_remote() {
let schema = remote_schema_name(schema);
let sql = format!(
"SELECT name, sql FROM {}.sqlite_master WHERE type = 'trigger' AND tbl_name = {} ORDER BY name",
sqlite_quote_ident(&schema),
sqlite_quote_string(table)
);
return Ok(query_remote_rows(pool, &sql)
.await?
.into_iter()
.map(|row| {
let sql_text = json_cell_text(&row, 1);
let upper = sql_text.to_uppercase();
let timing = if upper.contains("BEFORE") {
"BEFORE"
} else if upper.contains("AFTER") {
"AFTER"
} else {
"INSTEAD OF"
};
let event = if upper.contains("INSERT") {
"INSERT"
} else if upper.contains("UPDATE") {
"UPDATE"
} else {
"DELETE"
};
TriggerInfo {
name: json_cell_text(&row, 0),
event: event.to_string(),
timing: timing.to_string(),
level: None,
condition: None,
language: None,
enabled: None,
valid: None,
comment: None,
created_at: None,
statement: if sql_text.is_empty() { None } else { Some(sql_text) },
}
})
.collect());
}
let pool = pool.clone();
let requested_schema = schema.to_string();
let table = table.to_string();
@@ -2604,8 +2819,11 @@ pub async fn execute_query_with_max_rows(
sql: &str,
max_rows: Option<usize>,
) -> Result<QueryResult, String> {
let pool = pool.clone();
let sql = normalize_sqlite_sql(sql);
if let Some(worker) = pool.worker() {
return worker.query(&sql, max_rows).await;
}
let pool = pool.clone();
tokio::task::spawn_blocking(move || execute_query_blocking(&pool, &sql, max_rows))
.await
.map_err(|e| e.to_string())?
+888
View File
@@ -0,0 +1,888 @@
use std::collections::HashSet;
use std::path::{Path, PathBuf};
use std::pin::Pin;
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
use std::sync::Arc;
use std::time::Duration;
use dbx_sqlite_worker::{WorkerBody, WorkerOp, WorkerRequest, WorkerResponse};
use russh::client::Handle;
use russh::ChannelMsg;
use sha2::{Digest, Sha256};
use tokio::io::{AsyncBufRead, AsyncBufReadExt, AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt, BufReader};
use tokio::process::{Child, ChildStdin};
use tokio::sync::Mutex as AsyncMutex;
use crate::agent_manager::{AgentManager, SQLITE_WORKER_DRIVER_KEY};
use crate::db::ssh_prompt::{self, SshPromptAnswer, SshPromptKind, SshPromptRequest};
use crate::db::ssh_tunnel::{self, SshClient, TunnelManager};
use crate::models::connection::{ConnectionConfig, DatabaseType, SshTunnelConfig, TransportLayerConfig};
use crate::types::QueryResult;
const WORKER_PATH_ENV: &str = "DBX_SQLITE_WORKER_PATH";
const DEFAULT_PERSIST_DIR: &str = "~/.cache/dbx/sqlite-worker";
const CONSENT_FILE_NAME: &str = "sqlite-worker-consent.json";
const SQLITE_WORKER_MAX_RESPONSE_BYTES: usize = 16 * 1024 * 1024;
static SQLITE_SSH_RUNTIME_ENABLED: AtomicBool = AtomicBool::new(false);
pub fn sqlite_worker_chain_id(connection_id: &str) -> String {
format!("{connection_id}:sqlite-worker")
}
pub fn enable_sqlite_ssh_runtime(_app_version: impl Into<String>) {
SQLITE_SSH_RUNTIME_ENABLED.store(true, Ordering::SeqCst);
}
pub fn sqlite_ssh_runtime_enabled() -> bool {
SQLITE_SSH_RUNTIME_ENABLED.load(Ordering::SeqCst)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SqliteWorkerPlacement {
Session,
Persist,
Preplaced,
}
pub fn sqlite_ssh_worker_requested(config: &ConnectionConfig) -> bool {
config.db_type == DatabaseType::Sqlite && config.has_effective_ssh_tunnels()
}
fn sqlite_worker_placement(config: &ConnectionConfig) -> SqliteWorkerPlacement {
match url_param(config.url_params.as_deref(), "dbx_sqlite_worker").as_deref() {
Some("persist") => SqliteWorkerPlacement::Persist,
Some("preplaced") => SqliteWorkerPlacement::Preplaced,
_ => SqliteWorkerPlacement::Session,
}
}
fn removes_remote_worker_on_disconnect(placement: SqliteWorkerPlacement) -> bool {
matches!(placement, SqliteWorkerPlacement::Session)
}
fn session_worker_path(connection_id: &str, digest: &str) -> String {
format!("{DEFAULT_PERSIST_DIR}/session-{connection_id}-{digest}")
}
pub fn sqlite_worker_remote_path(config: &ConnectionConfig) -> String {
url_param(config.url_params.as_deref(), "dbx_sqlite_worker_path").unwrap_or_default()
}
fn url_param(params: Option<&str>, key: &str) -> Option<String> {
params?.trim().trim_start_matches('?').split('&').find_map(|part| {
let (raw_key, raw_value) = part.split_once('=')?;
(raw_key == key).then(|| {
percent_encoding::percent_decode_str(&raw_value.replace('+', " ")).decode_utf8_lossy().into_owned()
})
})
}
trait WorkerStream: AsyncRead + AsyncWrite + Send + Unpin {}
impl<T> WorkerStream for T where T: AsyncRead + AsyncWrite + Send + Unpin {}
type DynStream = Pin<Box<dyn WorkerStream>>;
pub struct SqliteWorkerClient {
io: AsyncMutex<WorkerIo>,
next_id: AtomicU64,
ssh_session: Option<Arc<Handle<SshClient>>>,
/// Session placement only: delete this uploaded file after the worker exits.
remove_remote_path: Option<String>,
}
enum WorkerIo {
#[allow(dead_code)]
Process {
child: Box<Child>,
stdin: Box<ChildStdin>,
stdout: Box<BufReader<tokio::process::ChildStdout>>,
},
Ssh {
stream: BufReader<DynStream>,
},
Closed,
}
impl SqliteWorkerClient {
pub async fn query(&self, sql: &str, max_rows: Option<usize>) -> Result<QueryResult, String> {
match self.roundtrip(WorkerOp::Query { sql: sql.to_string(), max_rows }).await? {
WorkerBody::Ok { columns, column_types, rows, affected_rows, truncated, .. } => Ok(QueryResult {
columns: columns.unwrap_or_default(),
column_types: column_types.unwrap_or_default(),
column_sortables: vec![],
spatial_columns: vec![],
spatial_values: vec![],
rows: rows.unwrap_or_default(),
affected_rows: affected_rows.unwrap_or(0),
execution_time_ms: 0,
truncated: truncated.unwrap_or(false),
session_id: None,
has_more: false,
elasticsearch_raw_body: None,
messages: Vec::new(),
}),
WorkerBody::Err { error } => Err(error),
}
}
pub async fn backup(&self, dest: &str) -> Result<(), String> {
match self.roundtrip(WorkerOp::Backup { dest: dest.to_string() }).await? {
WorkerBody::Ok { .. } => Ok(()),
WorkerBody::Err { error } => Err(error),
}
}
pub async fn restore(&self, src: &str) -> Result<(), String> {
match self.roundtrip(WorkerOp::Restore { src: src.to_string() }).await? {
WorkerBody::Ok { .. } => Ok(()),
WorkerBody::Err { error } => Err(error),
}
}
pub async fn backup_to_local_path(&self, dest: &Path) -> Result<(), String> {
let session = self.ssh_session().await?;
let remote = remote_xfer_path(session.as_ref()).await?;
let transfer = async {
self.backup(&remote).await?;
ssh_download_file(session.as_ref(), &remote, dest).await
}
.await;
if transfer.is_ok() {
let _ = ssh_remove_file(session.as_ref(), &remote).await;
}
transfer
}
pub async fn restore_from_local_path(&self, src: &Path) -> Result<(), String> {
let session = self.ssh_session().await?;
let remote = remote_xfer_path(session.as_ref()).await?;
let transfer = async {
ssh_upload_file(session.as_ref(), src, &remote).await?;
self.restore(&remote).await
}
.await;
let _ = ssh_remove_file(session.as_ref(), &remote).await;
transfer
}
async fn ssh_session(&self) -> Result<Arc<Handle<SshClient>>, String> {
self.ssh_session.clone().ok_or_else(|| "SQLite worker file transfer requires an SSH session".to_string())
}
pub async fn shutdown(&self) {
self.close_io().await;
self.remove_uploaded_session_worker_best_effort().await;
}
async fn close_io(&self) {
let previous = {
let mut io = self.io.lock().await;
std::mem::replace(&mut *io, WorkerIo::Closed)
};
match previous {
WorkerIo::Ssh { mut stream } => {
let _ = stream.get_mut().shutdown().await;
}
WorkerIo::Process { mut child, .. } => {
let _ = child.start_kill();
}
WorkerIo::Closed => {}
}
}
async fn remove_uploaded_session_worker_best_effort(&self) {
let Some(path) = self.remove_remote_path.as_deref() else {
return;
};
let Some(session) = self.ssh_session.as_ref() else {
return;
};
remove_uploaded_session_worker(session.as_ref(), path).await;
}
async fn open_database(&self, path: &str) -> Result<(), String> {
match self.roundtrip(WorkerOp::Open { path: path.to_string() }).await? {
WorkerBody::Ok { .. } => Ok(()),
WorkerBody::Err { error } => Err(error),
}
}
async fn roundtrip(&self, op: WorkerOp) -> Result<WorkerBody, String> {
let id = self.next_id.fetch_add(1, Ordering::SeqCst);
let mut encoded = serde_json::to_vec(&WorkerRequest { id, op }).map_err(|e| e.to_string())?;
encoded.push(b'\n');
let mut io = self.io.lock().await;
match &mut *io {
WorkerIo::Process { stdin, stdout, .. } => {
stdin.write_all(&encoded).await.map_err(|e| e.to_string())?;
stdin.flush().await.map_err(|e| e.to_string())?;
let mut line = String::new();
stdout.read_line(&mut line).await.map_err(|e| e.to_string())?;
parse_response(id, &line)
}
WorkerIo::Closed => Err("SQLite worker session is closed".to_string()),
WorkerIo::Ssh { stream, .. } => {
stream.write_all(&encoded).await.map_err(|e| e.to_string())?;
stream.flush().await.map_err(|e| e.to_string())?;
parse_response(id, &read_jsonl_line(stream).await?)
}
}
}
}
impl Drop for SqliteWorkerClient {
fn drop(&mut self) {
if let Ok(mut io) = self.io.try_lock() {
match std::mem::replace(&mut *io, WorkerIo::Closed) {
WorkerIo::Process { mut child, .. } => {
let _ = child.start_kill();
}
WorkerIo::Ssh { stream } => drop(stream),
WorkerIo::Closed => {}
}
}
let Some(path) = self.remove_remote_path.take() else {
return;
};
let Some(session) = self.ssh_session.take() else {
return;
};
if let Ok(handle) = tokio::runtime::Handle::try_current() {
handle.spawn(async move {
remove_uploaded_session_worker(session.as_ref(), &path).await;
});
}
}
}
fn parse_response(id: u64, line: &str) -> Result<WorkerBody, String> {
let response: WorkerResponse =
serde_json::from_str(line.trim()).map_err(|e| format!("invalid worker response: {e}"))?;
if response.id != id {
return Err(format!("SQLite worker response id {} did not match {id}", response.id));
}
Ok(response.body)
}
async fn read_jsonl_line<R: AsyncBufRead + Unpin>(reader: &mut R) -> Result<String, String> {
let mut line = Vec::new();
loop {
let buf = reader.fill_buf().await.map_err(|e| format!("SQLite worker closed the SSH session: {e}"))?;
if buf.is_empty() {
return Err("SQLite worker closed the SSH session".to_string());
}
if let Some(newline) = buf.iter().position(|&byte| byte == b'\n') {
if line.len() + newline > SQLITE_WORKER_MAX_RESPONSE_BYTES {
return Err("SQLite worker response exceeded 16 MiB".to_string());
}
line.extend_from_slice(&buf[..=newline]);
reader.consume(newline + 1);
break;
}
if line.len() + buf.len() > SQLITE_WORKER_MAX_RESPONSE_BYTES {
return Err("SQLite worker response exceeded 16 MiB".to_string());
}
let consumed = buf.len();
line.extend_from_slice(buf);
reader.consume(consumed);
}
Ok(String::from_utf8_lossy(&line).into_owned())
}
pub async fn connect_sqlite_worker(
tunnels: &TunnelManager,
agent_manager: &AgentManager,
data_dir: &Path,
connection_id: &str,
config: &ConnectionConfig,
transport_layers: &[TransportLayerConfig],
) -> Result<Arc<SqliteWorkerClient>, String> {
if !sqlite_ssh_runtime_enabled() {
return Err("Remote SQLite over SSH is only available in the DBX Desktop app".to_string());
}
if !config.password.is_empty() {
return Err("Remote SQLite over SSH does not support SQLCipher in v1".to_string());
}
if sqlite_has_extensions(config.url_params.as_deref()) {
return Err("Remote SQLite over SSH does not support loadable extensions in v1".to_string());
}
if !config.attached_databases.is_empty() {
return Err("Remote SQLite over SSH does not support attached databases in v1".to_string());
}
let hops = ssh_hops(transport_layers)?;
let chain_id = (hops.len() > 1).then(|| sqlite_worker_chain_id(connection_id));
let db_path = config.host.trim();
if db_path.is_empty() {
return Err("Remote SQLite path is empty".to_string());
}
if !Path::new(db_path).is_absolute() && !db_path.starts_with("~/") {
return Err("Remote SQLite path must be an absolute path on the final SSH hop".to_string());
}
validate_remote_path(db_path)?;
let placement = sqlite_worker_placement(config);
let remove_remote_on_close = removes_remote_worker_on_disconnect(placement);
let configured_path = sqlite_worker_remote_path(config);
let session = open_final_hop_session(tunnels, connection_id, &hops).await?;
let remote_home = ssh_capture(&session, "printf %s \"$HOME\"").await?;
let expand_home = |path: &str| {
if let Some(rest) = path.strip_prefix("~/") {
format!("{}/{}", remote_home.trim_end_matches('/'), rest)
} else if path == "~" {
remote_home.clone()
} else {
path.to_string()
}
};
let platform = remote_linux_platform(&session).await?;
if std::env::var_os(WORKER_PATH_ENV).is_none() && !agent_manager.driver_native_installed(SQLITE_WORKER_DRIVER_KEY) {
crate::agent_service::ensure_sqlite_worker_driver_ready(agent_manager).await?;
}
let local_worker = resolve_local_worker(agent_manager, &platform).await?;
let digest = local_worker.digest.clone();
let identity = hop_identity(hops.last().ok_or("SSH hop list is empty")?);
let remote_path = match placement {
SqliteWorkerPlacement::Preplaced => {
if configured_path.trim().is_empty() {
return Err("Pre-placed SQLite worker path is required".to_string());
}
validate_remote_path(&configured_path)?;
expand_home(&configured_path)
}
SqliteWorkerPlacement::Persist => {
let path = if configured_path.trim().is_empty() {
format!("{DEFAULT_PERSIST_DIR}/{digest}")
} else {
validate_remote_path(&configured_path)?;
configured_path
};
expand_home(&path)
}
SqliteWorkerPlacement::Session => expand_home(&session_worker_path(connection_id, &digest)),
};
let expanded_db = expand_home(db_path);
let session = Arc::new(session);
let start_worker = async {
ensure_remote_sqlite_file_exists(session.as_ref(), &expanded_db).await?;
if placement != SqliteWorkerPlacement::Preplaced {
ensure_worker_consent(data_dir, &identity, &remote_path, &digest).await?;
upload_worker(session.as_ref(), &remote_path, &local_worker.bytes).await?;
}
verify_remote_digest(session.as_ref(), &remote_path, &digest).await?;
let channel = session.channel_open_session().await.map_err(|e| format!("SSH session channel failed: {e}"))?;
channel
.exec(true, format!("exec {}", shell_quote(&remote_path)))
.await
.map_err(|e| format!("failed to start SQLite worker: {e}"))?;
let stream: DynStream = Box::pin(channel.into_stream());
let client = SqliteWorkerClient {
io: AsyncMutex::new(WorkerIo::Ssh { stream: BufReader::new(stream) }),
next_id: AtomicU64::new(1),
ssh_session: Some(Arc::clone(&session)),
remove_remote_path: remove_remote_on_close.then(|| remote_path.clone()),
};
client.open_database(&expanded_db).await?;
Ok(client)
};
match start_worker.await {
Ok(client) => Ok(Arc::new(client)),
Err(error) => {
if remove_remote_on_close {
remove_uploaded_session_worker(session.as_ref(), &remote_path).await;
}
if let Some(chain_id) = chain_id.as_deref() {
tunnels.stop_tunnel(chain_id).await;
}
Err(error)
}
}
}
struct LocalWorker {
bytes: Vec<u8>,
digest: String,
}
async fn resolve_local_worker(agent_manager: &AgentManager, platform: &str) -> Result<LocalWorker, String> {
if let Ok(path) = std::env::var(WORKER_PATH_ENV) {
let bytes = tokio::fs::read(&path).await.map_err(|e| format!("failed to read {WORKER_PATH_ENV}: {e}"))?;
return Ok(LocalWorker { digest: sha256_hex(&bytes), bytes });
}
let path = agent_manager.driver_native_platform_path(SQLITE_WORKER_DRIVER_KEY, platform);
if path.is_file() {
let bytes = tokio::fs::read(&path).await.map_err(|e| e.to_string())?;
return Ok(LocalWorker { digest: sha256_hex(&bytes), bytes });
}
Err(format!(
"{SQLITE_WORKER_DRIVER_KEY} driver is not installed. Please install it from the Driver Manager or set {WORKER_PATH_ENV}."
))
}
async fn ensure_worker_consent(data_dir: &Path, identity: &str, dest: &str, digest: &str) -> Result<(), String> {
if consent_remembered(data_dir, identity, digest) {
return Ok(());
}
let hostport = identity.rsplit_once('@').map(|(_, value)| value).unwrap_or(identity);
let (host, port) = hostport.rsplit_once(':').unwrap_or((hostport, "22"));
let request = SshPromptRequest {
id: uuid::Uuid::new_v4().to_string(),
kind: SshPromptKind::WorkerUploadConsent,
host: host.to_string(),
port: port.parse().unwrap_or(22),
key_type: Some("sha256".to_string()),
fingerprint: Some(digest.to_string()),
prompt: Some(dest.to_string()),
echo: false,
};
let Some(rx) = ssh_prompt::request_ssh_prompt(request) else {
return Err("Uploading a SQLite worker requires explicit consent in the Desktop UI".to_string());
};
match tokio::time::timeout(Duration::from_secs(300), rx).await {
Ok(Ok(SshPromptAnswer::Accept { remember })) => {
if remember {
remember_consent(data_dir, identity, digest);
}
Ok(())
}
Ok(Ok(SshPromptAnswer::Reject)) => Err("SQLite worker upload was declined".to_string()),
Ok(Ok(_)) => Err("SQLite worker upload received an invalid response".to_string()),
Ok(Err(_)) => Err("SQLite worker upload prompt was dismissed".to_string()),
Err(_) => Err("SQLite worker upload prompt timed out".to_string()),
}
}
fn consent_file(data_dir: &Path) -> PathBuf {
data_dir.join(CONSENT_FILE_NAME)
}
fn consent_remembered(data_dir: &Path, identity: &str, digest: &str) -> bool {
let Ok(text) = std::fs::read_to_string(consent_file(data_dir)) else {
return false;
};
let Ok(entries) = serde_json::from_str::<HashSet<String>>(&text) else {
return false;
};
entries.contains(&format!("{identity}|{digest}"))
}
fn remember_consent(data_dir: &Path, identity: &str, digest: &str) {
let path = consent_file(data_dir);
if let Some(parent) = path.parent() {
let _ = std::fs::create_dir_all(parent);
}
let mut entries = std::fs::read_to_string(&path)
.ok()
.and_then(|text| serde_json::from_str::<HashSet<String>>(&text).ok())
.unwrap_or_default();
entries.insert(format!("{identity}|{digest}"));
let _ = std::fs::write(path, serde_json::to_vec_pretty(&entries).unwrap_or_default());
}
async fn open_final_hop_session(
tunnels: &TunnelManager,
connection_id: &str,
hops: &[SshTunnelConfig],
) -> Result<Handle<SshClient>, String> {
let last = hops.last().ok_or("SSH hop list is empty")?;
if hops.len() == 1 {
return ssh_tunnel::connect_and_authenticate(
&last.host,
last.port,
&last.host,
last.port,
&last.user,
&last.password,
&last.key_path,
&last.key_passphrase,
last.use_ssh_agent,
&last.ssh_agent_sock_path,
&last.auth_method,
ssh_tunnel::effective_hop_timeout(last),
tunnels.known_hosts_path(),
)
.await;
}
let chain_id = sqlite_worker_chain_id(connection_id);
let local_port = tunnels.start_chain(&chain_id, &hops[..hops.len() - 1], &last.host, last.port).await?;
match ssh_tunnel::connect_and_authenticate(
"127.0.0.1",
local_port,
&last.host,
last.port,
&last.user,
&last.password,
&last.key_path,
&last.key_passphrase,
last.use_ssh_agent,
&last.ssh_agent_sock_path,
&last.auth_method,
ssh_tunnel::effective_hop_timeout(last),
tunnels.known_hosts_path(),
)
.await
{
Ok(session) => Ok(session),
Err(error) => {
tunnels.stop_tunnel(&chain_id).await;
Err(error)
}
}
}
async fn remote_linux_platform(session: &Handle<SshClient>) -> Result<String, String> {
linux_platform_from_uname(ssh_capture(session, "uname -m").await?.trim())
}
async fn ensure_remote_sqlite_file_exists(session: &Handle<SshClient>, path: &str) -> Result<(), String> {
let output = ssh_capture(session, &remote_sqlite_exists_command(path)).await?;
remote_sqlite_exists_from_output(path, &output)
}
fn remote_sqlite_exists_command(path: &str) -> String {
format!("test -f {} && echo ok", shell_quote(path))
}
fn remote_sqlite_exists_from_output(path: &str, output: &str) -> Result<(), String> {
if output.trim() == "ok" {
Ok(())
} else {
Err(format!("File does not exist: {path}"))
}
}
fn linux_platform_from_uname(machine: &str) -> Result<String, String> {
match machine {
"x86_64" | "amd64" => Ok("linux-x64".to_string()),
"aarch64" | "arm64" => Ok("linux-aarch64".to_string()),
other => Err(format!("Remote SQLite over SSH supports Linux amd64/arm64 only, found {other}")),
}
}
async fn ssh_capture(session: &Handle<SshClient>, command: &str) -> Result<String, String> {
let channel = session.channel_open_session().await.map_err(|e| e.to_string())?;
channel.exec(true, command).await.map_err(|e| e.to_string())?;
let mut stream = channel.into_stream();
let mut out = String::new();
stream.read_to_string(&mut out).await.map_err(|e| e.to_string())?;
Ok(out)
}
async fn remote_xfer_path(session: &Handle<SshClient>) -> Result<String, String> {
let home = ssh_capture(session, "printf %s \"$HOME\"").await?;
let home = home.trim().trim_end_matches('/');
if home.is_empty() {
return Err("Remote HOME is empty".to_string());
}
let parent = format!("{}/.cache/dbx/sqlite-worker", home);
ssh_capture(session, &format!("mkdir -p {}", shell_quote(&parent))).await?;
Ok(format!("{parent}/xfer-{}", uuid::Uuid::new_v4()))
}
async fn ssh_download_file(session: &Handle<SshClient>, remote: &str, dest: &Path) -> Result<(), String> {
if let Some(parent) = dest.parent() {
if !parent.as_os_str().is_empty() {
tokio::fs::create_dir_all(parent).await.map_err(|e| e.to_string())?;
}
}
let mut channel = session.channel_open_session().await.map_err(|e| e.to_string())?;
channel.exec(true, format!("cat {}", shell_quote(remote))).await.map_err(|e| e.to_string())?;
let mut file =
tokio::fs::File::create(dest).await.map_err(|e| format!("failed to create local backup file: {e}"))?;
let copied = {
let mut stream = channel.make_reader();
tokio::io::copy(&mut stream, &mut file).await.map_err(|e| format!("failed to download SQLite backup: {e}"))?
};
file.flush().await.map_err(|e| e.to_string())?;
let mut exit_status = None;
loop {
match channel.wait().await {
Some(ChannelMsg::ExitStatus { exit_status: status }) => exit_status = Some(status),
Some(ChannelMsg::Close) | None => break,
Some(ChannelMsg::Eof | ChannelMsg::Data { .. } | ChannelMsg::ExtendedData { .. }) => {}
Some(_) => {}
}
}
validate_remote_download(copied, exit_status)
}
async fn ssh_upload_file(session: &Handle<SshClient>, src: &Path, remote: &str) -> Result<(), String> {
let quoted = shell_quote(remote);
let command = format!("mkdir -p \"$(dirname {quoted})\" && cat > {quoted}.part && mv {quoted}.part {quoted}");
let mut file = tokio::fs::File::open(src).await.map_err(|e| format!("failed to read local restore file: {e}"))?;
ssh_exec_with_stdin(session, command, &mut file).await
}
async fn ssh_remove_file(session: &Handle<SshClient>, path: &str) -> Result<(), String> {
ssh_capture(session, &format!("rm -f {}", shell_quote(path))).await?;
Ok(())
}
fn empty_cache_dirs_to_remove(worker_path: &str) -> Vec<String> {
let path = Path::new(worker_path.trim_end_matches('/'));
let Some(sqlite_worker_dir) = path.parent() else {
return Vec::new();
};
if sqlite_worker_dir.file_name().is_none_or(|name| name != "sqlite-worker") {
return Vec::new();
}
let mut dirs = vec![sqlite_worker_dir.to_string_lossy().into_owned()];
if let Some(dbx_dir) = sqlite_worker_dir.parent() {
if dbx_dir.file_name().is_some_and(|name| name == "dbx") {
dirs.push(dbx_dir.to_string_lossy().into_owned());
}
}
dirs
}
fn remove_session_worker_command(path: &str) -> String {
let mut command = format!("rm -f {}", shell_quote(path));
for dir in empty_cache_dirs_to_remove(path) {
command.push_str("; rmdir ");
command.push_str(&shell_quote(&dir));
command.push_str(" 2>/dev/null");
}
command
}
async fn remove_uploaded_session_worker(session: &Handle<SshClient>, path: &str) {
if let Err(err) = ssh_capture(session, &remove_session_worker_command(path)).await {
log::warn!("Failed to remove session SQLite worker at {path}: {err}");
}
}
async fn upload_worker(session: &Handle<SshClient>, dest: &str, bytes: &[u8]) -> Result<(), String> {
let quoted = shell_quote(dest);
let command =
format!("mkdir -p \"$(dirname {quoted})\" && cat > {quoted}.part && chmod 700 {quoted}.part && mv {quoted}.part {quoted}");
ssh_exec_with_stdin(session, command, bytes).await
}
async fn ssh_exec_with_stdin<R: tokio::io::AsyncRead + Unpin>(
session: &Handle<SshClient>,
command: String,
stdin: R,
) -> Result<(), String> {
let mut channel = session.channel_open_session().await.map_err(|e| e.to_string())?;
channel.exec(true, command).await.map_err(|e| e.to_string())?;
channel.data(stdin).await.map_err(|e| e.to_string())?;
channel.eof().await.map_err(|e| e.to_string())?;
let mut exit_status = None;
loop {
match channel.wait().await {
Some(ChannelMsg::ExitStatus { exit_status: status }) => exit_status = Some(status),
Some(ChannelMsg::Close) | None => break,
Some(ChannelMsg::Eof | ChannelMsg::Data { .. } | ChannelMsg::ExtendedData { .. }) => {}
Some(_) => {}
}
}
remote_exec_status(exit_status)
}
fn remote_exec_status(exit_status: Option<u32>) -> Result<(), String> {
match exit_status {
Some(0) => Ok(()),
Some(code) => Err(format!("remote command exited with status {code}")),
None => Err("remote command closed without an exit status".to_string()),
}
}
fn validate_remote_download(copied: u64, exit_status: Option<u32>) -> Result<(), String> {
// Check exit status first: a failed `cat` with empty output is better
// explained by the nonzero status than by the emptiness it caused.
remote_exec_status(exit_status)?;
if copied == 0 {
return Err("Downloaded SQLite backup was empty".to_string());
}
Ok(())
}
async fn verify_remote_digest(session: &Handle<SshClient>, path: &str, digest: &str) -> Result<(), String> {
let command = format!("sha256sum {} | awk '{{print $1}}'", shell_quote(path));
let remote = ssh_capture(session, &command).await?;
let remote = remote.trim();
if remote != digest {
return Err(format!("Remote SQLite worker digest {remote} does not match {digest}"));
}
Ok(())
}
fn ssh_hops(layers: &[TransportLayerConfig]) -> Result<Vec<SshTunnelConfig>, String> {
let hops = layers
.iter()
.map(|layer| match layer {
TransportLayerConfig::Ssh(ssh) => {
let ssh = crate::ssh_config::resolve_ssh_tunnel_config(ssh);
if ssh.host.trim().is_empty() {
return Err("SSH host is required.".to_string());
}
Ok(ssh)
}
_ => Err("Remote SQLite over SSH does not support proxy or HTTP tunnel layers".to_string()),
})
.collect::<Result<Vec<_>, _>>()?;
if hops.is_empty() {
return Err("Remote SQLite requires at least one SSH hop".to_string());
}
Ok(hops)
}
fn hop_identity(hop: &SshTunnelConfig) -> String {
format!("{}@{}:{}", hop.user, hop.host, hop.port)
}
fn sqlite_has_extensions(params: Option<&str>) -> bool {
crate::db::sqlite::sqlite_extension_specs_from_url_params(params).into_iter().any(|spec| !spec.path.is_empty())
}
fn validate_remote_path(path: &str) -> Result<(), String> {
if path.contains('\0') || path.contains('\n') || path.contains('\r') {
return Err("Remote path contains invalid characters".to_string());
}
Ok(())
}
fn shell_quote(value: &str) -> String {
format!("'{}'", value.replace('\'', "'\\''"))
}
fn sha256_hex(bytes: &[u8]) -> String {
let mut hasher = Sha256::new();
hasher.update(bytes);
format!("{:x}", hasher.finalize())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn default_placement_is_session() {
let mut config = empty_config();
assert_eq!(sqlite_worker_placement(&config), SqliteWorkerPlacement::Session);
config.url_params = Some("dbx_sqlite_worker=persist".into());
assert_eq!(sqlite_worker_placement(&config), SqliteWorkerPlacement::Persist);
config.url_params = Some("dbx_sqlite_worker=preplaced&dbx_sqlite_worker_path=%2Fopt%2Fworker".into());
assert_eq!(sqlite_worker_placement(&config), SqliteWorkerPlacement::Preplaced);
assert_eq!(sqlite_worker_remote_path(&config), "/opt/worker");
}
#[test]
fn session_placement_removes_worker_on_disconnect() {
assert!(removes_remote_worker_on_disconnect(SqliteWorkerPlacement::Session));
assert!(!removes_remote_worker_on_disconnect(SqliteWorkerPlacement::Persist));
assert!(!removes_remote_worker_on_disconnect(SqliteWorkerPlacement::Preplaced));
assert_eq!(session_worker_path("conn-1", "abc123"), "~/.cache/dbx/sqlite-worker/session-conn-1-abc123");
assert_eq!(
empty_cache_dirs_to_remove("/home/u/.cache/dbx/sqlite-worker/session-conn-1-abc123"),
vec!["/home/u/.cache/dbx/sqlite-worker".to_string(), "/home/u/.cache/dbx".to_string()]
);
assert!(empty_cache_dirs_to_remove("/opt/dbx/dbx-sqlite-worker").is_empty());
assert!(remove_session_worker_command("/home/u/.cache/dbx/sqlite-worker/session-conn-1-abc123")
.contains("rmdir '/home/u/.cache/dbx/sqlite-worker' 2>/dev/null; rmdir '/home/u/.cache/dbx' 2>/dev/null"));
}
#[test]
fn maps_remote_uname_to_agent_linux_platforms() {
assert_eq!(linux_platform_from_uname("x86_64").unwrap(), "linux-x64");
assert_eq!(linux_platform_from_uname("amd64").unwrap(), "linux-x64");
assert_eq!(linux_platform_from_uname("aarch64").unwrap(), "linux-aarch64");
assert_eq!(linux_platform_from_uname("arm64").unwrap(), "linux-aarch64");
assert!(linux_platform_from_uname("ppc64le").unwrap_err().contains("ppc64le"));
}
#[test]
fn sqlite_worker_chain_id_is_distinct_from_connection_id() {
assert_eq!(sqlite_worker_chain_id("conn-1"), "conn-1:sqlite-worker");
}
#[test]
fn remote_download_requires_a_successful_non_empty_transfer() {
assert!(validate_remote_download(1, Some(0)).is_ok());
assert!(validate_remote_download(1, Some(1)).unwrap_err().contains("status 1"));
assert!(validate_remote_download(1, None).unwrap_err().contains("without an exit status"));
assert!(validate_remote_download(0, Some(0)).unwrap_err().contains("was empty"));
}
#[test]
fn remote_sqlite_exists_requires_a_regular_file() {
assert_eq!(remote_sqlite_exists_command("/remote/data/app.db"), "test -f '/remote/data/app.db' && echo ok");
assert!(remote_sqlite_exists_from_output("/remote/data/app.db", "ok\n").is_ok());
let error = remote_sqlite_exists_from_output("/remote/data/app.db", "").unwrap_err();
assert!(error.contains("File does not exist"), "{error}");
}
#[tokio::test]
async fn read_jsonl_line_stops_at_newline_and_enforces_size_cap() {
let mut reader = BufReader::new(&b"{\"id\":1}\nleftover"[..]);
let line = read_jsonl_line(&mut reader).await.unwrap();
assert_eq!(line, "{\"id\":1}\n");
let mut oversized = vec![b'a'; SQLITE_WORKER_MAX_RESPONSE_BYTES + 1];
oversized.push(b'\n');
let mut reader = BufReader::new(oversized.as_slice());
let error = read_jsonl_line(&mut reader).await.unwrap_err();
assert!(error.contains("16 MiB"), "{error}");
}
#[test]
fn remote_sqlite_requires_ssh() {
let config = empty_config();
assert!(!sqlite_ssh_worker_requested(&config));
}
#[test]
fn ssh_hops_uses_resolved_layers_instead_of_profile_stubs() {
let hops = ssh_hops(&[ssh_layer("203.0.113.10", "testuser")]).unwrap();
assert_eq!(hops.len(), 1);
assert_eq!(hops[0].host, "203.0.113.10");
assert_eq!(hops[0].user, "testuser");
}
#[test]
fn ssh_hops_rejects_unresolved_profile_stubs() {
let error = ssh_hops(&[ssh_layer("", "")]).unwrap_err();
assert!(error.contains("SSH host is required"), "{error}");
}
#[test]
fn ssh_hops_rejects_non_ssh_layers() {
let layer = serde_json::from_value(serde_json::json!({
"type": "proxy",
"id": "proxy-1",
"enabled": true,
"host": "203.0.113.10",
"port": 1080
}))
.unwrap();
let error = ssh_hops(&[layer]).unwrap_err();
assert!(error.contains("proxy or HTTP"), "{error}");
}
fn ssh_layer(host: &str, user: &str) -> TransportLayerConfig {
serde_json::from_value(serde_json::json!({
"type": "ssh",
"id": "hop-1",
"enabled": true,
"host": host,
"port": 22,
"user": user
}))
.unwrap()
}
fn empty_config() -> ConnectionConfig {
serde_json::from_value(serde_json::json!({
"id": "id",
"name": "n",
"db_type": "sqlite",
"host": "/tmp/a.db",
"port": 0,
"username": "",
"password": ""
}))
.unwrap()
}
}
+2
View File
@@ -26,6 +26,8 @@ pub enum SshPromptKind {
HostKeyVerify,
/// Collect a secret typed by the user (e.g. a dynamic verification code).
SecretInput,
/// Confirm uploading the SQLite worker binary onto the file host.
WorkerUploadConsent,
}
/// A request for user input, sent from the backend to the UI.
+7 -3
View File
@@ -48,7 +48,7 @@ const KEYBOARD_INTERACTIVE_PROMPT_TIMEOUT: Duration = Duration::from_secs(300);
/// SSH client handler. Holds a host-key verifier so that
/// [`client::Handler::check_server_key`] can reject untrusted/changed server
/// keys *before* any credential is sent.
struct SshClient {
pub(crate) struct SshClient {
host_key_verifier: Arc<HostKeyVerifier>,
host: String,
port: u16,
@@ -316,7 +316,7 @@ async fn authenticate_keyboard_interactive(
}
#[allow(clippy::too_many_arguments)]
async fn connect_and_authenticate(
pub(crate) async fn connect_and_authenticate(
connect_host: &str,
connect_port: u16,
host_key_host: &str,
@@ -1278,6 +1278,10 @@ impl TunnelManager {
Self { tunnels: Mutex::new(HashMap::new()), start_locks: Mutex::new(HashMap::new()), known_hosts_path }
}
pub fn known_hosts_path(&self) -> &Path {
&self.known_hosts_path
}
async fn start_lock(&self, connection_id: &str) -> Arc<Mutex<()>> {
self.start_locks
.lock()
@@ -1820,7 +1824,7 @@ async fn bind_tunnel_listener(
Ok((listener, local_port))
}
fn effective_hop_timeout(hop: &SshTunnelConfig) -> u64 {
pub(crate) fn effective_hop_timeout(hop: &SshTunnelConfig) -> u64 {
if hop.connect_timeout_secs == 0 {
crate::models::connection::default_ssh_connect_timeout_secs()
} else {
+35
View File
@@ -14,6 +14,16 @@ pub struct SqliteBackupOptions {
pub async fn backup_sqlite_database(pool: SqliteHandle, options: SqliteBackupOptions) -> Result<(), String> {
let destination = prepare_destination_path(&options)?;
let temp = backup_temp_path(&destination);
if let Some(worker) = pool.worker() {
let result = worker.backup_to_local_path(&temp).await;
return match result {
Ok(()) => replace_backup_file(&temp, &destination),
Err(error) => {
let _ = std::fs::remove_file(&temp);
Err(error)
}
};
}
tokio::task::spawn_blocking(move || {
pool.with_connection(|conn| {
conn.backup(DatabaseName::Main, &temp, None).map_err(|e| format!("SQLite backup failed: {e}"))
@@ -24,6 +34,31 @@ pub async fn backup_sqlite_database(pool: SqliteHandle, options: SqliteBackupOpt
.map_err(|e| e.to_string())?
}
pub async fn restore_sqlite_database(pool: SqliteHandle, source_path: &str) -> Result<(), String> {
let src = source_path.trim();
if src.is_empty() {
return Err("Restore source path is empty".to_string());
}
if src.contains('\0') {
return Err("Path contains an invalid NUL byte".to_string());
}
if let Some(worker) = pool.worker() {
let source = PathBuf::from(expand_tilde(src));
if !source.is_file() {
return Err("Restore source file does not exist".to_string());
}
return worker.restore_from_local_path(&source).await;
}
let source = PathBuf::from(expand_tilde(src));
tokio::task::spawn_blocking(move || {
pool.with_connection(|conn| {
conn.restore(DatabaseName::Main, &source, None::<fn(_)>).map_err(|e| format!("SQLite restore failed: {e}"))
})
})
.await
.map_err(|e| e.to_string())?
}
fn prepare_destination_path(options: &SqliteBackupOptions) -> Result<PathBuf, String> {
let destination = normalize_user_path(&options.destination_path, "Destination path is empty")?;
ensure_no_nul(&options.destination_path)?;