Skip to content

Commit 64c9fa5

Browse files
committed
Add rustls support
1 parent 62b7887 commit 64c9fa5

File tree

12 files changed

+202
-23
lines changed

12 files changed

+202
-23
lines changed

Cargo.lock

+109-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ runtime-actix-native-tls = [ "sqlx-core/runtime-actix-native-tls", "sqlx-macros/
6363
runtime-async-std-native-tls = [ "sqlx-core/runtime-async-std-native-tls", "sqlx-macros/runtime-async-std-native-tls", "_rt-async-std" ]
6464
runtime-tokio-native-tls = [ "sqlx-core/runtime-tokio-native-tls", "sqlx-macros/runtime-tokio-native-tls", "_rt-tokio" ]
6565

66+
runtime-actix-rustls = [ "sqlx-core/runtime-actix-rustls", "sqlx-macros/runtime-actix-rustls", "_rt-actix" ]
67+
runtime-async-std-rustls = [ "sqlx-core/runtime-async-std-rustls", "sqlx-macros/runtime-async-std-rustls", "_rt-async-std" ]
68+
runtime-tokio-rustls = [ "sqlx-core/runtime-tokio-rustls", "sqlx-macros/runtime-tokio-rustls", "_rt-tokio" ]
69+
6670
# for conditional compilation
6771
_rt-actix = []
6872
_rt-async-std = []

README.md

+11-3
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ SQLx is an async, pure Rust<sub>†</sub> SQL crate featuring compile-time check
6666

6767
* **Pure Rust**. The Postgres and MySQL/MariaDB drivers are written in pure Rust using **zero** unsafe<sub>††</sub> code.
6868

69-
* **Runtime Agnostic**. Works on different runtimes ([async-std](https://crates.io/crates/async-std) / [tokio](https://crates.io/crates/tokio) / [actix](https://crates.io/crates/actix-rt)).
69+
* **Runtime Agnostic**. Works on different runtimes ([async-std](https://crates.io/crates/async-std) / [tokio](https://crates.io/crates/tokio) / [actix](https://crates.io/crates/actix-rt)) and TLS backends ([native-tls](https://crates.io/crates/native-tls), [rustls](https://crates.io/crates/rustls)).
7070

7171
<sub><sup>† The SQLite driver uses the libsqlite3 C library as SQLite is an embedded database (the only way
7272
we could be pure Rust for SQLite is by porting _all_ of SQLite to Rust).</sup></sub>
@@ -109,12 +109,14 @@ SQLx is compatible with the [`async-std`], [`tokio`] and [`actix`] runtimes.
109109
[`tokio`]: https://github.com/tokio-rs/tokio
110110
[`actix`]: https://github.com/actix/actix-net
111111

112-
By default, you get `async-std`. If you want a different runtime or TLS backend, just disable the default features and activate the corresponding feature, for example for tokio:
112+
You can also select between [`native-tls`] and [`rustls`] for the TLS backend.
113+
114+
By default, you get `async-std` + `native-tls`. If you want a different runtime or TLS backend, just disable the default features and activate the corresponding feature, for example for tokio + rustls:
113115

114116
```toml
115117
# Cargo.toml
116118
[dependencies]
117-
sqlx = { version = "0.4.0-beta.1", default-features = false, features = [ "runtime-tokio-native-tls", "macros" ] }
119+
sqlx = { version = "0.4.0-beta.1", default-features = false, features = [ "runtime-tokio-rustls", "macros" ] }
118120
```
119121

120122
<sub><sup>The runtime and TLS backend not being separate feature sets to select is a workaround for a [Cargo issue](https://github.com/rust-lang/cargo/issues/3494).</sup></sub>
@@ -123,10 +125,16 @@ sqlx = { version = "0.4.0-beta.1", default-features = false, features = [ "runti
123125

124126
* `runtime-async-std-native-tls` (on by default): Use the `async-std` runtime and `native-tls` TLS backend.
125127

128+
* `runtime-async-std-rustls`: Use the `async-std` runtime and `rustls` TLS backend.
129+
126130
* `runtime-tokio-native-tls`: Use the `tokio` runtime and `native-tls` TLS backend.
127131

132+
* `runtime-tokio-rustls`: Use the `tokio` runtime and `rustls` TLS backend.
133+
128134
* `runtime-actix-native-tls`: Use the `actix` runtime and `native-tls` TLS backend.
129135

136+
* `runtime-actix-rustls`: Use the `actix` runtime and `rustls` TLS backend.
137+
130138
* `postgres`: Add support for the Postgres database server.
131139

132140
* `mysql`: Add support for the MySQL (and MariaDB) database server.

sqlx-bench/Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ runtime-actix-native-tls = [ "sqlx/runtime-actix-native-tls", "sqlx-rt/runtime-a
1010
runtime-async-std-native-tls = [ "sqlx/runtime-async-std-native-tls", "sqlx-rt/runtime-async-std-native-tls" ]
1111
runtime-tokio-native-tls = [ "sqlx/runtime-tokio-native-tls", "sqlx-rt/runtime-tokio-native-tls" ]
1212

13+
runtime-actix-rustls = [ "sqlx/runtime-actix-rustls", "sqlx-rt/runtime-actix-rustls" ]
14+
runtime-async-std-rustls = [ "sqlx/runtime-async-std-rustls", "sqlx-rt/runtime-async-std-rustls" ]
15+
runtime-tokio-rustls = [ "sqlx/runtime-tokio-rustls", "sqlx-rt/runtime-tokio-rustls" ]
16+
1317
postgres = ["sqlx/postgres"]
1418

1519
[dependencies]

sqlx-bench/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ You must choose a runtime to execute the benchmarks on; the feature flags are th
2424

2525
```bash
2626
cargo bench --features runtime-tokio-native-tls
27-
cargo bench --features runtime-async-std-native-tls
27+
cargo bench --features runtime-async-std-rustls
2828
```
2929

3030
When complete, the benchmark results will be in `target/criterion/`.

sqlx-core/Cargo.toml

+10-3
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,20 @@ decimal = [ "rust_decimal", "num-bigint" ]
3434
json = [ "serde", "serde_json" ]
3535

3636
# runtimes
37-
runtime-actix-native-tls = [ "sqlx-rt/runtime-actix-native-tls", "_rt-actix" ]
38-
runtime-async-std-native-tls = [ "sqlx-rt/runtime-async-std-native-tls", "_rt-async-std" ]
39-
runtime-tokio-native-tls = [ "sqlx-rt/runtime-tokio-native-tls", "_rt-tokio" ]
37+
runtime-actix-native-tls = [ "sqlx-rt/runtime-actix-native-tls", "_tls-native-tls", "_rt-actix" ]
38+
runtime-async-std-native-tls = [ "sqlx-rt/runtime-async-std-native-tls", "_tls-native-tls", "_rt-async-std" ]
39+
runtime-tokio-native-tls = [ "sqlx-rt/runtime-tokio-native-tls", "_tls-native-tls", "_rt-tokio" ]
40+
41+
runtime-actix-rustls = [ "sqlx-rt/runtime-actix-rustls", "_tls-rustls", "_rt-actix" ]
42+
runtime-async-std-rustls = [ "sqlx-rt/runtime-async-std-rustls", "_tls-rustls", "_rt-async-std" ]
43+
runtime-tokio-rustls = [ "sqlx-rt/runtime-tokio-rustls", "_tls-rustls", "_rt-tokio" ]
4044

4145
# for conditional compilation
4246
_rt-actix = []
4347
_rt-async-std = []
4448
_rt-tokio = []
49+
_tls-native-tls = []
50+
_tls-rustls = [ "webpki" ]
4551

4652
# support offline/decoupled building (enables serialization of `Describe`)
4753
offline = [ "serde", "either/serde" ]
@@ -98,3 +104,4 @@ uuid = { version = "0.8.1", default-features = false, optional = true, features
98104
whoami = "0.9.0"
99105
stringprep = "0.1.2"
100106
lru-cache = "0.1.2"
107+
webpki = { version = "0.21.3", optional = true }

sqlx-core/src/error.rs

+8
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,14 @@ impl From<crate::migrate::MigrateError> for Error {
240240
}
241241
}
242242

243+
#[cfg(feature = "_tls-rustls")]
244+
impl From<webpki::InvalidDNSNameError> for Error {
245+
#[inline]
246+
fn from(error: webpki::InvalidDNSNameError) -> Self {
247+
Error::Tls(Box::new(error))
248+
}
249+
}
250+
243251
// Format an error message as a `Protocol` error
244252
macro_rules! err_protocol {
245253
($expr:expr) => {

sqlx-core/src/net/tls.rs

+15-6
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ where
4444
}
4545
};
4646

47+
#[cfg(feature = "_tls-rustls")]
48+
let host = webpki::DNSNameRef::try_from_ascii_str(host)?;
49+
4750
*self = MaybeTlsStream::Tls(
4851
connector
4952
.connect(host, stream)
@@ -166,12 +169,15 @@ where
166169
match self {
167170
MaybeTlsStream::Raw(s) => s,
168171

169-
#[cfg(not(feature = "_rt-async-std"))]
170-
MaybeTlsStream::Tls(s) => s.get_ref().get_ref().get_ref(),
172+
#[cfg(feature = "_tls-rustls")]
173+
MaybeTlsStream::Tls(s) => s.get_ref().0,
171174

172-
#[cfg(feature = "_rt-async-std")]
175+
#[cfg(all(feature = "_rt-async-std", feature = "_tls-native-tls"))]
173176
MaybeTlsStream::Tls(s) => s.get_ref(),
174177

178+
#[cfg(all(not(feature = "_rt-async-std"), feature = "_tls-native-tls"))]
179+
MaybeTlsStream::Tls(s) => s.get_ref().get_ref().get_ref(),
180+
175181
MaybeTlsStream::Upgrading => panic!(io::Error::from(io::ErrorKind::ConnectionAborted)),
176182
}
177183
}
@@ -185,12 +191,15 @@ where
185191
match self {
186192
MaybeTlsStream::Raw(s) => s,
187193

188-
#[cfg(not(feature = "_rt-async-std"))]
189-
MaybeTlsStream::Tls(s) => s.get_mut().get_mut().get_mut(),
194+
#[cfg(feature = "_tls-rustls")]
195+
MaybeTlsStream::Tls(s) => s.get_mut().0,
190196

191-
#[cfg(feature = "_rt-async-std")]
197+
#[cfg(all(feature = "_rt-async-std", feature = "_tls-native-tls"))]
192198
MaybeTlsStream::Tls(s) => s.get_mut(),
193199

200+
#[cfg(all(not(feature = "_rt-async-std"), feature = "_tls-native-tls"))]
201+
MaybeTlsStream::Tls(s) => s.get_mut().get_mut().get_mut(),
202+
194203
MaybeTlsStream::Upgrading => panic!(io::Error::from(io::ErrorKind::ConnectionAborted)),
195204
}
196205
}

sqlx-macros/Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ runtime-actix-native-tls = [ "sqlx-core/runtime-actix-native-tls", "sqlx-rt/runt
2424
runtime-async-std-native-tls = [ "sqlx-core/runtime-async-std-native-tls", "sqlx-rt/runtime-async-std-native-tls", "_rt-async-std" ]
2525
runtime-tokio-native-tls = [ "sqlx-core/runtime-tokio-native-tls", "sqlx-rt/runtime-tokio-native-tls", "_rt-tokio" ]
2626

27+
runtime-actix-rustls = [ "sqlx-core/runtime-actix-rustls", "sqlx-rt/runtime-actix-rustls", "_rt-actix" ]
28+
runtime-async-std-rustls = [ "sqlx-core/runtime-async-std-rustls", "sqlx-rt/runtime-async-std-rustls", "_rt-async-std" ]
29+
runtime-tokio-rustls = [ "sqlx-core/runtime-tokio-rustls", "sqlx-rt/runtime-tokio-rustls", "_rt-tokio" ]
30+
2731
# for conditional compilation
2832
_rt-actix = []
2933
_rt-async-std = []

0 commit comments

Comments
 (0)