models

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: May 10, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MaxCredentialKeys  = 32
	MaxCredentialBytes = 4096
)

MaxCredentialKeys / MaxCredentialBytes bound credential payload size so a malicious request can't blow up daemon memory.

View Source
const (
	RuntimeDocker = "docker" // Docker's standard runc-backed runtime.
	RuntimeGvisor = "gvisor" // gVisor (runsc). User-space kernel for untrusted workloads.
	RuntimeKata   = "kata"   // Reserved: Kata Containers. Not yet implemented; rejected at create time.
)

User-facing runtime identifiers. These are the values the API, SDK, and stored sandbox row carry — chosen to match what an operator searching for "Docker" or "gVisor isolation" would type. The pkg/docker layer translates each one to the underlying OCI runtime binary name (runc / runsc / ...) when shaping the daemon request.

The empty string is reserved for legacy rows that pre-date the runtime field — those resolve to the host default at start time.

View Source
const (
	ExposedPortProtocolHTTP = "http"
	ExposedPortProtocolTCP  = "tcp"
	ExposedPortProtocolTLS  = "tls"
)

Exposed port protocols. http is the original behavior (Caddy HTTP reverse proxy); tcp and tls are the caddy-l4 paths added with the L4 work.

View Source
const MaxLifecycleDuration = 30 * 24 * time.Hour

MaxLifecycleDuration caps each Lifecycle field. 30 days is generous for any reasonable workload while still rejecting typos like "87600h" that would otherwise sit in the DB pretending to be useful.

View Source
const MaxMountsPerSandbox = 8

MaxMountsPerSandbox caps fan-out so a malicious request can't make sandboxd build an arbitrarily large container spec.

Variables

View Source
var ErrRuntimeNotImplemented = errors.New("runtime not yet implemented on this build")

ErrRuntimeNotImplemented is returned when a runtime is recognized as a valid identifier but the implementation has not been wired up yet. Today only "kata" hits this path. Surfaced through the API as a 4xx so operators get an actionable error instead of a generic 500.

Functions

func ResolveOCIRuntime

func ResolveOCIRuntime(value string) (string, error)

ResolveOCIRuntime maps a user-facing runtime identifier to the OCI runtime binary name to set on Docker's HostConfig.Runtime. Returns the binary name and true on success, or ErrRuntimeNotImplemented if the identifier is valid but the build does not implement it yet (today: kata).

The empty string is treated as "no override" — the caller leaves HostConfig.Runtime unset and Docker uses its compiled-in default. The "docker" identifier maps to "" for the same reason: avoiding an explicit "runc" entry means the daemon is happy even when /etc/docker/daemon.json has no runtimes.runc map entry, which is the common case.

func ValidExposedPortProtocol added in v0.1.4

func ValidExposedPortProtocol(value string) (string, error)

ValidExposedPortProtocol normalizes "" to http (the historical default) and rejects unknown values. Any caller that surfaces user input must run it through this before persistence.

func ValidRuntime

func ValidRuntime(value string) (string, error)

ValidRuntime normalizes and validates a user-facing runtime identifier. Empty input passes through unchanged so the caller can substitute the host default; any other value must be one of the recognized names. The intent here is "fail fast at the API boundary" — by the time a request reaches the runtime layer, we should already know the value is one we can act on.

Types

type CreateSandboxRequest

