-
Notifications
You must be signed in to change notification settings - Fork 165
/
redis.go
242 lines (194 loc) · 5.51 KB
/
redis.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
package backends
import (
"fmt"
"strconv"
"strings"
"time"
log "github.com/sirupsen/logrus"
"github.com/iegomez/mosquitto-go-auth/common"
goredis "github.com/go-redis/redis"
)
type Redis struct {
Host string
Port string
Password string
SaltEncoding string
DB int32
Conn *goredis.Client
}
func NewRedis(authOpts map[string]string, logLevel log.Level) (Redis, error) {
log.SetLevel(logLevel)
var redis = Redis{
Host: "localhost",
Port: "6379",
DB: 1,
}
if redisHost, ok := authOpts["redis_host"]; ok {
redis.Host = redisHost
}
if redisPort, ok := authOpts["redis_port"]; ok {
redis.Port = redisPort
}
if redisPassword, ok := authOpts["redis_password"]; ok {
redis.Password = redisPassword
}
if saltEncoding, ok := authOpts["redis_salt_encoding"]; ok {
redis.SaltEncoding = saltEncoding
} else {
redis.SaltEncoding = "base64"
}
if redisDB, ok := authOpts["redis_db"]; ok {
db, err := strconv.ParseInt(redisDB, 10, 32)
if err == nil {
redis.DB = int32(db)
}
}
addr := fmt.Sprintf("%s:%s", redis.Host, redis.Port)
//Try to start redis.
goredisClient := goredis.NewClient(&goredis.Options{
Addr: addr,
Password: redis.Password,
DB: int(redis.DB),
})
for {
if _, err := goredisClient.Ping().Result(); err != nil {
log.Errorf("ping redis error, will retry in 2s: %s", err)
time.Sleep(2 * time.Second)
} else {
break
}
}
redis.Conn = goredisClient
return redis, nil
}
//GetUser checks that the username exists and the given password hashes to the same password.
func (o Redis) GetUser(username, password, clientid string) bool {
pwHash, err := o.Conn.Get(username).Result()
if err != nil {
log.Debugf("Redis get user error: %s", err)
return false
}
if common.HashCompare(password, pwHash, o.SaltEncoding) {
return true
}
return false
}
//GetSuperuser checks that the key username:su exists and has value "true".
func (o Redis) GetSuperuser(username string) bool {
isSuper, err := o.Conn.Get(fmt.Sprintf("%s:su", username)).Result()
if err != nil {
log.Debugf("Redis get superuser error: %s", err)
return false
}
if isSuper == "true" {
return true
}
return false
}
//CheckAcl gets all acls for the username and tries to match against topic, acc, and username/clientid if needed.
func (o Redis) CheckAcl(username, topic, clientid string, acc int32) bool {
var acls []string //User specific acls.
var commonAcls []string //Common acls.
//We need to check if client is subscribing, reading or publishing to get correct acls.
switch acc {
case MOSQ_ACL_SUBSCRIBE:
//Get all user subscribe acls.
var err error
acls, err = o.Conn.SMembers(fmt.Sprintf("%s:sacls", username)).Result()
if err != nil {
log.Debugf("Redis check acl error: %s", err)
return false
}
//Get common subscribe acls.
commonAcls, err = o.Conn.SMembers("common:sacls").Result()
if err != nil {
log.Debugf("Redis check acl error: %s", err)
return false
}
case MOSQ_ACL_READ:
//Get all user read and readwrite acls.
urAcls, err := o.Conn.SMembers(fmt.Sprintf("%s:racls", username)).Result()
if err != nil {
log.Debugf("Redis check acl error: %s", err)
return false
}
urwAcls, err := o.Conn.SMembers(fmt.Sprintf("%s:rwacls", username)).Result()
if err != nil {
log.Debugf("Redis check acl error: %s", err)
return false
}
//Get common read and readwrite acls
rAcls, err := o.Conn.SMembers("common:racls").Result()
if err != nil {
log.Debugf("Redis check acl error: %s", err)
return false
}
rwAcls, err := o.Conn.SMembers("common:rwacls").Result()
if err != nil {
log.Debugf("Redis check acl error: %s", err)
return false
}
acls = make([]string, len(urAcls)+len(urwAcls))
acls = append(acls, urAcls...)
acls = append(acls, urwAcls...)
commonAcls = make([]string, len(rAcls)+len(rwAcls))
commonAcls = append(commonAcls, rAcls...)
commonAcls = append(commonAcls, rwAcls...)
case MOSQ_ACL_WRITE:
//Get all user write and readwrite acls.
uwAcls, err := o.Conn.SMembers(fmt.Sprintf("%s:wacls", username)).Result()
if err != nil {
log.Debugf("Redis check acl error: %s", err)
return false
}
urwAcls, err := o.Conn.SMembers(fmt.Sprintf("%s:rwacls", username)).Result()
if err != nil {
log.Debugf("Redis check acl error: %s", err)
return false
}
//Get common write and readwrite acls
wAcls, err := o.Conn.SMembers("common:wacls").Result()
if err != nil {
log.Debugf("Redis check acl error: %s", err)
return false
}
rwAcls, err := o.Conn.SMembers("common:rwacls").Result()
if err != nil {
log.Debugf("Redis check acl error: %s", err)
return false
}
acls = make([]string, len(uwAcls)+len(urwAcls))
acls = append(acls, uwAcls...)
acls = append(acls, urwAcls...)
commonAcls = make([]string, len(wAcls)+len(rwAcls))
commonAcls = append(commonAcls, wAcls...)
commonAcls = append(commonAcls, rwAcls...)
}
//Now loop through acls looking for a match.
for _, acl := range acls {
if common.TopicsMatch(acl, topic) {
return true
}
}
for _, acl := range commonAcls {
aclTopic := strings.Replace(acl, "%c", clientid, -1)
aclTopic = strings.Replace(aclTopic, "%u", username, -1)
if common.TopicsMatch(aclTopic, topic) {
return true
}
}
return false
}
//GetName returns the backend's name
func (o Redis) GetName() string {
return "Redis"
}
//Halt terminates the connection.
func (o Redis) Halt() {
if o.Conn != nil {
err := o.Conn.Close()
if err != nil {
log.Errorf("Redis cleanup error: %s", err)
}
}
}