Documentation
¶
Overview ¶
Package experiments decides whether an experiment is enabled for a subject user. Each user-scoped experiment can have one stored rule that inherits the startup default, forces it on or off, or targets users with a CEL condition. Every error decides off (fail closed). Decisions gate features; they are not an authorization boundary.
Index ¶
- Constants
- func AuditRecord(ex codersdk.Experiment, rule Rule) database.ExperimentRule
- func IsUserScoped(ex codersdk.Experiment) bool
- func ReadRules(ctx context.Context, db database.Store) (map[codersdk.Experiment]Rule, error)
- func ValidateRule(ex codersdk.Experiment, rule Rule) error
- type Evaluator
- type Mode
- type RevisionConflictError
- type Rule
- type Store
- type StoredRule
- type User
Constants ¶
const (
// MaxConditionSize is the maximum condition length in code points.
MaxConditionSize = 4096
)
Variables ¶
This section is empty.
Functions ¶
func AuditRecord ¶
func AuditRecord(ex codersdk.Experiment, rule Rule) database.ExperimentRule
AuditRecord returns the audited form of rule for ex. The ID is derived from the experiment name, so every audit entry for one experiment shares it. A zero Rule (no stored rule) yields empty mode and condition with revision 0.
func IsUserScoped ¶
func IsUserScoped(ex codersdk.Experiment) bool
IsUserScoped reports whether ex accepts runtime rules.
func ReadRules ¶
ReadRules returns the stored rule of every experiment that has one, including experiments that do not accept runtime rules. A stored value the Evaluator treats as malformed is returned with an empty Mode and the revision WriteRule expects to replace it, which is 0 when the stored revision is unreadable or not positive.
func ValidateRule ¶
func ValidateRule(ex codersdk.Experiment, rule Rule) error
ValidateRule checks that rule can be stored for ex. The returned error can contain full CEL diagnostics, which may quote the condition. Return it only to the rule author; never log it.
Types ¶
type Evaluator ¶
type Evaluator struct {
// contains filtered or unexported fields
}
Evaluator decides whether experiments are enabled for a subject user. It is safe for concurrent use. It does not cache decisions: every call reads the rules once, so rule changes apply to the next call.
func New ¶
New returns an Evaluator. static is the startup --experiments list; it is the default for experiments whose rule is absent or inherits.
func (*Evaluator) EnabledExperiments ¶
EnabledExperiments returns the experiments enabled for userID. Static entries keep their order, including unknown strings. User-scoped entries decided off are removed, and user-scoped entries decided on that are not in the static list are appended in ExperimentsKnown order. Rules and attributes are each loaded at most once.
type Mode ¶
type Mode string
Mode selects how a rule decides an experiment.
const ( // ModeInherit uses the startup --experiments default. ModeInherit Mode = "inherit" // ModeOn enables the experiment for every user. ModeOn Mode = "on" // ModeOff disables the experiment for every user, even when it is // enabled at startup. ModeOff Mode = "off" // ModeCondition enables the experiment for users whose condition is // true. ModeCondition Mode = "condition" )
type RevisionConflictError ¶
type RevisionConflictError struct {
Experiment codersdk.Experiment
Expected int64
// Current is the stored revision; zero means no rule is stored or the
// stored value has no readable positive revision.
Current int64
}
RevisionConflictError is returned by WriteRule when the expected revision is not the current revision of the stored rule.
func (*RevisionConflictError) Error ¶
func (e *RevisionConflictError) Error() string
type Rule ¶
type Rule struct {
Mode Mode `json:"mode"`
// Condition is a CEL expression over `user`. It is set only when Mode
// is ModeCondition.
Condition string `json:"condition,omitempty"`
// Revision increases on every write. Stored rules have revision 1 or
// higher; a stored rule below 1 is malformed. The zero Rule, with
// revision 0, stands for no stored rule.
Revision int64 `json:"revision"`
UpdatedBy uuid.UUID `json:"updated_by"`
UpdatedAt time.Time `json:"updated_at"`
}
Rule is the decoded form of one stored experiment rule.
func WriteRule ¶
func WriteRule( ctx context.Context, db database.Store, actorID uuid.UUID, ex codersdk.Experiment, rule Rule, expectedRevision int64, ) (oldRule, newRule Rule, changed bool, err error)
WriteRule stores the mode and condition of rule as the rule for ex when the stored revision equals expectedRevision. The Revision, UpdatedBy and UpdatedAt fields of rule are ignored: WriteRule sets them.
It returns the stored rule before and after the call, as read and written inside one transaction. Before the first write of ex, the old rule is the zero Rule (revision 0). When the stored mode and condition already equal rule's, nothing is written, changed is false and the new rule equals the old one. Writing inherit to an experiment without a row stores revision 1 and counts as a change.
A stored value without a readable positive revision, which only a write outside this API can produce, counts as revision 0: expectedRevision 0 replaces it and the rule restarts at revision 1.
A stale expectedRevision returns *RevisionConflictError. Validation errors can contain CEL diagnostics; return them only to the rule author and never log them.
type Store ¶
type Store interface {
// Rules returns the stored rule for every configured experiment.
// Experiments without a rule are absent from the map.
Rules(ctx context.Context) (map[codersdk.Experiment]StoredRule, error)
// UserAttributes loads the attributes conditions can read for userID.
UserAttributes(ctx context.Context, userID uuid.UUID) (User, error)
}
Store supplies stored rules and subject attributes to the Evaluator.
func NewDBStore ¶
NewDBStore returns a Store that reads rules and subject attributes from db. Reads run as the system: the Evaluator decides for any subject, not for the caller.
type StoredRule ¶
type StoredRule struct {
Value json.RawMessage
}
StoredRule is the raw stored value of one experiment rule. It stays undecoded so that the Evaluator decides how undecodable values and unknown modes behave: they decide off.
type User ¶
type User struct {
ID string `cel:"id"`
Username string `cel:"username"`
// Email is compared as stored (case-sensitive).
Email string `cel:"email"`
// Roles are the explicit site roles. The implied member role and
// organization roles are not included.
Roles []string `cel:"roles"`
// Organizations are the names of organizations the user belongs to.
Organizations []string `cel:"organizations"`
// Groups are "<org>/<group>" names, including "<org>/Everyone".
Groups []string `cel:"groups"`
}
User holds the attributes a condition can read as `user`. Only string and []string fields are allowed. Fields carry `cel` tags and no `json` tags so that CEL exposes exactly these names.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package experimentstest provides test doubles for the experiments package.
|
Package experimentstest provides test doubles for the experiments package. |