Skip to content

Commit

Permalink
Add support for using TLS with PostgreSQL (rust-db#260)
Browse files Browse the repository at this point in the history
  • Loading branch information
mossbanay committed Jan 14, 2023
1 parent fe3b8c1 commit 2e03c63
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
2 changes: 1 addition & 1 deletion refinery_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ path = "src/main.rs"

[features]
default = ["mysql", "postgresql", "sqlite-bundled", "mssql"]
postgresql = ["refinery-core/postgres"]
postgresql = ["refinery-core/postgres", "refinery-core/postgres-openssl", "refinery-core/openssl"]
mysql = ["refinery-core/mysql", "refinery-core/flate2"]
sqlite = ["refinery-core/rusqlite"]
sqlite-bundled = ["sqlite", "refinery-core/rusqlite-bundled"]
Expand Down
2 changes: 2 additions & 0 deletions refinery_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ walkdir = "2.3.1"
# allow multiple versions of the same dependency if API is similar
rusqlite = { version = ">= 0.23, <= 0.28", optional = true }
postgres = { version = "0.19", optional = true }
postgres-openssl = { version = "0.5", optional = true }
openssl = { version = "0.10", optional = true }
tokio-postgres = { version = "0.7", optional = true }
mysql = { version = ">= 21.0.0, <= 23", optional = true, default-features = false}
mysql_async = { version = ">= 0.28, <= 0.30", optional = true }
Expand Down
27 changes: 27 additions & 0 deletions refinery_core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ impl Config {
db_user: None,
db_pass: None,
db_name: None,
use_tls: false,
#[cfg(feature = "tiberius-config")]
trust_cert: false,
},
Expand Down Expand Up @@ -138,6 +139,10 @@ impl Config {
self.main.db_port.as_deref()
}

pub fn use_tls(&self) -> bool {
self.main.use_tls
}

pub fn set_db_user(self, db_user: &str) -> Config {
Config {
main: Main {
Expand Down Expand Up @@ -182,6 +187,15 @@ impl Config {
},
}
}

pub fn set_use_tls(self, use_tls: bool) -> Config {
Config {
main: Main {
use_tls: use_tls,
..self.main
},
}
}
}

impl TryFrom<Url> for Config {
Expand Down Expand Up @@ -219,6 +233,17 @@ impl TryFrom<Url> for Config {
None,
)
})?;

let use_tls = query_params.
get("sslmode")
.unwrap_or(&Cow::Borrowed("false"))
.parse::<bool>()
.map_err(|_| {
Error::new(
Kind::ConfigError("Invalid sslmode value, please use true/false".into()),
None,
)
})?;
}
}

Expand All @@ -237,6 +262,7 @@ impl TryFrom<Url> for Config {
db_user: Some(url.username().to_string()),
db_pass: url.password().map(|r| r.to_string()),
db_name: Some(url.path().trim_start_matches('/').to_string()),
use_tls,
#[cfg(feature = "tiberius-config")]
trust_cert,
},
Expand Down Expand Up @@ -268,6 +294,7 @@ struct Main {
db_user: Option<String>,
db_pass: Option<String>,
db_name: Option<String>,
use_tls: bool,
#[cfg(feature = "tiberius-config")]
#[serde(default)]
trust_cert: bool,
Expand Down
11 changes: 10 additions & 1 deletion refinery_core/src/drivers/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,16 @@ macro_rules! with_connection {
cfg_if::cfg_if! {
if #[cfg(feature = "postgres")] {
let path = build_db_url("postgresql", &$config);
let conn = postgres::Client::connect(path.as_str(), postgres::NoTls).migration_err("could not connect to database", None)?;

let conn;
if $config.use_tls() {
let builder = openssl::ssl::SslConnector::builder(openssl::ssl::SslMethod::tls()).unwrap();
let connector = postgres_openssl::MakeTlsConnector::new(builder.build());
conn = postgres::Client::connect(path.as_str(), connector).migration_err("could not connect to database", None)?;
} else {
conn = postgres::Client::connect(path.as_str(), postgres::NoTls).migration_err("could not connect to database", None)?;
}

$op(conn)
} else {
panic!("tried to migrate from config for a postgresql database, but feature postgres not enabled!");
Expand Down

0 comments on commit 2e03c63

Please sign in to comment.