fix(clickhouse): preserve TLS hostname through proxy routes

This commit is contained in:
CHEN JIANG authored and GitHub committed 2026-10-03 14:14:11 +08:00
1 parent 11ef109e5c
commit 0b1d3fa5c0
3 files changed
+567 -16

No files matched your search

+230 -5
View File
@@ -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;
+203 -2
View File
@@ -33,8 +33,23 @@ impl ChClient {
ca_cert_path: Option<&str>,
extra_params: Option<&str>,
timeout: Duration,
) -> Result<Self, String> {
Self::new_with_ca_cert_and_proxy(url, username, password, ca_cert_path, extra_params, None, timeout)
}
pub fn new_with_ca_cert_and_proxy(
url: &str,
username: Option<String>,
password: Option<String>,
ca_cert_path: Option<&str>,
extra_params: Option<&str>,
proxy: Option<reqwest::Proxy>,
timeout: Duration,
) -> Result<Self, String> {
let mut builder = http_client_builder(timeout);
if let Some(proxy) = proxy {
builder = builder.proxy(proxy);
}
if let Some(path) = ca_cert_path.map(str::trim).filter(|path| !path.is_empty()) {
let path = expand_cert_path(path);
let cert_bytes =
@@ -863,7 +878,7 @@ pub async fn execute_query_with_max_rows(
mod tests {
use super::*;
async fn read_http_request(socket: &mut tokio::net::TcpStream) -> String {
async fn read_http_request<Stream: tokio::io::AsyncRead + Unpin>(socket: &mut Stream) -> String {
use tokio::io::AsyncReadExt;
let mut request = Vec::new();
@@ -892,7 +907,7 @@ mod tests {
String::from_utf8(request).unwrap()
}
async fn write_http_response(socket: &mut tokio::net::TcpStream, status: &str, body: &str) {
async fn write_http_response<Stream: tokio::io::AsyncWrite + Unpin>(socket: &mut Stream, status: &str, body: &str) {
use tokio::io::AsyncWriteExt;
let response = format!(
@@ -902,6 +917,192 @@ mod tests {
socket.write_all(response.as_bytes()).await.unwrap();
}
const PROXY_TLS_HOST: &str = "clickhouse.proxy.test";
fn proxy_test_tls_acceptor(certificate_host: &str) -> (openssl::ssl::SslAcceptor, tempfile::NamedTempFile) {
use openssl::asn1::Asn1Time;
use openssl::bn::BigNum;
use openssl::ec::{EcGroup, EcKey};
use openssl::hash::MessageDigest;
use openssl::nid::Nid;
use openssl::pkey::PKey;
use openssl::ssl::{NameType, SniError, SslAcceptor, SslMethod};
use openssl::x509::extension::{BasicConstraints, ExtendedKeyUsage, KeyUsage, SubjectAlternativeName};
use openssl::x509::{X509NameBuilder, X509};
use std::io::Write;
let group = EcGroup::from_curve_name(Nid::X9_62_PRIME256V1).unwrap();
let ca_key = PKey::from_ec_key(EcKey::generate(&group).unwrap()).unwrap();
let mut ca_name = X509NameBuilder::new().unwrap();
ca_name.append_entry_by_text("CN", "DBX proxy test CA").unwrap();
let ca_name = ca_name.build();
let mut ca = X509::builder().unwrap();
ca.set_version(2).unwrap();
ca.set_serial_number(&BigNum::from_u32(1).unwrap().to_asn1_integer().unwrap()).unwrap();
ca.set_subject_name(&ca_name).unwrap();
ca.set_issuer_name(&ca_name).unwrap();
ca.set_pubkey(&ca_key).unwrap();
ca.set_not_before(&Asn1Time::days_from_now(0).unwrap()).unwrap();
ca.set_not_after(&Asn1Time::days_from_now(1).unwrap()).unwrap();
ca.append_extension(BasicConstraints::new().critical().ca().build().unwrap()).unwrap();
ca.append_extension(KeyUsage::new().critical().key_cert_sign().crl_sign().build().unwrap()).unwrap();
ca.sign(&ca_key, MessageDigest::sha256()).unwrap();
let ca = ca.build();
let mut ca_file = tempfile::NamedTempFile::new().unwrap();
ca_file.write_all(&ca.to_pem().unwrap()).unwrap();
let server_key = PKey::from_ec_key(EcKey::generate(&group).unwrap()).unwrap();
let mut server_name = X509NameBuilder::new().unwrap();
server_name.append_entry_by_text("CN", certificate_host).unwrap();
let mut certificate = X509::builder().unwrap();
certificate.set_version(2).unwrap();
certificate.set_serial_number(&BigNum::from_u32(2).unwrap().to_asn1_integer().unwrap()).unwrap();
certificate.set_subject_name(&server_name.build()).unwrap();
certificate.set_issuer_name(ca.subject_name()).unwrap();
certificate.set_pubkey(&server_key).unwrap();
certificate.set_not_before(&Asn1Time::days_from_now(0).unwrap()).unwrap();
certificate.set_not_after(&Asn1Time::days_from_now(1).unwrap()).unwrap();
certificate.append_extension(BasicConstraints::new().critical().build().unwrap()).unwrap();
certificate.append_extension(KeyUsage::new().critical().digital_signature().build().unwrap()).unwrap();
certificate.append_extension(ExtendedKeyUsage::new().server_auth().build().unwrap()).unwrap();
let names = SubjectAlternativeName::new()
.dns(certificate_host)
.build(&certificate.x509v3_context(Some(&ca), None))
.unwrap();
certificate.append_extension(names).unwrap();
certificate.sign(&ca_key, MessageDigest::sha256()).unwrap();
let mut acceptor = SslAcceptor::mozilla_intermediate(SslMethod::tls_server()).unwrap();
acceptor.set_certificate(&certificate.build()).unwrap();
acceptor.set_private_key(&server_key).unwrap();
acceptor.set_servername_callback(|connection, _alert| {
if connection.servername(NameType::HOST_NAME) == Some(PROXY_TLS_HOST) {
Ok(())
} else {
Err(SniError::ALERT_FATAL)
}
});
(acceptor.build(), ca_file)
}
async fn check_https_proxy(
proxy_type: &'static str,
credentials: Option<(&'static str, &'static str)>,
target_port: u16,
certificate_host: &str,
) -> Result<(), String> {
use base64::Engine;
use openssl::ssl::{NameType, Ssl};
use std::pin::Pin;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
let (acceptor, ca_file) = proxy_test_tls_acceptor(certificate_host);
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let address = listener.local_addr().unwrap();
let server = tokio::spawn(async move {
let (mut socket, _) = listener.accept().await.unwrap();
if proxy_type == "socks5h" {
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();
let method = if credentials.is_some() { 2 } else { 0 };
assert!(methods.contains(&method));
socket.write_all(&[5, method]).await.unwrap();
if let Some((username, password)) = credentials {
let mut auth = [0_u8; 2];
socket.read_exact(&mut auth).await.unwrap();
assert_eq!(auth[0], 1);
let mut user_bytes = vec![0_u8; auth[1] as usize];
socket.read_exact(&mut user_bytes).await.unwrap();
assert_eq!(user_bytes, username.as_bytes());
let mut password_length = [0_u8; 1];
socket.read_exact(&mut password_length).await.unwrap();
let mut password_bytes = vec![0_u8; password_length[0] as usize];
socket.read_exact(&mut password_bytes).await.unwrap();
assert_eq!(password_bytes, password.as_bytes());
socket.write_all(&[1, 0]).await.unwrap();
}
let mut request = [0_u8; 5];
socket.read_exact(&mut request).await.unwrap();
assert_eq!(&request[..4], &[5, 1, 0, 3]);
let mut hostname = vec![0_u8; request[4] as usize];
socket.read_exact(&mut hostname).await.unwrap();
assert_eq!(hostname, PROXY_TLS_HOST.as_bytes());
let mut port_bytes = [0_u8; 2];
socket.read_exact(&mut port_bytes).await.unwrap();
assert_eq!(u16::from_be_bytes(port_bytes), target_port);
socket.write_all(&[5, 0, 0, 1, 127, 0, 0, 1, 0, 0]).await.unwrap();
} else {
let request = read_http_request(&mut socket).await;
assert!(request.starts_with(&format!("CONNECT {PROXY_TLS_HOST}:{target_port} HTTP/1.1\r\n")));
if let Some((username, password)) = credentials {
let expected = format!(
"Basic {}",
base64::engine::general_purpose::STANDARD.encode(format!("{username}:{password}"))
);
let authorization = request.lines().find_map(|line| {
let (name, value) = line.split_once(':')?;
name.eq_ignore_ascii_case("proxy-authorization").then(|| value.trim())
});
assert_eq!(authorization, Some(expected.as_str()));
}
socket.write_all(b"HTTP/1.1 200 Connection established\r\n\r\n").await.unwrap();
}
let mut socket = tokio_openssl::SslStream::new(Ssl::new(acceptor.context()).unwrap(), socket).unwrap();
if Pin::new(&mut socket).accept().await.is_err() {
return false;
}
assert_eq!(socket.ssl().servername(NameType::HOST_NAME), Some(PROXY_TLS_HOST));
let request = read_http_request(&mut socket).await;
let authority =
if target_port == 443 { PROXY_TLS_HOST.to_string() } else { format!("{PROXY_TLS_HOST}:{target_port}") };
assert!(request.lines().any(|line| line.eq_ignore_ascii_case(&format!("host: {authority}"))));
assert!(request.lines().next().unwrap().contains("max_threads=1"));
write_http_response(&mut socket, "200 OK", "1\n").await;
true
});
let mut proxy = reqwest::Proxy::all(format!("{proxy_type}://{address}")).unwrap();
if let Some((username, password)) = credentials {
proxy = proxy.basic_auth(username, password);
}
let client = ChClient::new_with_ca_cert_and_proxy(
&format!("https://{PROXY_TLS_HOST}:{target_port}"),
None,
None,
ca_file.path().to_str(),
Some("ssl=true&max_threads=1"),
Some(proxy),
Duration::from_secs(10),
)
.unwrap();
let result = test_connection(&client, Duration::from_secs(10)).await;
let tls_accepted = tokio::time::timeout(Duration::from_secs(10), server).await.unwrap().unwrap();
assert_eq!(result.is_ok(), tls_accepted);
result
}
#[tokio::test]
async fn socks5_https_proxy_preserves_sni_and_remote_dns() {
check_https_proxy("socks5h", None, 443, PROXY_TLS_HOST).await.unwrap();
}
#[tokio::test]
async fn authenticated_socks5_https_proxy_preserves_authority() {
check_https_proxy("socks5h", Some(("proxy:user", "p@ss/word#")), 8443, PROXY_TLS_HOST).await.unwrap();
}
#[tokio::test]
async fn authenticated_http_connect_preserves_sni_and_authority() {
check_https_proxy("http", Some(("proxy-user", "p@ss/word#")), 8443, PROXY_TLS_HOST).await.unwrap();
}
#[tokio::test]
async fn socks5_https_proxy_rejects_mismatched_certificate_hostname() {
assert!(check_https_proxy("socks5h", None, 443, "wrong.proxy.test").await.is_err());
}
async fn spawn_readonly_fallback_server(
success_body: &'static str,
) -> (String, tokio::task::JoinHandle<Vec<String>>) {