policy

package
v0.20.0 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2026 License: Apache-2.0 Imports: 12 Imported by: 0 modules, 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 policy is the domain layer for the Policy entity: a reusable rule set a policy binding applies to accounts. A policy holds nothing of its own, only references: the models it allows, the pools it draws keys from, and the rate limits and budgets every request under it must fit, each counted once for the bound account (shared) or once per account under it. One kind serves every rung of the ladder; a host-owned policy is a tier preset and links no pools.

Wire format: Policy YAML/JSON carries entity *names*; the admin and seed boundaries resolve names to ids. PG storage: Spec.Models sits in a column, the links in junction tables (see store.go).

Out of scope: which accounts a policy governs (app/policybinding), and resolving the ladder per request (app/plan).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CanonicalModelRef added in v0.10.0

func CanonicalModelRef(r modelref.Ref) string

CanonicalModelRef renders a parsed Ref back to its shortest slug form. The Ref's segments are already slug-normalized by modelref.Parse.

func CanonicalizeModelRefs added in v0.10.0

func CanonicalizeModelRefs(refs []string) ([]string, error)

CanonicalizeModelRefs slugifies every ref to its shortest canonical form and drops post-slug duplicates, preserving order. Catalog resolution (does the ref match an enabled binding?) is a separate, store-backed check the caller layers on top.

Types

type BudgetLink struct {
	BudgetID string `json:"budgetId" yaml:"budgetId" validate:"required,uuid" doc:"Id of the linked budget."`
	// Shared reads back explicit; nil on write means true.
	Shared *bool `` /* 200-byte string literal not displayed */
}

BudgetLink applies one Budget to a policy's requests.

func (*BudgetLink) IsShared added in v0.20.0

func (l *BudgetLink) IsShared() bool

IsShared reports whether the link spends one budget for the bound account.

type Policy

type Policy struct {
	Meta meta.Metadata `json:"metadata" yaml:"metadata"`
	Spec Spec          `json:"spec"     yaml:"spec"`
}

Policy is the access gate. Composition enforces that every linked id resolves and that the author may use it.

func (*Policy) BudgetIDs added in v0.20.0

func (p *Policy) BudgetIDs() []string

BudgetIDs returns the ids of the linked budgets, in link order.

func (*Policy) CanonicalizeSpecModelRefs added in v0.10.0

func (p *Policy) CanonicalizeSpecModelRefs() error

CanonicalizeSpecModelRefs rewrites every grant list on the spec in place: Spec.Models and each RateLimitLink's Models. Errors name the offending field.

func (*Policy) IsEnabled

func (p *Policy) IsEnabled() bool

IsEnabled returns true when Enabled is unset or explicitly true.

func (*Policy) Normalize added in v0.20.0

func (p *Policy) Normalize()

Normalize gives the read form every boundary returns: models as a list (never null) and every link's shared flag explicit.

func (*Policy) Validate

func (p *Policy) Validate() error

Validate runs intra-row rules via the shared meta.Validator and enforces:

  • Owner.Kind is system, host, team, project or user.
  • no pool or budget is linked twice.
  • host-owned policies link no pools.
  • every model ref names a provider or a provider's model, never a host.

Cross-entity checks (every id resolves; the author may use it) live in app/refcheck.

type RateLimitLink struct {
	RateLimitID string   `json:"rateLimitId"      yaml:"rateLimitId"      validate:"required,uuid"    doc:"Id of the linked rate limit."`
	Models      []string `` /* 374-byte string literal not displayed */
	// Shared reads back explicit; nil on write means true.
	Shared *bool `` /* 206-byte string literal not displayed */
}

RateLimitLink applies one RateLimit to a policy's requests: to every model when Models is empty, otherwise only to requests for a model matching one of Models. One rate limit may be linked more than once with different Models or counting.

func (*RateLimitLink) IsShared added in v0.20.0

func (l *RateLimitLink) IsShared() bool

IsShared reports whether the link counts once for the bound account.

type Spec

type Spec struct {
	// Models is the catalog grant: "provider" for every model of a provider, "provider/model" for one model. Empty grants every model the policy's pools can serve. Hosts are the pools' business, so a ref naming one is refused.
	Models []string `` /* 372-byte string literal not displayed */

	// Enabled defaults to true when nil.
	Enabled *bool `` /* 145-byte string literal not displayed */

	// PoolIDs are the pools this policy draws keys from, tried in order, so a later pool is the failover for an earlier one. Host-owned policies list none.
	PoolIDs []string `` /* 238-byte string literal not displayed */

	// RateLimits are the rate limits a request under this policy must fit.
	RateLimits []RateLimitLink `` /* 141-byte string literal not displayed */

	// Budgets are the budgets a request under this policy must fit.
	Budgets []BudgetLink `` /* 175-byte string literal not displayed */
}

Spec carries the model grant, the links, and the enabled flag.

type Store

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

Store is the Policy data-access type. Holds a DB rather than Queries so Upsert can run a multi-table transaction; List reads without one.

func NewStore

func NewStore(db storage.DB) *Store

NewStore constructs a Store bound to a pool or an open transaction.

func (*Store) Delete

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

Delete removes a Policy by id. Junction rows cascade via FK.

func (*Store) Get

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

Get returns the Policy with the given id and its links, or (nil, nil) if not found.

func (*Store) List

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

List returns every Policy row with its links.

func (*Store) Upsert

func (s *Store) Upsert(ctx context.Context, p *Policy) error

Upsert writes p across policies + junction tables in a single tx.

Jump to

Keyboard shortcuts

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