diff --git a/pkg/utils/yaml_utils/yaml_utils.go b/pkg/utils/yaml_utils/yaml_utils.go index d93c7f57b..5957d8d7c 100644 --- a/pkg/utils/yaml_utils/yaml_utils.go +++ b/pkg/utils/yaml_utils/yaml_utils.go @@ -29,7 +29,9 @@ func UpdateYamlValue(yamlBytes []byte, path []string, value string) ([]byte, err return yamlBytes, errors.New("yaml document is not a dictionary") } - updateYamlNode(body, path, value) + if err := updateYamlNode(body, path, value); err != nil { + return yamlBytes, err + } // Convert the updated YAML node back to YAML bytes. updatedYAMLBytes, err := yaml.Marshal(body) @@ -41,17 +43,19 @@ func UpdateYamlValue(yamlBytes []byte, path []string, value string) ([]byte, err } // Recursive function to update the YAML node. -func updateYamlNode(node *yaml.Node, path []string, value string) { +func updateYamlNode(node *yaml.Node, path []string, value string) error { if len(path) == 0 { + if node.Kind != yaml.ScalarNode { + return errors.New("yaml node is not a scalar") + } node.Value = value - return + return nil } key := path[0] for i := 0; i < len(node.Content)-1; i += 2 { if node.Content[i].Value == key { - updateYamlNode(node.Content[i+1], path[1:], value) - return + return updateYamlNode(node.Content[i+1], path[1:], value) } } @@ -63,4 +67,5 @@ func updateYamlNode(node *yaml.Node, path []string, value string) { Kind: yaml.ScalarNode, Value: value, }) + return nil } diff --git a/pkg/utils/yaml_utils/yaml_utils_test.go b/pkg/utils/yaml_utils/yaml_utils_test.go index 2e1b2739c..4e7aaded3 100644 --- a/pkg/utils/yaml_utils/yaml_utils_test.go +++ b/pkg/utils/yaml_utils/yaml_utils_test.go @@ -66,6 +66,14 @@ func TestUpdateYamlValue(t *testing.T) { expectedOut: "42\n", expectedErr: "yaml document is not a dictionary", }, + { + name: "trying to update a note that is not a scalar", + in: "foo: [1, 2, 3]\n", + path: []string{"foo"}, + value: "bar", + expectedOut: "foo: [1, 2, 3]\n", + expectedErr: "yaml node is not a scalar", + }, } for _, test := range tests {