feat(transport): Expose more granular control of TLS configuration (#48)
This commit reworks TLS configuration of both servers and endpoints in order to provide a more flexible API. We now add options to configure the selected TLS library using the appropriate 'native' configuration structures, as well as retaining the existing simplier interface which is compatible with both. The new API can also be easily extended to support simple interfaces for configuring mTLS and a range of other options without creating sprawl in the builders for `Server` and `Endpoint`.
This commit is contained in:
committed by
Lucio Franco
parent
4628ff0258
commit
8db3961491
@@ -3,19 +3,23 @@ pub mod pb {
|
||||
}
|
||||
|
||||
use pb::{client::EchoClient, EchoRequest};
|
||||
use tonic::transport::{Certificate, Channel};
|
||||
use tonic::transport::{Certificate, Channel, ClientTlsConfig};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let pem = tokio::fs::read("tonic-examples/data/tls/ca.pem").await?;
|
||||
let ca = Certificate::from_pem(pem);
|
||||
|
||||
let tls = ClientTlsConfig::with_rustls()
|
||||
.ca_certificate(ca)
|
||||
.domain_name("example.com")
|
||||
.clone();
|
||||
|
||||
let channel = Channel::from_static("http://[::1]:50051")
|
||||
.rustls_tls(ca, Some("example.com".into()))
|
||||
.tls_config(&tls)
|
||||
.channel();
|
||||
|
||||
let mut client = EchoClient::new(channel);
|
||||
|
||||
let request = tonic::Request::new(EchoRequest {
|
||||
message: "hello".into(),
|
||||
});
|
||||
|
||||
@@ -5,7 +5,7 @@ pub mod pb {
|
||||
use pb::{EchoRequest, EchoResponse};
|
||||
use std::collections::VecDeque;
|
||||
use tonic::{
|
||||
transport::{Identity, Server},
|
||||
transport::{Identity, Server, ServerTlsConfig},
|
||||
Request, Response, Status, Streaming,
|
||||
};
|
||||
|
||||
@@ -59,7 +59,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let server = EchoServer::default();
|
||||
|
||||
Server::builder()
|
||||
.rustls_tls(identity)
|
||||
.tls_config(ServerTlsConfig::with_rustls().identity(identity))
|
||||
.clone()
|
||||
.serve(addr, pb::server::EchoServer::new(server))
|
||||
.await?;
|
||||
|
||||
Reference in New Issue
Block a user