Skip to content

Commit

Permalink
[WIP] Add rustls support
Browse files Browse the repository at this point in the history
  • Loading branch information
jplatte committed Oct 20, 2020
1 parent 9fb37a2 commit 01ef8b1
Show file tree
Hide file tree
Showing 10 changed files with 154 additions and 14 deletions.
113 changes: 108 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ runtime-actix-native-tls = [ "sqlx-core/runtime-actix-native-tls", "sqlx-macros/
runtime-async-std-native-tls = [ "sqlx-core/runtime-async-std-native-tls", "sqlx-macros/runtime-async-std-native-tls", "_rt-async-std" ]
runtime-tokio-native-tls = [ "sqlx-core/runtime-tokio-native-tls", "sqlx-macros/runtime-tokio-native-tls", "_rt-tokio" ]

runtime-actix-rustls = [ "sqlx-core/runtime-actix-rustls", "sqlx-macros/runtime-actix-rustls", "_rt-actix" ]
runtime-async-std-rustls = [ "sqlx-core/runtime-async-std-rustls", "sqlx-macros/runtime-async-std-rustls", "_rt-async-std" ]
runtime-tokio-rustls = [ "sqlx-core/runtime-tokio-rustls", "sqlx-macros/runtime-tokio-rustls", "_rt-tokio" ]

# for conditional compilation
_rt-actix = []
_rt-async-std = []
Expand Down
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ SQLx is an async, pure Rust<sub>†</sub> SQL crate featuring compile-time check

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

* **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)).
* **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)).

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

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:
You can also select between [`native-tls`] and [`rustls`] for the TLS backend.

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:

```toml
# Cargo.toml
[dependencies]
sqlx = { version = "0.4.0-beta.1", default-features = false, features = [ "runtime-tokio-native-tls", "macros" ] }
sqlx = { version = "0.4.0-beta.1", default-features = false, features = [ "runtime-tokio-rustls", "macros" ] }
```

