Skip to content

Commit 046b5b2

Browse files
committed
remove build-in tls support
1 parent 1183fe7 commit 046b5b2

File tree

10 files changed

+97
-340
lines changed

10 files changed

+97
-340
lines changed

.github/workflows/code-coverage.yml

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,6 @@ jobs:
3030
- 5432:5432
3131
# needed because the postgres container does not provide a healthcheck
3232
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
33-
postgres-tls:
34-
image: nimbustech/postgres-ssl:9.5
35-
env:
36-
POSTGRES_USER: postgres
37-
POSTGRES_PASSWORD: postgres
38-
POSTGRES_DB: postgres
39-
ports:
40-
- 5433:5432
41-
volumes:
42-
- tests/cert:/var/ssl
43-
# needed because the postgres container does not provide a healthcheck
44-
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
4533
steps:
4634
- name: Install psql
4735
run: |
@@ -63,7 +51,6 @@ jobs:
6351
args: --out Xml --all --all-features
6452
env:
6553
TEST_URL: "postgresql://localhost/postgres?user=postgres&password=postgres"
66-
TEST_TLS_URL: "postgresql://localhost:5433/postgres?user=postgres&password=postgres?sslmode=verify-ca"
6754
- name: Upload to codecov.io
6855
uses: codecov/codecov-action@v1.0.2
6956
with:

.github/workflows/stable-test.yml

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,6 @@ jobs:
3838
- 5432:5432
3939
# needed because the postgres container does not provide a healthcheck
4040
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
41-
postgres-tls:
42-
image: nimbustech/postgres-ssl:9.5
43-
env:
44-
POSTGRES_USER: postgres
45-
POSTGRES_PASSWORD: postgres
46-
POSTGRES_DB: postgres
47-
ports:
48-
- 5433:5432
49-
volumes:
50-
- tests/cert:/var/ssl
51-
# needed because the postgres container does not provide a healthcheck
52-
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
5341
steps:
5442
- name: Install psql
5543
run: |
@@ -72,4 +60,3 @@ jobs:
7260
args: --all --all-features --no-fail-fast -- --nocapture
7361
env:
7462
TEST_URL: "postgresql://localhost/postgres?user=postgres&password=postgres"
75-
TEST_TLS_URL: "postgresql://localhost:5433/postgres?user=postgres&password=postgres?sslmode=verify-ca"

Cargo.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ readme = "../README.md"
1010
keywords = ["database", "postgres", "postgresql", "sql", "async"]
1111
categories = ["database"]
1212

13+
[badges]
14+
codecov = { repository = "Hexilee/async-postgres" }
15+
16+
1317
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1418

1519
[dependencies]
@@ -18,8 +22,7 @@ tokio-postgres = { version = "0.5.2", default-features = false }
1822
tokio = "0.2.14"
1923
async-std = "1.5"
2024
futures = { version = "0.3.4", default-features = false }
21-
tokio-rustls = { version = "0.13.0", features = ["unstable"] }
22-
webpki = "0.21.2"
25+
2326

2427
[dev-dependencies]
2528
async-std = { version = "1.5", features = ["attributes"] }

src/lib.rs

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,40 @@
22
33
#![warn(missing_docs)]
44

5-
#[doc(no_inline)]
5+
pub use stream::AsyncStream;
66
pub use tokio_postgres::*;
77

8-
#[doc(inline)]
9-
pub use tls::{connect_with, Connection, TlsConfig};
10-
118
use std::io;
9+
use stream::connect_stream;
10+
use tokio_postgres::tls::{NoTls, NoTlsStream, TlsConnect};
11+
use tokio_postgres::{Client, Connection};
12+
13+
/// Connect to postgres server.
14+
///
15+
/// ```rust
16+
/// use async_postgres::connect;
17+
///
18+
/// use std::error::Error;
19+
/// use async_std::task::spawn;
20+
///
21+
/// async fn play() -> Result<(), Box<dyn Error>> {
22+
/// let url = "host=localhost user=postgres";
23+
/// let (client, conn) = connect(url.parse()?).await?;
24+
/// spawn(conn);
25+
/// let row = client.query_one("SELECT * FROM user WHERE id=$1", &[&0]).await?;
26+
/// let value: &str = row.get(0);
27+
/// println!("value: {}", value);
28+
/// Ok(())
29+
/// }
30+
/// ```
31+
#[inline]
32+
pub async fn connect(
33+
config: Config,
34+
) -> io::Result<(Client, Connection<AsyncStream, NoTlsStream>)> {
35+
connect_tls(config, NoTls).await
36+
}
1237

