Skip to content

Commit 9a85b01

Browse files
authored
Merge pull request kubernetes#84706 from yue9944882/feat/add-partial-non-resource-url-validation
Feature: Validates partial path for flow-schema's non-resource-url rules
2 parents 7f441dc + da612a3 commit 9a85b01

File tree

2 files changed

+122
-2
lines changed

2 files changed

+122
-2
lines changed

pkg/apis/flowcontrol/validation/validation.go

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ package validation
1818

1919
import (
2020
"fmt"
21+
"strings"
22+
2123
"k8s.io/apimachinery/pkg/api/validation"
2224
apimachineryvalidation "k8s.io/apimachinery/pkg/api/validation"
2325
"k8s.io/apimachinery/pkg/util/sets"
@@ -191,8 +193,16 @@ func ValidateFlowSchemaNonResourcePolicyRule(rule *flowcontrol.NonResourcePolicy
191193

192194
if len(rule.NonResourceURLs) == 0 {
193195
allErrs = append(allErrs, field.Required(fldPath.Child("nonResourceURLs"), "nonResourceURLs must contain at least one value"))
194-
} else if len(rule.NonResourceURLs) > 1 && hasWildcard(rule.NonResourceURLs) {
195-
allErrs = append(allErrs, field.Invalid(fldPath.Child("nonResourceURLs"), rule.NonResourceURLs, "if '*' is present, must not specify other non-resource URLs"))
196+
} else if hasWildcard(rule.NonResourceURLs) {
197+
if len(rule.NonResourceURLs) > 1 {
198+
allErrs = append(allErrs, field.Invalid(fldPath.Child("nonResourceURLs"), rule.NonResourceURLs, "if '*' is present, must not specify other non-resource URLs"))
199+
}
200+
} else {
201+
for i, nonResourceURL := range rule.NonResourceURLs {
202+
if err := ValidateNonResourceURLPath(nonResourceURL, fldPath.Child("nonResourceURLs").Index(i)); err != nil {
203+
allErrs = append(allErrs, err)
204+
}
205+
}
196206
}
197207

198208
return allErrs
@@ -332,6 +342,35 @@ func ValidatePriorityLevelConfigurationCondition(condition *flowcontrol.Priority
332342
return allErrs
333343
}
334344

345+
// ValidateNonResourceURLPath validates non-resource-url path by following rules:
346+
// 1. Slash must be the leading character of the path
347+
// 2. White-space is forbidden in the path
348+
// 3. Continuous/double slash is forbidden in the path
349+
// 4. Wildcard "*" should only do suffix glob matching. Note that wildcard also matches slashes.
350+
func ValidateNonResourceURLPath(path string, fldPath *field.Path) *field.Error {
351+
if len(path) == 0 {
352+
return field.Invalid(fldPath, path, "must not be empty")
353+
}
354+
if path == "/" { // root path
355+
return nil
356+
}
357+
358+
if !strings.HasPrefix(path, "/") {
359+
return field.Invalid(fldPath, path, "must start with slash")
360+
}
361+
if strings.Contains(path, " ") {
362+
return field.Invalid(fldPath, path, "must not contain white-space")
363+
}
364+
if strings.Contains(path, "//") {
365+
return field.Invalid(fldPath, path, "must not contain double slash")
366+
}
367+
wildcardCount := strings.Count(path, "*")
368+
if wildcardCount > 1 || (wildcardCount == 1 && path[len(path)-2:] != "/*") {
369+
return field.Invalid(fldPath, path, "wildcard can only do suffix matching")
370+
}
371+
return nil
372+
}
373+
335374
func hasWildcard(operations []string) bool {
336375
for _, o := range operations {
337376
if o == "*" {

pkg/apis/flowcontrol/validation/validation_test.go

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,3 +626,84 @@ func TestValidatePriorityLevelConfigurationStatus(t *testing.T) {
626626
})
627627
}
628628
}
629+
630+
func TestValidateNonResourceURLPath(t *testing.T) {
631+
testCases := []struct {
632+
name string
633+
path string
634+
expectingError bool
635+
}{
636+
{
637+
name: "empty string should fail",
638+
path: "",
639+
expectingError: true,
640+
},
641+
{
642+
name: "no slash should fail",
643+
path: "foo",
644+
expectingError: true,
645+
},
646+
{
647+
name: "single slash should work",
648+
path: "/",
649+
expectingError: false,
650+
},
651+
{
652+
name: "continuous slash should fail",
653+
path: "//",
654+
expectingError: true,
655+
},
656+
{
657+
name: "/foo slash should work",
658+
path: "/foo",
659+
expectingError: false,
660+
},
661+
{
662+
name: "multiple continuous slashes should fail",
663+
path: "/////",
664+
expectingError: true,
665+
},
666+
{
667+
name: "ending up with slash should work",
668+
path: "/apis/",
669+
expectingError: false,
670+
},
671+
{
672+
name: "ending up with wildcard should work",
673+
path: "/healthz/*",
674+
expectingError: false,
675+
},
676+
{
677+
name: "single wildcard inside the path should fail",
678+
path: "/healthz/*/foo",
679+
expectingError: true,
680+
},
681+
{
682+
name: "white-space in the path should fail",
683+
path: "/healthz/foo bar",
684+
expectingError: true,
685+
},
686+
{
687+
name: "wildcard plus plain path should fail",
688+
path: "/health*",
689+
expectingError: true,
690+
},
691+
{
692+
name: "wildcard plus plain path should fail 2",
693+
path: "/health*/foo",
694+
expectingError: true,
695+
},
696+
{
697+
name: "multiple wildcard internal and suffix should fail",
698+
path: "/*/*",
699+
expectingError: true,
700+
},
701+
}
702+
for _, testCase := range testCases {
703+
t.Run(testCase.name, func(t *testing.T) {
704+
err := ValidateNonResourceURLPath(testCase.path, field.NewPath(""))
705+
assert.Equal(t, testCase.expectingError, err != nil,
706+
"actual error: %v", err)
707+
})
708+
}
709+
}

0 commit comments

Comments
 (0)