Skip to content

rob-blackbourn/rust-plugin-experiment

Repository files navigation

Rust Plugin Experiment

An attempt to create a plugin for a rust system using separate processes communicating using stdin/stdout.

The Idea

Rather than import a dynamic library, this approach uses out of process executables than are launched by a service. Communication is achieved using stdin/stdout.

The Experiments

Experiment a

A service spawns a plugin in a new process.

The service receives lines from the terminal, and sends them to the plugin. The plugin reads lines from stdin and echoes them to stdout. The service reads lines from the stdout of the plugin, and sends them to stdout.

As the plugin isn't on the path, the plugin must have an absolute path (./a-plugin) which means the service must be run from the same folder (target/debug).

cd target/debug
./a-start

Experiment b

This experiment solves the issue in experiment a whereby the plugin must have an absolute path by discovering the folder from which the service was run from, and adding it to the path.

This time we can run the service with cargo.

cargo run --bin=b-service --package=b-service

Experiment c

This experiment introduces configuration through command line arguments. The service takes two command line arguments: --plugin-path and --plugin-cmdline. It adds the plugin path to the environment before launching the plugin, and passes the provided command line to the plugin.

The plugin command line must be passed as a vector of args (as if they were provided by the shell). To do this it uses the shell-words package.

cargo run --bin=c-service --package=c-service -- \
    --plugin-path $(pwd)/target/debug \
    --plugin-cmdline  "--filename foo --is-happy

Experiment d

This experiment brings all the elements together in creating an authentication plugin.

The plugin takes the argument --password-file <file> which specifies the location of a password file created using the htpasswd utility. There is an example file in d-etc/ht.passwd with the user names: "tom", "dick" and "harry". The password is the user name (so the password to "tom" is "tom").

The service and plugin communicate using JSON messages.

cargo run --bin=d-service --package=d-service -- \
    --plugin-path $(pwd)/target/debug \
    --plugin-cmdline "--password-file $(pwd)/d-etc/ht.passwd"

About

An experiment with creating plugins for rust

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages