Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add initial static component executor #237

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

danbev
Copy link
Contributor

@danbev danbev commented Jul 14, 2023

This commit adds a composed webassembly component model module that uses the policy engine module.

The idea here is that we can take a policy, perhaps created in the playground, and include it in the static component.


Example usage

To generate a .wasm module we specify a policy file and the policy name. We also have to specify a modules directory where the engine component, and the static-evaluator component are located:

$ wasm-generator -p policy.dog -n dog -m modules -o .
Created webassembly component: "working/target/static-config-component.wasm"
Composed into webassembly component:
"dog-composed.wasm"

With dog-composed.wasm we can then run it in a wasm runtime:

$ wasm-runtime -m dog-composed.wasm -i '{"name": "goodboy", "trained": true}'
"Ok: Because all fields were satisfied"

Sizes of component modules

$ cd engine/static-component
$ make show-component-sizes 
2.9M modules/seedwing-policy-engine-component.wasm
823K modules/static-evaluator-component.wasm
3.7M wasm-generator/working/target/dog-composed.wasm

Comments

I initially searched for a way to update a .wasm module with a policy which would have meant that we did not have to compile a new core wasm module. I looked at adding the policy as a custom section of the wasm module but that is only available from the "outside" of the module not from within which is what we want. Since we want to be able to run this from the command line, and from a server environment, we don't want to have to have the whole repository checked out to be able to generate this "static" component module.

What I've done in this draft is create a webassembly component module named static-evaluator that imports the policy engine, and also imports a component named static-config. The static-config component is what we then need to generate for each standalone .wasm. The way this is done is by generating a project on the file system, including the source for the static config component. This source has the wit is inlined so it does not have to have access to the wit files in this repository. This will then be compiled to a core wasm module, then programatically create a component of that core wasm module. Then we compose a new component using the component we just created and the policy-engine component. This generate component is what can then be run in a wasm runtime that supports the component model.

The drawbacks of the current solution is that it requires the environment this is run on to have a Rust compiler and also support the target wasm32-unknown-unknown. And the policy-engine component and the static-evaluator component must also be available on the file system.

Outstanding tasks

  • Add support for multiple policies to be created efficiently.
    Currently there is only one source file being generated in one project. But it would better to perhaps have a workspace generated and then have a member crate for each policy being generated. That way they should be able to share the same dependencies and at the same time not overwrite each other. (Hope that makes sense).
  • There was an issue when compiling the static-executor component as a --release build causing a compilation error.

Follow up tasks

  • Figure out how reporting should work. One idea is to add an option which will then cause more details of why a rule/pattern was not satisfied.

@danbev danbev force-pushed the static-wasm-component branch from 56b09f7 to 3fc8619 Compare August 14, 2023 06:20
@danbev danbev marked this pull request as ready for review August 17, 2023 05:59
This commit adds a composed webassembly component model module that uses
the policy engine module.

The idea here is that we can take a policy, perhaps created in the
playground, and include it in the static component. This component
will import the policy engine and call the eval function in the policy
engine component module. This way a user can have a self contained
.wasm which can be executed by and wasm runtime that supports the
webassembly component model.

Signed-off-by: Daniel Bevenius <[email protected]>
@danbev danbev force-pushed the static-wasm-component branch from fc8aea0 to 1478951 Compare August 17, 2023 06:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant