Skip to content

Commit

Permalink
Facilitate StringValue, mimics expressions.Value
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Snaps <[email protected]>
  • Loading branch information
alexsnaps committed Oct 15, 2024
1 parent 8eed1ec commit dd7b33d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
4 changes: 3 additions & 1 deletion api/v1beta3/auth_config_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,8 @@ type PlainIdentitySpec struct {
// Any pattern supported by https://pkg.go.dev/github.com/tidwall/gjson can be used.
// The following Authorino custom modifiers are supported: @extract:{sep:" ",pos:0}, @replace{old:"",new:""}, @case:upper|lower, @base64:encode|decode and @strip.
Selector string `json:"selector"`

Expression CelExpression `json:",omitempty"`
}

type AnonymousAccessSpec struct{}
Expand Down Expand Up @@ -449,7 +451,7 @@ type HttpEndpointSpec struct {
// E.g. https://ext-auth-server.io/metadata?p={request.path}
Url string `json:"url"`

UrlExpression CelExpression `json:"urlExpression,omitempty"`
UrlExpression CelExpression `json:",omitempty"`

// HTTP verb used in the request to the service. Accepted values: GET (default), POST.
// When the request method is POST, the authorization JSON is passed in the body of the request.
Expand Down
2 changes: 1 addition & 1 deletion controllers/auth_config_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -896,7 +896,7 @@ func (r *AuthConfigReconciler) buildGenericHttpEvaluator(ctx context.Context, ht
method = string(*m)
}

var dynamicEndpoint *cel.Expression
var dynamicEndpoint expressions.Value
if http.UrlExpression.Expression != "" {
endpoint, err := cel.NewStringExpression(http.UrlExpression.Expression)
if err != nil {
Expand Down
7 changes: 3 additions & 4 deletions pkg/evaluators/metadata/generic_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/kuadrant/authorino/pkg/auth"
"github.com/kuadrant/authorino/pkg/context"
"github.com/kuadrant/authorino/pkg/expressions"
"github.com/kuadrant/authorino/pkg/expressions/cel"
"github.com/kuadrant/authorino/pkg/json"
"github.com/kuadrant/authorino/pkg/log"
"github.com/kuadrant/authorino/pkg/oauth2"
Expand All @@ -24,7 +23,7 @@ import (

type GenericHttp struct {
Endpoint string
DynamicEndpoint *cel.Expression
DynamicEndpoint expressions.Value
Method string
Body expressions.Value
Parameters []json.JSONProperty
Expand All @@ -44,10 +43,10 @@ func (h *GenericHttp) Call(pipeline auth.AuthPipeline, ctx gocontext.Context) (i
authJSON := pipeline.GetAuthorizationJSON()
var endpoint string
if h.DynamicEndpoint != nil {
if val, err := h.DynamicEndpoint.EvaluateStringValue(authJSON); err != nil {
if val, err := h.DynamicEndpoint.ResolveFor(authJSON); err != nil {
return nil, err
} else {
endpoint = val
endpoint = val.(string)
}
} else {
endpoint = json.ReplaceJSONPlaceholders(h.Endpoint, authJSON)
Expand Down
18 changes: 14 additions & 4 deletions pkg/expressions/cel/expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ type Expression struct {
source string
}

type StringExpression struct {
expression Expression
}

func NewExpression(source string) (*Expression, error) {
program, err := Compile(source, nil)
if err != nil {
Expand All @@ -61,14 +65,16 @@ func NewExpression(source string) (*Expression, error) {
}, nil
}

func NewStringExpression(source string) (*Expression, error) {
func NewStringExpression(source string) (*StringExpression, error) {
program, err := Compile(source, cel.StringType)
if err != nil {
return nil, err
}
return &Expression{
program: program,
source: source,
return &StringExpression{
expression: Expression{
program: program,
source: source,
},
}, nil
}

Expand All @@ -85,6 +91,10 @@ func (e *Expression) ResolveFor(json string) (interface{}, error) {
}
}

func (e *StringExpression) ResolveFor(json string) (interface{}, error) {
return e.expression.EvaluateStringValue(json)
}

func (e *Expression) Evaluate(json string) (ref.Val, *cel.EvalDetails, error) {
input, err := AuthJsonToCel(json)
if err != nil {
Expand Down

0 comments on commit dd7b33d

Please sign in to comment.