Skip to content

Commit

Permalink
deno fmt: fix issues
Browse files Browse the repository at this point in the history
Signed-off-by: Stephan Renatus <stephan.renatus@gmail.com>
  • Loading branch information
srenatus committed Sep 13, 2021
1 parent d1a191c commit dfadee0
Show file tree
Hide file tree
Showing 14 changed files with 444 additions and 336 deletions.
79 changes: 43 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
**Work in Progress -- Contributions welcome!!**
**Work in Progress -- Contributions welcome!!**

# Open Policy Agent WebAssemby NPM Module

This is the source for the
[@open-policy-agent/opa-wasm](https://www.npmjs.com/package/@open-policy-agent/opa-wasm)
NPM module which is a small SDK for using WebAssembly (wasm) compiled
NPM module which is a small SDK for using WebAssembly (wasm) compiled
[Open Policy Agent](https://www.openpolicyagent.org/) Rego policies.

# Getting Started

## Install the module

```
npm install @open-policy-agent/opa-wasm
npm install @open-policy-agent/opa-wasm
```

## Usage
Expand All @@ -26,34 +28,35 @@ const { loadPolicy } = require("@open-policy-agent/opa-wasm");
### Load the policy

```javascript
loadPolicy(policyWasm)
loadPolicy(policyWasm);
```
The `loadPolicy` function returns a Promise with the loaded policy.
Typically this means loading it in an `async` function like:

The `loadPolicy` function returns a Promise with the loaded policy. Typically
this means loading it in an `async` function like:

```javascript
const policy = await loadPolicy(policyWasm)
const policy = await loadPolicy(policyWasm);
```

Or something like:

```javascript
loadPolicy(policyWasm).then(policy => {
// evaluate or save the policy
}, error => {
console.error("Failed to load policy: " + error)
})
loadPolicy(policyWasm).then((policy) => {
// evaluate or save the policy
}, (error) => {
console.error("Failed to load policy: " + error);
});
```

The `policyWasm` needs to be either the raw byte array of
the compiled policy Wasm file, or a WebAssembly module.
The `policyWasm` needs to be either the raw byte array of the compiled policy
Wasm file, or a WebAssembly module.

For example:

```javascript
const fs = require('fs');
const fs = require("fs");

const policyWasm = fs.readFileSync('policy.wasm');
const policyWasm = fs.readFileSync("policy.wasm");
```

Alternatively the bytes can be pulled in remotely from a `fetch` or in some
Expand All @@ -65,49 +68,53 @@ javascript context through external APIs.
The loaded policy object returned from `loadPolicy()` has a couple of important
APIs for policy evaluation:

`setData(obj)` -- Provide an external `data` document for policy evaluation. Requires a JSON serializable object.
`evaluate(input)` -- Evaluates the policy using any loaded data and the supplied `input` document.
`setData(obj)` -- Provide an external `data` document for policy evaluation.
Requires a JSON serializable object. `evaluate(input)` -- Evaluates the policy
using any loaded data and the supplied `input` document.

The `input` parameter must be a JSON string.

Example:

```javascript

input = '{"path": "/", "role": "admin"}';

loadPolicy(policyWasm).then(policy => {
resultSet = policy.evaluate(input);
if (resultSet == null) {
console.error("evaluation error")
}
if (resultSet.length == 0) {
console.log("undefined")
}
console.log("allowed = " + allowed[0].result);
}).catch( error => {
console.error("Failed to load policy: ", error);
})
loadPolicy(policyWasm).then((policy) => {
resultSet = policy.evaluate(input);
if (resultSet == null) {
console.error("evaluation error");
}
if (resultSet.length == 0) {
console.log("undefined");
}
console.log("allowed = " + allowed[0].result);
}).catch((error) => {
console.error("Failed to load policy: ", error);
});
```

> For any `opa build` created WASM binaries the result set, when defined, will
contain a `result` key with the value of the compiled entrypoint. See
[https://www.openpolicyagent.org/docs/latest/wasm/](https://www.openpolicyagent.org/docs/latest/wasm/)
for more details.
> contain a `result` key with the value of the compiled entrypoint. See
> [https://www.openpolicyagent.org/docs/latest/wasm/](https://www.openpolicyagent.org/docs/latest/wasm/)
> for more details.
## Writing the policy

See [https://www.openpolicyagent.org/docs/latest/how-do-i-write-policies/](https://www.openpolicyagent.org/docs/latest/how-do-i-write-policies/)
See
[https://www.openpolicyagent.org/docs/latest/how-do-i-write-policies/](https://www.openpolicyagent.org/docs/latest/how-do-i-write-policies/)

## Compiling the policy

Either use the [Compile REST API](https://www.openpolicyagent.org/docs/latest/rest-api/#compile-api) or `opa build` CLI tool.
Either use the
[Compile REST API](https://www.openpolicyagent.org/docs/latest/rest-api/#compile-api)
or `opa build` CLI tool.

For example, with OPA v0.20.5+:

```bash
opa build -t wasm -e 'example/allow' example.rego
```

Which is compiling the `example.rego` policy file with the result set to
`data.example.allow`. The result will be an OPA bundle with the `policy.wasm`
binary included. See [./examples](./examples) for a more comprehensive example.
Expand Down
21 changes: 14 additions & 7 deletions examples/nodejs-app/README.md
Original file line number Diff line number Diff line change
@@ -1,37 +1,43 @@
# Simple opa-wasm node application

The application is in [app.js](./app.js) and shows loading a `*.wasm` file, initializing
the policy, and evaluating it with input.
The application is in [app.js](./app.js) and shows loading a `*.wasm` file,
initializing the policy, and evaluating it with input.

## Install dependencies

This requires the `opa-wasm` package, see [package.json](./package.json) for details.
This requires the `opa-wasm` package, see [package.json](./package.json) for
details.

```bash
npm install
```

> The example uses a local path, in "real" use-cases use the standard NPM module.
> The example uses a local path, in "real" use-cases use the standard NPM
> module.
## Build the WebAssembly binary for the example policy:

> The syntax shown below requires OPA v0.20.5+
There is an example policy included with the example, see [example.rego](./example.rego)
There is an example policy included with the example, see
[example.rego](./example.rego)

```bash
opa build -t wasm -e 'example/hello' ./example.rego
tar -xzf ./bundle.tar.gz /policy.wasm
```

This will create a bundle tarball with the WASM binary included, and then unpack just the `policy.wasm` from the bundle.
This will create a bundle tarball with the WASM binary included, and then unpack
just the `policy.wasm` from the bundle.

## Run the example Node JS code that invokes the WASM binary:

```bash
node app.js '{"message": "world"}'
```

Produces:

```
[
{
Expand All @@ -40,11 +46,12 @@ Produces:
]
```


```bash
node app.js '{"message": "not-world"}'
```

Produces:

```
[
{
Expand Down
32 changes: 15 additions & 17 deletions examples/nodejs-app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,25 @@
// Use of this source code is governed by an Apache2
// license that can be found in the LICENSE file.

const fs = require('fs');
const fs = require("fs");
const { loadPolicy } = require("@open-policy-agent/opa-wasm");

// Read the policy wasm file
const policyWasm = fs.readFileSync('policy.wasm');
const policyWasm = fs.readFileSync("policy.wasm");

// Load the policy module asynchronously
loadPolicy(policyWasm).then(policy => {
loadPolicy(policyWasm).then((policy) => {
// Use console parameters for the input, do quick
// validation by json parsing. Not efficient.. but
// will raise an error
const input = JSON.parse(process.argv[2]);
// Provide a data document with a string value
policy.setData({ world: "world" });

// Use console parameters for the input, do quick
// validation by json parsing. Not efficient.. but
// will raise an error
const input = JSON.parse(process.argv[2]);
// Provide a data document with a string value
policy.setData({world: "world"});

// Evaluate the policy and log the result
const result = policy.evaluate(input);
console.log(JSON.stringify(result, null, 2))

}).catch(err => {
console.log("ERROR: ", err);
process.exit(1);
// Evaluate the policy and log the result
const result = policy.evaluate(input);
console.log(JSON.stringify(result, null, 2));
}).catch((err) => {
console.log("ERROR: ", err);
process.exit(1);
});
11 changes: 6 additions & 5 deletions examples/nodejs-ts-app-multi-entrypoint/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Multi-entrypoint OPA-WASM node demo script

This script demos loading a WASM OPA file and simulates 1,000,000 evaluations
on a few different entrypoints to demonstrate how entrypoints can be used.
This script demos loading a WASM OPA file and simulates 1,000,000 evaluations on
a few different entrypoints to demonstrate how entrypoints can be used.

## Install dependencies

Expand All @@ -11,9 +11,9 @@ npm install

## Build the WebAssembly binary for the example policies

There are two example policies located in the ./policies directory, these
are compiled into a WASM. Look in the package.json to see how the entrypoints
are defined.
There are two example policies located in the ./policies directory, these are
compiled into a WASM. Look in the package.json to see how the entrypoints are
defined.

> Tested with OPA v0.27.1
Expand All @@ -28,6 +28,7 @@ npm start
```

Sample Output

```
Running multi entrypoint demo suite
Iterations: 100000 iterations of 10 inputs for 1000000 total evals per entrypoint
Expand Down
Loading

0 comments on commit dfadee0

Please sign in to comment.