From a64198b704cecf80f6b1217c5a4aafadf5099ee0 Mon Sep 17 00:00:00 2001 From: Alex Snaps Date: Thu, 24 Oct 2024 18:05:48 -0400 Subject: [PATCH] I know it's halloween, but still... we should rewrite this in Ruby Signed-off-by: Alex Snaps --- controllers/auth_config_controller.go | 4 ++-- pkg/evaluators/identity.go | 10 ++++++---- pkg/evaluators/identity_extension.go | 5 ++--- pkg/evaluators/identity_extension_test.go | 24 +++++++++++------------ pkg/evaluators/identity_test.go | 4 ++-- 5 files changed, 24 insertions(+), 23 deletions(-) diff --git a/controllers/auth_config_controller.go b/controllers/auth_config_controller.go index e871c961..9140e322 100644 --- a/controllers/auth_config_controller.go +++ b/controllers/auth_config_controller.go @@ -183,13 +183,13 @@ func (r *AuthConfigReconciler) translateAuthConfig(ctx context.Context, authConf for identityCfgName, identity := range authConfigIdentityConfigs { extendedProperties := make([]evaluators.IdentityExtension, len(identity.Defaults)+len(identity.Overrides)) for propertyName, property := range identity.Defaults { - extendedProperties = append(extendedProperties, evaluators.NewIdentityExtension(propertyName, &json.JSONValue{ + extendedProperties = append(extendedProperties, evaluators.NewIdentityExtension(propertyName, json.JSONValue{ Static: property.Value, Pattern: property.Selector, }, false)) } for propertyName, property := range identity.Overrides { - extendedProperties = append(extendedProperties, evaluators.NewIdentityExtension(propertyName, &json.JSONValue{ + extendedProperties = append(extendedProperties, evaluators.NewIdentityExtension(propertyName, json.JSONValue{ Static: property.Value, Pattern: property.Selector, }, true)) diff --git a/pkg/evaluators/identity.go b/pkg/evaluators/identity.go index 0bce9cbf..01608d47 100644 --- a/pkg/evaluators/identity.go +++ b/pkg/evaluators/identity.go @@ -199,11 +199,13 @@ func (config *IdentityConfig) ResolveExtendedProperties(pipeline auth.AuthPipeli authJSON := pipeline.GetAuthorizationJSON() for _, extendedProperty := range config.ExtendedProperties { - resolved, err := extendedProperty.ResolveFor(extendedIdentityObject, authJSON) - if err != nil { - return nil, err + if extendedProperty.Value != nil { + resolved, err := extendedProperty.ResolveFor(extendedIdentityObject, authJSON) + if err != nil { + return nil, err + } + extendedIdentityObject[extendedProperty.Name] = resolved } - extendedIdentityObject[extendedProperty.Name] = resolved } return extendedIdentityObject, nil diff --git a/pkg/evaluators/identity_extension.go b/pkg/evaluators/identity_extension.go index 63dc87a9..bbea29fc 100644 --- a/pkg/evaluators/identity_extension.go +++ b/pkg/evaluators/identity_extension.go @@ -1,15 +1,14 @@ package evaluators import ( - "github.com/kuadrant/authorino/pkg/expressions" "github.com/kuadrant/authorino/pkg/json" ) -func NewIdentityExtension(name string, value expressions.Value, overwrite bool) IdentityExtension { +func NewIdentityExtension(name string, value json.JSONValue, overwrite bool) IdentityExtension { return IdentityExtension{ JSONProperty: json.JSONProperty{ Name: name, - Value: value, + Value: &value, }, Overwrite: overwrite, } diff --git a/pkg/evaluators/identity_extension_test.go b/pkg/evaluators/identity_extension_test.go index 34ee0f2b..922c1765 100644 --- a/pkg/evaluators/identity_extension_test.go +++ b/pkg/evaluators/identity_extension_test.go @@ -23,62 +23,62 @@ func TestResolveIdentityExtension(t *testing.T) { }{ { name: "static value for existing property without overwrite", - input: NewIdentityExtension("username", &json.JSONValue{Static: "foo"}, false), + input: NewIdentityExtension("username", json.JSONValue{Static: "foo"}, false), expected: "beth", }, { name: "static value for missing property without overwrite", - input: NewIdentityExtension("uid", &json.JSONValue{Static: "foo"}, false), + input: NewIdentityExtension("uid", json.JSONValue{Static: "foo"}, false), expected: "foo", }, { name: "static value for existing property without overwrite", - input: NewIdentityExtension("username", &json.JSONValue{Static: "foo"}, true), + input: NewIdentityExtension("username", json.JSONValue{Static: "foo"}, true), expected: "foo", }, { name: "static value for missing property without overwrite", - input: NewIdentityExtension("uid", &json.JSONValue{Static: "foo"}, true), + input: NewIdentityExtension("uid", json.JSONValue{Static: "foo"}, true), expected: "foo", }, { name: "existing pattern for existing property without overwrite", - input: NewIdentityExtension("username", &json.JSONValue{Pattern: "auth.identity.sub"}, false), + input: NewIdentityExtension("username", json.JSONValue{Pattern: "auth.identity.sub"}, false), expected: "beth", }, { name: "existing pattern for missing property without overwrite", - input: NewIdentityExtension("uid", &json.JSONValue{Pattern: "auth.identity.sub"}, false), + input: NewIdentityExtension("uid", json.JSONValue{Pattern: "auth.identity.sub"}, false), expected: "1234567890", }, { name: "existing pattern for existing property without overwrite", - input: NewIdentityExtension("username", &json.JSONValue{Pattern: "auth.identity.sub"}, true), + input: NewIdentityExtension("username", json.JSONValue{Pattern: "auth.identity.sub"}, true), expected: "1234567890", }, { name: "existing pattern for missing property without overwrite", - input: NewIdentityExtension("uid", &json.JSONValue{Pattern: "auth.identity.sub"}, true), + input: NewIdentityExtension("uid", json.JSONValue{Pattern: "auth.identity.sub"}, true), expected: "1234567890", }, { name: "missing pattern for existing property without overwrite", - input: NewIdentityExtension("username", &json.JSONValue{Pattern: "auth.identity.full_name"}, false), + input: NewIdentityExtension("username", json.JSONValue{Pattern: "auth.identity.full_name"}, false), expected: "beth", }, { name: "missing pattern for missing property without overwrite", - input: NewIdentityExtension("uid", &json.JSONValue{Pattern: "auth.identity.full_name"}, false), + input: NewIdentityExtension("uid", json.JSONValue{Pattern: "auth.identity.full_name"}, false), expected: "", }, { name: "missing pattern for existing property without overwrite", - input: NewIdentityExtension("username", &json.JSONValue{Pattern: "auth.identity.full_name"}, true), + input: NewIdentityExtension("username", json.JSONValue{Pattern: "auth.identity.full_name"}, true), expected: "", }, { name: "missing pattern for missing property without overwrite", - input: NewIdentityExtension("uid", &json.JSONValue{Pattern: "auth.identity.full_name"}, true), + input: NewIdentityExtension("uid", json.JSONValue{Pattern: "auth.identity.full_name"}, true), expected: "", }, } diff --git a/pkg/evaluators/identity_test.go b/pkg/evaluators/identity_test.go index 39620adf..8e16f6f2 100644 --- a/pkg/evaluators/identity_test.go +++ b/pkg/evaluators/identity_test.go @@ -41,8 +41,8 @@ func TestIdentityConfig_ResolveExtendedProperties(t *testing.T) { Name: "test", KubernetesAuth: &identity.KubernetesAuth{}, ExtendedProperties: []IdentityExtension{ - NewIdentityExtension("prop1", &json.JSONValue{Static: "value1"}, true), - NewIdentityExtension("prop2", &json.JSONValue{Pattern: "auth.identity.sub"}, true), + NewIdentityExtension("prop1", json.JSONValue{Static: "value1"}, true), + NewIdentityExtension("prop2", json.JSONValue{Pattern: "auth.identity.sub"}, true), }, }