Skip to content

Secure, Fast, and Extensible Sandbox runtime for AI agents.

16.5 KB
Raw Blame
// Copyright 2026 The OpenSandbox Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package opensandbox

import (
	"context"
	"fmt"
	"net/http"
	"net/url"
	"strconv"
	"time"
)

// LifecycleClient provides methods for the OpenSandbox Lifecycle API.
type LifecycleClient struct {
	*Client
	cache *EndpointCache
}

// NewLifecycleClient creates a new LifecycleClient.
// baseURL should include the version prefix (e.g. "http://localhost:8080/v1").
func NewLifecycleClient(baseURL, apiKey string, opts ...Option) *LifecycleClient {
	return &LifecycleClient{
		Client: NewClient(baseURL, apiKey, "OPEN-SANDBOX-API-KEY", opts...),
		cache:  NewEndpointCache(0, 0),
	}
}

// NewLifecycleClientWithCache creates a LifecycleClient with custom cache settings.
func NewLifecycleClientWithCache(baseURL, apiKey string, cache *EndpointCache, opts ...Option) *LifecycleClient {
	return &LifecycleClient{
		Client: NewClient(baseURL, apiKey, "OPEN-SANDBOX-API-KEY", opts...),
		cache:  cache,
	}
}

// ListOptions configures filtering and pagination for ListSandboxes.
type ListOptions struct {
	// States filters by lifecycle state. Multiple values use OR logic.
	States []SandboxState
	// Metadata filters by key-value metadata (AND logic).
	Metadata map[string]string
	// Page number (1-based). Defaults to 1.
	Page int
	// PageSize is the number of items per page. Defaults to 20.
	PageSize int
}

// ListSandboxes returns a paginated list of sandboxes with optional filtering.
func (c *LifecycleClient) ListSandboxes(ctx context.Context, opts ListOptions) (*ListSandboxesResponse, error) {
	params := url.Values{}
	for _, s := range opts.States {
		params.Add("state", string(s))
	}
	if len(opts.Metadata) > 0 {
		metaVals := url.Values{}
		for k, v := range opts.Metadata {
			metaVals.Set(k, v)
		}
		params.Set("metadata", metaVals.Encode())
	}
	if opts.Page > 0 {
		params.Set("page", strconv.Itoa(opts.Page))
	}
	if opts.PageSize > 0 {
		params.Set("pageSize", strconv.Itoa(opts.PageSize))
	}

	path := "/sandboxes"
	if encoded := params.Encode(); encoded != "" {
		path += "?" + encoded
	}

	var resp ListSandboxesResponse
	if err := c.doRequest(ctx, "GET", path, nil, &resp); err != nil {
		return nil, err
	}
	return &resp, nil
}

// CreateSandbox creates a new sandbox from a container image.
func (c *LifecycleClient) CreateSandbox(ctx context.Context, req CreateSandboxRequest) (*SandboxInfo, error) {
	if req.Lifecycle != nil {
		if err := req.Lifecycle.IdlePolicy.Validate(); err != nil {
			return nil, err
		}
		if err := req.Lifecycle.RenewalPolicy.Validate(); err != nil {
			return nil, err
		}
	}
	var resp SandboxInfo
	if err := c.doRequest(ctx, "POST", "/sandboxes", req, &resp); err != nil {
		return nil, err
	}
	return &resp, nil
}

// CreateTemplate declares a new fsb template (golden-image build).
// The build runs asynchronously: the response starts at
// TemplatePhasePending; poll GetTemplate until the phase is Succeeded (or
// Failed). Only Succeeded templates can back template-based sandbox
// creation.
func (c *LifecycleClient) CreateTemplate(ctx context.Context, req CreateTemplateRequest) (*TemplateInfo, error) {
	var resp TemplateInfo
	if err := c.doRequest(ctx, "POST", "/templates", req, &resp); err != nil {
		return nil, err
	}
	return &resp, nil
}

// GetTemplate retrieves one template with its latest build status by ID.
func (c *LifecycleClient) GetTemplate(ctx context.Context, templateID string) (*TemplateInfo, error) {
	var resp TemplateInfo
	if err := c.doRequest(ctx, "GET", "/templates/"+url.PathEscape(templateID), nil, &resp); err != nil {
		return nil, err
	}
	return &resp, nil
}

