mirror of
https://github.com/microsoft/regorus.git
synced 2026-08-05 02:16:11 +00:00
- Add PolicyLengthConfig struct with max_col, max_file_bytes, and max_lines fields, replacing hardcoded constants in the lexer. - Add Engine::set_policy_length_config and clear_policy_length_config to allow callers to override the default limits. - Add Source::from_contents_with_limits and from_file_with_limits for direct Source construction with custom limits; existing from_contents and from_file signatures are preserved using defaults. - Add tests for default rejection, custom limits, and engine plumbing. - Add bindings for C, C++, Python, WASM/JS, Java, Ruby, C#, Go
110 lines
2.7 KiB
C
110 lines
2.7 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 != Ok)
|
|
goto error;
|
|
|
|
// Raise the default col limit to 2000
|
|
RegorusPolicyLengthConfig len_config = { .max_col = 2000, .max_file_bytes = 1048576, .max_lines = 20000 };
|
|
r = regorus_engine_set_policy_length_config(engine, len_config);
|
|
if (r.status != Ok)
|
|
goto error;
|
|
regorus_result_drop(r);
|
|
|
|
// Load policies.
|
|
r = regorus_engine_add_policy_from_file(engine, "../../../tests/aci/framework.rego");
|
|
if (r.status != Ok)
|
|
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 != Ok)
|
|
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 != Ok)
|
|
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 != Ok)
|
|
goto error;
|
|
regorus_result_drop(r);
|
|
|
|
// Set input
|
|
r = regorus_engine_set_input_from_json_file(engine, "../../../tests/aci/input.json");
|
|
if (r.status != Ok)
|
|
goto error;
|
|
regorus_result_drop(r);
|
|
|
|
// Eval rule.
|
|
r = regorus_engine_eval_query(engine, "data.framework.mount_overlay");
|
|
if (r.status != Ok)
|
|
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 != Ok)
|
|
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 != Ok)
|
|
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 != Ok)
|
|
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;
|
|
}
|