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
2 changes: 2 additions & 0 deletions rust/contracts/application-boundary.md
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,8 @@ meaning; `lithe_protocol::ErrorCode` documents when it is reported:
- `reverse_forward_disabled`: the remote workspace settings do not allow reverse forwards
- `remote_server_invalid`: the server build `remote.connect` would send cannot
be read or is not the build its SHA-256 names
- `devcontainer_untrusted_folder`: the folder is not trusted, so its dev
container was not built or started
- `unknown`

GitHub authorization is independent of a Lithe account. Device Flow is the
Expand Down
125 changes: 118 additions & 7 deletions rust/contracts/rust-core-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,9 @@ or removing a command fails until the table changes in the same commit.
| `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 |
| `remote.listContainers` | List the running Docker or Podman containers on this machine or a connected host |
| `remote.devcontainerConfigs` | List the `devcontainer.json` files of a local folder |
| `remote.devcontainerUp` | Build or reuse and start a trusted local folder's dev container with the Dev Container CLI |
| `remote.preferences` | Read the remote workspace settings and what each automatic value resolves to |
| `remote.editPreferences` | Set the SSH program, the reconnect limit, whether reverse forwards are allowed or what closing a connection does to its programs, or make one automatic again |
| `remote.runningPrograms` | List the processes and terminals a connection's host session still runs |
Expand Down Expand Up @@ -5938,6 +5941,85 @@ 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.listContainers` takes `{ engine, sessionId? }`, where `engine` is
`docker` or `podman`, and returns `{ available, containers, diagnostics }`.
Core runs `<engine> ps --no-trunc --format '{{.ID}}\t{{.Names}}\t{{.Image}}\t{{.Status}}'`,
at most 15 seconds: on this machine in Core's own environment (the login
shell's `PATH` the application put in front, and variables such as
`DOCKER_HOST`) when `sessionId` is absent, and otherwise on that
connection's host in the server's environment, the same one the connection
later enters the container from. Only running containers are listed, since
Lithe never starts, creates or changes a container. `containers` are
`{ id, name, image, status }` in the engine's order, at most 256 and from at
most 256 KiB of output: `id` is the full id, `name` the first of the
container's names, and `image` and `status` the engine's text without
control characters, at most 256 bytes each. Lines that are not four fields,
or whose id or name is not a container name, are skipped, and so are
repeated ids. `available` is `false` when the engine CLI cannot start there;
when it runs and fails, for example because its service is not running,
`containers` is empty and `diagnostics` holds its redacted message for
display; it is never logged. Any other field is `invalid_request` ("Invalid
container listing request"), so a request cannot pick the program; an unknown
`sessionId` is `connection_closed`, and cancellation and the bound are
`cancelled` and `timed_out`. Nothing is saved.

#### Dev Containers

A local folder's dev container is built and started by the reference Dev
Container CLI (`devcontainer`, from `@devcontainers/cli`; `devcontainer.cmd`
on Windows) on `PATH`, then entered like any running container with
`remote.connect`'s `container`. Lithe does not interpret the configuration
and adds no mounts or options to it.

`remote.devcontainerConfigs` takes `{ folder }`, an absolute folder on this
machine, and returns `{ configs }`: `{ path, name? }` for each regular file in
the places the specification searches, in this order:
`.devcontainer/devcontainer.json`, `.devcontainer.json`, then
`.devcontainer/<subfolder>/devcontainer.json` for at most 64 subfolders in
name order, skipping names that start with `.`. `path` is relative to the
folder with `/` separators. `name` is the configuration's `name`, read as
JSON with comments and trailing commas, without control characters and at
most 128 bytes; it is absent when the file is larger than 1 MiB, resolves
outside the folder, cannot be read or has no non-empty string `name`. A
relative or empty `folder` is `invalid_request`; a folder that does not
exist is `workspace_not_found`. Reading starts nothing.

`remote.devcontainerUp` takes `{ folder, config?, engine,
projectCodeAllowed }` and returns `{ container?, diagnostics }`. Building runs
the folder's code on this machine (`initializeCommand`, Dockerfiles, feature
scripts), so when `projectCodeAllowed` is `false` nothing starts and the
request fails with `devcontainer_untrusted_folder`; native clients pass the
folder's trust decision, as for Git. `config` must be one of the `path`s
`remote.devcontainerConfigs` lists for the folder, otherwise
`invalid_request`; without it the CLI searches the folder itself. Core runs
`devcontainer up --workspace-folder <folder> [--config <folder>/<config>]`,
adding `--docker-path podman` after `up` for `engine: "podman"`, on this
machine in Core's own environment, for at most one hour. Each line the CLI
logs to standard error is sent as a `{ type: "devcontainerOutput", text }`
event, redacted, at most 4096 bytes per event and 2 MiB in all; after that
one `{ type: "devcontainerOutput", truncated: true }` event follows and later
lines are dropped. The log is shown, never logged.

