rego policy dissimilar results in golang and rego playground #309
-
Hello OPA's developer, I am so sorry to bother yours. I have a problem, the rego policy in playground testing is ok, but in golang is fault, I refer to many tutorials cannot solve this problem. This is my playground adderss: https://play.openpolicyagent.org/p/JFdryx8eqW This is my rego, check package k8s
import future.keywords.if
import future.keywords.in
default allow := false
allow if {
admin_verbs := {"create", "list", "delete", "update"}
admin_groups := {"admin"}
groups := {v | v := input.spec.group[_]}
count(admin_groups & groups) > 0
input.spec.resourceAttributes.verb in admin_verbs
}
allow if {
conf_groups := {"conf"}
conf_verbs := {"list"}
groups := {v | v := input.spec.group[_]}
count(conf_groups & groups) > 0
input.spec.resourceAttributes.verb in conf_verbs
} This is request {
"apiVersion": "authorization.k8s.io/v1beta1",
"kind": "SubjectAccessReview",
"metadata": {
"creationTimestamp": null
},
"spec": {
"group": [
"admin",
"system:authenticated"
],
"resourceAttributes": {
"namespace": "default",
"resource": "pods",
"verb": "list",
"version": "v1"
},
"uid": "searchUser",
"user": "searchUser"
},
"status": {
"allowed": false
}
} The result as expected in the playground. It's my golang code package rbac
import (
"context"
"fmt"
"github.com/open-policy-agent/opa/rego"
authoV1 "k8s.io/api/authorization/v1"
"k8s.io/klog/v2"
)
var module = `package k8s
import future.keywords.in
default allow = false
admin_verbs := {"create", "list", "delete", "update"}
admin_groups := {"admin"}
conf_groups := {"conf"}
conf_verbs := {"list"}
allow {
groups := {v | v := input.spec.group[_]}
count(admin_groups & groups) > 0
input.spec.resourceAttributes.verb in admin_verbs
}
allow {
groups := {v | v := input.spec.group[_]}
count(conf_groups & groups) > 0
input.spec.resourceAttributes.verb in conf_verbs
}
`
func RBACChek(req authoV1.SubjectAccessReview) bool {
query, err := rego.New(
rego.Input(req),
rego.Query("data.k8s == true"),
rego.Module("k8s.allow", module),
).PrepareForEval(context.TODO())
if err != nil {
klog.V(4).Info(err)
return false
}
result, err := query.Eval(context.TODO())
if err != nil {
klog.V(4).Info("evaluation error:", err)
return false
} else if len(result) == 0 {
klog.V(4).Info("undefined result", err)
return false
// Handle undefined result.
}
fmt.Printf("\n%+v\n", result.Allowed())
fmt.Printf("\n%+v\n", result)
return result.Allowed()
} This is my code output, You can see that the requested parameters should be allowed, but the code returns false
|
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
That query looks weird: rego.Query("data.k8s == true"), The playground will evaluate the full package extent, i.e. But that said, I suppose the query you're looking for is probably |
Beta Was this translation helpful? Give feedback.
-
change go lib, rego in golang work is ok, thank you OPA's developer srenatus. It's my really too careless |
Beta Was this translation helpful? Give feedback.
That query looks weird:
The playground will evaluate the full package extent, i.e.
data.k8s
, when you don't use "Evaluate Selection" with some rule highlighted.But that said, I suppose the query you're looking for is probably
rego.Query("data.k8s.allow")
.