-
Notifications
You must be signed in to change notification settings - Fork 162
/
sqlite.go
210 lines (161 loc) · 4.56 KB
/
sqlite.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
package backends
import (
"database/sql"
"strconv"
"strings"
"github.com/iegomez/mosquitto-go-auth/backends/topics"
"github.com/iegomez/mosquitto-go-auth/hashing"
"github.com/jmoiron/sqlx"
_ "github.com/mattn/go-sqlite3"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)
//Sqlite holds all fields of the sqlite db connection.
type Sqlite struct {
DB *sqlx.DB
Source string
UserQuery string
SuperuserQuery string
AclQuery string
hasher hashing.HashComparer
maxLifeTime int64
connectTries int
}
func NewSqlite(authOpts map[string]string, logLevel log.Level, hasher hashing.HashComparer) (Sqlite, error) {
log.SetLevel(logLevel)
//Set defaults for sqlite
sqliteOk := true
missingOptions := ""
var sqlite = Sqlite{
SuperuserQuery: "",
AclQuery: "",
hasher: hasher,
}
if source, ok := authOpts["sqlite_source"]; ok {
sqlite.Source = source
} else {
sqliteOk = false
missingOptions += " sqlite_source"
}
if userQuery, ok := authOpts["sqlite_userquery"]; ok {
sqlite.UserQuery = userQuery
} else {
sqliteOk = false
missingOptions += " sqlite_userquery"
}
if superuserQuery, ok := authOpts["sqlite_superquery"]; ok {
sqlite.SuperuserQuery = superuserQuery
}
if aclQuery, ok := authOpts["sqlite_aclquery"]; ok {
sqlite.AclQuery = aclQuery
}
if maxLifeTime, ok := authOpts["sqlite_max_life_time"]; ok {
lifeTime, err := strconv.ParseInt(maxLifeTime, 10, 64)
if err == nil {
sqlite.maxLifeTime = lifeTime
}
}
//Exit if any mandatory option is missing.
if !sqliteOk {
return sqlite, errors.Errorf("sqlite backend error: missing options: %s", missingOptions)
}
//Build the dsn string and try to connect to the db.
connStr := ":memory:"
if sqlite.Source != "memory" {
connStr = sqlite.Source
}
if tries, ok := authOpts["sqlite_connect_tries"]; ok {
connectTries, err := strconv.Atoi(tries)
if err != nil {
log.Warnf("invalid sqlite connect tries options: %s", err)
} else {
sqlite.connectTries = connectTries
}
}
var err error
sqlite.DB, err = OpenDatabase(connStr, "sqlite3", sqlite.connectTries, sqlite.maxLifeTime)
if err != nil {
return sqlite, errors.Errorf("sqlite backend error: couldn't open db %s: %s", connStr, err)
}
return sqlite, nil
}
//GetUser checks that the username exists and the given password hashes to the same password.
func (o Sqlite) GetUser(username, password, clientid string) (bool, error) {
var pwHash sql.NullString
err := o.DB.Get(&pwHash, o.UserQuery, username)
if err != nil {
if err == sql.ErrNoRows {
// avoid leaking the fact that user exists or not though error.
return false, nil
}
log.Debugf("SQlite get user error: %s", err)
return false, err
}
if !pwHash.Valid {
log.Debugf("SQlite get user error: user %s not found.", username)
return false, nil
}
if o.hasher.Compare(password, pwHash.String) {
return true, nil
}
return false, nil
}
//GetSuperuser checks that the username meets the superuser query.
func (o Sqlite) GetSuperuser(username string) (bool, error) {
//If there's no superuser query, return false.
if o.SuperuserQuery == "" {
return false, nil
}
var count sql.NullInt64
err := o.DB.Get(&count, o.SuperuserQuery, username)
if err != nil {
if err == sql.ErrNoRows {
// avoid leaking the fact that user exists or not though error.
return false, nil
}
log.Debugf("sqlite get superuser error: %s", err)
return false, err
}
if !count.Valid {
log.Debugf("sqlite get superuser error: user %s not found", username)
return false, nil
}
if count.Int64 > 0 {
return true, nil
}
return false, nil
}
//CheckAcl gets all acls for the username and tries to match against topic, acc, and username/clientid if needed.
func (o Sqlite) CheckAcl(username, topic, clientid string, acc int32) (bool, error) {
//If there's no acl query, assume all privileges for all users.
if o.AclQuery == "" {
return true, nil
}
var acls []string
err := o.DB.Select(&acls, o.AclQuery, username, acc)
if err != nil {
log.Debugf("sqlite check acl error: %s", err)
return false, err
}
for _, acl := range acls {
aclTopic := strings.Replace(acl, "%c", clientid, -1)
aclTopic = strings.Replace(aclTopic, "%u", username, -1)
if topics.Match(aclTopic, topic) {
return true, nil
}
}
return false, nil
}
//GetName returns the backend's name
func (o Sqlite) GetName() string {
return "Sqlite"
}
//Halt closes the mysql connection.
func (o Sqlite) Halt() {
if o.DB != nil {
err := o.DB.Close()
if err != nil {
log.Errorf("sqlite cleanup error: %s", err)
}
}
}