<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>
Expand All @@ -123,10 +125,16 @@ sqlx = { version = "0.4.0-beta.1", default-features = false, features = [ "runti

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

* `runtime-async-std-rustls`: Use the `async-std` runtime and `rustls` TLS backend.

* `runtime-tokio-native-tls`: Use the `tokio` runtime and `native-tls` TLS backend.

* `runtime-tokio-rustls`: Use the `tokio` runtime and `rustls` TLS backend.

* `runtime-actix-native-tls`: Use the `actix` runtime and `native-tls` TLS backend.

* `runtime-actix-rustls`: Use the `actix` runtime and `rustls` TLS backend.

* `postgres`: Add support for the Postgres database server.

* `mysql`: Add support for the MySQL (and MariaDB) database server.
Expand Down
4 changes: 4 additions & 0 deletions sqlx-bench/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ runtime-actix-native-tls = [ "sqlx/runtime-actix-native-tls", "sqlx-rt/runtime-a
runtime-async-std-native-tls = [ "sqlx/runtime-async-std-native-tls", "sqlx-rt/runtime-async-std-native-tls" ]
runtime-tokio-native-tls = [ "sqlx/runtime-tokio-native-tls", "sqlx-rt/runtime-tokio-native-tls" ]

runtime-actix-rustls = [ "sqlx/runtime-actix-rustls", "sqlx-rt/runtime-actix-rustls" ]
runtime-async-std-rustls = [ "sqlx/runtime-async-std-rustls", "sqlx-rt/runtime-async-std-rustls" ]
runtime-tokio-rustls = [ "sqlx/runtime-tokio-rustls", "sqlx-rt/runtime-tokio-rustls" ]

postgres = ["sqlx/postgres"]

[dependencies]
Expand Down
2 changes: 1 addition & 1 deletion sqlx-bench/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ You must choose a runtime to execute the benchmarks on; the feature flags are th

```bash
cargo bench --features runtime-tokio-native-tls
cargo bench --features runtime-async-std-native-tls
cargo bench --features runtime-async-std-rustls
```

When complete, the benchmark results will be in `target/criterion/`.
Expand Down
4 changes: 4 additions & 0 deletions sqlx-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ runtime-actix-native-tls = [ "sqlx-rt/runtime-actix-native-tls", "_rt-actix" ]
runtime-async-std-native-tls = [ "sqlx-rt/runtime-async-std-native-tls", "_rt-async-std" ]
runtime-tokio-native-tls = [ "sqlx-rt/runtime-tokio-native-tls", "_rt-tokio" ]

runtime-actix-rustls = [ "sqlx-rt/runtime-actix-rustls", "_rt-actix" ]
runtime-async-std-rustls = [ "sqlx-rt/runtime-async-std-rustls", "_rt-async-std" ]
runtime-tokio-rustls = [ "sqlx-rt/runtime-tokio-rustls", "_rt-tokio" ]

# for conditional compilation
_rt-actix = []
_rt-async-std = []
Expand Down
4 changes: 4 additions & 0 deletions sqlx-macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ runtime-actix-native-tls = [ "sqlx-core/runtime-actix-native-tls", "sqlx-rt/runt
runtime-async-std-native-tls = [ "sqlx-core/runtime-async-std-native-tls", "sqlx-rt/runtime-async-std-native-tls", "_rt-async-std" ]
runtime-tokio-native-tls = [ "sqlx-core/runtime-tokio-native-tls", "sqlx-rt/runtime-tokio-native-tls", "_rt-tokio" ]

runtime-actix-rustls = [ "sqlx-core/runtime-actix-rustls", "sqlx-rt/runtime-actix-rustls", "_rt-actix" ]
runtime-async-std-rustls = [ "sqlx-core/runtime-async-std-rustls", "sqlx-rt/runtime-async-std-rustls", "_rt-async-std" ]
runtime-tokio-rustls = [ "sqlx-core/runtime-tokio-rustls", "sqlx-rt/runtime-tokio-rustls", "_rt-tokio" ]

# for conditional compilation
_rt-actix = []
_rt-async-std = []
Expand Down
7 changes: 7 additions & 0 deletions sqlx-rt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,25 @@ runtime-actix-native-tls = [ "_rt-actix", "_tls-native-tls", "tokio-native-tls"
runtime-async-std-native-tls = [ "_rt-async-std", "_tls-native-tls", "async-native-tls" ]
runtime-tokio-native-tls = [ "_rt-tokio", "_tls-native-tls", "tokio-native-tls" ]

runtime-actix-rustls = [ "_rt-actix", "_tls-rustls", "tokio-rustls" ]
runtime-async-std-rustls = [ "_rt-async-std", "_tls-rustls", "async-rustls" ]
runtime-tokio-rustls = [ "_rt-tokio", "_tls-rustls", "tokio-rustls" ]

# Not used directly and not re-exported from sqlx
_rt-actix = [ "actix-rt", "actix-threadpool", "tokio", "once_cell" ]
_rt-async-std = [ "async-std" ]
_rt-tokio = [ "tokio", "once_cell" ]
_tls-native-tls = [ "native-tls" ]
_tls-rustls = [ ]

[dependencies]
async-native-tls = { version = "0.3.3", optional = true }
async-rustls = { version = "0.1.0", optional = true }
actix-rt = { version = "1.1.1", optional = true }
actix-threadpool = { version = "0.3.2", optional = true }
async-std = { version = "1.6.0", features = [ "unstable" ], optional = true }
tokio = { version = "0.2.21", optional = true, features = [ "blocking", "stream", "fs", "tcp", "uds", "macros", "rt-core", "rt-threaded", "time", "dns", "io-util" ] }
tokio-native-tls = { version = "0.1.0", optional = true }
tokio-rustls = { version = "0.14.0", optional = true }
native-tls = { version = "0.2.4", optional = true }
once_cell = { version = "1.4", features = ["std"], optional = true }
12 changes: 9 additions & 3 deletions sqlx-rt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,29 @@
feature = "runtime-actix-native-tls",
feature = "runtime-async-std-native-tls",
feature = "runtime-tokio-native-tls",
feature = "runtime-actix-rustls",
feature = "runtime-async-std-rustls",
feature = "runtime-tokio-rustls",
)))]
compile_error!(
"one of the features ['runtime-actix-native-tls', 'runtime-async-std-native-tls', \
'runtime-tokio-native-tls'] must be enabled"
'runtime-tokio-native-tls', 'runtime-actix-rustls', 'runtime-async-std-rustls', \
'runtime-tokio-rustls'] must be enabled"
);

#[cfg(any(
all(feature = "_rt-actix", feature = "_rt-async-std"),
all(feature = "_rt-actix", feature = "_rt-tokio"),
all(feature = "_rt-async-std", feature = "_rt-tokio"),
all(feature = "_tls-native-tls", feature = "_tls-rustls"),
))]
compile_error!(
"only one of ['runtime-actix-native-tls', 'runtime-async-std-native-tls', \
'runtime-tokio-native-tls'] can be enabled"
'runtime-tokio-native-tls', 'runtime-actix-rustls', 'runtime-async-std-rustls', \
'runtime-tokio-rustls'] can be enabled"
);

#[cfg(all(feature = "_tls-native-tls"))]
#[cfg(all(feature = "_tls-native-tls", not(feature = "_tls-rustls")))]
pub use native_tls::{self, Error as TlsError};

//
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
))]
compile_error!(
"the features 'runtime-actix', 'runtime-async-std' and 'runtime-tokio' have been removed in
favor of new features 'runtime-{rt}-{tls}' where rt is one of 'actix', 'async-std' and
'tokio'."
favor of new features 'runtime-{rt}-{tls}' where rt is one of 'actix', 'async-std' and 'tokio'
and 'tls' is one of 'native-tls' and 'rustls'."
);

pub use sqlx_core::acquire::Acquire;
Expand Down

0 comments on commit 01ef8b1

Please sign in to comment.