The result is the last line of standard output that is a JSON object with an
`outcome`:

- `success`: `container` is `{ engine, container, containerId, user?,
workspaceFolder }`. `containerId` is the CLI's container id, `container` the
name `<engine> inspect --format '{{.Name}}'` gives within 15 seconds
(without Docker's leading `/`), or the id when it gives none, `user` the
configuration's `remoteUser`, and `workspaceFolder` the absolute
`remoteWorkspaceFolder` to open. A container id that is not a container
name, a user `exec` cannot take, or a folder that is not absolute is
`parse_failed`, and so is a successful exit without a result line.
- `error`, or no result line with a failed exit: `container` is absent and
`diagnostics` holds the end of the CLI's log followed by its `message` and
`description`, redacted, for display.

A missing CLI is `runtime_missing`; any other field is `invalid_request`
("Invalid dev container start request"), so a request cannot add mounts or
pick the program; cancellation and the bound are `cancelled` and
`timed_out`. Nothing is saved.

#### Remote workspace preferences

The settings page "Tools › Remote Workspace" edits three values that apply to
Expand Down Expand Up @@ -6072,14 +6154,43 @@ because `wsl.exe` asks no questions. An unknown distribution is
the distribution and the account (`{ wsl: { distribution, user } }` as the
first and only hop).

The request may also name a running container as `container: { engine,
container, user? }`, where `engine` is `docker` or `podman`, `container` a
name or id such as `remote.listContainers` returned, and `user` a user, uid or
`user:group` inside it (the image's default user when absent or empty). Next
to `connection` or `wsl` the container is on that host: the remote command
becomes `<engine> exec -i [-u <user>] <container> …` there, after the login.
On its own the container is on this machine, and Core starts that command
itself in place of OpenSSH, with its own environment so the engine finds its
service. Core only enters the running container: it never starts, creates or
reconfigures it, mounts directories into it or asks for more rights than the
engine CLI has. The probe, the checked upload into the container's `$HOME`
and the stages are the same, and nothing is relayed through
`remote.authRespond` for the engine. When the engine gives up before the
server starts, a container that does not exist, is not running or is paused,
and an engine whose service does not answer, are `unreachable`; a service
socket the user may not open is `failed`, not `authenticationFailed`, even
over SSH; and other failures are `failed` with the engine's message in
`diagnostics` (or `<engine> exited with status <code>` when it printed
nothing). The connection log's `transport` is `container`, `ssh-container` or
`wsl-container`, without the container's name. Trust decisions name the
container after the other hops, as `{ container: { engine, program,
container, user? } }`, so a decision never carries over to another container
or account.

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
`details` `{ field: "wsl", reason: "invalid" }`, and one that names none of
`connection`, `wsl` and `container` `{ 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, a container that is empty (`{ field:
"container", reason: "required" }`), longer than 128 bytes or not a container
name, a container user that is longer than 64 bytes, starts with `-` or holds
other characters than letters, digits and `_.:-` (`{ field: "user" }`), an
unknown `engine` or field, an unknown `target` or a `sha256` that is not 64
hexadecimal digits is `invalid_request`, before OpenSSH, `wsl.exe` or the
engine starts. A missing `ssh`, `wsl.exe` or local engine CLI 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
Expand Down
12 changes: 12 additions & 0 deletions rust/crates/core/src/protocol/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1046,6 +1046,15 @@ pub enum RemoteCommand {
/// Lists the WSL distributions installed on this Windows machine
/// (`remote.listWslDistributions`).
ListWslDistributions,
/// Lists the running containers of a container engine on this machine
/// or on a connected host (`remote.listContainers`).
ListContainers,
/// Lists the `devcontainer.json` files of a local folder
/// (`remote.devcontainerConfigs`).
DevcontainerConfigs,
/// Builds or reuses and starts a local folder's dev container with the
/// Dev Container CLI (`remote.devcontainerUp`).
DevcontainerUp,
/// Reads the remote workspace preferences and what each automatic value
/// resolves to (`remote.preferences`).
Preferences,
Expand Down Expand Up @@ -1639,6 +1648,9 @@ impl CoreCommand {
"remote.listWslDistributions" => {
Some(Self::Remote(RemoteCommand::ListWslDistributions))
}
"remote.listContainers" => Some(Self::Remote(RemoteCommand::ListContainers)),
"remote.devcontainerConfigs" => Some(Self::Remote(RemoteCommand::DevcontainerConfigs)),
"remote.devcontainerUp" => Some(Self::Remote(RemoteCommand::DevcontainerUp)),
"remote.preferences" => Some(Self::Remote(RemoteCommand::Preferences)),
"remote.editPreferences" => Some(Self::Remote(RemoteCommand::EditPreferences)),
"remote.runningPrograms" => Some(Self::Remote(RemoteCommand::RunningPrograms)),
Expand Down
31 changes: 31 additions & 0 deletions rust/crates/core/src/runtime/dispatcher/remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,37 @@ pub(super) fn dispatch(command: RemoteCommand, id: Option<String>, payload: Valu
),
Err(error) => CoreResponse::failure(id, error),
},
RemoteCommand::ListContainers => answer(
id,
payload,
"Invalid container listing request",
|request| {
lithe_workspace::containers::list(request).map(|listed| {
serde_json::to_value(listed).expect("container listings should encode")
})
},
),
RemoteCommand::DevcontainerConfigs => answer(
id,
payload,
"Invalid dev container configuration request",
|request| {
lithe_workspace::devcontainers::configs(request).map(|listed| {
serde_json::to_value(listed)
.expect("dev container configurations should encode")
})
},
),
RemoteCommand::DevcontainerUp => answer(
id,
payload,
"Invalid dev container start request",
|request| {
lithe_workspace::devcontainers::up(request).map(|started| {
serde_json::to_value(started).expect("dev container starts should encode")
})
},
),
RemoteCommand::AuthRespond => {
#[derive(serde::Deserialize)]
#[serde(rename_all = "camelCase", deny_unknown_fields)]
Expand Down
73 changes: 73 additions & 0 deletions rust/crates/core/src/tests/remote_sessions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1434,6 +1434,37 @@ fn connection_window_helpers_answer_through_the_dispatcher() {
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"}));
// A container listing names a known engine and nothing that could pick
// the program.
for payload in [
json!({"engine": "nerdctl"}),
json!({"engine": "docker", "program": "/bin/sh"}),
] {
let refused = call("remote.listContainers", payload);
assert_eq!(refused["error"]["code"], "invalid_request", "{refused}");
assert_eq!(
refused["error"]["message"], "Invalid container listing request",
"{refused}"
);
}
// A dev container starts only for a trusted folder, from a configuration
// the folder lists; nothing runs otherwise.
let untrusted = call(
"remote.devcontainerUp",
json!({"folder": "/nonexistent", "engine": "docker", "projectCodeAllowed": false}),
);
assert_eq!(
untrusted["error"]["code"], "devcontainer_untrusted_folder",
"{untrusted}"
);
let unknown = call(
"remote.devcontainerUp",
json!({"folder": "/nonexistent", "engine": "docker", "projectCodeAllowed": true,
"mounts": ["/"]}),
);
assert_eq!(unknown["error"]["code"], "invalid_request", "{unknown}");
let relative = call("remote.devcontainerConfigs", json!({"folder": "app"}));
assert_eq!(relative["error"]["code"], "invalid_request", "{relative}");

// The settings page reads automatic values and stores an edit; a damaged
// document stops the SSH commands before OpenSSH starts.
Expand Down Expand Up @@ -1789,6 +1820,48 @@ fn remote_host_and_reverse_forwards_follow_their_own_rules() {
);
}

// The connection window lists a connected host's containers through the
// host's own engine CLI; a host without one says so instead of failing.
#[test]
fn container_listings_run_on_the_host_and_say_when_it_has_no_engine() {
use std::os::unix::fs::PermissionsExt;
let host = HostFixture::new();
let engine = host.root.join("host/docker");
std::fs::write(
&engine,
"#!/bin/sh\nprintf 'c0ffee\\tdev\\tubuntu:24.04\\tUp 1 minute\\n'\n",
)
.unwrap();
std::fs::set_permissions(&engine, std::fs::Permissions::from_mode(0o755)).unwrap();
let request = |engine: &str| {
serde_json::from_value(json!({"engine": engine, "sessionId": host.id})).unwrap()
};
let listed =
test_support::list_containers_with(request("docker"), engine.to_str().unwrap()).unwrap();
assert_eq!(
serde_json::to_value(listed).unwrap(),
json!({"available": true, "diagnostics": "", "containers": [
{"id": "c0ffee", "name": "dev", "image": "ubuntu:24.04", "status": "Up 1 minute"}
]})
);
let absent = host.root.join("host/absent");
let missing =
test_support::list_containers_with(request("podman"), absent.to_str().unwrap()).unwrap();
assert_eq!(
serde_json::to_value(missing).unwrap(),
json!({"available": false, "containers": [], "diagnostics": ""})
);
let unknown = test_support::list_containers_with(
serde_json::from_value(json!({"engine": "docker", "sessionId": "absent"})).unwrap(),
engine.to_str().unwrap(),
)
.unwrap_err();
assert!(
matches!(unknown.code, ErrorCode::ConnectionClosed),
"{unknown:?}"
);
}

#[test]
fn remote_programs_are_listed_ended_and_kept_only_by_a_persistent_session() {
use lithe_remote::client::api::{SpawnSpec, TerminalEvent, TerminalSpec};
Expand Down
Loading
Loading