Skip to content

Commit 424966b

Browse files
authored
Don't overwrite shell when updating user entry (#889)
Closes #880 UDENG-6659
2 parents 2acdaf6 + 5226dc9 commit 424966b

File tree

4 files changed

+33
-2
lines changed

4 files changed

+33
-2
lines changed

internal/users/db/db_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,13 @@ func TestUpdateUserEntry(t *testing.T) {
144144
Dir: "/new/home/user1",
145145
Shell: "/bin/bash",
146146
},
147+
"user1-new-shell": {
148+
Name: "user1",
149+
UID: 1111,
150+
Gecos: "User1 gecos\nOn multiple lines",
151+
Dir: "/new/home/user1",
152+
Shell: "/new/shell",
153+
},
147154
"user1-without-gecos": {
148155
Name: "user1",
149156
UID: 1111,
@@ -185,6 +192,7 @@ func TestUpdateUserEntry(t *testing.T) {
185192
// User and Group updates
186193
"Update_user_by_changing_attributes": {userCase: "user1-new-attributes", dbFile: "one_user_and_group"},
187194
"Update_user_does_not_change_homedir_if_it_exists": {userCase: "user1-new-homedir", dbFile: "one_user_and_group"},
195+
"Update_user_does_not_change_shell_if_it_exists": {userCase: "user1-new-shell", dbFile: "one_user_and_group"},
188196
"Update_user_by_removing_optional_gecos_field_if_not_set": {userCase: "user1-without-gecos", dbFile: "one_user_and_group"},
189197

190198
// Group updates

internal/users/db/testdata/golden/TestUpdateUserEntry/Update_user_by_changing_attributes

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ users:
44
gid: 11111
55
gecos: New user1 gecos
66
dir: /home/user1
7-
shell: /bin/dash
7+
shell: /bin/bash
88
groups:
99
- name: group1
1010
gid: 11111
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
users:
2+
- name: user1
3+
uid: 1111
4+
gid: 11111
5+
gecos: |-
6+
User1 gecos
7+
On multiple lines
8+
dir: /home/user1
9+
shell: /bin/bash
10+
groups:
11+
- name: group1
12+
gid: 11111
13+
ugid: "12345678"
14+
users_to_groups:
15+
- uid: 1111
16+
gid: 11111

internal/users/db/update.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ func (m *Manager) UpdateUserEntry(user UserRow, authdGroups []GroupRow, localGro
4747

4848
// handleUserUpdate updates the user record in the database.
4949
func handleUserUpdate(db queryable, u UserRow) error {
50+
log.Debugf(context.Background(), "Updating entry of user %q (UID: %d)", u.Name, u.UID)
51+
5052
existingUser, err := userByID(db, u.UID)
5153
if err != nil && !errors.Is(err, NoDataFoundError{}) {
5254
return err
@@ -64,7 +66,12 @@ func handleUserUpdate(db queryable, u UserRow) error {
6466
u.Dir = existingUser.Dir
6567
}
6668

67-
log.Debugf(context.Background(), "Updating entry of user %q (UID: %d)", u.Name, u.UID)
69+
// Ensure that we use the same shell as the one we have in the database.
70+
if existingUser.Shell != "" && existingUser.Shell != u.Shell {
71+
log.Debugf(context.TODO(), "Not updating shell to %q because it's already set to %q", u.Shell, existingUser.Shell)
72+
u.Shell = existingUser.Shell
73+
}
74+
6875
return insertOrUpdateUserByID(db, u)
6976
}
7077

0 commit comments

Comments
 (0)