From dd7b33d1f9a7cdfaabbb9aaf7c0f3c1492d3aaa2 Mon Sep 17 00:00:00 2001 From: Alex Snaps Date: Tue, 15 Oct 2024 12:56:42 -0400 Subject: [PATCH] Facilitate StringValue, mimics expressions.Value Signed-off-by: Alex Snaps --- api/v1beta3/auth_config_types.go | 4 +++- controllers/auth_config_controller.go | 2 +- pkg/evaluators/metadata/generic_http.go | 7 +++---- pkg/expressions/cel/expressions.go | 18 ++++++++++++++---- 4 files changed, 21 insertions(+), 10 deletions(-) diff --git a/api/v1beta3/auth_config_types.go b/api/v1beta3/auth_config_types.go index 5bcb3613..db2bbb2d 100644 --- a/api/v1beta3/auth_config_types.go +++ b/api/v1beta3/auth_config_types.go @@ -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{} @@ -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. diff --git a/controllers/auth_config_controller.go b/controllers/auth_config_controller.go index 976e6412..235125e4 100644 --- a/controllers/auth_config_controller.go +++ b/controllers/auth_config_controller.go @@ -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 { diff --git a/pkg/evaluators/metadata/generic_http.go b/pkg/evaluators/metadata/generic_http.go index 9e56ca81..437f21eb 100644 --- a/pkg/evaluators/metadata/generic_http.go +++ b/pkg/evaluators/metadata/generic_http.go @@ -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" @@ -24,7 +23,7 @@ import ( type GenericHttp struct { Endpoint string - DynamicEndpoint *cel.Expression + DynamicEndpoint expressions.Value Method string Body expressions.Value Parameters []json.JSONProperty @@ -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) diff --git a/pkg/expressions/cel/expressions.go b/pkg/expressions/cel/expressions.go index a92a75b4..8cd7b602 100644 --- a/pkg/expressions/cel/expressions.go +++ b/pkg/expressions/cel/expressions.go @@ -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 { @@ -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 } @@ -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 {