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

bump sqlx, add tls options for sqlx databases #2597

Closed
wants to merge 5 commits into from
Closed
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: 1 addition & 1 deletion contrib/db_pools/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ full usage details.
#[get("/<id>")]
async fn read(mut db: Connection<Logs>, id: i64) -> Result<Log> {
sqlx::query!("SELECT content FROM logs WHERE id = ?", id)
.fetch_one(&mut *db)
.fetch_one(&mut **db)
.map_ok(|r| Log(r.content))
.await
}
Expand Down
20 changes: 16 additions & 4 deletions contrib/db_pools/lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,23 @@ deadpool_redis = ["deadpool-redis", "deadpool"]
sqlx_mysql = ["sqlx", "sqlx/mysql"]
sqlx_postgres = ["sqlx", "sqlx/postgres"]
sqlx_sqlite = ["sqlx", "sqlx/sqlite"]
sqlx_mssql = ["sqlx", "sqlx/mssql"]
sqlx_native_tls = ["sqlx/tls-native-tls"]
sqlx_rustls = ["sqlx/tls-rustls"]
#sqlx_mssql = ["sqlx", "sqlx/mssql"] # This was removed in sqlx 0.7 - A good alternative might be deadpool+tiberius?
sqlx_macros = ["sqlx/macros"]
# diesel features
diesel_postgres = ["diesel-async/postgres", "diesel-async/deadpool", "diesel", "deadpool"]
diesel_mysql = ["diesel-async/mysql", "diesel-async/deadpool", "diesel", "deadpool"]
diesel_postgres = [
"diesel-async/postgres",
"diesel-async/deadpool",
"diesel",
"deadpool",
]
diesel_mysql = [
"diesel-async/mysql",
"diesel-async/deadpool",
"diesel",
"deadpool",
]
# implicit features: mongodb

[dependencies.rocket]
Expand Down Expand Up @@ -72,7 +84,7 @@ default-features = false
optional = true

[dependencies.sqlx]
version = "0.6"
version = "0.7"
default-features = false
features = ["runtime-tokio-rustls"]
optional = true
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: 16 additions & 15 deletions contrib/db_pools/lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
//! #[get("/<id>")]
//! async fn read(mut db: Connection<Logs>, id: i64) -> Option<Log> {
//! sqlx::query("SELECT content FROM logs WHERE id = ?").bind(id)
//! .fetch_one(&mut *db).await
//! .fetch_one(&mut **db).await
//! .and_then(|r| Ok(Log(r.try_get(0)?)))
//! .ok()
//! }
Expand Down Expand Up @@ -113,23 +113,20 @@
//! On shutdown, new connections are denied. Shutdown _does not_ wait for
//! connections to be returned.
//!
//! ## `sqlx` (v0.6)
//! ## `sqlx` (v0.7)
//!
//! | Database | Feature | [`Pool`] Type | [`Connection`] Deref |
//! |----------|-----------------|----------------------|------------------------------------------|
//! | Postgres | `sqlx_postgres` | [`sqlx::PgPool`] | [`sqlx::pool::PoolConnection<Postgres>`] |
//! | MySQL | `sqlx_mysql` | [`sqlx::MySqlPool`] | [`sqlx::pool::PoolConnection<MySql>`] |
//! | SQLite | `sqlx_sqlite` | [`sqlx::SqlitePool`] | [`sqlx::pool::PoolConnection<Sqlite>`] |
//! | MSSQL | `sqlx_mssql` | [`sqlx::MssqlPool`] | [`sqlx::pool::PoolConnection<Mssql>`] |
//!
//! [`sqlx::PgPool`]: https://docs.rs/sqlx/0.6/sqlx/type.PgPool.html
//! [`sqlx::MySqlPool`]: https://docs.rs/sqlx/0.6/sqlx/type.MySqlPool.html
//! [`sqlx::SqlitePool`]: https://docs.rs/sqlx/0.6/sqlx/type.SqlitePool.html
//! [`sqlx::MssqlPool`]: https://docs.rs/sqlx/0.6/sqlx/type.MssqlPool.html
//! [`sqlx::pool::PoolConnection<Postgres>`]: https://docs.rs/sqlx/0.6/sqlx/pool/struct.PoolConnection.html
//! [`sqlx::pool::PoolConnection<MySql>`]: https://docs.rs/sqlx/0.6/sqlx/pool/struct.PoolConnection.html
//! [`sqlx::pool::PoolConnection<Sqlite>`]: https://docs.rs/sqlx/0.6/sqlx/pool/struct.PoolConnection.html
//! [`sqlx::pool::PoolConnection<Mssql>`]: https://docs.rs/sqlx/0.6/sqlx/pool/struct.PoolConnection.html
//!
//! On shutdown, new connections are denied. Shutdown waits for connections to
//! be returned.
Expand Down Expand Up @@ -163,9 +160,9 @@
//!
//! ```toml
//! [dependencies.sqlx]
//! version = "0.6"
//! version = "0.7"
//! default-features = false
//! features = ["macros", "offline", "migrate"]
//! features = ["macros", "migrate"]
//!
//! [dependencies.rocket_db_pools]
//! version = "=0.1.0-rc.3"
Expand Down Expand Up @@ -234,7 +231,6 @@
#![doc(html_root_url = "https://api.rocket.rs/master/rocket_db_pools")]
#![doc(html_favicon_url = "https://rocket.rs/images/favicon.ico")]
#![doc(html_logo_url = "https://rocket.rs/images/logo-boxed.png")]

