Documentation
¶
Overview ¶
Package server is the ingest service behind costroid serve: the result-stream and OTLP paths, pricing against the pinned catalog, and the per-PR view.
Index ¶
- Variables
- func EgressMain(ctx context.Context, args []string, stdout, stderr io.Writer) int
- func KeysMain(ctx context.Context, args []string, stdout, stderr io.Writer) int
- func Main(ctx context.Context, args []string, stdout, stderr io.Writer) int
- type ApprovalHost
- type Config
- type GitHubApp
- type Installations
- type Ledger
- type RepositoryHosts
- type Server
- type SpendLedger
- type TokenVerifier
Constants ¶
This section is empty.
Variables ¶
var EgressUsage = cli.Page{ Command: "egress", Summary: egressGroup.Summary, Usage: []string{"costroid egress report [--json]"}, Examples: []string{"costroid egress report", "costroid egress report --json"}, Flags: []cli.Row{{"--json", "print the counters as one JSON object"}}, Sections: []cli.Section{ {Heading: "What the report holds", Body: []cli.Block{cli.Text(`A refused call never opened a socket. The report holds no path, query, header or body. A service writes its counters every 5 seconds. --json prints the counters as one JSON object. Its first key, schema, names the object costroid.egress/0.1.`)}}, }, Environment: []cli.Block{cli.Rows{{envDatabase, "the application role's connection"}}}, Exits: []cli.Row{ {"0", "the report was printed"}, cli.UsageExit, {"125", "costroid could not read the database"}, }, Docs: "ledger", }.String()
var KeysUsage = cli.Page{ Command: "keys", Summary: keysGroup.Summary, Usage: []string{ "costroid keys create --owner HOST:OWNER", "costroid keys revoke <id>", }, Examples: []string{ "costroid keys create --owner github.com:example > key", `costroid keys revoke "$KEY_ID"`, }, Flags: []cli.Row{{"--owner HOST:OWNER", "an SCM owner of the tenant, host:owner"}}, Sections: []cli.Section{ {Heading: "create and revoke", Body: []cli.Block{cli.Text(`create prints the new key on stdout, once, and its id on stderr. The ledger keeps the key's SHA-256, which is the id, so a lost key cannot be read back. --owner names an SCM owner of the tenant the key is for. revoke takes the id and stops the key at the next request.`)}}, {Heading: "What a key's rows read", Body: []cli.Block{cli.Text("A key's rows stay unverified and unattributed, and never reach a budget.")}}, }, Environment: []cli.Block{cli.Rows{{envMigration, "the owner role's connection"}}}, Exits: []cli.Row{ {"0", "the key was created or revoked"}, cli.UsageExit, {"125", `costroid could not reach the database or find the tenant or the key`}, }, Docs: "serve", }.String()
var ServeUsage = cli.Page{ Command: "serve", Summary: "serve runs the ingest service.", Usage: []string{ "costroid serve [--addr :8080] [--gitlab-host HOST]... [--bind HOST:OWNER]...", " [--egress-allow HOST]...", }, Examples: []string{ "costroid serve", "costroid serve --addr :8080 --bind github.com:example", "costroid serve --gitlab-host gitlab.example.com \\", " --egress-allow gitlab.example.com", }, Flags: []cli.Row{ {"--addr ADDRESS", "the address to listen on"}, {"--gitlab-host HOST", "a GitLab host whose CI tokens are trusted"}, {"--bind HOST:OWNER", "an SCM owner for this instance's tenant, host:owner"}, {"--egress-allow HOST", "an exact host the service may call"}, }, Sections: []cli.Section{ {Heading: "The address, the hosts and the tenant", Body: []cli.Block{cli.Text(`--addr is the address it listens on, :8080 by default. --gitlab-host trusts the CI tokens of one more GitLab instance. GitHub Actions and gitlab.com are trusted by default. --bind gives an SCM owner to this instance's tenant, and needs the owner role.`)}}, {Heading: "Outbound calls", Body: []cli.Block{cli.Text(`--egress-allow adds one exact host to the egress allowlist, and adds to COSTROID_EGRESS_ALLOW. The list starts empty, and an empty list refuses every outbound call. OIDC ingest needs each issuer's host on it, the default issuers included. Without one, a token from that issuer answers EGRESS_REFUSED. costroid egress report prints what was attempted.`)}}, {Heading: "Exact rows", Body: []cli.Block{cli.Text(`A row reads exact only when its SubAccountId is a gateway key that a job in that environment attested as minted for the run the row's CI token names. docs/serve.md says why.`)}}, }, Environment: []cli.Block{cli.Rows{ {envDatabase, "the application role's connection"}, {envMigration, `the owner role's connection; when set, migrations run before the service starts`}, {envAudience, `the audience CI tokens must name, never GitHub's default, https://github.com/<owner>`}, {envPseudonym, `at least 32 bytes in base64, the master key for per-tenant address pseudonyms`}, {envEgress, `hosts for the egress allowlist, comma-separated`}, {envGatewayEnv, `the GitHub environment whose jobs may attest a minted gateway key, costroid-gateway by default`}, {envWebhook, `at least 32 bytes, the secret the GitHub App signs its webhook deliveries with; unset, POST /webhooks/github is not served`}, {envAppID, "the GitHub App's id"}, {envAppKey, `the AWS KMS key that holds the App's private key; set with the App's id, the approve button posts as the App, and unset, a press posts nothing`}, {"AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_SESSION_TOKEN, AWS_REGION", `the credentials and region KMS is called with; a key ARN names its own region`}, }}, Exits: []cli.Row{ {"0", "the service stopped on a signal"}, cli.UsageExit, {"125", "the service could not start, or stopped on an error"}, }, Docs: "serve", }.String()
ServeUsage is serve's help page, which goes to stdout wherever it is asked for: costroid help serve, serve --help, and --help after other flags.
Functions ¶
func EgressMain ¶
EgressMain runs costroid egress and returns an exit code.
Types ¶
type ApprovalHost ¶
ApprovalHost is what the approve button needs from one repository: the pull request, the approver's role, costroid.yml at the head of the base branch, and the commit's check runs.
type Config ¶
type Config struct {
Ledger Ledger
Verifier TokenVerifier
Catalog *catalog.Catalog
// PseudonymKey is the master key the per-tenant HMAC keys derive from.
PseudonymKey []byte
// GatewayEnvironment is the GitHub environment whose jobs may attest a
// minted credential, when a run of a protected branch that workflow_run
// started runs them. Empty trusts none, and then no row reads exact.
GatewayEnvironment string
// Spend keeps the gateway's attested spend. Nil, the service refuses a
// spend attestation and treats every token-verified spend as agent counts.
Spend SpendLedger
// App serves POST /webhooks/github. Nil, the route is not served.
App *GitHubApp
Now func() time.Time
Logger *slog.Logger
}
Config wires a Server.
type GitHubApp ¶
type GitHubApp struct {
// Secret is the webhook secret GitHub signs each delivery with.
Secret []byte
Installations Installations
// Deliveries keeps the delivery ids taken. Nil keeps them in this
// process's memory, which a restart forgets.
Deliveries scm.DeliveryStore
// Hosts reaches a repository as one of the App's installations. Without
// it, an approve press is refused and logged: the service holds no App
// key to post a check with.
Hosts RepositoryHosts
}
GitHubApp configures POST /webhooks/github. A Config without one does not serve the route.
type Installations ¶
type Installations interface {
AddRepositories(ctx context.Context, installation int64, account string, repositories []store.Repository) error
RemoveRepositories(ctx context.Context, installation int64, repositories []int64) error
SuspendInstallation(ctx context.Context, installation int64, account string, suspended bool) error
DeleteInstallation(ctx context.Context, installation int64) error
// Installation answers store.ErrNotFound for an installation it does not
// hold.
Installation(ctx context.Context, installation int64) (store.Installation, error)
}
Installations keeps the App's installations: ids and names, never a token.
type Ledger ¶
type Ledger interface {
TenantForOwner(ctx context.Context, host, owner string) (store.TenantID, error)
TenantForKey(ctx context.Context, keySHA256 string) (store.TenantID, error)
Ingest(ctx context.Context, tenant store.TenantID, batch store.Batch) (store.Receipt, error)
PullRequest(ctx context.Context, tenant store.TenantID, repo, pr string) ([]store.Line, error)
Attest(ctx context.Context, tenant store.TenantID, minted store.MintedCredential) error
MintedCredentials(ctx context.Context, tenant store.TenantID, keys []string) (map[string]store.MintedCredential, error)
}
Ledger is what the service needs from the store.
type RepositoryHosts ¶
type RepositoryHosts interface {
Repository(installation int64, repository string) (ApprovalHost, error)
}
RepositoryHosts reaches one repository, owner/name, as one installation.
type Server ¶
type Server struct {
Config
// contains filtered or unexported fields
}
Server handles ingest and the view.
func New ¶
New returns the service. Now and Logger default to the real clock and the default logger.
func (*Server) ProcessWebhooks ¶
ProcessWebhooks applies the verified deliveries until ctx ends. It returns at once when the route is not served.
type SpendLedger ¶
type SpendLedger interface {
// AttestSpend records a key's USD spend once. The key must be attested as
// minted for the run named, or store.ErrNotMinted says it is not. A key
// holds one figure and an attesting run writes one: the same string again
// from the same run changes nothing, and any other write answers
// store.ErrAttestingRunUsed or store.ErrSpendHeld. A figure is never
// replaced, so the larger of two is never kept.
AttestSpend(ctx context.Context, tenant store.TenantID, attested store.AttestedSpend) error
// GatewaySpend reads the pull request's measured spend. USD adds up, each
// key once, the spend a protected run attested for every key the pull
// request's budget-eligible rows name, minted for the row's own run, with
// credential_basis minted, and for every key an attesting run named the
// pull request for, whether a row of it arrived or not. Rows counts the
// eligible rows on the first kind of key, and is 0 while a key of the
// second kind holds no spend.
GatewaySpend(ctx context.Context, tenant store.TenantID, repo, pr string) (rows int64, usd decimal.Decimal, err error)
}
SpendLedger keeps what costroid gateway close attests: the gateway's own count of a minted key's spend, in USD. The count is the gateway's, so it stands as a budget's evidence where the token counts on the rows cannot.