Skip to content

Commit 635b8cb

Browse files
committed
test: add examples
1 parent 858c8ff commit 635b8cb

File tree

7 files changed

+304
-5
lines changed

7 files changed

+304
-5
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
node_modules
2-
todo
2+
todo
3+
.tmp

example_functions_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package fastac_test
2+
3+
import (
4+
"github.com/abichinger/fastac"
5+
"github.com/abichinger/fastac/model"
6+
"github.com/abichinger/fastac/model/fm"
7+
)
8+
9+
//the model uses a custom MatchingFunc named customPathMatch
10+
var example_functions_model = `
11+
[request_definition]
12+
r = sub, obj, act
13+
14+
[policy_definition]
15+
p = sub, obj, act
16+
17+
[policy_effect]
18+
e = some(where (p.eft == allow))
19+
20+
[matchers]
21+
m = r.sub == p.sub && customPathMatch(r.obj, p.obj) && r.act == p.act`
22+
23+
var example_functions_policy = [][]string{
24+
{"p", "alice", "*", "GET"},
25+
{"p", "alice", "/user/alice", "PATCH"},
26+
}
27+
28+
// ExampleFunctions shows how to use a custom util.MatchingFunc
29+
func Example_functions() {
30+
31+
//customPathMatch needs to be registered before loading the model
32+
fm.SetFunction("customPathMatch", func(arguments ...interface{}) (interface{}, error) {
33+
rObj := arguments[0].(string)
34+
rSub := arguments[1].(string)
35+
36+
if rSub == "*" {
37+
return true, nil
38+
}
39+
return rObj == rSub, nil
40+
})
41+
42+
//create enforcer and add rules
43+
m := model.NewModel()
44+
_ = m.LoadModelFromText(example_functions_model)
45+
e, _ := fastac.NewEnforcer(m, nil)
46+
_ = e.AddRules(example_functions_policy)
47+
48+
//perform some requests
49+
printReq(e, "alice", "/user/alice/entry/1", "GET")
50+
printReq(e, "bob", "/user/alice/entry/1", "GET")
51+
printReq(e, "alice", "/user/alice", "PATCH")
52+
printReq(e, "bob", "/user/alice", "PATCH")
53+
54+
// Output: alice, /user/alice/entry/1, GET => allow
55+
// bob, /user/alice/entry/1, GET => deny
56+
// alice, /user/alice, PATCH => allow
57+
// bob, /user/alice, PATCH => deny
58+
}

