Skip to content

Commit

Permalink
Merge pull request #11293 from bisakhmondal/service_filter
Browse files Browse the repository at this point in the history
expression validation of service-resolver subset filter
  • Loading branch information
jkirschner-hashicorp authored Oct 20, 2021
2 parents c630ab6 + c364387 commit 14af8cb
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .changelog/11293.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:improvement
connect: service-resolver subset filters are validated for valid go-bexpr syntax on write
```
8 changes: 7 additions & 1 deletion agent/structs/config_entry_discoverychain.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"strings"
"time"

"github.com/hashicorp/go-bexpr"
"github.com/mitchellh/copystructure"
"github.com/mitchellh/hashstructure"

Expand Down Expand Up @@ -901,13 +902,18 @@ func (e *ServiceResolverConfigEntry) Validate() error {
}

if len(e.Subsets) > 0 {
for name := range e.Subsets {
for name, subset := range e.Subsets {
if name == "" {
return fmt.Errorf("Subset defined with empty name")
}
if err := validateServiceSubset(name); err != nil {
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)
}
}
}
}

Expand Down
11 changes: 11 additions & 0 deletions agent/structs/config_entry_discoverychain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,17 @@ func TestServiceResolverConfigEntry(t *testing.T) {
},
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",
entry: &ServiceResolverConfigEntry{
Expand Down

0 comments on commit 14af8cb

Please sign in to comment.