From 4cd9dab57744f1868a98a9a9b1e5dff778b0e022 Mon Sep 17 00:00:00 2001 From: Tomas Touceda Date: Tue, 23 Nov 2021 10:23:12 -0300 Subject: [PATCH] Update shell in host users when they exist (#3072) --- changes/update-host-user-shell | 1 + server/datastore/mysql/hosts.go | 8 +++++++- server/datastore/mysql/hosts_test.go | 3 ++- 3 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 changes/update-host-user-shell diff --git a/changes/update-host-user-shell b/changes/update-host-user-shell new file mode 100644 index 0000000000..ed3a5e16a1 --- /dev/null +++ b/changes/update-host-user-shell @@ -0,0 +1 @@ +* Update shell as host users are updated diff --git a/server/datastore/mysql/hosts.go b/server/datastore/mysql/hosts.go index 7fae018b37..06c141ffa4 100644 --- a/server/datastore/mysql/hosts.go +++ b/server/datastore/mysql/hosts.go @@ -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 { diff --git a/server/datastore/mysql/hosts_test.go b/server/datastore/mysql/hosts_test.go index f3e7676fcf..321fb117bf 100644 --- a/server/datastore/mysql/hosts_test.go +++ b/server/datastore/mysql/hosts_test.go @@ -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