Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose endpoint for fetching single desktop #9041

Merged
merged 5 commits into from
Nov 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -739,3 +739,6 @@ const UserSingleUseCertTTL = time.Minute
// StandardHTTPSPort is the default port used for the https URI scheme,
// cf. RFC 7230 § 2.7.2.
const StandardHTTPSPort = 443

// StandardRDPPort is the default port used for RDP.
const StandardRDPPort = 3389
3 changes: 2 additions & 1 deletion lib/web/apiserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,8 @@ func NewHandler(cfg Config, opts ...HandlerOption) (*RewritingHandler, error) {

// Desktop access endpoints.
h.GET("/webapi/sites/:site/desktops", h.WithClusterAuth(h.getDesktopsHandle))
h.GET("/webapi/sites/:site/desktop/:desktopUUID/connect", h.WithClusterAuth(h.handleDesktopAccessWebsocket))
h.GET("/webapi/sites/:site/desktops/:desktopName", h.WithClusterAuth(h.getDesktopHandle))
h.GET("/webapi/sites/:site/desktops/:desktopName/connect", h.WithClusterAuth(h.handleDesktopAccessWebsocket))

// if Web UI is enabled, check the assets dir:
var indexPage *template.Template
Expand Down
10 changes: 5 additions & 5 deletions lib/web/desktop.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ func (h *Handler) handleDesktopAccessWebsocket(
ctx *SessionContext,
site reversetunnel.RemoteSite,
) (interface{}, error) {
desktopUUID := p.ByName("desktopUUID")
if desktopUUID == "" {
return nil, trace.BadParameter("missing desktopUUID in request URL")
desktopName := p.ByName("desktopName")
if desktopName == "" {
return nil, trace.BadParameter("missing desktopName in request URL")
}
log := ctx.log.WithField("desktop-uuid", desktopUUID)
log := ctx.log.WithField("desktop-uuid", desktopName)
log.Debug("New desktop access websocket connection")

winServices, err := ctx.unsafeCachedAuthClient.GetWindowsDesktopServices(r.Context())
Expand Down Expand Up @@ -80,7 +80,7 @@ func (h *Handler) handleDesktopAccessWebsocket(
defer serviceCon.Close()
tlsConfig := ctx.clt.Config()
// Pass target desktop UUID via SNI.
tlsConfig.ServerName = desktopUUID + desktop.SNISuffix
tlsConfig.ServerName = desktopName + desktop.SNISuffix
serviceConTLS := tls.Client(serviceCon, ctx.clt.Config())
log.Debug("Connected to windows_desktop_service")

Expand Down
17 changes: 17 additions & 0 deletions lib/web/servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,20 @@ func (h *Handler) getDesktopsHandle(w http.ResponseWriter, r *http.Request, p ht

return ui.MakeDesktops(windowsDesktops), nil
}

// getDesktopHandle returns a desktop.
func (h *Handler) getDesktopHandle(w http.ResponseWriter, r *http.Request, p httprouter.Params, ctx *SessionContext, site reversetunnel.RemoteSite) (interface{}, error) {
clt, err := ctx.GetUserClient(site)
if err != nil {
return nil, trace.Wrap(err)
}

desktopName := p.ByName("desktopName")

windowsDesktop, err := clt.GetWindowsDesktop(r.Context(), desktopName)
if err != nil {
return nil, trace.Wrap(err)
}

return ui.MakeDesktop(windowsDesktop), nil
}
50 changes: 33 additions & 17 deletions lib/web/ui/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ package ui

import (
"sort"
"strconv"
"strings"

"github.com/gravitational/teleport"
"github.com/gravitational/teleport/api/constants"
"github.com/gravitational/teleport/api/types"
)
Expand Down Expand Up @@ -207,28 +210,41 @@ type Desktop struct {
Labels []Label `json:"labels"`
}

// MakeDesktop converts a desktop from its API form to a type the UI can display.
func MakeDesktop(windowsDesktop types.WindowsDesktop) Desktop {
// stripRdpPort strips the default rdp port from an ip address since it is unimportant to display
stripRdpPort := func(addr string) string {
splitAddr := strings.Split(addr, ":")
if len(splitAddr) > 1 && splitAddr[1] == strconv.Itoa(teleport.StandardRDPPort) {
return splitAddr[0]
}
return addr
}
uiLabels := []Label{}

for name, value := range windowsDesktop.GetAllLabels() {
uiLabels = append(uiLabels, Label{
Name: name,
Value: value,
})
}

sort.Sort(sortedLabels(uiLabels))

return Desktop{
OS: constants.WindowsOS,
Name: windowsDesktop.GetName(),
Addr: stripRdpPort(windowsDesktop.GetAddr()),
Labels: uiLabels,
}
}

// MakeDesktops converts desktops from their API form to a type the UI can display.
func MakeDesktops(windowsDesktops []types.WindowsDesktop) []Desktop {
uiDesktops := make([]Desktop, 0, len(windowsDesktops))

for _, windowsDesktop := range windowsDesktops {
uiLabels := []Label{}

for name, value := range windowsDesktop.GetAllLabels() {
uiLabels = append(uiLabels, Label{
Name: name,
Value: value,
})
}

sort.Sort(sortedLabels(uiLabels))

uiDesktops = append(uiDesktops, Desktop{
OS: constants.WindowsOS,
Name: windowsDesktop.GetName(),
Addr: windowsDesktop.GetAddr(),
Labels: uiLabels,
})
uiDesktops = append(uiDesktops, MakeDesktop(windowsDesktop))
}

return uiDesktops
Expand Down