From 5d7089c41f1390dd16cc655fb7660f17c8fcff88 Mon Sep 17 00:00:00 2001 From: Martijn Swaagman Date: Mon, 4 Nov 2024 12:51:15 +0100 Subject: [PATCH 1/8] chore: add metadata example Signed-off-by: Martijn Swaagman --- examples/filter_metadata/Cargo.toml | 21 ++++++ examples/filter_metadata/README.md | 27 ++++++++ examples/filter_metadata/docker-compose.yaml | 27 ++++++++ examples/filter_metadata/envoy.yaml | 73 ++++++++++++++++++++ examples/filter_metadata/src/lib.rs | 56 +++++++++++++++ 5 files changed, 204 insertions(+) create mode 100644 examples/filter_metadata/Cargo.toml create mode 100644 examples/filter_metadata/README.md create mode 100644 examples/filter_metadata/docker-compose.yaml create mode 100644 examples/filter_metadata/envoy.yaml create mode 100644 examples/filter_metadata/src/lib.rs diff --git a/examples/filter_metadata/Cargo.toml b/examples/filter_metadata/Cargo.toml new file mode 100644 index 0000000..5916bb4 --- /dev/null +++ b/examples/filter_metadata/Cargo.toml @@ -0,0 +1,21 @@ +[package] +publish = false +name = "proxy-wasm-example-filter-metadata" +version = "0.0.1" +authors = ["Martijn Swaagma "] +description = "Proxy-Wasm plugin example: Filter metadata" +license = "Apache-2.0" +edition = "2018" + +[lib] +crate-type = ["cdylib"] + +[dependencies] +proxy-wasm = { path = "../../" } + +[profile.release] +lto = true +opt-level = 3 +codegen-units = 1 +panic = "abort" +strip = "debuginfo" diff --git a/examples/filter_metadata/README.md b/examples/filter_metadata/README.md new file mode 100644 index 0000000..0b79fbc --- /dev/null +++ b/examples/filter_metadata/README.md @@ -0,0 +1,27 @@ +## Proxy-Wasm plugin example: Metadata + +Proxy-Wasm plugin that demonstrates reading metadata set by other filters + +### Building + +```sh +$ cargo build --target wasm32-wasip1 --release +``` + +### Using in Envoy + +This example can be run with [`docker compose`](https://docs.docker.com/compose/install/) +and has a matching Envoy configuration. + +```sh +$ docker compose up +``` + +Send HTTP request to `localhost:10000/` with a `x-custom-metadata` header value. + +> Not setting the header will return the configured welcome message. + +```sh +$ curl localhost:10000/ -H "x-custom-metadata: some-value" +Custom response with `x-custom-metadata` value "some-value" +``` diff --git a/examples/filter_metadata/docker-compose.yaml b/examples/filter_metadata/docker-compose.yaml new file mode 100644 index 0000000..844dc9b --- /dev/null +++ b/examples/filter_metadata/docker-compose.yaml @@ -0,0 +1,27 @@ +# Copyright 2022 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +services: + envoy: + image: envoyproxy/envoy:v1.31-latest + hostname: envoy + ports: + - "10000:10000" + volumes: + - ./envoy.yaml:/etc/envoy/envoy.yaml + - ./target/wasm32-wasip1/release:/etc/envoy/proxy-wasm-plugins + networks: + - envoymesh +networks: + envoymesh: {} diff --git a/examples/filter_metadata/envoy.yaml b/examples/filter_metadata/envoy.yaml new file mode 100644 index 0000000..083398c --- /dev/null +++ b/examples/filter_metadata/envoy.yaml @@ -0,0 +1,73 @@ +# Copyright 2022 Google LLC +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +static_resources: + listeners: + address: + socket_address: + address: 0.0.0.0 + port_value: 10000 + filter_chains: + - filters: + - name: envoy.filters.network.http_connection_manager + typed_config: + "@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager + stat_prefix: ingress_http + codec_type: AUTO + route_config: + name: local_routes + virtual_hosts: + - name: local_service + domains: + - "*" + routes: + - match: + prefix: "/" + direct_response: + status: 200 + body: + inline_string: "Welcome, set the `x-custom-metadata` header to change the response!\n" + http_filters: + # Set metadata + - name: envoy.filters.http.lua + typed_config: + "@type": type.googleapis.com/envoy.extensions.filters.http.lua.v3.Lua + default_source_code: + inline_string: | + function envoy_on_request(request_handle) + local headers = request_handle:headers() + local data = headers:get("x-custom-metadata") + + if data then + request_handle:streamInfo():dynamicMetadata():set("envoy.filters.http.lua", "x-custom-metadata", data) + end + + request_handle:logInfo("Metadata set by lua filter") + end + # Read it from a WASM filter + - name: envoy.filters.http.wasm + typed_config: + "@type": type.googleapis.com/udpa.type.v1.TypedStruct + type_url: type.googleapis.com/envoy.extensions.filters.http.wasm.v3.Wasm + value: + config: + name: "metadata_filter" + vm_config: + runtime: "envoy.wasm.runtime.v8" + code: + local: + filename: "/etc/envoy/proxy-wasm-plugins/proxy_wasm_example_filter_metadata.wasm" + - name: envoy.filters.http.router + typed_config: + "@type": type.googleapis.com/envoy.extensions.filters.http.router.v3.Router diff --git a/examples/filter_metadata/src/lib.rs b/examples/filter_metadata/src/lib.rs new file mode 100644 index 0000000..9017e3a --- /dev/null +++ b/examples/filter_metadata/src/lib.rs @@ -0,0 +1,56 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +use proxy_wasm::traits::*; +use proxy_wasm::types::*; + +proxy_wasm::main! {{ + proxy_wasm::set_log_level(LogLevel::Trace); + proxy_wasm::set_http_context(|_, _| -> Box { Box::new(MetadataHttp {}) }); +}} + +struct MetadataHttp {} + +impl Context for MetadataHttp {} + +impl HttpContext for MetadataHttp { + fn on_http_request_headers(&mut self, _: usize, _: bool) -> Action { + // Read data set by the lua filter + match self.get_property(vec![ + "metadata", + "filter_metadata", + "envoy.filters.http.lua", + "x-custom-metadata", + ]) { + Some(metadata) => match String::from_utf8(metadata) { + Ok(data) => { + self.send_http_response( + 200, + vec![("Powered-By", "proxy-wasm")], + Some( + format!( + "Custom response with `x-custom-metadata` value {:?}!\n", + data + ) + .as_bytes(), + ), + ); + Action::Pause + } + _ => Action::Continue, + }, + _ => Action::Continue, + } + } +} From 2e35a2028aaeb2464ed1a79aa9beceaf8acfdf44 Mon Sep 17 00:00:00 2001 From: Martijn Swaagman Date: Mon, 4 Nov 2024 13:05:32 +0100 Subject: [PATCH 2/8] doc: adjust example Signed-off-by: Martijn Swaagman --- examples/filter_metadata/README.md | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/examples/filter_metadata/README.md b/examples/filter_metadata/README.md index 0b79fbc..13eb08e 100644 --- a/examples/filter_metadata/README.md +++ b/examples/filter_metadata/README.md @@ -17,9 +17,15 @@ and has a matching Envoy configuration. $ docker compose up ``` -Send HTTP request to `localhost:10000/` with a `x-custom-metadata` header value. +Send a HTTP request to `localhost:10000/` will return the configured response. -> Not setting the header will return the configured welcome message. +```sh +$ curl localhost:10000/ +Welcome, set the `x-custom-metadata` header to change the response! +``` + + +Send a HTTP request to `localhost:10000/` with a `x-custom-metadata` header value. ```sh $ curl localhost:10000/ -H "x-custom-metadata: some-value" From 8f0bb41914101ac7a210a494dc8024134caf8cba Mon Sep 17 00:00:00 2001 From: Martijn Swaagman Date: Mon, 4 Nov 2024 13:10:27 +0100 Subject: [PATCH 3/8] doc: add example to list Signed-off-by: Martijn Swaagman --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 7db3cdc..97a9dc5 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,7 @@ - [HTTP Response body](./examples/http_body/) - [HTTP Configuration](./examples/http_config/) - [gRPC Auth (random)](./examples/grpc_auth_random/) +- [Filter metadata](./examples/filter_metadata/) ## Articles & blog posts from the community From 738c79d9c3fe15658fe2dff87796eeb31dfced38 Mon Sep 17 00:00:00 2001 From: Martijn Swaagman Date: Thu, 28 Nov 2024 10:26:22 +0100 Subject: [PATCH 4/8] chore: implement suggestion Signed-off-by: Martijn Swaagman --- README.md | 2 +- .../Cargo.toml | 4 ++-- .../README.md | 10 +++++----- .../docker-compose.yaml | 0 .../envoy.yaml | 8 ++++---- .../src/lib.rs | 6 +++--- 6 files changed, 15 insertions(+), 15 deletions(-) rename examples/{filter_metadata => envoy_filter_metadata}/Cargo.toml (73%) rename examples/{filter_metadata => envoy_filter_metadata}/README.md (71%) rename examples/{filter_metadata => envoy_filter_metadata}/docker-compose.yaml (100%) rename examples/{filter_metadata => envoy_filter_metadata}/envoy.yaml (92%) rename examples/{filter_metadata => envoy_filter_metadata}/src/lib.rs (88%) diff --git a/README.md b/README.md index 97a9dc5..fa53c22 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,7 @@ - [HTTP Response body](./examples/http_body/) - [HTTP Configuration](./examples/http_config/) - [gRPC Auth (random)](./examples/grpc_auth_random/) -- [Filter metadata](./examples/filter_metadata/) +- [Envoy filter metadata](./examples/envoy_filter_metadata/) ## Articles & blog posts from the community diff --git a/examples/filter_metadata/Cargo.toml b/examples/envoy_filter_metadata/Cargo.toml similarity index 73% rename from examples/filter_metadata/Cargo.toml rename to examples/envoy_filter_metadata/Cargo.toml index 5916bb4..86bd93a 100644 --- a/examples/filter_metadata/Cargo.toml +++ b/examples/envoy_filter_metadata/Cargo.toml @@ -1,9 +1,9 @@ [package] publish = false -name = "proxy-wasm-example-filter-metadata" +name = "proxy-wasm-example-envoy-filter-metadata" version = "0.0.1" authors = ["Martijn Swaagma "] -description = "Proxy-Wasm plugin example: Filter metadata" +description = "Proxy-Wasm plugin example: Envoy filter metadata" license = "Apache-2.0" edition = "2018" diff --git a/examples/filter_metadata/README.md b/examples/envoy_filter_metadata/README.md similarity index 71% rename from examples/filter_metadata/README.md rename to examples/envoy_filter_metadata/README.md index 13eb08e..8a6251d 100644 --- a/examples/filter_metadata/README.md +++ b/examples/envoy_filter_metadata/README.md @@ -1,6 +1,6 @@ -## Proxy-Wasm plugin example: Metadata +## Proxy-Wasm plugin example: Envoy metadata -Proxy-Wasm plugin that demonstrates reading metadata set by other filters +Proxy-Wasm plugin that demonstrates reading metadata set by other Envoy filters ### Building @@ -24,10 +24,10 @@ $ curl localhost:10000/ Welcome, set the `x-custom-metadata` header to change the response! ``` - -Send a HTTP request to `localhost:10000/` with a `x-custom-metadata` header value. +Send a HTTP request to `localhost:10000/` with a `x-custom-metadata` header value to get the uppercased value in the response. +The response will also set a response header `uppercased-metadata: SOME-VALUE`. ```sh $ curl localhost:10000/ -H "x-custom-metadata: some-value" -Custom response with `x-custom-metadata` value "some-value" +Custom response with Envoy metadata: "SOME-VALUE"! ``` diff --git a/examples/filter_metadata/docker-compose.yaml b/examples/envoy_filter_metadata/docker-compose.yaml similarity index 100% rename from examples/filter_metadata/docker-compose.yaml rename to examples/envoy_filter_metadata/docker-compose.yaml diff --git a/examples/filter_metadata/envoy.yaml b/examples/envoy_filter_metadata/envoy.yaml similarity index 92% rename from examples/filter_metadata/envoy.yaml rename to examples/envoy_filter_metadata/envoy.yaml index 083398c..e598af5 100644 --- a/examples/filter_metadata/envoy.yaml +++ b/examples/envoy_filter_metadata/envoy.yaml @@ -39,7 +39,7 @@ static_resources: body: inline_string: "Welcome, set the `x-custom-metadata` header to change the response!\n" http_filters: - # Set metadata + # Set uppercase metadata in Lua filter - name: envoy.filters.http.lua typed_config: "@type": type.googleapis.com/envoy.extensions.filters.http.lua.v3.Lua @@ -50,7 +50,7 @@ static_resources: local data = headers:get("x-custom-metadata") if data then - request_handle:streamInfo():dynamicMetadata():set("envoy.filters.http.lua", "x-custom-metadata", data) + request_handle:streamInfo():dynamicMetadata():set("envoy.filters.http.lua", "uppercased-custom-metadata", string.upper(data)) end request_handle:logInfo("Metadata set by lua filter") @@ -62,12 +62,12 @@ static_resources: type_url: type.googleapis.com/envoy.extensions.filters.http.wasm.v3.Wasm value: config: - name: "metadata_filter" + name: "envoy_metadata_filter" vm_config: runtime: "envoy.wasm.runtime.v8" code: local: - filename: "/etc/envoy/proxy-wasm-plugins/proxy_wasm_example_filter_metadata.wasm" + filename: "/etc/envoy/proxy-wasm-plugins/proxy_wasm_example_envoy_filter_metadata.wasm" - name: envoy.filters.http.router typed_config: "@type": type.googleapis.com/envoy.extensions.filters.http.router.v3.Router diff --git a/examples/filter_metadata/src/lib.rs b/examples/envoy_filter_metadata/src/lib.rs similarity index 88% rename from examples/filter_metadata/src/lib.rs rename to examples/envoy_filter_metadata/src/lib.rs index 9017e3a..febdb74 100644 --- a/examples/filter_metadata/src/lib.rs +++ b/examples/envoy_filter_metadata/src/lib.rs @@ -31,16 +31,16 @@ impl HttpContext for MetadataHttp { "metadata", "filter_metadata", "envoy.filters.http.lua", - "x-custom-metadata", + "uppercased-custom-metadata", ]) { Some(metadata) => match String::from_utf8(metadata) { Ok(data) => { self.send_http_response( 200, - vec![("Powered-By", "proxy-wasm")], + vec![("Powered-By", "proxy-wasm"), ("uppercased-metadata", &data)], Some( format!( - "Custom response with `x-custom-metadata` value {:?}!\n", + "Custom response with Envoy metadata: {:?}!\n", data ) .as_bytes(), From d3c04dfb13fa5324727cd6cae2abd77060a8f681 Mon Sep 17 00:00:00 2001 From: Martijn Swaagman Date: Mon, 2 Dec 2024 10:52:21 +0100 Subject: [PATCH 5/8] fix: update response and doc Signed-off-by: Martijn Swaagman --- examples/envoy_filter_metadata/README.md | 10 +++++----- examples/envoy_filter_metadata/src/lib.rs | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/envoy_filter_metadata/README.md b/examples/envoy_filter_metadata/README.md index 8a6251d..373bfd3 100644 --- a/examples/envoy_filter_metadata/README.md +++ b/examples/envoy_filter_metadata/README.md @@ -17,17 +17,17 @@ and has a matching Envoy configuration. $ docker compose up ``` -Send a HTTP request to `localhost:10000/` will return the configured response. +Send a HTTP request to `localhost:10000` will return the configured response. ```sh -$ curl localhost:10000/ +$ curl localhost:10000 Welcome, set the `x-custom-metadata` header to change the response! ``` -Send a HTTP request to `localhost:10000/` with a `x-custom-metadata` header value to get the uppercased value in the response. +Send a HTTP request to `localhost:10000` with a `x-custom-metadata` header value to get the uppercased value in the response. The response will also set a response header `uppercased-metadata: SOME-VALUE`. ```sh -$ curl localhost:10000/ -H "x-custom-metadata: some-value" -Custom response with Envoy metadata: "SOME-VALUE"! +$ curl localhost:10000 -H "x-custom-metadata: some-value" +Custom response with Envoy metadata: "SOME-VALUE" ``` diff --git a/examples/envoy_filter_metadata/src/lib.rs b/examples/envoy_filter_metadata/src/lib.rs index febdb74..5498697 100644 --- a/examples/envoy_filter_metadata/src/lib.rs +++ b/examples/envoy_filter_metadata/src/lib.rs @@ -40,7 +40,7 @@ impl HttpContext for MetadataHttp { vec![("Powered-By", "proxy-wasm"), ("uppercased-metadata", &data)], Some( format!( - "Custom response with Envoy metadata: {:?}!\n", + "Custom response with Envoy metadata: {:?}\n", data ) .as_bytes(), From b96a374a408dfdddcefc36cd9f85adc6c6864422 Mon Sep 17 00:00:00 2001 From: Martijn Swaagman Date: Mon, 2 Dec 2024 10:54:16 +0100 Subject: [PATCH 6/8] fix: add example to CI Signed-off-by: Martijn Swaagman --- .github/workflows/rust.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 4087b15..64d1e73 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -366,6 +366,7 @@ jobs: - 'http_config' - 'http_headers' - 'grpc_auth_random' + - 'envoy_filter_metadata' defaults: run: @@ -447,6 +448,7 @@ jobs: - 'http_config' - 'http_headers' - 'grpc_auth_random' + - 'envoy_filter_metadata' defaults: run: From 6337586eac6af8bb9494415822ac5570478d927d Mon Sep 17 00:00:00 2001 From: Martijn Swaagman Date: Tue, 3 Dec 2024 11:14:17 +0100 Subject: [PATCH 7/8] fix: formatting Signed-off-by: Martijn Swaagman --- examples/envoy_filter_metadata/src/lib.rs | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/examples/envoy_filter_metadata/src/lib.rs b/examples/envoy_filter_metadata/src/lib.rs index 5498697..e5f1905 100644 --- a/examples/envoy_filter_metadata/src/lib.rs +++ b/examples/envoy_filter_metadata/src/lib.rs @@ -39,11 +39,7 @@ impl HttpContext for MetadataHttp { 200, vec![("Powered-By", "proxy-wasm"), ("uppercased-metadata", &data)], Some( - format!( - "Custom response with Envoy metadata: {:?}\n", - data - ) - .as_bytes(), + format!("Custom response with Envoy metadata: {:?}\n", data).as_bytes(), ), ); Action::Pause From ec7ce256adf45506e005be8ba3aded00d338d957 Mon Sep 17 00:00:00 2001 From: Piotr Sikora Date: Wed, 15 Jan 2025 08:26:01 -0500 Subject: [PATCH 8/8] review: style. Signed-off-by: Piotr Sikora --- examples/envoy_filter_metadata/README.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/examples/envoy_filter_metadata/README.md b/examples/envoy_filter_metadata/README.md index 373bfd3..573acc8 100644 --- a/examples/envoy_filter_metadata/README.md +++ b/examples/envoy_filter_metadata/README.md @@ -1,6 +1,6 @@ ## Proxy-Wasm plugin example: Envoy metadata -Proxy-Wasm plugin that demonstrates reading metadata set by other Envoy filters +Proxy-Wasm plugin that demonstrates reading metadata set by other Envoy filters. ### Building @@ -17,15 +17,17 @@ and has a matching Envoy configuration. $ docker compose up ``` -Send a HTTP request to `localhost:10000` will return the configured response. +Send a HTTP request to `localhost:10000` that will return the configured response. ```sh $ curl localhost:10000 Welcome, set the `x-custom-metadata` header to change the response! ``` -Send a HTTP request to `localhost:10000` with a `x-custom-metadata` header value to get the uppercased value in the response. -The response will also set a response header `uppercased-metadata: SOME-VALUE`. +Send a HTTP request to `localhost:10000` with a `x-custom-metadata` header value to get +the uppercased value in the response. + +The response will also contain a response header `uppercased-metadata: SOME-VALUE`. ```sh $ curl localhost:10000 -H "x-custom-metadata: some-value"