Skip to content

Commit 81dc767

Browse files
Fix unmarshalling for configvarstring (#1413)
Signed-off-by: Waleed Malik <[email protected]> Signed-off-by: Waleed Malik <[email protected]> Co-authored-by: Waleed Malik <[email protected]>
1 parent 91a6b7c commit 81dc767

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

pkg/providerconfig/types/types.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"encoding/json"
2222
"errors"
2323
"fmt"
24+
"strconv"
2425

2526
clusterv1alpha1 "github.com/kubermatic/machine-controller/pkg/apis/cluster/v1alpha1"
2627
"github.com/kubermatic/machine-controller/pkg/jsonutil"
@@ -204,7 +205,16 @@ func (configVarString *ConfigVarString) UnmarshalJSON(b []byte) error {
204205
if !bytes.HasPrefix(b, []byte("{")) {
205206
b = bytes.TrimPrefix(b, []byte(`"`))
206207
b = bytes.TrimSuffix(b, []byte(`"`))
207-
configVarString.Value = string(b)
208+
209+
// `Unquote` expects the input string to be inside quotation marks.
210+
// Since we can have a string without any quotations, in which case `TrimPrefix` and
211+
// `TrimSuffix` will be noop. We explicitly add quotation marks to the input string
212+
// to make sure that `Unquote` never fails.
213+
s, err := strconv.Unquote("\"" + string(b) + "\"")
214+
if err != nil {
215+
return err
216+
}
217+
configVarString.Value = s
208218
return nil
209219
}
210220
// This type must have the same fields as ConfigVarString but not

pkg/providerconfig/types/types_test.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
"reflect"
2222
"testing"
2323

24-
"k8s.io/api/core/v1"
24+
v1 "k8s.io/api/core/v1"
2525
"k8s.io/utils/pointer"
2626
)
2727

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

164164
testCases := []ConfigVarString{
165165
{Value: "val"},
166+
{Value: "spe<ialv&lue"},
166167
{SecretKeyRef: GlobalSecretKeySelector{ObjectReference: v1.ObjectReference{Namespace: "ns", Name: "name"}, Key: "key"}},
167168
{Value: "val", SecretKeyRef: GlobalSecretKeySelector{ObjectReference: v1.ObjectReference{Namespace: "ns", Name: "name"}, Key: "key"}},
168169
{ConfigMapKeyRef: GlobalConfigMapKeySelector{ObjectReference: v1.ObjectReference{Namespace: "ns", Name: "name"}, Key: "key"}},
@@ -175,6 +176,11 @@ func TestConfigVarStringMarshallingAndUnmarshalling(t *testing.T) {
175176
ConfigMapKeyRef: GlobalConfigMapKeySelector{ObjectReference: v1.ObjectReference{Namespace: "ns", Name: "name"}, Key: "key"},
176177
SecretKeyRef: GlobalSecretKeySelector{ObjectReference: v1.ObjectReference{Namespace: "ns", Name: "name"}, Key: "key"},
177178
},
179+
{
180+
Value: "spe<ialv&lue",
181+
ConfigMapKeyRef: GlobalConfigMapKeySelector{ObjectReference: v1.ObjectReference{Namespace: "ns", Name: "name"}, Key: "key"},
182+
SecretKeyRef: GlobalSecretKeySelector{ObjectReference: v1.ObjectReference{Namespace: "ns", Name: "name"}, Key: "key"},
183+
},
178184
}
179185

180186
for _, cvs := range testCases {

0 commit comments

Comments
 (0)