Skip to content

Commit

Permalink
Validate that network backends doesn't have paths
Browse files Browse the repository at this point in the history
Signed-off-by: Mustafa Abdelrahman <[email protected]>
  • Loading branch information
MustafaSaber committed Nov 7, 2023
1 parent 4d7b5fc commit f0483b1
Show file tree
Hide file tree
Showing 6 changed files with 118 additions and 1 deletion.
15 changes: 15 additions & 0 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 @@ -37,6 +38,7 @@ func (rgv *RouteGroupValidator) Validate(item *RouteGroupItem) error {
var errs []error
errs = append(errs, rgv.filtersValidation(item))
errs = append(errs, rgv.predicatesValidation(item))
errs = append(errs, rgv.validateBackends(item))

return errorsJoin(errs...)
}
Expand Down Expand Up @@ -91,6 +93,19 @@ 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 {
address, err := url.Parse(backend.Address)
if err != nil {
errs = append(errs, fmt.Errorf("failed to parse backend address %s: %w", backend.Address, err))
} else if address.Path != "" {
errs = append(errs, fmt.Errorf("backend address %s contains path", 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,7 @@ 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
backend address "http://example.com/foo/bar" contains path
backend address "http://example.com/foo/" contains path
backend address "/foo" contains path
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,62 @@
}
]
}
},
{
"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": "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 @@
backend address \"http://example.com/foo\" contains path\nbackend address \"http://example.com/foo/bar\" contains path\nbackend address \"/foo\" contains path\nbackend address \"/foo/bar\" contains path
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
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
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)"

0 comments on commit f0483b1

Please sign in to comment.