Documentation
¶
Overview ¶
Package apikeys mints and verifies the caller credentials stored in the api_key table. A key is stored as a sha256 hash, never in plain — the plain value exists only in the response that mints it.
Deliberately not bcrypt: these keys are 32 bytes from crypto/rand, so there is nothing to guess and bcrypt's cost would buy only latency.
Index ¶
- Constants
- Variables
- func Generate(name string, scopes []string) (plain string, row *proxydb.APIKey, err error)
- func GenerateNodeKey(name, nodeID string, allowedCIDRs, allowedModels []string, ...) (plain string, row *proxydb.NodeKey, err error)
- func LookupHash(plain string) string
- func NormalizeScopes(in []string) ([]string, error)
- func ParseBearer(header string) (string, error)
- func VerifyHash(plain string) string
- func WithIdentity(ctx context.Context, id *Identity) context.Context
- type Identity
- type Store
- type Verifier
- func (v *Verifier) Verify(ctx context.Context, plain string) (*Identity, error)
- func (v *Verifier) VerifyScope(ctx context.Context, plain, scope string) (*Identity, error)
- func (v *Verifier) WithQueryTimeout(d time.Duration) *Verifier
- func (v *Verifier) WithStaleIfError(maxAge time.Duration) *Verifier
Constants ¶
const ( // ScopeGenerate allows the data-plane RPCs (GenerateText, ListModels). ScopeGenerate = "generate" ScopeAdmin = "admin" // ScopeAdminRead allows the Admin API reads and nothing that changes state. ScopeAdminRead = "admin:read" ScopeNode = "node" )
Scopes. A key carries a subset; admin implies all of them.
const ( KeyPrefix = "ktms_" NodeKeyPrefix = "ktms_node_" LegacyKeyPrefix = "llm_" LegacyNodeKeyPrefix = "llm_node_" )
KeyPrefix marks our keys so a leaked string is recognisable in a log scan.
Variables ¶
var ( ErrUnauthenticated = errors.New("apikeys: unauthenticated") ErrForbidden = errors.New("apikeys: insufficient scope") )
var ValidScopes = []string{ScopeGenerate, ScopeAdmin, ScopeAdminRead, ScopeNode}
ValidScopes is the closed set the admin endpoint accepts.
Functions ¶
func Generate ¶
Generate mints a key, returning the plain value (shown to the operator once) alongside the row to store.
func GenerateNodeKey ¶
func GenerateNodeKey(name, nodeID string, allowedCIDRs, allowedModels []string, maxBaseWeight float64, maxConcurrency int32) (plain string, row *proxydb.NodeKey, err error)
GenerateNodeKey mints a node key (prefixed with llm_node_), returning the plain secret alongside the database row to store.
func LookupHash ¶
LookupHash is the indexed sha256 hex of a plain key.
func NormalizeScopes ¶
NormalizeScopes rejects unknown scopes and drops duplicates.
func ParseBearer ¶
ParseBearer extracts the key from an "Authorization: Bearer <key>" value.
func VerifyHash ¶
VerifyHash is what key_hash holds. Same digest as LookupHash on purpose: the lookup column finds the row and the hash column authenticates it; a future change to how rows are found must not quietly change authentication.
Types ¶
type Identity ¶
type Identity struct {
KeyID string
Name string
Scopes []string
NodeID string
AllowedCIDRs []string
AllowedModels []string
MaxBaseWeight float64
MaxConcurrent int32
OrgID string
TeamID string
TeamAllowedModels []string
// The client key's own policy; empty and 0 mean no limit.
KeyAllowedModels []string
RPMLimit int32
TPMLimit int64
}
Identity is the authenticated caller.
func IdentityFor ¶ added in v1.2.0
IdentityFor is the caller a client key's row stands for, as Verify builds it; ExplainRoute uses it to route as that key would.
func IdentityFrom ¶
IdentityFrom returns the authenticated caller, or nil outside an authenticated RPC (exempt methods, tests).
func (*Identity) CanAccessModel ¶ added in v0.14.0
CanAccessModel reports whether both the key's and its team's allowlists (empty = any model) permit modelID.
func (*Identity) DeniedBy ¶ added in v1.0.0
DeniedBy names the allowlist that refuses modelID: "team" when the team's does, else "key". Refusal messages name it so the operator knows which to edit.
func (*Identity) RestrictsModels ¶ added in v1.0.0
RestrictsModels reports whether an allowlist narrows the models it may call.
type Store ¶
type Store interface {
GetAPIKeyByLookup(ctx context.Context, lookup string) (*proxydb.APIKey, error)
GetNodeKeyByLookup(ctx context.Context, lookup string) (*proxydb.NodeKey, error)
}
Store is the slice of proxydb the verifier needs, so tests can fake it.
type Verifier ¶
type Verifier struct {
// contains filtered or unexported fields
}
Verifier authenticates bearer keys for the gRPC interceptor.
func NewVerifier ¶
NewVerifier builds a verifier. bootstrapKey, when non-empty, is accepted as an implicit admin key — it exists only to mint the first real key, and can be unset afterwards.
func (*Verifier) Verify ¶
Verify authenticates a plain key. Auth is never dormant here: no key means no access, because this service holds LLM vendor keys.
func (*Verifier) VerifyScope ¶
VerifyScope authenticates and authorises in one step.
func (*Verifier) WithQueryTimeout ¶ added in v0.13.1
WithQueryTimeout bounds each key lookup, so a hung database fails calls instead of stalling them until every pool connection is stuck.
func (*Verifier) WithStaleIfError ¶ added in v0.13.1
WithStaleIfError lets a key verified within maxAge keep working while the store is unreachable; revocation applies whenever the store answers, so only one made just before an outage can lag, by at most maxAge. 0 disables it.