From 4af0114b7a88bd126ac37a23702d5df35127aff7 Mon Sep 17 00:00:00 2001 From: Guilherme Cassolato Date: Tue, 26 Sep 2023 14:02:40 +0200 Subject: [PATCH] fix: conversion of raw extension fields Fix back and forth conversion of [`runtime.RawExtension`)(https://pkg.go.dev/k8s.io/apimachinery@v0.23.0/pkg/runtime#RawExtension) fields. Example of fields and values affected: - Extended properties of identity objects (new `defaults` and `overrides` of the authentication config) - Custom `headers` of the former `denyWith` spec (new `response.(unauthenticated|unauthorized).headers`) - Custom `message` and `body` of the former `denyWith` spec, as well as all other fields typed in v1beta1 as [`StaticOrDynamicValue`](https://pkg.go.dev/github.com/kuadrant/authorino/api/v1beta1#StaticOrDynamicValue), when the field was set to a static string value containing line breaks or any other character that require escaping when marshalled Closes #430. --- api/v1beta2/auth_config_conversion.go | 18 +++++++++++++----- api/v1beta2/auth_config_conversion_test.go | 14 ++++++++++++++ 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/api/v1beta2/auth_config_conversion.go b/api/v1beta2/auth_config_conversion.go index d0acb1de..91f7056a 100644 --- a/api/v1beta2/auth_config_conversion.go +++ b/api/v1beta2/auth_config_conversion.go @@ -1,7 +1,7 @@ package v1beta2 import ( - "fmt" + "encoding/json" "github.com/kuadrant/authorino/api/v1beta1" "github.com/kuadrant/authorino/pkg/utils" @@ -263,7 +263,10 @@ func convertValueOrSelectorTo(src ValueOrSelector) v1beta1.StaticOrDynamicValue func convertValueOrSelectorFrom(src v1beta1.StaticOrDynamicValue) ValueOrSelector { value := k8sruntime.RawExtension{} if src.ValueFrom.AuthJSON == "" { - value.Raw = []byte(fmt.Sprintf(`"%s"`, src.Value)) + jsonString, err := json.Marshal(src.Value) + if err == nil { + value.Raw = jsonString + } } return ValueOrSelector{ Value: value, @@ -287,10 +290,11 @@ func convertPtrValueOrSelectorFrom(src *v1beta1.StaticOrDynamicValue) *ValueOrSe return &v } -func convertNamedValuesOrSelectorsTo(src NamedValuesOrSelectors) (jsonProperties []v1beta1.JsonProperty) { +func convertNamedValuesOrSelectorsTo(src NamedValuesOrSelectors) []v1beta1.JsonProperty { if src == nil { return nil } + jsonProperties := make([]v1beta1.JsonProperty, 0, len(src)) for name, valueOrSelector := range src { jsonProperties = append(jsonProperties, v1beta1.JsonProperty{ Name: name, @@ -298,7 +302,7 @@ func convertNamedValuesOrSelectorsTo(src NamedValuesOrSelectors) (jsonProperties ValueFrom: convertSelectorTo(valueOrSelector), }) } - return + return jsonProperties } func convertNamedValuesOrSelectorsFrom(src []v1beta1.JsonProperty) NamedValuesOrSelectors { @@ -307,8 +311,12 @@ func convertNamedValuesOrSelectorsFrom(src []v1beta1.JsonProperty) NamedValuesOr } namedValuesOrSelectors := NamedValuesOrSelectors{} for _, jsonProperty := range src { + value := k8sruntime.RawExtension{} + if jsonProperty.ValueFrom.AuthJSON == "" { + value.Raw = jsonProperty.Value.Raw + } namedValuesOrSelectors[jsonProperty.Name] = ValueOrSelector{ - Value: jsonProperty.Value, + Value: value, Selector: jsonProperty.ValueFrom.AuthJSON, } } diff --git a/api/v1beta2/auth_config_conversion_test.go b/api/v1beta2/auth_config_conversion_test.go index 0f6d95b1..5e4b91f4 100644 --- a/api/v1beta2/auth_config_conversion_test.go +++ b/api/v1beta2/auth_config_conversion_test.go @@ -428,7 +428,13 @@ func authConfig() *AuthConfig { } }, "unauthorized": { + "body": { + "value": "{\n \"kind\": \"Error\",\n \"id\": \"403\",\n \"href\": \"/forbidden\",\n \"code\": \"FORBIDDEN-403\",\n \"reason\": \"Forbidden\"\n}\n" + }, "headers": { + "content-type": { + "value": "application/json" + }, "random": { "selector": "auth.authorization.deny20percent" } @@ -642,7 +648,15 @@ func hubAuthConfig() *v1beta1.AuthConfig { } }, "unauthorized": { + "body": { + "value": "{\n \"kind\": \"Error\",\n \"id\": \"403\",\n \"href\": \"/forbidden\",\n \"code\": \"FORBIDDEN-403\",\n \"reason\": \"Forbidden\"\n}\n" + }, "headers": [ + { + "name": "content-type", + "value": "application/json", + "valueFrom": {} + }, { "name": "random", "valueFrom": {