Documentation
¶
Overview ¶
Package users manages per-user-path model access 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.
Index ¶
- Variables
- 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 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) 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) 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 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 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) NormalizeAllowedModels ¶
NormalizeAllowedModels canonicalizes an allowlist against the live catalog.
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"`
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.