store

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2026 License: Apache-2.0 Imports: 17 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 store keeps the node's sandbox records in a SQLite database in the state directory, so the daemon knows what it created after a restart.

Index

Constants

View Source
const AccessIDLen = 12

AccessIDLen is the length of an access token's ID: the start of its hash, which token ls shows and token revoke takes.

View Source
const AuditRetention = 90 * 24 * time.Hour

AuditRetention is how long this head keeps audit events.

Variables

View Source
var (
	ErrNotFound         = errors.New("sandbox not found")
	ErrSnapshotNotFound = errors.New("snapshot not found")
	// ErrNameTaken is wrapped as `sandbox name "web" already exists`.
	ErrNameTaken = errors.New("already exists")
)
View Source
var (
	ErrTokenInvalid = errors.New("token is invalid or already used")
	ErrTokenExpired = errors.New("token expired")
)
View Source
var ErrExposeNotFound = errors.New("not exposed")

ErrExposeNotFound is returned for a sandbox port that is not exposed.

View Source
var ErrNoSlots = errors.New("no free network slots")

ErrNoSlots is returned by NetSlot when every slot is taken.

Functions

func NewID

func NewID() string

Types

type AccessToken

type AccessToken struct {
	Hash      string
	Scope     string
	Sandbox   string
	CreatedAt time.Time
	ExpiresAt time.Time
	LastUsed  time.Time
}

AccessToken is an access token the head issued. A zero ExpiresAt or LastUsed means never.

shortcut: access tokens live only on the head that issued them and are not replicated with raft, so a promoted head has none; add them to PoolState when users run pools of two or more.

func (AccessToken) ID

func (t AccessToken) ID() string

ID is the start of the token's hash.

type AuditEvent

type AuditEvent struct {
	ID      int64     `json:"id"`
	Time    time.Time `json:"time"`
	TokenID string    `json:"token_id"`
	Action  string    `json:"action"`
	Target  string    `json:"target"`
}

AuditEvent records a successful action. TokenID is an access token ID, "local" for the local socket, or "public" for an unauthenticated exposed URL. Targets contain names and IDs, never token secrets or command text. Events live only on the head that recorded them, without raft replication.

type Expose

type Expose struct {
	Port        int
	Sandbox     string
	SandboxPort int
	Public      bool
	TokenID     string
	URL         string
	CreatedAt   time.Time
}

Expose is a sandbox's port that the head serves at URL, from its own loopback port Port. TokenID is the ID of the expose token made for it, "" when it is public. SandboxPort 0 reserves the authenticated desktop service; actual TCP ports are 1 through 65535.

shortcut: exposes live only on the head that made them and are not replicated with raft, like access tokens; add both to PoolState together.

type JoinToken

type JoinToken struct {
	Hash      string
	ExpiresAt time.Time
}

JoinToken is an unused join token: the hash of its secret, and when it expires.

type Node

type Node struct {
	ID       string
	Name     string
	OS       string
	Arch     string
	JoinedAt time.Time
	// Report is the node's last heartbeat as it sent it, and SeenAt when it
	// came: zero before the first.
	Report []byte
	SeenAt time.Time
}

Node is a machine that joined this head's pool.

type PoolState

type PoolState struct {
	Nodes  []Node
	Tokens []JoinToken
}

PoolState is the head's state that raft replicates: the pool's nodes, with their last reports, and its unused join tokens.

type RaftStore

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

RaftStore is raft's LogStore and StableStore in the store's database, on a machine that keeps a replica of the pool's state.

func (RaftStore) DeleteRange

func (s RaftStore) DeleteRange(min, max uint64) error

DeleteRange deletes the raft log entries from min to max inclusive.

func (RaftStore) FirstIndex

func (s RaftStore) FirstIndex() (uint64, error)

FirstIndex returns the first raft log index, 0 when the log is empty.

func (RaftStore) Get

func (s RaftStore) Get(key []byte) ([]byte, error)

Get returns a raft stable value.

func (RaftStore) GetLog

func (s RaftStore) GetLog(index uint64, l *raft.Log) error

GetLog reads the raft log entry at index. AppendedAt, which raft only uses for metrics, is not kept.

func (RaftStore) GetUint64

func (s RaftStore) GetUint64(key []byte) (uint64, error)

GetUint64 returns a raft stable number.

func (RaftStore) LastIndex

func (s RaftStore) LastIndex() (uint64, error)

LastIndex returns the last raft log index, 0 when the log is empty.

func (RaftStore) ResetRaft

func (s RaftStore) ResetRaft() error

ResetRaft deletes the raft log and stable state.

func (RaftStore) Set

func (s RaftStore) Set(key, val []byte) error

Set stores a raft stable value.

func (RaftStore) SetUint64

func (s RaftStore) SetUint64(key []byte, val uint64) error

SetUint64 stores a raft stable number.

func (RaftStore) StoreLog

func (s RaftStore) StoreLog(l *raft.Log) error

StoreLog stores a raft log entry.

func (RaftStore) StoreLogs

func (s RaftStore) StoreLogs(logs []*raft.Log) error

StoreLogs stores raft log entries, replacing any at the same indexes.

type Sandbox

type Sandbox struct {
	ID        string
	Backend   string
	Spec      backend.Spec
	State     backend.State
	CreatedAt time.Time
	// Restart is the restart policy: "always" or "" for none.
	Restart string
}

Sandbox is a sandbox record. Spec.Name is the sandbox's name, unique on this node.

type Snapshot

type Snapshot struct {
	ID        string
	Name      string
	Backend   string
	Sandbox   string
	DiskGB    int
	CreatedAt time.Time
}

Snapshot is a named copy of a sandbox's disk, which new sandboxes on the same backend can start from. Sandbox is the source's name when it was taken.

type Store

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

Store is the node's sandbox database.

func Open

func Open(path string) (*Store, error)

Open opens or creates the database at path and migrates it to the latest schema.

func (*Store) AppendAudit

func (s *Store) AppendAudit(ctx context.Context, events ...AuditEvent) error

AppendAudit appends events and expires old rows in one transaction. There is no update or individual-delete API. Retention is the only deletion.

func (*Store) Close

func (s *Store) Close() error

Close closes the database.

func (*Store) Create

func (s *Store) Create(ctx context.Context, sb Sandbox) (Sandbox, error)

Create stores sb under a new ID and returns it with ID and CreatedAt set.

func (*Store) CreateAccessToken

func (s *Store) CreateAccessToken(ctx context.Context, t AccessToken) error

CreateAccessToken stores t.

func (*Store) CreateExpose

func (s *Store) CreateExpose(ctx context.Context, e Expose) (Expose, error)

CreateExpose stores e, setting CreatedAt.

func (*Store) CreateJoinToken

func (s *Store) CreateJoinToken(ctx context.Context, hash string, expires time.Time) error

CreateJoinToken stores the hash of a join token's secret, valid until expires. Expired hashes are kept until use or revocation so that an unused expired token can be distinguished from an invalid token.

func (*Store) CreateNode

func (s *Store) CreateNode(ctx context.Context, n Node) (Node, error)

CreateNode stores n, under a new ID and join time unless it has an ID, and returns it with ID and JoinedAt set.

func (*Store) CreateSnapshot

func (s *Store) CreateSnapshot(ctx context.Context, snap Snapshot) (Snapshot, error)

CreateSnapshot stores snap under a new ID and returns it with ID and CreatedAt set.

func (*Store) Delete

func (s *Store) Delete(ctx context.Context, id string) error

Delete removes the sandbox with the given ID.

func (*Store) DeleteExpose

func (s *Store) DeleteExpose(ctx context.Context, port int) error

DeleteExpose removes the expose served on the head's port.

func (*Store) DeleteSnapshot

func (s *Store) DeleteSnapshot(ctx context.Context, id string) error

DeleteSnapshot removes the snapshot with the given ID.

func (*Store) FreeNetSlot

func (s *Store) FreeNetSlot(ctx context.Context, sandboxID string) error

FreeNetSlot frees the network slot of the sandbox with the given ID, if it has one.

func (*Store) Get

func (s *Store) Get(ctx context.Context, id string) (Sandbox, error)

Get returns the sandbox with the given ID.

func (*Store) GetByName

func (s *Store) GetByName(ctx context.Context, name string) (Sandbox, error)

GetByName returns the sandbox with the given name.

func (*Store) GetExpose

func (s *Store) GetExpose(ctx context.Context, sandbox string, port int) (Expose, error)

GetExpose returns the expose of port in the named sandbox.

func (*Store) GetSnapshot

func (s *Store) GetSnapshot(ctx context.Context, name string) (Snapshot, error)

GetSnapshot returns the snapshot with the given name.

func (*Store) List

func (s *Store) List(ctx context.Context) ([]Sandbox, error)

List returns all sandboxes, oldest first.

func (*Store) ListAccessTokens

func (s *Store) ListAccessTokens(ctx context.Context) ([]AccessToken, error)

ListAccessTokens returns every access token, oldest first.

func (*Store) ListAudit

func (s *Store) ListAudit(ctx context.Context, before int64, limit int) ([]AuditEvent, error)

ListAudit returns up to limit events, newest first, before the given ID (zero means latest). Reads also expire old rows, including on an idle head.

func (*Store) ListExposes

func (s *Store) ListExposes(ctx context.Context) ([]Expose, error)

ListExposes returns every expose, oldest first.

func (*Store) ListNodes

func (s *Store) ListNodes(ctx context.Context) ([]Node, error)

ListNodes returns every joined node, oldest first.

func (*Store) ListSnapshots

func (s *Store) ListSnapshots(ctx context.Context) ([]Snapshot, error)

ListSnapshots returns all snapshots, oldest first.

func (*Store) NetSlot

func (s *Store) NetSlot(ctx context.Context, sandboxID string, n int) (int, error)

NetSlot returns the network slot of the sandbox with the given ID, allocating the lowest free one below n if it has none. The ID need not be a stored sandbox's: the backend owns slots, and frees them on remove.

func (*Store) NetSlots

func (s *Store) NetSlots(ctx context.Context) (map[string]int, error)

NetSlots returns every allocated slot by sandbox ID.

func (*Store) PoolState

func (s *Store) PoolState(ctx context.Context) (PoolState, error)

PoolState returns the nodes and unused join tokens.

func (*Store) Raft

func (s *Store) Raft() RaftStore

Raft returns the store's raft log and stable state.

func (*Store) RevokeAccessToken

func (s *Store) RevokeAccessToken(ctx context.Context, id string) error

RevokeAccessToken deletes the access token with the given ID.

func (*Store) RevokeJoinToken

func (s *Store) RevokeJoinToken(ctx context.Context, hash string) error

RevokeJoinToken deletes the unused join token with the given hash.

func (*Store) SetNodeReport

func (s *Store) SetNodeReport(ctx context.Context, id string, report []byte, seen time.Time) error

SetNodeReport records the node's heartbeat report, received at seen. It returns ErrNotFound for an unknown node.

func (*Store) SetPoolState

func (s *Store) SetPoolState(ctx context.Context, state PoolState) error

SetPoolState replaces the nodes and join tokens with state's.

func (*Store) SetState

func (s *Store) SetState(ctx context.Context, id string, state backend.State) error

SetState updates the state of the sandbox with the given ID.

func (*Store) UseAccessToken

func (s *Store) UseAccessToken(ctx context.Context, hash string, now time.Time) (AccessToken, error)

UseAccessToken returns the access token with the given hash and records now as its last use. It returns ErrTokenInvalid for an unknown or revoked token, and ErrTokenExpired after it expires.

func (*Store) UseJoinToken

func (s *Store) UseJoinToken(ctx context.Context, hash string, now time.Time) error

UseJoinToken consumes the join token with the given hash: it succeeds once, before the token expires at the time now.

Jump to

Keyboard shortcuts

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