Skip to content

Commit

Permalink
add-header-filter and metadata-filter implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
NomadXD committed May 22, 2021
1 parent 82ef55b commit 89a74f1
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 39 deletions.
16 changes: 5 additions & 11 deletions header-to-metadata/add-header-filter/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use log::trace;
use log::info;
use proxy_wasm::traits::*;
use proxy_wasm::types::*;

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

Expand All @@ -30,20 +30,14 @@ 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");
trace!("3scale metadata headers added from the request path");
Action::Continue
}

fn on_http_response_headers(&mut self, _: usize) -> Action {
for (name, value) in &self.get_http_response_headers() {
trace!("#{} <- {}: {}", self.context_id, name, value);
}
info!("3scale metadata headers added from the request path");
Action::Continue
}

fn on_log(&mut self) {
trace!("#{} completed.", self.context_id);
info!("#{} completed.", self.context_id);
}
}
Binary file modified 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: {}
8 changes: 4 additions & 4 deletions header-to-metadata/metadata-filter/Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
build: metadata.wasm
build: metadata_filter.wasm

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

.PHONY: clean
clean:
rm ../build/metadata.wasm || true
rm ../build/metadata_filter.wasm || true
40 changes: 28 additions & 12 deletions header-to-metadata/metadata-filter/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use log::trace;
use log::info;
use proxy_wasm::traits::*;
use proxy_wasm::types::*;
use std::str;

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

Expand All @@ -30,20 +31,35 @@ impl Context for MetadataFilter {}

impl HttpContext for MetadataFilter {
fn on_http_request_headers(&mut self, _: usize) -> Action {
// self.add_http_request_header("x-3scale-service-key", "123456789");
// self.add_http_request_header("x-3scale-application-key", "987654321");
trace!("Metadata filter intercepted the HTTP request");
Action::Continue
}

fn on_http_response_headers(&mut self, _: usize) -> Action {
for (name, value) in &self.get_http_response_headers() {
trace!("#{} <- {}: {}", self.context_id, name, value);
for (name, value) in &self.get_http_request_headers() {
info!("#{} -> {}: {}", self.context_id, name, value);
}
info!("Metadata filter intercepted the HTTP request");
let service_key_utf8 = self
.get_property(vec!["metadata", "filter_metadata", "3scale", "service_key"])
.unwrap();
let service_key = match str::from_utf8(&service_key_utf8) {
Ok(sk) => sk,
Err(e) => panic!("Error : {}", e),
};
let application_key_utf8 = self
.get_property(vec![
"metadata",
"filter_metadata",
"3scale",
"application_key",
])
.unwrap();
let application_key = match str::from_utf8(&application_key_utf8) {
Ok(ak) => ak,
Err(e) => panic!("Error: {}", e),
};
info!("service key from dynamic metadata: {}", service_key);
info!("application key from dynamic metadata: {}", application_key);
Action::Continue
}

fn on_log(&mut self) {
trace!("#{} completed.", self.context_id);
info!("#{} completed.", self.context_id);
}
}
11 changes: 11 additions & 0 deletions header-to-metadata/resources/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM envoyproxy/envoy:v1.18.3

# Copy envoy.yaml & wasm module
COPY ./resources/envoy.yaml /etc/envoy/envoy.yaml
COPY ./build/add_header_filter.wasm /usr/local/bin/add_header_filter.wasm
COPY ./build/metadata_filter.wasm /usr/local/bin/metadata_filter.wasm

# Setup permission
RUN chmod go+r /etc/envoy/envoy.yaml /usr/local/bin/add_header_filter.wasm /usr/local/bin/metadata_filter.wasm

CMD /usr/local/bin/envoy -c /etc/envoy/envoy.yaml
58 changes: 46 additions & 12 deletions header-to-metadata/resources/envoy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -43,22 +43,56 @@ static_resources:
type_url: type.googleapis.com/envoy.extensions.filters.http.wasm.v3.Wasm
value:
config:
name: "ratelimit"
root_id: "ratelimit_root"
configuration:
"@type": type.googleapis.com/google.protobuf.StringValue
value: |
{
"ratelimit_limit": "10",
"ratelimit_remaining": "10",
"ratelimit_reset": "30s"
}
name: "htm"
root_id: "htm_root"
vm_config:
runtime: "envoy.wasm.runtime.v8"
vm_id: "ratelimit_vm"
vm_id: "htm_vm"
code:
local:
filename: "/usr/local/bin/envoy_rate_limit_header.wasm"
filename: "/usr/local/bin/add_header_filter.wasm"
configuration: {}
allow_precompiled: true
- name: envoy.filters.http.header_to_metadata
typed_config:
"@type": type.googleapis.com/envoy.extensions.filters.http.header_to_metadata.v3.Config
request_rules:
- header: x-3scale-service-key
on_header_present:
metadata_namespace: 3scale
key: service_key
type: STRING
on_header_missing:
metadata_namespace: 3scale
key: service_key
value: '123456'
type: STRING
remove: false
- header: x-3scale-application-key
on_header_present:
metadata_namespace: 3scale
key: application_key
type: STRING
on_header_missing:
metadata_namespace: 3scale
key: application_key
value: '123456'
type: STRING
remove: false
- 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: "htm"
root_id: "htm_root"
vm_config:
runtime: "envoy.wasm.runtime.v8"
vm_id: "htm_vm"
code:
local:
filename: "/usr/local/bin/metadata_filter.wasm"
configuration: {}
allow_precompiled: true
- name: envoy.filters.http.router
Expand Down

0 comments on commit 89a74f1

Please sign in to comment.