Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement TLS options for SQLx databases #2605

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,6 @@ scripts/redirect.html

# Uploads in pastebin example.
examples/pastebin/upload/*

# Editor/IDE configurations
.vscode/
2 changes: 2 additions & 0 deletions contrib/db_pools/lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ sqlx_mysql = ["sqlx", "sqlx/mysql"]
sqlx_postgres = ["sqlx", "sqlx/postgres"]
sqlx_sqlite = ["sqlx", "sqlx/sqlite"]
sqlx_macros = ["sqlx/macros"]
sqlx_native_tls = ["sqlx/tls-native-tls"]
sqlx_rustls = ["sqlx/tls-rustls"]
# diesel features
diesel_postgres = ["diesel-async/postgres", "diesel-async/deadpool", "diesel", "deadpool"]
diesel_mysql = ["diesel-async/mysql", "diesel-async/deadpool", "diesel", "deadpool"]
Expand Down
17 changes: 17 additions & 0 deletions contrib/db_pools/lib/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use rocket::serde::{Deserialize, Serialize};
use std::path::PathBuf;

/// Base configuration for all database drivers.
///
Expand Down Expand Up @@ -36,6 +37,9 @@ use rocket::serde::{Deserialize, Serialize};
/// max_connections: 1024,
/// connect_timeout: 3,
/// idle_timeout: None,
/// ssl_root_cert: None,
/// ssl_client_cert: None,
/// ssl_client_key: None
/// }));
///
/// rocket::custom(figment)
Expand Down Expand Up @@ -80,4 +84,17 @@ pub struct Config {
///
/// _Default:_ `None`.
pub idle_timeout: Option<u64>,
/// Sets the name of a file containing SSL certificate authority (CA) certificate(s).
/// If the file exists, the server’s certificate will be verified to be signed by one of these authorities.
///
/// _Default:_ `None`.
pub ssl_root_cert: Option<PathBuf>,
/// Sets the name of a file containing SSL client certificate.
///
/// _Default:_ `None`.
pub ssl_client_cert: Option<PathBuf>,
/// Sets the name of a file containing SSL client key.
///
/// _Default:_ `None`.
pub ssl_client_key: Option<PathBuf>,
}
31 changes: 31 additions & 0 deletions contrib/db_pools/lib/src/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,37 @@ mod sqlx {
.busy_timeout(Duration::from_secs(__config.connect_timeout))
.create_if_missing(true);
}

#[cfg(feature = "sqlx_postgres")]
if let Some(o) = __options.downcast_mut::<sqlx::postgres::PgConnectOptions>() {
if let Some(ref ssl_root_cert) = __config.ssl_root_cert {
*o = std::mem::take(o).ssl_root_cert(ssl_root_cert);
}

if let Some(ref ssl_client_cert) = __config.ssl_client_cert {
*o = std::mem::take(o).ssl_client_cert(ssl_client_cert);
}

if let Some(ref ssl_client_key) = __config.ssl_client_key {
*o = std::mem::take(o).ssl_client_key(ssl_client_key);
}
}

#[cfg(feature = "sqlx_mysql")]
if let Some(o) = __options.downcast_mut::<sqlx::mysql::MySqlConnectOptions>() {
if let Some(ref ssl_root_cert) = __config.ssl_root_cert {
*o = std::mem::take(o).ssl_ca(ssl_root_cert);
}

if let Some(ref ssl_client_cert) = __config.ssl_client_cert {
*o = std::mem::take(o).ssl_client_cert(ssl_client_cert);
}

if let Some(ref ssl_client_key) = __config.ssl_client_key {
*o = std::mem::take(o).ssl_client_key(ssl_client_key);
}
}

}

#[rocket::async_trait]
Expand Down
Loading