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 2 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.
ibeckermayer marked this conversation as resolved.
Show resolved Hide resolved
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/:desktopUUID", h.WithClusterAuth(h.getDesktopHandle))
ibeckermayer marked this conversation as resolved.
Show resolved Hide resolved
h.GET("/webapi/sites/:site/desktops/:desktopUUID/connect", h.WithClusterAuth(h.handleDesktopAccessWebsocket))

// if Web UI is enabled, check the assets dir:
var indexPage *template.Template
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
}

// getDesktopsHandle returns a desktop.
ibeckermayer marked this conversation as resolved.
Show resolved Hide resolved
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)
}

desktopUUID := p.ByName("desktopUUID")

windowsDesktop, err := clt.GetWindowsDesktop(r.Context(), desktopUUID)
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"`
}

// MakeDesktops converts a desktop from its API form to a type the UI can display.
ibeckermayer marked this conversation as resolved.
Show resolved Hide resolved
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