Repository navigation
feat(remote): 进入 Docker/Podman 容器、列出运行中的容器,并用 Dev Container CLI 从 devcontainer.json 启动 (#2231) - #162
Merged
Merged
Conversation
remote.connect 新增 container:单独出现时由本机的 docker/podman exec 进入, 跟在 connection 或 wsl 后面时在那台主机登录后再 exec 进入。只进入正在运行的容器, 不启动、不创建、不改动容器,不挂载目录,不提升权限(S32)。信任目标包含容器, 日志 transport 只记 container / ssh-container / wsl-container,不记容器名。 新增 remote.listContainers:在本机或已连接会话的主机上运行 <engine> ps, 最多 15 秒、256 个容器、256 KiB 输出;没有引擎时 available=false, 引擎报错时返回脱敏后的提示,只显示、不写日志。
28 of 34 tasks
- 新增 remote.devcontainerConfigs:按规范位置列出本机文件夹的 devcontainer.json(.devcontainer/devcontainer.json、.devcontainer.json、 .devcontainer/<子目录>/devcontainer.json),按 JSONC 读取 name; 只读文件夹内、不超过 1 MiB 的普通文件,不启动任何程序。 - 新增 remote.devcontainerUp:运行 devcontainer up --workspace-folder [--config],Podman 在 up 之后加 --docker-path podman;最长一小时,可取消。 文件夹不受信任(projectCodeAllowed=false)时什么都不运行,返回新错误码 devcontainer_untrusted_folder;config 只能是列出的路径;没有 CLI 为 runtime_missing。 - stderr 日志按行以 devcontainerOutput 事件发给窗口,脱敏,单行 4 KiB、 总共 2 MiB,之后发一次 truncated;只显示,不写日志。 - 结果读 stdout 最后一行 outcome JSON:成功时返回 engine、容器名(inspect 得到,没有时用 id)、containerId、remoteUser 与 remoteWorkspaceFolder, 供 remote.connect 的 container 进入;CLI 报错时返回脱敏的日志尾部和 message/description。Lithe 不解释配置,不额外挂载或加参数(S32)。 - 契约文档与错误码列表同步更新。
- 补 Log 的单元测试:跨两次读取的凭据作为同一行脱敏,CRLF 不进入文本; 超过 4096 字节的行分段发送,剩余部分在 CLI 退出时发出。 - 超过 2 MiB 后只发一次 truncated 事件,之后的行都不再发送。
Keep the container hops of the chain and the resume retry of the persistent sessions; the connection log still tells a container apart.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
关联 Issue
Refs 1lck/Lithe-IDEA#2231(R2 Dev Container:容器目标,以及从 devcontainer.json 构建并启动;安全项 S32)
桌面端在 1lck/Lithe-IDEA#2295(Draft,固定到本 PR 的 05c4139;e1e0d10 只加测试,不需要换固定版本):容器连接页,以及“从 devcontainer.json 打开”页。SSH 主机和 WSL 发行版上的
devcontainer up不在本 PR。类型
feat
改动说明
本 PR 有三个提交,下面按功能分两部分说明;第三个提交(e1e0d10)只补测试。
container进入。remote.connect新增container: { engine, container, user? }engine只能是docker或podman。container是名字或 id,例如remote.listContainers返回的值。user是容器里的用户、uid 或user:group;没填或为空时用镜像的默认用户。connection或wsl后面时,在那台主机登录后,远端命令变成<engine> exec -i [-u <user>] <container> …。PATH,以及DOCKER_HOST这类选择服务的变量。不走 AskPass。$HOME,以及之后的各个阶段,都和 SSH 主机一样,复用launch链。unreachable;failed,即使经过 SSH 也不算authenticationFailed;failed。diagnostics是引擎的提示;引擎什么都没打印时是<engine> exited with status <code>。{ field: "container", reason: "required" };超过 128 字节或不是合法的容器名,返回invalid。-开头,或含字母、数字、_.:-以外的字符,返回{ field: "user", reason: "invalid" }。invalid_request。runtime_missing。{ container: { engine, program, container, user? } },所以信任决定不会带到别的容器或账号。transport新增container、ssh-container、wsl-container三个值,不记容器名、镜像或用户。remote.listContainers(新模块workspace/src/containers.rs){ engine, sessionId? },结果是{ available, containers: [{ id, name, image, status }], diagnostics }。sessionId时在本机运行。带上时在该连接的主机上运行,用服务器的环境,和之后进入容器用的是同一种非登录环境,所以能列出来的就能连上。<engine> ps --no-trunc --format '{{.ID}}\t{{.Names}}\t{{.Image}}\t{{.Status}}',限制为最多 15 秒、256 个容器、256 KiB 输出。只列出正在运行的容器,因为 Lithe 不会启动容器。image、status去掉控制字符,最多 256 字节。Failure::Start,主机上SPAWN_FAILED或退出码 127),返回available: false。引擎运行后失败时(例如服务没启动),containers为空,diagnostics是脱敏后的提示,只给界面显示,不写日志。sessionId返回connection_closed;取消和超时分别返回cancelled和timed_out。什么都不保存。host::capture改为自己按session_id查连接,把真实的会话 id 写进请求,这样断开时的forget不受影响。remote.devcontainerConfigs(新模块workspace/src/devcontainers.rs、remote/src/client/devcontainer.rs){ folder },folder是本机上的绝对路径;结果是{ configs: [{ path, name? }] }。.devcontainer/devcontainer.json.devcontainer.json.devcontainer/<子目录>/devcontainer.json:按名字顺序,最多 64 个子目录,跳过以.开头的子目录。path相对文件夹,用/分隔。name是配置里的name,按带注释和尾逗号的 JSON 读取,去掉控制字符,最多 128 字节。以下情况不返回name:文件超过 1 MiB、解析后在文件夹外、读不了、没有非空字符串name。folder返回invalid_request,不存在的文件夹返回workspace_not_found。只读文件,不启动任何程序。remote.devcontainerUp{ folder, config?, engine, projectCodeAllowed },结果是{ container?, diagnostics }。initializeCommand、Dockerfile、feature 脚本),所以projectCodeAllowed为false时什么都不启动,返回新错误码devcontainer_untrusted_folder。客户端和 Git 一样,传入文件夹的信任决定。config只能是remote.devcontainerConfigs为这个文件夹列出的某个path,否则返回invalid_request。不带config时,由 CLI 自己在文件夹里查找。devcontainer up --workspace-folder <folder> [--config <folder>/<config>];engine: "podman"时在up后面加--docker-path podman。在本机用 Core 自己的环境运行,最长一小时,可以取消。CLI 来自@devcontainers/cli,Windows 上是devcontainer.cmd。{ type: "devcontainerOutput", text }事件发出;{ type: "devcontainerOutput", truncated: true },之后的行丢弃;outcome的 JSON 对象:success:container是{ engine, container, containerId, user?, workspaceFolder }。containerId是 CLI 返回的 id。container是 15 秒内<engine> inspect --format '{{.Name}}'给出的名字(去掉 Docker 的前导/),没有名字时用 id。user是配置的remoteUser。workspaceFolder是要打开的remoteWorkspaceFolder,必须是绝对路径。parse_failed:id 不是合法的容器名、exec不能接受这个用户、文件夹不是绝对路径,以及成功退出但没有结果行。error,或者退出失败且没有结果行:不返回container;diagnostics是 CLI 日志的末尾加上它的message和description,已脱敏,用于显示。runtime_missing。其他字段返回invalid_request(“Invalid dev container start request”),所以请求不能加挂载,也不能指定程序。取消和超时分别返回cancelled和timed_out。什么都不保存。remote 与 workspace 以外的改动
core:protocol/command.rs新增remote.listContainers、remote.devcontainerConfigs和remote.devcontainerUp三个命令;runtime/dispatcher/remote.rs把它们接到lithe_workspace::containers和lithe_workspace::devcontainers;tests/remote_sessions.rs增加拒绝请求和在主机上列出的测试,以及 devcontainer 的未受信任、未知字段和相对路径。protocol/src/error.rs:新增ErrorCode::DevcontainerUntrustedFolder,只表示“文件夹未受信任,所以没有构建”,不和其他错误码共用。diagnostics:重新生成catalog_generated.rs,因为logging/events/remote.json的transport新增了三个值。logging/events/remote.json:同上。contracts/rust-core-api.md:命令表、remote.listContainers段、remote.connect的container段和拒绝规则,以及新的“Dev Containers”段。contracts/application-boundary.md:错误码列表加了devcontainer_untrusted_folder。和 Core #160 的重叠
#160 也改了
workspace/src/sessions.rs:它在ConnectRequest加了resume字段,改了构建传输链的那一行。两个 PR 中先合入的那个不受影响;后合入的那个要在同一处把chain_hops(...)和resume合在一起,冲突很小。验证
cargo fmt --allcargo clippy -p lithe-remote -p lithe-workspace -p lithe-core --all-targets --features lithe-workspace/test-support -- -D warningscargo test -p lithe-remote -p lithe-workspace --features lithe-workspace/test-support:remote 142 个、workspace 79 个,全部通过。新增的测试覆盖:exec进入,且不带 AskPass 变量;unreachable,并带引擎提示;failed;--exec后面;name只读文件夹内的文件,按带注释和尾逗号的 JSON 读取;config不在列表里时拒绝;请求只接受规定的字段;up的参数:文件夹、配置,以及 Podman 的--docker-path放在up之后;inspect的名字去掉前导/;没有名字时用 id;runtime_missing;成功但容器不可用是parse_failed;truncated,之后的行不再发送。cargo test -p lithe-core --lib:890 个全部通过,包括在主机上列出容器、拒绝请求,以及 devcontainer 的拒绝规则。ac80382(b92fcaf)。冲突在三个文件:sessions.rs:保留 feat(remote): keep or end host programs when a connection closes (Refs 1lck/Lithe-IDEA#2231) #160 的常驻会话恢复重试,并接上本 PR 的跳点链(chain_hops);连接日志仍按log_kind(container)区分容器。probe/tests.rs和tests/remote_sessions.rs:两边新加的测试都保留。cargo clippy -p lithe-workspace -p lithe-core -p lithe-remote --all-targets --all-features -- -D warnings没有警告,cargo test -p lithe-workspace -p lithe-core -p lithe-remote全部通过(lithe-core892 个)。没有用真实的 Docker、Podman 或 Dev Container CLI 运行过;测试用的是替身程序。