feat(connection): add reusable tunnel profiles

This commit is contained in:
rainhan authored and GitHub committed 2026-07-12 22:03:46 +08:00
1 parent 4dcee1aca0
commit 977e7f78ca
34 files changed
+1335 -22

No files matched your search

+75 -9
View File
@@ -91,6 +91,10 @@ pub struct SyncSnapshot {
pub exported_at: String,
pub app_version: String,
pub connections: Vec<ConnectionConfig>,
/// Shared tunnel profiles (secrets scrubbed). `None` means the snapshot
/// predates tunnel profiles — applying it leaves local profiles alone.
#[serde(default)]
pub tunnel_profiles: Option<Vec<TransportLayerConfig>>,
pub sidebar_layout: Option<serde_json::Value>,
pub pinned_tree_node_ids: Vec<String>,
pub saved_sql: SavedSqlLibrary,
@@ -115,6 +119,9 @@ pub struct EncryptedSecretsBlob {
pub struct SensitiveSyncPayload {
pub connection_secrets: Vec<ConnectionSecretSnapshot>,
pub ai_config: Option<AiConfig>,
/// Full tunnel profiles including their secrets.
#[serde(default)]
pub tunnel_profiles: Option<Vec<TransportLayerConfig>>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -163,21 +170,27 @@ pub async fn build_sync_snapshot(
secrets_passphrase: Option<&str>,
) -> Result<SyncSnapshot, String> {
let mut connections = storage.load_connections().await?;
let mut tunnel_profiles = storage.load_tunnel_profiles().await?;
let encrypted_secrets = match normalized_passphrase(secrets_passphrase) {
Some(passphrase) => {
Some(encrypt_sensitive_payload(&build_sensitive_payload(storage, &connections).await?, passphrase)?)
}
Some(passphrase) => Some(encrypt_sensitive_payload(
&build_sensitive_payload(storage, &connections, &tunnel_profiles).await?,
passphrase,
)?),
None => None,
};
for config in &mut connections {
scrub_connection_secrets(config);
}
for profile in &mut tunnel_profiles {
profile.scrub_secrets();
}
Ok(SyncSnapshot {
schema_version: SNAPSHOT_SCHEMA_VERSION,
exported_at: Utc::now().to_rfc3339(),
app_version: app_version.into(),
connections,
tunnel_profiles: Some(tunnel_profiles),
sidebar_layout: storage.load_sidebar_layout().await?,
pinned_tree_node_ids: storage.load_pinned_tree_node_ids().await?,
saved_sql: storage.load_saved_sql_library().await?,
@@ -223,6 +236,9 @@ pub async fn apply_sync_snapshot(
}
storage.save_connection_metadata_preserving_secrets(&connections).await?;
if let Some(profiles) = &snapshot.tunnel_profiles {
storage.save_tunnel_profiles_preserving_secrets(profiles).await?;
}
if let Some(layout) = &snapshot.sidebar_layout {
storage.save_sidebar_layout(layout).await?;
}
@@ -602,6 +618,7 @@ fn webdav_password_account(config: &WebDavConfig) -> String {
async fn build_sensitive_payload(
storage: &Storage,
connections: &[ConnectionConfig],
tunnel_profiles: &[TransportLayerConfig],
) -> Result<SensitiveSyncPayload, String> {
let mut connection_secrets = Vec::new();
for config in connections {
@@ -648,7 +665,11 @@ async fn build_sensitive_payload(
push_nacos_external_config_secrets(&mut connection_secrets, config);
}
Ok(SensitiveSyncPayload { connection_secrets, ai_config: storage.load_ai_config().await? })
Ok(SensitiveSyncPayload {
connection_secrets,
ai_config: storage.load_ai_config().await?,
tunnel_profiles: Some(tunnel_profiles.to_vec()),
})
}
fn push_mq_external_config_secrets(secrets: &mut Vec<ConnectionSecretSnapshot>, config: &ConnectionConfig) {
@@ -769,6 +790,9 @@ async fn apply_sensitive_payload(storage: &Storage, payload: &SensitiveSyncPaylo
if let Some(ai_config) = &payload.ai_config {
storage.save_ai_config(ai_config).await?;
}
if let Some(profiles) = &payload.tunnel_profiles {
storage.save_tunnel_profiles(profiles).await?;
}
Ok(())
}
@@ -995,15 +1019,16 @@ fn parent_collection_paths(remote_path: &str) -> Vec<String> {
#[cfg(test)]
mod tests {
use super::{
build_sync_snapshot_with_saved_secrets, decrypt_sensitive_payload, encrypt_sensitive_payload,
forget_webdav_sync_secrets_passphrase, normalized_remote_path, parent_collection_paths,
resolve_webdav_sync_secrets_passphrase, save_webdav_sync_secrets_preference, scrub_connection_secrets,
snippet_file_content, snippet_response_id, webdav_sync_secrets_status, ConnectionSecretSnapshot,
apply_sync_snapshot, build_sync_snapshot, build_sync_snapshot_with_saved_secrets, decrypt_sensitive_payload,
encrypt_sensitive_payload, forget_webdav_sync_secrets_passphrase, normalized_remote_path,
parent_collection_paths, resolve_webdav_sync_secrets_passphrase, save_webdav_sync_secrets_preference,
scrub_connection_secrets, snippet_file_content, snippet_response_id, webdav_sync_secrets_status,
ApplySnapshotOptions, ConnectionSecretSnapshot,
SensitiveSyncPayload,
};
use crate::connection_secrets::NACOS_AUTH_PASSWORD_KEY;
use crate::models::connection::{
default_redis_key_separator, ConnectionConfig, DatabaseType, TransportLayerConfig,
default_redis_key_separator, ConnectionConfig, DatabaseType, SshTunnelConfig, TransportLayerConfig,
};
use crate::storage::Storage;
@@ -1172,6 +1197,7 @@ mod tests {
color: None,
transport_layers: vec![
TransportLayerConfig::Ssh(crate::models::connection::SshTunnelConfig {
profile_id: String::new(),
id: "hop-1".to_string(),
name: String::new(),
enabled: true,
@@ -1188,6 +1214,7 @@ mod tests {
auth_method: "password".to_string(),
}),
TransportLayerConfig::HttpTunnel(crate::models::connection::HttpTunnelConfig {
profile_id: String::new(),
id: "http".to_string(),
name: String::new(),
enabled: true,
@@ -1247,6 +1274,7 @@ mod tests {
#[test]
fn encrypted_sensitive_payload_round_trips() {
let payload = SensitiveSyncPayload {
tunnel_profiles: None,
connection_secrets: vec![
ConnectionSecretSnapshot {
connection_id: "c1".to_string(),
@@ -1271,6 +1299,7 @@ mod tests {
#[test]
fn encrypted_sensitive_payload_rejects_wrong_passphrase() {
let payload = SensitiveSyncPayload {
tunnel_profiles: None,
connection_secrets: vec![ConnectionSecretSnapshot {
connection_id: "c1".to_string(),
key: "password".to_string(),
@@ -1389,4 +1418,41 @@ mod tests {
secret.connection_id == "nacos" && secret.key == NACOS_AUTH_PASSWORD_KEY && secret.secret == "nacos-secret"
}));
}
#[tokio::test]
async fn sync_snapshot_round_trips_tunnel_profiles() {
let storage = Storage::open(&temp_db_path("tunnel-profiles-src")).await.unwrap();
let profile = TransportLayerConfig::Ssh(SshTunnelConfig {
id: "profile-1".to_string(),
name: "Bastion".to_string(),
enabled: true,
host: "bastion.example.com".to_string(),
port: 22,
user: "deploy".to_string(),
password: "tunnel-secret".to_string(),
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: "password".to_string(),
profile_id: String::new(),
});
storage.save_tunnel_profiles(std::slice::from_ref(&profile)).await.unwrap();
let snapshot = build_sync_snapshot(&storage, "test-version", None, Some("sync-pass")).await.unwrap();
// The plain snapshot carries the profiles with secrets scrubbed.
let public_profiles = snapshot.tunnel_profiles.as_ref().expect("tunnel profiles in snapshot");
let public_json = serde_json::to_string(public_profiles).unwrap();
assert!(!public_json.contains("tunnel-secret"));
// Applying with the passphrase restores the full profile on the target.
let target = Storage::open(&temp_db_path("tunnel-profiles-dst")).await.unwrap();
apply_sync_snapshot(&target, &snapshot, ApplySnapshotOptions { secrets_passphrase: Some("sync-pass") })
.await
.unwrap();
assert_eq!(target.load_tunnel_profiles().await.unwrap(), vec![profile]);
}
}