Skip to content

Commit

Permalink
Fix #2035: invalid YAML should not make the operator panic
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaferraro committed Mar 19, 2021
1 parent 6099666 commit 5ff1d5a
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 19 deletions.
41 changes: 22 additions & 19 deletions pkg/util/source/inspector_yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
14 changes: 14 additions & 0 deletions pkg/util/source/inspector_yaml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,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
Expand Down Expand Up @@ -89,6 +96,13 @@ func TestYAMLDependencies(t *testing.T) {
`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) {
Expand Down

0 comments on commit 5ff1d5a

Please sign in to comment.