fix(clickhouse): preserve TLS hostname through proxy routes
This commit is contained in:
1 parent
11ef109e5c
commit
0b1d3fa5c0
3 files changed
+567
-16
No files matched your search
@@ -2702,7 +2702,7 @@ impl AppState {
|
||||
if let Some(database) = self.resolve_legacy_postgres_like_database(connection_id, &db_config).await {
|
||||
db_config.database = Some(database);
|
||||
}
|
||||
if db_config.db_type != DatabaseType::Plugin {
|
||||
if db_config.db_type != DatabaseType::Plugin && runtime_proxy.is_none() {
|
||||
probe_connection_endpoint(&db_config, &host, port).await?;
|
||||
}
|
||||
if let Err(err) = self.ensure_current_connection_attempt(connection_id, connection_attempt).await {
|
||||
@@ -2949,12 +2949,13 @@ impl AppState {
|
||||
DatabaseType::ClickHouse => {
|
||||
let username = if db_config.username.is_empty() { None } else { Some(db_config.username.clone()) };
|
||||
let password = if db_config.password.is_empty() { None } else { Some(db_config.password.clone()) };
|
||||
let client = db::clickhouse_driver::ChClient::new_with_ca_cert(
|
||||
let client = db::clickhouse_driver::ChClient::new_with_ca_cert_and_proxy(
|
||||
&url,
|
||||
username,
|
||||
password,
|
||||
Some(&db_config.ca_cert_path),
|
||||
db_config.url_params.as_deref(),
|
||||
clickhouse_http_proxy(runtime_proxy.as_ref())?,
|
||||
connect_timeout,
|
||||
)?;
|
||||
db::clickhouse_driver::test_connection(&client, connect_timeout).await?;
|
||||
@@ -3578,6 +3579,11 @@ impl AppState {
|
||||
if transport_layers.is_empty() || db::sqlite_worker::sqlite_remote_worker_requested(config) {
|
||||
return Ok(ConnectionEndpoint::direct(config.host.clone(), config.port));
|
||||
}
|
||||
if config.db_type == DatabaseType::ClickHouse {
|
||||
if let Some(proxy) = self.clickhouse_proxy_for_transport_layers(connection_id, &transport_layers).await? {
|
||||
return Ok(ConnectionEndpoint { host: config.host.clone(), port: config.port, proxy: Some(proxy) });
|
||||
}
|
||||
}
|
||||
if config.uses_oracle_tns() {
|
||||
// A TNS descriptor may contain several failover addresses, so rewriting it
|
||||
// through one local tunnel endpoint would silently break Oracle Net routing.
|
||||
@@ -3632,6 +3638,43 @@ impl AppState {
|
||||
Ok(ConnectionEndpoint { host: "127.0.0.1".to_string(), port: local_port, proxy: None })
|
||||
}
|
||||
|
||||
async fn clickhouse_proxy_for_transport_layers(
|
||||
&self,
|
||||
connection_id: &str,
|
||||
transport_layers: &[TransportLayerConfig],
|
||||
) -> Result<Option<PluginRuntimeProxy>, String> {
|
||||
use crate::models::connection::ProxyType;
|
||||
|
||||
let Some(TransportLayerConfig::Proxy(proxy)) = transport_layers.last() else {
|
||||
return self.socks5_route_for_transport_layers(connection_id, transport_layers).await;
|
||||
};
|
||||
if proxy.proxy_type == ProxyType::Socks5 {
|
||||
return self.socks5_route_for_transport_layers(connection_id, transport_layers).await;
|
||||
}
|
||||
let (host, port) = if transport_layers.len() == 1 {
|
||||
(proxy.host.clone(), proxy.port)
|
||||
} else {
|
||||
let local_port = db::transport_layer_tunnel::start_transport_layers(
|
||||
connection_id,
|
||||
&transport_layers[..transport_layers.len() - 1],
|
||||
&proxy.host,
|
||||
proxy.port,
|
||||
&self.tunnels,
|
||||
&self.proxy_tunnels,
|
||||
&self.http_tunnels,
|
||||
)
|
||||
.await?;
|
||||
("127.0.0.1".to_string(), local_port)
|
||||
};
|
||||
Ok(Some(PluginRuntimeProxy {
|
||||
proxy_type: "http".to_string(),
|
||||
host,
|
||||
port,
|
||||
username: proxy.username.clone(),
|
||||
password: proxy.password.clone(),
|
||||
}))
|
||||
}
|
||||
|
||||
/// Builds the host-managed SOCKS5 route from the transport chain for
|
||||
/// plugin providers declaring `proxy_route` (mirrors the
|
||||
/// RocketMQ proxy path). `None` = fall back to the static tunnel path.
|
||||
@@ -6978,6 +7021,28 @@ pub fn connection_url_for_endpoint(config: &ConnectionConfig, host: &str, port:
|
||||
}
|
||||
}
|
||||
|
||||
pub fn clickhouse_http_proxy(proxy: Option<&PluginRuntimeProxy>) -> Result<Option<reqwest::Proxy>, String> {
|
||||
let Some(proxy) = proxy else {
|
||||
return Ok(None);
|
||||
};
|
||||
let scheme = match proxy.proxy_type.as_str() {
|
||||
"socks5" => "socks5h",
|
||||
"http" => "http",
|
||||
_ => return Err("Unsupported ClickHouse proxy type".to_string()),
|
||||
};
|
||||
let host = if proxy.host.contains(':') && !proxy.host.starts_with('[') {
|
||||
format!("[{}]", proxy.host)
|
||||
} else {
|
||||
proxy.host.clone()
|
||||
};
|
||||
let mut http_proxy = reqwest::Proxy::all(format!("{scheme}://{host}:{}", proxy.port))
|
||||
.map_err(|error| format!("Invalid ClickHouse proxy address: {error}"))?;
|
||||
if !proxy.username.is_empty() || !proxy.password.is_empty() {
|
||||
http_proxy = http_proxy.basic_auth(&proxy.username, &proxy.password);
|
||||
}
|
||||
Ok(Some(http_proxy))
|
||||
}
|
||||
|
||||
pub fn redacted_connection_url_for_endpoint(config: &ConnectionConfig, host: &str, port: u16) -> String {
|
||||
let normalized = native_postgres_url_config(config);
|
||||
let config = normalized.as_ref().unwrap_or(config);
|
||||
@@ -7169,9 +7234,9 @@ async fn detect_ob_oracle_mode(config: &ConnectionConfig, pool: &db::mysql::MySq
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{
|
||||
agent_connect_timeout, connection_configs_pool_equivalent, connection_configs_session_credentials_compatible,
|
||||
connection_probe_endpoints, connection_remote_endpoint, connection_url_for_endpoint,
|
||||
database_connection_config, database_connection_config_with_catalog,
|
||||
agent_connect_timeout, clickhouse_http_proxy, connection_configs_pool_equivalent,
|
||||
connection_configs_session_credentials_compatible, connection_probe_endpoints, connection_remote_endpoint,
|
||||
connection_url_for_endpoint, database_connection_config, database_connection_config_with_catalog,
|
||||
gaussdb_identifier_quote_from_query_result, gaussdb_m_jdbc_config_for_endpoint, gaussdb_uses_m_jdbc_driver,
|
||||
kafka_single_loopback_bootstrap_endpoint, keepalive_failure_proves_pool_dead, metadata_connection_config,
|
||||
metadata_pool_database, mysql_metadata_fallback_url, mysql_pool_setup_queries, oceanbase_mysql_setup_queries,
|
||||
@@ -7194,6 +7259,7 @@ mod tests {
|
||||
ConnectionLivenessFailureKind, ConnectionLivenessMessage, DatabaseType, HttpTunnelConfig, ProxyTunnelConfig,
|
||||
ProxyType, SshTunnelConfig, TransportLayerConfig,
|
||||
};
|
||||
use crate::plugins::PluginRuntimeProxy;
|
||||
use crate::query;
|
||||
use crate::schema;
|
||||
use std::sync::Arc;
|
||||
@@ -11289,6 +11355,165 @@ for line in sys.stdin:
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn clickhouse_proxy_endpoint_preserves_https_authority() {
|
||||
let (state, dir) = test_app_state().await;
|
||||
let mut config = mysql_config(None);
|
||||
config.db_type = DatabaseType::ClickHouse;
|
||||
config.host = "clickhouse.proxy.test".to_string();
|
||||
config.port = 8443;
|
||||
config.ssl = true;
|
||||
|
||||
for (proxy_type, expected_type) in [(ProxyType::Socks5, "socks5"), (ProxyType::Http, "http")] {
|
||||
config.transport_layers = vec![TransportLayerConfig::Proxy(ProxyTunnelConfig {
|
||||
id: "proxy".to_string(),
|
||||
name: String::new(),
|
||||
enabled: true,
|
||||
proxy_type,
|
||||
host: "127.0.0.1".to_string(),
|
||||
port: 65000,
|
||||
username: "proxy:user".to_string(),
|
||||
password: "p@ss/word#".to_string(),
|
||||
test_target: None,
|
||||
profile_id: String::new(),
|
||||
})];
|
||||
|
||||
let endpoint = state.connection_endpoint("proxied-clickhouse", &config).await.unwrap();
|
||||
assert_eq!(endpoint.host, config.host);
|
||||
assert_eq!(endpoint.port, config.port);
|
||||
assert_eq!(
|
||||
connection_url_for_endpoint(&config, &endpoint.host, endpoint.port),
|
||||
"https://clickhouse.proxy.test:8443"
|
||||
);
|
||||
let proxy = endpoint.proxy.unwrap();
|
||||
assert_eq!(proxy.proxy_type, expected_type);
|
||||
assert_eq!(proxy.host, "127.0.0.1");
|
||||
assert_eq!(proxy.port, 65000);
|
||||
assert_eq!(proxy.username, "proxy:user");
|
||||
assert_eq!(proxy.password, "p@ss/word#");
|
||||
assert!(clickhouse_http_proxy(Some(&proxy)).unwrap().is_some());
|
||||
}
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn clickhouse_http_proxy_accepts_ipv6_and_rejects_unknown_protocols() {
|
||||
for host in ["::1", "[::1]"] {
|
||||
let proxy = PluginRuntimeProxy::socks5(host.to_string(), 1080, String::new(), String::new());
|
||||
assert!(clickhouse_http_proxy(Some(&proxy)).unwrap().is_some());
|
||||
}
|
||||
assert!(clickhouse_http_proxy(None).unwrap().is_none());
|
||||
let proxy = PluginRuntimeProxy {
|
||||
proxy_type: "unsupported".to_string(),
|
||||
host: "localhost".to_string(),
|
||||
port: 1080,
|
||||
username: String::new(),
|
||||
password: String::new(),
|
||||
};
|
||||
assert!(clickhouse_http_proxy(Some(&proxy)).is_err());
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn clickhouse_proxy_chain_preserves_final_proxy_and_database_authority() {
|
||||
let (state, dir) = test_app_state().await;
|
||||
let mut config = mysql_config(None);
|
||||
config.db_type = DatabaseType::ClickHouse;
|
||||
config.host = "clickhouse.proxy.test".to_string();
|
||||
config.port = 8443;
|
||||
config.ssl = true;
|
||||
for (proxy_type, expected_type) in [(ProxyType::Socks5, "socks5"), (ProxyType::Http, "http")] {
|
||||
let mut first = proxy_layer("first", "");
|
||||
first.host = "127.0.0.1".to_string();
|
||||
first.port = 65001;
|
||||
let mut last = proxy_layer("last", "");
|
||||
last.proxy_type = proxy_type;
|
||||
last.host = "last.proxy.test".to_string();
|
||||
last.port = 65000;
|
||||
last.username = "final-user".to_string();
|
||||
last.password = "final-password".to_string();
|
||||
config.transport_layers = vec![TransportLayerConfig::Proxy(first), TransportLayerConfig::Proxy(last)];
|
||||
let endpoint = state.connection_endpoint("clickhouse-chain", &config).await.unwrap();
|
||||
assert_eq!(endpoint.host, config.host);
|
||||
assert_eq!(endpoint.port, config.port);
|
||||
let proxy = endpoint.proxy.unwrap();
|
||||
assert_eq!(proxy.proxy_type, expected_type);
|
||||
assert_eq!(proxy.host, "127.0.0.1");
|
||||
assert_ne!(proxy.port, 0);
|
||||
assert_ne!(proxy.port, 65000);
|
||||
assert_eq!(proxy.username, "final-user");
|
||||
assert_eq!(proxy.password, "final-password");
|
||||
state.proxy_tunnels.stop_tunnel("clickhouse-chain:transport:0").await;
|
||||
}
|
||||
state.shutdown(Duration::from_secs(5)).await;
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn clickhouse_proxy_pool_does_not_require_direct_dns_or_tcp_access() {
|
||||
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
||||
|
||||
let (state, dir) = test_app_state().await;
|
||||
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
||||
let proxy_port = listener.local_addr().unwrap().port();
|
||||
let server = tokio::spawn(async move {
|
||||
let (mut socket, _) = listener.accept().await.unwrap();
|
||||
let mut greeting = [0_u8; 2];
|
||||
socket.read_exact(&mut greeting).await.unwrap();
|
||||
assert_eq!(greeting[0], 5);
|
||||
let mut methods = vec![0_u8; greeting[1] as usize];
|
||||
socket.read_exact(&mut methods).await.unwrap();
|
||||
assert!(methods.contains(&0));
|
||||
socket.write_all(&[5, 0]).await.unwrap();
|
||||
let mut connect = [0_u8; 5];
|
||||
socket.read_exact(&mut connect).await.unwrap();
|
||||
assert_eq!(&connect[..4], &[5, 1, 0, 3]);
|
||||
let mut hostname = vec![0_u8; connect[4] as usize];
|
||||
socket.read_exact(&mut hostname).await.unwrap();
|
||||
assert_eq!(hostname.as_slice(), b"clickhouse.proxy.invalid");
|
||||
let mut port = [0_u8; 2];
|
||||
socket.read_exact(&mut port).await.unwrap();
|
||||
assert_eq!(u16::from_be_bytes(port), 8123);
|
||||
socket.write_all(&[5, 0, 0, 1, 127, 0, 0, 1, 0, 0]).await.unwrap();
|
||||
let mut request = Vec::new();
|
||||
while !request.ends_with(b"\r\n\r\n") {
|
||||
let mut byte = [0_u8; 1];
|
||||
socket.read_exact(&mut byte).await.unwrap();
|
||||
request.extend_from_slice(&byte);
|
||||
}
|
||||
let request = String::from_utf8(request).unwrap();
|
||||
assert!(request.starts_with("GET /?query=SELECT%201 "));
|
||||
assert!(request.lines().any(|line| line.eq_ignore_ascii_case("host: clickhouse.proxy.invalid:8123")));
|
||||
socket.write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 2\r\nConnection: close\r\n\r\n1\n").await.unwrap();
|
||||
});
|
||||
let mut config = mysql_config(None);
|
||||
config.id = "clickhouse-proxy-pool".to_string();
|
||||
config.db_type = DatabaseType::ClickHouse;
|
||||
config.host = "clickhouse.proxy.invalid".to_string();
|
||||
config.port = 8123;
|
||||
config.ssl = false;
|
||||
config.transport_layers = vec![TransportLayerConfig::Proxy(ProxyTunnelConfig {
|
||||
id: "proxy".to_string(),
|
||||
name: String::new(),
|
||||
enabled: true,
|
||||
proxy_type: ProxyType::Socks5,
|
||||
host: "127.0.0.1".to_string(),
|
||||
port: proxy_port,
|
||||
username: String::new(),
|
||||
password: String::new(),
|
||||
test_target: None,
|
||||
profile_id: String::new(),
|
||||
})];
|
||||
state.configs.write().await.insert(config.id.clone(), config);
|
||||
let result =
|
||||
tokio::time::timeout(Duration::from_secs(10), state.get_or_create_pool("clickhouse-proxy-pool", None))
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(result.is_ok(), "{result:?}");
|
||||
tokio::time::timeout(Duration::from_secs(10), server).await.unwrap().unwrap();
|
||||
state.shutdown(Duration::from_secs(5)).await;
|
||||
let _ = std::fs::remove_dir_all(dir);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn proxy_connection_uses_local_forward_endpoint() {
|
||||
let (state, dir) = test_app_state().await;
|
||||
|
||||
Reference in new issue
Block a user