-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun-rego.php
75 lines (65 loc) · 1.92 KB
/
run-rego.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?php
if(isset($_GET['task']))
{
$task = $_GET['task'];
}
else
{
die("ERROR: Invalid task number");
}
if(isset($_POST['rules']))
{
$rules = $_POST['rules'];
}
else
{
die("No rules provided");
}
if($task < 1 || $task > 7)
{
die("ERROR: Invalid task number");
}
$tmpfname = tempnam("/tmp", "rules");
rename($tmpfname, $tmpfname .= '.rego');
$handle = fopen($tmpfname, "w");
fwrite($handle, $rules);
fclose($handle);
$short_output = "";
$output = "";
$success = true;
for($i = 1; $i < 6; $i++)
{
$input = "./graphs/policy-" . strval($task) . "/input" . strval($i) . ".json";
$eval = shell_exec('./opa eval -d ' . $tmpfname . ' -i ' . $input . ' "data.study.final_policy"');
$correct = shell_exec('./opa eval -d ./graphs/policy-' . strval($task) . '/correct.rego -i ' . $input . ' "data.study.final_policy"');
$result = json_decode($eval);
$correct_result = json_decode($correct);
if(isset($result->result))
{
if(!isset($correct_result->result))
{
$short_output .= strval($i) . "v, ";
$output .= "Unexpected result: Input " . strval($i) . " should violate your policy.\n";
$success = false;
}
}
else if(isset($result->errors))
{
$output = "Error on line ". $result->errors[0]->location->row . " col " . $result->errors[0]->location->col . ": " . $result->errors[0]->message;
$short_output = "L" . $result->errors[0]->location->row . "C" . $result->errors[0]->location->col . ":" . $result->errors[0]->message;
$success = false;
break;
}
else
{
if(isset($correct_result->result))
{
$short_output .= strval($i) . "s, ";
$output .= "Unexpected result: Input " . strval($i) . " should satisfy your policy.\n";
$success = false;
}
}
}
$arr = array('success' => $success, 'text' => $output);
unlink($tmpfname);
echo json_encode($arr);