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
- Variables
- func AddKey(ctx context.Context, ownerURL string, owner Binding, digest string) error
- func KeyDigest(key string) string
- func Migrate(ctx context.Context, ownerURL string) error
- func MigrateFrom(ctx context.Context, ownerURL string, files fs.FS) error
- func NewKey() (string, error)
- func Provision(ctx context.Context, ownerURL string, tenant Tenant) error
- func RevokeKey(ctx context.Context, ownerURL, digest string) error
- type Batch
- type Binding
- type Deliveries
- type Installation
- type Line
- type MintedCredential
- type Receipt
- type Record
- type Repository
- type Store
- func (s *Store) AddEgress(ctx context.Context, counts []egress.Count) error
- func (s *Store) AddRepositories(ctx context.Context, installation int64, account string, ...) error
- func (s *Store) Attest(ctx context.Context, tenant TenantID, minted MintedCredential) error
- func (s *Store) AttestSpend(ctx context.Context, tenant TenantID, key MintedCredential, ...) error
- func (s *Store) Close()
- func (s *Store) DeleteInstallation(ctx context.Context, installation int64) error
- func (s *Store) Deliveries(retention time.Duration) Deliveries
- func (s *Store) Egress(ctx context.Context) ([]egress.Count, error)
- func (s *Store) GatewaySpend(ctx context.Context, tenant TenantID, repo, pr string) (rows int64, usd decimal.Decimal, err error)
- func (s *Store) Ingest(ctx context.Context, tenant TenantID, batch Batch) (Receipt, error)
- func (s *Store) Installation(ctx context.Context, installation int64) (Installation, error)
- func (s *Store) MintedCredentials(ctx context.Context, tenant TenantID, keys []string) (map[string]MintedCredential, error)
- func (s *Store) PullRequest(ctx context.Context, tenant TenantID, repo, pr string) ([]Line, error)
- func (s *Store) RemoveRepositories(ctx context.Context, installation int64, repositories []int64) error
- func (s *Store) SuspendInstallation(ctx context.Context, installation int64, account string, suspended bool) error
- func (s *Store) TenantForKey(ctx context.Context, keySHA256 string) (TenantID, error)
- func (s *Store) TenantForOwner(ctx context.Context, host, owner string) (TenantID, error)
- type Tenant
- type TenantID
Constants ¶
const ( HeadFromToken = "token" HeadStated = "stated" )
Values of HeadBasis.
const ( CredentialMinted = "minted" CredentialStated = "stated" )
Values of CredentialBasis.
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.
const KeyPrefix = "costroid_ik_"
KeyPrefix starts every ingest key. A bearer without it is a CI token.
Variables ¶
var ErrAttestedElsewhere = errors.New("store: the key is attested for another run")
ErrAttestedElsewhere means a key is already attested for another run.
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.
var ErrMigrationEdited = errors.New("store: an applied migration was edited")
ErrMigrationEdited means a migration already applied has changed on disk.
var ErrNotFound = errors.New("store: not found")
ErrNotFound means a lookup named nothing.
var ErrNotMinted = errors.New("store: the key is not attested as minted")
ErrNotMinted means a spend names a key no attestation holds.
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 ¶
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 MigrateFrom ¶
MigrateFrom applies the migrations in files, each in its own transaction.
func NewKey ¶
NewKey mints an ingest key. Only its digest is ever stored, so a key that is lost is revoked and replaced.
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 Deliveries ¶
type Deliveries struct {
// contains filtered or unexported fields
}
Deliveries keeps the webhook delivery ids the service has taken, so every instance refuses a replayed delivery, and a restart forgets none. The ids belong to the deployment, so no tenant is set.
type Installation ¶
type Installation struct {
ID int64
Account string
Suspended bool
// Repositories are the repositories the installation reaches, by id.
Repositories []Repository
}
Installation is one installation of the GitHub App as the deployment keeps it: ids and names, never a token.
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 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 Repository ¶
Repository is one repository an installation reaches.
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
Store is the application role's handle on the ledger.
func Open ¶
Open connects as the application role and refuses a role that could read past row level security.
func (*Store) AddEgress ¶
AddEgress adds to the deployment's egress counters, which belong to no tenant, so no tenant is set.
func (*Store) AddRepositories ¶
func (s *Store) AddRepositories(ctx context.Context, installation int64, account string, repositories []Repository) error
AddRepositories keeps an installation and the repositories it now reaches. An installation event states its account each time, so an installation the table does not hold yet is added, and a renamed account or repository takes its new name. It leaves the installation's suspension as it is. The App's installations belong to the deployment, so no tenant is set.
func (*Store) Attest ¶
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) DeleteInstallation ¶
DeleteInstallation forgets an installation and its repositories.
func (*Store) Deliveries ¶
func (s *Store) Deliveries(retention time.Duration) Deliveries
Deliveries returns the delivery ids held for retention after each is taken.
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 ¶
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) Installation ¶
Installation reads one installation and its repositories, ordered by id. An installation the table does not hold is ErrNotFound.
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 ¶
PullRequest derives one pull request's lines from the rows.
func (*Store) RemoveRepositories ¶
func (s *Store) RemoveRepositories(ctx context.Context, installation int64, repositories []int64) error
RemoveRepositories forgets repositories an installation no longer reaches.
func (*Store) SuspendInstallation ¶
func (s *Store) SuspendInstallation(ctx context.Context, installation int64, account string, suspended bool) error
SuspendInstallation keeps whether an installation is suspended, and keeps the installation when the table does not hold it yet.
func (*Store) TenantForKey ¶
TenantForKey resolves the SHA-256 of an ingest key 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 ParseTenantID ¶
ParseTenantID accepts the canonical lower-case UUID form only.