#![deny(missing_docs)]

pub use rocket;
Expand All @@ -243,20 +239,25 @@ pub use rocket;
#[doc(inline)]
pub use rocket::figment;

#[cfg(any(feature = "diesel_postgres", feature = "diesel_mysql"))] pub mod diesel;
#[cfg(feature = "deadpool_postgres")] pub use deadpool_postgres;
#[cfg(feature = "deadpool_redis")] pub use deadpool_redis;
#[cfg(feature = "mongodb")] pub use mongodb;
#[cfg(feature = "sqlx")] pub use sqlx;
#[cfg(any(feature = "diesel_postgres", feature = "diesel_mysql"))]
pub mod diesel;
#[cfg(feature = "deadpool_postgres")]
pub use deadpool_postgres;
#[cfg(feature = "deadpool_redis")]
pub use deadpool_redis;
#[cfg(feature = "mongodb")]
pub use mongodb;
#[cfg(feature = "sqlx")]
pub use sqlx;

mod config;
mod database;
mod error;
mod pool;
mod config;

pub use self::config::Config;
pub use self::database::{Connection, Database, Initializer};
pub use self::error::Error;
pub use self::pool::Pool;
pub use self::config::Config;

pub use rocket_db_pools_codegen::*;
69 changes: 57 additions & 12 deletions contrib/db_pools/lib/src/pool.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use rocket::figment::Figment;

#[allow(unused_imports)]
use {std::time::Duration, crate::{Error, Config}};
use {
crate::{Config, Error},
std::time::Duration,
};

/// Generic [`Database`](crate::Database) driver connection pool trait.
///
Expand Down Expand Up @@ -154,8 +157,11 @@ pub trait Pool: Sized + Send + Sync + 'static {