13-
/// Connect to postgres server with default tls config.
38+
/// Connect to postgres server with a tls connector.
1439
///
1540
/// ```rust
1641
/// use async_postgres::connect;
@@ -20,7 +45,7 @@ use std::io;
2045
///
2146
/// async fn play() -> Result<(), Box<dyn Error>> {
2247
/// let url = "host=localhost user=postgres";
23-
/// let (client, conn) = connect(&url.parse()?).await?;
48+
/// let (client, conn) = connect(url.parse()?).await?;
2449
/// spawn(conn);
2550
/// let row = client.query_one("SELECT * FROM user WHERE id=$1", &[&0]).await?;
2651
/// let value: &str = row.get(0);
@@ -29,9 +54,18 @@ use std::io;
2954
/// }
3055
/// ```
3156
#[inline]
32-
pub async fn connect(config: &Config) -> io::Result<(Client, Connection)> {
33-
connect_with(config, TlsConfig::default()).await
57+
pub async fn connect_tls<T>(
58+
config: Config,
59+
tls: T,
60+
) -> io::Result<(Client, Connection<AsyncStream, T::Stream>)>
61+
where
62+
T: TlsConnect<AsyncStream>,
63+
{
64+
let stream = connect_stream(&config).await?;
65+
config
66+
.connect_raw(stream, tls)
67+
.await
68+
.map_err(|err| io::Error::new(io::ErrorKind::Other, err))
3469
}
3570

3671
mod stream;
37-
mod tls;

src/stream.rs

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
use async_std::io::{self, Read, Write};
2+
use async_std::net::TcpStream;
23
use std::mem::MaybeUninit;
34
use std::pin::Pin;
45
use std::task::{Context, Poll};
56
use tokio::io::{AsyncRead, AsyncWrite};
7+
use tokio_postgres::config::{Config, Host};
68

7-
/// A adaptor between futures::io::{AsyncRead, AsyncWrite} and tokio::io::{AsyncRead, AsyncWrite}.
8-
pub struct AsyncStream<IO>(pub IO);
9+
/// Default port of postgres.
10+
const DEFAULT_PORT: u16 = 5432;
911

10-
impl<IO> AsyncRead for AsyncStream<IO>
11-
where
12-
IO: Unpin + Read,
13-
{
12+
/// A wrapper for async_std::net::TcpStream, implementing tokio::io::{AsyncRead, AsyncWrite}.
13+
pub struct AsyncStream(TcpStream);
14+
15+
impl AsyncRead for AsyncStream {
1416
#[inline]
1517
unsafe fn prepare_uninitialized_buffer(&self, _buf: &mut [MaybeUninit<u8>]) -> bool {
1618
false
@@ -26,10 +28,7 @@ where
2628
}
2729
}
2830

29-
impl<IO> AsyncWrite for AsyncStream<IO>
30-
where
31-
IO: Unpin + Write,
32-
{
31+
impl AsyncWrite for AsyncStream {
3332
#[inline]
3433
fn poll_write(
3534
mut self: Pin<&mut Self>,
@@ -55,3 +54,41 @@ where
5554
Pin::new(&mut self.0).poll_close(cx)
5655
}
5756
}
57+
58+
/// Establish connection to postgres server by AsyncStream.
59+
#[inline]
60+
pub async fn connect_stream(config: &Config) -> io::Result<AsyncStream> {
61+
let host = try_tcp_host(&config)?;
62+
let port = config
63+
.get_ports()
64+
.iter()
65+
.copied()
66+
.next()
67+
.unwrap_or(DEFAULT_PORT);
68+
69+
let tcp_stream = TcpStream::connect((host, port)).await?;
70+
Ok(AsyncStream(tcp_stream))
71+
}
72+
73+
/// Try to get TCP hostname from postgres config.
74+
#[inline]
75+
fn try_tcp_host(config: &Config) -> io::Result<&str> {
76+
match config
77+
.get_hosts()
78+
.iter()
79+
.filter_map(|host| {
80+
if let Host::Tcp(value) = host {
81+
Some(value)
82+
} else {
83+
None
84+
}
85+
})
86+
.next()
87+
{
88+
Some(host) => Ok(host),
89+
None => Err(io::Error::new(
90+
io::ErrorKind::Other,
91+
"At least one tcp hostname is required",
92+
)),
93+
}
94+
}

0 commit comments

Comments
 (0)