Documentation
¶
Overview ¶
Package sandbox provides isolation for untrusted tool/skill execution.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Backend ¶ added in v0.3.0
type Backend string
Backend identifies a sandbox backend type.
func ParseBackend ¶ added in v0.3.0
ParseBackend parses a backend string.
type Config ¶ added in v0.3.0
type Config struct {
Backend Backend `json:"backend"`
WorkDir string `json:"work_dir,omitempty"`
// Container-specific
Image string `json:"image,omitempty"`
Network string `json:"network,omitempty"`
// Runtime selects a hardened OCI runtime for the container backend
// (e.g. "runsc" for gVisor, "kata-runtime"). Empty uses the daemon default.
Runtime string `json:"runtime,omitempty"`
// K8s-specific
Namespace string `json:"namespace,omitempty"`
ServiceAcc string `json:"service_account,omitempty"`
// WASM-specific
WASMPath string `json:"wasm_path,omitempty"`
}
Config holds configuration for sandbox creation.
type ContainerConfig ¶
type ContainerConfig struct {
Image string
SocketPath string
MemoryBytes int64
CPUQuota int64
NetworkMode string
// Hardening. Zero values fall back to hardened defaults.
User string
PidsLimit int64
NofileLimit int64
NprocLimit int64
CapAdd []string
SeccompProfile string
// Runtime selects a hardened OCI runtime such as "runsc" (gVisor) or
// "kata-runtime". Empty uses the daemon default (runc). It is never required.
Runtime string
// Tmpfs overrides the default writable /tmp mount. Nil uses the default.
Tmpfs map[string]string
// WritableRootfs disables the read-only rootfs hardening when true.
WritableRootfs bool
Mounts []BindMount
WorkingDir string
Env []string
}
ContainerConfig holds container sandbox configuration.
type ContainerPool ¶ added in v0.3.0
type ContainerPool struct {
// contains filtered or unexported fields
}
ContainerPool maintains a pool of pre-warmed containers for reduced cold-start latency.
Idle containers are reclaimed lazily: any container that has sat in the pool longer than maxIdle is closed and dropped the next time the pool is touched (Acquire or Release). This avoids a background goroutine while still bounding how long stale containers linger.
func NewPool ¶ added in v0.3.0
func NewPool(cfg PoolConfig) (*ContainerPool, error)
NewPool creates a ContainerPool with the given configuration.
func (*ContainerPool) Acquire ¶ added in v0.3.0
func (p *ContainerPool) Acquire() (Sandbox, error)
Acquire returns a ready sandbox from the pool. If the pool is empty, a new sandbox is created. Returns immediately if a warm container is available.
func (*ContainerPool) Close ¶ added in v0.3.0
func (p *ContainerPool) Close() error
Close shuts down the pool and all containers.
func (*ContainerPool) InUse ¶ added in v0.3.0
func (p *ContainerPool) InUse() int
InUse returns the number of containers currently in use.
func (*ContainerPool) Release ¶ added in v0.3.0
func (p *ContainerPool) Release(sb Sandbox) error
Release returns a sandbox to the pool. If the pool is full, the sandbox is closed.
func (*ContainerPool) Size ¶ added in v0.3.0
func (p *ContainerPool) Size() int
Size returns the current number of available containers.
type ContainerSandbox ¶
type ContainerSandbox struct {
Image string
// Resource limits
MemoryBytes int64
CPUQuota int64
NetworkMode string
// Hardening
User string // non-root user/uid:gid the workload runs as
PidsLimit int64 // max processes inside the container
NofileLimit int64 // RLIMIT_NOFILE (open files)
NprocLimit int64 // RLIMIT_NPROC (processes/threads)
ReadonlyRootfs bool // mount the container root filesystem read-only
CapAdd []string // capabilities to re-add on top of CapDrop ALL
SeccompProfile string // custom seccomp profile JSON; empty keeps Docker's default
Runtime string // OCI runtime (e.g. "runsc" for gVisor, "kata-runtime")
Tmpfs map[string]string // writable tmpfs mounts (path -> mount options)
Mounts []BindMount // explicitly granted host paths
WorkingDir string // directory inside the container
Env []string // explicit environment; no host environment inheritance
// contains filtered or unexported fields
}
ContainerSandbox implements Sandbox using Docker Engine API for production-grade isolation.
func NewContainerSandbox ¶
func NewContainerSandbox(cfg ContainerConfig) *ContainerSandbox
NewContainerSandbox creates a Docker-based sandbox with a hardened default profile: non-root user, all capabilities dropped, no-new-privileges, the default seccomp profile, a pids limit, file/process ulimits, a read-only rootfs backed by a small tmpfs /tmp, and memory/CPU limits.
func (*ContainerSandbox) Close ¶
func (c *ContainerSandbox) Close() error
type K8sJobConfig ¶ added in v0.3.0
K8sJobConfig mirrors the real K8sJobConfig (see k8sjob.go, built with -tags sandbox_k8s) so callers compile identically either way.
type K8sJobSandbox ¶ added in v0.3.0
type K8sJobSandbox struct{}
K8sJobSandbox is a stub in the default build; see k8sjob.go under -tags sandbox_k8s for the real Kubernetes-Job-backed implementation.
func NewK8sJobSandbox ¶ added in v0.3.0
func NewK8sJobSandbox(cfg K8sJobConfig) (*K8sJobSandbox, error)
NewK8sJobSandbox is disabled in the default build to keep k8s.io/client-go (and its large transitive dependency graph) out of the binary — see chronos-code's ROADMAP.md "binary size" goal. Rebuild with -tags sandbox_k8s to enable the Kubernetes Job sandbox backend.
func (*K8sJobSandbox) Close ¶ added in v0.3.0
func (s *K8sJobSandbox) Close() error
type PoolConfig ¶ added in v0.3.0
type PoolConfig struct {
// MaxSize is the maximum number of containers in the pool (default 5).
MaxSize int
// MaxIdleTime is how long an idle container is kept before being destroyed (default 5m).
MaxIdleTime time.Duration
// Factory creates new sandbox instances.
Factory func() (Sandbox, error)
}
PoolConfig configures a container pool.
type ProcessSandbox ¶
type ProcessSandbox struct {
WorkDir string
}
ProcessSandbox runs commands in a subprocess with timeouts. For production, replace with container-based isolation (gVisor, Firecracker, etc.).
func NewProcessSandbox ¶
func NewProcessSandbox(workDir string) *ProcessSandbox
func (*ProcessSandbox) Close ¶
func (s *ProcessSandbox) Close() error
type Result ¶
type Result struct {
Stdout string `json:"stdout"`
Stderr string `json:"stderr"`
ExitCode int `json:"exit_code"`
}
Result captures the output of a sandboxed execution.
type Sandbox ¶
type Sandbox interface {
Execute(ctx context.Context, command string, args []string, timeout time.Duration) (*Result, error)
Close() error
}
Sandbox defines the interface for executing untrusted code.
func NewFromConfig ¶ added in v0.3.0
NewFromConfig creates a sandbox based on the configuration. This is the factory function that selects the backend by config string.
type WASMConfig ¶ added in v0.7.0
WASMConfig mirrors the real WASMConfig (see wasm.go, built with -tags sandbox_wasm) so callers compile identically either way.
type WASMSandbox ¶ added in v0.3.0
type WASMSandbox struct{}
WASMSandbox is a stub in the default build; see wasm.go under -tags sandbox_wasm for the real wazero-backed implementation.
func NewWASMSandbox ¶ added in v0.3.0
func NewWASMSandbox(wasmPath string) (*WASMSandbox, error)
NewWASMSandbox is disabled in the default build to keep the wazero runtime out of the binary — see chronos-code's ROADMAP.md "binary size" goal. Rebuild with -tags sandbox_wasm to enable the WASM sandbox backend.
func NewWASMSandboxWithConfig ¶ added in v0.7.0
func NewWASMSandboxWithConfig(cfg WASMConfig) (*WASMSandbox, error)
NewWASMSandboxWithConfig is disabled in the default build; see NewWASMSandbox.
func (*WASMSandbox) Close ¶ added in v0.3.0
func (s *WASMSandbox) Close() error