Skip to content

Support one-shot guest exec through Proxmox termproxy - #546

Open
runleveldev wants to merge 11 commits into
mainfrom
feat/guest-exec-termproxy
Open

runleveldev wants to merge 11 commits into
mainfrom
feat/guest-exec-termproxy

Conversation

@runleveldev

@runleveldev runleveldev commented Dec 4, 2026 •

Copy link
Copy Markdown
Collaborator

Part of #541. Replaces #543.

Owners and admins can run one command as root in a running Proxmox container with mieweb exec. Manager uses only the node's existing Proxmox API (termproxy + vncwebsocket). Nothing is installed on the Proxmox hosts or in the guest, Manager needs no SSH access to the nodes, and the guest needs no SSH. The design follows the review on #543.

How it works

With cmode: shell, the Proxmox console of a container is a root shell (lxc-attach) with no login prompt.

  1. Manager checks the guest's live hostname and running state through the API.
  2. It opens the console with the node's credentials.
  3. It types a bootstrap line that puts the guest terminal into raw mode and switches to a non-interactive sh -s.
  4. It sends a POSIX wrapper that runs the quoted argv with </dev/null, sends stdout and stderr to files in a private temp directory, and enforces a 30-second limit. The wrapper is one brace group, so the shell runs nothing until all of it has arrived. A timer started by the bootstrap line kills the shell if a complete wrapper doesn't arrive within 10 seconds.
  5. The wrapper prints the exit code and both streams (hex-encoded with od) between markers that contain a per-request nonce.

