From 983f1baddec7842ed27b4f5ec966c841f403befe Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Thu, 17 Oct 2019 15:52:05 -0700 Subject: [PATCH] ensure nil step does not cause problems --- engine/linter/linter.go | 3 +++ engine/resource/parser.go | 3 +++ engine/resource/parser_test.go | 8 ++++++++ engine/resource/testdata/nilstep.yml | 9 +++++++++ go.mod | 2 +- go.sum | 2 ++ 6 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 engine/resource/testdata/nilstep.yml diff --git a/engine/linter/linter.go b/engine/linter/linter.go index 36884da..3f24a0f 100644 --- a/engine/linter/linter.go +++ b/engine/linter/linter.go @@ -77,6 +77,9 @@ func checkPipeline(pipeline *resource.Pipeline, trusted bool) error { func checkSteps(pipeline *resource.Pipeline, trusted bool) error { steps := append(pipeline.Services, pipeline.Steps...) for _, step := range steps { + if step == nil { + return errors.New("linter: nil step") + } if err := checkStep(step, trusted); err != nil { return err } diff --git a/engine/resource/parser.go b/engine/resource/parser.go index 0dd4c85..c6d3f50 100644 --- a/engine/resource/parser.go +++ b/engine/resource/parser.go @@ -39,6 +39,9 @@ func lint(pipeline *Pipeline) error { // ensure pipeline steps are not unique. names := map[string]struct{}{} for _, step := range pipeline.Steps { + if step == nil { + return errors.New("Linter: detected nil step") + } if step.Name == "" { return errors.New("Linter: invalid or missing step name") } diff --git a/engine/resource/parser_test.go b/engine/resource/parser_test.go index 82cf43f..8bbc895 100644 --- a/engine/resource/parser_test.go +++ b/engine/resource/parser_test.go @@ -105,6 +105,14 @@ func TestParseLintErr(t *testing.T) { } } +func TestParseLintNilStep(t *testing.T) { + _, err := manifest.ParseFile("testdata/nilstep.yml") + if err == nil { + t.Errorf("Expect linter returns error") + return + } +} + func TestParseNoMatch(t *testing.T) { r := &manifest.RawResource{Kind: "pipeline", Type: "exec"} _, match, _ := parse(r) diff --git a/engine/resource/testdata/nilstep.yml b/engine/resource/testdata/nilstep.yml new file mode 100644 index 0000000..1f01a0d --- /dev/null +++ b/engine/resource/testdata/nilstep.yml @@ -0,0 +1,9 @@ +--- +kind: pipeline +type: docker +name: test + +steps: +- ~ + +... \ No newline at end of file diff --git a/go.mod b/go.mod index f5d9791..8c2db79 100644 --- a/go.mod +++ b/go.mod @@ -11,7 +11,7 @@ require ( github.com/drone/drone-runtime v1.0.7-0.20190729202838-87c84080f4a1 github.com/drone/drone-yaml v1.2.2 github.com/drone/envsubst v1.0.2 - github.com/drone/runner-go v1.2.3-0.20191017212814-48a0db6f4ed7 + github.com/drone/runner-go v1.2.3-0.20191017224419-ef1504fb90be github.com/drone/signal v1.0.0 github.com/ghodss/yaml v1.0.0 github.com/google/go-cmp v0.3.0 diff --git a/go.sum b/go.sum index d31fc8c..28d5442 100644 --- a/go.sum +++ b/go.sum @@ -53,6 +53,8 @@ github.com/drone/runner-go v1.2.3-0.20191008184914-35b0486b7f23 h1:sj9myNBjsR7C6 github.com/drone/runner-go v1.2.3-0.20191008184914-35b0486b7f23/go.mod h1:GvB5hX023g0n5UZUjMBmudk6givdDDuLcls7Nolm5v8= github.com/drone/runner-go v1.2.3-0.20191017212814-48a0db6f4ed7 h1:qGsY45PRwKdbaKZqa2jswjS5nbLEp1sDXFbVaIcbi4I= github.com/drone/runner-go v1.2.3-0.20191017212814-48a0db6f4ed7/go.mod h1:GvB5hX023g0n5UZUjMBmudk6givdDDuLcls7Nolm5v8= +github.com/drone/runner-go v1.2.3-0.20191017224419-ef1504fb90be h1:tk4tW92ajKvIxlepMUX0iOAJ0CFdO5x3LsKP0H30OMs= +github.com/drone/runner-go v1.2.3-0.20191017224419-ef1504fb90be/go.mod h1:GvB5hX023g0n5UZUjMBmudk6givdDDuLcls7Nolm5v8= github.com/drone/signal v1.0.0 h1:NrnM2M/4yAuU/tXs6RP1a1ZfxnaHwYkd0kJurA1p6uI= github.com/drone/signal v1.0.0/go.mod h1:S8t92eFT0g4WUgEc/LxG+LCuiskpMNsG0ajAMGnyZpc= github.com/ghodss/yaml v1.0.0 h1:wQHKEahhL6wmXdzwWG11gIVCkOv05bNOh+Rxn0yngAk=