Skip to content

Commit

Permalink
Fail when selector is input with duplicate keys
Browse files Browse the repository at this point in the history
  • Loading branch information
13ajay committed Nov 6, 2024
1 parent 7b73247 commit 7d8a090
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
8 changes: 7 additions & 1 deletion cmd/credentials.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ func getStringMap(s string) (map[string]string, error) {
m := make(map[string]string)
for {
match := regex.FindStringSubmatch(s)
if len(match) == 0 {
if match == nil || len(match) == 0 {
break
} else {
if len(match) < 3 {
Expand All @@ -114,6 +114,9 @@ func getStringMap(s string) (map[string]string, error) {
}
value := match[2]

if _, ok := m[key]; ok {
return nil, errors.New("cert selector contained duplicate key")
}
m[key] = value

// Remove the matching prefix from the input cert selector string
Expand Down Expand Up @@ -153,6 +156,9 @@ func getMapFromJsonEntries(jsonStr string) (map[string]string, error) {
if !isValidKey {
return nil, errors.New("cert selector contained invalid key")
}
if _, ok := m[mapEntry.Key]; ok {
return nil, errors.New("cert selector contained duplicate key")
}
m[mapEntry.Key] = mapEntry.Value
}
return m, nil
Expand Down
3 changes: 3 additions & 0 deletions cmd/credentials_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ func TestValidSelectorParsing(t *testing.T) {
"Key=x509Subject,Value=CN=CN With Spaces,O=O With Spaces Key=x509Issuer,Value=CN=Issuer",
" \n Key=x509Subject,Value=CN=CN With Spaces,O=O With Spaces \t Key=x509Issuer,Value=CN=Issuer \n",
"Key=x509Issuer,Value=CN=Issuer",
"Key=x509Issuer,Value=CN=Issuer,With,Commas,And Some Spaces",
" \n Key=x509Subject,Value=CN=CN With Trailing Spaces ,O=O With Spaces \t Key=x509Issuer,Value=CN=Issuer \n",
}
for _, fixture := range fixtures {
_, err := PopulateCertIdentifier(fixture, "MY")
Expand All @@ -38,6 +40,7 @@ func TestInvalidSelectorParsing(t *testing.T) {
"Key=laksdjf,Valalsd",
"Key=aljsdf,Value=aljsdfadsf",
"asdf Key=x509Subject,Value=Test",
"Key=x509Subject,Value=Test Key=x509Subject,Value=Test2",
}
for _, fixture := range fixtures {
_, err := PopulateCertIdentifier(fixture, "MY")
Expand Down

0 comments on commit 7d8a090

Please sign in to comment.