-
Notifications
You must be signed in to change notification settings - Fork 1
/
enforcer_b_test.go
147 lines (116 loc) · 3.81 KB
/
enforcer_b_test.go
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package fastac
import (
"fmt"
"testing"
)
func genACL(e *Enforcer, nUsers int, nObjects int, sub string, obj string) {
for i := 0; i < nUsers; i++ {
for j := 0; j < nObjects; j++ {
_, _ = e.AddRule([]string{"p", fmt.Sprintf("%s%d", sub, i), fmt.Sprintf("%s%d", obj, j), "read"})
}
}
}
func genACLWithPaths(e *Enforcer, nUsers int, nPaths int, sub string, obj string) {
for i := 0; i < nUsers; i++ {
for j := 0; j < nPaths; j++ {
_, _ = e.AddRule([]string{"p", fmt.Sprintf("%s%d", sub, i), fmt.Sprintf("/%s/%d/resource/:id/%s/%d", sub, i, obj, j), "read"})
}
}
}
func genUsers(e *Enforcer, nUsers int, nRoles int, user string, role string) {
for i := 0; i < nUsers; i++ {
_, _ = e.AddRule([]string{"g", fmt.Sprintf("%s%d", user, i), fmt.Sprintf("%s%d", role, i%nRoles)})
}
}
func genABAC(e *Enforcer, nSubRules int, nObjects int, subRule string, obj string) {
for i := 0; i < nSubRules; i++ {
for j := 0; j < nObjects; j++ {
_, _ = e.AddRule([]string{"p", fmt.Sprintf(subRule, i), fmt.Sprintf("%s%d", obj, j), "read"})
}
}
}
func BenchmarkACL(b *testing.B) {
bmUsers := []int{10, 100, 1000}
bmRules := []int{1000, 10000, 100000}
for _, nRules := range bmRules {
b.Run(fmt.Sprintf("rules=%d", nRules), func(b *testing.B) {
for _, nUsers := range bmUsers {
nObjects := nRules / nUsers
b.Run(fmt.Sprintf("users=%d/objects=%d", nUsers, nObjects), func(b *testing.B) {
e, _ := NewEnforcer("examples/basic_model.conf", nil)
genACL(e, nUsers, nObjects, "user", "data")
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = e.Enforce("user0", "data", "read") //returns false
}
})
}
})
}
}
func BenchmarkACLWithPaths(b *testing.B) {
bmUsers := []int{10, 100, 1000}
bmRules := []int{1000, 10000, 100000}
for _, nRules := range bmRules {
b.Run(fmt.Sprintf("rules=%d", nRules), func(b *testing.B) {
for _, nUsers := range bmUsers {
nObjects := nRules / nUsers
b.Run(fmt.Sprintf("users=%d/objects=%d", nUsers, nObjects), func(b *testing.B) {
e, _ := NewEnforcer("examples/pathmatch_model.conf", nil)
genACLWithPaths(e, nUsers, nObjects, "user", "data")
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = e.Enforce("user0", "/user/0/resource/10/data/foo", "read") //returns false
}
})
}
})
}
}
func BenchmarkRBAC(b *testing.B) {
bmUsers := []int{1000, 10000, 100000}
bmRoles := []int{10, 100, 1000}
bmRules := []int{1000, 10000, 100000}
for _, nRules := range bmRules {
b.Run(fmt.Sprintf("rules=%d", nRules), func(b *testing.B) {
for _, nRoles := range bmRoles {
b.Run(fmt.Sprintf("roles=%d", nRoles), func(b *testing.B) {
for _, nUsers := range bmUsers {
nObjects := nRules / nRoles
b.Run(fmt.Sprintf("users=%d/objects=%d", nUsers, nObjects), func(b *testing.B) {
e, _ := NewEnforcer("examples/rbac_model.conf", nil)
genUsers(e, nUsers, nRoles, "user", "role")
genACL(e, nRoles, nObjects, "role", "data")
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = e.Enforce("role0", "data", "read") //returns false
}
})
}
})
}
})
}
}
func BenchmarkABAC(b *testing.B) {
bmRules := []int{1000, 10000, 100000}
bmSubRules := []int{10, 100, 1000}
for _, nRules := range bmRules {
b.Run(fmt.Sprintf("rules=%d", nRules), func(b *testing.B) {
for _, nSubRules := range bmSubRules {
nObjects := nRules / nSubRules
b.Run(fmt.Sprintf("subrules=%d/objects=%d", nSubRules, nObjects), func(b *testing.B) {
sub := map[string]interface{}{
"Classification": -1,
}
e, _ := NewEnforcer("examples/abac_rule_model.conf", nil)
genABAC(e, nSubRules, nObjects, "r.sub.Classification > %d", "data")
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = e.Enforce(sub, "data0", "read") //returns false
}
})
}
})
}
}