/
soma_handler_supervisor__maps.go
587 lines (493 loc) · 11.7 KB
/
soma_handler_supervisor__maps.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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
/*-
Copyright (c) 2016, Jörg Pernfuß <joerg.pernfuss@1und1.de>
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package main
import (
"encoding/hex"
"sync"
"time"
"github.com/1and1/soma/lib/auth"
"github.com/mjolnir42/scrypth64"
"github.com/satori/go.uuid"
)
//
//
// supervisor internal storage format for tokens
type svToken struct {
validFrom time.Time
expiresAt time.Time
binToken []byte
binExpiresAt []byte
salt []byte
}
// read/write locked map of tokens
type svTokenMap struct {
// token(hex.string) -> svToken
TMap map[string]svToken
mutex sync.RWMutex
}
func (t *svTokenMap) read(token string) *svToken {
t.rlock()
defer t.runlock()
if tok, ok := t.TMap[token]; ok {
return &tok
}
return nil
}
func (t *svTokenMap) insert(token, valid, expires, salt string) error {
var (
err error
valTime, expTime time.Time
bExpTime, bSalt, bToken []byte
)
// convert input data into the different formats required to
// perform later actions without conversions
if valTime, err = time.Parse(rfc3339Milli, valid); err != nil {
return err
}
if expTime, err = time.Parse(rfc3339Milli, expires); err != nil {
return err
}
if bExpTime, err = expTime.MarshalBinary(); err != nil {
return err
}
if bToken, err = hex.DecodeString(token); err != nil {
return err
}
if bSalt, err = hex.DecodeString(salt); err != nil {
return err
}
// whiteout unstable subsecond timestamp part with "random" value
copy(bExpTime[9:], []byte{0xde, 0xad, 0xca, 0xfe})
// acquire write lock
t.lock()
defer t.unlock()
// insert token
t.TMap[token] = svToken{
validFrom: valTime,
expiresAt: expTime,
binToken: bToken,
binExpiresAt: bExpTime,
salt: bSalt,
}
return nil
}
// set writelock
func (t *svTokenMap) lock() {
t.mutex.Lock()
}
// set readlock
func (t *svTokenMap) rlock() {
t.mutex.RLock()
}
// release writelock
func (t *svTokenMap) unlock() {
t.mutex.Unlock()
}
// release readlock
func (t *svTokenMap) runlock() {
t.mutex.RUnlock()
}
//
//
// supervisor internal storage format for credentials
type svCredential struct {
id uuid.UUID
validFrom time.Time
expiresAt time.Time
cryptMCF scrypth64.Mcf
resetActive bool
isActive bool
}
type svCredMap struct {
// username -> svCredential
CMap map[string]svCredential
mutex sync.RWMutex
}
func (c *svCredMap) read(user string) *svCredential {
c.rlock()
defer c.runlock()
if cred, ok := c.CMap[user]; ok {
return &cred
}
return nil
}
func (c *svCredMap) insert(user string, uid uuid.UUID, valid, expires time.Time, mcf scrypth64.Mcf) {
c.lock()
defer c.unlock()
c.CMap[user] = svCredential{
id: uid,
validFrom: valid,
expiresAt: expires,
cryptMCF: mcf,
resetActive: false,
isActive: true,
}
}
func (c *svCredMap) restore(user string, uid uuid.UUID, valid, expires time.Time, mcf scrypth64.Mcf, reset, active bool) {
c.lock()
defer c.unlock()
c.CMap[user] = svCredential{
id: uid,
validFrom: valid,
expiresAt: expires,
cryptMCF: mcf,
resetActive: reset,
isActive: active,
}
}
func (c *svCredMap) revoke(user string) {
c.lock()
defer c.unlock()
delete(c.CMap, user)
}
// set writelock
func (c *svCredMap) lock() {
c.mutex.Lock()
}
// set readlock
func (c *svCredMap) rlock() {
c.mutex.RLock()
}
// release writelock
func (c *svCredMap) unlock() {
c.mutex.Unlock()
}
// release readlock
func (c *svCredMap) runlock() {
c.mutex.RUnlock()
}
//
//
// read/write locked map of key exchanges
type svKexMap struct {
// kexid(uuid.string) -> auth.Kex
KMap map[string]auth.Kex
mutex sync.RWMutex
}
// the nonce information would normally mean returning
// a copy is problematic, but since these keys are only
// used for one client/server exchange, they are never
// put back
func (k *svKexMap) read(kexRequest string) *auth.Kex {
k.rlock()
defer k.runlock()
if kex, ok := k.KMap[kexRequest]; ok {
return &kex
}
return nil
}
func (k *svKexMap) insert(kex auth.Kex) {
k.lock()
defer k.unlock()
k.KMap[kex.Request.String()] = kex
}
func (k *svKexMap) remove(kexRequest string) {
k.lock()
defer k.unlock()
delete(k.KMap, kexRequest)
}
// set writelock
func (k *svKexMap) lock() {
k.mutex.Lock()
}
// set readlock
func (k *svKexMap) rlock() {
k.mutex.RLock()
}
// release writelock
func (k *svKexMap) unlock() {
k.mutex.Unlock()
}
// release readlock
func (k *svKexMap) runlock() {
k.mutex.RUnlock()
}
//
//
// read/write locked map of global permissions
type svPermMapGlobal struct {
// user(uuid.string) -> permission(uuid.string) -> grant(uuid.string)
GMap map[string]map[string]string
mutex sync.RWMutex
}
func (g *svPermMapGlobal) grant(user, permission, id string) {
g.lock()
defer g.unlock()
g.load(user, permission, id)
}
func (g *svPermMapGlobal) load(user, permission, id string) {
// zero value for maps is nil
if m, ok := g.GMap[user]; !ok {
g.GMap[user] = make(map[string]string)
} else if m == nil {
g.GMap[user] = make(map[string]string)
}
// grant permission
g.GMap[user][permission] = id
}
func (g *svPermMapGlobal) revoke(user, permission string) {
g.lock()
defer g.unlock()
// user has no permissions
if m, ok := g.GMap[user]; !ok {
return
} else if m == nil {
return
}
// revoke permission
delete(g.GMap[user], permission)
}
// ATTENTION: named return parameter
func (g *svPermMapGlobal) assess(user, permission string) (verdict bool) {
g.rlock()
defer g.runlock()
// map[]map[] is volatile
defer func() {
if r := recover(); r != nil {
verdict = false
}
}()
verdict = false
if m, ok := g.GMap[user]; !ok {
g.GMap[user] = make(map[string]string)
return
} else if m == nil {
g.GMap[user] = make(map[string]string)
return
}
// let zero value `false` work for us
verdict = (g.GMap[user][permission] != "")
return
}
func (g *svPermMapGlobal) lock() {
g.mutex.Lock()
}
func (g *svPermMapGlobal) rlock() {
g.mutex.RLock()
}
func (g *svPermMapGlobal) unlock() {
g.mutex.Unlock()
}
func (g *svPermMapGlobal) runlock() {
g.mutex.RUnlock()
}
//
//
// read/write locked map of global grants
type svGrantMapGlobal struct {
// grant(uuid.string) -> user(uuid.string) -> permission(uuid.string)
GMap map[string][]string
mutex sync.RWMutex
}
func (g *svGrantMapGlobal) record(user, permission, id string) {
g.lock()
defer g.unlock()
g.load(user, permission, id)
}
func (g *svGrantMapGlobal) load(user, permission, id string) {
// zero value for slices is nil
if m, ok := g.GMap[id]; !ok {
g.GMap[id] = make([]string, 0)
} else if m == nil {
g.GMap[id] = make([]string, 0)
}
// record grant
g.GMap[id] = []string{user, permission}
}
func (g *svGrantMapGlobal) discard(id string) {
g.lock()
defer g.unlock()
// unknown grant
if m, ok := g.GMap[id]; !ok {
return
} else if m == nil {
return
}
// delete grant
delete(g.GMap, id)
}
func (g *svGrantMapGlobal) get(id string) []string {
g.rlock()
defer g.runlock()
// it is okay to return nil
return g.GMap[id]
}
func (g *svGrantMapGlobal) lock() {
g.mutex.Lock()
}
func (g *svGrantMapGlobal) rlock() {
g.mutex.RLock()
}
func (g *svGrantMapGlobal) unlock() {
g.mutex.Unlock()
}
func (g *svGrantMapGlobal) runlock() {
g.mutex.RUnlock()
}
//
//
// read/write locked map of limited permissions
type svPermMapLimited struct {
// user(uuid.string) -> permission(uuid.string) -> repository(uuid.string)
LMap map[string]map[string][]string
mutex sync.RWMutex
}
func (l *svPermMapLimited) grant(user, permission, repository string) {
l.lock()
defer l.unlock()
// zero value for maps is nil
if m, ok := l.LMap[user]; !ok {
l.LMap[user] = make(map[string][]string)
} else if m == nil {
l.LMap[user] = make(map[string][]string)
}
// zero value for slices is nil
s := l.LMap[user][permission]
if s == nil {
l.LMap[user][permission] = []string{repository}
} else {
s = append(s, repository)
l.LMap[user][permission] = s
}
}
func (l *svPermMapLimited) revoke(user, permission, repository string) {
l.lock()
defer l.unlock()
// initialize to remove a future nilptr-dereference hazard, but
// return early since the user did not have the permission in the
// first place
if m, ok := l.LMap[user]; !ok {
l.LMap[user] = make(map[string][]string)
return
} else if m == nil {
l.LMap[user] = make(map[string][]string)
return
}
// resolve this nilptr-deref hazard as well before returning
s := l.LMap[user][permission]
if s == nil {
l.LMap[user][permission] = []string{}
return
}
repoloop:
for i, _ := range s {
if s[i] == repository {
s = append(s[:i], s[i+1:]...)
break repoloop
}
}
l.LMap[user][permission] = s
}
// ATTENTION: named return parameter
func (l *svPermMapLimited) assess(user, permission, repository string) (verdict bool) {
l.rlock()
defer l.runlock()
defer func() {
if r := recover(); r != nil {
verdict = false
}
}()
verdict = false
if m, ok := l.LMap[user]; !ok {
l.LMap[user] = make(map[string][]string)
return
} else if m == nil {
l.LMap[user] = make(map[string][]string)
return
}
s := l.LMap[user][permission]
if s == nil {
l.LMap[user][permission] = []string{}
return
}
for _, p := range s {
if p == repository {
verdict = true
return
}
}
return
}
func (l *svPermMapLimited) lock() {
l.mutex.Lock()
}
func (l *svPermMapLimited) rlock() {
l.mutex.RLock()
}
func (l *svPermMapLimited) unlock() {
l.mutex.Unlock()
}
func (l *svPermMapLimited) runlock() {
l.mutex.RUnlock()
}
//
//
// read/write locked map[string]string
type svLockMap struct {
// user uuid -> team uuid
LockMap map[string]string
mutex sync.RWMutex
}
func (slm *svLockMap) insert(key, value string) {
slm.lock()
defer slm.unlock()
slm.LockMap[key] = value
}
func (slm *svLockMap) remove(key string) {
slm.lock()
defer slm.unlock()
delete(slm.LockMap, key)
}
func (slm *svLockMap) get(key string) (string, bool) {
slm.rlock()
defer slm.runlock()
value, ok := slm.LockMap[key]
return value, ok
}
func (slm *svLockMap) scan(value string) (string, bool) {
slm.rlock()
defer slm.runlock()
for key, _ := range slm.LockMap {
if slm.LockMap[key] == value {
return key, true
}
}
return "", false
}
// load is an unlocked insert for bulk loading at startup. The
// bulk loading mechanism must handle the locking itself
func (slm *svLockMap) load(key, value string) {
slm.LockMap[key] = value
}
func (slm *svLockMap) lock() {
slm.mutex.Lock()
}
func (slm *svLockMap) rlock() {
slm.mutex.RLock()
}
func (slm *svLockMap) unlock() {
slm.mutex.Unlock()
}
func (slm *svLockMap) runlock() {
slm.mutex.RUnlock()
}
// vim: ts=4 sw=4 sts=4 noet fenc=utf-8 ffs=unix