mirror of
https://github.com/TecharoHQ/anubis.git
synced 2026-04-05 16:28:17 +00:00
refactor: move cel environment creation to a subpackage
Signed-off-by: Xe Iaso <me@xeiaso.net>
This commit is contained in:
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
@@ -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)
|
||||
}
|
||||
Reference in New Issue
Block a user