forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
176 lines (140 loc) · 3.56 KB
/
user.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
package cluster
import (
"regexp"
"code.google.com/p/go.crypto/bcrypt"
"github.com/influxdb/go-cache"
"github.com/influxdb/influxdb/common"
)
var userCache *cache.Cache
func init() {
userCache = cache.New(0, 0)
}
type Matcher struct {
IsRegex bool
Name string
}
func (self *Matcher) Matches(name string) bool {
if self.IsRegex {
matches, _ := regexp.MatchString(self.Name, name)
return matches
}
return self.Name == name
}
type CommonUser struct {
Name string `json:"name"`
Hash string `json:"hash"`
IsUserDeleted bool `json:"is_deleted"`
CacheKey string `json:"cache_key"`
}
func (self *CommonUser) GetName() string {
return self.Name
}
func (self *CommonUser) IsDeleted() bool {
return self.IsUserDeleted
}
func (self *CommonUser) ChangePassword(hash string) error {
self.Hash = hash
userCache.Delete(self.CacheKey)
return nil
}
func (self *CommonUser) isValidPwd(password string) bool {
if pwd, ok := userCache.Get(self.CacheKey); ok {
return password == pwd.(string)
}
isValid := bcrypt.CompareHashAndPassword([]byte(self.Hash), []byte(password)) == nil
if isValid {
userCache.Set(self.CacheKey, password, 0)
}
return isValid
}
func (self *CommonUser) IsClusterAdmin() bool {
return false
}
func (self *CommonUser) IsDbAdmin(db string) bool {
return false
}
func (self *CommonUser) GetDb() string {
return ""
}
func (self *CommonUser) HasWriteAccess(name string) bool {
return false
}
func (self *CommonUser) HasReadAccess(name string) bool {
return false
}
type ClusterAdmin struct {
CommonUser `json:"common"`
}
func (self *ClusterAdmin) IsClusterAdmin() bool {
return true
}
func (self *ClusterAdmin) IsDbAdmin(_ string) bool {
return true
}
func (self *ClusterAdmin) HasWriteAccess(_ string) bool {
return true
}
func (self *ClusterAdmin) GetWritePermission() string {
return ".*"
}
func (self *ClusterAdmin) HasReadAccess(_ string) bool {
return true
}
func (self *ClusterAdmin) GetReadPermission() string {
return ".*"
}
type DbUser struct {
CommonUser `json:"common"`
Db string `json:"db"`
ReadFrom []*Matcher `json:"read_matchers"`
WriteTo []*Matcher `json:"write_matchers"`
IsAdmin bool `json:"is_admin"`
}
func (self *DbUser) IsDbAdmin(db string) bool {
return self.IsAdmin && self.Db == db
}
func (self *DbUser) HasWriteAccess(name string) bool {
for _, matcher := range self.WriteTo {
if matcher.Matches(name) {
return true
}
}
return false
}
func (self *DbUser) GetWritePermission() string {
if len(self.WriteTo) > 0 {
matcher := self.WriteTo[0]
return matcher.Name
}
return ""
}
func (self *DbUser) HasReadAccess(name string) bool {
for _, matcher := range self.ReadFrom {
if matcher.Matches(name) {
return true
}
}
return false
}
func (self *DbUser) GetReadPermission() string {
if len(self.ReadFrom) > 0 {
matcher := self.ReadFrom[0]
return matcher.Name
}
return ""
}
func (self *DbUser) GetDb() string {
return self.Db
}
func (self *DbUser) ChangePermissions(readPermissions, writePermissions string) {
self.ReadFrom = []*Matcher{{true, readPermissions}}
self.WriteTo = []*Matcher{{true, writePermissions}}
}
func HashPassword(password string) ([]byte, error) {
if length := len(password); length < 4 || length > 56 {
return nil, common.NewQueryError(common.InvalidArgument, "Password must be more than 4 and less than 56 characters")
}
// The second arg is the cost of the hashing, higher is slower but makes it harder
// to brute force, since it will be really slow and impractical
return bcrypt.GenerateFromPassword([]byte(password), 10)
}