Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 19 additions & 20 deletions enforcer.go
Original file line number Diff line number Diff line change
Expand Up @@ -804,21 +804,6 @@
return true, nil
}

functions := e.fm.GetFunctions()
if _, ok := e.model["g"]; ok {
for key, ast := range e.model["g"] {
// g must be a normal role definition (ast.RM != nil)
// or a conditional role definition (ast.CondRM != nil)
// ast.RM and ast.CondRM shouldn't be nil at the same time
if ast.RM != nil {
functions[key] = util.GenerateGFunction(ast.RM, e.gFunctionCache)
}
if ast.CondRM != nil {
functions[key] = util.GenerateConditionalGFunction(ast.CondRM)
}
}
}

var (
rType = "r"
pType = "p"
Expand Down Expand Up @@ -883,11 +868,8 @@
}

hasEval := util.HasEval(expString)
if hasEval {
functions["eval"] = generateEvalFunction(functions, &parameters)
}
var expression *govaluate.EvaluableExpression
expression, err = e.getAndStoreMatcherExpression(hasEval, expString, functions)
expression, err = e.getAndStoreMatcherExpression(hasEval, expString, &parameters)
if err != nil {
return false, err
}
Expand Down Expand Up @@ -1032,14 +1014,31 @@
return result, nil
}

func (e *Enforcer) getAndStoreMatcherExpression(hasEval bool, expString string, functions map[string]govaluate.ExpressionFunction) (*govaluate.EvaluableExpression, error) {
func (e *Enforcer) getAndStoreMatcherExpression(hasEval bool, expString string, parameters *enforceParameters) (*govaluate.EvaluableExpression, error) {
var expression *govaluate.EvaluableExpression
var err error
cachedExpression, isPresent := e.matcherMap.Load(expString)

if !hasEval && isPresent {

Check failure on line 1022 in enforcer.go

View workflow job for this annotation

GitHub Actions / golangci

`if !hasEval && isPresent` has complex nested blocks (complexity: 8) (nestif)
expression = cachedExpression.(*govaluate.EvaluableExpression)
} else {
functions := e.fm.GetFunctions()
if _, ok := e.model["g"]; ok {
for key, ast := range e.model["g"] {
// g must be a normal role definition (ast.RM != nil)
// or a conditional role definition (ast.CondRM != nil)
// ast.RM and ast.CondRM shouldn't be nil at the same time
if ast.RM != nil {
functions[key] = util.GenerateGFunction(ast.RM, e.gFunctionCache)
}
if ast.CondRM != nil {
functions[key] = util.GenerateConditionalGFunction(ast.CondRM)
}
}
}
if hasEval {
functions["eval"] = generateEvalFunction(functions, parameters)
}
expression, err = govaluate.NewEvaluableExpressionWithFunctions(expString, functions)
if err != nil {
return nil, err
Expand Down
84 changes: 84 additions & 0 deletions matcher_functions_review_bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package casbin

import (
"testing"

"github.com/casbin/casbin/v3/model"
)

const reviewACLModel = `
[request_definition]
r = sub, obj, act
[policy_definition]
p = sub, obj, act
[policy_effect]
e = some(where (p.eft == allow))
[matchers]
m = r.sub == p.sub && r.obj == p.obj && r.act == p.act
`

const reviewRBACModel = `
[request_definition]
r = sub, obj, act
[policy_definition]
p = sub, obj, act
[role_definition]
g = _, _
[policy_effect]
e = some(where (p.eft == allow))
[matchers]
m = g(r.sub, p.sub) && r.obj == p.obj && r.act == p.act
`

func BenchmarkReviewEnforce(b *testing.B) {
for _, tc := range []struct{ name, text, subject string }{
{"ACL", reviewACLModel, "alice"},
{"RBAC", reviewRBACModel, "admin"},
} {
b.Run(tc.name, func(b *testing.B) {
m, err := model.NewModelFromString(tc.text)
if err != nil {
b.Fatal(err)
}
e, err := NewSyncedEnforcer(m)
if err != nil {
b.Fatal(err)
}
if _, err := e.AddPolicy(tc.subject, "data", "read"); err != nil {
b.Fatal(err)
}
if tc.name == "RBAC" {
if _, err := e.AddGroupingPolicy("alice", "admin"); err != nil {
b.Fatal(err)
}
}
if ok, err := e.Enforce("alice", "data", "read"); err != nil || !ok {
b.Fatal(ok, err)
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
if ok, err := e.Enforce("alice", "data", "read"); err != nil || !ok {
b.Fatal(ok, err)
}
}
})
}
}
93 changes: 93 additions & 0 deletions matcher_functions_review_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package casbin

import (
"testing"

"github.com/casbin/casbin/v3/model"
)

func TestReviewEvalUsesCurrentRequest(t *testing.T) {
m, err := model.NewModelFromString(`
[request_definition]
r = sub, obj, act
[policy_definition]
p = sub_rule, obj, act
[policy_effect]
e = some(where (p.eft == allow))
[matchers]
m = eval(p.sub_rule) && r.obj == p.obj && r.act == p.act
`)
if err != nil {
t.Fatal(err)
}
e, err := NewSyncedEnforcer(m)
if err != nil {
t.Fatal(err)
}
if _, err := e.AddPolicy(`r.sub == "alice"`, "data", "read"); err != nil {
t.Fatal(err)
}
for _, subject := range []string{"alice", "bob", "alice", "carol"} {
ok, err := e.Enforce(subject, "data", "read")
if err != nil || ok != (subject == "alice") {
t.Fatalf("subject %s: %v, %v", subject, ok, err)
}
}
}

func TestReviewMatcherFunctionAfterInvalidation(t *testing.T) {
m, err := model.NewModelFromString(`
[request_definition]
r = sub, obj, act
[policy_definition]
p = sub, obj, act
[policy_effect]
e = some(where (p.eft == allow))
[matchers]
m = r.sub == p.sub && r.obj == p.obj && r.act == p.act
`)
if err != nil {
t.Fatal(err)
}
e, err := NewSyncedEnforcer(m)
if err != nil {
t.Fatal(err)
}
if _, err := e.AddPolicy("alice", "data", "read"); err != nil {
t.Fatal(err)
}
if ok, err := e.Enforce("alice", "data", "read"); err != nil || !ok {
t.Fatal(ok, err)
}
calls := 0
e.AddFunction("reviewCheck", func(args ...interface{}) (interface{}, error) {
calls++
return args[0] == "alice", nil
})
matcher := "reviewCheck(r.sub) && r.obj == p.obj && r.act == p.act"
for i := 0; i < 2; i++ {
if ok, err := e.EnforceWithMatcher(matcher, "alice", "data", "read"); err != nil || !ok {
t.Fatal(ok, err)
}
}
if calls != 2 {
t.Fatalf("cached matcher must still invoke the function on each decision: calls=%d", calls)
}
}
Loading