-
Notifications
You must be signed in to change notification settings - Fork 356
/
service.go
882 lines (808 loc) · 29.8 KB
/
service.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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
package auth
import (
"context"
"errors"
"fmt"
"reflect"
"strings"
"time"
sq "github.com/Masterminds/squirrel"
"github.com/georgysavva/scany/pgxscan"
"github.com/treeverse/lakefs/pkg/auth/crypt"
"github.com/treeverse/lakefs/pkg/auth/model"
"github.com/treeverse/lakefs/pkg/auth/params"
"github.com/treeverse/lakefs/pkg/auth/wildcard"
"github.com/treeverse/lakefs/pkg/db"
"github.com/treeverse/lakefs/pkg/logging"
"github.com/treeverse/lakefs/pkg/permissions"
)
type AuthorizationRequest struct {
Username string
RequiredPermissions []permissions.Permission
}
type AuthorizationResponse struct {
Allowed bool
Error error
}
type Service interface {
SecretStore() crypt.SecretStore
// users
CreateUser(ctx context.Context, user *model.User) error
DeleteUser(ctx context.Context, username string) error
GetUserByID(ctx context.Context, userID int) (*model.User, error)
GetUser(ctx context.Context, username string) (*model.User, error)
ListUsers(ctx context.Context, params *model.PaginationParams) ([]*model.User, *model.Paginator, error)
// groups
CreateGroup(ctx context.Context, group *model.Group) error
DeleteGroup(ctx context.Context, groupDisplayName string) error
GetGroup(ctx context.Context, groupDisplayName string) (*model.Group, error)
ListGroups(ctx context.Context, params *model.PaginationParams) ([]*model.Group, *model.Paginator, error)
// group<->user memberships
AddUserToGroup(ctx context.Context, username, groupDisplayName string) error
RemoveUserFromGroup(ctx context.Context, username, groupDisplayName string) error
ListUserGroups(ctx context.Context, username string, params *model.PaginationParams) ([]*model.Group, *model.Paginator, error)
ListGroupUsers(ctx context.Context, groupDisplayName string, params *model.PaginationParams) ([]*model.User, *model.Paginator, error)
// policies
WritePolicy(ctx context.Context, policy *model.Policy) error
GetPolicy(ctx context.Context, policyDisplayName string) (*model.Policy, error)
DeletePolicy(ctx context.Context, policyDisplayName string) error
ListPolicies(ctx context.Context, params *model.PaginationParams) ([]*model.Policy, *model.Paginator, error)
// credentials
CreateCredentials(ctx context.Context, username string) (*model.Credential, error)
AddCredentials(ctx context.Context, username, accessKeyID, secretAccessKey string) (*model.Credential, error)
DeleteCredentials(ctx context.Context, username, accessKeyID string) error
GetCredentialsForUser(ctx context.Context, username, accessKeyID string) (*model.Credential, error)
GetCredentials(ctx context.Context, accessKeyID string) (*model.Credential, error)
ListUserCredentials(ctx context.Context, username string, params *model.PaginationParams) ([]*model.Credential, *model.Paginator, error)
// policy<->user attachments
AttachPolicyToUser(ctx context.Context, policyDisplayName, username string) error
DetachPolicyFromUser(ctx context.Context, policyDisplayName, username string) error
ListUserPolicies(ctx context.Context, username string, params *model.PaginationParams) ([]*model.Policy, *model.Paginator, error)
ListEffectivePolicies(ctx context.Context, username string, params *model.PaginationParams) ([]*model.Policy, *model.Paginator, error)
// policy<->group attachments
AttachPolicyToGroup(ctx context.Context, policyDisplayName, groupDisplayName string) error
DetachPolicyFromGroup(ctx context.Context, policyDisplayName, groupDisplayName string) error
ListGroupPolicies(ctx context.Context, groupDisplayName string, params *model.PaginationParams) ([]*model.Policy, *model.Paginator, error)
// authorize user for an action
Authorize(ctx context.Context, req *AuthorizationRequest) (*AuthorizationResponse, error)
}
var psql = sq.StatementBuilder.PlaceholderFormat(sq.Dollar)
// fieldNameByTag returns the name of the field of t that is tagged tag on key, or an empty string.
func fieldByTag(t reflect.Type, key, tag string) string {
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
if l, ok := field.Tag.Lookup(key); ok {
if l == tag {
return field.Name
}
}
}
return ""
}
const maxPage = 1000
func ListPaged(ctx context.Context, db db.Querier, retType reflect.Type, params *model.PaginationParams, tokenColumnName string, queryBuilder sq.SelectBuilder) (*reflect.Value, *model.Paginator, error) {
ptrType := reflect.PtrTo(retType)
tokenField := fieldByTag(retType, "db", tokenColumnName)
if tokenField == "" {
return nil, nil, fmt.Errorf("[I] no field %s: %w", tokenColumnName, ErrNoField)
}
slice := reflect.MakeSlice(reflect.SliceOf(ptrType), 0, 0)
queryBuilder = queryBuilder.OrderBy(tokenColumnName)
amount := 0
if params != nil {
queryBuilder = queryBuilder.Where(sq.Gt{tokenColumnName: params.After})
if params.Amount >= 0 {
amount = params.Amount + 1
}
}
if amount > maxPage {
amount = maxPage
}
if amount > 0 {
queryBuilder = queryBuilder.Limit(uint64(amount))
}
query, args, err := queryBuilder.ToSql()
if err != nil {
return nil, nil, fmt.Errorf("convert to SQL: %w", err)
}
rows, err := db.Query(ctx, query, args...)
if err != nil {
return nil, nil, fmt.Errorf("query DB: %w", err)
}
rowScanner := pgxscan.NewRowScanner(rows)
for rows.Next() {
value := reflect.New(retType)
if err = rowScanner.Scan(value.Interface()); err != nil {
return nil, nil, fmt.Errorf("scan value from DB: %w", err)
}
slice = reflect.Append(slice, value)
}
p := &model.Paginator{}
if params != nil && params.Amount >= 0 && slice.Len() == params.Amount+1 {
// we have more pages
slice = slice.Slice(0, params.Amount)
p.Amount = params.Amount
lastElem := slice.Index(slice.Len() - 1).Elem()
p.NextPageToken = lastElem.FieldByName(tokenField).String()
return &slice, p, nil
}
p.Amount = slice.Len()
return &slice, p, nil
}
func getUser(tx db.Tx, username string) (*model.User, error) {
user := &model.User{}
err := tx.Get(user, `SELECT * FROM auth_users WHERE display_name = $1`, username)
if err != nil {
return nil, err
}
return user, nil
}
func getGroup(tx db.Tx, groupDisplayName string) (*model.Group, error) {
group := &model.Group{}
err := tx.Get(group, `SELECT * FROM auth_groups WHERE display_name = $1`, groupDisplayName)
if err != nil {
return nil, err
}
return group, nil
}
func getPolicy(tx db.Tx, policyDisplayName string) (*model.Policy, error) {
policy := &model.Policy{}
err := tx.Get(policy, `SELECT * FROM auth_policies WHERE display_name = $1`, policyDisplayName)
if err != nil {
return nil, err
}
return policy, nil
}
func deleteOrNotFound(tx db.Tx, stmt string, args ...interface{}) error {
res, err := tx.Exec(stmt, args...)
if err != nil {
return err
}
numRows := res.RowsAffected()
if numRows == 0 {
return ErrNotFound
}
return nil
}
func genAccessKeyID() string {
const accessKeyLength = 14
key := KeyGenerator(accessKeyLength)
return fmt.Sprintf("%s%s%s", "AKIAJ", key, "Q")
}
func genSecretAccessKey() string {
const secretKeyLength = 30
return Base64StringGenerator(secretKeyLength)
}
type DBAuthService struct {
db db.Database
secretStore crypt.SecretStore
cache Cache
}
func NewDBAuthService(db db.Database, secretStore crypt.SecretStore, cacheConf params.ServiceCache) *DBAuthService {
logging.Default().Info("initialized Auth service")
var cache Cache
if cacheConf.Enabled {
cache = NewLRUCache(cacheConf.Size, cacheConf.TTL, cacheConf.EvictionJitter)
} else {
cache = &DummyCache{}
}
return &DBAuthService{
db: db,
secretStore: secretStore,
cache: cache,
}
}
func (s *DBAuthService) decryptSecret(value []byte) (string, error) {
decrypted, err := s.secretStore.Decrypt(value)
if err != nil {
return "", err
}
return string(decrypted), nil
}
func (s *DBAuthService) encryptSecret(secretAccessKey string) ([]byte, error) {
encrypted, err := s.secretStore.Encrypt([]byte(secretAccessKey))
if err != nil {
return nil, err
}
return encrypted, nil
}
func (s *DBAuthService) SecretStore() crypt.SecretStore {
return s.secretStore
}
func (s *DBAuthService) DB() db.Database {
return s.db
}
func (s *DBAuthService) CreateUser(ctx context.Context, user *model.User) error {
_, err := s.db.Transact(ctx, func(tx db.Tx) (interface{}, error) {
if err := model.ValidateAuthEntityID(user.Username); err != nil {
return nil, err
}
err := tx.Get(user,
`INSERT INTO auth_users (display_name, created_at) VALUES ($1, $2) RETURNING id`, user.Username, user.CreatedAt)
return nil, err
})
return err
}
func (s *DBAuthService) DeleteUser(ctx context.Context, username string) error {
_, err := s.db.Transact(ctx, func(tx db.Tx) (interface{}, error) {
return nil, deleteOrNotFound(tx, `DELETE FROM auth_users WHERE display_name = $1`, username)
})
return err
}
func (s *DBAuthService) GetUser(ctx context.Context, username string) (*model.User, error) {
return s.cache.GetUser(username, func() (*model.User, error) {
user, err := s.db.Transact(ctx, func(tx db.Tx) (interface{}, error) {
return getUser(tx, username)
}, db.ReadOnly())
if err != nil {
return nil, err
}
return user.(*model.User), nil
})
}
func (s *DBAuthService) GetUserByID(ctx context.Context, userID int) (*model.User, error) {
return s.cache.GetUserByID(userID, func() (*model.User, error) {
user, err := s.db.Transact(ctx, func(tx db.Tx) (interface{}, error) {
user := &model.User{}
err := tx.Get(user, `SELECT * FROM auth_users WHERE id = $1`, userID)
if err != nil {
return nil, err
}
return user, nil
}, db.ReadOnly())
if err != nil {
return nil, err
}
return user.(*model.User), nil
})
}
func (s *DBAuthService) ListUsers(ctx context.Context, params *model.PaginationParams) ([]*model.User, *model.Paginator, error) {
var user model.User
slice, paginator, err := ListPaged(ctx, s.db, reflect.TypeOf(user), params, "display_name",
psql.Select("*").
From("auth_users").
Where(sq.Like{"display_name": fmt.Sprint(params.Prefix, "%")}))
if slice == nil {
return nil, paginator, err
}
return slice.Interface().([]*model.User), paginator, err
}
func (s *DBAuthService) ListUserCredentials(ctx context.Context, username string, params *model.PaginationParams) ([]*model.Credential, *model.Paginator, error) {
var credential model.Credential
slice, paginator, err := ListPaged(ctx, s.db, reflect.TypeOf(credential), params, "access_key_id", psql.Select("auth_credentials.*").
From("auth_credentials").
Join("auth_users ON (auth_credentials.user_id = auth_users.id)").
Where(sq.And{
sq.Eq{"auth_users.display_name": username},
sq.Like{"display_name": fmt.Sprint(params.Prefix, "%")},
}))
if slice == nil {
return nil, paginator, err
}
return slice.Interface().([]*model.Credential), paginator, err
}
func (s *DBAuthService) AttachPolicyToUser(ctx context.Context, policyDisplayName, username string) error {
_, err := s.db.Transact(ctx, func(tx db.Tx) (interface{}, error) {
if _, err := getUser(tx, username); err != nil {
return nil, fmt.Errorf("%s: %w", username, err)
}
if _, err := getPolicy(tx, policyDisplayName); err != nil {
return nil, fmt.Errorf("%s: %w", policyDisplayName, err)
}
_, err := tx.Exec(`
INSERT INTO auth_user_policies (user_id, policy_id)
VALUES (
(SELECT id FROM auth_users WHERE display_name = $1),
(SELECT id FROM auth_policies WHERE display_name = $2)
)`, username, policyDisplayName)
if errors.Is(err, db.ErrAlreadyExists) {
return nil, fmt.Errorf("policy attachment: %w", ErrAlreadyExists)
}
return nil, err
})
return err
}
func (s *DBAuthService) DetachPolicyFromUser(ctx context.Context, policyDisplayName, username string) error {
_, err := s.db.Transact(ctx, func(tx db.Tx) (interface{}, error) {
if _, err := getUser(tx, username); err != nil {
return nil, fmt.Errorf("%s: %w", username, err)
}
if _, err := getPolicy(tx, policyDisplayName); err != nil {
return nil, fmt.Errorf("%s: %w", policyDisplayName, err)
}
return nil, deleteOrNotFound(tx, `
DELETE FROM auth_user_policies USING auth_users, auth_policies
WHERE auth_user_policies.user_id = auth_users.id
AND auth_user_policies.policy_id = auth_policies.id
AND auth_users.display_name = $1
AND auth_policies.display_name = $2`,
username, policyDisplayName)
})
return err
}
func (s *DBAuthService) ListUserPolicies(ctx context.Context, username string, params *model.PaginationParams) ([]*model.Policy, *model.Paginator, error) {
var policy model.Policy
sub := psql.Select("auth_policies.*").
From("auth_policies").
Join("auth_user_policies ON (auth_policies.id = auth_user_policies.policy_id)").
Join("auth_users ON (auth_user_policies.user_id = auth_users.id)").
Where(sq.And{
sq.Eq{"auth_users.display_name": username},
sq.Like{"auth_policies.display_name": fmt.Sprint(params.Prefix, "%")},
})
slice, paginator, err := ListPaged(ctx, s.db, reflect.TypeOf(policy), params, "display_name",
psql.Select("*").FromSelect(sub, "p"))
if slice == nil {
return nil, paginator, err
}
return slice.Interface().([]*model.Policy), paginator, nil
}
func (s *DBAuthService) getEffectivePolicies(ctx context.Context, username string, params *model.PaginationParams) ([]*model.Policy, *model.Paginator, error) {
// resolve all policies attached to the user and its groups
resolvedCte := `
WITH resolved_policies_view AS (
SELECT auth_policies.id, auth_policies.created_at, auth_policies.display_name, auth_policies.statement, auth_users.display_name AS user_display_name
FROM auth_policies INNER JOIN
auth_user_policies ON (auth_policies.id = auth_user_policies.policy_id) INNER JOIN
auth_users ON (auth_users.id = auth_user_policies.user_id)
UNION
SELECT auth_policies.id, auth_policies.created_at, auth_policies.display_name, auth_policies.statement, auth_users.display_name AS user_display_name
FROM auth_policies INNER JOIN
auth_group_policies ON (auth_policies.id = auth_group_policies.policy_id) INNER JOIN
auth_groups ON (auth_groups.id = auth_group_policies.group_id) INNER JOIN
auth_user_groups ON (auth_user_groups.group_id = auth_groups.id) INNER JOIN
auth_users ON (auth_users.id = auth_user_groups.user_id)
)`
var policy model.Policy
slice, paginator, err := ListPaged(ctx,
s.db, reflect.TypeOf(policy), params, "display_name",
psql.Select("id", "created_at", "display_name", "statement").
Prefix(resolvedCte).
From("resolved_policies_view").
Where(sq.And{
sq.Eq{"user_display_name": username},
sq.Like{"display_name": fmt.Sprint(params.Prefix, "%")},
}))
if slice == nil {
return nil, paginator, err
}
return slice.Interface().([]*model.Policy), paginator, nil
}
func (s *DBAuthService) ListEffectivePolicies(ctx context.Context, username string, params *model.PaginationParams) ([]*model.Policy, *model.Paginator, error) {
if params.Amount == -1 {
// read through the cache when requesting the full list
policies, err := s.cache.GetUserPolicies(username, func() ([]*model.Policy, error) {
policies, _, err := s.getEffectivePolicies(ctx, username, params)
return policies, err
})
if err != nil {
return nil, nil, err
}
return policies, &model.Paginator{Amount: len(policies)}, nil
}
return s.getEffectivePolicies(ctx, username, params)
}
func (s *DBAuthService) ListGroupPolicies(ctx context.Context, groupDisplayName string, params *model.PaginationParams) ([]*model.Policy, *model.Paginator, error) {
var policy model.Policy
query := psql.Select("auth_policies.*").
From("auth_policies").
Join("auth_group_policies ON (auth_policies.id = auth_group_policies.policy_id)").
Join("auth_groups ON (auth_group_policies.group_id = auth_groups.id)").
Where(sq.And{
sq.Eq{"auth_groups.display_name": groupDisplayName},
sq.Like{"auth_policies.display_name": fmt.Sprint(params.Prefix, "%")},
})
slice, paginator, err := ListPaged(ctx, s.db, reflect.TypeOf(policy), params, "display_name",
psql.Select("*").FromSelect(query, "p"))
if err != nil {
return nil, paginator, err
}
return slice.Interface().([]*model.Policy), paginator, nil
}
func (s *DBAuthService) CreateGroup(ctx context.Context, group *model.Group) error {
_, err := s.db.Transact(ctx, func(tx db.Tx) (interface{}, error) {
if err := model.ValidateAuthEntityID(group.DisplayName); err != nil {
return nil, err
}
return nil, tx.Get(group, `INSERT INTO auth_groups (display_name, created_at) VALUES ($1, $2) RETURNING id`,
group.DisplayName, group.CreatedAt)
})
return err
}
func (s *DBAuthService) DeleteGroup(ctx context.Context, groupDisplayName string) error {
_, err := s.db.Transact(ctx, func(tx db.Tx) (interface{}, error) {
return nil, deleteOrNotFound(tx, `DELETE FROM auth_groups WHERE display_name = $1`, groupDisplayName)
})
return err
}
func (s *DBAuthService) GetGroup(ctx context.Context, groupDisplayName string) (*model.Group, error) {
group, err := s.db.Transact(ctx, func(tx db.Tx) (interface{}, error) {
return getGroup(tx, groupDisplayName)
}, db.ReadOnly())
if err != nil {
return nil, err
}
return group.(*model.Group), nil
}
func (s *DBAuthService) ListGroups(ctx context.Context, params *model.PaginationParams) ([]*model.Group, *model.Paginator, error) {
var group model.Group
slice, paginator, err := ListPaged(ctx, s.db, reflect.TypeOf(group), params, "display_name",
psql.Select("*").
From("auth_groups").
Where(sq.Like{"display_name": fmt.Sprint(params.Prefix, "%")}))
if err != nil {
return nil, paginator, err
}
return slice.Interface().([]*model.Group), paginator, nil
}
func (s *DBAuthService) AddUserToGroup(ctx context.Context, username, groupDisplayName string) error {
_, err := s.db.Transact(ctx, func(tx db.Tx) (interface{}, error) {
if _, err := getUser(tx, username); err != nil {
return nil, fmt.Errorf("%s: %w", username, err)
}
if _, err := getGroup(tx, groupDisplayName); err != nil {
return nil, fmt.Errorf("%s: %w", groupDisplayName, err)
}
_, err := tx.Exec(`
INSERT INTO auth_user_groups (user_id, group_id)
VALUES (
(SELECT id FROM auth_users WHERE display_name = $1),
(SELECT id FROM auth_groups WHERE display_name = $2)
)`, username, groupDisplayName)
if errors.Is(err, db.ErrAlreadyExists) {
return nil, fmt.Errorf("group membership: %w", ErrAlreadyExists)
}
return nil, err
})
return err
}
func (s *DBAuthService) RemoveUserFromGroup(ctx context.Context, username, groupDisplayName string) error {
_, err := s.db.Transact(ctx, func(tx db.Tx) (interface{}, error) {
if _, err := getUser(tx, username); err != nil {
return nil, fmt.Errorf("%s: %w", username, err)
}
if _, err := getGroup(tx, groupDisplayName); err != nil {
return nil, fmt.Errorf("%s: %w", groupDisplayName, err)
}
return nil, deleteOrNotFound(tx, `
DELETE FROM auth_user_groups USING auth_users, auth_groups
WHERE auth_user_groups.user_id = auth_users.id
AND auth_user_groups.group_id = auth_groups.id
AND auth_users.display_name = $1
AND auth_groups.display_name = $2`,
username, groupDisplayName)
})
return err
}
func (s *DBAuthService) ListUserGroups(ctx context.Context, username string, params *model.PaginationParams) ([]*model.Group, *model.Paginator, error) {
type res struct {
groups []*model.Group
paginator *model.Paginator
}
result, err := s.db.Transact(ctx, func(tx db.Tx) (interface{}, error) {
if _, err := getUser(tx, username); err != nil {
return nil, err
}
groups := make([]*model.Group, 0)
err := tx.Select(&groups, `
SELECT auth_groups.* FROM auth_groups
INNER JOIN auth_user_groups ON (auth_groups.id = auth_user_groups.group_id)
INNER JOIN auth_users ON (auth_user_groups.user_id = auth_users.id)
WHERE
auth_users.display_name = $1
AND auth_groups.display_name > $2
AND auth_groups.display_name like $3
ORDER BY auth_groups.display_name
LIMIT $4`,
username, params.After, fmt.Sprint(params.Prefix, "%"), params.Amount+1)
if err != nil {
return nil, err
}
p := &model.Paginator{}
if len(groups) == params.Amount+1 {
// we have more pages
groups = groups[0:params.Amount]
p.Amount = params.Amount
p.NextPageToken = groups[len(groups)-1].DisplayName
return &res{groups, p}, nil
}
p.Amount = len(groups)
return &res{groups, p}, nil
})
if err != nil {
return nil, nil, err
}
return result.(*res).groups, result.(*res).paginator, nil
}
func (s *DBAuthService) ListGroupUsers(ctx context.Context, groupDisplayName string, params *model.PaginationParams) ([]*model.User, *model.Paginator, error) {
type res struct {
users []*model.User
paginator *model.Paginator
}
result, err := s.db.Transact(ctx, func(tx db.Tx) (interface{}, error) {
if _, err := getGroup(tx, groupDisplayName); err != nil {
return nil, err
}
users := make([]*model.User, 0)
err := tx.Select(&users, `
SELECT auth_users.* FROM auth_users
INNER JOIN auth_user_groups ON (auth_users.id = auth_user_groups.user_id)
INNER JOIN auth_groups ON (auth_user_groups.group_id = auth_groups.id)
WHERE
auth_groups.display_name = $1
AND auth_users.display_name > $2
AND auth_users.display_name like $3
ORDER BY auth_groups.display_name
LIMIT $4`,
groupDisplayName, params.After, fmt.Sprint(params.Prefix, "%"), params.Amount+1)
if err != nil {
return nil, err
}
p := &model.Paginator{}
if len(users) == params.Amount+1 {
// we have more pages
users = users[0:params.Amount]
p.Amount = params.Amount
p.NextPageToken = users[len(users)-1].Username
return &res{users, p}, nil
}
p.Amount = len(users)
return &res{users, p}, nil
})
if err != nil {
return nil, nil, err
}
return result.(*res).users, result.(*res).paginator, nil
}
func (s *DBAuthService) WritePolicy(ctx context.Context, policy *model.Policy) error {
_, err := s.db.Transact(ctx, func(tx db.Tx) (interface{}, error) {
if err := model.ValidateAuthEntityID(policy.DisplayName); err != nil {
return nil, err
}
for _, stmt := range policy.Statement {
for _, action := range stmt.Action {
if err := model.ValidateActionName(action); err != nil {
return nil, err
}
}
if err := model.ValidateArn(stmt.Resource); err != nil {
return nil, err
}
if err := model.ValidateStatementEffect(stmt.Effect); err != nil {
return nil, err
}
}
return nil, tx.Get(policy, `
INSERT INTO auth_policies (display_name, created_at, statement)
VALUES ($1, $2, $3)
ON CONFLICT (display_name) DO UPDATE SET statement = $3
RETURNING id`,
policy.DisplayName, policy.CreatedAt, policy.Statement)
})
return err
}
func (s *DBAuthService) GetPolicy(ctx context.Context, policyDisplayName string) (*model.Policy, error) {
policy, err := s.db.Transact(ctx, func(tx db.Tx) (interface{}, error) {
return getPolicy(tx, policyDisplayName)
}, db.ReadOnly())
if err != nil {
return nil, err
}
return policy.(*model.Policy), nil
}
func (s *DBAuthService) DeletePolicy(ctx context.Context, policyDisplayName string) error {
_, err := s.db.Transact(ctx, func(tx db.Tx) (interface{}, error) {
return nil, deleteOrNotFound(tx, `DELETE FROM auth_policies WHERE display_name = $1`, policyDisplayName)
})
return err
}
func (s *DBAuthService) ListPolicies(ctx context.Context, params *model.PaginationParams) ([]*model.Policy, *model.Paginator, error) {
type res struct {
policies []*model.Policy
paginator *model.Paginator
}
result, err := s.db.Transact(ctx, func(tx db.Tx) (interface{}, error) {
policies := make([]*model.Policy, 0)
err := tx.Select(&policies, `
SELECT *
FROM auth_policies
WHERE display_name > $1
AND display_name LIKE $2
ORDER BY display_name
LIMIT $3`,
params.After, fmt.Sprint(params.Prefix, "%"), params.Amount+1)
if err != nil {
return nil, err
}
p := &model.Paginator{}
if len(policies) == params.Amount+1 {
// we have more pages
policies = policies[0:params.Amount]
p.Amount = params.Amount
p.NextPageToken = policies[len(policies)-1].DisplayName
return &res{policies, p}, nil
}
p.Amount = len(policies)
return &res{policies, p}, nil
})
if err != nil {
return nil, nil, err
}
return result.(*res).policies, result.(*res).paginator, nil
}
func (s *DBAuthService) CreateCredentials(ctx context.Context, username string) (*model.Credential, error) {
accessKeyID := genAccessKeyID()
secretAccessKey := genSecretAccessKey()
return s.AddCredentials(ctx, username, accessKeyID, secretAccessKey)
}
func (s *DBAuthService) AddCredentials(ctx context.Context, username, accessKeyID, secretAccessKey string) (*model.Credential, error) {
now := time.Now()
encryptedKey, err := s.encryptSecret(secretAccessKey)
if err != nil {
return nil, err
}
credentials, err := s.db.Transact(ctx, func(tx db.Tx) (interface{}, error) {
user, err := getUser(tx, username)
if err != nil {
return nil, err
}
c := &model.Credential{
AccessKeyID: accessKeyID,
SecretAccessKey: secretAccessKey,
SecretAccessKeyEncryptedBytes: encryptedKey,
IssuedDate: now,
UserID: user.ID,
}
_, err = tx.Exec(`
INSERT INTO auth_credentials (access_key_id, secret_access_key, issued_date, user_id)
VALUES ($1, $2, $3, $4)`,
c.AccessKeyID,
encryptedKey,
c.IssuedDate,
c.UserID,
)
return c, err
})
if err != nil {
return nil, err
}
return credentials.(*model.Credential), err
}
func (s *DBAuthService) DeleteCredentials(ctx context.Context, username, accessKeyID string) error {
_, err := s.db.Transact(ctx, func(tx db.Tx) (interface{}, error) {
return nil, deleteOrNotFound(tx, `
DELETE FROM auth_credentials USING auth_users
WHERE auth_credentials.user_id = auth_users.id
AND auth_users.display_name = $1
AND auth_credentials.access_key_id = $2`,
username, accessKeyID)
})
return err
}
func (s *DBAuthService) AttachPolicyToGroup(ctx context.Context, policyDisplayName, groupDisplayName string) error {
_, err := s.db.Transact(ctx, func(tx db.Tx) (interface{}, error) {
if _, err := getGroup(tx, groupDisplayName); err != nil {
return nil, fmt.Errorf("%s: %w", groupDisplayName, err)
}
if _, err := getPolicy(tx, policyDisplayName); err != nil {
return nil, fmt.Errorf("%s: %w", policyDisplayName, err)
}
_, err := tx.Exec(`
INSERT INTO auth_group_policies (group_id, policy_id)
VALUES (
(SELECT id FROM auth_groups WHERE display_name = $1),
(SELECT id FROM auth_policies WHERE display_name = $2)
)`, groupDisplayName, policyDisplayName)
if errors.Is(err, db.ErrAlreadyExists) {
return nil, fmt.Errorf("policy attachment: %w", ErrAlreadyExists)
}
return nil, err
})
return err
}
func (s *DBAuthService) DetachPolicyFromGroup(ctx context.Context, policyDisplayName, groupDisplayName string) error {
_, err := s.db.Transact(ctx, func(tx db.Tx) (interface{}, error) {
if _, err := getGroup(tx, groupDisplayName); err != nil {
return nil, fmt.Errorf("%s: %w", groupDisplayName, err)
}
if _, err := getPolicy(tx, policyDisplayName); err != nil {
return nil, fmt.Errorf("%s: %w", policyDisplayName, err)
}
return nil, deleteOrNotFound(tx, `
DELETE FROM auth_group_policies USING auth_groups, auth_policies
WHERE auth_group_policies.group_id = auth_groups.id
AND auth_group_policies.policy_id = auth_policies.id
AND auth_groups.display_name = $1
AND auth_policies.display_name = $2`,
groupDisplayName, policyDisplayName)
})
return err
}
func (s *DBAuthService) GetCredentialsForUser(ctx context.Context, username, accessKeyID string) (*model.Credential, error) {
credentials, err := s.db.Transact(ctx, func(tx db.Tx) (interface{}, error) {
if _, err := getUser(tx, username); err != nil {
return nil, err
}
credentials := &model.Credential{}
err := tx.Get(credentials, `
SELECT auth_credentials.*
FROM auth_credentials
INNER JOIN auth_users ON (auth_credentials.user_id = auth_users.id)
WHERE auth_credentials.access_key_id = $1
AND auth_users.display_name = $2`, accessKeyID, username)
if err != nil {
return nil, err
}
return credentials, nil
})
if err != nil {
return nil, err
}
return credentials.(*model.Credential), nil
}
func (s *DBAuthService) GetCredentials(ctx context.Context, accessKeyID string) (*model.Credential, error) {
return s.cache.GetCredential(accessKeyID, func() (*model.Credential, error) {
credentials, err := s.db.Transact(ctx, func(tx db.Tx) (interface{}, error) {
credentials := &model.Credential{}
err := tx.Get(credentials, `SELECT * FROM auth_credentials WHERE auth_credentials.access_key_id = $1`,
accessKeyID)
if err != nil {
return nil, err
}
key, err := s.decryptSecret(credentials.SecretAccessKeyEncryptedBytes)
if err != nil {
return nil, err
}
credentials.SecretAccessKey = key
return credentials, nil
})
if err != nil {
return nil, err
}
return credentials.(*model.Credential), nil
})
}
func interpolateUser(resource string, username string) string {
return strings.ReplaceAll(resource, "${user}", username)
}
func (s *DBAuthService) Authorize(ctx context.Context, req *AuthorizationRequest) (*AuthorizationResponse, error) {
policies, _, err := s.ListEffectivePolicies(ctx, req.Username, &model.PaginationParams{
After: "", // all
Amount: -1, // all
})
if err != nil {
return nil, err
}
allowed := false
for _, perm := range req.RequiredPermissions {
for _, policy := range policies {
for _, stmt := range policy.Statement {
resource := interpolateUser(stmt.Resource, req.Username)
if !ArnMatch(resource, perm.Resource) {
continue
}
for _, action := range stmt.Action {
if !wildcard.Match(action, perm.Action) {
continue // not a matching action
}
if stmt.Effect == model.StatementEffectDeny {
// this is a "Deny" and it takes precedence
return &AuthorizationResponse{
Allowed: false,
Error: ErrInsufficientPermissions,
}, nil
}
allowed = true
}
}
}
}
if !allowed {
return &AuthorizationResponse{
Allowed: false,
Error: ErrInsufficientPermissions,
}, nil
}
// we're allowed!
return &AuthorizationResponse{Allowed: true}, nil
}