Skip to content

Commit 06e4c65

Browse files
authored
[corechecks/helm] Add missing nil checks in getValue() (#13815) (#13821)
1 parent 6a7c649 commit 06e4c65

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

pkg/collector/corechecks/cluster/helm/map_utils.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,15 @@ import (
3131
// "agents.image.tag" and "agents.image.pullPolicy" are valid keys, but
3232
// "agents.image" is not.
3333
func getValue(m map[string]interface{}, dotSeparatedKey string) (string, error) {
34+
if dotSeparatedKey == "" {
35+
return "", fmt.Errorf("not found")
36+
}
37+
3438
keys := strings.Split(dotSeparatedKey, ".")
3539
var obj interface{} = m
3640

3741
for _, key := range keys {
38-
if reflect.TypeOf(obj).Kind() != reflect.Map {
42+
if obj == nil || reflect.TypeOf(obj).Kind() != reflect.Map {
3943
return "", fmt.Errorf("not found")
4044
}
4145

@@ -50,7 +54,7 @@ func getValue(m map[string]interface{}, dotSeparatedKey string) (string, error)
5054
obj = objValue.Interface()
5155
}
5256

53-
if reflect.TypeOf(obj).Kind() == reflect.Map {
57+
if obj == nil || reflect.TypeOf(obj).Kind() == reflect.Map {
5458
return "", fmt.Errorf("not found")
5559
}
5660

pkg/collector/corechecks/cluster/helm/map_utils_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,40 @@ func TestGetValue(t *testing.T) {
9898
dotSeparatedKey: "agents.image.abc",
9999
expectsError: true,
100100
},
101+
{
102+
name: "nil value for final part of the key",
103+
inputMap: map[string]interface{}{
104+
"agents": map[string]interface{}{
105+
"image": map[string]interface{}{
106+
"tag": nil,
107+
},
108+
},
109+
},
110+
dotSeparatedKey: "agents.image.tag",
111+
expectsError: true,
112+
},
113+
{
114+
name: "nil value for middle part of the key",
115+
inputMap: map[string]interface{}{
116+
"agents": map[string]interface{}{
117+
"image": nil,
118+
},
119+
},
120+
dotSeparatedKey: "agents.image.tag",
121+
expectsError: true,
122+
},
123+
{
124+
name: "empty key",
125+
inputMap: map[string]interface{}{
126+
"agents": map[string]interface{}{
127+
"image": map[string]string{
128+
"tag": "7.39.0",
129+
},
130+
},
131+
},
132+
dotSeparatedKey: "",
133+
expectsError: true,
134+
},
101135
}
102136

103137
for _, test := range tests {

0 commit comments

Comments
 (0)