example_matcher_test.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package fastac_test
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
"github.com/abichinger/fastac"
8+
"github.com/abichinger/fastac/model"
9+
"github.com/abichinger/fastac/rbac"
10+
"github.com/abichinger/fastac/util"
11+
)
12+
13+
//the model uses the built-in MatchingFunc pathMatch
14+
var example_matcher_model = `
15+
[request_definition]
16+
r = sub, obj, act
17+
18+
[policy_definition]
19+
p = sub, obj, act
20+
21+
[role_definition]
22+
g = _, _
23+
24+
[policy_effect]
25+
e = some(where (p.eft == allow))
26+
27+
[matchers]
28+
m = g(r.sub, p.sub) && pathMatch(r.obj, p.obj) && r.act == p.act`
29+
30+
var example_matcher_policy = [][]string{
31+
{"p", "role:user", "/user/:uid/entry/:eid", "GET"},
32+
{"p", "user:alice", "/user/alice/*", "POST"},
33+
{"p", "role:admin", "/user/:uid/entry/:eid", "DELETE"},
34+
{"g", "reg:user:.*", "role:user"},
35+
{"g", "user:alice", "role:admin"},
36+
}
37+
38+
func printReq(e *fastac.Enforcer, params ...interface{}) {
39+
b, _ := e.Enforce(params...)
40+
var rule []string
41+
for _, param := range params {
42+
rule = append(rule, param.(string))
43+
}
44+
if b {
45+
fmt.Printf("%s => allow\n", strings.Join(rule, ", "))
46+
} else {
47+
fmt.Printf("%s => deny\n", strings.Join(rule, ", "))
48+
}
49+
}
50+
51+
// ExampleMatchers shows the usage of util.MatchingFunc and util.IMatcher
52+
func Example_matchers() {
53+
54+
//create enforcer and add rules
55+
m := model.NewModel()
56+
_ = m.LoadModelFromText(example_matcher_model)
57+
e, _ := fastac.NewEnforcer(m, nil)
58+
_ = e.AddRules(example_matcher_policy)
59+
60+
//get the default rolemanager
61+
rm, _ := e.GetModel().GetRoleManager("g")
62+
63+
// set a role matcher.
64+
// create a PrefixMatcher. PrefixMatcher implements the interface util.IMatcher
65+
// each regex pattern needs to be marked with the prefix "reg:"
66+
roleMatcher := util.NewPrefixMatcher("reg:", util.RegexMatch)
67+
rm.(rbac.IDefaultRoleManager).SetMatcher(roleMatcher)
68+
69+
printReq(e, "user:alice", "/user/joe/entry/1", "GET") //allow, because user:alice has role:user
70+
printReq(e, "user:alice", "/user/alice/entry/2", "POST")
71+
printReq(e, "user:alice", "/user/bob/entry/3", "POST")
72+
printReq(e, "user:alice", "/user/bob/entry/3", "DELETE")
73+
printReq(e, "user:bob", "/user/alice/entry/2", "DELETE")
74+
75+
// Output: user:alice, /user/joe/entry/1, GET => allow
76+
// user:alice, /user/alice/entry/2, POST => allow
77+
// user:alice, /user/bob/entry/3, POST => deny
78+
// user:alice, /user/bob/entry/3, DELETE => allow
79+
// user:bob, /user/alice/entry/2, DELETE => deny
80+
}

example_rules_test.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package fastac_test
2+
3+
import (
4+
"fmt"
5+
"sort"
6+
7+
"github.com/abichinger/fastac"
8+
"github.com/abichinger/fastac/util"
9+
)
10+
11+
var example_rules_policy = [][]string{
12+
{"p", "alice", "data1", "read"},
13+
{"p", "alice", "data1", "write"},
14+
{"p", "bob", "data2", "read"},
15+
{"p", "bob", "data2", "write"},
16+
{"p", "alice", "data3", "read"},
17+
{"p", "bob", "data3", "read"},
18+
{"p", "manager", "data3", "write"},
19+
{"g", "bob", "manager"},
20+
}
21+
22+
// ExampleManagePolicy demonstrates the usage of functions to modify the policy
23+
func Example_managePolicy() {
24+
25+
//create enforcer with rbac model and empty policy
26+
e, _ := fastac.NewEnforcer("examples/rbac_model.conf", nil)
27+
28+
//add multiple rules at once
29+
_ = e.AddRules(example_rules_policy)
30+
31+
//remove all rules of user bob
32+
bobRules, _ := e.Filter(fastac.SetMatcher(`p.sub == "bob"`))
33+
bobGroupingRules, _ := e.Filter(fastac.SetMatcher(`g.user == "bob"`))
34+
_ = e.RemoveRules(append(bobRules, bobGroupingRules...))
35+
36+
//make alice a manager
37+
alice_manager := []string{"g", "alice", "manager"}
38+
added, _ := e.AddRule(alice_manager)
39+
if added {
40+
fmt.Println("rule added successfully")
41+
}
42+
43+
//get a list of all rules
44+
var allRules [][]string
45+
e.GetModel().RangeRules(func(rule []string) bool {
46+
allRules = append(allRules, rule)
47+
return true
48+
})
49+
50+
//sort and print rules
51+
allRulesStr := util.Join2D(allRules, ", ")
52+
sort.Strings(allRulesStr)
53+
for _, rule := range allRulesStr {
54+
fmt.Println(rule)
55+
}
56+
57+
// Output: rule added successfully
58+
// g, alice, manager
59+
// p, alice, data1, read
60+
// p, alice, data1, write
61+
// p, alice, data3, read
62+
// p, manager, data3, write
63+
}

