Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev/header to metadata #2

Merged
merged 2 commits into from
May 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
.vscode/
envoy-rate-limit-header/target/
mock-backend/node_modules/
mock-backend/node_modules/
header-to-metadata/add-header-filter/target/
header-to-metadata/metadata-filter/target/
151 changes: 151 additions & 0 deletions header-to-metadata/add-header-filter/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions header-to-metadata/add-header-filter/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[package]
name = "add-header-filter"
version = "0.1.0"
authors = ["Lahiru Udayanga <[email protected]>"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[lib]
path = "src/lib.rs"
crate-type = ["cdylib"]

[dependencies]
proxy-wasm = "0.1.3"
log = "0.4.14"
wasm-bindgen = "0.2.74"
9 changes: 9 additions & 0 deletions header-to-metadata/add-header-filter/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
build: add_header_filter.wasm

add_header_filter.wasm:
cargo build --target wasm32-unknown-unknown --release
cp target/wasm32-unknown-unknown/release/add_header_filter.wasm ../build/add_header_filter.wasm

.PHONY: clean
clean:
rm ../build/add_header_filter.wasm || true
43 changes: 43 additions & 0 deletions header-to-metadata/add-header-filter/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use log::info;
use proxy_wasm::traits::*;
use proxy_wasm::types::*;

#[no_mangle]
pub fn _start() {
proxy_wasm::set_log_level(LogLevel::Info);
proxy_wasm::set_root_context(|_| -> Box<dyn RootContext> { Box::new(HttpHeadersRoot) });
}

struct HttpHeadersRoot;

impl Context for HttpHeadersRoot {}

impl RootContext for HttpHeadersRoot {
fn get_type(&self) -> Option<ContextType> {
Some(ContextType::HttpContext)
}

fn create_http_context(&self, context_id: u32) -> Option<Box<dyn HttpContext>> {
Some(Box::new(HttpHeaders { context_id }))
}
}

struct HttpHeaders {
context_id: u32,
}

impl Context for HttpHeaders {}

impl HttpContext for HttpHeaders {
fn on_http_request_headers(&mut self, _: usize) -> Action {
// Add metadata headers. These metadata headers will be added as dynamic metadata from the header to metadata filter.
self.add_http_request_header("x-3scale-service-key", "123456789");
self.add_http_request_header("x-3scale-application-key", "987654321");
info!("3scale metadata headers added from the request path");
Action::Continue
}

fn on_log(&mut self) {
info!("#{} completed.", self.context_id);
}
}
Binary file added header-to-metadata/build/add_header_filter.wasm
Binary file not shown.
Binary file added header-to-metadata/build/metadata_filter.wasm
Binary file not shown.
23 changes: 23 additions & 0 deletions header-to-metadata/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
version: '3.7'
services:
envoy:
build:
context: ./
dockerfile: ./resources/Dockerfile
depends_on:
- backend_service
networks:
- envoymesh
ports:
- "9095:9095"
- "9000:9000"

backend_service:
build:
context: ../mock-backend
dockerfile: Dockerfile
networks:
- envoymesh

networks:
envoymesh: {}
Loading