diff --git a/.oss-scanner/threat_model.md b/.oss-scanner/threat_model.md index 4a121eeca6..0e2b962dbc 100644 --- a/.oss-scanner/threat_model.md +++ b/.oss-scanner/threat_model.md @@ -13,7 +13,7 @@ The user is trusted: the SQL they type, the connections they create, the plugins 1. **The database server and the network to it.** A malicious or compromised server, or anyone on the path when TLS is not verified, controls every byte of the protocol, every result set and every error message. This includes the HTTP and REST engines and the SSH server of a tunnel. 2. **Database contents.** Cell values (text, BLOBs, JSON, images, PHP-serialized data), table, column and schema names, comments and object definitions can be written by another user of a shared database. -3. **Files the user opens or imports.** `.sql`, CSV/TSV, JSON/JSONL, XLSX, Parquet, SQLite and DuckDB files, `.tablepro` connection bundles, connection files from other apps (TablePlus, DBeaver, DataGrip, Navicat `.ncx`, Sequel Ace, Beekeeper), project folders scanned for credentials (`.env`, docker-compose, framework configs), `~/.pgpass`, `~/.ssh/config`. +3. **Files the user opens or imports.** `.sql`, CSV/TSV, JSON/JSONL, XLSX, Parquet, SQLite and DuckDB files, `.tablepro` connection bundles, connection files from other apps (TablePlus, DBeaver, DataGrip, Navicat `.ncx`, Sequel Ace, Beekeeper, DBX), project folders scanned for credentials (`.env`, docker-compose, framework configs), `~/.pgpass`, `~/.ssh/config`. 4. **Links.** `tablepro://` deep links and database URLs (`postgresql://`, `mysql://`, `mongodb+srv://` and others in `apple/TablePro/Info.plist`) opened from a browser or another app. 5. **Local MCP clients.** The Mac app runs an MCP server on loopback. A process without a valid token, or a client acting beyond its token's scopes and connection allowlist, is untrusted. So are web pages reaching that port (DNS rebinding) and the remote MCP servers TablePro connects to as a client. 6. **AI model output.** Tool calls from a model, which database contents can steer by prompt injection. diff --git a/core/crates/foreign-import/Cargo.toml b/core/crates/foreign-import/Cargo.toml index 3dc33b8587..665061368e 100644 --- a/core/crates/foreign-import/Cargo.toml +++ b/core/crates/foreign-import/Cargo.toml @@ -16,7 +16,7 @@ chacha20.workspace = true flate2.workspace = true hex.workspace = true # The SQLite the storage crate links through sqlx, read directly because -# a Beekeeper Studio database is read once, blocking, read-only. +# a Beekeeper Studio or DBX database is read once, blocking, read-only. libsqlite3-sys.workspace = true plist.workspace = true roxmltree.workspace = true diff --git a/core/crates/foreign-import/src/app.rs b/core/crates/foreign-import/src/app.rs index c53c23ffde..96530e550d 100644 --- a/core/crates/foreign-import/src/app.rs +++ b/core/crates/foreign-import/src/app.rs @@ -13,6 +13,7 @@ pub enum ForeignApp { DataGrip, BeekeeperStudio, Navicat, + Dbx, } #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -36,13 +37,14 @@ pub enum SavedQuerySupport { } impl ForeignApp { - pub const ALL: [Self; 6] = [ + pub const ALL: [Self; 7] = [ Self::TablePlus, Self::SequelAce, Self::DBeaver, Self::DataGrip, Self::BeekeeperStudio, Self::Navicat, + Self::Dbx, ]; pub fn id(self) -> &'static str { @@ -53,6 +55,7 @@ impl ForeignApp { Self::DataGrip => "datagrip", Self::BeekeeperStudio => "beekeeperstudio", Self::Navicat => "navicat", + Self::Dbx => "dbx", } } @@ -68,6 +71,7 @@ impl ForeignApp { Self::DataGrip => "DataGrip", Self::BeekeeperStudio => "Beekeeper Studio", Self::Navicat => "Navicat", + Self::Dbx => "DBX", } } @@ -82,7 +86,7 @@ impl ForeignApp { pub fn password_source(self) -> PasswordSource { match self { - Self::TablePlus | Self::SequelAce | Self::DataGrip => PasswordSource::Keychain, + Self::TablePlus | Self::SequelAce | Self::DataGrip | Self::Dbx => PasswordSource::Keychain, Self::DBeaver | Self::BeekeeperStudio | Self::Navicat => PasswordSource::AppFiles, } } @@ -90,7 +94,7 @@ impl ForeignApp { pub fn saved_query_support(self) -> SavedQuerySupport { match self { Self::TablePlus | Self::SequelAce | Self::BeekeeperStudio => SavedQuerySupport::GlobalFolder, - Self::DBeaver => SavedQuerySupport::Scripts, + Self::DBeaver | Self::Dbx => SavedQuerySupport::Scripts, Self::DataGrip => SavedQuerySupport::Consoles, Self::Navicat => SavedQuerySupport::NotInExport, } @@ -202,6 +206,7 @@ impl ForeignImporter { ForeignApp::DataGrip => Box::new(apps::datagrip::DataGrip::locate(&self.dirs, keychain)), ForeignApp::BeekeeperStudio => Box::new(apps::beekeeper::Beekeeper::locate(&self.dirs)), ForeignApp::Navicat => Box::new(apps::navicat::Navicat::new(self.picked_file.clone())), + ForeignApp::Dbx => Box::new(apps::dbx::Dbx::locate(&self.dirs)), } } } diff --git a/core/crates/foreign-import/src/apps/beekeeper/mod.rs b/core/crates/foreign-import/src/apps/beekeeper/mod.rs index ce33330788..939746b0e8 100644 --- a/core/crates/foreign-import/src/apps/beekeeper/mod.rs +++ b/core/crates/foreign-import/src/apps/beekeeper/mod.rs @@ -1,7 +1,6 @@ mod encryptor; mod locations; mod saved_queries; -mod sqlite; use std::collections::{BTreeMap, HashMap}; use std::path::PathBuf; @@ -9,8 +8,8 @@ use std::path::PathBuf; use secrecy::{ExposeSecret, SecretString}; use tokio_util::sync::CancellationToken; -use self::sqlite::{Database, Row}; use crate::app::{AppReader, check_cancel}; +use crate::apps::sqlite::{Database, Row}; use crate::collect::assemble; use crate::{ CollectedImport, ForeignApp, ForeignAppDirs, ForeignColor, ForeignConnection, ForeignConnectionRecord, diff --git a/core/crates/foreign-import/src/apps/beekeeper/saved_queries.rs b/core/crates/foreign-import/src/apps/beekeeper/saved_queries.rs index 12a105e09e..a236845c98 100644 --- a/core/crates/foreign-import/src/apps/beekeeper/saved_queries.rs +++ b/core/crates/foreign-import/src/apps/beekeeper/saved_queries.rs @@ -2,8 +2,8 @@ use std::collections::{HashMap, HashSet}; use tokio_util::sync::CancellationToken; -use super::sqlite::Database; use crate::app::check_cancel; +use crate::apps::sqlite::Database; use crate::{ForeignImportError, ForeignSavedQuery, SavedQueryContent}; // Every install seeds this example, so importing it would add noise to every library. @@ -19,7 +19,7 @@ pub(super) fn read( limit: u64, cancel: &CancellationToken, ) -> Result, ForeignImportError> { - let columns = column_names(db, "favorite_query"); + let columns = db.columns("favorite_query"); if !columns.contains("title") || !columns.contains("text") { return Ok(Vec::new()); } @@ -70,7 +70,7 @@ pub(super) fn read( } pub(super) fn count(db: &Database) -> usize { - let columns = column_names(db, "favorite_query"); + let columns = db.columns("favorite_query"); if !columns.contains("title") || !columns.contains("text") { return 0; } @@ -107,7 +107,7 @@ fn where_clause(columns: &HashSet, conditions: &[&str]) -> String { } fn read_folders(db: &Database) -> HashMap { - let columns = column_names(db, "query_folder"); + let columns = db.columns("query_folder"); if !columns.contains("id") || !columns.contains("name") { return HashMap::new(); } @@ -149,19 +149,6 @@ fn folder_path(folder_id: Option, folders: &HashMap) -> Vec HashSet { - let Some(mut statement) = db.prepare(&format!("PRAGMA table_info({table})")) else { - return HashSet::new(); - }; - let mut names = HashSet::new(); - while let Some(row) = statement.next_row() { - if let Some(name) = row.text(1) { - names.insert(name); - } - } - names -} - #[cfg(test)] mod tests { use super::*; diff --git a/core/crates/foreign-import/src/apps/dbeaver/mod.rs b/core/crates/foreign-import/src/apps/dbeaver/mod.rs index 234ede609f..7e2bdb0a76 100644 --- a/core/crates/foreign-import/src/apps/dbeaver/mod.rs +++ b/core/crates/foreign-import/src/apps/dbeaver/mod.rs @@ -497,21 +497,7 @@ fn color(config: &Value) -> Option { let [r, g, b, ..] = components[..] else { return None; }; - if r > 200 && g < 100 && b < 100 { - Some(ForeignColor::Red) - } else if r > 200 && g > 100 && g < 200 && b < 100 { - Some(ForeignColor::Orange) - } else if r > 200 && g > 200 && b < 100 { - Some(ForeignColor::Yellow) - } else if r < 100 && g > 150 && b < 100 { - Some(ForeignColor::Green) - } else if r < 100 && g < 100 && b > 200 { - Some(ForeignColor::Blue) - } else if r > 100 && g < 100 && b > 150 { - Some(ForeignColor::Purple) - } else { - None - } + super::rgb_color(r, g, b) } // A port is a number or a numeric string; one outside 0...65535 counts as missing. diff --git a/core/crates/foreign-import/src/apps/dbx/config.rs b/core/crates/foreign-import/src/apps/dbx/config.rs new file mode 100644 index 0000000000..15eb45ace8 --- /dev/null +++ b/core/crates/foreign-import/src/apps/dbx/config.rs @@ -0,0 +1,254 @@ +use std::collections::{BTreeSet, HashMap}; + +use serde::Deserialize; + +use super::{HTTP_TUNNEL, PROXY, PROXY_COMMAND, SSH_TUNNEL}; + +// The fields of DBX's `ConnectionConfig` an import reads. A field this does not name is ignored, and +// one left out takes DBX's own default. +#[derive(Debug, Default, Deserialize)] +#[serde(default)] +pub(super) struct Config { + pub(super) id: String, + pub(super) name: String, + pub(super) db_type: String, + pub(super) driver_profile: Option, + pub(super) url_params: Option, + pub(super) host: String, + pub(super) port: u16, + pub(super) username: String, + pub(super) database: Option, + pub(super) color: Option, + pub(super) ssl: bool, + pub(super) ca_cert_path: String, + pub(super) client_cert_path: String, + pub(super) client_key_path: String, + pub(super) transport_layers: Vec, + pub(super) init_script: Option, + pub(super) attached_databases: Vec, + pub(super) connection_string: Option, + // A connection saved before `transport_layers` existed, which DBX turns into layers as it reads it. + ssh_enabled: Option, + ssh_tunnels: Option>, + ssh_host: Option, + ssh_port: Option, + ssh_user: Option, + ssh_key_path: Option, + proxy_enabled: Option, + proxy_host: Option, +} + +// The last layer is the hop next to the database. +#[derive(Debug, Clone, Deserialize)] +#[serde(tag = "type", rename_all = "snake_case")] +pub(super) enum Layer { + Ssh(SshLayer), + Proxy(OtherLayer), + HttpTunnel(OtherLayer), +} + +#[derive(Debug, Clone, Deserialize)] +#[serde(default)] +pub(super) struct SshLayer { + pub(super) enabled: bool, + pub(super) host: String, + pub(super) port: u16, + pub(super) user: String, + pub(super) key_path: String, + pub(super) use_ssh_agent: bool, + pub(super) auth_method: String, + pub(super) proxy_command: String, + pub(super) profile_id: String, +} + +impl Default for SshLayer { + fn default() -> Self { + Self { + enabled: true, + host: String::new(), + port: 22, + user: String::new(), + key_path: String::new(), + use_ssh_agent: false, + auth_method: String::new(), + proxy_command: String::new(), + profile_id: String::new(), + } + } +} + +#[derive(Debug, Clone, Deserialize)] +#[serde(default)] +pub(super) struct OtherLayer { + enabled: bool, + profile_id: String, +} + +impl Default for OtherLayer { + fn default() -> Self { + Self { + enabled: true, + profile_id: String::new(), + } + } +} + +impl Layer { + fn enabled(&self) -> bool { + match self { + Self::Ssh(ssh) => ssh.enabled, + Self::Proxy(other) | Self::HttpTunnel(other) => other.enabled, + } + } + + fn profile_id(&self) -> &str { + match self { + Self::Ssh(ssh) => ssh.profile_id.trim(), + Self::Proxy(other) | Self::HttpTunnel(other) => other.profile_id.trim(), + } + } + + fn lost_as(&self) -> &'static str { + match self { + Self::Ssh(_) => SSH_TUNNEL, + Self::Proxy(_) => PROXY, + Self::HttpTunnel(_) => HTTP_TUNNEL, + } + } +} + +impl Config { + // The SSH hops DBX connects through, first to last, and the review key of each part of the route + // TablePro has no field for. A layer that names a shared tunnel profile takes that profile's + // settings, as DBX does at connect time. + pub(super) fn route(&self, profiles: &HashMap) -> (Vec, BTreeSet<&'static str>) { + let mut hops = Vec::new(); + let mut lost = BTreeSet::new(); + for layer in self.layers().into_iter().filter(Layer::enabled) { + let resolved = match layer.profile_id() { + "" => Some(layer.clone()), + id => profiles.get(id).cloned(), + }; + match resolved { + Some(Layer::Ssh(ssh)) if !ssh.host.trim().is_empty() => { + if !ssh.proxy_command.trim().is_empty() { + lost.insert(PROXY_COMMAND); + } + hops.push(ssh); + } + Some(other) => { + lost.insert(other.lost_as()); + } + None => { + lost.insert(layer.lost_as()); + } + } + } + (hops, lost) + } + + fn layers(&self) -> Vec { + if !self.transport_layers.is_empty() { + return self.transport_layers.clone(); + } + let mut layers = Vec::new(); + if self.ssh_enabled == Some(true) { + let hops = self.ssh_tunnels.as_deref().unwrap_or_default(); + if !hops.is_empty() { + layers.extend(hops.iter().cloned().map(Layer::Ssh)); + } else if let Some(host) = named(self.ssh_host.as_deref()) { + layers.push(Layer::Ssh(SshLayer { + host: host.to_owned(), + port: self.ssh_port.filter(|port| *port > 0).unwrap_or(22), + user: self.ssh_user.clone().unwrap_or_default(), + key_path: self.ssh_key_path.clone().unwrap_or_default(), + ..SshLayer::default() + })); + } + } + if self.proxy_enabled == Some(true) && named(self.proxy_host.as_deref()).is_some() { + layers.push(Layer::Proxy(OtherLayer::default())); + } + layers + } +} + +fn named(value: Option<&str>) -> Option<&str> { + value.map(str::trim).filter(|value| !value.is_empty()) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn config(json: &str) -> Config { + serde_json::from_str(json).expect("a config") + } + + fn hosts(hops: &[SshLayer]) -> Vec<&str> { + hops.iter().map(|hop| hop.host.as_str()).collect() + } + + #[test] + fn hops_keep_their_order_and_a_disabled_one_is_left_out() { + let config = config( + r#"{"transport_layers":[{"type":"ssh","host":"edge"},{"type":"ssh","host":"off","enabled":false}, + {"type":"ssh","host":"inner","port":2222}]}"#, + ); + + let (hops, lost) = config.route(&HashMap::new()); + + assert_eq!(hosts(&hops), ["edge", "inner"]); + assert_eq!(hops[1].port, 2222); + assert!(lost.is_empty()); + } + + #[test] + fn a_layer_on_a_tunnel_profile_takes_the_profiles_settings_or_is_lost() { + let profile: Layer = + serde_json::from_str(r#"{"type":"ssh","host":"shared","user":"ops","enabled":false}"#).expect("a profile"); + let profiles = HashMap::from([("p1".to_owned(), profile)]); + let config = config( + r#"{"transport_layers":[{"type":"ssh","profile_id":"p1"}, + {"type":"ssh","profile_id":"gone","host":"stale"}]}"#, + ); + + let (hops, lost) = config.route(&profiles); + + assert_eq!(hosts(&hops), ["shared"]); + assert_eq!(hops[0].user, "ops"); + assert_eq!(lost, BTreeSet::from([SSH_TUNNEL])); + } + + #[test] + fn a_proxy_a_script_tunnel_and_a_proxy_command_are_named_as_lost() { + let config = config( + r#"{"transport_layers":[{"type":"proxy","host":"socks"},{"type":"http_tunnel","url":"https://x/t.php"}, + {"type":"ssh","host":"bastion","proxy_command":"nc %h %p"}]}"#, + ); + + let (hops, lost) = config.route(&HashMap::new()); + + assert_eq!(hosts(&hops), ["bastion"]); + assert_eq!(lost, BTreeSet::from([HTTP_TUNNEL, PROXY, PROXY_COMMAND])); + } + + #[test] + fn a_connection_saved_before_layers_reads_as_dbx_reads_it() { + let single = config( + r#"{"ssh_enabled":true,"ssh_host":" bastion ","ssh_port":0,"ssh_user":"ops", + "proxy_enabled":true,"proxy_host":"socks"}"#, + ); + let (hops, lost) = single.route(&HashMap::new()); + assert_eq!(hosts(&hops), ["bastion"]); + assert_eq!((hops[0].port, hops[0].user.as_str()), (22, "ops")); + assert_eq!(lost, BTreeSet::from([PROXY])); + + let chained = + config(r#"{"ssh_enabled":true,"ssh_host":"ignored","ssh_tunnels":[{"host":"edge"},{"host":"inner"}]}"#); + assert_eq!(hosts(&chained.route(&HashMap::new()).0), ["edge", "inner"]); + + let off = config(r#"{"ssh_enabled":false,"ssh_host":"bastion"}"#); + assert!(off.route(&HashMap::new()).0.is_empty()); + } +} diff --git a/core/crates/foreign-import/src/apps/dbx/locations.rs b/core/crates/foreign-import/src/apps/dbx/locations.rs new file mode 100644 index 0000000000..4ae5979125 --- /dev/null +++ b/core/crates/foreign-import/src/apps/dbx/locations.rs @@ -0,0 +1,38 @@ +use std::path::PathBuf; + +use crate::{ForeignAppDirs, ForeignPlatform}; + +// Tauri's bundle identifier names the app's data folder on every platform. +const FOLDER: &str = "com.dbx.app"; + +pub(super) const STORE: &str = "dbx.db"; + +// Tauri's app data directory: `$XDG_DATA_HOME` on Linux, Application Support on macOS and the +// roaming AppData on Windows. `DBX_DATA_DIR` and the portable build's folder are the app's own +// overrides, which an import does not follow since it cannot tell which one a run used. +pub(super) fn data_dir(dirs: &ForeignAppDirs) -> PathBuf { + match dirs.platform { + ForeignPlatform::Windows => dirs.config.join(FOLDER), + ForeignPlatform::Linux | ForeignPlatform::MacOs => dirs.data.join(FOLDER), + } +} + +#[cfg(test)] +mod tests { + use std::path::Path; + + use super::*; + + #[test] + fn each_platform_looks_where_tauri_keeps_the_app_data() { + let home = Path::new("/home/ada"); + let at = |platform| data_dir(&ForeignAppDirs::conventional(platform, home)); + + assert_eq!(at(ForeignPlatform::Linux), home.join(".local/share/com.dbx.app")); + assert_eq!( + at(ForeignPlatform::MacOs), + home.join("Library/Application Support/com.dbx.app") + ); + assert_eq!(at(ForeignPlatform::Windows), home.join("AppData/Roaming/com.dbx.app")); + } +} diff --git a/core/crates/foreign-import/src/apps/dbx/mod.rs b/core/crates/foreign-import/src/apps/dbx/mod.rs new file mode 100644 index 0000000000..c16a3deff6 --- /dev/null +++ b/core/crates/foreign-import/src/apps/dbx/mod.rs @@ -0,0 +1,423 @@ +// DBX (t8y2/dbx), a Tauri app that keeps everything in one SQLite file, `dbx.db`. The shapes read here +// are those of `crates/dbx-core/src/persistence/storage.rs` and `crates/dbx-types/src/models/connection.rs`. + +mod config; +mod locations; +mod store; + +use std::collections::{BTreeMap, HashMap, HashSet}; +use std::path::PathBuf; + +use tokio_util::sync::CancellationToken; + +use self::config::{Config, Layer, SshLayer}; +use crate::app::{AppReader, check_cancel}; +use crate::apps::jdbc; +use crate::apps::sqlite::Database; +use crate::collect::assemble; +use crate::engine::{self, LocalFilePathField}; +use crate::{ + CollectedImport, ForeignApp, ForeignAppDirs, ForeignColor, ForeignConnection, ForeignConnectionRecord, + ForeignImportError, ForeignJumpHost, ForeignSsh, ForeignSshAuth, ForeignSsl, ForeignSslMode, ImportRequest, + Inventory, PasswordOutcome, +}; + +const APP: ForeignApp = ForeignApp::Dbx; + +// What a connection does on DBX that TablePro has no field for, keyed as the review lists it. None of +// these is carried: each one runs SQL, starts a command or routes the connection another way. +const PROXY: &str = "proxy"; +const HTTP_TUNNEL: &str = "httpTunnel"; +const SSH_TUNNEL: &str = "sshTunnel"; +const PROXY_COMMAND: &str = "sshProxyCommand"; +const INIT_SCRIPT: &str = "initScript"; +const ATTACHED_DATABASES: &str = "attachedDatabases"; +const CONNECTION_STRING: &str = "connectionString"; + +// The six swatches of DBX's connection dialog. +const SWATCHES: [(&str, ForeignColor); 6] = [ + ("#22c55e", ForeignColor::Green), + ("#eab308", ForeignColor::Yellow), + ("#f97316", ForeignColor::Orange), + ("#ef4444", ForeignColor::Red), + ("#3b82f6", ForeignColor::Blue), + ("#a855f7", ForeignColor::Purple), +]; + +pub(crate) struct Dbx { + data_dir: PathBuf, +} + +impl Dbx { + pub(crate) fn locate(dirs: &ForeignAppDirs) -> Self { + Self { + data_dir: locations::data_dir(dirs), + } + } + + fn store(&self) -> PathBuf { + self.data_dir.join(locations::STORE) + } + + fn open(&self) -> Result { + let path = self.store(); + if !path.is_file() { + return Err(ForeignImportError::FileNotFound { app: APP }); + } + Database::open_read_only(&path).ok_or_else(|| ForeignImportError::parse("Could not open dbx.db")) + } +} + +struct Lookups { + profiles: HashMap, + groups: HashMap>, + secret_names: HashMap>, +} + +impl AppReader for Dbx { + fn has_data(&self) -> bool { + self.store().is_file() + } + + fn inventory(&self) -> Inventory { + let Ok(db) = self.open() else { + return Inventory::default(); + }; + Inventory { + connections: configs(&db).map_or(0, |configs| configs.len()), + saved_queries: store::saved_sql_count(&db), + } + } + + fn collect( + &self, + request: &ImportRequest, + cancel: &CancellationToken, + ) -> Result { + let db = self.open()?; + let configs = configs(&db)?; + let lookups = Lookups { + profiles: store::tunnel_profiles(&db), + groups: store::group_paths(&db), + secret_names: store::secret_names(&db), + }; + let mut records = Vec::with_capacity(configs.len()); + for (config, engine) in configs { + check_cancel(cancel)?; + records.push(record(&config, engine, &lookups)); + } + let saved_queries = if request.include_saved_queries { + store::saved_sql(&db, request.saved_query_byte_limit, cancel)? + } else { + Vec::new() + }; + // DBX encrypts every secret under a key it keeps in the system keyring. + let passwords = if request.include_passwords { + PasswordOutcome::LeftInKeychain + } else { + PasswordOutcome::NotRequested + }; + assemble(APP, records, saved_queries, passwords) + } +} + +// A row that is not valid JSON or names no engine is left out, as DBX leaves out a row it cannot read. +fn configs(db: &Database) -> Result, ForeignImportError> { + let configs = store::connection_rows(db)? + .iter() + .filter_map(|json| match serde_json::from_str::(json) { + Ok(config) => { + let engine = engine_of(&config); + if engine.is_none() { + tracing::warn!("skipping a DBX connection with no database type"); + } + engine.map(|engine| (config, engine)) + } + Err(error) => { + tracing::warn!(%error, "skipping a DBX connection that does not parse"); + None + } + }) + .collect(); + Ok(configs) +} + +// DBX's `db_type` ids, from `plugins/connection-types`, with the driver profiles TablePro has an engine +// for. Any other id goes through as the engine's name, which the review reports when no driver opens it. +fn engine_of(config: &Config) -> Option { + let db_type = config.db_type.trim().to_lowercase(); + if db_type.is_empty() { + return None; + } + let profile = config + .driver_profile + .as_deref() + .unwrap_or_default() + .trim() + .to_lowercase(); + let name = match (db_type.as_str(), profile.as_str()) { + ("mysql", "mariadb") => "MariaDB", + ("mysql", "tidb") => "TiDB", + ("mysql", "oceanbase") => "OceanBase", + ("postgres", "cockroachdb") => "CockroachDB", + ("postgres", _) => "PostgreSQL", + ("sqlserver", _) => "SQL Server", + ("kingbase", _) => "KingbaseES", + ("saphana", _) => "SAP HANA", + ("prestosql", _) => "Presto", + ("cloudflare-d1", _) => "Cloudflare D1", + _ => return Some(engine::resolve(&db_type)), + }; + Some(name.to_owned()) +} + +fn record(config: &Config, engine: String, lookups: &Lookups) -> ForeignConnectionRecord { + let mut fields = BTreeMap::new(); + let file_field = engine::local_file_path_field(&engine); + // A file engine keeps its file's path where the host goes. + let (host, port, database) = match file_field { + Some(LocalFilePathField::Database) => (String::new(), 0, config.host.clone()), + Some(LocalFilePathField::AdditionalField(field)) => { + if !config.host.trim().is_empty() { + fields.insert(field.to_owned(), config.host.clone()); + } + (String::new(), 0, String::new()) + } + None => ( + if config.host.trim().is_empty() { + "localhost".to_owned() + } else { + config.host.clone() + }, + match config.port { + 0 => engine::default_port(&engine), + port => port, + }, + config.database.clone().unwrap_or_default(), + ), + }; + + let (hops, lost) = config.route(&lookups.profiles); + let kept_apart = |name: &str| { + lookups + .secret_names + .get(&config.id) + .is_some_and(|names| names.contains(name)) + }; + let written = |value: &Option| value.as_deref().is_some_and(|value| !value.trim().is_empty()); + let connection_string = written(&config.connection_string) || kept_apart("connection_string"); + let startup = [ + (written(&config.init_script) || kept_apart("init_script"), INIT_SCRIPT), + (!config.attached_databases.is_empty(), ATTACHED_DATABASES), + (connection_string, CONNECTION_STRING), + ]; + let not_kept = lost + .into_iter() + .chain(startup.into_iter().filter_map(|(set, key)| set.then_some(key))); + for key in not_kept { + fields.insert(key.to_owned(), "true".to_owned()); + } + + // DBX saves driver parameters with the secrets it encrypts, and connects through a connection string + // in place of the host, so the TLS either one sets cannot be read. + let tls_unread = file_field.is_none() && (kept_apart("url_params") || connection_string); + let ssl = ssl_of(config, &engine, tls_unread, &mut fields); + let settings = ForeignConnection { + name: if config.name.trim().is_empty() { + "Untitled".to_owned() + } else { + config.name.clone() + }, + host, + port, + database, + username: config.username.clone(), + engine, + ssh: ssh_of(&hops), + ssl, + color: color_of(config.color.as_deref()), + additional_fields: fields, + }; + ForeignConnectionRecord { + source_id: Some(config.id.clone()).filter(|id| !id.is_empty()), + group_path: lookups.groups.get(&config.id).cloned().unwrap_or_default(), + credentials: None, + settings, + } +} + +fn ssh_of(hops: &[SshLayer]) -> Option { + let (target, jumps) = hops.split_last()?; + let (auth, private_key_path) = sign_in(target); + let jump_hosts = jumps + .iter() + .map(|hop| { + let (auth, private_key_path) = sign_in(hop); + ForeignJumpHost { + host: hop.host.clone(), + port: Some(hop.port), + username: hop.user.clone(), + auth, + private_key_path, + } + }) + .collect(); + Some(ForeignSsh { + host: target.host.clone(), + port: Some(target.port), + username: target.user.clone(), + auth, + private_key_path, + jump_hosts, + }) +} + +// `auth_method` names the one way DBX signs in. A connection saved before it tried a key, a password, +// then the agent, by which of them it had. +fn sign_in(hop: &SshLayer) -> (ForeignSshAuth, String) { + let key = super::resolve_key_path(hop.key_path.trim()); + let auth = match hop.auth_method.trim() { + "agent" => ForeignSshAuth::Agent, + "key" | "key+password" => ForeignSshAuth::PrivateKey, + "password" | "none" => ForeignSshAuth::Password, + _ if !key.is_empty() => ForeignSshAuth::PrivateKey, + _ if hop.use_ssh_agent => ForeignSshAuth::Agent, + _ => ForeignSshAuth::Password, + }; + match auth { + ForeignSshAuth::PrivateKey => (auth, key), + ForeignSshAuth::Agent | ForeignSshAuth::Password => (auth, String::new()), + } +} + +// DBX has no TLS mode field: `ssl` turns TLS on, a CA file has MySQL check the server, and an +// `sslmode` driver parameter can name any mode. The stricter of the two is kept, so no connection +// imports weaker than DBX may open it, and TLS set some other way marks the record for the review. +fn ssl_of(config: &Config, engine: &str, unread: bool, fields: &mut BTreeMap) -> Option { + let params = if unread { + fields.insert(crate::TLS_SETTINGS_UNREAD.to_owned(), "true".to_owned()); + "" + } else { + config + .url_params + .as_deref() + .unwrap_or_default() + .trim() + .trim_start_matches('?') + }; + let url = format!("dbx://host/?{params}"); + let named = jdbc::url_ssl_or_mark(&url, fields).map(|ssl| ssl.mode); + let file = |path: &str| Some(path.trim().to_owned()).filter(|path| !path.is_empty()); + let ca = file(&config.ca_cert_path); + let implied = match (config.ssl, ca.is_some()) { + (true, true) => Some(ForeignSslMode::VerifyCa), + (true, false) => Some(ForeignSslMode::Required), + (false, _) => None, + }; + let mode = match (named, implied) { + (Some(named), Some(implied)) => stricter(named, implied), + (Some(mode), None) | (None, Some(mode)) => mode, + // DBX writes libpq's own default into the URL of a connection that names no mode. + (None, None) if matches!(engine, "PostgreSQL" | "CockroachDB" | "Redshift") => { + return jdbc::driver_default_ssl(&url, unread, ForeignSslMode::Preferred, fields); + } + (None, None) => return None, + }; + Some(ForeignSsl { + mode, + ca_certificate_path: ca, + client_certificate_path: file(&config.client_cert_path), + client_key_path: file(&config.client_key_path), + }) +} + +fn stricter(a: ForeignSslMode, b: ForeignSslMode) -> ForeignSslMode { + let rank = |mode: ForeignSslMode| match mode { + ForeignSslMode::Disabled => 0, + ForeignSslMode::Preferred => 1, + ForeignSslMode::Required => 2, + ForeignSslMode::VerifyCa => 3, + ForeignSslMode::VerifyIdentity => 4, + }; + if rank(b) > rank(a) { b } else { a } +} + +// A swatch, else the clear hue of a custom `#rrggbb`. +fn color_of(raw: Option<&str>) -> Option { + let raw = raw?.trim().to_ascii_lowercase(); + if let Some((_, color)) = SWATCHES.iter().find(|(swatch, _)| *swatch == raw) { + return Some(*color); + } + let digits = raw.strip_prefix('#').filter(|digits| digits.len() == 6)?; + let channel = |at: usize| { + digits + .get(at..at + 2) + .and_then(|pair| i64::from_str_radix(pair, 16).ok()) + }; + super::rgb_color(channel(0)?, channel(2)?, channel(4)?) +} + +#[cfg(test)] +mod tests { + use super::*; + + fn config(json: &str) -> Config { + serde_json::from_str(json).expect("a config") + } + + #[test] + fn every_swatch_and_a_clear_custom_hue_map_to_a_colour() { + for (swatch, color) in SWATCHES { + assert_eq!(color_of(Some(swatch)), Some(color)); + } + assert_eq!(color_of(Some(" #EF4444 ")), Some(ForeignColor::Red)); + assert_eq!(color_of(Some("#1020f0")), Some(ForeignColor::Blue)); + assert_eq!(color_of(Some("#808080")), None); + assert_eq!(color_of(Some("red")), None); + assert_eq!(color_of(Some("#fff")), None); + assert_eq!(color_of(Some("")), None); + assert_eq!(color_of(None), None); + } + + #[test] + fn a_driver_profile_names_the_engine_it_is() { + let engine = |db_type: &str, profile: &str| { + engine_of(&config(&format!( + r#"{{"db_type":"{db_type}","driver_profile":"{profile}"}}"# + ))) + }; + + assert_eq!(engine("mysql", "mariadb").as_deref(), Some("MariaDB")); + assert_eq!(engine("mysql", "polardb").as_deref(), Some("MySQL")); + assert_eq!(engine("postgres", "cockroachdb").as_deref(), Some("CockroachDB")); + assert_eq!(engine("postgres", "").as_deref(), Some("PostgreSQL")); + assert_eq!(engine("sqlserver", "").as_deref(), Some("SQL Server")); + assert_eq!(engine("duckdb", "").as_deref(), Some("DuckDB")); + assert_eq!(engine("hive", "").as_deref(), Some("hive")); + assert_eq!(engine(" ", "").as_deref(), None); + } + + #[test] + fn each_hop_signs_in_the_way_it_names() { + let hop = |json: &str| sign_in(&serde_json::from_str::(json).expect("a hop")); + + assert_eq!( + hop(r#"{"auth_method":"key","key_path":"id_ed25519"}"#), + (ForeignSshAuth::PrivateKey, "~/.ssh/id_ed25519".to_owned()) + ); + assert_eq!( + hop(r#"{"auth_method":"agent","key_path":"id_ed25519"}"#), + (ForeignSshAuth::Agent, String::new()) + ); + assert_eq!( + hop(r#"{"auth_method":"password","use_ssh_agent":true}"#), + (ForeignSshAuth::Password, String::new()) + ); + assert_eq!( + hop(r#"{"key_path":"/keys/id"}"#), + (ForeignSshAuth::PrivateKey, "/keys/id".to_owned()) + ); + assert_eq!(hop(r#"{"use_ssh_agent":true}"#), (ForeignSshAuth::Agent, String::new())); + assert_eq!(hop("{}"), (ForeignSshAuth::Password, String::new())); + } +} diff --git a/core/crates/foreign-import/src/apps/dbx/store.rs b/core/crates/foreign-import/src/apps/dbx/store.rs new file mode 100644 index 0000000000..e632fe6ad9 --- /dev/null +++ b/core/crates/foreign-import/src/apps/dbx/store.rs @@ -0,0 +1,310 @@ +use std::collections::{HashMap, HashSet}; + +use serde::Deserialize; +use tokio_util::sync::CancellationToken; + +use super::config::Layer; +use crate::app::check_cancel; +use crate::apps::sqlite::Database; +use crate::{ForeignImportError, ForeignSavedQuery, SavedQueryContent}; + +const BLANKS: [char; 4] = [' ', '\t', '\n', '\r']; + +pub(super) fn connection_rows(db: &Database) -> Result, ForeignImportError> { + let Some(mut statement) = db.prepare("SELECT config_json FROM connections ORDER BY rowid") else { + return Err(ForeignImportError::parse("dbx.db has no connections table")); + }; + let mut rows = Vec::new(); + while let Some(row) = statement.next_row() { + rows.extend(row.text(0)); + } + Ok(rows) +} + +pub(super) fn tunnel_profiles(db: &Database) -> HashMap { + let mut profiles = HashMap::new(); + let Some(mut statement) = db.prepare("SELECT id, config_json FROM tunnel_profiles") else { + return profiles; + }; + while let Some(row) = statement.next_row() { + let (Some(id), Some(json)) = (row.text(0), row.text(1)) else { + continue; + }; + match serde_json::from_str::(&json) { + Ok(layer) => { + profiles.insert(id, layer); + } + Err(error) => tracing::warn!(%error, "skipping a DBX tunnel profile that does not parse"), + } + } + profiles +} + +// The names of the settings each connection keeps among its secrets, such as `init_script`. Only the +// names are read, never a value. +pub(super) fn secret_names(db: &Database) -> HashMap> { + let mut names: HashMap> = HashMap::new(); + let Some(mut statement) = db.prepare("SELECT connection_id, key FROM connection_secrets") else { + return names; + }; + while let Some(row) = statement.next_row() { + if let (Some(connection), Some(key)) = (row.text(0), row.text(1)) { + names.entry(connection).or_default().insert(key); + } + } + names +} + +#[derive(Default, Deserialize)] +#[serde(default)] +struct Layout { + groups: Vec, + order: Vec, +} + +#[derive(Deserialize)] +struct Group { + id: String, + name: String, +} + +#[derive(Deserialize)] +#[serde(tag = "type", rename_all = "lowercase")] +enum Entry { + Group { + id: String, + children: Option>, + // The first layouts listed a group's connections flat. + #[serde(rename = "connectionIds")] + connection_ids: Option>, + }, + Connection { + id: String, + }, +} + +// The group path of each connection in DBX's sidebar. +pub(super) fn group_paths(db: &Database) -> HashMap> { + let mut paths = HashMap::new(); + let Some(mut statement) = db.prepare("SELECT layout_json FROM sidebar_layout WHERE id = 1") else { + return paths; + }; + let Some(json) = statement.next_row().and_then(|row| row.text(0)) else { + return paths; + }; + match serde_json::from_str::(&json) { + Ok(layout) => { + let names: HashMap<&str, &str> = layout + .groups + .iter() + .map(|group| (group.id.as_str(), group.name.as_str())) + .collect(); + walk(&layout.order, &names, &mut Vec::new(), &mut paths); + } + Err(error) => tracing::warn!(%error, "skipping a DBX sidebar layout that does not parse"), + } + paths +} + +fn walk( + entries: &[Entry], + names: &HashMap<&str, &str>, + path: &mut Vec, + paths: &mut HashMap>, +) { + for entry in entries { + match entry { + Entry::Connection { id } => { + paths.insert(id.clone(), path.clone()); + } + Entry::Group { + id, + children, + connection_ids, + } => { + // A group the layout has no name for adds no level. + let name = names.get(id.as_str()).map(|name| (*name).to_owned()); + path.extend(name.clone()); + if let Some(children) = children { + walk(children, names, path, paths); + } else { + for connection in connection_ids.iter().flatten() { + paths.insert(connection.clone(), path.clone()); + } + } + if name.is_some() { + path.pop(); + } + } + } + } +} + +pub(super) fn saved_sql_count(db: &Database) -> usize { + let columns = db.columns("saved_sql_files"); + if !columns.contains("sql_text") { + return 0; + } + let Some(mut statement) = db.prepare( + "SELECT COUNT(*) FROM saved_sql_files \ + WHERE trim(COALESCE(sql_text, ''), ' ' || char(9) || char(10) || char(13)) != ''", + ) else { + return 0; + }; + statement + .next_row() + .map_or(0, |row| usize::try_from(row.int64(0)).unwrap_or(0)) +} + +// Each saved SQL file with the connection it runs on; the default query DBX opens for a database +// counts as named by the app. +pub(super) fn saved_sql( + db: &Database, + limit: u64, + cancel: &CancellationToken, +) -> Result, ForeignImportError> { + let columns = db.columns("saved_sql_files"); + if !["connection_id", "name", "sql_text"] + .iter() + .all(|column| columns.contains(*column)) + { + return Ok(Vec::new()); + } + let column = |name: &'static str| if columns.contains(name) { name } else { "NULL" }; + let sql = format!( + "SELECT connection_id, {}, name, CASE WHEN length(CAST(sql_text AS BLOB)) > ?1 THEN NULL ELSE sql_text END, \ + length(CAST(sql_text AS BLOB)), {} FROM saved_sql_files ORDER BY rowid", + column("folder_id"), + column("is_default_query"), + ); + let Some(mut statement) = db.prepare(&sql) else { + return Ok(Vec::new()); + }; + statement.bind_int64(1, i64::try_from(limit).unwrap_or(i64::MAX)); + let folders = folders(db); + + let mut queries = Vec::new(); + while let Some(row) = statement.next_row() { + check_cancel(cancel)?; + let byte_count = u64::try_from(row.int64(4)).unwrap_or(0); + let content = if byte_count > limit { + SavedQueryContent::Oversized { byte_count } + } else { + match row.text(3) { + Some(sql) if !sql.trim_matches(BLANKS).is_empty() => SavedQueryContent::Text(sql), + _ => continue, + } + }; + queries.push(ForeignSavedQuery { + name: row.text(2).unwrap_or_default(), + content, + keyword: None, + folder_path: folder_path(row.text(1), &folders), + source_connection_id: row.text(0).filter(|id| !id.is_empty()), + is_auto_named: row.optional_int64(5).is_some_and(|flag| flag != 0), + }); + } + Ok(queries) +} + +struct Folder { + name: String, + parent: Option, +} + +fn folders(db: &Database) -> HashMap { + let columns = db.columns("saved_sql_folders"); + if !columns.contains("id") || !columns.contains("name") { + return HashMap::new(); + } + let parent = if columns.contains("parent_folder_id") { + "parent_folder_id" + } else { + "NULL" + }; + let Some(mut statement) = db.prepare(&format!("SELECT id, name, {parent} FROM saved_sql_folders")) else { + return HashMap::new(); + }; + let mut folders = HashMap::new(); + while let Some(row) = statement.next_row() { + if let Some(id) = row.text(0) { + folders.insert( + id, + Folder { + name: row.text(1).unwrap_or_default(), + parent: row.text(2).filter(|parent| !parent.is_empty()), + }, + ); + } + } + folders +} + +// A parent chain that loops ends at the first folder seen twice. +fn folder_path(folder: Option, folders: &HashMap) -> Vec { + let mut path = Vec::new(); + let mut seen = HashSet::new(); + let mut current = folder; + while let Some(id) = current + && let Some(folder) = folders.get(&id) + && seen.insert(id) + { + path.push(folder.name.clone()); + current = folder.parent.clone(); + } + path.reverse(); + path +} + +#[cfg(test)] +mod tests { + use super::*; + + fn folder(name: &str, parent: Option<&str>) -> Folder { + Folder { + name: name.to_owned(), + parent: parent.map(str::to_owned), + } + } + + #[test] + fn a_folder_path_runs_from_the_root_down_and_stops_on_a_loop() { + let folders = HashMap::from([ + ("f1".to_owned(), folder("Reports", None)), + ("f2".to_owned(), folder("Daily", Some("f1"))), + ("a".to_owned(), folder("A", Some("b"))), + ("b".to_owned(), folder("B", Some("a"))), + ]); + + assert_eq!(folder_path(Some("f2".to_owned()), &folders), ["Reports", "Daily"]); + assert_eq!(folder_path(Some("a".to_owned()), &folders), ["B", "A"]); + assert!(folder_path(Some("gone".to_owned()), &folders).is_empty()); + assert!(folder_path(None, &folders).is_empty()); + } + + #[test] + fn a_layout_nests_groups_and_reads_the_first_flat_form() { + let layout: Layout = serde_json::from_str( + r#"{"groups":[{"id":"g1","name":"Prod"},{"id":"g2","name":"EU"},{"id":"g3","name":"Old"}], + "order":[{"type":"connection","id":"top"}, + {"type":"group","id":"g1","children":[{"type":"group","id":"g2","children":[ + {"type":"connection","id":"deep"}]}]}, + {"type":"group","id":"g3","connectionIds":["flat"]}, + {"type":"group","id":"nameless","children":[{"type":"connection","id":"loose"}]}]}"#, + ) + .expect("a layout"); + let names: HashMap<&str, &str> = layout + .groups + .iter() + .map(|group| (group.id.as_str(), group.name.as_str())) + .collect(); + let mut paths = HashMap::new(); + + walk(&layout.order, &names, &mut Vec::new(), &mut paths); + + assert_eq!(paths["top"], Vec::::new()); + assert_eq!(paths["deep"], ["Prod", "EU"]); + assert_eq!(paths["flat"], ["Old"]); + assert_eq!(paths["loose"], Vec::::new()); + } +} diff --git a/core/crates/foreign-import/src/apps/mod.rs b/core/crates/foreign-import/src/apps/mod.rs index be5e0395b8..03f39a3aec 100644 --- a/core/crates/foreign-import/src/apps/mod.rs +++ b/core/crates/foreign-import/src/apps/mod.rs @@ -1,12 +1,16 @@ pub(crate) mod beekeeper; pub(crate) mod datagrip; pub(crate) mod dbeaver; +pub(crate) mod dbx; pub(crate) mod jdbc; pub(crate) mod jetbrains; pub(crate) mod navicat; pub(crate) mod sequel_ace; +pub(crate) mod sqlite; pub(crate) mod tableplus; +use crate::ForeignColor; + // A key path the app saved as a bare file name lives in `~/.ssh`, as ssh reads it. pub(crate) fn resolve_key_path(path: &str) -> String { if path.is_empty() { @@ -18,6 +22,25 @@ pub(crate) fn resolve_key_path(path: &str) -> String { format!("~/.ssh/{path}") } +// Only the clear hues of a colour the user picked map to a TablePro colour. +pub(crate) fn rgb_color(r: i64, g: i64, b: i64) -> Option { + if r > 200 && g < 100 && b < 100 { + Some(ForeignColor::Red) + } else if r > 200 && g > 100 && g < 200 && b < 100 { + Some(ForeignColor::Orange) + } else if r > 200 && g > 200 && b < 100 { + Some(ForeignColor::Yellow) + } else if r < 100 && g > 150 && b < 100 { + Some(ForeignColor::Green) + } else if r < 100 && g < 100 && b > 200 { + Some(ForeignColor::Blue) + } else if r > 100 && g < 100 && b > 150 { + Some(ForeignColor::Purple) + } else { + None + } +} + fn is_windows_absolute(path: &str) -> bool { let bytes = path.as_bytes(); path.starts_with("\\\\") diff --git a/core/crates/foreign-import/src/apps/beekeeper/sqlite.rs b/core/crates/foreign-import/src/apps/sqlite.rs similarity index 68% rename from core/crates/foreign-import/src/apps/beekeeper/sqlite.rs rename to core/crates/foreign-import/src/apps/sqlite.rs index 947db495ad..271821bef6 100644 --- a/core/crates/foreign-import/src/apps/beekeeper/sqlite.rs +++ b/core/crates/foreign-import/src/apps/sqlite.rs @@ -1,3 +1,4 @@ +use std::collections::HashSet; use std::ffi::{CStr, CString, c_int}; use std::marker::PhantomData; use std::path::Path; @@ -5,13 +6,13 @@ use std::ptr::{self, NonNull}; use libsqlite3_sys as ffi; -pub(super) struct Database { +pub(crate) struct Database { raw: NonNull, } impl Database { // Read-only, so no journal file appears in another app's data directory. - pub(super) fn open_read_only(path: &Path) -> Option { + pub(crate) fn open_read_only(path: &Path) -> Option { let path = c_path(path)?; let mut raw = ptr::null_mut(); let flags = ffi::SQLITE_OPEN_READONLY | ffi::SQLITE_OPEN_NOMUTEX; @@ -21,10 +22,40 @@ impl Database { let database = Self { raw: NonNull::new(raw)?, }; - (code == ffi::SQLITE_OK).then_some(database) + (code == ffi::SQLITE_OK && database.distrust_schema()).then_some(database) } - pub(super) fn prepare(&self, sql: &str) -> Option> { + // The file is another app's, so no SQL its schema holds runs: a view named like a table read here + // could loop forever on open. A query on a view fails instead. + fn distrust_schema(&self) -> bool { + [ + (ffi::SQLITE_DBCONFIG_DEFENSIVE, 1), + (ffi::SQLITE_DBCONFIG_TRUSTED_SCHEMA, 0), + (ffi::SQLITE_DBCONFIG_ENABLE_VIEW, 0), + (ffi::SQLITE_DBCONFIG_ENABLE_TRIGGER, 0), + ] + .into_iter() + .all(|(option, value): (c_int, c_int)| { + // SAFETY: the connection is open, and each option takes an int and an int pointer that may be null. + let code = unsafe { ffi::sqlite3_db_config(self.raw.as_ptr(), option, value, ptr::null_mut::()) }; + code == ffi::SQLITE_OK + }) + } + + pub(crate) fn columns(&self, table: &str) -> HashSet { + let Some(mut statement) = self.prepare(&format!("PRAGMA table_info({table})")) else { + return HashSet::new(); + }; + let mut names = HashSet::new(); + while let Some(row) = statement.next_row() { + if let Some(name) = row.text(1) { + names.insert(name); + } + } + names + } + + pub(crate) fn prepare(&self, sql: &str) -> Option> { let sql = CString::new(sql).ok()?; let mut raw = ptr::null_mut(); // SAFETY: the connection is open while `self` lives, `sql` is NUL-terminated, `raw` is a valid out pointer. @@ -44,18 +75,18 @@ impl Drop for Database { } } -pub(super) struct Statement<'db> { +pub(crate) struct Statement<'db> { raw: NonNull, _database: PhantomData<&'db Database>, } impl Statement<'_> { - pub(super) fn bind_int64(&mut self, index: c_int, value: i64) -> bool { + pub(crate) fn bind_int64(&mut self, index: c_int, value: i64) -> bool { // SAFETY: the statement is live until drop, and no row borrows it while it binds. unsafe { ffi::sqlite3_bind_int64(self.raw.as_ptr(), index, value) == ffi::SQLITE_OK } } - pub(super) fn bind_text(&mut self, index: c_int, value: &str) -> bool { + pub(crate) fn bind_text(&mut self, index: c_int, value: &str) -> bool { let Ok(length) = c_int::try_from(value.len()) else { return false; }; @@ -73,7 +104,7 @@ impl Statement<'_> { } // Columns are read through the row, so none is read once the statement is done. - pub(super) fn next_row(&mut self) -> Option> { + pub(crate) fn next_row(&mut self) -> Option> { // SAFETY: the statement is live until drop, and a row borrows it mutably, so no column is read mid-step. let code = unsafe { ffi::sqlite3_step(self.raw.as_ptr()) }; (code == ffi::SQLITE_ROW).then_some(Row { @@ -90,28 +121,28 @@ impl Drop for Statement<'_> { } } -pub(super) struct Row<'stmt> { +pub(crate) struct Row<'stmt> { statement: NonNull, _statement: PhantomData<&'stmt ()>, } impl Row<'_> { - pub(super) fn is_null(&self, column: c_int) -> bool { + pub(crate) fn is_null(&self, column: c_int) -> bool { // SAFETY: the borrow keeps the statement on this row; SQLite answers NULL for a column out of range. unsafe { ffi::sqlite3_column_type(self.statement.as_ptr(), column) == ffi::SQLITE_NULL } } - pub(super) fn int64(&self, column: c_int) -> i64 { + pub(crate) fn int64(&self, column: c_int) -> i64 { // SAFETY: as in `is_null`. unsafe { ffi::sqlite3_column_int64(self.statement.as_ptr(), column) } } - pub(super) fn optional_int64(&self, column: c_int) -> Option { + pub(crate) fn optional_int64(&self, column: c_int) -> Option { (!self.is_null(column)).then(|| self.int64(column)) } // Stops at the first NUL, as the macOS app reads it. - pub(super) fn text(&self, column: c_int) -> Option { + pub(crate) fn text(&self, column: c_int) -> Option { // SAFETY: as in `is_null`. let text = unsafe { ffi::sqlite3_column_text(self.statement.as_ptr(), column) }; if text.is_null() { diff --git a/core/crates/foreign-import/tests/dbx.rs b/core/crates/foreign-import/tests/dbx.rs new file mode 100644 index 0000000000..ec226d715b --- /dev/null +++ b/core/crates/foreign-import/tests/dbx.rs @@ -0,0 +1,504 @@ +#![expect( + clippy::expect_used, + reason = "the setup helpers fail a test the way the test functions themselves do" +)] + +use std::ffi::CString; +use std::fs; +use std::path::Path; +use std::ptr; +use std::sync::Arc; + +use tablepro_foreign_import::{ + CollectedConnection, CollectedImport, ForeignApp, ForeignAppDirs, ForeignColor, ForeignConnection, + ForeignImportError, ForeignImporter, ForeignPlatform, ForeignSshAuth, ForeignSslMode, ImportRequest, Inventory, + PasswordOutcome, PasswordSource, PlainHost, SavedQueryContent, SavedQuerySupport, TLS_SETTINGS_UNREAD, +}; +use tempfile::TempDir; +use tokio_util::sync::CancellationToken; + +const CONNECTIONS_ONLY: ImportRequest = ImportRequest { + include_passwords: false, + include_saved_queries: false, + saved_query_byte_limit: 900_000, +}; +const WITH_PASSWORDS: ImportRequest = ImportRequest { + include_passwords: true, + ..CONNECTIONS_ONLY +}; +const WITH_SAVED_QUERIES: ImportRequest = ImportRequest { + include_saved_queries: true, + ..CONNECTIONS_ONLY +}; + +struct Home { + _dir: TempDir, + importer: ForeignImporter, +} + +impl Home { + fn with_store(platform: ForeignPlatform, sql: &str) -> Self { + let dir = tempfile::tempdir().expect("a home"); + let dirs = ForeignAppDirs::conventional(platform, dir.path()); + // Tauri's app data directory for the bundle identifier `com.dbx.app`. + let folder = match platform { + ForeignPlatform::Windows => dirs.config.join("com.dbx.app"), + ForeignPlatform::Linux | ForeignPlatform::MacOs => dirs.data.join("com.dbx.app"), + }; + fs::create_dir_all(&folder).expect("the data folder"); + execute(&folder.join("dbx.db"), sql); + Self { + _dir: dir, + importer: ForeignImporter::new(ForeignApp::Dbx, dirs, Arc::new(PlainHost)), + } + } + + fn fixture() -> Self { + Self::with_store(ForeignPlatform::Linux, &fixture()) + } + + fn collect(&self, request: &ImportRequest) -> CollectedImport { + self.importer + .collect_blocking(request, &CancellationToken::new()) + .expect("an import") + } +} + +fn fixture() -> String { + let path = Path::new(env!("CARGO_MANIFEST_DIR")).join("tests/fixtures/dbx/dbx.sql"); + fs::read_to_string(path).expect("the fixture") +} + +fn execute(path: &Path, sql: &str) { + let path = CString::new(path.to_str().expect("a UTF-8 path")).expect("a path"); + let sql = CString::new(sql).expect("SQL"); + let flags = libsqlite3_sys::SQLITE_OPEN_READWRITE | libsqlite3_sys::SQLITE_OPEN_CREATE; + let mut db = ptr::null_mut(); + // SAFETY: both strings are NUL-terminated and outlive the calls, the handle is closed once, and the exec + // passes no callback. + let (opened, executed) = unsafe { + let opened = libsqlite3_sys::sqlite3_open_v2(path.as_ptr(), &mut db, flags, ptr::null()); + let executed = libsqlite3_sys::sqlite3_exec(db, sql.as_ptr(), None, ptr::null_mut(), ptr::null_mut()); + libsqlite3_sys::sqlite3_close(db); + (opened, executed) + }; + assert_eq!( + (opened, executed), + (libsqlite3_sys::SQLITE_OK, libsqlite3_sys::SQLITE_OK) + ); +} + +fn connection<'a>(collected: &'a CollectedImport, reference: &str) -> &'a CollectedConnection { + collected + .connections + .iter() + .find(|connection| connection.reference == reference) + .expect("the connection") +} + +fn settings<'a>(collected: &'a CollectedImport, reference: &str) -> &'a ForeignConnection { + &connection(collected, reference).settings +} + +fn not_kept(settings: &ForeignConnection) -> Vec<&str> { + settings.additional_fields.keys().map(String::as_str).collect() +} + +#[test] +fn a_connection_imports_with_its_tunnel_chain_certificates_colour_and_group() { + let home = Home::fixture(); + + let collected = home.collect(&CONNECTIONS_ONLY); + + assert_eq!(collected.app, ForeignApp::Dbx); + let orders = connection(&collected, "c-pg"); + assert_eq!(orders.group_path, ["Production", "EU"]); + assert!(orders.credentials.is_none()); + let settings = &orders.settings; + assert_eq!( + ( + settings.name.as_str(), + settings.engine.as_str(), + settings.host.as_str(), + settings.port, + settings.username.as_str(), + settings.database.as_str() + ), + ("Orders", "PostgreSQL", "db.example.com", 5433, "ada", "orders") + ); + assert_eq!(settings.color, Some(ForeignColor::Red)); + assert!(settings.additional_fields.is_empty()); + + let ssh = settings.ssh.as_ref().expect("the tunnel"); + assert_eq!( + (ssh.host.as_str(), ssh.port, ssh.username.as_str(), ssh.auth), + ("bastion.internal", Some(2200), "ops", ForeignSshAuth::Agent), + "the last hop takes its settings from the tunnel profile it names" + ); + assert_eq!(ssh.jump_hosts.len(), 1, "the first hop goes first"); + let edge = &ssh.jump_hosts[0]; + assert_eq!( + (edge.host.as_str(), edge.port, edge.username.as_str(), edge.auth), + ("edge.example.com", Some(22), "jump", ForeignSshAuth::PrivateKey) + ); + assert_eq!(edge.private_key_path, "~/.ssh/id_ed25519"); + + let ssl = settings.ssl.as_ref().expect("TLS"); + assert_eq!(ssl.mode, ForeignSslMode::VerifyIdentity); + assert_eq!(ssl.ca_certificate_path.as_deref(), Some("/certs/ca.pem")); + assert_eq!(ssl.client_certificate_path.as_deref(), Some("/certs/client.pem")); + assert_eq!(ssl.client_key_path.as_deref(), Some("/certs/client.key")); +} + +#[test] +fn rows_import_in_order_and_one_dbx_cannot_read_is_left_out() { + let home = Home::fixture(); + + let collected = home.collect(&CONNECTIONS_ONLY); + + let references: Vec<&str> = collected + .connections + .iter() + .map(|connection| connection.reference.as_str()) + .collect(); + assert_eq!( + references, + [ + "c-pg", "c-maria", "c-sqlite", "c-duck", "c-legacy", "c-mongo", "c-gone", "c-cache", "c-hive" + ] + ); + assert_eq!( + home.importer.inventory_blocking(), + Inventory { + connections: 9, + saved_queries: 4 + } + ); + assert_eq!(settings(&collected, "c-hive").engine, "hive"); +} + +#[test] +fn a_driver_profile_an_empty_host_and_a_zero_port_take_the_engines_defaults() { + let home = Home::fixture(); + + let collected = home.collect(&CONNECTIONS_ONLY); + + let shop = connection(&collected, "c-maria"); + assert_eq!(shop.group_path, ["Production"]); + assert_eq!( + ( + shop.settings.engine.as_str(), + shop.settings.host.as_str(), + shop.settings.port + ), + ("MariaDB", "localhost", 3306) + ); + assert_eq!(shop.settings.color, Some(ForeignColor::Blue)); + assert!(shop.settings.ssl.is_none()); + assert_eq!(connection(&collected, "c-cache").group_path, ["Caches"]); + assert_eq!(settings(&collected, "c-cache").color, Some(ForeignColor::Purple)); +} + +#[test] +fn a_file_database_keeps_its_path_where_the_engine_reads_it() { + let home = Home::fixture(); + + let collected = home.collect(&CONNECTIONS_ONLY); + + let local = settings(&collected, "c-sqlite"); + assert_eq!( + ( + local.engine.as_str(), + local.host.as_str(), + local.database.as_str(), + local.port + ), + ("SQLite", "", "~/data/app.db", 0) + ); + let analytics = settings(&collected, "c-duck"); + assert_eq!(analytics.engine, "DuckDB"); + assert_eq!( + analytics.additional_fields.get("duckdbFilePath").map(String::as_str), + Some("/data/warehouse.duckdb") + ); +} + +#[test] +fn a_connection_saved_before_transport_layers_keeps_its_tunnel() { + let home = Home::fixture(); + + let collected = home.collect(&WITH_PASSWORDS); + + let legacy = connection(&collected, "c-legacy"); + let ssh = legacy.settings.ssh.as_ref().expect("the tunnel"); + assert_eq!( + (ssh.host.as_str(), ssh.port, ssh.username.as_str(), ssh.auth), + ("bastion.example.com", Some(2222), "jump", ForeignSshAuth::PrivateKey) + ); + assert_eq!(ssh.private_key_path, "~/.ssh/id_rsa"); + assert!(ssh.jump_hosts.is_empty()); + assert!( + legacy.credentials.is_none(), + "a password an older DBX left in the JSON is not read" + ); +} + +#[test] +fn what_runs_sql_or_a_command_or_routes_elsewhere_is_named_and_never_carried() { + let home = Home::fixture(); + + let collected = home.collect(&WITH_PASSWORDS); + + assert_eq!(not_kept(settings(&collected, "c-maria")), ["proxy"]); + assert_eq!(not_kept(settings(&collected, "c-sqlite")), ["attachedDatabases"]); + assert_eq!( + not_kept(settings(&collected, "c-duck")), + ["duckdbFilePath", "initScript"] + ); + assert_eq!( + not_kept(settings(&collected, "c-mongo")), + ["connectionString", TLS_SETTINGS_UNREAD] + ); + assert_eq!(not_kept(settings(&collected, "c-cache")), ["sshProxyCommand"]); + let gone = settings(&collected, "c-gone"); + assert_eq!(not_kept(gone), ["sshTunnel"]); + assert!(gone.ssh.is_none()); + let cache = settings(&collected, "c-cache").ssh.as_ref().expect("the tunnel"); + assert_eq!( + (cache.host.as_str(), cache.auth), + ("bastion.example.com", ForeignSshAuth::Agent) + ); + + let everything = format!("{collected:?}"); + for value in [ + "INSTALL httpfs", + "cloudflared", + "socks.example.com", + "/data/logs.db", + "inline-legacy-secret", + "inline-ssh-secret", + "dbxenc1", + ] { + assert!(!everything.contains(value), "{value} was carried into the import"); + } +} + +#[test] +fn passwords_stay_in_the_keyring_dbx_encrypts_them_under() { + let home = Home::fixture(); + + let collected = home.collect(&WITH_PASSWORDS); + + assert_eq!(collected.passwords, PasswordOutcome::LeftInKeychain); + assert!( + collected + .connections + .iter() + .all(|connection| connection.credentials.is_none()) + ); + assert_eq!(home.collect(&CONNECTIONS_ONLY).passwords, PasswordOutcome::NotRequested); + assert_eq!(ForeignApp::Dbx.password_source(), PasswordSource::Keychain); +} + +#[test] +fn saved_sql_lands_with_its_connection_and_the_default_query_starts_unchecked() { + let home = Home::fixture(); + assert_eq!(ForeignApp::Dbx.saved_query_support(), SavedQuerySupport::Scripts); + assert!(home.collect(&CONNECTIONS_ONLY).saved_queries.is_empty()); + + let collected = home.collect(&WITH_SAVED_QUERIES); + + let queries: Vec<(&str, Option<&str>, Vec<&str>, bool)> = collected + .saved_queries + .iter() + .map(|query| { + ( + query.name.as_str(), + query.connection.as_deref(), + query.folder_path.iter().map(String::as_str).collect(), + query.suggested, + ) + }) + .collect(); + assert_eq!( + queries, + [ + ("revenue.sql", Some("c-pg"), vec!["Reports", "Monthly"], true), + ("default.sql", Some("c-pg"), vec![], false), + ("orphan.sql", None, vec!["DBX"], false), + ("huge.sql", Some("c-maria"), vec![], false), + ] + ); + assert_eq!( + collected.saved_queries[0].content, + SavedQueryContent::Text("SELECT sum(total) FROM orders".to_owned()) + ); + assert_eq!( + collected.saved_queries[3].content, + SavedQueryContent::Oversized { byte_count: 900_001 } + ); +} + +#[test] +fn the_store_is_found_where_tauri_keeps_it_on_each_platform() { + for platform in [ForeignPlatform::Linux, ForeignPlatform::MacOs, ForeignPlatform::Windows] { + let home = Home::with_store(platform, &fixture()); + + assert!(home.importer.is_available_blocking(), "{platform:?}"); + assert_eq!(home.importer.inventory_blocking().connections, 9, "{platform:?}"); + } +} + +#[test] +fn no_store_means_the_app_is_not_there() { + let home = tempfile::tempdir().expect("a home"); + let dirs = ForeignAppDirs::conventional(ForeignPlatform::Linux, home.path()); + let importer = ForeignImporter::new(ForeignApp::Dbx, dirs, Arc::new(PlainHost)); + + assert!(!importer.is_available_blocking()); + let error = importer + .collect_blocking(&CONNECTIONS_ONLY, &CancellationToken::new()) + .expect_err("nothing to read"); + assert!(matches!( + error, + ForeignImportError::FileNotFound { app: ForeignApp::Dbx } + )); +} + +// A view that never ends, under the name of the table read: opening the store must not run it. +#[test] +fn sql_the_store_holds_never_runs() { + let home = Home::with_store( + ForeignPlatform::Linux, + "CREATE VIEW connections AS WITH RECURSIVE n(x) AS (SELECT 1 UNION ALL SELECT x + 1 FROM n) \ + SELECT x AS rowid, '{\"id\":\"v\",\"db_type\":\"mysql\"}' AS config_json FROM n;", + ); + + assert_eq!(home.importer.inventory_blocking(), Inventory::default()); + let outcome = home + .importer + .collect_blocking(&CONNECTIONS_ONLY, &CancellationToken::new()); + assert!(matches!(outcome, Err(ForeignImportError::Parse { .. }))); +} + +const TLS_STORE: &str = r#" +CREATE TABLE connections (id TEXT PRIMARY KEY, config_json TEXT NOT NULL); +INSERT INTO connections VALUES ('t1', '{"id":"t1","name":"Mode only","db_type":"postgres","host":"h","ssl":false, + "url_params":"sslmode=require&application_name=x"}'); +INSERT INTO connections VALUES ('t2', '{"id":"t2","name":"Verified","db_type":"postgres","host":"h","ssl":true, + "ca_cert_path":"/certs/ca.pem","url_params":"?sslmode=verify-full"}'); +INSERT INTO connections VALUES ('t3', '{"id":"t3","name":"Off","db_type":"postgres","host":"h","ssl":false, + "url_params":"sslmode=disable"}'); +INSERT INTO connections VALUES ('t4', '{"id":"t4","name":"Unknown mode","db_type":"postgres","host":"h","ssl":false, + "url_params":"sslmode=surprise"}'); +INSERT INTO connections VALUES ('t5', '{"id":"t5","name":"Other driver key","db_type":"mysql","host":"h","ssl":false, + "url_params":"ssl-mode=VERIFY_IDENTITY"}'); +INSERT INTO connections VALUES ('t6', '{"id":"t6","name":"Postgres default","db_type":"postgres","host":"h", + "ssl":false,"url_params":"application_name=x"}'); +INSERT INTO connections VALUES ('t7', '{"id":"t7","name":"CA and require","db_type":"postgres","host":"h","ssl":true, + "ca_cert_path":"/certs/ca.pem","url_params":"sslmode=require"}'); +INSERT INTO connections VALUES ('t8', '{"id":"t8","name":"MySQL plain","db_type":"mysql","host":"h","ssl":false}'); +INSERT INTO connections VALUES ('t9', '{"id":"t9","name":"Switched on","db_type":"sqlserver","host":"h","ssl":true}'); +INSERT INTO connections VALUES ('t10', '{"id":"t10","name":"Parameters apart","db_type":"postgres","host":"h", + "ssl":false,"url_params":null}'); +INSERT INTO connections VALUES ('t11', '{"id":"t11","name":"Verified, parameters apart","db_type":"postgres", + "host":"h","ssl":true,"ca_cert_path":"/certs/ca.pem","url_params":null}'); +INSERT INTO connections VALUES ('t12', '{"id":"t12","name":"By URI","db_type":"mongodb","host":"mongo.internal", + "port":27017,"ssl":false,"connection_string":null}'); +INSERT INTO connections VALUES ('t13', '{"id":"t13","name":"File with parameters","db_type":"sqlite", + "host":"/data/app.db","ssl":false,"url_params":null}'); +CREATE TABLE connection_secrets (connection_id TEXT NOT NULL, key TEXT NOT NULL, secret TEXT NOT NULL, + secret_enc TEXT, PRIMARY KEY (connection_id, key)); +INSERT INTO connection_secrets VALUES ('t10', 'url_params', '', 'dbxenc1.cGFyYW1zLXQxMA.c3NsbW9kZQ'), + ('t11', 'url_params', '', 'dbxenc1.cGFyYW1zLXQxMQ.c3NsbW9kZQ'), + ('t12', 'connection_string', '', 'dbxenc1.c3RyaW5nLXQxMg.bW9uZ29kYg'), + ('t13', 'url_params', '', 'dbxenc1.cGFyYW1zLXQxMw.ZXh0ZW5zaW9u'); +"#; + +#[test] +fn a_tls_mode_in_the_driver_parameters_is_kept_and_never_weakened() { + let home = Home::with_store(ForeignPlatform::Linux, TLS_STORE); + + let collected = home.collect(&CONNECTIONS_ONLY); + + let mode = |reference: &str| settings(&collected, reference).ssl.as_ref().map(|ssl| ssl.mode); + assert_eq!(mode("t1"), Some(ForeignSslMode::Required)); + assert_eq!(mode("t2"), Some(ForeignSslMode::VerifyIdentity)); + assert_eq!( + settings(&collected, "t2") + .ssl + .as_ref() + .and_then(|ssl| ssl.ca_certificate_path.as_deref()), + Some("/certs/ca.pem") + ); + assert_eq!(mode("t3"), None, "a mode turned off stays off"); + assert_eq!( + mode("t7"), + Some(ForeignSslMode::VerifyCa), + "a parameter must not lower what the CA file implies" + ); + assert_eq!(mode("t8"), None); + assert_eq!(mode("t9"), Some(ForeignSslMode::Required)); + for unread in ["t4", "t5"] { + assert!( + settings(&collected, unread) + .additional_fields + .contains_key(TLS_SETTINGS_UNREAD), + "{unread} was imported with its TLS setting as though it had none" + ); + } + assert!( + !settings(&collected, "t1") + .additional_fields + .contains_key(TLS_SETTINGS_UNREAD) + ); +} + +// DBX saves `url_params` and a connection string among the secrets it encrypts and leaves null in the JSON. +#[test] +fn tls_dbx_keeps_encrypted_is_unread_and_no_default_stands_in_for_it() { + let home = Home::with_store(ForeignPlatform::Linux, TLS_STORE); + + let collected = home.collect(&CONNECTIONS_ONLY); + + let mode = |reference: &str| settings(&collected, reference).ssl.as_ref().map(|ssl| ssl.mode); + for unread in ["t10", "t11", "t12"] { + assert!( + settings(&collected, unread) + .additional_fields + .contains_key(TLS_SETTINGS_UNREAD), + "{unread} was imported as though its TLS were known" + ); + } + assert_eq!(mode("t10"), None); + assert_eq!(not_kept(settings(&collected, "t10")), [TLS_SETTINGS_UNREAD]); + assert_eq!( + mode("t11"), + Some(ForeignSslMode::VerifyCa), + "what `ssl` and the CA file say still holds" + ); + assert_eq!(mode("t12"), None); + assert!( + not_kept(settings(&collected, "t13")).is_empty(), + "a database file has no TLS to read" + ); +} + +#[test] +fn postgres_with_no_mode_named_prefers_tls_as_dbx_asks_libpq_to() { + let home = Home::with_store(ForeignPlatform::Linux, TLS_STORE); + + let collected = home.collect(&CONNECTIONS_ONLY); + + let fallback = settings(&collected, "t6"); + assert_eq!( + fallback.ssl.as_ref().map(|ssl| ssl.mode), + Some(ForeignSslMode::Preferred) + ); + assert!(fallback.additional_fields.contains_key("tlsFromDriverDefault")); + let legacy = Home::fixture().collect(&CONNECTIONS_ONLY); + assert_eq!( + settings(&legacy, "c-legacy").ssl.as_ref().map(|ssl| ssl.mode), + Some(ForeignSslMode::Preferred) + ); +} diff --git a/core/crates/foreign-import/tests/fixtures/dbx/dbx.sql b/core/crates/foreign-import/tests/fixtures/dbx/dbx.sql new file mode 100644 index 0000000000..c8a37a3284 --- /dev/null +++ b/core/crates/foreign-import/tests/fixtures/dbx/dbx.sql @@ -0,0 +1,121 @@ +-- The tables of t8y2/dbx at 39a77b5 (crates/dbx-core/src/persistence/storage.rs, SCHEMA_STATEMENTS), +-- with rows in the shape DBX writes them: `ConnectionConfig` and `TransportLayerConfig` as serde +-- writes them (crates/dbx-types/src/models/connection.rs), secrets as `dbxenc1` envelopes +-- (persistence/secret_codec.rs) whose bytes are random, and the sidebar layout of ai/mcp_policy.rs. +-- The inline `url_params`, `init_script` and passwords are as a DBX before its secret migration left +-- them; DBX now writes those to connection_secrets and null to the JSON. +CREATE TABLE IF NOT EXISTS connections ( + id TEXT PRIMARY KEY, + config_json TEXT NOT NULL +); +CREATE TABLE IF NOT EXISTS connection_secrets ( + connection_id TEXT NOT NULL, + key TEXT NOT NULL, + secret TEXT NOT NULL, + secret_enc TEXT, + PRIMARY KEY (connection_id, key) +); +CREATE TABLE IF NOT EXISTS tunnel_profiles ( + id TEXT PRIMARY KEY, + config_json TEXT NOT NULL +); +CREATE TABLE IF NOT EXISTS sidebar_layout ( + id INTEGER PRIMARY KEY CHECK (id = 1), + layout_json TEXT NOT NULL +); +CREATE TABLE IF NOT EXISTS saved_sql_folders ( + id TEXT PRIMARY KEY, + connection_id TEXT NOT NULL, + parent_folder_id TEXT, + name TEXT NOT NULL DEFAULT '', + order_index INTEGER NOT NULL DEFAULT 0, + created_at TEXT NOT NULL DEFAULT '', + updated_at TEXT NOT NULL DEFAULT '' +); +CREATE TABLE IF NOT EXISTS saved_sql_files ( + is_default_query INTEGER NOT NULL DEFAULT 0, + id TEXT PRIMARY KEY, + connection_id TEXT NOT NULL, + folder_id TEXT, + name TEXT NOT NULL DEFAULT '', + database_name TEXT NOT NULL DEFAULT '', + catalog_name TEXT, + schema_name TEXT, + sql_text TEXT NOT NULL DEFAULT '', + order_index INTEGER NOT NULL DEFAULT 0, + open_count INTEGER NOT NULL DEFAULT 0, + opened_at TEXT, + created_at TEXT NOT NULL DEFAULT '', + updated_at TEXT NOT NULL DEFAULT '' +); + +INSERT INTO connections VALUES ('c-pg', '{"id":"c-pg","name":"Orders","db_type":"postgres","driver_profile":"postgres", + "url_params":"sslmode=verify-full&application_name=dbx","host":"db.example.com","port":5433,"username":"ada", + "password":"","database":"orders","color":"#ef4444", + "transport_layers":[ + {"type":"ssh","id":"l1","name":"Edge","enabled":true,"host":"edge.example.com","port":22,"user":"jump", + "password":"","key_path":"id_ed25519","key_passphrase":"","connect_timeout_secs":5,"expose_lan":false, + "use_ssh_agent":false,"ssh_agent_sock_path":"","ssh_agent_key_fingerprint":"","auth_method":"key"}, + {"type":"ssh","id":"l2","name":"","enabled":true,"host":"","port":22,"user":"","password":"","key_path":"", + "key_passphrase":"","connect_timeout_secs":5,"expose_lan":false,"use_ssh_agent":false, + "ssh_agent_sock_path":"","ssh_agent_key_fingerprint":"","auth_method":"","profile_id":"tp1"}], + "connect_timeout_secs":10,"query_timeout_secs":60,"idle_timeout_secs":60,"manual_transaction_timeout_secs":300, + "idle_disconnect_secs":0,"keepalive_interval_secs":30,"ssl":true,"ca_cert_path":"/certs/ca.pem", + "client_cert_path":"/certs/client.pem","client_key_path":"/certs/client.key","sysdba":false, + "connection_string":null,"external_config":null,"jdbc_driver_class":null,"jdbc_driver_paths":[],"save_password":true}'); +INSERT INTO connections VALUES ('c-maria', '{"id":"c-maria","name":"Shop","db_type":"mysql","driver_profile":"mariadb", + "url_params":null,"host":"","port":0,"username":"root","password":"","database":null,"color":"#3b82f6", + "transport_layers":[{"type":"proxy","id":"p1","name":"","enabled":true,"proxy_type":"socks5", + "host":"socks.example.com","port":1080,"username":"","password":""}], + "ssl":false,"save_password":true}'); +INSERT INTO connections VALUES ('c-sqlite', '{"id":"c-sqlite","name":"Local file","db_type":"sqlite", + "host":"~/data/app.db","port":0,"username":"","password":"","database":null,"color":null, + "attached_databases":[{"name":"logs","path":"/data/logs.db"}],"ssl":false}'); +INSERT INTO connections VALUES ('c-duck', '{"id":"c-duck","name":"Analytics","db_type":"duckdb", + "host":"/data/warehouse.duckdb","port":0,"username":"","password":"","database":null, + "init_script":"INSTALL httpfs; LOAD httpfs; SELECT 1;","ssl":false}'); +INSERT INTO connections VALUES ('c-legacy', '{"id":"c-legacy","name":"Legacy","db_type":"postgres", + "host":"legacy.example.com","port":5432,"username":"app","password":"inline-legacy-secret","database":"app", + "ssh_enabled":true,"ssh_host":"bastion.example.com","ssh_port":2222,"ssh_user":"jump","ssh_key_path":"id_rsa", + "ssh_password":"inline-ssh-secret","ssl":false}'); +INSERT INTO connections VALUES ('c-mongo', '{"id":"c-mongo","name":"Atlas","db_type":"mongodb","host":"","port":27017, + "username":"","password":"","database":null,"ssl":false}'); +INSERT INTO connections VALUES ('c-gone', '{"id":"c-gone","name":"Behind a profile","db_type":"mysql", + "host":"10.0.0.5","port":3306,"username":"ops","password":"","database":null, + "transport_layers":[{"type":"ssh","id":"l9","enabled":true,"host":"","port":22,"profile_id":"deleted"}],"ssl":false}'); +INSERT INTO connections VALUES ('c-cache', '{"id":"c-cache","name":"Cache","db_type":"redis","host":"cache.internal", + "port":6379,"username":"","password":"","database":null,"color":"#c026d3", + "transport_layers":[{"type":"ssh","id":"l3","enabled":true,"host":"bastion.example.com","port":22,"user":"ops", + "use_ssh_agent":true,"auth_method":"agent","proxy_command":"cloudflared access ssh --hostname %h"}],"ssl":false}'); +INSERT INTO connections VALUES ('c-hive', '{"id":"c-hive","name":"Warehouse","db_type":"hive","host":"hive.internal", + "port":10000,"username":"","password":"","database":null,"ssl":false}'); +INSERT INTO connections VALUES ('c-broken', 'this is not json'); +INSERT INTO connections VALUES ('c-none', '{"id":"c-none","name":"No type","db_type":"","host":"x","port":1, + "username":"","password":""}'); + +INSERT INTO connection_secrets VALUES ('c-pg', 'password', '', + 'dbxenc1.-3FKum-zY14Xff6W.h5ByNWu8dVhyOfARKADbpJk0cgGYUaCLBP_eVw0I'); +INSERT INTO connection_secrets VALUES ('c-mongo', 'connection_string', '', + 'dbxenc1.6jf3WfZK-cmIAQlO.lknV_79yjNVuqp1ptK1V8Ho9ezogPpB6dVedneQENQFTSKfAO7sOSNI'); + +INSERT INTO tunnel_profiles VALUES ('tp1', '{"type":"ssh","id":"tp1","name":"Shared bastion","enabled":true, + "host":"bastion.internal","port":2200,"user":"ops","password":"","key_path":"","key_passphrase":"", + "connect_timeout_secs":5,"expose_lan":false,"use_ssh_agent":true,"ssh_agent_sock_path":"", + "ssh_agent_key_fingerprint":"","auth_method":"agent"}'); + +INSERT INTO sidebar_layout VALUES (1, '{"groups":[{"id":"g1","name":"Production"},{"id":"g2","name":"EU"}, + {"id":"g3","name":"Caches"}], + "order":[{"type":"group","id":"g1","children":[{"type":"group","id":"g2","children":[ + {"type":"connection","id":"c-pg"}]},{"type":"connection","id":"c-maria"}]}, + {"type":"group","id":"g3","connectionIds":["c-cache"]}, + {"type":"connection","id":"c-sqlite"}]}'); + +INSERT INTO saved_sql_folders (id, connection_id, parent_folder_id, name) VALUES + ('sf1', 'c-pg', NULL, 'Reports'), + ('sf2', 'c-pg', 'sf1', 'Monthly'); +INSERT INTO saved_sql_files (is_default_query, id, connection_id, folder_id, name, database_name, sql_text) VALUES + (0, 'q1', 'c-pg', 'sf2', 'revenue.sql', 'orders', 'SELECT sum(total) FROM orders'), + (1, 'q2', 'c-pg', NULL, 'default.sql', 'orders', 'SELECT 1'), + (0, 'q3', 'c-pg', NULL, 'blank.sql', 'orders', ' ' || char(10)), + (0, 'q4', 'c-deleted', NULL, 'orphan.sql', '', 'SELECT 2'), + (0, 'q5', 'c-maria', NULL, 'huge.sql', '', replace(hex(zeroblob(900001)), '00', 'x')); diff --git a/linux/changelog.d/4147.added.md b/linux/changelog.d/4147.added.md new file mode 100644 index 0000000000..7366acce3b --- /dev/null +++ b/linux/changelog.d/4147.added.md @@ -0,0 +1 @@ +Import from Another App reads the connections and saved SQL of DBX; its passwords stay in the system keyring.