store

package
v1.0.0-alpha.13 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2026 License: Apache-2.0 Imports: 25 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 is the ledger: FOCUS rows in Postgres, one tenant at a time.

Every query runs inside a transaction that names its tenant with SET LOCAL, and row level security does the filtering. No statement here carries a tenant predicate of its own.

Index

Constants

View Source
const (
	HeadFromToken = "token"
	HeadStated    = "stated"
)

Values of HeadBasis.

View Source
const (
	CredentialMinted = "minted"
	CredentialStated = "stated"
)

Values of CredentialBasis.

View Source
const (
	TagRepo          = focus.TagRepo
	TagPR            = focus.TagPR
	TagCommitSHA     = focus.TagCommitSHA
	TagWorkflowRunID = focus.TagWorkflowRunID
)

Tag keys of the convention that the focus package leaves to the join.

View Source
const KeyPrefix = "costroid_ik_"

KeyPrefix starts every ingest key. A bearer without it is a CI token.

Variables

View Source
var ErrAttestedElsewhere = errors.New("store: the key is attested for another run")

ErrAttestedElsewhere means a key is already attested for another run.

View Source
var ErrDatabaseAhead = errors.New("store: database is ahead of this binary")

ErrDatabaseAhead means the database holds a version this binary does not know. Migrations run forward only, so the binary is what has to change.

View Source
var ErrMigrationEdited = errors.New("store: an applied migration was edited")

ErrMigrationEdited means a migration already applied has changed on disk.

View Source
var ErrNotFound = errors.New("store: not found")

ErrNotFound means a lookup named nothing.

View Source
var ErrNotMinted = errors.New("store: the key is not attested as minted")

ErrNotMinted means a spend names a key no attestation holds.

View Source
var ErrUnsafeRole = errors.New("store: unsafe application role")

ErrUnsafeRole means the service was given a role that could read past row level security.

Functions

func AddKey

func AddKey(ctx context.Context, ownerURL string, owner Binding, digest string) error

AddKey stores a key's digest for the tenant holding an SCM owner, through the owner role's connection. An owner no tenant holds answers ErrNotFound.

func KeyDigest

func KeyDigest(key string) string

KeyDigest is the SHA-256 of a key, which is also its public id.

func Migrate

func Migrate(ctx context.Context, ownerURL string) error

Migrate applies the embedded migrations through the owner's connection.

func MigrateFrom

func MigrateFrom(ctx context.Context, ownerURL string, files fs.FS) error

MigrateFrom applies the migrations in files, each in its own transaction.

func NewKey

func NewKey() (string, error)

NewKey mints an ingest key. Only its digest is ever stored, so a key that is lost is revoked and replaced.

func Provision

func Provision(ctx context.Context, ownerURL string, tenant Tenant) error

Provision writes a tenant through the owner's connection, which the service holds only to migrate and to bind. A binding another tenant holds fails it.

func RevokeKey

func RevokeKey(ctx context.Context, ownerURL, digest string) error

RevokeKey stops a key at the next request, through the owner role's connection. A key already revoked keeps its first stamp, and a digest no tenant holds answers ErrNotFound.

Types

type Batch

type Batch struct {
	Records []Record
	// Drops counts fields dropped at the edge, by field name.
	Drops map[string]int64
	Day   time.Time
}

Batch is one ingest request's writes.

type Binding

type Binding struct {
	Host  string
	Owner string
}

Binding names one SCM owner a tenant holds.

type Line

type Line struct {
	Currency string
	// BillingAccountID is a sender's statement, so a line never adds across it.
	BillingAccountID string
	ServiceCategory  string
	Agent            string
	Model            string
	SkuMeter         string
	ConsumedUnit     string
	CostBasis        string
	CostSource       string
	AttributionGrade string
	RunIdentity      string
	BudgetEligible   bool
	Rows             int64
	ConsumedQuantity decimal.Decimal
	ListCost         decimal.Decimal
	EffectiveCost    decimal.Decimal
	FirstCharge      time.Time
	LastCharge       time.Time
}

Line is one group of a pull request's rows. Lines never add across currencies or across units.

type MintedCredential

type MintedCredential struct {
	KeySHA256   string
	Repository  string
	RunID       string
	RunAttempt  string
	Environment string
}

MintedCredential is a gateway key the minting job attested for one run.

func (MintedCredential) SameRun

func (m MintedCredential) SameRun(other MintedCredential) bool

SameRun reports whether two attestations name one run.

type Receipt

type Receipt struct {
	Inserted   int
	Duplicates int
}

