Repository: kubermatic/machine-controller
Pull request #1414 (2022-08-23 09:42:41)

[release/v1.43] Fix unmarshalling for `configvarstring` in cloudProviderSpec
This is an automated cherry-pick of #1367

/assign ahmedwaleedmalik

```release-note
Fix unmarshalling for `configvarstring` in cloudProviderSpec.
```

kubermatic-bot: [APPROVALNOTIFIER] This PR is **NOT APPROVED**

This pull-request has been approved by: *<a href="https://github.com/kubermatic/machine-controller/pull/1414#" title="Author self-approved">kubermatic-bot</a>*
To complete the [pull request process](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process), please assign **embik** after the PR has been reviewed.
You can assign the PR to them by writing `/assign @embik` in a comment when ready.

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=kubermatic%2Fmachine-controller).

<details open>
Needs approval from an approver in each of these files:

- **[OWNERS](https://github.com/kubermatic/machine-controller/blob/release/v1.43/OWNERS)**

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":["embik"]} -->
kubermatic-bot: LGTM label has been added.  <details>Git tree hash: d2325d42c402da174e1c3bce990e3b28ee540d22</details>
kubermatic-bot: [APPROVALNOTIFIER] This PR is **APPROVED**

This pull-request has been approved by: *<a href="https://github.com/kubermatic/machine-controller/pull/1414#pullrequestreview-1081818543" title="Approved">ahmedwaleedmalik</a>*, *<a href="https://github.com/kubermatic/machine-controller/pull/1414#" title="Author self-approved">kubermatic-bot</a>*

The full list of commands accepted by this bot can be found [here](https://go.k8s.io/bot-commands?repo=kubermatic%2Fmachine-controller).

The pull request process is described [here](https://git.k8s.io/community/contributors/guide/owners.md#the-code-review-process)

<details >
Needs approval from an approver in each of these files:

- ~~[OWNERS](https://github.com/kubermatic/machine-controller/blob/release/v1.43/OWNERS)~~ [ahmedwaleedmalik]

Approvers can indicate their approval by writing `/approve` in a comment
Approvers can cancel approval by writing `/approve cancel` in a comment
</details>
<!-- META={"approvers":[]} -->

Patch:
diff --git a/pkg/providerconfig/types/types.go b/pkg/providerconfig/types/types.go
index 6bf7063c..b9f00440 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


Test patch:
diff --git a/pkg/providerconfig/types/types_test.go b/pkg/providerconfig/types/types_test.go
index 1ee10796..53cf64a8 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<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"}},
@@ -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 {