Skip to content

Commit 6f930d7

Browse files
committed
Fixed problem with require sslmode
Signed-off-by: chandr-andr (Kiselev Aleksandr) <[email protected]>
1 parent 2f3f5c1 commit 6f930d7

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

python/tests/test_ssl_mode.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,25 @@ async def test_ssl_mode_require_pool_builder(
7474
pool = builder.build()
7575

7676
await pool.execute("SELECT 1")
77+
78+
79+
async def test_ssl_mode_require_without_ca_file(
80+
postgres_host: str,
81+
postgres_user: str,
82+
postgres_password: str,
83+
postgres_port: int,
84+
postgres_dbname: str,
85+
) -> None:
86+
builder = (
87+
ConnectionPoolBuilder()
88+
.max_pool_size(10)
89+
.host(postgres_host)
90+
.port(postgres_port)
91+
.user(postgres_user)
92+
.password(postgres_password)
93+
.dbname(postgres_dbname)
94+
.ssl_mode(SslMode.Require)
95+
)
96+
pool = builder.build()
97+
98+
await pool.execute("SELECT 1")

src/driver/common_options.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl TargetSessionAttrs {
6464
}
6565

6666
#[pyclass]
67-
#[derive(Clone, Copy)]
67+
#[derive(Clone, Copy, PartialEq)]
6868
pub enum SslMode {
6969
/// Do not use TLS.
7070
Disable,

src/driver/connection_pool.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::runtime::tokio_runtime;
22
use deadpool_postgres::{Manager, ManagerConfig, Object, Pool, RecyclingMethod};
3-
use openssl::ssl::{SslConnector, SslMethod};
3+
use openssl::ssl::{SslConnector, SslMethod, SslVerifyMode};
44
use postgres_openssl::MakeTlsConnector;
55
use pyo3::{pyclass, pyfunction, pymethods, PyAny};
66
use std::{sync::Arc, vec};
@@ -13,7 +13,7 @@ use crate::{
1313
};
1414

1515
use super::{
16-
common_options::{ConnRecyclingMethod, LoadBalanceHosts, SslMode, TargetSessionAttrs},
16+
common_options::{self, ConnRecyclingMethod, LoadBalanceHosts, SslMode, TargetSessionAttrs},
1717
connection::Connection,
1818
utils::build_connection_config,
1919
};
@@ -104,6 +104,15 @@ pub fn connect(
104104
builder.set_ca_file(ca_file)?;
105105
let tls_connector = MakeTlsConnector::new(builder.build());
106106
mgr = Manager::from_config(pg_config, tls_connector, mgr_config);
107+
} else if let Some(ssl_mode) = ssl_mode {
108+
if ssl_mode == common_options::SslMode::Require {
109+
let mut builder = SslConnector::builder(SslMethod::tls())?;
110+
builder.set_verify(SslVerifyMode::NONE);
111+
let tls_connector = MakeTlsConnector::new(builder.build());
112+
mgr = Manager::from_config(pg_config, tls_connector, mgr_config);
113+
} else {
114+
mgr = Manager::from_config(pg_config, NoTls, mgr_config);
115+
}
107116
} else {
108117
mgr = Manager::from_config(pg_config, NoTls, mgr_config);
109118
}

0 commit comments

Comments
 (0)