@@ -91,6 +91,9 @@ type AuthenticateParamIndex struct{}
91
91
// AuthenticateParamSimpleTokenPrefix is used for a key of context in the parameters of Authenticate()
92
92
type AuthenticateParamSimpleTokenPrefix struct {}
93
93
94
+ // saveConsistentIndexFunc is used to sync consistentIndex to backend, now reusing store.saveIndex
95
+ type saveConsistentIndexFunc func (tx backend.BatchTx )
96
+
94
97
// AuthStore defines auth storage interface.
95
98
type AuthStore interface {
96
99
// AuthEnable turns on the authentication feature
@@ -183,6 +186,9 @@ type AuthStore interface {
183
186
184
187
// HasRole checks that user has role
185
188
HasRole (user , role string ) bool
189
+
190
+ // SetConsistentIndexSyncer sets consistentIndex syncer
191
+ SetConsistentIndexSyncer (syncer saveConsistentIndexFunc )
186
192
}
187
193
188
194
type TokenProvider interface {
@@ -206,10 +212,14 @@ type authStore struct {
206
212
207
213
rangePermCache map [string ]* unifiedRangePermissions // username -> unifiedRangePermissions
208
214
209
- tokenProvider TokenProvider
210
- bcryptCost int // the algorithm cost / strength for hashing auth passwords
215
+ tokenProvider TokenProvider
216
+ syncConsistentIndex saveConsistentIndexFunc
217
+ bcryptCost int // the algorithm cost / strength for hashing auth passwords
211
218
}
212
219
220
+ func (as * authStore ) SetConsistentIndexSyncer (syncer saveConsistentIndexFunc ) {
221
+ as .syncConsistentIndex = syncer
222
+ }
213
223
func (as * authStore ) AuthEnable () error {
214
224
as .enabledMu .Lock ()
215
225
defer as .enabledMu .Unlock ()
@@ -258,6 +268,7 @@ func (as *authStore) AuthDisable() {
258
268
tx .Lock ()
259
269
tx .UnsafePut (authBucketName , enableFlagKey , authDisabled )
260
270
as .commitRevision (tx )
271
+ as .saveConsistentIndex (tx )
261
272
tx .Unlock ()
262
273
b .ForceCommit ()
263
274
@@ -403,6 +414,7 @@ func (as *authStore) UserAdd(r *pb.AuthUserAddRequest) (*pb.AuthUserAddResponse,
403
414
putUser (as .lg , tx , newUser )
404
415
405
416
as .commitRevision (tx )
417
+ as .saveConsistentIndex (tx )
406
418
407
419
as .lg .Info ("added a user" , zap .String ("user-name" , r .Name ))
408
420
return & pb.AuthUserAddResponse {}, nil
@@ -426,6 +438,7 @@ func (as *authStore) UserDelete(r *pb.AuthUserDeleteRequest) (*pb.AuthUserDelete
426
438
delUser (tx , r .Name )
427
439
428
440
as .commitRevision (tx )
441
+ as .saveConsistentIndex (tx )
429
442
430
443
as .invalidateCachedPerm (r .Name )
431
444
as .tokenProvider .invalidateUser (r .Name )
@@ -470,6 +483,7 @@ func (as *authStore) UserChangePassword(r *pb.AuthUserChangePasswordRequest) (*p
470
483
putUser (as .lg , tx , updatedUser )
471
484
472
485
as .commitRevision (tx )
486
+ as .saveConsistentIndex (tx )
473
487
474
488
as .invalidateCachedPerm (r .Name )
475
489
as .tokenProvider .invalidateUser (r .Name )
@@ -518,6 +532,7 @@ func (as *authStore) UserGrantRole(r *pb.AuthUserGrantRoleRequest) (*pb.AuthUser
518
532
as .invalidateCachedPerm (r .User )
519
533
520
534
as .commitRevision (tx )
535
+ as .saveConsistentIndex (tx )
521
536
522
537
as .lg .Info (
523
538
"granted a role to a user" ,
@@ -596,6 +611,7 @@ func (as *authStore) UserRevokeRole(r *pb.AuthUserRevokeRoleRequest) (*pb.AuthUs
596
611
as .invalidateCachedPerm (r .Name )
597
612
598
613
as .commitRevision (tx )
614
+ as .saveConsistentIndex (tx )
599
615
600
616
as .lg .Info (
601
617
"revoked a role from a user" ,
@@ -666,6 +682,7 @@ func (as *authStore) RoleRevokePermission(r *pb.AuthRoleRevokePermissionRequest)
666
682
as .clearCachedPerm ()
667
683
668
684
as .commitRevision (tx )
685
+ as .saveConsistentIndex (tx )
669
686
670
687
as .lg .Info (
671
688
"revoked a permission on range" ,
@@ -717,6 +734,7 @@ func (as *authStore) RoleDelete(r *pb.AuthRoleDeleteRequest) (*pb.AuthRoleDelete
717
734
}
718
735
719
736
as .commitRevision (tx )
737
+ as .saveConsistentIndex (tx )
720
738
721
739
as .lg .Info ("deleted a role" , zap .String ("role-name" , r .Role ))
722
740
return & pb.AuthRoleDeleteResponse {}, nil
@@ -743,6 +761,7 @@ func (as *authStore) RoleAdd(r *pb.AuthRoleAddRequest) (*pb.AuthRoleAddResponse,
743
761
putRole (as .lg , tx , newRole )
744
762
745
763
as .commitRevision (tx )
764
+ as .saveConsistentIndex (tx )
746
765
747
766
as .lg .Info ("created a role" , zap .String ("role-name" , r .Name ))
748
767
return & pb.AuthRoleAddResponse {}, nil
@@ -781,6 +800,16 @@ func (as *authStore) RoleGrantPermission(r *pb.AuthRoleGrantPermissionRequest) (
781
800
})
782
801
783
802
if idx < len (role .KeyPermission ) && bytes .Equal (role .KeyPermission [idx ].Key , r .Perm .Key ) && bytes .Equal (role .KeyPermission [idx ].RangeEnd , r .Perm .RangeEnd ) {
803
+ if role .KeyPermission [idx ].PermType == r .Perm .PermType {
804
+ as .lg .Warn (
805
+ "ignored grant permission request to a role, existing permission" ,
806
+ zap .String ("role-name" , r .Name ),
807
+ zap .ByteString ("key" , r .Perm .Key ),
808
+ zap .ByteString ("range-end" , r .Perm .RangeEnd ),
809
+ zap .String ("permission-type" , authpb .Permission_Type_name [int32 (r .Perm .PermType )]),
810
+ )
811
+ return & pb.AuthRoleGrantPermissionResponse {}, nil
812
+ }
784
813
// update existing permission
785
814
role .KeyPermission [idx ].PermType = r .Perm .PermType
786
815
} else {
@@ -802,6 +831,7 @@ func (as *authStore) RoleGrantPermission(r *pb.AuthRoleGrantPermissionRequest) (
802
831
as .clearCachedPerm ()
803
832
804
833
as .commitRevision (tx )
834
+ as .saveConsistentIndex (tx )
805
835
806
836
as .lg .Info (
807
837
"granted/updated a permission to a user" ,
@@ -1035,6 +1065,7 @@ func NewAuthStore(lg *zap.Logger, be backend.Backend, tp TokenProvider, bcryptCo
1035
1065
1036
1066
if as .Revision () == 0 {
1037
1067
as .commitRevision (tx )
1068
+ as .saveConsistentIndex (tx )
1038
1069
}
1039
1070
1040
1071
tx .Unlock ()
@@ -1279,3 +1310,11 @@ func (as *authStore) HasRole(user, role string) bool {
1279
1310
func (as * authStore ) BcryptCost () int {
1280
1311
return as .bcryptCost
1281
1312
}
1313
+
1314
+ func (as * authStore ) saveConsistentIndex (tx backend.BatchTx ) {
1315
+ if as .syncConsistentIndex != nil {
1316
+ as .syncConsistentIndex (tx )
1317
+ } else {
1318
+ as .lg .Error ("failed to save consistentIndex,syncConsistentIndex is nil" )
1319
+ }
1320
+ }
0 commit comments