From 0cbad19d60c0b6789e8c40424009c327415df0c5 Mon Sep 17 00:00:00 2001 From: rosstimothy <39066650+rosstimothy@users.noreply.github.com> Date: Thu, 6 Oct 2022 08:42:16 -0400 Subject: [PATCH] Fix ListResources for WindowsDesktops (#17093) ListResources was adding the namespace to the backend key when retrieving WindowsDesktopServices, however UpsertWindowsDesktopService doesn't include the namespace in the prefix. This results in never getting any items from the backend. Removing the namespace in the prefix to match GetWindowsDesktopService revealed that we were also trying to unmarshal the resource as a types.Server. Switching the unmarshal function to use `services.UnmarshalWindowsDesktopService` as GetWindowsDesktopService allows ListResources to provide the correct resources. A new test case for WindowsDesktops was added to `local.TestListResources` to prevent any regressions. --- lib/services/local/presence.go | 20 +++++++++++++++----- lib/services/local/presence_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/lib/services/local/presence.go b/lib/services/local/presence.go index 988842703521b..d5a7fa5b69234 100644 --- a/lib/services/local/presence.go +++ b/lib/services/local/presence.go @@ -1500,8 +1500,8 @@ func (s *PresenceService) listResources(ctx context.Context, req proto.ListResou keyPrefix = []string{nodesPrefix, req.Namespace} unmarshalItemFunc = backendItemToServer(types.KindNode) case types.KindWindowsDesktopService: - keyPrefix = []string{windowsDesktopServicesPrefix, req.Namespace} - unmarshalItemFunc = backendItemToServer(types.KindWindowsDesktopService) + keyPrefix = []string{windowsDesktopServicesPrefix} + unmarshalItemFunc = backendItemToWindowsDesktopService case types.KindKubeService: keyPrefix = []string{kubeServicesPrefix} unmarshalItemFunc = backendItemToServer(types.KindKubeService) @@ -1694,7 +1694,7 @@ func FakePaginate(resources []types.ResourceWithLabels, req proto.ListResourcesR } // backendItemToDatabaseServer unmarshals `backend.Item` into a -// `types.DatabaseServer`, returning it as a `types.Resource`. +// `types.DatabaseServer`, returning it as a `types.ResourceWithLabels`. func backendItemToDatabaseServer(item backend.Item) (types.ResourceWithLabels, error) { return services.UnmarshalDatabaseServer( item.Value, @@ -1704,7 +1704,7 @@ func backendItemToDatabaseServer(item backend.Item) (types.ResourceWithLabels, e } // backendItemToApplicationServer unmarshals `backend.Item` into a -// `types.AppServer`, returning it as a `types.Resource`. +// `types.AppServer`, returning it as a `types.ResourceWithLabels`. func backendItemToApplicationServer(item backend.Item) (types.ResourceWithLabels, error) { return services.UnmarshalAppServer( item.Value, @@ -1715,7 +1715,7 @@ func backendItemToApplicationServer(item backend.Item) (types.ResourceWithLabels // backendItemToServer returns `backendItemToResourceFunc` to unmarshal a // `backend.Item` into a `types.ServerV2` with a specific `kind`, returning it -// as a `types.Resource`. +// as a `types.ResourceWithLabels`. func backendItemToServer(kind string) backendItemToResourceFunc { return func(item backend.Item) (types.ResourceWithLabels, error) { return services.UnmarshalServer( @@ -1726,6 +1726,16 @@ func backendItemToServer(kind string) backendItemToResourceFunc { } } +// backendItemToWindowsDesktopService unmarshals `backend.Item` into a +// `types.WindowsDesktopService`, returning it as a `types.ResourceWithLabels`. +func backendItemToWindowsDesktopService(item backend.Item) (types.ResourceWithLabels, error) { + return services.UnmarshalWindowsDesktopService( + item.Value, + services.WithResourceID(item.ID), + services.WithExpires(item.Expires), + ) +} + const ( reverseTunnelsPrefix = "reverseTunnels" tunnelConnectionsPrefix = "tunnelConnections" diff --git a/lib/services/local/presence_test.go b/lib/services/local/presence_test.go index 6e919b03bc360..197853d474ee8 100644 --- a/lib/services/local/presence_test.go +++ b/lib/services/local/presence_test.go @@ -28,6 +28,7 @@ import ( "github.com/jonboulle/clockwork" "github.com/stretchr/testify/require" + "github.com/gravitational/teleport" "github.com/gravitational/teleport/api/client/proto" apidefaults "github.com/gravitational/teleport/api/defaults" "github.com/gravitational/teleport/api/types" @@ -553,6 +554,29 @@ func TestListResources(t *testing.T) { return presence.DeleteAllNodes(ctx, apidefaults.Namespace) }, }, + "WindowsDesktopService": { + resourceType: types.KindWindowsDesktopService, + createResourceFunc: func(ctx context.Context, presence *PresenceService, name string, labels map[string]string) error { + desktop, err := types.NewWindowsDesktopServiceV3( + types.Metadata{ + Name: name, + Labels: labels, + }, + types.WindowsDesktopServiceSpecV3{ + Addr: "localhost:1234", + TeleportVersion: teleport.Version, + }) + if err != nil { + return err + } + + _, err = presence.UpsertWindowsDesktopService(ctx, desktop) + return err + }, + deleteAllResourcesFunc: func(ctx context.Context, presence *PresenceService) error { + return presence.DeleteAllWindowsDesktopServices(ctx) + }, + }, } for testName, test := range tests {