Skip to content

Commit 3fbdac2

Browse files
committed
dynamic_modules: add examples for network & listener modules
Signed-off-by: Rohit Agrawal <rohit.agrawal@databricks.com>
1 parent 2d4b47f commit 3fbdac2

17 files changed

Lines changed: 2677 additions & 18 deletions

ENVOY_VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
dc2d3098ae5641555f15c71d5bb5ce0060a8015c
1+
bc6c8052f90b3eda7d7bb3c5387b22cd6d98c96d

rust/Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
[package]
32
name = "envoy-proxy-dynamic-modules-rust-sdk-examples"
43
version = "0.1.0"
@@ -8,7 +7,7 @@ repository = "https://github.com/envoyproxy/dynamic-modules-example"
87

98
[dependencies]
109
# The SDK version must match the Envoy version due to the strict compatibility requirements.
11-
envoy-proxy-dynamic-modules-rust-sdk = { git = "https://github.com/envoyproxy/envoy", rev = "dc2d3098ae5641555f15c71d5bb5ce0060a8015c" }
10+
envoy-proxy-dynamic-modules-rust-sdk = { git = "https://github.com/envoyproxy/envoy", rev = "bc6c8052f90b3eda7d7bb3c5387b22cd6d98c96d" }
1211
serde = { version = "1.0", features = ["derive"] }
1312
serde_json = "1.0"
1413
rand = "0.9.0"
@@ -17,7 +16,8 @@ matchers = "0.2.0"
1716
[dev-dependencies]
1817
tempfile = "3.16.0"
1918

19+
# Main library - HTTP filters (backward compatible)
2020
[lib]
2121
name = "rust_module"
2222
path = "src/lib.rs"
23-
crate-type = ["cdylib"]
23+
crate-type = ["cdylib", "rlib"]

