Skip to content

Commit

Permalink
Fix update for DynamicWindowsDesktops
Browse files Browse the repository at this point in the history
  • Loading branch information
probakowski committed Oct 28, 2024
1 parent c8b1ad6 commit de348ed
Show file tree
Hide file tree
Showing 9 changed files with 69 additions and 4 deletions.
16 changes: 16 additions & 0 deletions api/client/dynamicwindows/dynamicwindows.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,25 @@ func (c *Client) UpdateDynamicWindowsDesktop(ctx context.Context, desktop types.
}
}

func (c *Client) UpsertDynamicWindowsDesktop(ctx context.Context, desktop types.DynamicWindowsDesktop) (types.DynamicWindowsDesktop, error) {
switch desktop := desktop.(type) {
case *types.DynamicWindowsDesktopV1:
desktop, err := c.grpcClient.UpsertDynamicWindowsDesktop(ctx, &dynamicwindows.UpsertDynamicWindowsDesktopRequest{
Desktop: desktop,
})
return desktop, trace.Wrap(err)
default:
return nil, trace.BadParameter("unknown desktop type: %T", desktop)
}
}

func (c *Client) DeleteDynamicWindowsDesktop(ctx context.Context, name string) error {
_, err := c.grpcClient.DeleteDynamicWindowsDesktop(ctx, &dynamicwindows.DeleteDynamicWindowsDesktopRequest{
Name: name,
})
return trace.Wrap(err)
}

