forked from couchbase/cbauth
/
cbauth_test.go
440 lines (375 loc) · 10.4 KB
/
cbauth_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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
package cbauth
import (
"crypto/hmac"
"crypto/sha1"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"time"
"github.com/couchbase/cbauth/cbauthimpl"
"github.com/couchbase/cbauth/revrpc"
)
func newAuth(initPeriod time.Duration) *authImpl {
return &authImpl{cbauthimpl.NewSVC(initPeriod, &DBStaleError{})}
}
func must(err error) {
if err != nil {
panic(err)
}
}
func hashPassword(password string, salt []byte) []byte {
h := hmac.New(sha1.New, salt)
h.Write([]byte(password))
return h.Sum(nil)
}
func mkUser(user, password, salt string) (u cbauthimpl.User) {
u.User = user
u.Salt = []byte(salt)
u.Mac = hashPassword(password, u.Salt)
return
}
func newAuthForTest(body func(freshChan chan struct{}, timeoutBody func())) *authImpl {
testDur := 555 * time.Hour
wf := func(period time.Duration, ch chan struct{}, timeoutBody func()) {
if period != testDur {
panic(period)
}
body(ch, timeoutBody)
}
return &authImpl{cbauthimpl.NewSVCForTest(testDur, &DBStaleError{}, wf)}
}
func acc(ok bool, err error) bool {
must(err)
return ok
}
func assertAdmins(t *testing.T, c Creds, needAdmin, needROAdmin bool) {
if acc(c.IsAdmin()) != needAdmin {
t.Fatalf("admin access must be: %v", needAdmin)
}
roadmin := !acc(c.IsAdmin()) && c.CanReadAnyMetadata()
if roadmin != needROAdmin {
t.Fatalf("ro-admin access must be: %v", needROAdmin)
}
}
type testingRoundTripper struct {
method string
url string
user string
source string
token string
role string
tripped bool
}
func newTestingRT(method, uri string) *testingRoundTripper {
return &testingRoundTripper{
method: method,
url: uri,
}
}
func (rt *testingRoundTripper) RoundTrip(req *http.Request) (res *http.Response, err error) {
if rt.tripped {
log.Fatalf("Already tripped")
}
rt.tripped = true
if req.URL.String() != rt.url {
log.Fatalf("Bad url: %v != %v", rt.url, req.URL)
}
if req.Method != rt.method {
log.Fatalf("Bad method: %s != %s", rt.method, req.Method)
}
statusCode := 200
if req.Header.Get("ns-server-ui") == "yes" {
token, err := req.Cookie("ui-auth-q")
if err != nil || rt.token != token.Value {
statusCode = 401
}
} else {
log.Fatal("Expect to be called only with ns-server-ui=yes")
}
response := ""
status := "401 Unauthorized"
if statusCode == 200 {
response = fmt.Sprintf(`{"role": "%s", "user": "%s", "source": "%s"}`, rt.role, rt.user, rt.source)
status = "200 OK"
}
respBody := ioutil.NopCloser(strings.NewReader(response))
return &http.Response{
Status: status,
StatusCode: statusCode,
Proto: "HTTP/1.0",
ProtoMajor: 1,
ProtoMinor: 0,
Header: http.Header{},
Body: respBody,
ContentLength: -1,
Trailer: http.Header{},
Request: req,
}, nil
}
func (rt *testingRoundTripper) resetTripped() {
rt.tripped = false
}
func (rt *testingRoundTripper) assertTripped(t *testing.T, expected bool) {
if rt.tripped != expected {
t.Fatalf("Tripped is not expected. Have: %v, need: %v", rt.tripped, expected)
}
}
func (rt *testingRoundTripper) setTokenAuth(user, source, token, role string) {
rt.token = token
rt.source = source
rt.user = user
rt.role = role
}
func TestStaleBasic(t *testing.T) {
for _, period := range []time.Duration{1, 0} {
a := newAuth(period)
_, err := a.Auth("asd", "bsd")
if _, ok := err.(*DBStaleError); !ok {
t.Fatalf("For period: %v expect ErrStale in stale state. Got %v", period, err)
}
}
}
func TestStale(t *testing.T) {
sync1 := make(chan bool)
sync2 := make(chan bool)
go func() {
sync1 <- true
sync2 <- true
close(sync2)
}()
a := newAuthForTest(func(ch chan struct{}, timeoutBody func()) {
<-sync1
go func() {
<-sync2
timeoutBody()
}()
})
_, err := a.Auth("a", "b")
if _, ok := err.(*DBStaleError); !ok {
t.Fatalf("Expect ErrStale in stale state. Got %v", err)
}
if _, ok := <-sync2; ok {
t.Fatal("Some bad sync")
}
}
func doTestStaleThenAdmin(t *testing.T, updateBeforeTimer bool) {
timerchan := make(chan bool)
var freshChan chan struct{}
a := newAuthForTest(func(ch chan struct{}, timeoutBody func()) {
freshChan = ch
go func() {
<-timerchan
timeoutBody()
}()
})
updatechan := make(chan bool)
go func() {
c := cbauthimpl.Cache{Admin: mkUser("admin", "asdasd", "nacl")}
<-updatechan
must(a.svc.UpdateDB(&c, nil))
<-updatechan
}()
go func() {
freshChan <- struct{}{}
if !updateBeforeTimer {
close(timerchan)
return
}
updatechan <- true
updatechan <- true
close(timerchan)
}()
cred, err := a.Auth("admin", "asdasd")
if updateBeforeTimer {
must(err)
if ok, _ := cred.IsAdmin(); !ok {
t.Fatal("user admin must be recognised as admin")
}
} else {
if _, ok := err.(*DBStaleError); !ok {
t.Fatal("db must be stale")
}
updatechan <- true
updatechan <- true
}
if _, ok := <-timerchan; ok {
t.Fatal("timerchan must be closed")
}
cred, err = a.Auth("admin", "badpass")
if err != nil || cred != NoAccessCreds {
t.Fatalf("badpass must not work. Instead got: %v and %v", cred, err)
}
cred, err = a.Auth("admin", "asdasd")
must(err)
if ok, _ := cred.IsAdmin(); !ok {
t.Fatal("user admin must be recognised as admin")
}
}
func TestStaleThenAdminTimerCase(t *testing.T) {
doTestStaleThenAdmin(t, false)
}
func TestStaleThenAdminUpdateCase(t *testing.T) {
doTestStaleThenAdmin(t, true)
}
func mkBucket(name, pwd string) (rv cbauthimpl.Bucket) {
rv.Name = name
rv.Password = pwd
return
}
func TestBucketsAuth(t *testing.T) {
a := newAuth(0)
must(a.svc.UpdateDB(&cbauthimpl.Cache{
Buckets: append(cbauthimpl.Cache{}.Buckets,
mkBucket("default", ""),
mkBucket("foo", "bar"))}, nil))
c, err := a.Auth("foo", "bar")
must(err)
if !acc(c.CanAccessBucket("foo")) {
t.Fatal("Expect foo access with right pw to work")
}
if acc(c.CanAccessBucket("default")) {
t.Fatal("Expect default access to not work when authed towards foo")
}
if acc(c.CanAccessBucket("unknown")) {
t.Fatal("Expect unknown bucket access to not work")
}
assertAdmins(t, c, false, false)
c, err = a.Auth("foo", "notbar")
if err != nil || c != NoAccessCreds {
t.Fatalf("Expect wrong password auth to fail. Got: %v and %v", c, err)
}
c, err = a.Auth("", "")
must(err)
assertAdmins(t, c, false, false)
if acc(c.CanAccessBucket("foo")) {
t.Fatal("Expect foo access to not work under anon auth")
}
if !acc(c.CanAccessBucket("default")) {
t.Fatal("Expect default access to work under anon auth")
}
// now somebody deletes no-password default bucket
must(a.svc.UpdateDB(&cbauthimpl.Cache{
Buckets: append(cbauthimpl.Cache{}.Buckets,
mkBucket("foo", "bar"))}, nil))
c, err = a.Auth("foo", "bar")
must(err)
assertAdmins(t, c, false, false)
if !acc(c.CanAccessBucket("foo")) {
t.Fatal("Expect foo access to work under right pw")
}
// and no password access should not work
c, err = a.Auth("", "")
if err != nil || c != NoAccessCreds {
t.Fatalf("Expect no password access to fail after deletion of default bucket. Got: %v and %v", c, err)
}
}
func mkNode(host, user, pwd string, ports []int, local bool) (rv cbauthimpl.Node) {
rv.Host = host
rv.User = user
rv.Password = pwd
rv.Ports = ports
rv.Local = local
return
}
func TestServicePwd(t *testing.T) {
a := newAuth(0)
c := cbauthimpl.Cache{
Nodes: append(cbauthimpl.Cache{}.Nodes,
mkNode("beta.local", "_admin", "foobar", []int{9000, 12000}, false),
mkNode("chi.local", "_admin", "barfoo", []int{9001, 12001}, false)),
SpecialUser: "@component",
}
must(a.svc.UpdateDB(&c, nil))
u, p, err := a.GetMemcachedServiceAuth("unknown:9000")
if _, ok := err.(UnknownHostPortError); u != "" || p != "" || !ok {
t.Fatal("Expect error trying to get auth for unknown service")
}
u, p, _ = a.GetMemcachedServiceAuth("beta.local:9000")
if u != "_admin" || p != "foobar" {
t.Fatalf("Expect valid creds for beta.local:9000. Got: %s:%s", u, p)
}
u, p, _ = a.GetMemcachedServiceAuth("chi.local:12001")
if u != "_admin" || p != "barfoo" {
t.Fatalf("Expect valid creds for chi.local:12001. Got: %s:%s", u, p)
}
u, p, _ = a.GetHTTPServiceAuth("chi.local:9001")
if u != "@component" || p != "barfoo" {
t.Fatalf("Expect valid http creds for chi.local:9001. Got: %s:%s", u, p)
}
}
func overrideDefClient(c *http.Client) func() {
var old *http.Client
old, http.DefaultClient = http.DefaultClient, c
return func() {
http.DefaultClient = old
}
}
func TestTokenAdmin(t *testing.T) {
url := "http://127.0.0.1:9000/_auth"
tr := newTestingRT("POST", url)
tr.setTokenAuth("Administrator", "saslauthd", "1234567890", "admin")
defer overrideDefClient(&http.Client{Transport: tr})()
a := newAuth(0)
must(a.svc.UpdateDB(&cbauthimpl.Cache{TokenCheckURL: url}, nil))
req, err := http.NewRequest("GET", "http://q:11234/_queryStatsmaybe", nil)
must(err)
req.Header.Set("Cookie", "ui-auth-q=1234567890")
req.Header.Set("ns-server-ui", "yes")
c, err := a.AuthWebCreds(req)
must(err)
tr.assertTripped(t, true)
assertAdmins(t, c, true, false)
if c.Name() != "Administrator" {
t.Errorf("Expect name to be Administrator")
}
if c.Source() != "saslauthd" {
t.Errorf("Expect source to be saslauthd. Got %s", c.Source())
}
if !acc(c.CanAccessBucket("asdasdasdasd")) {
t.Errorf("Expected to be able to access all buckets. Failed at asdasdasdasd")
}
if !acc(c.CanAccessBucket("ffee")) {
t.Errorf("Expected to be able to access all buckets. Failed at ffee")
}
}
func TestUnknownHostPortErrorFormatting(t *testing.T) {
t.Log("Error: ", UnknownHostPortError("asdsd").Error())
}
func TestStaleErrorFormatting(t *testing.T) {
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusUnauthorized)
}))
defer s.Close()
rpcsvc := revrpc.MustService(s.URL + "/test")
a := newAuth(10 * time.Second)
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
oldDef := revrpc.DefaultBabysitErrorPolicy.(revrpc.DefaultErrorPolicy)
defer func() {
revrpc.DefaultBabysitErrorPolicy = oldDef
}()
tmpDef := oldDef
tmpDef.RestartsToExit = 1
revrpc.DefaultBabysitErrorPolicy = tmpDef
runRPCForSvc(rpcsvc, a.svc)
wg.Done()
}()
_, err := a.Auth("", "")
se, ok := err.(*DBStaleError)
if !ok {
t.Fatalf("Expected stale error. Got: %s", err)
}
errString := se.Error()
t.Log("error string: ", errString)
expectedString := "CBAuth database is stale: last reason: Need 200 status!. Got "
if errString[:len(expectedString)] != expectedString {
t.Fatalf("Expecting specific prefix of stale error. Got %s", errString)
}
wg.Wait()
}