From b348399f7ec332429749511fc0d54f8b898fbf1d Mon Sep 17 00:00:00 2001 From: Jahziel Villasana-Espinoza Date: Thu, 21 Dec 2023 15:42:44 -0500 Subject: [PATCH] fix: email searching when filtering by labels (#15774) # Checklist for submitter If some of the following don't apply, delete the relevant line. - [x] Changes file added for user-visible changes in `changes/` or `orbit/changes/`. See [Changes files](https://fleetdm.com/docs/contributing/committing-changes#changes-files) for more information. - [x] Input data is properly validated, `SELECT *` is avoided, SQL injection is prevented (using placeholders for values in statements) - [x] Added/updated tests - [x] Manual QA for all new/changed functionality --- changes/15525-email-search | 1 + server/datastore/mysql/labels.go | 2 +- server/service/integration_core_test.go | 14 ++++++++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 changes/15525-email-search diff --git a/changes/15525-email-search b/changes/15525-email-search new file mode 100644 index 0000000000..7eb1dec5f5 --- /dev/null +++ b/changes/15525-email-search @@ -0,0 +1 @@ +- Fixes bug where searching hosts by email when filtering by a label returned no results. \ No newline at end of file diff --git a/server/datastore/mysql/labels.go b/server/datastore/mysql/labels.go index d2cf21737d..ae8a798762 100644 --- a/server/datastore/mysql/labels.go +++ b/server/datastore/mysql/labels.go @@ -596,7 +596,7 @@ func (ds *Datastore) applyHostLabelFilters(ctx context.Context, filter fleet.Tea query, params = ds.filterHostsByOSSettingsDiskEncryptionStatus(query, opt, params, enableDiskEncryption) } // TODO: should search columns include display_name (requires join to host_display_names)? - query, params = searchLike(query, params, opt.MatchQuery, hostSearchColumns...) + query, params, _ = hostSearchLike(query, params, opt.MatchQuery, hostSearchColumns...) query, params = appendListOptionsWithCursorToSQL(query, params, &opt.ListOptions) return query, params, nil diff --git a/server/service/integration_core_test.go b/server/service/integration_core_test.go index 4b2017d5e0..0e808172c1 100644 --- a/server/service/integration_core_test.go +++ b/server/service/integration_core_test.go @@ -3604,6 +3604,20 @@ func (s *integrationTestSuite) TestLabels() { assert.Equal(t, hosts[len(hosts)-1].ID, listHostsResp.Hosts[0].ID) assert.Equal(t, hosts[0].ID, listHostsResp.Hosts[len(hosts)-1].ID) + mysql.ExecAdhocSQL(t, s.ds, func(db sqlx.ExtContext) error { + _, err := db.ExecContext( + context.Background(), + `INSERT INTO host_emails (host_id, email, source) VALUES (?, ?, ?)`, + hosts[0].ID, "a@b.c", "src1") + + return err + }) + + // list hosts in label searching by email address + s.DoJSON("GET", fmt.Sprintf("/api/latest/fleet/labels/%d/hosts", lbl2.ID), nil, http.StatusOK, &listHostsResp, "query", "a@b.c") + assert.Len(t, listHostsResp.Hosts, 1) + assert.Equal(t, hosts[0].ID, listHostsResp.Hosts[0].ID) + // count hosts in label order by display_name var countResp countHostsResponse s.DoJSON("GET", "/api/latest/fleet/hosts/count", nil, http.StatusOK, &countResp, "label_id", fmt.Sprint(lbl2.ID), "order_key", "display_name", "order_direction", "desc")