Skip to content

Commit 692f85e

Browse files
committed
db: Use danger_accept_invalid_certs() if TLS is not enforced
... otherwise on CI our tests start failing due to the TLS stack complaining about a self-signed certificate in the chain.
1 parent 14bb090 commit 692f85e

File tree

3 files changed

+21
-13
lines changed

3 files changed

+21
-13
lines changed

src/app.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ impl App {
8888
};
8989

9090
let url = connection_url(&config.db, config.db.primary.url.expose_secret());
91-
let manager = AsyncDieselConnectionManager::new_with_config(url, make_manager_config());
91+
let manager_config = make_manager_config(config.db.enforce_tls);
92+
let manager = AsyncDieselConnectionManager::new_with_config(url, manager_config);
9293

9394
DeadpoolPool::builder(manager)
9495
.runtime(Runtime::Tokio1)
@@ -108,7 +109,8 @@ impl App {
108109
};
109110

110111
let url = connection_url(&config.db, pool_config.url.expose_secret());
111-
let manager = AsyncDieselConnectionManager::new_with_config(url, make_manager_config());
112+
let manager_config = make_manager_config(config.db.enforce_tls);
113+
let manager = AsyncDieselConnectionManager::new_with_config(url, manager_config);
112114

113115
let pool = DeadpoolPool::builder(manager)
114116
.runtime(Runtime::Tokio1)

src/bin/background-worker.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ fn main() -> anyhow::Result<()> {
8282
let fastly = Fastly::from_environment(client.clone());
8383
let team_repo = TeamRepoImpl::default();
8484

85-
let manager = AsyncDieselConnectionManager::new_with_config(db_url, make_manager_config());
85+
let manager_config = make_manager_config(config.db.enforce_tls);
86+
let manager = AsyncDieselConnectionManager::new_with_config(db_url, manager_config);
8687
let deadpool = Pool::builder(manager).max_size(10).build().unwrap();
8788

8889
let environment = Environment::builder()

src/db.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,30 +49,35 @@ fn maybe_append_url_param(url: &mut Url, key: &str, value: &str) {
4949

5050
/// Create a new [ManagerConfig] for the database connection pool, which can
5151
/// be used with [diesel_async::pooled_connection::AsyncDieselConnectionManager::new_with_config()].
52-
pub fn make_manager_config() -> ManagerConfig<AsyncPgConnection> {
52+
pub fn make_manager_config(enforce_tls: bool) -> ManagerConfig<AsyncPgConnection> {
5353
let mut manager_config = ManagerConfig::default();
54-
manager_config.custom_setup = Box::new(|url| Box::pin(establish_async_connection(url)));
54+
manager_config.custom_setup =
55+
Box::new(move |url| Box::pin(establish_async_connection(url, enforce_tls)));
5556
manager_config
5657
}
5758

5859
/// Establish a new database connection with the given URL.
5960
///
6061
/// Adapted from <https://github.com/weiznich/diesel_async/blob/v0.5.0/examples/postgres/pooled-with-rustls/src/main.rs>.
61-
async fn establish_async_connection(url: &str) -> ConnectionResult<AsyncPgConnection> {
62+
async fn establish_async_connection(
63+
url: &str,
64+
enforce_tls: bool,
65+
) -> ConnectionResult<AsyncPgConnection> {
6266
use diesel::ConnectionError::BadConnection;
6367

6468
let cert = Certificate::from_pem(CRUNCHY).map_err(|err| BadConnection(err.to_string()))?;
6569

6670
let connector = TlsConnector::builder()
6771
.add_root_certificate(cert)
68-
// The TLS certificate of our current database server has a long validity
69-
// period and OSX rejects such certificates as "not trusted". If you run
70-
// into "Certificate was not trusted" errors during local development,
71-
// you may consider temporarily (!) enabling the following instruction.
72+
// On OSX the native TLS stack is complaining about the long validity
73+
// period of the certificate, so if locally we don't enforce TLS
74+
// connections, we also don't enforce the validity of the certificate.
7275
//
73-
// See also https://github.com/sfackler/rust-native-tls/issues/143.
74-
//
75-
// .danger_accept_invalid_certs(true)
76+
// Similarly, on CI the native TLS stack is complaining about the
77+
// certificate being self-signed. On CI we are connecting to a locally
78+
// running database, so we also don't need to enforce the validity of
79+
// the certificate either.
80+
.danger_accept_invalid_certs(!enforce_tls)
7681
.build()
7782
.map_err(|err| BadConnection(err.to_string()))?;
7883

0 commit comments

Comments
 (0)