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>
104 lines
2.5 KiB
C
104 lines
2.5 KiB
C
#include <stdio.h>
|
|
#include "regorus.h"
|
|
|
|
|
|
// Regorus has been built for no_std and cannot access files.
|
|
char* file_to_string(const char* file) {
|
|
char * buffer = 0;
|
|
long length;
|
|
FILE * f = fopen (file, "rb");
|
|
|
|
if (f)
|
|
{
|
|
fseek (f, 0, SEEK_END);
|
|
length = ftell (f);
|
|
fseek (f, 0, SEEK_SET);
|
|
buffer = malloc (length + 1);
|
|
buffer[length] = '\0';
|
|
if (buffer)
|
|
{
|
|
fread (buffer, 1, length, f);
|
|
}
|
|
fclose (f);
|
|
}
|
|
|
|
return buffer;
|
|
}
|
|
|
|
// If regorus is built with custom-allocator, then provide implementation.
|
|
uint8_t* regorus_aligned_alloc(size_t alignment, size_t size) {
|
|
return (uint8_t*) aligned_alloc(alignment, size);
|
|
}
|
|
|
|
void regorus_free(uint8_t* ptr) {
|
|
free(ptr);
|
|
}
|
|
|
|
|
|
int main() {
|
|
// Create engine.
|
|
RegorusEngine* engine = regorus_engine_new();
|
|
RegorusResult r;
|
|
char* buffer = NULL;
|
|
|
|
// 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(engine, "framework.rego", (buffer = file_to_string("../../../tests/aci/framework.rego")));
|
|
free(buffer);
|
|
if (r.status != RegorusStatusOk)
|
|
goto error;
|
|
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 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 package %s\n", r.output);
|
|
regorus_result_drop(r);
|
|
|
|
// Add data
|
|
r = regorus_engine_add_data_json(engine, (buffer = file_to_string("../../../tests/aci/data.json")));
|
|
free(buffer);
|
|
if (r.status != RegorusStatusOk)
|
|
goto error;
|
|
regorus_result_drop(r);
|
|
|
|
// Set input
|
|
r = regorus_engine_set_input_json(engine, (buffer = file_to_string("../../../tests/aci/input.json")));
|
|
free(buffer);
|
|
if (r.status != RegorusStatusOk)
|
|
goto error;
|
|
regorus_result_drop(r);
|
|
|
|
// Eval rule.
|
|
r = regorus_engine_eval_rule(engine, "data.framework.mount_overlay");
|
|
if (r.status != RegorusStatusOk)
|
|
goto error;
|
|
|
|
// Print output
|
|
printf("%s", r.output);
|
|
regorus_result_drop(r);
|
|
|
|
|
|
// Free the engine.
|
|
regorus_engine_drop(engine);
|
|
|
|
return 0;
|
|
error:
|
|
printf("%s", r.error_message);
|
|
|
|
return 1;
|
|
}
|