mirror of
https://github.com/microsoft/regorus.git
synced 2026-08-05 02:16:11 +00:00
Regorus now defaults to rego v1. `import rego.v1` is no longer needed. Additionally, `future` keywords are automatically imported. See https://www.openpolicyagent.org/docs/latest/v0-upgrade/#changes-to-rego-in-opa-v10 to understand the differences between rego v1 and v0. BREAKING CHANGE: v0 style policies will error out by default. To enable v0 behavior, call engine.set_rego_v0(true) before loading policies. Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>
103 lines
2.5 KiB
C
103 lines
2.5 KiB
C
#include <stdio.h>
|
|
#include "regorus.h"
|
|
|
|
int main() {
|
|
// Create engine.
|
|
RegorusEngine* engine = regorus_engine_new();
|
|
RegorusResult r;
|
|
|
|
// Turn on rego v0 since policy uses v0.
|
|
r = regorus_engine_set_rego_v0(engine, true);
|
|
if (r.status != RegorusStatusOk)
|
|
goto error;
|
|
|
|
// Load policies.
|
|
r = regorus_engine_add_policy_from_file(engine, "../../../tests/aci/framework.rego");
|
|
if (r.status != RegorusStatusOk)
|
|
goto error;
|
|
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 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 package %s\n", r.output);
|
|
regorus_result_drop(r);
|
|
|
|
// Add data
|
|
r = regorus_engine_add_data_from_json_file(engine, "../../../tests/aci/data.json");
|
|
if (r.status != RegorusStatusOk)
|
|
goto error;
|
|
regorus_result_drop(r);
|
|
|
|
// Set input
|
|
r = regorus_engine_set_input_from_json_file(engine, "../../../tests/aci/input.json");
|
|
if (r.status != RegorusStatusOk)
|
|
goto error;
|
|
regorus_result_drop(r);
|
|
|
|
// Eval rule.
|
|
r = regorus_engine_eval_query(engine, "data.framework.mount_overlay");
|
|
if (r.status != RegorusStatusOk)
|
|
goto error;
|
|
|
|
// Print 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;
|
|
}
|