Skip to content
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
1 change: 1 addition & 0 deletions lib/srv/usermgmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,7 @@ func (u *HostUserManagement) getHostUser(username string) (*HostUser, error) {
group, err := u.backend.LookupGroupByID(gid)
if err != nil {
groupErrs = append(groupErrs, err)
continue
}

groups[group.Name] = struct{}{}
Expand Down
30 changes: 30 additions & 0 deletions lib/srv/usermgmt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ type testHostUserBackend struct {

setUserGroupsCalls int
createHomeDirectoryCalls int
groupDatabaseErr error
}

func newTestUserMgmt() *testHostUserBackend {
Expand Down Expand Up @@ -95,6 +96,10 @@ func (tm *testHostUserBackend) LookupGroup(groupname string) (*user.Group, error
}

func (tm *testHostUserBackend) LookupGroupByID(gid string) (*user.Group, error) {
if tm.groupDatabaseErr != nil {
return nil, tm.groupDatabaseErr
}

for groupName, groupGid := range tm.groups {
if groupGid == gid {
return &user.Group{
Expand Down Expand Up @@ -1105,3 +1110,28 @@ func TestHostUsersResolveGroups(t *testing.T) {
})
}
}

// errors fetching groups related to a user should not cause panics during UpsertUser
func TestRegressionGroupErrorDoesNotPanic(t *testing.T) {
t.Parallel()

allGroups := []string{"foo", "bar", "baz"}
users, backend := initBackend(t, allGroups)

userinfo := services.HostUsersInfo{
Groups: slices.Clone(allGroups[:2]),
Mode: services.HostUserModeKeep,
}

// Create user
closer, err := users.UpsertUser("alice", userinfo)
assert.NoError(t, err)
assert.Equal(t, nil, closer)
assert.Zero(t, backend.setUserGroupsCalls)
assert.ElementsMatch(t, append(userinfo.Groups, types.TeleportKeepGroup), backend.users["alice"])
assert.NotContains(t, backend.users["alice"], types.TeleportDropGroup)

backend.groupDatabaseErr = errors.New("could not find group")
_, err = users.UpsertUser("alice", userinfo)
require.Error(t, err)
}