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

Update bindings to include recently added APIs #250

Merged
merged 1 commit into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .github/workflows/test-ffi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: bindings/c-cpp

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
test:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Test FFI
run: |
cargo build -r
cargo clippy --all-targets --no-deps -- -Dwarnings
working-directory: ./bindings/ffi
4 changes: 3 additions & 1 deletion .github/workflows/test-java.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ jobs:
- uses: dtolnay/rust-toolchain@stable

- name: Building binding
run: cargo build --release --manifest-path bindings/java/Cargo.toml
run: |
cargo clippy --all-targets --no-deps -- -Dwarnings
cargo build --release --manifest-path bindings/java/Cargo.toml

- name: Build jar
run: mvn package
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/test-python.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ jobs:
run: |
pip3 install dist/regorus-*.whl
cd bindings/python
cargo clippy --all-targets --no-deps -- -Dwarnings
python3 test.py
1 change: 1 addition & 0 deletions .github/workflows/test-ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ jobs:
- name: Run ruby tests
run: |
cd bindings/ruby
cargo clippy --all-targets --no-deps -- -Dwarnings
bundle exec rake
2 changes: 2 additions & 0 deletions .github/workflows/test-wasm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,7 @@ jobs:
- name: Test wasm binding
run: |
cd bindings/wasm
cargo clippy --all-targets --no-deps -- -Dwarnings
wasm-pack build --target nodejs --release
wasm-pack test --release --node
node test.js
9 changes: 8 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,11 @@ Cargo.lock
worktrees/

# build folders
**/build
**/build