type CreateSandboxRequest struct {
	Image string `json:"image"`
	// CPU is the number of CPU cores to allocate. Fractional values are
	// supported (e.g. 0.5 = half a core, 1.5 = one and a half cores).
	// Translates to Docker's CpuQuota at 100ms periods.
	CPU              float64           `json:"cpu"`
	MemoryMB         int               `json:"memory_mb"`
	DiskGB           int               `json:"disk_gb"`
	Env              map[string]string `json:"env"`
	OSUser           string            `json:"os_user"`
	NetworkBlockAll  bool              `json:"network_block_all"`
	Registry         *RegistryAuth     `json:"registry,omitempty"`
	ContainerCommand []string          `json:"container_command,omitempty"`
	Mounts           []MountSpec       `json:"mounts,omitempty"`
	Lifecycle        *Lifecycle        `json:"lifecycle,omitempty"`
	// Runtime selects the container runtime for this sandbox. Empty falls back
	// to the host default (SB_CONTAINER_RUNTIME). Allowed values: "docker"
	// (standard runc-backed Docker runtime, default), "gvisor" (runsc-backed
	// userspace kernel — use for untrusted workloads), or "kata" (reserved,
	// not yet implemented).
	Runtime string `json:"runtime,omitempty"`
	// GPUs attaches GPU resources to the sandbox. Nil means no GPU. GPU access
	// is not supported with the gVisor runtime — the API returns an error if
	// both GPUs and runtime="gvisor" are set.
	GPUs *GPURequest `json:"gpus,omitempty"`
}

type CreateSandboxResponse

type CreateSandboxResponse struct {
	Sandbox
	SSHPrivateKey string `json:"ssh_private_key,omitempty"`
}

CreateSandboxResponse is what the API returns from POST /v1/sandboxes. SSHPrivateKey is generated server-side per sandbox and returned exactly once — it is never persisted and never returned again. The corresponding public key is stored on the sandbox record and is the only key authorized to SSH into that sandbox.

type CreateSessionRequest

type CreateSessionRequest struct {
	// Name is the human-friendly session label. Two callers asking for the
	// same Name see the same session (idempotent attach). Default "default".
	Name    string            `json:"name,omitempty"`
	Argv    []string          `json:"argv,omitempty"`
	Command string            `json:"command,omitempty"`
	WorkDir string            `json:"workdir,omitempty"`
	Env     map[string]string `json:"env,omitempty"`
	PTY     bool              `json:"pty,omitempty"`
	Cols    int               `json:"cols,omitempty"`
	Rows    int               `json:"rows,omitempty"`
}

CreateSessionRequest is the body of POST /sessions on toolboxd (and the shape the sandboxd proxy forwards). Either Argv or Command may be supplied; Command is run via `sh -c` for convenience.

type ErrorResponse

type ErrorResponse struct {
	Error string `json:"error"`
}

type ExecRequest

type ExecRequest struct {
	Command        string            `json:"command"`
	WorkDir        string            `json:"workdir,omitempty"`
	Env            map[string]string `json:"env,omitempty"`
	TimeoutSeconds int               `json:"timeout_seconds,omitempty"`
}

type ExecResult

type ExecResult struct {
	Stdout     string `json:"stdout"`
	Stderr     string `json:"stderr"`
	ExitCode   int    `json:"exit_code"`
	DurationMS int64  `json:"duration_ms"`
}

type ExposePortRequest added in v0.1.4

type ExposePortRequest struct {
	Protocol string `json:"protocol,omitempty"`
}

ExposePortRequest is the optional JSON body for POST /v1/sandboxes/{id}/ports/{port}. Empty body or empty Protocol falls back to "http" — the historical default — so old SDK callers keep working unchanged.

type ExposedPort

type ExposedPort struct {
	SandboxID string `json:"sandbox_id"`
	Port      int    `json:"port"`
	// Protocol is one of "http" (default — Caddy HTTP reverse proxy), "tcp"
	// (caddy-l4 listener bound to HostPort, raw TCP forward to the container),
	// or "tls" (caddy-l4 SNI route on the shared TLS listener). Pre-migration
	// rows carry "http" via the column default.
	Protocol string `json:"protocol"`
	// HostPort is the parent-host TCP listener allocated for protocol="tcp"
	// from the configured pool (default [22000, 23000]). Zero for http/tls
	// modes, which don't reserve a per-exposure host port.
	HostPort  int       `json:"host_port,omitempty"`
	PublicURL string    `json:"public_url"`
	CreatedAt time.Time `json:"created_at"`
}

type GPURequest added in v0.1.3

