cel

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

content.go — text-analysis helpers for Keep rule expressions.

Package cel provides Keep's custom CEL environment for rule expressions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContainsAnyFunc

func ContainsAnyFunc(field string, terms []string) bool

ContainsAnyFunc returns true if field contains any of the terms (case-insensitive).

func DayOfWeek

func DayOfWeek(timestamp time.Time) string

DayOfWeek returns the lowercase name of the weekday (e.g., "monday") for timestamp interpreted in UTC.

func DayOfWeekTZ

func DayOfWeekTZ(tz string, timestamp time.Time) string

DayOfWeekTZ returns the lowercase name of the weekday for timestamp interpreted in the given IANA timezone. Returns empty string on invalid tz.

func EstimateTokensFunc

func EstimateTokensFunc(field string) int

EstimateTokensFunc returns a rough token count (chars/4).

func InTimeWindow

func InTimeWindow(start, end, tz string, timestamp time.Time) bool

InTimeWindow reports whether timestamp falls within the [start, end) time-of-day window in the given IANA timezone. start and end are "HH:MM" strings (24-hour). If end <= start, midnight-wrapping is NOT supported and false is always returned.

func InjectOriginalParams added in v0.2.0

func InjectOriginalParams(expr string) string

InjectOriginalParams rewrites CEL expressions so that single-argument calls to any function in originalParamsFunctions get _originalParams injected as a second argument.

Only single-argument calls are rewritten; calls that already have multiple arguments are left as-is. String literals inside the call are handled correctly (parentheses inside strings do not confuse the rewriter).

func LowerFunc

func LowerFunc(s string) string

func MatchesDomainFunc

func MatchesDomainFunc(email string, domains []string) bool

func ParseGraphQLRequest added in v0.7.0

func ParseGraphQLRequest(body any) map[string]any

ParseGraphQLRequest parses a GraphQL-over-HTTP request body (the decoded JSON object {"query": ..., "variables": ..., "operationName": ...}) and returns a structured, JSON-shaped view of it for policy evaluation:

{
  "valid": bool,          // false on any input or parse problem
  "error": string,        // "" when valid
  "operations": [{"type": "query"|"mutation"|"subscription", "name": string, "fields": [field]}],
  "queries":       [field], // root fields of every query operation
  "mutations":     [field], // root fields of every mutation operation
  "subscriptions": [field], // root fields of every subscription operation
}
field = {"name": string, "alias": string, "args": map}

Every operation in the document is reported regardless of operationName, so a policy sees a mutation even when the client asks the server to run a different operation from the same document. Fragment spreads and inline fragments in an operation's root selection set are expanded; type conditions and directives (@skip/@include) are ignored, so a conditionally skipped field is still reported. Arguments have variables substituted from "variables", falling back to the variable's default value; a variable with neither is omitted from an object (or null inside a list).

It never returns an error: malformed input yields valid=false with empty operation and field lists, so `.exists()` over them is always safe.

func PrepareWhen added in v0.7.0

func PrepareWhen(expr string, aliases, defs map[string]string) string

PrepareWhen turns a rule's `when` expression into the source that is compiled: profile aliases and defs are substituted, then original-case params are injected for the functions that need them. The engine and rule-file validation both use it, so an expression validates exactly when it compiles at load time.

func ResolveAliases

func ResolveAliases(expr string, aliases map[string]string) string

ResolveAliases rewrites a CEL expression string, replacing unqualified identifiers that match alias names with their params.* paths. Returns the expression unchanged if aliases is nil or empty.

An identifier is considered unqualified when it is NOT immediately preceded by a dot (field-access operator). String literals (single- or double-quoted) are left untouched.

func UpperFunc

func UpperFunc(s string) string

Types

type Env

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

Env is Keep's configured CEL environment with custom functions.

func NewEnv

func NewEnv(opts ...EnvOption) (*Env, error)

NewEnv creates a new CEL environment with Keep's input variables (params, context) and all custom functions registered.

func (*Env) Compile

func (e *Env) Compile(expr string) (*Program, error)

Compile parses and type-checks a CEL expression string.

type EnvOption

type EnvOption func(*envConfig)

EnvOption configures the CEL environment.

func WithRateStore

func WithRateStore(store *rate.Store) EnvOption

WithRateStore configures the CEL environment with a rate counter store, enabling the rateCount(key, window) function.

func WithSecretDetector

func WithSecretDetector(d *secrets.Detector) EnvOption

WithSecretDetector configures the CEL environment with a secret detector, enabling the hasSecrets(field) function.

type Program

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

Program is a compiled CEL expression ready for evaluation.

func (*Program) Eval

func (p *Program) Eval(params map[string]any, ctx map[string]any, originalParams ...map[string]any) (bool, error)

Eval evaluates a compiled program against the given params and context. Returns the boolean result. Returns an error if evaluation fails or the expression does not return a bool. Missing field accesses return false rather than an error.

originalParams is optional: when provided, it is passed as _originalParams for functions like hasSecrets that need pre-normalization values.

func (*Program) ReferencesParam added in v0.5.0

func (p *Program) ReferencesParam(field string) bool

ReferencesParam reports whether the compiled expression may read the given field off the params input variable. It returns true for idiomatic dot- or index-access (params.body, params["body"], including nested forms like params.body.foo), and — failing safe — for every field when the expression touches the params map opaquely (see collectParamRefs). The result is computed at compile time, so this is a cheap lookup.

Jump to

Keyboard shortcuts

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