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
This commit is contained in:
Juan Fernandez 2022-09-23 15:18:19 -04:00 committed by GitHub
parent 140c5caa9d
commit d7ca8fcd66
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 36 additions and 21 deletions

View file

@ -1,2 +0,0 @@
Updated Fleet Desktop to use the new endpoint introduced in
https://github.com/fleetdm/fleet/issues/7084

View file

@ -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" {

View file

@ -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
}

View file

@ -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)
})
}