rust/src/http_access_logger.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ impl FilterConfig {
7777

7878
impl<EHF: EnvoyHttpFilter> HttpFilterConfig<EHF> for FilterConfig {
7979
/// This is called for each new HTTP filter.
80-
fn new_http_filter(&mut self, _envoy: &mut EHF) -> Box<dyn HttpFilter<EHF>> {
80+
fn new_http_filter(&self, _envoy: &mut EHF) -> Box<dyn HttpFilter<EHF>> {
8181
let tx = self.tx.clone();
8282
Box::new(Filter {
8383
tx,

rust/src/http_header_mutation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ impl FilterConfig {
3131

3232
impl<EHF: EnvoyHttpFilter> HttpFilterConfig<EHF> for FilterConfig {
3333
/// This is called for each new HTTP filter.
34-
fn new_http_filter(&mut self, _envoy: &mut EHF) -> Box<dyn HttpFilter<EHF>> {
34+
fn new_http_filter(&self, _envoy: &mut EHF) -> Box<dyn HttpFilter<EHF>> {
3535
Box::new(Filter {
3636
request_headers: self.request_headers.clone(),
3737
remove_request_headers: self.remove_request_headers.clone(),

rust/src/http_metrics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ impl FilterConfig {
3838

3939
impl<EHF: EnvoyHttpFilter> HttpFilterConfig<EHF> for FilterConfig {
4040
/// This is called for each new HTTP filter.
41-
fn new_http_filter(&mut self, _envoy: &mut EHF) -> Box<dyn HttpFilter<EHF>> {
41+
fn new_http_filter(&self, _envoy: &mut EHF) -> Box<dyn HttpFilter<EHF>> {
4242
Box::new(Filter {
4343
version: self.config.version.clone(),
4444
start_time: None,

rust/src/http_passthrough.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ impl FilterConfig {
2121

2222
impl<EHF: EnvoyHttpFilter> HttpFilterConfig<EHF> for FilterConfig {
2323
/// This is called for each new HTTP filter.
24-
fn new_http_filter(&mut self, _envoy: &mut EHF) -> Box<dyn HttpFilter<EHF>> {
24+
fn new_http_filter(&self, _envoy: &mut EHF) -> Box<dyn HttpFilter<EHF>> {
2525
Box::new(Filter {})
2626
}
2727
}

rust/src/http_random_auth.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ impl FilterConfig {
1818

1919
impl<EHF: EnvoyHttpFilter> HttpFilterConfig<EHF> for FilterConfig {
2020
/// This is called for each new HTTP filter.
21-
fn new_http_filter(&mut self, _envoy: &mut EHF) -> Box<dyn HttpFilter<EHF>> {
21+
fn new_http_filter(&self, _envoy: &mut EHF) -> Box<dyn HttpFilter<EHF>> {
2222
Box::new(Filter {})
2323
}
2424
}
@@ -37,7 +37,7 @@ impl<EHF: EnvoyHttpFilter> HttpFilter<EHF> for Filter {
3737
) -> abi::envoy_dynamic_module_type_on_http_filter_request_headers_status {
3838
let reject = rand::rng().random::<bool>();
3939
if reject {
40-
envoy_filter.send_response(403, vec![], Some(b"Access forbidden"));
40+
envoy_filter.send_response(403, vec![], Some(b"Access forbidden"), None);
4141
return abi::envoy_dynamic_module_type_on_http_filter_request_headers_status::StopIteration;
4242
}
4343
abi::envoy_dynamic_module_type_on_http_filter_request_headers_status::Continue

rust/src/http_zero_copy_regex_waf.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ impl FilterConfig {
3030

3131
impl<EHF: EnvoyHttpFilter> HttpFilterConfig<EHF> for FilterConfig {
3232
/// This is called for each new HTTP filter.
33-
fn new_http_filter(&mut self, _envoy: &mut EHF) -> Box<dyn HttpFilter<EHF>> {
33+
fn new_http_filter(&self, _envoy: &mut EHF) -> Box<dyn HttpFilter<EHF>> {
3434
Box::new(Filter {
3535
re: self.re.clone(),
3636
})
@@ -61,7 +61,7 @@ impl<EHF: EnvoyHttpFilter> HttpFilter<EHF> for Filter {
6161
// [`EnvoyMutBuffer`]s. Each [`EnvoyMutBuffer`] is a mutable buffer that can be
6262
// used to read the body data.
6363
let data = envoy_filter
64-
.get_request_body()
64+
.get_buffered_request_body()
6565
.expect("Failed to get request body");
6666
let mut body_reader = BodyReader::new(data);
6767
let matched = self
@@ -70,7 +70,7 @@ impl<EHF: EnvoyHttpFilter> HttpFilter<EHF> for Filter {
7070
.expect("Failed to do regex match");
7171
if matched {
7272
// If the regex matches, we send a 403 response.
73-
envoy_filter.send_response(403, vec![], Some(b"Access forbidden"));
73+
envoy_filter.send_response(403, vec![], Some(b"Access forbidden"), None);
7474
return abi::envoy_dynamic_module_type_on_http_filter_request_body_status::StopIterationNoBuffer;
7575
}
7676
abi::envoy_dynamic_module_type_on_http_filter_request_body_status::Continue
@@ -136,7 +136,7 @@ mod tests {
136136

137137
// End of stream and matching regex, so we should send a 403 response.
138138
envoy_filter
139-
.expect_get_request_body()
139+
.expect_get_buffered_request_body()
140140
.returning(|| {
141141
static mut HELLO: [u8; 6] = *b"Hello ";
142142
static mut WORLD: [u8; 6] = *b"World!";
@@ -148,14 +148,14 @@ mod tests {
148148
.times(1);
149149
envoy_filter
150150
.expect_send_response()
151-
.withf(|status, _, _| *status == 403)
152-
.returning(|_, _, _| {})
151+
.withf(|status, _, _, _| *status == 403)
152+
.returning(|_, _, _, _| {})
153153
.times(1);
154154
assert_eq!(filter.on_request_body(&mut envoy_filter, true), abi::envoy_dynamic_module_type_on_http_filter_request_body_status::StopIterationNoBuffer);
155155

156156
// End of stream and not matching regex, so we should continue.
157157
envoy_filter
158-
.expect_get_request_body()
158+
.expect_get_buffered_request_body()
159159
.returning(|| {
160160
static mut GOOD: [u8; 5] = *b"Good ";
161161
static mut MORNING: [u8; 8] = *b"Morning!";

rust/src/lib.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,65 @@
1+
//! Envoy Dynamic Modules Rust SDK Examples
2+
//!
3+
//! This crate contains example implementations of Envoy dynamic modules using the Rust SDK.
4+
//!
5+
//! # HTTP Filters
6+
//!
7+
//! The main library exports HTTP filter examples that work with `declare_init_functions!`:
8+
//! - `passthrough` - A minimal filter that passes all data through unchanged.
9+
//! - `access_logger` - Logs request/response information.
10+
//! - `random_auth` - Randomly rejects requests (for testing).
11+
//! - `zero_copy_regex_waf` - Zero-copy regex-based WAF filter.
12+
//! - `header_mutation` - Adds/removes/modifies headers.
13+
//! - `metrics` - Collects request/response metrics.
14+
//!
15+
//! # Network Filters
16+
//!
17+
//! Network filter examples are provided as reusable modules. To use them, create a separate
18+
//! crate that includes this library and uses `declare_network_filter_init_functions!` with
19+
//! the module's `new_filter_config` function.
20+
//!
21+
//! Available network filters:
22+
//! - [`network_echo`] - Echoes data back to the client.
23+
//! - [`network_rate_limiter`] - Limits concurrent connections.
24+
//! - [`network_protocol_logger`] - Logs protocol information.
25+
//! - [`network_redis`] - Redis RESP protocol parser and command filter.
26+
//!
27+
//! # Listener Filters
28+
//!
29+
//! Listener filter examples are provided as reusable modules. To use them, create a separate
30+
//! crate that includes this library and uses `declare_listener_filter_init_functions!` with
31+
//! the module's `new_filter_config` function.
32+
//!
33+
//! Available listener filters:
34+
//! - [`listener_ip_allowlist`] - IP allowlist/blocklist filter.
35+
//! - [`listener_tls_detector`] - TLS protocol detection filter.
36+
//! - [`listener_sni_router`] - SNI-based routing filter.
37+
138
use envoy_proxy_dynamic_modules_rust_sdk::*;
239

40+
// HTTP filter examples.
341
mod http_access_logger;
442
mod http_header_mutation;
543
mod http_metrics;
644
mod http_passthrough;
745
mod http_random_auth;
846
mod http_zero_copy_regex_waf;
947

48+
// Network filter examples.
49+
// These modules can be used to create standalone network filter cdylibs.
50+
// See each module's documentation for usage instructions.
51+
pub mod network_echo;
52+
pub mod network_protocol_logger;
53+
pub mod network_rate_limiter;
54+
pub mod network_redis;
55+
56+
// Listener filter examples.
57+
// These modules can be used to create standalone listener filter cdylibs.
58+
// See each module's documentation for usage instructions.
59+
pub mod listener_ip_allowlist;
60+
pub mod listener_sni_router;
61+
pub mod listener_tls_detector;
62+
1063
declare_init_functions!(init, new_http_filter_config_fn);
1164

1265
/// This implements the [`envoy_proxy_dynamic_modules_rust_sdk::ProgramInitFunction`].

0 commit comments

Comments
 (0)