add service resolver subset filter validation

This commit is contained in:
Bisakh Mondal 2021-10-13 02:49:11 +05:30
parent 8804577de1
commit 929ad1e80f
No known key found for this signature in database
GPG Key ID: 0F8E19159B05E7C0
2 changed files with 18 additions and 1 deletions

View File

@ -11,6 +11,7 @@ import (
"strings" "strings"
"time" "time"
"github.com/hashicorp/go-bexpr"
"github.com/mitchellh/copystructure" "github.com/mitchellh/copystructure"
"github.com/mitchellh/hashstructure" "github.com/mitchellh/hashstructure"
@ -901,13 +902,18 @@ func (e *ServiceResolverConfigEntry) Validate() error {
} }
if len(e.Subsets) > 0 { if len(e.Subsets) > 0 {
for name := range e.Subsets { for name, subset := range e.Subsets {
if name == "" { if name == "" {
return fmt.Errorf("Subset defined with empty name") return fmt.Errorf("Subset defined with empty name")
} }
if err := validateServiceSubset(name); err != nil { if err := validateServiceSubset(name); err != nil {
return fmt.Errorf("Subset %q is invalid: %v", name, err) return fmt.Errorf("Subset %q is invalid: %v", name, err)
} }
if subset.Filter != "" {
if _, err := bexpr.CreateEvaluator(subset.Filter, nil); err != nil {
return fmt.Errorf("Filter for subset %q is not a valid expression: %v", name, err)
}
}
} }
} }

View File

@ -552,6 +552,17 @@ func TestServiceResolverConfigEntry(t *testing.T) {
}, },
validateErr: "Subset defined with empty name", validateErr: "Subset defined with empty name",
}, },
{
name: "invalid boolean expression subset filter",
entry: &ServiceResolverConfigEntry{
Kind: ServiceResolver,
Name: "test",
Subsets: map[string]ServiceResolverSubset{
"v1": {Filter: "random string"},
},
},
validateErr: `Filter for subset "v1" is not a valid expression`,
},
{ {
name: "default subset does not exist", name: "default subset does not exist",
entry: &ServiceResolverConfigEntry{ entry: &ServiceResolverConfigEntry{