func (c *Client) DeleteAllDynamicWindowsDesktops(ctx context.Context) error {
return trace.NotImplemented("DeleteAllDynamicWindowsDesktops is not supported in the gRPC client")
}
2 changes: 2 additions & 0 deletions lib/auth/accesspoint/accesspoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ type Config struct {
Users services.UsersService
WebSession types.WebSessionInterface
WebToken types.WebTokenInterface
DynamicWindowsDesktops services.DynamicWindowsDesktops
WindowsDesktops services.WindowsDesktops
AutoUpdateService services.AutoUpdateServiceGetter
ProvisioningStates services.ProvisioningStates
Expand Down Expand Up @@ -201,6 +202,7 @@ func NewCache(cfg Config) (*cache.Cache, error) {
WebSession: cfg.WebSession,
WebToken: cfg.WebToken,
WindowsDesktops: cfg.WindowsDesktops,
DynamicWindowsDesktops: cfg.DynamicWindowsDesktops,
ProvisioningStates: cfg.ProvisioningStates,
IdentityCenter: cfg.IdentityCenter,
}
Expand Down
25 changes: 25 additions & 0 deletions lib/auth/dynamicwindows/dynamicwindowsv1/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,31 @@ func (s *Service) UpdateDynamicWindowsDesktop(ctx context.Context, req *dynamicw
return updatedDesktop, nil
}

// UpsertDynamicWindowsDesktop updates an existing dynamic Windows desktop or creates one if it doesn't exist.
func (s *Service) UpsertDynamicWindowsDesktop(ctx context.Context, req *dynamicwindowspb.UpsertDynamicWindowsDesktopRequest) (*types.DynamicWindowsDesktopV1, error) {
auth, err := s.authorizer.Authorize(ctx)
if err != nil {
return nil, trace.Wrap(err)
}
if err := auth.AuthorizeAdminAction(); err != nil {
return nil, trace.Wrap(err)
}
if err := auth.CheckAccessToKind(types.KindDynamicWindowsDesktop, types.VerbCreate, types.VerbUpdate); err != nil {
return nil, trace.Wrap(err)
}
d, err := s.backend.UpsertDynamicWindowsDesktop(ctx, req.Desktop)
if err != nil {
return nil, trace.Wrap(err)
}

updatedDesktop, ok := d.(*types.DynamicWindowsDesktopV1)
if !ok {
return nil, trace.BadParameter("unexpected type %T", d)
}

return updatedDesktop, nil
}

// DeleteDynamicWindowsDesktop removes the specified dynamic Windows desktop.
func (s *Service) DeleteDynamicWindowsDesktop(ctx context.Context, req *dynamicwindowspb.DeleteDynamicWindowsDesktopRequest) (*emptypb.Empty, error) {
auth, err := s.authorizer.Authorize(ctx)
Expand Down
9 changes: 9 additions & 0 deletions lib/auth/dynamicwindows/dynamicwindowsv1/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ func TestServiceAccess(t *testing.T) {
allowedStates: []authz.AdminActionAuthState{authz.AdminActionAuthNotRequired, authz.AdminActionAuthMFAVerified},
allowedVerbs: []string{types.VerbUpdate},
},
{
name: "UpsertDynamicWindowsDesktop",
allowedStates: []authz.AdminActionAuthState{authz.AdminActionAuthNotRequired, authz.AdminActionAuthMFAVerified},
allowedVerbs: []string{types.VerbCreate, types.VerbUpdate},
},
{
name: "DeleteDynamicWindowsDesktop",
allowedStates: []authz.AdminActionAuthState{authz.AdminActionAuthNotRequired, authz.AdminActionAuthMFAVerified},
Expand Down Expand Up @@ -160,6 +165,10 @@ func callMethod(service *Service, method string) error {
arg.Desktop, _ = types.NewDynamicWindowsDesktopV1("test", nil, types.DynamicWindowsDesktopSpecV1{
Addr: "test",
})
case *dynamicwindowsv1.UpsertDynamicWindowsDesktopRequest:
arg.Desktop, _ = types.NewDynamicWindowsDesktopV1("test", nil, types.DynamicWindowsDesktopSpecV1{
Addr: "test",
})
}
return nil
}, nil)
Expand Down
2 changes: 1 addition & 1 deletion lib/cache/collections.go
Original file line number Diff line number Diff line change
Expand Up @@ -2328,7 +2328,7 @@ func (dynamicWindowsDesktopsExecutor) getAll(ctx context.Context, cache *Cache,
var desktops []types.DynamicWindowsDesktop
next := ""
for {
d, token, err := cache.dynamicWindowsDesktopsCache.ListDynamicWindowsDesktops(ctx, defaults.MaxIterationLimit, next)
d, token, err := cache.Config.DynamicWindowsDesktops.ListDynamicWindowsDesktops(ctx, defaults.MaxIterationLimit, next)
if err != nil {
return nil, err
}
Expand Down
2 changes: 2 additions & 0 deletions lib/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -2529,6 +2529,7 @@ func (process *TeleportProcess) newAccessCacheForServices(cfg accesspoint.Config
cfg.WebSession = services.Identity.WebSessions()
cfg.WebToken = services.Identity.WebTokens()
cfg.WindowsDesktops = services.WindowsDesktops
cfg.DynamicWindowsDesktops = services.DynamicWindowsDesktops
cfg.AutoUpdateService = services.AutoUpdateService
cfg.ProvisioningStates = services.ProvisioningStates
cfg.IdentityCenter = services.IdentityCenter
Expand Down Expand Up @@ -2576,6 +2577,7 @@ func (process *TeleportProcess) newAccessCacheForClient(cfg accesspoint.Config,
cfg.WebSession = client.WebSessions()
cfg.WebToken = client.WebTokens()
cfg.WindowsDesktops = client
cfg.DynamicWindowsDesktops = client.DynamicDesktopClient()
cfg.AutoUpdateService = client

return accesspoint.NewCache(cfg)
Expand Down
6 changes: 5 additions & 1 deletion lib/services/local/dynamic_desktops.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,11 @@ func (s *DynamicWindowsDesktopService) CreateDynamicWindowsDesktop(ctx context.C

// UpdateDynamicWindowsDesktop updates a dynamic Windows desktop resource.
func (s *DynamicWindowsDesktopService) UpdateDynamicWindowsDesktop(ctx context.Context, desktop types.DynamicWindowsDesktop) (types.DynamicWindowsDesktop, error) {
d, err := s.service.UpdateResource(ctx, desktop)
// ConditionalUpdateResource can return invalid revision instead of not found, so we'll check if resource exists first
if _, err := s.service.GetResource(ctx, desktop.GetName()); trace.IsNotFound(err) {
return nil, err
}
d, err := s.service.ConditionalUpdateResource(ctx, desktop)
if err != nil {
return nil, trace.Wrap(err)
}
Expand Down
9 changes: 8 additions & 1 deletion lib/services/local/dynamic_desktops_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,15 @@ func TestDynamicWindowsService_UpdateDynamicDesktop(t *testing.T) {
require.Error(t, err)
require.True(t, trace.IsNotFound(err))
})
t.Run("revision doesn't match", func(t *testing.T) {
want := newDynamicDesktop(t, "example1")
_, err := service.CreateDynamicWindowsDesktop(ctx, want.Copy())
require.NoError(t, err)
_, err = service.UpdateDynamicWindowsDesktop(ctx, want)
require.Error(t, err)
})
t.Run("ok", func(t *testing.T) {
want := newDynamicDesktop(t, "example")
want := newDynamicDesktop(t, "example2")
created, err := service.CreateDynamicWindowsDesktop(ctx, want.Copy())
require.NoError(t, err)
updated, err := service.UpdateDynamicWindowsDesktop(ctx, created.Copy())
Expand Down
2 changes: 1 addition & 1 deletion tool/tctl/common/resource_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -904,7 +904,7 @@ func (rc *ResourceCommand) createDynamicWindowsDesktop(ctx context.Context, clie
if !rc.force {
return trace.AlreadyExists("application %q already exists", wd.GetName())
}
if _, err := dynamicDesktopClient.UpdateDynamicWindowsDesktop(ctx, wd); err != nil {
if _, err := dynamicDesktopClient.UpsertDynamicWindowsDesktop(ctx, wd); err != nil {
return trace.Wrap(err)
}
fmt.Printf("dynamic windows desktop %q has been updated\n", wd.GetName())
Expand Down

0 comments on commit de348ed

Please sign in to comment.