-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
role_manager_test.go
319 lines (258 loc) · 11.5 KB
/
role_manager_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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// Copyright 2017 EDOMO Systems GmbH. All Rights Reserved.
//
// Licensed 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 sessionrolemanager
import (
"strconv"
"testing"
"time"
"github.com/casbin/casbin/v2"
fileadapter "github.com/casbin/casbin/v2/persist/file-adapter"
"github.com/casbin/casbin/v2/rbac"
"github.com/casbin/casbin/v2/util"
)
func testEnforce(t *testing.T, e *casbin.Enforcer, sub string, obj interface{}, act string, time string, res bool) {
t.Helper()
if myRes, _ := e.Enforce(sub, obj, act, time); myRes != res {
t.Errorf("%s, %v, %s, %s: %t, supposed to be %t", sub, obj, act, time, !res, res)
}
}
func testSessionRole(t *testing.T, rm rbac.RoleManager, name1 string, name2 string, requestTime string, res bool) {
t.Helper()
myRes, _ := rm.HasLink(name1, name2, requestTime)
if myRes != res {
t.Errorf("%s < %s at time %s: %t, supposed to be %t", name1, name2, requestTime, !res, res)
}
}
func testPrintSessionRoles(t *testing.T, rm rbac.RoleManager, name1 string, requestTime string, res []string) {
t.Helper()
myRes, _ := rm.GetRoles(name1, requestTime)
if !util.ArrayEquals(myRes, res) {
t.Errorf("%s should have the roles %s at time %s, but has: %s", name1, res, requestTime, myRes)
}
}
func getCurrentTime() string {
return strconv.FormatInt(time.Now().UnixNano(), 10)
}
func getAfterCurrentTime() string {
return strconv.FormatInt(time.Now().UnixNano()+1, 10)
}
func getOneHourAgo() string {
return strconv.FormatInt(time.Now().UnixNano()-60*60*100000000000, 10)
}
func getInOneHour() string {
return strconv.FormatInt(time.Now().Add(time.Hour).UnixNano(), 10)
}
func getAfterOneHour() string {
return strconv.FormatInt(time.Now().Add(time.Hour).UnixNano()+1, 10)
}
func TestSessionRole(t *testing.T) {
rm := NewRoleManager(3)
rm.AddLink("alpha", "bravo", getCurrentTime(), getInOneHour())
rm.AddLink("alpha", "charlie", getCurrentTime(), getInOneHour())
rm.AddLink("bravo", "delta", getCurrentTime(), getInOneHour())
rm.AddLink("bravo", "echo", getCurrentTime(), getInOneHour())
rm.AddLink("charlie", "echo", getCurrentTime(), getInOneHour())
rm.AddLink("charlie", "foxtrott", getCurrentTime(), getInOneHour())
testSessionRole(t, rm, "alpha", "bravo", getCurrentTime(), true)
testSessionRole(t, rm, "alpha", "charlie", getCurrentTime(), true)
testSessionRole(t, rm, "bravo", "delta", getCurrentTime(), true)
testSessionRole(t, rm, "bravo", "echo", getCurrentTime(), true)
testSessionRole(t, rm, "charlie", "echo", getCurrentTime(), true)
testSessionRole(t, rm, "charlie", "foxtrott", getCurrentTime(), true)
testSessionRole(t, rm, "alpha", "bravo", getOneHourAgo(), false)
testSessionRole(t, rm, "alpha", "charlie", getOneHourAgo(), false)
testSessionRole(t, rm, "bravo", "delta", getOneHourAgo(), false)
testSessionRole(t, rm, "bravo", "echo", getOneHourAgo(), false)
testSessionRole(t, rm, "charlie", "echo", getOneHourAgo(), false)
testSessionRole(t, rm, "charlie", "foxtrott", getOneHourAgo(), false)
testSessionRole(t, rm, "alpha", "bravo", getAfterOneHour(), false)
testSessionRole(t, rm, "alpha", "charlie", getAfterOneHour(), false)
testSessionRole(t, rm, "bravo", "delta", getAfterOneHour(), false)
testSessionRole(t, rm, "bravo", "echo", getAfterOneHour(), false)
testSessionRole(t, rm, "charlie", "echo", getAfterOneHour(), false)
testSessionRole(t, rm, "charlie", "foxtrott", getAfterOneHour(), false)
}
func TestClear(t *testing.T) {
rm := NewRoleManager(3)
rm.AddLink("alpha", "bravo", getCurrentTime(), getInOneHour())
rm.AddLink("alpha", "charlie", getCurrentTime(), getInOneHour())
rm.AddLink("bravo", "delta", getCurrentTime(), getInOneHour())
rm.AddLink("bravo", "echo", getCurrentTime(), getInOneHour())
rm.AddLink("charlie", "echo", getCurrentTime(), getInOneHour())
rm.AddLink("charlie", "foxtrott", getCurrentTime(), getInOneHour())
rm.Clear()
// All data is cleared.
// No role inheritance now.
testSessionRole(t, rm, "alpha", "bravo", getCurrentTime(), false)
testSessionRole(t, rm, "alpha", "charlie", getCurrentTime(), false)
testSessionRole(t, rm, "bravo", "delta", getCurrentTime(), false)
testSessionRole(t, rm, "bravo", "echo", getCurrentTime(), false)
testSessionRole(t, rm, "charlie", "echo", getCurrentTime(), false)
testSessionRole(t, rm, "charlie", "foxtrott", getCurrentTime(), false)
}
func TestAddLink(t *testing.T) {
rm := NewRoleManager(3)
rm.AddLink("alpha", "bravo")
testSessionRole(t, rm, "alpha", "bravo", getCurrentTime(), false)
rm.AddLink("alpha", "bravo", getCurrentTime())
testSessionRole(t, rm, "alpha", "bravo", getCurrentTime(), false)
}
func TestHasLink(t *testing.T) {
rm := NewRoleManager(3)
alpha := "alpha"
bravo := "bravo"
if res, _ := rm.HasLink(alpha, bravo); res {
t.Errorf("Role manager should not have link %s < %s", alpha, bravo)
}
if res, _ := rm.HasLink(alpha, alpha, getCurrentTime()); !res {
t.Errorf("Role manager should have link %s < %s", alpha, alpha)
}
if res, _ := rm.HasLink(alpha, bravo, getCurrentTime()); res {
t.Errorf("Role manager should not have link %s < %s", alpha, bravo)
}
rm.AddLink(alpha, bravo, getCurrentTime(), getInOneHour())
if res, _ := rm.HasLink(alpha, bravo, getCurrentTime()); !res {
t.Errorf("Role manager should have link %s < %s", alpha, bravo)
}
}
func TestDeleteLink(t *testing.T) {
rm := NewRoleManager(3)
alpha := "alpha"
bravo := "bravo"
charlie := "charlie"
rm.AddLink(alpha, bravo, getOneHourAgo(), getInOneHour())
rm.AddLink(alpha, charlie, getOneHourAgo(), getInOneHour())
rm.DeleteLink(alpha, bravo)
if res, _ := rm.HasLink(alpha, bravo, getCurrentTime()); res {
t.Errorf("Role manager should not have link %s < %s", alpha, bravo)
}
rm.DeleteLink(alpha, "delta")
rm.DeleteLink(bravo, charlie)
if res, _ := rm.HasLink(alpha, charlie, getCurrentTime()); !res {
t.Errorf("Role manager should have link %s < %s", alpha, charlie)
}
}
func TestHierarchieLevel(t *testing.T) {
rm := NewRoleManager(2)
rm.AddLink("alpha", "bravo", getOneHourAgo(), getInOneHour())
rm.AddLink("bravo", "charlie", getOneHourAgo(), getInOneHour())
if res, _ := rm.HasLink("alpha", "charlie", getCurrentTime()); res {
t.Error("Role manager should not have link alpha < charlie")
}
}
func TestOutdatedSessions(t *testing.T) {
rm := NewRoleManager(3)
rm.AddLink("alpha", "bravo", getOneHourAgo(), getCurrentTime())
rm.AddLink("bravo", "charlie", getOneHourAgo(), getInOneHour())
if res, _ := rm.HasLink("alpha", "bravo", getInOneHour()); res {
t.Error("Role manager should not have link alpha < bravo")
}
if res, _ := rm.HasLink("alpha", "charlie", getOneHourAgo()); !res {
t.Error("Role manager should have link alpha < charlie")
}
}
func TestGetRoles(t *testing.T) {
rm := NewRoleManager(3)
if res, _ := rm.GetRoles("alpha"); res != nil {
t.Error("Calling GetRoles without a time should return nil.")
}
if res, _ := rm.GetRoles("bravo", getCurrentTime()); res != nil {
t.Error("bravo should not exist")
}
rm.AddLink("alpha", "bravo", getOneHourAgo(), getInOneHour())
testPrintSessionRoles(t, rm, "alpha", getOneHourAgo(), []string{"bravo"})
testPrintSessionRoles(t, rm, "alpha", getCurrentTime(), []string{"bravo"})
testPrintSessionRoles(t, rm, "alpha", getAfterOneHour(), []string{})
rm.AddLink("alpha", "charlie", getOneHourAgo(), getCurrentTime())
testPrintSessionRoles(t, rm, "alpha", getOneHourAgo(), []string{"bravo", "charlie"})
testPrintSessionRoles(t, rm, "alpha", getAfterCurrentTime(), []string{"bravo"})
testPrintSessionRoles(t, rm, "alpha", getAfterOneHour(), []string{})
rm.AddLink("alpha", "charlie", getOneHourAgo(), getInOneHour())
testPrintSessionRoles(t, rm, "alpha", getOneHourAgo(), []string{"bravo", "charlie"})
testPrintSessionRoles(t, rm, "alpha", getCurrentTime(), []string{"bravo", "charlie"})
testPrintSessionRoles(t, rm, "alpha", getAfterOneHour(), []string{})
}
func TestGetUsers(t *testing.T) {
rm := NewRoleManager(3)
rm.AddLink("bravo", "alpha", getOneHourAgo(), getInOneHour())
rm.AddLink("charlie", "alpha", getOneHourAgo(), getInOneHour())
rm.AddLink("delta", "alpha", getOneHourAgo(), getInOneHour())
myRes, _ := rm.GetUsers("alpha")
if myRes != nil {
t.Errorf("Calling GetUsers without a time should return nil.")
}
myRes, _ = rm.GetUsers("alpha", getCurrentTime())
if !util.ArrayEquals(myRes, []string{"bravo", "charlie", "delta"}) {
t.Errorf("Alpha should have the using roles [bravo charlie delta] but has %s", myRes)
}
myRes, _ = rm.GetUsers("alpha", getOneHourAgo())
if !util.ArrayEquals(myRes, []string{"bravo", "charlie", "delta"}) {
t.Errorf("Alpha should have the using roles [bravo charlie delta] but has %s", myRes)
}
myRes, _ = rm.GetUsers("alpha", getAfterOneHour())
if !util.ArrayEquals(myRes, []string{}) {
t.Errorf("Alpha should not have any using roles but has %s", myRes)
}
myRes, _ = rm.GetUsers("bravo", getCurrentTime())
if !util.ArrayEquals(myRes, []string{}) {
t.Error("bravo should have no using roles")
}
}
func TestEnforcer(t *testing.T) {
// NewEnforcer(modelPath, policyPath) automatically uses the default
// role manager when loading policy. So if we want to use a custom
// role manager, and this role manager relies on Casbin policy,
// we should manually set the role manager before loading policy.
e, _ := casbin.NewEnforcer("examples/rbac_model_with_sessions.conf")
// Manually set an adapter.
a := fileadapter.NewAdapter("examples/rbac_policy_with_sessions.csv")
e.SetAdapter(a)
// Use our role manager.
rm := NewRoleManager(10)
e.SetRoleManager(rm)
// If our role manager relies on Casbin policy (like reading "g"
// policy rules), then we have to set the role manager before loading
// policy.
//
// Otherwise, we can set the role manager at any time, because role
// manager has nothing to do with the adapter.
e.LoadPolicy()
// Current role inheritance tree:
// delta echo foxtrott
// \ / \ /
// (0-20) \ (5-15) / \ (10-20) / (10-12)
// \ / \ /
// bravo charlie
// \ /
// (0-10) \ / (5-15)
// \ /
// alpha
// Note: we use small integers as time range just for example, in real
// environment, it will be Unix time range like (1508503308708903372-1508506908708903907)
testEnforce(t, e, "alpha", "data1", "read", "00", true)
testEnforce(t, e, "alpha", "data1", "read", "05", true)
testEnforce(t, e, "alpha", "data1", "read", "10", true)
testEnforce(t, e, "alpha", "data1", "read", "15", false)
testEnforce(t, e, "alpha", "data1", "read", "20", false)
testEnforce(t, e, "alpha", "data2", "read", "00", false)
testEnforce(t, e, "alpha", "data2", "read", "05", true)
testEnforce(t, e, "alpha", "data2", "read", "10", true)
testEnforce(t, e, "alpha", "data2", "read", "15", true)
testEnforce(t, e, "alpha", "data2", "read", "20", false)
testEnforce(t, e, "alpha", "data3", "read", "00", false)
testEnforce(t, e, "alpha", "data3", "read", "05", false)
testEnforce(t, e, "alpha", "data3", "read", "10", true)
testEnforce(t, e, "alpha", "data3", "read", "15", false)
testEnforce(t, e, "alpha", "data3", "read", "20", false)
}