Skip to content

Commit

Permalink
Manually unquote configuration values and add unit tests with edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
varunsrinivasan2 committed Jan 22, 2024
1 parent bc8df9f commit 446e63d
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 9 deletions.
11 changes: 7 additions & 4 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand Down
66 changes: 61 additions & 5 deletions pkg/utils/utils_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down

0 comments on commit 446e63d

Please sign in to comment.