Skip to content

Commit

Permalink
Refactored Cel into own pkg, removed CelResponse
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Snaps <alex@wcgw.dev>
  • Loading branch information
alexsnaps committed Oct 14, 2024
1 parent 81ec2b4 commit fc65fbc
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 49 deletions.
3 changes: 0 additions & 3 deletions api/v1beta2/auth_config_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ const (
PlainAuthResponse
JsonAuthResponse
WristbandAuthResponse
CelAuthResponse

// The following constants are used to identify the different methods of callback functions.
UnknownCallbackMethod CallbackMethod = iota
Expand Down Expand Up @@ -734,8 +733,6 @@ func (s *SuccessResponseSpec) GetMethod() AuthResponseMethod {
type AuthResponseMethodSpec struct {
// Plain text content
Plain *PlainAuthResponseSpec `json:"plain,omitempty"`
// Cel Expression, where the result is outputted as JSON
Expression string `json:"expression,omitempty"`
// JSON object
// Specify it as the list of properties of the object, whose values can combine static values and values selected from the authorization JSON.
Json *JsonAuthResponseSpec `json:"json,omitempty"`
Expand Down
7 changes: 0 additions & 7 deletions controllers/auth_config_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -628,13 +628,6 @@ func injectResponseConfig(ctx context.Context, authConfig *api.AuthConfig, succe

translatedResponse.DynamicJSON = response_evaluators.NewDynamicJSONResponse(jsonProperties)

case api.CelAuthResponse:
if exp, err := response_evaluators.NewDynamicCelResponse(string(*successResponse.Expression)); err != nil {
return err
} else {
translatedResponse.DynamicCEL = exp
}

// plain
case api.PlainAuthResponse:
translatedResponse.Plain = &response_evaluators.Plain{
Expand Down
45 changes: 6 additions & 39 deletions pkg/evaluators/response/dynamic_cel.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,14 @@ package response

import (
"context"
"reflect"
"strings"

"github.com/golang/protobuf/jsonpb"
"github.com/google/cel-go/cel"
"github.com/google/cel-go/checker/decls"
"github.com/google/cel-go/common/types/ref"
"github.com/kuadrant/authorino/pkg/auth"
"github.com/kuadrant/authorino/pkg/expressions"
"google.golang.org/protobuf/types/known/structpb"

"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
)

const rootBinding = "auth"
Expand All @@ -22,30 +18,14 @@ func NewDynamicCelResponse(expression string) (*DynamicCEL, error) {

cel_exp := DynamicCEL{}

env, err := cel.NewEnv(cel.Declarations(
if program, err := expressions.CelCompile(expression, cel.Declarations(
decls.NewConst(rootBinding, decls.NewObjectType("google.protobuf.Struct"), nil),
))
if err != nil {
return nil, err
}

ast, issues := env.Parse(expression)
if issues.Err() != nil {
return nil, issues.Err()
}

checked, issues := env.Check(ast)
if issues.Err() != nil {
return nil, issues.Err()
}

program, err := env.Program(checked)
if err != nil {
)); err != nil {
return nil, err
} else {
cel_exp.program = program
}

cel_exp.program = program

return &cel_exp, nil
}

Expand All @@ -69,22 +49,9 @@ func (c *DynamicCEL) Call(pipeline auth.AuthPipeline, ctx context.Context) (inte
return nil, err
}

if jsonVal, err := valueToJSON(result); err != nil {
if jsonVal, err := expressions.CelValueToJSON(result); err != nil {
return nil, err
} else {
return jsonVal, nil
}
}

func valueToJSON(val ref.Val) (string, error) {
v, err := val.ConvertToNative(reflect.TypeOf(&structpb.Value{}))
if err != nil {
return "", err
}
marshaller := protojson.MarshalOptions{Multiline: false}
bytes, err := marshaller.Marshal(v.(proto.Message))
if err != nil {
return "", err
}
return string(bytes), nil
}
47 changes: 47 additions & 0 deletions pkg/expressions/cel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package expressions

import (
"reflect"

"github.com/google/cel-go/cel"
"github.com/google/cel-go/common/types/ref"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/structpb"
)

func CelCompile(expression string, opts ...cel.EnvOption) (cel.Program, error) {
env, env_err := cel.NewEnv(opts...)
if env_err != nil {
return nil, env_err
}

ast, issues := env.Parse(expression)
if issues.Err() != nil {
return nil, issues.Err()
}

checked, issues := env.Check(ast)
if issues.Err() != nil {
return nil, issues.Err()
}

program, err := env.Program(checked)
if err != nil {
return nil, err
}
return program, nil
}

func CelValueToJSON(val ref.Val) (string, error) {
v, err := val.ConvertToNative(reflect.TypeOf(&structpb.Value{}))
if err != nil {
return "", err
}
marshaller := protojson.MarshalOptions{Multiline: false}
bytes, err := marshaller.Marshal(v.(proto.Message))
if err != nil {
return "", err
}
return string(bytes), nil
}

0 comments on commit fc65fbc

Please sign in to comment.