-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Add native TLS support with tokio-rustls #3573
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
This commit adds native TLS support using tokio-rustls. The feature
is gated behind a feature flag and can be optionally enabled.
Since axum already had all the necessary connection handling logic
in place but was missing TLS acceptor functionality, this PR adds
that support by implementing a `TlsListener` wrapper for `TcpListener`
that first accepts TCP connections and then performs TLS handshakes.
# Example
Wrap `TcpListener` with `TlsListener` to allow axum to serve HTTPS
connections.
```rust
let cert = CertificateDer::from_slice(&[0]);
let key = PrivateKeyDer::from_pem_slice(&[0]).unwrap();
let config = ServerConfig::builder()
.with_no_client_auth()
.with_single_cert(vec![cert], key).unwrap();
let tcp = TcpListener::bind(("0.0.0.0", 8443)).await.unwrap();
let tls_listener = TlsListener::new(tcp, config);
let app = Router::new().route("/", routing::get(|| async { "Hello" }));
let _ = axum::serve(tls_listener, app.into_make_service());
```
|
As far for tests, there are doc tests, but I did not yet write any unit tests for the change. I'd like some pointers for direction whether it is useful to test the server TLS somehow? Should set the server up using TLS, (self signed cert & key needed ), and then fire some requests to see whether they come through. This comes with an example as well which can be found from examples folder. |
|
But I guess there is noway around this deny, since |
|
However, should that be allowed? The ISC license is comparable to MIT and is non-copyleft permissive license. Example of ISC license |
|
We've always been intentionally avoiding the complexity that comes with supporting TLS, and I don't think anything has changed. This can just be a third-party crate (just like axum-server is), no? |
This commit adds native TLS support using tokio-rustls. The feature is gated behind a feature flag and can be optionally enabled.
Since axum already had all the necessary connection handling logic in place but was missing TLS acceptor functionality, this PR adds that support by implementing a
TlsListenerwrapper forTcpListenerthat first accepts TCP connections and then performs TLS handshakes.Example
Wrap
TcpListenerwithTlsListenerto allow axum to serve HTTPS connections.Motivation
Axum has all necessary plumbing in place for handling connections but lacks the ability to serve TLS connections directly. While there are multiple examples on repository how to achieve this, I feel it should be something that is part of the library for completeness and simplified usage.
With having this functionality in axum directly there is no need for users to duplicate the loop logic to their codebase just for few lines of code.
No goals
While this commit adds support for
tokio-rustlsuses ofopensslwas not considered, but could be something to consider in future.Solution
In it's simplicity this functionality integrates to existing
axum::servefunctionality with one single line addition as shown below:let tcp = TcpListener::bind(("0.0.0.0", 8443)).await.unwrap(); +let tls_listener = TlsListener::new(tcp, config); let app = Router::new().route("/", routing::get(|| async { "Hello" })); let _ = axum::serve(tls_listener, app.into_make_service());