From d7bfcb3bcb7be0fde817cd5dcb07f2c3ecf7b335 Mon Sep 17 00:00:00 2001 From: zwiterrion Date: Wed, 19 Jun 2024 14:26:29 +0200 Subject: [PATCH] otoroshi wasm response transformer --- .../index.js | 38 +++++++------- .../index.ts | 23 +++++++++ .../OTOROSHI_WASM_RESPONSE_TRANSFORMER/lib.rs | 27 ++++++++++ .../main.go | 51 +++++++++++++++++++ server/templates/otoroshi/rust/Cargo.toml | 2 +- server/templates/otoroshi/ts/package.json | 2 +- server/templates/rust/Cargo.toml | 1 + server/templates/ts/package.json | 2 +- 8 files changed, 123 insertions(+), 23 deletions(-) create mode 100755 server/templates/otoroshi/OTOROSHI_WASM_RESPONSE_TRANSFORMER/index.ts create mode 100755 server/templates/otoroshi/OTOROSHI_WASM_RESPONSE_TRANSFORMER/lib.rs create mode 100755 server/templates/otoroshi/OTOROSHI_WASM_RESPONSE_TRANSFORMER/main.go diff --git a/server/templates/otoroshi/OTOROSHI_WASM_RESPONSE_TRANSFORMER/index.js b/server/templates/otoroshi/OTOROSHI_WASM_RESPONSE_TRANSFORMER/index.js index c54cb54..91fe07b 100755 --- a/server/templates/otoroshi/OTOROSHI_WASM_RESPONSE_TRANSFORMER/index.js +++ b/server/templates/otoroshi/OTOROSHI_WASM_RESPONSE_TRANSFORMER/index.js @@ -1,21 +1,19 @@ export function execute() { - let context = JSON.parse(Host.inputString()); - - if (context.request.headers["foo"] === "bar") { - const out = { - result: true - }; - Host.outputString(JSON.stringify(out)); - } else { - const error = { - result: false, - error: { - message: "you're not authorized", - status: 401 - } - }; - Host.outputString(JSON.stringify(error)); - } - - return 0; - } \ No newline at end of file + let context = JSON.parse(Host.inputString()) + + Host.outputString(JSON.stringify({ + ...context, + status: 200, + headers: { + ...context.otoroshi_response.headers, + OTOROSHI_WASM_PLUGIN_ID: "OTOROSHI_WASM_RESPONSE_TRANSFORMER", + "Content-Type": "application/json" + }, + body_json: { + foo: "bar" + }, + // cookies + })) + + return 0 +} \ No newline at end of file diff --git a/server/templates/otoroshi/OTOROSHI_WASM_RESPONSE_TRANSFORMER/index.ts b/server/templates/otoroshi/OTOROSHI_WASM_RESPONSE_TRANSFORMER/index.ts new file mode 100755 index 0000000..e43e47d --- /dev/null +++ b/server/templates/otoroshi/OTOROSHI_WASM_RESPONSE_TRANSFORMER/index.ts @@ -0,0 +1,23 @@ +import { WasmRequestTransformerContext, WasmTransformerResponse } from 'otoroshi-ts-types'; + +export declare var Host: any; + +export function execute() { + let context = JSON.parse(Host.inputString()) as WasmRequestTransformerContext; + + const error: WasmTransformerResponse = { + ...context, + status: 200, + headers: { + ...context.otoroshi_request.headers, + OTOROSHI_WASM_PLUGIN_ID: "OTOROSHI_WASM_RESPONSE_TRANSFORMER", + "Content-Type": "application/json" + }, + body_json: { + foo: "bar" + } + } + Host.outputString(JSON.stringify(error)); + + return 0 +} \ No newline at end of file diff --git a/server/templates/otoroshi/OTOROSHI_WASM_RESPONSE_TRANSFORMER/lib.rs b/server/templates/otoroshi/OTOROSHI_WASM_RESPONSE_TRANSFORMER/lib.rs new file mode 100755 index 0000000..94e94a3 --- /dev/null +++ b/server/templates/otoroshi/OTOROSHI_WASM_RESPONSE_TRANSFORMER/lib.rs @@ -0,0 +1,27 @@ +use extism_pdk::*; +use otoroshi_rust_types::types; + +#[plugin_fn] +pub fn execute( + Json(context): Json, +) -> FnResult> { + let mut out_headers = context.request.headers; + out_headers.insert( + "OTOROSHI_WASM_PLUGIN_ID".to_string(), + "OTOROSHI_WASM_RESPONSE_TRANSFORMER".to_string(), + ); + out_headers.insert("Content-Type".to_string(), "text/plain".to_string()); + + let out = types::WasmResponse { + status: Some(200), + error: None, + body_str: Some("{ \"foo\": \"bar\" }".to_string()), + headers: out_headers, + cookies: serde_json::Value::Null, + body_base64: None, + body_bytes: None, + body_json: None, + }; + + Ok(Json(out)) +} diff --git a/server/templates/otoroshi/OTOROSHI_WASM_RESPONSE_TRANSFORMER/main.go b/server/templates/otoroshi/OTOROSHI_WASM_RESPONSE_TRANSFORMER/main.go new file mode 100755 index 0000000..58b131b --- /dev/null +++ b/server/templates/otoroshi/OTOROSHI_WASM_RESPONSE_TRANSFORMER/main.go @@ -0,0 +1,51 @@ +package main + +import ( + "encoding/json" + + "github.com/buger/jsonparser" + "github.com/extism/go-pdk" + // "github.com/MAIF/otoroshi-go-types" +) + +//export execute +func execute() int32 { + input := pdk.Input() + + var headers, dataType, offset, err = jsonparser.Get(input, "request", "headers") + + _ = dataType + _ = offset + + var outHeaders map[string]interface{} + + // Unmarshal the JSON data into the map + err = json.Unmarshal(headers, &outHeaders) + if err != nil { + + } + + outHeaders["Content-Type"] = "application/json" + outHeaders["OTOROSHI_WASM_PLUGIN_ID"] = "OTOROSHI_WASM_RESPONSE_TRANSFORMER" + + if err != nil { + } + + jsonHeaders, marshallingError := json.Marshal(outHeaders) + if marshallingError != nil { + + } + + output := `{ + "status": 200, + "headers": ` + string(jsonHeaders) + `, + "body_json": { "foo": "bar" } + }` + + mem := pdk.AllocateString(output) + pdk.OutputMemory(mem) + + return 0 +} + +func main() {} diff --git a/server/templates/otoroshi/rust/Cargo.toml b/server/templates/otoroshi/rust/Cargo.toml index e06a5d8..6d0f75d 100755 --- a/server/templates/otoroshi/rust/Cargo.toml +++ b/server/templates/otoroshi/rust/Cargo.toml @@ -11,7 +11,7 @@ wasi = false extism-pdk = "1.0.1" serde = "1.0.152" serde_json = "1.0.91" -otoroshi_rust_types = "0.0.5" +otoroshi_rust_types = "1.0.0" [lib] crate_type = ["cdylib"] diff --git a/server/templates/otoroshi/ts/package.json b/server/templates/otoroshi/ts/package.json index 44c35f9..9fd1c92 100755 --- a/server/templates/otoroshi/ts/package.json +++ b/server/templates/otoroshi/ts/package.json @@ -3,6 +3,6 @@ "version": "@@PLUGIN_VERSION@@", "devDependencies": { "esbuild": "^0.17.9", - "otoroshi-ts-types": "1.0.0" + "otoroshi-ts-types": "1.0.2" } } \ No newline at end of file diff --git a/server/templates/rust/Cargo.toml b/server/templates/rust/Cargo.toml index c2881c3..6d0f75d 100755 --- a/server/templates/rust/Cargo.toml +++ b/server/templates/rust/Cargo.toml @@ -11,6 +11,7 @@ wasi = false extism-pdk = "1.0.1" serde = "1.0.152" serde_json = "1.0.91" +otoroshi_rust_types = "1.0.0" [lib] crate_type = ["cdylib"] diff --git a/server/templates/ts/package.json b/server/templates/ts/package.json index 44c35f9..9fd1c92 100755 --- a/server/templates/ts/package.json +++ b/server/templates/ts/package.json @@ -3,6 +3,6 @@ "version": "@@PLUGIN_VERSION@@", "devDependencies": { "esbuild": "^0.17.9", - "otoroshi-ts-types": "1.0.0" + "otoroshi-ts-types": "1.0.2" } } \ No newline at end of file