Skip to content

Commit

Permalink
exclude except in dedicated ds method
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob Shandling committed Jan 8, 2025
1 parent 394c89b commit 9e13c22
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
3 changes: 2 additions & 1 deletion server/datastore/mysql/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ func (ds *Datastore) NewUser(ctx context.Context, user *fleet.User) (*fleet.User

func (ds *Datastore) findUser(ctx context.Context, searchCol string, searchVal interface{}) (*fleet.User, error) {
sqlStatement := fmt.Sprintf(
"SELECT * FROM users "+
// everything except `settings`
"SELECT id, created_at, updated_at, password, salt, name, email, admin_forced_password_reset, gravatar_url, position, sso_enabled, global_role, api_only, mfa_enabled FROM users "+
"WHERE %s = ? LIMIT 1",
searchCol,
)
Expand Down
9 changes: 5 additions & 4 deletions server/datastore/mysql/users_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,15 +169,16 @@ func testSettingsAttribute(t *testing.T, ds fleet.Datastore, users []*fleet.User

verify, err := ds.UserByID(context.Background(), user.ID)
assert.Nil(t, err)
assert.Empty(t, verify.Settings.HiddenHostColumns)
// settings should only be returned via dedicated method
assert.Nil(t, verify.Settings)

user.Settings.HiddenHostColumns = []string{"osquery_version"}
err = ds.SaveUser(context.Background(), user)
assert.Nil(t, err)

verify, err = ds.UserByID(context.Background(), user.ID)
assert.Nil(t, err)
assert.Equal(t, verify.Settings.HiddenHostColumns, user.Settings.HiddenHostColumns)
// call the settings db method here
settings, err := ds.UserSettings(context.Background(), user.ID)

Check failure on line 180 in server/datastore/mysql/users_test.go

View workflow job for this annotation

GitHub Actions / lint (ubuntu-latest)

ineffectual assignment to err (ineffassign)

Check failure on line 180 in server/datastore/mysql/users_test.go

View workflow job for this annotation

GitHub Actions / lint (macos-latest)

ineffectual assignment to err (ineffassign)
assert.Equal(t, settings.HiddenHostColumns, user.Settings.HiddenHostColumns)
}
}

Expand Down
16 changes: 11 additions & 5 deletions server/service/integration_core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4671,7 +4671,9 @@ func (s *integrationTestSuite) TestUsers() {
// session user id 1
assert.Equal(t, uint(1), getMeResp.User.ID)
assert.NotNil(t, getMeResp.User.GlobalRole)
assert.Empty(t, getMeResp.User.Settings)
// settings should only be present in dedicated settings field, not in user object
assert.Nil(t, getMeResp.User.Settings)
assert.Empty(t, getMeResp.Settings)

// modify session user - add ui setting
var modResp modifyUserResponse
Expand All @@ -4683,7 +4685,8 @@ func (s *integrationTestSuite) TestUsers() {
// get session user with ui settings, should now be present, two endpoints
s.DoJSON("GET", fmt.Sprintf("/api/latest/fleet/users/%d", 1), nil, http.StatusOK, &getResp, "include_ui_settings", "true")
assert.Equal(t, uint(1), getResp.User.ID)
assert.Equal(t, getResp.User.Settings, &fleet.UserSettings{HiddenHostColumns: []string{"osquery_version"}})
assert.Nil(t, getMeResp.User.Settings)
assert.Equal(t, getResp.Settings, &fleet.UserSettings{HiddenHostColumns: []string{"osquery_version"}})

resp = s.DoRawWithHeaders("GET", "/api/latest/fleet/me", []byte(""), http.StatusOK, map[string]string{
"Authorization": fmt.Sprintf("Bearer %s", ssn.Key),
Expand All @@ -4692,7 +4695,8 @@ func (s *integrationTestSuite) TestUsers() {
require.NoError(t, err)
assert.Equal(t, uint(1), getMeResp.User.ID)
assert.NotNil(t, getMeResp.User.GlobalRole)
assert.Equal(t, getResp.User.Settings, &fleet.UserSettings{HiddenHostColumns: []string{"osquery_version"}})
assert.Nil(t, getMeResp.User.Settings)
assert.Equal(t, getResp.Settings, &fleet.UserSettings{HiddenHostColumns: []string{"osquery_version"}})

// modify user ui settings, check they are returned modified
s.DoJSON("PATCH", fmt.Sprintf("/api/latest/fleet/users/%d", 1), json.RawMessage(`{
Expand All @@ -4703,7 +4707,8 @@ func (s *integrationTestSuite) TestUsers() {
// get session user with ui settings, should now be present, two endpoints
s.DoJSON("GET", fmt.Sprintf("/api/latest/fleet/users/%d", 1), nil, http.StatusOK, &getResp, "include_ui_settings", "true")
assert.Equal(t, uint(1), getResp.User.ID)
assert.Equal(t, getResp.User.Settings, &fleet.UserSettings{HiddenHostColumns: []string{"hostname", "osquery_version"}})
assert.Nil(t, getResp.User.Settings)
assert.Equal(t, getResp.Settings, &fleet.UserSettings{HiddenHostColumns: []string{"hostname", "osquery_version"}})

resp = s.DoRawWithHeaders("GET", "/api/latest/fleet/me", []byte(""), http.StatusOK, map[string]string{
"Authorization": fmt.Sprintf("Bearer %s", ssn.Key),
Expand All @@ -4712,7 +4717,8 @@ func (s *integrationTestSuite) TestUsers() {
require.NoError(t, err)
assert.Equal(t, uint(1), getMeResp.User.ID)
assert.NotNil(t, getMeResp.User.GlobalRole)
assert.Equal(t, getResp.User.Settings, &fleet.UserSettings{HiddenHostColumns: []string{"hostname", "osquery_version"}})
assert.Nil(t, getResp.User.Settings)
assert.Equal(t, getResp.Settings, &fleet.UserSettings{HiddenHostColumns: []string{"hostname", "osquery_version"}})

// create a new user
var createResp createUserResponse
Expand Down

0 comments on commit 9e13c22

Please sign in to comment.