// ListTemplates returns a paginated list of templates with optional filtering.
func (c *LifecycleClient) ListTemplates(ctx context.Context, opts ListTemplatesOptions) (*ListTemplatesResponse, error) {
	params := url.Values{}
	if len(opts.Metadata) > 0 {
		metaVals := url.Values{}
		for k, v := range opts.Metadata {
			metaVals.Set(k, v)
		}
		params.Set("metadata", metaVals.Encode())
	}
	if opts.Page > 0 {
		params.Set("page", strconv.Itoa(opts.Page))
	}
	if opts.PageSize > 0 {
		params.Set("pageSize", strconv.Itoa(opts.PageSize))
	}

	path := "/templates"
	if encoded := params.Encode(); encoded != "" {
		path += "?" + encoded
	}

	var resp ListTemplatesResponse
	if err := c.doRequest(ctx, "GET", path, nil, &resp); err != nil {
		return nil, err
	}
	return &resp, nil
}

// DeleteTemplate deletes a template by ID. Sandboxes already created from the
// template are unaffected.
func (c *LifecycleClient) DeleteTemplate(ctx context.Context, templateID string) error {
	return c.doRequest(ctx, "DELETE", "/templates/"+url.PathEscape(templateID), nil, nil)
}

// GetNetworkPolicy returns the sandbox's egress policy intent from the
// lifecycle control plane. This is the template-backed path for egress
// policy: fsb sandboxes have no sandbox-side egress sidecar.
func (c *LifecycleClient) GetNetworkPolicy(ctx context.Context, sandboxID string) (*PolicyStatusResponse, error) {
	var resp PolicyStatusResponse
	if err := c.doRequest(ctx, "GET", "/sandboxes/"+url.PathEscape(sandboxID)+"/networkpolicy", nil, &resp); err != nil {
		return nil, err
	}
	return &resp, nil
}

// PatchNetworkPolicy merges egress rules into the sandbox's policy using the
// same semantics as the sandbox-side egress service: incoming rules take
// priority over existing rules with the same target and replace them in
// place; within one patch payload, the first rule for a target wins;
// existing rules for other targets and the current defaultAction are
// preserved.
func (c *LifecycleClient) PatchNetworkPolicy(ctx context.Context, sandboxID string, rules []NetworkRule) (*PolicyStatusResponse, error) {
	var resp PolicyStatusResponse
	if err := c.doRequest(ctx, "PATCH", "/sandboxes/"+url.PathEscape(sandboxID)+"/networkpolicy", rules, &resp); err != nil {
		return nil, err
	}
	return &resp, nil
}

// DeleteNetworkPolicyRules removes the sandbox's egress rules whose target
// matches one of the given entries. Matching is by exact target string;
// unknown targets are silently ignored (idempotent). The current
// defaultAction is preserved.
func (c *LifecycleClient) DeleteNetworkPolicyRules(ctx context.Context, sandboxID string, targets []string) (*PolicyStatusResponse, error) {
	var resp PolicyStatusResponse
	if err := c.doRequest(ctx, "DELETE", "/sandboxes/"+url.PathEscape(sandboxID)+"/networkpolicy", targets, &resp); err != nil {
		return nil, err
	}
	return &resp, nil
}

// vaultPath builds the server-routed Credential Vault path for a sandbox.
func vaultPath(sandboxID, suffix string) string {
	return "/sandboxes/" + url.PathEscape(sandboxID) + "/credential-vault" + suffix
}

// CreateCredentialVault creates the initial server-routed Credential Vault
// revision (create-only: 409 when a vault already exists). Inline credential
// values are write-only and are never returned on any read. This is the
// template-backed path for the vault (OSEP-0022): the server relays to the
// egress-held vault and the relay response is the synchronous ack.
func (c *LifecycleClient) CreateCredentialVault(ctx context.Context, sandboxID string, req CredentialVaultCreateRequest) (*CredentialVaultState, error) {
	var resp CredentialVaultState
	if err := c.doRequest(ctx, "POST", vaultPath(sandboxID, ""), req, &resp); err != nil {
		return nil, err
	}
	return &resp, nil
}

// GetCredentialVault returns the sanitized server-routed Credential Vault
// state. Credential values are never returned on any read surface; readback
// after a lost mutation ack resolves the current revision (retry anchor 412).
func (c *LifecycleClient) GetCredentialVault(ctx context.Context, sandboxID string) (*CredentialVaultState, error) {
	var resp CredentialVaultState
	if err := c.doRequest(ctx, "GET", vaultPath(sandboxID, ""), nil, &resp); err != nil {
		return nil, err
	}
	return &resp, nil
}

// PatchCredentialVault atomically mutates server-routed credentials and
// bindings (delete → replace → add per set; every mentioned name must resolve
// exactly once). expectedRevision, when set, must match the current revision
// (412 otherwise): re-read and retry with the fresh revision.
func (c *LifecycleClient) PatchCredentialVault(ctx context.Context, sandboxID string, req CredentialVaultPatchRequest) (*CredentialVaultState, error) {
	var resp CredentialVaultState
	if err := c.doRequest(ctx, "PATCH", vaultPath(sandboxID, ""), req, &resp); err != nil {
		return nil, err
	}
	return &resp, nil
}

