key

package
v0.10.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 10, 2026 License: Apache-2.0 Imports: 13 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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

View Source
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.

View Source
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

type Generated struct {
	Plaintext string
	KeyHash   string
	Prefix    string
}

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.

func Generate

func Generate() (Generated, error)

Generate produces a fresh key plaintext and the derived KeyHash + display Prefix. The plaintext is `sk-wr-<base64url(48 random bytes)>`. Callers store KeyHash + Prefix and return Plaintext to the user once.

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) InGrace

func (k *Key) InGrace(now time.Time) bool

InGrace reports whether the pre-rotation hash still authenticates at now.

func (*Key) IsActive

func (k *Key) IsActive(now time.Time) bool

IsActive returns true when the key is enabled, not revoked, and not past its expiry at now.

func (*Key) IsEnabled

func (k *Key) IsEnabled() bool

IsEnabled returns true when Enabled is unset or explicitly true.

func (*Key) Validate

func (k *Key) Validate() error

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. Ignored while a policy governs the key: the policy's own
	// flag decides then (policy.CapturesPayload).
	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 NewStore

func NewStore(q *gen.Queries) *Store

func (*Store) Delete

func (s *Store) Delete(ctx context.Context, id string) error

func (*Store) Get

func (s *Store) Get(ctx context.Context, id string) (*Key, error)

Get returns (nil, nil) when no row has id.

func (*Store) List

func (s *Store) List(ctx context.Context) ([]*Key, error)

func (*Store) Rotate

func (s *Store) Rotate(ctx context.Context, k *Key, prevHash string, seen time.Time) error

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.

func (*Store) Upsert

func (s *Store) Upsert(ctx context.Context, k *Key) error

Upsert expects Meta.ID and Spec.KeyHash set by the caller.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL