feat: Add gRPC interceptors (#232)
This change introduces proper gRPC interceptors that are avilable regardless of the transport used. Each codegen service now produces an additional method called `with_interceptor` that accepts a `Interceptor`. All examples have been updated to use this new style and interop has a custom `tower::Service` middleware to echo the headers. There is also a new `interceptor` example that shows basic usage. BREAKING CHANGE: removed `interceptor_fn` and `intercep_headers_fn` from `transport` in favor of using `tonic::Interceptor`.
This commit is contained in:
+11
-8
@@ -86,27 +86,30 @@ path = "src/uds/client.rs"
|
||||
name = "uds-server"
|
||||
path = "src/uds/server.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "interceptor-client"
|
||||
path = "src/interceptor/client.rs"
|
||||
|
||||
[[bin]]
|
||||
name = "interceptor-server"
|
||||
path = "src/interceptor/server.rs"
|
||||
|
||||
[dependencies]
|
||||
tonic = { path = "../tonic", features = ["tls"] }
|
||||
prost = "0.6"
|
||||
|
||||
tokio = { version = "0.2", features = ["rt-threaded", "time", "stream", "fs", "macros", "uds"] }
|
||||
futures = { version = "0.3", default-features = false, features = ["alloc"]}
|
||||
futures = { version = "0.3", default-features = false, features = ["alloc"] }
|
||||
async-stream = "0.2"
|
||||
http = "0.2"
|
||||
tower = "0.3"
|
||||
|
||||
tower = "0.3"
|
||||
# Required for routeguide
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
rand = "0.7"
|
||||
|
||||
# Tracing
|
||||
tracing = "0.1"
|
||||
tracing-subscriber = { version = "0.2.0-alpha", features = ["tracing-log"] }
|
||||
tracing-subscriber = { version = "0.2.0-alpha", features = ["tracing-log"] }
|
||||
tracing-attributes = "0.1"
|
||||
tracing-futures = "0.2"
|
||||
|
||||
# Required for wellknown types
|
||||
prost-types = "0.6"
|
||||
|
||||
|
||||
@@ -2,23 +2,19 @@ pub mod pb {
|
||||
tonic::include_proto!("grpc.examples.echo");
|
||||
}
|
||||
|
||||
use http::header::HeaderValue;
|
||||
use pb::{echo_client::EchoClient, EchoRequest};
|
||||
use tonic::transport::Channel;
|
||||
use tonic::{metadata::MetadataValue, transport::Channel, Request};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let channel = Channel::from_static("http://[::1]:50051")
|
||||
.intercept_headers(|headers| {
|
||||
headers.insert(
|
||||
"authorization",
|
||||
HeaderValue::from_static("Bearer some-secret-token"),
|
||||
);
|
||||
})
|
||||
.connect()
|
||||
.await?;
|
||||
let channel = Channel::from_static("http://[::1]:50051").connect().await?;
|
||||
|
||||
let mut client = EchoClient::new(channel);
|
||||
let token = MetadataValue::from_str("Bearer some-auth-token")?;
|
||||
|
||||
let mut client = EchoClient::with_interceptor(channel, move |mut req: Request<()>| {
|
||||
req.metadata_mut().insert("authorization", token.clone());
|
||||
Ok(req)
|
||||
});
|
||||
|
||||
let request = tonic::Request::new(EchoRequest {
|
||||
message: "hello".into(),
|
||||
|
||||
@@ -5,8 +5,7 @@ pub mod pb {
|
||||
use futures::Stream;
|
||||
use pb::{EchoRequest, EchoResponse};
|
||||
use std::pin::Pin;
|
||||
use tonic::{body::BoxBody, transport::Server, Request, Response, Status, Streaming};
|
||||
use tower::Service;
|
||||
use tonic::{metadata::MetadataValue, transport::Server, Request, Response, Status, Streaming};
|
||||
|
||||
type EchoResult<T> = Result<Response<T>, Status>;
|
||||
type ResponseStream = Pin<Box<dyn Stream<Item = Result<EchoResponse, Status>> + Send + Sync>>;
|
||||
@@ -52,36 +51,18 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let addr = "[::1]:50051".parse().unwrap();
|
||||
let server = EchoServer::default();
|
||||
|
||||
Server::builder()
|
||||
.interceptor_fn(move |svc, req| {
|
||||
let auth_header = req.headers().get("authorization").clone();
|
||||
let svc = pb::echo_server::EchoServer::with_interceptor(server, check_auth);
|
||||
|
||||
let authed = if let Some(auth_header) = auth_header {
|
||||
auth_header == "Bearer some-secret-token"
|
||||
} else {
|
||||
false
|
||||
};
|
||||
|
||||
let fut = svc.call(req);
|
||||
|
||||
async move {
|
||||
if authed {
|
||||
fut.await
|
||||
} else {
|
||||
// Cancel the inner future since we never await it
|
||||
// the IO never gets registered.
|
||||
drop(fut);
|
||||
let res = http::Response::builder()
|
||||
.header("grpc-status", "16")
|
||||
.body(BoxBody::empty())
|
||||
.unwrap();
|
||||
Ok(res)
|
||||
}
|
||||
}
|
||||
})
|
||||
.add_service(pb::echo_server::EchoServer::new(server))
|
||||
.serve(addr)
|
||||
.await?;
|
||||
Server::builder().add_service(svc).serve(addr).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn check_auth(req: Request<()>) -> Result<Request<()>, Status> {
|
||||
let token = MetadataValue::from_str("Bearer some-secret-token").unwrap();
|
||||
|
||||
match req.metadata().get("authorization") {
|
||||
Some(t) if token == t => Ok(req),
|
||||
_ => Err(Status::unauthenticated("No valid auth token")),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,8 +3,8 @@ pub mod api {
|
||||
}
|
||||
|
||||
use api::{publisher_client::PublisherClient, ListTopicsRequest};
|
||||
use http::header::HeaderValue;
|
||||
use tonic::{
|
||||
metadata::MetadataValue,
|
||||
transport::{Certificate, Channel, ClientTlsConfig},
|
||||
Request,
|
||||
};
|
||||
@@ -23,7 +23,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
.ok_or("Expected a project name as the first argument.".to_string())?;
|
||||
|
||||
let bearer_token = format!("Bearer {}", token);
|
||||
let header_value = HeaderValue::from_str(&bearer_token)?;
|
||||
let header_value = MetadataValue::from_str(&bearer_token)?;
|
||||
|
||||
let certs = tokio::fs::read("examples/data/gcp/roots.pem").await?;
|
||||
|
||||
@@ -32,14 +32,15 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
.domain_name("pubsub.googleapis.com");
|
||||
|
||||
let channel = Channel::from_static(ENDPOINT)
|
||||
.intercept_headers(move |headers| {
|
||||
headers.insert("authorization", header_value.clone());
|
||||
})
|
||||
.tls_config(tls_config)
|
||||
.connect()
|
||||
.await?;
|
||||
|
||||
let mut service = PublisherClient::new(channel);
|
||||
let mut service = PublisherClient::with_interceptor(channel, move |mut req: Request<()>| {
|
||||
req.metadata_mut()
|
||||
.insert("authorization", header_value.clone());
|
||||
Ok(req)
|
||||
});
|
||||
|
||||
let response = service
|
||||
.list_topics(Request::new(ListTopicsRequest {
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
use hello_world::greeter_client::GreeterClient;
|
||||
use hello_world::HelloRequest;
|
||||
use tonic::{transport::Endpoint, Request, Status};
|
||||
|
||||
pub mod hello_world {
|
||||
tonic::include_proto!("helloworld");
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let channel = Endpoint::from_static("http://[::1]:50051")
|
||||
.connect()
|
||||
.await?;
|
||||
|
||||
let mut client = GreeterClient::with_interceptor(channel, intercept);
|
||||
|
||||
let request = tonic::Request::new(HelloRequest {
|
||||
name: "Tonic".into(),
|
||||
});
|
||||
|
||||
let response = client.say_hello(request).await?;
|
||||
|
||||
println!("RESPONSE={:?}", response);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// This function will get called on each outbound request. Returning a
|
||||
/// `Status` here will cancel the request and have that status returned to
|
||||
/// the caller.
|
||||
fn intercept(req: Request<()>) -> Result<Request<()>, Status> {
|
||||
println!("Intercepting request: {:?}", req);
|
||||
Ok(req)
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
use tonic::{transport::Server, Request, Response, Status};
|
||||
|
||||
use hello_world::greeter_server::{Greeter, GreeterServer};
|
||||
use hello_world::{HelloReply, HelloRequest};
|
||||
|
||||
pub mod hello_world {
|
||||
tonic::include_proto!("helloworld");
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct MyGreeter {}
|
||||
|
||||
#[tonic::async_trait]
|
||||
impl Greeter for MyGreeter {
|
||||
async fn say_hello(
|
||||
&self,
|
||||
request: Request<HelloRequest>,
|
||||
) -> Result<Response<HelloReply>, Status> {
|
||||
let reply = hello_world::HelloReply {
|
||||
message: format!("Hello {}!", request.into_inner().name),
|
||||
};
|
||||
Ok(Response::new(reply))
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let addr = "[::1]:50051".parse().unwrap();
|
||||
let greeter = MyGreeter::default();
|
||||
|
||||
let svc = GreeterServer::with_interceptor(greeter, intercept);
|
||||
|
||||
println!("GreeterServer listening on {}", addr);
|
||||
|
||||
Server::builder().add_service(svc).serve(addr).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// This function will get called on each inbound request, if a `Status`
|
||||
/// is returned, it will cancel the request and return that status to the
|
||||
/// client.
|
||||
fn intercept(req: Request<()>) -> Result<Request<()>, Status> {
|
||||
println!("Intercepting request: {:?}", req);
|
||||
Ok(req)
|
||||
}
|
||||
@@ -5,11 +5,10 @@ pub mod hello_world {
|
||||
}
|
||||
|
||||
use hello_world::{greeter_client::GreeterClient, HelloRequest};
|
||||
use http::Uri;
|
||||
use std::convert::TryFrom;
|
||||
#[cfg(unix)]
|
||||
use tokio::net::UnixStream;
|
||||
use tonic::transport::Endpoint;
|
||||
use tonic::transport::{Endpoint, Uri};
|
||||
use tower::service_fn;
|
||||
|
||||
#[cfg(unix)]
|
||||
|
||||
Reference in New Issue
Block a user