Skip to content

Commit

Permalink
Merge pull request #431 from Kuadrant/fix/conversion-raw-extension
Browse files Browse the repository at this point in the history
fix: conversion of raw extension fields
  • Loading branch information
guicassolato authored Sep 26, 2023
2 parents d6111da + 4af0114 commit 991af36
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
18 changes: 13 additions & 5 deletions api/v1beta2/auth_config_conversion.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package v1beta2

import (
"fmt"
"encoding/json"

"github.com/kuadrant/authorino/api/v1beta1"
"github.com/kuadrant/authorino/pkg/utils"
Expand Down Expand Up @@ -289,7 +289,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,
Expand All @@ -313,18 +316,19 @@ 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,
Value: valueOrSelector.Value,
ValueFrom: convertSelectorTo(valueOrSelector),
})
}
return
return jsonProperties
}

func convertNamedValuesOrSelectorsFrom(src []v1beta1.JsonProperty) NamedValuesOrSelectors {
Expand All @@ -333,8 +337,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,
}
}
Expand Down
14 changes: 14 additions & 0 deletions api/v1beta2/auth_config_conversion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand Down Expand Up @@ -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": {
Expand Down

0 comments on commit 991af36

Please sign in to comment.