e2b

package
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 3, 2026 License: Apache-2.0 Imports: 31 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package e2b is the sandbox driver for E2B-compatible services: E2B Cloud, a self-hosted E2B, and CubeSandbox, which serves the same API. Each sandbox is a microVM the service runs; kōjō reaches it over the network.

Two APIs are used (api.go has every path and header):

  • the control API, with the team's API key: create a sandbox from a template, get, list and kill sandboxes, replace a sandbox's egress rules;
  • envd, the daemon in each sandbox, with the access token the control API hands out for that sandbox: start a process and follow its output, signal it, upload a file.

How the contract maps onto them:

  • A spec's image is a template name or ID (Options.Template when the spec has none). CPU, memory and disk are the template's: the spec's resources are not sent. The workspace is a directory in the sandbox's own filesystem and lives as long as the sandbox.
  • The session, the skill, the expiry and the sandbox's proxy token are stored as sandbox metadata, and the spec's environment as the sandbox's environment variables. All state is at the service, so another process finds a sandbox again with Get; only the credential placeholders are kept in memory and return with the next ApplyPolicy.
  • The spec's TTL is the service's timeout, after which the service kills the sandbox. Reap removes what is past its TTL as well, going by the metadata, and clears the proxy's policies.
  • Exec starts a process through envd and follows its event stream. A command's standard input is uploaded to a file first and opened by a one-line shell wrapper. A timeout kills every process that carries the Exec's marker in its environment, as the other drivers do.
  • CopyIn and CopyOut stream tar through such a process.

Egress and credentials work as with the docker and k8s drivers: a sandbox reaches the network only through kojo-proxy, which holds the allowlist and the credentials, and the sandbox gets placeholders, the proxy's address with its own ID and token, and the proxy's CA. What stops a sandbox from going around the proxy is the service's own firewall: every sandbox is created with all outbound traffic denied and one exception, the IPv4 address of the proxy. The driver reads the sandbox back and only counts the restriction as in place when the service reports it; Create refuses a sandbox without it unless Options.AllowOpenEgress is set, and Health says which of the two holds. The sandbox's public URLs are closed to anyone without its traffic access token as well, so that a process in the sandbox cannot serve data to the outside.

The proxy must therefore be at an address the service's sandboxes can connect to, which for a hosted service is a public one, and the sandbox is told that address as a number: with everything else denied it cannot look a name up. For the same reason the driver empties the resolver configuration of a sandbox it found restricted, so that a lookup fails at once. The connection from a sandbox to the proxy is plain HTTP proxy protocol, as with the other drivers; what travels in it is TLS wherever the destination is.

Health needs a sandbox to tell whether the service restricts egress. When this process has created none and none is running, it creates one from Options.Template for a moment.

A template must provide /bin/sh, id, mkdir, chown, cat, mv, rm, tar, tr, grep and kill (BusyBox or coreutils), CA certificates, and whatever the skills run: git above all.

assumptions.go lists what the driver takes for granted beyond the service's documentation.

Index

Constants

View Source
const (
	// DefaultDomain is E2B Cloud's.
	DefaultDomain = "e2b.app"
	// DefaultTTL is the lifetime of a sandbox whose spec names none.
	DefaultTTL = time.Hour
	// DefaultReadyTimeout bounds Create's wait for a new sandbox.
	DefaultReadyTimeout = 3 * time.Minute
)

Defaults for Options.

View Source
const (
	// MetaManaged marks a sandbox this driver created.
	MetaManaged = "kojo"
	// MetaSession is SandboxSpec.SessionID.
	MetaSession = "kojoSession"
	// MetaSkill is SandboxSpec.Skill.
	MetaSkill = "kojoSkill"
	// MetaExpires is when the sandbox's TTL ends, in Unix seconds.
	MetaExpires = "kojoExpires"
	// MetaWorkspace is where the workspace is.
	MetaWorkspace = "kojoWorkspace"
)

Metadata the driver sets on its sandboxes. The service stores it and gives it back on a get and in a listing, so another process finds a sandbox again and Reap knows when it is due. Keys and values stay within letters and digits where the driver chooses them, so that any service takes them.

View Source
const (

	// CABundlePath is the bundle every Exec's TLS variables point at: the
	// template's system roots plus the proxy CA.
	CABundlePath = caDir + "/ca-bundle.crt"
)
View Source
const Name = "e2b"

Name is the driver's key in kojo.yaml.

Variables

This section is empty.

Functions

This section is empty.

Types

type Driver

type Driver struct {
	// contains filtered or unexported fields
}

Driver runs sandboxes on an E2B-compatible service. It is safe for concurrent use.

func New

func New(opts Options) (*Driver, error)

New returns a driver. It does not talk to the service: Health does.

func (*Driver) Create

func (d *Driver) Create(ctx context.Context, spec sandbox.SandboxSpec) (sandbox.Sandbox, error)

Create starts a sandbox from the spec's template and waits until Exec works in it. The spec's policy is applied before it returns.

func (*Driver) Destroy

func (d *Driver) Destroy(ctx context.Context, id string) error

Destroy kills the sandbox and deletes its proxy policy.

func (*Driver) Get

func (d *Driver) Get(ctx context.Context, id string) (sandbox.Sandbox, error)

Get finds a sandbox by ID, whichever process created it.

func (*Driver) Health

func (d *Driver) Health(ctx context.Context) (sandbox.Health, error)

Health checks that the control API answers and accepts the key, and reports whether the service restricts a sandbox's outbound network to the proxy. The second part needs a sandbox to look at: one created earlier by this process, one that is running, or else a sandbox Health creates from Options.Template and removes again.

func (*Driver) Name

func (d *Driver) Name() string

Name returns "e2b".

func (*Driver) Reap

func (d *Driver) Reap(ctx context.Context) ([]string, error)

Reap destroys the sandboxes whose TTL has passed and returns their IDs. It goes by the expiry in the metadata, so it also removes sandboxes another process created and left behind. The service kills a sandbox at its own timeout as well; for those Reap only clears what kojo-proxy and this process still held. The core calls it periodically.

type Options

type Options struct {
	// APIKey authenticates to the service's control API. Required.
	APIKey string
	// APIURL is the control API. Default https://api.<Domain>.
	APIURL string
	// Domain is the base domain sandboxes are reachable under: envd of
	// sandbox ID answers at https://49983-ID.<Domain>. Default DefaultDomain.
	// A sandbox that reports a domain of its own is reached there instead.
	Domain string
	// SandboxURL, when set, is one fixed URL for the envd of every sandbox:
	// a sandbox proxy that routes by the E2b-Sandbox-Id header (E2B's
	// sandbox.<domain>, CubeProxy without wildcard DNS).
	SandboxURL string
	// Template is the template of a spec without an image. On this driver a
	// spec's image is a template name or ID.
	Template string
	// Proxy is the kojo-proxy sandboxes go through. Required. Its endpoint
	// must be an address the service's sandboxes can connect to.
	Proxy proxy.Controller
	// Credentials resolves a policy's credential references. It may be nil
	// when no policy names a credential.
	Credentials sandbox.CredentialSource
	// AllowOpenEgress lets the driver hand out a sandbox although the
	// service did not confirm that its outbound network is restricted to the
	// proxy. Health reports EgressEnforced false either way. Without it,
	// Create refuses such a sandbox. Development only.
	AllowOpenEgress bool
	// Logger receives the driver's log lines. Default: discard.
	Logger *slog.Logger
	// ReadyTimeout bounds Create's wait for the sandbox to answer. Default
	// DefaultReadyTimeout.
	ReadyTimeout time.Duration
	// HTTPClient makes the control API calls and, without its timeout, the
	// envd calls. Default: a client with the default transport.
	HTTPClient *http.Client
	// Resolver looks up the IPv4 addresses of the proxy's host. Default: the
	// system resolver.
	Resolver func(ctx context.Context, host string) ([]netip.Addr, error)
}

Options configure the driver.

Directories

Path Synopsis
Package e2btest is an in-process E2B-compatible service for tests: the control API and envd behind one HTTP server, as far as the e2b driver uses them (see internal/sandbox/e2b/api.go).
Package e2btest is an in-process E2B-compatible service for tests: the control API and envd behind one HTTP server, as far as the e2b driver uses them (see internal/sandbox/e2b/api.go).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL