Update shell in host users when they exist (#3072)

This commit is contained in:
Tomas Touceda 2021-11-23 10:23:12 -03:00 committed by GitHub
parent 028c2643f7
commit 4cd9dab577
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 2 deletions

View file

@ -0,0 +1 @@
* Update shell as host users are updated

View file

@ -929,7 +929,13 @@ func saveHostUsersDB(ctx context.Context, tx sqlx.ExtContext, host *fleet.Host)
insertValues := strings.TrimSuffix(strings.Repeat("(?, ?, ?, ?, ?, ?),", len(host.Users)), ",")
insertSql := fmt.Sprintf(
`INSERT INTO host_users (host_id, uid, username, user_type, groupname, shell) VALUES %s ON DUPLICATE KEY UPDATE removed_at=NULL`,
`INSERT INTO host_users (host_id, uid, username, user_type, groupname, shell)
VALUES %s
ON DUPLICATE KEY UPDATE
user_type = VALUES(user_type),
groupname = VALUES(groupname),
shell = VALUES(shell),
removed_at=NULL`,
insertValues,
)
if _, err := tx.ExecContext(ctx, insertSql, insertArgs...); err != nil {

View file

@ -1569,7 +1569,8 @@ func testHostsSaveUsers(t *testing.T, ds *Datastore) {
require.Len(t, host.Users, 1)
assert.Equal(t, host.Users[0].Uid, u2.Uid)
// readd u1
// readd u1 but with a different shell
u1.Shell = "/some/new/shell"
host.Users = []fleet.HostUser{u1, u2}
host.Modified = true