Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid parsing known empty inputs #618

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
}