oxdock-ssh-plugin 0.27.1-alpha

Ephemeral user-space SSH server and client for OxDock scripts, bridged to stdio pipelines through the Engine facade.
Documentation

oxdock-ssh-plugin

Ephemeral user-space SSH server and client for OxDock scripts, bridged to stdio pipelines through the Engine facade.

Part of the OxDock workspace.

Overview

The SSH module spins up ephemeral loopback SSH servers with in-memory credentials, dequeues each session with its metadata, and pumps bytes through explicit DSL pipes. Import it with IMPORT [STD, SSH] in a runner that registers the host module on the Engine facade. The function reference below lists every entry with signatures; the type reference lists the session and server handles.

Functions

Callable as MODULE::NAME(...) in expressions (or bare NAME(...) with the module imported via IMPORT). Introspectable from scripts with FUNCTIONS() and DESCRIBE(name).

SSH_ACCEPT

Accept one SSH session into pipes.

Accept the next authenticated session and pump it through explicit pipes until the channel closes. Must run inside ASYNC. Returns a MAP with closed (BOOL) and command (STRING, empty for shells). Thin wrapper over SSH_DEQUEUE + SSH_PUMP_CHANNEL for worker loops that need no pre-pump inspection; use those directly to route on session metadata first. Returns a MAP with closed (BOOL) and command (STRING, empty for shells); the example below asserts both keys on the awaited result. The server sends first: the client side never EOFs its input, so the reply cannot race teardown.

Signature: SSH_ACCEPT($server: SSH_SERVER, $in_pipe: PIPE, $out_pipe: PIPE) -> MAP<closed: BOOL, command: STRING>

Contexts: AST only

Parameters:

  • $server (SSH_SERVER): Server handle from SSH_SERVE.
  • $in_pipe (PIPE): Pipe carrying bytes consumed by the wire side.
  • $out_pipe (PIPE): Pipe carrying bytes produced by the wire side.

Returns: MAP<closed: BOOL, command: STRING>

  • closed (BOOL): True when the session closed cleanly.
  • command (STRING): Executed command, empty for shells.

Examples:

Example: accept and close

IMPORT [STD, SSH]
LET $m: SSH_SERVE_INFO = SSH_SERVE("doc-ssh-accept", {username: "u", password: "p"})
LET $in: PIPE
LET $out: PIPE
LET $acc: HANDLE = ASYNC { SSH_ACCEPT($m.server, $in, $out) }
LET $cin: PIPE
LET $cout: PIPE
LET $c: HANDLE = ASYNC { SSH_CONNECT("doc-ssh-accept", "u", "p", $cin, $cout) }

# Close both inputs: each pump observes EOF, half-closes, and both
# tasks return their result maps with no cancellation.
WITH_IO [stdout=$cin] ECHO "client-hello"
WITH_IO [stdout=$in] ECHO "server-greeting"
LET $done: MAP<ANY> = AWAIT $acc
ASSERT_EQ $done.closed true
ASSERT_CONTAINS $done "command"
LET $cDone: MAP<ANY> = AWAIT $c
ASSERT_EQ $cDone.closed true
SSH_CLOSE($m.server)

SSH_CLOSE

Shut down an SSH server.

Shut a server down and join its runtime thread (bounded). Idempotent: returns BOOL true when no thread remains.

Signature: SSH_CLOSE($server: SSH_SERVER) -> BOOL

Contexts: AST only

Parameters:

  • $server (SSH_SERVER): Server handle from SSH_SERVE.

Returns: BOOL

Examples:

Example: shut down

IMPORT [STD, SSH]
LET $m: SSH_SERVE_INFO = SSH_SERVE("doc-ssh-close", {username: "u", password: "p"})
LET $ok: BOOL = SSH_CLOSE($m.server)
ASSERT_EQ $ok true

SSH_CONNECT

Open an SSH client session into pipes.

Connect to an SSH server with distinct inner credentials and pump the shell channel through explicit pipes until it closes. Must run inside ASYNC, concurrently with the SSH_PUMP tasks (never before them). target is a service name resolved through runner mappings: SSH needs a TCP socket, so unmapped services bail with the CLI mapping hint. Under --offline the dial bails before any socket work. Returns a MAP with closed (BOOL).

Signature: SSH_CONNECT($target: STRING, $username: STRING, $password: STRING, $in_pipe: PIPE, $out_pipe: PIPE) -> MAP

Contexts: AST only

