-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Envoy filter metadata example. (#289)
Signed-off-by: Martijn Swaagman <martijn@swaagman.online>
- Loading branch information
Showing
7 changed files
with
211 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
[package] | ||
publish = false | ||
name = "proxy-wasm-example-envoy-filter-metadata" | ||
version = "0.0.1" | ||
authors = ["Martijn Swaagma <martijn@swaagman.online>"] | ||
description = "Proxy-Wasm plugin example: Envoy 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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
## Proxy-Wasm plugin example: Envoy metadata | ||
|
||
Proxy-Wasm plugin that demonstrates reading metadata set by other Envoy 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 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 contain a response header `uppercased-metadata: SOME-VALUE`. | ||
|
||
```sh | ||
$ curl localhost:10000 -H "x-custom-metadata: some-value" | ||
Custom response with Envoy metadata: "SOME-VALUE" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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: {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 uppercase metadata in Lua filter | ||
- 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", "uppercased-custom-metadata", string.upper(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: "envoy_metadata_filter" | ||
vm_config: | ||
runtime: "envoy.wasm.runtime.v8" | ||
code: | ||
local: | ||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// 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<dyn HttpContext> { 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", | ||
"uppercased-custom-metadata", | ||
]) { | ||
Some(metadata) => match String::from_utf8(metadata) { | ||
Ok(data) => { | ||
self.send_http_response( | ||
200, | ||
vec![("Powered-By", "proxy-wasm"), ("uppercased-metadata", &data)], | ||
Some( | ||
format!("Custom response with Envoy metadata: {:?}\n", data).as_bytes(), | ||
), | ||
); | ||
Action::Pause | ||
} | ||
_ => Action::Continue, | ||
}, | ||
_ => Action::Continue, | ||
} | ||
} | ||
} |