From 06c8dd076549d751405dbe99274f4802e87d9919 Mon Sep 17 00:00:00 2001 From: Kubermatic Bot <41968677+kubermatic-bot@users.noreply.github.com> Date: Tue, 23 Aug 2022 12:02:00 +0200 Subject: [PATCH] Fix unmarshalling for configvarstring (#1415) Signed-off-by: Waleed Malik Signed-off-by: Waleed Malik Co-authored-by: Waleed Malik --- pkg/providerconfig/types/types.go | 12 +++++++++++- pkg/providerconfig/types/types_test.go | 8 +++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/pkg/providerconfig/types/types.go b/pkg/providerconfig/types/types.go index 6bf7063c5..b9f00440b 100644 --- a/pkg/providerconfig/types/types.go +++ b/pkg/providerconfig/types/types.go @@ -21,6 +21,7 @@ import ( "encoding/json" "errors" "fmt" + "strconv" clusterv1alpha1 "github.com/kubermatic/machine-controller/pkg/apis/cluster/v1alpha1" @@ -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 diff --git a/pkg/providerconfig/types/types_test.go b/pkg/providerconfig/types/types_test.go index 1ee107966..53cf64a81 100644 --- a/pkg/providerconfig/types/types_test.go +++ b/pkg/providerconfig/types/types_test.go @@ -21,7 +21,7 @@ import ( "reflect" "testing" - "k8s.io/api/core/v1" + v1 "k8s.io/api/core/v1" "k8s.io/utils/pointer" ) @@ -163,6 +163,7 @@ func TestConfigVarStringMarshallingAndUnmarshalling(t *testing.T) { testCases := []ConfigVarString{ {Value: "val"}, + {Value: "spe