store

package
v0.7.2 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2026 License: MIT Imports: 37 Imported by: 0

Documentation

Overview

Package store provides SQLite-backed persistence for the dashboard, using an Ent client for queries and Atlas-generated versioned migrations. It depends only on the model leaf package so the dashboard package can import it for wiring without an import cycle.

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrGroupNotFound    = errors.New("group not found")
	ErrInstanceNotFound = errors.New("instance not found")
)

ErrGroupNotFound / ErrInstanceNotFound signal unknown IDs to handlers.

Functions

func Migrate

func Migrate(ctx context.Context, db *sql.DB) error

Migrate applies all pending versioned migrations, tracked in schema_migrations so re-running is a no-op. It must run (and succeed) before the HTTP server starts serving; callers should treat an error as fatal rather than serve against a half-migrated database.

func Open

func Open(ctx context.Context, path string) (*ent.Client, *sql.DB, error)

Open opens (creating if needed) the SQLite database at path with WAL and busy-timeout pragmas suitable for concurrent readers with a single writer, and wraps it in an Ent client. The returned *sql.DB is the same underlying handle (used by Migrate); closing the client closes it.

Types

type APIKeyStore

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

APIKeyStore is the SQLite-backed API key store. Active key hashes are cached in memory so the ingest hot path never hits the database.

func NewAPIKeyStore

func NewAPIKeyStore(ctx context.Context, client *ent.Client, lg *slog.Logger) (*APIKeyStore, error)

NewAPIKeyStore creates the store and loads the active-key cache.

func (*APIKeyStore) Create

func (s *APIKeyStore) Create(ctx context.Context, label string) (string, model.APIKey, error)

Create mints a new key, returning the plaintext exactly once.

func (*APIKeyStore) List

func (s *APIKeyStore) List(ctx context.Context) ([]model.APIKey, error)

List returns all keys, active and revoked.

func (*APIKeyStore) Revoke

func (s *APIKeyStore) Revoke(ctx context.Context, id string) error

Revoke marks a key revoked and drops it from the active cache.

func (*APIKeyStore) Seed

func (s *APIKeyStore) Seed(ctx context.Context, label, key string) (bool, error)

Seed idempotently inserts an externally supplied key (the API_KEY env bootstrap). The bool reports whether this call inserted it. If the key already exists - including revoked - it is left untouched, so a revocation in the database sticks across restarts.

func (*APIKeyStore) Validate

func (s *APIKeyStore) Validate(_ context.Context, presented string) (string, bool)

Validate reports whether the presented key matches an active key. Comparison is constant-time over the cached hashes; the database is only touched asynchronously to record last_used_at.

type FleetStore

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

FleetStore is the SQLite-backed instance/group control plane.

func NewFleetStore

func NewFleetStore(client *ent.Client, lg *slog.Logger) *FleetStore

NewFleetStore creates a SQLite-backed fleet store.

func (*FleetStore) CreateGroup

func (s *FleetStore) CreateGroup(ctx context.Context, name string) (model.Group, error)

CreateGroup creates an empty group with the given name.

func (*FleetStore) DeleteGroup

func (s *FleetStore) DeleteGroup(ctx context.Context, id string) error

DeleteGroup removes a group; member instances fall back to unmanaged.

func (*FleetStore) DeleteInstance

func (s *FleetStore) DeleteInstance(ctx context.Context, id string) error

DeleteInstance removes an instance record.

func (*FleetStore) ListGroups

func (s *FleetStore) ListGroups(ctx context.Context) ([]model.Group, error)

ListGroups returns all groups.

func (*FleetStore) ListInstances

func (s *FleetStore) ListInstances(ctx context.Context) ([]model.Instance, error)

ListInstances returns all instances with resolved group + sync state.

func (*FleetStore) Reconcile

func (s *FleetStore) Reconcile(ctx context.Context, rep model.SyncReport) (model.DesiredConfig, error)

Reconcile records an instance report and returns its resolved desired config.

func (*FleetStore) SetGroupPolicy

func (s *FleetStore) SetGroupPolicy(ctx context.Context, id, policy, filterMode string) error

SetGroupPolicy updates a group's policy and optional filter mode.

func (*FleetStore) SetInstanceGroup

func (s *FleetStore) SetInstanceGroup(ctx context.Context, id, groupID string) error

SetInstanceGroup assigns (groupID != "") or clears an instance's group.

func (*FleetStore) SetInstancePolicy

