From 38942a2a51c638983501bbc5b362e9f1831537fe Mon Sep 17 00:00:00 2001 From: Gyu-Ho Lee Date: Mon, 6 Nov 2017 09:26:20 -0800 Subject: [PATCH] auth: clean up mutex lock/unlocks Only hold locks when needed. Signed-off-by: Gyu-Ho Lee --- auth/store.go | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/auth/store.go b/auth/store.go index 8fee1a619..363456cb2 100644 --- a/auth/store.go +++ b/auth/store.go @@ -467,14 +467,14 @@ func (as *authStore) UserGrantRole(r *pb.AuthUserGrantRoleRequest) (*pb.AuthUser func (as *authStore) UserGet(r *pb.AuthUserGetRequest) (*pb.AuthUserGetResponse, error) { tx := as.be.BatchTx() tx.Lock() - defer tx.Unlock() - - var resp pb.AuthUserGetResponse - user := getUser(tx, r.Name) + tx.Unlock() + if user == nil { return nil, ErrUserNotFound } + + var resp pb.AuthUserGetResponse resp.Roles = append(resp.Roles, user.Roles...) return &resp, nil } @@ -482,9 +482,8 @@ func (as *authStore) UserGet(r *pb.AuthUserGetRequest) (*pb.AuthUserGetResponse, func (as *authStore) UserList(r *pb.AuthUserListRequest) (*pb.AuthUserListResponse, error) { tx := as.be.BatchTx() tx.Lock() - defer tx.Unlock() - users := getAllUsers(tx) + tx.Unlock() resp := &pb.AuthUserListResponse{Users: make([]string, len(users))} for i := range users { @@ -551,9 +550,8 @@ func (as *authStore) RoleGet(r *pb.AuthRoleGetRequest) (*pb.AuthRoleGetResponse, func (as *authStore) RoleList(r *pb.AuthRoleListRequest) (*pb.AuthRoleListResponse, error) { tx := as.be.BatchTx() tx.Lock() - defer tx.Unlock() - roles := getAllRoles(tx) + tx.Unlock() resp := &pb.AuthRoleListResponse{Roles: make([]string, len(roles))} for i := range roles { @@ -785,9 +783,9 @@ func (as *authStore) IsAdminPermitted(authInfo *AuthInfo) error { tx := as.be.BatchTx() tx.Lock() - defer tx.Unlock() - u := getUser(tx, authInfo.Username) + tx.Unlock() + if u == nil { return ErrUserNotFound } @@ -1092,9 +1090,9 @@ func (as *authStore) WithRoot(ctx context.Context) context.Context { func (as *authStore) HasRole(user, role string) bool { tx := as.be.BatchTx() tx.Lock() - defer tx.Unlock() - u := getUser(tx, user) + tx.Unlock() + if u == nil { plog.Warningf("tried to check user %s has role %s, but user %s doesn't exist", user, role, user) return false