# Generated C# bindings
**/*.g.cs

# Generated C, C++ headers
bindings/ffi/regorus.h
bindings/ffi/regorus.ffi.hpp
10 changes: 5 additions & 5 deletions bindings/c-nostd/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,21 @@ int main() {
free(buffer);
if (r.status != RegorusStatusOk)
goto error;
printf("Loaded policy %s\n", r.output);
printf("Loaded package %s\n", r.output);
regorus_result_drop(r);

r = regorus_engine_add_policy(engine, "api.rego", (buffer = file_to_string("../../../tests/aci/api.rego")));
free(buffer);
if (r.status != RegorusStatusOk)
goto error;
printf("Loaded policy %s\n", r.output);
printf("Loaded package %s\n", r.output);
regorus_result_drop(r);

r = regorus_engine_add_policy(engine, "policy.rego", (buffer = file_to_string("../../../tests/aci/policy.rego")));
free(buffer);
if (r.status != RegorusStatusOk)
goto error;
printf("Loaded policy %s\n", r.output);
printf("Loaded package %s\n", r.output);
regorus_result_drop(r);

// Add data
Expand All @@ -77,8 +77,8 @@ int main() {
goto error;
regorus_result_drop(r);

// Eval query
r = regorus_engine_eval_query(engine, "data.framework.mount_overlay=x");
// Eval rule.
r = regorus_engine_eval_rule(engine, "data.framework.mount_overlay");
if (r.status != RegorusStatusOk)
goto error;

Expand Down
2 changes: 1 addition & 1 deletion bindings/c/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ corrosion_import_crate(
FEATURES "regorus/semver"

# Link statically
CRATE_TYPES "staticlib"
CRATE_TYPES "cdylib"
)

add_executable(regorus_test main.c)
Expand Down
53 changes: 46 additions & 7 deletions bindings/c/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ int main() {
r = regorus_engine_add_policy_from_file(engine, "../../../tests/aci/framework.rego");
if (r.status != RegorusStatusOk)
goto error;
printf("Loaded policy %s\n", r.output);
printf("Loaded package %s\n", r.output);
regorus_result_drop(r);

r = regorus_engine_add_policy_from_file(engine, "../../../tests/aci/api.rego");
if (r.status != RegorusStatusOk)
goto error;
printf("Loaded policy %s\n", r.output);
printf("Loaded package %s\n", r.output);
regorus_result_drop(r);

r = regorus_engine_add_policy_from_file(engine, "../../../tests/aci/policy.rego");
if (r.status != RegorusStatusOk)
goto error;
printf("Loaded policy %s\n", r.output);
printf("Loaded package %s\n", r.output);
regorus_result_drop(r);

// Add data
Expand All @@ -37,22 +37,61 @@ int main() {
goto error;
regorus_result_drop(r);

// Eval query
r = regorus_engine_eval_query(engine, "data.framework.mount_overlay=x");
// Eval rule.
r = regorus_engine_eval_query(engine, "data.framework.mount_overlay");
if (r.status != RegorusStatusOk)
goto error;

// Print output
printf("%s", r.output);
printf("%s\n", r.output);
regorus_result_drop(r);


// Free the engine.
regorus_engine_drop(engine);

// Create another engine.
engine = regorus_engine_new();

r = regorus_engine_add_policy(
engine,
"test.rego",
"package test\n"
"x = 1\n"
"message = `Hello`"
);

// Evaluate rule.
if (r.status != RegorusStatusOk)
goto error;

r = regorus_engine_set_enable_coverage(engine, true);
regorus_result_drop(r);

r = regorus_engine_eval_query(engine, "data.test.message");
if (r.status != RegorusStatusOk)
goto error;

// Print output
printf("%s\n", r.output);
regorus_result_drop(r);

// Print pretty coverage report.
r = regorus_engine_get_coverage_report_pretty(engine);
if (r.status != RegorusStatusOk)
goto error;

printf("%s\n", r.output);
regorus_result_drop(r);

// Free the engine.
regorus_engine_drop(engine);

return 0;

error:
printf("%s", r.error_message);
regorus_result_drop(r);
regorus_engine_drop(engine);

return 1;
}
2 changes: 1 addition & 1 deletion bindings/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ corrosion_import_crate(
FEATURES "regorus/semver"

# Link statically
CRATE_TYPES "staticlib")
CRATE_TYPES "cdylib")

add_executable(regorus_test main.cpp)
# Add path to <regorus-source-folder>/bindings/ffi
Expand Down
18 changes: 14 additions & 4 deletions bindings/cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ void example()
// Create engine
regorus::Engine engine;

engine.set_enable_coverage(true);

// Add policies.
engine.add_policy("objects.rego",R"(package objects

Expand Down Expand Up @@ -67,6 +69,14 @@ f := e["dev"])");
} else {
std::cerr<<result.error()<<std::endl;
}

// Print coverage report
auto result1 = engine.get_coverage_report_pretty();
if (result1) {
std::cout<<result1.output()<<std::endl;
} else {
std::cerr<<result1.error()<<std::endl;
}
}

int main() {
Expand All @@ -89,7 +99,7 @@ int main() {
std::cerr<<result.error()<<std::endl;
return -1;
}
std::cout<<"Loaded policy "<<result.output()<< std::endl;
std::cout<<"Loaded package "<<result.output()<< std::endl;
}
{
auto result = engine.add_data_from_json_file("../../../tests/aci/data.json");
Expand All @@ -99,21 +109,21 @@ int main() {
}
}

// Set input and eval query.
// Set input and eval rule.
{
auto result = engine.set_input_from_json_file("../../../tests/aci/input.json");
if (!result) {
std::cerr<<result.error()<<std::endl;
return -1;
}
}
auto result = engine.eval_query("data.framework.mount_overlay = x");
auto result = engine.eval_rule("data.framework.mount_overlay");
if (!result) {
std::cerr<<result.error()<<std::endl;
return -1;
}

std::cout<<result.output()<<std::endl;

example();
}
20 changes: 20 additions & 0 deletions bindings/cpp/regorus.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,26 @@ namespace regorus {
return Result(regorus_engine_eval_query(engine, query));
}

Result eval_rule(const char* rule) {
return Result(regorus_engine_eval_rule(engine, rule));
}

Result set_enable_coverage(bool enable) {
return Result(regorus_engine_set_enable_coverage(engine, enable));
}

Result clear_coverage_data() {
return Result(regorus_engine_clear_coverage_data(engine));
}

Result get_coverage_report() {
return Result(regorus_engine_get_coverage_report(engine));
}

Result get_coverage_report_pretty() {
return Result(regorus_engine_get_coverage_report_pretty(engine));
}

~Engine() {
regorus_engine_drop(engine);
}
Expand Down
15 changes: 11 additions & 4 deletions bindings/csharp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,26 @@

w.Restart();

// Set input and eval query.
// Set input and eval rule.
engine.SetInputFromJsonFile("../../tests/aci/input.json");
var results = engine.EvalQuery("data.framework.mount_overlay = x");
var resultsDoc = System.Text.Json.JsonDocument.Parse(results);
var value = engine.EvalQuery("data.framework.mount_overlay");
var valueDoc = System.Text.Json.JsonDocument.Parse(value);

w.Stop();
var evalTicks = w.ElapsedTicks;

Console.WriteLine("{0}", results);
Console.WriteLine("{0}", valueDoc);


Console.WriteLine("Engine creation took {0} msecs", (newEngineTicks*nanosecPerTick)/(1000.0*1000.0));
Console.WriteLine("Load policies and data took {0} msecs", (loadPoliciesTicks*nanosecPerTick)/(1000.0*1000.0));
Console.WriteLine("EvalQuery took {0} msecs", (evalTicks*nanosecPerTick)/(1000.0*1000.0));

engine = new Regorus.Engine();
engine.AddPolicy(
"test.rego",
"package test\nx = 1\nmessage = `Hello`");

engine.SetEnableCoverage(true);
Console.WriteLine("{0}", engine.EvalRule("data.test.message"));
Console.WriteLine("{0}", engine.GetCoverageReportPretty());
Loading
Loading