Skip to content

Commit 8bf5393

Browse files
authored
Merge pull request #70 from qaspen-python/feature/require_sslmode_fixes
Fixed problem with require sslmode
2 parents 2f3f5c1 + 37feaf9 commit 8bf5393

File tree

4 files changed

+48
-5
lines changed

4 files changed

+48
-5
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
}

src/driver/connection_pool_builder.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
use std::{net::IpAddr, time::Duration};
22

33
use deadpool_postgres::{Manager, ManagerConfig, Pool, RecyclingMethod};
4-
use openssl::ssl::{SslConnector, SslMethod};
4+
use openssl::ssl::{SslConnector, SslMethod, SslVerifyMode};
55
use postgres_openssl::MakeTlsConnector;
66
use pyo3::{pyclass, pymethods, Py, Python};
77
use tokio_postgres::NoTls;
88

99
use crate::exceptions::rust_errors::{RustPSQLDriverError, RustPSQLDriverPyResult};
1010

11-
use super::connection_pool::ConnectionPool;
11+
use super::{common_options, connection_pool::ConnectionPool};
1212

1313
#[pyclass]
1414
pub struct ConnectionPoolBuilder {
1515
config: tokio_postgres::Config,
1616
max_db_pool_size: Option<usize>,
1717
conn_recycling_method: Option<RecyclingMethod>,
1818
ca_file: Option<String>,
19+
ssl_mode: Option<common_options::SslMode>,
1920
}
2021

2122
#[pymethods]
@@ -28,6 +29,7 @@ impl ConnectionPoolBuilder {
2829
max_db_pool_size: Some(2),
2930
conn_recycling_method: None,
3031
ca_file: None,
32+
ssl_mode: None,
3133
}
3234
}
3335

@@ -53,6 +55,15 @@ impl ConnectionPoolBuilder {
5355
builder.set_ca_file(ca_file)?;
5456
let tls_connector = MakeTlsConnector::new(builder.build());
5557
mgr = Manager::from_config(self.config.clone(), tls_connector, mgr_config);
58+
} else if let Some(ssl_mode) = self.ssl_mode {
59+
if ssl_mode == common_options::SslMode::Require {
60+
let mut builder = SslConnector::builder(SslMethod::tls())?;
61+
builder.set_verify(SslVerifyMode::NONE);
62+
let tls_connector = MakeTlsConnector::new(builder.build());
63+
mgr = Manager::from_config(self.config.clone(), tls_connector, mgr_config);
64+
} else {
65+
mgr = Manager::from_config(self.config.clone(), NoTls, mgr_config);
66+
}
5667
} else {
5768
mgr = Manager::from_config(self.config.clone(), NoTls, mgr_config);
5869
}
@@ -167,6 +178,7 @@ impl ConnectionPoolBuilder {
167178
pub fn ssl_mode(self_: Py<Self>, ssl_mode: crate::driver::common_options::SslMode) -> Py<Self> {
168179
Python::with_gil(|gil| {
169180
let mut self_ = self_.borrow_mut(gil);
181+
self_.ssl_mode = Some(ssl_mode);
170182
self_.config.ssl_mode(ssl_mode.to_internal());
171183
});
172184
self_

0 commit comments

Comments
 (0)