Skip to content

Commit 843fcf2

Browse files
committed
chore: add metadata example
Signed-off-by: Martijn Swaagman <[email protected]>
1 parent 6d88ed5 commit 843fcf2

File tree

5 files changed

+204
-0
lines changed

5 files changed

+204
-0
lines changed

examples/filter_metadata/Cargo.toml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[package]
2+
publish = false
3+
name = "proxy-wasm-example-filter-metadata"
4+
version = "0.0.1"
5+
authors = ["Martijn Swaagma <[email protected]>"]
6+
description = "Proxy-Wasm plugin example: 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"

examples/filter_metadata/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
## Proxy-Wasm plugin example: Metadata
2+
3+
Proxy-Wasm plugin that demonstrates reading metadata set by other 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 HTTP request to `localhost:10000/` with a `x-custom-metadata` header value.
21+
22+
> Not setting the header will return the configured welcome message.
23+
24+
```sh
25+
$ curl localhost:10000/ -H "x-custom-metadata: some-value"
26+
Custom response with `x-custom-metadata` value "some-value"
27+
```
Lines changed: 27 additions & 0 deletions
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: {}

examples/filter_metadata/envoy.yaml

Lines changed: 73 additions & 0 deletions
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 metadata
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", "x-custom-metadata", 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: "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_filter_metadata.wasm"
71+
- name: envoy.filters.http.router
72+
typed_config:
73+
"@type": type.googleapis.com/envoy.extensions.filters.http.router.v3.Router

examples/filter_metadata/src/lib.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
"x-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")],
41+
Some(
42+
format!(
43+
"Custom response with `x-custom-metadata` value {:?}!\n",
44+
data
45+
)
46+
.as_bytes(),
47+
),
48+
);
49+
Action::Pause
50+
}
51+
_ => Action::Continue,
52+
},
53+
_ => Action::Continue,
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)