@@ -10,9 +10,9 @@ import (
10
10
"github.com/layer5io/meshkit/utils"
11
11
"github.com/meshery/schemas/models/v1beta1/pattern"
12
12
"github.com/open-policy-agent/opa/rego"
13
-
14
13
"github.com/open-policy-agent/opa/storage"
15
14
"github.com/open-policy-agent/opa/storage/inmem"
15
+ "github.com/open-policy-agent/opa/topdown/print"
16
16
"github.com/sirupsen/logrus"
17
17
)
18
18
@@ -51,24 +51,44 @@ func NewRegoInstance(policyDir string, regManager *registry.RegistryManager) (*R
51
51
}, nil
52
52
}
53
53
54
+ // CustomPrintHook implements the print.Hook interface
55
+ type CustomPrintHook struct {
56
+ Messages []string
57
+ }
58
+
59
+ // Print captures print messages from policy evaluation
60
+ // Implements print.Hook interface
61
+ func (h * CustomPrintHook ) Print (ctx print.Context , s string ) error {
62
+ h .Messages = append (h .Messages , s )
63
+ logrus .Info ("[OPA] " , s )
64
+ return nil
65
+ }
66
+
54
67
// RegoPolicyHandler takes the required inputs and run the query against all the policy files provided
55
68
func (r * Rego ) RegoPolicyHandler (designFile pattern.PatternFile , regoQueryString string , relationshipsToEvalaute ... string ) (pattern.EvaluationResponse , error ) {
56
69
var evaluationResponse pattern.EvaluationResponse
57
70
if r == nil {
58
71
return evaluationResponse , ErrEval (fmt .Errorf ("policy engine is not yet ready" ))
59
72
}
73
+ // Create custom print hook
74
+ printHook := & CustomPrintHook {
75
+ Messages : make ([]string , 0 ),
76
+ }
60
77
regoEngine , err := rego .New (
78
+ rego .PrintHook (printHook ),
79
+ rego .EnablePrintStatements (true ), // Explicitly enable print statements
80
+ rego .Transaction (r .transaction ),
61
81
rego .Query (regoQueryString ),
62
82
rego .Load ([]string {r .policyDir }, nil ),
63
83
rego .Store (r .store ),
64
- rego .Transaction (r .transaction ),
65
84
).PrepareForEval (r .ctx )
66
85
if err != nil {
67
86
logrus .Error ("error preparing for evaluation" , err )
68
87
return evaluationResponse , ErrPrepareForEval (err )
69
88
}
70
89
71
90
eval_result , err := regoEngine .Eval (r .ctx , rego .EvalInput (designFile ))
91
+
72
92
if err != nil {
73
93
return evaluationResponse , ErrEval (err )
74
94
}
0 commit comments