Skip to content
This repository has been archived by the owner on Oct 17, 2018. It is now read-only.

Commit

Permalink
Add validation for filter tag names
Browse files Browse the repository at this point in the history
  • Loading branch information
m-sandusky committed Oct 19, 2017
1 parent c18e039 commit 13dc377
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
8 changes: 7 additions & 1 deletion rules/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,13 @@ func (v *validator) validateRollupRules(rrv map[string]*RollupRuleView) error {
}

func (v *validator) validateFilters(f map[string]string) error {
for _, filter := range f {
invalidChars := v.opts.TagNameInvalidChars()
for tag, filter := range f {
// Validating the filter tag name does not contain invalid chars
if err := validateChars(tag, invalidChars); err != nil {
return fmt.Errorf("filter tag name %s contains invalid character: %v", tag, err)
}

// Validating the filter expression by actually constructing the filter.
if _, err := filters.NewFilter([]byte(filter)); err != nil {
return err
Expand Down
34 changes: 34 additions & 0 deletions rules/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,23 @@ func TestValidatorValidateMappingRuleInvalidFilter(t *testing.T) {
require.Error(t, rs.Validate(validator))
}

func TestValidatorValidateMappingRuleInvalidFilterTagName(t *testing.T) {
invalidChars := []rune{'$'}
invalidFilterSnapshot := &mappingRuleSnapshot{
rawFilters: map[string]string{
"random$Tag": "!=",
},
}
invalidFilterRule := &mappingRule{
snapshots: []*mappingRuleSnapshot{invalidFilterSnapshot},
}
rs := &ruleSet{
mappingRules: []*mappingRule{invalidFilterRule},
}
validator := NewValidator(testValidatorOptions().SetTagNameInvalidChars(invalidChars))
require.Error(t, rs.Validate(validator))
}

func TestValidatorValidateMappingRuleInvalidMetricType(t *testing.T) {
ruleSet := testRuleSetWithMappingRules(t, testInvalidMetricTypeMappingRulesConfig())
validator := NewValidator(testValidatorOptions())
Expand Down Expand Up @@ -178,6 +195,23 @@ func TestValidatorValidateRollupRuleInvalidFilter(t *testing.T) {
require.Error(t, rs.Validate(validator))
}

func TestValidatorValidateRollupRuleInvalidFilterTagName(t *testing.T) {
invalidChars := []rune{'$'}
invalidFilterSnapshot := &mappingRuleSnapshot{
rawFilters: map[string]string{
"random$Tag": "!=",
},
}
invalidFilterRule := &mappingRule{
snapshots: []*mappingRuleSnapshot{invalidFilterSnapshot},
}
rs := &ruleSet{
mappingRules: []*mappingRule{invalidFilterRule},
}
validator := NewValidator(testValidatorOptions().SetTagNameInvalidChars(invalidChars))
require.Error(t, rs.Validate(validator))
}

func TestValidatorValidateRollupRuleInvalidMetricType(t *testing.T) {
ruleSet := testRuleSetWithRollupRules(t, testInvalidMetricTypeRollupRulesConfig())
validator := NewValidator(testValidatorOptions())
Expand Down

0 comments on commit 13dc377

Please sign in to comment.