example_storage_test.go

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package fastac_test
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"sort"
7+
"strings"
8+
9+
"github.com/abichinger/fastac"
10+
gormadapter "github.com/abichinger/gorm-adapter"
11+
"gorm.io/driver/sqlite"
12+
"gorm.io/gorm"
13+
)
14+
15+
var example_rules_1 = [][]string{
16+
{"p", "alice", "data1", "read"},
17+
{"p", "alice", "data1", "write"},
18+
{"p", "bob", "data1", "read"},
19+
}
20+
21+
func createDB(name string) *gorm.DB {
22+
_ = os.Mkdir(".tmp", 0755)
23+
db, _ := gorm.Open(sqlite.Open(".tmp/"+name+".db"), &gorm.Config{})
24+
return db
25+
}
26+
27+
func removeDB(name string) {
28+
os.Remove(".tmp/" + name + ".db")
29+
}
30+
31+
// ExampleStorageAdapter shows how to store/load policy rules to/from a storage adapter
32+
func Example_storageAdapter() {
33+
34+
//init adapter
35+
db := createDB("example")
36+
defer removeDB("example")
37+
a, err := gormadapter.NewAdapter(db)
38+
if err != nil {
39+
panic(err)
40+
}
41+
42+
//create enforcer and store rules using the autosave feature
43+
e, _ := fastac.NewEnforcer("examples/basic_model.conf", a, fastac.OptionAutosave(true))
44+
err = e.AddRules(example_rules_1)
45+
if err != nil {
46+
panic(err)
47+
}
48+
49+
//second enforcer to demonstrate LoadPolicy
50+
e2, _ := fastac.NewEnforcer("examples/basic_model.conf", a)
51+
err = e2.LoadPolicy()
52+
if err != nil {
53+
panic(err)
54+
}
55+
56+
loadedRules := []string{}
57+
e2.GetModel().RangeRules(func(rule []string) bool {
58+
loadedRules = append(loadedRules, strings.Join(rule, ", "))
59+
return true
60+
})
61+
62+
sort.Strings(loadedRules)
63+
for _, rule := range loadedRules {
64+
fmt.Println(rule)
65+
}
66+
// Output: p, alice, data1, read
67+
// p, alice, data1, write
68+
// p, bob, data1, read
69+
}

go.mod

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,24 @@ module github.com/abichinger/fastac
33
go 1.17
44

55
require (
6+
github.com/abichinger/gorm-adapter v1.0.1
67
github.com/abichinger/govaluate v1.5.1-0.20220503123756-74b96f998566
78
github.com/casbin/casbin/v2 v2.44.3
89
github.com/go-ini/ini v1.66.4
910
github.com/sirupsen/logrus v1.8.1
1011
github.com/stretchr/testify v1.7.1
1112
github.com/vansante/go-event-emitter v1.0.2
13+
gorm.io/driver/sqlite v1.3.2
14+
gorm.io/gorm v1.23.5
1215
)
1316

1417
require (
1518
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible // indirect
1619
github.com/davecgh/go-spew v1.1.1 // indirect
20+
github.com/jinzhu/inflection v1.0.0 // indirect
21+
github.com/jinzhu/now v1.1.5 // indirect
22+
github.com/mattn/go-sqlite3 v1.14.12 // indirect
1723
github.com/pmezard/go-difflib v1.0.0 // indirect
18-
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 // indirect
19-
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
24+
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd // indirect
25+
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
2026
)

