-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadapter.go
467 lines (391 loc) · 10.9 KB
/
adapter.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
package boltadapter
import (
"bytes"
"encoding/csv"
"encoding/json"
"errors"
"fmt"
"strings"
"github.com/casbin/casbin/v2/model"
"github.com/casbin/casbin/v2/persist"
bolt "go.etcd.io/bbolt"
)
var _ persist.Adapter = (*adapter)(nil)
var _ persist.UpdatableAdapter = (*adapter)(nil)
// CasbinRule represents a Casbin rule line.
type CasbinRule struct {
Key string `json:"key"`
PType string `json:"p_type"`
V0 string `json:"v0"`
V1 string `json:"v1"`
V2 string `json:"v2"`
V3 string `json:"v3"`
V4 string `json:"v4"`
V5 string `json:"v5"`
}
func (cr *CasbinRule) Rule() []string {
return strings.Split(cr.Key, "::")[1:]
}
type adapter struct {
db *bolt.DB
bucket []byte
builtinPolicy string
}
// NewAdapter creates a new adapter. It assumes that the Bolt DB is already open. A bucket name is required and
// represents the Bolt bucket to save the data into. like to save to. The builtinPolicy is a string representation
// of a Casbin csv policy definition. If left builtinPolicy will not be used.
func NewAdapter(db *bolt.DB, bucket string) (*adapter, error) {
if bucket == "" {
return nil, errors.New("must provide a bucket")
}
adapter := &adapter{
db: db,
bucket: []byte(bucket),
}
if err := adapter.init(); err != nil {
return nil, err
}
return adapter, nil
}
func (a *adapter) init() error {
return a.db.Update(func(tx *bolt.Tx) error {
_, err := tx.CreateBucketIfNotExists(a.bucket)
return err
})
}
// LoadPolicy performs a scan on the bucket and individually loads every line into the Casbin model.
// Not particularity efficient but should only be required on when you application starts up as this adapter can
// leverage auto-save functionality.
func (a *adapter) LoadPolicy(model model.Model) error {
return a.db.View(func(tx *bolt.Tx) error {
bucket := tx.Bucket(a.bucket)
return bucket.ForEach(func(k, v []byte) error {
var line CasbinRule
if err := json.Unmarshal(v, &line); err != nil {
return err
}
loadPolicy(line, model)
return nil
})
})
}
// SavePolicy is not supported for this adapter. Auto-save should be used.
func (a *adapter) SavePolicy(model model.Model) error {
return errors.New("not supported: must use auto-save with this adapter")
}
// AddPolicy inserts or updates a rule.
func (a *adapter) AddPolicy(_ string, ptype string, rule []string) error {
return a.db.Update(func(tx *bolt.Tx) error {
bucket := tx.Bucket(a.bucket)
line := convertRule(ptype, rule)
bts, err := json.Marshal(line)
if err != nil {
return err
}
return bucket.Put([]byte(line.Key), bts)
})
}
// AddPolicies inserts or updates multiple rules by iterating over each one and inserting it into the bucket.
func (a *adapter) AddPolicies(_ string, ptype string, rules [][]string) error {
return a.db.Update(func(tx *bolt.Tx) error {
for _, r := range rules {
bucket := tx.Bucket(a.bucket)
line := convertRule(ptype, r)
bts, err := json.Marshal(line)
if err != nil {
return err
}
if err := bucket.Put([]byte(line.Key), bts); err != nil {
return err
}
}
return nil
})
}
// RemoveFilteredPolicy has an implementation that is slightly limited in that we can only find and remove elements
// using a policy line prefix.
//
// For example, if you have the following policy:
// p, subject-a, action-a, get
// p, subject-a, action-a, write
// p, subject-b, action-a, get
// p, subject-b, action-a, write
//
// The following would remove all subject-a rules:
// enforcer.RemoveFilteredPolicy(0, "subject-a")
// The following would remove all subject-a rules that contain action-a:
// enforcer.RemoveFilteredPolicy(0, "subject-a", "action-a")
//
// The following does not work and will return an error:
// enforcer.RemoveFilteredPolicy(1, "action-a")
//
// This is because we use leverage Bolts seek and prefix to find an item by prefix.
// Once these keys are found we can iterate over and remove them.
// Each policy rule is stored as a row in Bolt: p::subject-a::action-a::get
func (a *adapter) RemoveFilteredPolicy(_ string, ptype string, fieldIndex int, fieldValues ...string) error {
if fieldIndex != 0 {
return errors.New("fieldIndex != 0: adapter only supports filter by prefix")
}
rule := CasbinRule{}
rule.PType = ptype
if fieldIndex <= 0 && 0 < fieldIndex+len(fieldValues) {
rule.V0 = fieldValues[0-fieldIndex]
}
if fieldIndex <= 1 && 1 < fieldIndex+len(fieldValues) {
rule.V1 = fieldValues[1-fieldIndex]
}
if fieldIndex <= 2 && 2 < fieldIndex+len(fieldValues) {
rule.V2 = fieldValues[2-fieldIndex]
}
if fieldIndex <= 3 && 3 < fieldIndex+len(fieldValues) {
rule.V3 = fieldValues[3-fieldIndex]
}
if fieldIndex <= 4 && 4 < fieldIndex+len(fieldValues) {
rule.V4 = fieldValues[4-fieldIndex]
}
if fieldIndex <= 5 && 5 < fieldIndex+len(fieldValues) {
rule.V5 = fieldValues[5-fieldIndex]
}
filterPrefix := a.buildFilter(rule)
matched := [][]byte{}
if err := a.db.View(func(tx *bolt.Tx) error {
c := tx.Bucket(a.bucket).Cursor()
prefix := []byte(filterPrefix)
for k, _ := c.Seek(prefix); k != nil && bytes.HasPrefix(k, prefix); k, _ = c.Next() {
matched = append(matched, k)
}
return nil
}); err != nil {
return err
}
return a.db.Update(func(tx *bolt.Tx) error {
bucket := tx.Bucket(a.bucket)
for _, k := range matched {
if err := bucket.Delete(k); err != nil {
return err
}
}
return nil
})
}
func (a *adapter) buildFilter(rule CasbinRule) string {
filter := rule.PType
if rule.V0 != "" {
filter = fmt.Sprintf("%s::%s", filter, rule.V0)
}
if rule.V1 != "" {
filter = fmt.Sprintf("%s::%s", filter, rule.V1)
}
if rule.V2 != "" {
filter = fmt.Sprintf("%s::%s", filter, rule.V2)
}
if rule.V3 != "" {
filter = fmt.Sprintf("%s::%s", filter, rule.V3)
}
if rule.V4 != "" {
filter = fmt.Sprintf("%s::%s", filter, rule.V4)
}
if rule.V5 != "" {
filter = fmt.Sprintf("%s::%s", filter, rule.V5)
}
return filter
}
// RemovePolicy removes a policy line that matches key.
func (a *adapter) RemovePolicy(_ string, ptype string, line []string) error {
return a.db.Update(func(tx *bolt.Tx) error {
rule := convertRule(ptype, line)
bucket := tx.Bucket(a.bucket)
return bucket.Delete([]byte(rule.Key))
})
}
// RemovePolicies removes multiple policies.
func (a *adapter) RemovePolicies(_ string, ptype string, rules [][]string) error {
return a.db.Update(func(tx *bolt.Tx) error {
for _, r := range rules {
rule := convertRule(ptype, r)
bucket := tx.Bucket(a.bucket)
if err := bucket.Delete([]byte(rule.Key)); err != nil {
return err
}
}
return nil
})
}
func (a *adapter) UpdatePolicy(_ string, ptype string, oldRule, newRule []string) error {
return a.db.Update(func(tx *bolt.Tx) error {
old := convertRule(ptype, oldRule)
new := convertRule(ptype, newRule)
bucket := tx.Bucket(a.bucket)
if err := bucket.Delete([]byte(old.Key)); err != nil {
return err
}
bts, err := json.Marshal(new)
if err != nil {
return err
}
if err := bucket.Put([]byte(new.Key), bts); err != nil {
return err
}
return nil
})
}
func (a *adapter) UpdatePolicies(_ string, ptype string, oldRules, newRules [][]string) error {
return a.db.Update(func(tx *bolt.Tx) error {
bucket := tx.Bucket(a.bucket)
for _, r := range oldRules {
old := convertRule(ptype, r)
if err := bucket.Delete([]byte(old.Key)); err != nil {
return err
}
}
for _, r := range newRules {
new := convertRule(ptype, r)
bts, err := json.Marshal(new)
if err != nil {
return err
}
if err := bucket.Put([]byte(new.Key), bts); err != nil {
return err
}
}
return nil
})
}
func (a *adapter) UpdateFilteredPolicies(_ string, ptype string, newPolicies [][]string, fieldIndex int, fieldValues ...string) ([][]string, error) {
if fieldIndex != 0 {
return nil, errors.New("fieldIndex != 0: adapter only supports filter by prefix")
}
rule := CasbinRule{}
rule.PType = ptype
if fieldIndex <= 0 && 0 < fieldIndex+len(fieldValues) {
rule.V0 = fieldValues[0-fieldIndex]
}
if fieldIndex <= 1 && 1 < fieldIndex+len(fieldValues) {
rule.V1 = fieldValues[1-fieldIndex]
}
if fieldIndex <= 2 && 2 < fieldIndex+len(fieldValues) {
rule.V2 = fieldValues[2-fieldIndex]
}
if fieldIndex <= 3 && 3 < fieldIndex+len(fieldValues) {
rule.V3 = fieldValues[3-fieldIndex]
}
if fieldIndex <= 4 && 4 < fieldIndex+len(fieldValues) {
rule.V4 = fieldValues[4-fieldIndex]
}
if fieldIndex <= 5 && 5 < fieldIndex+len(fieldValues) {
rule.V5 = fieldValues[5-fieldIndex]
}
filterPrefix := a.buildFilter(rule)
fmt.Println(filterPrefix)
matched := []CasbinRule{}
if err := a.db.View(func(tx *bolt.Tx) error {
c := tx.Bucket(a.bucket).Cursor()
prefix := []byte(filterPrefix)
for k, v := c.Seek(prefix); k != nil && bytes.HasPrefix(k, prefix); k, v = c.Next() {
r := CasbinRule{}
if err := json.Unmarshal(v, &r); err != nil {
return err
}
matched = append(matched, r)
}
return nil
}); err != nil {
return nil, err
}
err := a.db.Update(func(tx *bolt.Tx) error {
bucket := tx.Bucket(a.bucket)
for _, r := range matched {
fmt.Println(r.Key)
if err := bucket.Delete([]byte(r.Key)); err != nil {
return err
}
}
for _, r := range newPolicies {
new := convertRule(ptype, r)
bts, err := json.Marshal(new)
if err != nil {
return err
}
if err := bucket.Put([]byte(new.Key), bts); err != nil {
return err
}
}
return nil
})
if err != nil {
return nil, err
}
oldRules := make([][]string, 0, len(matched))
for _, r := range matched {
oldRules = append(oldRules, r.Rule())
}
return oldRules, err
}
func loadPolicy(rule CasbinRule, model model.Model) {
lineText := rule.PType
if rule.V0 != "" {
lineText += ", " + rule.V0
}
if rule.V1 != "" {
lineText += ", " + rule.V1
}
if rule.V2 != "" {
lineText += ", " + rule.V2
}
if rule.V3 != "" {
lineText += ", " + rule.V3
}
if rule.V4 != "" {
lineText += ", " + rule.V4
}
if rule.V5 != "" {
lineText += ", " + rule.V5
}
persist.LoadPolicyLine(lineText, model)
}
func loadCsvPolicyLine(line string, model model.Model) error {
if line == "" || strings.HasPrefix(line, "#") {
return nil
}
reader := csv.NewReader(strings.NewReader(line))
reader.TrimLeadingSpace = true
tokens, err := reader.Read()
if err != nil {
return err
}
key := tokens[0]
sec := key[:1]
model[sec][key].Policy = append(model[sec][key].Policy, tokens[1:])
return nil
}
func convertRule(ptype string, line []string) CasbinRule {
rule := CasbinRule{PType: ptype}
keySlice := []string{ptype}
l := len(line)
if l > 0 {
rule.V0 = line[0]
keySlice = append(keySlice, line[0])
}
if l > 1 {
rule.V1 = line[1]
keySlice = append(keySlice, line[1])
}
if l > 2 {
rule.V2 = line[2]
keySlice = append(keySlice, line[2])
}
if l > 3 {
rule.V3 = line[3]
keySlice = append(keySlice, line[3])
}
if l > 4 {
rule.V4 = line[4]
keySlice = append(keySlice, line[4])
}
if l > 5 {
rule.V5 = line[5]
keySlice = append(keySlice, line[5])
}
rule.Key = strings.Join(keySlice, "::")
return rule
}