type GPURequest struct {
	// Vendor is required. Allowed values: "nvidia", "amd", "apple".
	Vendor GPUVendor `json:"vendor"`
	// Count is the number of GPUs to allocate. Use -1 to request all GPUs on
	// the host. Zero is treated as 1 (default). Ignored for AMD (all AMD GPUs
	// on the host are exposed via /dev/kfd and /dev/dri).
	Count int `json:"count,omitempty"`
	// DeviceIDs pins the sandbox to specific GPU device indices or UUIDs.
	// For NVIDIA: GPU indices ("0", "1") or UUIDs ("GPU-abc123...").
	// For AMD and Apple: ignored.
	DeviceIDs []string `json:"device_ids,omitempty"`
}

GPURequest describes the GPU resources to attach to a sandbox. The parent CreateSandboxRequest.GPUs field is a pointer, so omitting it entirely means no GPU — this struct only appears when the caller explicitly opts in.

func (*GPURequest) Validate added in v0.1.3

func (g *GPURequest) Validate() error

Validate checks GPURequest fields for consistency.

type GPUVendor added in v0.1.3

type GPUVendor string

GPUVendor identifies the GPU hardware vendor for sandbox GPU allocation.

const (
	// GPUVendorNVIDIA selects NVIDIA GPUs via nvidia-container-runtime.
	// Requires nvidia-container-toolkit installed on the host.
	GPUVendorNVIDIA GPUVendor = "nvidia"
	// GPUVendorAMD selects AMD GPUs via ROCm device bind-mounts (/dev/kfd,
	// /dev/dri). Requires ROCm drivers on the host.
	GPUVendorAMD GPUVendor = "amd"
	// GPUVendorApple selects Apple Silicon GPU via Docker Desktop's
	// experimental Metal support. Only functional on macOS with Docker Desktop;
	// Linux hosts will receive a Docker daemon error at container creation.
	GPUVendorApple GPUVendor = "apple"
)

type HealthStatus

type HealthStatus struct {
	Status     string `json:"status"`
	Sandboxes  int    `json:"sandboxes"`
	Docker     string `json:"docker"`
	Caddy      string `json:"caddy"`
	SSHGateway string `json:"ssh_gateway"`
	Version    string `json:"version"`
}

type Lifecycle

type Lifecycle struct {
	StopIfIdleFor    time.Duration `json:"stop_if_idle_for,omitempty"`
	DestroyIfIdleFor time.Duration `json:"destroy_if_idle_for,omitempty"`
	StopAtAge        time.Duration `json:"stop_at_age,omitempty"`
	DestroyAtAge     time.Duration `json:"destroy_at_age,omitempty"`
}

Lifecycle declares automatic stop/destroy timers for a sandbox. Each field is a duration; zero means "disabled" for that axis. Idle triggers measure time since LastActiveAt (i.e. since the last toolbox/exec/SSH activity). Age triggers measure time since CreatedAt — they are absolute deadlines that do not reset on Stop+Start, so a "destroy at 24h" sandbox stays destroyable even if the user restarts it minutes before the deadline.

Multiple fields can be set; whichever timer fires first wins. Destroy supersedes Stop for the same trigger axis. The lifecycle sweep runs every minute, so deadlines are honored to roughly that resolution.

func (Lifecycle) IsZero

func (l Lifecycle) IsZero() bool

IsZero reports whether all four timers are disabled. Useful for skipping Lifecycle inspection in the sweep when there's nothing to evaluate.

func (Lifecycle) Validate

func (l Lifecycle) Validate() error

Validate enforces the invariants we rely on at sweep time:

  • no negative durations (zero means disabled, negative is meaningless),
  • each field <= MaxLifecycleDuration,
  • if both stop and destroy of the same trigger are set, destroy must not fire before stop. Without this we could surprise a user who set "stop at 2h, destroy at 1h" by skipping the stop entirely.

type MountSpec

