Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eskip: improve invalid predicate arguments error message #3380

Merged
merged 1 commit into from
Jan 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/webhook/admission/admission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func TestIngressAdmitter(t *testing.T) {
{
name: "invalid eskip routes",
inputFile: "invalid-routes.json",
message: `invalid \"zalando.org/skipper-routes\" annotation: invalid predicate count arg`,
message: `invalid \"zalando.org/skipper-routes\" annotation: invalid route \"r1\": Header predicate expects 2 string arguments`,
},
{
name: "invalid eskip filters and predicates",
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
\[ingress\] invalid \\\"zalando\.org\/skipper-routes\\\" annotation: invalid predicate count arg
\[ingress\] invalid \\\"zalando\.org\/skipper-routes\\\" annotation: invalid route \\\"r1\\\": Header predicate expects 2 string arguments
42 changes: 24 additions & 18 deletions eskip/eskip.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ import (
const duplicateHeaderPredicateErrorFmt = "duplicate header predicate: %s"

var (
invalidPredicateArgError = errors.New("invalid predicate arg")
invalidPredicateArgCountError = errors.New("invalid predicate count arg")
duplicatePathTreePredicateError = errors.New("duplicate path tree predicate")
duplicateMethodPredicateError = errors.New("duplicate method predicate")
)
Expand Down Expand Up @@ -446,17 +444,25 @@ func (t BackendType) String() string {
}

// Expects exactly n arguments of type string, or fails.
func getStringArgs(n int, args []interface{}) ([]string, error) {
if len(args) != n {
return nil, invalidPredicateArgCountError
func getStringArgs(p *Predicate, n int) ([]string, error) {
MustafaSaber marked this conversation as resolved.
Show resolved Hide resolved
failure := func() ([]string, error) {
if n == 1 {
return nil, fmt.Errorf("%s predicate expects 1 string argument", p.Name)
} else {
return nil, fmt.Errorf("%s predicate expects %d string arguments", p.Name, n)
}
}

if len(p.Args) != n {
return failure()
}

sargs := make([]string, n)
for i, a := range args {
for i, a := range p.Args {
if sa, ok := a.(string); ok {
sargs[i] = sa
} else {
return nil, invalidPredicateArgError
return failure()
}
}

Expand All @@ -475,47 +481,43 @@ func applyPredicates(route *Route, proute *parsedRoute) error {
)

for _, p := range proute.predicates {
if err != nil {
return err
}

switch p.Name {
case "Path":
if pathSet {
return duplicatePathTreePredicateError
}

if args, err = getStringArgs(1, p.Args); err == nil {
if args, err = getStringArgs(p, 1); err == nil {
route.Path = args[0]
pathSet = true
}
case "Host":
if args, err = getStringArgs(1, p.Args); err == nil {
if args, err = getStringArgs(p, 1); err == nil {
route.HostRegexps = append(route.HostRegexps, args[0])
}
case "PathRegexp":
if args, err = getStringArgs(1, p.Args); err == nil {
if args, err = getStringArgs(p, 1); err == nil {
route.PathRegexps = append(route.PathRegexps, args[0])
}
case "Method":
if methodSet {
return duplicateMethodPredicateError
}

if args, err = getStringArgs(1, p.Args); err == nil {
if args, err = getStringArgs(p, 1); err == nil {
route.Method = args[0]
methodSet = true
}
case "HeaderRegexp":
if args, err = getStringArgs(2, p.Args); err == nil {
if args, err = getStringArgs(p, 2); err == nil {
if route.HeaderRegexps == nil {
route.HeaderRegexps = make(map[string][]string)
}

route.HeaderRegexps[args[0]] = append(route.HeaderRegexps[args[0]], args[1])
}
case "Header":
if args, err = getStringArgs(2, p.Args); err == nil {
if args, err = getStringArgs(p, 2); err == nil {
if route.Headers == nil {
route.Headers = make(map[string]string)
}
Expand All @@ -531,9 +533,13 @@ func applyPredicates(route *Route, proute *parsedRoute) error {
default:
route.Predicates = append(route.Predicates, p)
}

if err != nil {
return fmt.Errorf("invalid route %q: %w", proute.id, err)
}
}

return err
return nil
Comment on lines +537 to +542
Copy link
Member Author

@AlexanderYastrebov AlexanderYastrebov Jan 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved error check inside of the loop to add route id.

Its also a cleaner fix for #169 then #170 was.

}

// Converts a parsing route objects to the exported route definition with
Expand Down
7 changes: 6 additions & 1 deletion eskip/eskip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,12 @@ func TestParse(t *testing.T) {
"invalid method predicate",
`Path("/endpoint") && Method("GET", "POST") -> "https://www.example.org"`,
nil,
"invalid predicate count arg",
`invalid route "": Method predicate expects 1 string argument`,
}, {
"invalid header predicate",
`foo: Path("/endpoint") && Header("Foo") -> "https://www.example.org";`,
nil,
`invalid route "foo": Header predicate expects 2 string arguments`,
}, {
"host regexps",
`Host(/^www[.]/) && Host(/[.]org$/) -> "https://www.example.org"`,
Expand Down
Loading