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.
Index ¶
- Constants
- Variables
- 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 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 `` /* 155-byte string literal not displayed */
ID string `json:"id" yaml:"id" validate:"required,uuid" doc:"Id of the service account or user."`
// Derived: filled on control API reads, never stored; compare principals by Kind and ID.
Name string `` /* 191-byte string literal not displayed */
}
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 `` /* 130-byte string literal not displayed */
// AccountID is the key's default lane: the account its requests run in and are charged to unless a request names another the principal holds.
AccountID string `` /* 445-byte string literal not displayed */
// KeyHash is sha256(plaintext) hex. Required and immutable after create.
KeyHash string `` /* 168-byte string literal not displayed */
// 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 `` /* 147-byte string literal not displayed */
// PreviousKeyHash is the hash rotation replaced. Server-set; accepted
// alongside KeyHash until GraceUntil passes.
PreviousKeyHash string `` /* 210-byte string literal not displayed */
// GraceUntil bounds how long PreviousKeyHash still authenticates.
// Server-set by rotate.
GraceUntil *time.Time `` /* 133-byte string literal not displayed */
// ExpiresAt, when non-nil, stops the key authenticating once passed.
ExpiresAt *time.Time `json:"expiresAt,omitempty" yaml:"expiresAt,omitempty" doc:"When the key stops authenticating. Omitted never expires."`
// RevokedAt, when non-nil, marks the key as rejected at auth time.
RevokedAt *time.Time `` /* 126-byte string literal not displayed */
// Enabled defaults to true when nil.
Enabled *bool `` /* 134-byte string literal not displayed */
// 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 `` /* 175-byte string literal not displayed */
// PayloadLoggingEnabled opts requests authenticated by this key into
// full request/response body capture by the payloadlog observer. Off
// by default. An account on the request's ladder that sets payloadLogging decides instead.
PayloadLoggingEnabled bool `` /* 254-byte string literal not displayed */
}
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.