Documentation
¶
Overview ¶
Package docker is the sandbox driver that runs each sandbox as a plain Docker container. It is for development only.
A container shares the host's kernel, so this driver reports sandbox.IsolationContainer, which kojo doctor refuses outside dev mode: use sbx or k8s to run code you do not trust. What the driver does provide is the rest of the security model, so that a development setup behaves like a real one:
- Credentials never enter the container. The driver resolves a policy's credential references on the host and hands the values to kojo-proxy; the container only gets placeholders (KOJO_CRED_<NAME>), the proxy address, and the proxy's CA certificate.
- Egress goes through kojo-proxy. Sandboxes join a Docker network created with --internal, which has no route to the outside and no external DNS, and with inhibit_ipv4, which gives the host no address on it. The only thing reachable on it is the proxy: directly, when the proxy is attached to that network (docker compose), or through a small relay container that forwards one port to the proxy on the host (`kojo serve` on a laptop). Health reports EgressEnforced truthfully: it is false when the configured network is not internal, or when the host has an address on it (a plain `docker network create --internal`), where a sandbox reaches the host's own services.
- Containers run with no-new-privileges, without NET_RAW (no ARP spoofing of the proxy on the shared network), with a PID limit, and with the CPU and memory limits of the spec, lowered to Options.MaxResources and to what the Docker daemon has (docker info, read once): Docker refuses more CPUs than the daemon has.
Known limits, all acceptable for development and none for production: sandboxes on the same network can open connections to each other; the Disk resource is not enforced; a sandbox that needs its own Docker daemon is refused, because that takes a privileged container; and the driver runs a few fixed shell snippets as root inside the container (installing the CA bundle, killing a timed-out command), which trusts the image's sh and coreutils.
The driver shells out to the docker CLI. All state lives in Docker (container, volume, labels), so another process finds a sandbox again with Get; only the credential placeholders are kept in memory and return with the next ApplyPolicy.
An image must provide sh, sleep, true, id, chown, tar, tr, grep, sed, cut and kill: BusyBox or coreutils. Commands run as the image's user, and the workspace volume is handed to that user when the sandbox is created.
Index ¶
- Constants
- type Driver
- func (d *Driver) Create(ctx context.Context, spec sandbox.SandboxSpec) (sandbox.Sandbox, error)
- func (d *Driver) Destroy(ctx context.Context, id string) error
- func (d *Driver) Get(ctx context.Context, id string) (sandbox.Sandbox, error)
- func (d *Driver) Health(ctx context.Context) (sandbox.Health, error)
- func (d *Driver) Name() string
- func (d *Driver) Reap(ctx context.Context) ([]string, error)
- func (d *Driver) Teardown(ctx context.Context) error
- type Options
Constants ¶
const ( // DefaultNetwork is the internal network the driver creates when // Options.Network is empty. DefaultNetwork = "kojo-sandbox" // DefaultRelayImage runs the relay between an internal network and a // proxy on the host. It must provide sh and socat. DefaultRelayImage = "alpine/socat:1.8.1.3" // DefaultReadyTimeout bounds the wait for a new container to accept an // Exec, image pull included. DefaultReadyTimeout = 5 * time.Minute )
Defaults for Options.
const ( LabelSandbox = "kojo.sandbox" // the sandbox ID LabelSession = "kojo.session" // SandboxSpec.SessionID LabelSkill = "kojo.skill" // SandboxSpec.Skill LabelExpires = "kojo.expires" // RFC 3339, absent without a TTL LabelWorkspace = "kojo.workspace" // where the workspace volume is mounted LabelNetwork = "kojo.network" // the sandbox network, which scopes Reap )
Labels the driver sets on containers and volumes.
const ( // CABundlePath is the bundle every Exec's TLS variables point at: the // image's system roots plus the proxy CA. CABundlePath = caDir + "/ca-bundle.crt" )
const Name = "docker"
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 as Docker containers. It is safe for concurrent use.
func (*Driver) Create ¶
Create starts a container with a named volume as its workspace and waits until Exec works in it. The spec's policy is applied before it returns.
func (*Driver) Get ¶
Get finds a sandbox by ID, whichever process created it. A container that is stopped (the Docker daemon restarted) is started again: its workspace volume is still there.
func (*Driver) Health ¶
Health reports the Docker version and whether egress is enforced. It sets up the sandbox network (and the relay, when one is needed) to find out, as the first Create would.
func (*Driver) Reap ¶
Reap destroys the sandboxes whose TTL has passed and returns their IDs. It goes by the kojo.expires label, so it also removes sandboxes another process created and left behind, and volumes whose container is gone. It only looks at sandboxes of this driver's network, so two kojo installs on one Docker daemon leave each other alone. The core calls it periodically.
func (*Driver) Teardown ¶
Teardown removes what the driver set up around sandboxes: the relay container of its network and, when the driver created the network and nothing is attached to it any more, the network. Sandboxes are not touched: Destroy or Reap them first. Tests use it; a deployment has no reason to.
type Options ¶
type Options struct {
// Binary is the docker CLI. Default "docker" on PATH.
Binary string
// Network is the Docker network sandboxes join. When empty the driver
// creates DefaultNetwork as an internal network. A network named here
// must exist; when it is not internal, egress is not enforced and Health
// says so.
Network string
// Proxy is the kojo-proxy sandboxes go through. Required.
Proxy proxy.Controller
// Credentials resolves a policy's credential references. It may be nil
// when no policy names a credential.
Credentials sandbox.CredentialSource
// Logger receives the driver's log lines. Default: discard.
Logger *slog.Logger
// RelayImage is the image of the relay container. Default
// DefaultRelayImage.
RelayImage string
// ReadyTimeout bounds Create's wait for the container, image pull
// included. Default DefaultReadyTimeout.
ReadyTimeout time.Duration
// MaxResources is the most CPU and memory a sandbox gets
// (sandbox.max_resources). A spec that asks for more gets this much.
// Independently of it, a request is lowered to the CPUs and memory the
// Docker daemon has. Disk is not enforced by this driver.
MaxResources sandbox.Resources
}
Options configure the driver.