mirror of
https://github.com/TecharoHQ/anubis.git
synced 2026-04-26 01:52:42 +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"
|
"net/http"
|
||||||
|
|
||||||
"github.com/TecharoHQ/anubis/internal"
|
"github.com/TecharoHQ/anubis/internal"
|
||||||
|
"github.com/TecharoHQ/anubis/lib/checker/expression/environment"
|
||||||
"github.com/TecharoHQ/anubis/lib/policy/expressions"
|
"github.com/TecharoHQ/anubis/lib/policy/expressions"
|
||||||
"github.com/google/cel-go/cel"
|
"github.com/google/cel-go/cel"
|
||||||
"github.com/google/cel-go/common/types"
|
"github.com/google/cel-go/common/types"
|
||||||
@@ -17,12 +18,12 @@ type Checker struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func New(cfg *Config) (*Checker, error) {
|
func New(cfg *Config) (*Checker, error) {
|
||||||
env, err := expressions.BotEnvironment()
|
env, err := environment.Bot()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
program, err := expressions.Compile(env, cfg.String())
|
program, err := environment.Compile(env, cfg.String())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("can't compile CEL program: %w", err)
|
return nil, fmt.Errorf("can't compile CEL program: %w", err)
|
||||||
}
|
}
|
||||||
|
|||||||
+10
-9
@@ -1,4 +1,4 @@
|
|||||||
package expressions
|
package environment
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"math/rand/v2"
|
"math/rand/v2"
|
||||||
@@ -10,11 +10,11 @@ import (
|
|||||||
"github.com/google/cel-go/ext"
|
"github.com/google/cel-go/ext"
|
||||||
)
|
)
|
||||||
|
|
||||||
// BotEnvironment creates a new CEL environment, this is the set of
|
// Bot creates a new CEL environment, this is the set of variables and
|
||||||
// variables and functions that are passed into the CEL scope so that
|
// functions that are passed into the CEL scope so that Anubis can fail
|
||||||
// Anubis can fail loudly and early when something is invalid instead
|
// loudly and early when something is invalid instead of blowing up at
|
||||||
// of blowing up at runtime.
|
// runtime.
|
||||||
func BotEnvironment() (*cel.Env, error) {
|
func Bot() (*cel.Env, error) {
|
||||||
return New(
|
return New(
|
||||||
// Variables exposed to CEL programs:
|
// Variables exposed to CEL programs:
|
||||||
cel.Variable("remoteAddress", cel.StringType),
|
cel.Variable("remoteAddress", cel.StringType),
|
||||||
@@ -57,13 +57,14 @@ func BotEnvironment() (*cel.Env, error) {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewThreshold creates a new CEL environment for threshold checking.
|
// Threshold creates a new CEL environment for threshold checking.
|
||||||
func ThresholdEnvironment() (*cel.Env, error) {
|
func Threshold() (*cel.Env, error) {
|
||||||
return New(
|
return New(
|
||||||
cel.Variable("weight", cel.IntType),
|
cel.Variable("weight", cel.IntType),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// New creates a new base CEL environment.
|
||||||
func New(opts ...cel.EnvOption) (*cel.Env, error) {
|
func New(opts ...cel.EnvOption) (*cel.Env, error) {
|
||||||
args := []cel.EnvOption{
|
args := []cel.EnvOption{
|
||||||
ext.Strings(
|
ext.Strings(
|
||||||
@@ -95,7 +96,7 @@ func New(opts ...cel.EnvOption) (*cel.Env, error) {
|
|||||||
return cel.NewEnv(args...)
|
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.
|
// Program for execution.
|
||||||
func Compile(env *cel.Env, src string) (cel.Program, error) {
|
func Compile(env *cel.Env, src string) (cel.Program, error) {
|
||||||
intermediate, iss := env.Compile(src)
|
intermediate, iss := env.Compile(src)
|
||||||
+5
-5
@@ -1,4 +1,4 @@
|
|||||||
package expressions
|
package environment
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
@@ -6,8 +6,8 @@ import (
|
|||||||
"github.com/google/cel-go/common/types"
|
"github.com/google/cel-go/common/types"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestBotEnvironment(t *testing.T) {
|
func TestBot(t *testing.T) {
|
||||||
env, err := BotEnvironment()
|
env, err := Bot()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed to create bot environment: %v", err)
|
t.Fatalf("failed to create bot environment: %v", err)
|
||||||
}
|
}
|
||||||
@@ -108,8 +108,8 @@ func TestBotEnvironment(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestThresholdEnvironment(t *testing.T) {
|
func TestThreshold(t *testing.T) {
|
||||||
env, err := ThresholdEnvironment()
|
env, err := Threshold()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed to create threshold environment: %v", err)
|
t.Fatalf("failed to create threshold environment: %v", err)
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user