From cfe3abcdaa7f287aeb6e9d2fe1a192d2b6b22529 Mon Sep 17 00:00:00 2001 From: Davanum Srinivas Date: Fri, 4 Aug 2023 11:44:49 -0400 Subject: [PATCH 1/2] Avoid parsing known empty inputs Signed-off-by: Davanum Srinivas --- pkg/mapper/configmap/configmap.go | 58 ++++++++++++++++++++++---- pkg/mapper/configmap/configmap_test.go | 22 ++++++++++ 2 files changed, 71 insertions(+), 9 deletions(-) diff --git a/pkg/mapper/configmap/configmap.go b/pkg/mapper/configmap/configmap.go index fa40be7fa..7097f23e8 100644 --- a/pkg/mapper/configmap/configmap.go +++ b/pkg/mapper/configmap/configmap.go @@ -3,6 +3,7 @@ package configmap import ( "context" "errors" + "strings" "sync" "fmt" @@ -117,25 +118,59 @@ 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) { + userJson, err := utilyaml.ToJSON([]byte(userData)) + if err != nil { + errs = append(errs, err) + } else { + err = json.Unmarshal(userJson, &rawUserMappings) + if err != nil { + errs = append(errs, err) + } + + for _, userMapping := range rawUserMappings { + err = userMapping.Validate() + if err != nil { + errs = append(errs, err) + } else { + userMappings = append(userMappings, userMapping) + } + } + } } } 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) { + roleJson, err := utilyaml.ToJSON([]byte(roleData)) + if err != nil { + errs = append(errs, err) + } else { + err = json.Unmarshal(roleJson, &rawRoleMappings) + if err != nil { + errs = append(errs, err) + } + + 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) + } } } @@ -146,6 +181,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) diff --git a/pkg/mapper/configmap/configmap_test.go b/pkg/mapper/configmap/configmap_test.go index 6ae3dac10..d6692d066 100644 --- a/pkg/mapper/configmap/configmap_test.go +++ b/pkg/mapper/configmap/configmap_test.go @@ -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) + } +} From c1975bfe2ce2797cd8117c8dc946d5891d1288ca Mon Sep 17 00:00:00 2001 From: Daniel Kennedy <19178580+DanielCKennedy@users.noreply.github.com> Date: Mon, 7 Aug 2023 11:55:42 -0700 Subject: [PATCH 2/2] Fix incorrect merge conflict resolution causing wrong parser to be used --- pkg/mapper/configmap/configmap.go | 33 ++----------------------------- 1 file changed, 2 insertions(+), 31 deletions(-) diff --git a/pkg/mapper/configmap/configmap.go b/pkg/mapper/configmap/configmap.go index 7097f23e8..70aad0cb9 100644 --- a/pkg/mapper/configmap/configmap.go +++ b/pkg/mapper/configmap/configmap.go @@ -3,7 +3,6 @@ package configmap import ( "context" "errors" - "strings" "sync" "fmt" @@ -119,23 +118,9 @@ func ParseMap(m map[string]string) (userMappings []config.UserMapping, roleMappi userMappings = make([]config.UserMapping, 0) if userData, ok := m["mapUsers"]; ok { if !isSkippable(userData) { - userJson, err := utilyaml.ToJSON([]byte(userData)) + err := yaml.Unmarshal([]byte(userData), &userMappings) if err != nil { errs = append(errs, err) - } else { - err = json.Unmarshal(userJson, &rawUserMappings) - if err != nil { - errs = append(errs, err) - } - - for _, userMapping := range rawUserMappings { - err = userMapping.Validate() - if err != nil { - errs = append(errs, err) - } else { - userMappings = append(userMappings, userMapping) - } - } } } } @@ -143,23 +128,9 @@ func ParseMap(m map[string]string) (userMappings []config.UserMapping, roleMappi roleMappings = make([]config.RoleMapping, 0) if roleData, ok := m["mapRoles"]; ok { if !isSkippable(roleData) { - roleJson, err := utilyaml.ToJSON([]byte(roleData)) + err := yaml.Unmarshal([]byte(roleData), &roleMappings) if err != nil { errs = append(errs, err) - } else { - err = json.Unmarshal(roleJson, &rawRoleMappings) - if err != nil { - errs = append(errs, err) - } - - for _, roleMapping := range rawRoleMappings { - err = roleMapping.Validate() - if err != nil { - errs = append(errs, err) - } else { - roleMappings = append(roleMappings, roleMapping) - } - } } } }