Skip to content
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
8 changes: 6 additions & 2 deletions desktop/internal/core/remote_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,8 +287,9 @@ type RemoteWorkspaceTrustTarget struct {
}

// Place is the route of the target's hops, outermost first: "destination:port" for an SSH hop,
// with the User option in front when the destination names no user, and "engine:container" for
// a container, joined by " → ". It is empty for a target it cannot read.
// with the User option in front when the destination names no user, "engine:container" for a
// container and "wsl:[user@]distribution" for a WSL distribution, as WSLConnection.Identity
// writes it, joined by " → ". It is empty for a target it cannot read.
func (t RemoteWorkspaceTrustTarget) Place() string {
var target struct {
Hops []struct {
Expand All @@ -302,6 +303,7 @@ func (t RemoteWorkspaceTrustTarget) Place() string {
Engine string `json:"engine"`
Container string `json:"container"`
} `json:"container"`
WSL *WSLConnection `json:"wsl"`
} `json:"hops"`
}
if json.Unmarshal(t.Target, &target) != nil {
Expand All @@ -323,6 +325,8 @@ func (t RemoteWorkspaceTrustTarget) Place() string {
hops = append(hops, place)
case hop.Container != nil:
hops = append(hops, hop.Container.Engine+":"+hop.Container.Container)
case hop.WSL != nil:
hops = append(hops, hop.WSL.Identity())
}
}
return strings.Join(hops, " → ")
Expand Down
61 changes: 56 additions & 5 deletions desktop/internal/core/remote_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,15 @@ type RemoteServerBuild struct {
SHA256 string `json:"sha256"`
}

// RemoteConnectRequest opens a session to the host of Connection, installing the server build
// for the host's platform when the host does not already run that exact build.
// RemoteConnectRequest opens a session to the host of Connection, or to the local WSL
// distribution WSL names, installing the server build for the host's platform when the host does
// not already run that exact build. Exactly one of the two is set; Core refuses a request with
// both or neither as CodeInvalidRequest, before anything starts.
type RemoteConnectRequest struct {
Connection SSHConnection `json:"connection"`
Connection SSHConnection `json:"connection,omitzero"`
// WSL opens a distribution of the Windows Subsystem for Linux through the system's wsl.exe
// instead of OpenSSH. Core asks nothing of the user for it: there is no AskPass.
WSL *WSLConnection `json:"wsl,omitempty"`
// Servers are the builds the application ships; Core picks the one for the host.
Servers []RemoteServerBuild `json:"servers"`
// ServerVersion is the version of the shipped builds.
Expand All @@ -35,6 +40,51 @@ type RemoteConnectRequest struct {
InstallRoot string `json:"installRoot,omitempty"`
}

// WSLConnection is a distribution of the Windows Subsystem for Linux on this machine, as
// remote.connect's wsl field names it. Core trims both fields; an empty User is the
// distribution's default user. Core refuses a malformed field by name ("distribution" or "user")
// with the reasons required, tooLong and invalid, read by RemoteConnectionFieldError.
type WSLConnection struct {
// Distribution is the name wsl.exe --list prints, such as Ubuntu-24.04.
Distribution string `json:"distribution"`
// User is the Linux account, passed as wsl.exe --user.
User string `json:"user,omitempty"`
}

// Identity is the connection as Core names it in the trust records and the recent projects:
// wsl:distribution, or wsl:user@distribution.
func (c WSLConnection) Identity() string {
distribution, user := strings.TrimSpace(c.Distribution), strings.TrimSpace(c.User)
if user == "" {
return "wsl:" + distribution
}
return "wsl:" + user + "@" + distribution
}

// RemoteWSLDistributions is what remote.listWslDistributions found on this machine.
type RemoteWSLDistributions struct {
// Available says whether wsl.exe ran: false on systems other than Windows and on a Windows
// without WSL.
Available bool `json:"available"`
// Distributions are the installed distributions in wsl.exe's order, without Docker Desktop's
// engine distributions; at most 64.
Distributions []string `json:"distributions"`
// Diagnostics is what wsl.exe printed when it failed, such as the message that WSL is not set
// up, redacted. It is upstream text, shown as it is and never logged.
Diagnostics string `json:"diagnostics"`
}

// RemoteListWSLDistributions lists the WSL distributions installed on this machine. Core runs
// wsl.exe --list, bounded to 15 seconds (CodeTimedOut past them), and saves nothing.
func (c *Client) RemoteListWSLDistributions(ctx context.Context) (RemoteWSLDistributions, error) {
var result RemoteWSLDistributions
if err := c.Execute(ctx, "remote.listWslDistributions", nil, &result); err != nil {
return RemoteWSLDistributions{}, err
}
result.Distributions = nonNil(result.Distributions)
return result, nil
}

// RemoteSession is a session remote.connect opened.
type RemoteSession struct {
// ID names the session in every later request.
Expand Down Expand Up @@ -62,8 +112,9 @@ type RemoteConnectResult struct {

// RemoteConnect opens a session as request asks. Questions OpenSSH asks reach the prompt
// WithGitAuthentication put in ctx, as for RemoteProbe. Core bounds an attempt, answers
// included, to five minutes. Failures are CodeInvalidRequest before OpenSSH starts,
// CodeRuntimeMissing without ssh, CodeRemoteServerInvalid when the build cannot be read or
// included, to five minutes; a WSL connection asks nothing, and a distribution that is not
// installed is RemoteProbeUnreachable. Failures are CodeInvalidRequest before OpenSSH or wsl.exe
// starts, CodeRuntimeMissing without ssh, CodeRemoteServerInvalid when the build cannot be read or
// differs from its SHA-256, and the context's error when ctx ends. A session whose events could
// not all be observed is closed again and the observation failure returned: the user may not
// have seen every question it asked.
Expand Down
42 changes: 42 additions & 0 deletions desktop/internal/core/remote_session_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"os"
"os/exec"
"runtime"
"strings"
"sync/atomic"
"testing"
Expand Down Expand Up @@ -243,3 +244,44 @@ func TestRemoteConnectRefusesAMalformedBuildBeforeConnecting(t *testing.T) {
t.Fatalf("connect = %v, want invalid_request", err)
}
}

// A WSL connection is sent as the wsl field alone, and Core refuses one it cannot run by the field
// it names before wsl.exe starts: so do a request naming both a host and a distribution and one
// naming neither.
func TestRemoteConnectSendsWSLAloneAndCoreRefusesItByField(t *testing.T) {
wire, err := json.Marshal(RemoteConnectRequest{WSL: &WSLConnection{Distribution: "Ubuntu"}, Servers: []RemoteServerBuild{}})
if err != nil || strings.Contains(string(wire), `"connection"`) || !strings.Contains(string(wire), `"wsl":{"distribution":"Ubuntu"}`) {
t.Fatalf("request = %s, %v, want the wsl field without a connection", wire, err)
}
if identity := (WSLConnection{Distribution: " Debian ", User: " dev "}).Identity(); identity != "wsl:dev@Debian" {
t.Fatalf("identity = %q", identity)
}
client := NewClient()
for _, tc := range []struct {
request RemoteConnectRequest
field, reason string
}{
{RemoteConnectRequest{WSL: &WSLConnection{Distribution: " "}}, "distribution", "required"},
{RemoteConnectRequest{WSL: &WSLConnection{Distribution: "-d"}}, "distribution", "invalid"},
{RemoteConnectRequest{WSL: &WSLConnection{Distribution: "Ubuntu", User: "a b"}}, "user", "invalid"},
{RemoteConnectRequest{Connection: SSHConnection{Host: "devbox"}, WSL: &WSLConnection{Distribution: "Ubuntu"}}, "wsl", "invalid"},
{RemoteConnectRequest{}, "connection", "required"},
} {
tc.request.Servers, tc.request.ServerVersion = shippedTargets, "0.0.0-test"
_, err := client.RemoteConnect(testContext(t), tc.request)
if field, reason, ok := RemoteConnectionFieldError(err); !ok || field != tc.field || reason != tc.reason {
t.Fatalf("connect refused %q %q (%v), want %q %q", field, reason, ok, tc.field, tc.reason)
}
}
}

// Outside Windows there is no WSL: the listing says so without running anything.
func TestRemoteListWSLDistributionsIsUnavailableOutsideWindows(t *testing.T) {
if runtime.GOOS == "windows" {
t.Skip("Windows lists the machine's own distributions")
}
listed, err := NewClient().RemoteListWSLDistributions(testContext(t))
if err != nil || listed.Available || listed.Distributions == nil || len(listed.Distributions) != 0 || listed.Diagnostics != "" {
t.Fatalf("listing = %+v, %v, want unavailable with an empty list", listed, err)
}
}
50 changes: 38 additions & 12 deletions desktop/internal/i18n/shared.generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading
Loading