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

validator: ensure no network backend path #2719

Merged
merged 7 commits into from
Nov 15, 2023
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
5 changes: 5 additions & 0 deletions cmd/webhook/admission/admission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,11 @@ func TestRouteGroupAdmitter(t *testing.T) {
inputFile: "rg-with-multiple-predicates.json",
message: `single predicate expected at \"Method(\\\"GET\\\") && Path(\\\"/\\\")\"\nsingle predicate expected at \" \"`,
},
{
name: "routegroup with invalid backends",
inputFile: "rg-with-invalid-backend-path.json",
message: `backend address \"http://example.com/foo\" contains path, query or missing scheme\nbackend address \"http://example.com/foo/bar\" contains path, query or missing scheme\nbackend address \"http://example.com/foo/\" contains path, query or missing scheme\nbackend address \"/foo\" contains path, query or missing scheme\nbackend address \"http://example.com/\" contains path, query or missing scheme\nbackend address \"example.com/\" contains path, query or missing scheme\nbackend address \"example.com/foo\" contains path, query or missing scheme\nbackend address \"http://example.com?foo=bar\" contains path, query or missing scheme\nbackend address \"example.com\" contains path, query or missing scheme`,
},
} {
t.Run(tc.name, func(t *testing.T) {
expectedResponse := responseAllowedFmt
Expand Down
103 changes: 103 additions & 0 deletions cmd/webhook/admission/testdata/rg/rg-with-invalid-backend-path.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
{
"request": {
"uid": "req-uid",
"name": "req1",
"operation": "create",
"kind": {
"group": "zalando",
"version": "v1",
"kind": "RouteGroup"
},
"namespace": "n1",
"object": {
"metadata": {
"name": "rg1",
"namespace": "n1"
},
"spec": {
"backends": [
{
"name": "valid-backend1",
"type": "shunt"
},
{
"name": "valid-backend3",
"type": "network",
"address": "http://example.com"
MustafaSaber marked this conversation as resolved.
Show resolved Hide resolved
},
{
"name": "invalid-backend2",
"type": "network",
"address": "http://example.com/foo"
},
{
"name": "invalid-backend3",
"type": "network",
"address": "http://example.com/foo/bar"
},
{
"name": "invalid-backend4",
"type": "network",
"address": "http://example.com/foo/"
},
{
"name": "invalid-backend5",
"type": "network",
"address": "/foo"
},
{
"name": "valid-backend2",
"type": "network",
"address": "http://user:[email protected]"
RomanZavodskikh marked this conversation as resolved.
Show resolved Hide resolved
},
{
"name": "invalid-backend6",
"type": "network",
"address": "http://example.com/"
},
{
"name": "invalid-backend7",
"type": "network",
"address": "example.com/"
},
{
"name" : "invalid-backend8",
"type" : "network",
"address" : "example.com/foo"
},
{
"name" : "invalid-backend9",
"type" : "network",
"address" : "http://example.com?foo=bar"
},
{
"name": "invalid-backend10",
"type": "network",
"address": "example.com"
}
],
"defaultBackends": [
{
"backendName": "valid-backend1"
}
],
"routes": [
{
"backends": [
{
"backendName": "valid-backend1"
}
],
"filters": [
"status(201)"
],
"path": "/",
"predicates": [
"Method(\"GET\")"
]
}
]
}
}
}
}
25 changes: 21 additions & 4 deletions dataclients/kubernetes/definitions/routegroupvalidator.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package definitions
import (
"errors"
"fmt"
"net/url"

"github.com/zalando/skipper/eskip"
)
Expand Down Expand Up @@ -35,8 +36,9 @@ func (rgv *RouteGroupValidator) Validate(item *RouteGroupItem) error {
return err
}
var errs []error
errs = append(errs, rgv.filtersValidation(item))
errs = append(errs, rgv.predicatesValidation(item))
errs = append(errs, rgv.validateFilters(item))
errs = append(errs, rgv.validatePredicates(item))
errs = append(errs, rgv.validateBackends(item))

return errorsJoin(errs...)
}
Expand All @@ -60,7 +62,7 @@ func (rgv *RouteGroupValidator) basicValidation(r *RouteGroupItem) error {
return nil
}

func (rgv *RouteGroupValidator) filtersValidation(item *RouteGroupItem) error {
func (rgv *RouteGroupValidator) validateFilters(item *RouteGroupItem) error {
var errs []error
for _, r := range item.Spec.Routes {
for _, f := range r.Filters {
Expand All @@ -76,7 +78,7 @@ func (rgv *RouteGroupValidator) filtersValidation(item *RouteGroupItem) error {
return errorsJoin(errs...)
}

func (rgv *RouteGroupValidator) predicatesValidation(item *RouteGroupItem) error {
func (rgv *RouteGroupValidator) validatePredicates(item *RouteGroupItem) error {
var errs []error
for _, r := range item.Spec.Routes {
for _, p := range r.Predicates {
Expand All @@ -91,6 +93,21 @@ func (rgv *RouteGroupValidator) predicatesValidation(item *RouteGroupItem) error
return errorsJoin(errs...)
}

func (rgv *RouteGroupValidator) validateBackends(item *RouteGroupItem) error {
var errs []error
for _, backend := range item.Spec.Backends {
if backend.Type == eskip.NetworkBackend {
address, err := url.Parse(backend.Address)
if err != nil {
errs = append(errs, fmt.Errorf("failed to parse backend address %q: %w", backend.Address, err))
} else if address.Path != "" || address.RawQuery != "" || address.Scheme == "" {
errs = append(errs, fmt.Errorf("backend address %q contains path, query or missing scheme", backend.Address))
}
}
}
return errorsJoin(errs...)
}

// TODO: we need to pass namespace/name in all errors
func (rg *RouteGroupSpec) validate() error {
// has at least one backend:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,10 @@ single predicate expected at "Path(\"/foo\") && Method(\"GET\")"
single predicate expected at ""
single filter expected at "inlineContent(\"/foo\") -> status(200)"
single filter expected at " "
backend address "http://example.com/foo" contains path, query or missing scheme
backend address "http://example.com/foo/bar" contains path, query or missing scheme
backend address "http://example.com/foo/" contains path, query or missing scheme
backend address "/foo" contains path, query or missing scheme
backend address "example.com/" contains path, query or missing scheme
backend address "http://example.com?foo=bar" contains path, query or missing scheme
backend address "example.com" contains path, query or missing scheme
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,82 @@
}
]
}
},
{
"apiVersion": "zalando.org/v1",
"metadata": {
"name": "rg1"
},
"kind": "RouteGroup",
"spec": {
"backends": [
{
"name": "backend1",
"type": "network",
"address": "http://example.com"
},
{
"name": "backend2",
"type": "network",
"address": "http://example.com/foo"
},
{
"name": "backend3",
"type": "network",
"address": "http://example.com/foo/bar"
},
{
"name": "backend4",
"type": "network",
"address": "http://example.com/foo/"
},
{
"name": "backend5",
"type": "network",
"address": "/foo"
},
{
"name": "backend6",
"type": "network",
"address": "example.com/"
},
{
"name": "backend7",
"type": "network",
"address": "http://user:[email protected]"
},
{
"name": "backend8",
"type": "network",
"address": "http://example.com?foo=bar"
},
{
"name": "backend9",
"type": "network",
"address": "example.com"
},
{
"name": "shunt",
"type": "shunt"
}
],
"hosts": [
"test.example.com"
],
"routes": [
{
"backends": [
{
"backendName": "shunt"
}
],
"filters": [
"inlineContent(\"/foo\")"
],
"path": "/foo"
}
]
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
backend address \\\"http://example\.com/foo\\\" contains path, query or missing scheme
backend address \\\"http://example\.com/foo/bar\\\" contains path, query or missing scheme
backend address \\\"/foo\\\" contains path, query or missing scheme
backend address \\\"/foo/bar\\\" contains path, query or missing scheme
backend address \\\"example\.com/foo\\\" contains path, query or missing scheme
backend address \\\"http://example\.com/\\\" contains path, query or missing scheme
backend address \\\"http://example\.com\?foo=bar\\\" contains path, query or missing scheme
backend address \\\"example\.com\\\" contains path, query or missing scheme
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
apiVersion: zalando.org/v1
kind: RouteGroup
metadata:
name: test-route-group
spec:
hosts:
- example.org
backends:
- name: app
type: service
serviceName: app-svc
servicePort: 80
- name: backend1
type: network
address: http://example.com
- name: backend2
type: network
address: http://example.com/foo
- name: backend3
type: network
address: http://example.com/foo/bar
- name: backend4
type: network
address: /foo
- name: backend5
type: network
address: /foo/bar
- name: backend6
type: network
address: example.com/foo
- name: backend7
type: network
address: http://example.com/
- name: backend8
type: network
address: http://user:[email protected]
- name: backend9
type: network
address: http://example.com?foo=bar
- name: backend10
type: network
address: example.com
defaultBackends:
- backendName: app
routes:
- path: /
methods:
- GET
- HEAD
predicates:
- Foo("X-Bar", "42")
filters:
- foo(42)
- bar(24)
backends:
- backendName: app
Original file line number Diff line number Diff line change
@@ -1 +1 @@
single filter expected
single filter expected at \\\"foo\(42\) -> bar\(24\)\\\"
Copy link
Member Author

Choose a reason for hiding this comment

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

Not related to the change but makes this test case more robust.

Original file line number Diff line number Diff line change
@@ -1 +1 @@
single predicate expected
single predicate expected at \\\"Foo\(\\\\\\"X-Bar\\\\\\", \\\\\\"42\\\\\\"\) && Bar\(\\\\\\"X-Foo\\\\\\", \\\\\\"24\\\\\\"\)\\\"
Copy link
Member Author

Choose a reason for hiding this comment

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

Not related to the change but makes this test case more robust.