Documentation
¶
Overview ¶
Package key is the domain layer for the Key entity — an inbound API key the relay issues to a client. Authentication only: plaintext is never stored, only sha256(KeyHash) plus a short display Prefix.
A Key is a credential OF a principal (a ServiceAccount or a User), not an identity of its own, so rotating one leaves the principal — and everything bound to it — untouched.
The plaintext never enters this package; callers hash it. The principal and rotation hashes live in real columns (FK cascade, unique index) as well as the spec JSONB, and the columns win on read.
Index ¶
- Constants
- Variables
- type Generated
- type Key
- type Principal
- type PrincipalKind
- type Spec
- type Store
- func (s *Store) Delete(ctx context.Context, id string) error
- func (s *Store) Get(ctx context.Context, id string) (*Key, error)
- func (s *Store) List(ctx context.Context) ([]*Key, error)
- func (s *Store) Rotate(ctx context.Context, k *Key, prevHash string, seen time.Time) error
- func (s *Store) Upsert(ctx context.Context, k *Key) error
Constants ¶
const TokenPrefix = "sk-wr-"
TokenPrefix is the literal prefix every relay-issued bearer token starts with. The leading "sk-" matches the LLM-API convention so the token visually scans as a secret credential in logs.
Variables ¶
var ErrHashInUse = errors.New("key: hash in use by another key")
ErrHashInUse reports a key whose hash (current or pre-rotation) already belongs to another row. Both live at once on the snapshot's hash index, so letting the second row in would silently hand it the first row's traffic — including a rotating key's grace window.
var ErrRotationRaced = errors.New("key: rotated concurrently")
ErrRotationRaced reports a rotate whose stored hash no longer matches the one the caller read: another rotation landed in between, and writing over it would hand out a plaintext that authenticates nothing.
Functions ¶
This section is empty.
Types ¶
type Generated ¶
Generated carries the result of Generate. Plaintext is the only field the server ever sees in cleartext; it is returned to the caller exactly once and never persisted.
type Key ¶
type Key struct {
Meta meta.Metadata `json:"metadata" yaml:"metadata"`
Spec Spec `json:"spec" yaml:"spec"`
}
Key is an inbound bearer credential.
func (*Key) IsActive ¶
IsActive returns true when the key is enabled, not revoked, and not past its expiry at now.
func (*Key) Validate ¶
Validate runs intra-row rules via the shared meta.Validator and enforces:
- Owner.Kind is project (serviceaccount principal) or user.
- a user principal is owned by that same user.
The serviceaccount's own project is checked against the snapshot, not here: this package sees one row at a time.
type Principal ¶
type Principal struct {
Kind PrincipalKind `json:"kind" yaml:"kind" validate:"required,oneof=serviceaccount user"`
ID string `json:"id" yaml:"id" validate:"required,uuid"`
}
Principal is the subject a Key authenticates as. ID is a ServiceAccount id or a User id, per Kind.
type PrincipalKind ¶
type PrincipalKind string
PrincipalKind names the sort of subject a Key authenticates as.
const ( PrincipalServiceAccount PrincipalKind = "serviceaccount" PrincipalUser PrincipalKind = "user" )
type Spec ¶
type Spec struct {
// Principal is the subject this key authenticates as. Required.
Principal Principal `json:"principal" yaml:"principal" validate:"required"`
// PolicyID is the Policy this Key serves under. **Optional** —
// when empty, the key is "policy-less" and the relay's behavior is
// controlled by the inference settings section
// (AllowMissingPolicy). A policy-less key with that flag on is
// allowed to reach any model served by any host the relay has
// hostkeys for, with no policy-level rate limits. With the flag off,
// such requests are rejected.
PolicyID string `json:"policyId,omitempty" yaml:"policyId,omitempty" validate:"omitempty,uuid"`
// KeyHash is sha256(plaintext) hex. Required and immutable after create.
KeyHash string `json:"keyHash" yaml:"keyHash" validate:"required,len=64,hexadecimal"`
// Prefix is the leading visible portion of the token (e.g. "rk_a8b3f2")
// retained so the UI can show a recognisable identifier without ever
// holding the plaintext.
Prefix string `json:"prefix,omitempty" yaml:"prefix,omitempty"`
// PreviousKeyHash is the hash rotation replaced. Server-set; accepted
// alongside KeyHash until GraceUntil passes.
PreviousKeyHash string `json:"previousKeyHash,omitempty" yaml:"-" validate:"omitempty,len=64,hexadecimal"`
// GraceUntil bounds how long PreviousKeyHash still authenticates.
// Server-set by rotate.
GraceUntil *time.Time `json:"graceUntil,omitempty" yaml:"-"`
// ExpiresAt, when non-nil, stops the key authenticating once passed.
ExpiresAt *time.Time `json:"expiresAt,omitempty" yaml:"expiresAt,omitempty"`
// RevokedAt, when non-nil, marks the key as rejected at auth time.
RevokedAt *time.Time `json:"revokedAt,omitempty" yaml:"revokedAt,omitempty"`
// Enabled defaults to true when nil.
Enabled *bool `json:"enabled,omitempty" yaml:"enabled,omitempty"`
// PassthroughAllowed, when true, permits this key to forward an upstream
// Authorization header verbatim to the provider. Gated by the relay's
// global passthrough mode — when that mode is off the flag is moot.
PassthroughAllowed bool `json:"passthroughAllowed,omitempty" yaml:"passthroughAllowed,omitempty"`
// PayloadLoggingEnabled opts requests authenticated by this key into
// full request/response body capture by the payloadlog observer. Off
// by default. Independent of the policy-level flag — either enables
// capture.
PayloadLoggingEnabled bool `json:"payloadLoggingEnabled,omitempty" yaml:"payloadLoggingEnabled,omitempty"`
}
Spec carries the auth material and gating flags. KeyHash is the sha256 hex of the bearer token (lowercase, 64 chars); the plaintext is never stored anywhere.
type Store ¶
type Store struct {
// contains filtered or unexported fields
}
func (*Store) Rotate ¶
Rotate replaces the key's hashes conditionally on prevHash and seen still being the row's stored hash and version. Everything else on the row (principal, policy, flags, slug) is untouched, and seen makes a plain update the caller never read lose the race rather than be overwritten.