Repository: apache/camel-k
Pull request #2150 (2021-03-19 16:28:22)

Backport fix for #2035: invalid YAML should not make the operator panic
<!-- Description -->

Fix  #2035


<!--
Enter your extended release note in the below block. If the PR requires
additional action from users switching to the new release, include the string
"action required". If no release note is required, write "NONE". 

You can (optionally) mark this PR with labels "kind/bug" or "kind/feature" to make sure
the text is added to the right section of the release notes. 
-->

**Release Note**
```release-note
NONE
```



Patch:
diff --git a/pkg/util/source/inspector_yaml.go b/pkg/util/source/inspector_yaml.go
index bcfa6cf4a..76b9e9064 100644
--- a/pkg/util/source/inspector_yaml.go
+++ b/pkg/util/source/inspector_yaml.go
@@ -91,34 +91,37 @@ func (i YAMLInspector) parseStep(key string, content interface{}, meta *Metadata
 		} else if u, ok := t["from"]; ok {
 			return i.parseStep("from", u, meta)
 		} else if u, ok := t["steps"]; ok {
-			steps := u.([]interface{})
+			if steps, stepsFormatOk := u.([]interface{}); stepsFormatOk {
+				for _, raw := range steps {
+					if step, stepFormatOk := raw.(map[interface{}]interface{}); stepFormatOk {
 
-			for _, raw := range steps {
-				step := raw.(map[interface{}]interface{})
-
-				if len(step) != 1 {
-					return fmt.Errorf("unable to parse step: %v", step)
-				}
-
-				for k, v := range step {
-					switch kt := k.(type) {
-					case fmt.Stringer:
-						if err := i.parseStep(kt.String(), v, meta); err != nil {
-							return err
+						if len(step) != 1 {
+							return fmt.Errorf("unable to parse step: %v", step)
 						}
-					case string:
-						if err := i.parseStep(kt, v, meta); err != nil {
-							return err
+
+						for k, v := range step {
+							switch kt := k.(type) {
+							case fmt.Stringer:
+								if err := i.parseStep(kt.String(), v, meta); err != nil {
+									return err
+								}
+							case string:
+								if err := i.parseStep(kt, v, meta); err != nil {
+									return err
+								}
+							default:
+								return fmt.Errorf("unknown key type: %v, step: %v", k, step)
+							}
 						}
-					default:
-						return fmt.Errorf("unknown key type: %v, step: %v", k, step)
 					}
 				}
 			}
 		}
 
 		if u, ok := t["uri"]; ok {
-			maybeURI = u.(string)
+			if v, isString := u.(string); isString {
+				maybeURI = v
+			}
 		}
 
 		if _, ok := t["language"]; ok {


Test patch:
diff --git a/pkg/util/source/inspector_yaml_test.go b/pkg/util/source/inspector_yaml_test.go
index 18960d0ef..a033201e9 100644
--- a/pkg/util/source/inspector_yaml_test.go
+++ b/pkg/util/source/inspector_yaml_test.go
@@ -61,6 +61,13 @@ const YAMLRouteTransformer = `
           uri: knative:endpoint/service
 `
 
+const YAMLInvalid = `
+- from:
+    uri: knative:endpoint/default
+    steps:
+      - "log:out"
+`
+
 func TestYAMLDependencies(t *testing.T) {
 	tests := []struct {
 		name                string
@@ -85,6 +92,13 @@ func TestYAMLDependencies(t *testing.T) {
 			source:       YAMLRouteTransformer,
 			dependencies: []string{`mvn:org.apache.camel.k/camel-k-knative-producer`, `mvn:org.apache.camel.k/camel-k-knative-consumer`},
 		},
+		{
+			name:   "invalid",
+			source: YAMLInvalid,
+			dependencies: []string{
+				`mvn:org.apache.camel.k/camel-k-knative-consumer`,
+			},
+		},
 	}
 	for _, test := range tests {
 		t.Run(test.name, func(t *testing.T) {