The ScyllaDB Rust Driver supports encrypted connections using TLS/SSL. This is essential for production environments where you need to protect data in transit. The driver supports two TLS implementations:
OpenSSL 0.10 (feature: openssl-010)
Rustls 0.23 (feature: rustls-023)
By default, TLS is disabled. To enable it, you must configure a TlsContext on the SessionBuilder.
Here’s a complete example based on the driver’s test suite:
use anyhow::Result;use std::env;use std::fs;use std::path::PathBuf;use openssl::ssl::{SslContextBuilder, SslMethod, SslVerifyMode};use scylla::client::session::Session;use scylla::client::session_builder::SessionBuilder;#[tokio::main]async fn main() -> Result<()> { // Get the URI (default to TLS port 9142) let uri = env::var("SCYLLA_URI") .unwrap_or_else(|_| "127.0.0.1:9142".to_string()); println!("Connecting to {uri} with TLS..."); // Create SSL context let mut context_builder = SslContextBuilder::new(SslMethod::tls())?; let ca_dir = fs::canonicalize(PathBuf::from("./test/tls/ca.crt"))?; context_builder.set_ca_file(ca_dir.as_path())?; context_builder.set_verify(SslVerifyMode::PEER); // Build session with TLS let session: Session = SessionBuilder::new() .known_node(uri) .tls_context(Some(context_builder.build())) .build() .await?; // Create keyspace and table session.query_unpaged( "CREATE KEYSPACE IF NOT EXISTS examples_ks \ WITH REPLICATION = {'class': 'NetworkTopologyStrategy', 'replication_factor': 1}", &[], ).await?; session.query_unpaged( "CREATE TABLE IF NOT EXISTS examples_ks.tls \ (a int, b int, c text, primary key (a, b))", &[], ).await?; // Insert data session.query_unpaged( "INSERT INTO examples_ks.tls (a, b, c) VALUES (?, ?, ?)", (1, 2, "abc"), ).await?; println!("Successfully connected and executed queries with TLS"); Ok(())}
Here’s a complete example based on the driver’s test suite:
use std::{env, sync::Arc};use anyhow::Result;use rustls::pki_types::{CertificateDer, pem::PemObject};use scylla::client::{session::Session, session_builder::SessionBuilder};#[tokio::main]async fn main() -> Result<()> { // Get the URI (default to TLS port 9142) let uri = env::var("SCYLLA_URI") .unwrap_or_else(|_| "127.0.0.1:9142".to_string()); println!("Connecting to {uri} with TLS (Rustls)..."); // Load CA certificate let rustls_ca = CertificateDer::from_pem_file("./test/tls/ca.crt")?; let mut root_store = rustls::RootCertStore::empty(); root_store.add(rustls_ca)?; // Build session with TLS let session: Session = SessionBuilder::new() .known_node(uri) .tls_context(Some(Arc::new( rustls::ClientConfig::builder() .with_root_certificates(root_store) .with_no_client_auth(), ))) .build() .await?; // Create keyspace and table session.query_unpaged( "CREATE KEYSPACE IF NOT EXISTS examples_ks \ WITH REPLICATION = {'class': 'NetworkTopologyStrategy', 'replication_factor': 1}", &[], ).await?; session.query_unpaged( "CREATE TABLE IF NOT EXISTS examples_ks.tls \ (a int, b int, c text, primary key (a, b))", &[], ).await?; // Insert data session.query_unpaged( "INSERT INTO examples_ks.tls (a, b, c) VALUES (?, ?, ?)", (1, 2, "abc"), ).await?; println!("Successfully connected and executed queries with TLS"); Ok(())}