Skip to content

Commit

Permalink
Use string package to check for quotes and trim them
Browse files Browse the repository at this point in the history
  • Loading branch information
varunsrinivasan2 committed Jan 25, 2024
1 parent ae36e54 commit b07e32a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
13 changes: 6 additions & 7 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,15 +166,14 @@ func ParseLines(lines []string, params map[string]interface{}, logger logrus.Fie
value := strings.TrimSpace(parts[1])
// Skip the quotes in the value if present
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]
// Check if value is double-quoted
if strings.Contains(value, `"`) {
// Remove double-quotes
unquotedValue = strings.Trim(value, `"`)
params[key] = unquotedValue
} else {
logger.Debugf("Failed to unquote value %v for key %v. Just store the original value string", value, key)
params[key] = string(value)
continue
params[key] = value
}
params[key] = unquotedValue
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions pkg/utils/utils_unit_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,18 @@ func TestParseLines(t *testing.T) {
port = "443"
`

vcCredentialsWithNoQuotes := `[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"
`

tests := []struct {
name string
sEnc string
Expand Down Expand Up @@ -450,6 +462,12 @@ func TestParseLines(t *testing.T) {
vc: "sc-rdops-vm06-dhcp-184-231.eng.vmware.com",
password: `6^54#,RDvwgJ\nEdg$2`,
},
{
name: `Password with no quotes and one \`,
sEnc: vcCredentialsWithNoQuotes,
vc: "sc-rdops-vm06-dhcp-184-231.eng.vmware.com",
password: `6^54#,RDvwgJ\Edg$2`,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
Expand Down

0 comments on commit b07e32a

Please sign in to comment.