#[cfg(feature = "deadpool")]
mod deadpool_postgres {
use deadpool::{managed::{Manager, Pool, PoolError, Object, BuildError}, Runtime};
use super::{Duration, Error, Config, Figment};
use super::{Config, Duration, Error, Figment};
use deadpool::{
managed::{BuildError, Manager, Object, Pool, PoolError},
Runtime,
};

#[cfg(any(feature = "diesel_postgres", feature = "diesel_mysql"))]
use diesel_async::pooled_connection::AsyncDieselConnectionManager;
Expand All @@ -167,7 +173,10 @@ mod deadpool_postgres {
#[cfg(feature = "deadpool_postgres")]
impl DeadManager for deadpool_postgres::Manager {
fn new(config: &Config) -> Result<Self, Self::Error> {
Ok(Self::new(config.url.parse()?, deadpool_postgres::tokio_postgres::NoTls))
Ok(Self::new(
config.url.parse()?,
deadpool_postgres::tokio_postgres::NoTls,
))
}
}

Expand All @@ -194,7 +203,10 @@ mod deadpool_postgres {

#[rocket::async_trait]
impl<M: DeadManager, C: From<Object<M>>> crate::Pool for Pool<M, C>
where M::Type: Send, C: Send + Sync + 'static, M::Error: std::error::Error
where
M::Type: Send,
C: Send + Sync + 'static,
M::Error: std::error::Error,
{
type Error = Error<BuildError<M::Error>, PoolError<M::Error>>;

Expand Down Expand Up @@ -226,9 +238,9 @@ mod deadpool_postgres {

#[cfg(feature = "sqlx")]
mod sqlx {
use sqlx::ConnectOptions;
use super::{Duration, Error, Config, Figment};
use super::{Config, Duration, Error, Figment};
use rocket::config::LogLevel;
use sqlx::ConnectOptions;

type Options<D> = <<D as sqlx::Database>::Connection as sqlx::Connection>::Options;

Expand All @@ -240,6 +252,36 @@ 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 All @@ -253,10 +295,11 @@ mod sqlx {
let mut opts = config.url.parse::<Options<D>>().map_err(Error::Init)?;
specialize(&mut opts, &config);

opts.disable_statement_logging();
opts = opts.disable_statement_logging();
if let Ok(level) = figment.extract_inner::<LogLevel>(rocket::Config::LOG_LEVEL) {
if !matches!(level, LogLevel::Normal | LogLevel::Off) {
opts.log_statements(level.into())
opts = opts
.log_statements(level.into())
.log_slow_statements(level.into(), Duration::default());
}
}
Expand All @@ -283,8 +326,8 @@ mod sqlx {

#[cfg(feature = "mongodb")]
mod mongodb {
use mongodb::{Client, options::ClientOptions};
use super::{Duration, Error, Config, Figment};
use super::{Config, Duration, Error, Figment};
use mongodb::{options::ClientOptions, Client};

#[rocket::async_trait]
impl crate::Pool for Client {
Expand All @@ -294,7 +337,9 @@ mod mongodb {

async fn init(figment: &Figment) -> Result<Self, Self::Error> {
let config = figment.extract::<Config>()?;
let mut opts = ClientOptions::parse(&config.url).await.map_err(Error::Init)?;
let mut opts = ClientOptions::parse(&config.url)
.await
.map_err(Error::Init)?;
opts.min_pool_size = config.min_connections;
opts.max_pool_size = Some(config.max_connections as u32);
opts.max_idle_time = config.idle_timeout.map(Duration::from_secs);
Expand Down
18 changes: 3 additions & 15 deletions contrib/db_pools/lib/tests/databases.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
macro_rules! check_types_match {
($feature:expr, $name:ident, $Pool:ty, $Conn:ty $(,)?) => (
($feature:expr, $name:ident, $Pool:ty, $Conn:ty $(,)?) => {
#[cfg(feature = $feature)]
mod $name {
use rocket::*;
Expand All @@ -14,7 +14,7 @@ macro_rules! check_types_match {
let _: &$Conn = &*conn;
}
}
)
};
}

check_types_match!(
Expand Down Expand Up @@ -52,16 +52,4 @@ check_types_match!(
sqlx::pool::PoolConnection<sqlx::Sqlite>,
);

check_types_match!(
"sqlx_mssql",
sqlx_mssql,
sqlx::MssqlPool,
sqlx::pool::PoolConnection<sqlx::Mssql>,
);

check_types_match!(
"mongodb",
mongodb,
mongodb::Client,
mongodb::Client,
);
check_types_match!("mongodb", mongodb, mongodb::Client, mongodb::Client,);
4 changes: 2 additions & 2 deletions contrib/sync_db_pools/lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ diesel = { version = "2.0.0", default-features = false, optional = true }
postgres = { version = "0.19", optional = true }
r2d2_postgres = { version = "0.18", optional = true }

rusqlite = { version = "0.27.0", optional = true }
r2d2_sqlite = { version = "0.20.0", optional = true }
rusqlite = { version = "0.29.0", optional = true }
r2d2_sqlite = { version = "0.22.0", optional = true }

memcache = { version = "0.15", optional = true }
r2d2-memcache = { version = "0.6", optional = true }
Expand Down
1 change: 1 addition & 0 deletions examples/databases/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.db

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading