From a494d267082b6a631bc9dfab234ae3d1b181288b Mon Sep 17 00:00:00 2001 From: Xe Iaso Date: Fri, 25 Jul 2025 19:55:56 +0000 Subject: [PATCH] refactor: move cel environment creation to a subpackage Signed-off-by: Xe Iaso --- lib/checker/expression/checker.go | 5 +++-- .../expression/environment}/environment.go | 19 ++++++++++--------- .../environment}/environment_test.go | 10 +++++----- 3 files changed, 18 insertions(+), 16 deletions(-) rename lib/{policy/expressions => checker/expression/environment}/environment.go (84%) rename lib/{policy/expressions => checker/expression/environment}/environment_test.go (97%) diff --git a/lib/checker/expression/checker.go b/lib/checker/expression/checker.go index 557d69a1..4c80513f 100644 --- a/lib/checker/expression/checker.go +++ b/lib/checker/expression/checker.go @@ -5,6 +5,7 @@ import ( "net/http" "github.com/TecharoHQ/anubis/internal" + "github.com/TecharoHQ/anubis/lib/checker/expression/environment" "github.com/TecharoHQ/anubis/lib/policy/expressions" "github.com/google/cel-go/cel" "github.com/google/cel-go/common/types" @@ -17,12 +18,12 @@ type Checker struct { } func New(cfg *Config) (*Checker, error) { - env, err := expressions.BotEnvironment() + env, err := environment.Bot() if err != nil { return nil, err } - program, err := expressions.Compile(env, cfg.String()) + program, err := environment.Compile(env, cfg.String()) if err != nil { return nil, fmt.Errorf("can't compile CEL program: %w", err) } diff --git a/lib/policy/expressions/environment.go b/lib/checker/expression/environment/environment.go similarity index 84% rename from lib/policy/expressions/environment.go rename to lib/checker/expression/environment/environment.go index 14b57be3..f52da798 100644 --- a/lib/policy/expressions/environment.go +++ b/lib/checker/expression/environment/environment.go @@ -1,4 +1,4 @@ -package expressions +package environment import ( "math/rand/v2" @@ -10,11 +10,11 @@ import ( "github.com/google/cel-go/ext" ) -// BotEnvironment creates a new CEL environment, this is the set of -// variables and functions that are passed into the CEL scope so that -// Anubis can fail loudly and early when something is invalid instead -// of blowing up at runtime. -func BotEnvironment() (*cel.Env, error) { +// Bot creates a new CEL environment, this is the set of variables and +// functions that are passed into the CEL scope so that Anubis can fail +// loudly and early when something is invalid instead of blowing up at +// runtime. +func Bot() (*cel.Env, error) { return New( // Variables exposed to CEL programs: cel.Variable("remoteAddress", cel.StringType), @@ -57,13 +57,14 @@ func BotEnvironment() (*cel.Env, error) { ) } -// NewThreshold creates a new CEL environment for threshold checking. -func ThresholdEnvironment() (*cel.Env, error) { +// Threshold creates a new CEL environment for threshold checking. +func Threshold() (*cel.Env, error) { return New( cel.Variable("weight", cel.IntType), ) } +// New creates a new base CEL environment. func New(opts ...cel.EnvOption) (*cel.Env, error) { args := []cel.EnvOption{ ext.Strings( @@ -95,7 +96,7 @@ func New(opts ...cel.EnvOption) (*cel.Env, error) { return cel.NewEnv(args...) } -// Compile takes CEL environment and syntax tree then emits an optimized +// Compile takes a CEL environment and syntax tree then emits an optimized // Program for execution. func Compile(env *cel.Env, src string) (cel.Program, error) { intermediate, iss := env.Compile(src) diff --git a/lib/policy/expressions/environment_test.go b/lib/checker/expression/environment/environment_test.go similarity index 97% rename from lib/policy/expressions/environment_test.go rename to lib/checker/expression/environment/environment_test.go index 9878e1ce..673a270d 100644 --- a/lib/policy/expressions/environment_test.go +++ b/lib/checker/expression/environment/environment_test.go @@ -1,4 +1,4 @@ -package expressions +package environment import ( "testing" @@ -6,8 +6,8 @@ import ( "github.com/google/cel-go/common/types" ) -func TestBotEnvironment(t *testing.T) { - env, err := BotEnvironment() +func TestBot(t *testing.T) { + env, err := Bot() if err != nil { t.Fatalf("failed to create bot environment: %v", err) } @@ -108,8 +108,8 @@ func TestBotEnvironment(t *testing.T) { }) } -func TestThresholdEnvironment(t *testing.T) { - env, err := ThresholdEnvironment() +func TestThreshold(t *testing.T) { + env, err := Threshold() if err != nil { t.Fatalf("failed to create threshold environment: %v", err) }