feat(connection): add HTTP tunnel
This commit is contained in:
1 parent
91c77e53c4
commit
b80f3e8088
21 files changed
+1515
-123
No files matched your search
@@ -319,6 +319,9 @@ fn scrub_connection_secrets(config: &mut ConnectionConfig) {
|
||||
TransportLayerConfig::Proxy(proxy) => {
|
||||
proxy.password.clear();
|
||||
}
|
||||
TransportLayerConfig::HttpTunnel(http) => {
|
||||
http.token.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
config.redis_sentinel_password.clear();
|
||||
@@ -364,6 +367,14 @@ async fn build_sensitive_payload(
|
||||
&proxy.password,
|
||||
);
|
||||
}
|
||||
TransportLayerConfig::HttpTunnel(http) => {
|
||||
push_secret(
|
||||
&mut connection_secrets,
|
||||
&config.id,
|
||||
&transport_layer_http_tunnel_token_key(index, layer),
|
||||
&http.token,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
push_secret(&mut connection_secrets, &config.id, "redis_sentinel_password", &config.redis_sentinel_password);
|
||||
@@ -449,6 +460,9 @@ async fn clear_connection_secrets(storage: &Storage, connections: &[ConnectionCo
|
||||
TransportLayerConfig::Proxy(_) => {
|
||||
storage.delete_secret(&config.id, &transport_layer_proxy_password_key(index, layer)).await?;
|
||||
}
|
||||
TransportLayerConfig::HttpTunnel(_) => {
|
||||
storage.delete_secret(&config.id, &transport_layer_http_tunnel_token_key(index, layer)).await?;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -476,6 +490,10 @@ fn transport_layer_proxy_password_key(index: usize, layer: &TransportLayerConfig
|
||||
format!("{}{}.proxy_password", TRANSPORT_LAYER_SECRET_PREFIX, transport_layer_secret_segment(index, layer))
|
||||
}
|
||||
|
||||
fn transport_layer_http_tunnel_token_key(index: usize, layer: &TransportLayerConfig) -> String {
|
||||
format!("{}{}.http_tunnel_token", TRANSPORT_LAYER_SECRET_PREFIX, transport_layer_secret_segment(index, layer))
|
||||
}
|
||||
|
||||
fn encrypt_sensitive_payload(payload: &SensitiveSyncPayload, passphrase: &str) -> Result<EncryptedSecretsBlob, String> {
|
||||
let plaintext = serde_json::to_vec(payload).map_err(|e| e.to_string())?;
|
||||
encrypt_bytes_with_secret(&plaintext, passphrase)
|
||||
@@ -605,21 +623,31 @@ mod tests {
|
||||
visible_schemas: None,
|
||||
attached_databases: Vec::new(),
|
||||
color: None,
|
||||
transport_layers: vec![TransportLayerConfig::Ssh(crate::models::connection::SshTunnelConfig {
|
||||
id: "hop-1".to_string(),
|
||||
name: String::new(),
|
||||
enabled: true,
|
||||
host: "bastion".to_string(),
|
||||
port: 22,
|
||||
user: "user".to_string(),
|
||||
password: "hop-password".to_string(),
|
||||
key_path: String::new(),
|
||||
key_passphrase: "hop-passphrase".to_string(),
|
||||
connect_timeout_secs: 5,
|
||||
expose_lan: false,
|
||||
use_ssh_agent: false,
|
||||
ssh_agent_sock_path: String::new(),
|
||||
})],
|
||||
transport_layers: vec![
|
||||
TransportLayerConfig::Ssh(crate::models::connection::SshTunnelConfig {
|
||||
id: "hop-1".to_string(),
|
||||
name: String::new(),
|
||||
enabled: true,
|
||||
host: "bastion".to_string(),
|
||||
port: 22,
|
||||
user: "user".to_string(),
|
||||
password: "hop-password".to_string(),
|
||||
key_path: String::new(),
|
||||
key_passphrase: "hop-passphrase".to_string(),
|
||||
connect_timeout_secs: 5,
|
||||
expose_lan: false,
|
||||
use_ssh_agent: false,
|
||||
ssh_agent_sock_path: String::new(),
|
||||
}),
|
||||
TransportLayerConfig::HttpTunnel(crate::models::connection::HttpTunnelConfig {
|
||||
id: "http".to_string(),
|
||||
name: String::new(),
|
||||
enabled: true,
|
||||
url: "https://dbx.example.com/dbx_tunnel.php".to_string(),
|
||||
token: "tunnel-token".to_string(),
|
||||
connect_timeout_secs: 10,
|
||||
}),
|
||||
],
|
||||
connect_timeout_secs: 5,
|
||||
query_timeout_secs: 30,
|
||||
idle_timeout_secs: 60,
|
||||
@@ -656,7 +684,11 @@ mod tests {
|
||||
assert!(ssh.password.is_empty());
|
||||
assert!(ssh.key_passphrase.is_empty());
|
||||
}
|
||||
TransportLayerConfig::Proxy(_) => panic!("expected ssh layer"),
|
||||
_ => panic!("expected ssh layer"),
|
||||
}
|
||||
match &config.transport_layers[1] {
|
||||
TransportLayerConfig::HttpTunnel(http) => assert!(http.token.is_empty()),
|
||||
_ => panic!("expected http tunnel layer"),
|
||||
}
|
||||
assert!(config.redis_sentinel_password.is_empty());
|
||||
assert!(config.connection_string.is_none());
|
||||
|
||||
@@ -17,6 +17,7 @@ use crate::agent_manager::{JavaRuntimeMode, DEFAULT_JRE_KEY};
|
||||
use crate::database_capabilities;
|
||||
use crate::db;
|
||||
use crate::db::agent_driver::AgentMethod;
|
||||
use crate::db::http_tunnel::HttpTunnelManager;
|
||||
use crate::db::proxy_tunnel::ProxyTunnelManager;
|
||||
use crate::db::ssh_tunnel::TunnelManager;
|
||||
use crate::models::connection::{
|
||||
@@ -161,6 +162,7 @@ pub struct AppState {
|
||||
pub running_queries: RunningQueries,
|
||||
pub tunnels: TunnelManager,
|
||||
pub proxy_tunnels: ProxyTunnelManager,
|
||||
pub http_tunnels: HttpTunnelManager,
|
||||
pub storage: Storage,
|
||||
pub plugins: PluginRegistry,
|
||||
pub agent_manager: crate::agent_manager::AgentManager,
|
||||
@@ -457,6 +459,7 @@ impl AppState {
|
||||
running_queries: RunningQueries::default(),
|
||||
tunnels: TunnelManager::new(),
|
||||
proxy_tunnels: ProxyTunnelManager::new(),
|
||||
http_tunnels: HttpTunnelManager::new(),
|
||||
storage,
|
||||
plugins: PluginRegistry::new(plugin_dir),
|
||||
agent_manager: crate::agent_manager::AgentManager::new_with_base_dir_and_app_version(
|
||||
@@ -1123,6 +1126,7 @@ impl AppState {
|
||||
remote_port,
|
||||
&self.tunnels,
|
||||
&self.proxy_tunnels,
|
||||
&self.http_tunnels,
|
||||
)
|
||||
.await?;
|
||||
|
||||
@@ -1154,6 +1158,7 @@ impl AppState {
|
||||
sentinel.port,
|
||||
&self.tunnels,
|
||||
&self.proxy_tunnels,
|
||||
&self.http_tunnels,
|
||||
)
|
||||
.await
|
||||
{
|
||||
@@ -1178,6 +1183,7 @@ impl AppState {
|
||||
layer_count,
|
||||
&self.tunnels,
|
||||
&self.proxy_tunnels,
|
||||
&self.http_tunnels,
|
||||
)
|
||||
.await;
|
||||
continue;
|
||||
@@ -1192,6 +1198,7 @@ impl AppState {
|
||||
master.port,
|
||||
&self.tunnels,
|
||||
&self.proxy_tunnels,
|
||||
&self.http_tunnels,
|
||||
)
|
||||
.await
|
||||
{
|
||||
@@ -1206,6 +1213,7 @@ impl AppState {
|
||||
layer_count,
|
||||
&self.tunnels,
|
||||
&self.proxy_tunnels,
|
||||
&self.http_tunnels,
|
||||
)
|
||||
.await;
|
||||
continue;
|
||||
@@ -1226,6 +1234,7 @@ impl AppState {
|
||||
layer_count,
|
||||
&self.tunnels,
|
||||
&self.proxy_tunnels,
|
||||
&self.http_tunnels,
|
||||
)
|
||||
.await;
|
||||
db::transport_layer_tunnel::stop_transport_layers(
|
||||
@@ -1233,6 +1242,7 @@ impl AppState {
|
||||
layer_count,
|
||||
&self.tunnels,
|
||||
&self.proxy_tunnels,
|
||||
&self.http_tunnels,
|
||||
)
|
||||
.await;
|
||||
}
|
||||
@@ -1247,6 +1257,7 @@ impl AppState {
|
||||
let redis_sentinel_prefix = redis_sentinel_transport_prefix(connection_id);
|
||||
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;
|
||||
}
|
||||
|
||||
result
|
||||
@@ -1278,6 +1289,7 @@ impl AppState {
|
||||
let redis_cluster_prefix = redis_cluster_transport_prefix(connection_id);
|
||||
self.tunnels.stop_tunnels_with_prefix(&redis_cluster_prefix).await;
|
||||
self.proxy_tunnels.stop_tunnels_with_prefix(&redis_cluster_prefix).await;
|
||||
self.http_tunnels.stop_tunnels_with_prefix(&redis_cluster_prefix).await;
|
||||
}
|
||||
|
||||
result
|
||||
@@ -1299,6 +1311,7 @@ impl AppState {
|
||||
node.port,
|
||||
&self.tunnels,
|
||||
&self.proxy_tunnels,
|
||||
&self.http_tunnels,
|
||||
)
|
||||
.await?;
|
||||
routes.push(db::redis_driver::RedisNodeRoute {
|
||||
@@ -1743,18 +1756,22 @@ impl AppState {
|
||||
let redis_cluster_prefix = redis_cluster_transport_prefix(connection_id);
|
||||
self.tunnels.stop_tunnels_with_prefix(&redis_cluster_prefix).await;
|
||||
self.proxy_tunnels.stop_tunnels_with_prefix(&redis_cluster_prefix).await;
|
||||
self.http_tunnels.stop_tunnels_with_prefix(&redis_cluster_prefix).await;
|
||||
let redis_sentinel_prefix = redis_sentinel_transport_prefix(connection_id);
|
||||
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;
|
||||
db::transport_layer_tunnel::stop_transport_layers(
|
||||
connection_id,
|
||||
layer_count,
|
||||
&self.tunnels,
|
||||
&self.proxy_tunnels,
|
||||
&self.http_tunnels,
|
||||
)
|
||||
.await;
|
||||
self.tunnels.stop_tunnel(connection_id).await;
|
||||
self.proxy_tunnels.stop_tunnel(connection_id).await;
|
||||
self.http_tunnels.stop_tunnel(connection_id).await;
|
||||
}
|
||||
|
||||
/// Health-check the base connection pool for a given connection_id.
|
||||
|
||||
@@ -188,6 +188,9 @@ fn persist_transport_layer_secrets(
|
||||
TransportLayerConfig::Proxy(proxy) => {
|
||||
persist_secret(store, connection_id, &transport_layer_proxy_password_key(index, layer), &proxy.password)?;
|
||||
}
|
||||
TransportLayerConfig::HttpTunnel(http) => {
|
||||
persist_secret(store, connection_id, &transport_layer_http_tunnel_token_key(index, layer), &http.token)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -244,6 +247,17 @@ fn hydrate_transport_layer_secrets(
|
||||
*needs_rewrite = true;
|
||||
}
|
||||
}
|
||||
TransportLayerConfig::HttpTunnel(http) => {
|
||||
let token_key = transport_layer_http_tunnel_token_key(index, &layer_for_key);
|
||||
if http.token.is_empty() {
|
||||
if let Some(secret) = store.get_secret(&config.id, &token_key)? {
|
||||
http.token = secret;
|
||||
}
|
||||
} else {
|
||||
store.set_secret(&config.id, &token_key, &http.token)?;
|
||||
*needs_rewrite = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
@@ -584,6 +598,10 @@ fn transport_layer_proxy_password_key(index: usize, layer: &TransportLayerConfig
|
||||
format!("{}{}.proxy_password", TRANSPORT_LAYER_SECRET_PREFIX, transport_layer_secret_segment(index, layer))
|
||||
}
|
||||
|
||||
fn transport_layer_http_tunnel_token_key(index: usize, layer: &TransportLayerConfig) -> String {
|
||||
format!("{}{}.http_tunnel_token", TRANSPORT_LAYER_SECRET_PREFIX, transport_layer_secret_segment(index, layer))
|
||||
}
|
||||
|
||||
fn read_connections(path: &Path) -> Result<Vec<ConnectionConfig>, String> {
|
||||
let json = std::fs::read_to_string(path).map_err(|e| e.to_string())?;
|
||||
serde_json::from_str(&json).map_err(|e| e.to_string())
|
||||
@@ -610,6 +628,9 @@ fn sanitize_connections(configs: &[ConnectionConfig]) -> Vec<ConnectionConfig> {
|
||||
TransportLayerConfig::Proxy(proxy) => {
|
||||
proxy.password.clear();
|
||||
}
|
||||
TransportLayerConfig::HttpTunnel(http) => {
|
||||
http.token.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
config.redis_sentinel_password.clear();
|
||||
@@ -632,7 +653,9 @@ mod tests {
|
||||
MAIN_PASSWORD_KEY, MQ_AUTH_PASSWORD_KEY, MQ_AUTH_TOKEN_KEY, MQ_TOKEN_SIGNING_KEY, REDIS_SENTINEL_PASSWORD_KEY,
|
||||
SSH_PASSWORD_KEY,
|
||||
};
|
||||
use crate::models::connection::{ConnectionConfig, DatabaseType, SshTunnelConfig, TransportLayerConfig};
|
||||
use crate::models::connection::{
|
||||
ConnectionConfig, DatabaseType, HttpTunnelConfig, SshTunnelConfig, TransportLayerConfig,
|
||||
};
|
||||
use std::cell::RefCell;
|
||||
use std::collections::HashMap;
|
||||
use std::path::Path;
|
||||
@@ -758,6 +781,17 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
fn http_tunnel(id: &str, token: &str) -> TransportLayerConfig {
|
||||
TransportLayerConfig::HttpTunnel(HttpTunnelConfig {
|
||||
id: id.to_string(),
|
||||
name: String::new(),
|
||||
enabled: true,
|
||||
url: "https://dbx.example.com/dbx_tunnel.php".to_string(),
|
||||
token: token.to_string(),
|
||||
connect_timeout_secs: 10,
|
||||
})
|
||||
}
|
||||
|
||||
fn read_configs(path: &Path) -> Vec<ConnectionConfig> {
|
||||
let json = std::fs::read_to_string(path).unwrap();
|
||||
serde_json::from_str(&json).unwrap()
|
||||
@@ -785,7 +819,7 @@ mod tests {
|
||||
assert_eq!(ssh.password, "");
|
||||
assert_eq!(ssh.key_passphrase, "");
|
||||
}
|
||||
TransportLayerConfig::Proxy(_) => panic!("expected ssh layer"),
|
||||
_ => panic!("expected ssh layer"),
|
||||
}
|
||||
assert_eq!(persisted[0].redis_sentinel_password, "");
|
||||
}
|
||||
@@ -812,11 +846,37 @@ mod tests {
|
||||
assert_eq!(ssh.password, "hop-secret");
|
||||
assert_eq!(ssh.key_passphrase, "hop-key");
|
||||
}
|
||||
TransportLayerConfig::Proxy(_) => panic!("expected ssh layer"),
|
||||
_ => panic!("expected ssh layer"),
|
||||
}
|
||||
assert_eq!(loaded[0].redis_sentinel_password, "sentinel-secret");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn save_and_load_connections_move_http_tunnel_token_to_secret_store() {
|
||||
let path = temp_connections_file("http-tunnel-token");
|
||||
let store = MemorySecretStore::default();
|
||||
let mut config = connection("main", "", "");
|
||||
config.transport_layers = vec![http_tunnel("http", "tunnel-secret")];
|
||||
|
||||
save_connections_to_file(&path, &[config], &store).unwrap();
|
||||
|
||||
assert_eq!(
|
||||
store.get_existing("main", "transport_layers.http.http_tunnel_token").as_deref(),
|
||||
Some("tunnel-secret")
|
||||
);
|
||||
let persisted = read_configs(&path);
|
||||
match &persisted[0].transport_layers[0] {
|
||||
TransportLayerConfig::HttpTunnel(http) => assert_eq!(http.token, ""),
|
||||
_ => panic!("expected http tunnel layer"),
|
||||
}
|
||||
|
||||
let loaded = load_connections_from_file(&path, &store).unwrap();
|
||||
match &loaded[0].transport_layers[0] {
|
||||
TransportLayerConfig::HttpTunnel(http) => assert_eq!(http.token, "tunnel-secret"),
|
||||
_ => panic!("expected http tunnel layer"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn load_connections_migrates_plaintext_passwords_and_rewrites_sanitized_file() {
|
||||
let path = temp_connections_file("migrates-plaintext");
|
||||
@@ -843,7 +903,7 @@ mod tests {
|
||||
assert_eq!(loaded[0].password, "plain-db");
|
||||
match &loaded[0].transport_layers[0] {
|
||||
TransportLayerConfig::Ssh(ssh) => assert_eq!(ssh.password, "plain-ssh"),
|
||||
TransportLayerConfig::Proxy(_) => panic!("expected ssh layer"),
|
||||
_ => panic!("expected ssh layer"),
|
||||
}
|
||||
assert_eq!(store.get_existing("legacy", MAIN_PASSWORD_KEY).as_deref(), Some("plain-db"));
|
||||
assert_eq!(store.get_existing("legacy", "transport_layers.legacy.ssh_password").as_deref(), Some("plain-ssh"));
|
||||
@@ -851,7 +911,7 @@ mod tests {
|
||||
assert_eq!(persisted[0].password, "");
|
||||
match &persisted[0].transport_layers[0] {
|
||||
TransportLayerConfig::Ssh(ssh) => assert_eq!(ssh.password, ""),
|
||||
TransportLayerConfig::Proxy(_) => panic!("expected ssh layer"),
|
||||
_ => panic!("expected ssh layer"),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,413 @@
|
||||
use bytes::Bytes;
|
||||
use reqwest::header::{HeaderValue, AUTHORIZATION, CONTENT_TYPE};
|
||||
use reqwest::{Client, StatusCode, Url};
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||
use tokio::net::{TcpListener, TcpStream};
|
||||
use tokio::sync::Mutex;
|
||||
use tokio::task::JoinHandle;
|
||||
use tokio::time::{timeout, Duration};
|
||||
use uuid::Uuid;
|
||||
|
||||
const DEFAULT_CONNECT_TIMEOUT: Duration = Duration::from_secs(10);
|
||||
const TCP_READ_BUFFER_SIZE: usize = 16 * 1024;
|
||||
const HTTP_READ_WAIT_MS: u64 = 1_000;
|
||||
const HTTP_REQUEST_TIMEOUT: Duration = Duration::from_secs(15);
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct HttpTunnelManager {
|
||||
tunnels: Mutex<HashMap<String, (JoinHandle<()>, u16)>>,
|
||||
}
|
||||
|
||||
impl HttpTunnelManager {
|
||||
pub fn new() -> Self {
|
||||
Self { tunnels: Mutex::new(HashMap::new()) }
|
||||
}
|
||||
|
||||
pub async fn start_tunnel(
|
||||
&self,
|
||||
connection_id: &str,
|
||||
tunnel_url: &str,
|
||||
token: &str,
|
||||
connect_timeout_secs: u64,
|
||||
remote_host: &str,
|
||||
remote_port: u16,
|
||||
) -> Result<u16, String> {
|
||||
if let Some(local_port) = self.local_port(connection_id).await {
|
||||
return Ok(local_port);
|
||||
}
|
||||
let local_port = portpicker::pick_unused_port().ok_or("No available port")?;
|
||||
let listener = TcpListener::bind(("127.0.0.1", local_port))
|
||||
.await
|
||||
.map_err(|e| format!("Failed to bind HTTP tunnel local port: {e}"))?;
|
||||
|
||||
let endpoint =
|
||||
Arc::new(HttpTunnelEndpoint::new(tunnel_url, token, connect_timeout_secs, remote_host, remote_port)?);
|
||||
let handle = tokio::spawn(http_tunnel_forward_loop(listener, endpoint));
|
||||
|
||||
let mut tunnels = self.tunnels.lock().await;
|
||||
if let Some((_, existing_port)) = tunnels.get(connection_id) {
|
||||
handle.abort();
|
||||
return Ok(*existing_port);
|
||||
}
|
||||
|
||||
tunnels.insert(connection_id.to_string(), (handle, local_port));
|
||||
Ok(local_port)
|
||||
}
|
||||
|
||||
pub async fn local_port(&self, connection_id: &str) -> Option<u16> {
|
||||
self.tunnels.lock().await.get(connection_id).map(|(_, port)| *port)
|
||||
}
|
||||
|
||||
pub async fn stop_tunnel(&self, connection_id: &str) {
|
||||
if let Some((handle, _)) = self.tunnels.lock().await.remove(connection_id) {
|
||||
handle.abort();
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn stop_tunnels_with_prefix(&self, connection_id_prefix: &str) {
|
||||
let mut tunnels = self.tunnels.lock().await;
|
||||
let keys: Vec<String> = tunnels.keys().filter(|key| key.starts_with(connection_id_prefix)).cloned().collect();
|
||||
for key in keys {
|
||||
if let Some((handle, _)) = tunnels.remove(&key) {
|
||||
handle.abort();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct HttpTunnelEndpoint {
|
||||
url: String,
|
||||
token: String,
|
||||
connect_timeout: Duration,
|
||||
target_host: String,
|
||||
target_port: u16,
|
||||
client: Client,
|
||||
}
|
||||
|
||||
impl HttpTunnelEndpoint {
|
||||
fn new(
|
||||
tunnel_url: &str,
|
||||
token: &str,
|
||||
connect_timeout_secs: u64,
|
||||
remote_host: &str,
|
||||
remote_port: u16,
|
||||
) -> Result<Self, String> {
|
||||
validate_script_url(tunnel_url)?;
|
||||
Ok(Self {
|
||||
url: tunnel_url.trim().to_string(),
|
||||
token: token.trim().to_string(),
|
||||
connect_timeout: effective_connect_timeout(connect_timeout_secs),
|
||||
target_host: remote_host.to_string(),
|
||||
target_port: remote_port,
|
||||
client: Client::new(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
async fn http_tunnel_forward_loop(listener: TcpListener, endpoint: Arc<HttpTunnelEndpoint>) {
|
||||
loop {
|
||||
let (inbound, _) = match listener.accept().await {
|
||||
Ok(pair) => pair,
|
||||
Err(_) => break,
|
||||
};
|
||||
let endpoint = endpoint.clone();
|
||||
tokio::spawn(async move {
|
||||
let _ = bridge_tcp_to_http_script(inbound, endpoint).await;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async fn bridge_tcp_to_http_script(mut inbound: TcpStream, endpoint: Arc<HttpTunnelEndpoint>) -> Result<(), String> {
|
||||
let session = Uuid::new_v4().simple().to_string();
|
||||
open_session(&endpoint, &session).await?;
|
||||
|
||||
let (mut tcp_reader, mut tcp_writer) = inbound.split();
|
||||
let tcp_to_http = async {
|
||||
let mut buf = vec![0_u8; TCP_READ_BUFFER_SIZE];
|
||||
loop {
|
||||
let n = tcp_reader.read(&mut buf).await.map_err(|e| format!("Failed to read local TCP stream: {e}"))?;
|
||||
if n == 0 {
|
||||
return Ok::<(), String>(());
|
||||
}
|
||||
write_session(&endpoint, &session, Bytes::copy_from_slice(&buf[..n])).await?;
|
||||
}
|
||||
};
|
||||
|
||||
let http_to_tcp = async {
|
||||
loop {
|
||||
let bytes = read_session(&endpoint, &session).await?;
|
||||
if !bytes.is_empty() {
|
||||
tcp_writer.write_all(&bytes).await.map_err(|e| format!("Failed to write local TCP stream: {e}"))?;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
let result = tokio::select! {
|
||||
result = tcp_to_http => result,
|
||||
result = http_to_tcp => result,
|
||||
};
|
||||
let _ = close_session(&endpoint, &session).await;
|
||||
result
|
||||
}
|
||||
|
||||
async fn open_session(endpoint: &HttpTunnelEndpoint, session: &str) -> Result<(), String> {
|
||||
let url = script_url(
|
||||
&endpoint.url,
|
||||
"open",
|
||||
session,
|
||||
Some((&endpoint.target_host, endpoint.target_port)),
|
||||
Some(endpoint.connect_timeout.as_secs()),
|
||||
None,
|
||||
)?;
|
||||
let response = timeout(
|
||||
endpoint.connect_timeout + HTTP_REQUEST_TIMEOUT,
|
||||
authorized_request(endpoint, endpoint.client.post(url)).send(),
|
||||
)
|
||||
.await
|
||||
.map_err(|_| "HTTP tunnel script open timed out".to_string())?
|
||||
.map_err(|e| format!("Failed to open HTTP tunnel script session: {e}"))?;
|
||||
ensure_success(response, "open HTTP tunnel script session").await
|
||||
}
|
||||
|
||||
async fn write_session(endpoint: &HttpTunnelEndpoint, session: &str, data: Bytes) -> Result<(), String> {
|
||||
let url = script_url(&endpoint.url, "write", session, None, None, None)?;
|
||||
let response = timeout(
|
||||
HTTP_REQUEST_TIMEOUT,
|
||||
authorized_request(endpoint, endpoint.client.post(url))
|
||||
.header(CONTENT_TYPE, "application/octet-stream")
|
||||
.body(data)
|
||||
.send(),
|
||||
)
|
||||
.await
|
||||
.map_err(|_| "HTTP tunnel script write timed out".to_string())?
|
||||
.map_err(|e| format!("Failed to write HTTP tunnel script session: {e}"))?;
|
||||
ensure_success(response, "write HTTP tunnel script session").await
|
||||
}
|
||||
|
||||
async fn read_session(endpoint: &HttpTunnelEndpoint, session: &str) -> Result<Bytes, String> {
|
||||
let url = script_url(&endpoint.url, "read", session, None, None, Some(HTTP_READ_WAIT_MS))?;
|
||||
let response = timeout(HTTP_REQUEST_TIMEOUT, authorized_request(endpoint, endpoint.client.post(url)).send())
|
||||
.await
|
||||
.map_err(|_| "HTTP tunnel script read timed out".to_string())?
|
||||
.map_err(|e| format!("Failed to read HTTP tunnel script session: {e}"))?;
|
||||
let status = response.status();
|
||||
if status == StatusCode::NO_CONTENT {
|
||||
return Ok(Bytes::new());
|
||||
}
|
||||
if status == StatusCode::OK {
|
||||
return response.bytes().await.map_err(|e| format!("Failed to read HTTP tunnel script response: {e}"));
|
||||
}
|
||||
if status == StatusCode::GONE {
|
||||
return Err("HTTP tunnel script session closed".to_string());
|
||||
}
|
||||
Err(error_response_message(response, "read HTTP tunnel script session").await)
|
||||
}
|
||||
|
||||
async fn close_session(endpoint: &HttpTunnelEndpoint, session: &str) -> Result<(), String> {
|
||||
let url = script_url(&endpoint.url, "close", session, None, None, None)?;
|
||||
let response = timeout(HTTP_REQUEST_TIMEOUT, authorized_request(endpoint, endpoint.client.post(url)).send())
|
||||
.await
|
||||
.map_err(|_| "HTTP tunnel script close timed out".to_string())?
|
||||
.map_err(|e| format!("Failed to close HTTP tunnel script session: {e}"))?;
|
||||
ensure_success(response, "close HTTP tunnel script session").await
|
||||
}
|
||||
|
||||
fn authorized_request(endpoint: &HttpTunnelEndpoint, builder: reqwest::RequestBuilder) -> reqwest::RequestBuilder {
|
||||
if endpoint.token.is_empty() {
|
||||
return builder;
|
||||
}
|
||||
match HeaderValue::from_str(&endpoint.token) {
|
||||
Ok(token) => builder
|
||||
.header("X-DBX-Tunnel-Token", token.clone())
|
||||
.header(AUTHORIZATION, format!("Bearer {}", endpoint.token)),
|
||||
Err(_) => builder,
|
||||
}
|
||||
}
|
||||
|
||||
async fn ensure_success(response: reqwest::Response, action: &str) -> Result<(), String> {
|
||||
if response.status().is_success() {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(error_response_message(response, action).await)
|
||||
}
|
||||
}
|
||||
|
||||
async fn error_response_message(response: reqwest::Response, action: &str) -> String {
|
||||
let status = response.status();
|
||||
let body = response.text().await.unwrap_or_default();
|
||||
if body.trim().is_empty() {
|
||||
format!("Failed to {action}: HTTP {status}")
|
||||
} else {
|
||||
format!("Failed to {action}: HTTP {status}: {}", body.trim())
|
||||
}
|
||||
}
|
||||
|
||||
fn script_url(
|
||||
tunnel_url: &str,
|
||||
action: &str,
|
||||
session: &str,
|
||||
target: Option<(&str, u16)>,
|
||||
connect_timeout_secs: Option<u64>,
|
||||
wait_ms: Option<u64>,
|
||||
) -> Result<Url, String> {
|
||||
let mut url = validate_script_url(tunnel_url)?;
|
||||
url.query_pairs_mut().append_pair("dbx_action", action).append_pair("dbx_session", session);
|
||||
if let Some((host, port)) = target {
|
||||
url.query_pairs_mut().append_pair("dbx_target_host", host).append_pair("dbx_target_port", &port.to_string());
|
||||
}
|
||||
if let Some(connect_timeout_secs) = connect_timeout_secs {
|
||||
url.query_pairs_mut()
|
||||
.append_pair("dbx_connect_timeout", &effective_connect_timeout_secs(connect_timeout_secs).to_string());
|
||||
}
|
||||
if let Some(wait_ms) = wait_ms {
|
||||
url.query_pairs_mut().append_pair("dbx_wait_ms", &wait_ms.to_string());
|
||||
}
|
||||
Ok(url)
|
||||
}
|
||||
|
||||
fn validate_script_url(tunnel_url: &str) -> Result<Url, String> {
|
||||
let trimmed = tunnel_url.trim();
|
||||
if trimmed.is_empty() {
|
||||
return Err("HTTP tunnel script URL is required".to_string());
|
||||
}
|
||||
let url = Url::parse(trimmed).map_err(|e| format!("HTTP tunnel script URL is invalid: {e}"))?;
|
||||
match url.scheme() {
|
||||
"http" | "https" => Ok(url),
|
||||
other => Err(format!("Unsupported HTTP tunnel script URL scheme: {other}")),
|
||||
}
|
||||
}
|
||||
|
||||
fn effective_connect_timeout(value: u64) -> Duration {
|
||||
Duration::from_secs(effective_connect_timeout_secs(value))
|
||||
}
|
||||
|
||||
fn effective_connect_timeout_secs(value: u64) -> u64 {
|
||||
if value == 0 {
|
||||
DEFAULT_CONNECT_TIMEOUT.as_secs()
|
||||
} else {
|
||||
value.clamp(1, 300)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{script_url, validate_script_url, HttpTunnelManager};
|
||||
use tokio::io::{AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader};
|
||||
use tokio::net::{TcpListener, TcpStream};
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
#[test]
|
||||
fn script_url_preserves_existing_query_and_appends_action() {
|
||||
let url = script_url(
|
||||
"https://gateway.example.com/dbx_tunnel.php?keep=1",
|
||||
"open",
|
||||
"session-1",
|
||||
Some(("mysql.internal", 3306)),
|
||||
Some(10),
|
||||
None,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(
|
||||
url.as_str(),
|
||||
"https://gateway.example.com/dbx_tunnel.php?keep=1&dbx_action=open&dbx_session=session-1&dbx_target_host=mysql.internal&dbx_target_port=3306&dbx_connect_timeout=10"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn script_url_rejects_non_http_schemes() {
|
||||
let err = validate_script_url("file:///tmp/tunnel.php").unwrap_err();
|
||||
|
||||
assert!(err.contains("Unsupported HTTP tunnel script URL scheme"));
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn manager_forwards_bytes_through_http_script_protocol() {
|
||||
let script = MockScript::start().await;
|
||||
let manager = HttpTunnelManager::new();
|
||||
let local_port = manager.start_tunnel("test", &script.url, "secret", 5, "mysql.internal", 3306).await.unwrap();
|
||||
let mut client = TcpStream::connect(("127.0.0.1", local_port)).await.unwrap();
|
||||
client.write_all(b"ping").await.unwrap();
|
||||
let mut response = [0_u8; 4];
|
||||
client.read_exact(&mut response).await.unwrap();
|
||||
|
||||
assert_eq!(&response, b"ping");
|
||||
|
||||
manager.stop_tunnel("test").await;
|
||||
script.handle.abort();
|
||||
}
|
||||
|
||||
struct MockScript {
|
||||
url: String,
|
||||
handle: tokio::task::JoinHandle<()>,
|
||||
}
|
||||
|
||||
impl MockScript {
|
||||
async fn start() -> Self {
|
||||
let listener = TcpListener::bind(("127.0.0.1", 0)).await.unwrap();
|
||||
let addr = listener.local_addr().unwrap();
|
||||
let buffered = std::sync::Arc::new(Mutex::new(Vec::<u8>::new()));
|
||||
let handle = {
|
||||
let buffered = buffered.clone();
|
||||
tokio::spawn(async move {
|
||||
loop {
|
||||
let Ok((stream, _)) = listener.accept().await else {
|
||||
break;
|
||||
};
|
||||
let buffered = buffered.clone();
|
||||
tokio::spawn(async move {
|
||||
handle_mock_http_request(stream, buffered).await;
|
||||
});
|
||||
}
|
||||
})
|
||||
};
|
||||
Self { url: format!("http://{addr}/dbx_tunnel.php"), handle }
|
||||
}
|
||||
}
|
||||
|
||||
async fn handle_mock_http_request(stream: TcpStream, buffered: std::sync::Arc<Mutex<Vec<u8>>>) {
|
||||
let mut reader = BufReader::new(stream);
|
||||
let mut request_line = String::new();
|
||||
reader.read_line(&mut request_line).await.unwrap();
|
||||
let mut content_length = 0_usize;
|
||||
loop {
|
||||
let mut line = String::new();
|
||||
reader.read_line(&mut line).await.unwrap();
|
||||
let trimmed = line.trim_end();
|
||||
if trimmed.is_empty() {
|
||||
break;
|
||||
}
|
||||
let lower = trimmed.to_ascii_lowercase();
|
||||
if let Some(value) = lower.strip_prefix("content-length:") {
|
||||
content_length = value.trim().parse().unwrap();
|
||||
}
|
||||
}
|
||||
let mut body = vec![0_u8; content_length];
|
||||
reader.read_exact(&mut body).await.unwrap();
|
||||
let mut stream = reader.into_inner();
|
||||
if request_line.contains("dbx_action=write") {
|
||||
buffered.lock().await.extend_from_slice(&body);
|
||||
write_http_response(&mut stream, "200 OK", b"OK").await;
|
||||
} else if request_line.contains("dbx_action=read") {
|
||||
let bytes = {
|
||||
let mut buffered = buffered.lock().await;
|
||||
std::mem::take(&mut *buffered)
|
||||
};
|
||||
if bytes.is_empty() {
|
||||
write_http_response(&mut stream, "204 No Content", b"").await;
|
||||
} else {
|
||||
write_http_response(&mut stream, "200 OK", &bytes).await;
|
||||
}
|
||||
} else {
|
||||
write_http_response(&mut stream, "200 OK", b"OK").await;
|
||||
}
|
||||
}
|
||||
|
||||
async fn write_http_response(stream: &mut TcpStream, status: &str, body: &[u8]) {
|
||||
let header = format!("HTTP/1.1 {status}\r\nContent-Length: {}\r\nConnection: close\r\n\r\n", body.len());
|
||||
stream.write_all(header.as_bytes()).await.unwrap();
|
||||
stream.write_all(body).await.unwrap();
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ pub mod duckdb_driver;
|
||||
pub mod elasticsearch_driver;
|
||||
pub mod elasticsearch_sql;
|
||||
pub mod file_validator;
|
||||
pub mod http_tunnel;
|
||||
pub mod influxdb_driver;
|
||||
pub mod manticoresearch;
|
||||
pub mod mongo_driver;
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
use crate::models::connection::TransportLayerConfig;
|
||||
|
||||
use super::http_tunnel::HttpTunnelManager;
|
||||
use super::proxy_tunnel::ProxyTunnelManager;
|
||||
use super::ssh_tunnel::TunnelManager;
|
||||
|
||||
@@ -26,10 +27,12 @@ pub async fn start_transport_layers(
|
||||
remote_port: u16,
|
||||
ssh_tunnels: &TunnelManager,
|
||||
proxy_tunnels: &ProxyTunnelManager,
|
||||
http_tunnels: &HttpTunnelManager,
|
||||
) -> Result<u16, String> {
|
||||
if layers.is_empty() {
|
||||
return Err("No transport layers configured".to_string());
|
||||
}
|
||||
validate_transport_layers(layers)?;
|
||||
|
||||
let mut next_connect_endpoint: Option<LayerEndpoint> = None;
|
||||
let mut final_local_port = 0;
|
||||
@@ -80,6 +83,17 @@ pub async fn start_transport_layers(
|
||||
)
|
||||
.await
|
||||
.map_err(|err| format!("Proxy layer {} failed: {err}", index + 1))?,
|
||||
TransportLayerConfig::HttpTunnel(http) => http_tunnels
|
||||
.start_tunnel(
|
||||
&layer_id,
|
||||
&http.url,
|
||||
&http.token,
|
||||
http.connect_timeout_secs,
|
||||
&target_endpoint.host,
|
||||
target_endpoint.port,
|
||||
)
|
||||
.await
|
||||
.map_err(|err| format!("HTTP tunnel layer {} failed: {err}", index + 1))?,
|
||||
};
|
||||
|
||||
final_local_port = local_port;
|
||||
@@ -94,14 +108,25 @@ pub async fn stop_transport_layers(
|
||||
layer_count: usize,
|
||||
ssh_tunnels: &TunnelManager,
|
||||
proxy_tunnels: &ProxyTunnelManager,
|
||||
http_tunnels: &HttpTunnelManager,
|
||||
) {
|
||||
for index in 0..layer_count {
|
||||
let layer_id = layer_id(connection_id, index);
|
||||
ssh_tunnels.stop_tunnel(&layer_id).await;
|
||||
proxy_tunnels.stop_tunnel(&layer_id).await;
|
||||
http_tunnels.stop_tunnel(&layer_id).await;
|
||||
}
|
||||
}
|
||||
|
||||
fn validate_transport_layers(layers: &[TransportLayerConfig]) -> Result<(), String> {
|
||||
for (index, layer) in layers.iter().enumerate() {
|
||||
if matches!(layer, TransportLayerConfig::HttpTunnel(_)) && index != 0 {
|
||||
return Err("HTTP tunnel must be the first transport layer".to_string());
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn layer_id(connection_id: &str, index: usize) -> String {
|
||||
format!("{connection_id}:transport:{index}")
|
||||
}
|
||||
@@ -119,6 +144,7 @@ fn effective_ssh_connect_timeout_secs(value: u64) -> u64 {
|
||||
enum PlannedLayerType {
|
||||
Ssh,
|
||||
Proxy,
|
||||
HttpTunnel,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -154,6 +180,7 @@ fn plan_transport_layers(
|
||||
let layer_type = match layer {
|
||||
TransportLayerConfig::Ssh(_) => PlannedLayerType::Ssh,
|
||||
TransportLayerConfig::Proxy(_) => PlannedLayerType::Proxy,
|
||||
TransportLayerConfig::HttpTunnel(_) => PlannedLayerType::HttpTunnel,
|
||||
};
|
||||
planned.push(PlannedTransportLayer {
|
||||
layer_type,
|
||||
@@ -171,8 +198,10 @@ fn plan_transport_layers(
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{plan_transport_layers, PlannedLayerType, PlannedTransportLayer};
|
||||
use crate::models::connection::{ProxyTunnelConfig, ProxyType, SshTunnelConfig, TransportLayerConfig};
|
||||
use super::{plan_transport_layers, validate_transport_layers, PlannedLayerType, PlannedTransportLayer};
|
||||
use crate::models::connection::{
|
||||
HttpTunnelConfig, ProxyTunnelConfig, ProxyType, SshTunnelConfig, TransportLayerConfig,
|
||||
};
|
||||
|
||||
fn ssh_layer(id: &str, host: &str, port: u16) -> TransportLayerConfig {
|
||||
TransportLayerConfig::Ssh(SshTunnelConfig {
|
||||
@@ -205,6 +234,17 @@ mod tests {
|
||||
})
|
||||
}
|
||||
|
||||
fn http_tunnel_layer(id: &str, url: &str) -> TransportLayerConfig {
|
||||
TransportLayerConfig::HttpTunnel(HttpTunnelConfig {
|
||||
id: id.to_string(),
|
||||
name: String::new(),
|
||||
enabled: true,
|
||||
url: url.to_string(),
|
||||
token: String::new(),
|
||||
connect_timeout_secs: 10,
|
||||
})
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn mixed_transport_plan_routes_layers_in_configured_order() {
|
||||
let layers = vec![
|
||||
@@ -242,4 +282,46 @@ mod tests {
|
||||
]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn http_tunnel_must_be_outermost_layer() {
|
||||
let layers = vec![
|
||||
ssh_layer("ssh-a", "bastion-a", 22),
|
||||
http_tunnel_layer("http", "https://dbx.example.com/dbx_tunnel.php"),
|
||||
];
|
||||
|
||||
let err = validate_transport_layers(&layers).unwrap_err();
|
||||
|
||||
assert!(err.contains("HTTP tunnel must be the first transport layer"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn http_tunnel_first_layer_targets_next_layer() {
|
||||
let layers = vec![
|
||||
http_tunnel_layer("http", "https://dbx.example.com/dbx_tunnel.php"),
|
||||
ssh_layer("ssh-a", "bastion-a", 22),
|
||||
];
|
||||
|
||||
let planned = plan_transport_layers(&layers, "db.internal", 5432, &[41001, 41002]);
|
||||
|
||||
assert_eq!(
|
||||
planned,
|
||||
vec![
|
||||
PlannedTransportLayer {
|
||||
layer_type: PlannedLayerType::HttpTunnel,
|
||||
connect_host: "".to_string(),
|
||||
connect_port: 0,
|
||||
remote_host: "bastion-a".to_string(),
|
||||
remote_port: 22,
|
||||
},
|
||||
PlannedTransportLayer {
|
||||
layer_type: PlannedLayerType::Ssh,
|
||||
connect_host: "127.0.0.1".to_string(),
|
||||
connect_port: 41001,
|
||||
remote_host: "db.internal".to_string(),
|
||||
remote_port: 5432,
|
||||
},
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -95,6 +95,8 @@ pub struct ConnectionConfig {
|
||||
pub enum TransportLayerConfig {
|
||||
Ssh(SshTunnelConfig),
|
||||
Proxy(ProxyTunnelConfig),
|
||||
#[serde(rename = "http_tunnel")]
|
||||
HttpTunnel(HttpTunnelConfig),
|
||||
}
|
||||
|
||||
impl TransportLayerConfig {
|
||||
@@ -102,6 +104,7 @@ impl TransportLayerConfig {
|
||||
match self {
|
||||
TransportLayerConfig::Ssh(layer) => &layer.id,
|
||||
TransportLayerConfig::Proxy(layer) => &layer.id,
|
||||
TransportLayerConfig::HttpTunnel(layer) => &layer.id,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -109,6 +112,7 @@ impl TransportLayerConfig {
|
||||
match self {
|
||||
TransportLayerConfig::Ssh(layer) => &layer.name,
|
||||
TransportLayerConfig::Proxy(layer) => &layer.name,
|
||||
TransportLayerConfig::HttpTunnel(layer) => &layer.name,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -116,6 +120,7 @@ impl TransportLayerConfig {
|
||||
match self {
|
||||
TransportLayerConfig::Ssh(layer) => layer.enabled,
|
||||
TransportLayerConfig::Proxy(layer) => layer.enabled,
|
||||
TransportLayerConfig::HttpTunnel(layer) => layer.enabled,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -123,6 +128,9 @@ impl TransportLayerConfig {
|
||||
match self {
|
||||
TransportLayerConfig::Ssh(layer) => (&layer.host, layer.port),
|
||||
TransportLayerConfig::Proxy(layer) => (&layer.host, layer.port),
|
||||
// HTTP script tunnel layers dial a PHP script URL instead of a host:port
|
||||
// endpoint, and are validated as the outermost transport layer.
|
||||
TransportLayerConfig::HttpTunnel(_) => ("", 0),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -180,6 +188,22 @@ pub struct ProxyTunnelConfig {
|
||||
pub password: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
||||
pub struct HttpTunnelConfig {
|
||||
#[serde(default)]
|
||||
pub id: String,
|
||||
#[serde(default)]
|
||||
pub name: String,
|
||||
#[serde(default = "default_true")]
|
||||
pub enabled: bool,
|
||||
#[serde(default)]
|
||||
pub url: String,
|
||||
#[serde(default)]
|
||||
pub token: String,
|
||||
#[serde(default = "default_http_tunnel_connect_timeout_secs")]
|
||||
pub connect_timeout_secs: u64,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
|
||||
pub struct AttachedDatabaseConfig {
|
||||
pub name: String,
|
||||
@@ -198,6 +222,10 @@ pub fn default_ssh_connect_timeout_secs() -> u64 {
|
||||
5
|
||||
}
|
||||
|
||||
pub fn default_http_tunnel_connect_timeout_secs() -> u64 {
|
||||
10
|
||||
}
|
||||
|
||||
pub fn default_connect_timeout_secs() -> u64 {
|
||||
10
|
||||
}
|
||||
@@ -592,7 +620,7 @@ impl ConnectionConfig {
|
||||
.into_iter()
|
||||
.filter_map(|layer| match layer {
|
||||
TransportLayerConfig::Ssh(ssh) => Some(ssh),
|
||||
TransportLayerConfig::Proxy(_) => None,
|
||||
TransportLayerConfig::Proxy(_) | TransportLayerConfig::HttpTunnel(_) => None,
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
@@ -1920,7 +1948,7 @@ mod tests {
|
||||
assert_eq!(proxy.username, "alice");
|
||||
assert_eq!(proxy.password, "secret");
|
||||
}
|
||||
TransportLayerConfig::Ssh(_) => panic!("expected proxy layer"),
|
||||
_ => panic!("expected proxy layer"),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -458,6 +458,10 @@ fn transport_layer_proxy_password_key(index: usize, layer: &TransportLayerConfig
|
||||
format!("{}{}.proxy_password", TRANSPORT_LAYER_SECRET_PREFIX, transport_layer_secret_segment(index, layer))
|
||||
}
|
||||
|
||||
fn transport_layer_http_tunnel_token_key(index: usize, layer: &TransportLayerConfig) -> String {
|
||||
format!("{}{}.http_tunnel_token", TRANSPORT_LAYER_SECRET_PREFIX, transport_layer_secret_segment(index, layer))
|
||||
}
|
||||
|
||||
fn scrub_transport_layer_secrets(config: &mut ConnectionConfig) {
|
||||
for layer in &mut config.transport_layers {
|
||||
match layer {
|
||||
@@ -468,6 +472,9 @@ fn scrub_transport_layer_secrets(config: &mut ConnectionConfig) {
|
||||
TransportLayerConfig::Proxy(proxy) => {
|
||||
proxy.password.clear();
|
||||
}
|
||||
TransportLayerConfig::HttpTunnel(http) => {
|
||||
http.token.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1091,6 +1098,14 @@ impl Storage {
|
||||
&proxy.password,
|
||||
)?;
|
||||
}
|
||||
TransportLayerConfig::HttpTunnel(http) => {
|
||||
persist_secret_in_tx(
|
||||
&tx,
|
||||
&config.id,
|
||||
&transport_layer_http_tunnel_token_key(index, layer),
|
||||
&http.token,
|
||||
)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
persist_secret_in_tx(&tx, &config.id, "redis_sentinel_password", &config.redis_sentinel_password)?;
|
||||
@@ -1154,7 +1169,7 @@ impl Storage {
|
||||
TransportLayerConfig::Ssh(layer) => {
|
||||
self.get_secret(&id, &ssh_tunnel_password_key(index, layer)).await?
|
||||
}
|
||||
TransportLayerConfig::Proxy(_) => None,
|
||||
TransportLayerConfig::Proxy(_) | TransportLayerConfig::HttpTunnel(_) => None,
|
||||
})
|
||||
.unwrap_or_default();
|
||||
ssh.key_passphrase = self
|
||||
@@ -1167,7 +1182,7 @@ impl Storage {
|
||||
TransportLayerConfig::Ssh(layer) => {
|
||||
self.get_secret(&id, &ssh_tunnel_key_passphrase_key(index, layer)).await?
|
||||
}
|
||||
TransportLayerConfig::Proxy(_) => None,
|
||||
TransportLayerConfig::Proxy(_) | TransportLayerConfig::HttpTunnel(_) => None,
|
||||
})
|
||||
.unwrap_or_default();
|
||||
}
|
||||
@@ -1183,6 +1198,12 @@ impl Storage {
|
||||
})
|
||||
.unwrap_or_default();
|
||||
}
|
||||
TransportLayerConfig::HttpTunnel(http) => {
|
||||
http.token = self
|
||||
.get_secret(&id, &transport_layer_http_tunnel_token_key(index, &layer_for_key))
|
||||
.await?
|
||||
.unwrap_or_default();
|
||||
}
|
||||
}
|
||||
}
|
||||
config.redis_sentinel_password = self.get_secret(&id, "redis_sentinel_password").await?.unwrap_or_default();
|
||||
|
||||
Reference in new issue
Block a user