feat(connection): add reusable tunnel profiles
This commit is contained in:
1 parent
4dcee1aca0
commit
977e7f78ca
34 files changed
+1335
-22
No files matched your search
@@ -23,6 +23,7 @@ use crate::db::proxy_tunnel::ProxyTunnelManager;
|
||||
use crate::db::ssh_tunnel::TunnelManager;
|
||||
use crate::models::connection::{
|
||||
parse_jdbc_host_port, parse_mongo_first_host, rewrite_jdbc_url_host, ConnectionConfig, DatabaseType,
|
||||
TransportLayerConfig,
|
||||
};
|
||||
use crate::path_utils::expand_tilde;
|
||||
use crate::plugins::{PluginDriverSession, PluginRegistry, PluginRuntimeEnv};
|
||||
@@ -1376,12 +1377,61 @@ impl AppState {
|
||||
Ok(pool_key)
|
||||
}
|
||||
|
||||
/// Returns the enabled transport layers for a connection with tunnel
|
||||
/// profile references resolved: a layer carrying a `profile_id` is
|
||||
/// replaced by the shared profile from storage (Settings > Tunnels), so
|
||||
/// edits to a profile take effect for every connection referencing it.
|
||||
/// Fails when a referenced profile no longer exists — connecting without
|
||||
/// the intended tunnel would silently bypass it.
|
||||
pub async fn resolved_transport_layers(
|
||||
&self,
|
||||
config: &ConnectionConfig,
|
||||
) -> Result<Vec<TransportLayerConfig>, String> {
|
||||
let layers = config.effective_transport_layers();
|
||||
if layers.iter().all(|layer| layer.profile_id().is_empty()) {
|
||||
return Ok(layers);
|
||||
}
|
||||
|
||||
let profiles: HashMap<String, TransportLayerConfig> = self
|
||||
.storage
|
||||
.load_tunnel_profiles()
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(|profile| (profile.id().to_string(), profile))
|
||||
.collect();
|
||||
|
||||
layers
|
||||
.into_iter()
|
||||
.map(|layer| {
|
||||
let profile_id = layer.profile_id();
|
||||
if profile_id.is_empty() {
|
||||
return Ok(layer);
|
||||
}
|
||||
let Some(profile) = profiles.get(profile_id) else {
|
||||
let label = if layer.name().is_empty() { profile_id } else { layer.name() };
|
||||
return Err(format!(
|
||||
"Tunnel profile '{label}' referenced by this connection no longer exists. Re-create it in Settings > Tunnels or edit the connection's tunnel settings."
|
||||
));
|
||||
};
|
||||
// Validate the stored reference again at connect time because synced or
|
||||
// externally supplied configs may bypass the editor's type constraints.
|
||||
if !layer.same_type_as(profile) {
|
||||
return Err(format!(
|
||||
"Tunnel profile '{}' has a different type than the referencing transport layer.",
|
||||
if layer.name().is_empty() { profile_id } else { layer.name() }
|
||||
));
|
||||
}
|
||||
Ok(layer.resolved_from_profile(profile))
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub async fn connection_host_port(
|
||||
&self,
|
||||
connection_id: &str,
|
||||
config: &ConnectionConfig,
|
||||
) -> Result<(String, u16), String> {
|
||||
let transport_layers = config.effective_transport_layers();
|
||||
let transport_layers = self.resolved_transport_layers(config).await?;
|
||||
if transport_layers.is_empty() {
|
||||
return Ok((config.host.clone(), config.port));
|
||||
}
|
||||
@@ -1406,7 +1456,7 @@ impl AppState {
|
||||
connection_id: &str,
|
||||
config: &ConnectionConfig,
|
||||
) -> Result<redis::aio::MultiplexedConnection, String> {
|
||||
let transport_layers = config.effective_transport_layers();
|
||||
let transport_layers = self.resolved_transport_layers(config).await?;
|
||||
if transport_layers.is_empty() {
|
||||
return db::redis_driver::connect_sentinel(config).await;
|
||||
}
|
||||
@@ -1536,7 +1586,7 @@ impl AppState {
|
||||
connection_id: &str,
|
||||
config: &ConnectionConfig,
|
||||
) -> Result<db::redis_driver::RedisClusterPool, String> {
|
||||
let transport_layers = config.effective_transport_layers();
|
||||
let transport_layers = self.resolved_transport_layers(config).await?;
|
||||
if transport_layers.is_empty() {
|
||||
return db::redis_driver::connect_cluster(config).await;
|
||||
}
|
||||
@@ -3098,8 +3148,8 @@ mod tests {
|
||||
use crate::database_capabilities;
|
||||
use crate::db;
|
||||
use crate::models::connection::{
|
||||
default_connect_timeout_secs, default_redis_key_separator, ConnectionConfig, DatabaseType, ProxyTunnelConfig,
|
||||
ProxyType, TransportLayerConfig,
|
||||
default_connect_timeout_secs, default_redis_key_separator, ConnectionConfig, DatabaseType, HttpTunnelConfig,
|
||||
ProxyTunnelConfig, ProxyType, SshTunnelConfig, TransportLayerConfig,
|
||||
};
|
||||
use crate::query;
|
||||
use crate::schema;
|
||||
@@ -4352,11 +4402,141 @@ mod tests {
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
fn ssh_layer(id: &str, profile_id: &str) -> SshTunnelConfig {
|
||||
SshTunnelConfig {
|
||||
id: id.to_string(),
|
||||
name: String::new(),
|
||||
enabled: true,
|
||||
host: String::new(),
|
||||
port: 22,
|
||||
user: String::new(),
|
||||
password: String::new(),
|
||||
key_path: String::new(),
|
||||
key_passphrase: String::new(),
|
||||
connect_timeout_secs: 5,
|
||||
expose_lan: false,
|
||||
use_ssh_agent: false,
|
||||
ssh_agent_sock_path: String::new(),
|
||||
auth_method: String::new(),
|
||||
profile_id: profile_id.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
fn proxy_layer(id: &str, profile_id: &str) -> ProxyTunnelConfig {
|
||||
ProxyTunnelConfig {
|
||||
id: id.to_string(),
|
||||
name: String::new(),
|
||||
enabled: true,
|
||||
proxy_type: ProxyType::Socks5,
|
||||
host: String::new(),
|
||||
port: 1080,
|
||||
username: String::new(),
|
||||
password: String::new(),
|
||||
profile_id: profile_id.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
fn http_tunnel_layer(id: &str, profile_id: &str) -> HttpTunnelConfig {
|
||||
HttpTunnelConfig {
|
||||
id: id.to_string(),
|
||||
name: String::new(),
|
||||
enabled: true,
|
||||
url: String::new(),
|
||||
token: String::new(),
|
||||
connect_timeout_secs: 10,
|
||||
profile_id: profile_id.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn resolved_transport_layers_substitutes_shared_profiles() {
|
||||
let (state, dir) = test_app_state().await;
|
||||
|
||||
let mut profile = ssh_layer("shared-bastion", "");
|
||||
profile.name = "Bastion".to_string();
|
||||
profile.host = "bastion.example.com".to_string();
|
||||
profile.user = "deploy".to_string();
|
||||
profile.password = "s3cret".to_string();
|
||||
profile.auth_method = "password".to_string();
|
||||
state.storage.save_tunnel_profiles(&[TransportLayerConfig::Ssh(profile)]).await.unwrap();
|
||||
|
||||
let mut config = mysql_config(Some("app"));
|
||||
config.transport_layers = vec![TransportLayerConfig::Ssh(ssh_layer("layer-1", "shared-bastion"))];
|
||||
|
||||
let resolved = state.resolved_transport_layers(&config).await.unwrap();
|
||||
assert_eq!(resolved.len(), 1);
|
||||
match &resolved[0] {
|
||||
TransportLayerConfig::Ssh(ssh) => {
|
||||
// Profile supplies the configuration; the layer keeps its identity.
|
||||
assert_eq!(ssh.id, "layer-1");
|
||||
assert_eq!(ssh.profile_id, "shared-bastion");
|
||||
assert_eq!(ssh.host, "bastion.example.com");
|
||||
assert_eq!(ssh.user, "deploy");
|
||||
assert_eq!(ssh.password, "s3cret");
|
||||
}
|
||||
other => panic!("expected ssh layer, got {other:?}"),
|
||||
}
|
||||
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn resolved_transport_layers_fails_closed_on_missing_profile() {
|
||||
let (state, dir) = test_app_state().await;
|
||||
|
||||
let mut config = mysql_config(Some("app"));
|
||||
config.transport_layers = vec![TransportLayerConfig::Ssh(ssh_layer("layer-1", "deleted-profile"))];
|
||||
|
||||
let err = state.resolved_transport_layers(&config).await.unwrap_err();
|
||||
assert!(err.contains("no longer exists"), "unexpected error: {err}");
|
||||
|
||||
// Disabled reference layers are filtered out before resolution, so a
|
||||
// dangling reference on a disabled layer must not block connecting.
|
||||
let mut disabled = ssh_layer("layer-1", "deleted-profile");
|
||||
disabled.enabled = false;
|
||||
config.transport_layers = vec![TransportLayerConfig::Ssh(disabled)];
|
||||
assert!(state.resolved_transport_layers(&config).await.unwrap().is_empty());
|
||||
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn resolved_transport_layers_rejects_mismatched_profile_types() {
|
||||
let (state, dir) = test_app_state().await;
|
||||
|
||||
let mismatches = [
|
||||
(
|
||||
TransportLayerConfig::Ssh(ssh_layer("layer", "shared")),
|
||||
TransportLayerConfig::Proxy(proxy_layer("shared", "")),
|
||||
),
|
||||
(
|
||||
TransportLayerConfig::Proxy(proxy_layer("layer", "shared")),
|
||||
TransportLayerConfig::HttpTunnel(http_tunnel_layer("shared", "")),
|
||||
),
|
||||
(
|
||||
TransportLayerConfig::HttpTunnel(http_tunnel_layer("layer", "shared")),
|
||||
TransportLayerConfig::Ssh(ssh_layer("shared", "")),
|
||||
),
|
||||
];
|
||||
|
||||
for (layer, profile) in mismatches {
|
||||
state.storage.save_tunnel_profiles(&[profile]).await.unwrap();
|
||||
let mut config = mysql_config(Some("app"));
|
||||
config.transport_layers = vec![layer];
|
||||
|
||||
let error = state.resolved_transport_layers(&config).await.unwrap_err();
|
||||
assert!(error.contains("different type"));
|
||||
}
|
||||
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn proxy_connection_uses_local_forward_endpoint() {
|
||||
let (state, dir) = test_app_state().await;
|
||||
let mut config = mysql_config(Some("app"));
|
||||
config.transport_layers = vec![TransportLayerConfig::Proxy(ProxyTunnelConfig {
|
||||
profile_id: String::new(),
|
||||
id: "proxy".to_string(),
|
||||
name: String::new(),
|
||||
enabled: true,
|
||||
@@ -4405,6 +4585,7 @@ mod tests {
|
||||
"auth": { "kind": "none" }
|
||||
}));
|
||||
config.transport_layers = vec![TransportLayerConfig::Proxy(ProxyTunnelConfig {
|
||||
profile_id: String::new(),
|
||||
id: "proxy".to_string(),
|
||||
name: String::new(),
|
||||
enabled: true,
|
||||
@@ -4462,6 +4643,7 @@ mod tests {
|
||||
"auth": { "kind": "none" }
|
||||
}));
|
||||
config.transport_layers = vec![TransportLayerConfig::Proxy(ProxyTunnelConfig {
|
||||
profile_id: String::new(),
|
||||
id: "proxy".to_string(),
|
||||
name: String::new(),
|
||||
enabled: true,
|
||||
|
||||
Reference in new issue
Block a user