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
- Variables
- func NewID() string
- type AccessToken
- type AuditEvent
- type Expose
- type JoinToken
- type Node
- type PoolState
- type RaftStore
- func (s RaftStore) DeleteRange(min, max uint64) error
- func (s RaftStore) FirstIndex() (uint64, error)
- func (s RaftStore) Get(key []byte) ([]byte, error)
- func (s RaftStore) GetLog(index uint64, l *raft.Log) error
- func (s RaftStore) GetUint64(key []byte) (uint64, error)
- func (s RaftStore) LastIndex() (uint64, error)
- func (s RaftStore) ResetRaft() error
- func (s RaftStore) Set(key, val []byte) error
- func (s RaftStore) SetUint64(key []byte, val uint64) error
- func (s RaftStore) StoreLog(l *raft.Log) error
- func (s RaftStore) StoreLogs(logs []*raft.Log) error
- type Sandbox
- type Snapshot
- type Store
- func (s *Store) AppendAudit(ctx context.Context, events ...AuditEvent) error
- func (s *Store) Close() error
- func (s *Store) Create(ctx context.Context, sb Sandbox) (Sandbox, error)
- func (s *Store) CreateAccessToken(ctx context.Context, t AccessToken) error
- func (s *Store) CreateExpose(ctx context.Context, e Expose) (Expose, error)
- func (s *Store) CreateJoinToken(ctx context.Context, hash string, expires time.Time) error
- func (s *Store) CreateNode(ctx context.Context, n Node) (Node, error)
- func (s *Store) CreateSnapshot(ctx context.Context, snap Snapshot) (Snapshot, error)
- func (s *Store) Delete(ctx context.Context, id string) error
- func (s *Store) DeleteExpose(ctx context.Context, port int) error
- func (s *Store) DeleteSnapshot(ctx context.Context, id string) error
- func (s *Store) FreeNetSlot(ctx context.Context, sandboxID string) error
- func (s *Store) Get(ctx context.Context, id string) (Sandbox, error)
- func (s *Store) GetByName(ctx context.Context, name string) (Sandbox, error)
- func (s *Store) GetExpose(ctx context.Context, sandbox string, port int) (Expose, error)
- func (s *Store) GetSnapshot(ctx context.Context, name string) (Snapshot, error)
- func (s *Store) List(ctx context.Context) ([]Sandbox, error)
- func (s *Store) ListAccessTokens(ctx context.Context) ([]AccessToken, error)
- func (s *Store) ListAudit(ctx context.Context, before int64, limit int) ([]AuditEvent, error)
- func (s *Store) ListExposes(ctx context.Context) ([]Expose, error)
- func (s *Store) ListNodes(ctx context.Context) ([]Node, error)
- func (s *Store) ListSnapshots(ctx context.Context) ([]Snapshot, error)
- func (s *Store) NetSlot(ctx context.Context, sandboxID string, n int) (int, error)
- func (s *Store) NetSlots(ctx context.Context) (map[string]int, error)
- func (s *Store) PoolState(ctx context.Context) (PoolState, error)
- func (s *Store) Raft() RaftStore
- func (s *Store) RevokeAccessToken(ctx context.Context, id string) error
- func (s *Store) RevokeJoinToken(ctx context.Context, hash string) error
- func (s *Store) SetNodeReport(ctx context.Context, id string, report []byte, seen time.Time) error
- func (s *Store) SetPoolState(ctx context.Context, state PoolState) error
- func (s *Store) SetState(ctx context.Context, id string, state backend.State) error
- func (s *Store) UseAccessToken(ctx context.Context, hash string, now time.Time) (AccessToken, error)
- func (s *Store) UseJoinToken(ctx context.Context, hash string, now time.Time) error
Constants ¶
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.
const AuditRetention = 90 * 24 * time.Hour
AuditRetention is how long this head keeps audit events.
Variables ¶
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") )
var ( ErrTokenInvalid = errors.New("token is invalid or already used") ErrTokenExpired = errors.New("token expired") )
var ErrExposeNotFound = errors.New("not exposed")
ErrExposeNotFound is returned for a sandbox port that is not exposed.
var ErrNoSlots = errors.New("no free network slots")
ErrNoSlots is returned by NetSlot when every slot is taken.
Functions ¶
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.
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 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 ¶
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 ¶
DeleteRange deletes the raft log entries from min to max inclusive.
func (RaftStore) FirstIndex ¶
FirstIndex returns the first raft log index, 0 when the log is empty.
func (RaftStore) GetLog ¶
GetLog reads the raft log entry at index. AppendedAt, which raft only uses for metrics, is not kept.
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 (*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) CreateAccessToken ¶
func (s *Store) CreateAccessToken(ctx context.Context, t AccessToken) error
CreateAccessToken stores t.
func (*Store) CreateExpose ¶
CreateExpose stores e, setting CreatedAt.
func (*Store) CreateJoinToken ¶
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 ¶
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 ¶
CreateSnapshot stores snap under a new ID and returns it with ID and CreatedAt set.
func (*Store) DeleteExpose ¶
DeleteExpose removes the expose served on the head's port.
func (*Store) DeleteSnapshot ¶
DeleteSnapshot removes the snapshot with the given ID.
func (*Store) FreeNetSlot ¶
FreeNetSlot frees the network slot of the sandbox with the given ID, if it has one.
func (*Store) GetSnapshot ¶
GetSnapshot returns the snapshot with the given name.
func (*Store) ListAccessTokens ¶
func (s *Store) ListAccessTokens(ctx context.Context) ([]AccessToken, error)
ListAccessTokens returns every access token, oldest first.
func (*Store) ListAudit ¶
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 ¶
ListExposes returns every expose, oldest first.
func (*Store) ListSnapshots ¶
ListSnapshots returns all snapshots, oldest first.
func (*Store) NetSlot ¶
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) RevokeAccessToken ¶
RevokeAccessToken deletes the access token with the given ID.
func (*Store) RevokeJoinToken ¶
RevokeJoinToken deletes the unused join token with the given hash.
func (*Store) SetNodeReport ¶
SetNodeReport records the node's heartbeat report, received at seen. It returns ErrNotFound for an unknown node.
func (*Store) SetPoolState ¶
SetPoolState replaces the nodes and join tokens with state's.
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.