e2b driver
The driver for sandboxes at a sandbox service, outside your infrastructure or cluster. This page is for whoever runs kōjō against such a service, from one machine or from the Helm chart; the sandboxes reach the network through kojo-proxy.
The e2b driver runs each sandbox at an E2B-compatible service: E2B Cloud, a self-hosted E2B, or CubeSandbox, which serves the same API. A sandbox is a microVM the service starts from a template. kōjō uses two APIs: the service’s control API, with your API key, to create, find and remove sandboxes, and the daemon inside each sandbox (envd), with a token the service issues per sandbox, to run commands.
Requirements
- An API key of the service.
- A template with what kōjō and your skills need. See Templates.
- kojo-proxy at an address the service’s sandboxes can connect to. See Where the proxy must be.
- A service that restricts the outbound network of each sandbox (
allowOutanddenyOutin its API). E2B and CubeSandbox do. - From the machine kōjō runs on, HTTPS to the control API and to the sandboxes’ own host names.
Configuration
sandbox:
driver: e2b
ttl: 1h
e2b:
api_key: "${E2B_API_KEY}"
template: kojo-toolchain
proxy:
listen: ":3128"
advertise_url: http://203.0.113.7:3128
A self-hosted service names its domain. One whose sandbox proxy has no wildcard domain, such as a CubeSandbox reached by address, also names that proxy:
sandbox:
driver: e2b
e2b:
api_key: "${E2B_API_KEY}"
domain: sandboxes.example.org
sandbox_url: http://10.20.0.5:8080
template: kojo-toolchain
proxy:
advertise_url: http://10.20.0.9:3128
| Key | Default | Meaning |
|---|---|---|
sandbox.e2b.api_key |
none, required | The service’s API key. Use a ${VARIABLE}. |
sandbox.e2b.template |
none, required unless sandbox.image is set |
The template of a sandbox whose skill names no image. sandbox.image, when set, is used instead. |
sandbox.e2b.domain |
e2b.app |
The service’s domain. The daemon of the sandbox with the ID abc is reached at https://49983-abc.e2b.app. |
sandbox.e2b.api_url |
https://api. followed by the domain |
The control API. |
sandbox.e2b.sandbox_url |
none | One fixed URL for every sandbox, for a service whose sandbox proxy routes by the E2b-Sandbox-Id header instead of by host name. |
sandbox.proxy.advertise_url |
none, required with the embedded proxy | The address under which sandboxes reach kojo-proxy. |
How it works
sequenceDiagram
participant K as kōjō
participant A as Service API
participant S as Sandbox
participant P as kojo-proxy
participant H as Allowlisted host
K->>A: create from the template, all egress denied except the proxy's address
A-->>K: sandbox ID and its envd token
K->>A: read the sandbox back
Note over K: The sandbox is used only when the service reports the restriction
K->>S: envd, as root, install the proxy CA and create /workspace
K->>P: policy with the allowlist and the credentials
K->>S: envd, run a command with the proxy URL and the placeholders
S->>P: request, as sandbox ID and token
P->>H: the request, with the credential header set
K->>A: kill, when the session ends
- A kit’s
imageis a template name or ID on this driver. CPU, memory and disk are the template’s: a kit’sresourcesare not sent. - The workspace is the directory
/workspacein the sandbox’s own filesystem. The driver creates it and gives it to the template’s default user, who runs every command. It lives as long as the sandbox. sandbox.ttlis the service’s timeout: the service kills the sandbox when it ends, whether kōjō is running or not. A TTL longer than your plan allows is refused by the service when a sandbox is created, and the error namessandbox.ttl. On E2B Cloud the limit is 1 hour on the Hobby plan and 24 hours on Pro.- A command’s standard input is uploaded to a file in the sandbox’s
/tmpand removed when the command has opened it. - A command that times out is killed inside the sandbox with every process it started: the driver finds them by a marker in their environment, as root.
Egress and credentials
- Every sandbox is created with all outbound traffic denied and one exception, the IPv4 address of kojo-proxy, and with its public URLs closed to requests without the sandbox’s access token. The driver then reads the sandbox back from the service. Outside
dev: trueit removes a sandbox whose restriction the service does not report, and the session fails, instead of running code in it. - A sandbox restricted this way has no name server either. The driver hands it the proxy as an address, and empties
/etc/resolv.confso that a program which looks a name up by itself fails at once instead of waiting. - The session ID, the skill, the end of the TTL and the sandbox’s proxy token are stored as the sandbox’s metadata at the service, so any kōjō process finds a sandbox again and nothing is kept on kōjō’s side. No credential is ever sent to the service: the sandbox gets placeholders, and kojo-proxy holds the real values.
Where the proxy must be
The sandboxes run in the service’s network, not in yours, and kojo-proxy is the only thing they may connect to. So:
sandbox.proxy.advertise_urlmust be an address the service’s sandboxes can reach: for E2B Cloud a public IPv4 address, for a self-hosted service one that its sandbox hosts route to. E2B does not let a sandbox reach private, loopback or link-local addresses, whatever the allow list says.- Write it as
http://203.0.113.7:3128, with the address as a number, when you can. A host name works too: kōjō resolves it and gives sandboxes the address, so the name must resolve, where kōjō runs, to the address the sandboxes can reach. - Open that port to the addresses the service’s sandboxes come from, and to nothing else if the service publishes them. Every request to the proxy needs a sandbox’s ID and its token; without them the proxy answers 407.
- Keep the admin API (
:3129, inremotemode) off that address. It takes credentials and is for kōjō only.
On a single machine the embedded proxy listens on sandbox.proxy.listen; open that port in the machine’s firewall and advertise the machine’s public address. On Kubernetes, the Helm chart exposes the proxy port with proxy.external.enabled and takes the address in proxy.advertiseURL; see the chart’s README. The load balancer sends a sandbox’s requests to any of the proxy’s replicas that is ready, so E2B’s allow list still names one address.
Templates
A template is the service’s image of a sandbox. It must contain:
/bin/shand the usual tools:id,mkdir,chown,cat,mv,rm,tar,tr,grep,kill(BusyBox or coreutils);git,findandprintf, which the core uses on every driver;- CA certificates, in one of the usual bundle files (
/etc/ssl/certs/ca-certificates.crton Debian, Ubuntu and Alpine). The driver appends the proxy’s CA to a copy of it and pointsSSL_CERT_FILE,GIT_SSL_CAINFO,CURL_CA_BUNDLE,REQUESTS_CA_BUNDLEandNODE_EXTRA_CA_CERTSat the copy; curlorwget, for the egress probe ofkojo doctor;- whatever your skills run: the language toolchains, test runners and linters.
The service builds a template from a Dockerfile. kōjō’s own toolchain image has all of the above, so the shortest template is one line. Put it in a file named e2b.Dockerfile:
FROM ghcr.io/xavidop/kojo-toolchain:latest
Build it with the service’s tooling, under a name of your choice. With the E2B CLI, which also sets the template’s CPU and memory:
e2b template create kojo-toolchain --dockerfile e2b.Dockerfile --cpu-count 4 --memory-mb 8192
Then set sandbox.e2b.template: kojo-toolchain. A repository with other needs builds a template of its own and names it as the image of its kit.
Limits
- The service’s allow list is by address, not by port: a sandbox can connect to every port of the proxy’s address. Give kojo-proxy an address that serves nothing else.
Healthreports egress as enforced when the service reports the restriction on a sandbox. When no sandbox is running to look at,Health(and sokojo doctor) creates one from the default template for a moment.- Sandboxes cannot run their own Docker daemon on this driver: a kit’s
needs_dockeris not honoured. - When kojo-proxy’s address changes, the egress rules of a running sandbox are replaced the next time a skill starts in it.
- The connection from a sandbox to kojo-proxy is the HTTP proxy protocol, as with every driver: a request to an
https://host is TLS inside it, the sandbox’s proxy token is not. Between a hosted service and your proxy that connection crosses the internet. The security page says what the token gives access to.