Skip to content

Commit ac6b04d

Browse files
committed
add a user hash function to serve as ID
1 parent 2b7f3c8 commit ac6b04d

File tree

4 files changed

+23
-6
lines changed

4 files changed

+23
-6
lines changed

Diff for: core/clustersmngr/factory.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -460,8 +460,8 @@ func (cf *clustersManager) UpdateUserNamespaces(ctx context.Context, user *auth.
460460
wg.Wait()
461461
}
462462

463-
func (cf *clustersManager) UserLock(userID string) *sync.Mutex {
464-
actual, _ := cf.usersLock.LoadOrStore(userID, &sync.Mutex{})
463+
func (cf *clustersManager) UserLock(user *auth.UserPrincipal) *sync.Mutex {
464+
actual, _ := cf.usersLock.LoadOrStore(user.Hash(), &sync.Mutex{})
465465
lock := actual.(*sync.Mutex)
466466
lock.Lock()
467467
return lock
@@ -472,7 +472,7 @@ func (cf *clustersManager) GetUserNamespaces(user *auth.UserPrincipal) map[strin
472472
}
473473

474474
func (cf *clustersManager) userNsList(ctx context.Context, user *auth.UserPrincipal) map[string][]v1.Namespace {
475-
userLock := cf.UserLock(user.ID)
475+
userLock := cf.UserLock(user)
476476
defer userLock.Unlock()
477477

478478
userNamespaces := cf.GetUserNamespaces(user)

Diff for: core/clustersmngr/factory_caches.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,5 +151,5 @@ func (un *UsersNamespaces) Clear() {
151151
}
152152

153153
func (un UsersNamespaces) cacheKey(user *auth.UserPrincipal, cluster string) uint64 {
154-
return ttlcache.StringKey(fmt.Sprintf("%s:%s", user.ID, cluster))
154+
return ttlcache.StringKey(fmt.Sprintf("%s:%s", user.Hash(), cluster))
155155
}

Diff for: pkg/server/auth/auth.go

+5
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,11 @@ func (p *UserPrincipal) String() string {
9898
return fmt.Sprintf("id=%q groups=%v", p.ID, p.Groups)
9999
}
100100

101+
// Hash returns a unique string using user id,token and groups.
102+
func (p *UserPrincipal) Hash() string {
103+
return fmt.Sprintf("%s/%s/%v", p.ID, p.Token(), p.Groups)
104+
}
105+
101106
func (p *UserPrincipal) Valid() bool {
102107
if p.ID == "" && p.Token() == "" {
103108
return false

Diff for: pkg/server/auth/auth_test.go

+14-2
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,19 @@ func TestUserPrincipal_String(t *testing.T) {
331331
// principal is logged out.
332332
p := auth.NewUserPrincipal(auth.ID("testing"), auth.Groups([]string{"group1", "group2"}), auth.Token("test-token"))
333333

334-
if s := p.String(); s != `id="testing" groups=[group1 group2]` {
335-
t.Fatalf("principal.String() got %s, want %s", s, `id="testing" groups=[group1 group2]`)
334+
want := `id="testing" groups=[group1 group2]`
335+
if s := p.String(); s != want {
336+
t.Fatalf("principal.String() got %s, want %s", s, want)
337+
}
338+
}
339+
340+
func TestUserPrincipal_Hash(t *testing.T) {
341+
// This is primarily to guard against leaking the auth token if the
342+
// principal is logged out.
343+
p := auth.NewUserPrincipal(auth.ID("testing"), auth.Groups([]string{"group1", "group2"}), auth.Token("test-token"))
344+
345+
want := "testing/test-token/[group1 group2]"
346+
if s := p.Hash(); s != want {
347+
t.Fatalf("principal.String() got %s, want %s", s, want)
336348
}
337349
}

0 commit comments

Comments
 (0)