go.sum

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible h1:1G1pk05UrOh0NlF1oeaaix1x8XzrfjIDK47TY0Zehcw=
22
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible/go.mod h1:r7JcOSlj0wfOMncg0iLm8Leh48TZaKVeNIfJntJ2wa0=
3+
github.com/abichinger/fastac v1.0.1/go.mod h1:HWQYswCOqXnViLAJzBOiRdIIi2rUhzhhKb7Z+TZ6Di4=
4+
github.com/abichinger/gorm-adapter v1.0.1 h1:9MxhILBlN07w3JPUy6DUeQBjjCCODw0aEvcKb8p3YpM=
5+
github.com/abichinger/gorm-adapter v1.0.1/go.mod h1:aQT/0ZwJDx/mo83sPdVfS2YTV3/XzQs+NIXfOVgAmW8=
6+
github.com/abichinger/govaluate v1.5.1-0.20220429195714-1a681aad947c/go.mod h1:kxvs9v5HO4iu/FbZPqhiWJ43Vv7INmjYTlyBbfGq3Xo=
37
github.com/abichinger/govaluate v1.5.1-0.20220503123756-74b96f998566 h1:rKOEJDa0kBdr+pOIZHb6gCzACW8mfI+zkDqjxgMPflY=
48
github.com/abichinger/govaluate v1.5.1-0.20220503123756-74b96f998566/go.mod h1:kxvs9v5HO4iu/FbZPqhiWJ43Vv7INmjYTlyBbfGq3Xo=
59
github.com/casbin/casbin/v2 v2.44.3 h1:tPP1YypfG2fqe7S2zivYuD11yc+/mc0GXJe/sDVbb1U=
@@ -11,12 +15,21 @@ github.com/go-ini/ini v1.66.4 h1:dKjMqkcbkzfddhIhyglTPgMoJnkvmG+bSLrU9cTHc5M=
1115
github.com/go-ini/ini v1.66.4/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
1216
github.com/golang/mock v1.4.4 h1:l75CXGRSwbaYNpl/Z2X1XIIAMSCquvXgpVZDhwEIJsc=
1317
github.com/golang/mock v1.4.4/go.mod h1:l3mdAwkq5BuhzHwde/uurv3sEJeZMXNpwsxVWU71h+4=
18+
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
19+
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
20+
github.com/jinzhu/now v1.1.4/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
21+
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
22+
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
23+
github.com/mattn/go-sqlite3 v1.14.9/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
24+
github.com/mattn/go-sqlite3 v1.14.12 h1:TJ1bhYJPV44phC+IMu1u2K/i5RriLTPe+yc68XDJ1Z0=
25+
github.com/mattn/go-sqlite3 v1.14.12/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
1426
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1527
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
1628
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
1729
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
1830
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
1931
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
32+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
2033
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
2134
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
2235
github.com/vansante/go-event-emitter v1.0.2 h1:Qh/B4aM2OKyWWqToiIgS9XCf5sR8/R6vAp/rOpSuwss=
@@ -25,11 +38,20 @@ golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACk
2538
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
2639
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
2740
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
28-
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
2941
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
42+
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd h1:xhmwyvizuTgC2qz7ZlMluP20uW+C3Rm0FD/WLDX8884=
43+
golang.org/x/sys v0.0.0-20200323222414-85ca7c5b95cd/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
3044
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
3145
golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
3246
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
3347
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
34-
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
3548
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
49+
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
50+
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
51+
gorm.io/driver/sqlite v1.3.1/go.mod h1:wJx0hJspfycZ6myN38x1O/AqLtNS6c5o9TndewFbELg=
52+
gorm.io/driver/sqlite v1.3.2 h1:nWTy4cE52K6nnMhv23wLmur9Y3qWbZvOBz+V4PrGAxg=
53+
gorm.io/driver/sqlite v1.3.2/go.mod h1:B+8GyC9K7VgzJAcrcXMRPdnMcck+8FgJynEehEPM16U=
54+
gorm.io/gorm v1.23.1/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=
55+
gorm.io/gorm v1.23.4/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=
56+
gorm.io/gorm v1.23.5 h1:TnlF26wScKSvknUC/Rn8t0NLLM22fypYBlvj1+aH6dM=
57+
gorm.io/gorm v1.23.5/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk=

0 commit comments

Comments
 (0)