Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: conversion of raw extension fields #431

Merged
merged 1 commit into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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,
Expand All @@ -287,18 +290,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 @@ -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,
}
}
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