diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 97b19f16..39ddb820 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -165,9 +165,12 @@ func ParseLines(lines []string, params map[string]interface{}, logger logrus.Fie key := strings.TrimSpace(parts[0]) value := strings.TrimSpace(parts[1]) // Skip the quotes in the value if present - unquotedValue, err := strconv.Unquote(string(value)) - if err != nil { - logger.WithError(err).Debugf("Failed to unquote value %v for key %v. Just store the original value string", value, key) + var unquotedValue string + // Check for existence of a quoted value, and manually remove strip the quotes + if len(value) >= 2 && value[0] == '"' && value[len(value)-1] == '"' { + unquotedValue = value[1 : len(value)-1] + } else { + logger.Debugf("Failed to unquote value %v for key %v. Just store the original value string", value, key) params[key] = string(value) continue } @@ -1022,7 +1025,7 @@ func waitForPvSecret(ctx context.Context, clientSet *kubernetes.Clientset, names } /* - Adds the Velero label to exclude this K8S resource from the backup +Adds the Velero label to exclude this K8S resource from the backup */ func AddVeleroExcludeLabelToObjectMeta(objectMeta *metav1.ObjectMeta) { objectMeta.Labels = AppendVeleroExcludeLabels(objectMeta.Labels) diff --git a/pkg/utils/utils_unit_test.go b/pkg/utils/utils_unit_test.go index e1a74fef..45bb498c 100644 --- a/pkg/utils/utils_unit_test.go +++ b/pkg/utils/utils_unit_test.go @@ -370,6 +370,43 @@ func TestParseLines(t *testing.T) { logger.SetFormatter(formatter) logger.SetLevel(logrus.DebugLevel) + // Tests cases with a fully populated configuration + vcCredentialsWithSingleEscChar := `[Global] + insecure-flag = "true" + cluster-id = "cluster1" + cluster-distribution = "CSI-Vanilla" + + [VirtualCenter "sc-rdops-vm06-dhcp-184-231.eng.vmware.com"] + user = "Administrator@vsphere.local" + password = "6^54#,RDvwgJ\Edg$2" + datacenters = "VSAN-DC" + port = "443" + ` + + vcCredentialsWithTwoEscChar := `[Global] + insecure-flag = "true" + cluster-id = "cluster1" + cluster-distribution = "CSI-Vanilla" + + [VirtualCenter "sc-rdops-vm06-dhcp-184-231.eng.vmware.com"] + user = "Administrator@vsphere.local" + password = "6^54#,RDvwgJ\\Edg$2" + datacenters = "VSAN-DC" + port = "443" + ` + + vcCredentialsWithNewLineChar := `[Global] + insecure-flag = "true" + cluster-id = "cluster1" + cluster-distribution = "CSI-Vanilla" + + [VirtualCenter "sc-rdops-vm06-dhcp-184-231.eng.vmware.com"] + user = "Administrator@vsphere.local" + password = "6^54#,RDvwgJ\nEdg$2" + datacenters = "VSAN-DC" + port = "443" + ` + tests := []struct { name string sEnc string @@ -378,21 +415,40 @@ func TestParseLines(t *testing.T) { }{ { name: "Password with special character \\ in it", - sEnc: "[VirtualCenter \"sc-rdops-vm06-dhcp-184-231.eng.vmware.com\"]\npassword = \"GpI4G`OK'?in40Fo/0\\\\;\"", + sEnc: "[VirtualCenter \"sc-rdops-vm06-dhcp-184-231.eng.vmware.com\"]\npassword = \"GpI4G`OK'?in40Fo/0\\;\"", vc: "sc-rdops-vm06-dhcp-184-231.eng.vmware.com", password: "GpI4G`OK'?in40Fo/0\\;", }, { name: "Password with multiple = in it", - sEnc: "[VirtualCenter \"sc-rdops-vm06-dhcp-184-231.eng.vmware.com\"]\npassword = \"GpI4G`OK'?in40Fo/0\\\\;=h=\"", + sEnc: "[VirtualCenter \"sc-rdops-vm06-dhcp-184-231.eng.vmware.com\"]\npassword = \"GpI4G`OK'?in40Fo/0\\;=h=\"", vc: "sc-rdops-vm06-dhcp-184-231.eng.vmware.com", password: "GpI4G`OK'?in40Fo/0\\;=h=", }, { - name: "Password with special character \\t in it", - sEnc: "[VirtualCenter \"sc-rdops-vm06-dhcp-184-231.eng.vmware.com\"]\npassword = \"G4\\t4t\"", + name: `Password with special character \t in it`, + sEnc: `[VirtualCenter "sc-rdops-vm06-dhcp-184-231.eng.vmware.com"] + password = "G4\t4t"`, + vc: "sc-rdops-vm06-dhcp-184-231.eng.vmware.com", + password: `G4\t4t`, + }, + { + name: `Password with special character \ in it`, + sEnc: vcCredentialsWithSingleEscChar, + vc: "sc-rdops-vm06-dhcp-184-231.eng.vmware.com", + password: `6^54#,RDvwgJ\Edg$2`, + }, + { + name: `Password with two \\ in it`, + sEnc: vcCredentialsWithTwoEscChar, + vc: "sc-rdops-vm06-dhcp-184-231.eng.vmware.com", + password: `6^54#,RDvwgJ\\Edg$2`, + }, + { + name: `Password with \n in it`, + sEnc: vcCredentialsWithNewLineChar, vc: "sc-rdops-vm06-dhcp-184-231.eng.vmware.com", - password: "G4\t4t", + password: `6^54#,RDvwgJ\nEdg$2`, }, } for _, test := range tests {