users

package
v0.1.91 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2026 License: MIT Imports: 22 Imported by: 0

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

Constants

This section is empty.

Variables

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

func IsValidationError(err error) bool

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

func NormalizeAllowedModels(catalog Catalog, raw []string) ([]string, error)

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

func (*MongoDBStore) Delete

func (s *MongoDBStore) Delete(ctx context.Context, userPath string) error

func (*MongoDBStore) List

func (s *MongoDBStore) List(ctx context.Context) ([]User, error)

func (*MongoDBStore) Upsert

func (s *MongoDBStore) Upsert(ctx context.Context, user User) error

type Result

type Result struct {
	Service *Service
	Store   Store
	// contains filtered or unexported fields
}

Result holds the initialized user policy service and its owned resources.

func New

func New(ctx context.Context, cfg *config.Config, shared storage.Storage, catalog Catalog, declaredProviders []string) (*Result, error)

New creates the users subsystem on the shared storage connection. Declared config policies are validated against declaredProviders at startup.

func (*Result) Close

func (r *Result) Close() error

Close releases resources held by the users subsystem.

type SQLStore

type SQLStore struct {
	// contains filtered or unexported fields
}

SQLStore stores user policies in a SQL database.

func NewSQLStore

func NewSQLStore(ctx context.Context, db sqlx.DB) (*SQLStore, error)

NewSQLStore creates the users table if needed.

func (*SQLStore) Close

func (s *SQLStore) Close() error

func (*SQLStore) Delete

func (s *SQLStore) Delete(ctx context.Context, userPath string) error

func (*SQLStore) List

func (s *SQLStore) List(ctx context.Context) ([]User, error)

func (*SQLStore) Upsert

func (s *SQLStore) Upsert(ctx context.Context, user User) error

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

func NewService(store Store, catalog Catalog) (*Service, error)

NewService creates a user policy service backed by the store and catalog.

func (*Service) AllowsModel

func (s *Service) AllowsModel(ctx context.Context, selector core.ModelSelector) bool

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

func (s *Service) Constraints(userPath string) []User

Constraints returns the policies that restrict userPath, root first: every ancestor (including the path itself) whose allowlist is non-empty.

func (*Service) Delete

func (s *Service) Delete(ctx context.Context, userPath string) error

Delete removes one policy and refreshes the snapshot.

func (*Service) Get

func (s *Service) Get(userPath string) (User, bool)

Get returns the policy stored for exactly userPath.

func (*Service) List

func (s *Service) List() []User

List returns every cached policy sorted by path.

func (*Service) NormalizeAllowedModels

func (s *Service) NormalizeAllowedModels(raw []string) ([]string, error)

NormalizeAllowedModels canonicalizes an allowlist against the live catalog.

func (*Service) Refresh

func (s *Service) Refresh(ctx context.Context) error

Refresh reloads policies from storage and atomically swaps the snapshot.

func (*Service) SetConfigUsers

func (s *Service) SetConfigUsers(users []User)

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

func (s *Service) StartBackgroundRefresh(interval time.Duration) func()

StartBackgroundRefresh periodically reloads policies from storage until stopped.

func (*Service) Upsert

func (s *Service) Upsert(ctx context.Context, user User) (User, error)

Upsert validates and persists one policy, then refreshes the snapshot.

func (*Service) ValidateManagedConfig

func (s *Service) ValidateManagedConfig(declaredProviders []string) error

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.

Jump to

Keyboard shortcuts

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