-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathgrpc_interceptor.rs
222 lines (197 loc) · 8.51 KB
/
grpc_interceptor.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
// SPDX-License-Identifier: MIT
use bytes::Bytes;
use core::future::Future;
use dyn_clone::DynClone;
use futures_core::task::{Context, Poll};
use http::uri::Uri;
use http_body::Body;
use log::warn;
use regex::Regex;
use std::error::Error;
use std::pin::Pin;
use tower::{Layer, Service};
// This module provides the gRPC Interceptor construct. It can be used to
// intercept gRPC calls, and examine/modify their requests and responses.
/// The gRPC header represents the gRPC call's Compress-Flag and Message-Length.
const GRPC_HEADER_LENGTH: usize = 5;
/// This is the trait that a gRPC Interceptor needs to imnplement.
pub trait GrpcInterceptor: Sync + DynClone {
/// Is this interceptor applicable?
///
/// # Arguments
/// * `service_name` - The gRPC call's service name.
/// * `method_name` - The gRPC call's method name.
fn is_applicable(&self, service_name: &str, method_name: &str) -> bool;
/// Indicates that the request must be handled.
fn must_handle_request(&self) -> bool;
/// Indicates that the response must be handled.
fn must_handle_response(&self) -> bool;
/// Handle request. Return the new new request.
///
/// # Arguments
/// * `service_name` - The gRPC call's service name.
/// * `method_name` - The gRPC call's method name.
/// * `protobuf_message_bytes` - The request's protobuf messages as bytes.
fn handle_request(
&self,
service_name: &str,
method_name: &str,
protobuf_message: Bytes,
) -> Result<Bytes, Box<dyn Error + Send + Sync>>;
/// Handle response. Return the new response.
///
/// # Arguments
/// * `service_name` - The gRPC call's service name.
/// * `method_name` - The gRPC call's method name.
/// * `protobuf_message_bytes` - The response's protobuf messages as bytes.
fn handle_response(
&self,
service_name: &str,
method_name: &str,
protobuf_message: Bytes,
) -> Result<Bytes, Box<dyn Error + Send + Sync>>;
}
// Macro that allows for clonable dynamic traits.
dyn_clone::clone_trait_object!(GrpcInterceptor);
/// The tower layer that hosts a service that hosts a gRPC Interceptor.
#[derive(Clone)]
pub struct GrpcInterceptorLayer {
interceptor: Box<dyn GrpcInterceptor + Send>,
}
impl GrpcInterceptorLayer {
/// Create the tower layer for a gRPC Interceptor.
///
/// # Arguments
/// * `interceptor` - The boxed gRPC Interceptor.
pub fn new(interceptor: Box<dyn GrpcInterceptor + Send>) -> Self {
Self { interceptor }
}
}
impl<S> Layer<S> for GrpcInterceptorLayer {
type Service = GrpcInterceptorService<S>;
fn layer(&self, service: S) -> Self::Service {
GrpcInterceptorService { service, interceptor: self.interceptor.clone() }
}
}
#[derive(Clone)]
/// The tower service that hosts a gRPC Interceptor.
pub struct GrpcInterceptorService<S> {
service: S,
interceptor: Box<dyn GrpcInterceptor + Send>,
}
impl<S> GrpcInterceptorService<S> {
/// Retrieve the gRPC service name and method name from a URI.
/// If it cannot succesfully be parsed, then an empty service name and/or method name will be returned.
///
/// * `uri` - The uri used for the gRPC call.
fn retrieve_grpc_names_from_uri(uri: &Uri) -> (String, String) {
let mut service_name = String::new();
let mut method_name = String::new();
// A gRPC URI path looks like this "/invehicle_digital_twin.InvehicleDigitalTwin/FindById".
match Regex::new(r"^/[^/\.]+\.([^/]+)/(.+)$") {
Ok(regex_pattern) => {
if let Some(caps) = regex_pattern.captures(uri.path()) {
// Note: caps.get(0) represents the entire string that matched.
// In the earlier gRPC URI path example it would be
// "/invehicle_digital_twin.InvehicleDigitalTwin/FindById".
// caps.get(1) and caps.get(2) represent the sub-parts that matched.
if caps.len() == 3 {
service_name = caps.get(1).unwrap().as_str().to_string();
method_name = caps.get(2).unwrap().as_str().to_string();
}
}
}
Err(err) => warn!("Regex pattern for gRPC names is not valid: {err}"),
}
(service_name, method_name)
}
}
impl<S> Service<http::request::Request<tonic::transport::Body>> for GrpcInterceptorService<S>
where
S: Service<
http::request::Request<tonic::transport::Body>,
Response = http::response::Response<tonic::body::BoxBody>,
Error = Box<dyn std::error::Error + Sync + Send>,
> + Send,
S::Future: Send + 'static,
{
type Response = S::Response;
type Error = S::Error;
type Future =
Pin<Box<dyn Future<Output = Result<Self::Response, Self::Error>> + Send + 'static>>;
/// Implementation of tower's Service trait's poll_ready method.
/// See https://docs.rs/tower/latest/tower/trait.Service.html
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
self.service.poll_ready(cx)
}
/// Implementation of tower's Service trait's call method.
/// See https://docs.rs/tower/latest/tower/trait.Service.html
fn call(
&mut self,
mut request: http::request::Request<tonic::transport::Body>,
) -> Self::Future {
let interceptor = self.interceptor.clone();
let (service_name, method_name) = Self::retrieve_grpc_names_from_uri(request.uri());
let is_applicable = interceptor.is_applicable(&service_name, &method_name);
if is_applicable && interceptor.must_handle_request() {
let (parts, body) = request.into_parts();
let mut body_bytes: Bytes =
match futures::executor::block_on(hyper::body::to_bytes(body)) {
Ok(bytes) => bytes,
Err(err) => {
return Box::pin(async move {
Err(Box::new(err) as Box<dyn std::error::Error + Sync + Send>)
})
}
};
let protobuf_message_bytes: Bytes = body_bytes.split_off(GRPC_HEADER_LENGTH);
let grpc_header_bytes = body_bytes;
let new_protobuf_message_bytes: Bytes = match interceptor.handle_request(
&service_name,
&method_name,
protobuf_message_bytes,
) {
Ok(bytes) => bytes,
Err(err) => return Box::pin(async move { Err(err) }),
};
let new_body_chunks: Vec<Result<_, std::io::Error>> =
vec![Ok(grpc_header_bytes), Ok(new_protobuf_message_bytes)];
let stream = futures_util::stream::iter(new_body_chunks);
let new_body = tonic::transport::Body::wrap_stream(stream);
request = http::request::Request::from_parts(parts, new_body);
}
let fut = self.service.call(request);
Box::pin(async move {
let mut response = fut.await?;
if is_applicable && interceptor.must_handle_response() {
let (parts, body) = response.into_parts();
let mut body_bytes = match hyper::body::to_bytes(body).await {
Ok(bytes) => bytes,
Err(err) => {
return Err(Box::new(err) as Box<dyn std::error::Error + Sync + Send>)
}
};
let protobuf_message_bytes = body_bytes.split_off(GRPC_HEADER_LENGTH);
let grpc_header_bytes = body_bytes;
let new_protobuf_message_bytes = match interceptor.handle_response(
&service_name,
&method_name,
protobuf_message_bytes,
) {
Ok(bytes) => bytes,
Err(err) => return Err(err),
};
let new_body_chunks: Vec<Result<_, std::io::Error>> =
vec![Ok(grpc_header_bytes), Ok(new_protobuf_message_bytes)];
let stream = futures_util::stream::iter(new_body_chunks);
let new_body = tonic::transport::Body::wrap_stream(stream);
let new_box_body =
new_body.map_err(|e| tonic::Status::from_error(Box::new(e))).boxed_unsync();
response = http::response::Response::from_parts(parts, new_box_body);
}
Ok(response)
})
}
}