Skip to content

Commit f7a2644

Browse files
committed
align hostaddr tls behavior with documentation
1 parent d16a9cd commit f7a2644

File tree

8 files changed

+33
-28
lines changed

8 files changed

+33
-28
lines changed

tokio-postgres/src/cancel_query.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::client::SocketConfig;
2-
use crate::config::{Host, SslMode};
2+
use crate::config::SslMode;
33
use crate::tls::MakeTlsConnect;
44
use crate::{cancel_query_raw, connect_socket, Error, Socket};
55
use std::io;
@@ -24,14 +24,10 @@ where
2424
}
2525
};
2626

27-
let hostname = match &config.host {
28-
Host::Tcp(host) => &**host,
29-
// postgres doesn't support TLS over unix sockets, so the choice here doesn't matter
30-
#[cfg(unix)]
31-
Host::Unix(_) => "",
32-
};
33-
let tls = tls
34-
.make_tls_connect(hostname)
27+
let tls = config
28+
.hostname
29+
.map(|s| tls.make_tls_connect(&s))
30+
.transpose()
3531
.map_err(|e| Error::tls(e.into()))?;
3632

3733
let socket = connect_socket::connect_socket(

tokio-postgres/src/cancel_query_raw.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use tokio::io::{AsyncRead, AsyncWrite, AsyncWriteExt};
88
pub async fn cancel_query_raw<S, T>(
99
stream: S,
1010
mode: SslMode,
11-
tls: T,
11+
tls: Option<T>,
1212
process_id: i32,
1313
secret_key: i32,
1414
) -> Result<(), Error>

tokio-postgres/src/cancel_token.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ impl CancelToken {
5454
cancel_query_raw::cancel_query_raw(
5555
stream,
5656
self.ssl_mode,
57-
tls,
57+
Some(tls),
5858
self.process_id,
5959
self.secret_key,
6060
)

tokio-postgres/src/client.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ impl InnerClient {
154154
#[derive(Clone)]
155155
pub(crate) struct SocketConfig {
156156
pub host: Host,
157+
pub hostname: Option<String>,
157158
pub port: u16,
158159
pub connect_timeout: Option<Duration>,
159160
pub tcp_user_timeout: Option<Duration>,

tokio-postgres/src/config.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ pub enum Host {
9797
/// * `hostaddr` - Numeric IP address of host to connect to. This should be in the standard IPv4 address format,
9898
/// e.g., 172.28.40.9. If your machine supports IPv6, you can also use those addresses.
9999
/// If this parameter is not specified, the value of `host` will be looked up to find the corresponding IP address,
100-
/// - or if host specifies an IP address, that value will be used directly.
100+
/// or if host specifies an IP address, that value will be used directly.
101101
/// Using `hostaddr` allows the application to avoid a host name look-up, which might be important in applications
102-
/// with time constraints. However, a host name is required for verify-full SSL certificate verification.
102+
/// with time constraints. However, a host name is required for TLS certificate verification.
103103
/// Specifically:
104104
/// * If `hostaddr` is specified without `host`, the value for `hostaddr` gives the server network address.
105105
/// The connection attempt will fail if the authentication method requires a host name;
@@ -645,7 +645,7 @@ impl Config {
645645
S: AsyncRead + AsyncWrite + Unpin,
646646
T: TlsConnect<S>,
647647
{
648-
connect_raw(stream, tls, self).await
648+
connect_raw(stream, Some(tls), self).await
649649
}
650650
}
651651

tokio-postgres/src/connect.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,17 @@ where
5252
.unwrap_or(5432);
5353

5454
// The value of host is used as the hostname for TLS validation,
55-
// if it's not present, use the value of hostaddr.
5655
let hostname = match host {
57-
Some(Host::Tcp(host)) => host.clone(),
56+
Some(Host::Tcp(host)) => Some(host.clone()),
5857
// postgres doesn't support TLS over unix sockets, so the choice here doesn't matter
5958
#[cfg(unix)]
60-
Some(Host::Unix(_)) => "".to_string(),
61-
None => hostaddr.map_or("".to_string(), |ipaddr| ipaddr.to_string()),
59+
Some(Host::Unix(_)) => None,
60+
None => None,
6261
};
63-
let tls = tls
64-
.make_tls_connect(&hostname)
62+
let tls = hostname
63+
.as_ref()
64+
.map(|s| tls.make_tls_connect(s))
65+
.transpose()
6566
.map_err(|e| Error::tls(e.into()))?;
6667

6768
// Try to use the value of hostaddr to establish the TCP connection,
@@ -78,7 +79,7 @@ where
7879
}
7980
};
8081

81-
match connect_once(&addr, port, tls, config).await {
82+
match connect_once(addr, hostname, port, tls, config).await {
8283
Ok((client, connection)) => return Ok((client, connection)),
8384
Err(e) => error = Some(e),
8485
}
@@ -88,16 +89,17 @@ where
8889
}
8990

9091
async fn connect_once<T>(
91-
host: &Host,
92+
host: Host,
93+
hostname: Option<String>,
9294
port: u16,
93-
tls: T,
95+
tls: Option<T>,
9496
config: &Config,
9597
) -> Result<(Client, Connection<Socket, T::Stream>), Error>
9698
where
9799
T: TlsConnect<Socket>,
98100
{
99101
let socket = connect_socket(
100-
host,
102+
&host,
101103
port,
102104
config.connect_timeout,
103105
config.tcp_user_timeout,
@@ -151,7 +153,8 @@ where
151153
}
152154

153155
client.set_socket_config(SocketConfig {
154-
host: host.clone(),
156+
host,
157+
hostname,
155158
port,
156159
connect_timeout: config.connect_timeout,
157160
tcp_user_timeout: config.tcp_user_timeout,

tokio-postgres/src/connect_raw.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ where
8080

8181
pub async fn connect_raw<S, T>(
8282
stream: S,
83-
tls: T,
83+
tls: Option<T>,
8484
config: &Config,
8585
) -> Result<(Client, Connection<S, T::Stream>), Error>
8686
where

tokio-postgres/src/connect_tls.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,19 @@ use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
1010
pub async fn connect_tls<S, T>(
1111
mut stream: S,
1212
mode: SslMode,
13-
tls: T,
13+
tls: Option<T>,
1414
) -> Result<MaybeTlsStream<S, T::Stream>, Error>
1515
where
1616
S: AsyncRead + AsyncWrite + Unpin,
1717
T: TlsConnect<S>,
1818
{
1919
match mode {
2020
SslMode::Disable => return Ok(MaybeTlsStream::Raw(stream)),
21-
SslMode::Prefer if !tls.can_connect(ForcePrivateApi) => {
21+
SslMode::Prefer
22+
if tls
23+
.as_ref()
24+
.map_or(false, |tls| !tls.can_connect(ForcePrivateApi)) =>
25+
{
2226
return Ok(MaybeTlsStream::Raw(stream))
2327
}
2428
SslMode::Prefer | SslMode::Require => {}
@@ -40,6 +44,7 @@ where
4044
}
4145

4246
let stream = tls
47+
.ok_or_else(|| Error::tls("no hostname provided for TLS handshake".into()))?
4348
.connect(stream)
4449
.await
4550
.map_err(|e| Error::tls(e.into()))?;

0 commit comments

Comments
 (0)