Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/device-mapping-order
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Made the order of a host's `device_mapping` emails deterministic (sorted by email) in host list and label host list responses.
2 changes: 1 addition & 1 deletion server/datastore/mysql/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -1167,7 +1167,7 @@ func (ds *Datastore) ListHosts(ctx context.Context, filter fleet.TeamFilter, opt
// idx_host_emails_host_id_email.
sql += fmt.Sprintf(`,
COALESCE((
SELECT CONCAT('[', GROUP_CONCAT(JSON_OBJECT('email', he.email, 'source', %s)), ']')
SELECT CONCAT('[', GROUP_CONCAT(JSON_OBJECT('email', he.email, 'source', %s) ORDER BY he.email, he.source), ']')
FROM host_emails he
WHERE he.host_id = h.id
), 'null') as device_mapping
Expand Down
2 changes: 1 addition & 1 deletion server/datastore/mysql/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -1234,7 +1234,7 @@ func (ds *Datastore) ListHostsInLabel(ctx context.Context, filter fleet.TeamFilt
deviceMappingJoin := fmt.Sprintf(`LEFT JOIN (
SELECT
host_id,
CONCAT('[', GROUP_CONCAT(JSON_OBJECT('email', email, 'source', %s)), ']') AS device_mapping
CONCAT('[', GROUP_CONCAT(JSON_OBJECT('email', email, 'source', %s) ORDER BY email, source), ']') AS device_mapping
FROM
host_emails
GROUP BY
Expand Down
10 changes: 6 additions & 4 deletions server/service/integration_core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6149,12 +6149,13 @@ func (s *integrationTestSuite) TestListHostsByLabel() {
),
)

// Add device mapping
// Add device mapping, inserted out of order to verify the response is
// sorted by email
require.NoError(
t, s.ds.ReplaceHostDeviceMapping(
context.Background(), host.ID, []*fleet.HostDeviceMapping{
{HostID: hosts[0].ID, Email: "a@b.c", Source: fleet.DeviceMappingGoogleChromeProfiles},
{HostID: hosts[0].ID, Email: "b@b.c", Source: fleet.DeviceMappingGoogleChromeProfiles},
{HostID: hosts[0].ID, Email: "a@b.c", Source: fleet.DeviceMappingGoogleChromeProfiles},
}, fleet.DeviceMappingGoogleChromeProfiles,
),
)
Expand Down Expand Up @@ -10542,10 +10543,11 @@ func (s *integrationTestSuite) TestHostsReportDownload() {
err = s.ds.RecordPolicyQueryExecutions(ctx, hosts[1], map[uint]*bool{pol.ID: new(false)}, time.Now(), false, nil)
require.NoError(t, err)

// create some device mappings for host[2]
// create some device mappings for host[2], inserted out of order to verify
// the response is sorted by email
err = s.ds.ReplaceHostDeviceMapping(ctx, hosts[2].ID, []*fleet.HostDeviceMapping{
{HostID: hosts[2].ID, Email: "a@b.c", Source: "google_chrome_profiles"},
{HostID: hosts[2].ID, Email: "b@b.c", Source: "google_chrome_profiles"},
{HostID: hosts[2].ID, Email: "a@b.c", Source: "google_chrome_profiles"},
}, "google_chrome_profiles")
require.NoError(t, err)

Expand Down
Loading