Fix a panic when doing variable replacement.

This fixes an issue where Kustomize expects a []string, but gets a slice
of something else instead, which causing a runtime panic.

This fixes 2241
This commit is contained in:
Ian Howell
2020-04-27 11:00:41 -05:00
parent 6aabe72fce
commit 1d46edccb5
2 changed files with 44 additions and 11 deletions

View File

@@ -40,7 +40,11 @@ func (rv *refVarTransformer) replaceVars(in interface{}) (interface{}, error) {
case []interface{}: case []interface{}:
var xs []interface{} var xs []interface{}
for _, a := range in.([]interface{}) { for _, a := range in.([]interface{}) {
xs = append(xs, expansion2.Expand(a.(string), rv.mappingFunc)) x, ok := a.(string)
if !ok {
return nil, fmt.Errorf("expected array of strings, found %v", in)
}
xs = append(xs, expansion2.Expand(x, rv.mappingFunc))
} }
return xs, nil return xs, nil
case map[string]interface{}: case map[string]interface{}:
@@ -49,7 +53,7 @@ func (rv *refVarTransformer) replaceVars(in interface{}) (interface{}, error) {
for k, v := range inMap { for k, v := range inMap {
s, ok := v.(string) s, ok := v.(string)
if !ok { if !ok {
// This field not contain a $(VAR) since it is not // This field can not contain a $(VAR) since it is not
// of string type. For instance .spec.replicas: 3 in // of string type. For instance .spec.replicas: 3 in
// a Deployment object // a Deployment object
xs[k] = v xs[k] = v
@@ -64,7 +68,7 @@ func (rv *refVarTransformer) replaceVars(in interface{}) (interface{}, error) {
case interface{}: case interface{}:
s, ok := in.(string) s, ok := in.(string)
if !ok { if !ok {
// This field not contain a $(VAR) since it is not of string type. // This field can not contain a $(VAR) since it is not of string type.
return in, nil return in, nil
} }
// This field can potentially contain a $(VAR) since it is // This field can potentially contain a $(VAR) since it is

View File

@@ -29,6 +29,7 @@ func TestRefVarTransformer(t *testing.T) {
description string description string
given given given given
expected expected expected expected
errMessage string
}{ }{
{ {
description: "var replacement in map[string]", description: "var replacement in map[string]",
@@ -111,6 +112,27 @@ func TestRefVarTransformer(t *testing.T) {
unused: []string{"BAR"}, unused: []string{"BAR"},
}, },
}, },
{
description: "var replacement panic in map[string]",
given: given{
varMap: map[string]interface{}{},
fs: []types.FieldSpec{
{Gvk: resid.Gvk{Version: "v1", Kind: "ConfigMap"}, Path: "data/slice"},
},
res: resmaptest_test.NewRmBuilder(
t, resource.NewFactory(kunstruct.NewKunstructuredFactoryImpl())).
Add(map[string]interface{}{
"apiVersion": "v1",
"kind": "ConfigMap",
"metadata": map[string]interface{}{
"name": "cm1",
},
"data": map[string]interface{}{
"slice": []interface{}{5}, // noticeably *not* a []string
}}).ResMap(),
},
errMessage: "expected array of strings, found [5]",
},
} }
for _, tc := range testCases { for _, tc := range testCases {
@@ -122,6 +144,13 @@ func TestRefVarTransformer(t *testing.T) {
err := tr.Transform(tc.given.res) err := tr.Transform(tc.given.res)
// assert // assert
if tc.errMessage != "" {
if err == nil {
t.Fatalf("missing expected error %v", tc.errMessage)
} else if err.Error() != tc.errMessage {
t.Fatalf("actual error doesn't match expected error: \nACTUAL: %v\nEXPECTED: %v", err.Error(), tc.errMessage)
}
} else {
if err != nil { if err != nil {
t.Errorf("unexpected error: %v", err) t.Errorf("unexpected error: %v", err)
} }
@@ -131,7 +160,7 @@ func TestRefVarTransformer(t *testing.T) {
err = e.ErrorIfNotEqualLists(a) err = e.ErrorIfNotEqualLists(a)
t.Fatalf("actual doesn't match expected: \nACTUAL:\n%v\nEXPECTED:\n%v\nERR: %v", a, e, err) t.Fatalf("actual doesn't match expected: \nACTUAL:\n%v\nEXPECTED:\n%v\nERR: %v", a, e, err)
} }
}
}) })
} }
} }