Prevent invalid label names with labelmap (#3868)

This change ensures that the relabeling configurations using labelmap
can't generate invalid label names.
This commit is contained in:
pasquier-s 2018-02-21 11:02:22 +01:00 committed by Brian Brazil
parent 1fd20fc954
commit fc8cf08f42
3 changed files with 11 additions and 0 deletions

View file

@ -569,6 +569,9 @@ func (c *RelabelConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
if c.Action == RelabelReplace && !relabelTarget.MatchString(c.TargetLabel) {
return fmt.Errorf("%q is invalid 'target_label' for %s action", c.TargetLabel, c.Action)
}
if c.Action == RelabelLabelMap && !relabelTarget.MatchString(c.Replacement) {
return fmt.Errorf("%q is invalid 'replacement' for %s action", c.Replacement, c.Action)
}
if c.Action == RelabelHashMod && !model.LabelName(c.TargetLabel).IsValid() {
return fmt.Errorf("%q is invalid 'target_label' for %s action", c.TargetLabel, c.Action)
}

View file

@ -635,6 +635,9 @@ var expectedErrors = []struct {
}, {
filename: "labeldrop5.bad.yml",
errMsg: "labeldrop action requires only 'regex', and no other fields",
}, {
filename: "labelmap.bad.yml",
errMsg: "\"l-$1\" is invalid 'replacement' for labelmap action",
}, {
filename: "rules.bad.yml",
errMsg: "invalid rule file path",

5
config/testdata/labelmap.bad.yml vendored Normal file
View file

@ -0,0 +1,5 @@
scrape_configs:
- job_name: prometheus
relabel_configs:
- action: labelmap
replacement: l-$1