Skip to content

Commit

Permalink
starting docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Zwiterrion committed Jun 19, 2024
1 parent b5f5879 commit 610500a
Show file tree
Hide file tree
Showing 27 changed files with 658 additions and 31 deletions.
3 changes: 2 additions & 1 deletion server/datastores/s3.js
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,8 @@ module.exports = class S3Datastore extends Datastore {
filename: metadata.name.replace(/ /g, '-'),
type: metadata.type,
pluginId: pluginId,
template: metadata.template
template: metadata.template,
productTemplate: metadata.productTemplate
};

console.log('generate pluginID', pluginId, newPlugin.pluginId)
Expand Down
3 changes: 2 additions & 1 deletion server/routers/plugins.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ router.post('/', async (req, res) => {
const out = await Datastore.createEmptyPlugin(req.user.email, req.body.metadata || {
name: req.body.plugin,
type: req.body.type,
template: req.body.template || 'empty'
template: req.body.template || 'empty',
productTemplate: req.body.productTemplate
});

if (out.status !== 201 || !req.body.files) {
Expand Down
8 changes: 4 additions & 4 deletions server/routers/templates.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ router.get('/', (req, res) => {
error: 'Missing type of project'
})
} else {
const { type } = req.query;
const { type, productTemplate } = req.query;
const template = !req.query.template || req.query.template === 'undefined' ? 'empty' : req.query.template;

if (['rust', 'js', 'go', 'ts', 'opa'].includes(type)) {
getTemplates(type, template, res);
getTemplates(type, template, productTemplate, res);
} else {
res
.status(404)
Expand All @@ -36,7 +36,7 @@ function getTemplatesFromPath(type, template, res) {
return res.sendFile(path.join(__dirname, '../templates', `${type}.zip`))
}

function getTemplates(type, template, res) {
function getTemplates(type, template, productTemplate, res) {
const source = ENV.MANAGER_TEMPLATES;
const zipName = `${type}.zip`;

Expand Down
21 changes: 21 additions & 0 deletions server/templates/otoroshi/OTOROSHI_WASM_ACCESS_CONTROL/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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;
}
25 changes: 25 additions & 0 deletions server/templates/otoroshi/OTOROSHI_WASM_ACCESS_CONTROL/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { WasmAccessValidatorContext, WasmAccessValidatorResponse } from 'otoroshi-ts-types';

export declare var Host: any;

export function execute() {
let context = JSON.parse(Host.inputString()) as WasmAccessValidatorContext;

if (context.request.headers["foo"] === "bar") {
const out: WasmAccessValidatorResponse = {
result: true
};
Host.outputString(JSON.stringify(out));
} else {
const error: WasmAccessValidatorResponse = {
result: false,
error: {
message: "you're not authorized",
status: 401
}
};
Host.outputString(JSON.stringify(error));
}

return 0;
}
35 changes: 35 additions & 0 deletions server/templates/otoroshi/OTOROSHI_WASM_ACCESS_CONTROL/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use extism_pdk::*;
use otoroshi_rust_types::types::*;

#[plugin_fn]
pub fn execute(
Json(_context): Json<WasmAccessValidatorContext>,
) -> FnResult<Json<WasmAccessValidatorResponse>> {
let out = types::WasmAccessValidatorResponse {
result: false,
error: Some(types::WasmAccessValidatorError {
message: "you're not authorized".to_owned(),
status: 401,
}),
};

match context.request.headers.get("foo") {
Some(foo) => {
if foo == "bar" {
Ok(Json(types::WasmAccessValidatorResponse {
result: true,
error: None,
}))
} else {
Ok(Json(types::WasmAccessValidatorResponse {
result: false,
error: Some(types::WasmAccessValidatorError {
message: format!("{} is not authorized", foo).to_owned(),
status: 401,
}),
}))
}
}
None => Ok(Json(out)),
}
}
39 changes: 39 additions & 0 deletions server/templates/otoroshi/OTOROSHI_WASM_ACCESS_CONTROL/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package main

import (
"github.com/extism/go-pdk"
"github.com/buger/jsonparser"
// "github.com/MAIF/otoroshi-go-types"
)

//export execute
func execute() int32 {
input := pdk.Input()

var foo, err = jsonparser.GetString(input, "request", "headers", "foo")

if err != nil {}

var output = ""

if foo == "bar" {
output = `{
"result": true
}`
} else {
output = `{
"result": false,
"error": {
"message": "you're not authorized",
"status": 401
}
}`
}

mem := pdk.AllocateString(output)
pdk.OutputMemory(mem)

return 0
}

func main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package example

default can_access = false

can_access {
input.request.headers.foo == "bar"
}
39 changes: 39 additions & 0 deletions server/templates/otoroshi/OTOROSHI_WASM_BACKEND/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
export function execute() {
const str = Host.inputString()
const context = JSON.parse(str)

let headers = { ...context.request.headers }
headers["foo"] = "bar"

const response = {
headers,
'Content-Type': "text/html",
body: `<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body style="
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
height: 100vh;
font-family: monospace;
">
<h1>This HTML comes from the Wasmo plugin</h1>
<img src="https://maif.github.io/wasmo/wasmo.png" style="
width: 200;
">
</body>
</html>`,
status: 200
}

Host.outputString(JSON.stringify(response))

return 0
}
43 changes: 43 additions & 0 deletions server/templates/otoroshi/OTOROSHI_WASM_BACKEND/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { WasmQueryContext, WasmQueryResponse } from 'otoroshi-ts-types';

export declare var Host: any;

export function execute() {
const context = JSON.parse(Host.inputString()) as WasmQueryContext;

const headers = {
"foo": "bar",
...(context.request.headers || {})
}

const response: WasmQueryResponse = {
headers,
status: 200,
body: `<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body style="
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
height: 100vh;
font-family: monospace;
">
<h1>This HTML comes from the Wasmo plugin</h1>
<img src="https://maif.github.io/wasmo/wasmo.png" style="
width: 200;
">
</body>
</html>`,
};
Host.outputString(JSON.stringify(response));

return 0;
}

38 changes: 38 additions & 0 deletions server/templates/otoroshi/OTOROSHI_WASM_BACKEND/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use extism_pdk::*;
use otoroshi_rust_types::types::*;
use std::collections::HashMap;

#[plugin_fn]
pub fn execute(Json(context): Json<types::WasmQueryContext>) -> FnResult<Json<types::WasmQueryResponse>> {
let mut headers = HashMap::new();
headers.insert("foo".to_string(), "bar".to_string());

let response = types::WasmQueryResponse {
headers: Some(headers.into_iter().chain(context.raw_request.headers).collect()),
body: r#"<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body style="
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
height: 100vh;
font-family: monospace;
">
<h1>This HTML comes from the Wasmo plugin</h1>
<img src="https://maif.github.io/wasmo/wasmo.png" style="
width: 200;
">
</body>
</html>"#.to_owned(),
status: 200
};

Ok(Json(response))
}
28 changes: 28 additions & 0 deletions server/templates/otoroshi/OTOROSHI_WASM_BACKEND/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
"github.com/extism/go-pdk"
"github.com/buger/jsonparser"
// "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
_ = headers

if err != nil {}

output := `{ "headers": { "Content-Type": "text/html" }, "body": "<html lang=\"en\"><head><meta charset=\"utf-8\"><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"></head><body style=\"display: flex;align-items: center;justify-content: center;flex-direction: column;height: 100vh;font-family: monospace;\"><h1>This HTML comes from the Wasmo plugin</h1><img src=\"https://maif.github.io/wasmo/wasmo.png\" style=\"width: 200;\"></body></html>", "status": 200 }`
mem := pdk.AllocateString(output)
pdk.OutputMemory(mem)

return 0
}

func main() {}
20 changes: 20 additions & 0 deletions server/templates/otoroshi/OTOROSHI_WASM_PRE_ROUTE/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export function execute() {
let context = JSON.parse(Host.inputString())

if (context.request.headers["bar"] === "foo") {
Host.outputString(JSON.stringify({
error: false
}))
} else {
Host.outputString(JSON.stringify({
error: true,
headers: {
'Content-Type': 'text/plain'
},
status: 401,
body: "you're not authorized"
}))
}

return 0
}
26 changes: 26 additions & 0 deletions server/templates/otoroshi/OTOROSHI_WASM_PRE_ROUTE/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { WasmPreRouteContext, WasmPreRouteResponse } from 'otoroshi-ts-types';

export declare var Host: any;

export function execute() {
let context = JSON.parse(Host.inputString()) as WasmPreRouteContext;

if (context.request.headers["bar"] === "foo") {
const out: WasmPreRouteResponse = {
error: false
}
Host.outputString(JSON.stringify(out));
} else {
const error = {
error: true,
status: 401,
headers: {
'Content-Type': 'text/plain'
},
body: "you're not authorized"
}
Host.outputString(JSON.stringify(error));
}

return 0
}
Loading

0 comments on commit 610500a

Please sign in to comment.