Parameters:

  • $target (STRING): Dial target: a service name resolved through runner mappings.
  • $username (STRING): Inner credentials for the SSH session.
  • $password (STRING): Inner credentials for the SSH session.
  • $in_pipe (PIPE): Pipe carrying bytes consumed by the wire side.
  • $out_pipe (PIPE): Pipe carrying bytes produced by the wire side.

Returns: MAP

Examples:

Example: connect and close

IMPORT [STD, SSH]
LET $m: SSH_SERVE_INFO = SSH_SERVE("doc-ssh-connect", {username: "u", password: "p"})
LET $in: PIPE
LET $out: PIPE
LET $acc: HANDLE = ASYNC { SSH_ACCEPT($m.server, $in, $out) }
LET $cin: PIPE
LET $cout: PIPE
LET $c: HANDLE = ASYNC { SSH_CONNECT("doc-ssh-connect", "u", "p", $cin, $cout) }

# Close both inputs: each pump observes EOF, half-closes, and both
# tasks return their result maps with no cancellation.
WITH_IO [stdout=$cin] ECHO "client-hello"
WITH_IO [stdout=$in] ECHO "server-greeting"
LET $done: MAP<ANY> = AWAIT $c
ASSERT_EQ $done.closed true
LET $adone: MAP<ANY> = AWAIT $acc
ASSERT_EQ $adone.closed true
SSH_CLOSE($m.server)

SSH_DEQUEUE

Dequeue one SSH session with its metadata.

Dequeue one authenticated session and expose its metadata before any byte pumping starts, so scripts can route on the requested command or client identity. Must run inside ASYNC. Returns a MAP with session (SSH_SESSION), command (STRING, empty for shells), username and addr (STRINGs, empty when unknown).

Routing shape: compare the dequeued command against known commands, build a fresh pipe pair per session, and pump a synthetic reply with SSH_PUMP_CHANNEL. The server sends first: the client side never EOFs its input, so the reply cannot race teardown.

Signature: SSH_DEQUEUE($server: SSH_SERVER) -> MAP<session: SSH_SESSION, command: STRING, username: STRING, addr: STRING>

Contexts: AST only

Parameters:

  • $server (SSH_SERVER): Server handle from SSH_SERVE.

Returns: MAP<session: SSH_SESSION, command: STRING, username: STRING, addr: STRING>

  • session (SSH_SESSION): Session handle for SSH_PUMP_CHANNEL (pump ends take once).
  • command (STRING): Executed command, empty for shells.
  • username (STRING): Authenticated username, empty when unset.
  • addr (STRING): Peer address, empty when unset.

Examples:

Example: route one session

IMPORT [STD, SSH]
LET $m: SSH_SERVE_INFO = SSH_SERVE("doc-ssh-route", {username: "u", password: "p"})

# Serve: dequeue one session and pump a synthetic reply.
LET $in: PIPE
LET $out: PIPE
LET $w: HANDLE = ASYNC {
    LET $sess: SSH_DEQUEUE_INFO = SSH_DEQUEUE($m.server)
    ASSERT_EQ $sess.username "u"
    SSH_PUMP_CHANNEL($sess.session, $in, $out)
}

# Connect a client to the server.
LET $cin: PIPE
LET $cout: PIPE
LET $c: HANDLE = ASYNC { SSH_CONNECT("doc-ssh-route", "u", "p", $cin, $cout) }

# Close both inputs: each pump observes EOF, half-closes, and every
# task returns with no cancellation.
WITH_IO [stdout=$cin] ECHO "client-hello"
WITH_IO [stdout=$in] ECHO "server-greeting"
LET $cDone: MAP<ANY> = AWAIT $c
ASSERT_EQ $cDone.closed true
AWAIT $w
SSH_CLOSE($m.server)

SSH_PTY_RUN

Run a command under a sized local terminal into pipes.

