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

[release/v1.42] Fix unmarshalling for configvarstring in cloudProviderSpec #1415

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
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