Skip to content

Commit

Permalink
Check for nil handler in legacy pipeline (#4584)
Browse files Browse the repository at this point in the history
* fix crash when pipeline handler is not found

Signed-off-by: Justin Kolberg <amd.prophet@gmail.com>

* Add test for bugfix

Signed-off-by: Eric Chlebek <eric@sensu.io>

Co-authored-by: Eric Chlebek <eric@sensu.io>
  • Loading branch information
amdprophet and echlebek authored Jan 13, 2022
1 parent 7c0eb7d commit 0e26b95
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ Versioning](http://semver.org/spec/v2.0.0.html).
- Add `sensu_go_event_metric_points_processed` counter metric and
included it in tessen reporting.

### Fixed
- Referencing a non-existent handler in a pipeline no longer results in a crash.

## [6.6.3] - 2021-12-15

### Added
Expand Down
6 changes: 6 additions & 0 deletions backend/pipeline/handler/legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,19 @@ func (l *LegacyAdapter) Handle(ctx context.Context, ref *corev2.ResourceReferenc
fields := utillogging.EventFields(event, false)
fields["pipeline"] = corev2.ContextPipeline(ctx)
fields["pipeline_workflow"] = corev2.ContextPipelineWorkflow(ctx)
fields["handler"] = ref.Name

tctx, cancel := context.WithTimeout(ctx, l.StoreTimeout)
handler, err := l.Store.GetHandlerByName(tctx, ref.Name)
cancel()
if err != nil {
return fmt.Errorf("failed to fetch handler from store: %v", err)
}
if handler == nil {
logger.WithFields(fields).
Error("handler not found, skipping handler execution")
return nil
}

switch handler.Type {
case "pipe":
Expand Down
13 changes: 13 additions & 0 deletions backend/pipeline/handler/legacy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,19 @@ func init() {
logrus.SetOutput(ioutil.Discard)
}

func TestNilHandlerBug_GH4584(t *testing.T) {
// tests to make sure Handle() doesn't panic when the handler is not found.
mockStore := new(mockstore.MockStore)
mockStore.On("GetHandlerByName", mock.Anything, mock.Anything).Return((*corev2.Handler)(nil), nil)
adapter := &LegacyAdapter{
Store: mockStore,
}
err := adapter.Handle(context.Background(), &corev2.ResourceReference{}, corev2.FixtureEvent("foo", "bar"), nil)
if err != nil {
t.Fatal(err)
}
}

func TestHelperHandlerProcess(t *testing.T) {
if os.Getenv("GO_WANT_HELPER_HANDLER_PROCESS") != "1" {
return
Expand Down

0 comments on commit 0e26b95

Please sign in to comment.