Run argv under a local pseudo-terminal sized from the dequeued session and pump it through explicit pipes until the child exits. rows/cols seed the initial size when positive; non-positive falls back to the session's requested size (the outer pty request, 24x80 default). Outer window-change requests resize this session's terminal live; every session owns its size cell, so concurrent guests never observe each other. Must run inside ASYNC. Returns the INT exit code. Environment is inherited from the host process and layered with the script environment like RUN: block-scoped ENV (such as the session's SSH_USER / SSH_CLIENT / SSH_SERVER / SSH_COMMAND relay) reaches the child; the working directory comes from the script.

Signature: SSH_PTY_RUN($session: SSH_SESSION, $argv: LIST<STRING>, $rows: INT, $cols: INT, $in_pipe: PIPE, $out_pipe: PIPE) -> INT

Contexts: AST only

Parameters:

  • $session (SSH_SESSION): Session handle from SSH_DEQUEUE.
  • $argv (LIST<STRING>): Program and arguments to run.
  • $rows (INT): Terminal rows when positive, session size otherwise.
  • $cols (INT): Terminal columns when positive, session size otherwise.
  • $in_pipe (PIPE): Pipe carrying bytes consumed by the child.
  • $out_pipe (PIPE): Pipe carrying bytes produced by the child.

Returns: INT

Examples:

Example: run under a terminal

IMPORT [STD, SSH]
LET $m: SSH_SERVE_INFO = SSH_SERVE("doc-ssh-pty", {username: "u", password: "p"})
LET $d: HANDLE = ASYNC { SSH_DEQUEUE($m.server) }
LET $cin: PIPE
LET $cout: PIPE
LET $c: HANDLE = ASYNC { SSH_CONNECT("doc-ssh-pty", "u", "p", $cin, $cout) }
WITH_IO [stdout=$cin] ECHO "client-hello"
LET $sess: SSH_DEQUEUE_INFO = AWAIT $d

# The client task only proved the session live: cancel it before the
# terminal run, so no two pumps share the session.
CANCEL $c
LET $pin: PIPE
LET $pout: PIPE
# Assert on the whole pipe: terminal streams carry setup noise ahead
# of child output, so line reads are version-dependent.
[family:unix] {
    LET $t: HANDLE = ASYNC { SSH_PTY_RUN($sess.session, ["sh", "-c", "echo pty-hi"], 0, 0, $pin, $pout) }
    LET $code: INT = AWAIT $t
    ASSERT_EQ $code 0
    ASSERT_CONTAINS $pout "pty-hi"
}
[family:windows] {
    LET $t: HANDLE = ASYNC { SSH_PTY_RUN($sess.session, ["cmd", "/c", "echo pty-hi"], 0, 0, $pin, $pout) }
    LET $code: INT = AWAIT $t
    ASSERT_EQ $code 0
    ASSERT_CONTAINS $pout "pty-hi"
}
SSH_CLOSE($m.server)

SSH_PUMP

Copy one pipe into another until EOF.

Copy one pipe into another until EOF, then close the target. Returns the INT byte count. Either task placement works, as long as the other end is live (usually an ASYNC task).

Signature: SSH_PUMP($from_pipe: PIPE, $to_pipe: PIPE) -> INT

Contexts: AST only

Parameters:

  • $from_pipe (PIPE): Source pipe to drain.
  • $to_pipe (PIPE): Target pipe receiving the bytes (closed at EOF).

Returns: INT

Examples:

Example: copy

# Closing the source ends the copy: the count proves the bytes moved.
IMPORT [STD, SSH]
LET $in: PIPE
WITH_IO [stdout=$in] ECHO "pump-me"
LET $out: PIPE
LET $n: INT = SSH_PUMP($in, $out)
ASSERT_EQ $n 8
WITH_IO [stdin=$out] READ_LINE $got
ASSERT_EQ $got "pump-me"

SSH_PUMP_CHANNEL

Pump a dequeued SSH session through pipes.

Pump a dequeued session between explicit DSL pipes until the channel closes. Must run inside ASYNC. The session ends are take-once: a second pump on the same session bails instead of splitting bytes. Returns a MAP with closed (BOOL).

Signature: SSH_PUMP_CHANNEL($session: SSH_SESSION, $in_pipe: PIPE, $out_pipe: PIPE) -> MAP<closed: BOOL>

Contexts: AST only

Parameters:

  • $session (SSH_SESSION): Session handle from SSH_DEQUEUE (pump ends take once).
  • $in_pipe (PIPE): Pipe carrying bytes consumed by the wire side.
  • $out_pipe (PIPE): Pipe carrying bytes produced by the wire side.

Returns: MAP<closed: BOOL>

  • closed (BOOL): True when the session closed cleanly.

Examples:

Example: pump a session

IMPORT [STD, SSH]
LET $m: SSH_SERVE_INFO = SSH_SERVE("doc-ssh-pump", {username: "u", password: "p"})
LET $d: HANDLE = ASYNC { SSH_DEQUEUE($m.server) }
LET $cin: PIPE
LET $cout: PIPE
LET $c: HANDLE = ASYNC { SSH_CONNECT("doc-ssh-pump", "u", "p", $cin, $cout) }
WITH_IO [stdout=$cin] ECHO "client-hello"
LET $sess: SSH_DEQUEUE_INFO = AWAIT $d
LET $sin: PIPE
LET $sout: PIPE
LET $p: HANDLE = ASYNC { SSH_PUMP_CHANNEL($sess.session, $sin, $sout) }
WITH_IO [stdout=$sin] ECHO "server-greeting"
LET $cDone: MAP<ANY> = AWAIT $c
ASSERT_EQ $cDone.closed true
LET $pDone: MAP<ANY> = AWAIT $p
ASSERT_EQ $pDone.closed true
SSH_CLOSE($m.server)

SSH_SERVE

Serve SSH on a virtual service endpoint.

Spin up an SSH server on a runner-mapped service with the given credentials. bind is a service name; the runner maps it with -p/--listen, and unmapped services bail with the mapping hint (SSH needs a TCP socket, so there is no memory fallback). options is a MAP with required username/password STRINGs (a server without credentials is meaningless, so blanks bail) and the optional key_path STRING (workspace-relative OpenSSH Ed25519 file, load-or-create; a leading / anchors to the workspace root like WRITE, and escapes still bail; absent or blank keeps the ephemeral in-memory key). Non-blocking: returns a MAP with server (SSH_SERVER), addr (STRING: the physical bind, or the virtual endpoint echo when socketless), username and password (STRINGs), and virtual (STRING echo). Unmapped and memory services bail with the CLI mapping hint: SSH needs a TCP socket, so map the name with -p/--listen.

Signature: SSH_SERVE($bind: STRING, $options: MAP<username: STRING, password: STRING, key_path?: STRING>) -> MAP<server: SSH_SERVER, addr: STRING, username: STRING, password: STRING, virtual: STRING>

Contexts: AST only

Parameters:

  • $bind (STRING): Service name to serve; the runner maps it with -p/--listen.
  • $options (MAP<username: STRING, password: STRING, key_path?: STRING>): Server credentials and host key.
    • username (STRING, required)
    • password (STRING, required)
    • key_path (STRING, optional)

Returns: MAP<server: SSH_SERVER, addr: STRING, username: STRING, password: STRING, virtual: STRING>

  • server (SSH_SERVER): Server handle for SSH_ACCEPT, SSH_DEQUEUE, and SSH_CLOSE.
  • addr (STRING): Bound socket address (ip:port) of the server.
  • username (STRING): Configured username.
  • password (STRING): Configured password.
  • virtual (STRING): Virtual endpoint echo of the claimed slot.

Examples:

Example: serve and close

IMPORT [STD, SSH]
LET $m: SSH_SERVE_INFO = SSH_SERVE("doc-ssh-serve", {username: "u", password: "p"})
ASSERT_EQ $m.virtual "doc-ssh-serve"
SSH_CLOSE($m.server)

Value types

Value type: SSH_SERVER

Handle to one ephemeral SSH server instance.

Cloning the value shares the server; dropping the last clone signals shutdown.

Minted by SSH_SERVE; used by SSH_ACCEPT, SSH_CLOSE, SSH_DEQUEUE.

Value type: SSH_SESSION

Handle to one dequeued SSH session instance.

Cloning the value shares the session; metadata reads never consume. Display shows id and peer only: the command string may carry secrets.

Minted by SSH_DEQUEUE; used by SSH_PTY_RUN, SSH_PUMP_CHANNEL.

Contexts

Every function lists the contexts it runs in. AST is always available. RPN additionally requires a pure function or an explicit rpn opt in.

Context: AST

The parsed tree of a script, walked one step at a time. Statements, declarations, scopes, pipes, and IO live here. Every step knows its line number, so failures name it. Every function runs in this context.

Context: RPN

Arithmetic and comparison expressions compiled to a flat stack program of values with no statements, scopes, pipes, or step numbers. Pure functions run here with no flag. Stateful functions run here only when they opt in with #[oxdock_func(rpn)], which is reserved for read only queries that stay meaningful inside math (GLOB, LOAD_TOML, LOAD_JSON do this). A call that fails inside math reports the bare error with no step number.

License

oxdock-ssh-plugin is distributed under the terms of the Apache License (Version 2.0).