Documentation
¶
Overview ¶
Package users manages per-user-path policies. A user path is the hierarchy behind every managed API key (/team/alpha/alice): interior nodes act as groups, leaves as users. Each node may carry an allowlist of model selectors; a request must satisfy every non-empty allowlist between its key, its user path, and the root, so a child can narrow but never widen what its group permits. A node may also carry a request policy (a default model and ceilings on reasoning effort and output tokens) that the paths below it inherit; see RequestPolicy.
Index ¶
- Variables
- func Covers(allowed []string, entry string) bool
- func IsValidationError(err error) bool
- func Matches(allowed []string, selector core.ModelSelector) bool
- func NormalizeAllowedModels(catalog Catalog, raw []string) ([]string, error)
- type Catalog
- type MongoDBStore
- type RequestPolicy
- type Result
- type SQLStore
- type Service
- func (s *Service) AllowsModel(ctx context.Context, selector core.ModelSelector) bool
- func (s *Service) Constraints(userPath string) []User
- func (s *Service) DefaultModel(ctx context.Context) string
- func (s *Service) Delete(ctx context.Context, userPath string) error
- func (s *Service) Get(userPath string) (User, bool)
- func (s *Service) List() []User
- func (s *Service) NormalizeAllowedModels(raw []string) ([]string, error)
- func (s *Service) Refresh(ctx context.Context) error
- func (s *Service) RequestCeilings(ctx context.Context) requestparams.Ceilings
- func (s *Service) RequestPolicy(userPath string) RequestPolicy
- func (s *Service) SetChangeNotifier(fn func(context.Context))
- func (s *Service) SetConfigUsers(users []User)
- func (s *Service) StartBackgroundRefresh(interval time.Duration) func()
- func (s *Service) Upsert(ctx context.Context, user User) (User, error)
- func (s *Service) ValidateManagedConfig(declaredProviders []string) error
- type Store
- type User
- type ValidationError
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNotFound indicates the requested user path has no policy. ErrNotFound = errors.New("user not found") // ErrManaged indicates the user path is declared in config and cannot be // changed through the admin API. ErrManaged = errors.New("user is managed by configuration") )
Functions ¶
func Covers ¶ added in v0.1.107
Covers reports whether every model that the canonical entry permits is also permitted by allowed, so a credential limited to allowed may grant entry to another key. An empty allowlist covers everything.
func IsValidationError ¶
IsValidationError reports whether err is a validation error.
func Matches ¶
func Matches(allowed []string, selector core.ModelSelector) bool
Matches reports whether selector satisfies at least one canonical allowlist entry. An empty allowlist matches everything.
func NormalizeAllowedModels ¶
NormalizeAllowedModels validates and canonicalizes a model allowlist. It accepts the dashboard-friendly wildcard forms "*" (every model) and "provider/*" (every model of one provider) and stores them as the canonical "/" and "provider/" selectors. Duplicates collapse; order is preserved.
Types ¶
type Catalog ¶
type Catalog interface {
ProviderNames() []string
}
Catalog is the configured-provider surface needed to validate selectors.
type MongoDBStore ¶
type MongoDBStore struct {
// contains filtered or unexported fields
}
MongoDBStore stores user policies in MongoDB.
func NewMongoDBStore ¶
func NewMongoDBStore(database *mongo.Database) (*MongoDBStore, error)
NewMongoDBStore binds the users collection.
func (*MongoDBStore) Close ¶
func (s *MongoDBStore) Close() error
type RequestPolicy ¶ added in v0.2.7
type RequestPolicy struct {
// DefaultModel comes from the nearest path that sets one: a child's
// default replaces its group's.
DefaultModel string `json:"default_model,omitempty"`
DefaultModelFrom string `json:"default_model_from,omitempty"`
// MaxReasoningEffort and MaxOutputTokens are the lowest ceilings on the
// way to the root: a child can tighten its group's ceiling, never loosen
// it.
MaxReasoningEffort string `json:"max_reasoning_effort,omitempty"`
MaxReasoningEffortFrom string `json:"max_reasoning_effort_from,omitempty"`
MaxOutputTokens int `json:"max_output_tokens,omitempty"`
MaxOutputTokensFrom string `json:"max_output_tokens_from,omitempty"`
}
RequestPolicy is the request policy in force on one user path once its own row and every ancestor's are combined. Each *From field names the path the value comes from, empty when no path sets it.
func (RequestPolicy) Ceilings ¶ added in v0.2.7
func (p RequestPolicy) Ceilings() requestparams.Ceilings
Ceilings returns the policy's ceilings in the form request overrides carry.
func (RequestPolicy) IsZero ¶ added in v0.2.7
func (p RequestPolicy) IsZero() bool
IsZero reports whether no path sets any request policy.
type Result ¶
Result holds the initialized user policy service and its owned resources.
type SQLStore ¶
type SQLStore struct {
// contains filtered or unexported fields
}
SQLStore stores user policies in a SQL database.
func NewSQLStore ¶
NewSQLStore creates the users table if needed.
type Service ¶
type Service struct {
// contains filtered or unexported fields
}
Service keeps user policies cached in memory and evaluates model access for requests.
func NewService ¶
NewService creates a user policy service backed by the store and catalog.
func (*Service) AllowsModel ¶
AllowsModel reports whether the request may use selector: the credential's own allowlist and every non-empty allowlist on the request user path and its ancestors must all match. Requests without a key or user path are unrestricted here.
func (*Service) Constraints ¶
Constraints returns the policies that restrict userPath, root first: every ancestor (including the path itself) whose allowlist is non-empty.
func (*Service) DefaultModel ¶ added in v0.2.7
DefaultModel returns the model that serves a request on the request's user path when it names none, or "" when no path sets one.
func (*Service) NormalizeAllowedModels ¶
NormalizeAllowedModels canonicalizes an allowlist against the live catalog.
func (*Service) RequestCeilings ¶ added in v0.2.7
func (s *Service) RequestCeilings(ctx context.Context) requestparams.Ceilings
RequestCeilings returns the ceilings in force for the request's user path.
func (*Service) RequestPolicy ¶ added in v0.2.7
func (s *Service) RequestPolicy(userPath string) RequestPolicy
RequestPolicy returns the request policy in force on userPath. The root row's policy applies to every request: one without a user path (the master key, a key with no bound path that sent no header) answers to "/".
func (*Service) SetChangeNotifier ¶ added in v0.1.109
SetChangeNotifier installs fn, called after every successful policy write so other gateway instances reload the policies. Call it before serving.
func (*Service) SetConfigUsers ¶
SetConfigUsers installs the declarative (config.yaml / USERS) policies that shadow store rows of the same path. Call it before the first Refresh.
func (*Service) StartBackgroundRefresh ¶
StartBackgroundRefresh periodically reloads policies from storage until stopped.
func (*Service) ValidateManagedConfig ¶
ValidateManagedConfig canonicalizes every declared policy against the declared provider names so a misspelled selector fails startup loudly.
type Store ¶
type Store interface {
List(ctx context.Context) ([]User, error)
Upsert(ctx context.Context, user User) error
Delete(ctx context.Context, userPath string) error
Close() error
}
Store defines persistence operations for user policies.
type User ¶
type User struct {
UserPath string `json:"user_path" bson:"_id"`
// AllowedModels lists canonical model selectors: exact "provider/model",
// provider-wide "provider/", or model-wide "model". Empty means the node
// itself does not restrict models.
AllowedModels []string `json:"allowed_models" bson:"allowed_models"`
// DefaultModel serves a chat, Responses or Messages request under this
// path that names neither a model nor a provider. It is resolved like a
// model the client sent, so it may name a virtual model. The nearest path
// that sets one wins.
DefaultModel string `json:"default_model,omitempty" bson:"default_model,omitempty"`
// MaxReasoningEffort is the highest reasoning effort requests under this
// path may send upstream, one of requestparams.ReasoningEffortLevels.
// The lowest ceiling between the path and the root applies. A model that
// cannot reason below a minimum level keeps that minimum, and batches and
// /p/ passthrough are not capped.
MaxReasoningEffort string `json:"max_reasoning_effort,omitempty" bson:"max_reasoning_effort,omitempty"`
// MaxOutputTokens is the highest output token limit requests under this
// path may send upstream, and the limit of a request that sends none;
// zero sets no ceiling. The lowest ceiling between the path and the root
// applies. Batches and /p/ passthrough are not capped.
MaxOutputTokens int `json:"max_output_tokens,omitempty" bson:"max_output_tokens,omitempty"`
Description string `json:"description,omitempty" bson:"description,omitempty"`
CreatedAt time.Time `json:"created_at" bson:"created_at"`
UpdatedAt time.Time `json:"updated_at" bson:"updated_at"`
// Managed marks a row declared in config.yaml / USERS. Managed rows shadow
// store rows of the same path and are read-only through the admin API.
Managed bool `json:"managed,omitempty" bson:"-"`
}
User is one persisted access policy keyed by user path.
func ConfigUsers ¶
func ConfigUsers(entries []config.UserConfig) []User
ConfigUsers converts declarative config entries into managed policy rows.
type ValidationError ¶
type ValidationError = validation.Error
ValidationError indicates invalid user input.