Documentation
¶
Overview ¶
Package store persists clusters, snapshots and evaluations for the upgradescope server. Store is the seam P3's Postgres implementation fills in; P2 ships the SQLite implementation in this package.
Index ¶
- Variables
- func HashToken(token string) string
- func Migrate(ctx context.Context, db *sql.DB, fsys fs.FS) ([]string, error)
- type Cluster
- type Evaluation
- type Postgres
- func (p *Postgres) Close() error
- func (p *Postgres) CreateToken(ctx context.Context, clusterName, token string) error
- func (p *Postgres) GetCluster(ctx context.Context, id int64) (Cluster, error)
- func (p *Postgres) InsertEvaluation(ctx context.Context, e Evaluation) (int64, error)
- func (p *Postgres) InsertSnapshot(ctx context.Context, snap Snapshot) (int64, bool, error)
- func (p *Postgres) LatestEvaluation(ctx context.Context, clusterID int64, target string) (Evaluation, error)
- func (p *Postgres) LatestSnapshot(ctx context.Context, clusterID int64) (Snapshot, error)
- func (p *Postgres) ListClusters(ctx context.Context) ([]Cluster, error)
- func (p *Postgres) RevokeToken(ctx context.Context, clusterName string) error
- func (p *Postgres) ScoreHistory(ctx context.Context, clusterID int64, target string, limit int) ([]ScorePoint, error)
- func (p *Postgres) UpsertCluster(ctx context.Context, c Cluster) (int64, error)
- func (p *Postgres) ValidToken(ctx context.Context, token string) (string, bool, error)
- type SQLite
- func (s *SQLite) Close() error
- func (s *SQLite) CreateToken(ctx context.Context, clusterName, token string) error
- func (s *SQLite) GetCluster(ctx context.Context, id int64) (Cluster, error)
- func (s *SQLite) InsertEvaluation(ctx context.Context, e Evaluation) (int64, error)
- func (s *SQLite) InsertSnapshot(ctx context.Context, snap Snapshot) (int64, bool, error)
- func (s *SQLite) LatestEvaluation(ctx context.Context, clusterID int64, target string) (Evaluation, error)
- func (s *SQLite) LatestSnapshot(ctx context.Context, clusterID int64) (Snapshot, error)
- func (s *SQLite) ListClusters(ctx context.Context) ([]Cluster, error)
- func (s *SQLite) RevokeToken(ctx context.Context, clusterName string) error
- func (s *SQLite) ScoreHistory(ctx context.Context, clusterID int64, target string, limit int) ([]ScorePoint, error)
- func (s *SQLite) UpsertCluster(ctx context.Context, c Cluster) (int64, error)
- func (s *SQLite) ValidToken(ctx context.Context, token string) (string, bool, error)
- type ScorePoint
- type Snapshot
- type Store
Constants ¶
This section is empty.
Variables ¶
var ErrNotFound = errors.New("store: not found")
ErrNotFound is returned (possibly wrapped) by lookups that match no row. Test with errors.Is.
Functions ¶
func HashToken ¶
HashToken is the storage form of an ingest token: lowercase hex sha256. Exported so callers (CLI, tests) can locate rows without the plaintext.
func Migrate ¶
Migrate applies every "*.sql" file in fsys that has not been applied yet, in lexicographic filename order. Each migration runs in its own transaction and its filename is recorded in schema_migrations inside that same transaction, so a failed migration leaves no trace. Running Migrate again is a no-op for already-recorded files. Returns the filenames applied on this run.
Migrate speaks '?' placeholders (SQLite); the Postgres store calls migrate with postgresMigrations. applied_at stays TEXT (RFC 3339) in both backends — it is bookkeeping written and read only by this file.
Types ¶
type Evaluation ¶
type Evaluation struct {
ID int64 `json:"id"`
ClusterID int64 `json:"clusterId"`
SnapshotID int64 `json:"snapshotId"`
Target string `json:"target"`
KBVersion string `json:"kbVersion"`
Score int `json:"score"`
Ready bool `json:"ready"`
Blockers int `json:"blockers"`
Warnings int `json:"warnings"`
Report []byte `json:"-"` // full engine.Report JSON
CreatedAt time.Time `json:"createdAt"`
}
type Postgres ¶
type Postgres struct {
// contains filtered or unexported fields
}
Postgres is the Store implementation for fleets: same contract as SQLite (pinned by storetest.RunStoreConformance), backed by jackc/pgx via database/sql. Times are TIMESTAMPTZ columns; every time.Time returned is normalized to UTC.
func OpenPostgres ¶
OpenPostgres connects to dsn (any pgx-accepted form, e.g. postgres://user:pass@host:5432/db), verifies the connection with a ping, and applies the embedded pgmigrations. Fails fast on unreachable or unauthorized servers instead of deferring the error to the first query.
func (*Postgres) CreateToken ¶
CreateToken stores a new active ingest token for clusterName; only the sha256 of token is persisted. Fails if the token is already issued.
func (*Postgres) GetCluster ¶
GetCluster returns the cluster by id, or ErrNotFound.
func (*Postgres) InsertEvaluation ¶
InsertEvaluation stores e. Zero CreatedAt defaults to now (UTC).
func (*Postgres) InsertSnapshot ¶
InsertSnapshot stores snap unless its hash equals the hash of the cluster's LATEST snapshot, in which case it returns (latestID, true, nil) without writing. An older same-hash snapshot superseded by a different one does NOT count as a duplicate. Zero ReceivedAt defaults to now (UTC).
Concurrency: the read-latest → insert pair must be serialized per cluster or two racing pushes of the same hash both miss the duplicate and insert twice (READ COMMITTED gives no protection — both SELECTs see the same "latest"). Locking the parent clusters row FOR UPDATE serializes writers of one cluster without blocking other clusters.
func (*Postgres) LatestEvaluation ¶
func (p *Postgres) LatestEvaluation(ctx context.Context, clusterID int64, target string) (Evaluation, error)
LatestEvaluation returns the newest evaluation for (cluster, target) by created_at (ties broken by id), or ErrNotFound.
func (*Postgres) LatestSnapshot ¶
LatestSnapshot returns the most recently inserted snapshot for the cluster (highest id), or ErrNotFound.
func (*Postgres) ListClusters ¶
ListClusters returns all clusters, ascending by name.
func (*Postgres) RevokeToken ¶
RevokeToken revokes every active token of clusterName, or ErrNotFound when the cluster has none.
func (*Postgres) ScoreHistory ¶
func (p *Postgres) ScoreHistory(ctx context.Context, clusterID int64, target string, limit int) ([]ScorePoint, error)
ScoreHistory returns score points for (cluster, target), oldest-first ascending by created_at. limit > 0 selects the most recent N rows (still returned oldest-first); limit <= 0 returns all. An unknown cluster or target yields an empty slice and nil error.
func (*Postgres) UpsertCluster ¶
UpsertCluster inserts the cluster or, if a row with the same name exists, updates cluster_uid and last_seen (first_seen never moves). Zero FirstSeen/LastSeen default to time.Now().UTC().
type SQLite ¶
type SQLite struct {
// contains filtered or unexported fields
}
SQLite is the Store implementation backed by a single SQLite database file (modernc.org/sqlite — pure Go, CGO-free).
func Open ¶
Open opens (creating if needed) the database at path and applies all embedded migrations. Every pooled connection gets WAL journaling, a 5s busy timeout and foreign-key enforcement via DSN pragmas.
path must not contain '?' or '#' — it is interpolated into a SQLite URI, where either character corrupts the path.
func (*SQLite) CreateToken ¶
CreateToken stores a new active ingest token for clusterName; only the sha256 of token is persisted. Fails if the token is already issued.
func (*SQLite) GetCluster ¶
GetCluster returns the cluster by id, or ErrNotFound.
func (*SQLite) InsertEvaluation ¶
InsertEvaluation stores e. Zero CreatedAt defaults to now (UTC).
func (*SQLite) InsertSnapshot ¶
InsertSnapshot stores snap unless its hash equals the hash of the cluster's LATEST snapshot, in which case it returns (latestID, true, nil) without writing. An older same-hash snapshot superseded by a different one does NOT count as a duplicate. Zero ReceivedAt defaults to now (UTC).
func (*SQLite) LatestEvaluation ¶
func (s *SQLite) LatestEvaluation(ctx context.Context, clusterID int64, target string) (Evaluation, error)
LatestEvaluation returns the newest evaluation for (cluster, target) by created_at (ties broken by id), or ErrNotFound.
func (*SQLite) LatestSnapshot ¶
LatestSnapshot returns the most recently inserted snapshot for the cluster (highest id), or ErrNotFound.
func (*SQLite) ListClusters ¶
ListClusters returns all clusters, ascending by name.
func (*SQLite) RevokeToken ¶
RevokeToken revokes every active token of clusterName, or ErrNotFound when the cluster has none.
func (*SQLite) ScoreHistory ¶
func (s *SQLite) ScoreHistory(ctx context.Context, clusterID int64, target string, limit int) ([]ScorePoint, error)
ScoreHistory returns score points for (cluster, target), oldest-first ascending by created_at. limit > 0 selects the most recent N rows (still returned oldest-first); limit <= 0 returns all. An unknown cluster or target yields an empty slice and nil error.
func (*SQLite) UpsertCluster ¶
UpsertCluster inserts the cluster or, if a row with the same name exists, updates cluster_uid and last_seen (first_seen never moves). Zero FirstSeen/LastSeen default to time.Now().UTC().
type ScorePoint ¶
type Snapshot ¶
type Snapshot struct {
ID int64 `json:"id"`
ClusterID int64 `json:"clusterId"`
Hash string `json:"hash"` // sha256 of canonical inventory JSON
KBVersion string `json:"kbVersion"`
AgentVersion string `json:"agentVersion"`
ReceivedAt time.Time `json:"receivedAt"`
Inventory []byte `json:"-"` // raw canonical JSON
}
type Store ¶
type Store interface {
UpsertCluster(ctx context.Context, c Cluster) (int64, error) // by name; returns id
InsertSnapshot(ctx context.Context, s Snapshot) (int64, bool, error) // (id, duplicate, err) — duplicate iff same cluster+hash as latest
LatestSnapshot(ctx context.Context, clusterID int64) (Snapshot, error)
ListClusters(ctx context.Context) ([]Cluster, error)
GetCluster(ctx context.Context, id int64) (Cluster, error)
InsertEvaluation(ctx context.Context, e Evaluation) (int64, error)
LatestEvaluation(ctx context.Context, clusterID int64, target string) (Evaluation, error)
ScoreHistory(ctx context.Context, clusterID int64, target string, limit int) ([]ScorePoint, error)
// Per-cluster ingest tokens (P3, spec §8). Tokens are keyed by cluster
// NAME (not id): a token may be minted before the cluster's first push
// registers it. Only the sha256 of the plaintext is ever stored.
CreateToken(ctx context.Context, clusterName, token string) error // errors if the token is already issued (any cluster)
ValidToken(ctx context.Context, token string) (string, bool, error) // (clusterName, true) for active; ("", false, nil) for unknown/revoked
RevokeToken(ctx context.Context, clusterName string) error // revokes ALL active tokens; ErrNotFound when none are active
Close() error
}
Store is the persistence contract. SQLite implements it in P2; P3 adds Postgres. Behavioral semantics are pinned by storetest.RunStoreConformance.