Skip to content

Commit 2ee96ce

Browse files
committed
implement TLS options for SQLx databases
1 parent 26a3f00 commit 2ee96ce

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,6 @@ scripts/redirect.html
3131

3232
# Uploads in pastebin example.
3333
examples/pastebin/upload/*
34+
35+
# Editor/IDE configurations
36+
.vscode/

contrib/db_pools/lib/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ sqlx_mysql = ["sqlx", "sqlx/mysql"]
2222
sqlx_postgres = ["sqlx", "sqlx/postgres"]
2323
sqlx_sqlite = ["sqlx", "sqlx/sqlite"]
2424
sqlx_macros = ["sqlx/macros"]
25+
sqlx_native_tls = ["sqlx/tls-native-tls"]
26+
sqlx_rustls = ["sqlx/tls-rustls"]
2527
# diesel features
2628
diesel_postgres = ["diesel-async/postgres", "diesel-async/deadpool", "diesel", "deadpool"]
2729
diesel_mysql = ["diesel-async/mysql", "diesel-async/deadpool", "diesel", "deadpool"]

contrib/db_pools/lib/src/config.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use rocket::serde::{Deserialize, Serialize};
2+
use std::path::PathBuf;
23

34
/// Base configuration for all database drivers.
45
///
@@ -36,6 +37,9 @@ use rocket::serde::{Deserialize, Serialize};
3637
/// max_connections: 1024,
3738
/// connect_timeout: 3,
3839
/// idle_timeout: None,
40+
/// ssl_root_cert: None,
41+
/// ssl_client_cert: None,
42+
/// ssl_client_key: None
3943
/// }));
4044
///
4145
/// rocket::custom(figment)
@@ -80,4 +84,17 @@ pub struct Config {
8084
///
8185
/// _Default:_ `None`.
8286
pub idle_timeout: Option<u64>,
87+
/// Sets the name of a file containing SSL certificate authority (CA) certificate(s).
88+
/// If the file exists, the server’s certificate will be verified to be signed by one of these authorities.
89+
///
90+
/// _Default:_ `None`.
91+
pub ssl_root_cert: Option<PathBuf>,
92+
/// Sets the name of a file containing SSL client certificate.
93+
///
94+
/// _Default:_ `None`.
95+
pub ssl_client_cert: Option<PathBuf>,
96+
/// Sets the name of a file containing SSL client key.
97+
///
98+
/// _Default:_ `None`.
99+
pub ssl_client_key: Option<PathBuf>,
83100
}

contrib/db_pools/lib/src/pool.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,37 @@ mod sqlx {
240240
.busy_timeout(Duration::from_secs(__config.connect_timeout))
241241
.create_if_missing(true);
242242
}
243+
244+
#[cfg(feature = "sqlx_postgres")]
245+
if let Some(o) = __options.downcast_mut::<sqlx::postgres::PgConnectOptions>() {
246+
if let Some(ref ssl_root_cert) = __config.ssl_root_cert {
247+
*o = std::mem::take(o).ssl_root_cert(ssl_root_cert);
248+
}
249+
250+
if let Some(ref ssl_client_cert) = __config.ssl_client_cert {
251+
*o = std::mem::take(o).ssl_client_cert(ssl_client_cert);
252+
}
253+
254+
if let Some(ref ssl_client_key) = __config.ssl_client_key {
255+
*o = std::mem::take(o).ssl_client_key(ssl_client_key);
256+
}
257+
}
258+
259+
#[cfg(feature = "sqlx_mysql")]
260+
if let Some(o) = __options.downcast_mut::<sqlx::mysql::MySqlConnectOptions>() {
261+
if let Some(ref ssl_root_cert) = __config.ssl_root_cert {
262+
*o = std::mem::take(o).ssl_ca(ssl_root_cert);
263+
}
264+
265+
if let Some(ref ssl_client_cert) = __config.ssl_client_cert {
266+
*o = std::mem::take(o).ssl_client_cert(ssl_client_cert);
267+
}
268+
269+
if let Some(ref ssl_client_key) = __config.ssl_client_key {
270+
*o = std::mem::take(o).ssl_client_key(ssl_client_key);
271+
}
272+
}
273+
243274
}
244275

245276
#[rocket::async_trait]

0 commit comments

Comments
 (0)