Skip to content

Commit

Permalink
Merge pull request #623 from DanielCKennedy/avoid_parsing_empty_6
Browse files Browse the repository at this point in the history
Avoid parsing empty 6
  • Loading branch information
k8s-ci-robot committed Aug 8, 2023
2 parents 08f0ebc + e6fb30a commit fab88ce
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 13 deletions.
37 changes: 24 additions & 13 deletions pkg/mapper/configmap/configmap.go
Expand Up @@ -117,35 +117,41 @@ func ParseMap(m map[string]string) (userMappings []config.UserMapping, roleMappi
errs := make([]error, 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, &userMappings)
if !isSkippable(userData) {
userJson, err := utilyaml.ToJSON([]byte(userData))
if err != nil {
errs = append(errs, err)
} else {
err = json.Unmarshal(userJson, &userMappings)
if err != nil {
errs = append(errs, err)
}
}
}
}

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, &roleMappings)
if !isSkippable(roleData) {
roleJson, err := utilyaml.ToJSON([]byte(roleData))
if err != nil {
errs = append(errs, err)
} else {
err = json.Unmarshal(roleJson, &roleMappings)
if err != nil {
errs = append(errs, err)
}
}
}
}

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 @@ -156,6 +162,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 == "\"\"" || trimmed == "''"
}

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

Expand Down
44 changes: 44 additions & 0 deletions pkg/mapper/configmap/configmap_test.go
Expand Up @@ -285,3 +285,47 @@ 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)
}
}

func TestBadParseMapSingleQuote(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 fab88ce

Please sign in to comment.