func (s *FleetStore) SetInstancePolicy(ctx context.Context, id string, policy *string) error

SetInstancePolicy sets (non-nil) or clears (nil) an instance policy override.

type LogStore

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

LogStore is an Ent-backed implementation of the dashboard LogStore interface, used when DB_PATH is set. Pruning is amortized (every logPruneEvery inserts), so the row count settles around `retention` but can transiently exceed it by up to logPruneEvery-1 rows between prunes.

func NewLogStore

func NewLogStore(client *ent.Client, retention int) *LogStore

NewLogStore creates a SQLite-backed log store with the given row retention.

func (*LogStore) Aggregate added in v0.7.2

func (s *LogStore) Aggregate(
	ctx context.Context, from, to time.Time, q string, buckets int,
) (model.AggregateResult, error)

Aggregate summarizes every retained row in the requested time window.

func (*LogStore) Clear

func (s *LogStore) Clear(ctx context.Context) error

Clear removes all stored logs.

func (*LogStore) Insert

func (s *LogStore) Insert(ctx context.Context, e *model.LogEntry) (int64, error)

Insert stores an entry and returns its assigned ID.

func (*LogStore) Query

func (s *LogStore) Query(ctx context.Context, q string, sinceID int64, limit int) ([]model.LogEntry, error)

Query returns entries matching q (substring over msg+fields) with ID greater than sinceID, newest first, capped at limit. IDs come from the row id.

type SessionStore

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

SessionStore is the SQLite-backed session store. Only the SHA-256 of each token is persisted, so a database read cannot hijack live sessions.

func NewSessionStore

func NewSessionStore(client *ent.Client, lg *slog.Logger) *SessionStore

NewSessionStore creates a SQLite-backed session store.

func (*SessionStore) Create

func (s *SessionStore) Create(ctx context.Context, user model.User, ttl time.Duration) (string, error)

Create mints a session for the user and returns the opaque token.

func (*SessionStore) GC

func (s *SessionStore) GC(ctx context.Context) error

GC prunes expired sessions.

func (*SessionStore) Lookup

func (s *SessionStore) Lookup(ctx context.Context, token string) (model.Session, bool)

Lookup resolves a token to its session, if valid and unexpired.

func (*SessionStore) Revoke

func (s *SessionStore) Revoke(ctx context.Context, token string) error

Revoke deletes the session for the given token.

type UnblockStore

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

UnblockStore is the SQLite-backed implementation of the dashboard's UnblockStore interface. The interface predates persistence and carries no context or error returns, so failures are logged and reported as zero values.

func NewUnblockStore

func NewUnblockStore(client *ent.Client, lg *slog.Logger) *UnblockStore

NewUnblockStore creates a SQLite-backed unblock store.

func (*UnblockStore) Acknowledge

func (s *UnblockStore) Acknowledge(id string) bool

Acknowledge moves a pending request to the completed list. Returns false if the ID is unknown or on error.

func (*UnblockStore) Add

func (s *UnblockStore) Add(reqType, value, targetHostname string) string

Add queues an unblock request, deduplicating on (type, value, target). Returns the request ID, or "" when the store is full or on error.

func (*UnblockStore) GetCompleted

func (s *UnblockStore) GetCompleted() []model.CompletedUnblock

GetCompleted returns the bounded list of completed unblocks, oldest first.

func (*UnblockStore) GetPending

func (s *UnblockStore) GetPending() []model.UnblockRequest

GetPending returns all pending unblock requests.

func (*UnblockStore) GetPendingForHost

func (s *UnblockStore) GetPendingForHost(hostname string) []model.UnblockRequest

GetPendingForHost returns pending requests targeting the given hostname or all hosts.

type UserStore

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

UserStore is the SQLite-backed user store.

func NewUserStore

func NewUserStore(client *ent.Client, lg *slog.Logger) *UserStore

NewUserStore creates a SQLite-backed user store.

func (*UserStore) Count

func (s *UserStore) Count(ctx context.Context) (int64, error)

Count returns the number of users.

func (*UserStore) Upsert

func (s *UserStore) Upsert(ctx context.Context, username, passwordHash string) error

Upsert inserts the user or updates the password hash for an existing username.

func (*UserStore) VerifyPassword

func (s *UserStore) VerifyPassword(ctx context.Context, username, password string) (model.User, bool)

VerifyPassword checks credentials, with uniform timing for unknown users.

Jump to

Keyboard shortcuts

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