From d7ca8fcd66fae984193fee008d88f1481a7532a2 Mon Sep 17 00:00:00 2001 From: Juan Fernandez Date: Fri, 23 Sep 2022 15:18:19 -0400 Subject: [PATCH] Reverted changes made to Fleet Desktop - should not use the lightweight 'desktop' endpoint (#7919) Reverted changes made to Fleet Desktop. Desktop should not use the lightweight 'desktop' endpoint --- changes/6946-fleet-desktop-uses-min-endpoint | 2 -- orbit/cmd/desktop/desktop.go | 19 ++++++++++++------ server/service/device_client.go | 15 +++++++------- server/service/device_client_test.go | 21 ++++++++++++++------ 4 files changed, 36 insertions(+), 21 deletions(-) delete mode 100644 changes/6946-fleet-desktop-uses-min-endpoint diff --git a/changes/6946-fleet-desktop-uses-min-endpoint b/changes/6946-fleet-desktop-uses-min-endpoint deleted file mode 100644 index 59ff4f2285..0000000000 --- a/changes/6946-fleet-desktop-uses-min-endpoint +++ /dev/null @@ -1,2 +0,0 @@ -Updated Fleet Desktop to use the new endpoint introduced in -https://github.com/fleetdm/fleet/issues/7084 diff --git a/orbit/cmd/desktop/desktop.go b/orbit/cmd/desktop/desktop.go index 12708e06cd..8890b7ce0f 100644 --- a/orbit/cmd/desktop/desktop.go +++ b/orbit/cmd/desktop/desktop.go @@ -98,7 +98,7 @@ func main() { defer close(done) for { - _, err := client.GetDesktopPayload() + _, err := client.ListDevicePolicies() if err == nil || errors.Is(err, service.ErrMissingLicense) { myDeviceItem.SetTitle("My device") @@ -124,7 +124,7 @@ func main() { for { <-tic.C - res, err := client.GetDesktopPayload() + policies, err := client.ListDevicePolicies() switch { case err == nil: // OK @@ -136,17 +136,24 @@ func main() { continue } - if res.FailingPolicies != nil && *res.FailingPolicies > 0 { + failedPolicyCount := 0 + for _, policy := range policies { + if policy.Response != "pass" { + failedPolicyCount++ + } + } + + if failedPolicyCount > 0 { if runtime.GOOS == "windows" { // Windows (or maybe just the systray library?) doesn't support color emoji // in the system tray menu, so we use text as an alternative. - if *res.FailingPolicies == 1 { + if failedPolicyCount == 1 { myDeviceItem.SetTitle("My device (1 issue)") } else { - myDeviceItem.SetTitle(fmt.Sprintf("My device (%d issues)", *res.FailingPolicies)) + myDeviceItem.SetTitle(fmt.Sprintf("My device (%d issues)", failedPolicyCount)) } } else { - myDeviceItem.SetTitle(fmt.Sprintf("🔴 My device (%d)", res.FailingPolicies)) + myDeviceItem.SetTitle(fmt.Sprintf("🔴 My device (%d)", failedPolicyCount)) } } else { if runtime.GOOS == "windows" { diff --git a/server/service/device_client.go b/server/service/device_client.go index 5865b3f47a..4e67a7d8e5 100644 --- a/server/service/device_client.go +++ b/server/service/device_client.go @@ -4,6 +4,8 @@ import ( "bytes" "fmt" "net/http" + + "github.com/fleetdm/fleet/v4/server/fleet" ) // Device client is used consume the `device/...` endpoints and meant to be used by Fleet Desktop @@ -46,14 +48,13 @@ func NewDeviceClient(addr, token string, insecureSkipVerify bool, rootCA string) }, nil } -// Get fetches payload used by Fleet Desktop. -func (dc *DeviceClient) GetDesktopPayload() (*FleetDesktopResponse, error) { - verb, path := "GET", "/api/latest/fleet/device/"+dc.token+"/desktop" - - var r FleetDesktopResponse - err := dc.request(verb, path, "", &r) +// ListDevicePolicies fetches all policies for the device with the provided token +func (dc *DeviceClient) ListDevicePolicies() ([]*fleet.HostPolicy, error) { + verb, path := "GET", "/api/latest/fleet/device/"+dc.token+"/policies" + var responseBody listDevicePoliciesResponse + err := dc.request(verb, path, "", &responseBody) if err != nil { return nil, err } - return &r, nil + return responseBody.Policies, nil } diff --git a/server/service/device_client_test.go b/server/service/device_client_test.go index c57c4d817b..202edc89b2 100644 --- a/server/service/device_client_test.go +++ b/server/service/device_client_test.go @@ -28,7 +28,7 @@ func (m *mockHttpClient) Do(req *http.Request) (*http.Response, error) { return res, nil } -func TestDeviceClientGetDesktopPayload(t *testing.T) { +func TestDeviceClientListPolicies(t *testing.T) { client, err := NewDeviceClient("https://test.com", "test-token", true, "") require.NoError(t, err) @@ -37,15 +37,24 @@ func TestDeviceClientGetDesktopPayload(t *testing.T) { t.Run("with wrong license", func(t *testing.T) { mockRequestDoer.statusCode = http.StatusPaymentRequired - _, err = client.GetDesktopPayload() + _, err = client.ListDevicePolicies() require.ErrorIs(t, err, ErrMissingLicense) }) - t.Run("with failing policies", func(t *testing.T) { + t.Run("with empty policies", func(t *testing.T) { mockRequestDoer.statusCode = http.StatusOK - mockRequestDoer.resBody = `{"failing_policies_count": 1}` - res, err := client.GetDesktopPayload() + mockRequestDoer.resBody = `{"policies": []}` + policies, err := client.ListDevicePolicies() require.NoError(t, err) - require.Equal(t, uint(1), *res.FailingPolicies) + require.Len(t, policies, 0) + }) + + t.Run("with policies", func(t *testing.T) { + mockRequestDoer.statusCode = http.StatusOK + mockRequestDoer.resBody = `{"policies": [{"id": 1}]}` + policies, err := client.ListDevicePolicies() + require.NoError(t, err) + require.Len(t, policies, 1) + require.Equal(t, uint(1), policies[0].ID) }) }