From e8be1d0a5ceadc7ae1a65017ed245cd197e7ef69 Mon Sep 17 00:00:00 2001 From: DrAuYueng Date: Tue, 31 Aug 2021 23:52:57 +0800 Subject: [PATCH] Check relabel action at yaml unmarshal stage (#9224) Signed-off-by: DrAuYueng --- config/config_test.go | 4 ++++ config/testdata/empty_scrape_config_action.bad.yml | 4 ++++ pkg/relabel/relabel.go | 3 +++ 3 files changed, 11 insertions(+) create mode 100644 config/testdata/empty_scrape_config_action.bad.yml diff --git a/config/config_test.go b/config/config_test.go index d5cc092a2..4d9155a38 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -1302,6 +1302,10 @@ var expectedErrors = []struct { filename: "http_url_bad_scheme.bad.yml", errMsg: "URL scheme must be 'http' or 'https'", }, + { + filename: "empty_scrape_config_action.bad.yml", + errMsg: "relabel action cannot be empty", + }, } func TestBadConfigs(t *testing.T) { diff --git a/config/testdata/empty_scrape_config_action.bad.yml b/config/testdata/empty_scrape_config_action.bad.yml new file mode 100644 index 000000000..8e5f747a2 --- /dev/null +++ b/config/testdata/empty_scrape_config_action.bad.yml @@ -0,0 +1,4 @@ +scrape_configs: + - job_name: prometheus + relabel_configs: + - action: null diff --git a/pkg/relabel/relabel.go b/pkg/relabel/relabel.go index 0bd00fe8a..ec452f5b5 100644 --- a/pkg/relabel/relabel.go +++ b/pkg/relabel/relabel.go @@ -100,6 +100,9 @@ func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error { if c.Regex.Regexp == nil { c.Regex = MustNewRegexp("") } + if c.Action == "" { + return errors.Errorf("relabel action cannot be empty") + } if c.Modulus == 0 && c.Action == HashMod { return errors.Errorf("relabel configuration for hashmod requires non-zero modulus") }