Skip to content

Commit

Permalink
fix: should reject bad FilterSubPolicy (#819)
Browse files Browse the repository at this point in the history
Signed-off-by: spacewander <[email protected]>
  • Loading branch information
spacewander authored Dec 12, 2024
1 parent 429af5a commit ec3d119
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 1 deletion.
16 changes: 15 additions & 1 deletion types/apis/v1/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,21 @@ func validateFilterPolicy(policy *FilterPolicy, strict bool) error {
}
}

for _, policy := range policy.Spec.SubPolicies {
names := map[string]struct{}{}
for i, policy := range policy.Spec.SubPolicies {
if policy.SectionName == "" {
return fmt.Errorf("sectionName in SubPolicies[%d] is required", i)
}
if len(policy.Filters) == 0 {
return fmt.Errorf("filters in SubPolicies[%d] is required", i)
}

if _, ok := names[string(policy.SectionName)]; ok {
return fmt.Errorf("multiple SubPolicies should not have same sectionName %s", policy.SectionName)
}

names[string(policy.SectionName)] = struct{}{}

for name, filter := range policy.Filters {
err := validateFilter(name, filter, strict, targetGateway)
if err != nil {
Expand Down
88 changes: 88 additions & 0 deletions types/apis/v1/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package v1

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -269,6 +270,86 @@ func TestValidateFilterPolicy(t *testing.T) {
},
strictErr: "unknown http filter: property",
},
{
name: "bad subPolicy (no filters)",
policy: &FilterPolicy{
Spec: FilterPolicySpec{
TargetRef: &gwapiv1a2.PolicyTargetReferenceWithSectionName{
PolicyTargetReference: gwapiv1a2.PolicyTargetReference{
Group: "networking.istio.io",
Kind: "VirtualService",
},
},
SubPolicies: []FilterSubPolicy{
{
SectionName: sectionName,
},
},
},
},
err: "filters in SubPolicies[0] is required",
},
{
name: "bad subPolicy (no sectionName)",
policy: &FilterPolicy{
Spec: FilterPolicySpec{
TargetRef: &gwapiv1a2.PolicyTargetReferenceWithSectionName{
PolicyTargetReference: gwapiv1a2.PolicyTargetReference{
Group: "networking.istio.io",
Kind: "VirtualService",
},
},
SubPolicies: []FilterSubPolicy{
{
Filters: map[string]Plugin{
"animal": {
Config: runtime.RawExtension{
Raw: []byte(`{"pet":"cat"}`),
},
},
},
},
},
},
},
err: "sectionName in SubPolicies[0] is required",
},
{
name: "bad subPolicy (repeated sectionName)",
policy: &FilterPolicy{
Spec: FilterPolicySpec{
TargetRef: &gwapiv1a2.PolicyTargetReferenceWithSectionName{
PolicyTargetReference: gwapiv1a2.PolicyTargetReference{
Group: "networking.istio.io",
Kind: "VirtualService",
},
},
SubPolicies: []FilterSubPolicy{
{
SectionName: sectionName,
Filters: map[string]Plugin{
"animal": {
Config: runtime.RawExtension{
Raw: []byte(`{"pet":"cat"}`),
},
},
},
},
{
SectionName: sectionName,
Filters: map[string]Plugin{
"animal": {
Config: runtime.RawExtension{
Raw: []byte(`{"pet":"dog"}`),
},
},
},
},
},
},
},
err: fmt.Sprintf("multiple SubPolicies should not have same sectionName %s", sectionName),
},
{
name: "targetRef.SectionName and SubPolicies can not be used together",
policy: &FilterPolicy{
Expand All @@ -283,6 +364,13 @@ func TestValidateFilterPolicy(t *testing.T) {
SubPolicies: []FilterSubPolicy{
{
SectionName: sectionName,
Filters: map[string]Plugin{
"animal": {
Config: runtime.RawExtension{
Raw: []byte(`{"pet":"cat"}`),
},
},
},
},
},
},
Expand Down

0 comments on commit ec3d119

Please sign in to comment.