Documentation
¶
Overview ¶
Package gateway gives one CI run its own key on a LiteLLM proxy. The agent's job draws the key and seals it to the gateway's sealing key; a job that holds the sealing key's private half and the proxy's admin key opens it and registers it with the run's budget; the agent runs on it; a last job deletes it. No key crosses a job in the clear.
Index ¶
- Constants
- Variables
- func ActionsToken(ctx context.Context, client *http.Client, getenv func(string) string, ...) (string, error)
- func Attest(ctx context.Context, client *http.Client, credentialsURL, token string, ...) error
- func AttestSpend(ctx context.Context, client *http.Client, spendURL, token string, ...) error
- func CheckURL(base string) error
- func Close(ctx context.Context, client *Client, adminKey string, run Binding) (bool, error)
- func CredentialsURL(ingest string) (string, error)
- func KeyID(key string) string
- func NewKey() string
- func NewSealingKey() (private, public string, err error)
- func PublicKey(sealingKey string) (string, error)
- func SameSpend(counted, total decimal.Decimal) bool
- func SpendURL(ingest, keyID string) (string, error)
- type Artifacts
- type Attestation
- type Binding
- type Client
- func (c *Client) Count(ctx context.Context, adminKey, alias string) (Count, bool, error)
- func (c *Client) Delete(ctx context.Context, adminKey, alias string) (bool, error)
- func (c *Client) EnsureDailyBudget(ctx context.Context, adminKey, kind, id string, budget decimal.Decimal) error
- func (c *Client) Generate(ctx context.Context, adminKey string, spec KeySpec) error
- func (c *Client) Info(ctx context.Context, bearer, id string) (KeyInfo, error)
- func (c *Client) Readiness(ctx context.Context) (Readiness, error)
- type Count
- type Counter
- type DailyCaps
- type Expect
- type Grant
- type KeyInfo
- type KeySpec
- type Mint
- type Readiness
- type Request
- type Session
- type SpendAttestation
Constants ¶
const ( MetaRepository = "costroid_repository" MetaPullRequest = "costroid_pull_request" MetaHead = "costroid_head" MetaRun = "costroid_run_id" MetaAttempt = "costroid_run_attempt" MetaMaxRequestTokens = "costroid_max_request_tokens" )
The metadata keys a run's key carries.
const RequestFile = "costroid-gateway-request.json"
RequestFile is the name of the sealed request inside its artifact.
const ( // SealingKeyPrefix starts the sealing key's private half, so it is never // taken for the public half. SealingKeyPrefix = "costroid-sealing-key-v1-" )
Variables ¶
var ErrDerivedSealingKey = errors.New("gateway: the request is sealed to a public key derived from the admin key, which anyone who reads it can test guesses at the admin key against. " +
"Run costroid gateway keygen, store the private half as the environment secret COSTROID_GATEWAY_SEALING_KEY and the public half as the variable COSTROID_GATEWAY_PUBLIC_KEY, and rotate the admin key")
ErrDerivedSealingKey is a request sealed to a public key derived from the admin key, as versions before keygen made it. Anyone who reads that public key can test guesses at the admin key offline.
var ErrNotFound = errors.New("gateway: not found")
ErrNotFound is a gateway's answer that the key, team or user is absent.
var ErrUnsettled = errors.New("may not have settled")
ErrUnsettled marks a figure that was still moving when the wait for it ended. The figure is answered beside it.
Functions ¶
func ActionsToken ¶
func ActionsToken(ctx context.Context, client *http.Client, getenv func(string) string, audience string) (string, error)
ActionsToken asks GitHub Actions for the job's OIDC token, as the ingest job does. It needs id-token: write, which sets the two variables it reads.
func Attest ¶
func Attest(ctx context.Context, client *http.Client, credentialsURL, token string, attestation Attestation) error
Attest sends an attestation to the ledger under the job's OIDC token.
func AttestSpend ¶
func AttestSpend(ctx context.Context, client *http.Client, spendURL, token string, attestation SpendAttestation) error
AttestSpend sends a key's spend to the ledger under the job's OIDC token.
func CheckURL ¶
CheckURL refuses a gateway URL a key would cross the network in the clear to. Every call sends the admin key or the run's key in its Authorization header, so the URL must be https, except on localhost and 127.0.0.1.
func CredentialsURL ¶
CredentialsURL is where the ledger behind an ingest URL takes attestations. The URL must be https, because the request carries the job's OIDC token.
func KeyID ¶
KeyID is the key's identifier: the SHA-256 of the key in hex, which is what LiteLLM stores in place of the key and writes on its spend logs.
func NewKey ¶
func NewKey() string
NewKey draws a key for one run: 128 random bits after LiteLLM's prefix.
func NewSealingKey ¶
NewSealingKey draws a sealing key pair at random. It returns the private half, for an environment secret, and the public half, for a repository variable. Nothing about the pair depends on the admin key.
Types ¶
type Artifacts ¶
type Artifacts struct {
API string
Repository string
Run string
Token string
HTTP *http.Client
Every time.Duration
}
Artifacts reads one artifact of the current run through GitHub's REST API, with a token that holds actions: read.
type Attestation ¶
type Attestation struct {
KeyID string `json:"key_id"`
Repository string `json:"repository"`
RunID string `json:"run_id"`
RunAttempt string `json:"run_attempt"`
}
Attestation tells the ledger that a key was minted for one run. KeyID is the value the run's rows carry in SubAccountId. It holds no secret.
func AttestationFor ¶
func AttestationFor(keyID string, run Binding) Attestation
AttestationFor names the run a binding is for.
type Binding ¶
type Binding struct {
Repository string `json:"repository"`
Run string `json:"run_id"`
Attempt string `json:"run_attempt"`
}
Binding names the run a key is for. A key sealed for one binding opens for no other.
func ReadBinding ¶
ReadBinding reads the run from GitHub Actions' variables.
type Client ¶
Client speaks LiteLLM's proxy API. It never logs a key: errors name the route and the status, and quote no request.
func (*Client) Count ¶
Count reads the key with the alias, as the gateway's admin. It answers false when no key has the alias. LiteLLM keeps aliases unique, so a second key with the alias is an error.
func (*Client) EnsureDailyBudget ¶
func (c *Client) EnsureDailyBudget(ctx context.Context, adminKey, kind, id string, budget decimal.Decimal) error
EnsureDailyBudget creates a team or a user with a daily budget, or sets the budget of one that exists. Kind is team or user.
type Count ¶
Count is what the gateway counted on a run's key: the key's id and its spend, in USD at the gateway's own prices.
type Counter ¶
type Counter struct {
Client *Client
AdminKey string
Run Binding
// Poll is how often the key is read. Stable is how long a figure that has
// moved must then hold to count as settled, and Settle bounds the whole
// wait. settle says why.
Poll, Stable, Settle time.Duration
}
Counter reads what the gateway counted on a run's key once the figure has settled. LiteLLM writes spend to its store in batches, so a key read once can still lack its last request.
func (Counter) Settled ¶
Settled reads the run's key until its spend has settled as settle reads it, with no figure accepted early, since close has no rows to compare with. It answers false when no key has the run's alias. A figure still moving when Settle passes is answered with an error wrapping ErrUnsettled.
type Grant ¶
type Grant struct {
KeyID string
MaxBudget decimal.Decimal
MaxParallel int64
MaxRequestTokens int64
// Problems name each fact the key lacks. A grant with any reads none.
Problems []string
}
Grant is what the gateway holds the run to.
type KeyInfo ¶
type KeyInfo struct {
MaxBudget *json.Number `json:"max_budget"`
Spend json.Number `json:"spend"`
// Expires is read as it came, so a form other than RFC 3339 is named
// rather than failing the whole answer.
Expires json.RawMessage `json:"expires"`
MaxParallel *int `json:"max_parallel_requests"`
Metadata map[string]any `json:"metadata"`
Team *string `json:"team_id"`
User *string `json:"user_id"`
Status string `json:"status"`
}
KeyInfo is what the gateway says about a key.
type KeySpec ¶
type KeySpec struct {
Key string
Alias string
MaxBudget decimal.Decimal
Lifetime time.Duration
MaxParallel int
Metadata map[string]string
Team string
User string
}
KeySpec is one run's key as the gateway registers it.
type Mint ¶
type Mint struct {
Client *Client
AdminKey string
// SealingKey is the private half the request was sealed to.
SealingKey string
Run Binding
Request Request
Budget decimal.Decimal
Lifetime time.Duration
MaxParallel int
MaxRequestTokens int64
PullRequest string
Head string
Author string
Caps DailyCaps
}
Mint is one key to register for one run.
type Request ¶
type Request struct {
Format string `json:"format"`
Binding
// SealingKey is the public key the key was sealed to.
SealingKey string `json:"sealing_key"`
KeyID string `json:"key_id"`
Sealed string `json:"sealed"`
}
Request is the sealed key and the run it is for. It holds no secret: only a holder of the sealing key's private half can open it.
func NewRequest ¶
NewRequest draws a key for the run and seals it to the gateway's public key. The key is returned to be kept on the runner, and never written to the request.
func ReadRequest ¶
ReadRequest parses a request file.
type Session ¶
type Session struct {
Client *Client
Key string
// Wait is how long to wait for the key to be registered.
Wait time.Duration
// Every is how often the store is probed while the agent runs.
Every time.Duration
// Poll is how often the key is asked for while it is awaited or while
// its spend settles.
Poll time.Duration
// Settle bounds the wait for the key's spend to reach the gateway's store,
// and must outlast one of LiteLLM's batch writes. Stable is how long a
// figure that has moved must then hold to count as settled. settle says
// why.
Settle, Stable time.Duration
}
Session is one run's key on one gateway, as the agent's job holds it.
func (Session) Await ¶
Await waits until the key authenticates, then reads it back. A URL the key would cross the network in the clear to is refused before any wait.
func (Session) Env ¶
Env is the agent's environment: the base given, with the agent's own variables for the gateway's URL and the run's key in place of any it held, and without each credential the agent names to be removed.
func (Session) Spend ¶
Spend reads what the gateway counted on the key, in its own prices, once the agent has ended and the figure has settled. It answers as soon as the figure is the same as want, and otherwise waits as settle does. A figure still moving when Settle passes is answered with an error wrapping ErrUnsettled.
type SpendAttestation ¶
type SpendAttestation struct {
Repository string `json:"repository"`
RunID string `json:"run_id"`
RunAttempt string `json:"run_attempt"`
Currency string `json:"currency"`
Spend string `json:"spend"`
}
SpendAttestation tells the ledger what the gateway counted on a key minted for one run. The key's id is in the URL.
func SpendFor ¶
func SpendFor(count Count, run Binding) SpendAttestation
SpendFor states a count for the run. The ledger keeps 12 fractional digits, so a figure with more is rounded up, and the ledger's figure is never below the gateway's.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package gatewaytest serves the LiteLLM proxy routes costroid gateway calls, as LiteLLM v1.108.1 answered them on 2026-11-07, for tests.
|
Package gatewaytest serves the LiteLLM proxy routes costroid gateway calls, as LiteLLM v1.108.1 answered them on 2026-11-07, for tests. |