type MountSpec struct {
	Type        MountType         `json:"type"`
	Target      string            `json:"target"`
	Source      string            `json:"source"`
	Options     map[string]string `json:"options,omitempty"`
	Credentials map[string]string `json:"credentials,omitempty"`
	ReadOnly    bool              `json:"read_only,omitempty"`
}

MountSpec describes a single external-storage mount the user wants to be available inside their sandbox. Credentials are encrypted at rest in the daemon's database, materialized only on the host as the FUSE process needs them, and never returned by any read API.

func (*MountSpec) Redact

func (m *MountSpec) Redact() MountSpecRedacted

Redact strips credentials, returning the user-safe view.

func (*MountSpec) Validate

func (m *MountSpec) Validate(toolboxMountPath string) error

Validate checks the spec against the daemon's mount policy. toolboxMountPath is the path the toolbox binary is bind-mounted to inside the container; we refuse to let the user shadow it.

type MountSpecFile

type MountSpecFile struct {
	Mounts []MountSpec `json:"mounts"`
}

MountSpecFile is the JSON envelope used to (de)serialize the encrypted blob of a sandbox's mount specs in the daemon's database. Kept as a struct rather than a bare slice so future fields (version, key id) can be added without a migration.

type MountSpecRedacted

type MountSpecRedacted struct {
	Type           MountType         `json:"type"`
	Target         string            `json:"target"`
	Source         string            `json:"source"`
	Options        map[string]string `json:"options,omitempty"`
	ReadOnly       bool              `json:"read_only,omitempty"`
	HasCredentials bool              `json:"has_credentials"`
}

MountSpecRedacted is the read-only view returned by the API. It mirrors MountSpec but strips credentials.

func RedactMounts

func RedactMounts(mounts []MountSpec) []MountSpecRedacted

RedactMounts returns the read-only API view for a slice of mounts.

type MountType

type MountType string

MountType identifies the storage backend the user wants mounted inside their container. The daemon runs the mount tool on the host (in a per-sandbox directory) and bind-mounts that directory into the container, so credentials never enter the container and the user's image needs no mount tooling.

const (
	MountTypeS3     MountType = "s3"
	MountTypeNFS    MountType = "nfs"
	MountTypeSSHFS  MountType = "sshfs"
	MountTypeRclone MountType = "rclone"
)

type RegistryAuth

type RegistryAuth struct {
	Server   string `json:"server"`
	Username string `json:"username"`
	Password string `json:"password"`
}

type ResizeSandboxRequest

type ResizeSandboxRequest struct {
	CPU      float64 `json:"cpu"`
	MemoryMB int     `json:"memory_mb"`
	DiskGB   int     `json:"disk_gb"`
}

type Sandbox

type Sandbox struct {
	ID               string            `json:"id"`
	Image            string            `json:"image"`
	Status           SandboxStatus     `json:"status"`
	PublicURL        string            `json:"public_url"`
	ContainerID      string            `json:"container_id,omitempty"`
	ContainerIP      string            `json:"container_ip,omitempty"`
	CPU              float64           `json:"cpu"`
	MemoryMB         int               `json:"memory_mb"`
	DiskGB           int               `json:"disk_gb"`
	OSUser           string            `json:"os_user"`
	Env              map[string]string `json:"env,omitempty"`
	NetworkBlockAll  bool              `json:"network_block_all"`
	ToolboxEnabled   bool              `json:"toolbox_enabled"`
	ToolboxToken     string            `json:"-"`
	SSHPublicKey     string            `json:"ssh_public_key,omitempty"`
	ExposedPorts     []ExposedPort     `json:"exposed_ports,omitempty"`
	CreatedAt        time.Time         `json:"created_at"`
	UpdatedAt        time.Time         `json:"updated_at"`
	LastActiveAt     time.Time         `json:"last_active_at"`
	LastError        string            `json:"last_error,omitempty"`
	ContainerCommand []string          `json:"container_command,omitempty"`
	Lifecycle        Lifecycle         `json:"lifecycle"`
	// Runtime is the container runtime this sandbox uses (one of "docker",
	// "gvisor", or "kata"). Pre-migration rows carry "" and resolve to the
	// host default at start time; new sandboxes always store the resolved
	// value so the choice cannot drift across host restarts.
	Runtime string `json:"runtime"`
	// GPUs is the GPU configuration this sandbox was created with. Nil means
	// no GPU was requested.
	GPUs *GPURequest `json:"gpus,omitempty"`
}

