-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
141 additions
and
25 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Kiwi Plugins | ||
|
||
Kiwi allows operators to create WASM plugins that can be loaded at start-up to extend the functionality of the server. | ||
|
||
|
||
## Creating a Plugin | ||
|
||
A plugin is a WebAssembly module that implements a specific interface. Currently, Kiwi offers a Rust SDK to streamline the process of creating plugins | ||
|
||
First, bootstrap a new plugin project using the `cargo` command-line tool: | ||
|
||
```sh | ||
cargo new --lib my-kiwi-plugin | ||
``` | ||
|
||
Next, in the project folder, run the following command to add the `kiwi-sdk` crate: | ||
|
||
```sh | ||
cargo add kiwi-sdk | ||
``` | ||
|
||
Before writing any code, ensure that crate type is specified as `cdylib` in the `Cargo.toml` file. This is necessary to compile a dynamic library that can be loaded by Kiwi at runtime. | ||
|
||
```toml | ||
[lib] | ||
crate-type = ["cdylib"] | ||
``` | ||
|
||
**Note**: The plugin must be built with the `--target` flag set to `wasm32-wasi` | ||
|
||
```sh | ||
cargo build --target wasm32-wasi | ||
``` | ||
|
||
Nice! You're ready to start writing your plugin. Take a look in the [examples](../examples) directory for Kiwi plugin samples. |
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,62 @@ | ||
# Authenticate w/ Outbound HTTP | ||
|
||
This example demonstrates how to make outbound HTTP requests in the authenticate hook using the Kiwi SDK. | ||
|
||
- [Authenticate w/ Outbound HTTP](#authenticate-w-outbound-http) | ||
- [Running the Example](#running-the-example) | ||
- [Building the WASM Hook](#building-the-wasm-hook) | ||
- [Running Kiwi](#running-kiwi) | ||
- [Establishing a Connection](#establishing-a-connection) | ||
- [Recap](#recap) | ||
|
||
## Running the Example | ||
|
||
> **NOTE**: The commands in this example should be run from this directory (`examples/authenticate-http`). | ||
### Building the WASM Hook | ||
|
||
The `wasm32-wasi` target is required to build the WASM hook. This target is not installed by default, so it must be added using the following command: | ||
|
||
```sh | ||
rustup target add wasm32-wasi | ||
``` | ||
|
||
Once the target is installed, the WASM hook can be built using the following command: | ||
|
||
```sh | ||
cargo build --target wasm32-wasi | ||
``` | ||
|
||
This command will produce the WASM hook at `target/wasm32-wasi/debug/authenticate_http.wasm`. | ||
|
||
### Running Kiwi | ||
|
||
Now that the WASM hook is built, it can be run with Kiwi. The following command will run Kiwi with the WASM hook and the provided configuration file: | ||
|
||
```sh | ||
docker run -p 8000:8000 -v $(pwd)/kiwi.yml:/etc/kiwi/config/kiwi.yml \ | ||
-v $(pwd)/target/wasm32-wasi/debug/authenticate_http.wasm:/etc/kiwi/hook/authenticate.wasm \ | ||
ghcr.io/rkrishn7/kiwi:main | ||
``` | ||
|
||
### Establishing a Connection | ||
|
||
Now we can interact with the Kiwi server at `ws://localhost:8000`. Let's try it out by subscribing to a counter source and emitting some events. First, let's try to connect to the server using `wscat`, without providing an `x-api-key` query parameter: | ||
|
||
```sh | ||
wscat -c ws://127.0.0.1:8000 | ||
``` | ||
|
||
The connection should be rejected by the server. Now, let's try to connect to the server, making sure to provide an `x-api-key` query parameter: | ||
|
||
```sh | ||
wscat -c ws://127.0.0.1:8000/some-path?x-api-key=secret | ||
``` | ||
|
||
Success! The connection should be accepted by the server. Behind the scenes, your WASM hook is making an outbound HTTP request to a mock authentication server to verify the `x-api-key` query parameter. | ||
|
||
## Recap | ||
|
||
This example demonstrated how to write an authentication hook using the Kiwi SDK and load it into Kiwi for execution. The hook makes an outbound HTTP request to a mock authentication server to verify the `x-api-key` query parameter. | ||
|
||
Real-world use cases for this type of hook include verifying JWT tokens, checking for specific user roles, and more. |
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,12 @@ | ||
hooks: | ||
authenticate: '/etc/kiwi/hook/authenticate.wasm' | ||
|
||
sources: | ||
- type: counter | ||
id: counter1 | ||
interval_ms: 1000 | ||
lazy: true | ||
min: 0 | ||
|
||
server: | ||
address: '0.0.0.0:8000' |
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