@@ -49,30 +49,35 @@ fn maybe_append_url_param(url: &mut Url, key: &str, value: &str) {
49
49
50
50
/// Create a new [ManagerConfig] for the database connection pool, which can
51
51
/// 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 > {
53
53
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) ) ) ;
55
56
manager_config
56
57
}
57
58
58
59
/// Establish a new database connection with the given URL.
59
60
///
60
61
/// 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 > {
62
66
use diesel:: ConnectionError :: BadConnection ;
63
67
64
68
let cert = Certificate :: from_pem ( CRUNCHY ) . map_err ( |err| BadConnection ( err. to_string ( ) ) ) ?;
65
69
66
70
let connector = TlsConnector :: builder ( )
67
71
. 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.
72
75
//
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)
76
81
. build ( )
77
82
. map_err ( |err| BadConnection ( err. to_string ( ) ) ) ?;
78
83
0 commit comments