type SandboxRuntimeState

type SandboxRuntimeState struct {
	SandboxID   string
	ContainerID string
	ContainerIP string
	Status      SandboxStatus
}

SandboxRuntimeState is the runtime view of a sandbox returned by the container runtime layer (Docker today, gVisor/native runsc tomorrow). It carries only what the service needs to reconcile and route — anything else belongs on models.Sandbox or stays in the runtime implementation.

type SandboxStatus

type SandboxStatus string
const (
	SandboxStatusCreating  SandboxStatus = "creating"
	SandboxStatusStarted   SandboxStatus = "started"
	SandboxStatusStopped   SandboxStatus = "stopped"
	SandboxStatusDestroyed SandboxStatus = "destroyed"
	SandboxStatusError     SandboxStatus = "error"
)

Sandbox lifecycle states.

SandboxStatusDestroyed marks a sandbox whose container is gone — either because the user called DELETE /sandboxes/{id}, or because the container died out-of-band and the event monitor noticed. The status exists as a transient marker only: the API-driven destroy path deletes the row in the same call, and the reconcile loop deletes any row whose container has gone missing on its next pass. Destroyed rows are not retained — keeping them around would also keep their host_port reservations in exposed_ports, slowly exhausting the L4 TCP allocator pool over the daemon's lifetime.

type Session

type Session struct {
	ID         string        `json:"id"`
	Name       string        `json:"name"`
	Argv       []string      `json:"argv"`
	WorkDir    string        `json:"workdir,omitempty"`
	PTY        bool          `json:"pty"`
	Status     SessionStatus `json:"status"`
	ExitCode   int           `json:"exit_code"`
	ExitSignal string        `json:"exit_signal,omitempty"`
	CreatedAt  time.Time     `json:"created_at"`
	StartedAt  time.Time     `json:"started_at"`
	ExitedAt   time.Time     `json:"exited_at,omitempty"`
	Recording  bool          `json:"recording"`
	Bytes      int64         `json:"bytes"`    // total stdout+stderr bytes produced
	Attached   int           `json:"attached"` // current number of attached clients
}

Session is the metadata view of a session — never includes its output.

type SessionList

type SessionList struct {
	Sessions []Session `json:"sessions"`
}

SessionList is the GET /sessions response shape.

type SessionResizeRequest

type SessionResizeRequest struct {
	Cols int `json:"cols"`
	Rows int `json:"rows"`
}

SessionResizeRequest is the POST /sessions/{id}/resize body.

type SessionSignalRequest

type SessionSignalRequest struct {
	Signal string `json:"signal"`
}

SessionSignalRequest is the POST /sessions/{id}/signal body. Signal is one of INT, TERM, KILL, HUP, QUIT (or the SIG*-prefixed forms).

type SessionStatus

type SessionStatus string

SessionStatus is the lifecycle stage of a long-running command session inside a sandbox container.

const (
	SessionStatusRunning SessionStatus = "running"
	SessionStatusExited  SessionStatus = "exited"
	SessionStatusKilled  SessionStatus = "killed"
	SessionStatusFailed  SessionStatus = "failed" // could not start
)

type UpdateLifecycleRequest

type UpdateLifecycleRequest struct {
	Lifecycle
}

UpdateLifecycleRequest is the body for PUT /v1/sandboxes/{id}/lifecycle. Full-replacement semantics: send all four fields. To clear a field, set it to zero. To preserve a field, send its current value (read it via GET first if the caller doesn't already have it).

Jump to

Keyboard shortcuts

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