sangkeon / java-opa-wasm

Apache License 2.0
15 stars 2 forks source link

Error occurred when policy contains call to regex.split method #11

Open cch0 opened 1 year ago

cch0 commented 1 year ago

Description

Rego Policy

package play

import future.keywords.if

default hello := false

hello := true if {
    regex.split("[|]", "a|b")
}

Versions

sangkeon commented 1 year ago

It's because regex.split builtin function is not supported.

I tested the rego code on npm-opa-wasm, it worked because npm-opa-wasm supported the builtin function. I tested the rego code on python-opa-wasm, I got following error.

Traceback (most recent call last): File "/home/sangkenlee/pipregextest/test.py", line 4, in policy = OPAPolicy('./policy.wasm') File "/home/sangkenlee/.local/lib/python3.10/site-packages/opa_wasm/opa_wasm.py", line 53, in init self.builtins_by_id = self._create_builtins_map(builtins if builtins else {}) File "/home/sangkenlee/.local/lib/python3.10/site-packages/opa_wasm/opa_wasm.py", line 159, in _create_builtins_map raise LookupError(f"A required builtin '{function_name}' function was not provided.") LookupError: A required builtin 'regex.split' function was not provided.

May be the error occured in the java-opa-wam for the same reason.

I'll add checking for not supported builtin functions like the implementation of the python-opa-wasm in the next release. I'll check implementing regex.split in the opa-java-wasm is possible when I have a time. Or ask the OPA team, they can include regex.split implementation in WASM on the policy.wasm in the future version of the OPA

Thanks.

cch0 commented 1 year ago

Thank you for the explanation.

Thanks again.

sangkeon commented 1 year ago

regex.split could be added to the java-opa-wasm. Changing wasmtime-java is not required.

It could be added to OPAModule.initImports().

In the npm-opa-wasm, implementation of regex.split is very straight foward.(https://github.com/open-policy-agent/npm-opa-wasm/blob/main/src/builtins/regex.js)

But implenting the code in the Java working exactly same may be not easy work.