Changes

  • First commit: brings over the parts of Support one-shot guest exec through the Manager API #543 that don't depend on how the command reaches the guest, co-authored by @horner:
    • the POST /api/v1/sites/{siteId}/containers/{id}/exec OpenAPI contract
    • the owner/admin route, with argument limits, error mapping and audit logging
    • the route tests
    • the OS provider exec verb and its tests
  • New utils/guest-exec.js: the console client, bootstrap, wrapper and result parser.
  • New ProxmoxApi methods: lxcTermproxy() and openLxcTermSocket(), plus a current option on lxcConfig().
  • New ws dependency: Node's built-in WebSocket can't use the node's TLS agent or auth headers.
  • Console mode: containers get cmode: shell at create and reconfigure. Proxmox applies a cmode change to a running container only after it restarts, so the first exec on an older container sets it and returns exec_restart_required (409).
  • Root by default: commands run as guest root. There is no UID 1000 default and no root grants.
  • Background processes: when the command returns, plain & jobs are hung up. Processes started with setsid, nohup or a service manager keep running.
  • One exec per container at a time: returns exec_busy (409), because Proxmox shares one dtach console session per container. The claim is a 150-second lease on the Container row (new nullable execLeaseUntil column, set with a conditional update), so it holds across load-balanced Manager processes and expires on its own if a process dies mid-exec. If the outcome is unknown, the lease is kept until it expires, because the command may still be running in the shared session. executeGuest won't write to the console unless the lease outlasts the remaining deadlines.
  • Bounded setup: Proxmox requests (including the ticket login, via a new timeout option on Node#api), the WebSocket handshake and termproxy's OK each time out after 10 s. After the result, Manager waits up to 5 s for the console to close before releasing the lease.
  • Command environment: the command runs in the foreground, so it, and any daemon it starts, keeps default SIGINT/SIGQUIT handling. It also keeps the shell's umask. Output is copied before it is measured, so background jobs still writing can't skew the result.
  • Error codes: exec_forbidden is removed.
  • CI: the os-cloud-provider workflow now runs on any change under create-a-container/** except the React client, instead of a growing list of files.
  • Admin docs: new docs/admins/guest-exec.md covering the required privileges (VM.Audit, VM.Console, VM.Config.Options), console mode, restarting older containers, limits, and the shared console session.

Testing

Real Proxmox (compose stack, PVE 9.2.20). The test guest was created through Manager from ghcr.io/mieweb/opensource-server/base:latest.

  • It got cmode: shell at create, and the first exec worked without a restart.
  • id; uname -n; printf err >&2; exit 23 returned root, the hostname, err on stderr and exit 23, in about 0.5 s.
  • Arguments arrived byte-exact: one containing every byte 0x01–0x7f (including ^\, ^U, ^C, ^D), a 10 KB argument, quotes, unicode and CR/LF.
  • All 256 byte values came back on stdout. 70,000 bytes were truncated to 65,536 with the flag set. A missing command gave exit 127, and kill -TERM $$ gave 143.
  • sleep & was hung up, while setsid sleep & and nohup sleep & kept running. No timeout watcher, temp directory or console session was left behind.
  • A second exec while one was running got exec_busy (with the earlier in-memory lock; the lease that replaced it was tested on SQLite and Postgres). sleep 45 got exec_timeout after 30.5 s.
  • A stopped guest got guest_not_running. A running guest in tty mode got exec_restart_required with cmode: shell pending, and worked after a reboot.
  • A sh -s left in the shared console session by an interrupted exec was recovered on the next exec.
  • An API token limited to VM.Audit, VM.Console and VM.Config.Options worked through vncwebsocket, including the restart-required path.

The real-Proxmox results above were taken before the review hardening (brace-group wrapper, bootstrap timer, foreground command, stream copies). Those changes were checked through a real terminal with interactive bash and dash, but not yet re-run on Proxmox.

Automated tests

  • utils/__tests__/guest-exec.test.js runs the wrapper through a real sh (bash, and dash in node:24-bookworm-slim). It covers literal argv, binary streams, truncation, exit codes and background-job cleanup.
  • The same file runs executeGuest against a fake termproxy server that frames input and turns \n into \r\n like the real console. It covers the success path plus the hostname, stopped, restart-required, busy, dropped-connection and refused-ticket cases.
  • Route tests cover a concurrent exec that sees the lease in the database, keeping the lease on an unknown outcome, releasing it after any other failure, and reclaiming a lease left by a dead process.
  • Wrapper tests also cover: a script cut off mid-delivery runs nothing; a shell that never receives a wrapper kills itself; output stays consistent while a background job keeps writing; the time limit kills a long-running command (exit 137, partial output kept); the command keeps the shell's umask and default SIGINT/SIGQUIT handling. The lease migration and claim/release were also checked against Postgres 17.
  • Full create-a-container suite: 325/325 tests pass, excluding mcp-proxy and agents.checkin, which also fail on main. The OS provider typecheck passes, and all 126 provider tests pass.

Diff stats

Against main, including comments. Lines are shown as +added −removed.

Component Implementation Tests Docs Total
Manager (create-a-container) +607 −8 +428 −1 +1035 −9
OS provider (packages/os-cloud-provider) +77 −5 +92 −0 +13 −0 +182 −5
Docs site (mie-opensource-landing) +51 −1 +51 −1
CI (.github/workflows) +9 −11 +9 −11
package-lock.json (generated) +22 −0
Total +693 −24 +520 −1 +64 −1 +1299 −26

Manager breakdown:

Part Files Implementation Tests
Console backend utils/guest-exec.js, utils/proxmox-api.js +381 −2 +251 −0 (guest-exec.test.js)
API route and contract routers/api/v1/containers.js, openapi.v1.yaml +157 −0 +169 −0 (containers.exec.test.js)
Exec lease models/container.js, migration +56 −1 (in containers.exec.test.js)
Proxmox request timeout models/node.js +7 −3
Console mode at create utils/container-state.js +5 −2 +8 −1 (container-state.test.js)
ws dependency package.json +1 −0

Known limitations

  • Shared console session: if someone has the container's Proxmox web console open during an exec, they see the session and their typing reaches it. This is documented.
  • Passwordless root: with cmode: shell, any Proxmox user with VM.Console on the container gets a root shell from the web console without logging in.
  • Guest tools: the guest needs sh, stty, mkdir, sleep, cat, wc, dd and od. Images with no shell (distroless) are unsupported.
  • No disk cap while the command runs: output is truncated only after the command finishes.

The CLI companion, mieweb/cloud#30, is already merged and doesn't depend on specific error codes.

@runleveldev
runleveldev marked this pull request as ready for review December 4, 2026 19:14
Copilot AI balanced review requested due to automatic review settings December 4, 2026 19:14

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Unbounded WebSocket handshakes can leave guests permanently busy, partial script delivery can bypass execution timeouts, and output races can misclassify successful commands.

3 open issues
1 file not reviewed
  • create-a-container/package-lock.json: Generated file
What changed in this PR

Adds owner/admin one-shot guest execution through the Proxmox console API, enabling mieweb exec without SSH access or host-installed runners.

Changes:

  • Adds the authenticated Manager endpoint and OS provider integration.
  • Implements console transport, command wrapping, output parsing, and shell-mode configuration.
  • Adds tests, CI coverage, and administrator documentation.
File Description
packages/​os-cloud-provider/​test/​provider.test.ts Tests exec requests, results, and errors.
packages/​os-cloud-provider/​test/​exec-guest.e2e.mjs Adds CLI verification against real guests.
packages/​os-cloud-provider/​src/​index.ts Exposes the exec capability.
packages/​os-cloud-provider/​src/​exec.ts Implements Manager-backed execution.
packages/​os-cloud-provider/​src/​deploy.ts Exports the shared client factory.
packages/​os-cloud-provider/​src/​client.ts Preserves exec permission errors.
packages/​os-cloud-provider/​README.md Documents CLI usage.
mie-opensource-landing/​zensical.toml Links administrator documentation.
mie-opensource-landing/​docs/​admins/​guest-exec.md Documents requirements, limits, and console risks.
create-a-container/​utils/​proxmox-api.js Adds console API and WebSocket methods.
create-a-container/​utils/​guest-exec.js Implements guest execution and result parsing.
create-a-container/​utils/​container-state.js Configures shell console mode.
create-a-container/​utils/​__tests__/​guest-exec.test.js Tests wrappers and console execution.
create-a-container/​utils/​__tests__/​container-state.test.js Tests shell-mode configuration.
create-a-container/​routers/​api/​v1/​containers.js Adds authorization, validation, and auditing.
create-a-container/​routers/​api/​v1/​__tests__/​containers.exec.test.js Tests endpoint access and responses.
create-a-container/​package.json Adds the WebSocket dependency.
create-a-container/​package-lock.json Locks the WebSocket dependency.
create-a-container/​openapi.v1.yaml Defines the exec API contract.
.github/​workflows/​os-cloud-provider.yml Includes exec changes and tests in CI.

Review effort: Balanced

Comment thread create-a-container/utils/guest-exec.js Resolved
Comment thread create-a-container/utils/proxmox-api.js Resolved
Comment thread create-a-container/utils/guest-exec.js Resolved
Comment thread create-a-container/utils/guest-exec.js Outdated
const INPUT_CHUNK = 4096;

// Proxmox runs one shared `dtach` console session per guest, so commands on
// the same guest would interleave. This only serializes within one process.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a strong limitation. Is there a mitigation for multiple load balanced processes?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in a48b465. The lock now lives in the database instead of process memory, so it works across load-balanced Manager processes.

  • How it works: a new nullable Containers.execLeaseUntil column, plus Container#claimExecLease / releaseExecLease. A claim is a conditional UPDATE ... WHERE execLeaseUntil IS NULL OR execLeaseUntil < now. Only one process can win it, and the loser gets exec_busy (409).
  • Crash recovery: the lease lasts 2 minutes, longer than the exec's own deadlines. A process that dies mid-exec only blocks the container until the lease expires.
  • Release: it matches on the exact expiry the claim set, so a slow process can't clear a lease that a later exec has since taken.
  • Where it moved: the route now claims and releases the lease. executeGuest no longer tracks busy guests.

I tested it against both SQLite and Postgres (migration, claim, busy, release, reclaiming an expired lease, and ignoring a release with the wrong expiry). The route tests now cover a concurrent exec that sees the lease through the database.

Comment thread .github/workflows/os-cloud-provider.yml Outdated
- 'create-a-container/routers/api/v1/__tests__/containers.exec.test.js'
- 'create-a-container/utils/guest-exec.js'
- 'create-a-container/utils/__tests__/guest-exec.test.js'
- 'create-a-container/utils/proxmox-api.js'

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems this is just gonna grow. At what point do we stop caring about path restrictions?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, we stop now. As of a48b465 the workflow runs on any change under create-a-container/** except create-a-container/client/**, so the list no longer grows. The React client can't affect the API the provider calls. The job takes under a minute.

Copilot AI balanced review requested due to automatic review settings December 4, 2026 19:54

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Incomplete watchdog delivery and unsafe lease handling can leave commands running while another request gains access to the shared console.

6 open issues
1 file not reviewed
  • create-a-container/package-lock.json: Generated file

Review effort: Balanced

Comment thread create-a-container/utils/guest-exec.js Resolved
runleveldev added a commit that referenced this pull request Dec 4, 2026
Addresses the Copilot review on #546.

* The wrapper is one brace group, so the shell runs nothing until it has
  read the whole script. A connection that drops mid-script can no
  longer start the command without its time limit. The bootstrap line
  starts a timer that kills the shell unless a complete wrapper cancels
  it within 10 seconds, so a half-received script can't hold the
  shared console session.
* Each output stream is copied (up to 64 KiB + 1 byte) before it is
  measured and encoded, so background jobs that are still writing can't
  make the reported size disagree with the bytes sent.
* Bound every step before dispatch: Proxmox requests (including the
  ticket login) and the WebSocket handshake time out after 10 s.
  executeGuest takes the lease expiry and refuses to open the console,
  or to send the wrapper, unless the lease outlasts the remaining
  deadlines. The lease is now 150 s to cover them.
* Keep the lease when the outcome is unknown, since the command may
  still be running in the shared session. It expires after the
  guest-side limits have stopped the command.
* The route test mock keeps the real EXEC_LEASE_SECONDS.
Copilot AI balanced review requested due to automatic review settings December 4, 2026 21:51

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Human assessment is needed because the inherited restrictive umask can make command-created files and directories inaccessible to non-root applications.

No open issues 🎉

6 resolved since last review
1 file not reviewed
  • create-a-container/package-lock.json: Generated file

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings December 4, 2026 21:58

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Human assessment is needed because lease release may precede session teardown, allowing subsequent executions to attach to a dying console and fail.

No open issues 🎉

1 file not reviewed
  • create-a-container/package-lock.json: Generated file

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings December 4, 2026 22:05

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Enabling cmode:shell grants existing VM.Console holders passwordless guest root, requiring human confirmation of deployment ACLs and the trust-boundary change that repository context cannot establish.

No open issues 🎉

1 file not reviewed
  • create-a-container/package-lock.json: Generated file

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings December 4, 2026 22:12

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Human assessment is needed because the wrapper alters SIGINT/SIGQUIT handling, potentially suppressing cancellation and masking signal-related exit statuses.

No open issues 🎉

1 file not reviewed
  • create-a-container/package-lock.json: Generated file

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings December 4, 2026 22:23

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Human assessment is needed because detached processes may retain deleted capture files, allowing continued logging to exhaust guest disk space.

No open issues 🎉

1 file not reviewed
  • create-a-container/package-lock.json: Generated file

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings December 4, 2026 22:30

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Human assessment is needed because an undocumented watchdog dependency and missing timeout coverage leave the 30-second execution limit unreliable.

No open issues 🎉

1 file not reviewed
  • create-a-container/package-lock.json: Generated file

Review effort: Balanced

Copilot AI balanced review requested due to automatic review settings December 4, 2026 22:38

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues remain for the guest-execution changes.

No open issues 🎉

1 file not reviewed
  • create-a-container/package-lock.json: Generated file

Review effort: Balanced

runleveldev and others added 11 commits December 4, 2026 19:09
Carry over the parts of #543 that don't depend on how a command reaches
the guest. These are the exec_container OpenAPI contract, the
owner/admin route with its argument limits, error mapping and audit
logging, the route tests, the OS provider exec verb with its tests,
and the CI triggers.

The SSH node runner, its installer and fixtures, and the setup docs
are intentionally left behind (see the review on #543). The route
still imports utils/guest-exec. A Proxmox termproxy backend replaces
it in a follow-up commit.

Co-authored-by: Doug Horner <horner@mieweb.com>
Replace the SSH node runner from #543 with the Proxmox API alone.
Nothing is installed on the Proxmox hosts and Manager needs no SSH
access to them.

With cmode: shell, the console that termproxy opens for a container is
a root shell with no login. Manager connects to it with the existing
node credentials over vncwebsocket and types a bootstrap line that
puts the terminal into raw mode and switches to a non-interactive
sh -s. It then sends a POSIX wrapper that runs the quoted argv with
stdout and stderr redirected to files and a 30-second limit, and
prints the exit code and hex-encoded streams between markers that
contain a per-request nonce.

* Containers get cmode: shell at create and reconfigure. On a running
  container the change waits for a restart, so the first exec on an
  older container sets it and returns exec_restart_required.
* Commands run as guest root. The UID 1000 default and root grants are
  gone.
* When the command returns, the shell exits, so plain background jobs
  get SIGHUP. Processes started with setsid or nohup keep running.
* Only one exec runs per container at a time (exec_busy), because
  Proxmox shares a single dtach console session per container.
* The live hostname and running state are checked through the API
  before the console is opened.

Adds ws, because the WebSocket built into Node can't take the node's
TLS agent or auth headers.
Running guest exec against the Proxmox in the compose stack turned up
four problems that the fake termproxy did not show.

* lxc-attach's host-side terminal keeps output processing on, so every
  \n arrives as \r\n and the ready marker never matched. Drop \r from
  console output. Markers and hex never contain it.
* dash treats `sh -s` on a terminal as interactive and gives background
  jobs their own process group, so they never got SIGHUP and the
  timeout watcher outlived the exec. Turn job control off and hang up
  the shell's own group at the end, which gives SSH-like cleanup in any
  shell.
* Input typed before the login shell starts is discarded. Wait for
  output other than termproxy's OK and dtach's clear-screen, and start
  the bootstrap line with ^U to clear any partial line.
* A `sh -s` left in the shared session by an interrupted exec only
  runs lines that end in LF. End the bootstrap line with CR LF, with a
  trailing `:` to absorb the CR.

The fake termproxy now translates \n to \r\n like the real console, and
a new test covers background job cleanup. The admin docs list the
privileges confirmed with an API token: VM.Audit, VM.Console and
VM.Config.Options.
Commands on one container share a single Proxmox console session, so
only one exec may run per container at a time. The in-memory set only
enforced that within one Manager process. Replace it with a lease on the
Container row (execLeaseUntil, set by a conditional update), so the
claim holds across load-balanced Manager processes. The lease expires
after 2 minutes, so a process that dies mid-exec doesn't block the
container for good.

The os-cloud-provider workflow listed more create-a-container files
with each change. Trigger it on any server change instead, excluding
the React client.
Addresses the Copilot review on #546.

* The wrapper is one brace group, so the shell runs nothing until it has
  read the whole script. A connection that drops mid-script can no
  longer start the command without its time limit. The bootstrap line
  starts a timer that kills the shell unless a complete wrapper cancels
  it within 10 seconds, so a half-received script can't hold the
  shared console session.
* Each output stream is copied (up to 64 KiB + 1 byte) before it is
  measured and encoded, so background jobs that are still writing can't
  make the reported size disagree with the bytes sent.
* Bound every step before dispatch: Proxmox requests (including the
  ticket login) and the WebSocket handshake time out after 10 s.
  executeGuest takes the lease expiry and refuses to open the console,
  or to send the wrapper, unless the lease outlasts the remaining
  deadlines. The lease is now 150 s to cover them.
* Keep the lease when the outcome is unknown, since the command may
  still be running in the shared session. It expires after the
  guest-side limits have stopped the command.
* The route test mock keeps the real EXEC_LEASE_SECONDS.
The wrapper set umask 077 to make its scratch directory private, and the
command inherited it, so files a command created (an installed config, a
web root) were unreadable to non-root services. Create the directory with
mkdir -m 700 instead and leave the umask alone.
The wrapper exits after printing its result, which ends the shared dtach
session, but the lease was released as soon as the result was parsed. An
exec admitted in that gap could attach to the dying session and fail. Wait
up to 5 s for termproxy to close the console first.
CodeQL flagged String#replace with a string pattern, which removes only
the first match.
Shells start background jobs with SIGINT and SIGQUIT ignored, and the
command ran as one, so a daemon it started kept ignoring them. Run the
command in the foreground through a small sh -c that records its pid,
and let the background time limit kill that pid.
The time limit can be shortened in wrapperScript for a test that checks
a command running past it is killed with exit 137, the timed-out flag
set, and its partial output kept. The watchdog reads the command's pid
with cat, so the admin docs now list it.
@runleveldev
runleveldev force-pushed the feat/guest-exec-termproxy branch from faff729 to db9f81a Compare December 5, 2026 00:09
Copilot AI balanced review requested due to automatic review settings December 5, 2026 00:09

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The final bootstrap, watchdog, and cleanup changes need human validation through real Proxmox termproxy/dtach because existing fixtures do not establish session cleanup and lease safety in that topology.

No open issues 🎉

1 file not reviewed
  • create-a-container/package-lock.json: Generated file

Review effort: Balanced

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants