Sandbox plugin
1 note
- Plugins are part of enterprise plans. Contact support to enable them for your account. support@unikraft.com.
The sandbox plugin lets you run commands and manipulate the filesystem inside an instance, over its plugin endpoint:
- Commands: start a shell command inside the instance, inspect it, read its output, feed its standard input, wait for it, and signal it.
- Filesystem: create directories, and read, write, and upload files.
The API reference lists every request of the plugin.
Load the sandbox plugin with --plugin when you create, run, or edit an instance:
Code
The plugin takes these keys in config, all optional:
| Key | Default | Description |
|---|---|---|
persist_path | None | Directory where the plugin keeps commands and their output, so that they survive a restart |
inherit_env | false | true starts each command from the environment of the instance |
An instance with the sandbox plugin loaded accepts five unikraft commands:
| Command | Description |
|---|---|
unikraft instance exec | Runs a command on the instance, where --dir sets the working directory, --env sets the environment, and the command's exit status becomes the CLI's. |
unikraft instance shell | Opens an interactive shell that runs on your machine and sends each command to the instance, so paths resolve against the instance. |
unikraft instance copy | Copies a single file in either direction, to or from a path that you write as <instance>:<path>, or as <metro>/<instance>:<path> to pick the metro (alias: cp). |
unikraft instance write | Uploads a single local file to the instance, where --parents creates missing directories, and --append adds to the remote file instead of replacing it. |
unikraft instance read | Downloads a single file from the instance to an optional local path, by default the remote file's base name. |
Code
The commands use the plugin named sandbox by default.
Pass --plugin-name <name> to use a sandbox plugin that you loaded under a different name.
If the instance has no sandbox plugin, the commands offer to attach one and restart the instance, and --plugin-image picks the image to attach.
The offer only appears in an interactive terminal.
Elsewhere, the error says how to attach the plugin.
Terminal
From a terminal, unikraft instance exec runs the command on a pseudo-terminal on the instance.
Full-screen and interactive programs such as vim, top, less, or a password prompt then work.
The CLI puts your terminal in raw mode, and passes on its size when it changes.
-T (--no-tty) runs the command on pipes instead, and -t (--tty) fails when standard input or output isn't a terminal.
Code
In unikraft instance shell, a command gets the pseudo-terminal when it reads from and writes to the terminal, so not in a pipe or with a redirection.
ctrl-c then goes to that command, as in a local shell.
unikraft instance shell -c runs its commands on pipes.
The instance terminal of the dashboard also runs commands on a pseudo-terminal.
Environment
By default, a command sees only the environment that the request gives it.
With unikraft instance exec, that's the --env values and nothing else:
Code
The plugin still finds a program such as env through its own PATH.
The command doesn't get that PATH, so a program that it starts by name may fail.
Pass a PATH with --env in that case.
Set inherit_env to true in the plugin config to start each command from the environment of the instance.
That environment has the ENV values of the image, the values set on the instance, and a default PATH.
The request's values then add to it, and replace a variable of the same name:
Code
unikraft instance shell reads the environment of the instance when it connects, whatever the value of inherit_env.
Its --env values add to that environment.
Long-running commands
The plugin API bounds every wait.
A request that waits for a command returns after at most 55 seconds, with status 408 if the command still runs.
The API gateway ends a request that stays silent for about a minute, so the bound keeps a long command from failing there.
A client that waits over HTTP asks again after each 408.
The CLI and the Go SDK don't wait over HTTP.
They follow each command over a single WebSocket, at /commands/<command>/ws on the plugin endpoint.
The connection carries the output as the command writes it, the standard input, terminal resizes, and the exit status.
The plugin keeps an idle connection alive, so a command that writes nothing for a long time stays connected.
The JavaScript SDK waits over HTTP and repeats the request.
Shell builtins
Inside unikraft instance shell, a line that starts with : runs in the CLI instead of on the instance, so you can manage the instance you're in without leaving it:
Code
The builtins are :get, :volumes, :edit, :mount, :unmount, :start, :stop, :restart, and :suspend, and :help lists them.
The same builtins work in the instance terminal of the dashboard.
A few limitations apply:
- The instance must run on the
base-compatruntime. Use an image that ships the standard Linux utilities, such asofficial/alpine:latestorofficial/debian-slim:latest, so that more commands work in the sandbox. - The shell is experimental and has no job control, so
ctrl-z,bg, andfgdon't work. copy,read, andwritecarry one regular file at a time, and don't carry file permissions across.- Without a pseudo-terminal,
execforwards standard input only when you redirect it from a file or a pipe. - On Windows, the shell is the same POSIX-style shell as on Linux and macOS, not PowerShell.
Learn more
- Plugins: how plugins load, and how the platform authorizes requests to them.
- Sandbox plugin API reference: every request of the plugin.
- Sandboxes: run untrusted code in a hardware-isolated microVM.
- Network shield: filter the network traffic of a sandbox, and add credentials to its requests.
- JavaScript SDK and Go SDK: use the sandbox plugin from code.