From 376d08044791fa8c1f75f263bed9a6c6fccb6843 Mon Sep 17 00:00:00 2001 From: Georg Heiler Date: Sat, 3 Oct 2026 09:14:12 +0200 Subject: [PATCH 1/3] feat: custom TLS root bundle and mutual TLS options Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 13 ++ Cargo.lock | 13 ++ Cargo.toml | 59 +++++++- README.md | 15 ++ src/lib.rs | 64 +++++++- src/options.rs | 67 +++++++++ src/tls.rs | 394 +++++++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 618 insertions(+), 7 deletions(-) create mode 100644 src/tls.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 85ba2f7..7417b66 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added + +* New database/connection options for customizing the TLS transport (requires a + `rustls-tls-aws-lc` or `rustls-tls-ring` feature, e.g. `rustls-tls`): + * `clickhouse.client.tls_root_certs`: a PEM root bundle that *replaces* the built-in roots + used to verify the server certificate (e.g. for a private CA / self-signed endpoint). + * `clickhouse.client.mtls_cert_chain` + `clickhouse.client.mtls_private_key`: a PEM client + certificate chain and matching private key presented to the server for mutual TLS. + * Malformed PEM is refused with `InvalidArguments`; a client identity with only one of the + two halves (chain or key) is refused when the connection is created. + * Options may be set on `ClickhouseDatabase` or passed to `Connection` creation via + `new_connection_with_opts()`; the private key is never read back. + ## [0.1.1] - 2026-08-31 ### Added diff --git a/Cargo.lock b/Cargo.lock index 5341f47..8bbd8d1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -18,7 +18,11 @@ dependencies = [ "clickhouse", "clickhouse-ext-arrow", "clickhouse-types", + "hyper-rustls", + "hyper-util", "rand", + "rustls", + "rustls-pemfile", "serde", "serde_bytes", "tokio", @@ -1478,6 +1482,15 @@ dependencies = [ "security-framework", ] +[[package]] +name = "rustls-pemfile" +version = "2.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dce314e5fee3f39953d46bb63bb8a46d40c2f8fb7cc5a3b6cab2bde9721d6e50" +dependencies = [ + "rustls-pki-types", +] + [[package]] name = "rustls-pki-types" version = "1.15.0" diff --git a/Cargo.toml b/Cargo.toml index c022360..7f6cadd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,11 +19,38 @@ crate-type = ["lib", "cdylib"] ffi = ["adbc_ffi"] native-tls = ["clickhouse/native-tls"] -rustls-tls = ["clickhouse/rustls-tls"] -rustls-tls-aws-lc = ["clickhouse/rustls-tls-aws-lc"] -rustls-tls-ring = ["clickhouse/rustls-tls-ring"] -rustls-tls-native-roots = ["clickhouse/rustls-tls-native-roots"] -rustls-tls-webpki-roots = ["clickhouse/rustls-tls-webpki-roots"] + +# Enable both a crypto provider (`rustls-tls-aws-lc`) and a root store +# (`rustls-tls-webpki-roots`), mirroring the `rustls-tls` feature of the +# underlying `clickhouse` crate. +rustls-tls = ["rustls-tls-aws-lc", "rustls-tls-webpki-roots"] +rustls-tls-aws-lc = [ + "clickhouse/rustls-tls-aws-lc", + "hyper-rustls?/aws-lc-rs", + "tls-mtls", +] +rustls-tls-ring = [ + "clickhouse/rustls-tls-ring", + "hyper-rustls?/ring", + "tls-mtls", +] +rustls-tls-native-roots = [ + "clickhouse/rustls-tls-native-roots", + "hyper-rustls?/native-tokio", +] +rustls-tls-webpki-roots = [ + "clickhouse/rustls-tls-webpki-roots", + "hyper-rustls?/webpki-tokio", +] +# Internal: enables the driver's custom TLS root bundle and mutual TLS support. +# It needs a crypto provider, so it is only ever turned on by +# `rustls-tls-aws-lc`/`rustls-tls-ring`. +tls-mtls = [ + "dep:rustls", + "dep:hyper-rustls", + "dep:hyper-util", + "dep:rustls-pemfile", +] [dependencies] arrow-array = "58.3.0" @@ -66,6 +93,28 @@ features = ["rt"] version = "1.23.3" features = ["serde"] +# Used to build a `rustls::ClientConfig` when the `rustls-tls-*` features are enabled. +[dependencies.hyper-rustls] +version = "0.27" +optional = true +default-features = false +features = ["http1", "tls12"] + +[dependencies.hyper-util] +version = "0.1" +optional = true +features = ["client-legacy", "http1", "tokio"] + +[dependencies.rustls] +version = "0.23" +optional = true +default-features = false + +[dependencies.rustls-pemfile] +version = "2" +optional = true +features = ["std"] + [[test]] name = "it" path = "tests/it/main.rs" diff --git a/README.md b/README.md index 15f798d..c2d1aa7 100644 --- a/README.md +++ b/README.md @@ -160,6 +160,21 @@ This package exposes the same Transport Layer Security (TLS) features as Note that Rustls has no TLS roots by default; when using the `rustls-tls-aws-lc` or `rustls-tls-ring` features, you should also enable either `rustls-tls-native-roots` or `rustls-tls-webpki-roots` to choose a TLS root store. +When built with a crypto-provider feature (`rustls-tls-aws-lc` or `rustls-tls-ring`, e.g. `rustls-tls`), +the driver also exposes three `clickhouse.client.*` options for customizing the transport: + +* `clickhouse.client.tls_root_certs`: a PEM root bundle that *replaces* the built-in roots used to + verify the ClickHouse server certificate. This is how deployments running the server behind their + own TLS endpoints (a private CA or a self-signed certificate) trust them. +* `clickhouse.client.mtls_cert_chain` and `clickhouse.client.mtls_private_key`: a PEM client + certificate chain and matching private key, presented to the server when it requests (or requires) + a client certificate for mutual TLS. + +These options may be set on a `Database` or passed to connection creation via +`new_connection_with_opts()`. Malformed PEM and a client identity given with only one of its two +halves (a chain without a key, or a key without a chain) are refused with an `InvalidArguments` error. +The private key is never read back. + [Rustls]: https://github.com/rustls/rustls [`aws-lc`]: https://github.com/aws/aws-lc-rs [`ring`]: https://github.com/briansmith/ring diff --git a/src/lib.rs b/src/lib.rs index 26b5bed..dbe35a9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -128,6 +128,7 @@ pub mod options; mod reader; mod schema; mod statement; +mod tls; mod writer; #[cfg(doctest)] @@ -242,6 +243,7 @@ impl Driver for ClickhouseDriver { password: None, product_info: self.product_info.clone(), settings: HashMap::new(), + tls: tls::TlsOptions::default(), }; for (key, value) in opts { @@ -356,6 +358,7 @@ pub struct ClickhouseDatabase { password: Option, product_info: ProductInfo, settings: HashMap, + tls: tls::TlsOptions, } impl Database for ClickhouseDatabase { @@ -369,6 +372,18 @@ impl Database for ClickhouseDatabase { &self, opts: impl IntoIterator, ) -> adbc_core::error::Result { + let opts: Vec<(OptionConnection, OptionValue)> = opts.into_iter().collect(); + + // The TLS transport is fixed when the HTTP client is built, so assemble it now from the + // database-level defaults plus any per-connection overrides. + let mut tls = self.tls.clone(); + for (key, value) in opts.iter().filter_map(|(key, value)| match key { + OptionConnection::Other(s) if tls::is_tls_option(s) => Some((s, value)), + _ => None, + }) { + tls.try_set(key, value.clone().try_string(key)?)?; + } + let tokio = self .tokio .clone() @@ -380,7 +395,29 @@ impl Database for ClickhouseDatabase { // It's better to create a new `Client` for each connection because it may error or deadlock // if it tries to hop runtimes in the current-thread case. // TODO: configure the HTTP client to pool only a single connection (necessary?) - let mut client = Client::default() + // + // When TLS options were set we substitute a `hyper-rustls` transport carrying our custom + // `rustls::ClientConfig` for the default one; otherwise fall back to `Client::default()`. + let client = { + #[cfg(feature = "tls-mtls")] + { + if tls.is_empty() { + Client::default() + } else { + let connector = tls.build_https_connector()?; + let http = hyper_util::client::legacy::Client::builder( + hyper_util::rt::TokioExecutor::new(), + ) + .build(connector); + Client::with_http_client(http) + } + } + #[cfg(not(feature = "tls-mtls"))] + { + Client::default() + } + }; + let mut client = client // Default `product_info` that should always be included // Note: we don't apply `self.product_info` at this level in case it's set // to a different value at a lower level; `AugmentedClient` covers that. @@ -407,9 +444,17 @@ impl Database for ClickhouseDatabase { drop(tokio_guard); - let mut connection = ClickhouseConnection { client, tokio }; + let mut connection = ClickhouseConnection { client, tokio, tls }; for (key, value) in opts { + // TLS options were already applied above; the transport is immutable after creation. + let tls_option = match &key { + OptionConnection::Other(s) => tls::is_tls_option(s), + _ => false, + }; + if tls_option { + continue; + } connection.set_option(key, value)?; } @@ -583,6 +628,9 @@ impl ClickhouseDatabase { options::PRODUCT_INFO => { self.product_info = value.try_into()?; } + other if tls::is_tls_option(other) => { + self.tls.try_set(other, value.try_string(other)?)?; + } other => { return Err(Error::with_message_and_status( format!("unknown database option {other:?}"), @@ -605,6 +653,7 @@ impl ClickhouseDatabase { match key { options::PRODUCT_INFO => Ok(self.product_info.to_string()), + other if tls::is_tls_option(other) => self.tls.read_back(other), other => Err(Error::with_message_and_status( format!("unknown database option {other:?}"), Status::InvalidArguments, @@ -648,6 +697,7 @@ impl ClickhouseDatabase { pub struct ClickhouseConnection { client: AugmentedClient, tokio: TokioContext, + tls: tls::TlsOptions, } impl Connection for ClickhouseConnection { @@ -826,6 +876,15 @@ impl ClickhouseConnection { return Ok(()); } + if tls::is_tls_option(key) { + return Err(Error::with_message_and_status( + "TLS options can only be set on the database or when creating the \ + connection (via `Database::new_connection_with_opts`); \ + the transport cannot be changed after the connection exists", + Status::InvalidArguments, + )); + } + match key { options::PRODUCT_INFO => { self.client.set_product_info(&value.try_into()?); @@ -866,6 +925,7 @@ impl ClickhouseConnection { .get_option(options::as_setting::SESSION_ID) .unwrap_or("") .into()), + other if tls::is_tls_option(other) => self.tls.read_back(other), other => Err(Error::with_message_and_status( format!("unknown connection option {other:?}"), Status::InvalidArguments, diff --git a/src/options.rs b/src/options.rs index 1412dfc..fbdae5f 100644 --- a/src/options.rs +++ b/src/options.rs @@ -164,6 +164,73 @@ pub const QUERY_ID: &str = "clickhouse.client.query_id"; /// as Arrow binary strings instead. pub const OUTPUT_STRING_AS_STRING: &str = "clickhouse.client.output_string_as_string"; +/// PEM root certificates used to verify the ClickHouse server's certificate. +/// +/// When set, this bundle *replaces* (rather than extends) the built-in root store +/// (native platform roots or [`webpki-roots`]) that the driver would otherwise use, +/// so only the given certificate authority is trusted. +/// +/// This is how any deployment that runs ClickHouse behind its own TLS endpoints +/// (a private CA, a self-signed certificate, or an internal PKI) can authenticate those +/// endpoints without relying on the public roots, and it is required when the server +/// certificate is not signed by a public CA. +/// +/// The value must be one or more PEM-encoded certificates. Malformed PEM is refused +/// with an error with [`Status::InvalidArguments`]. +/// +/// May be set on [`ClickhouseDatabase`] or passed to [`Database::new_connection_with_opts()`] +/// for a specific connection. +/// +/// Requires the crate to be built with a `rustls-tls-aws-lc` or `rustls-tls-ring` feature. +/// +/// [`webpki-roots`]: https://github.com/rustls/webpki-roots +/// [`Status::InvalidArguments`]: adbc_core::error::Status::InvalidArguments +pub const TLS_ROOT_CERTS: &str = "clickhouse.client.tls_root_certs"; + +/// PEM certificate chain presented to the server as the client identity for mutual TLS. +/// +/// The chain is sent when the ClickHouse server requests (or requires) a client certificate. +/// Deployments that enforce mutual TLS need this to authenticate the client before the server +/// accepts any query. +/// +/// Must be paired with [`MTLS_PRIVATE_KEY`]: specifying a certificate chain without a private +/// key (or a key without a chain) is refused with an error with [`Status::InvalidArguments`] +/// when the connection is created. +/// +/// The value must be one or more PEM-encoded certificates (the leaf first, followed by any +/// intermediate certificates). Malformed PEM is refused with an error with +/// [`Status::InvalidArguments`]. +/// +/// May be set on [`ClickhouseDatabase`] or passed to [`Database::new_connection_with_opts()`] +/// for a specific connection. +/// +/// Requires the crate to be built with a `rustls-tls-aws-lc` or `rustls-tls-ring` feature. +/// +/// [`Status::InvalidArguments`]: adbc_core::error::Status::InvalidArguments +pub const MTLS_CERT_CHAIN: &str = "clickhouse.client.mtls_cert_chain"; + +/// PEM-encoded private key matched to the [`MTLS_CERT_CHAIN`] used as the client identity +/// for mutual TLS. +/// +/// This key is never read back through [`Optionable::get_option_string()`] +/// (its read-back returns an error with [`Status::NotFound`]) to avoid leaking a secret. +/// +/// Must be paired with [`MTLS_CERT_CHAIN`]: specifying a private key without a certificate +/// chain (or a chain without a key) is refused with an error with [`Status::InvalidArguments`] +/// when the connection is created. +/// +/// The value must be a single PEM-encoded private key. Malformed PEM is refused with an error +/// with [`Status::InvalidArguments`]. +/// +/// May be set on [`ClickhouseDatabase`] or passed to [`Database::new_connection_with_opts()`] +/// for a specific connection. +/// +/// Requires the crate to be built with a `rustls-tls-aws-lc` or `rustls-tls-ring` feature. +/// +/// [`Status::NotFound`]: adbc_core::error::Status::NotFound +/// [`Status::InvalidArguments`]: adbc_core::error::Status::InvalidArguments +pub const MTLS_PRIVATE_KEY: &str = "clickhouse.client.mtls_private_key"; + /// Prefix for passing arbitrary [ClickHouse settings] through as driver options. /// /// An option key of the form `clickhouse.setting.` maps directly to the diff --git a/src/tls.rs b/src/tls.rs new file mode 100644 index 0000000..eb760e7 --- /dev/null +++ b/src/tls.rs @@ -0,0 +1,394 @@ +//! Client-side TLS configuration for the HTTP transport. +//! +//! When the crate is built with a crypto-provider feature (`rustls-tls-aws-lc` or +//! `rustls-tls-ring`, which enable the internal `tls-mtls` feature), the driver can verify the +//! ClickHouse server against a caller-supplied PEM root bundle (replacing the built-in roots) +//! and/or present a client identity (PEM certificate chain + private key) for mutual TLS. +//! +//! The transport is assembled into a custom [`rustls::ClientConfig`] that is handed to the +//! underlying `clickhouse` client through [`clickhouse::Client::with_http_client`], which accepts +//! any `hyper-util` legacy client whose connector is a [`hyper_rustls::HttpsConnector`]. + +use std::io::BufReader; + +use adbc_core::error::{Error, Status}; + +use crate::options::{MTLS_CERT_CHAIN, MTLS_PRIVATE_KEY, TLS_ROOT_CERTS}; + +/// The TLS trust and identity options for the HTTP transport. +/// +/// Holds the raw PEM strings set through the driver options. Each PEM value is validated as soon +/// as it is set; whether a client identity is fully specified (both halves) is only checked when +/// the transport is built, so a caller may set the two halves across separate option-setting calls. +#[derive(Clone, Default)] +pub(crate) struct TlsOptions { + /// PEM root certificates that *replace* the built-in roots when set. + pub(crate) root_certs: Option, + /// PEM client certificate chain for mutual TLS. + pub(crate) client_cert_chain: Option, + /// PEM client private key for mutual TLS. + pub(crate) client_private_key: Option, +} + +// The private key is a secret, so `Debug` reports only whether it is set. +impl std::fmt::Debug for TlsOptions { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("TlsOptions") + .field("root_certs", &self.root_certs) + .field("client_cert_chain", &self.client_cert_chain) + .field( + "client_private_key", + &self.client_private_key.as_ref().map(|_| ""), + ) + .finish() + } +} + +/// Whether `key` is one of this module's TLS options. +pub(crate) fn is_tls_option(key: &str) -> bool { + matches!(key, TLS_ROOT_CERTS | MTLS_CERT_CHAIN | MTLS_PRIVATE_KEY) +} + +impl TlsOptions { + pub(crate) fn is_empty(&self) -> bool { + self.root_certs.is_none() + && self.client_cert_chain.is_none() + && self.client_private_key.is_none() + } + + /// Validate and store a single option value. + /// + /// Malformed PEM is refused with [`Status::InvalidArguments`] immediately. The mutual-TLS + /// pairing rule (certificate chain without key, or key without chain) is enforced later, at + /// transport-build time, via [`Self::check_identity`]. Without a crypto-provider feature this + /// refuses loudly rather than silently ignoring the option. + pub(crate) fn try_set(&mut self, key: &str, value: String) -> Result<(), Error> { + #[cfg(feature = "tls-mtls")] + { + match key { + TLS_ROOT_CERTS => { + ensure_certificates(&value, key)?; + self.root_certs = Some(value); + } + MTLS_CERT_CHAIN => { + ensure_certificates(&value, key)?; + self.client_cert_chain = Some(value); + } + MTLS_PRIVATE_KEY => { + parse_private_key(&value, key)?; + self.client_private_key = Some(value); + } + other => return Err(unknown_option(other)), + } + Ok(()) + } + + #[cfg(not(feature = "tls-mtls"))] + { + let _ = (&key, &value); + Err(Error::with_message_and_status( + format!( + "option {key:?} requires this crate to be built with a \ + `rustls-tls-aws-lc` or `rustls-tls-ring` feature" + ), + Status::InvalidArguments, + )) + } + } + + /// Refuse a client identity specified with only one half (chain without key, or key without + /// chain). + pub(crate) fn check_identity(&self) -> Result<(), Error> { + if self.client_cert_chain.is_some() != self.client_private_key.is_some() { + return Err(Error::with_message_and_status( + format!( + "client identity must set both {MTLS_CERT_CHAIN:?} and {MTLS_PRIVATE_KEY:?}; \ + got certificate chain: {}, private key: {}", + self.client_cert_chain.is_some(), + self.client_private_key.is_some(), + ), + Status::InvalidArguments, + )); + } + Ok(()) + } + + /// Read back an option's stored value. + /// + /// The private key is treated as a secret and its read-back is refused with + /// [`Status::NotFound`] (mirroring the driver's handling of other sensitive options). + pub(crate) fn read_back(&self, key: &str) -> Result { + match key { + MTLS_PRIVATE_KEY => Err(Error::with_message_and_status( + "read-back of the private key has been omitted to avoid leaking a secret", + Status::NotFound, + )), + TLS_ROOT_CERTS => Ok(self.root_certs.clone().unwrap_or_default()), + MTLS_CERT_CHAIN => Ok(self.client_cert_chain.clone().unwrap_or_default()), + other => Err(unknown_option(other)), + } + } + + /// Build the `hyper-rustls` HTTPS connector carrying this configuration. + /// + /// Only called when `!self.is_empty()`, i.e. the caller wants to customize the transport. + #[cfg(feature = "tls-mtls")] + pub(crate) fn build_https_connector( + &self, + ) -> Result< + hyper_rustls::HttpsConnector, + Error, + > { + use hyper_rustls::ConfigBuilderExt; + use hyper_rustls::HttpsConnectorBuilder; + use hyper_util::client::legacy::connect::HttpConnector; + use rustls::{ClientConfig, RootCertStore}; + + self.check_identity()?; + + // Mirror the `clickhouse` crate: the crypto provider follows the enabled feature. + #[cfg(feature = "rustls-tls-aws-lc")] + let provider = rustls::crypto::aws_lc_rs::default_provider(); + #[cfg(all(feature = "rustls-tls-ring", not(feature = "rustls-tls-aws-lc")))] + let provider = rustls::crypto::ring::default_provider(); + + let builder = ClientConfig::builder_with_provider(std::sync::Arc::new(provider)) + .with_safe_default_protocol_versions() + .map_err(transport_error)?; + + // A caller-supplied bundle *replaces* (rather than extends) the built-in root store. + let builder = if let Some(pem) = &self.root_certs { + let mut store = RootCertStore::empty(); + store.add_parsable_certificates(parse_certificates(pem, TLS_ROOT_CERTS)?); + builder.with_root_certificates(store) + } else { + #[cfg(feature = "rustls-tls-native-roots")] + { + builder.with_native_roots().map_err(transport_error)? + } + #[cfg(all( + feature = "rustls-tls-webpki-roots", + not(feature = "rustls-tls-native-roots") + ))] + { + builder.with_webpki_roots() + } + }; + + let config = if let (Some(certs), Some(key)) = ( + self.client_cert_chain.as_deref(), + self.client_private_key.as_deref(), + ) { + builder + .with_client_auth_cert( + parse_certificates(certs, MTLS_CERT_CHAIN)?, + parse_private_key(key, MTLS_PRIVATE_KEY)?, + ) + .map_err(|e| { + invalid_arguments(format!("error configuring client certificate: {e}")) + })? + } else { + builder.with_no_client_auth() + }; + + let http = HttpConnector::new(); + Ok(HttpsConnectorBuilder::new() + .with_tls_config(config) + .https_or_http() + .enable_http1() + .wrap_connector(http)) + } +} + +#[cfg(feature = "tls-mtls")] +fn parse_certificates( + pem: &str, + key: &str, +) -> Result>, Error> { + rustls_pemfile::certs(&mut BufReader::new(pem.as_bytes())) + .collect::, _>>() + .map_err(|e| invalid_arguments(format!("malformed PEM in option {key:?}: {e}"))) +} + +#[cfg(feature = "tls-mtls")] +fn ensure_certificates(pem: &str, key: &str) -> Result<(), Error> { + let certs = parse_certificates(pem, key)?; + if certs.is_empty() { + return Err(invalid_arguments(format!( + "no certificates found in option {key:?}" + ))); + } + Ok(()) +} + +#[cfg(feature = "tls-mtls")] +fn parse_private_key( + pem: &str, + key: &str, +) -> Result, Error> { + rustls_pemfile::private_key(&mut BufReader::new(pem.as_bytes())) + .map_err(|e| invalid_arguments(format!("malformed PEM in option {key:?}: {e}")))? + .ok_or_else(|| invalid_arguments(format!("no private key found in option {key:?}"))) +} + +fn invalid_arguments(message: impl Into) -> Error { + Error::with_message_and_status(message, Status::InvalidArguments) +} + +fn transport_error(e: impl std::fmt::Display) -> Error { + Error::with_message_and_status( + format!("error building TLS configuration: {e}"), + Status::Internal, + ) +} + +fn unknown_option(key: &str) -> Error { + Error::with_message_and_status( + format!("unknown TLS option {key:?}"), + Status::InvalidArguments, + ) +} + +#[cfg(all(test, feature = "tls-mtls"))] +mod tests { + use super::*; + use adbc_core::error::Status; + + const CA_PEM: &str = r#"-----BEGIN CERTIFICATE----- +MIIC1DCCAbygAwIBAgIJAN6l6UQNGiZvMA0GCSqGSIb3DQEBCwUAMBcxFTATBgNV +BAMMDGFkYmMtdGVzdC1jYTAeFw0yNjEwMDIxMTU1NDRaFw0zNjA5MjkxMTU1NDRa +MBcxFTATBgNVBAMMDGFkYmMtdGVzdC1jYTCCASIwDQYJKoZIhvcNAQEBBQADggEP +ADCCAQoCggEBANiRjQ301ic4oFdnLqLO/h9tOUSMv6pnOnnRQaS0kF3nI3DxbWwd +UwA2OHMgfwr2hyDR0RQ8kApI2yqNx5L00vXTd+1NYfc+gErGzQQNaNatFa7qqm3r +NVAmlESRKPb9qhahUY/6T1fYcA7S91kHd6r0JDHrfYuVfmGuW+YA5m5qmQXonrhb +5yzKZGs+0R7Rnz7yn2smpFhMgKlqXg47aUpHm2YMQN/GphNCr4nOMDl0s6k675Zv +7MAk60jV+NmD46zLbWg8E2idjlnF3eJVeoIdQUpruL+M6rhsnLNEZ/2j2sqekpKJ +UO3mN8U3qHp/kI0oX8U2Y7YsnFy4RIrtdfkCAwEAAaMjMCEwDwYDVR0TAQH/BAUw +AwEB/zAOBgNVHQ8BAf8EBAMCAQYwDQYJKoZIhvcNAQELBQADggEBABPtldaCqF0v +x2ddEKh/erNzoPd8FU6MLRJ7bZdst8/FdcirzheKAohY0IlShBkWT0uRV78YfjuR +P9RpgWlZIaJXx8/H0SxXSJi3mxLbfRq3sAHoN70fnZOUaJrNCMk1P1s5ludcbvu0 +k/wIJvfa7U6EUK3gv18Vn/w99XtpDbk7gzgZXL7+jHjGL3iyxNG5G2QKmLyCDhoD +XZ7jVyZrco9+kOaR+NKVJOkFA7yja/jp9nhNfE28pO4bYdkhW43sEZM5SaWeV3vH +HBrp40dYSiXyHQRNhvjh+lATV+VRGQX+ZgZH/i5PUSyiokFBY9duhcMmq4uQUUA8 +BAVnu77lGtc= +-----END CERTIFICATE-----"#; + const CLIENT_CERT_PEM: &str = r#"-----BEGIN CERTIFICATE----- +MIIC3TCCAcWgAwIBAgIJAKO/xlCxuIUKMA0GCSqGSIb3DQEBCwUAMBcxFTATBgNV +BAMMDGFkYmMtdGVzdC1jYTAeFw0yNjEwMDIxMTU1NDRaFw0zNjA5MjkxMTU1NDRa +MBExDzANBgNVBAMMBmNsaWVudDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoC +ggEBAMXX9+eo1kMLwibbe4zErkQoXGQtFcgqvIvjLTpHgzk0i8ffdKSdpgfx45hz +5Y6IkssHOy5fW1T/RSs6WDCekj1f4C/c6f/6gAF7FV2sHSI7Kzst2QhAYobyHVf2 +DOYYfCL5r7jdddguwpLnPyE0LBTFXtZIE8b0rZmpyyCuR0Rqz+JPRqxibnTjnzAm +TdV14Dn/t/d35XpEvuGh9V1kpF0n45lz/jL1Wa/CPTyzX5L8AQAuvIKHSy7fqVs4 +lmLc5rQLNwpw2hRAM1hFn+z30QMHxPoBGXswhBHNJuf+60LtU263cm6UV6th/JmL +SnCNa/iNnaxNItm1NhfhKOqBx9kCAwEAAaMyMDAwDAYDVR0TAQH/BAIwADATBgNV +HSUEDDAKBggrBgEFBQcDAjALBgNVHQ8EBAMCBaAwDQYJKoZIhvcNAQELBQADggEB +ALeAdzcWSrFd4EroPu+LJKIqTYWCRcls2wYk6d6g1E5HCx48bonFpOt6WiVjNsko +rggUV8NwbO3F5fPIbu/z9OU+umXNPcBAL4HVMILB5gbUkItDZ591hWjcZYZ4IkVc +nCbVG2LOOWJKebED0urhtCMTj/1KWiJRwMJd3LC08CPhPRjWgXfj8+XF49dfeM74 +dfKncbqZTeZgweYpf46nW5J0sMADgCx43JY7mBG61uGitReG1ROII+987wFUAFK4 +Rm9QLDqbJXM9G8IpZMLt0lMaFW5t4YJATzK9VzpgsQIpJjqQ6yFGAP+eKZLlNPHT +3dpoWDnAH7z6djCo2eocDzM= +-----END CERTIFICATE-----"#; + const CLIENT_KEY_PEM: &str = r#"-----BEGIN RSA PRIVATE KEY----- +MIIEpQIBAAKCAQEAxdf356jWQwvCJtt7jMSuRChcZC0VyCq8i+MtOkeDOTSLx990 +pJ2mB/HjmHPljoiSywc7Ll9bVP9FKzpYMJ6SPV/gL9zp//qAAXsVXawdIjsrOy3Z +CEBihvIdV/YM5hh8IvmvuN112C7Ckuc/ITQsFMVe1kgTxvStmanLIK5HRGrP4k9G +rGJudOOfMCZN1XXgOf+393flekS+4aH1XWSkXSfjmXP+MvVZr8I9PLNfkvwBAC68 +godLLt+pWziWYtzmtAs3CnDaFEAzWEWf7PfRAwfE+gEZezCEEc0m5/7rQu1Tbrdy +bpRXq2H8mYtKcI1r+I2drE0i2bU2F+Eo6oHH2QIDAQABAoIBABKSW2sbpkFU8TzE +jayBKfqr70BYGxtXJ6vZ/3zhCahtDlIM3940DkngowTEnF/s4hq86wlHWkN4Uv3j +1DN8Ly4jud/EVRL6aNm6vB628WUtOLmtbBoiMR2JzMddCYjv83c1CRHJeC569Tjl +DEAJrbmlbWmgCns6rLEoBzFiKr55PfqQxcleJsVObSj34zSTQR+lD5aqgbOZYVIj +seUKQTcTArKf3X/r1GPxBJl+N31CwvBq4Rb+mUi3zOD93z4C5mrX0iTsHGk+g0Cz +FWqqM0Pwuyf3dKFeA1HAhm+tKsQHICzomlilxzz98bGKnPAqZGoGxhnz4uaGE7WI +rAMlEIECgYEA8typ0h9DWTp5i4C7bGAVjTv9VMBZqTrKxLI45nnV4vofmuuu8x0w +3sOjQHFj2mOPSdTh0hFzsR+VyQM5rd8xiMqaboFeTcUkM+pX8XmWjXEAEGtuuqiv +cADpg7ZmY72Y9OqjGdPu6/apmLiT5Lm0Lb3aV9j5YvusFimb6I5sxxUCgYEA0Ivb +XtLd1Cs3tNV9rqwJMz8AFtN+bZiQvh3kEDCeJDPq6jvPsODH9nAOpyJpx0mhTixj +0rpT1wJCpw1Pyw67fqFGTCqZ+5uN/svgHGa+5RqvhkE69Zd4j5Tj9mkKR43oAHhl +RZOmbJbgZJDkJwNxhUk3GIVYioDIcwFlenVWbrUCgYEAqtSC89Wf1xzlsDYuH+56 +Wm1PJhVjPEGprbA7jAs5Yz5TivKrjkEDRj7RD5xzA4Qyn1mKOuyht36jKtEE+oPb +4P+6n26KeiPo8ket8qJ3Pyi0Rdh7+/p55UwO9eIQpaoQ9H4zmhBUqFBKPa3gAeyK +i/5WnqJ10aV8B97e1zeP1n0CgYEAyOMQDAWf/bMr1BwbF1lM+3pigZp1HOlr9Fci +y52pmEIStlOrOz0sIe3Lj6AMuRXqNj14YxNtk53vyq99/6JPZinAZ7pZ3g7390s4 +tOMcW+Ea136aZtzkG5tXBJQmfMrrgxh2pVEXbV7hBL61qAcfIT9nvU0l/zUS4gWk +VRQWkEkCgYEAitkEz6TO4Tl+38y/FM/vMB/z6fpxtIGyYbx92Mtllcxi1mSgW6K4 +A8/soZvMNWlT6HvX2o+D8hxV+vrT07+ezjl8rYfQysHcGcwXyfGVo67zVs1ZOm+3 +O6N3BhqxCUTBtZJDOj2gZP5b1REU3YrRIbCNPX8Z29s1ZSXCRCenGUc= +-----END RSA PRIVATE KEY-----"#; + + #[test] + fn tls_option_names_use_repo_style() { + assert_eq!(TLS_ROOT_CERTS, "clickhouse.client.tls_root_certs"); + assert_eq!(MTLS_CERT_CHAIN, "clickhouse.client.mtls_cert_chain"); + assert_eq!(MTLS_PRIVATE_KEY, "clickhouse.client.mtls_private_key"); + for key in [TLS_ROOT_CERTS, MTLS_CERT_CHAIN, MTLS_PRIVATE_KEY] { + assert!(is_tls_option(key)); + assert!(key.starts_with("clickhouse.client.")); + } + assert!(!is_tls_option("clickhouse.client.product_info")); + } + + #[test] + fn refuses_malformed_pem() { + let mut opts = TlsOptions::default(); + let err = opts + .try_set(TLS_ROOT_CERTS, "not a certificate".into()) + .unwrap_err(); + assert_eq!(err.status, Status::InvalidArguments); + + let err = opts + .try_set( + MTLS_CERT_CHAIN, + "-----BEGIN CERTIFICATE-----\n!!!!!!!!\n-----END CERTIFICATE-----".into(), + ) + .unwrap_err(); + assert_eq!(err.status, Status::InvalidArguments); + + let err = opts + .try_set(MTLS_PRIVATE_KEY, "not a key".into()) + .unwrap_err(); + assert_eq!(err.status, Status::InvalidArguments); + } + + #[test] + fn accepts_valid_pem() { + let mut opts = TlsOptions::default(); + opts.try_set(TLS_ROOT_CERTS, CA_PEM.into()).unwrap(); + opts.try_set(MTLS_CERT_CHAIN, CLIENT_CERT_PEM.into()) + .unwrap(); + opts.try_set(MTLS_PRIVATE_KEY, CLIENT_KEY_PEM.into()) + .unwrap(); + assert!(!opts.is_empty()); + opts.check_identity().unwrap(); + } + + #[test] + fn refuses_half_identity() { + let mut opts = TlsOptions::default(); + opts.try_set(MTLS_CERT_CHAIN, CLIENT_CERT_PEM.into()) + .unwrap(); + // Certificate chain without a private key is refused. + let err = opts.check_identity().unwrap_err(); + assert_eq!(err.status, Status::InvalidArguments); + + let mut opts = TlsOptions::default(); + opts.try_set(MTLS_PRIVATE_KEY, CLIENT_KEY_PEM.into()) + .unwrap(); + // Private key without a certificate chain is refused. + let err = opts.check_identity().unwrap_err(); + assert_eq!(err.status, Status::InvalidArguments); + } + + #[test] + fn builds_https_connector() { + let mut opts = TlsOptions::default(); + opts.try_set(TLS_ROOT_CERTS, CA_PEM.into()).unwrap(); + opts.try_set(MTLS_CERT_CHAIN, CLIENT_CERT_PEM.into()) + .unwrap(); + opts.try_set(MTLS_PRIVATE_KEY, CLIENT_KEY_PEM.into()) + .unwrap(); + opts.build_https_connector().unwrap(); + } +} From da1b7838577f38b813d666cbe577b63e015353c2 Mon Sep 17 00:00:00 2001 From: Georg Heiler Date: Fri, 23 Oct 2026 13:48:56 +0200 Subject: [PATCH 2/3] fix: refuse a declared TLS option with an http URI A declared TLS option (root bundle, client certificate chain or client private key) is now refused when the URI scheme is http, or when no URI is set and the default http applies. The driver returns `InvalidArguments` that names the option. Tests cover the refusal and an https URI that still builds the connection. --- src/lib.rs | 61 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/tls.rs | 56 +++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 113 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index dbe35a9..b5206cf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -404,6 +404,7 @@ impl Database for ClickhouseDatabase { if tls.is_empty() { Client::default() } else { + tls.require_https(self.url.as_ref().map(Url::scheme))?; let connector = tls.build_https_connector()?; let http = hyper_util::client::legacy::Client::builder( hyper_util::rt::TokioExecutor::new(), @@ -1116,6 +1117,66 @@ mod tests { use adbc_core::{Connection, Database, Driver, Optionable}; use url::Url; + #[cfg(feature = "tls-mtls")] + fn database_declaring_tls(uri: Option<&str>, identity: bool) -> crate::ClickhouseDatabase { + use crate::tls::tests::{CA_PEM, CLIENT_CERT_PEM, CLIENT_KEY_PEM}; + + let mut opts = Vec::<(OptionDatabase, OptionValue)>::new(); + if let Some(uri) = uri { + opts.push((OptionDatabase::Uri, uri.into())); + } + if identity { + opts.push((options::MTLS_CERT_CHAIN.into(), CLIENT_CERT_PEM.into())); + opts.push((options::MTLS_PRIVATE_KEY.into(), CLIENT_KEY_PEM.into())); + } else { + opts.push((options::TLS_ROOT_CERTS.into(), CA_PEM.into())); + } + ClickhouseDriver::init() + .new_database_with_opts(opts) + .unwrap() + } + + #[cfg(feature = "tls-mtls")] + #[test] + fn a_declared_tls_setting_is_refused_on_an_http_uri() { + for (uri, identity, setting) in [ + (Some("http://127.0.0.1:1"), false, options::TLS_ROOT_CERTS), + (Some("http://127.0.0.1:1"), true, options::MTLS_CERT_CHAIN), + (None, false, options::TLS_ROOT_CERTS), + ] { + let db = database_declaring_tls(uri, identity); + + let Err(err) = db.new_connection() else { + panic!("{setting} declared with URI {uri:?} should be refused"); + }; + + assert_eq!(err.status, Status::InvalidArguments, "{err:?}"); + assert!(err.message.contains("requires an https URI"), "{err:?}"); + assert!(err.message.contains(setting), "{err:?}"); + } + } + + #[cfg(feature = "tls-mtls")] + #[test] + fn a_declared_tls_root_on_an_https_uri_reaches_the_dial() { + use adbc_core::Statement as _; + + let db = database_declaring_tls(Some("https://127.0.0.1:1"), false); + + let mut conn = db.new_connection().unwrap(); + + let mut statement = conn.new_statement().unwrap(); + statement.set_sql_query("SELECT 1").unwrap(); + + // Nothing listens on port 1, so the query fails at the connection, not at the option check. + let Err(err) = statement.execute() else { + panic!("nothing listens on port 1, so no query should be answered"); + }; + + assert!(!err.message.contains("requires an https URI"), "{err:?}"); + assert_ne!(err.status, Status::InvalidArguments, "{err:?}"); + } + #[test] fn test_set_product_info() { let mut driver = ClickhouseDriver::init(); diff --git a/src/tls.rs b/src/tls.rs index eb760e7..2c93724 100644 --- a/src/tls.rs +++ b/src/tls.rs @@ -56,6 +56,32 @@ impl TlsOptions { && self.client_private_key.is_none() } + /// Refuse a declared TLS setting on a URI that is not `https`. + /// + /// `scheme` is the scheme of the configured URI, or `None` where none is configured and the + /// client's default (`http`) applies. + #[cfg(feature = "tls-mtls")] + pub(crate) fn require_https(&self, scheme: Option<&str>) -> Result<(), Error> { + let declared = [ + (TLS_ROOT_CERTS, self.root_certs.is_some()), + (MTLS_CERT_CHAIN, self.client_cert_chain.is_some()), + (MTLS_PRIVATE_KEY, self.client_private_key.is_some()), + ] + .into_iter() + .filter_map(|(name, set)| set.then_some(name)) + .collect::>(); + if declared.is_empty() || scheme == Some("https") { + return Ok(()); + } + Err(Error::with_message_and_status( + format!( + "a declared TLS setting requires an https URI: {declared:?} set, URI scheme is {:?}", + scheme.unwrap_or("http"), + ), + Status::InvalidArguments, + )) + } + /// Validate and store a single option value. /// /// Malformed PEM is refused with [`Status::InvalidArguments`] immediately. The mutual-TLS @@ -250,11 +276,11 @@ fn unknown_option(key: &str) -> Error { } #[cfg(all(test, feature = "tls-mtls"))] -mod tests { +pub(crate) mod tests { use super::*; use adbc_core::error::Status; - const CA_PEM: &str = r#"-----BEGIN CERTIFICATE----- + pub(crate) const CA_PEM: &str = r#"-----BEGIN CERTIFICATE----- MIIC1DCCAbygAwIBAgIJAN6l6UQNGiZvMA0GCSqGSIb3DQEBCwUAMBcxFTATBgNV BAMMDGFkYmMtdGVzdC1jYTAeFw0yNjEwMDIxMTU1NDRaFw0zNjA5MjkxMTU1NDRa MBcxFTATBgNVBAMMDGFkYmMtdGVzdC1jYTCCASIwDQYJKoZIhvcNAQEBBQADggEP @@ -272,7 +298,7 @@ XZ7jVyZrco9+kOaR+NKVJOkFA7yja/jp9nhNfE28pO4bYdkhW43sEZM5SaWeV3vH HBrp40dYSiXyHQRNhvjh+lATV+VRGQX+ZgZH/i5PUSyiokFBY9duhcMmq4uQUUA8 BAVnu77lGtc= -----END CERTIFICATE-----"#; - const CLIENT_CERT_PEM: &str = r#"-----BEGIN CERTIFICATE----- + pub(crate) const CLIENT_CERT_PEM: &str = r#"-----BEGIN CERTIFICATE----- MIIC3TCCAcWgAwIBAgIJAKO/xlCxuIUKMA0GCSqGSIb3DQEBCwUAMBcxFTATBgNV BAMMDGFkYmMtdGVzdC1jYTAeFw0yNjEwMDIxMTU1NDRaFw0zNjA5MjkxMTU1NDRa MBExDzANBgNVBAMMBmNsaWVudDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoC @@ -290,7 +316,7 @@ dfKncbqZTeZgweYpf46nW5J0sMADgCx43JY7mBG61uGitReG1ROII+987wFUAFK4 Rm9QLDqbJXM9G8IpZMLt0lMaFW5t4YJATzK9VzpgsQIpJjqQ6yFGAP+eKZLlNPHT 3dpoWDnAH7z6djCo2eocDzM= -----END CERTIFICATE-----"#; - const CLIENT_KEY_PEM: &str = r#"-----BEGIN RSA PRIVATE KEY----- + pub(crate) const CLIENT_KEY_PEM: &str = r#"-----BEGIN RSA PRIVATE KEY----- MIIEpQIBAAKCAQEAxdf356jWQwvCJtt7jMSuRChcZC0VyCq8i+MtOkeDOTSLx990 pJ2mB/HjmHPljoiSywc7Ll9bVP9FKzpYMJ6SPV/gL9zp//qAAXsVXawdIjsrOy3Z CEBihvIdV/YM5hh8IvmvuN112C7Ckuc/ITQsFMVe1kgTxvStmanLIK5HRGrP4k9G @@ -391,4 +417,26 @@ O6N3BhqxCUTBtZJDOj2gZP5b1REU3YrRIbCNPX8Z29s1ZSXCRCenGUc= .unwrap(); opts.build_https_connector().unwrap(); } + + #[test] + fn declared_tls_requires_an_https_uri() { + // Nothing declared: any scheme (or none) is accepted. + TlsOptions::default().require_https(None).unwrap(); + TlsOptions::default().require_https(Some("http")).unwrap(); + + let mut opts = TlsOptions::default(); + opts.try_set(TLS_ROOT_CERTS, CA_PEM.into()).unwrap(); + opts.require_https(Some("https")).unwrap(); + + for scheme in [None, Some("http")] { + let err = opts.require_https(scheme).unwrap_err(); + assert_eq!(err.status, Status::InvalidArguments); + assert!(err.message.contains(TLS_ROOT_CERTS)); + } + + let mut opts = TlsOptions::default(); + opts.try_set(MTLS_PRIVATE_KEY, CLIENT_KEY_PEM.into()) + .unwrap(); + assert!(opts.require_https(Some("http")).is_err()); + } } From 48af5687251f056fc08b55116940796b761db513 Mon Sep 17 00:00:00 2001 From: Georg Heiler Date: Fri, 13 Nov 2026 22:48:33 +0100 Subject: [PATCH 3/3] fix(tls): permit HTTPS and retire idle connections --- Cargo.lock | 232 ++++++++++++++++++++++++++++++++++++++++++++++++++++- Cargo.toml | 1 + src/lib.rs | 97 ++++++++++++++++++++++ src/tls.rs | 4 +- 4 files changed, 330 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 830f1e3..2bfaca1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -21,6 +21,7 @@ dependencies = [ "hyper-rustls", "hyper-util", "rand 0.10.3", + "rcgen", "rustls", "serde", "serde_bytes", @@ -185,7 +186,7 @@ dependencies = [ "arrow-schema", "arrow-select", "atoi", - "base64", + "base64 0.22.1", "chrono", "half", "lexical-core", @@ -326,6 +327,45 @@ dependencies = [ "regex-syntax", ] +[[package]] +name = "asn1-rs" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b7f43a50ac4fdca5df8e885c21b835997f0a1cdee65494a6847694a98652d9d8" +dependencies = [ + "asn1-rs-derive", + "asn1-rs-impl", + "displaydoc", + "nom", + "num-traits", + "rusticata-macros", + "thiserror", + "time", +] + +[[package]] +name = "asn1-rs-derive" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3109e49b1e4909e9db6515a30c633684d68cdeaa252f215214cb4fa1a5bfee2c" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", + "synstructure 0.13.2", +] + +[[package]] +name = "asn1-rs-impl" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7b18050c2cd6fe86c3a76584ef5e0baf286d038cda203eb6223df2cc413565f7" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + [[package]] name = "atoi" version = "2.0.0" @@ -376,6 +416,21 @@ version = "0.22.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" +[[package]] +name = "base64" +version = "0.23.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac07cdecf99051d9a5238b80f35af32cdeba5b336e55d957b318b50137e18da5" + +[[package]] +name = "bit-vec" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b71798fca2c1fe1086445a7258a4bc81e6e49dcd24c8d0dd9a1e57395b603f51" +dependencies = [ + "serde", +] + [[package]] name = "bitflags" version = "2.13.2" @@ -613,6 +668,32 @@ dependencies = [ "memchr", ] +[[package]] +name = "data-encoding" +version = "2.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4583a4551df46e2792f82ceeac45e850d2e2d5debba0b91f102385cda5b11f06" + +[[package]] +name = "der-parser" +version = "10.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "07da5016415d5a3c4dd39b11ed26f915f52fc4e0dc197d87908bc916e51bc1a6" +dependencies = [ + "asn1-rs", + "displaydoc", + "nom", + "num-bigint", + "num-traits", + "rusticata-macros", +] + +[[package]] +name = "deranged" +version = "0.5.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e9de72ce2ad1f90dc62fa25f0f430ef85eb4b0d8fa0be4f30373bc40a21d28e" + [[package]] name = "displaydoc" version = "0.2.7" @@ -1088,6 +1169,12 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "lazy_static" +version = "1.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20870f649af7073d53e38067b2a84312175d56ea15217e1b15bc83506ec50afb" + [[package]] name = "lexical-core" version = "1.0.6" @@ -1197,6 +1284,12 @@ version = "2.8.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98" +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + [[package]] name = "mio" version = "1.2.4" @@ -1225,6 +1318,16 @@ dependencies = [ "tempfile", ] +[[package]] +name = "nom" +version = "7.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" +dependencies = [ + "memchr", + "minimal-lexical", +] + [[package]] name = "num-bigint" version = "0.4.8" @@ -1244,6 +1347,12 @@ dependencies = [ "num-traits", ] +[[package]] +name = "num-conv" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "521739c6d2bac4aa25192232afe6841231376b2b26d4d9fae5ecf8ca5772e441" + [[package]] name = "num-integer" version = "0.1.47" @@ -1263,6 +1372,15 @@ dependencies = [ "libm", ] +[[package]] +name = "oid-registry" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12f40cff3dde1b6087cc5d5f5d4d65712f34016a03ed60e9c08dcc392736b5b7" +dependencies = [ + "asn1-rs", +] + [[package]] name = "once_cell" version = "1.21.4" @@ -1318,6 +1436,16 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1e91099d4268b0e11973f036e885d652fb0b21fedcf69738c627f94db6a44f42" +[[package]] +name = "pem" +version = "4.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d354a98a3d1251555de99e8fdd8afda05573c31b82f59063a7b0a29b5527f120" +dependencies = [ + "base64 0.23.1", + "serde_core", +] + [[package]] name = "percent-encoding" version = "2.3.2" @@ -1345,6 +1473,12 @@ dependencies = [ "zerovec", ] +[[package]] +name = "powerfmt" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4a6394b9e965e73d0a289ee54f589087e2c676aedf60885baf52c76b771e4958" + [[package]] name = "ppv-lite86" version = "0.2.21" @@ -1430,6 +1564,20 @@ version = "0.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "63b8176103e19a2643978565ca18b50549f6101881c443590420e4dc998a3c69" +[[package]] +name = "rcgen" +version = "0.14.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8774e05a7d0de114588e6a28fe7e71694b82614ed569d86d8b389dfbc98b8ad8" +dependencies = [ + "pem", + "ring", + "rustls-pki-types", + "time", + "x509-parser", + "yasna", +] + [[package]] name = "regex" version = "1.13.1" @@ -1482,6 +1630,15 @@ dependencies = [ "semver", ] +[[package]] +name = "rusticata-macros" +version = "4.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "faf0c4a6ece9950b9abdb62b1cfcf2a68b3b67a10ba445b3bb85be2a293d0632" +dependencies = [ + "nom", +] + [[package]] name = "rustix" version = "1.1.6" @@ -1734,6 +1891,17 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "synstructure" +version = "0.13.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "728a70f3dbaf5bab7f0c4b1ac8d7ae5ea60a4b5549c8a5914361c99147a709d2" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + [[package]] name = "synstructure" version = "0.14.0" @@ -1778,6 +1946,36 @@ dependencies = [ "syn 3.0.10", ] +[[package]] +name = "time" +version = "0.3.55" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cdb87b95ec50ddfa440816d227a17b2ccbdda963a316a727fda0fc4334f7d134" +dependencies = [ + "deranged", + "num-conv", + "powerfmt", + "serde_core", + "time-core", + "time-macros", +] + +[[package]] +name = "time-core" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e1c906769ad99c88eaa54e728060edef082f8e358ff32030cb7c7d315e81109" + +[[package]] +name = "time-macros" +version = "0.2.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7e689342a48d2ea927c87ea50cabf8594854bf940e9310208848d680d668ed85" +dependencies = [ + "num-conv", + "time-core", +] + [[package]] name = "tiny-keccak" version = "2.0.2" @@ -2240,6 +2438,34 @@ version = "0.6.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3ad82d2a33cdc9674dc7465672f271e096168fcdbe0f799d9e6db8c5892679dc" +[[package]] +name = "x509-parser" +version = "0.18.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d43b0f71ce057da06bc0851b23ee24f3f86190b07203dd8f567d0b706a185202" +dependencies = [ + "asn1-rs", + "data-encoding", + "der-parser", + "lazy_static", + "nom", + "oid-registry", + "ring", + "rusticata-macros", + "thiserror", + "time", +] + +[[package]] +name = "yasna" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5f6765e852b9b4dc8e2a76843e4d64d1cea8e79bcde0b6901aea8e7c7f08282" +dependencies = [ + "bit-vec", + "time", +] + [[package]] name = "yoke" version = "0.8.4" @@ -2260,7 +2486,7 @@ dependencies = [ "proc-macro2", "quote", "syn 3.0.10", - "synstructure", + "synstructure 0.14.0", ] [[package]] @@ -2301,7 +2527,7 @@ dependencies = [ "proc-macro2", "quote", "syn 3.0.10", - "synstructure", + "synstructure 0.14.0", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index d879541..3bf661a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -115,6 +115,7 @@ name = "it" path = "tests/it/main.rs" [dev-dependencies] +rcgen = { version = "0.14", default-features = false, features = ["pem", "ring"] } arrow = "58.3.0" adbc_driver_manager = "0.23.0" diff --git a/src/lib.rs b/src/lib.rs index 524c4a5..d3a3766 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -409,6 +409,8 @@ impl Database for ClickhouseDatabase { let http = hyper_util::client::legacy::Client::builder( hyper_util::rt::TokioExecutor::new(), ) + .pool_timer(hyper_util::rt::TokioTimer::new()) + .pool_idle_timeout(std::time::Duration::from_secs(2)) .build(connector); Client::with_http_client(http) } @@ -1195,6 +1197,101 @@ mod tests { assert_ne!(err.status, Status::InvalidArguments, "{err:?}"); } + #[cfg(feature = "_tls-mtls")] + #[test] + fn a_custom_tls_connection_answers_and_retires_its_idle_socket() { + use adbc_core::Statement as _; + use std::io::{self, Read, Write}; + use std::net::TcpListener; + use std::sync::Arc; + use std::time::{Duration, Instant}; + + let pair = rcgen::generate_simple_self_signed([String::from("127.0.0.1")]).unwrap(); + let root = pair.cert.pem(); + #[cfg(feature = "rustls-tls-aws-lc")] + let provider = rustls::crypto::aws_lc_rs::default_provider(); + #[cfg(all(feature = "rustls-tls-ring", not(feature = "rustls-tls-aws-lc")))] + let provider = rustls::crypto::ring::default_provider(); + let server = rustls::ServerConfig::builder_with_provider(Arc::new(provider)) + .with_safe_default_protocol_versions() + .unwrap() + .with_no_client_auth() + .with_single_cert( + vec![pair.cert.der().clone()], + rustls::pki_types::PrivatePkcs8KeyDer::from(pair.signing_key.serialize_der()) + .into(), + ) + .unwrap(); + let listener = TcpListener::bind("127.0.0.1:0").unwrap(); + let address = listener.local_addr().unwrap(); + listener.set_nonblocking(true).unwrap(); + let peer = std::thread::spawn(move || -> io::Result<()> { + let deadline = Instant::now() + Duration::from_secs(10); + let socket = loop { + match listener.accept() { + Ok((socket, _)) => break socket, + Err(error) if error.kind() == io::ErrorKind::WouldBlock => { + if Instant::now() >= deadline { + return Err(io::ErrorKind::TimedOut.into()); + } + std::thread::sleep(Duration::from_millis(10)); + } + Err(error) => return Err(error), + } + }; + socket.set_read_timeout(Some(Duration::from_secs(8)))?; + socket.set_write_timeout(Some(Duration::from_secs(6)))?; + let connection = rustls::ServerConnection::new(Arc::new(server)).unwrap(); + let mut tls = rustls::StreamOwned::new(connection, socket); + let mut headers = Vec::new(); + while !headers.ends_with(b"\r\n\r\n") { + let mut byte = [0]; + tls.read_exact(&mut byte)?; + headers.push(byte[0]); + if headers.len() > 16 * 1024 { + return Err(io::ErrorKind::InvalidData.into()); + } + } + let mut query = [0; 8]; + tls.read_exact(&mut query)?; + if &query != b"SELECT 1" { + return Err(io::ErrorKind::InvalidData.into()); + } + tls.write_all(b"HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n")?; + tls.flush()?; + // The 8s read deadline leaves room for the client's 2s idle eviction. + match tls.read(&mut [0]) { + Ok(0) => Ok(()), + Err(error) if error.kind() == io::ErrorKind::UnexpectedEof => Ok(()), + Err(error) => Err(error), + Ok(_) => Err(io::ErrorKind::InvalidData.into()), + } + }); + let runtime = Arc::new( + tokio::runtime::Builder::new_multi_thread() + .worker_threads(1) + .enable_all() + .build() + .unwrap(), + ); + let database = ClickhouseDriver::init_with(runtime) + .new_database_with_opts([ + (OptionDatabase::Uri, format!("https://{address}").into()), + (options::TLS_ROOT_CERTS.into(), root.into()), + ]) + .unwrap(); + let mut connection = database.new_connection().unwrap(); + let mut statement = connection.new_statement().unwrap(); + statement.set_sql_query("SELECT 1").unwrap(); + let result = statement.execute_update(); + let closed = peer.join().unwrap(); + assert!(result.is_ok(), "custom TLS request failed: {result:?}"); + assert!( + closed.is_ok(), + "idle TLS socket was not retired: {closed:?}" + ); + } + #[test] fn test_set_product_info() { let mut driver = ClickhouseDriver::init(); diff --git a/src/tls.rs b/src/tls.rs index 140e8d4..e9d0b74 100644 --- a/src/tls.rs +++ b/src/tls.rs @@ -219,7 +219,9 @@ impl TlsOptions { builder.with_no_client_auth() }; - let http = HttpConnector::new(); + let mut http = HttpConnector::new(); + http.enforce_http(false); + http.set_keepalive(Some(std::time::Duration::from_secs(60))); Ok(HttpsConnectorBuilder::new() .with_tls_config(config) .https_or_http()