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
45 changes: 39 additions & 6 deletions rust/contracts/rust-core-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -802,9 +802,9 @@ or removing a command fails until the table changes in the same commit.
| `git.authRespond` | Answer or cancel a live authentication challenge once |
| `remote.probe` | Check that an SSH connection reaches a supported host |
| `remote.authRespond` | Answer or cancel a prompt relayed by `remote.probe` or `remote.connect` once |
| `remote.connect` | Open a connection, installing the shipped server when needed |
| `remote.connect` | Open a connection over SSH or to a local WSL distribution, installing the shipped server when needed |
| `remote.readDir` | List a directory over an open remote connection |
| `remote.disconnect` | Close an open connection and its OpenSSH process tree |
| `remote.disconnect` | Close an open connection and its OpenSSH or `wsl.exe` process tree |
| `remote.closeAll` | Close every open connection; the host calls it at application exit |
| `remote.readFile` | Read a file over an open remote connection |
| `remote.writeFile` | Save a file over an open remote connection |
Expand All @@ -827,6 +827,7 @@ or removing a command fails until the table changes in the same commit.
| `remote.effectiveSettings` | Tell what OpenSSH makes of a connection and where each value comes from |
| `remote.sshConfigHosts` | List the Host aliases of the user's SSH configuration for an import |
| `remote.mottyHosts` | List the SSH hosts saved in Motty for an import |
| `remote.listWslDistributions` | List the WSL distributions installed on this Windows machine |
| `git.historyRewritePreview` | Review undo, message edit, squash, or drop with complete messages, eligibility, and an immutable checkout expectation |
| `git.remotes` | Return configured fetch remote names and URLs |
| `git.tags` | Return tag names, tag object IDs, target IDs, dates, and annotations |
Expand Down Expand Up @@ -5903,6 +5904,19 @@ connection?, fields }`:
A relative directory is `invalid_request` and a damaged document
`parse_failed`. Nothing is written to Motty's files.

`remote.listWslDistributions` (no payload) returns `{ available,
distributions, diagnostics }`. On Windows Core runs `wsl.exe --list --quiet`
from `%SystemRoot%\System32` with `WSL_UTF8=1`, at most 15 seconds, and reads
its UTF-16 or UTF-8 output. `distributions` are the installed names in
`wsl.exe` order, at most 64, without Docker Desktop's `docker-desktop` and
`docker-desktop-data` and without names repeated in another case.
`available` is `false` on other systems, where nothing runs, and when
`wsl.exe` cannot start. When `wsl.exe` fails, for example because no
distribution is installed, `distributions` is empty and `diagnostics` holds
its redacted message for display; it is never logged. Cancellation and the
bound are `cancelled` and `timed_out`. Nothing is saved: the window lists
the distributions each time it opens.

#### Remote connections that stay open

`remote.connect` takes `{ connection, servers: [{ target, path, sha256 }],
Expand Down Expand Up @@ -5932,17 +5946,36 @@ host, diagnostics }`; `session` is `{ id, uploaded, hostName, userName,
homeDirectory }` and is set only for `connected`. A host without a matching
build is `unsupportedPlatform` and nothing is uploaded.

A malformed connection, an unknown `target` or a `sha256` that is not 64
hexadecimal digits is `invalid_request`, before OpenSSH starts. A missing
`ssh` is `runtime_missing`. When the build for the host's platform cannot be
Instead of `connection`, the request may name a WSL distribution on this
Windows machine as `wsl: { distribution, user? }`, where `distribution` is a
name `remote.listWslDistributions` returned and `user` a Linux account (the
distribution's default user when absent). Core then runs
`wsl.exe --distribution <distribution> [--user <user>] --cd ~ --exec …` from
`%SystemRoot%\System32` in place of OpenSSH, keeping the Windows environment
`wsl.exe` needs and setting `WSL_UTF8=1`; the probe, the checked upload and
the stages are the same. Nothing is relayed through `remote.authRespond`,
because `wsl.exe` asks no questions. An unknown distribution is
`unreachable`; other failures are `failed` with `wsl.exe`'s message in
`diagnostics`, decoded from UTF-16 when it prints that. Trust decisions name
the distribution and the account (`{ wsl: { distribution, user } }` as the
first and only hop).

A request that names both `connection` and `wsl` is `invalid_request` with
`details` `{ field: "wsl", reason: "invalid" }`, and one that names neither
`{ field: "connection", reason: "required" }`. A malformed connection, a
distribution that is empty, longer than 64 bytes or not a WSL distribution
name, a user that is longer than 32 bytes or not a Linux login name, an
unknown `target` or a `sha256` that is not 64 hexadecimal digits is
`invalid_request`, before OpenSSH or `wsl.exe` starts. A missing `ssh` or
`wsl.exe` is `runtime_missing`. When the build for the host's platform cannot be
read or its SHA-256 differs, the attempt ends as `remote_server_invalid`
without sending anything; `details` says which of the two happened and, for a
mismatch, the SHA-256 Core computed, but never the path. Cancellation and the
five-minute bound are `cancelled` and `timed_out`.

Core keeps an open connection until `remote.disconnect` (`{ sessionId }`,
returning `{ closed }`) or until the host process exits; ending it terminates
the OpenSSH process tree. `remote.closeAll` (no payload, returning
the OpenSSH or `wsl.exe` process tree. `remote.closeAll` (no payload, returning
`{ closed }` with the number of connections) ends all of them; the host calls
it while quitting because exiting without it can leave the OpenSSH tree and
the remote server running. Requests over it take `sessionId`:
Expand Down
6 changes: 6 additions & 0 deletions rust/crates/core/src/protocol/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1037,6 +1037,9 @@ pub enum RemoteCommand {
SshConfigHosts,
/// Lists the SSH hosts saved in Motty for an import (`remote.mottyHosts`).
MottyHosts,
/// Lists the WSL distributions installed on this Windows machine
/// (`remote.listWslDistributions`).
ListWslDistributions,
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -1614,6 +1617,9 @@ impl CoreCommand {
"remote.effectiveSettings" => Some(Self::Remote(RemoteCommand::EffectiveSettings)),
"remote.sshConfigHosts" => Some(Self::Remote(RemoteCommand::SshConfigHosts)),
"remote.mottyHosts" => Some(Self::Remote(RemoteCommand::MottyHosts)),
"remote.listWslDistributions" => {
Some(Self::Remote(RemoteCommand::ListWslDistributions))
}
"git.historyRewritePreview" => Some(Self::Git(GitCommand::HistoryRewritePreview)),
"git.remotes" => Some(Self::Git(GitCommand::Remotes)),
"git.tags" => Some(Self::Git(GitCommand::Tags)),
Expand Down
7 changes: 7 additions & 0 deletions rust/crates/core/src/runtime/dispatcher/remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,13 @@ pub(super) fn dispatch(command: RemoteCommand, id: Option<String>, payload: Valu
id,
json!({ "closed": lithe_workspace::sessions::close_all() }),
),
RemoteCommand::ListWslDistributions => match lithe_workspace::wsl::list() {
Ok(listed) => CoreResponse::success(
id,
serde_json::to_value(listed).expect("WSL distributions should encode"),
),
Err(error) => CoreResponse::failure(id, error),
},
RemoteCommand::AuthRespond => {
#[derive(serde::Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
Expand Down
24 changes: 24 additions & 0 deletions rust/crates/core/src/tests/remote_sessions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1306,7 +1306,31 @@ fn connection_window_helpers_answer_through_the_dispatcher() {
"a refused address is not echoed"
);

// Outside Windows there is no WSL, and nothing runs.
if !cfg!(windows) {
let distributions = call("remote.listWslDistributions", Value::Null);
assert_eq!(
distributions["data"],
json!({"available": false, "distributions": [], "diagnostics": ""}),
"{distributions}"
);
}
// A connection names SSH or WSL, not both, before anything starts.
let both = call(
"remote.connect",
json!({"connection": {"host": "devbox"}, "wsl": {"distribution": "Ubuntu"},
"servers": [], "serverVersion": "0.1.0"}),
);
assert_eq!(both["error"]["code"], "invalid_request", "{both}");
let details: Value = serde_json::from_str(both["error"]["details"].as_str().unwrap()).unwrap();
assert_eq!(details, json!({"field": "wsl", "reason": "invalid"}));

for (command, payload) in [
(
"remote.connect",
json!({"wsl": {"distribution": "Ubuntu", "program": "C:\\x.exe"},
"servers": [], "serverVersion": "0.1.0"}),
),
(
"remote.parseForwardAddress",
json!({"text": "3000", "extra": 1}),
Expand Down
14 changes: 7 additions & 7 deletions rust/crates/diagnostics/src/catalog_generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1309,7 +1309,7 @@ pub static EVENTS: &[EventMetadata] = &[
FieldMetadata { name: "resumed", field_type: FieldType::Bool, required: false, description: "Whether this connection reattached to an existing persistent session, i.e. a reconnect", reason: None },
FieldMetadata { name: "server_version", field_type: FieldType::Version, required: false, description: "Version the remote server reported in server.hello once the handshake succeeded", reason: None },
FieldMetadata { name: "stage", field_type: FieldType::Token, required: false, description: "Connection step that failed: probe, artifact, deploy, handshake, authentication, network or connect", reason: None },
FieldMetadata { name: "transport", field_type: FieldType::Token, required: true, description: "How the client reached the server: ssh, command (any spawned transport chain) or streams (caller-supplied pipes)", reason: None },
FieldMetadata { name: "transport", field_type: FieldType::Token, required: true, description: "How the client reached the server: ssh, wsl (a local WSL distribution through wsl.exe), command (any spawned transport chain) or streams (caller-supplied pipes)", reason: None },
],
span: Some(SpanPhase::Failed),
rate: 60,
Expand All @@ -1328,7 +1328,7 @@ pub static EVENTS: &[EventMetadata] = &[
FieldMetadata { name: "resumed", field_type: FieldType::Bool, required: false, description: "Whether this connection reattached to an existing persistent session, i.e. a reconnect", reason: None },
FieldMetadata { name: "server_version", field_type: FieldType::Version, required: false, description: "Version the remote server reported in server.hello once the handshake succeeded", reason: None },
FieldMetadata { name: "stage", field_type: FieldType::Token, required: false, description: "Connection step that failed: probe, artifact, deploy, handshake, authentication, network or connect", reason: None },
FieldMetadata { name: "transport", field_type: FieldType::Token, required: true, description: "How the client reached the server: ssh, command (any spawned transport chain) or streams (caller-supplied pipes)", reason: None },
FieldMetadata { name: "transport", field_type: FieldType::Token, required: true, description: "How the client reached the server: ssh, wsl (a local WSL distribution through wsl.exe), command (any spawned transport chain) or streams (caller-supplied pipes)", reason: None },
],
span: Some(SpanPhase::Finished),
rate: 60,
Expand All @@ -1346,7 +1346,7 @@ pub static EVENTS: &[EventMetadata] = &[
FieldMetadata { name: "resumed", field_type: FieldType::Bool, required: false, description: "Whether this connection reattached to an existing persistent session, i.e. a reconnect", reason: None },
FieldMetadata { name: "server_version", field_type: FieldType::Version, required: false, description: "Version the remote server reported in server.hello once the handshake succeeded", reason: None },
FieldMetadata { name: "stage", field_type: FieldType::Token, required: false, description: "Connection step that failed: probe, artifact, deploy, handshake, authentication, network or connect", reason: None },
FieldMetadata { name: "transport", field_type: FieldType::Token, required: true, description: "How the client reached the server: ssh, command (any spawned transport chain) or streams (caller-supplied pipes)", reason: None },
FieldMetadata { name: "transport", field_type: FieldType::Token, required: true, description: "How the client reached the server: ssh, wsl (a local WSL distribution through wsl.exe), command (any spawned transport chain) or streams (caller-supplied pipes)", reason: None },
],
span: Some(SpanPhase::Started),
rate: 60,
Expand Down Expand Up @@ -1488,8 +1488,8 @@ pub static EVENTS: &[EventMetadata] = &[
level: LogLevel::Warn,
message: "Remote session cleanup failed",
fields: &[
FieldMetadata { name: "error", field_type: FieldType::Error, required: true, description: "Stable native, thread, or remote category from ending an owned SSH connection", reason: None },
FieldMetadata { name: "stage", field_type: FieldType::Token, required: true, description: "SSH connection cleanup stage, e.g. shutdown, tree-kill, wait, or closer", reason: None },
FieldMetadata { name: "error", field_type: FieldType::Error, required: true, description: "Stable native, thread, or remote category from ending an owned ssh or wsl.exe connection", reason: None },
FieldMetadata { name: "stage", field_type: FieldType::Token, required: true, description: "Connection cleanup stage of the owned ssh or wsl.exe process, e.g. shutdown, tree-kill, wait, or closer", reason: None },
],
span: None,
rate: 60,
Expand All @@ -1501,8 +1501,8 @@ pub static EVENTS: &[EventMetadata] = &[
level: LogLevel::Warn,
message: "Remote session diagnostic reader failed",
fields: &[
FieldMetadata { name: "error", field_type: FieldType::Error, required: true, description: "Stable I/O or thread category explaining incomplete SSH connection diagnostic evidence", reason: None },
FieldMetadata { name: "stage", field_type: FieldType::Token, required: true, description: "SSH diagnostic collection stage: reader join or stderr read", reason: None },
FieldMetadata { name: "error", field_type: FieldType::Error, required: true, description: "Stable I/O or thread category explaining incomplete ssh or wsl.exe diagnostic evidence", reason: None },
FieldMetadata { name: "stage", field_type: FieldType::Token, required: true, description: "Diagnostic collection stage of the ssh or wsl.exe process: reader join or stderr read", reason: None },
],
span: None,
rate: 60,
Expand Down
3 changes: 3 additions & 0 deletions rust/crates/remote/src/client/connection_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ const RELEASED: u8 = 3;
pub enum ConnectionKind {
/// An SSH connection owned by the caller.
Ssh,
/// A WSL distribution started through `wsl.exe`, owned by the caller.
Wsl,
/// A transport chain [`crate::client::RemoteSession::connect`] spawns.
Command,
/// Streams the caller supplies to
Expand All @@ -80,6 +82,7 @@ impl ConnectionKind {
fn as_str(self) -> &'static str {
match self {
Self::Ssh => "ssh",
Self::Wsl => "wsl",
Self::Command => "command",
Self::Streams => "streams",
}
Expand Down
2 changes: 1 addition & 1 deletion rust/crates/remote/src/client/connections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ pub enum Reason {
}

impl ConnectionError {
fn at(field: &'static str, reason: Reason) -> Self {
pub(crate) fn at(field: &'static str, reason: Reason) -> Self {
Self { field, reason }
}
}
Expand Down
12 changes: 9 additions & 3 deletions rust/crates/remote/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
//!
//! - [`connections`] keeps the SSH connections the user saved and turns each
//! into an SSH hop with allowlisted options only.
//! - [`transport`] turns a chain of hops (SSH, container exec) into the local
//! command line that runs a program at the end of the chain.
//! - [`transport`] turns a chain of hops (SSH, WSL, container exec) into the
//! local command line that runs a program at the end of the chain.
//! - [`wsl`] turns a WSL distribution into the first hop and lists the
//! installed distributions.
//! - [`bootstrap`] probes the platform at the end of the chain and describes
//! the local server builds that can run there.
//! - [`launch`] probes, installs and starts the server over one connection,
Expand Down Expand Up @@ -50,6 +52,7 @@ pub mod session;
pub mod ssh_config;
pub mod transport;
pub mod trust;
pub mod wsl;

pub use api::{SpawnSpec, TerminalSpec, WriteOptions};
pub use bootstrap::{ArtifactError, ServerArtifact, ServerTarget};
Expand All @@ -58,4 +61,7 @@ pub use forward::PortForward;
pub use forward_address::{AddressError, ForwardAddress};
pub use forward_set::{ForwardError, ForwardInfo, ForwardSet, ForwardSpec, TargetField};
pub use session::{ClientError, ConnectOptions, Event, RemoteSession, ResumeMode};
pub use transport::{ContainerEngine, ContainerHop, Hop, SshHop, TransportChain, TransportError};
pub use transport::{
ContainerEngine, ContainerHop, Hop, SshHop, TransportChain, TransportError, WslHop,
};
pub use wsl::WslConnection;
Loading
Loading