Receipt says what a batch changed.

type Record

type Record struct {
	Row        focus.Row
	RunAttempt string
	// Actor is an SCM login or a per-tenant pseudonym, never an address.
	Actor string
	Event string
	// TokenCommitSHA is the commit the CI token verifies, empty without one.
	TokenCommitSHA string
	// HeadBasis says where costroid/commit_sha came from, empty when the row
	// has none.
	HeadBasis string
	// CredentialBasis says whether SubAccountId is a key attested as minted
	// for the row's run or only stated, empty when the row names no credential.
	CredentialBasis string
	// RequestID is the provider's id for the request the row counts, empty
	// when the sender stated none. It is not stored: it lets a cache write
	// row be matched with the request's write rows of other lifetimes.
	RequestID string
}

Record is one row and the facts about its run that have no FOCUS column.

type Store

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

Store is the application role's handle on the ledger.

func Open

func Open(ctx context.Context, url string) (*Store, error)

Open connects as the application role and refuses a role that could read past row level security.

func (*Store) AddEgress

func (s *Store) AddEgress(ctx context.Context, counts []egress.Count) error

AddEgress adds to the deployment's egress counters, which belong to no tenant, so no tenant is set.

func (*Store) Attest

func (s *Store) Attest(ctx context.Context, tenant TenantID, minted MintedCredential) error

Attest stores an attestation. The same attestation again changes nothing, and one that names another run for a stored key answers ErrAttestedElsewhere.

func (*Store) AttestSpend

func (s *Store) AttestSpend(ctx context.Context, tenant TenantID, key MintedCredential, usd decimal.Decimal) error

AttestSpend keeps what the gateway counted on a minted key, in USD. The key must be attested as minted for the run named: a key attested for another repository, run or attempt answers ErrAttestedElsewhere, and a key never attested answers ErrNotMinted. A key's spend is the largest figure attested for it, because a gateway's count only grows, so a smaller one changes nothing.

func (*Store) Close

func (s *Store) Close()

Close releases the pool.

func (*Store) Egress

func (s *Store) Egress(ctx context.Context) ([]egress.Count, error)

Egress reads the counters, newest day first.

func (*Store) GatewaySpend

func (s *Store) GatewaySpend(ctx context.Context, tenant TenantID, repo, pr string) (rows int64, usd decimal.Decimal, err error)

GatewaySpend reads the pull request's measured spend. rows counts its budget-eligible rows whose credential is a key minted for the row's own run, with credential_basis minted, and whose spend a closing job attested. usd adds up the attested spend of those keys, each key once.

func (*Store) Ingest

func (s *Store) Ingest(ctx context.Context, tenant TenantID, batch Batch) (Receipt, error)

Ingest writes a batch whole or not at all. A row id already stored for this tenant is a duplicate and changes nothing.

func (*Store) MintedCredentials

func (s *Store) MintedCredentials(ctx context.Context, tenant TenantID, keys []string) (map[string]MintedCredential, error)

MintedCredentials reads the attestations of the keys named, by key.

func (*Store) PullRequest

func (s *Store) PullRequest(ctx context.Context, tenant TenantID, repo, pr string) ([]Line, error)

PullRequest derives one pull request's lines from the rows.

func (*Store) TenantForKey

func (s *Store) TenantForKey(ctx context.Context, keySHA256 string) (TenantID, error)

TenantForKey resolves the SHA-256 of an ingest key to its tenant.

func (*Store) TenantForOwner

func (s *Store) TenantForOwner(ctx context.Context, host, owner string) (TenantID, error)

TenantForOwner resolves an SCM owner, host and name, to its tenant.

type Tenant

type Tenant struct {
	ID   TenantID
	Name string
	// Bindings are the SCM owners whose CI tokens resolve to this tenant.
	Bindings []Binding
	// KeySHA256 lists the SHA-256 of each ingest key.
	KeySHA256 []string
}

Tenant is one provisioned tenant.

type TenantID

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

TenantID is a validated tenant identifier. It is safe to place in SET LOCAL, which takes no bind parameters.

func NewTenantID

func NewTenantID() (TenantID, error)

NewTenantID mints a random version 4 UUID.

func ParseTenantID

func ParseTenantID(text string) (TenantID, error)

ParseTenantID accepts the canonical lower-case UUID form only.

func (TenantID) String

func (t TenantID) String() string

String returns the canonical form.

Directories

Path Synopsis
Package storetest runs Postgres in Docker for the packages that need it.
Package storetest runs Postgres in Docker for the packages that need it.

Jump to

Keyboard shortcuts

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