This repository has been archived by the owner on Aug 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
adapter.go
127 lines (111 loc) · 3.2 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
package minioadapter
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"strings"
"github.com/casbin/casbin/model"
"github.com/casbin/casbin/persist"
"github.com/casbin/casbin/util"
minio "github.com/minio/minio-go"
)
// MinioAdapter the struct that implements
type MinioAdapter struct {
client *minio.Client
bucket string
objectName string
}
// LoadPolicy loads all policy rules from the storage.
func (a *MinioAdapter) LoadPolicy(model model.Model) error {
obj, err := a.client.GetObject(a.bucket, a.objectName, minio.GetObjectOptions{})
if err != nil {
return err
}
buf := bufio.NewReader(obj)
for {
line, err := buf.ReadString('\n')
line = strings.TrimSpace(line)
persist.LoadPolicyLine(line, model)
if err != nil {
if err == io.EOF {
return nil
}
return err
}
}
}
// SavePolicy saves all policy rules to the storage.
func (a *MinioAdapter) SavePolicy(model model.Model) error {
var tmp bytes.Buffer
var streamLength int64
for ptype, ast := range model["p"] {
for _, rule := range ast.Policy {
l, err := tmp.WriteString(fmt.Sprintf("%s, %s\n", ptype, util.ArrayToString(rule)))
if err != nil {
return err
}
streamLength += int64(l)
}
}
for ptype, ast := range model["g"] {
for _, rule := range ast.Policy {
l, err := tmp.WriteString(fmt.Sprintf("%s, %s\n", ptype, util.ArrayToString(rule)))
if err != nil {
return err
}
streamLength += int64(l)
}
}
a.client.PutObject(a.bucket, a.objectName, &tmp, streamLength, minio.PutObjectOptions{})
return nil
}
// AddPolicy adds a policy rule to the storage.
// This is part of the Auto-Save feature.
func (a *MinioAdapter) AddPolicy(sec string, ptype string, rule []string) error {
return errors.New("Not implemented")
}
// RemovePolicy removes a policy rule from the storage.
// This is part of the Auto-Save feature.
func (a *MinioAdapter) RemovePolicy(sec string, ptype string, rule []string) error {
return errors.New("Not implemented")
}
// RemoveFilteredPolicy removes policy rules that match the filter from the storage.
// This is part of the Auto-Save feature.
func (a *MinioAdapter) RemoveFilteredPolicy(sec string, ptype string, fieldIndex int, fieldValues ...string) error {
return errors.New("Not implemented")
}
// NewAdapter create new MinioAdapter
// Parameters:
// - endpoint
// URL to object storage service.
// - accessKey
// Access key is the user ID that uniquely identifies your account.
// - secretKey
// Secret key is the password to your account.
// - secure
// Set this value to 'true' to enable secure (HTTPS) access.
// - bucket
// Name of the bucket where the policy is stored
// - objectName
// Name of the object that contains policy
func NewAdapter(endpoint string, accessKey string, secretKey string, secure bool, bucket string, objectName string) (persist.Adapter, error) {
client, err := minio.New(endpoint, accessKey, secretKey, secure)
if err != nil {
return nil, err
}
ma := &MinioAdapter{
client: client,
bucket: bucket,
objectName: objectName,
}
ok, err := ma.client.BucketExists(ma.bucket)
if err != nil {
return nil, err
}
if !ok {
return nil, fmt.Errorf("MinioAdapterError: bucket %s doesn't exist", ma.bucket)
}
return ma, nil
}