-
Notifications
You must be signed in to change notification settings - Fork 162
/
postgres_test.go
288 lines (216 loc) · 10.3 KB
/
postgres_test.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
package backends
import (
"testing"
. "github.com/iegomez/mosquitto-go-auth/backends/constants"
"github.com/iegomez/mosquitto-go-auth/hashing"
log "github.com/sirupsen/logrus"
. "github.com/smartystreets/goconvey/convey"
)
func TestPostgres(t *testing.T) {
//Initialize Postgres without mandatory values (fail).
authOpts := make(map[string]string)
authOpts["pg_host"] = "localhost"
authOpts["pg_port"] = "5432"
Convey("If mandatory params are not set initialization should fail", t, func() {
_, err := NewPostgres(authOpts, log.DebugLevel, hashing.NewHasher(authOpts, "postgres"))
So(err, ShouldBeError)
})
//Initialize Postgres with some test values (omit tls).
authOpts["pg_dbname"] = "go_auth_test"
authOpts["pg_user"] = "go_auth_test"
authOpts["pg_sslmode"] = "disable"
authOpts["pg_password"] = "go_auth_test"
authOpts["pg_userquery"] = "SELECT password_hash FROM test_user WHERE username = $1 limit 1"
authOpts["pg_superquery"] = "select count(*) from test_user where username = $1 and is_admin = true"
authOpts["pg_aclquery"] = "SELECT test_acl.topic FROM test_acl, test_user WHERE test_user.username = $1 AND test_acl.test_user_id = test_user.id AND (rw = $2 or rw = 3)"
Convey("Given valid params NewPostgres should return a Postgres backend instance", t, func() {
postgres, err := NewPostgres(authOpts, log.DebugLevel, hashing.NewHasher(authOpts, "postgres"))
So(err, ShouldBeNil)
//Empty db
postgres.DB.MustExec("delete from test_user where 1 = 1")
postgres.DB.MustExec("delete from test_acl where 1 = 1")
//Insert a user to test auth
username := "test"
userPass := "testpw"
//Hash generated by the pw utility
userPassHash := "PBKDF2$sha512$100000$os24lcPr9cJt2QDVWssblQ==$BK1BQ2wbwU1zNxv3Ml3wLuu5//hPop3/LvaPYjjCwdBvnpwusnukJPpcXQzyyjOlZdieXTx6sXAcX4WnZRZZnw=="
wrongUsername := "not_present"
insertQuery := "INSERT INTO test_user(username, password_hash, is_admin) values($1, $2, $3) returning id"
userID := 0
err = postgres.DB.Get(&userID, insertQuery, username, userPassHash, true)
So(err, ShouldBeNil)
So(userID, ShouldBeGreaterThan, 0)
Convey("Given a username and a correct password, it should correctly authenticate it", func() {
authenticated, err := postgres.GetUser(username, userPass, "")
So(err, ShouldBeNil)
So(authenticated, ShouldBeTrue)
})
Convey("Given a username and an incorrect password, it should not authenticate it", func() {
authenticated, err := postgres.GetUser(username, "wrong_password", "")
So(err, ShouldBeNil)
So(authenticated, ShouldBeFalse)
})
Convey("Given a wrong username, it should not authenticate it and not return error", func() {
authenticated, err := postgres.GetUser(wrongUsername, "whatever_password", "")
So(err, ShouldBeNil)
So(authenticated, ShouldBeFalse)
})
Convey("Given a username that is admin, super user should pass", func() {
superuser, err := postgres.GetSuperuser(username)
So(err, ShouldBeNil)
So(superuser, ShouldBeTrue)
})
Convey("Given a wrong username, super user should not return error", func() {
superuser, err := postgres.GetSuperuser(wrongUsername)
So(err, ShouldBeNil)
So(superuser, ShouldBeFalse)
})
//Now create some acls and test topics
strictAcl := "test/topic/1"
singleLevelAcl := "test/topic/+"
hierarchyAcl := "test/#"
userPattern := "test/%u"
clientPattern := "test/%c"
clientID := "test_client"
aclID := 0
aclQuery := "INSERT INTO test_acl(test_user_id, topic, rw) values($1, $2, $3) returning id"
err = postgres.DB.Get(&aclID, aclQuery, userID, strictAcl, MOSQ_ACL_READ)
So(err, ShouldBeNil)
Convey("Given only strict acl in db, an exact match should work and and inexact one not", func() {
testTopic1 := `test/topic/1`
testTopic2 := `test/topic/2`
tt1, err1 := postgres.CheckAcl(username, testTopic1, clientID, MOSQ_ACL_READ)
tt2, err2 := postgres.CheckAcl(username, testTopic2, clientID, MOSQ_ACL_READ)
So(err1, ShouldBeNil)
So(err2, ShouldBeNil)
So(tt1, ShouldBeTrue)
So(tt2, ShouldBeFalse)
})
Convey("Given read only privileges, a pub check should fail", func() {
testTopic1 := "test/topic/1"
tt1, err1 := postgres.CheckAcl(username, testTopic1, clientID, MOSQ_ACL_WRITE)
So(err1, ShouldBeNil)
So(tt1, ShouldBeFalse)
})
Convey("Given wildcard subscriptions against strict db acl, acl checks should fail", func() {
tt1, err1 := postgres.CheckAcl(username, singleLevelAcl, clientID, MOSQ_ACL_READ)
tt2, err2 := postgres.CheckAcl(username, hierarchyAcl, clientID, MOSQ_ACL_READ)
So(err1, ShouldBeNil)
So(err2, ShouldBeNil)
So(tt1, ShouldBeFalse)
So(tt2, ShouldBeFalse)
})
//Now check against patterns.
err = postgres.DB.Get(&aclID, aclQuery, userID, userPattern, MOSQ_ACL_READ)
So(err, ShouldBeNil)
Convey("Given a topic that mentions username, acl check should pass", func() {
tt1, err1 := postgres.CheckAcl(username, "test/test", clientID, MOSQ_ACL_READ)
So(err1, ShouldBeNil)
So(tt1, ShouldBeTrue)
})
err = postgres.DB.Get(&aclID, aclQuery, userID, clientPattern, MOSQ_ACL_READ)
So(err, ShouldBeNil)
Convey("Given a topic that mentions clientid, acl check should pass", func() {
tt1, err1 := postgres.CheckAcl(username, "test/test_client", clientID, MOSQ_ACL_READ)
So(err1, ShouldBeNil)
So(tt1, ShouldBeTrue)
})
//Now insert single level topic to check against.
err = postgres.DB.Get(&aclID, aclQuery, userID, singleLevelAcl, MOSQ_ACL_READ)
So(err, ShouldBeNil)
Convey("Given a topic not strictly present that matches a db single level wildcard, acl check should pass", func() {
tt1, err1 := postgres.CheckAcl(username, "test/topic/whatever", clientID, MOSQ_ACL_READ)
So(err1, ShouldBeNil)
So(tt1, ShouldBeTrue)
})
//Now insert hierarchy wildcard to check against.
err = postgres.DB.Get(&aclID, aclQuery, userID, hierarchyAcl, MOSQ_ACL_READ)
So(err, ShouldBeNil)
Convey("Given a topic not strictly present that matches a hierarchy wildcard, acl check should pass", func() {
tt1, err1 := postgres.CheckAcl(username, "test/what/ever", clientID, MOSQ_ACL_READ)
So(err1, ShouldBeNil)
So(tt1, ShouldBeTrue)
})
Convey("Given a bad username, acl check should not return error", func() {
testTopic1 := `test/topic/1`
tt1, err1 := postgres.CheckAcl(wrongUsername, testTopic1, clientID, MOSQ_ACL_READ)
So(err1, ShouldBeNil)
So(tt1, ShouldBeFalse)
})
//Empty db
postgres.DB.MustExec("delete from test_user where 1 = 1")
postgres.DB.MustExec("delete from test_acl where 1 = 1")
postgres.Halt()
})
}
func TestPostgresTls(t *testing.T) {
authOpts := make(map[string]string)
authOpts["pg_host"] = "localhost"
authOpts["pg_port"] = "5432"
authOpts["pg_sslmode"] = "disable"
authOpts["pg_dbname"] = "go_auth_test"
authOpts["pg_user"] = "go_auth_test_tls"
authOpts["pg_password"] = "go_auth_test_tls"
authOpts["pg_userquery"] = "SELECT password_hash FROM test_user WHERE username = $1 limit 1"
authOpts["pg_superquery"] = "select count(*) from test_user where username = $1 and is_admin = true"
authOpts["pg_aclquery"] = "SELECT test_acl.topic FROM test_acl, test_user WHERE test_user.username = $1 AND test_acl.test_user_id = test_user.id AND (rw = $2 or rw = 3)"
Convey("Given custom ssl disabled, it should fail", t, func() {
postgres, err := NewPostgres(authOpts, log.DebugLevel, hashing.NewHasher(authOpts, "postgres"))
So(err, ShouldBeError)
So(err.Error(), ShouldContainSubstring, "pg_hba.conf rejects connection")
So(postgres.DB, ShouldBeNil)
})
authOpts["pg_sslmode"] = "verify-full"
authOpts["pg_sslrootcert"] = "/test-files/certificates/ca.pem"
Convey("Given custom ssl enabled, it should work without a client certificate", t, func() {
postgres, err := NewPostgres(authOpts, log.DebugLevel, hashing.NewHasher(authOpts, "postgres"))
So(err, ShouldBeNil)
rows, err := postgres.DB.Query("SELECT cipher FROM pg_stat_activity JOIN pg_stat_ssl USING(pid);")
So(err, ShouldBeNil)
So(rows.Next(), ShouldBeTrue)
var sslCipher string
err = rows.Scan(&sslCipher)
So(err, ShouldBeNil)
So(sslCipher, ShouldNotBeBlank)
})
}
func TestPostgresMutualTls(t *testing.T) {
authOpts := make(map[string]string)
authOpts["pg_host"] = "localhost"
authOpts["pg_port"] = "5432"
authOpts["pg_dbname"] = "go_auth_test"
authOpts["pg_user"] = "go_auth_test_mutual_tls"
authOpts["pg_password"] = "go_auth_test_mutual_tls"
authOpts["pg_userquery"] = "SELECT password_hash FROM test_user WHERE username = $1 limit 1"
authOpts["pg_superquery"] = "select count(*) from test_user where username = $1 and is_admin = true"
authOpts["pg_aclquery"] = "SELECT test_acl.topic FROM test_acl, test_user WHERE test_user.username = $1 AND test_acl.test_user_id = test_user.id AND (rw = $2 or rw = 3)"
authOpts["pg_sslmode"] = "verify-full"
authOpts["pg_sslrootcert"] = "/test-files/certificates/ca.pem"
Convey("Given custom ssl enabled and no client certificate is given, it should fail", t, func() {
postgres, err := NewPostgres(authOpts, log.DebugLevel, hashing.NewHasher(authOpts, "postgres"))
So(err, ShouldBeError)
So(err.Error(), ShouldEqual, "PG backend error: couldn't open db: couldn't ping database postgres: pq: connection requires a valid client certificate")
So(postgres.DB, ShouldBeNil)
})
authOpts["pg_sslcert"] = "/test-files/certificates/grpc/client.pem"
authOpts["pg_sslkey"] = "/test-files/certificates/grpc/client-key.pem"
Convey("Given custom ssl enabled and invalid client certificate is given, it should fail", t, func() {
postgres, err := NewPostgres(authOpts, log.DebugLevel, hashing.NewHasher(authOpts, "postgres"))
So(err, ShouldBeError)
So(err.Error(), ShouldEqual, "PG backend error: couldn't open db: couldn't ping database postgres: pq: connection requires a valid client certificate")
So(postgres.DB, ShouldBeNil)
})
authOpts["pg_sslcert"] = "/test-files/certificates/db/client.pem"
authOpts["pg_sslkey"] = "/test-files/certificates/db/client-key.pem"
Convey("Given custom ssl enabled and client certificate is given, it should work", t, func() {
postgres, err := NewPostgres(authOpts, log.DebugLevel, hashing.NewHasher(authOpts, "postgres"))
So(err, ShouldBeNil)
rows, err := postgres.DB.Query("SELECT cipher FROM pg_stat_activity JOIN pg_stat_ssl USING(pid);")
So(err, ShouldBeNil)
So(rows.Next(), ShouldBeTrue)
var sslCipher string
err = rows.Scan(&sslCipher)
So(err, ShouldBeNil)
So(sslCipher, ShouldNotBeBlank)
})
}