From 3a73ca5b6527a904402d3ce4ef3e567110c6bcf7 Mon Sep 17 00:00:00 2001 From: Julius Volz Date: Sat, 2 Mar 2019 12:33:40 +0100 Subject: [PATCH] Fix receiver name checking in deep sub-routes Fixes https://github.com/prometheus/alertmanager/issues/1759 Signed-off-by: Julius Volz --- config/config.go | 10 +++++----- config/config_test.go | 28 ++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/config/config.go b/config/config.go index 8c46740c..b7dac403 100644 --- a/config/config.go +++ b/config/config.go @@ -435,17 +435,17 @@ func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error { // checkReceiver returns an error if a node in the routing tree // references a receiver not in the given map. func checkReceiver(r *Route, receivers map[string]struct{}) error { + for _, sr := range r.Routes { + if err := checkReceiver(sr, receivers); err != nil { + return err + } + } if r.Receiver == "" { return nil } if _, ok := receivers[r.Receiver]; !ok { return fmt.Errorf("undefined receiver %q used in route", r.Receiver) } - for _, sr := range r.Routes { - if err := checkReceiver(sr, receivers); err != nil { - return err - } - } return nil } diff --git a/config/config_test.go b/config/config_test.go index da43d7ee..94aac38d 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -103,6 +103,34 @@ receivers: } +func TestReceiverExistsForDeepSubRoute(t *testing.T) { + in := ` +route: + receiver: team-X + routes: + - match: + foo: bar + routes: + - match: + foo: bar + receiver: nonexistent + +receivers: +- name: 'team-X' +` + _, err := Load(in) + + expected := "undefined receiver \"nonexistent\" used in route" + + if err == nil { + t.Fatalf("no error returned, expected:\n%q", expected) + } + if err.Error() != expected { + t.Errorf("\nexpected:\n%q\ngot:\n%q", expected, err.Error()) + } + +} + func TestReceiverHasName(t *testing.T) { in := ` route: