Skip to content

Commit

Permalink
Merge pull request #618 from dims/avoid-parsing-known-empty-input
Browse files Browse the repository at this point in the history
Avoid parsing known empty inputs
  • Loading branch information
k8s-ci-robot authored Aug 4, 2023
2 parents 3800682 + 897942a commit 2787f5e
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 25 deletions.
62 changes: 37 additions & 25 deletions pkg/mapper/configmap/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"errors"
"fmt"
"strings"
"sync"
"time"

Expand Down Expand Up @@ -117,21 +118,23 @@ func ParseMap(m map[string]string) (userMappings []config.UserMapping, roleMappi
rawUserMappings := make([]config.UserMapping, 0)
userMappings = make([]config.UserMapping, 0)
if userData, ok := m["mapUsers"]; ok {
userJson, err := utilyaml.ToJSON([]byte(userData))
if err != nil {
errs = append(errs, err)
} else {
err = json.Unmarshal(userJson, &rawUserMappings)
if !isSkippable(userData) {
userJson, err := utilyaml.ToJSON([]byte(userData))
if err != nil {
errs = append(errs, err)
}

for _, userMapping := range rawUserMappings {
err = userMapping.Validate()
} else {
err = json.Unmarshal(userJson, &rawUserMappings)
if err != nil {
errs = append(errs, err)
} else {
userMappings = append(userMappings, userMapping)
}

for _, userMapping := range rawUserMappings {
err = userMapping.Validate()
if err != nil {
errs = append(errs, err)
} else {
userMappings = append(userMappings, userMapping)
}
}
}
}
Expand All @@ -140,31 +143,35 @@ func ParseMap(m map[string]string) (userMappings []config.UserMapping, roleMappi
rawRoleMappings := make([]config.RoleMapping, 0)
roleMappings = make([]config.RoleMapping, 0)
if roleData, ok := m["mapRoles"]; ok {
roleJson, err := utilyaml.ToJSON([]byte(roleData))
if err != nil {
errs = append(errs, err)
} else {
err = json.Unmarshal(roleJson, &rawRoleMappings)
if !isSkippable(roleData) {
roleJson, err := utilyaml.ToJSON([]byte(roleData))
if err != nil {
errs = append(errs, err)
}

for _, roleMapping := range rawRoleMappings {
err = roleMapping.Validate()
} else {
err = json.Unmarshal(roleJson, &rawRoleMappings)
if err != nil {
errs = append(errs, err)
} else {
roleMappings = append(roleMappings, roleMapping)
}

for _, roleMapping := range rawRoleMappings {
err = roleMapping.Validate()
if err != nil {
errs = append(errs, err)
} else {
roleMappings = append(roleMappings, roleMapping)
}
}
}
}
}

awsAccounts = make([]string, 0)
if accountsData, ok := m["mapAccounts"]; ok {
err := yaml.Unmarshal([]byte(accountsData), &awsAccounts)
if err != nil {
errs = append(errs, err)
if !isSkippable(accountsData) {
err := yaml.Unmarshal([]byte(accountsData), &awsAccounts)
if err != nil {
errs = append(errs, err)
}
}
}

Expand All @@ -175,6 +182,11 @@ func ParseMap(m map[string]string) (userMappings []config.UserMapping, roleMappi
return userMappings, roleMappings, awsAccounts, err
}

func isSkippable(data string) bool {
trimmed := strings.TrimSpace(data)
return trimmed == "" || trimmed == "``" || trimmed == "\"\""
}

func EncodeMap(userMappings []config.UserMapping, roleMappings []config.RoleMapping, awsAccounts []string) (m map[string]string, err error) {
m = make(map[string]string)

Expand Down
22 changes: 22 additions & 0 deletions pkg/mapper/configmap/configmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,3 +331,25 @@ func TestParseMap(t *testing.T) {
t.Fatalf("unexpected %v != %v", m1, m2)
}
}

func TestBadParseMap(t *testing.T) {
m1 := map[string]string{
"mapAccounts": ``,
"mapRoles": `""`,
"mapUsers": "``",
}

u, r, a, err := ParseMap(m1)
if err != nil {
t.Fatal(err)
}

m2, err := EncodeMap(u, r, a)
if err != nil {
t.Fatal(err)
}
emptyMap := map[string]string{}
if !reflect.DeepEqual(emptyMap, m2) {
t.Fatalf("unexpected %v != %v", emptyMap, m2)
}
}

0 comments on commit 2787f5e

Please sign in to comment.