From a6cf9b3d8d6f03b5a998c624843176e06c2a34ba Mon Sep 17 00:00:00 2001 From: Roberto Dip Date: Thu, 9 Jun 2022 10:17:55 -0300 Subject: [PATCH] add endpoint in devices API to query for api features (#6152) Related to #6063, this adds a new device API to get an object with boolean values that we can use as feature flags to manage backwards compatibility in Fleet Desktop. --- server/fleet/app.go | 5 +++++ server/service/devices.go | 23 +++++++++++++++++++++++ server/service/handler.go | 1 + server/service/integration_core_test.go | 8 ++++++++ 4 files changed, 37 insertions(+) diff --git a/server/fleet/app.go b/server/fleet/app.go index 54f6cea8da..44ce16b50f 100644 --- a/server/fleet/app.go +++ b/server/fleet/app.go @@ -435,3 +435,8 @@ type KafkaRESTConfig struct { ResultTopic string `json:"result_topic"` ProxyHost string `json:"proxyhost"` } + +// DeviceAPIFeatures specifies a list of features supported +// by the current API version. Each field in the struct is +// meant to be a boolean value. +type DeviceAPIFeatures struct{} diff --git a/server/service/devices.go b/server/service/devices.go index db36774ceb..fa9110eabd 100644 --- a/server/service/devices.go +++ b/server/service/devices.go @@ -214,3 +214,26 @@ func (svc *Service) ListDevicePolicies(ctx context.Context, host *fleet.Host) ([ return nil, fleet.ErrMissingLicense } + +//////////////////////////////////////////////////////////////////////////////// +// Device API features +//////////////////////////////////////////////////////////////////////////////// + +type deviceAPIFeaturesRequest struct { + Token string `url:"token"` +} + +func (r *deviceAPIFeaturesRequest) deviceAuthToken() string { + return r.Token +} + +type deviceAPIFeaturesResponse struct { + Err error `json:"error,omitempty"` + Features fleet.DeviceAPIFeatures +} + +func (r deviceAPIFeaturesResponse) error() error { return r.Err } + +func deviceAPIFeaturesEndpoint(ctx context.Context, request interface{}, svc fleet.Service) (interface{}, error) { + return deviceAPIFeaturesResponse{Features: fleet.DeviceAPIFeatures{}}, nil +} diff --git a/server/service/handler.go b/server/service/handler.go index 203293e5da..5d3d9b90ff 100644 --- a/server/service/handler.go +++ b/server/service/handler.go @@ -396,6 +396,7 @@ func attachFleetAPIRoutes(r *mux.Router, svc fleet.Service, config config.FleetC de.GET("/api/_version_/fleet/device/{token}/device_mapping", listDeviceHostDeviceMappingEndpoint, listDeviceHostDeviceMappingRequest{}) de.GET("/api/_version_/fleet/device/{token}/macadmins", getDeviceMacadminsDataEndpoint, getDeviceMacadminsDataRequest{}) de.GET("/api/_version_/fleet/device/{token}/policies", listDevicePoliciesEndpoint, listDevicePoliciesRequest{}) + de.GET("/api/_version_/fleet/device/{token}/api_features", deviceAPIFeaturesEndpoint, deviceAPIFeaturesRequest{}) // host-authenticated endpoints he := newHostAuthenticatedEndpointer(svc, logger, opts, r, apiVersions...) diff --git a/server/service/integration_core_test.go b/server/service/integration_core_test.go index a70db25dd4..e83ecfb4ff 100644 --- a/server/service/integration_core_test.go +++ b/server/service/integration_core_test.go @@ -4624,6 +4624,14 @@ func (s *integrationTestSuite) TestDeviceAuthenticatedEndpoints() { json.NewDecoder(res.Body).Decode(&getHostResp) res.Body.Close() require.Nil(t, listPoliciesResp.Policies) + + // get list of api features + apiFeaturesResp := deviceAPIFeaturesResponse{} + res = s.DoRawNoAuth("GET", "/api/latest/fleet/device/"+token+"/api_features", nil, http.StatusOK) + json.NewDecoder(res.Body).Decode(&apiFeaturesResp) + res.Body.Close() + require.Nil(t, apiFeaturesResp.Err) + require.NotNil(t, apiFeaturesResp.Features) } func (s *integrationTestSuite) TestModifyUser() {