// DeleteCredentialVault deletes the server-routed Credential Vault; the
// network policy is kept.
func (c *LifecycleClient) DeleteCredentialVault(ctx context.Context, sandboxID string) error {
	return c.doRequest(ctx, "DELETE", vaultPath(sandboxID, ""), nil, nil)
}

// ListCredentialVaultCredentials returns sanitized credential metadata; values
// are never returned.
func (c *LifecycleClient) ListCredentialVaultCredentials(ctx context.Context, sandboxID string) (*CredentialListResponse, error) {
	var resp CredentialListResponse
	if err := c.doRequest(ctx, "GET", vaultPath(sandboxID, "/credentials"), nil, &resp); err != nil {
		return nil, err
	}
	return &resp, nil
}

// GetCredentialVaultCredential returns sanitized metadata for one credential.
func (c *LifecycleClient) GetCredentialVaultCredential(ctx context.Context, sandboxID, name string) (*CredentialMetadata, error) {
	var resp CredentialMetadata
	if err := c.doRequest(ctx, "GET", vaultPath(sandboxID, "/credentials/"+url.PathEscape(name)), nil, &resp); err != nil {
		return nil, err
	}
	return &resp, nil
}

// ListCredentialVaultBindings returns sanitized binding metadata.
func (c *LifecycleClient) ListCredentialVaultBindings(ctx context.Context, sandboxID string) (*CredentialBindingListResponse, error) {
	var resp CredentialBindingListResponse
	if err := c.doRequest(ctx, "GET", vaultPath(sandboxID, "/bindings"), nil, &resp); err != nil {
		return nil, err
	}
	return &resp, nil
}

// GetCredentialVaultBinding returns sanitized metadata for one binding.
func (c *LifecycleClient) GetCredentialVaultBinding(ctx context.Context, sandboxID, name string) (*CredentialBindingMetadata, error) {
	var resp CredentialBindingMetadata
	if err := c.doRequest(ctx, "GET", vaultPath(sandboxID, "/bindings/"+url.PathEscape(name)), nil, &resp); err != nil {
		return nil, err
	}
	return &resp, nil
}

// ListSnapshots returns a paginated list of snapshots with optional filtering.
func (c *LifecycleClient) ListSnapshots(ctx context.Context, opts ListSnapshotsOptions) (*ListSnapshotsResponse, error) {
	params := url.Values{}
	if opts.SandboxID != "" {
		params.Set("sandboxId", opts.SandboxID)
	}
	if opts.Name != "" {
		params.Set("name", opts.Name)
	}
	for _, s := range opts.States {
		params.Add("state", string(s))
	}
	if opts.Page > 0 {
		params.Set("page", strconv.Itoa(opts.Page))
	}
	if opts.PageSize > 0 {
		params.Set("pageSize", strconv.Itoa(opts.PageSize))
	}

	path := "/snapshots"
	if encoded := params.Encode(); encoded != "" {
		path += "?" + encoded
	}

	var resp ListSnapshotsResponse
	if err := c.doRequest(ctx, "GET", path, nil, &resp); err != nil {
		return nil, err
	}
	return &resp, nil
}

// CreateSnapshot creates a snapshot from the given sandbox.
func (c *LifecycleClient) CreateSnapshot(ctx context.Context, sandboxID string, req CreateSnapshotRequest) (*SnapshotInfo, error) {
	var resp SnapshotInfo
	if err := c.doRequest(ctx, "POST", "/sandboxes/"+url.PathEscape(sandboxID)+"/snapshots", req, &resp); err != nil {
		return nil, err
	}
	return &resp, nil
}

// GetSnapshot retrieves snapshot information by ID.
func (c *LifecycleClient) GetSnapshot(ctx context.Context, id string) (*SnapshotInfo, error) {
	var resp SnapshotInfo
	if err := c.doRequest(ctx, "GET", "/snapshots/"+url.PathEscape(id), nil, &resp); err != nil {
		return nil, err
	}
	return &resp, nil
}

// DeleteSnapshot deletes a snapshot by ID.
func (c *LifecycleClient) DeleteSnapshot(ctx context.Context, id string) error {
	return c.doRequest(ctx, "DELETE", "/snapshots/"+url.PathEscape(id), nil, nil)
}

// GetSandbox retrieves the complete sandbox information by ID.
func (c *LifecycleClient) GetSandbox(ctx context.Context, id string) (*SandboxInfo, error) {
	var resp SandboxInfo
	if err := c.doRequest(ctx, "GET", "/sandboxes/"+url.PathEscape(id), nil, &resp); err != nil {
		return nil, err
	}
	return &resp, nil
}

// PatchSandboxMetadata patches metadata for a sandbox. Non-nil values add or
// replace keys. Nil values delete keys.
func (c *LifecycleClient) PatchSandboxMetadata(ctx context.Context, id string, patch MetadataPatch) (*SandboxInfo, error) {
	var resp SandboxInfo
	if err := c.doRequest(ctx, "PATCH", "/sandboxes/"+url.PathEscape(id)+"/metadata", patch, &resp); err != nil {
		return nil, err
	}
	return &resp, nil
}

// DeleteSandbox requests irreversible sandbox termination.
// Returns when deletion is accepted; runtime cleanup may continue afterward.
func (c *LifecycleClient) DeleteSandbox(ctx context.Context, id string) error {
	return c.doRequest(ctx, "DELETE", "/sandboxes/"+url.PathEscape(id), nil, nil)
}

// PauseSandbox pauses a running sandbox while preserving its state.
func (c *LifecycleClient) PauseSandbox(ctx context.Context, id string) error {
	return c.doRequest(ctx, "POST", "/sandboxes/"+url.PathEscape(id)+"/pause", nil, nil)
}

// ResumeSandbox resumes a paused sandbox.
func (c *LifecycleClient) ResumeSandbox(ctx context.Context, id string) error {
	return c.doRequest(ctx, "POST", "/sandboxes/"+url.PathEscape(id)+"/resume", nil, nil)
}

// RenewExpiration renews the sandbox's absolute expiration time.
// The caller is responsible for computing the desired expiresAt time.
func (c *LifecycleClient) RenewExpiration(ctx context.Context, id string, expiresAt time.Time) (*RenewExpirationResponse, error) {
	req := RenewExpirationRequest{
		ExpiresAt: expiresAt.UTC(),
	}
	var resp RenewExpirationResponse
	if err := c.doRequest(ctx, "POST", "/sandboxes/"+url.PathEscape(id)+"/renew-expiration", req, &resp); err != nil {
		return nil, err
	}
	return &resp, nil
}

// GetEndpoint retrieves the public access endpoint for a service running
// on the specified port inside the sandbox. Results are cached with LRU+TTL
// and deduplicated via singleflight. If useServerProxy is non-nil,
// the server proxy query parameter is included.
func (c *LifecycleClient) GetEndpoint(ctx context.Context, sandboxID string, port int, useServerProxy *bool) (*Endpoint, error) {
	if c.cache == nil {
		return c.getEndpointFromServer(ctx, sandboxID, port, useServerProxy)
	}

	key := endpointCacheKey{
		sandboxID:      sandboxID,
		port:           port,
		useServerProxy: useServerProxy != nil && *useServerProxy,
	}

	// The shared fetch uses a background context so one caller's deadline
	// doesn't cancel the request for all waiters. Each caller's ctx is
	// respected via DoChan select in GetOrFetch.
	return c.cache.GetOrFetch(ctx, key, func() (*Endpoint, error) {
		return c.getEndpointFromServer(context.Background(), sandboxID, port, useServerProxy)
	})
}

func (c *LifecycleClient) getEndpointFromServer(ctx context.Context, sandboxID string, port int, useServerProxy *bool) (*Endpoint, error) {
	path := fmt.Sprintf("/sandboxes/%s/endpoints/%d", url.PathEscape(sandboxID), port)
	params := url.Values{}
	if useServerProxy != nil {
		params.Set("use_server_proxy", strconv.FormatBool(*useServerProxy))
	}
	if encoded := params.Encode(); encoded != "" {
		path += "?" + encoded
	}
	var resp Endpoint
	if err := c.doRequestCapture(ctx, "GET", path, nil, &resp, func(h http.Header) {
		resp.Origin = SandboxOrigin(h.Get(SandboxOriginHeader))
	}); err != nil {
		return nil, err
	}
	return &resp, nil
}

// GetSignedEndpoint retrieves a cryptographically signed endpoint URL for a
// sandbox port. The returned endpoint embeds an OSEP-0011 signed route token
// that expires at the given Unix epoch timestamp (seconds).
func (c *LifecycleClient) GetSignedEndpoint(ctx context.Context, sandboxID string, port int, expires int64) (*Endpoint, error) {
	path := fmt.Sprintf("/sandboxes/%s/endpoints/%d?expires=%d", url.PathEscape(sandboxID), port, expires)
	var resp Endpoint
	if err := c.doRequestCapture(ctx, "GET", path, nil, &resp, func(h http.Header) {
		resp.Origin = SandboxOrigin(h.Get(SandboxOriginHeader))
	}); err != nil {
		return nil, err
	}
	return &resp, nil
}