Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

server/auth: fix auth panic bug when user changes password #15432

Merged
merged 1 commit into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion server/auth/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,8 @@ func (as *authStore) UserChangePassword(r *pb.AuthUserChangePasswordRequest) (*p
var password []byte
var err error

if !user.Options.NoPassword {
mitake marked this conversation as resolved.
Show resolved Hide resolved
// Backward compatible with old versions of etcd, user options is nil
if user.Options == nil || !user.Options.NoPassword {
tangcong marked this conversation as resolved.
Show resolved Hide resolved
password, err = as.selectPassword(r.Password, r.HashedPassword)
if err != nil {
return nil, ErrNoPasswordUser
Expand Down
26 changes: 24 additions & 2 deletions server/auth/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,28 @@ func setupAuthStore(t *testing.T) (store *authStore, teardownfunc func(t *testin
t.Fatal(err)
}

// The UserAdd function cannot generate old etcd version user data (user's option is nil)
// add special users through the underlying interface
addUserWithNoOption(as)
mitake marked this conversation as resolved.
Show resolved Hide resolved

tearDown := func(_ *testing.T) {
as.Close()
}
return as, tearDown
}

func addUserWithNoOption(as *authStore) {
tx := as.be.BatchTx()
tx.Lock()
defer tx.Unlock()
tx.UnsafePutUser(&authpb.User{
Name: []byte("foo-no-user-options"),
Password: []byte("bar"),
})
as.commitRevision(tx)
as.refreshRangePermCache(tx)
}

func enableAuthAndCreateRoot(as *authStore) error {
_, err := as.UserAdd(&pb.AuthUserAddRequest{Name: "root", HashedPassword: encodePassword("root"), Options: &authpb.UserAddOptions{NoPassword: false}})
if err != nil {
Expand Down Expand Up @@ -191,8 +207,8 @@ func TestRecoverWithEmptyRangePermCache(t *testing.T) {
t.Fatalf("expected auth enabled got disabled")
}

if len(as.rangePermCache) != 2 {
t.Fatalf("rangePermCache should have permission information for 2 users (\"root\" and \"foo\"), but has %d information", len(as.rangePermCache))
if len(as.rangePermCache) != 3 {
t.Fatalf("rangePermCache should have permission information for 3 users (\"root\" and \"foo\",\"foo-no-user-options\"), but has %d information", len(as.rangePermCache))
}
if _, ok := as.rangePermCache["root"]; !ok {
t.Fatal("user \"root\" should be created by setupAuthStore() but doesn't exist in rangePermCache")
Expand Down Expand Up @@ -323,6 +339,12 @@ func TestUserChangePassword(t *testing.T) {
if err != ErrUserNotFound {
t.Fatalf("expected %v, got %v", ErrUserNotFound, err)
}

// change a user(user option is nil) password
_, err = as.UserChangePassword(&pb.AuthUserChangePasswordRequest{Name: "foo-no-user-options", HashedPassword: encodePassword("bar")})
if err != nil {
t.Fatal(err)
}
}

func TestRoleAdd(t *testing.T) {
Expand Down