Skip to content

Commit

Permalink
Fix unmarshalling for configvarstring (#1415)
Browse files Browse the repository at this point in the history
Signed-off-by: Waleed Malik <[email protected]>

Signed-off-by: Waleed Malik <[email protected]>
Co-authored-by: Waleed Malik <[email protected]>
  • Loading branch information
kubermatic-bot and ahmedwaleedmalik authored Aug 23, 2022
1 parent 7cf528b commit 06c8dd0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
12 changes: 11 additions & 1 deletion pkg/providerconfig/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"encoding/json"
"errors"
"fmt"
"strconv"

clusterv1alpha1 "github.com/kubermatic/machine-controller/pkg/apis/cluster/v1alpha1"

Expand Down Expand Up @@ -203,7 +204,16 @@ func (configVarString *ConfigVarString) UnmarshalJSON(b []byte) error {
if !bytes.HasPrefix(b, []byte("{")) {
b = bytes.TrimPrefix(b, []byte(`"`))
b = bytes.TrimSuffix(b, []byte(`"`))
configVarString.Value = string(b)

// `Unquote` expects the input string to be inside quotation marks.
// Since we can have a string without any quotations, in which case `TrimPrefix` and
// `TrimSuffix` will be noop. We explicitly add quotation marks to the input string
// to make sure that `Unquote` never fails.
s, err := strconv.Unquote("\"" + string(b) + "\"")
if err != nil {
return err
}
configVarString.Value = s
return nil
}
// This type must have the same fields as ConfigVarString but not
Expand Down
8 changes: 7 additions & 1 deletion pkg/providerconfig/types/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"reflect"
"testing"

"k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/utils/pointer"
)

Expand Down Expand Up @@ -163,6 +163,7 @@ func TestConfigVarStringMarshallingAndUnmarshalling(t *testing.T) {

testCases := []ConfigVarString{
{Value: "val"},
{Value: "spe<ialv&lue"},
{SecretKeyRef: GlobalSecretKeySelector{ObjectReference: v1.ObjectReference{Namespace: "ns", Name: "name"}, Key: "key"}},
{Value: "val", SecretKeyRef: GlobalSecretKeySelector{ObjectReference: v1.ObjectReference{Namespace: "ns", Name: "name"}, Key: "key"}},
{ConfigMapKeyRef: GlobalConfigMapKeySelector{ObjectReference: v1.ObjectReference{Namespace: "ns", Name: "name"}, Key: "key"}},
Expand All @@ -175,6 +176,11 @@ func TestConfigVarStringMarshallingAndUnmarshalling(t *testing.T) {
ConfigMapKeyRef: GlobalConfigMapKeySelector{ObjectReference: v1.ObjectReference{Namespace: "ns", Name: "name"}, Key: "key"},
SecretKeyRef: GlobalSecretKeySelector{ObjectReference: v1.ObjectReference{Namespace: "ns", Name: "name"}, Key: "key"},
},
{
Value: "spe<ialv&lue",
ConfigMapKeyRef: GlobalConfigMapKeySelector{ObjectReference: v1.ObjectReference{Namespace: "ns", Name: "name"}, Key: "key"},
SecretKeyRef: GlobalSecretKeySelector{ObjectReference: v1.ObjectReference{Namespace: "ns", Name: "name"}, Key: "key"},
},
}

for _, cvs := range testCases {
Expand Down

0 comments on commit 06c8dd0

Please sign in to comment.