Skip to content

Commit f14c4f6

Browse files
authored
Add Envoy filter metadata example. (#289)
Signed-off-by: Martijn Swaagman <[email protected]>
1 parent c502d38 commit f14c4f6

File tree

7 files changed

+211
-0
lines changed

7 files changed

+211
-0
lines changed

.github/workflows/rust.yml

+2
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ jobs:
384384
- 'http_config'
385385
- 'http_headers'
386386
- 'grpc_auth_random'
387+
- 'envoy_filter_metadata'
387388

388389
defaults:
389390
run:
@@ -456,6 +457,7 @@ jobs:
456457
- 'http_config'
457458
- 'http_headers'
458459
- 'grpc_auth_random'
460+
- 'envoy_filter_metadata'
459461

460462
defaults:
461463
run:

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
- [HTTP Response body](./examples/http_body/)
2323
- [HTTP Configuration](./examples/http_config/)
2424
- [gRPC Auth (random)](./examples/grpc_auth_random/)
25+
- [Envoy filter metadata](./examples/envoy_filter_metadata/)
2526

2627
## Articles & blog posts from the community
2728

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[package]
2+
publish = false
3+
name = "proxy-wasm-example-envoy-filter-metadata"
4+
version = "0.0.1"
5+
authors = ["Martijn Swaagma <[email protected]>"]
6+
description = "Proxy-Wasm plugin example: Envoy filter metadata"
7+
license = "Apache-2.0"
8+
edition = "2018"
9+
10+
[lib]
11+
crate-type = ["cdylib"]
12+
13+
[dependencies]
14+
proxy-wasm = { path = "../../" }
15+
16+
[profile.release]
17+
lto = true
18+
opt-level = 3
19+
codegen-units = 1
20+
panic = "abort"
21+
strip = "debuginfo"
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
## Proxy-Wasm plugin example: Envoy metadata
2+
3+
Proxy-Wasm plugin that demonstrates reading metadata set by other Envoy filters.
4+
5+
### Building
6+
7+
```sh
8+
$ cargo build --target wasm32-wasip1 --release
9+
```
10+
11+
### Using in Envoy
12+
13+
This example can be run with [`docker compose`](https://docs.docker.com/compose/install/)
14+
and has a matching Envoy configuration.
15+
16+
```sh
17+
$ docker compose up
18+
```
19+
20+
Send a HTTP request to `localhost:10000` that will return the configured response.
21+
22+
```sh
23+
$ curl localhost:10000
24+
Welcome, set the `x-custom-metadata` header to change the response!
25+
```
26+
27+
Send a HTTP request to `localhost:10000` with a `x-custom-metadata` header value to get
28+
the uppercased value in the response.
29+
30+
The response will also contain a response header `uppercased-metadata: SOME-VALUE`.
31+
32+
```sh
33+
$ curl localhost:10000 -H "x-custom-metadata: some-value"
34+
Custom response with Envoy metadata: "SOME-VALUE"
35+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
services:
16+
envoy:
17+
image: envoyproxy/envoy:v1.31-latest
18+
hostname: envoy
19+
ports:
20+
- "10000:10000"
21+
volumes:
22+
- ./envoy.yaml:/etc/envoy/envoy.yaml
23+
- ./target/wasm32-wasip1/release:/etc/envoy/proxy-wasm-plugins
24+
networks:
25+
- envoymesh
26+
networks:
27+
envoymesh: {}
+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
static_resources:
16+
listeners:
17+
address:
18+
socket_address:
19+
address: 0.0.0.0
20+
port_value: 10000
21+
filter_chains:
22+
- filters:
23+
- name: envoy.filters.network.http_connection_manager
24+
typed_config:
25+
"@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
26+
stat_prefix: ingress_http
27+
codec_type: AUTO
28+
route_config:
29+
name: local_routes
30+
virtual_hosts:
31+
- name: local_service
32+
domains:
33+
- "*"
34+
routes:
35+
- match:
36+
prefix: "/"
37+
direct_response:
38+
status: 200
39+
body:
40+
inline_string: "Welcome, set the `x-custom-metadata` header to change the response!\n"
41+
http_filters:
42+
# Set uppercase metadata in Lua filter
43+
- name: envoy.filters.http.lua
44+
typed_config:
45+
"@type": type.googleapis.com/envoy.extensions.filters.http.lua.v3.Lua
46+
default_source_code:
47+
inline_string: |
48+
function envoy_on_request(request_handle)
49+
local headers = request_handle:headers()
50+
local data = headers:get("x-custom-metadata")
51+
52+
if data then
53+
request_handle:streamInfo():dynamicMetadata():set("envoy.filters.http.lua", "uppercased-custom-metadata", string.upper(data))
54+
end
55+
56+
request_handle:logInfo("Metadata set by lua filter")
57+
end
58+
# Read it from a WASM filter
59+
- name: envoy.filters.http.wasm
60+
typed_config:
61+
"@type": type.googleapis.com/udpa.type.v1.TypedStruct
62+
type_url: type.googleapis.com/envoy.extensions.filters.http.wasm.v3.Wasm
63+
value:
64+
config:
65+
name: "envoy_metadata_filter"
66+
vm_config:
67+
runtime: "envoy.wasm.runtime.v8"
68+
code:
69+
local:
70+
filename: "/etc/envoy/proxy-wasm-plugins/proxy_wasm_example_envoy_filter_metadata.wasm"
71+
- name: envoy.filters.http.router
72+
typed_config:
73+
"@type": type.googleapis.com/envoy.extensions.filters.http.router.v3.Router
+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
use proxy_wasm::traits::*;
16+
use proxy_wasm::types::*;
17+
18+
proxy_wasm::main! {{
19+
proxy_wasm::set_log_level(LogLevel::Trace);
20+
proxy_wasm::set_http_context(|_, _| -> Box<dyn HttpContext> { Box::new(MetadataHttp {}) });
21+
}}
22+
23+
struct MetadataHttp {}
24+
25+
impl Context for MetadataHttp {}
26+
27+
impl HttpContext for MetadataHttp {
28+
fn on_http_request_headers(&mut self, _: usize, _: bool) -> Action {
29+
// Read data set by the lua filter
30+
match self.get_property(vec![
31+
"metadata",
32+
"filter_metadata",
33+
"envoy.filters.http.lua",
34+
"uppercased-custom-metadata",
35+
]) {
36+
Some(metadata) => match String::from_utf8(metadata) {
37+
Ok(data) => {
38+
self.send_http_response(
39+
200,
40+
vec![("Powered-By", "proxy-wasm"), ("uppercased-metadata", &data)],
41+
Some(
42+
format!("Custom response with Envoy metadata: {:?}\n", data).as_bytes(),
43+
),
44+
);
45+
Action::Pause
46+
}
47+
_ => Action::Continue,
48+
},
49+
_ => Action::Continue,
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)