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 #619

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
29 changes: 20 additions & 9 deletions pkg/mapper/configmap/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,25 +117,31 @@ 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 {
err := yaml.Unmarshal([]byte(userData), &userMappings)
if err != nil {
errs = append(errs, err)
if !isSkippable(userData) {
err := yaml.Unmarshal([]byte(userData), &userMappings)
if err != nil {
errs = append(errs, err)
}
}
}

roleMappings = make([]config.RoleMapping, 0)
if roleData, ok := m["mapRoles"]; ok {
err := yaml.Unmarshal([]byte(roleData), &roleMappings)
if err != nil {
errs = append(errs, err)
if !isSkippable(roleData) {
err := yaml.Unmarshal([]byte(roleData), &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 @@ -146,6 +152,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 == "\"\""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about single quotes? Your example has one with single quotes in the PR description.

}

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 @@ -285,3 +285,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)
}
}